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