xref: /dflybsd-src/sys/dev/drm/i915/intel_opregion.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright 2008 Intel Corporation <hong.liu@intel.com>
3e3adcf8fSFrançois Tigeot  * Copyright 2008 Red Hat <mjg@redhat.com>
4e3adcf8fSFrançois Tigeot  *
5e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining
6e3adcf8fSFrançois Tigeot  * a copy of this software and associated documentation files (the
7e3adcf8fSFrançois Tigeot  * "Software"), to deal in the Software without restriction, including
8e3adcf8fSFrançois Tigeot  * without limitation the rights to use, copy, modify, merge, publish,
9e3adcf8fSFrançois Tigeot  * distribute, sub license, and/or sell copies of the Software, and to
10e3adcf8fSFrançois Tigeot  * permit persons to whom the Software is furnished to do so, subject to
11e3adcf8fSFrançois Tigeot  * the following conditions:
12e3adcf8fSFrançois Tigeot  *
13e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the
14e3adcf8fSFrançois Tigeot  * next paragraph) shall be included in all copies or substantial
15e3adcf8fSFrançois Tigeot  * portions of the Software.
16e3adcf8fSFrançois Tigeot  *
17e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18e3adcf8fSFrançois Tigeot  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19e3adcf8fSFrançois Tigeot  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20e3adcf8fSFrançois Tigeot  * NON-INFRINGEMENT.  IN NO EVENT SHALL INTEL AND/OR ITS SUPPLIERS BE
21e3adcf8fSFrançois Tigeot  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22e3adcf8fSFrançois Tigeot  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23e3adcf8fSFrançois Tigeot  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24e3adcf8fSFrançois Tigeot  * SOFTWARE.
25e3adcf8fSFrançois Tigeot  *
26e3adcf8fSFrançois Tigeot  */
27e3adcf8fSFrançois Tigeot 
28*3f2dd94aSFrançois Tigeot #include <linux/acpi.h>
29aee94f86SFrançois Tigeot #include <linux/dmi.h>
30*3f2dd94aSFrançois Tigeot #include <linux/firmware.h>
31*3f2dd94aSFrançois Tigeot #include <acpi/video.h>
32aee94f86SFrançois Tigeot 
3318e26a6dSFrançois Tigeot #include <drm/drmP.h>
345c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
35e3adcf8fSFrançois Tigeot #include "i915_drv.h"
36e3adcf8fSFrançois Tigeot #include "intel_drv.h"
37a2fdbec6SFrançois Tigeot 
3890c722cdSMarkus Pfeiffer #include <contrib/dev/acpica/source/include/acpi.h>
3990c722cdSMarkus Pfeiffer #include <contrib/dev/acpica/source/include/accommon.h>
4090c722cdSMarkus Pfeiffer #include <dev/acpica/acpivar.h>
41e3adcf8fSFrançois Tigeot 
42e3adcf8fSFrançois Tigeot #define OPREGION_HEADER_OFFSET 0
43e3adcf8fSFrançois Tigeot #define OPREGION_ACPI_OFFSET   0x100
44e3adcf8fSFrançois Tigeot #define   ACPI_CLID 0x01ac /* current lid state indicator */
45e3adcf8fSFrançois Tigeot #define   ACPI_CDCK 0x01b0 /* current docking state indicator */
46e3adcf8fSFrançois Tigeot #define OPREGION_SWSCI_OFFSET  0x200
47e3adcf8fSFrançois Tigeot #define OPREGION_ASLE_OFFSET   0x300
48e3adcf8fSFrançois Tigeot #define OPREGION_VBT_OFFSET    0x400
49aee94f86SFrançois Tigeot #define OPREGION_ASLE_EXT_OFFSET	0x1C00
50e3adcf8fSFrançois Tigeot 
51e3adcf8fSFrançois Tigeot #define OPREGION_SIGNATURE "IntelGraphicsMem"
52e3adcf8fSFrançois Tigeot #define MBOX_ACPI      (1<<0)
53e3adcf8fSFrançois Tigeot #define MBOX_SWSCI     (1<<1)
54e3adcf8fSFrançois Tigeot #define MBOX_ASLE      (1<<2)
55a05eeebfSFrançois Tigeot #define MBOX_ASLE_EXT  (1<<4)
56e3adcf8fSFrançois Tigeot 
57e3adcf8fSFrançois Tigeot struct opregion_header {
58e3adcf8fSFrançois Tigeot 	u8 signature[16];
59e3adcf8fSFrançois Tigeot 	u32 size;
60e3adcf8fSFrançois Tigeot 	u32 opregion_ver;
61e3adcf8fSFrançois Tigeot 	u8 bios_ver[32];
62e3adcf8fSFrançois Tigeot 	u8 vbios_ver[16];
63e3adcf8fSFrançois Tigeot 	u8 driver_ver[16];
64e3adcf8fSFrançois Tigeot 	u32 mboxes;
65a05eeebfSFrançois Tigeot 	u32 driver_model;
66a05eeebfSFrançois Tigeot 	u32 pcon;
67a05eeebfSFrançois Tigeot 	u8 dver[32];
68a05eeebfSFrançois Tigeot 	u8 rsvd[124];
699edbd4a0SFrançois Tigeot } __packed;
70e3adcf8fSFrançois Tigeot 
71e3adcf8fSFrançois Tigeot /* OpRegion mailbox #1: public ACPI methods */
72e3adcf8fSFrançois Tigeot struct opregion_acpi {
73e3adcf8fSFrançois Tigeot 	u32 drdy;       /* driver readiness */
74e3adcf8fSFrançois Tigeot 	u32 csts;       /* notification status */
75e3adcf8fSFrançois Tigeot 	u32 cevt;       /* current event */
76e3adcf8fSFrançois Tigeot 	u8 rsvd1[20];
77e3adcf8fSFrançois Tigeot 	u32 didl[8];    /* supported display devices ID list */
78e3adcf8fSFrançois Tigeot 	u32 cpdl[8];    /* currently presented display list */
79e3adcf8fSFrançois Tigeot 	u32 cadl[8];    /* currently active display list */
80e3adcf8fSFrançois Tigeot 	u32 nadl[8];    /* next active devices list */
81e3adcf8fSFrançois Tigeot 	u32 aslp;       /* ASL sleep time-out */
82e3adcf8fSFrançois Tigeot 	u32 tidx;       /* toggle table index */
83e3adcf8fSFrançois Tigeot 	u32 chpd;       /* current hotplug enable indicator */
84e3adcf8fSFrançois Tigeot 	u32 clid;       /* current lid state*/
85e3adcf8fSFrançois Tigeot 	u32 cdck;       /* current docking state */
86e3adcf8fSFrançois Tigeot 	u32 sxsw;       /* Sx state resume */
87e3adcf8fSFrançois Tigeot 	u32 evts;       /* ASL supported events */
88e3adcf8fSFrançois Tigeot 	u32 cnot;       /* current OS notification */
89e3adcf8fSFrançois Tigeot 	u32 nrdy;       /* driver status */
90a05eeebfSFrançois Tigeot 	u32 did2[7];	/* extended supported display devices ID list */
91a05eeebfSFrançois Tigeot 	u32 cpd2[7];	/* extended attached display devices list */
92a05eeebfSFrançois Tigeot 	u8 rsvd2[4];
939edbd4a0SFrançois Tigeot } __packed;
94e3adcf8fSFrançois Tigeot 
95e3adcf8fSFrançois Tigeot /* OpRegion mailbox #2: SWSCI */
96e3adcf8fSFrançois Tigeot struct opregion_swsci {
97e3adcf8fSFrançois Tigeot 	u32 scic;       /* SWSCI command|status|data */
98e3adcf8fSFrançois Tigeot 	u32 parm;       /* command parameters */
99e3adcf8fSFrançois Tigeot 	u32 dslp;       /* driver sleep time-out */
100e3adcf8fSFrançois Tigeot 	u8 rsvd[244];
1019edbd4a0SFrançois Tigeot } __packed;
102e3adcf8fSFrançois Tigeot 
103e3adcf8fSFrançois Tigeot /* OpRegion mailbox #3: ASLE */
104e3adcf8fSFrançois Tigeot struct opregion_asle {
105e3adcf8fSFrançois Tigeot 	u32 ardy;       /* driver readiness */
106e3adcf8fSFrançois Tigeot 	u32 aslc;       /* ASLE interrupt command */
107e3adcf8fSFrançois Tigeot 	u32 tche;       /* technology enabled indicator */
108e3adcf8fSFrançois Tigeot 	u32 alsi;       /* current ALS illuminance reading */
109e3adcf8fSFrançois Tigeot 	u32 bclp;       /* backlight brightness to set */
110e3adcf8fSFrançois Tigeot 	u32 pfit;       /* panel fitting state */
111e3adcf8fSFrançois Tigeot 	u32 cblv;       /* current brightness level */
112e3adcf8fSFrançois Tigeot 	u16 bclm[20];   /* backlight level duty cycle mapping table */
113e3adcf8fSFrançois Tigeot 	u32 cpfm;       /* current panel fitting mode */
114e3adcf8fSFrançois Tigeot 	u32 epfm;       /* enabled panel fitting modes */
115e3adcf8fSFrançois Tigeot 	u8 plut[74];    /* panel LUT and identifier */
116e3adcf8fSFrançois Tigeot 	u32 pfmb;       /* PWM freq and min brightness */
1179edbd4a0SFrançois Tigeot 	u32 cddv;       /* color correction default values */
1189edbd4a0SFrançois Tigeot 	u32 pcft;       /* power conservation features */
1199edbd4a0SFrançois Tigeot 	u32 srot;       /* supported rotation angles */
1209edbd4a0SFrançois Tigeot 	u32 iuer;       /* IUER events */
121a05eeebfSFrançois Tigeot 	u64 fdss;
122a05eeebfSFrançois Tigeot 	u32 fdsp;
123a05eeebfSFrançois Tigeot 	u32 stat;
124aee94f86SFrançois Tigeot 	u64 rvda;	/* Physical address of raw vbt data */
125aee94f86SFrançois Tigeot 	u32 rvds;	/* Size of raw vbt data */
126aee94f86SFrançois Tigeot 	u8 rsvd[58];
127aee94f86SFrançois Tigeot } __packed;
128aee94f86SFrançois Tigeot 
129aee94f86SFrançois Tigeot /* OpRegion mailbox #5: ASLE ext */
130aee94f86SFrançois Tigeot struct opregion_asle_ext {
131aee94f86SFrançois Tigeot 	u32 phed;	/* Panel Header */
132aee94f86SFrançois Tigeot 	u8 bddc[256];	/* Panel EDID */
133aee94f86SFrançois Tigeot 	u8 rsvd[764];
1349edbd4a0SFrançois Tigeot } __packed;
135e3adcf8fSFrançois Tigeot 
1365d0b1887SFrançois Tigeot /* Driver readiness indicator */
1375d0b1887SFrançois Tigeot #define ASLE_ARDY_READY		(1 << 0)
1385d0b1887SFrançois Tigeot #define ASLE_ARDY_NOT_READY	(0 << 0)
1395d0b1887SFrançois Tigeot 
1409edbd4a0SFrançois Tigeot /* ASLE Interrupt Command (ASLC) bits */
1419edbd4a0SFrançois Tigeot #define ASLC_SET_ALS_ILLUM		(1 << 0)
1429edbd4a0SFrançois Tigeot #define ASLC_SET_BACKLIGHT		(1 << 1)
1439edbd4a0SFrançois Tigeot #define ASLC_SET_PFIT			(1 << 2)
1449edbd4a0SFrançois Tigeot #define ASLC_SET_PWM_FREQ		(1 << 3)
1459edbd4a0SFrançois Tigeot #define ASLC_SUPPORTED_ROTATION_ANGLES	(1 << 4)
1469edbd4a0SFrançois Tigeot #define ASLC_BUTTON_ARRAY		(1 << 5)
1479edbd4a0SFrançois Tigeot #define ASLC_CONVERTIBLE_INDICATOR	(1 << 6)
1489edbd4a0SFrançois Tigeot #define ASLC_DOCKING_INDICATOR		(1 << 7)
1499edbd4a0SFrançois Tigeot #define ASLC_ISCT_STATE_CHANGE		(1 << 8)
1509edbd4a0SFrançois Tigeot #define ASLC_REQ_MSK			0x1ff
1519edbd4a0SFrançois Tigeot /* response bits */
1529edbd4a0SFrançois Tigeot #define ASLC_ALS_ILLUM_FAILED		(1 << 10)
1539edbd4a0SFrançois Tigeot #define ASLC_BACKLIGHT_FAILED		(1 << 12)
1549edbd4a0SFrançois Tigeot #define ASLC_PFIT_FAILED		(1 << 14)
1559edbd4a0SFrançois Tigeot #define ASLC_PWM_FREQ_FAILED		(1 << 16)
1569edbd4a0SFrançois Tigeot #define ASLC_ROTATION_ANGLES_FAILED	(1 << 18)
1579edbd4a0SFrançois Tigeot #define ASLC_BUTTON_ARRAY_FAILED	(1 << 20)
1589edbd4a0SFrançois Tigeot #define ASLC_CONVERTIBLE_FAILED		(1 << 22)
1599edbd4a0SFrançois Tigeot #define ASLC_DOCKING_FAILED		(1 << 24)
1609edbd4a0SFrançois Tigeot #define ASLC_ISCT_STATE_FAILED		(1 << 26)
161e3adcf8fSFrançois Tigeot 
1625d0b1887SFrançois Tigeot /* Technology enabled indicator */
1635d0b1887SFrançois Tigeot #define ASLE_TCHE_ALS_EN	(1 << 0)
1645d0b1887SFrançois Tigeot #define ASLE_TCHE_BLC_EN	(1 << 1)
1655d0b1887SFrançois Tigeot #define ASLE_TCHE_PFIT_EN	(1 << 2)
1665d0b1887SFrançois Tigeot #define ASLE_TCHE_PFMB_EN	(1 << 3)
1675d0b1887SFrançois Tigeot 
168e3adcf8fSFrançois Tigeot /* ASLE backlight brightness to set */
169e3adcf8fSFrançois Tigeot #define ASLE_BCLP_VALID                (1<<31)
170e3adcf8fSFrançois Tigeot #define ASLE_BCLP_MSK          (~(1<<31))
171e3adcf8fSFrançois Tigeot 
172e3adcf8fSFrançois Tigeot /* ASLE panel fitting request */
173e3adcf8fSFrançois Tigeot #define ASLE_PFIT_VALID         (1<<31)
174e3adcf8fSFrançois Tigeot #define ASLE_PFIT_CENTER (1<<0)
175e3adcf8fSFrançois Tigeot #define ASLE_PFIT_STRETCH_TEXT (1<<1)
176e3adcf8fSFrançois Tigeot #define ASLE_PFIT_STRETCH_GFX (1<<2)
177e3adcf8fSFrançois Tigeot 
178e3adcf8fSFrançois Tigeot /* PWM frequency and minimum brightness */
179e3adcf8fSFrançois Tigeot #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
180e3adcf8fSFrançois Tigeot #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
181e3adcf8fSFrançois Tigeot #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
182e3adcf8fSFrançois Tigeot #define ASLE_PFMB_PWM_VALID (1<<31)
183e3adcf8fSFrançois Tigeot 
184e3adcf8fSFrançois Tigeot #define ASLE_CBLV_VALID         (1<<31)
185e3adcf8fSFrançois Tigeot 
1869edbd4a0SFrançois Tigeot /* IUER */
1879edbd4a0SFrançois Tigeot #define ASLE_IUER_DOCKING		(1 << 7)
1889edbd4a0SFrançois Tigeot #define ASLE_IUER_CONVERTIBLE		(1 << 6)
1899edbd4a0SFrançois Tigeot #define ASLE_IUER_ROTATION_LOCK_BTN	(1 << 4)
1909edbd4a0SFrançois Tigeot #define ASLE_IUER_VOLUME_DOWN_BTN	(1 << 3)
1919edbd4a0SFrançois Tigeot #define ASLE_IUER_VOLUME_UP_BTN		(1 << 2)
1929edbd4a0SFrançois Tigeot #define ASLE_IUER_WINDOWS_BTN		(1 << 1)
1939edbd4a0SFrançois Tigeot #define ASLE_IUER_POWER_BTN		(1 << 0)
1949edbd4a0SFrançois Tigeot 
1959edbd4a0SFrançois Tigeot /* Software System Control Interrupt (SWSCI) */
1969edbd4a0SFrançois Tigeot #define SWSCI_SCIC_INDICATOR		(1 << 0)
1979edbd4a0SFrançois Tigeot #define SWSCI_SCIC_MAIN_FUNCTION_SHIFT	1
1989edbd4a0SFrançois Tigeot #define SWSCI_SCIC_MAIN_FUNCTION_MASK	(0xf << 1)
1999edbd4a0SFrançois Tigeot #define SWSCI_SCIC_SUB_FUNCTION_SHIFT	8
2009edbd4a0SFrançois Tigeot #define SWSCI_SCIC_SUB_FUNCTION_MASK	(0xff << 8)
2019edbd4a0SFrançois Tigeot #define SWSCI_SCIC_EXIT_PARAMETER_SHIFT	8
2029edbd4a0SFrançois Tigeot #define SWSCI_SCIC_EXIT_PARAMETER_MASK	(0xff << 8)
2039edbd4a0SFrançois Tigeot #define SWSCI_SCIC_EXIT_STATUS_SHIFT	5
2049edbd4a0SFrançois Tigeot #define SWSCI_SCIC_EXIT_STATUS_MASK	(7 << 5)
2059edbd4a0SFrançois Tigeot #define SWSCI_SCIC_EXIT_STATUS_SUCCESS	1
2069edbd4a0SFrançois Tigeot 
2079edbd4a0SFrançois Tigeot #define SWSCI_FUNCTION_CODE(main, sub) \
2089edbd4a0SFrançois Tigeot 	((main) << SWSCI_SCIC_MAIN_FUNCTION_SHIFT | \
2099edbd4a0SFrançois Tigeot 	 (sub) << SWSCI_SCIC_SUB_FUNCTION_SHIFT)
2109edbd4a0SFrançois Tigeot 
2119edbd4a0SFrançois Tigeot /* SWSCI: Get BIOS Data (GBDA) */
2129edbd4a0SFrançois Tigeot #define SWSCI_GBDA			4
2139edbd4a0SFrançois Tigeot #define SWSCI_GBDA_SUPPORTED_CALLS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 0)
2149edbd4a0SFrançois Tigeot #define SWSCI_GBDA_REQUESTED_CALLBACKS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 1)
2159edbd4a0SFrançois Tigeot #define SWSCI_GBDA_BOOT_DISPLAY_PREF	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 4)
2169edbd4a0SFrançois Tigeot #define SWSCI_GBDA_PANEL_DETAILS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 5)
2179edbd4a0SFrançois Tigeot #define SWSCI_GBDA_TV_STANDARD		SWSCI_FUNCTION_CODE(SWSCI_GBDA, 6)
2189edbd4a0SFrançois Tigeot #define SWSCI_GBDA_INTERNAL_GRAPHICS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 7)
2199edbd4a0SFrançois Tigeot #define SWSCI_GBDA_SPREAD_SPECTRUM	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 10)
2209edbd4a0SFrançois Tigeot 
2219edbd4a0SFrançois Tigeot /* SWSCI: System BIOS Callbacks (SBCB) */
2229edbd4a0SFrançois Tigeot #define SWSCI_SBCB			6
2239edbd4a0SFrançois Tigeot #define SWSCI_SBCB_SUPPORTED_CALLBACKS	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 0)
2249edbd4a0SFrançois Tigeot #define SWSCI_SBCB_INIT_COMPLETION	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 1)
2259edbd4a0SFrançois Tigeot #define SWSCI_SBCB_PRE_HIRES_SET_MODE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 3)
2269edbd4a0SFrançois Tigeot #define SWSCI_SBCB_POST_HIRES_SET_MODE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 4)
2279edbd4a0SFrançois Tigeot #define SWSCI_SBCB_DISPLAY_SWITCH	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 5)
2289edbd4a0SFrançois Tigeot #define SWSCI_SBCB_SET_TV_FORMAT	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 6)
2299edbd4a0SFrançois Tigeot #define SWSCI_SBCB_ADAPTER_POWER_STATE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 7)
2309edbd4a0SFrançois Tigeot #define SWSCI_SBCB_DISPLAY_POWER_STATE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 8)
2319edbd4a0SFrançois Tigeot #define SWSCI_SBCB_SET_BOOT_DISPLAY	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 9)
2329edbd4a0SFrançois Tigeot #define SWSCI_SBCB_SET_PANEL_DETAILS	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 10)
2339edbd4a0SFrançois Tigeot #define SWSCI_SBCB_SET_INTERNAL_GFX	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 11)
2349edbd4a0SFrançois Tigeot #define SWSCI_SBCB_POST_HIRES_TO_DOS_FS	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 16)
2359edbd4a0SFrançois Tigeot #define SWSCI_SBCB_SUSPEND_RESUME	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 17)
2369edbd4a0SFrançois Tigeot #define SWSCI_SBCB_SET_SPREAD_SPECTRUM	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 18)
2379edbd4a0SFrançois Tigeot #define SWSCI_SBCB_POST_VBE_PM		SWSCI_FUNCTION_CODE(SWSCI_SBCB, 19)
2389edbd4a0SFrançois Tigeot #define SWSCI_SBCB_ENABLE_DISABLE_AUDIO	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 21)
2399edbd4a0SFrançois Tigeot 
2401487f786SFrançois Tigeot /*
2411487f786SFrançois Tigeot  * ACPI Specification, Revision 5.0, Appendix B.3.2 _DOD (Enumerate All Devices
2421487f786SFrançois Tigeot  * Attached to the Display Adapter).
2431487f786SFrançois Tigeot  */
2441487f786SFrançois Tigeot #define ACPI_DISPLAY_INDEX_SHIFT		0
2451487f786SFrançois Tigeot #define ACPI_DISPLAY_INDEX_MASK			(0xf << 0)
2461487f786SFrançois Tigeot #define ACPI_DISPLAY_PORT_ATTACHMENT_SHIFT	4
2471487f786SFrançois Tigeot #define ACPI_DISPLAY_PORT_ATTACHMENT_MASK	(0xf << 4)
2481487f786SFrançois Tigeot #define ACPI_DISPLAY_TYPE_SHIFT			8
2491487f786SFrançois Tigeot #define ACPI_DISPLAY_TYPE_MASK			(0xf << 8)
2501487f786SFrançois Tigeot #define ACPI_DISPLAY_TYPE_OTHER			(0 << 8)
2511487f786SFrançois Tigeot #define ACPI_DISPLAY_TYPE_VGA			(1 << 8)
2521487f786SFrançois Tigeot #define ACPI_DISPLAY_TYPE_TV			(2 << 8)
2531487f786SFrançois Tigeot #define ACPI_DISPLAY_TYPE_EXTERNAL_DIGITAL	(3 << 8)
2541487f786SFrançois Tigeot #define ACPI_DISPLAY_TYPE_INTERNAL_DIGITAL	(4 << 8)
2551487f786SFrançois Tigeot #define ACPI_VENDOR_SPECIFIC_SHIFT		12
2561487f786SFrançois Tigeot #define ACPI_VENDOR_SPECIFIC_MASK		(0xf << 12)
2571487f786SFrançois Tigeot #define ACPI_BIOS_CAN_DETECT			(1 << 16)
2581487f786SFrançois Tigeot #define ACPI_DEPENDS_ON_VGA			(1 << 17)
2591487f786SFrançois Tigeot #define ACPI_PIPE_ID_SHIFT			18
2601487f786SFrançois Tigeot #define ACPI_PIPE_ID_MASK			(7 << 18)
2611487f786SFrançois Tigeot #define ACPI_DEVICE_ID_SCHEME			(1 << 31)
262e3adcf8fSFrançois Tigeot 
2639edbd4a0SFrançois Tigeot #define MAX_DSLP	1500
2649edbd4a0SFrançois Tigeot 
swsci(struct drm_i915_private * dev_priv,u32 function,u32 parm,u32 * parm_out)2651487f786SFrançois Tigeot static int swsci(struct drm_i915_private *dev_priv,
2661487f786SFrançois Tigeot 		 u32 function, u32 parm, u32 *parm_out)
2679edbd4a0SFrançois Tigeot {
268352ff8bdSFrançois Tigeot 	struct opregion_swsci *swsci = dev_priv->opregion.swsci;
269303bf270SFrançois Tigeot 	struct pci_dev *pdev = dev_priv->drm.pdev;
2709edbd4a0SFrançois Tigeot 	u32 main_function, sub_function, scic;
2718621f407SFrançois Tigeot 	u16 swsci_val;
2729edbd4a0SFrançois Tigeot 	u32 dslp;
2739edbd4a0SFrançois Tigeot 
2749edbd4a0SFrançois Tigeot 	if (!swsci)
2759edbd4a0SFrançois Tigeot 		return -ENODEV;
2769edbd4a0SFrançois Tigeot 
2779edbd4a0SFrançois Tigeot 	main_function = (function & SWSCI_SCIC_MAIN_FUNCTION_MASK) >>
2789edbd4a0SFrançois Tigeot 		SWSCI_SCIC_MAIN_FUNCTION_SHIFT;
2799edbd4a0SFrançois Tigeot 	sub_function = (function & SWSCI_SCIC_SUB_FUNCTION_MASK) >>
2809edbd4a0SFrançois Tigeot 		SWSCI_SCIC_SUB_FUNCTION_SHIFT;
2819edbd4a0SFrançois Tigeot 
2829edbd4a0SFrançois Tigeot 	/* Check if we can call the function. See swsci_setup for details. */
2839edbd4a0SFrançois Tigeot 	if (main_function == SWSCI_SBCB) {
2849edbd4a0SFrançois Tigeot 		if ((dev_priv->opregion.swsci_sbcb_sub_functions &
2859edbd4a0SFrançois Tigeot 		     (1 << sub_function)) == 0)
2869edbd4a0SFrançois Tigeot 			return -EINVAL;
2879edbd4a0SFrançois Tigeot 	} else if (main_function == SWSCI_GBDA) {
2889edbd4a0SFrançois Tigeot 		if ((dev_priv->opregion.swsci_gbda_sub_functions &
2899edbd4a0SFrançois Tigeot 		     (1 << sub_function)) == 0)
2909edbd4a0SFrançois Tigeot 			return -EINVAL;
2919edbd4a0SFrançois Tigeot 	}
2929edbd4a0SFrançois Tigeot 
2939edbd4a0SFrançois Tigeot 	/* Driver sleep timeout in ms. */
294352ff8bdSFrançois Tigeot 	dslp = swsci->dslp;
2959edbd4a0SFrançois Tigeot 	if (!dslp) {
2969edbd4a0SFrançois Tigeot 		/* The spec says 2ms should be the default, but it's too small
2979edbd4a0SFrançois Tigeot 		 * for some machines. */
2989edbd4a0SFrançois Tigeot 		dslp = 50;
2999edbd4a0SFrançois Tigeot 	} else if (dslp > MAX_DSLP) {
3009edbd4a0SFrançois Tigeot 		/* Hey bios, trust must be earned. */
3019edbd4a0SFrançois Tigeot 		DRM_INFO_ONCE("ACPI BIOS requests an excessive sleep of %u ms, "
3029edbd4a0SFrançois Tigeot 			      "using %u ms instead\n", dslp, MAX_DSLP);
3039edbd4a0SFrançois Tigeot 		dslp = MAX_DSLP;
3049edbd4a0SFrançois Tigeot 	}
3059edbd4a0SFrançois Tigeot 
3069edbd4a0SFrançois Tigeot 	/* The spec tells us to do this, but we are the only user... */
307352ff8bdSFrançois Tigeot 	scic = swsci->scic;
3089edbd4a0SFrançois Tigeot 	if (scic & SWSCI_SCIC_INDICATOR) {
3099edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("SWSCI request already in progress\n");
3109edbd4a0SFrançois Tigeot 		return -EBUSY;
3119edbd4a0SFrançois Tigeot 	}
3129edbd4a0SFrançois Tigeot 
3139edbd4a0SFrançois Tigeot 	scic = function | SWSCI_SCIC_INDICATOR;
3149edbd4a0SFrançois Tigeot 
315352ff8bdSFrançois Tigeot 	swsci->parm = parm;
316352ff8bdSFrançois Tigeot 	swsci->scic = scic;
3179edbd4a0SFrançois Tigeot 
3189edbd4a0SFrançois Tigeot 	/* Ensure SCI event is selected and event trigger is cleared. */
3191487f786SFrançois Tigeot 	pci_read_config_word(pdev, SWSCI, &swsci_val);
3208621f407SFrançois Tigeot 	if (!(swsci_val & SWSCI_SCISEL) || (swsci_val & SWSCI_GSSCIE)) {
3218621f407SFrançois Tigeot 		swsci_val |= SWSCI_SCISEL;
3228621f407SFrançois Tigeot 		swsci_val &= ~SWSCI_GSSCIE;
3231487f786SFrançois Tigeot 		pci_write_config_word(pdev, SWSCI, swsci_val);
3249edbd4a0SFrançois Tigeot 	}
3259edbd4a0SFrançois Tigeot 
3269edbd4a0SFrançois Tigeot 	/* Use event trigger to tell bios to check the mail. */
3278621f407SFrançois Tigeot 	swsci_val |= SWSCI_GSSCIE;
3281487f786SFrançois Tigeot 	pci_write_config_word(pdev, SWSCI, swsci_val);
3299edbd4a0SFrançois Tigeot 
3309edbd4a0SFrançois Tigeot 	/* Poll for the result. */
331352ff8bdSFrançois Tigeot #define C (((scic = swsci->scic) & SWSCI_SCIC_INDICATOR) == 0)
3329edbd4a0SFrançois Tigeot 	if (wait_for(C, dslp)) {
3339edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("SWSCI request timed out\n");
3349edbd4a0SFrançois Tigeot 		return -ETIMEDOUT;
3359edbd4a0SFrançois Tigeot 	}
3369edbd4a0SFrançois Tigeot 
3379edbd4a0SFrançois Tigeot 	scic = (scic & SWSCI_SCIC_EXIT_STATUS_MASK) >>
3389edbd4a0SFrançois Tigeot 		SWSCI_SCIC_EXIT_STATUS_SHIFT;
3399edbd4a0SFrançois Tigeot 
3409edbd4a0SFrançois Tigeot 	/* Note: scic == 0 is an error! */
3419edbd4a0SFrançois Tigeot 	if (scic != SWSCI_SCIC_EXIT_STATUS_SUCCESS) {
3429edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("SWSCI request error %u\n", scic);
3439edbd4a0SFrançois Tigeot 		return -EIO;
3449edbd4a0SFrançois Tigeot 	}
3459edbd4a0SFrançois Tigeot 
3469edbd4a0SFrançois Tigeot 	if (parm_out)
347352ff8bdSFrançois Tigeot 		*parm_out = swsci->parm;
3489edbd4a0SFrançois Tigeot 
3499edbd4a0SFrançois Tigeot 	return 0;
3509edbd4a0SFrançois Tigeot 
3519edbd4a0SFrançois Tigeot #undef C
3529edbd4a0SFrançois Tigeot }
3539edbd4a0SFrançois Tigeot 
3549edbd4a0SFrançois Tigeot #define DISPLAY_TYPE_CRT			0
3559edbd4a0SFrançois Tigeot #define DISPLAY_TYPE_TV				1
3569edbd4a0SFrançois Tigeot #define DISPLAY_TYPE_EXTERNAL_FLAT_PANEL	2
3579edbd4a0SFrançois Tigeot #define DISPLAY_TYPE_INTERNAL_FLAT_PANEL	3
3589edbd4a0SFrançois Tigeot 
intel_opregion_notify_encoder(struct intel_encoder * intel_encoder,bool enable)3599edbd4a0SFrançois Tigeot int intel_opregion_notify_encoder(struct intel_encoder *intel_encoder,
3609edbd4a0SFrançois Tigeot 				  bool enable)
3619edbd4a0SFrançois Tigeot {
3621487f786SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
3639edbd4a0SFrançois Tigeot 	u32 parm = 0;
3649edbd4a0SFrançois Tigeot 	u32 type = 0;
3659edbd4a0SFrançois Tigeot 	u32 port;
3669edbd4a0SFrançois Tigeot 
3679edbd4a0SFrançois Tigeot 	/* don't care about old stuff for now */
3681487f786SFrançois Tigeot 	if (!HAS_DDI(dev_priv))
3699edbd4a0SFrançois Tigeot 		return 0;
3709edbd4a0SFrançois Tigeot 
371352ff8bdSFrançois Tigeot 	if (intel_encoder->type == INTEL_OUTPUT_DSI)
372352ff8bdSFrançois Tigeot 		port = 0;
373352ff8bdSFrançois Tigeot 	else
3749edbd4a0SFrançois Tigeot 		port = intel_ddi_get_encoder_port(intel_encoder);
375352ff8bdSFrançois Tigeot 
3769edbd4a0SFrançois Tigeot 	if (port == PORT_E)  {
3779edbd4a0SFrançois Tigeot 		port = 0;
3789edbd4a0SFrançois Tigeot 	} else {
3799edbd4a0SFrançois Tigeot 		parm |= 1 << port;
3809edbd4a0SFrançois Tigeot 		port++;
3819edbd4a0SFrançois Tigeot 	}
3829edbd4a0SFrançois Tigeot 
3839edbd4a0SFrançois Tigeot 	if (!enable)
3849edbd4a0SFrançois Tigeot 		parm |= 4 << 8;
3859edbd4a0SFrançois Tigeot 
3869edbd4a0SFrançois Tigeot 	switch (intel_encoder->type) {
3879edbd4a0SFrançois Tigeot 	case INTEL_OUTPUT_ANALOG:
3889edbd4a0SFrançois Tigeot 		type = DISPLAY_TYPE_CRT;
3899edbd4a0SFrançois Tigeot 		break;
3909edbd4a0SFrançois Tigeot 	case INTEL_OUTPUT_UNKNOWN:
391303bf270SFrançois Tigeot 	case INTEL_OUTPUT_DP:
3929edbd4a0SFrançois Tigeot 	case INTEL_OUTPUT_HDMI:
393352ff8bdSFrançois Tigeot 	case INTEL_OUTPUT_DP_MST:
3949edbd4a0SFrançois Tigeot 		type = DISPLAY_TYPE_EXTERNAL_FLAT_PANEL;
3959edbd4a0SFrançois Tigeot 		break;
3969edbd4a0SFrançois Tigeot 	case INTEL_OUTPUT_EDP:
397352ff8bdSFrançois Tigeot 	case INTEL_OUTPUT_DSI:
3989edbd4a0SFrançois Tigeot 		type = DISPLAY_TYPE_INTERNAL_FLAT_PANEL;
3999edbd4a0SFrançois Tigeot 		break;
4009edbd4a0SFrançois Tigeot 	default:
4019edbd4a0SFrançois Tigeot 		WARN_ONCE(1, "unsupported intel_encoder type %d\n",
4029edbd4a0SFrançois Tigeot 			  intel_encoder->type);
4039edbd4a0SFrançois Tigeot 		return -EINVAL;
4049edbd4a0SFrançois Tigeot 	}
4059edbd4a0SFrançois Tigeot 
4069edbd4a0SFrançois Tigeot 	parm |= type << (16 + port * 3);
4079edbd4a0SFrançois Tigeot 
4081487f786SFrançois Tigeot 	return swsci(dev_priv, SWSCI_SBCB_DISPLAY_POWER_STATE, parm, NULL);
4099edbd4a0SFrançois Tigeot }
4109edbd4a0SFrançois Tigeot 
4119edbd4a0SFrançois Tigeot static const struct {
4129edbd4a0SFrançois Tigeot 	pci_power_t pci_power_state;
4139edbd4a0SFrançois Tigeot 	u32 parm;
4149edbd4a0SFrançois Tigeot } power_state_map[] = {
4159edbd4a0SFrançois Tigeot 	{ PCI_D0,	0x00 },
4169edbd4a0SFrançois Tigeot 	{ PCI_D1,	0x01 },
4179edbd4a0SFrançois Tigeot 	{ PCI_D2,	0x02 },
4189edbd4a0SFrançois Tigeot 	{ PCI_D3hot,	0x04 },
4199edbd4a0SFrançois Tigeot 	{ PCI_D3cold,	0x04 },
4209edbd4a0SFrançois Tigeot };
4219edbd4a0SFrançois Tigeot 
intel_opregion_notify_adapter(struct drm_i915_private * dev_priv,pci_power_t state)4221487f786SFrançois Tigeot int intel_opregion_notify_adapter(struct drm_i915_private *dev_priv,
4231487f786SFrançois Tigeot 				  pci_power_t state)
4249edbd4a0SFrançois Tigeot {
4259edbd4a0SFrançois Tigeot 	int i;
4269edbd4a0SFrançois Tigeot 
4271487f786SFrançois Tigeot 	if (!HAS_DDI(dev_priv))
4289edbd4a0SFrançois Tigeot 		return 0;
4299edbd4a0SFrançois Tigeot 
4309edbd4a0SFrançois Tigeot 	for (i = 0; i < ARRAY_SIZE(power_state_map); i++) {
4319edbd4a0SFrançois Tigeot 		if (state == power_state_map[i].pci_power_state)
4321487f786SFrançois Tigeot 			return swsci(dev_priv, SWSCI_SBCB_ADAPTER_POWER_STATE,
4339edbd4a0SFrançois Tigeot 				     power_state_map[i].parm, NULL);
4349edbd4a0SFrançois Tigeot 	}
4359edbd4a0SFrançois Tigeot 
4369edbd4a0SFrançois Tigeot 	return -EINVAL;
4379edbd4a0SFrançois Tigeot }
4389edbd4a0SFrançois Tigeot 
asle_set_backlight(struct drm_i915_private * dev_priv,u32 bclp)4391487f786SFrançois Tigeot static u32 asle_set_backlight(struct drm_i915_private *dev_priv, u32 bclp)
440e3adcf8fSFrançois Tigeot {
441aee94f86SFrançois Tigeot 	struct intel_connector *connector;
442a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
443352ff8bdSFrançois Tigeot 	struct opregion_asle *asle = dev_priv->opregion.asle;
444303bf270SFrançois Tigeot 	struct drm_device *dev = &dev_priv->drm;
445e3adcf8fSFrançois Tigeot 
44600640ec9SFrançois Tigeot 	DRM_DEBUG_DRIVER("bclp = 0x%08x\n", bclp);
44700640ec9SFrançois Tigeot 
448352ff8bdSFrançois Tigeot #ifndef __DragonFly__
44919c468b4SFrançois Tigeot 	if (acpi_video_get_backlight_type() == acpi_backlight_native) {
450ba55f2f5SFrançois Tigeot 		DRM_DEBUG_KMS("opregion backlight request ignored\n");
451ba55f2f5SFrançois Tigeot 		return 0;
452ba55f2f5SFrançois Tigeot 	}
453352ff8bdSFrançois Tigeot #endif
454ba55f2f5SFrançois Tigeot 
455e3adcf8fSFrançois Tigeot 	if (!(bclp & ASLE_BCLP_VALID))
4569edbd4a0SFrançois Tigeot 		return ASLC_BACKLIGHT_FAILED;
457e3adcf8fSFrançois Tigeot 
458e3adcf8fSFrançois Tigeot 	bclp &= ASLE_BCLP_MSK;
459e3adcf8fSFrançois Tigeot 	if (bclp > 255)
4609edbd4a0SFrançois Tigeot 		return ASLC_BACKLIGHT_FAILED;
461e3adcf8fSFrançois Tigeot 
462ba55f2f5SFrançois Tigeot 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
4639edbd4a0SFrançois Tigeot 
4649edbd4a0SFrançois Tigeot 	/*
4659edbd4a0SFrançois Tigeot 	 * Update backlight on all connectors that support backlight (usually
4669edbd4a0SFrançois Tigeot 	 * only one).
4679edbd4a0SFrançois Tigeot 	 */
4689edbd4a0SFrançois Tigeot 	DRM_DEBUG_KMS("updating opregion backlight %d/255\n", bclp);
469a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(dev, &conn_iter);
470a85cb24fSFrançois Tigeot 	for_each_intel_connector_iter(connector, &conn_iter)
471*3f2dd94aSFrançois Tigeot 		intel_panel_set_backlight_acpi(connector->base.state, bclp, 255);
472a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
473352ff8bdSFrançois Tigeot 	asle->cblv = DIV_ROUND_UP(bclp * 100, 255) | ASLE_CBLV_VALID;
4749edbd4a0SFrançois Tigeot 
475ba55f2f5SFrançois Tigeot 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
4769edbd4a0SFrançois Tigeot 
477e3adcf8fSFrançois Tigeot 
478e3adcf8fSFrançois Tigeot 	return 0;
479e3adcf8fSFrançois Tigeot }
480e3adcf8fSFrançois Tigeot 
asle_set_als_illum(struct drm_i915_private * dev_priv,u32 alsi)4811487f786SFrançois Tigeot static u32 asle_set_als_illum(struct drm_i915_private *dev_priv, u32 alsi)
482e3adcf8fSFrançois Tigeot {
483e3adcf8fSFrançois Tigeot 	/* alsi is the current ALS reading in lux. 0 indicates below sensor
484e3adcf8fSFrançois Tigeot 	   range, 0xffff indicates above sensor range. 1-0xfffe are valid */
4855d0b1887SFrançois Tigeot 	DRM_DEBUG_DRIVER("Illum is not supported\n");
4869edbd4a0SFrançois Tigeot 	return ASLC_ALS_ILLUM_FAILED;
487e3adcf8fSFrançois Tigeot }
488e3adcf8fSFrançois Tigeot 
asle_set_pwm_freq(struct drm_i915_private * dev_priv,u32 pfmb)4891487f786SFrançois Tigeot static u32 asle_set_pwm_freq(struct drm_i915_private *dev_priv, u32 pfmb)
490e3adcf8fSFrançois Tigeot {
4915d0b1887SFrançois Tigeot 	DRM_DEBUG_DRIVER("PWM freq is not supported\n");
4929edbd4a0SFrançois Tigeot 	return ASLC_PWM_FREQ_FAILED;
493e3adcf8fSFrançois Tigeot }
494e3adcf8fSFrançois Tigeot 
asle_set_pfit(struct drm_i915_private * dev_priv,u32 pfit)4951487f786SFrançois Tigeot static u32 asle_set_pfit(struct drm_i915_private *dev_priv, u32 pfit)
496e3adcf8fSFrançois Tigeot {
497e3adcf8fSFrançois Tigeot 	/* Panel fitting is currently controlled by the X code, so this is a
498e3adcf8fSFrançois Tigeot 	   noop until modesetting support works fully */
4995d0b1887SFrançois Tigeot 	DRM_DEBUG_DRIVER("Pfit is not supported\n");
5009edbd4a0SFrançois Tigeot 	return ASLC_PFIT_FAILED;
5019edbd4a0SFrançois Tigeot }
5029edbd4a0SFrançois Tigeot 
asle_set_supported_rotation_angles(struct drm_i915_private * dev_priv,u32 srot)5031487f786SFrançois Tigeot static u32 asle_set_supported_rotation_angles(struct drm_i915_private *dev_priv, u32 srot)
5049edbd4a0SFrançois Tigeot {
5059edbd4a0SFrançois Tigeot 	DRM_DEBUG_DRIVER("SROT is not supported\n");
5069edbd4a0SFrançois Tigeot 	return ASLC_ROTATION_ANGLES_FAILED;
5079edbd4a0SFrançois Tigeot }
5089edbd4a0SFrançois Tigeot 
asle_set_button_array(struct drm_i915_private * dev_priv,u32 iuer)5091487f786SFrançois Tigeot static u32 asle_set_button_array(struct drm_i915_private *dev_priv, u32 iuer)
5109edbd4a0SFrançois Tigeot {
5119edbd4a0SFrançois Tigeot 	if (!iuer)
5129edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Button array event is not supported (nothing)\n");
5139edbd4a0SFrançois Tigeot 	if (iuer & ASLE_IUER_ROTATION_LOCK_BTN)
5149edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Button array event is not supported (rotation lock)\n");
5159edbd4a0SFrançois Tigeot 	if (iuer & ASLE_IUER_VOLUME_DOWN_BTN)
5169edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Button array event is not supported (volume down)\n");
5179edbd4a0SFrançois Tigeot 	if (iuer & ASLE_IUER_VOLUME_UP_BTN)
5189edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Button array event is not supported (volume up)\n");
5199edbd4a0SFrançois Tigeot 	if (iuer & ASLE_IUER_WINDOWS_BTN)
5209edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Button array event is not supported (windows)\n");
5219edbd4a0SFrançois Tigeot 	if (iuer & ASLE_IUER_POWER_BTN)
5229edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Button array event is not supported (power)\n");
5239edbd4a0SFrançois Tigeot 
5249edbd4a0SFrançois Tigeot 	return ASLC_BUTTON_ARRAY_FAILED;
5259edbd4a0SFrançois Tigeot }
5269edbd4a0SFrançois Tigeot 
asle_set_convertible(struct drm_i915_private * dev_priv,u32 iuer)5271487f786SFrançois Tigeot static u32 asle_set_convertible(struct drm_i915_private *dev_priv, u32 iuer)
5289edbd4a0SFrançois Tigeot {
5299edbd4a0SFrançois Tigeot 	if (iuer & ASLE_IUER_CONVERTIBLE)
5309edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Convertible is not supported (clamshell)\n");
5319edbd4a0SFrançois Tigeot 	else
5329edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Convertible is not supported (slate)\n");
5339edbd4a0SFrançois Tigeot 
5349edbd4a0SFrançois Tigeot 	return ASLC_CONVERTIBLE_FAILED;
5359edbd4a0SFrançois Tigeot }
5369edbd4a0SFrançois Tigeot 
asle_set_docking(struct drm_i915_private * dev_priv,u32 iuer)5371487f786SFrançois Tigeot static u32 asle_set_docking(struct drm_i915_private *dev_priv, u32 iuer)
5389edbd4a0SFrançois Tigeot {
5399edbd4a0SFrançois Tigeot 	if (iuer & ASLE_IUER_DOCKING)
5409edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Docking is not supported (docked)\n");
5419edbd4a0SFrançois Tigeot 	else
5429edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("Docking is not supported (undocked)\n");
5439edbd4a0SFrançois Tigeot 
5449edbd4a0SFrançois Tigeot 	return ASLC_DOCKING_FAILED;
5459edbd4a0SFrançois Tigeot }
5469edbd4a0SFrançois Tigeot 
asle_isct_state(struct drm_i915_private * dev_priv)5471487f786SFrançois Tigeot static u32 asle_isct_state(struct drm_i915_private *dev_priv)
5489edbd4a0SFrançois Tigeot {
5499edbd4a0SFrançois Tigeot 	DRM_DEBUG_DRIVER("ISCT is not supported\n");
5509edbd4a0SFrançois Tigeot 	return ASLC_ISCT_STATE_FAILED;
5519edbd4a0SFrançois Tigeot }
5529edbd4a0SFrançois Tigeot 
asle_work(struct work_struct * work)5539edbd4a0SFrançois Tigeot static void asle_work(struct work_struct *work)
5549edbd4a0SFrançois Tigeot {
5559edbd4a0SFrançois Tigeot 	struct intel_opregion *opregion =
5569edbd4a0SFrançois Tigeot 		container_of(work, struct intel_opregion, asle_work);
5579edbd4a0SFrançois Tigeot 	struct drm_i915_private *dev_priv =
5589edbd4a0SFrançois Tigeot 		container_of(opregion, struct drm_i915_private, opregion);
559352ff8bdSFrançois Tigeot 	struct opregion_asle *asle = dev_priv->opregion.asle;
5609edbd4a0SFrançois Tigeot 	u32 aslc_stat = 0;
5619edbd4a0SFrançois Tigeot 	u32 aslc_req;
5629edbd4a0SFrançois Tigeot 
5639edbd4a0SFrançois Tigeot 	if (!asle)
5649edbd4a0SFrançois Tigeot 		return;
5659edbd4a0SFrançois Tigeot 
566352ff8bdSFrançois Tigeot 	aslc_req = asle->aslc;
5679edbd4a0SFrançois Tigeot 
5689edbd4a0SFrançois Tigeot 	if (!(aslc_req & ASLC_REQ_MSK)) {
5699edbd4a0SFrançois Tigeot 		DRM_DEBUG_DRIVER("No request on ASLC interrupt 0x%08x\n",
5709edbd4a0SFrançois Tigeot 				 aslc_req);
5719edbd4a0SFrançois Tigeot 		return;
5729edbd4a0SFrançois Tigeot 	}
5739edbd4a0SFrançois Tigeot 
5749edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_SET_ALS_ILLUM)
5751487f786SFrançois Tigeot 		aslc_stat |= asle_set_als_illum(dev_priv, asle->alsi);
5769edbd4a0SFrançois Tigeot 
5779edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_SET_BACKLIGHT)
5781487f786SFrançois Tigeot 		aslc_stat |= asle_set_backlight(dev_priv, asle->bclp);
5799edbd4a0SFrançois Tigeot 
5809edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_SET_PFIT)
5811487f786SFrançois Tigeot 		aslc_stat |= asle_set_pfit(dev_priv, asle->pfit);
5829edbd4a0SFrançois Tigeot 
5839edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_SET_PWM_FREQ)
5841487f786SFrançois Tigeot 		aslc_stat |= asle_set_pwm_freq(dev_priv, asle->pfmb);
5859edbd4a0SFrançois Tigeot 
5869edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_SUPPORTED_ROTATION_ANGLES)
5871487f786SFrançois Tigeot 		aslc_stat |= asle_set_supported_rotation_angles(dev_priv,
588352ff8bdSFrançois Tigeot 							asle->srot);
5899edbd4a0SFrançois Tigeot 
5909edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_BUTTON_ARRAY)
5911487f786SFrançois Tigeot 		aslc_stat |= asle_set_button_array(dev_priv, asle->iuer);
5929edbd4a0SFrançois Tigeot 
5939edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_CONVERTIBLE_INDICATOR)
5941487f786SFrançois Tigeot 		aslc_stat |= asle_set_convertible(dev_priv, asle->iuer);
5959edbd4a0SFrançois Tigeot 
5969edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_DOCKING_INDICATOR)
5971487f786SFrançois Tigeot 		aslc_stat |= asle_set_docking(dev_priv, asle->iuer);
5989edbd4a0SFrançois Tigeot 
5999edbd4a0SFrançois Tigeot 	if (aslc_req & ASLC_ISCT_STATE_CHANGE)
6001487f786SFrançois Tigeot 		aslc_stat |= asle_isct_state(dev_priv);
6019edbd4a0SFrançois Tigeot 
602352ff8bdSFrançois Tigeot 	asle->aslc = aslc_stat;
603e3adcf8fSFrançois Tigeot }
604e3adcf8fSFrançois Tigeot 
intel_opregion_asle_intr(struct drm_i915_private * dev_priv)6051487f786SFrançois Tigeot void intel_opregion_asle_intr(struct drm_i915_private *dev_priv)
606e3adcf8fSFrançois Tigeot {
6079edbd4a0SFrançois Tigeot 	if (dev_priv->opregion.asle)
6089edbd4a0SFrançois Tigeot 		schedule_work(&dev_priv->opregion.asle_work);
609e3adcf8fSFrançois Tigeot }
610e3adcf8fSFrançois Tigeot 
611e3adcf8fSFrançois Tigeot #define ACPI_EV_DISPLAY_SWITCH (1<<0)
612e3adcf8fSFrançois Tigeot #define ACPI_EV_LID            (1<<1)
613e3adcf8fSFrançois Tigeot #define ACPI_EV_DOCK           (1<<2)
614e3adcf8fSFrançois Tigeot 
615e3adcf8fSFrançois Tigeot static struct intel_opregion *system_opregion;
616e3adcf8fSFrançois Tigeot 
61790c722cdSMarkus Pfeiffer #if 0
618e3adcf8fSFrançois Tigeot static int intel_opregion_video_event(struct notifier_block *nb,
619e3adcf8fSFrançois Tigeot 				      unsigned long val, void *data)
620e3adcf8fSFrançois Tigeot {
621e3adcf8fSFrançois Tigeot 	/* The only video events relevant to opregion are 0x80. These indicate
622e3adcf8fSFrançois Tigeot 	   either a docking event, lid switch or display switch request. In
623e3adcf8fSFrançois Tigeot 	   Linux, these are handled by the dock, button and video drivers.
624e3adcf8fSFrançois Tigeot 	*/
62500640ec9SFrançois Tigeot 
626e3adcf8fSFrançois Tigeot 	struct acpi_bus_event *event = data;
627352ff8bdSFrançois Tigeot 	struct opregion_acpi *acpi;
628e3adcf8fSFrançois Tigeot 	int ret = NOTIFY_OK;
629e3adcf8fSFrançois Tigeot 
630e3adcf8fSFrançois Tigeot 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
631e3adcf8fSFrançois Tigeot 		return NOTIFY_DONE;
632e3adcf8fSFrançois Tigeot 
633e3adcf8fSFrançois Tigeot 	if (!system_opregion)
634e3adcf8fSFrançois Tigeot 		return NOTIFY_DONE;
635e3adcf8fSFrançois Tigeot 
636e3adcf8fSFrançois Tigeot 	acpi = system_opregion->acpi;
637e3adcf8fSFrançois Tigeot 
638352ff8bdSFrançois Tigeot 	if (event->type == 0x80 && ((acpi->cevt & 1) == 0))
639e3adcf8fSFrançois Tigeot 		ret = NOTIFY_BAD;
640e3adcf8fSFrançois Tigeot 
641352ff8bdSFrançois Tigeot 	acpi->csts = 0;
642e3adcf8fSFrançois Tigeot 
643e3adcf8fSFrançois Tigeot 	return ret;
644e3adcf8fSFrançois Tigeot }
645e3adcf8fSFrançois Tigeot 
646e3adcf8fSFrançois Tigeot static struct notifier_block intel_opregion_notifier = {
647e3adcf8fSFrançois Tigeot 	.notifier_call = intel_opregion_video_event,
648e3adcf8fSFrançois Tigeot };
64990c722cdSMarkus Pfeiffer #endif
65090c722cdSMarkus Pfeiffer 
651e3adcf8fSFrançois Tigeot /*
652e3adcf8fSFrançois Tigeot  * Initialise the DIDL field in opregion. This passes a list of devices to
653e3adcf8fSFrançois Tigeot  * the firmware. Values are defined by section B.4.2 of the ACPI specification
654e3adcf8fSFrançois Tigeot  * (version 3)
655e3adcf8fSFrançois Tigeot  */
656e3adcf8fSFrançois Tigeot 
set_did(struct intel_opregion * opregion,int i,u32 val)657a05eeebfSFrançois Tigeot static void set_did(struct intel_opregion *opregion, int i, u32 val)
658a05eeebfSFrançois Tigeot {
659a05eeebfSFrançois Tigeot 	if (i < ARRAY_SIZE(opregion->acpi->didl)) {
660352ff8bdSFrançois Tigeot 		opregion->acpi->didl[i] = val;
661a05eeebfSFrançois Tigeot 	} else {
662a05eeebfSFrançois Tigeot 		i -= ARRAY_SIZE(opregion->acpi->didl);
663a05eeebfSFrançois Tigeot 
664a05eeebfSFrançois Tigeot 		if (WARN_ON(i >= ARRAY_SIZE(opregion->acpi->did2)))
665a05eeebfSFrançois Tigeot 			return;
666a05eeebfSFrançois Tigeot 
667352ff8bdSFrançois Tigeot 		opregion->acpi->did2[i] = val;
668a05eeebfSFrançois Tigeot 	}
669a05eeebfSFrançois Tigeot }
670a05eeebfSFrançois Tigeot 
acpi_display_type(struct intel_connector * connector)6714be47400SFrançois Tigeot static u32 acpi_display_type(struct intel_connector *connector)
672303bf270SFrançois Tigeot {
673303bf270SFrançois Tigeot 	u32 display_type;
674303bf270SFrançois Tigeot 
6754be47400SFrançois Tigeot 	switch (connector->base.connector_type) {
676303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_VGA:
677303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_DVIA:
678303bf270SFrançois Tigeot 		display_type = ACPI_DISPLAY_TYPE_VGA;
679303bf270SFrançois Tigeot 		break;
680303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_Composite:
681303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_SVIDEO:
682303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_Component:
683303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_9PinDIN:
684303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_TV:
685303bf270SFrançois Tigeot 		display_type = ACPI_DISPLAY_TYPE_TV;
686303bf270SFrançois Tigeot 		break;
687303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_DVII:
688303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_DVID:
689303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_DisplayPort:
690303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_HDMIA:
691303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_HDMIB:
692303bf270SFrançois Tigeot 		display_type = ACPI_DISPLAY_TYPE_EXTERNAL_DIGITAL;
693303bf270SFrançois Tigeot 		break;
694303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_LVDS:
695303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_eDP:
696303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_DSI:
697303bf270SFrançois Tigeot 		display_type = ACPI_DISPLAY_TYPE_INTERNAL_DIGITAL;
698303bf270SFrançois Tigeot 		break;
699303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_Unknown:
700303bf270SFrançois Tigeot 	case DRM_MODE_CONNECTOR_VIRTUAL:
701303bf270SFrançois Tigeot 		display_type = ACPI_DISPLAY_TYPE_OTHER;
702303bf270SFrançois Tigeot 		break;
703303bf270SFrançois Tigeot 	default:
7044be47400SFrançois Tigeot 		MISSING_CASE(connector->base.connector_type);
705303bf270SFrançois Tigeot 		display_type = ACPI_DISPLAY_TYPE_OTHER;
706303bf270SFrançois Tigeot 		break;
707303bf270SFrançois Tigeot 	}
708303bf270SFrançois Tigeot 
709303bf270SFrançois Tigeot 	return display_type;
710303bf270SFrançois Tigeot }
711303bf270SFrançois Tigeot 
intel_didl_outputs(struct drm_i915_private * dev_priv)7121487f786SFrançois Tigeot static void intel_didl_outputs(struct drm_i915_private *dev_priv)
713e3adcf8fSFrançois Tigeot {
714e3adcf8fSFrançois Tigeot 	struct intel_opregion *opregion = &dev_priv->opregion;
7154be47400SFrançois Tigeot 	struct intel_connector *connector;
716a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
7174be47400SFrançois Tigeot 	int i = 0, max_outputs;
7184be47400SFrançois Tigeot 	int display_index[16] = {};
719e3adcf8fSFrançois Tigeot 
720a05eeebfSFrançois Tigeot 	/*
721a05eeebfSFrançois Tigeot 	 * In theory, did2, the extended didl, gets added at opregion version
722a05eeebfSFrançois Tigeot 	 * 3.0. In practice, however, we're supposed to set it for earlier
723a05eeebfSFrançois Tigeot 	 * versions as well, since a BIOS that doesn't understand did2 should
724a05eeebfSFrançois Tigeot 	 * not look at it anyway. Use a variable so we can tweak this if a need
725a05eeebfSFrançois Tigeot 	 * arises later.
726a05eeebfSFrançois Tigeot 	 */
727a05eeebfSFrançois Tigeot 	max_outputs = ARRAY_SIZE(opregion->acpi->didl) +
728a05eeebfSFrançois Tigeot 		ARRAY_SIZE(opregion->acpi->did2);
729a05eeebfSFrançois Tigeot 
730a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
731a85cb24fSFrançois Tigeot 	for_each_intel_connector_iter(connector, &conn_iter) {
7324be47400SFrançois Tigeot 		u32 device_id, type;
7334be47400SFrançois Tigeot 
7344be47400SFrançois Tigeot 		device_id = acpi_display_type(connector);
7354be47400SFrançois Tigeot 
7364be47400SFrançois Tigeot 		/* Use display type specific display index. */
7374be47400SFrançois Tigeot 		type = (device_id & ACPI_DISPLAY_TYPE_MASK)
7384be47400SFrançois Tigeot 			>> ACPI_DISPLAY_TYPE_SHIFT;
7394be47400SFrançois Tigeot 		device_id |= display_index[type]++ << ACPI_DISPLAY_INDEX_SHIFT;
7404be47400SFrançois Tigeot 
7414be47400SFrançois Tigeot 		connector->acpi_device_id = device_id;
7424be47400SFrançois Tigeot 		if (i < max_outputs)
7434be47400SFrançois Tigeot 			set_did(opregion, i, device_id);
7444be47400SFrançois Tigeot 		i++;
745e3adcf8fSFrançois Tigeot 	}
746a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
747a2fdbec6SFrançois Tigeot 
748a05eeebfSFrançois Tigeot 	DRM_DEBUG_KMS("%d outputs detected\n", i);
749a05eeebfSFrançois Tigeot 
7504be47400SFrançois Tigeot 	if (i > max_outputs)
7514be47400SFrançois Tigeot 		DRM_ERROR("More than %d outputs in connector list\n",
7524be47400SFrançois Tigeot 			  max_outputs);
7534be47400SFrançois Tigeot 
754a05eeebfSFrançois Tigeot 	/* If fewer than max outputs, the list must be null terminated */
755a05eeebfSFrançois Tigeot 	if (i < max_outputs)
756a05eeebfSFrançois Tigeot 		set_did(opregion, i, 0);
757e3adcf8fSFrançois Tigeot }
758e3adcf8fSFrançois Tigeot 
intel_setup_cadls(struct drm_i915_private * dev_priv)7591487f786SFrançois Tigeot static void intel_setup_cadls(struct drm_i915_private *dev_priv)
76000640ec9SFrançois Tigeot {
76100640ec9SFrançois Tigeot 	struct intel_opregion *opregion = &dev_priv->opregion;
7624be47400SFrançois Tigeot 	struct intel_connector *connector;
763a85cb24fSFrançois Tigeot 	struct drm_connector_list_iter conn_iter;
76400640ec9SFrançois Tigeot 	int i = 0;
76500640ec9SFrançois Tigeot 
7664be47400SFrançois Tigeot 	/*
7674be47400SFrançois Tigeot 	 * Initialize the CADL field from the connector device ids. This is
7684be47400SFrançois Tigeot 	 * essentially the same as copying from the DIDL. Technically, this is
7694be47400SFrançois Tigeot 	 * not always correct as display outputs may exist, but not active. This
7704be47400SFrançois Tigeot 	 * initialization is necessary for some Clevo laptops that check this
7714be47400SFrançois Tigeot 	 * field before processing the brightness and display switching hotkeys.
7724be47400SFrançois Tigeot 	 *
7734be47400SFrançois Tigeot 	 * Note that internal panels should be at the front of the connector
7744be47400SFrançois Tigeot 	 * list already, ensuring they're not left out.
7754be47400SFrançois Tigeot 	 */
776a85cb24fSFrançois Tigeot 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
777a85cb24fSFrançois Tigeot 	for_each_intel_connector_iter(connector, &conn_iter) {
7784be47400SFrançois Tigeot 		if (i >= ARRAY_SIZE(opregion->acpi->cadl))
7794be47400SFrançois Tigeot 			break;
7804be47400SFrançois Tigeot 		opregion->acpi->cadl[i++] = connector->acpi_device_id;
7814be47400SFrançois Tigeot 	}
782a85cb24fSFrançois Tigeot 	drm_connector_list_iter_end(&conn_iter);
7834be47400SFrançois Tigeot 
7844be47400SFrançois Tigeot 	/* If fewer than 8 active devices, the list must be null terminated */
7854be47400SFrançois Tigeot 	if (i < ARRAY_SIZE(opregion->acpi->cadl))
7864be47400SFrançois Tigeot 		opregion->acpi->cadl[i] = 0;
78700640ec9SFrançois Tigeot }
78800640ec9SFrançois Tigeot 
intel_opregion_register(struct drm_i915_private * dev_priv)7891487f786SFrançois Tigeot void intel_opregion_register(struct drm_i915_private *dev_priv)
790e3adcf8fSFrançois Tigeot {
791e3adcf8fSFrançois Tigeot 	struct intel_opregion *opregion = &dev_priv->opregion;
792e3adcf8fSFrançois Tigeot 
793e3adcf8fSFrançois Tigeot 	if (!opregion->header)
794e3adcf8fSFrançois Tigeot 		return;
795e3adcf8fSFrançois Tigeot 
796e3adcf8fSFrançois Tigeot 	if (opregion->acpi) {
7971487f786SFrançois Tigeot 		intel_didl_outputs(dev_priv);
7981487f786SFrançois Tigeot 		intel_setup_cadls(dev_priv);
799e3adcf8fSFrançois Tigeot 
800e3adcf8fSFrançois Tigeot 		/* Notify BIOS we are ready to handle ACPI video ext notifs.
801e3adcf8fSFrançois Tigeot 		 * Right now, all the events are handled by the ACPI video module.
802e3adcf8fSFrançois Tigeot 		 * We don't actually need to do anything with them. */
803352ff8bdSFrançois Tigeot 		opregion->acpi->csts = 0;
804352ff8bdSFrançois Tigeot 		opregion->acpi->drdy = 1;
805e3adcf8fSFrançois Tigeot 
806e3adcf8fSFrançois Tigeot 		system_opregion = opregion;
807e3adcf8fSFrançois Tigeot 	}
808e3adcf8fSFrançois Tigeot 
8095d0b1887SFrançois Tigeot 	if (opregion->asle) {
810352ff8bdSFrançois Tigeot 		opregion->asle->tche = ASLE_TCHE_BLC_EN;
811352ff8bdSFrançois Tigeot 		opregion->asle->ardy = ASLE_ARDY_READY;
8125d0b1887SFrançois Tigeot 	}
813e3adcf8fSFrançois Tigeot }
814e3adcf8fSFrançois Tigeot 
intel_opregion_unregister(struct drm_i915_private * dev_priv)8151487f786SFrançois Tigeot void intel_opregion_unregister(struct drm_i915_private *dev_priv)
816e3adcf8fSFrançois Tigeot {
817e3adcf8fSFrançois Tigeot 	struct intel_opregion *opregion = &dev_priv->opregion;
818e3adcf8fSFrançois Tigeot 
819e3adcf8fSFrançois Tigeot 	if (!opregion->header)
820e3adcf8fSFrançois Tigeot 		return;
821e3adcf8fSFrançois Tigeot 
8225d0b1887SFrançois Tigeot 	if (opregion->asle)
823352ff8bdSFrançois Tigeot 		opregion->asle->ardy = ASLE_ARDY_NOT_READY;
8245d0b1887SFrançois Tigeot 
8259edbd4a0SFrançois Tigeot 	cancel_work_sync(&dev_priv->opregion.asle_work);
8269edbd4a0SFrançois Tigeot 
827e3adcf8fSFrançois Tigeot 	if (opregion->acpi) {
828352ff8bdSFrançois Tigeot 		opregion->acpi->drdy = 0;
829e3adcf8fSFrançois Tigeot 
830e3adcf8fSFrançois Tigeot 		system_opregion = NULL;
831e3adcf8fSFrançois Tigeot 	}
832e3adcf8fSFrançois Tigeot 
833e3adcf8fSFrançois Tigeot 	/* just clear all opregion memory pointers now */
834146a3843SFrançois Tigeot 	iounmap(opregion->header);
835aee94f86SFrançois Tigeot 	if (opregion->rvda) {
836aee94f86SFrançois Tigeot 		iounmap(opregion->rvda);
837aee94f86SFrançois Tigeot 		opregion->rvda = NULL;
838aee94f86SFrançois Tigeot 	}
839*3f2dd94aSFrançois Tigeot 	if (opregion->vbt_firmware) {
840*3f2dd94aSFrançois Tigeot 		kfree(opregion->vbt_firmware);
841*3f2dd94aSFrançois Tigeot 		opregion->vbt_firmware = NULL;
842*3f2dd94aSFrançois Tigeot 	}
843e3adcf8fSFrançois Tigeot 	opregion->header = NULL;
844e3adcf8fSFrançois Tigeot 	opregion->acpi = NULL;
845e3adcf8fSFrançois Tigeot 	opregion->swsci = NULL;
846e3adcf8fSFrançois Tigeot 	opregion->asle = NULL;
847e3adcf8fSFrançois Tigeot 	opregion->vbt = NULL;
8489edbd4a0SFrançois Tigeot 	opregion->lid_state = NULL;
8499edbd4a0SFrançois Tigeot }
8509edbd4a0SFrançois Tigeot 
swsci_setup(struct drm_i915_private * dev_priv)8511487f786SFrançois Tigeot static void swsci_setup(struct drm_i915_private *dev_priv)
8529edbd4a0SFrançois Tigeot {
8539edbd4a0SFrançois Tigeot 	struct intel_opregion *opregion = &dev_priv->opregion;
8549edbd4a0SFrançois Tigeot 	bool requested_callbacks = false;
8559edbd4a0SFrançois Tigeot 	u32 tmp;
8569edbd4a0SFrançois Tigeot 
8579edbd4a0SFrançois Tigeot 	/* Sub-function code 0 is okay, let's allow them. */
8589edbd4a0SFrançois Tigeot 	opregion->swsci_gbda_sub_functions = 1;
8599edbd4a0SFrançois Tigeot 	opregion->swsci_sbcb_sub_functions = 1;
8609edbd4a0SFrançois Tigeot 
8619edbd4a0SFrançois Tigeot 	/* We use GBDA to ask for supported GBDA calls. */
8621487f786SFrançois Tigeot 	if (swsci(dev_priv, SWSCI_GBDA_SUPPORTED_CALLS, 0, &tmp) == 0) {
8639edbd4a0SFrançois Tigeot 		/* make the bits match the sub-function codes */
8649edbd4a0SFrançois Tigeot 		tmp <<= 1;
8659edbd4a0SFrançois Tigeot 		opregion->swsci_gbda_sub_functions |= tmp;
8669edbd4a0SFrançois Tigeot 	}
8679edbd4a0SFrançois Tigeot 
8689edbd4a0SFrançois Tigeot 	/*
8699edbd4a0SFrançois Tigeot 	 * We also use GBDA to ask for _requested_ SBCB callbacks. The driver
8709edbd4a0SFrançois Tigeot 	 * must not call interfaces that are not specifically requested by the
8719edbd4a0SFrançois Tigeot 	 * bios.
8729edbd4a0SFrançois Tigeot 	 */
8731487f786SFrançois Tigeot 	if (swsci(dev_priv, SWSCI_GBDA_REQUESTED_CALLBACKS, 0, &tmp) == 0) {
8749edbd4a0SFrançois Tigeot 		/* here, the bits already match sub-function codes */
8759edbd4a0SFrançois Tigeot 		opregion->swsci_sbcb_sub_functions |= tmp;
8769edbd4a0SFrançois Tigeot 		requested_callbacks = true;
8779edbd4a0SFrançois Tigeot 	}
8789edbd4a0SFrançois Tigeot 
8799edbd4a0SFrançois Tigeot 	/*
8809edbd4a0SFrançois Tigeot 	 * But we use SBCB to ask for _supported_ SBCB calls. This does not mean
8819edbd4a0SFrançois Tigeot 	 * the callback is _requested_. But we still can't call interfaces that
8829edbd4a0SFrançois Tigeot 	 * are not requested.
8839edbd4a0SFrançois Tigeot 	 */
8841487f786SFrançois Tigeot 	if (swsci(dev_priv, SWSCI_SBCB_SUPPORTED_CALLBACKS, 0, &tmp) == 0) {
8859edbd4a0SFrançois Tigeot 		/* make the bits match the sub-function codes */
8869edbd4a0SFrançois Tigeot 		u32 low = tmp & 0x7ff;
8879edbd4a0SFrançois Tigeot 		u32 high = tmp & ~0xfff; /* bit 11 is reserved */
8889edbd4a0SFrançois Tigeot 		tmp = (high << 4) | (low << 1) | 1;
8899edbd4a0SFrançois Tigeot 
8909edbd4a0SFrançois Tigeot 		/* best guess what to do with supported wrt requested */
8919edbd4a0SFrançois Tigeot 		if (requested_callbacks) {
8929edbd4a0SFrançois Tigeot 			u32 req = opregion->swsci_sbcb_sub_functions;
8939edbd4a0SFrançois Tigeot 			if ((req & tmp) != req)
8949edbd4a0SFrançois Tigeot 				DRM_DEBUG_DRIVER("SWSCI BIOS requested (%08x) SBCB callbacks that are not supported (%08x)\n", req, tmp);
8959edbd4a0SFrançois Tigeot 			/* XXX: for now, trust the requested callbacks */
8969edbd4a0SFrançois Tigeot 			/* opregion->swsci_sbcb_sub_functions &= tmp; */
8979edbd4a0SFrançois Tigeot 		} else {
8989edbd4a0SFrançois Tigeot 			opregion->swsci_sbcb_sub_functions |= tmp;
8999edbd4a0SFrançois Tigeot 		}
9009edbd4a0SFrançois Tigeot 	}
9019edbd4a0SFrançois Tigeot 
9029edbd4a0SFrançois Tigeot 	DRM_DEBUG_DRIVER("SWSCI GBDA callbacks %08x, SBCB callbacks %08x\n",
9039edbd4a0SFrançois Tigeot 			 opregion->swsci_gbda_sub_functions,
9049edbd4a0SFrançois Tigeot 			 opregion->swsci_sbcb_sub_functions);
905e3adcf8fSFrançois Tigeot }
906e3adcf8fSFrançois Tigeot 
intel_no_opregion_vbt_callback(const struct dmi_system_id * id)907aee94f86SFrançois Tigeot static int intel_no_opregion_vbt_callback(const struct dmi_system_id *id)
908aee94f86SFrançois Tigeot {
909aee94f86SFrançois Tigeot 	DRM_DEBUG_KMS("Falling back to manually reading VBT from "
910aee94f86SFrançois Tigeot 		      "VBIOS ROM for %s\n", id->ident);
911aee94f86SFrançois Tigeot 	return 1;
912aee94f86SFrançois Tigeot }
913aee94f86SFrançois Tigeot 
914aee94f86SFrançois Tigeot static const struct dmi_system_id intel_no_opregion_vbt[] = {
915aee94f86SFrançois Tigeot 	{
916aee94f86SFrançois Tigeot 		.callback = intel_no_opregion_vbt_callback,
917aee94f86SFrançois Tigeot 		.ident = "ThinkCentre A57",
918aee94f86SFrançois Tigeot 		.matches = {
919aee94f86SFrançois Tigeot 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
920aee94f86SFrançois Tigeot 			DMI_MATCH(DMI_PRODUCT_NAME, "97027RG"),
921aee94f86SFrançois Tigeot 		},
922aee94f86SFrançois Tigeot 	},
923aee94f86SFrançois Tigeot 	{ }
924aee94f86SFrançois Tigeot };
925aee94f86SFrançois Tigeot 
intel_load_vbt_firmware(struct drm_i915_private * dev_priv)926*3f2dd94aSFrançois Tigeot static int intel_load_vbt_firmware(struct drm_i915_private *dev_priv)
927*3f2dd94aSFrançois Tigeot {
928*3f2dd94aSFrançois Tigeot 	struct intel_opregion *opregion = &dev_priv->opregion;
929*3f2dd94aSFrançois Tigeot 	const struct firmware *fw = NULL;
930*3f2dd94aSFrançois Tigeot 	const char *name = i915_modparams.vbt_firmware;
931*3f2dd94aSFrançois Tigeot 	int ret;
932*3f2dd94aSFrançois Tigeot 
933*3f2dd94aSFrançois Tigeot 	if (!name || !*name)
934*3f2dd94aSFrançois Tigeot 		return -ENOENT;
935*3f2dd94aSFrançois Tigeot 
936*3f2dd94aSFrançois Tigeot 	ret = request_firmware(&fw, name, &dev_priv->drm.pdev->dev);
937*3f2dd94aSFrançois Tigeot 	if (ret) {
938*3f2dd94aSFrançois Tigeot 		DRM_ERROR("Requesting VBT firmware \"%s\" failed (%d)\n",
939*3f2dd94aSFrançois Tigeot 			  name, ret);
940*3f2dd94aSFrançois Tigeot 		return ret;
941*3f2dd94aSFrançois Tigeot 	}
942*3f2dd94aSFrançois Tigeot 
943*3f2dd94aSFrançois Tigeot 	if (intel_bios_is_valid_vbt(fw->data, fw->datasize)) {
944*3f2dd94aSFrançois Tigeot 		opregion->vbt_firmware = kmemdup(fw->data, fw->datasize, GFP_KERNEL);
945*3f2dd94aSFrançois Tigeot 		if (opregion->vbt_firmware) {
946*3f2dd94aSFrançois Tigeot 			DRM_DEBUG_KMS("Found valid VBT firmware \"%s\"\n", name);
947*3f2dd94aSFrançois Tigeot 			opregion->vbt = opregion->vbt_firmware;
948*3f2dd94aSFrançois Tigeot 			opregion->vbt_size = fw->datasize;
949*3f2dd94aSFrançois Tigeot 			ret = 0;
950*3f2dd94aSFrançois Tigeot 		} else {
951*3f2dd94aSFrançois Tigeot 			ret = -ENOMEM;
952*3f2dd94aSFrançois Tigeot 		}
953*3f2dd94aSFrançois Tigeot 	} else {
954*3f2dd94aSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid VBT firmware \"%s\"\n", name);
955*3f2dd94aSFrançois Tigeot 		ret = -EINVAL;
956*3f2dd94aSFrançois Tigeot 	}
957*3f2dd94aSFrançois Tigeot 
958*3f2dd94aSFrançois Tigeot 	release_firmware(fw);
959*3f2dd94aSFrançois Tigeot 
960*3f2dd94aSFrançois Tigeot 	return ret;
961*3f2dd94aSFrançois Tigeot }
962*3f2dd94aSFrançois Tigeot 
intel_opregion_setup(struct drm_i915_private * dev_priv)9631487f786SFrançois Tigeot int intel_opregion_setup(struct drm_i915_private *dev_priv)
964e3adcf8fSFrançois Tigeot {
965e3adcf8fSFrançois Tigeot 	struct intel_opregion *opregion = &dev_priv->opregion;
966303bf270SFrançois Tigeot 	struct pci_dev *pdev = dev_priv->drm.pdev;
967e3adcf8fSFrançois Tigeot 	u32 asls, mboxes;
96800640ec9SFrançois Tigeot 	char buf[sizeof(OPREGION_SIGNATURE)];
969e3adcf8fSFrançois Tigeot 	int err = 0;
970a85cb24fSFrançois Tigeot 	void *base;
971a85cb24fSFrançois Tigeot 	const void *vbt;
972a85cb24fSFrançois Tigeot 	u32 vbt_size;
973e3adcf8fSFrançois Tigeot 
974a05eeebfSFrançois Tigeot 	BUILD_BUG_ON(sizeof(struct opregion_header) != 0x100);
975a05eeebfSFrançois Tigeot 	BUILD_BUG_ON(sizeof(struct opregion_acpi) != 0x100);
976a05eeebfSFrançois Tigeot 	BUILD_BUG_ON(sizeof(struct opregion_swsci) != 0x100);
977a05eeebfSFrançois Tigeot 	BUILD_BUG_ON(sizeof(struct opregion_asle) != 0x100);
978aee94f86SFrançois Tigeot 	BUILD_BUG_ON(sizeof(struct opregion_asle_ext) != 0x400);
979a05eeebfSFrançois Tigeot 
9801487f786SFrançois Tigeot 	pci_read_config_dword(pdev, ASLS, &asls);
98100640ec9SFrançois Tigeot 	DRM_DEBUG_DRIVER("graphic opregion physical addr: 0x%x\n", asls);
982e3adcf8fSFrançois Tigeot 	if (asls == 0) {
98300640ec9SFrançois Tigeot 		DRM_DEBUG_DRIVER("ACPI OpRegion not supported!\n");
984a85cb24fSFrançois Tigeot 		return -ENOTSUPP;
985e3adcf8fSFrançois Tigeot 	}
986e3adcf8fSFrançois Tigeot 
9879edbd4a0SFrançois Tigeot 	INIT_WORK(&opregion->asle_work, asle_work);
9889edbd4a0SFrançois Tigeot 
989e3adcf8fSFrançois Tigeot 	base = (void *)pmap_mapbios(asls, OPREGION_SIZE);
990e3adcf8fSFrançois Tigeot 	if (!base)
991e3adcf8fSFrançois Tigeot 		return -ENOMEM;
992e3adcf8fSFrançois Tigeot 
993352ff8bdSFrançois Tigeot 	memcpy(buf, base, sizeof(buf));
99400640ec9SFrançois Tigeot 
99500640ec9SFrançois Tigeot 	if (memcmp(buf, OPREGION_SIGNATURE, 16)) {
99600640ec9SFrançois Tigeot 		DRM_DEBUG_DRIVER("opregion signature mismatch\n");
997e3adcf8fSFrançois Tigeot 		err = -EINVAL;
998e3adcf8fSFrançois Tigeot 		goto err_out;
999e3adcf8fSFrançois Tigeot 	}
1000a85cb24fSFrançois Tigeot 	opregion->header = base;
1001a85cb24fSFrançois Tigeot 	opregion->lid_state = base + ACPI_CLID;
1002e3adcf8fSFrançois Tigeot 
1003352ff8bdSFrançois Tigeot 	mboxes = opregion->header->mboxes;
1004e3adcf8fSFrançois Tigeot 	if (mboxes & MBOX_ACPI) {
100500640ec9SFrançois Tigeot 		DRM_DEBUG_DRIVER("Public ACPI methods supported\n");
1006a85cb24fSFrançois Tigeot 		opregion->acpi = base + OPREGION_ACPI_OFFSET;
1007e3adcf8fSFrançois Tigeot 	}
1008e3adcf8fSFrançois Tigeot 
1009e3adcf8fSFrançois Tigeot 	if (mboxes & MBOX_SWSCI) {
101000640ec9SFrançois Tigeot 		DRM_DEBUG_DRIVER("SWSCI supported\n");
1011a85cb24fSFrançois Tigeot 		opregion->swsci = base + OPREGION_SWSCI_OFFSET;
10121487f786SFrançois Tigeot 		swsci_setup(dev_priv);
1013e3adcf8fSFrançois Tigeot 	}
1014aee94f86SFrançois Tigeot 
1015e3adcf8fSFrançois Tigeot 	if (mboxes & MBOX_ASLE) {
101600640ec9SFrançois Tigeot 		DRM_DEBUG_DRIVER("ASLE supported\n");
1017a85cb24fSFrançois Tigeot 		opregion->asle = base + OPREGION_ASLE_OFFSET;
10185d0b1887SFrançois Tigeot 
1019352ff8bdSFrançois Tigeot 		opregion->asle->ardy = ASLE_ARDY_NOT_READY;
1020e3adcf8fSFrançois Tigeot 	}
1021e3adcf8fSFrançois Tigeot 
1022aee94f86SFrançois Tigeot 	if (mboxes & MBOX_ASLE_EXT)
1023aee94f86SFrançois Tigeot 		DRM_DEBUG_DRIVER("ASLE extension supported\n");
1024aee94f86SFrançois Tigeot 
1025*3f2dd94aSFrançois Tigeot 	if (intel_load_vbt_firmware(dev_priv) == 0)
1026*3f2dd94aSFrançois Tigeot 		goto out;
1027*3f2dd94aSFrançois Tigeot 
1028a85cb24fSFrançois Tigeot 	if (dmi_check_system(intel_no_opregion_vbt))
1029a85cb24fSFrançois Tigeot 		goto out;
1030aee94f86SFrançois Tigeot 
1031aee94f86SFrançois Tigeot 	if (opregion->header->opregion_ver >= 2 && opregion->asle &&
1032aee94f86SFrançois Tigeot 	    opregion->asle->rvda && opregion->asle->rvds) {
1033aee94f86SFrançois Tigeot 		opregion->rvda = pmap_mapbios(opregion->asle->rvda,
1034aee94f86SFrançois Tigeot 					  opregion->asle->rvds);
1035aee94f86SFrançois Tigeot 		vbt = opregion->rvda;
1036aee94f86SFrançois Tigeot 		vbt_size = opregion->asle->rvds;
1037aee94f86SFrançois Tigeot 		if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
1038aee94f86SFrançois Tigeot 			DRM_DEBUG_KMS("Found valid VBT in ACPI OpRegion (RVDA)\n");
1039aee94f86SFrançois Tigeot 			opregion->vbt = vbt;
1040aee94f86SFrançois Tigeot 			opregion->vbt_size = vbt_size;
1041a85cb24fSFrançois Tigeot 			goto out;
1042aee94f86SFrançois Tigeot 		} else {
1043a85cb24fSFrançois Tigeot 			DRM_DEBUG_KMS("Invalid VBT in ACPI OpRegion (RVDA)\n");
1044a85cb24fSFrançois Tigeot 		}
1045a85cb24fSFrançois Tigeot 	}
1046a85cb24fSFrançois Tigeot 
1047aee94f86SFrançois Tigeot 	vbt = base + OPREGION_VBT_OFFSET;
10484be47400SFrançois Tigeot 	/*
1049a85cb24fSFrançois Tigeot 	 * The VBT specification says that if the ASLE ext mailbox is not used
1050a85cb24fSFrançois Tigeot 	 * its area is reserved, but on some CHT boards the VBT extends into the
1051a85cb24fSFrançois Tigeot 	 * ASLE ext area. Allow this even though it is against the spec, so we
1052a85cb24fSFrançois Tigeot 	 * do not end up rejecting the VBT on those boards (and end up not
1053a85cb24fSFrançois Tigeot 	 * finding the LCD panel because of this).
10544be47400SFrançois Tigeot 	 */
10554be47400SFrançois Tigeot 	vbt_size = (mboxes & MBOX_ASLE_EXT) ?
10564be47400SFrançois Tigeot 		OPREGION_ASLE_EXT_OFFSET : OPREGION_SIZE;
10574be47400SFrançois Tigeot 	vbt_size -= OPREGION_VBT_OFFSET;
1058aee94f86SFrançois Tigeot 	if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
1059aee94f86SFrançois Tigeot 		DRM_DEBUG_KMS("Found valid VBT in ACPI OpRegion (Mailbox #4)\n");
1060aee94f86SFrançois Tigeot 		opregion->vbt = vbt;
1061aee94f86SFrançois Tigeot 		opregion->vbt_size = vbt_size;
1062a85cb24fSFrançois Tigeot 	} else {
1063a85cb24fSFrançois Tigeot 		DRM_DEBUG_KMS("Invalid VBT in ACPI OpRegion (Mailbox #4)\n");
1064aee94f86SFrançois Tigeot 	}
1065aee94f86SFrançois Tigeot 
1066a85cb24fSFrançois Tigeot out:
1067e3adcf8fSFrançois Tigeot 	return 0;
1068e3adcf8fSFrançois Tigeot 
1069e3adcf8fSFrançois Tigeot err_out:
1070146a3843SFrançois Tigeot 	iounmap(base);
1071e3adcf8fSFrançois Tigeot 	return err;
1072e3adcf8fSFrançois Tigeot }
10738621f407SFrançois Tigeot 
intel_use_opregion_panel_type_callback(const struct dmi_system_id * id)10748621f407SFrançois Tigeot static int intel_use_opregion_panel_type_callback(const struct dmi_system_id *id)
10758621f407SFrançois Tigeot {
10768621f407SFrançois Tigeot 	DRM_INFO("Using panel type from OpRegion on %s\n", id->ident);
10778621f407SFrançois Tigeot 	return 1;
10788621f407SFrançois Tigeot }
10798621f407SFrançois Tigeot 
10808621f407SFrançois Tigeot static const struct dmi_system_id intel_use_opregion_panel_type[] = {
10818621f407SFrançois Tigeot 	{
10828621f407SFrançois Tigeot 		.callback = intel_use_opregion_panel_type_callback,
10838621f407SFrançois Tigeot 		.ident = "Conrac GmbH IX45GM2",
10848621f407SFrançois Tigeot 		.matches = {DMI_MATCH(DMI_SYS_VENDOR, "Conrac GmbH"),
10858621f407SFrançois Tigeot 			    DMI_MATCH(DMI_PRODUCT_NAME, "IX45GM2"),
10868621f407SFrançois Tigeot 		},
10878621f407SFrançois Tigeot 	},
10888621f407SFrançois Tigeot 	{ }
10898621f407SFrançois Tigeot };
10908621f407SFrançois Tigeot 
10918621f407SFrançois Tigeot int
intel_opregion_get_panel_type(struct drm_i915_private * dev_priv)10921487f786SFrançois Tigeot intel_opregion_get_panel_type(struct drm_i915_private *dev_priv)
10938621f407SFrançois Tigeot {
10948621f407SFrançois Tigeot 	u32 panel_details;
10958621f407SFrançois Tigeot 	int ret;
10968621f407SFrançois Tigeot 
10971487f786SFrançois Tigeot 	ret = swsci(dev_priv, SWSCI_GBDA_PANEL_DETAILS, 0x0, &panel_details);
10988621f407SFrançois Tigeot 	if (ret) {
10998621f407SFrançois Tigeot 		DRM_DEBUG_KMS("Failed to get panel details from OpRegion (%d)\n",
11008621f407SFrançois Tigeot 			      ret);
11018621f407SFrançois Tigeot 		return ret;
11028621f407SFrançois Tigeot 	}
11038621f407SFrançois Tigeot 
11048621f407SFrançois Tigeot 	ret = (panel_details >> 8) & 0xff;
11058621f407SFrançois Tigeot 	if (ret > 0x10) {
11068621f407SFrançois Tigeot 		DRM_DEBUG_KMS("Invalid OpRegion panel type 0x%x\n", ret);
11078621f407SFrançois Tigeot 		return -EINVAL;
11088621f407SFrançois Tigeot 	}
11098621f407SFrançois Tigeot 
11108621f407SFrançois Tigeot 	/* fall back to VBT panel type? */
11118621f407SFrançois Tigeot 	if (ret == 0x0) {
11128621f407SFrançois Tigeot 		DRM_DEBUG_KMS("No panel type in OpRegion\n");
11138621f407SFrançois Tigeot 		return -ENODEV;
11148621f407SFrançois Tigeot 	}
11158621f407SFrançois Tigeot 
11168621f407SFrançois Tigeot 	/*
11178621f407SFrançois Tigeot 	 * So far we know that some machined must use it, others must not use it.
11188621f407SFrançois Tigeot 	 * There doesn't seem to be any way to determine which way to go, except
11198621f407SFrançois Tigeot 	 * via a quirk list :(
11208621f407SFrançois Tigeot 	 */
11218621f407SFrançois Tigeot 	if (!dmi_check_system(intel_use_opregion_panel_type)) {
11228621f407SFrançois Tigeot 		DRM_DEBUG_KMS("Ignoring OpRegion panel type (%d)\n", ret - 1);
11238621f407SFrançois Tigeot 		return -ENODEV;
11248621f407SFrançois Tigeot 	}
11258621f407SFrançois Tigeot 
11268621f407SFrançois Tigeot 	return ret - 1;
11278621f407SFrançois Tigeot }
1128