xref: /linux/drivers/gpu/drm/xe/display/xe_display.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include "xe_display.h"
7 #include "regs/xe_irq_regs.h"
8 
9 #include <linux/fb.h>
10 
11 #include <drm/drm_client.h>
12 #include <drm/drm_client_event.h>
13 #include <drm/drm_drv.h>
14 #include <drm/drm_managed.h>
15 #include <drm/drm_probe_helper.h>
16 #include <drm/intel/display_member.h>
17 #include <drm/intel/display_parent_interface.h>
18 #include <uapi/drm/xe_drm.h>
19 
20 #include "intel_acpi.h"
21 #include "intel_audio.h"
22 #include "intel_bw.h"
23 #include "intel_display.h"
24 #include "intel_display_core.h"
25 #include "intel_display_device.h"
26 #include "intel_display_driver.h"
27 #include "intel_display_irq.h"
28 #include "intel_display_types.h"
29 #include "intel_dmc.h"
30 #include "intel_dmc_wl.h"
31 #include "intel_dp.h"
32 #include "intel_dram.h"
33 #include "intel_encoder.h"
34 #include "intel_fbdev.h"
35 #include "intel_hdcp.h"
36 #include "intel_hotplug.h"
37 #include "intel_opregion.h"
38 #include "skl_watermark.h"
39 #include "xe_display_bo.h"
40 #include "xe_display_pcode.h"
41 #include "xe_display_rpm.h"
42 #include "xe_dsb_buffer.h"
43 #include "xe_frontbuffer.h"
44 #include "xe_hdcp_gsc.h"
45 #include "xe_initial_plane.h"
46 #include "xe_module.h"
47 #include "xe_panic.h"
48 #include "xe_stolen.h"
49 
50 /* Ensure drm and display members are placed properly. */
51 INTEL_DISPLAY_MEMBER_STATIC_ASSERT(struct xe_device, drm, display);
52 
53 /* Xe device functions */
54 
55 /**
56  * xe_display_driver_probe_defer - Detect if we need to wait for other drivers
57  *				   early on
58  * @pdev: PCI device
59  *
60  * Note: This is called before xe or display device creation.
61  *
62  * Returns: true if probe needs to be deferred, false otherwise
63  */
64 bool xe_display_driver_probe_defer(struct pci_dev *pdev)
65 {
66 	if (!xe_modparam.probe_display)
67 		return 0;
68 
69 	return intel_display_driver_probe_defer(pdev);
70 }
71 
72 /**
73  * xe_display_driver_set_hooks - Add driver flags and hooks for display
74  * @driver: DRM device driver
75  *
76  * Set features and function hooks in @driver that are needed for driving the
77  * display IP. This sets the driver's capability of driving display, regardless
78  * if the device has it enabled
79  *
80  * Note: This is called before xe or display device creation.
81  */
82 void xe_display_driver_set_hooks(struct drm_driver *driver)
83 {
84 	if (!xe_modparam.probe_display)
85 		return;
86 
87 #ifdef CONFIG_DRM_FBDEV_EMULATION
88 	driver->fbdev_probe = intel_fbdev_driver_fbdev_probe;
89 #endif
90 
91 	driver->driver_features |= DRIVER_MODESET | DRIVER_ATOMIC;
92 }
93 
94 static void unset_display_features(struct xe_device *xe)
95 {
96 	xe->drm.driver_features &= ~(DRIVER_MODESET | DRIVER_ATOMIC);
97 }
98 
99 static void xe_display_fini_early(void *arg)
100 {
101 	struct xe_device *xe = arg;
102 	struct intel_display *display = xe->display;
103 
104 	if (!xe->info.probe_display)
105 		return;
106 
107 	intel_hpd_cancel_work(display);
108 	intel_display_driver_remove_nogem(display);
109 	intel_display_driver_remove_noirq(display);
110 	intel_opregion_cleanup(display);
111 	intel_power_domains_cleanup(display);
112 }
113 
114 int xe_display_init_early(struct xe_device *xe)
115 {
116 	struct intel_display *display = xe->display;
117 	int err;
118 
119 	if (!xe->info.probe_display)
120 		return 0;
121 
122 	/* Fake uncore lock */
123 	spin_lock_init(&xe->uncore.lock);
124 
125 	intel_display_driver_early_probe(display);
126 
127 	intel_display_device_info_runtime_init(display);
128 
129 	/* Display may have been disabled at runtime init */
130 	if (!intel_display_device_present(display)) {
131 		xe->info.probe_display = false;
132 		unset_display_features(xe);
133 		return 0;
134 	}
135 
136 	/* Early display init.. */
137 	intel_opregion_setup(display);
138 
139 	/*
140 	 * Fill the dram structure to get the system dram info. This will be
141 	 * used for memory latency calculation.
142 	 */
143 	err = intel_dram_detect(display);
144 	if (err)
145 		goto err_opregion;
146 
147 	intel_bw_init_hw(display);
148 
149 	err = intel_display_driver_probe_noirq(display);
150 	if (err)
151 		goto err_opregion;
152 
153 	err = intel_display_driver_probe_nogem(display);
154 	if (err)
155 		goto err_noirq;
156 
157 	return devm_add_action_or_reset(xe->drm.dev, xe_display_fini_early, xe);
158 err_noirq:
159 	intel_display_driver_remove_noirq(display);
160 	intel_power_domains_cleanup(display);
161 err_opregion:
162 	intel_opregion_cleanup(display);
163 	return err;
164 }
165 
166 static void xe_display_fini(void *arg)
167 {
168 	struct xe_device *xe = arg;
169 	struct intel_display *display = xe->display;
170 
171 	intel_hpd_poll_fini(display);
172 	intel_hdcp_component_fini(display);
173 	intel_audio_deinit(display);
174 	intel_display_driver_remove(display);
175 }
176 
177 int xe_display_init(struct xe_device *xe)
178 {
179 	struct intel_display *display = xe->display;
180 	int err;
181 
182 	if (!xe->info.probe_display)
183 		return 0;
184 
185 	err = intel_display_driver_probe(display);
186 	if (err)
187 		return err;
188 
189 	return devm_add_action_or_reset(xe->drm.dev, xe_display_fini, xe);
190 }
191 
192 void xe_display_register(struct xe_device *xe)
193 {
194 	struct intel_display *display = xe->display;
195 
196 	if (!xe->info.probe_display)
197 		return;
198 
199 	intel_display_driver_register(display);
200 	intel_power_domains_enable(display);
201 }
202 
203 void xe_display_unregister(struct xe_device *xe)
204 {
205 	struct intel_display *display = xe->display;
206 
207 	if (!xe->info.probe_display)
208 		return;
209 
210 	intel_power_domains_disable(display);
211 	intel_display_driver_unregister(display);
212 }
213 
214 /* IRQ-related functions */
215 
216 void xe_display_irq_handler(struct xe_device *xe, u32 master_ctl)
217 {
218 	struct intel_display *display = xe->display;
219 
220 	if (!xe->info.probe_display)
221 		return;
222 
223 	if (master_ctl & DISPLAY_IRQ)
224 		gen11_display_irq_handler(display);
225 }
226 
227 void xe_display_irq_enable(struct xe_device *xe, u32 gu_misc_iir)
228 {
229 	struct intel_display *display = xe->display;
230 
231 	if (!xe->info.probe_display)
232 		return;
233 
234 	if (gu_misc_iir & GU_MISC_GSE)
235 		intel_opregion_asle_intr(display);
236 }
237 
238 void xe_display_irq_reset(struct xe_device *xe)
239 {
240 	struct intel_display *display = xe->display;
241 
242 	if (!xe->info.probe_display)
243 		return;
244 
245 	gen11_display_irq_reset(display);
246 }
247 
248 void xe_display_irq_postinstall(struct xe_device *xe)
249 {
250 	struct intel_display *display = xe->display;
251 
252 	if (!xe->info.probe_display)
253 		return;
254 
255 	gen11_de_irq_postinstall(display);
256 }
257 
258 static bool suspend_to_idle(void)
259 {
260 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
261 	if (acpi_target_system_state() < ACPI_STATE_S3)
262 		return true;
263 #endif
264 	return false;
265 }
266 
267 static void xe_display_flush_cleanup_work(struct xe_device *xe)
268 {
269 	struct intel_crtc *crtc;
270 
271 	for_each_intel_crtc(&xe->drm, crtc) {
272 		struct drm_crtc_commit *commit;
273 
274 		spin_lock(&crtc->base.commit_lock);
275 		commit = list_first_entry_or_null(&crtc->base.commit_list,
276 						  struct drm_crtc_commit, commit_entry);
277 		if (commit)
278 			drm_crtc_commit_get(commit);
279 		spin_unlock(&crtc->base.commit_lock);
280 
281 		if (commit) {
282 			wait_for_completion(&commit->cleanup_done);
283 			drm_crtc_commit_put(commit);
284 		}
285 	}
286 }
287 
288 static void xe_display_enable_d3cold(struct xe_device *xe)
289 {
290 	struct intel_display *display = xe->display;
291 
292 	if (!xe->info.probe_display)
293 		return;
294 
295 	/*
296 	 * We do a lot of poking in a lot of registers, make sure they work
297 	 * properly.
298 	 */
299 	intel_power_domains_disable(display);
300 
301 	xe_display_flush_cleanup_work(xe);
302 
303 	intel_opregion_suspend(display, PCI_D3cold);
304 
305 	intel_dmc_suspend(display);
306 
307 	if (intel_display_device_present(display))
308 		intel_hpd_poll_enable(display);
309 }
310 
311 static void xe_display_disable_d3cold(struct xe_device *xe)
312 {
313 	struct intel_display *display = xe->display;
314 
315 	if (!xe->info.probe_display)
316 		return;
317 
318 	intel_dmc_resume(display);
319 
320 	if (intel_display_device_present(display))
321 		drm_mode_config_reset(&xe->drm);
322 
323 	intel_display_driver_init_hw(display);
324 
325 	intel_hpd_init(display);
326 
327 	if (intel_display_device_present(display))
328 		intel_hpd_poll_disable(display);
329 
330 	intel_opregion_resume(display);
331 
332 	intel_power_domains_enable(display);
333 }
334 
335 void xe_display_pm_suspend(struct xe_device *xe)
336 {
337 	struct intel_display *display = xe->display;
338 	bool s2idle = suspend_to_idle();
339 
340 	if (!xe->info.probe_display)
341 		return;
342 
343 	/*
344 	 * We do a lot of poking in a lot of registers, make sure they work
345 	 * properly.
346 	 */
347 	intel_power_domains_disable(display);
348 	drm_client_dev_suspend(&xe->drm);
349 
350 	if (intel_display_device_present(display)) {
351 		drm_kms_helper_poll_disable(&xe->drm);
352 		intel_display_driver_disable_user_access(display);
353 		intel_display_driver_suspend(display);
354 	}
355 
356 	xe_display_flush_cleanup_work(xe);
357 
358 	intel_encoder_block_all_hpds(display);
359 
360 	intel_hpd_cancel_work(display);
361 
362 	if (intel_display_device_present(display)) {
363 		intel_display_driver_suspend_access(display);
364 		intel_encoder_suspend_all(display);
365 	}
366 
367 	intel_opregion_suspend(display, s2idle ? PCI_D1 : PCI_D3cold);
368 
369 	intel_dmc_suspend(display);
370 }
371 
372 void xe_display_pm_shutdown(struct xe_device *xe)
373 {
374 	struct intel_display *display = xe->display;
375 
376 	if (!xe->info.probe_display)
377 		return;
378 
379 	intel_power_domains_disable(display);
380 	drm_client_dev_suspend(&xe->drm);
381 
382 	if (intel_display_device_present(display)) {
383 		drm_kms_helper_poll_disable(&xe->drm);
384 		intel_display_driver_disable_user_access(display);
385 		intel_display_driver_suspend(display);
386 	}
387 
388 	xe_display_flush_cleanup_work(xe);
389 	intel_dp_mst_suspend(display);
390 	intel_encoder_block_all_hpds(display);
391 	intel_hpd_cancel_work(display);
392 
393 	if (intel_display_device_present(display))
394 		intel_display_driver_suspend_access(display);
395 
396 	intel_encoder_suspend_all(display);
397 	intel_encoder_shutdown_all(display);
398 
399 	intel_opregion_suspend(display, PCI_D3cold);
400 
401 	intel_dmc_suspend(display);
402 }
403 
404 void xe_display_pm_runtime_suspend(struct xe_device *xe)
405 {
406 	struct intel_display *display = xe->display;
407 
408 	if (!xe->info.probe_display)
409 		return;
410 
411 	if (xe->d3cold.allowed) {
412 		xe_display_enable_d3cold(xe);
413 		return;
414 	}
415 
416 	intel_hpd_poll_enable(display);
417 }
418 
419 void xe_display_pm_suspend_late(struct xe_device *xe)
420 {
421 	struct intel_display *display = xe->display;
422 	bool s2idle = suspend_to_idle();
423 
424 	if (!xe->info.probe_display)
425 		return;
426 
427 	intel_display_power_suspend_late(display, s2idle);
428 }
429 
430 void xe_display_pm_runtime_suspend_late(struct xe_device *xe)
431 {
432 	struct intel_display *display = xe->display;
433 
434 	if (!xe->info.probe_display)
435 		return;
436 
437 	if (xe->d3cold.allowed)
438 		xe_display_pm_suspend_late(xe);
439 
440 	/*
441 	 * If xe_display_pm_suspend_late() is not called, it is likely
442 	 * that we will be on dynamic DC states with DMC wakelock enabled. We
443 	 * need to flush the release work in that case.
444 	 */
445 	intel_dmc_wl_flush_release_work(display);
446 }
447 
448 void xe_display_pm_shutdown_late(struct xe_device *xe)
449 {
450 	struct intel_display *display = xe->display;
451 
452 	if (!xe->info.probe_display)
453 		return;
454 
455 	/*
456 	 * The only requirement is to reboot with display DC states disabled,
457 	 * for now leaving all display power wells in the INIT power domain
458 	 * enabled.
459 	 */
460 	intel_power_domains_driver_remove(display);
461 }
462 
463 void xe_display_pm_resume_early(struct xe_device *xe)
464 {
465 	struct intel_display *display = xe->display;
466 
467 	if (!xe->info.probe_display)
468 		return;
469 
470 	intel_display_power_resume_early(display);
471 }
472 
473 void xe_display_pm_resume(struct xe_device *xe)
474 {
475 	struct intel_display *display = xe->display;
476 
477 	if (!xe->info.probe_display)
478 		return;
479 
480 	intel_dmc_resume(display);
481 
482 	if (intel_display_device_present(display))
483 		drm_mode_config_reset(&xe->drm);
484 
485 	intel_display_driver_init_hw(display);
486 
487 	if (intel_display_device_present(display))
488 		intel_display_driver_resume_access(display);
489 
490 	intel_hpd_init(display);
491 
492 	intel_encoder_unblock_all_hpds(display);
493 
494 	if (intel_display_device_present(display)) {
495 		intel_display_driver_resume(display);
496 		drm_kms_helper_poll_enable(&xe->drm);
497 		intel_display_driver_enable_user_access(display);
498 	}
499 
500 	if (intel_display_device_present(display))
501 		intel_hpd_poll_disable(display);
502 
503 	intel_opregion_resume(display);
504 
505 	drm_client_dev_resume(&xe->drm);
506 
507 	intel_power_domains_enable(display);
508 }
509 
510 void xe_display_pm_runtime_resume(struct xe_device *xe)
511 {
512 	struct intel_display *display = xe->display;
513 
514 	if (!xe->info.probe_display)
515 		return;
516 
517 	if (xe->d3cold.allowed) {
518 		xe_display_disable_d3cold(xe);
519 		return;
520 	}
521 
522 	intel_hpd_init(display);
523 	intel_hpd_poll_disable(display);
524 	skl_watermark_ipc_update(display);
525 }
526 
527 
528 static void display_device_remove(struct drm_device *dev, void *arg)
529 {
530 	struct intel_display *display = arg;
531 
532 	intel_display_device_remove(display);
533 }
534 
535 static bool irq_enabled(struct drm_device *drm)
536 {
537 	struct xe_device *xe = to_xe_device(drm);
538 
539 	return atomic_read(&xe->irq.enabled);
540 }
541 
542 static void irq_synchronize(struct drm_device *drm)
543 {
544 	synchronize_irq(to_pci_dev(drm->dev)->irq);
545 }
546 
547 static const struct intel_display_irq_interface xe_display_irq_interface = {
548 	.enabled = irq_enabled,
549 	.synchronize = irq_synchronize,
550 };
551 
552 static bool has_auxccs(struct drm_device *drm)
553 {
554 	struct xe_device *xe = to_xe_device(drm);
555 
556 	return xe->info.platform == XE_ALDERLAKE_P;
557 }
558 
559 static const struct intel_display_parent_interface parent = {
560 	.bo = &xe_display_bo_interface,
561 	.dsb = &xe_display_dsb_interface,
562 	.frontbuffer = &xe_display_frontbuffer_interface,
563 	.hdcp = &xe_display_hdcp_interface,
564 	.initial_plane = &xe_display_initial_plane_interface,
565 	.irq = &xe_display_irq_interface,
566 	.panic = &xe_display_panic_interface,
567 	.pcode = &xe_display_pcode_interface,
568 	.rpm = &xe_display_rpm_interface,
569 	.stolen = &xe_display_stolen_interface,
570 	.has_auxccs = has_auxccs,
571 };
572 
573 /**
574  * xe_display_probe - probe display and create display struct
575  * @xe: XE device instance
576  *
577  * Initialize all fields used by the display part.
578  *
579  * TODO: once everything can be inside a single struct, make the struct opaque
580  * to the rest of xe and return it to be xe->display.
581  *
582  * Returns: 0 on success
583  */
584 int xe_display_probe(struct xe_device *xe)
585 {
586 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
587 	struct intel_display *display;
588 	int err;
589 
590 	if (!xe->info.probe_display)
591 		goto no_display;
592 
593 	display = intel_display_device_probe(pdev, &parent);
594 	if (IS_ERR(display))
595 		return PTR_ERR(display);
596 
597 	err = drmm_add_action_or_reset(&xe->drm, display_device_remove, display);
598 	if (err)
599 		return err;
600 
601 	xe->display = display;
602 
603 	if (intel_display_device_present(display))
604 		return 0;
605 
606 no_display:
607 	xe->info.probe_display = false;
608 	unset_display_features(xe);
609 	return 0;
610 }
611