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