xref: /linux/drivers/gpu/drm/i915/display/intel_atomic.c (revision f49410baedcb46c015b7bf3ca66c449b05fe3cf0)
1 /*
2  * Copyright © 2015 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 /**
25  * DOC: atomic modeset support
26  *
27  * The functions here implement the state management and hardware programming
28  * dispatch required by the atomic modeset infrastructure.
29  * See intel_plane.c for the plane-specific atomic functionality.
30  */
31 
32 #include <drm/display/drm_dp_tunnel.h>
33 #include <drm/drm_atomic.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_fourcc.h>
36 #include <drm/drm_print.h>
37 
38 #include "intel_atomic.h"
39 #include "intel_cdclk.h"
40 #include "intel_display_core.h"
41 #include "intel_display_types.h"
42 #include "intel_dp_tunnel.h"
43 #include "intel_fb.h"
44 #include "intel_global_state.h"
45 #include "intel_hdcp.h"
46 #include "intel_psr.h"
47 #include "skl_universal_plane.h"
48 
49 /**
50  * intel_digital_connector_atomic_get_property - hook for connector->atomic_get_property.
51  * @connector: Connector to get the property for.
52  * @state: Connector state to retrieve the property from.
53  * @property: Property to retrieve.
54  * @val: Return value for the property.
55  *
56  * Returns the atomic property value for a digital connector.
57  */
58 int intel_digital_connector_atomic_get_property(struct drm_connector *connector,
59 						const struct drm_connector_state *state,
60 						struct drm_property *property,
61 						u64 *val)
62 {
63 	struct intel_display *display = to_intel_display(connector->dev);
64 	const struct intel_digital_connector_state *intel_conn_state =
65 		to_intel_digital_connector_state(state);
66 
67 	if (property == display->properties.force_audio)
68 		*val = intel_conn_state->force_audio;
69 	else if (property == display->properties.broadcast_rgb)
70 		*val = intel_conn_state->broadcast_rgb;
71 	else {
72 		drm_dbg_atomic(display->drm,
73 			       "Unknown property [PROP:%d:%s]\n",
74 			       property->base.id, property->name);
75 		return -EINVAL;
76 	}
77 
78 	return 0;
79 }
80 
81 /**
82  * intel_digital_connector_atomic_set_property - hook for connector->atomic_set_property.
83  * @connector: Connector to set the property for.
84  * @state: Connector state to set the property on.
85  * @property: Property to set.
86  * @val: New value for the property.
87  *
88  * Sets the atomic property value for a digital connector.
89  */
90 int intel_digital_connector_atomic_set_property(struct drm_connector *connector,
91 						struct drm_connector_state *state,
92 						struct drm_property *property,
93 						u64 val)
94 {
95 	struct intel_display *display = to_intel_display(connector->dev);
96 	struct intel_digital_connector_state *intel_conn_state =
97 		to_intel_digital_connector_state(state);
98 
99 	if (property == display->properties.force_audio) {
100 		intel_conn_state->force_audio = val;
101 		return 0;
102 	}
103 
104 	if (property == display->properties.broadcast_rgb) {
105 		intel_conn_state->broadcast_rgb = val;
106 		return 0;
107 	}
108 
109 	drm_dbg_atomic(display->drm, "Unknown property [PROP:%d:%s]\n",
110 		       property->base.id, property->name);
111 	return -EINVAL;
112 }
113 
114 int intel_digital_connector_atomic_check(struct drm_connector *conn,
115 					 struct drm_atomic_commit *state)
116 {
117 	struct drm_connector_state *new_state =
118 		drm_atomic_get_new_connector_state(state, conn);
119 	struct intel_digital_connector_state *new_conn_state =
120 		to_intel_digital_connector_state(new_state);
121 	struct drm_connector_state *old_state =
122 		drm_atomic_get_old_connector_state(state, conn);
123 	struct intel_digital_connector_state *old_conn_state =
124 		to_intel_digital_connector_state(old_state);
125 	struct drm_crtc_state *crtc_state;
126 
127 	intel_hdcp_atomic_check(conn, old_state, new_state);
128 
129 	if (!new_state->crtc)
130 		return 0;
131 
132 	crtc_state = drm_atomic_get_new_crtc_state(state, new_state->crtc);
133 
134 	/*
135 	 * These properties are handled by fastset, and might not end
136 	 * up in a modeset.
137 	 */
138 	if (new_conn_state->force_audio != old_conn_state->force_audio ||
139 	    new_conn_state->broadcast_rgb != old_conn_state->broadcast_rgb ||
140 	    new_conn_state->base.colorspace != old_conn_state->base.colorspace ||
141 	    new_conn_state->base.picture_aspect_ratio != old_conn_state->base.picture_aspect_ratio ||
142 	    new_conn_state->base.content_type != old_conn_state->base.content_type ||
143 	    new_conn_state->base.scaling_mode != old_conn_state->base.scaling_mode ||
144 	    new_conn_state->base.privacy_screen_sw_state != old_conn_state->base.privacy_screen_sw_state ||
145 	    !drm_connector_atomic_hdr_metadata_equal(old_state, new_state))
146 		crtc_state->mode_changed = true;
147 
148 	return 0;
149 }
150 
151 /**
152  * intel_digital_connector_duplicate_state - duplicate connector state
153  * @connector: digital connector
154  *
155  * Allocates and returns a copy of the connector state (both common and
156  * digital connector specific) for the specified connector.
157  *
158  * Returns: The newly allocated connector state, or NULL on failure.
159  */
160 struct drm_connector_state *
161 intel_digital_connector_duplicate_state(struct drm_connector *connector)
162 {
163 	struct intel_digital_connector_state *state;
164 
165 	state = kmemdup(connector->state, sizeof(*state), GFP_KERNEL);
166 	if (!state)
167 		return NULL;
168 
169 	__drm_atomic_helper_connector_duplicate_state(connector, &state->base);
170 	return &state->base;
171 }
172 
173 /**
174  * intel_connector_needs_modeset - check if connector needs a modeset
175  * @state: the atomic state corresponding to this modeset
176  * @connector: the connector
177  */
178 bool
179 intel_connector_needs_modeset(struct intel_atomic_state *state,
180 			      struct drm_connector *connector)
181 {
182 	const struct drm_connector_state *old_conn_state, *new_conn_state;
183 
184 	old_conn_state = drm_atomic_get_old_connector_state(&state->base, connector);
185 	new_conn_state = drm_atomic_get_new_connector_state(&state->base, connector);
186 
187 	return old_conn_state->crtc != new_conn_state->crtc ||
188 	       (new_conn_state->crtc &&
189 		drm_atomic_crtc_needs_modeset(drm_atomic_get_new_crtc_state(&state->base,
190 									    new_conn_state->crtc)));
191 }
192 
193 /**
194  * intel_any_crtc_needs_modeset - check if any CRTC needs a modeset
195  * @state: the atomic state corresponding to this modeset
196  *
197  * Returns true if any CRTC in @state needs a modeset.
198  */
199 bool intel_any_crtc_needs_modeset(struct intel_atomic_state *state)
200 {
201 	struct intel_crtc *crtc;
202 	struct intel_crtc_state *crtc_state;
203 
204 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state) {
205 		if (intel_crtc_needs_modeset(crtc_state))
206 			return true;
207 	}
208 
209 	return false;
210 }
211 
212 struct intel_digital_connector_state *
213 intel_atomic_get_digital_connector_state(struct intel_atomic_state *state,
214 					 struct intel_connector *connector)
215 {
216 	struct drm_connector_state *conn_state;
217 
218 	conn_state = drm_atomic_get_connector_state(&state->base,
219 						    &connector->base);
220 	if (IS_ERR(conn_state))
221 		return ERR_CAST(conn_state);
222 
223 	return to_intel_digital_connector_state(conn_state);
224 }
225 
226 /**
227  * intel_crtc_duplicate_state - duplicate crtc state
228  * @crtc: drm crtc
229  *
230  * Allocates and returns a copy of the crtc state (both common and
231  * Intel-specific) for the specified crtc.
232  *
233  * Returns: The newly allocated crtc state, or NULL on failure.
234  */
235 struct drm_crtc_state *
236 intel_crtc_duplicate_state(struct drm_crtc *crtc)
237 {
238 	const struct intel_crtc_state *old_crtc_state = to_intel_crtc_state(crtc->state);
239 	struct intel_crtc_state *crtc_state;
240 
241 	crtc_state = kmemdup(old_crtc_state, sizeof(*crtc_state), GFP_KERNEL);
242 	if (!crtc_state)
243 		return NULL;
244 
245 	__drm_atomic_helper_crtc_duplicate_state(crtc, &crtc_state->uapi);
246 
247 	/* copy color blobs */
248 	if (crtc_state->hw.degamma_lut)
249 		drm_property_blob_get(crtc_state->hw.degamma_lut);
250 	if (crtc_state->hw.ctm)
251 		drm_property_blob_get(crtc_state->hw.ctm);
252 	if (crtc_state->hw.gamma_lut)
253 		drm_property_blob_get(crtc_state->hw.gamma_lut);
254 
255 	if (crtc_state->pre_csc_lut)
256 		drm_property_blob_get(crtc_state->pre_csc_lut);
257 	if (crtc_state->post_csc_lut)
258 		drm_property_blob_get(crtc_state->post_csc_lut);
259 
260 	if (crtc_state->dp_tunnel_ref.tunnel)
261 		drm_dp_tunnel_ref_get(crtc_state->dp_tunnel_ref.tunnel,
262 				      &crtc_state->dp_tunnel_ref);
263 
264 	crtc_state->update_pipe = false;
265 	crtc_state->update_m_n = false;
266 	crtc_state->update_lrr = false;
267 	crtc_state->disable_cxsr = false;
268 	crtc_state->update_wm_pre = false;
269 	crtc_state->update_wm_post = false;
270 	crtc_state->fifo_changed = false;
271 	crtc_state->preload_luts = false;
272 	crtc_state->wm.need_postvbl_update = false;
273 	crtc_state->do_async_flip = false;
274 	crtc_state->fb_bits = 0;
275 	crtc_state->update_planes = 0;
276 	crtc_state->dsb_color = NULL;
277 	crtc_state->dsb_commit = NULL;
278 	crtc_state->use_dsb = false;
279 
280 	return &crtc_state->uapi;
281 }
282 
283 static void intel_crtc_put_color_blobs(struct intel_crtc_state *crtc_state)
284 {
285 	drm_property_blob_put(crtc_state->hw.degamma_lut);
286 	drm_property_blob_put(crtc_state->hw.gamma_lut);
287 	drm_property_blob_put(crtc_state->hw.ctm);
288 
289 	drm_property_blob_put(crtc_state->pre_csc_lut);
290 	drm_property_blob_put(crtc_state->post_csc_lut);
291 }
292 
293 void intel_crtc_free_hw_state(struct intel_crtc_state *crtc_state)
294 {
295 	intel_crtc_put_color_blobs(crtc_state);
296 }
297 
298 /**
299  * intel_crtc_destroy_state - destroy crtc state
300  * @crtc: drm crtc
301  * @state: the state to destroy
302  *
303  * Destroys the crtc state (both common and Intel-specific) for the
304  * specified crtc.
305  */
306 void
307 intel_crtc_destroy_state(struct drm_crtc *crtc,
308 			 struct drm_crtc_state *state)
309 {
310 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(state);
311 
312 	drm_WARN_ON(crtc->dev, crtc_state->dsb_color);
313 	drm_WARN_ON(crtc->dev, crtc_state->dsb_commit);
314 
315 	__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
316 	intel_crtc_free_hw_state(crtc_state);
317 	if (crtc_state->dp_tunnel_ref.tunnel)
318 		drm_dp_tunnel_ref_put(&crtc_state->dp_tunnel_ref);
319 	kfree(crtc_state);
320 }
321 
322 struct drm_atomic_commit *
323 intel_atomic_state_alloc(struct drm_device *dev)
324 {
325 	struct intel_atomic_state *state = kzalloc_obj(*state);
326 
327 	if (!state || drm_atomic_commit_init(dev, &state->base) < 0) {
328 		kfree(state);
329 		return NULL;
330 	}
331 
332 	return &state->base;
333 }
334 
335 void intel_atomic_state_free(struct drm_atomic_commit *_state)
336 {
337 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
338 
339 	drm_atomic_commit_default_release(&state->base);
340 	kfree(state->global_objs);
341 	kfree(state);
342 }
343 
344 void intel_atomic_state_clear(struct drm_atomic_commit *s)
345 {
346 	struct intel_atomic_state *state = to_intel_atomic_state(s);
347 
348 	drm_atomic_commit_default_clear(&state->base);
349 	intel_atomic_clear_global_state(state);
350 
351 	/* state->internal not reset on purpose */
352 
353 	state->dpll_set = state->modeset = false;
354 
355 	intel_dp_tunnel_atomic_cleanup_inherited_state(state);
356 }
357 
358 struct intel_crtc_state *
359 intel_atomic_get_crtc_state(struct drm_atomic_commit *state,
360 			    struct intel_crtc *crtc)
361 {
362 	struct drm_crtc_state *crtc_state;
363 	crtc_state = drm_atomic_get_crtc_state(state, &crtc->base);
364 	if (IS_ERR(crtc_state))
365 		return ERR_CAST(crtc_state);
366 
367 	return to_intel_crtc_state(crtc_state);
368 }
369