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