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