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