xref: /linux/drivers/gpu/drm/xe/display/xe_display.c (revision 5c9deba5578db5a3ea05be8a3e4a59ecb08ee76f)
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_display.h"
23 #include "intel_display_core.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_fbdev.h"
32 #include "intel_hdcp.h"
33 #include "intel_hotplug.h"
34 #include "intel_opregion.h"
35 #include "skl_watermark.h"
36 #include "xe_device.h"
37 #include "xe_display_bo.h"
38 #include "xe_display_pcode.h"
39 #include "xe_display_rpm.h"
40 #include "xe_dsb_buffer.h"
41 #include "xe_fb_pin.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 static void unset_display_features(struct xe_device *xe)
72 {
73 	xe->drm.driver_features &= ~XE_DISPLAY_DRIVER_FEATURES;
74 }
75 
76 static void xe_display_fini_early(void *arg)
77 {
78 	struct xe_device *xe = arg;
79 	struct intel_display *display = xe->display;
80 
81 	if (!xe->info.probe_display)
82 		return;
83 
84 	intel_display_driver_remove_noirq(display);
85 	intel_display_driver_remove_nogem(display);
86 	intel_display_power_cleanup(display);
87 }
88 
89 int xe_display_init_early(struct xe_device *xe)
90 {
91 	struct intel_display *display = xe->display;
92 	int err;
93 
94 	if (!xe->info.probe_display)
95 		return 0;
96 
97 	/* Fake uncore lock */
98 	spin_lock_init(&xe->uncore.lock);
99 
100 	intel_display_driver_early_probe(display);
101 
102 	intel_display_device_info_runtime_init(display);
103 
104 	/* Display may have been disabled at runtime init */
105 	if (!intel_display_device_present(display)) {
106 		xe->info.probe_display = false;
107 		unset_display_features(xe);
108 		return 0;
109 	}
110 
111 	err = intel_display_driver_probe_noirq(display);
112 	if (err)
113 		return err;
114 
115 	err = intel_display_driver_probe_nogem(display);
116 	if (err)
117 		goto err_noirq;
118 
119 	return devm_add_action_or_reset(xe->drm.dev, xe_display_fini_early, xe);
120 err_noirq:
121 	intel_display_driver_remove_noirq(display);
122 	intel_display_power_cleanup(display);
123 
124 	return err;
125 }
126 
127 static void xe_display_fini(void *arg)
128 {
129 	struct xe_device *xe = arg;
130 	struct intel_display *display = xe->display;
131 
132 	intel_hpd_poll_fini(display);
133 	intel_hdcp_component_fini(display);
134 	intel_audio_deinit(display);
135 	intel_display_driver_remove(display);
136 }
137 
138 int xe_display_init(struct xe_device *xe)
139 {
140 	struct intel_display *display = xe->display;
141 	int err;
142 
143 	if (!xe->info.probe_display)
144 		return 0;
145 
146 	err = intel_display_driver_probe(display);
147 	if (err)
148 		return err;
149 
150 	return devm_add_action_or_reset(xe->drm.dev, xe_display_fini, xe);
151 }
152 
153 void xe_display_register(struct xe_device *xe)
154 {
155 	struct intel_display *display = xe->display;
156 
157 	if (!xe->info.probe_display)
158 		return;
159 
160 	intel_display_driver_register(display);
161 	intel_display_power_enable(display);
162 }
163 
164 void xe_display_unregister(struct xe_device *xe)
165 {
166 	struct intel_display *display = xe->display;
167 
168 	if (!xe->info.probe_display)
169 		return;
170 
171 	intel_display_power_disable(display);
172 	intel_display_driver_unregister(display);
173 }
174 
175 void xe_display_shutdown(struct xe_device *xe)
176 {
177 	struct intel_display *display = xe->display;
178 
179 	if (!xe->info.probe_display)
180 		return;
181 
182 	intel_display_driver_shutdown(display);
183 
184 	intel_opregion_suspend(display, PCI_D3cold);
185 
186 	intel_dmc_suspend(display);
187 }
188 
189 void xe_display_shutdown_late(struct xe_device *xe)
190 {
191 	struct intel_display *display = xe->display;
192 
193 	if (!xe->info.probe_display)
194 		return;
195 
196 	intel_display_driver_shutdown_late(display);
197 }
198 
199 /* IRQ-related functions */
200 
201 void xe_display_irq_handler(struct xe_device *xe, u32 master_ctl)
202 {
203 	struct intel_display *display = xe->display;
204 
205 	if (!xe->info.probe_display)
206 		return;
207 
208 	if (master_ctl & DISPLAY_IRQ)
209 		intel_display_irq_handler(display, NULL);
210 }
211 
212 void xe_display_irq_enable(struct xe_device *xe, u32 gu_misc_iir)
213 {
214 	struct intel_display *display = xe->display;
215 
216 	if (!xe->info.probe_display)
217 		return;
218 
219 	if (gu_misc_iir & GU_MISC_GSE)
220 		intel_opregion_asle_intr(display);
221 }
222 
223 void xe_display_irq_reset(struct xe_device *xe)
224 {
225 	struct intel_display *display = xe->display;
226 
227 	if (!xe->info.probe_display)
228 		return;
229 
230 	intel_display_irq_reset(display);
231 }
232 
233 void xe_display_irq_postinstall(struct xe_device *xe)
234 {
235 	struct intel_display *display = xe->display;
236 
237 	if (!xe->info.probe_display)
238 		return;
239 
240 	intel_display_irq_postinstall(display);
241 }
242 
243 static bool suspend_to_idle(void)
244 {
245 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
246 	if (acpi_target_system_state() < ACPI_STATE_S3)
247 		return true;
248 #endif
249 	return false;
250 }
251 
252 void xe_display_pm_suspend(struct xe_device *xe)
253 {
254 	struct intel_display *display = xe->display;
255 	bool s2idle = suspend_to_idle();
256 
257 	if (!xe->info.probe_display)
258 		return;
259 
260 	intel_display_driver_pm_suspend(display);
261 
262 	intel_opregion_suspend(display, s2idle ? PCI_D1 : PCI_D3cold);
263 
264 	intel_dmc_suspend(display);
265 }
266 
267 void xe_display_pm_suspend_late(struct xe_device *xe)
268 {
269 	struct intel_display *display = xe->display;
270 	bool s2idle = suspend_to_idle();
271 
272 	if (!xe->info.probe_display)
273 		return;
274 
275 	intel_display_driver_pm_suspend_late(display, s2idle);
276 }
277 
278 void xe_display_pm_resume_early(struct xe_device *xe)
279 {
280 	struct intel_display *display = xe->display;
281 
282 	if (!xe->info.probe_display)
283 		return;
284 
285 	intel_display_driver_pm_resume_early(display);
286 }
287 
288 void xe_display_pm_resume(struct xe_device *xe)
289 {
290 	struct intel_display *display = xe->display;
291 
292 	if (!xe->info.probe_display)
293 		return;
294 
295 	intel_dmc_resume(display);
296 
297 	if (intel_display_device_present(display))
298 		drm_mode_config_reset(&xe->drm);
299 
300 	intel_display_driver_init_hw(display);
301 
302 	intel_display_driver_pm_resume(display);
303 }
304 
305 static void xe_display_enable_d3cold(struct xe_device *xe)
306 {
307 	struct intel_display *display = xe->display;
308 
309 	if (!xe->info.probe_display)
310 		return;
311 
312 	/*
313 	 * We do a lot of poking in a lot of registers, make sure they work
314 	 * properly.
315 	 */
316 	intel_display_power_disable(display);
317 
318 	intel_display_flush_cleanup_work(display);
319 
320 	intel_opregion_suspend(display, PCI_D3cold);
321 
322 	intel_dmc_suspend(display);
323 
324 	if (intel_display_device_present(display))
325 		intel_hpd_poll_enable(display);
326 }
327 
328 static void xe_display_disable_d3cold(struct xe_device *xe)
329 {
330 	struct intel_display *display = xe->display;
331 
332 	if (!xe->info.probe_display)
333 		return;
334 
335 	intel_dmc_resume(display);
336 
337 	if (intel_display_device_present(display))
338 		drm_mode_config_reset(&xe->drm);
339 
340 	intel_display_driver_init_hw(display);
341 
342 	intel_hpd_init(display);
343 
344 	if (intel_display_device_present(display))
345 		intel_hpd_poll_disable(display);
346 
347 	intel_opregion_resume(display);
348 
349 	intel_display_power_enable(display);
350 }
351 
352 void xe_display_pm_runtime_suspend(struct xe_device *xe)
353 {
354 	struct intel_display *display = xe->display;
355 
356 	if (!xe->info.probe_display)
357 		return;
358 
359 	if (xe->d3cold.allowed) {
360 		xe_display_enable_d3cold(xe);
361 		return;
362 	}
363 
364 	intel_hpd_poll_enable(display);
365 }
366 
367 void xe_display_pm_runtime_suspend_late(struct xe_device *xe)
368 {
369 	struct intel_display *display = xe->display;
370 
371 	if (!xe->info.probe_display)
372 		return;
373 
374 	if (xe->d3cold.allowed)
375 		xe_display_pm_suspend_late(xe);
376 
377 	/*
378 	 * If xe_display_pm_suspend_late() is not called, it is likely
379 	 * that we will be on dynamic DC states with DMC wakelock enabled. We
380 	 * need to flush the release work in that case.
381 	 */
382 	intel_dmc_wl_flush_release_work(display);
383 }
384 
385 void xe_display_pm_runtime_resume(struct xe_device *xe)
386 {
387 	struct intel_display *display = xe->display;
388 
389 	if (!xe->info.probe_display)
390 		return;
391 
392 	if (xe->d3cold.allowed) {
393 		xe_display_disable_d3cold(xe);
394 		return;
395 	}
396 
397 	intel_hpd_init(display);
398 	intel_hpd_poll_disable(display);
399 	skl_watermark_ipc_update(display);
400 }
401 
402 
403 static void display_device_remove(struct drm_device *dev, void *arg)
404 {
405 	struct xe_device *xe = arg;
406 
407 	intel_display_device_remove(xe->display);
408 	xe->display = NULL;
409 }
410 
411 static bool irq_enabled(struct drm_device *drm)
412 {
413 	struct xe_device *xe = to_xe_device(drm);
414 
415 	return atomic_read(&xe->irq.enabled);
416 }
417 
418 static void irq_synchronize(struct drm_device *drm)
419 {
420 	synchronize_irq(to_pci_dev(drm->dev)->irq);
421 }
422 
423 static const struct intel_display_irq_interface xe_display_irq_interface = {
424 	.enabled = irq_enabled,
425 	.synchronize = irq_synchronize,
426 };
427 
428 static bool has_auxccs(struct drm_device *drm)
429 {
430 	struct xe_device *xe = to_xe_device(drm);
431 
432 	return xe->info.platform == XE_ALDERLAKE_P;
433 }
434 
435 static const struct intel_display_parent_interface parent = {
436 	.bo = &xe_display_bo_interface,
437 	.dsb = &xe_display_dsb_interface,
438 	.fb_pin = &xe_display_fb_pin_interface,
439 	.frontbuffer = &xe_display_frontbuffer_interface,
440 	.hdcp = &xe_display_hdcp_interface,
441 	.initial_plane = &xe_display_initial_plane_interface,
442 	.irq = &xe_display_irq_interface,
443 	.panic = &xe_display_panic_interface,
444 	.pcode = &xe_display_pcode_interface,
445 	.rpm = &xe_display_rpm_interface,
446 	.stolen = &xe_display_stolen_interface,
447 	.has_auxccs = has_auxccs,
448 };
449 
450 /**
451  * xe_display_probe - probe display and create display struct
452  * @xe: XE device instance
453  *
454  * Initialize all fields used by the display part.
455  *
456  * TODO: once everything can be inside a single struct, make the struct opaque
457  * to the rest of xe and return it to be xe->display.
458  *
459  * Returns: 0 on success
460  */
461 int xe_display_probe(struct xe_device *xe)
462 {
463 	struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
464 	struct intel_display *display;
465 	int err;
466 
467 	if (!xe->info.probe_display)
468 		goto no_display;
469 
470 	display = intel_display_device_probe(pdev, &parent);
471 	if (IS_ERR(display))
472 		return PTR_ERR(display);
473 
474 	xe->display = display;
475 
476 	err = drmm_add_action_or_reset(&xe->drm, display_device_remove, xe);
477 	if (err)
478 		return err;
479 
480 	if (intel_display_device_present(display))
481 		return 0;
482 
483 no_display:
484 	xe->info.probe_display = false;
485 	unset_display_features(xe);
486 	return 0;
487 }
488 
489 #ifdef CONFIG_DRM_FBDEV_EMULATION
490 int xe_display_driver_fbdev_probe(struct drm_fb_helper *fbh,
491 				  struct drm_fb_helper_surface_size *sizes)
492 {
493 	return intel_fbdev_driver_fbdev_probe(fbh, sizes);
494 }
495 #endif
496