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