2 * Copyright 2008 Intel Corporation <hong.liu@intel.com>
3 * Copyright 2008 Red Hat <mjg@redhat.com>
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:
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
38 #include <contrib/dev/acpica/include/acpi.h>
39 #include <contrib/dev/acpica/include/accommon.h>
40 #include <dev/acpica/acpivar.h>
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
53 #define OPREGION_SIGNATURE "IntelGraphicsMem"
54 #define MBOX_ACPI (1<<0)
55 #define MBOX_SWSCI (1<<1)
56 #define MBOX_ASLE (1<<2)
58 struct opregion_header {
67 } __attribute__((packed));
69 /* OpRegion mailbox #1: public ACPI methods */
70 struct opregion_acpi {
71 u32 drdy; /* driver readiness */
72 u32 csts; /* notification status */
73 u32 cevt; /* current event */
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 */
89 } __attribute__((packed));
91 /* OpRegion mailbox #2: SWSCI */
92 struct opregion_swsci {
93 u32 scic; /* SWSCI command|status|data */
94 u32 parm; /* command parameters */
95 u32 dslp; /* driver sleep time-out */
97 } __attribute__((packed));
99 /* OpRegion mailbox #3: ASLE */
100 struct 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 */
114 } __attribute__((packed));
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
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)
129 /* ASLE backlight brightness to set */
130 #define ASLE_BCLP_VALID (1<<31)
131 #define ASLE_BCLP_MSK (~(1<<31))
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)
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)
145 #define ASLE_CBLV_VALID (1<<31)
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)
154 static u32 asle_set_backlight(struct drm_device *dev, u32 bclp)
156 struct drm_i915_private *dev_priv = dev->dev_private;
157 struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
160 DRM_DEBUG_DRIVER("bclp = 0x%08x\n", bclp);
162 if (!(bclp & ASLE_BCLP_VALID))
163 return ASLE_BACKLIGHT_FAILED;
165 bclp &= ASLE_BCLP_MSK;
167 return ASLE_BACKLIGHT_FAILED;
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);
176 static u32 asle_set_als_illum(struct drm_device *dev, u32 alsi)
178 /* alsi is the current ALS reading in lux. 0 indicates below sensor
179 range, 0xffff indicates above sensor range. 1-0xfffe are valid */
183 static u32 asle_set_pwm_freq(struct drm_device *dev, u32 pfmb)
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;
191 /* FIXME - what do we do with the PWM? */
196 static u32 asle_set_pfit(struct drm_device *dev, u32 pfit)
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;
205 void intel_opregion_asle_intr(struct drm_device *dev)
207 struct drm_i915_private *dev_priv = dev->dev_private;
208 struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
215 asle_req = ioread32(&asle->aslc) & ASLE_REQ_MSK;
218 DRM_DEBUG_DRIVER("non asle set request??\n");
222 if (asle_req & ASLE_SET_ALS_ILLUM)
223 asle_stat |= asle_set_als_illum(dev, ioread32(&asle->alsi));
225 if (asle_req & ASLE_SET_BACKLIGHT)
226 asle_stat |= asle_set_backlight(dev, ioread32(&asle->bclp));
228 if (asle_req & ASLE_SET_PFIT)
229 asle_stat |= asle_set_pfit(dev, ioread32(&asle->pfit));
231 if (asle_req & ASLE_SET_PWM_FREQ)
232 asle_stat |= asle_set_pwm_freq(dev, ioread32(&asle->pfmb));
234 iowrite32(asle_stat, &asle->aslc);
237 void intel_opregion_gse_intr(struct drm_device *dev)
239 struct drm_i915_private *dev_priv = dev->dev_private;
240 struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
247 asle_req = ioread32(&asle->aslc) & ASLE_REQ_MSK;
250 DRM_DEBUG_DRIVER("non asle set request??\n");
254 if (asle_req & ASLE_SET_ALS_ILLUM) {
255 DRM_DEBUG_DRIVER("Illum is not supported\n");
256 asle_stat |= ASLE_ALS_ILLUM_FAILED;
259 if (asle_req & ASLE_SET_BACKLIGHT)
260 asle_stat |= asle_set_backlight(dev, ioread32(&asle->bclp));
262 if (asle_req & ASLE_SET_PFIT) {
263 DRM_DEBUG_DRIVER("Pfit is not supported\n");
264 asle_stat |= ASLE_PFIT_FAILED;
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;
272 iowrite32(asle_stat, &asle->aslc);
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)
279 void intel_opregion_enable_asle(struct drm_device *dev)
281 struct drm_i915_private *dev_priv = dev->dev_private;
282 struct opregion_asle __iomem *asle = dev_priv->opregion.asle;
286 intel_enable_asle(dev);
288 iowrite32(ASLE_ALS_EN | ASLE_BLC_EN | ASLE_PFIT_EN |
291 iowrite32(1, &asle->ardy);
295 #define ACPI_EV_DISPLAY_SWITCH (1<<0)
296 #define ACPI_EV_LID (1<<1)
297 #define ACPI_EV_DOCK (1<<2)
299 static struct intel_opregion *system_opregion;
302 static int intel_opregion_video_event(struct notifier_block *nb,
303 unsigned long val, void *data)
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.
310 struct opregion_acpi __iomem *acpi;
311 struct acpi_bus_event *event = data;
314 if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
317 if (!system_opregion)
320 acpi = system_opregion->acpi;
322 if (event->type == 0x80 &&
323 (ioread32(&acpi->cevt) & 1) == 0)
326 iowrite32(0, &acpi->csts);
331 static struct notifier_block intel_opregion_notifier = {
332 .notifier_call = intel_opregion_video_event,
334 #endif /* FREEBSD_WIP */
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
342 static int acpi_is_video_device(ACPI_HANDLE devh) {
344 if (ACPI_FAILURE(AcpiGetHandle(devh, "_DOD", &h)) ||
345 ACPI_FAILURE(AcpiGetHandle(devh, "_DOS", &h))) {
351 static void intel_didl_outputs(struct drm_device *dev)
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;
362 handle = acpi_get_handle(dev->dev);
366 if (acpi_is_video_device(handle))
367 acpi_video_bus = handle;
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;
379 if (!acpi_video_bus) {
380 device_printf(dev->dev, "No ACPI video bus found\n");
385 while (AcpiGetNextObject(ACPI_TYPE_DEVICE, acpi_video_bus, acpi_cdev,
386 &acpi_cdev) != AE_NOT_FOUND) {
388 device_printf(dev->dev, "More than 8 outputs detected\n");
392 acpi_GetInteger(acpi_cdev, "_ADR",
394 if (ACPI_SUCCESS(status)) {
397 iowrite32((u32)(device_id & 0x0f0f),
398 &opregion->acpi->didl[i]);
404 /* If fewer than 8 outputs, the list must be null terminated */
406 iowrite32(0, &opregion->acpi->didl[i]);
411 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
412 int output_type = ACPI_OTHER_OUTPUT;
414 device_printf(dev->dev,
415 "More than 8 outputs detected\n");
418 switch (connector->connector_type) {
419 case DRM_MODE_CONNECTOR_VGA:
420 case DRM_MODE_CONNECTOR_DVIA:
421 output_type = ACPI_VGA_OUTPUT;
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;
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;
436 case DRM_MODE_CONNECTOR_LVDS:
437 output_type = ACPI_LVDS_OUTPUT;
440 temp = ioread32(&opregion->acpi->didl[i]);
441 iowrite32(temp | (1<<31) | output_type | i,
442 &opregion->acpi->didl[i]);
448 static void intel_setup_cadls(struct drm_device *dev)
450 struct drm_i915_private *dev_priv = dev->dev_private;
451 struct intel_opregion *opregion = &dev_priv->opregion;
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. */
462 disp_id = ioread32(&opregion->acpi->didl[i]);
463 iowrite32(disp_id, &opregion->acpi->cadl[i]);
464 } while (++i < 8 && disp_id != 0);
467 void intel_opregion_init(struct drm_device *dev)
469 struct drm_i915_private *dev_priv = dev->dev_private;
470 struct intel_opregion *opregion = &dev_priv->opregion;
472 if (!opregion->header)
475 if (opregion->acpi) {
476 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
477 intel_didl_outputs(dev);
478 intel_setup_cadls(dev);
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);
487 system_opregion = opregion;
489 register_acpi_notifier(&intel_opregion_notifier);
490 #endif /* FREEBSD_WIP */
494 intel_opregion_enable_asle(dev);
497 void intel_opregion_fini(struct drm_device *dev)
499 struct drm_i915_private *dev_priv = dev->dev_private;
500 struct intel_opregion *opregion = &dev_priv->opregion;
502 if (!opregion->header)
505 if (opregion->acpi) {
506 iowrite32(0, &opregion->acpi->drdy);
508 system_opregion = NULL;
510 unregister_acpi_notifier(&intel_opregion_notifier);
511 #endif /* FREEBSD_WIP */
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;
524 int intel_opregion_setup(struct drm_device *dev)
526 struct drm_i915_private *dev_priv = dev->dev_private;
527 struct intel_opregion *opregion = &dev_priv->opregion;
530 char buf[sizeof(OPREGION_SIGNATURE)];
533 pci_read_config_dword(dev->dev, PCI_ASLS, &asls);
534 DRM_DEBUG_DRIVER("graphic opregion physical addr: 0x%x\n", asls);
536 DRM_DEBUG_DRIVER("ACPI OpRegion not supported!\n");
540 base = (void *)pmap_mapbios(asls, OPREGION_SIZE);
544 memcpy_fromio(buf, base, sizeof(buf));
546 if (memcmp(buf, OPREGION_SIGNATURE, 16)) {
547 DRM_DEBUG_DRIVER("opregion signature mismatch\n");
551 opregion->header = (struct opregion_header *)base;
552 opregion->vbt = (char *)base + OPREGION_VBT_OFFSET;
554 opregion->lid_state = (u32 *)((char *)base + ACPI_CLID);
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);
563 if (mboxes & MBOX_SWSCI) {
564 DRM_DEBUG_DRIVER("SWSCI supported\n");
565 opregion->swsci = (struct opregion_swsci *)((char *)base +
566 OPREGION_SWSCI_OFFSET);
568 if (mboxes & MBOX_ASLE) {
569 DRM_DEBUG_DRIVER("ASLE supported\n");
570 opregion->asle = (struct opregion_asle *)((char *)base +
571 OPREGION_ASLE_OFFSET);
577 pmap_unmapdev((vm_offset_t)base, OPREGION_SIZE);