xref: /linux/drivers/gpu/drm/xe/display/xe_display.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
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 <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 
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  */
47 bool xe_display_driver_probe_defer(struct pci_dev *pdev)
48 {
49 	if (!xe_modparam.enable_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  */
63 void xe_display_driver_set_hooks(struct drm_driver *driver)
64 {
65 	if (!xe_modparam.enable_display)
66 		return;
67 
68 	driver->driver_features |= DRIVER_MODESET | DRIVER_ATOMIC;
69 }
70 
71 static void unset_display_features(struct xe_device *xe)
72 {
73 	xe->drm.driver_features &= ~(DRIVER_MODESET | DRIVER_ATOMIC);
74 }
75 
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  */
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 
100 	return drmm_add_action_or_reset(&xe->drm, display_destroy, NULL);
101 }
102 
103 static void xe_display_fini_nommio(struct drm_device *dev, void *dummy)
104 {
105 	struct xe_device *xe = to_xe_device(dev);
106 
107 	if (!xe->info.enable_display)
108 		return;
109 
110 	intel_power_domains_cleanup(xe);
111 }
112 
113 int xe_display_init_nommio(struct xe_device *xe)
114 {
115 	if (!xe->info.enable_display)
116 		return 0;
117 
118 	/* Fake uncore lock */
119 	spin_lock_init(&xe->uncore.lock);
120 
121 	/* This must be called before any calls to HAS_PCH_* */
122 	intel_detect_pch(xe);
123 
124 	return drmm_add_action_or_reset(&xe->drm, xe_display_fini_nommio, xe);
125 }
126 
127 static void xe_display_fini_noirq(void *arg)
128 {
129 	struct xe_device *xe = arg;
130 
131 	if (!xe->info.enable_display)
132 		return;
133 
134 	intel_display_driver_remove_noirq(xe);
135 }
136 
137 int xe_display_init_noirq(struct xe_device *xe)
138 {
139 	int err;
140 
141 	if (!xe->info.enable_display)
142 		return 0;
143 
144 	intel_display_driver_early_probe(xe);
145 
146 	/* Early display init.. */
147 	intel_opregion_setup(xe);
148 
149 	/*
150 	 * Fill the dram structure to get the system dram info. This will be
151 	 * used for memory latency calculation.
152 	 */
153 	intel_dram_detect(xe);
154 
155 	intel_bw_init_hw(xe);
156 
157 	intel_display_device_info_runtime_init(xe);
158 
159 	err = intel_display_driver_probe_noirq(xe);
160 	if (err)
161 		return err;
162 
163 	return devm_add_action_or_reset(xe->drm.dev, xe_display_fini_noirq, xe);
164 }
165 
166 static void xe_display_fini_noaccel(void *arg)
167 {
168 	struct xe_device *xe = arg;
169 
170 	if (!xe->info.enable_display)
171 		return;
172 
173 	intel_display_driver_remove_nogem(xe);
174 }
175 
176 int xe_display_init_noaccel(struct xe_device *xe)
177 {
178 	int err;
179 
180 	if (!xe->info.enable_display)
181 		return 0;
182 
183 	err = intel_display_driver_probe_nogem(xe);
184 	if (err)
185 		return err;
186 
187 	return devm_add_action_or_reset(xe->drm.dev, xe_display_fini_noaccel, xe);
188 }
189 
190 int xe_display_init(struct xe_device *xe)
191 {
192 	if (!xe->info.enable_display)
193 		return 0;
194 
195 	return intel_display_driver_probe(xe);
196 }
197 
198 void xe_display_fini(struct xe_device *xe)
199 {
200 	if (!xe->info.enable_display)
201 		return;
202 
203 	intel_hpd_poll_fini(xe);
204 
205 	intel_hdcp_component_fini(xe);
206 	intel_audio_deinit(xe);
207 }
208 
209 void xe_display_register(struct xe_device *xe)
210 {
211 	if (!xe->info.enable_display)
212 		return;
213 
214 	intel_display_driver_register(xe);
215 	intel_register_dsm_handler();
216 	intel_power_domains_enable(xe);
217 }
218 
219 void xe_display_unregister(struct xe_device *xe)
220 {
221 	if (!xe->info.enable_display)
222 		return;
223 
224 	intel_unregister_dsm_handler();
225 	intel_power_domains_disable(xe);
226 	intel_display_driver_unregister(xe);
227 }
228 
229 void xe_display_driver_remove(struct xe_device *xe)
230 {
231 	if (!xe->info.enable_display)
232 		return;
233 
234 	intel_display_driver_remove(xe);
235 }
236 
237 /* IRQ-related functions */
238 
239 void xe_display_irq_handler(struct xe_device *xe, u32 master_ctl)
240 {
241 	if (!xe->info.enable_display)
242 		return;
243 
244 	if (master_ctl & DISPLAY_IRQ)
245 		gen11_display_irq_handler(xe);
246 }
247 
248 void xe_display_irq_enable(struct xe_device *xe, u32 gu_misc_iir)
249 {
250 	if (!xe->info.enable_display)
251 		return;
252 
253 	if (gu_misc_iir & GU_MISC_GSE)
254 		intel_opregion_asle_intr(xe);
255 }
256 
257 void xe_display_irq_reset(struct xe_device *xe)
258 {
259 	if (!xe->info.enable_display)
260 		return;
261 
262 	gen11_display_irq_reset(xe);
263 }
264 
265 void xe_display_irq_postinstall(struct xe_device *xe, struct xe_gt *gt)
266 {
267 	if (!xe->info.enable_display)
268 		return;
269 
270 	if (gt->info.id == XE_GT0)
271 		gen11_de_irq_postinstall(xe);
272 }
273 
274 static bool suspend_to_idle(void)
275 {
276 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
277 	if (acpi_target_system_state() < ACPI_STATE_S3)
278 		return true;
279 #endif
280 	return false;
281 }
282 
283 void xe_display_pm_suspend(struct xe_device *xe, bool runtime)
284 {
285 	bool s2idle = suspend_to_idle();
286 	if (!xe->info.enable_display)
287 		return;
288 
289 	/*
290 	 * We do a lot of poking in a lot of registers, make sure they work
291 	 * properly.
292 	 */
293 	intel_power_domains_disable(xe);
294 	if (has_display(xe))
295 		drm_kms_helper_poll_disable(&xe->drm);
296 
297 	if (!runtime)
298 		intel_display_driver_suspend(xe);
299 
300 	intel_dp_mst_suspend(xe);
301 
302 	intel_hpd_cancel_work(xe);
303 
304 	intel_encoder_suspend_all(&xe->display);
305 
306 	intel_opregion_suspend(xe, s2idle ? PCI_D1 : PCI_D3cold);
307 
308 	intel_fbdev_set_suspend(&xe->drm, FBINFO_STATE_SUSPENDED, true);
309 
310 	intel_dmc_suspend(xe);
311 }
312 
313 void xe_display_pm_suspend_late(struct xe_device *xe)
314 {
315 	bool s2idle = suspend_to_idle();
316 	if (!xe->info.enable_display)
317 		return;
318 
319 	intel_power_domains_suspend(xe, s2idle);
320 
321 	intel_display_power_suspend_late(xe);
322 }
323 
324 void xe_display_pm_resume_early(struct xe_device *xe)
325 {
326 	if (!xe->info.enable_display)
327 		return;
328 
329 	intel_display_power_resume_early(xe);
330 
331 	intel_power_domains_resume(xe);
332 }
333 
334 void xe_display_pm_resume(struct xe_device *xe, bool runtime)
335 {
336 	if (!xe->info.enable_display)
337 		return;
338 
339 	intel_dmc_resume(xe);
340 
341 	if (has_display(xe))
342 		drm_mode_config_reset(&xe->drm);
343 
344 	intel_display_driver_init_hw(xe);
345 	intel_hpd_init(xe);
346 
347 	/* MST sideband requires HPD interrupts enabled */
348 	intel_dp_mst_resume(xe);
349 	if (!runtime)
350 		intel_display_driver_resume(xe);
351 
352 	intel_hpd_poll_disable(xe);
353 	if (has_display(xe))
354 		drm_kms_helper_poll_enable(&xe->drm);
355 
356 	intel_opregion_resume(xe);
357 
358 	intel_fbdev_set_suspend(&xe->drm, FBINFO_STATE_RUNNING, false);
359 
360 	intel_power_domains_enable(xe);
361 }
362 
363 static void display_device_remove(struct drm_device *dev, void *arg)
364 {
365 	struct xe_device *xe = arg;
366 
367 	intel_display_device_remove(xe);
368 }
369 
370 int xe_display_probe(struct xe_device *xe)
371 {
372 	int err;
373 
374 	if (!xe->info.enable_display)
375 		goto no_display;
376 
377 	intel_display_device_probe(xe);
378 
379 	err = drmm_add_action_or_reset(&xe->drm, display_device_remove, xe);
380 	if (err)
381 		return err;
382 
383 	if (has_display(xe))
384 		return 0;
385 
386 no_display:
387 	xe->info.enable_display = false;
388 	unset_display_features(xe);
389 	return 0;
390 }
391