1/*
2 * Copyright 2008 Intel Corporation <hong.liu@intel.com>
3 * Copyright 2008 Red Hat <mjg@redhat.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NON-INFRINGEMENT.  IN NO EVENT SHALL INTEL AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 */
27
28#include <sys/cdefs.h>
29__FBSDID("$FreeBSD$");
30
31#include <dev/drm2/drmP.h>
32#include <dev/drm2/i915/i915_drm.h>
33#include <dev/drm2/i915/i915_drv.h>
34#include <dev/drm2/i915/intel_drv.h>
35#include <contrib/dev/acpica/include/acpi.h>
36#include <contrib/dev/acpica/include/accommon.h>
37#include <dev/acpica/acpivar.h>
38
39#define PCI_ASLE 0xe4
40#define PCI_ASLS 0xfc
41
42#define OPREGION_HEADER_OFFSET 0
43#define OPREGION_ACPI_OFFSET   0x100
44#define   ACPI_CLID 0x01ac /* current lid state indicator */
45#define   ACPI_CDCK 0x01b0 /* current docking state indicator */
46#define OPREGION_SWSCI_OFFSET  0x200
47#define OPREGION_ASLE_OFFSET   0x300
48#define OPREGION_VBT_OFFSET    0x400
49
50#define OPREGION_SIGNATURE "IntelGraphicsMem"
51#define MBOX_ACPI      (1<<0)
52#define MBOX_SWSCI     (1<<1)
53#define MBOX_ASLE      (1<<2)
54
55struct opregion_header {
56	u8 signature[16];
57	u32 size;
58	u32 opregion_ver;
59	u8 bios_ver[32];
60	u8 vbios_ver[16];
61	u8 driver_ver[16];
62	u32 mboxes;
63	u8 reserved[164];
64} __attribute__((packed));
65
66/* OpRegion mailbox #1: public ACPI methods */
67struct opregion_acpi {
68	u32 drdy;       /* driver readiness */
69	u32 csts;       /* notification status */
70	u32 cevt;       /* current event */
71	u8 rsvd1[20];
72	u32 didl[8];    /* supported display devices ID list */
73	u32 cpdl[8];    /* currently presented display list */
74	u32 cadl[8];    /* currently active display list */
75	u32 nadl[8];    /* next active devices list */
76	u32 aslp;       /* ASL sleep time-out */
77	u32 tidx;       /* toggle table index */
78	u32 chpd;       /* current hotplug enable indicator */
79	u32 clid;       /* current lid state*/
80	u32 cdck;       /* current docking state */
81	u32 sxsw;       /* Sx state resume */
82	u32 evts;       /* ASL supported events */
83	u32 cnot;       /* current OS notification */
84	u32 nrdy;       /* driver status */
85	u8 rsvd2[60];
86} __attribute__((packed));
87
88/* OpRegion mailbox #2: SWSCI */
89struct opregion_swsci {
90	u32 scic;       /* SWSCI command|status|data */
91	u32 parm;       /* command parameters */
92	u32 dslp;       /* driver sleep time-out */
93	u8 rsvd[244];
94} __attribute__((packed));
95
96/* OpRegion mailbox #3: ASLE */
97struct opregion_asle {
98	u32 ardy;       /* driver readiness */
99	u32 aslc;       /* ASLE interrupt command */
100	u32 tche;       /* technology enabled indicator */
101	u32 alsi;       /* current ALS illuminance reading */
102	u32 bclp;       /* backlight brightness to set */
103	u32 pfit;       /* panel fitting state */
104	u32 cblv;       /* current brightness level */
105	u16 bclm[20];   /* backlight level duty cycle mapping table */
106	u32 cpfm;       /* current panel fitting mode */
107	u32 epfm;       /* enabled panel fitting modes */
108	u8 plut[74];    /* panel LUT and identifier */
109	u32 pfmb;       /* PWM freq and min brightness */
110	u8 rsvd[102];
111} __attribute__((packed));
112
113/* ASLE irq request bits */
114#define ASLE_SET_ALS_ILLUM     (1 << 0)
115#define ASLE_SET_BACKLIGHT     (1 << 1)
116#define ASLE_SET_PFIT          (1 << 2)
117#define ASLE_SET_PWM_FREQ      (1 << 3)
118#define ASLE_REQ_MSK           0xf
119
120/* response bits of ASLE irq request */
121#define ASLE_ALS_ILLUM_FAILED	(1<<10)
122#define ASLE_BACKLIGHT_FAILED	(1<<12)
123#define ASLE_PFIT_FAILED	(1<<14)
124#define ASLE_PWM_FREQ_FAILED	(1<<16)
125
126/* ASLE backlight brightness to set */
127#define ASLE_BCLP_VALID                (1<<31)
128#define ASLE_BCLP_MSK          (~(1<<31))
129
130/* ASLE panel fitting request */
131#define ASLE_PFIT_VALID         (1<<31)
132#define ASLE_PFIT_CENTER (1<<0)
133#define ASLE_PFIT_STRETCH_TEXT (1<<1)
134#define ASLE_PFIT_STRETCH_GFX (1<<2)
135
136/* PWM frequency and minimum brightness */
137#define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
138#define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
139#define ASLE_PFMB_PWM_MASK (0x7ffffe00)
140#define ASLE_PFMB_PWM_VALID (1<<31)
141
142#define ASLE_CBLV_VALID         (1<<31)
143
144#define ACPI_OTHER_OUTPUT (0<<8)
145#define ACPI_VGA_OUTPUT (1<<8)
146#define ACPI_TV_OUTPUT (2<<8)
147#define ACPI_DIGITAL_OUTPUT (3<<8)
148#define ACPI_LVDS_OUTPUT (4<<8)
149
150#if defined(CONFIG_ACPI)
151static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
152{
153	struct drm_i915_private *dev_priv = dev->dev_private;
154	struct opregion_asle *asle = dev_priv->opregion.asle;
155	u32 max;
156
157	if (!(bclp & ASLE_BCLP_VALID))
158		return ASLE_BACKLIGHT_FAILED;
159
160	bclp &= ASLE_BCLP_MSK;
161	if (bclp > 255)
162		return ASLE_BACKLIGHT_FAILED;
163
164	max = intel_panel_get_max_backlight(dev);
165	intel_panel_set_backlight(dev, bclp * max / 255);
166	asle->cblv = (bclp*0x64)/0xff | ASLE_CBLV_VALID;
167
168	return 0;
169}
170
171static u32 asle_set_als_illum(struct drm_device *dev, u32 alsi)
172{
173	/* alsi is the current ALS reading in lux. 0 indicates below sensor
174	   range, 0xffff indicates above sensor range. 1-0xfffe are valid */
175	return 0;
176}
177
178static u32 asle_set_pwm_freq(struct drm_device *dev, u32 pfmb)
179{
180	struct drm_i915_private *dev_priv = dev->dev_private;
181	if (pfmb & ASLE_PFMB_PWM_VALID) {
182		u32 blc_pwm_ctl = I915_READ(BLC_PWM_CTL);
183		u32 pwm = pfmb & ASLE_PFMB_PWM_MASK;
184		blc_pwm_ctl &= BACKLIGHT_DUTY_CYCLE_MASK;
185		pwm = pwm >> 9;
186		/* FIXME - what do we do with the PWM? */
187	}
188	return 0;
189}
190
191static u32 asle_set_pfit(struct drm_device *dev, u32 pfit)
192{
193	/* Panel fitting is currently controlled by the X code, so this is a
194	   noop until modesetting support works fully */
195	if (!(pfit & ASLE_PFIT_VALID))
196		return ASLE_PFIT_FAILED;
197	return 0;
198}
199
200void intel_opregion_asle_intr(struct drm_device *dev)
201{
202	struct drm_i915_private *dev_priv = dev->dev_private;
203	struct opregion_asle *asle = dev_priv->opregion.asle;
204	u32 asle_stat = 0;
205	u32 asle_req;
206
207	if (!asle)
208		return;
209
210	asle_req = asle->aslc & ASLE_REQ_MSK;
211
212	if (!asle_req) {
213		DRM_DEBUG("non asle set request??\n");
214		return;
215	}
216
217	if (asle_req & ASLE_SET_ALS_ILLUM)
218		asle_stat |= asle_set_als_illum(dev, asle->alsi);
219
220	if (asle_req & ASLE_SET_BACKLIGHT)
221		asle_stat |= asle_set_backlight(dev, asle->bclp);
222
223	if (asle_req & ASLE_SET_PFIT)
224		asle_stat |= asle_set_pfit(dev, asle->pfit);
225
226	if (asle_req & ASLE_SET_PWM_FREQ)
227		asle_stat |= asle_set_pwm_freq(dev, asle->pfmb);
228
229	asle->aslc = asle_stat;
230}
231
232void intel_opregion_gse_intr(struct drm_device *dev)
233{
234	struct drm_i915_private *dev_priv = dev->dev_private;
235	struct opregion_asle *asle = dev_priv->opregion.asle;
236	u32 asle_stat = 0;
237	u32 asle_req;
238
239	if (!asle)
240		return;
241
242	asle_req = asle->aslc & ASLE_REQ_MSK;
243
244	if (!asle_req) {
245		DRM_DEBUG("non asle set request??\n");
246		return;
247	}
248
249	if (asle_req & ASLE_SET_ALS_ILLUM) {
250		DRM_DEBUG("Illum is not supported\n");
251		asle_stat |= ASLE_ALS_ILLUM_FAILED;
252	}
253
254	if (asle_req & ASLE_SET_BACKLIGHT)
255		asle_stat |= asle_set_backlight(dev, asle->bclp);
256
257	if (asle_req & ASLE_SET_PFIT) {
258		DRM_DEBUG("Pfit is not supported\n");
259		asle_stat |= ASLE_PFIT_FAILED;
260	}
261
262	if (asle_req & ASLE_SET_PWM_FREQ) {
263		DRM_DEBUG("PWM freq is not supported\n");
264		asle_stat |= ASLE_PWM_FREQ_FAILED;
265	}
266
267	asle->aslc = asle_stat;
268}
269#define ASLE_ALS_EN    (1<<0)
270#define ASLE_BLC_EN    (1<<1)
271#define ASLE_PFIT_EN   (1<<2)
272#define ASLE_PFMB_EN   (1<<3)
273
274void intel_opregion_enable_asle(struct drm_device *dev)
275{
276	struct drm_i915_private *dev_priv = dev->dev_private;
277	struct opregion_asle *asle = dev_priv->opregion.asle;
278
279	if (asle) {
280		if (IS_MOBILE(dev))
281			intel_enable_asle(dev);
282
283		asle->tche = ASLE_ALS_EN | ASLE_BLC_EN | ASLE_PFIT_EN |
284			ASLE_PFMB_EN;
285		asle->ardy = 1;
286	}
287}
288
289#define ACPI_EV_DISPLAY_SWITCH (1<<0)
290#define ACPI_EV_LID            (1<<1)
291#define ACPI_EV_DOCK           (1<<2)
292
293static struct intel_opregion *system_opregion;
294
295#if 0
296static int intel_opregion_video_event(struct notifier_block *nb,
297				      unsigned long val, void *data)
298{
299	/* The only video events relevant to opregion are 0x80. These indicate
300	   either a docking event, lid switch or display switch request. In
301	   Linux, these are handled by the dock, button and video drivers.
302	*/
303	struct opregion_acpi *acpi;
304	struct acpi_bus_event *event = data;
305	int ret = NOTIFY_OK;
306
307	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
308		return NOTIFY_DONE;
309
310	if (!system_opregion)
311		return NOTIFY_DONE;
312
313	acpi = system_opregion->acpi;
314
315	if (event->type == 0x80 && !(acpi->cevt & 0x1))
316		ret = NOTIFY_BAD;
317
318	acpi->csts = 0;
319
320	return ret;
321}
322
323static struct notifier_block intel_opregion_notifier = {
324	.notifier_call = intel_opregion_video_event,
325};
326#endif
327
328/*
329 * Initialise the DIDL field in opregion. This passes a list of devices to
330 * the firmware. Values are defined by section B.4.2 of the ACPI specification
331 * (version 3)
332 */
333
334static int acpi_is_video_device(ACPI_HANDLE devh) {
335	ACPI_HANDLE h;
336	if (ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
337		ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h))) {
338		return 0;
339	}
340	return 1;
341}
342
343static void intel_didl_outputs(struct drm_device *dev)
344{
345	struct drm_i915_private *dev_priv = dev->dev_private;
346	struct intel_opregion *opregion = &dev_priv->opregion;
347	struct drm_connector *connector;
348	u32 device_id;
349	ACPI_HANDLE handle, acpi_video_bus, acpi_cdev;
350	ACPI_STATUS status;
351	int i = 0;
352
353	handle = acpi_get_handle(dev->dev);
354	if (!handle)
355		return;
356
357	if (acpi_is_video_device(handle))
358		acpi_video_bus = handle;
359	else {
360		acpi_cdev = NULL;
361		acpi_video_bus = NULL;
362		while (AcpiGetNextObject(ACPI_TYPE_DEVICE, handle, acpi_cdev,
363					&acpi_cdev) != AE_NOT_FOUND) {
364			if (acpi_is_video_device(acpi_cdev)) {
365				acpi_video_bus = acpi_cdev;
366				break;
367			}
368		}
369#if 0
370		list_for_each_entry(acpi_cdev, &acpi_dev->children, node) {
371			if (acpi_is_video_device(acpi_cdev)) {
372				acpi_video_bus = acpi_cdev;
373				break;
374			}
375		}
376#endif
377	}
378
379	if (!acpi_video_bus) {
380		device_printf(dev->dev, "No ACPI video bus found\n");
381		return;
382	}
383
384	acpi_cdev = NULL;
385	while (AcpiGetNextObject(ACPI_TYPE_DEVICE, acpi_video_bus, acpi_cdev,
386				&acpi_cdev) != AE_NOT_FOUND) {
387		if (i >= 8) {
388			device_printf(dev->dev, "More than 8 outputs detected\n");
389			return;
390		}
391		status = acpi_GetInteger(acpi_cdev, "_ADR", &device_id);
392		if (ACPI_SUCCESS(status)) {
393			if (!device_id)
394				goto blind_set;
395			opregion->acpi->didl[i] = (u32)(device_id & 0x0f0f);
396			i++;
397		}
398	}
399#if 0
400	list_for_each_entry(acpi_cdev, &acpi_video_bus->children, node) {
401		if (i >= 8) {
402			dev_printk(KERN_ERR, &dev->pdev->dev,
403				    "More than 8 outputs detected\n");
404			return;
405		}
406		status =
407			acpi_evaluate_integer(acpi_cdev->handle, "_ADR",
408						NULL, &device_id);
409		if (ACPI_SUCCESS(status)) {
410			if (!device_id)
411				goto blind_set;
412			opregion->acpi->didl[i] = (u32)(device_id & 0x0f0f);
413			i++;
414		}
415	}
416#endif
417
418end:
419	/* If fewer than 8 outputs, the list must be null terminated */
420	if (i < 8)
421		opregion->acpi->didl[i] = 0;
422	return;
423
424blind_set:
425	i = 0;
426	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
427		int output_type = ACPI_OTHER_OUTPUT;
428		if (i >= 8) {
429			device_printf(dev->dev,
430				    "More than 8 outputs detected\n");
431			return;
432		}
433		switch (connector->connector_type) {
434		case DRM_MODE_CONNECTOR_VGA:
435		case DRM_MODE_CONNECTOR_DVIA:
436			output_type = ACPI_VGA_OUTPUT;
437			break;
438		case DRM_MODE_CONNECTOR_Composite:
439		case DRM_MODE_CONNECTOR_SVIDEO:
440		case DRM_MODE_CONNECTOR_Component:
441		case DRM_MODE_CONNECTOR_9PinDIN:
442			output_type = ACPI_TV_OUTPUT;
443			break;
444		case DRM_MODE_CONNECTOR_DVII:
445		case DRM_MODE_CONNECTOR_DVID:
446		case DRM_MODE_CONNECTOR_DisplayPort:
447		case DRM_MODE_CONNECTOR_HDMIA:
448		case DRM_MODE_CONNECTOR_HDMIB:
449			output_type = ACPI_DIGITAL_OUTPUT;
450			break;
451		case DRM_MODE_CONNECTOR_LVDS:
452			output_type = ACPI_LVDS_OUTPUT;
453			break;
454		}
455		opregion->acpi->didl[i] |= (1<<31) | output_type | i;
456		i++;
457	}
458	goto end;
459}
460
461static void intel_setup_cadls(struct drm_device *dev)
462{
463	struct drm_i915_private *dev_priv = dev->dev_private;
464	struct intel_opregion *opregion = &dev_priv->opregion;
465	int i = 0;
466	u32 disp_id;
467
468	/* Initialize the CADL field by duplicating the DIDL values.
469	 * Technically, this is not always correct as display outputs may exist,
470	 * but not active. This initialization is necessary for some Clevo
471	 * laptops that check this field before processing the brightness and
472	 * display switching hotkeys. Just like DIDL, CADL is NULL-terminated if
473	 * there are less than eight devices. */
474	do {
475		disp_id = opregion->acpi->didl[i];
476		opregion->acpi->cadl[i] = disp_id;
477	} while (++i < 8 && disp_id != 0);
478}
479
480void intel_opregion_init(struct drm_device *dev)
481{
482	struct drm_i915_private *dev_priv = dev->dev_private;
483	struct intel_opregion *opregion = &dev_priv->opregion;
484
485	if (!opregion->header)
486		return;
487
488	if (opregion->acpi) {
489		if (drm_core_check_feature(dev, DRIVER_MODESET)) {
490			intel_didl_outputs(dev);
491			intel_setup_cadls(dev);
492		}
493
494		/* Notify BIOS we are ready to handle ACPI video ext notifs.
495		 * Right now, all the events are handled by the ACPI video module.
496		 * We don't actually need to do anything with them. */
497		opregion->acpi->csts = 0;
498		opregion->acpi->drdy = 1;
499
500		system_opregion = opregion;
501#if 0
502		register_acpi_notifier(&intel_opregion_notifier);
503#endif
504	}
505
506	if (opregion->asle)
507		intel_opregion_enable_asle(dev);
508}
509
510void intel_opregion_fini(struct drm_device *dev)
511{
512	struct drm_i915_private *dev_priv = dev->dev_private;
513	struct intel_opregion *opregion = &dev_priv->opregion;
514
515	if (!opregion->header)
516		return;
517
518	if (opregion->acpi) {
519		opregion->acpi->drdy = 0;
520
521		system_opregion = NULL;
522#if 0
523		unregister_acpi_notifier(&intel_opregion_notifier);
524#endif
525	}
526
527	/* just clear all opregion memory pointers now */
528	pmap_unmapdev((vm_offset_t)opregion->header, OPREGION_SIZE);
529	opregion->header = NULL;
530	opregion->acpi = NULL;
531	opregion->swsci = NULL;
532	opregion->asle = NULL;
533	opregion->vbt = NULL;
534}
535#else
536void
537intel_opregion_init(struct drm_device *dev)
538{
539}
540
541void
542intel_opregion_fini(struct drm_device *dev)
543{
544	struct drm_i915_private *dev_priv;
545	struct intel_opregion *opregion;
546
547	dev_priv = dev->dev_private;
548	opregion = &dev_priv->opregion;
549
550	if (opregion->header == NULL)
551		return;
552
553	pmap_unmapdev((vm_offset_t)opregion->header, OPREGION_SIZE);
554	opregion->header = NULL;
555	opregion->acpi = NULL;
556	opregion->swsci = NULL;
557	opregion->asle = NULL;
558	opregion->vbt = NULL;
559}
560#endif
561
562int intel_opregion_setup(struct drm_device *dev)
563{
564	struct drm_i915_private *dev_priv = dev->dev_private;
565	struct intel_opregion *opregion = &dev_priv->opregion;
566	char *base;
567	u32 asls, mboxes;
568	int err = 0;
569
570	asls = pci_read_config(dev->dev, PCI_ASLS, 4);
571	DRM_DEBUG("graphic opregion physical addr: 0x%x\n", asls);
572	if (asls == 0) {
573		DRM_DEBUG("ACPI OpRegion not supported!\n");
574		return -ENOTSUP;
575	}
576
577	base = (void *)pmap_mapbios(asls, OPREGION_SIZE);
578	if (!base)
579		return -ENOMEM;
580
581	if (memcmp(base, OPREGION_SIGNATURE, 16)) {
582		DRM_DEBUG("opregion signature mismatch\n");
583		err = -EINVAL;
584		goto err_out;
585	}
586	opregion->header = (struct opregion_header *)base;
587	opregion->vbt = base + OPREGION_VBT_OFFSET;
588
589	opregion->lid_state = (u32 *)(base + ACPI_CLID);
590
591	mboxes = opregion->header->mboxes;
592	if (mboxes & MBOX_ACPI) {
593		DRM_DEBUG("Public ACPI methods supported\n");
594		opregion->acpi = (struct opregion_acpi *)(base +
595		    OPREGION_ACPI_OFFSET);
596	}
597
598	if (mboxes & MBOX_SWSCI) {
599		DRM_DEBUG("SWSCI supported\n");
600		opregion->swsci = (struct opregion_swsci *)(base +
601		    OPREGION_SWSCI_OFFSET);
602	}
603	if (mboxes & MBOX_ASLE) {
604		DRM_DEBUG("ASLE supported\n");
605		opregion->asle = (struct opregion_asle *)(base +
606		    OPREGION_ASLE_OFFSET);
607	}
608
609	return 0;
610
611err_out:
612	pmap_unmapdev((vm_offset_t)base, OPREGION_SIZE);
613	return err;
614}
615