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