1// SPDX-License-Identifier: MIT
2/*
3 * Copyright © 2023 Intel Corporation
4 */
5
6#include <drm/drm_atomic_helper.h>
7
8#include "i915_drv.h"
9#include "intel_clock_gating.h"
10#include "intel_cx0_phy.h"
11#include "intel_display_core.h"
12#include "intel_display_driver.h"
13#include "intel_display_reset.h"
14#include "intel_display_types.h"
15#include "intel_hotplug.h"
16#include "intel_pps.h"
17
18bool intel_display_reset_test(struct intel_display *display)
19{
20 return display->params.force_reset_modeset_test;
21}
22
23/* returns true if intel_display_reset_finish() needs to be called */
24bool intel_display_reset_prepare(struct intel_display *display,
25 modeset_stuck_fn modeset_stuck, void *context)
26{
27 struct drm_modeset_acquire_ctx *ctx = &display->restore.reset_ctx;
28 struct drm_atomic_state *state;
29 int ret;
30
31 if (!HAS_DISPLAY(display))
32 return false;
33
34 if (atomic_read(v: &display->restore.pending_fb_pin)) {
35 drm_dbg_kms(display->drm,
36 "Modeset potentially stuck, unbreaking through wedging\n");
37 modeset_stuck(context);
38 }
39
40 /*
41 * Need mode_config.mutex so that we don't
42 * trample ongoing ->detect() and whatnot.
43 */
44 mutex_lock(lock: &display->drm->mode_config.mutex);
45 drm_modeset_acquire_init(ctx, flags: 0);
46 while (1) {
47 ret = drm_modeset_lock_all_ctx(dev: display->drm, ctx);
48 if (ret != -EDEADLK)
49 break;
50
51 drm_modeset_backoff(ctx);
52 }
53 /*
54 * Disabling the crtcs gracefully seems nicer. Also the
55 * g33 docs say we should at least disable all the planes.
56 */
57 state = drm_atomic_helper_duplicate_state(dev: display->drm, ctx);
58 if (IS_ERR(ptr: state)) {
59 ret = PTR_ERR(ptr: state);
60 drm_err(display->drm, "Duplicating state failed with %i\n",
61 ret);
62 return true;
63 }
64
65 ret = drm_atomic_helper_disable_all(dev: display->drm, ctx);
66 if (ret) {
67 drm_err(display->drm, "Suspending crtc's failed with %i\n",
68 ret);
69 drm_atomic_state_put(state);
70 return true;
71 }
72
73 display->restore.modeset_state = state;
74 state->acquire_ctx = ctx;
75
76 return true;
77}
78
79void intel_display_reset_finish(struct intel_display *display, bool test_only)
80{
81 struct drm_i915_private *i915 = to_i915(dev: display->drm);
82 struct drm_modeset_acquire_ctx *ctx = &display->restore.reset_ctx;
83 struct drm_atomic_state *state;
84 int ret;
85
86 if (!HAS_DISPLAY(display))
87 return;
88
89 state = fetch_and_zero(&display->restore.modeset_state);
90 if (!state)
91 goto unlock;
92
93 /* reset doesn't touch the display */
94 if (test_only) {
95 /* for testing only restore the display */
96 ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
97 if (ret) {
98 drm_WARN_ON(display->drm, ret == -EDEADLK);
99 drm_err(display->drm,
100 "Restoring old state failed with %i\n", ret);
101 }
102 } else {
103 /*
104 * The display has been reset as well,
105 * so need a full re-initialization.
106 */
107 intel_pps_unlock_regs_wa(display);
108 intel_display_driver_init_hw(display);
109 intel_clock_gating_init(i915);
110 intel_cx0_pll_power_save_wa(display);
111 intel_hpd_init(display);
112
113 ret = __intel_display_driver_resume(display, state, ctx);
114 if (ret)
115 drm_err(display->drm,
116 "Restoring old state failed with %i\n", ret);
117
118 intel_hpd_poll_disable(display);
119 }
120
121 drm_atomic_state_put(state);
122unlock:
123 drm_modeset_drop_locks(ctx);
124 drm_modeset_acquire_fini(ctx);
125 mutex_unlock(lock: &display->drm->mode_config.mutex);
126}
127