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