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