xref: /linux/drivers/gpu/drm/i915/display/intel_display_driver.c (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022-2023 Intel Corporation
4  *
5  * High level display driver entry points. This is a layer between top level
6  * driver code and low level display functionality; no low level display code or
7  * details here.
8  */
9 
10 #include <linux/vga_switcheroo.h>
11 #include <acpi/video.h>
12 #include <drm/display/drm_dp_mst_helper.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_client_event.h>
15 #include <drm/drm_mode_config.h>
16 #include <drm/drm_privacy_screen_consumer.h>
17 #include <drm/drm_print.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_vblank.h>
20 
21 #include "i9xx_wm.h"
22 #include "intel_acpi.h"
23 #include "intel_atomic.h"
24 #include "intel_audio.h"
25 #include "intel_bios.h"
26 #include "intel_bw.h"
27 #include "intel_cdclk.h"
28 #include "intel_color.h"
29 #include "intel_crtc.h"
30 #include "intel_cursor.h"
31 #include "intel_dbuf_bw.h"
32 #include "intel_display_core.h"
33 #include "intel_display_debugfs.h"
34 #include "intel_display_driver.h"
35 #include "intel_display_irq.h"
36 #include "intel_display_power.h"
37 #include "intel_display_types.h"
38 #include "intel_display_utils.h"
39 #include "intel_display_wa.h"
40 #include "intel_dkl_phy.h"
41 #include "intel_dmc.h"
42 #include "intel_dp.h"
43 #include "intel_dp_tunnel.h"
44 #include "intel_dpll.h"
45 #include "intel_dpll_mgr.h"
46 #include "intel_dram.h"
47 #include "intel_encoder.h"
48 #include "intel_fb.h"
49 #include "intel_fbc.h"
50 #include "intel_fbdev.h"
51 #include "intel_fdi.h"
52 #include "intel_flipq.h"
53 #include "intel_gmbus.h"
54 #include "intel_hdcp.h"
55 #include "intel_hotplug.h"
56 #include "intel_hti.h"
57 #include "intel_initial_plane.h"
58 #include "intel_modeset_lock.h"
59 #include "intel_modeset_setup.h"
60 #include "intel_opregion.h"
61 #include "intel_overlay.h"
62 #include "intel_pmdemand.h"
63 #include "intel_pps.h"
64 #include "intel_psr.h"
65 #include "intel_quirks.h"
66 #include "intel_vga.h"
67 #include "intel_wm.h"
68 #include "skl_watermark.h"
69 
70 static int __intel_display_driver_pm_suspend(struct intel_display *display, bool shutdown);
71 
72 bool intel_display_driver_probe_defer(struct pci_dev *pdev)
73 {
74 	struct drm_privacy_screen *privacy_screen;
75 
76 	/*
77 	 * apple-gmux is needed on dual GPU MacBook Pro
78 	 * to probe the panel if we're the inactive GPU.
79 	 */
80 	if (vga_switcheroo_client_probe_defer(pdev))
81 		return true;
82 
83 	/* If the LCD panel has a privacy-screen, wait for it */
84 	privacy_screen = drm_privacy_screen_get(&pdev->dev, NULL);
85 	if (IS_ERR(privacy_screen) && PTR_ERR(privacy_screen) == -EPROBE_DEFER)
86 		return true;
87 
88 	drm_privacy_screen_put(privacy_screen);
89 
90 	return false;
91 }
92 
93 void intel_display_driver_init_hw(struct intel_display *display)
94 {
95 	if (!HAS_DISPLAY(display))
96 		return;
97 
98 	intel_cdclk_read_hw(display);
99 
100 	intel_display_wa_apply(display);
101 }
102 
103 static const struct drm_mode_config_funcs intel_mode_funcs = {
104 	.fb_create = intel_user_framebuffer_create,
105 	.get_format_info = intel_fb_get_format_info,
106 	.mode_valid = intel_mode_valid,
107 	.atomic_check = intel_atomic_check,
108 	.atomic_commit = intel_atomic_commit,
109 	.atomic_state_alloc = intel_atomic_state_alloc,
110 	.atomic_state_clear = intel_atomic_state_clear,
111 	.atomic_state_free = intel_atomic_state_free,
112 };
113 
114 static const struct drm_mode_config_helper_funcs intel_mode_config_funcs = {
115 	.atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
116 };
117 
118 static void intel_mode_config_init(struct intel_display *display)
119 {
120 	struct drm_mode_config *mode_config = &display->drm->mode_config;
121 
122 	drm_mode_config_init(display->drm);
123 	INIT_LIST_HEAD(&display->global.obj_list);
124 	INIT_LIST_HEAD(&display->pipe_list);
125 
126 	mode_config->min_width = 0;
127 	mode_config->min_height = 0;
128 
129 	mode_config->preferred_depth = 24;
130 	mode_config->prefer_shadow = 1;
131 
132 	mode_config->funcs = &intel_mode_funcs;
133 	mode_config->helper_private = &intel_mode_config_funcs;
134 
135 	mode_config->async_page_flip = HAS_ASYNC_FLIPS(display);
136 
137 	/*
138 	 * Maximum framebuffer dimensions, chosen to match
139 	 * the maximum render engine surface size on gen4+.
140 	 */
141 	if (DISPLAY_VER(display) >= 7) {
142 		mode_config->max_width = 16384;
143 		mode_config->max_height = 16384;
144 	} else if (DISPLAY_VER(display) >= 4) {
145 		mode_config->max_width = 8192;
146 		mode_config->max_height = 8192;
147 	} else if (DISPLAY_VER(display) == 3) {
148 		mode_config->max_width = 4096;
149 		mode_config->max_height = 4096;
150 	} else {
151 		mode_config->max_width = 2048;
152 		mode_config->max_height = 2048;
153 	}
154 
155 	intel_cursor_mode_config_init(display);
156 }
157 
158 static void intel_mode_config_cleanup(struct intel_display *display)
159 {
160 	intel_atomic_global_obj_cleanup(display);
161 	drm_mode_config_cleanup(display->drm);
162 }
163 
164 static void intel_plane_possible_crtcs_init(struct intel_display *display)
165 {
166 	struct intel_plane *plane;
167 
168 	for_each_intel_plane(display->drm, plane) {
169 		struct intel_crtc *crtc = intel_crtc_for_pipe(display,
170 							      plane->pipe);
171 
172 		plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
173 	}
174 }
175 
176 void intel_display_driver_early_probe(struct intel_display *display)
177 {
178 	/* This must be called before any calls to HAS_PCH_* */
179 	intel_pch_detect(display);
180 
181 	if (!HAS_DISPLAY(display))
182 		return;
183 
184 	spin_lock_init(&display->fb_tracking.lock);
185 	mutex_init(&display->backlight.lock);
186 	mutex_init(&display->audio.mutex);
187 	mutex_init(&display->wm.wm_mutex);
188 	mutex_init(&display->pps.mutex);
189 	mutex_init(&display->hdcp.hdcp_mutex);
190 
191 	intel_display_irq_init(display);
192 	intel_dkl_phy_init(display);
193 	intel_color_init_hooks(display);
194 	intel_init_cdclk_hooks(display);
195 	intel_audio_hooks_init(display);
196 	intel_dpll_init_clock_hook(display);
197 	intel_init_display_hooks(display);
198 	intel_fdi_init_hook(display);
199 	intel_dmc_wl_init(display);
200 }
201 
202 /* part #1: call before irq install */
203 int intel_display_driver_probe_noirq(struct intel_display *display)
204 {
205 	int ret;
206 
207 	intel_opregion_setup(display);
208 
209 	/*
210 	 * Fill the dram structure to get the system dram info. This will be
211 	 * used for memory latency calculation.
212 	 */
213 	ret = intel_dram_detect(display);
214 	if (ret)
215 		goto cleanup_opregion;
216 
217 	intel_bw_init_hw(display);
218 
219 	if (HAS_DISPLAY(display)) {
220 		ret = drm_vblank_init(display->drm,
221 				      INTEL_NUM_PIPES(display));
222 		if (ret)
223 			goto cleanup_opregion;
224 	}
225 
226 	intel_bios_init(display);
227 
228 	intel_psr_dc5_dc6_wa_init(display);
229 
230 	/* FIXME: completely on the wrong abstraction layer */
231 	ret = intel_display_power_init(display);
232 	if (ret < 0)
233 		goto cleanup_bios;
234 
235 	intel_pmdemand_init_early(display);
236 
237 	intel_display_power_init_hw(display);
238 
239 	if (!HAS_DISPLAY(display))
240 		return 0;
241 
242 	display->hotplug.dp_wq = alloc_ordered_workqueue("intel-dp", 0);
243 	if (!display->hotplug.dp_wq) {
244 		ret = -ENOMEM;
245 		goto cleanup_pw_domain_dmc;
246 	}
247 
248 	display->wq.modeset = alloc_ordered_workqueue("i915_modeset", 0);
249 	if (!display->wq.modeset) {
250 		ret = -ENOMEM;
251 		goto cleanup_wq_dp;
252 	}
253 
254 	display->wq.flip = alloc_workqueue("i915_flip", WQ_HIGHPRI |
255 						WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
256 	if (!display->wq.flip) {
257 		ret = -ENOMEM;
258 		goto cleanup_wq_modeset;
259 	}
260 
261 	display->wq.cleanup = alloc_workqueue("i915_cleanup", WQ_HIGHPRI | WQ_PERCPU, 0);
262 	if (!display->wq.cleanup) {
263 		ret = -ENOMEM;
264 		goto cleanup_wq_flip;
265 	}
266 
267 	display->wq.unordered = alloc_workqueue("display_unordered", WQ_PERCPU, 0);
268 	if (!display->wq.unordered) {
269 		ret = -ENOMEM;
270 		goto cleanup_wq_cleanup;
271 	}
272 
273 	intel_dmc_init(display);
274 
275 	intel_mode_config_init(display);
276 
277 	ret = intel_cdclk_init(display);
278 	if (ret)
279 		goto cleanup_wq_unordered;
280 
281 	ret = intel_color_init(display);
282 	if (ret)
283 		goto cleanup_wq_unordered;
284 
285 	ret = intel_dbuf_init(display);
286 	if (ret)
287 		goto cleanup_wq_unordered;
288 
289 	ret = intel_dbuf_bw_init(display);
290 	if (ret)
291 		goto cleanup_wq_unordered;
292 
293 	ret = intel_bw_init(display);
294 	if (ret)
295 		goto cleanup_wq_unordered;
296 
297 	ret = intel_pmdemand_init(display);
298 	if (ret)
299 		goto cleanup_wq_unordered;
300 
301 	intel_init_quirks(display);
302 
303 	intel_fbc_init(display);
304 
305 	return 0;
306 
307 cleanup_wq_unordered:
308 	destroy_workqueue(display->wq.unordered);
309 cleanup_wq_cleanup:
310 	destroy_workqueue(display->wq.cleanup);
311 cleanup_wq_flip:
312 	destroy_workqueue(display->wq.flip);
313 cleanup_wq_modeset:
314 	destroy_workqueue(display->wq.modeset);
315 cleanup_wq_dp:
316 	destroy_workqueue(display->hotplug.dp_wq);
317 cleanup_pw_domain_dmc:
318 	intel_dmc_fini(display);
319 	intel_display_power_driver_remove(display);
320 cleanup_bios:
321 	intel_bios_driver_remove(display);
322 cleanup_opregion:
323 	intel_opregion_cleanup(display);
324 
325 	return ret;
326 }
327 ALLOW_ERROR_INJECTION(intel_display_driver_probe_noirq, ERRNO);
328 
329 static void set_display_access(struct intel_display *display,
330 			       bool any_task_allowed,
331 			       struct task_struct *allowed_task)
332 {
333 	struct drm_modeset_acquire_ctx ctx;
334 	int err;
335 
336 	intel_modeset_lock_ctx_retry(&ctx, NULL, 0, err) {
337 		err = drm_modeset_lock_all_ctx(display->drm, &ctx);
338 		if (err)
339 			continue;
340 
341 		display->access.any_task_allowed = any_task_allowed;
342 		display->access.allowed_task = allowed_task;
343 	}
344 
345 	drm_WARN_ON(display->drm, err);
346 }
347 
348 /**
349  * intel_display_driver_enable_user_access - Enable display HW access for all threads
350  * @display: display device instance
351  *
352  * Enable the display HW access for all threads. Examples for such accesses
353  * are modeset commits and connector probing.
354  *
355  * This function should be called during driver loading and system resume once
356  * all the HW initialization steps are done.
357  */
358 void intel_display_driver_enable_user_access(struct intel_display *display)
359 {
360 	set_display_access(display, true, NULL);
361 
362 	intel_hpd_enable_detection_work(display);
363 }
364 
365 /**
366  * intel_display_driver_disable_user_access - Disable display HW access for user threads
367  * @display: display device instance
368  *
369  * Disable the display HW access for user threads. Examples for such accesses
370  * are modeset commits and connector probing. For the current thread the
371  * access is still enabled, which should only perform HW init/deinit
372  * programming (as the initial modeset during driver loading or the disabling
373  * modeset during driver unloading and system suspend/shutdown). This function
374  * should be followed by calling either intel_display_driver_enable_user_access()
375  * after completing the HW init programming or
376  * intel_display_driver_suspend_access() after completing the HW deinit
377  * programming.
378  *
379  * This function should be called during driver loading/unloading and system
380  * suspend/shutdown before starting the HW init/deinit programming.
381  */
382 void intel_display_driver_disable_user_access(struct intel_display *display)
383 {
384 	intel_hpd_disable_detection_work(display);
385 
386 	set_display_access(display, false, current);
387 }
388 
389 /**
390  * intel_display_driver_suspend_access - Suspend display HW access for all threads
391  * @display: display device instance
392  *
393  * Disable the display HW access for all threads. Examples for such accesses
394  * are modeset commits and connector probing. This call should be either
395  * followed by calling intel_display_driver_resume_access(), or the driver
396  * should be unloaded/shutdown.
397  *
398  * This function should be called during driver unloading and system
399  * suspend/shutdown after completing the HW deinit programming.
400  */
401 void intel_display_driver_suspend_access(struct intel_display *display)
402 {
403 	set_display_access(display, false, NULL);
404 }
405 
406 /**
407  * intel_display_driver_resume_access - Resume display HW access for the resume thread
408  * @display: display device instance
409  *
410  * Enable the display HW access for the current resume thread, keeping the
411  * access disabled for all other (user) threads. Examples for such accesses
412  * are modeset commits and connector probing. The resume thread should only
413  * perform HW init programming (as the restoring modeset). This function
414  * should be followed by calling intel_display_driver_enable_user_access(),
415  * after completing the HW init programming steps.
416  *
417  * This function should be called during system resume before starting the HW
418  * init steps.
419  */
420 void intel_display_driver_resume_access(struct intel_display *display)
421 {
422 	set_display_access(display, false, current);
423 }
424 
425 /**
426  * intel_display_driver_check_access - Check if the current thread has disaplay HW access
427  * @display: display device instance
428  *
429  * Check whether the current thread has display HW access, print a debug
430  * message if it doesn't. Such accesses are modeset commits and connector
431  * probing. If the function returns %false any HW access should be prevented.
432  *
433  * Returns %true if the current thread has display HW access, %false
434  * otherwise.
435  */
436 bool intel_display_driver_check_access(struct intel_display *display)
437 {
438 	char current_task[TASK_COMM_LEN + 16];
439 	char allowed_task[TASK_COMM_LEN + 16] = "none";
440 
441 	if (display->access.any_task_allowed ||
442 	    display->access.allowed_task == current)
443 		return true;
444 
445 	snprintf(current_task, sizeof(current_task), "%s[%d]",
446 		 current->comm, task_pid_vnr(current));
447 
448 	if (display->access.allowed_task)
449 		snprintf(allowed_task, sizeof(allowed_task), "%s[%d]",
450 			 display->access.allowed_task->comm,
451 			 task_pid_vnr(display->access.allowed_task));
452 
453 	drm_dbg_kms(display->drm,
454 		    "Reject display access from task %s (allowed to %s)\n",
455 		    current_task, allowed_task);
456 
457 	return false;
458 }
459 
460 /* part #2: call after irq install, but before gem init */
461 int intel_display_driver_probe_nogem(struct intel_display *display)
462 {
463 	int ret;
464 
465 	if (!HAS_DISPLAY(display))
466 		return 0;
467 
468 	intel_wm_init(display);
469 
470 	intel_panel_sanitize_ssc(display);
471 
472 	intel_pps_setup(display);
473 
474 	intel_gmbus_setup(display);
475 
476 	ret = intel_crtc_init(display);
477 	if (ret)
478 		goto err_mode_config;
479 
480 	intel_plane_possible_crtcs_init(display);
481 	intel_dpll_init(display);
482 	intel_fdi_pll_freq_update(display);
483 
484 	intel_display_driver_init_hw(display);
485 	intel_dpll_update_ref_clks(display);
486 
487 	if (display->cdclk.max_cdclk_freq == 0)
488 		intel_update_max_cdclk(display);
489 
490 	intel_hti_init(display);
491 
492 	intel_setup_outputs(display);
493 
494 	ret = intel_dp_tunnel_mgr_init(display);
495 	if (ret)
496 		goto err_hdcp;
497 
498 	intel_display_driver_disable_user_access(display);
499 
500 	drm_modeset_lock_all(display->drm);
501 	intel_modeset_setup_hw_state(display, display->drm->mode_config.acquire_ctx);
502 	intel_acpi_assign_connector_fwnodes(display);
503 	drm_modeset_unlock_all(display->drm);
504 
505 	intel_initial_plane_config(display);
506 
507 	/*
508 	 * Make sure hardware watermarks really match the state we read out.
509 	 * Note that we need to do this after reconstructing the BIOS fb's
510 	 * since the watermark calculation done here will use pstate->fb.
511 	 */
512 	if (!HAS_GMCH(display))
513 		ilk_wm_sanitize(display);
514 
515 	return 0;
516 
517 err_hdcp:
518 	intel_hdcp_component_fini(display);
519 err_mode_config:
520 	intel_mode_config_cleanup(display);
521 
522 	return ret;
523 }
524 
525 /* part #3: call after gem init */
526 int intel_display_driver_probe(struct intel_display *display)
527 {
528 	int ret;
529 
530 	if (!HAS_DISPLAY(display))
531 		return 0;
532 
533 	/*
534 	 * This will bind stuff into ggtt, so it needs to be done after
535 	 * the BIOS fb takeover and whatever else magic ggtt reservations
536 	 * happen during gem/ggtt init.
537 	 */
538 	intel_hdcp_component_init(display);
539 
540 	intel_flipq_init(display);
541 
542 	/*
543 	 * Force all active planes to recompute their states. So that on
544 	 * mode_setcrtc after probe, all the intel_plane_state variables
545 	 * are already calculated and there is no assert_plane warnings
546 	 * during bootup.
547 	 */
548 	ret = intel_initial_commit(display);
549 	if (ret)
550 		drm_dbg_kms(display->drm, "Initial modeset failed, %d\n", ret);
551 
552 	intel_overlay_setup(display);
553 
554 	/* Only enable hotplug handling once the fbdev is fully set up. */
555 	intel_hpd_init(display);
556 
557 	skl_watermark_ipc_init(display);
558 
559 	return 0;
560 }
561 
562 void intel_display_driver_register(struct intel_display *display)
563 {
564 	struct drm_printer p = drm_dbg_printer(display->drm, DRM_UT_KMS,
565 					       "i915 display info:");
566 
567 	if (!HAS_DISPLAY(display))
568 		return;
569 
570 	intel_vga_register(display);
571 
572 	/* Must be done after probing outputs */
573 	intel_opregion_register(display);
574 	intel_acpi_video_register(display);
575 
576 	intel_audio_init(display);
577 
578 	intel_display_driver_enable_user_access(display);
579 
580 	intel_audio_register(display);
581 
582 	intel_display_debugfs_register(display);
583 
584 	/*
585 	 * We need to coordinate the hotplugs with the asynchronous
586 	 * fbdev configuration, for which we use the
587 	 * fbdev->async_cookie.
588 	 */
589 	drm_kms_helper_poll_init(display->drm);
590 	intel_hpd_poll_disable(display);
591 
592 	intel_fbdev_setup(display);
593 
594 	intel_display_device_info_print(DISPLAY_INFO(display),
595 					DISPLAY_RUNTIME_INFO(display), &p);
596 
597 	intel_register_dsm_handler();
598 }
599 
600 /* part #1: call before irq uninstall */
601 void intel_display_driver_remove(struct intel_display *display)
602 {
603 	if (!HAS_DISPLAY(display))
604 		return;
605 
606 	flush_workqueue(display->wq.flip);
607 	flush_workqueue(display->wq.modeset);
608 	flush_workqueue(display->wq.cleanup);
609 	flush_workqueue(display->wq.unordered);
610 
611 	/*
612 	 * MST topology needs to be suspended so we don't have any calls to
613 	 * fbdev after it's finalized. MST will be destroyed later as part of
614 	 * drm_mode_config_cleanup()
615 	 */
616 	intel_dp_mst_suspend(display);
617 }
618 
619 /* part #2: call after irq uninstall */
620 void intel_display_driver_remove_noirq(struct intel_display *display)
621 {
622 	if (!HAS_DISPLAY(display))
623 		return;
624 
625 	intel_hpd_cancel_work(display);
626 
627 	intel_display_driver_suspend_access(display);
628 
629 	/*
630 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
631 	 * poll handlers. Hence disable polling after hpd handling is shut down.
632 	 */
633 	intel_hpd_poll_fini(display);
634 
635 	intel_unregister_dsm_handler();
636 
637 	/* flush any delayed tasks or pending work */
638 	flush_workqueue(display->wq.unordered);
639 
640 	intel_hdcp_component_fini(display);
641 
642 	intel_mode_config_cleanup(display);
643 
644 	intel_dp_tunnel_mgr_cleanup(display);
645 
646 	intel_overlay_cleanup(display);
647 
648 	intel_gmbus_teardown(display);
649 
650 	destroy_workqueue(display->hotplug.dp_wq);
651 	destroy_workqueue(display->wq.flip);
652 	destroy_workqueue(display->wq.modeset);
653 	destroy_workqueue(display->wq.cleanup);
654 	destroy_workqueue(display->wq.unordered);
655 
656 	intel_fbc_cleanup(display);
657 }
658 
659 /* part #3: call after gem init */
660 void intel_display_driver_remove_nogem(struct intel_display *display)
661 {
662 	intel_dmc_fini(display);
663 
664 	intel_display_power_driver_remove(display);
665 
666 	intel_bios_driver_remove(display);
667 
668 	intel_opregion_cleanup(display);
669 }
670 
671 void intel_display_driver_unregister(struct intel_display *display)
672 {
673 	if (!HAS_DISPLAY(display))
674 		return;
675 
676 	intel_unregister_dsm_handler();
677 
678 	drm_client_dev_unregister(display->drm);
679 
680 	/*
681 	 * After flushing the fbdev (incl. a late async config which
682 	 * will have delayed queuing of a hotplug event), then flush
683 	 * the hotplug events.
684 	 */
685 	drm_kms_helper_poll_fini(display->drm);
686 
687 	intel_display_driver_disable_user_access(display);
688 
689 	intel_audio_deinit(display);
690 
691 	drm_atomic_helper_shutdown(display->drm);
692 
693 	acpi_video_unregister();
694 	intel_opregion_unregister(display);
695 
696 	intel_vga_unregister(display);
697 }
698 
699 void intel_display_driver_shutdown(struct intel_display *display)
700 {
701 	if (!HAS_DISPLAY(display))
702 		return;
703 
704 	__intel_display_driver_pm_suspend(display, true);
705 
706 	intel_encoder_shutdown_all(display);
707 }
708 
709 void intel_display_driver_shutdown_late(struct intel_display *display)
710 {
711 	if (!HAS_DISPLAY(display))
712 		return;
713 
714 	/*
715 	 * The only requirement is to reboot with display DC states disabled,
716 	 * for now leaving all display power wells in the INIT power domain
717 	 * enabled.
718 	 */
719 	intel_display_power_driver_remove(display);
720 }
721 
722 /*
723  * turn all crtc's off, but do not adjust state
724  * This has to be paired with a call to intel_modeset_setup_hw_state.
725  */
726 static int __intel_display_driver_pm_suspend(struct intel_display *display, bool shutdown)
727 {
728 	int ret = 0;
729 
730 	if (!HAS_DISPLAY(display))
731 		return 0;
732 
733 	/*
734 	 * We do a lot of poking in a lot of registers, make sure they work
735 	 * properly.
736 	 */
737 	intel_display_power_disable(display);
738 
739 	drm_client_dev_suspend(display->drm);
740 
741 	drm_kms_helper_poll_disable(display->drm);
742 	intel_display_driver_disable_user_access(display);
743 
744 	if (shutdown) {
745 		drm_atomic_helper_shutdown(display->drm);
746 	} else {
747 		struct drm_atomic_commit *state;
748 
749 		state = drm_atomic_helper_suspend(display->drm);
750 		ret = PTR_ERR_OR_ZERO(state);
751 		if (ret)
752 			drm_err(display->drm, "Suspending crtc's failed with %i\n",
753 				ret);
754 		else
755 			display->restore.modeset_state = state;
756 	}
757 
758 	/* ensure all DPT VMAs have been unpinned for intel_dpt_suspend() */
759 	flush_workqueue(display->wq.cleanup);
760 
761 	intel_dp_mst_suspend(display);
762 
763 	intel_encoder_block_all_hpds(display);
764 
765 	intel_hpd_cancel_work(display);
766 
767 	intel_display_driver_suspend_access(display);
768 
769 	intel_encoder_suspend_all(display);
770 
771 	return ret;
772 }
773 
774 int intel_display_driver_pm_suspend(struct intel_display *display)
775 {
776 	return __intel_display_driver_pm_suspend(display, false);
777 }
778 
779 void intel_display_driver_pm_suspend_late(struct intel_display *display, bool s2idle)
780 {
781 	if (!HAS_DISPLAY(display))
782 		return;
783 
784 	intel_display_power_suspend_late(display, s2idle);
785 }
786 
787 void intel_display_driver_pm_resume_early(struct intel_display *display)
788 {
789 	if (!HAS_DISPLAY(display))
790 		return;
791 
792 	intel_display_power_resume_early(display);
793 }
794 
795 int
796 __intel_display_driver_resume(struct intel_display *display,
797 			      struct drm_atomic_commit *state,
798 			      struct drm_modeset_acquire_ctx *ctx)
799 {
800 	struct drm_crtc_state *crtc_state;
801 	struct drm_crtc *crtc;
802 	int ret, i;
803 
804 	intel_modeset_setup_hw_state(display, ctx);
805 
806 	if (!state)
807 		return 0;
808 
809 	/*
810 	 * We've duplicated the state, pointers to the old state are invalid.
811 	 *
812 	 * Don't attempt to use the old state until we commit the duplicated state.
813 	 */
814 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
815 		/*
816 		 * Force recalculation even if we restore
817 		 * current state. With fast modeset this may not result
818 		 * in a modeset when the state is compatible.
819 		 */
820 		crtc_state->mode_changed = true;
821 	}
822 
823 	/* ignore any reset values/BIOS leftovers in the WM registers */
824 	if (!HAS_GMCH(display))
825 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
826 
827 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
828 
829 	drm_WARN_ON(display->drm, ret == -EDEADLK);
830 
831 	return ret;
832 }
833 
834 void intel_display_driver_pm_resume(struct intel_display *display)
835 {
836 	struct drm_atomic_commit *state = display->restore.modeset_state;
837 	struct drm_modeset_acquire_ctx ctx;
838 	int ret;
839 
840 	if (!HAS_DISPLAY(display))
841 		return;
842 
843 	intel_display_driver_resume_access(display);
844 
845 	intel_hpd_init(display);
846 
847 	intel_encoder_unblock_all_hpds(display);
848 
849 	/* MST sideband requires HPD interrupts enabled */
850 	intel_dp_mst_resume(display);
851 
852 	display->restore.modeset_state = NULL;
853 	if (state)
854 		state->acquire_ctx = &ctx;
855 
856 	drm_modeset_acquire_init(&ctx, 0);
857 
858 	while (1) {
859 		ret = drm_modeset_lock_all_ctx(display->drm, &ctx);
860 		if (ret != -EDEADLK)
861 			break;
862 
863 		drm_modeset_backoff(&ctx);
864 	}
865 
866 	if (!ret)
867 		ret = __intel_display_driver_resume(display, state, &ctx);
868 
869 	skl_watermark_ipc_update(display);
870 	drm_modeset_drop_locks(&ctx);
871 	drm_modeset_acquire_fini(&ctx);
872 
873 	if (ret)
874 		drm_err(display->drm,
875 			"Restoring old state failed with %i\n", ret);
876 	if (state)
877 		drm_atomic_commit_put(state);
878 
879 	intel_display_driver_enable_user_access(display);
880 	drm_kms_helper_poll_enable(display->drm);
881 
882 	intel_hpd_poll_disable(display);
883 
884 	intel_opregion_resume(display);
885 
886 	drm_client_dev_resume(display->drm);
887 
888 	intel_display_power_enable(display);
889 }
890 
891 void intel_display_driver_runtime_pm_enable(struct intel_display *display)
892 {
893 	intel_display_power_enable(display);
894 }
895 
896 void intel_display_driver_runtime_pm_disable(struct intel_display *display)
897 {
898 	intel_display_power_disable(display);
899 }
900 
901 /* before irq suspend */
902 void intel_display_driver_pm_runtime_suspend(struct intel_display *display)
903 {
904 }
905 
906 /* after irq suspend */
907 void intel_display_driver_pm_runtime_suspend_late(struct intel_display *display)
908 {
909 	intel_display_power_runtime_suspend(display);
910 
911 	/*
912 	 * FIXME: We really should find a document that references the arguments
913 	 * used below!
914 	 */
915 	if (display->platform.broadwell) {
916 		/*
917 		 * On Broadwell, if we use PCI_D1 the PCH DDI ports will stop
918 		 * being detected, and the call we do at i915_pm_runtime_resume()
919 		 * won't be able to restore them. Since PCI_D3hot matches the
920 		 * actual specification and appears to be working, use it.
921 		 */
922 		intel_opregion_notify_adapter(display, PCI_D3hot);
923 	} else {
924 		/*
925 		 * current versions of firmware which depend on this opregion
926 		 * notification have repurposed the D1 definition to mean
927 		 * "runtime suspended" vs. what you would normally expect (D3)
928 		 * to distinguish it from notifications that might be sent via
929 		 * the suspend path.
930 		 */
931 		intel_opregion_notify_adapter(display, PCI_D1);
932 	}
933 
934 	if (!display->platform.valleyview && !display->platform.cherryview)
935 		intel_hpd_poll_enable(display);
936 }
937 
938 /* before irq resume */
939 void intel_display_driver_pm_runtime_resume_early(struct intel_display *display)
940 {
941 	intel_opregion_notify_adapter(display, PCI_D0);
942 
943 	intel_display_power_runtime_resume(display);
944 }
945 
946 /* after irq resume */
947 void intel_display_driver_pm_runtime_resume(struct intel_display *display)
948 {
949 	/*
950 	 * On VLV/CHV display interrupts are part of the display
951 	 * power well, so hpd is reinitialized from there. For
952 	 * everyone else do it here.
953 	 */
954 	if (!display->platform.valleyview && !display->platform.cherryview) {
955 		intel_hpd_init(display);
956 		intel_hpd_poll_disable(display);
957 	}
958 
959 	skl_watermark_ipc_update(display);
960 }
961