drm/i915: Don't check for NULL before freeing state
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_atomic.c
CommitLineData
5ee67f1c
MR
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_atomic_plane.c for the plane-specific atomic functionality.
30 */
31
32#include <drm/drmP.h>
33#include <drm/drm_atomic.h>
34#include <drm/drm_atomic_helper.h>
35#include <drm/drm_plane_helper.h>
36#include "intel_drv.h"
37
38
39/**
40 * intel_atomic_check - validate state object
41 * @dev: drm device
42 * @state: state to validate
43 */
44int intel_atomic_check(struct drm_device *dev,
45 struct drm_atomic_state *state)
46{
47 int nplanes = dev->mode_config.num_total_plane;
48 int ncrtcs = dev->mode_config.num_crtc;
49 int nconnectors = dev->mode_config.num_connector;
50 enum pipe nuclear_pipe = INVALID_PIPE;
e04fa803
CK
51 struct intel_crtc *nuclear_crtc = NULL;
52 struct intel_crtc_state *crtc_state = NULL;
5ee67f1c
MR
53 int ret;
54 int i;
55 bool not_nuclear = false;
56
57 /*
58 * FIXME: At the moment, we only support "nuclear pageflip" on a
59 * single CRTC. Cross-crtc updates will be added later.
60 */
61 for (i = 0; i < nplanes; i++) {
62 struct intel_plane *plane = to_intel_plane(state->planes[i]);
63 if (!plane)
64 continue;
65
66 if (nuclear_pipe == INVALID_PIPE) {
67 nuclear_pipe = plane->pipe;
68 } else if (nuclear_pipe != plane->pipe) {
69 DRM_DEBUG_KMS("i915 only support atomic plane operations on a single CRTC at the moment\n");
70 return -EINVAL;
71 }
72 }
73
74 /*
75 * FIXME: We only handle planes for now; make sure there are no CRTC's
76 * or connectors involved.
77 */
78 state->allow_modeset = false;
79 for (i = 0; i < ncrtcs; i++) {
80 struct intel_crtc *crtc = to_intel_crtc(state->crtcs[i]);
f1e2daea
MR
81 if (crtc)
82 memset(&crtc->atomic, 0, sizeof(crtc->atomic));
5ee67f1c
MR
83 if (crtc && crtc->pipe != nuclear_pipe)
84 not_nuclear = true;
e04fa803
CK
85 if (crtc && crtc->pipe == nuclear_pipe) {
86 nuclear_crtc = crtc;
87 crtc_state = to_intel_crtc_state(state->crtc_states[i]);
88 }
5ee67f1c
MR
89 }
90 for (i = 0; i < nconnectors; i++)
91 if (state->connectors[i] != NULL)
92 not_nuclear = true;
93
94 if (not_nuclear) {
95 DRM_DEBUG_KMS("i915 only supports atomic plane operations at the moment\n");
96 return -EINVAL;
97 }
98
99 ret = drm_atomic_helper_check_planes(dev, state);
100 if (ret)
101 return ret;
102
e04fa803
CK
103 /* FIXME: move to crtc atomic check function once it is ready */
104 ret = intel_atomic_setup_scalers(dev, nuclear_crtc, crtc_state);
105 if (ret)
106 return ret;
107
5ee67f1c
MR
108 return ret;
109}
110
111
112/**
113 * intel_atomic_commit - commit validated state object
114 * @dev: DRM device
115 * @state: the top-level driver state object
116 * @async: asynchronous commit
117 *
118 * This function commits a top-level state object that has been validated
119 * with drm_atomic_helper_check().
120 *
121 * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
122 * we can only handle plane-related operations and do not yet support
123 * asynchronous commit.
124 *
125 * RETURNS
126 * Zero for success or -errno.
127 */
128int intel_atomic_commit(struct drm_device *dev,
129 struct drm_atomic_state *state,
130 bool async)
131{
132 int ret;
133 int i;
134
135 if (async) {
136 DRM_DEBUG_KMS("i915 does not yet support async commit\n");
137 return -EINVAL;
138 }
139
140 ret = drm_atomic_helper_prepare_planes(dev, state);
141 if (ret)
142 return ret;
143
144 /* Point of no return */
145
146 /*
147 * FIXME: The proper sequence here will eventually be:
148 *
149 * drm_atomic_helper_swap_state(dev, state)
1af434a9 150 * drm_atomic_helper_commit_modeset_disables(dev, state);
5ee67f1c 151 * drm_atomic_helper_commit_planes(dev, state);
1af434a9 152 * drm_atomic_helper_commit_modeset_enables(dev, state);
5ee67f1c
MR
153 * drm_atomic_helper_wait_for_vblanks(dev, state);
154 * drm_atomic_helper_cleanup_planes(dev, state);
155 * drm_atomic_state_free(state);
156 *
157 * once we have full atomic modeset. For now, just manually update
158 * plane states to avoid clobbering good states with dummy states
159 * while nuclear pageflipping.
160 */
161 for (i = 0; i < dev->mode_config.num_total_plane; i++) {
162 struct drm_plane *plane = state->planes[i];
163
164 if (!plane)
165 continue;
166
167 plane->state->state = state;
168 swap(state->plane_states[i], plane->state);
169 plane->state->state = NULL;
170 }
f76f35dc 171
6156a456 172 /* swap crtc_scaler_state */
f76f35dc
CK
173 for (i = 0; i < dev->mode_config.num_crtc; i++) {
174 struct drm_crtc *crtc = state->crtcs[i];
175 if (!crtc) {
176 continue;
177 }
178
179 to_intel_crtc(crtc)->config->scaler_state =
180 to_intel_crtc_state(state->crtc_states[i])->scaler_state;
6156a456
CK
181
182 if (INTEL_INFO(dev)->gen >= 9)
183 skl_detach_scalers(to_intel_crtc(crtc));
f76f35dc
CK
184 }
185
5ee67f1c
MR
186 drm_atomic_helper_commit_planes(dev, state);
187 drm_atomic_helper_wait_for_vblanks(dev, state);
188 drm_atomic_helper_cleanup_planes(dev, state);
189 drm_atomic_state_free(state);
190
191 return 0;
192}
2545e4a6
MR
193
194/**
195 * intel_connector_atomic_get_property - fetch connector property value
196 * @connector: connector to fetch property for
197 * @state: state containing the property value
198 * @property: property to look up
199 * @val: pointer to write property value into
200 *
201 * The DRM core does not store shadow copies of properties for
202 * atomic-capable drivers. This entrypoint is used to fetch
203 * the current value of a driver-specific connector property.
204 */
205int
206intel_connector_atomic_get_property(struct drm_connector *connector,
207 const struct drm_connector_state *state,
208 struct drm_property *property,
209 uint64_t *val)
210{
211 int i;
212
213 /*
214 * TODO: We only have atomic modeset for planes at the moment, so the
215 * crtc/connector code isn't quite ready yet. Until it's ready,
216 * continue to look up all property values in the DRM's shadow copy
217 * in obj->properties->values[].
218 *
219 * When the crtc/connector state work matures, this function should
220 * be updated to read the values out of the state structure instead.
221 */
222 for (i = 0; i < connector->base.properties->count; i++) {
223 if (connector->base.properties->properties[i] == property) {
224 *val = connector->base.properties->values[i];
225 return 0;
226 }
227 }
228
229 return -EINVAL;
230}
1356837e
MR
231
232/*
233 * intel_crtc_duplicate_state - duplicate crtc state
234 * @crtc: drm crtc
235 *
236 * Allocates and returns a copy of the crtc state (both common and
237 * Intel-specific) for the specified crtc.
238 *
239 * Returns: The newly allocated crtc state, or NULL on failure.
240 */
241struct drm_crtc_state *
242intel_crtc_duplicate_state(struct drm_crtc *crtc)
243{
244 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
a91572f3 245 struct intel_crtc_state *crtc_state;
1356837e
MR
246
247 if (WARN_ON(!intel_crtc->config))
a91572f3
ACO
248 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
249 else
250 crtc_state = kmemdup(intel_crtc->config,
251 sizeof(*intel_crtc->config), GFP_KERNEL);
1356837e 252
a91572f3
ACO
253 if (crtc_state)
254 crtc_state->base.crtc = crtc;
255
256 return &crtc_state->base;
1356837e
MR
257}
258
259/**
260 * intel_crtc_destroy_state - destroy crtc state
261 * @crtc: drm crtc
262 *
263 * Destroys the crtc state (both common and Intel-specific) for the
264 * specified crtc.
265 */
266void
267intel_crtc_destroy_state(struct drm_crtc *crtc,
268 struct drm_crtc_state *state)
269{
270 drm_atomic_helper_crtc_destroy_state(crtc, state);
271}
d03c93d4
CK
272
273/**
274 * intel_atomic_setup_scalers() - setup scalers for crtc per staged requests
275 * @dev: DRM device
276 * @crtc: intel crtc
277 * @crtc_state: incoming crtc_state to validate and setup scalers
278 *
279 * This function sets up scalers based on staged scaling requests for
280 * a @crtc and its planes. It is called from crtc level check path. If request
281 * is a supportable request, it attaches scalers to requested planes and crtc.
282 *
283 * This function takes into account the current scaler(s) in use by any planes
284 * not being part of this atomic state
285 *
286 * Returns:
287 * 0 - scalers were setup succesfully
288 * error code - otherwise
289 */
290int intel_atomic_setup_scalers(struct drm_device *dev,
291 struct intel_crtc *intel_crtc,
292 struct intel_crtc_state *crtc_state)
293{
294 struct drm_plane *plane = NULL;
295 struct intel_plane *intel_plane;
296 struct intel_plane_state *plane_state = NULL;
297 struct intel_crtc_scaler_state *scaler_state;
298 struct drm_atomic_state *drm_state;
299 int num_scalers_need;
300 int i, j;
301
302 if (INTEL_INFO(dev)->gen < 9 || !intel_crtc || !crtc_state)
303 return 0;
304
305 scaler_state = &crtc_state->scaler_state;
306 drm_state = crtc_state->base.state;
307
308 num_scalers_need = hweight32(scaler_state->scaler_users);
309 DRM_DEBUG_KMS("crtc_state = %p need = %d avail = %d scaler_users = 0x%x\n",
310 crtc_state, num_scalers_need, intel_crtc->num_scalers,
311 scaler_state->scaler_users);
312
313 /*
314 * High level flow:
315 * - staged scaler requests are already in scaler_state->scaler_users
316 * - check whether staged scaling requests can be supported
317 * - add planes using scalers that aren't in current transaction
318 * - assign scalers to requested users
319 * - as part of plane commit, scalers will be committed
320 * (i.e., either attached or detached) to respective planes in hw
321 * - as part of crtc_commit, scaler will be either attached or detached
322 * to crtc in hw
323 */
324
325 /* fail if required scalers > available scalers */
326 if (num_scalers_need > intel_crtc->num_scalers){
327 DRM_DEBUG_KMS("Too many scaling requests %d > %d\n",
328 num_scalers_need, intel_crtc->num_scalers);
329 return -EINVAL;
330 }
331
332 /* walkthrough scaler_users bits and start assigning scalers */
333 for (i = 0; i < sizeof(scaler_state->scaler_users) * 8; i++) {
334 int *scaler_id;
335
336 /* skip if scaler not required */
337 if (!(scaler_state->scaler_users & (1 << i)))
338 continue;
339
340 if (i == SKL_CRTC_INDEX) {
341 /* panel fitter case: assign as a crtc scaler */
342 scaler_id = &scaler_state->scaler_id;
343 } else {
344 if (!drm_state)
345 continue;
346
347 /* plane scaler case: assign as a plane scaler */
348 /* find the plane that set the bit as scaler_user */
349 plane = drm_state->planes[i];
350
351 /*
352 * to enable/disable hq mode, add planes that are using scaler
353 * into this transaction
354 */
355 if (!plane) {
356 struct drm_plane_state *state;
357 plane = drm_plane_from_index(dev, i);
358 state = drm_atomic_get_plane_state(drm_state, plane);
359 if (IS_ERR(state)) {
360 DRM_DEBUG_KMS("Failed to add [PLANE:%d] to drm_state\n",
361 plane->base.id);
362 return PTR_ERR(state);
363 }
364 }
365
366 intel_plane = to_intel_plane(plane);
367
368 /* plane on different crtc cannot be a scaler user of this crtc */
369 if (WARN_ON(intel_plane->pipe != intel_crtc->pipe)) {
370 continue;
371 }
372
373 plane_state = to_intel_plane_state(drm_state->plane_states[i]);
374 scaler_id = &plane_state->scaler_id;
375 }
376
377 if (*scaler_id < 0) {
378 /* find a free scaler */
379 for (j = 0; j < intel_crtc->num_scalers; j++) {
380 if (!scaler_state->scalers[j].in_use) {
381 scaler_state->scalers[j].in_use = 1;
382 *scaler_id = scaler_state->scalers[j].id;
383 DRM_DEBUG_KMS("Attached scaler id %u.%u to %s:%d\n",
384 intel_crtc->pipe,
385 i == SKL_CRTC_INDEX ? scaler_state->scaler_id :
386 plane_state->scaler_id,
387 i == SKL_CRTC_INDEX ? "CRTC" : "PLANE",
388 i == SKL_CRTC_INDEX ? intel_crtc->base.base.id :
389 plane->base.id);
390 break;
391 }
392 }
393 }
394
395 if (WARN_ON(*scaler_id < 0)) {
396 DRM_DEBUG_KMS("Cannot find scaler for %s:%d\n",
397 i == SKL_CRTC_INDEX ? "CRTC" : "PLANE",
398 i == SKL_CRTC_INDEX ? intel_crtc->base.base.id:plane->base.id);
399 continue;
400 }
401
402 /* set scaler mode */
403 if (num_scalers_need == 1 && intel_crtc->pipe != PIPE_C) {
404 /*
405 * when only 1 scaler is in use on either pipe A or B,
406 * scaler 0 operates in high quality (HQ) mode.
407 * In this case use scaler 0 to take advantage of HQ mode
408 */
409 *scaler_id = 0;
410 scaler_state->scalers[0].in_use = 1;
411 scaler_state->scalers[0].mode = PS_SCALER_MODE_HQ;
412 scaler_state->scalers[1].in_use = 0;
413 } else {
414 scaler_state->scalers[*scaler_id].mode = PS_SCALER_MODE_DYN;
415 }
416 }
417
418 return 0;
419}
This page took 0.076752 seconds and 5 git commands to generate.