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