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