1 // SPDX-License-Identifier: MIT
3 * Copyright © 2022-2023 Intel Corporation
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
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_probe_helper.h>
18 #include <drm/drm_vblank.h>
22 #include "intel_acpi.h"
23 #include "intel_atomic.h"
24 #include "intel_audio.h"
25 #include "intel_bios.h"
27 #include "intel_cdclk.h"
28 #include "intel_color.h"
29 #include "intel_crtc.h"
30 #include "intel_display_debugfs.h"
31 #include "intel_display_driver.h"
32 #include "intel_display_irq.h"
33 #include "intel_display_power.h"
34 #include "intel_display_types.h"
35 #include "intel_display_wa.h"
36 #include "intel_dkl_phy.h"
37 #include "intel_dmc.h"
39 #include "intel_dp_tunnel.h"
40 #include "intel_dpll.h"
41 #include "intel_dpll_mgr.h"
43 #include "intel_fbc.h"
44 #include "intel_fbdev.h"
45 #include "intel_fdi.h"
46 #include "intel_gmbus.h"
47 #include "intel_hdcp.h"
48 #include "intel_hotplug.h"
49 #include "intel_hti.h"
50 #include "intel_modeset_lock.h"
51 #include "intel_modeset_setup.h"
52 #include "intel_opregion.h"
53 #include "intel_overlay.h"
54 #include "intel_plane_initial.h"
55 #include "intel_pmdemand.h"
56 #include "intel_pps.h"
57 #include "intel_quirks.h"
58 #include "intel_vga.h"
60 #include "skl_watermark.h"
62 bool intel_display_driver_probe_defer(struct pci_dev *pdev)
64 struct drm_privacy_screen *privacy_screen;
67 * apple-gmux is needed on dual GPU MacBook Pro
68 * to probe the panel if we're the inactive GPU.
70 if (vga_switcheroo_client_probe_defer(pdev))
73 /* If the LCD panel has a privacy-screen, wait for it */
74 privacy_screen = drm_privacy_screen_get(&pdev->dev, NULL);
75 if (IS_ERR(privacy_screen) && PTR_ERR(privacy_screen) == -EPROBE_DEFER)
78 drm_privacy_screen_put(privacy_screen);
83 void intel_display_driver_init_hw(struct intel_display *display)
85 struct drm_i915_private *i915 = to_i915(display->drm);
86 struct intel_cdclk_state *cdclk_state;
88 if (!HAS_DISPLAY(display))
91 cdclk_state = to_intel_cdclk_state(display->cdclk.obj.state);
93 intel_update_cdclk(display);
94 intel_cdclk_dump_config(display, &display->cdclk.hw, "Current CDCLK");
95 cdclk_state->logical = cdclk_state->actual = display->cdclk.hw;
97 intel_display_wa_apply(i915);
100 static const struct drm_mode_config_funcs intel_mode_funcs = {
101 .fb_create = intel_user_framebuffer_create,
102 .get_format_info = intel_fb_get_format_info,
103 .mode_valid = intel_mode_valid,
104 .atomic_check = intel_atomic_check,
105 .atomic_commit = intel_atomic_commit,
106 .atomic_state_alloc = intel_atomic_state_alloc,
107 .atomic_state_clear = intel_atomic_state_clear,
108 .atomic_state_free = intel_atomic_state_free,
111 static const struct drm_mode_config_helper_funcs intel_mode_config_funcs = {
112 .atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
115 static void intel_mode_config_init(struct intel_display *display)
117 struct drm_mode_config *mode_config = &display->drm->mode_config;
119 drm_mode_config_init(display->drm);
120 INIT_LIST_HEAD(&display->global.obj_list);
122 mode_config->min_width = 0;
123 mode_config->min_height = 0;
125 mode_config->preferred_depth = 24;
126 mode_config->prefer_shadow = 1;
128 mode_config->funcs = &intel_mode_funcs;
129 mode_config->helper_private = &intel_mode_config_funcs;
131 mode_config->async_page_flip = HAS_ASYNC_FLIPS(display);
134 * Maximum framebuffer dimensions, chosen to match
135 * the maximum render engine surface size on gen4+.
137 if (DISPLAY_VER(display) >= 7) {
138 mode_config->max_width = 16384;
139 mode_config->max_height = 16384;
140 } else if (DISPLAY_VER(display) >= 4) {
141 mode_config->max_width = 8192;
142 mode_config->max_height = 8192;
143 } else if (DISPLAY_VER(display) == 3) {
144 mode_config->max_width = 4096;
145 mode_config->max_height = 4096;
147 mode_config->max_width = 2048;
148 mode_config->max_height = 2048;
151 if (display->platform.i845g || display->platform.i865g) {
152 mode_config->cursor_width = display->platform.i845g ? 64 : 512;
153 mode_config->cursor_height = 1023;
154 } else if (display->platform.i830 || display->platform.i85x ||
155 display->platform.i915g || display->platform.i915gm) {
156 mode_config->cursor_width = 64;
157 mode_config->cursor_height = 64;
159 mode_config->cursor_width = 256;
160 mode_config->cursor_height = 256;
164 static void intel_mode_config_cleanup(struct intel_display *display)
166 intel_atomic_global_obj_cleanup(display);
167 drm_mode_config_cleanup(display->drm);
170 static void intel_plane_possible_crtcs_init(struct intel_display *display)
172 struct intel_plane *plane;
174 for_each_intel_plane(display->drm, plane) {
175 struct intel_crtc *crtc = intel_crtc_for_pipe(display,
178 plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
182 void intel_display_driver_early_probe(struct intel_display *display)
184 struct drm_i915_private *i915 = to_i915(display->drm);
186 if (!HAS_DISPLAY(display))
189 spin_lock_init(&display->fb_tracking.lock);
190 mutex_init(&display->backlight.lock);
191 mutex_init(&display->audio.mutex);
192 mutex_init(&display->wm.wm_mutex);
193 mutex_init(&display->pps.mutex);
194 mutex_init(&display->hdcp.hdcp_mutex);
196 intel_display_irq_init(i915);
197 intel_dkl_phy_init(i915);
198 intel_color_init_hooks(display);
199 intel_init_cdclk_hooks(display);
200 intel_audio_hooks_init(i915);
201 intel_dpll_init_clock_hook(i915);
202 intel_init_display_hooks(i915);
203 intel_fdi_init_hook(i915);
204 intel_dmc_wl_init(display);
207 /* part #1: call before irq install */
208 int intel_display_driver_probe_noirq(struct intel_display *display)
210 struct drm_i915_private *i915 = to_i915(display->drm);
213 if (i915_inject_probe_failure(i915))
216 if (HAS_DISPLAY(display)) {
217 ret = drm_vblank_init(display->drm,
218 INTEL_NUM_PIPES(display));
223 intel_bios_init(display);
225 ret = intel_vga_register(display);
229 /* FIXME: completely on the wrong abstraction layer */
230 ret = intel_power_domains_init(display);
234 intel_pmdemand_init_early(display);
236 intel_power_domains_init_hw(display, false);
238 if (!HAS_DISPLAY(display))
241 intel_dmc_init(display);
243 display->wq.modeset = alloc_ordered_workqueue("i915_modeset", 0);
244 display->wq.flip = alloc_workqueue("i915_flip", WQ_HIGHPRI |
245 WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
246 display->wq.cleanup = alloc_workqueue("i915_cleanup", WQ_HIGHPRI, 0);
248 intel_mode_config_init(display);
250 ret = intel_cdclk_init(display);
252 goto cleanup_vga_client_pw_domain_dmc;
254 ret = intel_color_init(display);
256 goto cleanup_vga_client_pw_domain_dmc;
258 ret = intel_dbuf_init(i915);
260 goto cleanup_vga_client_pw_domain_dmc;
262 ret = intel_bw_init(i915);
264 goto cleanup_vga_client_pw_domain_dmc;
266 ret = intel_pmdemand_init(display);
268 goto cleanup_vga_client_pw_domain_dmc;
270 intel_init_quirks(display);
272 intel_fbc_init(display);
276 cleanup_vga_client_pw_domain_dmc:
277 intel_dmc_fini(display);
278 intel_power_domains_driver_remove(display);
280 intel_vga_unregister(display);
282 intel_bios_driver_remove(display);
287 static void set_display_access(struct intel_display *display,
288 bool any_task_allowed,
289 struct task_struct *allowed_task)
291 struct drm_modeset_acquire_ctx ctx;
294 intel_modeset_lock_ctx_retry(&ctx, NULL, 0, err) {
295 err = drm_modeset_lock_all_ctx(display->drm, &ctx);
299 display->access.any_task_allowed = any_task_allowed;
300 display->access.allowed_task = allowed_task;
303 drm_WARN_ON(display->drm, err);
307 * intel_display_driver_enable_user_access - Enable display HW access for all threads
308 * @display: display device instance
310 * Enable the display HW access for all threads. Examples for such accesses
311 * are modeset commits and connector probing.
313 * This function should be called during driver loading and system resume once
314 * all the HW initialization steps are done.
316 void intel_display_driver_enable_user_access(struct intel_display *display)
318 struct drm_i915_private *i915 = to_i915(display->drm);
320 set_display_access(display, true, NULL);
322 intel_hpd_enable_detection_work(i915);
326 * intel_display_driver_disable_user_access - Disable display HW access for user threads
327 * @display: display device instance
329 * Disable the display HW access for user threads. Examples for such accesses
330 * are modeset commits and connector probing. For the current thread the
331 * access is still enabled, which should only perform HW init/deinit
332 * programming (as the initial modeset during driver loading or the disabling
333 * modeset during driver unloading and system suspend/shutdown). This function
334 * should be followed by calling either intel_display_driver_enable_user_access()
335 * after completing the HW init programming or
336 * intel_display_driver_suspend_access() after completing the HW deinit
339 * This function should be called during driver loading/unloading and system
340 * suspend/shutdown before starting the HW init/deinit programming.
342 void intel_display_driver_disable_user_access(struct intel_display *display)
344 struct drm_i915_private *i915 = to_i915(display->drm);
346 intel_hpd_disable_detection_work(i915);
348 set_display_access(display, false, current);
352 * intel_display_driver_suspend_access - Suspend display HW access for all threads
353 * @display: display device instance
355 * Disable the display HW access for all threads. Examples for such accesses
356 * are modeset commits and connector probing. This call should be either
357 * followed by calling intel_display_driver_resume_access(), or the driver
358 * should be unloaded/shutdown.
360 * This function should be called during driver unloading and system
361 * suspend/shutdown after completing the HW deinit programming.
363 void intel_display_driver_suspend_access(struct intel_display *display)
365 set_display_access(display, false, NULL);
369 * intel_display_driver_resume_access - Resume display HW access for the resume thread
370 * @display: display device instance
372 * Enable the display HW access for the current resume thread, keeping the
373 * access disabled for all other (user) threads. Examples for such accesses
374 * are modeset commits and connector probing. The resume thread should only
375 * perform HW init programming (as the restoring modeset). This function
376 * should be followed by calling intel_display_driver_enable_user_access(),
377 * after completing the HW init programming steps.
379 * This function should be called during system resume before starting the HW
382 void intel_display_driver_resume_access(struct intel_display *display)
384 set_display_access(display, false, current);
388 * intel_display_driver_check_access - Check if the current thread has disaplay HW access
389 * @display: display device instance
391 * Check whether the current thread has display HW access, print a debug
392 * message if it doesn't. Such accesses are modeset commits and connector
393 * probing. If the function returns %false any HW access should be prevented.
395 * Returns %true if the current thread has display HW access, %false
398 bool intel_display_driver_check_access(struct intel_display *display)
400 char current_task[TASK_COMM_LEN + 16];
401 char allowed_task[TASK_COMM_LEN + 16] = "none";
403 if (display->access.any_task_allowed ||
404 display->access.allowed_task == current)
407 snprintf(current_task, sizeof(current_task), "%s[%d]",
408 current->comm, task_pid_vnr(current));
410 if (display->access.allowed_task)
411 snprintf(allowed_task, sizeof(allowed_task), "%s[%d]",
412 display->access.allowed_task->comm,
413 task_pid_vnr(display->access.allowed_task));
415 drm_dbg_kms(display->drm,
416 "Reject display access from task %s (allowed to %s)\n",
417 current_task, allowed_task);
422 /* part #2: call after irq install, but before gem init */
423 int intel_display_driver_probe_nogem(struct intel_display *display)
425 struct drm_i915_private *i915 = to_i915(display->drm);
429 if (!HAS_DISPLAY(display))
434 intel_panel_sanitize_ssc(i915);
436 intel_pps_setup(display);
438 intel_gmbus_setup(display);
440 drm_dbg_kms(display->drm, "%d display pipe%s available.\n",
441 INTEL_NUM_PIPES(display),
442 INTEL_NUM_PIPES(display) > 1 ? "s" : "");
444 for_each_pipe(display, pipe) {
445 ret = intel_crtc_init(i915, pipe);
447 goto err_mode_config;
450 intel_plane_possible_crtcs_init(display);
451 intel_shared_dpll_init(i915);
452 intel_fdi_pll_freq_update(i915);
454 intel_update_czclk(i915);
455 intel_display_driver_init_hw(display);
456 intel_dpll_update_ref_clks(i915);
458 if (display->cdclk.max_cdclk_freq == 0)
459 intel_update_max_cdclk(display);
461 intel_hti_init(display);
463 /* Just disable it once at startup */
464 intel_vga_disable(display);
465 intel_setup_outputs(i915);
467 ret = intel_dp_tunnel_mgr_init(display);
471 intel_display_driver_disable_user_access(display);
473 drm_modeset_lock_all(display->drm);
474 intel_modeset_setup_hw_state(i915, display->drm->mode_config.acquire_ctx);
475 intel_acpi_assign_connector_fwnodes(display);
476 drm_modeset_unlock_all(display->drm);
478 intel_initial_plane_config(display);
481 * Make sure hardware watermarks really match the state we read out.
482 * Note that we need to do this after reconstructing the BIOS fb's
483 * since the watermark calculation done here will use pstate->fb.
485 if (!HAS_GMCH(display))
486 ilk_wm_sanitize(i915);
491 intel_hdcp_component_fini(display);
493 intel_mode_config_cleanup(display);
498 /* part #3: call after gem init */
499 int intel_display_driver_probe(struct intel_display *display)
501 struct drm_i915_private *i915 = to_i915(display->drm);
504 if (!HAS_DISPLAY(display))
508 * This will bind stuff into ggtt, so it needs to be done after
509 * the BIOS fb takeover and whatever else magic ggtt reservations
510 * happen during gem/ggtt init.
512 intel_hdcp_component_init(display);
515 * Force all active planes to recompute their states. So that on
516 * mode_setcrtc after probe, all the intel_plane_state variables
517 * are already calculated and there is no assert_plane warnings
520 ret = intel_initial_commit(display->drm);
522 drm_dbg_kms(display->drm, "Initial modeset failed, %d\n", ret);
524 intel_overlay_setup(display);
526 /* Only enable hotplug handling once the fbdev is fully set up. */
527 intel_hpd_init(i915);
529 skl_watermark_ipc_init(i915);
534 void intel_display_driver_register(struct intel_display *display)
536 struct drm_i915_private *i915 = to_i915(display->drm);
537 struct drm_printer p = drm_dbg_printer(display->drm, DRM_UT_KMS,
538 "i915 display info:");
540 if (!HAS_DISPLAY(display))
543 /* Must be done after probing outputs */
544 intel_opregion_register(display);
545 intel_acpi_video_register(display);
547 intel_audio_init(i915);
549 intel_display_driver_enable_user_access(display);
551 intel_audio_register(i915);
553 intel_display_debugfs_register(i915);
556 * We need to coordinate the hotplugs with the asynchronous
557 * fbdev configuration, for which we use the
558 * fbdev->async_cookie.
560 drm_kms_helper_poll_init(display->drm);
561 intel_hpd_poll_disable(i915);
563 intel_fbdev_setup(i915);
565 intel_display_device_info_print(DISPLAY_INFO(display),
566 DISPLAY_RUNTIME_INFO(display), &p);
569 /* part #1: call before irq uninstall */
570 void intel_display_driver_remove(struct intel_display *display)
572 if (!HAS_DISPLAY(display))
575 flush_workqueue(display->wq.flip);
576 flush_workqueue(display->wq.modeset);
577 flush_workqueue(display->wq.cleanup);
580 * MST topology needs to be suspended so we don't have any calls to
581 * fbdev after it's finalized. MST will be destroyed later as part of
582 * drm_mode_config_cleanup()
584 intel_dp_mst_suspend(display);
587 /* part #2: call after irq uninstall */
588 void intel_display_driver_remove_noirq(struct intel_display *display)
590 struct drm_i915_private *i915 = to_i915(display->drm);
592 if (!HAS_DISPLAY(display))
595 intel_display_driver_suspend_access(display);
598 * Due to the hpd irq storm handling the hotplug work can re-arm the
599 * poll handlers. Hence disable polling after hpd handling is shut down.
601 intel_hpd_poll_fini(i915);
603 intel_unregister_dsm_handler();
605 /* flush any delayed tasks or pending work */
606 flush_workqueue(i915->unordered_wq);
608 intel_hdcp_component_fini(display);
610 intel_mode_config_cleanup(display);
612 intel_dp_tunnel_mgr_cleanup(display);
614 intel_overlay_cleanup(display);
616 intel_gmbus_teardown(display);
618 destroy_workqueue(display->wq.flip);
619 destroy_workqueue(display->wq.modeset);
620 destroy_workqueue(display->wq.cleanup);
622 intel_fbc_cleanup(display);
625 /* part #3: call after gem init */
626 void intel_display_driver_remove_nogem(struct intel_display *display)
628 intel_dmc_fini(display);
630 intel_power_domains_driver_remove(display);
632 intel_vga_unregister(display);
634 intel_bios_driver_remove(display);
637 void intel_display_driver_unregister(struct intel_display *display)
639 struct drm_i915_private *i915 = to_i915(display->drm);
641 if (!HAS_DISPLAY(display))
644 drm_client_dev_unregister(display->drm);
647 * After flushing the fbdev (incl. a late async config which
648 * will have delayed queuing of a hotplug event), then flush
649 * the hotplug events.
651 drm_kms_helper_poll_fini(display->drm);
653 intel_display_driver_disable_user_access(display);
655 intel_audio_deinit(i915);
657 drm_atomic_helper_shutdown(display->drm);
659 acpi_video_unregister();
660 intel_opregion_unregister(display);
664 * turn all crtc's off, but do not adjust state
665 * This has to be paired with a call to intel_modeset_setup_hw_state.
667 int intel_display_driver_suspend(struct intel_display *display)
669 struct drm_atomic_state *state;
672 if (!HAS_DISPLAY(display))
675 state = drm_atomic_helper_suspend(display->drm);
676 ret = PTR_ERR_OR_ZERO(state);
678 drm_err(display->drm, "Suspending crtc's failed with %i\n",
681 display->restore.modeset_state = state;
683 /* ensure all DPT VMAs have been unpinned for intel_dpt_suspend() */
684 flush_workqueue(display->wq.cleanup);
686 intel_dp_mst_suspend(display);
692 __intel_display_driver_resume(struct intel_display *display,
693 struct drm_atomic_state *state,
694 struct drm_modeset_acquire_ctx *ctx)
696 struct drm_i915_private *i915 = to_i915(display->drm);
697 struct drm_crtc_state *crtc_state;
698 struct drm_crtc *crtc;
701 intel_modeset_setup_hw_state(i915, ctx);
702 intel_vga_redisable(display);
708 * We've duplicated the state, pointers to the old state are invalid.
710 * Don't attempt to use the old state until we commit the duplicated state.
712 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
714 * Force recalculation even if we restore
715 * current state. With fast modeset this may not result
716 * in a modeset when the state is compatible.
718 crtc_state->mode_changed = true;
721 /* ignore any reset values/BIOS leftovers in the WM registers */
722 if (!HAS_GMCH(display))
723 to_intel_atomic_state(state)->skip_intermediate_wm = true;
725 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
727 drm_WARN_ON(display->drm, ret == -EDEADLK);
732 void intel_display_driver_resume(struct intel_display *display)
734 struct drm_i915_private *i915 = to_i915(display->drm);
735 struct drm_atomic_state *state = display->restore.modeset_state;
736 struct drm_modeset_acquire_ctx ctx;
739 if (!HAS_DISPLAY(display))
742 /* MST sideband requires HPD interrupts enabled */
743 intel_dp_mst_resume(display);
745 display->restore.modeset_state = NULL;
747 state->acquire_ctx = &ctx;
749 drm_modeset_acquire_init(&ctx, 0);
752 ret = drm_modeset_lock_all_ctx(display->drm, &ctx);
756 drm_modeset_backoff(&ctx);
760 ret = __intel_display_driver_resume(display, state, &ctx);
762 skl_watermark_ipc_update(i915);
763 drm_modeset_drop_locks(&ctx);
764 drm_modeset_acquire_fini(&ctx);
767 drm_err(display->drm,
768 "Restoring old state failed with %i\n", ret);
770 drm_atomic_state_put(state);