xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_opregion.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1df0566a6SJani Nikula /*
2df0566a6SJani Nikula  * Copyright 2008 Intel Corporation <hong.liu@intel.com>
3df0566a6SJani Nikula  * Copyright 2008 Red Hat <mjg@redhat.com>
4df0566a6SJani Nikula  *
5df0566a6SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining
6df0566a6SJani Nikula  * a copy of this software and associated documentation files (the
7df0566a6SJani Nikula  * "Software"), to deal in the Software without restriction, including
8df0566a6SJani Nikula  * without limitation the rights to use, copy, modify, merge, publish,
9df0566a6SJani Nikula  * distribute, sub license, and/or sell copies of the Software, and to
10df0566a6SJani Nikula  * permit persons to whom the Software is furnished to do so, subject to
11df0566a6SJani Nikula  * the following conditions:
12df0566a6SJani Nikula  *
13df0566a6SJani Nikula  * The above copyright notice and this permission notice (including the
14df0566a6SJani Nikula  * next paragraph) shall be included in all copies or substantial
15df0566a6SJani Nikula  * portions of the Software.
16df0566a6SJani Nikula  *
17df0566a6SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18df0566a6SJani Nikula  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19df0566a6SJani Nikula  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20df0566a6SJani Nikula  * NON-INFRINGEMENT.  IN NO EVENT SHALL INTEL AND/OR ITS SUPPLIERS BE
21df0566a6SJani Nikula  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22df0566a6SJani Nikula  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23df0566a6SJani Nikula  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24df0566a6SJani Nikula  * SOFTWARE.
25df0566a6SJani Nikula  *
26df0566a6SJani Nikula  */
27df0566a6SJani Nikula 
28df0566a6SJani Nikula #include <linux/acpi.h>
29df0566a6SJani Nikula #include <linux/dmi.h>
30df0566a6SJani Nikula #include <linux/firmware.h>
31df0566a6SJani Nikula #include <acpi/video.h>
32df0566a6SJani Nikula 
33255490f9SVille Syrjälä #include <drm/drm_edid.h>
34255490f9SVille Syrjälä 
35df0566a6SJani Nikula #include "i915_drv.h"
365fb44306SRajat Jain #include "intel_acpi.h"
376cc42fbeSJani Nikula #include "intel_backlight.h"
381d455f8dSJani Nikula #include "intel_display_types.h"
39df0566a6SJani Nikula #include "intel_opregion.h"
407e470f10SJani Nikula #include "intel_pci_config.h"
41df0566a6SJani Nikula 
42df0566a6SJani Nikula #define OPREGION_HEADER_OFFSET 0
43df0566a6SJani Nikula #define OPREGION_ACPI_OFFSET   0x100
44df0566a6SJani Nikula #define   ACPI_CLID 0x01ac /* current lid state indicator */
45df0566a6SJani Nikula #define   ACPI_CDCK 0x01b0 /* current docking state indicator */
46df0566a6SJani Nikula #define OPREGION_SWSCI_OFFSET  0x200
47df0566a6SJani Nikula #define OPREGION_ASLE_OFFSET   0x300
48df0566a6SJani Nikula #define OPREGION_VBT_OFFSET    0x400
49df0566a6SJani Nikula #define OPREGION_ASLE_EXT_OFFSET	0x1C00
50df0566a6SJani Nikula 
51df0566a6SJani Nikula #define OPREGION_SIGNATURE "IntelGraphicsMem"
52a391e069SJani Nikula #define MBOX_ACPI		BIT(0)	/* Mailbox #1 */
53a391e069SJani Nikula #define MBOX_SWSCI		BIT(1)	/* Mailbox #2 (obsolete from v2.x) */
54a391e069SJani Nikula #define MBOX_ASLE		BIT(2)	/* Mailbox #3 */
55a391e069SJani Nikula #define MBOX_ASLE_EXT		BIT(4)	/* Mailbox #5 */
56a391e069SJani Nikula #define MBOX_BACKLIGHT		BIT(5)	/* Mailbox #2 (valid from v3.x) */
57df0566a6SJani Nikula 
58eb2983c0SJouni Högander #define PCON_HEADLESS_SKU	BIT(13)
59eb2983c0SJouni Högander 
60df0566a6SJani Nikula struct opregion_header {
61df0566a6SJani Nikula 	u8 signature[16];
62df0566a6SJani Nikula 	u32 size;
63df0566a6SJani Nikula 	struct {
64df0566a6SJani Nikula 		u8 rsvd;
65df0566a6SJani Nikula 		u8 revision;
66df0566a6SJani Nikula 		u8 minor;
67df0566a6SJani Nikula 		u8 major;
68df0566a6SJani Nikula 	}  __packed over;
69df0566a6SJani Nikula 	u8 bios_ver[32];
70df0566a6SJani Nikula 	u8 vbios_ver[16];
71df0566a6SJani Nikula 	u8 driver_ver[16];
72df0566a6SJani Nikula 	u32 mboxes;
73df0566a6SJani Nikula 	u32 driver_model;
74df0566a6SJani Nikula 	u32 pcon;
75df0566a6SJani Nikula 	u8 dver[32];
76df0566a6SJani Nikula 	u8 rsvd[124];
77df0566a6SJani Nikula } __packed;
78df0566a6SJani Nikula 
79df0566a6SJani Nikula /* OpRegion mailbox #1: public ACPI methods */
80df0566a6SJani Nikula struct opregion_acpi {
81df0566a6SJani Nikula 	u32 drdy;       /* driver readiness */
82df0566a6SJani Nikula 	u32 csts;       /* notification status */
83df0566a6SJani Nikula 	u32 cevt;       /* current event */
84df0566a6SJani Nikula 	u8 rsvd1[20];
85df0566a6SJani Nikula 	u32 didl[8];    /* supported display devices ID list */
86df0566a6SJani Nikula 	u32 cpdl[8];    /* currently presented display list */
87df0566a6SJani Nikula 	u32 cadl[8];    /* currently active display list */
88df0566a6SJani Nikula 	u32 nadl[8];    /* next active devices list */
89df0566a6SJani Nikula 	u32 aslp;       /* ASL sleep time-out */
90df0566a6SJani Nikula 	u32 tidx;       /* toggle table index */
91df0566a6SJani Nikula 	u32 chpd;       /* current hotplug enable indicator */
92df0566a6SJani Nikula 	u32 clid;       /* current lid state*/
93df0566a6SJani Nikula 	u32 cdck;       /* current docking state */
94df0566a6SJani Nikula 	u32 sxsw;       /* Sx state resume */
95df0566a6SJani Nikula 	u32 evts;       /* ASL supported events */
96df0566a6SJani Nikula 	u32 cnot;       /* current OS notification */
97df0566a6SJani Nikula 	u32 nrdy;       /* driver status */
98df0566a6SJani Nikula 	u32 did2[7];	/* extended supported display devices ID list */
99df0566a6SJani Nikula 	u32 cpd2[7];	/* extended attached display devices list */
100df0566a6SJani Nikula 	u8 rsvd2[4];
101df0566a6SJani Nikula } __packed;
102df0566a6SJani Nikula 
103df0566a6SJani Nikula /* OpRegion mailbox #2: SWSCI */
104df0566a6SJani Nikula struct opregion_swsci {
105df0566a6SJani Nikula 	u32 scic;       /* SWSCI command|status|data */
106df0566a6SJani Nikula 	u32 parm;       /* command parameters */
107df0566a6SJani Nikula 	u32 dslp;       /* driver sleep time-out */
108df0566a6SJani Nikula 	u8 rsvd[244];
109df0566a6SJani Nikula } __packed;
110df0566a6SJani Nikula 
111df0566a6SJani Nikula /* OpRegion mailbox #3: ASLE */
112df0566a6SJani Nikula struct opregion_asle {
113df0566a6SJani Nikula 	u32 ardy;       /* driver readiness */
114df0566a6SJani Nikula 	u32 aslc;       /* ASLE interrupt command */
115df0566a6SJani Nikula 	u32 tche;       /* technology enabled indicator */
116df0566a6SJani Nikula 	u32 alsi;       /* current ALS illuminance reading */
117df0566a6SJani Nikula 	u32 bclp;       /* backlight brightness to set */
118df0566a6SJani Nikula 	u32 pfit;       /* panel fitting state */
119df0566a6SJani Nikula 	u32 cblv;       /* current brightness level */
120df0566a6SJani Nikula 	u16 bclm[20];   /* backlight level duty cycle mapping table */
121df0566a6SJani Nikula 	u32 cpfm;       /* current panel fitting mode */
122df0566a6SJani Nikula 	u32 epfm;       /* enabled panel fitting modes */
123df0566a6SJani Nikula 	u8 plut[74];    /* panel LUT and identifier */
124df0566a6SJani Nikula 	u32 pfmb;       /* PWM freq and min brightness */
125df0566a6SJani Nikula 	u32 cddv;       /* color correction default values */
126df0566a6SJani Nikula 	u32 pcft;       /* power conservation features */
127df0566a6SJani Nikula 	u32 srot;       /* supported rotation angles */
128df0566a6SJani Nikula 	u32 iuer;       /* IUER events */
129df0566a6SJani Nikula 	u64 fdss;
130df0566a6SJani Nikula 	u32 fdsp;
131df0566a6SJani Nikula 	u32 stat;
132df0566a6SJani Nikula 	u64 rvda;	/* Physical (2.0) or relative from opregion (2.1+)
133df0566a6SJani Nikula 			 * address of raw VBT data. */
134df0566a6SJani Nikula 	u32 rvds;	/* Size of raw vbt data */
135df0566a6SJani Nikula 	u8 rsvd[58];
136df0566a6SJani Nikula } __packed;
137df0566a6SJani Nikula 
138df0566a6SJani Nikula /* OpRegion mailbox #5: ASLE ext */
139df0566a6SJani Nikula struct opregion_asle_ext {
140df0566a6SJani Nikula 	u32 phed;	/* Panel Header */
141df0566a6SJani Nikula 	u8 bddc[256];	/* Panel EDID */
142df0566a6SJani Nikula 	u8 rsvd[764];
143df0566a6SJani Nikula } __packed;
144df0566a6SJani Nikula 
145df0566a6SJani Nikula /* Driver readiness indicator */
146df0566a6SJani Nikula #define ASLE_ARDY_READY		(1 << 0)
147df0566a6SJani Nikula #define ASLE_ARDY_NOT_READY	(0 << 0)
148df0566a6SJani Nikula 
149df0566a6SJani Nikula /* ASLE Interrupt Command (ASLC) bits */
150df0566a6SJani Nikula #define ASLC_SET_ALS_ILLUM		(1 << 0)
151df0566a6SJani Nikula #define ASLC_SET_BACKLIGHT		(1 << 1)
152df0566a6SJani Nikula #define ASLC_SET_PFIT			(1 << 2)
153df0566a6SJani Nikula #define ASLC_SET_PWM_FREQ		(1 << 3)
154df0566a6SJani Nikula #define ASLC_SUPPORTED_ROTATION_ANGLES	(1 << 4)
155df0566a6SJani Nikula #define ASLC_BUTTON_ARRAY		(1 << 5)
156df0566a6SJani Nikula #define ASLC_CONVERTIBLE_INDICATOR	(1 << 6)
157df0566a6SJani Nikula #define ASLC_DOCKING_INDICATOR		(1 << 7)
158df0566a6SJani Nikula #define ASLC_ISCT_STATE_CHANGE		(1 << 8)
159df0566a6SJani Nikula #define ASLC_REQ_MSK			0x1ff
160df0566a6SJani Nikula /* response bits */
161df0566a6SJani Nikula #define ASLC_ALS_ILLUM_FAILED		(1 << 10)
162df0566a6SJani Nikula #define ASLC_BACKLIGHT_FAILED		(1 << 12)
163df0566a6SJani Nikula #define ASLC_PFIT_FAILED		(1 << 14)
164df0566a6SJani Nikula #define ASLC_PWM_FREQ_FAILED		(1 << 16)
165df0566a6SJani Nikula #define ASLC_ROTATION_ANGLES_FAILED	(1 << 18)
166df0566a6SJani Nikula #define ASLC_BUTTON_ARRAY_FAILED	(1 << 20)
167df0566a6SJani Nikula #define ASLC_CONVERTIBLE_FAILED		(1 << 22)
168df0566a6SJani Nikula #define ASLC_DOCKING_FAILED		(1 << 24)
169df0566a6SJani Nikula #define ASLC_ISCT_STATE_FAILED		(1 << 26)
170df0566a6SJani Nikula 
171df0566a6SJani Nikula /* Technology enabled indicator */
172df0566a6SJani Nikula #define ASLE_TCHE_ALS_EN	(1 << 0)
173df0566a6SJani Nikula #define ASLE_TCHE_BLC_EN	(1 << 1)
174df0566a6SJani Nikula #define ASLE_TCHE_PFIT_EN	(1 << 2)
175df0566a6SJani Nikula #define ASLE_TCHE_PFMB_EN	(1 << 3)
176df0566a6SJani Nikula 
177df0566a6SJani Nikula /* ASLE backlight brightness to set */
178df0566a6SJani Nikula #define ASLE_BCLP_VALID                (1<<31)
179df0566a6SJani Nikula #define ASLE_BCLP_MSK          (~(1<<31))
180df0566a6SJani Nikula 
181df0566a6SJani Nikula /* ASLE panel fitting request */
182df0566a6SJani Nikula #define ASLE_PFIT_VALID         (1<<31)
183df0566a6SJani Nikula #define ASLE_PFIT_CENTER (1<<0)
184df0566a6SJani Nikula #define ASLE_PFIT_STRETCH_TEXT (1<<1)
185df0566a6SJani Nikula #define ASLE_PFIT_STRETCH_GFX (1<<2)
186df0566a6SJani Nikula 
187df0566a6SJani Nikula /* PWM frequency and minimum brightness */
188df0566a6SJani Nikula #define ASLE_PFMB_BRIGHTNESS_MASK (0xff)
189df0566a6SJani Nikula #define ASLE_PFMB_BRIGHTNESS_VALID (1<<8)
190df0566a6SJani Nikula #define ASLE_PFMB_PWM_MASK (0x7ffffe00)
191df0566a6SJani Nikula #define ASLE_PFMB_PWM_VALID (1<<31)
192df0566a6SJani Nikula 
193df0566a6SJani Nikula #define ASLE_CBLV_VALID         (1<<31)
194df0566a6SJani Nikula 
195df0566a6SJani Nikula /* IUER */
196df0566a6SJani Nikula #define ASLE_IUER_DOCKING		(1 << 7)
197df0566a6SJani Nikula #define ASLE_IUER_CONVERTIBLE		(1 << 6)
198df0566a6SJani Nikula #define ASLE_IUER_ROTATION_LOCK_BTN	(1 << 4)
199df0566a6SJani Nikula #define ASLE_IUER_VOLUME_DOWN_BTN	(1 << 3)
200df0566a6SJani Nikula #define ASLE_IUER_VOLUME_UP_BTN		(1 << 2)
201df0566a6SJani Nikula #define ASLE_IUER_WINDOWS_BTN		(1 << 1)
202df0566a6SJani Nikula #define ASLE_IUER_POWER_BTN		(1 << 0)
203df0566a6SJani Nikula 
204e35d8762SAnisse Astier #define ASLE_PHED_EDID_VALID_MASK	0x3
205e35d8762SAnisse Astier 
206df0566a6SJani Nikula /* Software System Control Interrupt (SWSCI) */
207df0566a6SJani Nikula #define SWSCI_SCIC_INDICATOR		(1 << 0)
208df0566a6SJani Nikula #define SWSCI_SCIC_MAIN_FUNCTION_SHIFT	1
209df0566a6SJani Nikula #define SWSCI_SCIC_MAIN_FUNCTION_MASK	(0xf << 1)
210df0566a6SJani Nikula #define SWSCI_SCIC_SUB_FUNCTION_SHIFT	8
211df0566a6SJani Nikula #define SWSCI_SCIC_SUB_FUNCTION_MASK	(0xff << 8)
212df0566a6SJani Nikula #define SWSCI_SCIC_EXIT_PARAMETER_SHIFT	8
213df0566a6SJani Nikula #define SWSCI_SCIC_EXIT_PARAMETER_MASK	(0xff << 8)
214df0566a6SJani Nikula #define SWSCI_SCIC_EXIT_STATUS_SHIFT	5
215df0566a6SJani Nikula #define SWSCI_SCIC_EXIT_STATUS_MASK	(7 << 5)
216df0566a6SJani Nikula #define SWSCI_SCIC_EXIT_STATUS_SUCCESS	1
217df0566a6SJani Nikula 
218df0566a6SJani Nikula #define SWSCI_FUNCTION_CODE(main, sub) \
219df0566a6SJani Nikula 	((main) << SWSCI_SCIC_MAIN_FUNCTION_SHIFT | \
220df0566a6SJani Nikula 	 (sub) << SWSCI_SCIC_SUB_FUNCTION_SHIFT)
221df0566a6SJani Nikula 
222df0566a6SJani Nikula /* SWSCI: Get BIOS Data (GBDA) */
223df0566a6SJani Nikula #define SWSCI_GBDA			4
224df0566a6SJani Nikula #define SWSCI_GBDA_SUPPORTED_CALLS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 0)
225df0566a6SJani Nikula #define SWSCI_GBDA_REQUESTED_CALLBACKS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 1)
226df0566a6SJani Nikula #define SWSCI_GBDA_BOOT_DISPLAY_PREF	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 4)
227df0566a6SJani Nikula #define SWSCI_GBDA_PANEL_DETAILS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 5)
228df0566a6SJani Nikula #define SWSCI_GBDA_TV_STANDARD		SWSCI_FUNCTION_CODE(SWSCI_GBDA, 6)
229df0566a6SJani Nikula #define SWSCI_GBDA_INTERNAL_GRAPHICS	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 7)
230df0566a6SJani Nikula #define SWSCI_GBDA_SPREAD_SPECTRUM	SWSCI_FUNCTION_CODE(SWSCI_GBDA, 10)
231df0566a6SJani Nikula 
232df0566a6SJani Nikula /* SWSCI: System BIOS Callbacks (SBCB) */
233df0566a6SJani Nikula #define SWSCI_SBCB			6
234df0566a6SJani Nikula #define SWSCI_SBCB_SUPPORTED_CALLBACKS	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 0)
235df0566a6SJani Nikula #define SWSCI_SBCB_INIT_COMPLETION	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 1)
236df0566a6SJani Nikula #define SWSCI_SBCB_PRE_HIRES_SET_MODE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 3)
237df0566a6SJani Nikula #define SWSCI_SBCB_POST_HIRES_SET_MODE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 4)
238df0566a6SJani Nikula #define SWSCI_SBCB_DISPLAY_SWITCH	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 5)
239df0566a6SJani Nikula #define SWSCI_SBCB_SET_TV_FORMAT	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 6)
240df0566a6SJani Nikula #define SWSCI_SBCB_ADAPTER_POWER_STATE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 7)
241df0566a6SJani Nikula #define SWSCI_SBCB_DISPLAY_POWER_STATE	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 8)
242df0566a6SJani Nikula #define SWSCI_SBCB_SET_BOOT_DISPLAY	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 9)
243df0566a6SJani Nikula #define SWSCI_SBCB_SET_PANEL_DETAILS	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 10)
244df0566a6SJani Nikula #define SWSCI_SBCB_SET_INTERNAL_GFX	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 11)
245df0566a6SJani Nikula #define SWSCI_SBCB_POST_HIRES_TO_DOS_FS	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 16)
246df0566a6SJani Nikula #define SWSCI_SBCB_SUSPEND_RESUME	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 17)
247df0566a6SJani Nikula #define SWSCI_SBCB_SET_SPREAD_SPECTRUM	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 18)
248df0566a6SJani Nikula #define SWSCI_SBCB_POST_VBE_PM		SWSCI_FUNCTION_CODE(SWSCI_SBCB, 19)
249df0566a6SJani Nikula #define SWSCI_SBCB_ENABLE_DISABLE_AUDIO	SWSCI_FUNCTION_CODE(SWSCI_SBCB, 21)
250df0566a6SJani Nikula 
251df0566a6SJani Nikula #define MAX_DSLP	1500
252df0566a6SJani Nikula 
check_swsci_function(struct drm_i915_private * i915,u32 function)25373144c0cSJani Nikula static int check_swsci_function(struct drm_i915_private *i915, u32 function)
254df0566a6SJani Nikula {
2557249dfcbSJani Nikula 	struct opregion_swsci *swsci = i915->display.opregion.swsci;
25673144c0cSJani Nikula 	u32 main_function, sub_function;
257df0566a6SJani Nikula 
258df0566a6SJani Nikula 	if (!swsci)
259df0566a6SJani Nikula 		return -ENODEV;
260df0566a6SJani Nikula 
261df0566a6SJani Nikula 	main_function = (function & SWSCI_SCIC_MAIN_FUNCTION_MASK) >>
262df0566a6SJani Nikula 		SWSCI_SCIC_MAIN_FUNCTION_SHIFT;
263df0566a6SJani Nikula 	sub_function = (function & SWSCI_SCIC_SUB_FUNCTION_MASK) >>
264df0566a6SJani Nikula 		SWSCI_SCIC_SUB_FUNCTION_SHIFT;
265df0566a6SJani Nikula 
266df0566a6SJani Nikula 	/* Check if we can call the function. See swsci_setup for details. */
267df0566a6SJani Nikula 	if (main_function == SWSCI_SBCB) {
2687249dfcbSJani Nikula 		if ((i915->display.opregion.swsci_sbcb_sub_functions &
269df0566a6SJani Nikula 		     (1 << sub_function)) == 0)
270df0566a6SJani Nikula 			return -EINVAL;
271df0566a6SJani Nikula 	} else if (main_function == SWSCI_GBDA) {
2727249dfcbSJani Nikula 		if ((i915->display.opregion.swsci_gbda_sub_functions &
273df0566a6SJani Nikula 		     (1 << sub_function)) == 0)
274df0566a6SJani Nikula 			return -EINVAL;
275df0566a6SJani Nikula 	}
276df0566a6SJani Nikula 
27773144c0cSJani Nikula 	return 0;
27873144c0cSJani Nikula }
27973144c0cSJani Nikula 
swsci(struct drm_i915_private * dev_priv,u32 function,u32 parm,u32 * parm_out)28073144c0cSJani Nikula static int swsci(struct drm_i915_private *dev_priv,
28173144c0cSJani Nikula 		 u32 function, u32 parm, u32 *parm_out)
28273144c0cSJani Nikula {
2837249dfcbSJani Nikula 	struct opregion_swsci *swsci = dev_priv->display.opregion.swsci;
28473144c0cSJani Nikula 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
28573144c0cSJani Nikula 	u32 scic, dslp;
28673144c0cSJani Nikula 	u16 swsci_val;
28773144c0cSJani Nikula 	int ret;
28873144c0cSJani Nikula 
28973144c0cSJani Nikula 	ret = check_swsci_function(dev_priv, function);
29073144c0cSJani Nikula 	if (ret)
29173144c0cSJani Nikula 		return ret;
29273144c0cSJani Nikula 
293df0566a6SJani Nikula 	/* Driver sleep timeout in ms. */
294df0566a6SJani Nikula 	dslp = swsci->dslp;
295df0566a6SJani Nikula 	if (!dslp) {
296df0566a6SJani Nikula 		/* The spec says 2ms should be the default, but it's too small
297df0566a6SJani Nikula 		 * for some machines. */
298df0566a6SJani Nikula 		dslp = 50;
299df0566a6SJani Nikula 	} else if (dslp > MAX_DSLP) {
300df0566a6SJani Nikula 		/* Hey bios, trust must be earned. */
301df0566a6SJani Nikula 		DRM_INFO_ONCE("ACPI BIOS requests an excessive sleep of %u ms, "
302df0566a6SJani Nikula 			      "using %u ms instead\n", dslp, MAX_DSLP);
303df0566a6SJani Nikula 		dslp = MAX_DSLP;
304df0566a6SJani Nikula 	}
305df0566a6SJani Nikula 
306df0566a6SJani Nikula 	/* The spec tells us to do this, but we are the only user... */
307df0566a6SJani Nikula 	scic = swsci->scic;
308df0566a6SJani Nikula 	if (scic & SWSCI_SCIC_INDICATOR) {
3095cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm, "SWSCI request already in progress\n");
310df0566a6SJani Nikula 		return -EBUSY;
311df0566a6SJani Nikula 	}
312df0566a6SJani Nikula 
313df0566a6SJani Nikula 	scic = function | SWSCI_SCIC_INDICATOR;
314df0566a6SJani Nikula 
315df0566a6SJani Nikula 	swsci->parm = parm;
316df0566a6SJani Nikula 	swsci->scic = scic;
317df0566a6SJani Nikula 
318df0566a6SJani Nikula 	/* Ensure SCI event is selected and event trigger is cleared. */
319df0566a6SJani Nikula 	pci_read_config_word(pdev, SWSCI, &swsci_val);
320df0566a6SJani Nikula 	if (!(swsci_val & SWSCI_SCISEL) || (swsci_val & SWSCI_GSSCIE)) {
321df0566a6SJani Nikula 		swsci_val |= SWSCI_SCISEL;
322df0566a6SJani Nikula 		swsci_val &= ~SWSCI_GSSCIE;
323df0566a6SJani Nikula 		pci_write_config_word(pdev, SWSCI, swsci_val);
324df0566a6SJani Nikula 	}
325df0566a6SJani Nikula 
326df0566a6SJani Nikula 	/* Use event trigger to tell bios to check the mail. */
327df0566a6SJani Nikula 	swsci_val |= SWSCI_GSSCIE;
328df0566a6SJani Nikula 	pci_write_config_word(pdev, SWSCI, swsci_val);
329df0566a6SJani Nikula 
330df0566a6SJani Nikula 	/* Poll for the result. */
331df0566a6SJani Nikula #define C (((scic = swsci->scic) & SWSCI_SCIC_INDICATOR) == 0)
332df0566a6SJani Nikula 	if (wait_for(C, dslp)) {
3335cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm, "SWSCI request timed out\n");
334df0566a6SJani Nikula 		return -ETIMEDOUT;
335df0566a6SJani Nikula 	}
336df0566a6SJani Nikula 
337df0566a6SJani Nikula 	scic = (scic & SWSCI_SCIC_EXIT_STATUS_MASK) >>
338df0566a6SJani Nikula 		SWSCI_SCIC_EXIT_STATUS_SHIFT;
339df0566a6SJani Nikula 
340df0566a6SJani Nikula 	/* Note: scic == 0 is an error! */
341df0566a6SJani Nikula 	if (scic != SWSCI_SCIC_EXIT_STATUS_SUCCESS) {
3425cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm, "SWSCI request error %u\n", scic);
343df0566a6SJani Nikula 		return -EIO;
344df0566a6SJani Nikula 	}
345df0566a6SJani Nikula 
346df0566a6SJani Nikula 	if (parm_out)
347df0566a6SJani Nikula 		*parm_out = swsci->parm;
348df0566a6SJani Nikula 
349df0566a6SJani Nikula 	return 0;
350df0566a6SJani Nikula 
351df0566a6SJani Nikula #undef C
352df0566a6SJani Nikula }
353df0566a6SJani Nikula 
354df0566a6SJani Nikula #define DISPLAY_TYPE_CRT			0
355df0566a6SJani Nikula #define DISPLAY_TYPE_TV				1
356df0566a6SJani Nikula #define DISPLAY_TYPE_EXTERNAL_FLAT_PANEL	2
357df0566a6SJani Nikula #define DISPLAY_TYPE_INTERNAL_FLAT_PANEL	3
358df0566a6SJani Nikula 
intel_opregion_notify_encoder(struct intel_encoder * intel_encoder,bool enable)359df0566a6SJani Nikula int intel_opregion_notify_encoder(struct intel_encoder *intel_encoder,
360df0566a6SJani Nikula 				  bool enable)
361df0566a6SJani Nikula {
362df0566a6SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
363df0566a6SJani Nikula 	u32 parm = 0;
364df0566a6SJani Nikula 	u32 type = 0;
365df0566a6SJani Nikula 	u32 port;
36666b09dddSJani Nikula 	int ret;
367df0566a6SJani Nikula 
368df0566a6SJani Nikula 	/* don't care about old stuff for now */
369df0566a6SJani Nikula 	if (!HAS_DDI(dev_priv))
370df0566a6SJani Nikula 		return 0;
371df0566a6SJani Nikula 
37266b09dddSJani Nikula 	/* Avoid port out of bounds checks if SWSCI isn't there. */
37366b09dddSJani Nikula 	ret = check_swsci_function(dev_priv, SWSCI_SBCB_DISPLAY_POWER_STATE);
37466b09dddSJani Nikula 	if (ret)
37566b09dddSJani Nikula 		return ret;
37666b09dddSJani Nikula 
377df0566a6SJani Nikula 	if (intel_encoder->type == INTEL_OUTPUT_DSI)
378df0566a6SJani Nikula 		port = 0;
379df0566a6SJani Nikula 	else
380df0566a6SJani Nikula 		port = intel_encoder->port;
381df0566a6SJani Nikula 
382df0566a6SJani Nikula 	if (port == PORT_E)  {
383df0566a6SJani Nikula 		port = 0;
384df0566a6SJani Nikula 	} else {
385df0566a6SJani Nikula 		parm |= 1 << port;
386df0566a6SJani Nikula 		port++;
387df0566a6SJani Nikula 	}
388df0566a6SJani Nikula 
38924a644ebSJani Nikula 	/*
39024a644ebSJani Nikula 	 * The port numbering and mapping here is bizarre. The now-obsolete
39124a644ebSJani Nikula 	 * swsci spec supports ports numbered [0..4]. Port E is handled as a
39224a644ebSJani Nikula 	 * special case, but port F and beyond are not. The functionality is
39324a644ebSJani Nikula 	 * supposed to be obsolete for new platforms. Just bail out if the port
39424a644ebSJani Nikula 	 * number is out of bounds after mapping.
39524a644ebSJani Nikula 	 */
39624a644ebSJani Nikula 	if (port > 4) {
39724a644ebSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
39824a644ebSJani Nikula 			    "[ENCODER:%d:%s] port %c (index %u) out of bounds for display power state notification\n",
39924a644ebSJani Nikula 			    intel_encoder->base.base.id, intel_encoder->base.name,
40024a644ebSJani Nikula 			    port_name(intel_encoder->port), port);
40124a644ebSJani Nikula 		return -EINVAL;
40224a644ebSJani Nikula 	}
40324a644ebSJani Nikula 
404df0566a6SJani Nikula 	if (!enable)
405df0566a6SJani Nikula 		parm |= 4 << 8;
406df0566a6SJani Nikula 
407df0566a6SJani Nikula 	switch (intel_encoder->type) {
408df0566a6SJani Nikula 	case INTEL_OUTPUT_ANALOG:
409df0566a6SJani Nikula 		type = DISPLAY_TYPE_CRT;
410df0566a6SJani Nikula 		break;
411df0566a6SJani Nikula 	case INTEL_OUTPUT_DDI:
412df0566a6SJani Nikula 	case INTEL_OUTPUT_DP:
413df0566a6SJani Nikula 	case INTEL_OUTPUT_HDMI:
414df0566a6SJani Nikula 	case INTEL_OUTPUT_DP_MST:
415df0566a6SJani Nikula 		type = DISPLAY_TYPE_EXTERNAL_FLAT_PANEL;
416df0566a6SJani Nikula 		break;
417df0566a6SJani Nikula 	case INTEL_OUTPUT_EDP:
418df0566a6SJani Nikula 	case INTEL_OUTPUT_DSI:
419df0566a6SJani Nikula 		type = DISPLAY_TYPE_INTERNAL_FLAT_PANEL;
420df0566a6SJani Nikula 		break;
421df0566a6SJani Nikula 	default:
422f4224a4cSPankaj Bharadiya 		drm_WARN_ONCE(&dev_priv->drm, 1,
423f4224a4cSPankaj Bharadiya 			      "unsupported intel_encoder type %d\n",
424df0566a6SJani Nikula 			      intel_encoder->type);
425df0566a6SJani Nikula 		return -EINVAL;
426df0566a6SJani Nikula 	}
427df0566a6SJani Nikula 
428df0566a6SJani Nikula 	parm |= type << (16 + port * 3);
429df0566a6SJani Nikula 
430df0566a6SJani Nikula 	return swsci(dev_priv, SWSCI_SBCB_DISPLAY_POWER_STATE, parm, NULL);
431df0566a6SJani Nikula }
432df0566a6SJani Nikula 
433df0566a6SJani Nikula static const struct {
434df0566a6SJani Nikula 	pci_power_t pci_power_state;
435df0566a6SJani Nikula 	u32 parm;
436df0566a6SJani Nikula } power_state_map[] = {
437df0566a6SJani Nikula 	{ PCI_D0,	0x00 },
438df0566a6SJani Nikula 	{ PCI_D1,	0x01 },
439df0566a6SJani Nikula 	{ PCI_D2,	0x02 },
440df0566a6SJani Nikula 	{ PCI_D3hot,	0x04 },
441df0566a6SJani Nikula 	{ PCI_D3cold,	0x04 },
442df0566a6SJani Nikula };
443df0566a6SJani Nikula 
intel_opregion_notify_adapter(struct drm_i915_private * dev_priv,pci_power_t state)444df0566a6SJani Nikula int intel_opregion_notify_adapter(struct drm_i915_private *dev_priv,
445df0566a6SJani Nikula 				  pci_power_t state)
446df0566a6SJani Nikula {
447df0566a6SJani Nikula 	int i;
448df0566a6SJani Nikula 
449df0566a6SJani Nikula 	if (!HAS_DDI(dev_priv))
450df0566a6SJani Nikula 		return 0;
451df0566a6SJani Nikula 
452df0566a6SJani Nikula 	for (i = 0; i < ARRAY_SIZE(power_state_map); i++) {
453df0566a6SJani Nikula 		if (state == power_state_map[i].pci_power_state)
454df0566a6SJani Nikula 			return swsci(dev_priv, SWSCI_SBCB_ADAPTER_POWER_STATE,
455df0566a6SJani Nikula 				     power_state_map[i].parm, NULL);
456df0566a6SJani Nikula 	}
457df0566a6SJani Nikula 
458df0566a6SJani Nikula 	return -EINVAL;
459df0566a6SJani Nikula }
460df0566a6SJani Nikula 
asle_set_backlight(struct drm_i915_private * dev_priv,u32 bclp)461df0566a6SJani Nikula static u32 asle_set_backlight(struct drm_i915_private *dev_priv, u32 bclp)
462df0566a6SJani Nikula {
463df0566a6SJani Nikula 	struct intel_connector *connector;
464df0566a6SJani Nikula 	struct drm_connector_list_iter conn_iter;
4657249dfcbSJani Nikula 	struct opregion_asle *asle = dev_priv->display.opregion.asle;
466df0566a6SJani Nikula 
4675cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm, "bclp = 0x%08x\n", bclp);
468df0566a6SJani Nikula 
469df0566a6SJani Nikula 	if (acpi_video_get_backlight_type() == acpi_backlight_native) {
4705cb82703SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
4715cb82703SWambui Karuga 			    "opregion backlight request ignored\n");
472df0566a6SJani Nikula 		return 0;
473df0566a6SJani Nikula 	}
474df0566a6SJani Nikula 
475df0566a6SJani Nikula 	if (!(bclp & ASLE_BCLP_VALID))
476df0566a6SJani Nikula 		return ASLC_BACKLIGHT_FAILED;
477df0566a6SJani Nikula 
478df0566a6SJani Nikula 	bclp &= ASLE_BCLP_MSK;
479df0566a6SJani Nikula 	if (bclp > 255)
480df0566a6SJani Nikula 		return ASLC_BACKLIGHT_FAILED;
481df0566a6SJani Nikula 
4823703060dSAndrzej Hajda 	drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex, NULL);
483df0566a6SJani Nikula 
484df0566a6SJani Nikula 	/*
485df0566a6SJani Nikula 	 * Update backlight on all connectors that support backlight (usually
486df0566a6SJani Nikula 	 * only one).
487df0566a6SJani Nikula 	 */
4885cb82703SWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "updating opregion backlight %d/255\n",
4895cb82703SWambui Karuga 		    bclp);
4903703060dSAndrzej Hajda 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
491df0566a6SJani Nikula 	for_each_intel_connector_iter(connector, &conn_iter)
492c0a52f8bSJani Nikula 		intel_backlight_set_acpi(connector->base.state, bclp, 255);
493df0566a6SJani Nikula 	drm_connector_list_iter_end(&conn_iter);
494df0566a6SJani Nikula 	asle->cblv = DIV_ROUND_UP(bclp * 100, 255) | ASLE_CBLV_VALID;
495df0566a6SJani Nikula 
4963703060dSAndrzej Hajda 	drm_modeset_unlock(&dev_priv->drm.mode_config.connection_mutex);
497df0566a6SJani Nikula 
498df0566a6SJani Nikula 
499df0566a6SJani Nikula 	return 0;
500df0566a6SJani Nikula }
501df0566a6SJani Nikula 
asle_set_als_illum(struct drm_i915_private * dev_priv,u32 alsi)502df0566a6SJani Nikula static u32 asle_set_als_illum(struct drm_i915_private *dev_priv, u32 alsi)
503df0566a6SJani Nikula {
504df0566a6SJani Nikula 	/* alsi is the current ALS reading in lux. 0 indicates below sensor
505df0566a6SJani Nikula 	   range, 0xffff indicates above sensor range. 1-0xfffe are valid */
5065cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm, "Illum is not supported\n");
507df0566a6SJani Nikula 	return ASLC_ALS_ILLUM_FAILED;
508df0566a6SJani Nikula }
509df0566a6SJani Nikula 
asle_set_pwm_freq(struct drm_i915_private * dev_priv,u32 pfmb)510df0566a6SJani Nikula static u32 asle_set_pwm_freq(struct drm_i915_private *dev_priv, u32 pfmb)
511df0566a6SJani Nikula {
5125cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm, "PWM freq is not supported\n");
513df0566a6SJani Nikula 	return ASLC_PWM_FREQ_FAILED;
514df0566a6SJani Nikula }
515df0566a6SJani Nikula 
asle_set_pfit(struct drm_i915_private * dev_priv,u32 pfit)516df0566a6SJani Nikula static u32 asle_set_pfit(struct drm_i915_private *dev_priv, u32 pfit)
517df0566a6SJani Nikula {
518df0566a6SJani Nikula 	/* Panel fitting is currently controlled by the X code, so this is a
519df0566a6SJani Nikula 	   noop until modesetting support works fully */
5205cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm, "Pfit is not supported\n");
521df0566a6SJani Nikula 	return ASLC_PFIT_FAILED;
522df0566a6SJani Nikula }
523df0566a6SJani Nikula 
asle_set_supported_rotation_angles(struct drm_i915_private * dev_priv,u32 srot)524df0566a6SJani Nikula static u32 asle_set_supported_rotation_angles(struct drm_i915_private *dev_priv, u32 srot)
525df0566a6SJani Nikula {
5265cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm, "SROT is not supported\n");
527df0566a6SJani Nikula 	return ASLC_ROTATION_ANGLES_FAILED;
528df0566a6SJani Nikula }
529df0566a6SJani Nikula 
asle_set_button_array(struct drm_i915_private * dev_priv,u32 iuer)530df0566a6SJani Nikula static u32 asle_set_button_array(struct drm_i915_private *dev_priv, u32 iuer)
531df0566a6SJani Nikula {
532df0566a6SJani Nikula 	if (!iuer)
5335cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5345cb82703SWambui Karuga 			"Button array event is not supported (nothing)\n");
535df0566a6SJani Nikula 	if (iuer & ASLE_IUER_ROTATION_LOCK_BTN)
5365cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5375cb82703SWambui Karuga 			"Button array event is not supported (rotation lock)\n");
538df0566a6SJani Nikula 	if (iuer & ASLE_IUER_VOLUME_DOWN_BTN)
5395cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5405cb82703SWambui Karuga 			"Button array event is not supported (volume down)\n");
541df0566a6SJani Nikula 	if (iuer & ASLE_IUER_VOLUME_UP_BTN)
5425cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5435cb82703SWambui Karuga 			"Button array event is not supported (volume up)\n");
544df0566a6SJani Nikula 	if (iuer & ASLE_IUER_WINDOWS_BTN)
5455cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5465cb82703SWambui Karuga 			"Button array event is not supported (windows)\n");
547df0566a6SJani Nikula 	if (iuer & ASLE_IUER_POWER_BTN)
5485cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5495cb82703SWambui Karuga 			"Button array event is not supported (power)\n");
550df0566a6SJani Nikula 
551df0566a6SJani Nikula 	return ASLC_BUTTON_ARRAY_FAILED;
552df0566a6SJani Nikula }
553df0566a6SJani Nikula 
asle_set_convertible(struct drm_i915_private * dev_priv,u32 iuer)554df0566a6SJani Nikula static u32 asle_set_convertible(struct drm_i915_private *dev_priv, u32 iuer)
555df0566a6SJani Nikula {
556df0566a6SJani Nikula 	if (iuer & ASLE_IUER_CONVERTIBLE)
5575cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5585cb82703SWambui Karuga 			"Convertible is not supported (clamshell)\n");
559df0566a6SJani Nikula 	else
5605cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5615cb82703SWambui Karuga 			"Convertible is not supported (slate)\n");
562df0566a6SJani Nikula 
563df0566a6SJani Nikula 	return ASLC_CONVERTIBLE_FAILED;
564df0566a6SJani Nikula }
565df0566a6SJani Nikula 
asle_set_docking(struct drm_i915_private * dev_priv,u32 iuer)566df0566a6SJani Nikula static u32 asle_set_docking(struct drm_i915_private *dev_priv, u32 iuer)
567df0566a6SJani Nikula {
568df0566a6SJani Nikula 	if (iuer & ASLE_IUER_DOCKING)
5695cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm, "Docking is not supported (docked)\n");
570df0566a6SJani Nikula 	else
5715cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
5725cb82703SWambui Karuga 			"Docking is not supported (undocked)\n");
573df0566a6SJani Nikula 
574df0566a6SJani Nikula 	return ASLC_DOCKING_FAILED;
575df0566a6SJani Nikula }
576df0566a6SJani Nikula 
asle_isct_state(struct drm_i915_private * dev_priv)577df0566a6SJani Nikula static u32 asle_isct_state(struct drm_i915_private *dev_priv)
578df0566a6SJani Nikula {
5795cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm, "ISCT is not supported\n");
580df0566a6SJani Nikula 	return ASLC_ISCT_STATE_FAILED;
581df0566a6SJani Nikula }
582df0566a6SJani Nikula 
asle_work(struct work_struct * work)583df0566a6SJani Nikula static void asle_work(struct work_struct *work)
584df0566a6SJani Nikula {
585df0566a6SJani Nikula 	struct intel_opregion *opregion =
586df0566a6SJani Nikula 		container_of(work, struct intel_opregion, asle_work);
587df0566a6SJani Nikula 	struct drm_i915_private *dev_priv =
5887249dfcbSJani Nikula 		container_of(opregion, struct drm_i915_private, display.opregion);
5897249dfcbSJani Nikula 	struct opregion_asle *asle = dev_priv->display.opregion.asle;
590df0566a6SJani Nikula 	u32 aslc_stat = 0;
591df0566a6SJani Nikula 	u32 aslc_req;
592df0566a6SJani Nikula 
593df0566a6SJani Nikula 	if (!asle)
594df0566a6SJani Nikula 		return;
595df0566a6SJani Nikula 
596df0566a6SJani Nikula 	aslc_req = asle->aslc;
597df0566a6SJani Nikula 
598df0566a6SJani Nikula 	if (!(aslc_req & ASLC_REQ_MSK)) {
5995cb82703SWambui Karuga 		drm_dbg(&dev_priv->drm,
6005cb82703SWambui Karuga 			"No request on ASLC interrupt 0x%08x\n", aslc_req);
601df0566a6SJani Nikula 		return;
602df0566a6SJani Nikula 	}
603df0566a6SJani Nikula 
604df0566a6SJani Nikula 	if (aslc_req & ASLC_SET_ALS_ILLUM)
605df0566a6SJani Nikula 		aslc_stat |= asle_set_als_illum(dev_priv, asle->alsi);
606df0566a6SJani Nikula 
607df0566a6SJani Nikula 	if (aslc_req & ASLC_SET_BACKLIGHT)
608df0566a6SJani Nikula 		aslc_stat |= asle_set_backlight(dev_priv, asle->bclp);
609df0566a6SJani Nikula 
610df0566a6SJani Nikula 	if (aslc_req & ASLC_SET_PFIT)
611df0566a6SJani Nikula 		aslc_stat |= asle_set_pfit(dev_priv, asle->pfit);
612df0566a6SJani Nikula 
613df0566a6SJani Nikula 	if (aslc_req & ASLC_SET_PWM_FREQ)
614df0566a6SJani Nikula 		aslc_stat |= asle_set_pwm_freq(dev_priv, asle->pfmb);
615df0566a6SJani Nikula 
616df0566a6SJani Nikula 	if (aslc_req & ASLC_SUPPORTED_ROTATION_ANGLES)
617df0566a6SJani Nikula 		aslc_stat |= asle_set_supported_rotation_angles(dev_priv,
618df0566a6SJani Nikula 							asle->srot);
619df0566a6SJani Nikula 
620df0566a6SJani Nikula 	if (aslc_req & ASLC_BUTTON_ARRAY)
621df0566a6SJani Nikula 		aslc_stat |= asle_set_button_array(dev_priv, asle->iuer);
622df0566a6SJani Nikula 
623df0566a6SJani Nikula 	if (aslc_req & ASLC_CONVERTIBLE_INDICATOR)
624df0566a6SJani Nikula 		aslc_stat |= asle_set_convertible(dev_priv, asle->iuer);
625df0566a6SJani Nikula 
626df0566a6SJani Nikula 	if (aslc_req & ASLC_DOCKING_INDICATOR)
627df0566a6SJani Nikula 		aslc_stat |= asle_set_docking(dev_priv, asle->iuer);
628df0566a6SJani Nikula 
629df0566a6SJani Nikula 	if (aslc_req & ASLC_ISCT_STATE_CHANGE)
630df0566a6SJani Nikula 		aslc_stat |= asle_isct_state(dev_priv);
631df0566a6SJani Nikula 
632df0566a6SJani Nikula 	asle->aslc = aslc_stat;
633df0566a6SJani Nikula }
634df0566a6SJani Nikula 
intel_opregion_asle_intr(struct drm_i915_private * dev_priv)635df0566a6SJani Nikula void intel_opregion_asle_intr(struct drm_i915_private *dev_priv)
636df0566a6SJani Nikula {
6377249dfcbSJani Nikula 	if (dev_priv->display.opregion.asle)
6387249dfcbSJani Nikula 		queue_work(dev_priv->unordered_wq,
639df0566a6SJani Nikula 			   &dev_priv->display.opregion.asle_work);
640df0566a6SJani Nikula }
641df0566a6SJani Nikula 
642df0566a6SJani Nikula #define ACPI_EV_DISPLAY_SWITCH (1<<0)
643df0566a6SJani Nikula #define ACPI_EV_LID            (1<<1)
644df0566a6SJani Nikula #define ACPI_EV_DOCK           (1<<2)
645df0566a6SJani Nikula 
646df0566a6SJani Nikula /*
647df0566a6SJani Nikula  * The only video events relevant to opregion are 0x80. These indicate either a
648df0566a6SJani Nikula  * docking event, lid switch or display switch request. In Linux, these are
649df0566a6SJani Nikula  * handled by the dock, button and video drivers.
650df0566a6SJani Nikula  */
intel_opregion_video_event(struct notifier_block * nb,unsigned long val,void * data)651df0566a6SJani Nikula static int intel_opregion_video_event(struct notifier_block *nb,
652df0566a6SJani Nikula 				      unsigned long val, void *data)
653df0566a6SJani Nikula {
654df0566a6SJani Nikula 	struct intel_opregion *opregion = container_of(nb, struct intel_opregion,
655df0566a6SJani Nikula 						       acpi_notifier);
656df0566a6SJani Nikula 	struct acpi_bus_event *event = data;
657df0566a6SJani Nikula 	struct opregion_acpi *acpi;
658df0566a6SJani Nikula 	int ret = NOTIFY_OK;
659df0566a6SJani Nikula 
660df0566a6SJani Nikula 	if (strcmp(event->device_class, ACPI_VIDEO_CLASS) != 0)
661df0566a6SJani Nikula 		return NOTIFY_DONE;
662df0566a6SJani Nikula 
663df0566a6SJani Nikula 	acpi = opregion->acpi;
664df0566a6SJani Nikula 
665df0566a6SJani Nikula 	if (event->type == 0x80 && ((acpi->cevt & 1) == 0))
666df0566a6SJani Nikula 		ret = NOTIFY_BAD;
667df0566a6SJani Nikula 
668df0566a6SJani Nikula 	acpi->csts = 0;
669df0566a6SJani Nikula 
670df0566a6SJani Nikula 	return ret;
671df0566a6SJani Nikula }
672df0566a6SJani Nikula 
673df0566a6SJani Nikula /*
674df0566a6SJani Nikula  * Initialise the DIDL field in opregion. This passes a list of devices to
675df0566a6SJani Nikula  * the firmware. Values are defined by section B.4.2 of the ACPI specification
676df0566a6SJani Nikula  * (version 3)
677df0566a6SJani Nikula  */
678df0566a6SJani Nikula 
set_did(struct intel_opregion * opregion,int i,u32 val)679df0566a6SJani Nikula static void set_did(struct intel_opregion *opregion, int i, u32 val)
680df0566a6SJani Nikula {
681df0566a6SJani Nikula 	if (i < ARRAY_SIZE(opregion->acpi->didl)) {
682df0566a6SJani Nikula 		opregion->acpi->didl[i] = val;
683df0566a6SJani Nikula 	} else {
684df0566a6SJani Nikula 		i -= ARRAY_SIZE(opregion->acpi->didl);
685df0566a6SJani Nikula 
686df0566a6SJani Nikula 		if (WARN_ON(i >= ARRAY_SIZE(opregion->acpi->did2)))
687df0566a6SJani Nikula 			return;
688df0566a6SJani Nikula 
689df0566a6SJani Nikula 		opregion->acpi->did2[i] = val;
690df0566a6SJani Nikula 	}
691df0566a6SJani Nikula }
692df0566a6SJani Nikula 
intel_didl_outputs(struct drm_i915_private * dev_priv)693df0566a6SJani Nikula static void intel_didl_outputs(struct drm_i915_private *dev_priv)
6947249dfcbSJani Nikula {
695df0566a6SJani Nikula 	struct intel_opregion *opregion = &dev_priv->display.opregion;
696df0566a6SJani Nikula 	struct intel_connector *connector;
697df0566a6SJani Nikula 	struct drm_connector_list_iter conn_iter;
698df0566a6SJani Nikula 	int i = 0, max_outputs;
699df0566a6SJani Nikula 
700df0566a6SJani Nikula 	/*
701df0566a6SJani Nikula 	 * In theory, did2, the extended didl, gets added at opregion version
702df0566a6SJani Nikula 	 * 3.0. In practice, however, we're supposed to set it for earlier
703df0566a6SJani Nikula 	 * versions as well, since a BIOS that doesn't understand did2 should
704df0566a6SJani Nikula 	 * not look at it anyway. Use a variable so we can tweak this if a need
705df0566a6SJani Nikula 	 * arises later.
706df0566a6SJani Nikula 	 */
707df0566a6SJani Nikula 	max_outputs = ARRAY_SIZE(opregion->acpi->didl) +
708df0566a6SJani Nikula 		ARRAY_SIZE(opregion->acpi->did2);
7095fb44306SRajat Jain 
7105fb44306SRajat Jain 	intel_acpi_device_id_update(dev_priv);
711df0566a6SJani Nikula 
712df0566a6SJani Nikula 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
713df0566a6SJani Nikula 	for_each_intel_connector_iter(connector, &conn_iter) {
7145fb44306SRajat Jain 		if (i < max_outputs)
715df0566a6SJani Nikula 			set_did(opregion, i, connector->acpi_device_id);
716df0566a6SJani Nikula 		i++;
717df0566a6SJani Nikula 	}
718df0566a6SJani Nikula 	drm_connector_list_iter_end(&conn_iter);
7195cb82703SWambui Karuga 
720df0566a6SJani Nikula 	drm_dbg_kms(&dev_priv->drm, "%d outputs detected\n", i);
721df0566a6SJani Nikula 
7225cb82703SWambui Karuga 	if (i > max_outputs)
7235cb82703SWambui Karuga 		drm_err(&dev_priv->drm,
724df0566a6SJani Nikula 			"More than %d outputs in connector list\n",
725df0566a6SJani Nikula 			max_outputs);
726df0566a6SJani Nikula 
727df0566a6SJani Nikula 	/* If fewer than max outputs, the list must be null terminated */
728df0566a6SJani Nikula 	if (i < max_outputs)
729df0566a6SJani Nikula 		set_did(opregion, i, 0);
730df0566a6SJani Nikula }
731df0566a6SJani Nikula 
intel_setup_cadls(struct drm_i915_private * dev_priv)732df0566a6SJani Nikula static void intel_setup_cadls(struct drm_i915_private *dev_priv)
7337249dfcbSJani Nikula {
734df0566a6SJani Nikula 	struct intel_opregion *opregion = &dev_priv->display.opregion;
735df0566a6SJani Nikula 	struct intel_connector *connector;
736df0566a6SJani Nikula 	struct drm_connector_list_iter conn_iter;
737df0566a6SJani Nikula 	int i = 0;
738df0566a6SJani Nikula 
739df0566a6SJani Nikula 	/*
740df0566a6SJani Nikula 	 * Initialize the CADL field from the connector device ids. This is
741df0566a6SJani Nikula 	 * essentially the same as copying from the DIDL. Technically, this is
742df0566a6SJani Nikula 	 * not always correct as display outputs may exist, but not active. This
743df0566a6SJani Nikula 	 * initialization is necessary for some Clevo laptops that check this
744df0566a6SJani Nikula 	 * field before processing the brightness and display switching hotkeys.
745df0566a6SJani Nikula 	 *
746df0566a6SJani Nikula 	 * Note that internal panels should be at the front of the connector
747df0566a6SJani Nikula 	 * list already, ensuring they're not left out.
748df0566a6SJani Nikula 	 */
749df0566a6SJani Nikula 	drm_connector_list_iter_begin(&dev_priv->drm, &conn_iter);
750df0566a6SJani Nikula 	for_each_intel_connector_iter(connector, &conn_iter) {
751df0566a6SJani Nikula 		if (i >= ARRAY_SIZE(opregion->acpi->cadl))
752df0566a6SJani Nikula 			break;
753df0566a6SJani Nikula 		opregion->acpi->cadl[i++] = connector->acpi_device_id;
754df0566a6SJani Nikula 	}
755df0566a6SJani Nikula 	drm_connector_list_iter_end(&conn_iter);
756df0566a6SJani Nikula 
757df0566a6SJani Nikula 	/* If fewer than 8 active devices, the list must be null terminated */
758df0566a6SJani Nikula 	if (i < ARRAY_SIZE(opregion->acpi->cadl))
759df0566a6SJani Nikula 		opregion->acpi->cadl[i] = 0;
760df0566a6SJani Nikula }
761df0566a6SJani Nikula 
swsci_setup(struct drm_i915_private * dev_priv)762df0566a6SJani Nikula static void swsci_setup(struct drm_i915_private *dev_priv)
7637249dfcbSJani Nikula {
764df0566a6SJani Nikula 	struct intel_opregion *opregion = &dev_priv->display.opregion;
765df0566a6SJani Nikula 	bool requested_callbacks = false;
766df0566a6SJani Nikula 	u32 tmp;
767df0566a6SJani Nikula 
768df0566a6SJani Nikula 	/* Sub-function code 0 is okay, let's allow them. */
769df0566a6SJani Nikula 	opregion->swsci_gbda_sub_functions = 1;
770df0566a6SJani Nikula 	opregion->swsci_sbcb_sub_functions = 1;
771df0566a6SJani Nikula 
772df0566a6SJani Nikula 	/* We use GBDA to ask for supported GBDA calls. */
773df0566a6SJani Nikula 	if (swsci(dev_priv, SWSCI_GBDA_SUPPORTED_CALLS, 0, &tmp) == 0) {
774df0566a6SJani Nikula 		/* make the bits match the sub-function codes */
775df0566a6SJani Nikula 		tmp <<= 1;
776df0566a6SJani Nikula 		opregion->swsci_gbda_sub_functions |= tmp;
777df0566a6SJani Nikula 	}
778df0566a6SJani Nikula 
779df0566a6SJani Nikula 	/*
780df0566a6SJani Nikula 	 * We also use GBDA to ask for _requested_ SBCB callbacks. The driver
781df0566a6SJani Nikula 	 * must not call interfaces that are not specifically requested by the
782df0566a6SJani Nikula 	 * bios.
783df0566a6SJani Nikula 	 */
784df0566a6SJani Nikula 	if (swsci(dev_priv, SWSCI_GBDA_REQUESTED_CALLBACKS, 0, &tmp) == 0) {
785df0566a6SJani Nikula 		/* here, the bits already match sub-function codes */
786df0566a6SJani Nikula 		opregion->swsci_sbcb_sub_functions |= tmp;
787df0566a6SJani Nikula 		requested_callbacks = true;
788df0566a6SJani Nikula 	}
789df0566a6SJani Nikula 
790df0566a6SJani Nikula 	/*
791df0566a6SJani Nikula 	 * But we use SBCB to ask for _supported_ SBCB calls. This does not mean
792df0566a6SJani Nikula 	 * the callback is _requested_. But we still can't call interfaces that
793df0566a6SJani Nikula 	 * are not requested.
794df0566a6SJani Nikula 	 */
795df0566a6SJani Nikula 	if (swsci(dev_priv, SWSCI_SBCB_SUPPORTED_CALLBACKS, 0, &tmp) == 0) {
796df0566a6SJani Nikula 		/* make the bits match the sub-function codes */
797df0566a6SJani Nikula 		u32 low = tmp & 0x7ff;
798df0566a6SJani Nikula 		u32 high = tmp & ~0xfff; /* bit 11 is reserved */
799df0566a6SJani Nikula 		tmp = (high << 4) | (low << 1) | 1;
800df0566a6SJani Nikula 
801df0566a6SJani Nikula 		/* best guess what to do with supported wrt requested */
802df0566a6SJani Nikula 		if (requested_callbacks) {
803df0566a6SJani Nikula 			u32 req = opregion->swsci_sbcb_sub_functions;
8045cb82703SWambui Karuga 			if ((req & tmp) != req)
8055cb82703SWambui Karuga 				drm_dbg(&dev_priv->drm,
8065cb82703SWambui Karuga 					"SWSCI BIOS requested (%08x) SBCB callbacks that are not supported (%08x)\n",
807df0566a6SJani Nikula 					req, tmp);
808df0566a6SJani Nikula 			/* XXX: for now, trust the requested callbacks */
809df0566a6SJani Nikula 			/* opregion->swsci_sbcb_sub_functions &= tmp; */
810df0566a6SJani Nikula 		} else {
811df0566a6SJani Nikula 			opregion->swsci_sbcb_sub_functions |= tmp;
812df0566a6SJani Nikula 		}
813df0566a6SJani Nikula 	}
8145cb82703SWambui Karuga 
8155cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm,
816df0566a6SJani Nikula 		"SWSCI GBDA callbacks %08x, SBCB callbacks %08x\n",
817df0566a6SJani Nikula 		opregion->swsci_gbda_sub_functions,
818df0566a6SJani Nikula 		opregion->swsci_sbcb_sub_functions);
819df0566a6SJani Nikula }
820df0566a6SJani Nikula 
intel_no_opregion_vbt_callback(const struct dmi_system_id * id)821df0566a6SJani Nikula static int intel_no_opregion_vbt_callback(const struct dmi_system_id *id)
822df0566a6SJani Nikula {
823df0566a6SJani Nikula 	DRM_DEBUG_KMS("Falling back to manually reading VBT from "
824df0566a6SJani Nikula 		      "VBIOS ROM for %s\n", id->ident);
825df0566a6SJani Nikula 	return 1;
826df0566a6SJani Nikula }
827df0566a6SJani Nikula 
828df0566a6SJani Nikula static const struct dmi_system_id intel_no_opregion_vbt[] = {
829df0566a6SJani Nikula 	{
830df0566a6SJani Nikula 		.callback = intel_no_opregion_vbt_callback,
831df0566a6SJani Nikula 		.ident = "ThinkCentre A57",
832df0566a6SJani Nikula 		.matches = {
833df0566a6SJani Nikula 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
834df0566a6SJani Nikula 			DMI_MATCH(DMI_PRODUCT_NAME, "97027RG"),
835df0566a6SJani Nikula 		},
836df0566a6SJani Nikula 	},
837df0566a6SJani Nikula 	{ }
838df0566a6SJani Nikula };
839df0566a6SJani Nikula 
intel_load_vbt_firmware(struct drm_i915_private * dev_priv)840df0566a6SJani Nikula static int intel_load_vbt_firmware(struct drm_i915_private *dev_priv)
8417249dfcbSJani Nikula {
842df0566a6SJani Nikula 	struct intel_opregion *opregion = &dev_priv->display.opregion;
8438a25c4beSJani Nikula 	const struct firmware *fw = NULL;
844df0566a6SJani Nikula 	const char *name = dev_priv->params.vbt_firmware;
845df0566a6SJani Nikula 	int ret;
846df0566a6SJani Nikula 
847df0566a6SJani Nikula 	if (!name || !*name)
848df0566a6SJani Nikula 		return -ENOENT;
8498ff5446aSThomas Zimmermann 
850df0566a6SJani Nikula 	ret = request_firmware(&fw, name, dev_priv->drm.dev);
8515cb82703SWambui Karuga 	if (ret) {
8525cb82703SWambui Karuga 		drm_err(&dev_priv->drm,
853df0566a6SJani Nikula 			"Requesting VBT firmware \"%s\" failed (%d)\n",
854df0566a6SJani Nikula 			name, ret);
855df0566a6SJani Nikula 		return ret;
856df0566a6SJani Nikula 	}
857df0566a6SJani Nikula 
858df0566a6SJani Nikula 	if (intel_bios_is_valid_vbt(fw->data, fw->size)) {
859df0566a6SJani Nikula 		opregion->vbt_firmware = kmemdup(fw->data, fw->size, GFP_KERNEL);
8605cb82703SWambui Karuga 		if (opregion->vbt_firmware) {
8615cb82703SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
862df0566a6SJani Nikula 				    "Found valid VBT firmware \"%s\"\n", name);
863df0566a6SJani Nikula 			opregion->vbt = opregion->vbt_firmware;
864df0566a6SJani Nikula 			opregion->vbt_size = fw->size;
865df0566a6SJani Nikula 			ret = 0;
866df0566a6SJani Nikula 		} else {
867df0566a6SJani Nikula 			ret = -ENOMEM;
868df0566a6SJani Nikula 		}
8695cb82703SWambui Karuga 	} else {
8705cb82703SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Invalid VBT firmware \"%s\"\n",
871df0566a6SJani Nikula 			    name);
872df0566a6SJani Nikula 		ret = -EINVAL;
873df0566a6SJani Nikula 	}
874df0566a6SJani Nikula 
875df0566a6SJani Nikula 	release_firmware(fw);
876df0566a6SJani Nikula 
877df0566a6SJani Nikula 	return ret;
878df0566a6SJani Nikula }
879df0566a6SJani Nikula 
intel_opregion_setup(struct drm_i915_private * dev_priv)880df0566a6SJani Nikula int intel_opregion_setup(struct drm_i915_private *dev_priv)
8817249dfcbSJani Nikula {
8828ff5446aSThomas Zimmermann 	struct intel_opregion *opregion = &dev_priv->display.opregion;
883df0566a6SJani Nikula 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
884df0566a6SJani Nikula 	u32 asls, mboxes;
885df0566a6SJani Nikula 	char buf[sizeof(OPREGION_SIGNATURE)];
886df0566a6SJani Nikula 	int err = 0;
887df0566a6SJani Nikula 	void *base;
888df0566a6SJani Nikula 	const void *vbt;
889df0566a6SJani Nikula 	u32 vbt_size;
890df0566a6SJani Nikula 
891df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(struct opregion_header) != 0x100);
892df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(struct opregion_acpi) != 0x100);
893df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(struct opregion_swsci) != 0x100);
894df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(struct opregion_asle) != 0x100);
895df0566a6SJani Nikula 	BUILD_BUG_ON(sizeof(struct opregion_asle_ext) != 0x400);
896df0566a6SJani Nikula 
8975cb82703SWambui Karuga 	pci_read_config_dword(pdev, ASLS, &asls);
8985cb82703SWambui Karuga 	drm_dbg(&dev_priv->drm, "graphic opregion physical addr: 0x%x\n",
899df0566a6SJani Nikula 		asls);
9005cb82703SWambui Karuga 	if (asls == 0) {
901df0566a6SJani Nikula 		drm_dbg(&dev_priv->drm, "ACPI OpRegion not supported!\n");
902df0566a6SJani Nikula 		return -ENOTSUPP;
903df0566a6SJani Nikula 	}
904df0566a6SJani Nikula 
905df0566a6SJani Nikula 	INIT_WORK(&opregion->asle_work, asle_work);
906df0566a6SJani Nikula 
907df0566a6SJani Nikula 	base = memremap(asls, OPREGION_SIZE, MEMREMAP_WB);
908df0566a6SJani Nikula 	if (!base)
909df0566a6SJani Nikula 		return -ENOMEM;
910df0566a6SJani Nikula 
911df0566a6SJani Nikula 	memcpy(buf, base, sizeof(buf));
912df0566a6SJani Nikula 
9135cb82703SWambui Karuga 	if (memcmp(buf, OPREGION_SIGNATURE, 16)) {
914df0566a6SJani Nikula 		drm_dbg(&dev_priv->drm, "opregion signature mismatch\n");
915df0566a6SJani Nikula 		err = -EINVAL;
916df0566a6SJani Nikula 		goto err_out;
917df0566a6SJani Nikula 	}
918df0566a6SJani Nikula 	opregion->header = base;
919df0566a6SJani Nikula 	opregion->lid_state = base + ACPI_CLID;
9205cb82703SWambui Karuga 
921df0566a6SJani Nikula 	drm_dbg(&dev_priv->drm, "ACPI OpRegion version %u.%u.%u\n",
922df0566a6SJani Nikula 		opregion->header->over.major,
923df0566a6SJani Nikula 		opregion->header->over.minor,
924df0566a6SJani Nikula 		opregion->header->over.revision);
925df0566a6SJani Nikula 
926df0566a6SJani Nikula 	mboxes = opregion->header->mboxes;
9275cb82703SWambui Karuga 	if (mboxes & MBOX_ACPI) {
928df0566a6SJani Nikula 		drm_dbg(&dev_priv->drm, "Public ACPI methods supported\n");
9296fb0a14fSHans de Goede 		opregion->acpi = base + OPREGION_ACPI_OFFSET;
9306fb0a14fSHans de Goede 		/*
9316fb0a14fSHans de Goede 		 * Indicate we handle monitor hotplug events ourselves so we do
9326fb0a14fSHans de Goede 		 * not need ACPI notifications for them. Disabling these avoids
9336fb0a14fSHans de Goede 		 * triggering the AML code doing the notifation, which may be
9346fb0a14fSHans de Goede 		 * broken as Windows also seems to disable these.
9356fb0a14fSHans de Goede 		 */
936df0566a6SJani Nikula 		opregion->acpi->chpd = 1;
937df0566a6SJani Nikula 	}
938df0566a6SJani Nikula 
93969898171SJani Nikula 	if (mboxes & MBOX_SWSCI) {
94069898171SJani Nikula 		u8 major = opregion->header->over.major;
94169898171SJani Nikula 
94269898171SJani Nikula 		if (major >= 3) {
94369898171SJani Nikula 			drm_err(&dev_priv->drm, "SWSCI Mailbox #2 present for opregion v3.x, ignoring\n");
94469898171SJani Nikula 		} else {
94569898171SJani Nikula 			if (major >= 2)
9465cb82703SWambui Karuga 				drm_dbg(&dev_priv->drm, "SWSCI Mailbox #2 present for opregion v2.x\n");
947df0566a6SJani Nikula 			drm_dbg(&dev_priv->drm, "SWSCI supported\n");
948df0566a6SJani Nikula 			opregion->swsci = base + OPREGION_SWSCI_OFFSET;
949df0566a6SJani Nikula 			swsci_setup(dev_priv);
95069898171SJani Nikula 		}
951df0566a6SJani Nikula 	}
952df0566a6SJani Nikula 
9535cb82703SWambui Karuga 	if (mboxes & MBOX_ASLE) {
954df0566a6SJani Nikula 		drm_dbg(&dev_priv->drm, "ASLE supported\n");
955df0566a6SJani Nikula 		opregion->asle = base + OPREGION_ASLE_OFFSET;
956df0566a6SJani Nikula 
957df0566a6SJani Nikula 		opregion->asle->ardy = ASLE_ARDY_NOT_READY;
958df0566a6SJani Nikula 	}
959e35d8762SAnisse Astier 
9605cb82703SWambui Karuga 	if (mboxes & MBOX_ASLE_EXT) {
961e35d8762SAnisse Astier 		drm_dbg(&dev_priv->drm, "ASLE extension supported\n");
962e35d8762SAnisse Astier 		opregion->asle_ext = base + OPREGION_ASLE_EXT_OFFSET;
963df0566a6SJani Nikula 	}
964a391e069SJani Nikula 
965a391e069SJani Nikula 	if (mboxes & MBOX_BACKLIGHT) {
966a391e069SJani Nikula 		drm_dbg(&dev_priv->drm, "Mailbox #2 for backlight present\n");
967a391e069SJani Nikula 	}
968df0566a6SJani Nikula 
969df0566a6SJani Nikula 	if (intel_load_vbt_firmware(dev_priv) == 0)
970df0566a6SJani Nikula 		goto out;
971df0566a6SJani Nikula 
972df0566a6SJani Nikula 	if (dmi_check_system(intel_no_opregion_vbt))
973df0566a6SJani Nikula 		goto out;
974df0566a6SJani Nikula 
975df0566a6SJani Nikula 	if (opregion->header->over.major >= 2 && opregion->asle &&
976df0566a6SJani Nikula 	    opregion->asle->rvda && opregion->asle->rvds) {
977df0566a6SJani Nikula 		resource_size_t rvda = opregion->asle->rvda;
978df0566a6SJani Nikula 
979df0566a6SJani Nikula 		/*
980df0566a6SJani Nikula 		 * opregion 2.0: rvda is the physical VBT address.
981df0566a6SJani Nikula 		 *
982df0566a6SJani Nikula 		 * opregion 2.1+: rvda is unsigned, relative offset from
983df0566a6SJani Nikula 		 * opregion base, and should never point within opregion.
984df0566a6SJani Nikula 		 */
985df0566a6SJani Nikula 		if (opregion->header->over.major > 2 ||
986f4224a4cSPankaj Bharadiya 		    opregion->header->over.minor >= 1) {
987df0566a6SJani Nikula 			drm_WARN_ON(&dev_priv->drm, rvda < OPREGION_SIZE);
988df0566a6SJani Nikula 
989df0566a6SJani Nikula 			rvda += asls;
990df0566a6SJani Nikula 		}
991df0566a6SJani Nikula 
992df0566a6SJani Nikula 		opregion->rvda = memremap(rvda, opregion->asle->rvds,
993df0566a6SJani Nikula 					  MEMREMAP_WB);
994df0566a6SJani Nikula 
995df0566a6SJani Nikula 		vbt = opregion->rvda;
996df0566a6SJani Nikula 		vbt_size = opregion->asle->rvds;
9975cb82703SWambui Karuga 		if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
9985cb82703SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
999df0566a6SJani Nikula 				    "Found valid VBT in ACPI OpRegion (RVDA)\n");
1000df0566a6SJani Nikula 			opregion->vbt = vbt;
1001df0566a6SJani Nikula 			opregion->vbt_size = vbt_size;
1002df0566a6SJani Nikula 			goto out;
10035cb82703SWambui Karuga 		} else {
10045cb82703SWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
1005df0566a6SJani Nikula 				    "Invalid VBT in ACPI OpRegion (RVDA)\n");
1006df0566a6SJani Nikula 			memunmap(opregion->rvda);
1007df0566a6SJani Nikula 			opregion->rvda = NULL;
1008df0566a6SJani Nikula 		}
1009df0566a6SJani Nikula 	}
1010df0566a6SJani Nikula 
1011df0566a6SJani Nikula 	vbt = base + OPREGION_VBT_OFFSET;
1012df0566a6SJani Nikula 	/*
1013df0566a6SJani Nikula 	 * The VBT specification says that if the ASLE ext mailbox is not used
1014df0566a6SJani Nikula 	 * its area is reserved, but on some CHT boards the VBT extends into the
1015df0566a6SJani Nikula 	 * ASLE ext area. Allow this even though it is against the spec, so we
1016df0566a6SJani Nikula 	 * do not end up rejecting the VBT on those boards (and end up not
1017df0566a6SJani Nikula 	 * finding the LCD panel because of this).
1018df0566a6SJani Nikula 	 */
1019df0566a6SJani Nikula 	vbt_size = (mboxes & MBOX_ASLE_EXT) ?
1020df0566a6SJani Nikula 		OPREGION_ASLE_EXT_OFFSET : OPREGION_SIZE;
1021df0566a6SJani Nikula 	vbt_size -= OPREGION_VBT_OFFSET;
10225cb82703SWambui Karuga 	if (intel_bios_is_valid_vbt(vbt, vbt_size)) {
10235cb82703SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1024df0566a6SJani Nikula 			    "Found valid VBT in ACPI OpRegion (Mailbox #4)\n");
1025df0566a6SJani Nikula 		opregion->vbt = vbt;
1026df0566a6SJani Nikula 		opregion->vbt_size = vbt_size;
10275cb82703SWambui Karuga 	} else {
10285cb82703SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1029df0566a6SJani Nikula 			    "Invalid VBT in ACPI OpRegion (Mailbox #4)\n");
1030df0566a6SJani Nikula 	}
1031df0566a6SJani Nikula 
1032df0566a6SJani Nikula out:
1033df0566a6SJani Nikula 	return 0;
1034df0566a6SJani Nikula 
1035df0566a6SJani Nikula err_out:
1036df0566a6SJani Nikula 	memunmap(base);
1037df0566a6SJani Nikula 	return err;
1038df0566a6SJani Nikula }
1039df0566a6SJani Nikula 
intel_use_opregion_panel_type_callback(const struct dmi_system_id * id)1040df0566a6SJani Nikula static int intel_use_opregion_panel_type_callback(const struct dmi_system_id *id)
1041df0566a6SJani Nikula {
1042df0566a6SJani Nikula 	DRM_INFO("Using panel type from OpRegion on %s\n", id->ident);
1043df0566a6SJani Nikula 	return 1;
1044df0566a6SJani Nikula }
1045df0566a6SJani Nikula 
1046df0566a6SJani Nikula static const struct dmi_system_id intel_use_opregion_panel_type[] = {
1047df0566a6SJani Nikula 	{
1048df0566a6SJani Nikula 		.callback = intel_use_opregion_panel_type_callback,
1049df0566a6SJani Nikula 		.ident = "Conrac GmbH IX45GM2",
1050df0566a6SJani Nikula 		.matches = {DMI_MATCH(DMI_SYS_VENDOR, "Conrac GmbH"),
1051df0566a6SJani Nikula 			    DMI_MATCH(DMI_PRODUCT_NAME, "IX45GM2"),
1052df0566a6SJani Nikula 		},
1053df0566a6SJani Nikula 	},
1054df0566a6SJani Nikula 	{ }
1055df0566a6SJani Nikula };
1056df0566a6SJani Nikula 
1057df0566a6SJani Nikula int
intel_opregion_get_panel_type(struct drm_i915_private * dev_priv)1058df0566a6SJani Nikula intel_opregion_get_panel_type(struct drm_i915_private *dev_priv)
1059df0566a6SJani Nikula {
1060df0566a6SJani Nikula 	u32 panel_details;
1061df0566a6SJani Nikula 	int ret;
1062df0566a6SJani Nikula 
1063859d74f6SLucas De Marchi 	ret = swsci(dev_priv, SWSCI_GBDA_PANEL_DETAILS, 0x0, &panel_details);
1064df0566a6SJani Nikula 	if (ret)
1065df0566a6SJani Nikula 		return ret;
1066df0566a6SJani Nikula 
1067df0566a6SJani Nikula 	ret = (panel_details >> 8) & 0xff;
10685cb82703SWambui Karuga 	if (ret > 0x10) {
10695cb82703SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1070df0566a6SJani Nikula 			    "Invalid OpRegion panel type 0x%x\n", ret);
1071df0566a6SJani Nikula 		return -EINVAL;
1072df0566a6SJani Nikula 	}
1073df0566a6SJani Nikula 
1074df0566a6SJani Nikula 	/* fall back to VBT panel type? */
10755cb82703SWambui Karuga 	if (ret == 0x0) {
1076df0566a6SJani Nikula 		drm_dbg_kms(&dev_priv->drm, "No panel type in OpRegion\n");
1077df0566a6SJani Nikula 		return -ENODEV;
1078df0566a6SJani Nikula 	}
1079df0566a6SJani Nikula 
1080df0566a6SJani Nikula 	/*
1081df0566a6SJani Nikula 	 * So far we know that some machined must use it, others must not use it.
1082df0566a6SJani Nikula 	 * There doesn't seem to be any way to determine which way to go, except
1083df0566a6SJani Nikula 	 * via a quirk list :(
1084df0566a6SJani Nikula 	 */
10855cb82703SWambui Karuga 	if (!dmi_check_system(intel_use_opregion_panel_type)) {
10865cb82703SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1087df0566a6SJani Nikula 			    "Ignoring OpRegion panel type (%d)\n", ret - 1);
1088df0566a6SJani Nikula 		return -ENODEV;
1089df0566a6SJani Nikula 	}
1090df0566a6SJani Nikula 
1091df0566a6SJani Nikula 	return ret - 1;
1092df0566a6SJani Nikula }
1093e35d8762SAnisse Astier 
1094e35d8762SAnisse Astier /**
1095e35d8762SAnisse Astier  * intel_opregion_get_edid - Fetch EDID from ACPI OpRegion mailbox #5
1096e35d8762SAnisse Astier  * @intel_connector: eDP connector
1097e35d8762SAnisse Astier  *
1098e35d8762SAnisse Astier  * This reads the ACPI Opregion mailbox #5 to extract the EDID that is passed
1099e35d8762SAnisse Astier  * to it.
1100e35d8762SAnisse Astier  *
1101e35d8762SAnisse Astier  * Returns:
1102e35d8762SAnisse Astier  * The EDID in the OpRegion, or NULL if there is none or it's invalid.
1103e35d8762SAnisse Astier  *
110491ec555fSJani Nikula  */
intel_opregion_get_edid(struct intel_connector * intel_connector)1105e35d8762SAnisse Astier const struct drm_edid *intel_opregion_get_edid(struct intel_connector *intel_connector)
1106e35d8762SAnisse Astier {
1107e35d8762SAnisse Astier 	struct drm_connector *connector = &intel_connector->base;
11087249dfcbSJani Nikula 	struct drm_i915_private *i915 = to_i915(connector->dev);
110991ec555fSJani Nikula 	struct intel_opregion *opregion = &i915->display.opregion;
111091ec555fSJani Nikula 	const struct drm_edid *drm_edid;
1111e35d8762SAnisse Astier 	const void *edid;
1112e35d8762SAnisse Astier 	int len;
1113e35d8762SAnisse Astier 
1114e35d8762SAnisse Astier 	if (!opregion->asle_ext)
1115e35d8762SAnisse Astier 		return NULL;
111691ec555fSJani Nikula 
1117e35d8762SAnisse Astier 	edid = opregion->asle_ext->bddc;
1118e35d8762SAnisse Astier 
1119e35d8762SAnisse Astier 	/* Validity corresponds to number of 128-byte blocks */
112091ec555fSJani Nikula 	len = (opregion->asle_ext->phed & ASLE_PHED_EDID_VALID_MASK) * 128;
1121e35d8762SAnisse Astier 	if (!len || !memchr_inv(edid, 0, len))
1122e35d8762SAnisse Astier 		return NULL;
112391ec555fSJani Nikula 
1124e35d8762SAnisse Astier 	drm_edid = drm_edid_alloc(edid, len);
112591ec555fSJani Nikula 
1126e35d8762SAnisse Astier 	if (!drm_edid_valid(drm_edid)) {
112791ec555fSJani Nikula 		drm_dbg_kms(&i915->drm, "Invalid EDID in ACPI OpRegion (Mailbox #5)\n");
112891ec555fSJani Nikula 		drm_edid_free(drm_edid);
1129e35d8762SAnisse Astier 		drm_edid = NULL;
113091ec555fSJani Nikula 	}
113191ec555fSJani Nikula 
1132e35d8762SAnisse Astier 	return drm_edid;
1133e35d8762SAnisse Astier }
1134eb2983c0SJouni Högander 
intel_opregion_headless_sku(struct drm_i915_private * i915)1135eb2983c0SJouni Högander bool intel_opregion_headless_sku(struct drm_i915_private *i915)
11367249dfcbSJani Nikula {
1137eb2983c0SJouni Högander 	struct intel_opregion *opregion = &i915->display.opregion;
1138eb2983c0SJouni Högander 	struct opregion_header *header = opregion->header;
1139eb2983c0SJouni Högander 
1140eb2983c0SJouni Högander 	if (!header || header->over.major < 2 ||
1141eb2983c0SJouni Högander 	    (header->over.major == 2 && header->over.minor < 3))
1142eb2983c0SJouni Högander 		return false;
1143eb2983c0SJouni Högander 
1144eb2983c0SJouni Högander 	return opregion->header->pcon & PCON_HEADLESS_SKU;
1145eb2983c0SJouni Högander }
1146df0566a6SJani Nikula 
intel_opregion_register(struct drm_i915_private * i915)1147df0566a6SJani Nikula void intel_opregion_register(struct drm_i915_private *i915)
11487249dfcbSJani Nikula {
1149df0566a6SJani Nikula 	struct intel_opregion *opregion = &i915->display.opregion;
1150df0566a6SJani Nikula 
1151df0566a6SJani Nikula 	if (!opregion->header)
1152df0566a6SJani Nikula 		return;
1153df0566a6SJani Nikula 
1154df0566a6SJani Nikula 	if (opregion->acpi) {
1155df0566a6SJani Nikula 		opregion->acpi_notifier.notifier_call =
1156df0566a6SJani Nikula 			intel_opregion_video_event;
1157df0566a6SJani Nikula 		register_acpi_notifier(&opregion->acpi_notifier);
1158df0566a6SJani Nikula 	}
1159df0566a6SJani Nikula 
1160df0566a6SJani Nikula 	intel_opregion_resume(i915);
1161df0566a6SJani Nikula }
1162c4a1e57bSImre Deak 
intel_opregion_resume_display(struct drm_i915_private * i915)1163df0566a6SJani Nikula static void intel_opregion_resume_display(struct drm_i915_private *i915)
11647249dfcbSJani Nikula {
1165df0566a6SJani Nikula 	struct intel_opregion *opregion = &i915->display.opregion;
1166df0566a6SJani Nikula 
1167df0566a6SJani Nikula 	if (opregion->acpi) {
1168df0566a6SJani Nikula 		intel_didl_outputs(i915);
1169df0566a6SJani Nikula 		intel_setup_cadls(i915);
1170df0566a6SJani Nikula 
1171df0566a6SJani Nikula 		/*
1172df0566a6SJani Nikula 		 * Notify BIOS we are ready to handle ACPI video ext notifs.
1173df0566a6SJani Nikula 		 * Right now, all the events are handled by the ACPI video
1174df0566a6SJani Nikula 		 * module. We don't actually need to do anything with them.
1175df0566a6SJani Nikula 		 */
1176df0566a6SJani Nikula 		opregion->acpi->csts = 0;
1177df0566a6SJani Nikula 		opregion->acpi->drdy = 1;
1178df0566a6SJani Nikula 	}
1179df0566a6SJani Nikula 
1180df0566a6SJani Nikula 	if (opregion->asle) {
1181df0566a6SJani Nikula 		opregion->asle->tche = ASLE_TCHE_BLC_EN;
1182df0566a6SJani Nikula 		opregion->asle->ardy = ASLE_ARDY_READY;
1183df0566a6SJani Nikula 	}
11848e55f99cSKai-Heng Feng 
11858e55f99cSKai-Heng Feng 	/* Some platforms abuse the _DSM to enable MUX */
1186c4a1e57bSImre Deak 	intel_dsm_get_bios_data_funcs_supported(i915);
1187c4a1e57bSImre Deak }
1188c4a1e57bSImre Deak 
intel_opregion_resume(struct drm_i915_private * i915)1189c4a1e57bSImre Deak void intel_opregion_resume(struct drm_i915_private *i915)
1190c4a1e57bSImre Deak {
1191c4a1e57bSImre Deak 	struct intel_opregion *opregion = &i915->display.opregion;
1192c4a1e57bSImre Deak 
1193c4a1e57bSImre Deak 	if (!opregion->header)
1194c4a1e57bSImre Deak 		return;
1195c4a1e57bSImre Deak 
1196c4a1e57bSImre Deak 	if (HAS_DISPLAY(i915))
11978e55f99cSKai-Heng Feng 		intel_opregion_resume_display(i915);
1198df0566a6SJani Nikula 
1199df0566a6SJani Nikula 	intel_opregion_notify_adapter(i915, PCI_D0);
1200df0566a6SJani Nikula }
1201c4a1e57bSImre Deak 
intel_opregion_suspend_display(struct drm_i915_private * i915)1202c4a1e57bSImre Deak static void intel_opregion_suspend_display(struct drm_i915_private *i915)
1203c4a1e57bSImre Deak {
1204c4a1e57bSImre Deak 	struct intel_opregion *opregion = &i915->display.opregion;
1205c4a1e57bSImre Deak 
1206c4a1e57bSImre Deak 	if (opregion->asle)
1207c4a1e57bSImre Deak 		opregion->asle->ardy = ASLE_ARDY_NOT_READY;
1208c4a1e57bSImre Deak 
1209c4a1e57bSImre Deak 	cancel_work_sync(&i915->display.opregion.asle_work);
1210c4a1e57bSImre Deak 
1211c4a1e57bSImre Deak 	if (opregion->acpi)
1212c4a1e57bSImre Deak 		opregion->acpi->drdy = 0;
1213c4a1e57bSImre Deak }
1214df0566a6SJani Nikula 
intel_opregion_suspend(struct drm_i915_private * i915,pci_power_t state)1215df0566a6SJani Nikula void intel_opregion_suspend(struct drm_i915_private *i915, pci_power_t state)
12167249dfcbSJani Nikula {
1217df0566a6SJani Nikula 	struct intel_opregion *opregion = &i915->display.opregion;
1218df0566a6SJani Nikula 
1219df0566a6SJani Nikula 	if (!opregion->header)
1220df0566a6SJani Nikula 		return;
1221df0566a6SJani Nikula 
1222df0566a6SJani Nikula 	intel_opregion_notify_adapter(i915, state);
1223c4a1e57bSImre Deak 
1224c4a1e57bSImre Deak 	if (HAS_DISPLAY(i915))
1225df0566a6SJani Nikula 		intel_opregion_suspend_display(i915);
1226df0566a6SJani Nikula }
1227df0566a6SJani Nikula 
intel_opregion_unregister(struct drm_i915_private * i915)1228df0566a6SJani Nikula void intel_opregion_unregister(struct drm_i915_private *i915)
12297249dfcbSJani Nikula {
1230df0566a6SJani Nikula 	struct intel_opregion *opregion = &i915->display.opregion;
1231df0566a6SJani Nikula 
1232df0566a6SJani Nikula 	intel_opregion_suspend(i915, PCI_D1);
1233df0566a6SJani Nikula 
1234df0566a6SJani Nikula 	if (!opregion->header)
1235df0566a6SJani Nikula 		return;
1236df0566a6SJani Nikula 
1237df0566a6SJani Nikula 	if (opregion->acpi_notifier.notifier_call) {
1238df0566a6SJani Nikula 		unregister_acpi_notifier(&opregion->acpi_notifier);
1239df0566a6SJani Nikula 		opregion->acpi_notifier.notifier_call = NULL;
1240*3e226e4aSImre Deak 	}
1241*3e226e4aSImre Deak }
1242*3e226e4aSImre Deak 
intel_opregion_cleanup(struct drm_i915_private * i915)1243*3e226e4aSImre Deak void intel_opregion_cleanup(struct drm_i915_private *i915)
1244*3e226e4aSImre Deak {
1245*3e226e4aSImre Deak 	struct intel_opregion *opregion = &i915->display.opregion;
1246*3e226e4aSImre Deak 
1247*3e226e4aSImre Deak 	if (!opregion->header)
1248df0566a6SJani Nikula 		return;
1249df0566a6SJani Nikula 
1250df0566a6SJani Nikula 	/* just clear all opregion memory pointers now */
1251df0566a6SJani Nikula 	memunmap(opregion->header);
1252df0566a6SJani Nikula 	if (opregion->rvda) {
1253df0566a6SJani Nikula 		memunmap(opregion->rvda);
1254df0566a6SJani Nikula 		opregion->rvda = NULL;
1255df0566a6SJani Nikula 	}
1256df0566a6SJani Nikula 	if (opregion->vbt_firmware) {
1257df0566a6SJani Nikula 		kfree(opregion->vbt_firmware);
1258df0566a6SJani Nikula 		opregion->vbt_firmware = NULL;
1259df0566a6SJani Nikula 	}
1260df0566a6SJani Nikula 	opregion->header = NULL;
1261df0566a6SJani Nikula 	opregion->acpi = NULL;
1262df0566a6SJani Nikula 	opregion->swsci = NULL;
1263e35d8762SAnisse Astier 	opregion->asle = NULL;
1264df0566a6SJani Nikula 	opregion->asle_ext = NULL;
1265df0566a6SJani Nikula 	opregion->vbt = NULL;
1266df0566a6SJani Nikula 	opregion->lid_state = NULL;
1267 }
1268