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