xref: /linux/drivers/gpu/drm/xe/display/xe_display.c (revision 40ccd6aa3e2e05be93394e3cd560c718dedfcc77)
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_fbdev.h"
27 #include "intel_hdcp.h"
28 #include "intel_hotplug.h"
29 #include "intel_opregion.h"
30 #include "xe_module.h"
31 
32 /* Xe device functions */
33 
34 static bool has_display(struct xe_device *xe)
35 {
36 	return HAS_DISPLAY(xe);
37 }
38 
39 /**
40  * xe_display_driver_probe_defer - Detect if we need to wait for other drivers
41  *				   early on
42  * @pdev: PCI device
43  *
44  * Returns: true if probe needs to be deferred, false otherwise
45  */
46 bool xe_display_driver_probe_defer(struct pci_dev *pdev)
47 {
48 	if (!xe_modparam.enable_display)
49 		return 0;
50 
51 	return intel_display_driver_probe_defer(pdev);
52 }
53 
54 /**
55  * xe_display_driver_set_hooks - Add driver flags and hooks for display
56  * @driver: DRM device driver
57  *
58  * Set features and function hooks in @driver that are needed for driving the
59  * display IP. This sets the driver's capability of driving display, regardless
60  * if the device has it enabled
61  */
62 void xe_display_driver_set_hooks(struct drm_driver *driver)
63 {
64 	if (!xe_modparam.enable_display)
65 		return;
66 
67 	driver->driver_features |= DRIVER_MODESET | DRIVER_ATOMIC;
68 }
69 
70 static void unset_display_features(struct xe_device *xe)
71 {
72 	xe->drm.driver_features &= ~(DRIVER_MODESET | DRIVER_ATOMIC);
73 }
74 
75 static void display_destroy(struct drm_device *dev, void *dummy)
76 {
77 	struct xe_device *xe = to_xe_device(dev);
78 
79 	destroy_workqueue(xe->display.hotplug.dp_wq);
80 }
81 
82 /**
83  * xe_display_create - create display struct
84  * @xe: XE device instance
85  *
86  * Initialize all fields used by the display part.
87  *
88  * TODO: once everything can be inside a single struct, make the struct opaque
89  * to the rest of xe and return it to be xe->display.
90  *
91  * Returns: 0 on success
92  */
93 int xe_display_create(struct xe_device *xe)
94 {
95 	spin_lock_init(&xe->display.fb_tracking.lock);
96 
97 	xe->display.hotplug.dp_wq = alloc_ordered_workqueue("xe-dp", 0);
98 
99 	drmm_mutex_init(&xe->drm, &xe->sb_lock);
100 	xe->enabled_irq_mask = ~0;
101 
102 	return drmm_add_action_or_reset(&xe->drm, display_destroy, NULL);
103 }
104 
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.enable_display)
110 		return;
111 
112 	intel_power_domains_cleanup(xe);
113 }
114 
115 int xe_display_init_nommio(struct xe_device *xe)
116 {
117 	if (!xe->info.enable_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 
129 static void xe_display_fini_noirq(struct drm_device *dev, void *dummy)
130 {
131 	struct xe_device *xe = to_xe_device(dev);
132 
133 	if (!xe->info.enable_display)
134 		return;
135 
136 	intel_display_driver_remove_noirq(xe);
137 	intel_power_domains_driver_remove(xe);
138 }
139 
140 int xe_display_init_noirq(struct xe_device *xe)
141 {
142 	int err;
143 
144 	if (!xe->info.enable_display)
145 		return 0;
146 
147 	intel_display_driver_early_probe(xe);
148 
149 	/* Early display init.. */
150 	intel_opregion_setup(xe);
151 
152 	/*
153 	 * Fill the dram structure to get the system dram info. This will be
154 	 * used for memory latency calculation.
155 	 */
156 	intel_dram_detect(xe);
157 
158 	intel_bw_init_hw(xe);
159 
160 	intel_display_device_info_runtime_init(xe);
161 
162 	err = intel_display_driver_probe_noirq(xe);
163 	if (err)
164 		return err;
165 
166 	return drmm_add_action_or_reset(&xe->drm, xe_display_fini_noirq, NULL);
167 }
168 
169 static void xe_display_fini_noaccel(struct drm_device *dev, void *dummy)
170 {
171 	struct xe_device *xe = to_xe_device(dev);
172 
173 	if (!xe->info.enable_display)
174 		return;
175 
176 	intel_display_driver_remove_nogem(xe);
177 }
178 
179 int xe_display_init_noaccel(struct xe_device *xe)
180 {
181 	int err;
182 
183 	if (!xe->info.enable_display)
184 		return 0;
185 
186 	err = intel_display_driver_probe_nogem(xe);
187 	if (err)
188 		return err;
189 
190 	return drmm_add_action_or_reset(&xe->drm, xe_display_fini_noaccel, NULL);
191 }
192 
193 int xe_display_init(struct xe_device *xe)
194 {
195 	if (!xe->info.enable_display)
196 		return 0;
197 
198 	return intel_display_driver_probe(xe);
199 }
200 
201 void xe_display_fini(struct xe_device *xe)
202 {
203 	if (!xe->info.enable_display)
204 		return;
205 
206 	intel_hpd_poll_fini(xe);
207 
208 	intel_hdcp_component_fini(xe);
209 	intel_audio_deinit(xe);
210 }
211 
212 void xe_display_register(struct xe_device *xe)
213 {
214 	if (!xe->info.enable_display)
215 		return;
216 
217 	intel_display_driver_register(xe);
218 	intel_register_dsm_handler();
219 	intel_power_domains_enable(xe);
220 }
221 
222 void xe_display_unregister(struct xe_device *xe)
223 {
224 	if (!xe->info.enable_display)
225 		return;
226 
227 	intel_unregister_dsm_handler();
228 	intel_power_domains_disable(xe);
229 	intel_display_driver_unregister(xe);
230 }
231 
232 void xe_display_driver_remove(struct xe_device *xe)
233 {
234 	if (!xe->info.enable_display)
235 		return;
236 
237 	intel_display_driver_remove(xe);
238 
239 	intel_display_device_remove(xe);
240 }
241 
242 /* IRQ-related functions */
243 
244 void xe_display_irq_handler(struct xe_device *xe, u32 master_ctl)
245 {
246 	if (!xe->info.enable_display)
247 		return;
248 
249 	if (master_ctl & DISPLAY_IRQ)
250 		gen11_display_irq_handler(xe);
251 }
252 
253 void xe_display_irq_enable(struct xe_device *xe, u32 gu_misc_iir)
254 {
255 	if (!xe->info.enable_display)
256 		return;
257 
258 	if (gu_misc_iir & GU_MISC_GSE)
259 		intel_opregion_asle_intr(xe);
260 }
261 
262 void xe_display_irq_reset(struct xe_device *xe)
263 {
264 	if (!xe->info.enable_display)
265 		return;
266 
267 	gen11_display_irq_reset(xe);
268 }
269 
270 void xe_display_irq_postinstall(struct xe_device *xe, struct xe_gt *gt)
271 {
272 	if (!xe->info.enable_display)
273 		return;
274 
275 	if (gt->info.id == XE_GT0)
276 		gen11_de_irq_postinstall(xe);
277 }
278 
279 static void intel_suspend_encoders(struct xe_device *xe)
280 {
281 	struct drm_device *dev = &xe->drm;
282 	struct intel_encoder *encoder;
283 
284 	if (has_display(xe))
285 		return;
286 
287 	drm_modeset_lock_all(dev);
288 	for_each_intel_encoder(dev, encoder)
289 		if (encoder->suspend)
290 			encoder->suspend(encoder);
291 	drm_modeset_unlock_all(dev);
292 }
293 
294 static bool suspend_to_idle(void)
295 {
296 #if IS_ENABLED(CONFIG_ACPI_SLEEP)
297 	if (acpi_target_system_state() < ACPI_STATE_S3)
298 		return true;
299 #endif
300 	return false;
301 }
302 
303 void xe_display_pm_suspend(struct xe_device *xe)
304 {
305 	bool s2idle = suspend_to_idle();
306 	if (!xe->info.enable_display)
307 		return;
308 
309 	/*
310 	 * We do a lot of poking in a lot of registers, make sure they work
311 	 * properly.
312 	 */
313 	intel_power_domains_disable(xe);
314 	if (has_display(xe))
315 		drm_kms_helper_poll_disable(&xe->drm);
316 
317 	intel_display_driver_suspend(xe);
318 
319 	intel_dp_mst_suspend(xe);
320 
321 	intel_hpd_cancel_work(xe);
322 
323 	intel_suspend_encoders(xe);
324 
325 	intel_opregion_suspend(xe, s2idle ? PCI_D1 : PCI_D3cold);
326 
327 	intel_fbdev_set_suspend(&xe->drm, FBINFO_STATE_SUSPENDED, true);
328 
329 	intel_dmc_suspend(xe);
330 }
331 
332 void xe_display_pm_suspend_late(struct xe_device *xe)
333 {
334 	bool s2idle = suspend_to_idle();
335 	if (!xe->info.enable_display)
336 		return;
337 
338 	intel_power_domains_suspend(xe, s2idle);
339 
340 	intel_display_power_suspend_late(xe);
341 }
342 
343 void xe_display_pm_resume_early(struct xe_device *xe)
344 {
345 	if (!xe->info.enable_display)
346 		return;
347 
348 	intel_display_power_resume_early(xe);
349 
350 	intel_power_domains_resume(xe);
351 }
352 
353 void xe_display_pm_resume(struct xe_device *xe)
354 {
355 	if (!xe->info.enable_display)
356 		return;
357 
358 	intel_dmc_resume(xe);
359 
360 	if (has_display(xe))
361 		drm_mode_config_reset(&xe->drm);
362 
363 	intel_display_driver_init_hw(xe);
364 	intel_hpd_init(xe);
365 
366 	/* MST sideband requires HPD interrupts enabled */
367 	intel_dp_mst_resume(xe);
368 	intel_display_driver_resume(xe);
369 
370 	intel_hpd_poll_disable(xe);
371 	if (has_display(xe))
372 		drm_kms_helper_poll_enable(&xe->drm);
373 
374 	intel_opregion_resume(xe);
375 
376 	intel_fbdev_set_suspend(&xe->drm, FBINFO_STATE_RUNNING, false);
377 
378 	intel_power_domains_enable(xe);
379 }
380 
381 void xe_display_probe(struct xe_device *xe)
382 {
383 	if (!xe->info.enable_display)
384 		goto no_display;
385 
386 	intel_display_device_probe(xe);
387 
388 	if (has_display(xe))
389 		return;
390 
391 no_display:
392 	xe->info.enable_display = false;
393 	unset_display_features(xe);
394 }
395