xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_load_detect.c (revision 9f771739a04919226081a107167596de75108fbb)
1b13604c0SJani Nikula // SPDX-License-Identifier: MIT
2b13604c0SJani Nikula /*
3b13604c0SJani Nikula  * Copyright © 2023 Intel Corporation
4b13604c0SJani Nikula  */
5b13604c0SJani Nikula 
6b13604c0SJani Nikula #include <drm/drm_atomic.h>
7b13604c0SJani Nikula #include <drm/drm_atomic_helper.h>
8b13604c0SJani Nikula #include <drm/drm_atomic_uapi.h>
9b13604c0SJani Nikula 
10b13604c0SJani Nikula #include "i915_drv.h"
11b13604c0SJani Nikula #include "intel_atomic.h"
12b13604c0SJani Nikula #include "intel_crtc.h"
13b13604c0SJani Nikula #include "intel_display_types.h"
14b13604c0SJani Nikula #include "intel_load_detect.h"
15b13604c0SJani Nikula 
16b13604c0SJani Nikula /* VESA 640x480x72Hz mode to set on the pipe */
17b13604c0SJani Nikula static const struct drm_display_mode load_detect_mode = {
18b13604c0SJani Nikula 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
19b13604c0SJani Nikula 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
20b13604c0SJani Nikula };
21b13604c0SJani Nikula 
intel_modeset_disable_planes(struct drm_atomic_state * state,struct drm_crtc * crtc)22b13604c0SJani Nikula static int intel_modeset_disable_planes(struct drm_atomic_state *state,
23b13604c0SJani Nikula 					struct drm_crtc *crtc)
24b13604c0SJani Nikula {
25b13604c0SJani Nikula 	struct drm_plane *plane;
26b13604c0SJani Nikula 	struct drm_plane_state *plane_state;
27b13604c0SJani Nikula 	int ret, i;
28b13604c0SJani Nikula 
29b13604c0SJani Nikula 	ret = drm_atomic_add_affected_planes(state, crtc);
30b13604c0SJani Nikula 	if (ret)
31b13604c0SJani Nikula 		return ret;
32b13604c0SJani Nikula 
33b13604c0SJani Nikula 	for_each_new_plane_in_state(state, plane, plane_state, i) {
34b13604c0SJani Nikula 		if (plane_state->crtc != crtc)
35b13604c0SJani Nikula 			continue;
36b13604c0SJani Nikula 
37b13604c0SJani Nikula 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
38b13604c0SJani Nikula 		if (ret)
39b13604c0SJani Nikula 			return ret;
40b13604c0SJani Nikula 
41b13604c0SJani Nikula 		drm_atomic_set_fb_for_plane(plane_state, NULL);
42b13604c0SJani Nikula 	}
43b13604c0SJani Nikula 
44b13604c0SJani Nikula 	return 0;
45b13604c0SJani Nikula }
46b13604c0SJani Nikula 
47*8902a55dSJani Nikula struct drm_atomic_state *
intel_load_detect_get_pipe(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx)48*8902a55dSJani Nikula intel_load_detect_get_pipe(struct drm_connector *connector,
49b13604c0SJani Nikula 			   struct drm_modeset_acquire_ctx *ctx)
50b13604c0SJani Nikula {
51b13604c0SJani Nikula 	struct intel_encoder *encoder =
52b13604c0SJani Nikula 		intel_attached_encoder(to_intel_connector(connector));
53b13604c0SJani Nikula 	struct intel_crtc *possible_crtc;
54b13604c0SJani Nikula 	struct intel_crtc *crtc = NULL;
55b13604c0SJani Nikula 	struct drm_device *dev = encoder->base.dev;
56b13604c0SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
57b13604c0SJani Nikula 	struct drm_mode_config *config = &dev->mode_config;
58b13604c0SJani Nikula 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
59b13604c0SJani Nikula 	struct drm_connector_state *connector_state;
60b13604c0SJani Nikula 	struct intel_crtc_state *crtc_state;
61b13604c0SJani Nikula 	int ret;
62b13604c0SJani Nikula 
63b13604c0SJani Nikula 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
64b13604c0SJani Nikula 		    connector->base.id, connector->name,
65b13604c0SJani Nikula 		    encoder->base.base.id, encoder->base.name);
66b13604c0SJani Nikula 
67b13604c0SJani Nikula 	drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
68b13604c0SJani Nikula 
69b13604c0SJani Nikula 	/*
70b13604c0SJani Nikula 	 * Algorithm gets a little messy:
71b13604c0SJani Nikula 	 *
72b13604c0SJani Nikula 	 *   - if the connector already has an assigned crtc, use it (but make
73b13604c0SJani Nikula 	 *     sure it's on first)
74b13604c0SJani Nikula 	 *
75b13604c0SJani Nikula 	 *   - try to find the first unused crtc that can drive this connector,
76b13604c0SJani Nikula 	 *     and use that if we find one
77b13604c0SJani Nikula 	 */
78b13604c0SJani Nikula 
79b13604c0SJani Nikula 	/* See if we already have a CRTC for this connector */
80b13604c0SJani Nikula 	if (connector->state->crtc) {
81b13604c0SJani Nikula 		crtc = to_intel_crtc(connector->state->crtc);
82b13604c0SJani Nikula 
83b13604c0SJani Nikula 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
84b13604c0SJani Nikula 		if (ret)
85b13604c0SJani Nikula 			goto fail;
86b13604c0SJani Nikula 
87b13604c0SJani Nikula 		/* Make sure the crtc and connector are running */
88b13604c0SJani Nikula 		goto found;
89b13604c0SJani Nikula 	}
90b13604c0SJani Nikula 
91b13604c0SJani Nikula 	/* Find an unused one (if possible) */
92b13604c0SJani Nikula 	for_each_intel_crtc(dev, possible_crtc) {
93b13604c0SJani Nikula 		if (!(encoder->base.possible_crtcs &
94b13604c0SJani Nikula 		      drm_crtc_mask(&possible_crtc->base)))
95b13604c0SJani Nikula 			continue;
96b13604c0SJani Nikula 
97b13604c0SJani Nikula 		ret = drm_modeset_lock(&possible_crtc->base.mutex, ctx);
98b13604c0SJani Nikula 		if (ret)
99b13604c0SJani Nikula 			goto fail;
100b13604c0SJani Nikula 
101b13604c0SJani Nikula 		if (possible_crtc->base.state->enable) {
102b13604c0SJani Nikula 			drm_modeset_unlock(&possible_crtc->base.mutex);
103b13604c0SJani Nikula 			continue;
104b13604c0SJani Nikula 		}
105b13604c0SJani Nikula 
106b13604c0SJani Nikula 		crtc = possible_crtc;
107b13604c0SJani Nikula 		break;
108b13604c0SJani Nikula 	}
109b13604c0SJani Nikula 
110b13604c0SJani Nikula 	/*
111b13604c0SJani Nikula 	 * If we didn't find an unused CRTC, don't use any.
112b13604c0SJani Nikula 	 */
113b13604c0SJani Nikula 	if (!crtc) {
114b13604c0SJani Nikula 		drm_dbg_kms(&dev_priv->drm,
115b13604c0SJani Nikula 			    "no pipe available for load-detect\n");
116b13604c0SJani Nikula 		ret = -ENODEV;
117b13604c0SJani Nikula 		goto fail;
118b13604c0SJani Nikula 	}
119b13604c0SJani Nikula 
120b13604c0SJani Nikula found:
121b13604c0SJani Nikula 	state = drm_atomic_state_alloc(dev);
122b13604c0SJani Nikula 	restore_state = drm_atomic_state_alloc(dev);
123b13604c0SJani Nikula 	if (!state || !restore_state) {
124b13604c0SJani Nikula 		ret = -ENOMEM;
125b13604c0SJani Nikula 		goto fail;
126b13604c0SJani Nikula 	}
127b13604c0SJani Nikula 
128b13604c0SJani Nikula 	state->acquire_ctx = ctx;
129b13604c0SJani Nikula 	to_intel_atomic_state(state)->internal = true;
130b13604c0SJani Nikula 
131b13604c0SJani Nikula 	restore_state->acquire_ctx = ctx;
132b13604c0SJani Nikula 	to_intel_atomic_state(restore_state)->internal = true;
133b13604c0SJani Nikula 
134b13604c0SJani Nikula 	connector_state = drm_atomic_get_connector_state(state, connector);
135b13604c0SJani Nikula 	if (IS_ERR(connector_state)) {
136b13604c0SJani Nikula 		ret = PTR_ERR(connector_state);
137b13604c0SJani Nikula 		goto fail;
138b13604c0SJani Nikula 	}
139b13604c0SJani Nikula 
140b13604c0SJani Nikula 	ret = drm_atomic_set_crtc_for_connector(connector_state, &crtc->base);
141b13604c0SJani Nikula 	if (ret)
142b13604c0SJani Nikula 		goto fail;
143b13604c0SJani Nikula 
144b13604c0SJani Nikula 	crtc_state = intel_atomic_get_crtc_state(state, crtc);
145b13604c0SJani Nikula 	if (IS_ERR(crtc_state)) {
146b13604c0SJani Nikula 		ret = PTR_ERR(crtc_state);
147b13604c0SJani Nikula 		goto fail;
148b13604c0SJani Nikula 	}
149b13604c0SJani Nikula 
150b13604c0SJani Nikula 	crtc_state->uapi.active = true;
151b13604c0SJani Nikula 
152b13604c0SJani Nikula 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
153b13604c0SJani Nikula 					   &load_detect_mode);
154b13604c0SJani Nikula 	if (ret)
155b13604c0SJani Nikula 		goto fail;
156b13604c0SJani Nikula 
157b13604c0SJani Nikula 	ret = intel_modeset_disable_planes(state, &crtc->base);
158b13604c0SJani Nikula 	if (ret)
159b13604c0SJani Nikula 		goto fail;
160b13604c0SJani Nikula 
161b13604c0SJani Nikula 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
162b13604c0SJani Nikula 	if (!ret)
163b13604c0SJani Nikula 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, &crtc->base));
164b13604c0SJani Nikula 	if (!ret)
165b13604c0SJani Nikula 		ret = drm_atomic_add_affected_planes(restore_state, &crtc->base);
166b13604c0SJani Nikula 	if (ret) {
167b13604c0SJani Nikula 		drm_dbg_kms(&dev_priv->drm,
168b13604c0SJani Nikula 			    "Failed to create a copy of old state to restore: %i\n",
169b13604c0SJani Nikula 			    ret);
170b13604c0SJani Nikula 		goto fail;
171b13604c0SJani Nikula 	}
172b13604c0SJani Nikula 
173b13604c0SJani Nikula 	ret = drm_atomic_commit(state);
174b13604c0SJani Nikula 	if (ret) {
175b13604c0SJani Nikula 		drm_dbg_kms(&dev_priv->drm,
176b13604c0SJani Nikula 			    "failed to set mode on load-detect pipe\n");
177b13604c0SJani Nikula 		goto fail;
178b13604c0SJani Nikula 	}
179b13604c0SJani Nikula 
180b13604c0SJani Nikula 	drm_atomic_state_put(state);
181b13604c0SJani Nikula 
182b13604c0SJani Nikula 	/* let the connector get through one full cycle before testing */
183b13604c0SJani Nikula 	intel_crtc_wait_for_next_vblank(crtc);
184b13604c0SJani Nikula 
185*8902a55dSJani Nikula 	return restore_state;
186b13604c0SJani Nikula 
187b13604c0SJani Nikula fail:
188b13604c0SJani Nikula 	if (state) {
189b13604c0SJani Nikula 		drm_atomic_state_put(state);
190b13604c0SJani Nikula 		state = NULL;
191b13604c0SJani Nikula 	}
192b13604c0SJani Nikula 	if (restore_state) {
193b13604c0SJani Nikula 		drm_atomic_state_put(restore_state);
194b13604c0SJani Nikula 		restore_state = NULL;
195b13604c0SJani Nikula 	}
196b13604c0SJani Nikula 
197b13604c0SJani Nikula 	if (ret == -EDEADLK)
198*8902a55dSJani Nikula 		return ERR_PTR(ret);
199b13604c0SJani Nikula 
200*8902a55dSJani Nikula 	return NULL;
201b13604c0SJani Nikula }
202b13604c0SJani Nikula 
intel_load_detect_release_pipe(struct drm_connector * connector,struct drm_atomic_state * state,struct drm_modeset_acquire_ctx * ctx)203b13604c0SJani Nikula void intel_load_detect_release_pipe(struct drm_connector *connector,
204*8902a55dSJani Nikula 				    struct drm_atomic_state *state,
205b13604c0SJani Nikula 				    struct drm_modeset_acquire_ctx *ctx)
206b13604c0SJani Nikula {
207b13604c0SJani Nikula 	struct intel_encoder *intel_encoder =
208b13604c0SJani Nikula 		intel_attached_encoder(to_intel_connector(connector));
209b13604c0SJani Nikula 	struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
210b13604c0SJani Nikula 	struct drm_encoder *encoder = &intel_encoder->base;
211b13604c0SJani Nikula 	int ret;
212b13604c0SJani Nikula 
213b13604c0SJani Nikula 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
214b13604c0SJani Nikula 		    connector->base.id, connector->name,
215b13604c0SJani Nikula 		    encoder->base.id, encoder->name);
216b13604c0SJani Nikula 
217*8902a55dSJani Nikula 	if (IS_ERR_OR_NULL(state))
218b13604c0SJani Nikula 		return;
219b13604c0SJani Nikula 
220b13604c0SJani Nikula 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
221b13604c0SJani Nikula 	if (ret)
222b13604c0SJani Nikula 		drm_dbg_kms(&i915->drm,
223b13604c0SJani Nikula 			    "Couldn't release load detect pipe: %i\n", ret);
224b13604c0SJani Nikula 	drm_atomic_state_put(state);
225b13604c0SJani Nikula }
226