Subversion Repositories Kolibri OS

Rev

Rev 6084 | Rev 6937 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6084 Rev 6660
1
/*
1
/*
2
 * Copyright (C) 2014 Red Hat
2
 * Copyright (C) 2014 Red Hat
3
 * Copyright (C) 2014 Intel Corp.
3
 * Copyright (C) 2014 Intel Corp.
4
 *
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
11
 *
12
 * The above copyright notice and this permission notice shall be included in
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
13
 * all copies or substantial portions of the Software.
14
 *
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
 * OTHER DEALINGS IN THE SOFTWARE.
21
 * OTHER DEALINGS IN THE SOFTWARE.
22
 *
22
 *
23
 * Authors:
23
 * Authors:
24
 * Rob Clark 
24
 * Rob Clark 
25
 * Daniel Vetter 
25
 * Daniel Vetter 
26
 */
26
 */
27
 
27
 
28
 
28
 
29
#include 
29
#include 
30
#include 
30
#include 
31
#include 
31
#include 
32
 
32
 
33
/**
33
/**
34
 * drm_atomic_state_default_release -
34
 * drm_atomic_state_default_release -
35
 * release memory initialized by drm_atomic_state_init
35
 * release memory initialized by drm_atomic_state_init
36
 * @state: atomic state
36
 * @state: atomic state
37
 *
37
 *
38
 * Free all the memory allocated by drm_atomic_state_init.
38
 * Free all the memory allocated by drm_atomic_state_init.
39
 * This is useful for drivers that subclass the atomic state.
39
 * This is useful for drivers that subclass the atomic state.
40
 */
40
 */
41
void drm_atomic_state_default_release(struct drm_atomic_state *state)
41
void drm_atomic_state_default_release(struct drm_atomic_state *state)
42
{
42
{
43
	kfree(state->connectors);
43
	kfree(state->connectors);
44
	kfree(state->connector_states);
44
	kfree(state->connector_states);
45
	kfree(state->crtcs);
45
	kfree(state->crtcs);
46
	kfree(state->crtc_states);
46
	kfree(state->crtc_states);
47
	kfree(state->planes);
47
	kfree(state->planes);
48
	kfree(state->plane_states);
48
	kfree(state->plane_states);
49
}
49
}
50
EXPORT_SYMBOL(drm_atomic_state_default_release);
50
EXPORT_SYMBOL(drm_atomic_state_default_release);
51
 
51
 
52
/**
52
/**
53
 * drm_atomic_state_init - init new atomic state
53
 * drm_atomic_state_init - init new atomic state
54
 * @dev: DRM device
54
 * @dev: DRM device
55
 * @state: atomic state
55
 * @state: atomic state
56
 *
56
 *
57
 * Default implementation for filling in a new atomic state.
57
 * Default implementation for filling in a new atomic state.
58
 * This is useful for drivers that subclass the atomic state.
58
 * This is useful for drivers that subclass the atomic state.
59
 */
59
 */
60
int
60
int
61
drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
61
drm_atomic_state_init(struct drm_device *dev, struct drm_atomic_state *state)
62
{
62
{
63
	/* TODO legacy paths should maybe do a better job about
63
	/* TODO legacy paths should maybe do a better job about
64
	 * setting this appropriately?
64
	 * setting this appropriately?
65
	 */
65
	 */
66
	state->allow_modeset = true;
66
	state->allow_modeset = true;
67
 
67
 
68
	state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
68
	state->num_connector = ACCESS_ONCE(dev->mode_config.num_connector);
69
 
69
 
70
	state->crtcs = kcalloc(dev->mode_config.num_crtc,
70
	state->crtcs = kcalloc(dev->mode_config.num_crtc,
71
			       sizeof(*state->crtcs), GFP_KERNEL);
71
			       sizeof(*state->crtcs), GFP_KERNEL);
72
	if (!state->crtcs)
72
	if (!state->crtcs)
73
		goto fail;
73
		goto fail;
74
	state->crtc_states = kcalloc(dev->mode_config.num_crtc,
74
	state->crtc_states = kcalloc(dev->mode_config.num_crtc,
75
				     sizeof(*state->crtc_states), GFP_KERNEL);
75
				     sizeof(*state->crtc_states), GFP_KERNEL);
76
	if (!state->crtc_states)
76
	if (!state->crtc_states)
77
		goto fail;
77
		goto fail;
78
	state->planes = kcalloc(dev->mode_config.num_total_plane,
78
	state->planes = kcalloc(dev->mode_config.num_total_plane,
79
				sizeof(*state->planes), GFP_KERNEL);
79
				sizeof(*state->planes), GFP_KERNEL);
80
	if (!state->planes)
80
	if (!state->planes)
81
		goto fail;
81
		goto fail;
82
	state->plane_states = kcalloc(dev->mode_config.num_total_plane,
82
	state->plane_states = kcalloc(dev->mode_config.num_total_plane,
83
				      sizeof(*state->plane_states), GFP_KERNEL);
83
				      sizeof(*state->plane_states), GFP_KERNEL);
84
	if (!state->plane_states)
84
	if (!state->plane_states)
85
		goto fail;
85
		goto fail;
86
	state->connectors = kcalloc(state->num_connector,
86
	state->connectors = kcalloc(state->num_connector,
87
				    sizeof(*state->connectors),
87
				    sizeof(*state->connectors),
88
				    GFP_KERNEL);
88
				    GFP_KERNEL);
89
	if (!state->connectors)
89
	if (!state->connectors)
90
		goto fail;
90
		goto fail;
91
	state->connector_states = kcalloc(state->num_connector,
91
	state->connector_states = kcalloc(state->num_connector,
92
					  sizeof(*state->connector_states),
92
					  sizeof(*state->connector_states),
93
					  GFP_KERNEL);
93
					  GFP_KERNEL);
94
	if (!state->connector_states)
94
	if (!state->connector_states)
95
		goto fail;
95
		goto fail;
96
 
96
 
97
	state->dev = dev;
97
	state->dev = dev;
98
 
98
 
99
	DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
99
	DRM_DEBUG_ATOMIC("Allocated atomic state %p\n", state);
100
 
100
 
101
	return 0;
101
	return 0;
102
fail:
102
fail:
103
	drm_atomic_state_default_release(state);
103
	drm_atomic_state_default_release(state);
104
	return -ENOMEM;
104
	return -ENOMEM;
105
}
105
}
106
EXPORT_SYMBOL(drm_atomic_state_init);
106
EXPORT_SYMBOL(drm_atomic_state_init);
107
 
107
 
108
/**
108
/**
109
 * drm_atomic_state_alloc - allocate atomic state
109
 * drm_atomic_state_alloc - allocate atomic state
110
 * @dev: DRM device
110
 * @dev: DRM device
111
 *
111
 *
112
 * This allocates an empty atomic state to track updates.
112
 * This allocates an empty atomic state to track updates.
113
 */
113
 */
114
struct drm_atomic_state *
114
struct drm_atomic_state *
115
drm_atomic_state_alloc(struct drm_device *dev)
115
drm_atomic_state_alloc(struct drm_device *dev)
116
{
116
{
117
	struct drm_mode_config *config = &dev->mode_config;
117
	struct drm_mode_config *config = &dev->mode_config;
118
	struct drm_atomic_state *state;
118
	struct drm_atomic_state *state;
119
 
119
 
120
	if (!config->funcs->atomic_state_alloc) {
120
	if (!config->funcs->atomic_state_alloc) {
121
		state = kzalloc(sizeof(*state), GFP_KERNEL);
121
		state = kzalloc(sizeof(*state), GFP_KERNEL);
122
		if (!state)
122
		if (!state)
123
			return NULL;
123
			return NULL;
124
		if (drm_atomic_state_init(dev, state) < 0) {
124
		if (drm_atomic_state_init(dev, state) < 0) {
125
			kfree(state);
125
			kfree(state);
126
			return NULL;
126
			return NULL;
127
		}
127
		}
128
		return state;
128
		return state;
129
	}
129
	}
130
 
130
 
131
	return config->funcs->atomic_state_alloc(dev);
131
	return config->funcs->atomic_state_alloc(dev);
132
}
132
}
133
EXPORT_SYMBOL(drm_atomic_state_alloc);
133
EXPORT_SYMBOL(drm_atomic_state_alloc);
134
 
134
 
135
/**
135
/**
136
 * drm_atomic_state_default_clear - clear base atomic state
136
 * drm_atomic_state_default_clear - clear base atomic state
137
 * @state: atomic state
137
 * @state: atomic state
138
 *
138
 *
139
 * Default implementation for clearing atomic state.
139
 * Default implementation for clearing atomic state.
140
 * This is useful for drivers that subclass the atomic state.
140
 * This is useful for drivers that subclass the atomic state.
141
 */
141
 */
142
void drm_atomic_state_default_clear(struct drm_atomic_state *state)
142
void drm_atomic_state_default_clear(struct drm_atomic_state *state)
143
{
143
{
144
	struct drm_device *dev = state->dev;
144
	struct drm_device *dev = state->dev;
145
	struct drm_mode_config *config = &dev->mode_config;
145
	struct drm_mode_config *config = &dev->mode_config;
146
	int i;
146
	int i;
147
 
147
 
148
	DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
148
	DRM_DEBUG_ATOMIC("Clearing atomic state %p\n", state);
149
 
149
 
150
	for (i = 0; i < state->num_connector; i++) {
150
	for (i = 0; i < state->num_connector; i++) {
151
		struct drm_connector *connector = state->connectors[i];
151
		struct drm_connector *connector = state->connectors[i];
152
 
152
 
153
		if (!connector)
153
		if (!connector || !connector->funcs)
154
			continue;
154
			continue;
155
 
155
 
156
		/*
156
		/*
157
		 * FIXME: Async commits can race with connector unplugging and
157
		 * FIXME: Async commits can race with connector unplugging and
158
		 * there's currently nothing that prevents cleanup up state for
158
		 * there's currently nothing that prevents cleanup up state for
159
		 * deleted connectors. As long as the callback doesn't look at
159
		 * deleted connectors. As long as the callback doesn't look at
160
		 * the connector we'll be fine though, so make sure that's the
160
		 * the connector we'll be fine though, so make sure that's the
161
		 * case by setting all connector pointers to NULL.
161
		 * case by setting all connector pointers to NULL.
162
		 */
162
		 */
163
		state->connector_states[i]->connector = NULL;
163
		state->connector_states[i]->connector = NULL;
164
		connector->funcs->atomic_destroy_state(NULL,
164
		connector->funcs->atomic_destroy_state(NULL,
165
						       state->connector_states[i]);
165
						       state->connector_states[i]);
166
		state->connectors[i] = NULL;
166
		state->connectors[i] = NULL;
167
		state->connector_states[i] = NULL;
167
		state->connector_states[i] = NULL;
168
	}
168
	}
169
 
169
 
170
	for (i = 0; i < config->num_crtc; i++) {
170
	for (i = 0; i < config->num_crtc; i++) {
171
		struct drm_crtc *crtc = state->crtcs[i];
171
		struct drm_crtc *crtc = state->crtcs[i];
172
 
172
 
173
		if (!crtc)
173
		if (!crtc)
174
			continue;
174
			continue;
175
 
175
 
176
		crtc->funcs->atomic_destroy_state(crtc,
176
		crtc->funcs->atomic_destroy_state(crtc,
177
						  state->crtc_states[i]);
177
						  state->crtc_states[i]);
178
		state->crtcs[i] = NULL;
178
		state->crtcs[i] = NULL;
179
		state->crtc_states[i] = NULL;
179
		state->crtc_states[i] = NULL;
180
	}
180
	}
181
 
181
 
182
	for (i = 0; i < config->num_total_plane; i++) {
182
	for (i = 0; i < config->num_total_plane; i++) {
183
		struct drm_plane *plane = state->planes[i];
183
		struct drm_plane *plane = state->planes[i];
184
 
184
 
185
		if (!plane)
185
		if (!plane)
186
			continue;
186
			continue;
187
 
187
 
188
		plane->funcs->atomic_destroy_state(plane,
188
		plane->funcs->atomic_destroy_state(plane,
189
						   state->plane_states[i]);
189
						   state->plane_states[i]);
190
		state->planes[i] = NULL;
190
		state->planes[i] = NULL;
191
		state->plane_states[i] = NULL;
191
		state->plane_states[i] = NULL;
192
	}
192
	}
193
}
193
}
194
EXPORT_SYMBOL(drm_atomic_state_default_clear);
194
EXPORT_SYMBOL(drm_atomic_state_default_clear);
195
 
195
 
196
/**
196
/**
197
 * drm_atomic_state_clear - clear state object
197
 * drm_atomic_state_clear - clear state object
198
 * @state: atomic state
198
 * @state: atomic state
199
 *
199
 *
200
 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
200
 * When the w/w mutex algorithm detects a deadlock we need to back off and drop
201
 * all locks. So someone else could sneak in and change the current modeset
201
 * all locks. So someone else could sneak in and change the current modeset
202
 * configuration. Which means that all the state assembled in @state is no
202
 * configuration. Which means that all the state assembled in @state is no
203
 * longer an atomic update to the current state, but to some arbitrary earlier
203
 * longer an atomic update to the current state, but to some arbitrary earlier
204
 * state. Which could break assumptions the driver's ->atomic_check likely
204
 * state. Which could break assumptions the driver's ->atomic_check likely
205
 * relies on.
205
 * relies on.
206
 *
206
 *
207
 * Hence we must clear all cached state and completely start over, using this
207
 * Hence we must clear all cached state and completely start over, using this
208
 * function.
208
 * function.
209
 */
209
 */
210
void drm_atomic_state_clear(struct drm_atomic_state *state)
210
void drm_atomic_state_clear(struct drm_atomic_state *state)
211
{
211
{
212
	struct drm_device *dev = state->dev;
212
	struct drm_device *dev = state->dev;
213
	struct drm_mode_config *config = &dev->mode_config;
213
	struct drm_mode_config *config = &dev->mode_config;
214
 
214
 
215
	if (config->funcs->atomic_state_clear)
215
	if (config->funcs->atomic_state_clear)
216
		config->funcs->atomic_state_clear(state);
216
		config->funcs->atomic_state_clear(state);
217
	else
217
	else
218
		drm_atomic_state_default_clear(state);
218
		drm_atomic_state_default_clear(state);
219
}
219
}
220
EXPORT_SYMBOL(drm_atomic_state_clear);
220
EXPORT_SYMBOL(drm_atomic_state_clear);
221
 
221
 
222
/**
222
/**
223
 * drm_atomic_state_free - free all memory for an atomic state
223
 * drm_atomic_state_free - free all memory for an atomic state
224
 * @state: atomic state to deallocate
224
 * @state: atomic state to deallocate
225
 *
225
 *
226
 * This frees all memory associated with an atomic state, including all the
226
 * This frees all memory associated with an atomic state, including all the
227
 * per-object state for planes, crtcs and connectors.
227
 * per-object state for planes, crtcs and connectors.
228
 */
228
 */
229
void drm_atomic_state_free(struct drm_atomic_state *state)
229
void drm_atomic_state_free(struct drm_atomic_state *state)
230
{
230
{
231
	struct drm_device *dev;
231
	struct drm_device *dev;
232
	struct drm_mode_config *config;
232
	struct drm_mode_config *config;
233
 
233
 
234
	if (!state)
234
	if (!state)
235
		return;
235
		return;
236
 
236
 
237
	dev = state->dev;
237
	dev = state->dev;
238
	config = &dev->mode_config;
238
	config = &dev->mode_config;
239
 
239
 
240
	drm_atomic_state_clear(state);
240
	drm_atomic_state_clear(state);
241
 
241
 
242
	DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
242
	DRM_DEBUG_ATOMIC("Freeing atomic state %p\n", state);
243
 
243
 
244
	if (config->funcs->atomic_state_free) {
244
	if (config->funcs->atomic_state_free) {
245
		config->funcs->atomic_state_free(state);
245
		config->funcs->atomic_state_free(state);
246
	} else {
246
	} else {
247
		drm_atomic_state_default_release(state);
247
		drm_atomic_state_default_release(state);
248
		kfree(state);
248
		kfree(state);
249
	}
249
	}
250
}
250
}
251
EXPORT_SYMBOL(drm_atomic_state_free);
251
EXPORT_SYMBOL(drm_atomic_state_free);
252
 
252
 
253
/**
253
/**
254
 * drm_atomic_get_crtc_state - get crtc state
254
 * drm_atomic_get_crtc_state - get crtc state
255
 * @state: global atomic state object
255
 * @state: global atomic state object
256
 * @crtc: crtc to get state object for
256
 * @crtc: crtc to get state object for
257
 *
257
 *
258
 * This function returns the crtc state for the given crtc, allocating it if
258
 * This function returns the crtc state for the given crtc, allocating it if
259
 * needed. It will also grab the relevant crtc lock to make sure that the state
259
 * needed. It will also grab the relevant crtc lock to make sure that the state
260
 * is consistent.
260
 * is consistent.
261
 *
261
 *
262
 * Returns:
262
 * Returns:
263
 *
263
 *
264
 * Either the allocated state or the error code encoded into the pointer. When
264
 * Either the allocated state or the error code encoded into the pointer. When
265
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
265
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
266
 * entire atomic sequence must be restarted. All other errors are fatal.
266
 * entire atomic sequence must be restarted. All other errors are fatal.
267
 */
267
 */
268
struct drm_crtc_state *
268
struct drm_crtc_state *
269
drm_atomic_get_crtc_state(struct drm_atomic_state *state,
269
drm_atomic_get_crtc_state(struct drm_atomic_state *state,
270
			  struct drm_crtc *crtc)
270
			  struct drm_crtc *crtc)
271
{
271
{
272
	int ret, index = drm_crtc_index(crtc);
272
	int ret, index = drm_crtc_index(crtc);
273
	struct drm_crtc_state *crtc_state;
273
	struct drm_crtc_state *crtc_state;
274
 
274
 
275
	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
275
	crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
276
	if (crtc_state)
276
	if (crtc_state)
277
		return crtc_state;
277
		return crtc_state;
278
 
278
 
279
	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
279
	ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
280
	if (ret)
280
	if (ret)
281
		return ERR_PTR(ret);
281
		return ERR_PTR(ret);
282
 
282
 
283
	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
283
	crtc_state = crtc->funcs->atomic_duplicate_state(crtc);
284
	if (!crtc_state)
284
	if (!crtc_state)
285
		return ERR_PTR(-ENOMEM);
285
		return ERR_PTR(-ENOMEM);
286
 
286
 
287
	state->crtc_states[index] = crtc_state;
287
	state->crtc_states[index] = crtc_state;
288
	state->crtcs[index] = crtc;
288
	state->crtcs[index] = crtc;
289
	crtc_state->state = state;
289
	crtc_state->state = state;
290
 
290
 
291
	DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
291
	DRM_DEBUG_ATOMIC("Added [CRTC:%d] %p state to %p\n",
292
			 crtc->base.id, crtc_state, state);
292
			 crtc->base.id, crtc_state, state);
293
 
293
 
294
	return crtc_state;
294
	return crtc_state;
295
}
295
}
296
EXPORT_SYMBOL(drm_atomic_get_crtc_state);
296
EXPORT_SYMBOL(drm_atomic_get_crtc_state);
297
 
297
 
298
/**
298
/**
299
 * drm_atomic_set_mode_for_crtc - set mode for CRTC
299
 * drm_atomic_set_mode_for_crtc - set mode for CRTC
300
 * @state: the CRTC whose incoming state to update
300
 * @state: the CRTC whose incoming state to update
301
 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
301
 * @mode: kernel-internal mode to use for the CRTC, or NULL to disable
302
 *
302
 *
303
 * Set a mode (originating from the kernel) on the desired CRTC state. Does
303
 * Set a mode (originating from the kernel) on the desired CRTC state. Does
304
 * not change any other state properties, including enable, active, or
304
 * not change any other state properties, including enable, active, or
305
 * mode_changed.
305
 * mode_changed.
306
 *
306
 *
307
 * RETURNS:
307
 * RETURNS:
308
 * Zero on success, error code on failure. Cannot return -EDEADLK.
308
 * Zero on success, error code on failure. Cannot return -EDEADLK.
309
 */
309
 */
310
int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
310
int drm_atomic_set_mode_for_crtc(struct drm_crtc_state *state,
311
				 struct drm_display_mode *mode)
311
				 struct drm_display_mode *mode)
312
{
312
{
313
	struct drm_mode_modeinfo umode;
313
	struct drm_mode_modeinfo umode;
314
 
314
 
315
	/* Early return for no change. */
315
	/* Early return for no change. */
316
	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
316
	if (mode && memcmp(&state->mode, mode, sizeof(*mode)) == 0)
317
		return 0;
317
		return 0;
318
 
318
 
319
	if (state->mode_blob)
319
	if (state->mode_blob)
320
		drm_property_unreference_blob(state->mode_blob);
320
		drm_property_unreference_blob(state->mode_blob);
321
	state->mode_blob = NULL;
321
	state->mode_blob = NULL;
322
 
322
 
323
	if (mode) {
323
	if (mode) {
324
		drm_mode_convert_to_umode(&umode, mode);
324
		drm_mode_convert_to_umode(&umode, mode);
325
		state->mode_blob =
325
		state->mode_blob =
326
			drm_property_create_blob(state->crtc->dev,
326
			drm_property_create_blob(state->crtc->dev,
327
		                                 sizeof(umode),
327
		                                 sizeof(umode),
328
		                                 &umode);
328
		                                 &umode);
329
		if (IS_ERR(state->mode_blob))
329
		if (IS_ERR(state->mode_blob))
330
			return PTR_ERR(state->mode_blob);
330
			return PTR_ERR(state->mode_blob);
331
 
331
 
332
		drm_mode_copy(&state->mode, mode);
332
		drm_mode_copy(&state->mode, mode);
333
		state->enable = true;
333
		state->enable = true;
334
		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
334
		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
335
				 mode->name, state);
335
				 mode->name, state);
336
	} else {
336
	} else {
337
		memset(&state->mode, 0, sizeof(state->mode));
337
		memset(&state->mode, 0, sizeof(state->mode));
338
		state->enable = false;
338
		state->enable = false;
339
		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
339
		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
340
				 state);
340
				 state);
341
	}
341
	}
342
 
342
 
343
	return 0;
343
	return 0;
344
}
344
}
345
EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
345
EXPORT_SYMBOL(drm_atomic_set_mode_for_crtc);
346
 
346
 
347
/**
347
/**
348
 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
348
 * drm_atomic_set_mode_prop_for_crtc - set mode for CRTC
349
 * @state: the CRTC whose incoming state to update
349
 * @state: the CRTC whose incoming state to update
350
 * @blob: pointer to blob property to use for mode
350
 * @blob: pointer to blob property to use for mode
351
 *
351
 *
352
 * Set a mode (originating from a blob property) on the desired CRTC state.
352
 * Set a mode (originating from a blob property) on the desired CRTC state.
353
 * This function will take a reference on the blob property for the CRTC state,
353
 * This function will take a reference on the blob property for the CRTC state,
354
 * and release the reference held on the state's existing mode property, if any
354
 * and release the reference held on the state's existing mode property, if any
355
 * was set.
355
 * was set.
356
 *
356
 *
357
 * RETURNS:
357
 * RETURNS:
358
 * Zero on success, error code on failure. Cannot return -EDEADLK.
358
 * Zero on success, error code on failure. Cannot return -EDEADLK.
359
 */
359
 */
360
int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
360
int drm_atomic_set_mode_prop_for_crtc(struct drm_crtc_state *state,
361
                                      struct drm_property_blob *blob)
361
                                      struct drm_property_blob *blob)
362
{
362
{
363
	if (blob == state->mode_blob)
363
	if (blob == state->mode_blob)
364
		return 0;
364
		return 0;
365
 
365
 
366
	if (state->mode_blob)
366
	if (state->mode_blob)
367
		drm_property_unreference_blob(state->mode_blob);
367
		drm_property_unreference_blob(state->mode_blob);
368
	state->mode_blob = NULL;
368
	state->mode_blob = NULL;
-
 
369
 
-
 
370
	memset(&state->mode, 0, sizeof(state->mode));
369
 
371
 
370
	if (blob) {
372
	if (blob) {
371
		if (blob->length != sizeof(struct drm_mode_modeinfo) ||
373
		if (blob->length != sizeof(struct drm_mode_modeinfo) ||
372
		    drm_mode_convert_umode(&state->mode,
374
		    drm_mode_convert_umode(&state->mode,
373
		                           (const struct drm_mode_modeinfo *)
375
		                           (const struct drm_mode_modeinfo *)
374
		                            blob->data))
376
		                            blob->data))
375
			return -EINVAL;
377
			return -EINVAL;
376
 
378
 
377
		state->mode_blob = drm_property_reference_blob(blob);
379
		state->mode_blob = drm_property_reference_blob(blob);
378
		state->enable = true;
380
		state->enable = true;
379
		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
381
		DRM_DEBUG_ATOMIC("Set [MODE:%s] for CRTC state %p\n",
380
				 state->mode.name, state);
382
				 state->mode.name, state);
381
	} else {
383
	} else {
382
		memset(&state->mode, 0, sizeof(state->mode));
-
 
383
		state->enable = false;
384
		state->enable = false;
384
		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
385
		DRM_DEBUG_ATOMIC("Set [NOMODE] for CRTC state %p\n",
385
				 state);
386
				 state);
386
	}
387
	}
387
 
388
 
388
	return 0;
389
	return 0;
389
}
390
}
390
EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
391
EXPORT_SYMBOL(drm_atomic_set_mode_prop_for_crtc);
391
 
392
 
392
/**
393
/**
393
 * drm_atomic_crtc_set_property - set property on CRTC
394
 * drm_atomic_crtc_set_property - set property on CRTC
394
 * @crtc: the drm CRTC to set a property on
395
 * @crtc: the drm CRTC to set a property on
395
 * @state: the state object to update with the new property value
396
 * @state: the state object to update with the new property value
396
 * @property: the property to set
397
 * @property: the property to set
397
 * @val: the new property value
398
 * @val: the new property value
398
 *
399
 *
399
 * Use this instead of calling crtc->atomic_set_property directly.
400
 * Use this instead of calling crtc->atomic_set_property directly.
400
 * This function handles generic/core properties and calls out to
401
 * This function handles generic/core properties and calls out to
401
 * driver's ->atomic_set_property() for driver properties.  To ensure
402
 * driver's ->atomic_set_property() for driver properties.  To ensure
402
 * consistent behavior you must call this function rather than the
403
 * consistent behavior you must call this function rather than the
403
 * driver hook directly.
404
 * driver hook directly.
404
 *
405
 *
405
 * RETURNS:
406
 * RETURNS:
406
 * Zero on success, error code on failure
407
 * Zero on success, error code on failure
407
 */
408
 */
408
int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
409
int drm_atomic_crtc_set_property(struct drm_crtc *crtc,
409
		struct drm_crtc_state *state, struct drm_property *property,
410
		struct drm_crtc_state *state, struct drm_property *property,
410
		uint64_t val)
411
		uint64_t val)
411
{
412
{
412
	struct drm_device *dev = crtc->dev;
413
	struct drm_device *dev = crtc->dev;
413
	struct drm_mode_config *config = &dev->mode_config;
414
	struct drm_mode_config *config = &dev->mode_config;
414
	int ret;
415
	int ret;
415
 
416
 
416
	if (property == config->prop_active)
417
	if (property == config->prop_active)
417
		state->active = val;
418
		state->active = val;
418
	else if (property == config->prop_mode_id) {
419
	else if (property == config->prop_mode_id) {
419
		struct drm_property_blob *mode =
420
		struct drm_property_blob *mode =
420
			drm_property_lookup_blob(dev, val);
421
			drm_property_lookup_blob(dev, val);
421
		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
422
		ret = drm_atomic_set_mode_prop_for_crtc(state, mode);
422
		if (mode)
423
		if (mode)
423
			drm_property_unreference_blob(mode);
424
			drm_property_unreference_blob(mode);
424
		return ret;
425
		return ret;
425
	}
426
	}
426
	else if (crtc->funcs->atomic_set_property)
427
	else if (crtc->funcs->atomic_set_property)
427
		return crtc->funcs->atomic_set_property(crtc, state, property, val);
428
		return crtc->funcs->atomic_set_property(crtc, state, property, val);
428
	else
429
	else
429
		return -EINVAL;
430
		return -EINVAL;
430
 
431
 
431
	return 0;
432
	return 0;
432
}
433
}
433
EXPORT_SYMBOL(drm_atomic_crtc_set_property);
434
EXPORT_SYMBOL(drm_atomic_crtc_set_property);
434
 
435
 
435
/*
436
/*
436
 * This function handles generic/core properties and calls out to
437
 * This function handles generic/core properties and calls out to
437
 * driver's ->atomic_get_property() for driver properties.  To ensure
438
 * driver's ->atomic_get_property() for driver properties.  To ensure
438
 * consistent behavior you must call this function rather than the
439
 * consistent behavior you must call this function rather than the
439
 * driver hook directly.
440
 * driver hook directly.
440
 */
441
 */
441
static int
442
static int
442
drm_atomic_crtc_get_property(struct drm_crtc *crtc,
443
drm_atomic_crtc_get_property(struct drm_crtc *crtc,
443
		const struct drm_crtc_state *state,
444
		const struct drm_crtc_state *state,
444
		struct drm_property *property, uint64_t *val)
445
		struct drm_property *property, uint64_t *val)
445
{
446
{
446
	struct drm_device *dev = crtc->dev;
447
	struct drm_device *dev = crtc->dev;
447
	struct drm_mode_config *config = &dev->mode_config;
448
	struct drm_mode_config *config = &dev->mode_config;
448
 
449
 
449
	if (property == config->prop_active)
450
	if (property == config->prop_active)
450
		*val = state->active;
451
		*val = state->active;
451
	else if (property == config->prop_mode_id)
452
	else if (property == config->prop_mode_id)
452
		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
453
		*val = (state->mode_blob) ? state->mode_blob->base.id : 0;
453
	else if (crtc->funcs->atomic_get_property)
454
	else if (crtc->funcs->atomic_get_property)
454
		return crtc->funcs->atomic_get_property(crtc, state, property, val);
455
		return crtc->funcs->atomic_get_property(crtc, state, property, val);
455
	else
456
	else
456
		return -EINVAL;
457
		return -EINVAL;
457
 
458
 
458
	return 0;
459
	return 0;
459
}
460
}
460
 
461
 
461
/**
462
/**
462
 * drm_atomic_crtc_check - check crtc state
463
 * drm_atomic_crtc_check - check crtc state
463
 * @crtc: crtc to check
464
 * @crtc: crtc to check
464
 * @state: crtc state to check
465
 * @state: crtc state to check
465
 *
466
 *
466
 * Provides core sanity checks for crtc state.
467
 * Provides core sanity checks for crtc state.
467
 *
468
 *
468
 * RETURNS:
469
 * RETURNS:
469
 * Zero on success, error code on failure
470
 * Zero on success, error code on failure
470
 */
471
 */
471
static int drm_atomic_crtc_check(struct drm_crtc *crtc,
472
static int drm_atomic_crtc_check(struct drm_crtc *crtc,
472
		struct drm_crtc_state *state)
473
		struct drm_crtc_state *state)
473
{
474
{
474
	/* NOTE: we explicitly don't enforce constraints such as primary
475
	/* NOTE: we explicitly don't enforce constraints such as primary
475
	 * layer covering entire screen, since that is something we want
476
	 * layer covering entire screen, since that is something we want
476
	 * to allow (on hw that supports it).  For hw that does not, it
477
	 * to allow (on hw that supports it).  For hw that does not, it
477
	 * should be checked in driver's crtc->atomic_check() vfunc.
478
	 * should be checked in driver's crtc->atomic_check() vfunc.
478
	 *
479
	 *
479
	 * TODO: Add generic modeset state checks once we support those.
480
	 * TODO: Add generic modeset state checks once we support those.
480
	 */
481
	 */
481
 
482
 
482
	if (state->active && !state->enable) {
483
	if (state->active && !state->enable) {
483
		DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
484
		DRM_DEBUG_ATOMIC("[CRTC:%d] active without enabled\n",
484
				 crtc->base.id);
485
				 crtc->base.id);
485
		return -EINVAL;
486
		return -EINVAL;
486
	}
487
	}
487
 
488
 
488
	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
489
	/* The state->enable vs. state->mode_blob checks can be WARN_ON,
489
	 * as this is a kernel-internal detail that userspace should never
490
	 * as this is a kernel-internal detail that userspace should never
490
	 * be able to trigger. */
491
	 * be able to trigger. */
491
	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
492
	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
492
	    WARN_ON(state->enable && !state->mode_blob)) {
493
	    WARN_ON(state->enable && !state->mode_blob)) {
493
		DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
494
		DRM_DEBUG_ATOMIC("[CRTC:%d] enabled without mode blob\n",
494
				 crtc->base.id);
495
				 crtc->base.id);
495
		return -EINVAL;
496
		return -EINVAL;
496
	}
497
	}
497
 
498
 
498
	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
499
	if (drm_core_check_feature(crtc->dev, DRIVER_ATOMIC) &&
499
	    WARN_ON(!state->enable && state->mode_blob)) {
500
	    WARN_ON(!state->enable && state->mode_blob)) {
500
		DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
501
		DRM_DEBUG_ATOMIC("[CRTC:%d] disabled with mode blob\n",
501
				 crtc->base.id);
502
				 crtc->base.id);
502
		return -EINVAL;
503
		return -EINVAL;
503
	}
504
	}
504
 
505
 
505
	return 0;
506
	return 0;
506
}
507
}
507
 
508
 
508
/**
509
/**
509
 * drm_atomic_get_plane_state - get plane state
510
 * drm_atomic_get_plane_state - get plane state
510
 * @state: global atomic state object
511
 * @state: global atomic state object
511
 * @plane: plane to get state object for
512
 * @plane: plane to get state object for
512
 *
513
 *
513
 * This function returns the plane state for the given plane, allocating it if
514
 * This function returns the plane state for the given plane, allocating it if
514
 * needed. It will also grab the relevant plane lock to make sure that the state
515
 * needed. It will also grab the relevant plane lock to make sure that the state
515
 * is consistent.
516
 * is consistent.
516
 *
517
 *
517
 * Returns:
518
 * Returns:
518
 *
519
 *
519
 * Either the allocated state or the error code encoded into the pointer. When
520
 * Either the allocated state or the error code encoded into the pointer. When
520
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
521
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
521
 * entire atomic sequence must be restarted. All other errors are fatal.
522
 * entire atomic sequence must be restarted. All other errors are fatal.
522
 */
523
 */
523
struct drm_plane_state *
524
struct drm_plane_state *
524
drm_atomic_get_plane_state(struct drm_atomic_state *state,
525
drm_atomic_get_plane_state(struct drm_atomic_state *state,
525
			  struct drm_plane *plane)
526
			  struct drm_plane *plane)
526
{
527
{
527
	int ret, index = drm_plane_index(plane);
528
	int ret, index = drm_plane_index(plane);
528
	struct drm_plane_state *plane_state;
529
	struct drm_plane_state *plane_state;
529
 
530
 
530
	plane_state = drm_atomic_get_existing_plane_state(state, plane);
531
	plane_state = drm_atomic_get_existing_plane_state(state, plane);
531
	if (plane_state)
532
	if (plane_state)
532
		return plane_state;
533
		return plane_state;
533
 
534
 
534
	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
535
	ret = drm_modeset_lock(&plane->mutex, state->acquire_ctx);
535
	if (ret)
536
	if (ret)
536
		return ERR_PTR(ret);
537
		return ERR_PTR(ret);
537
 
538
 
538
	plane_state = plane->funcs->atomic_duplicate_state(plane);
539
	plane_state = plane->funcs->atomic_duplicate_state(plane);
539
	if (!plane_state)
540
	if (!plane_state)
540
		return ERR_PTR(-ENOMEM);
541
		return ERR_PTR(-ENOMEM);
541
 
542
 
542
	state->plane_states[index] = plane_state;
543
	state->plane_states[index] = plane_state;
543
	state->planes[index] = plane;
544
	state->planes[index] = plane;
544
	plane_state->state = state;
545
	plane_state->state = state;
545
 
546
 
546
	DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
547
	DRM_DEBUG_ATOMIC("Added [PLANE:%d] %p state to %p\n",
547
			 plane->base.id, plane_state, state);
548
			 plane->base.id, plane_state, state);
548
 
549
 
549
	if (plane_state->crtc) {
550
	if (plane_state->crtc) {
550
		struct drm_crtc_state *crtc_state;
551
		struct drm_crtc_state *crtc_state;
551
 
552
 
552
		crtc_state = drm_atomic_get_crtc_state(state,
553
		crtc_state = drm_atomic_get_crtc_state(state,
553
						       plane_state->crtc);
554
						       plane_state->crtc);
554
		if (IS_ERR(crtc_state))
555
		if (IS_ERR(crtc_state))
555
			return ERR_CAST(crtc_state);
556
			return ERR_CAST(crtc_state);
556
	}
557
	}
557
 
558
 
558
	return plane_state;
559
	return plane_state;
559
}
560
}
560
EXPORT_SYMBOL(drm_atomic_get_plane_state);
561
EXPORT_SYMBOL(drm_atomic_get_plane_state);
561
 
562
 
562
/**
563
/**
563
 * drm_atomic_plane_set_property - set property on plane
564
 * drm_atomic_plane_set_property - set property on plane
564
 * @plane: the drm plane to set a property on
565
 * @plane: the drm plane to set a property on
565
 * @state: the state object to update with the new property value
566
 * @state: the state object to update with the new property value
566
 * @property: the property to set
567
 * @property: the property to set
567
 * @val: the new property value
568
 * @val: the new property value
568
 *
569
 *
569
 * Use this instead of calling plane->atomic_set_property directly.
570
 * Use this instead of calling plane->atomic_set_property directly.
570
 * This function handles generic/core properties and calls out to
571
 * This function handles generic/core properties and calls out to
571
 * driver's ->atomic_set_property() for driver properties.  To ensure
572
 * driver's ->atomic_set_property() for driver properties.  To ensure
572
 * consistent behavior you must call this function rather than the
573
 * consistent behavior you must call this function rather than the
573
 * driver hook directly.
574
 * driver hook directly.
574
 *
575
 *
575
 * RETURNS:
576
 * RETURNS:
576
 * Zero on success, error code on failure
577
 * Zero on success, error code on failure
577
 */
578
 */
578
int drm_atomic_plane_set_property(struct drm_plane *plane,
579
int drm_atomic_plane_set_property(struct drm_plane *plane,
579
		struct drm_plane_state *state, struct drm_property *property,
580
		struct drm_plane_state *state, struct drm_property *property,
580
		uint64_t val)
581
		uint64_t val)
581
{
582
{
582
	struct drm_device *dev = plane->dev;
583
	struct drm_device *dev = plane->dev;
583
	struct drm_mode_config *config = &dev->mode_config;
584
	struct drm_mode_config *config = &dev->mode_config;
584
 
585
 
585
	if (property == config->prop_fb_id) {
586
	if (property == config->prop_fb_id) {
586
		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
587
		struct drm_framebuffer *fb = drm_framebuffer_lookup(dev, val);
587
		drm_atomic_set_fb_for_plane(state, fb);
588
		drm_atomic_set_fb_for_plane(state, fb);
588
		if (fb)
589
		if (fb)
589
			drm_framebuffer_unreference(fb);
590
			drm_framebuffer_unreference(fb);
590
	} else if (property == config->prop_crtc_id) {
591
	} else if (property == config->prop_crtc_id) {
591
		struct drm_crtc *crtc = drm_crtc_find(dev, val);
592
		struct drm_crtc *crtc = drm_crtc_find(dev, val);
592
		return drm_atomic_set_crtc_for_plane(state, crtc);
593
		return drm_atomic_set_crtc_for_plane(state, crtc);
593
	} else if (property == config->prop_crtc_x) {
594
	} else if (property == config->prop_crtc_x) {
594
		state->crtc_x = U642I64(val);
595
		state->crtc_x = U642I64(val);
595
	} else if (property == config->prop_crtc_y) {
596
	} else if (property == config->prop_crtc_y) {
596
		state->crtc_y = U642I64(val);
597
		state->crtc_y = U642I64(val);
597
	} else if (property == config->prop_crtc_w) {
598
	} else if (property == config->prop_crtc_w) {
598
		state->crtc_w = val;
599
		state->crtc_w = val;
599
	} else if (property == config->prop_crtc_h) {
600
	} else if (property == config->prop_crtc_h) {
600
		state->crtc_h = val;
601
		state->crtc_h = val;
601
	} else if (property == config->prop_src_x) {
602
	} else if (property == config->prop_src_x) {
602
		state->src_x = val;
603
		state->src_x = val;
603
	} else if (property == config->prop_src_y) {
604
	} else if (property == config->prop_src_y) {
604
		state->src_y = val;
605
		state->src_y = val;
605
	} else if (property == config->prop_src_w) {
606
	} else if (property == config->prop_src_w) {
606
		state->src_w = val;
607
		state->src_w = val;
607
	} else if (property == config->prop_src_h) {
608
	} else if (property == config->prop_src_h) {
608
		state->src_h = val;
609
		state->src_h = val;
609
	} else if (property == config->rotation_property) {
610
	} else if (property == config->rotation_property) {
610
		state->rotation = val;
611
		state->rotation = val;
611
	} else if (plane->funcs->atomic_set_property) {
612
	} else if (plane->funcs->atomic_set_property) {
612
		return plane->funcs->atomic_set_property(plane, state,
613
		return plane->funcs->atomic_set_property(plane, state,
613
				property, val);
614
				property, val);
614
	} else {
615
	} else {
615
		return -EINVAL;
616
		return -EINVAL;
616
	}
617
	}
617
 
618
 
618
	return 0;
619
	return 0;
619
}
620
}
620
EXPORT_SYMBOL(drm_atomic_plane_set_property);
621
EXPORT_SYMBOL(drm_atomic_plane_set_property);
621
 
622
 
622
/*
623
/*
623
 * This function handles generic/core properties and calls out to
624
 * This function handles generic/core properties and calls out to
624
 * driver's ->atomic_get_property() for driver properties.  To ensure
625
 * driver's ->atomic_get_property() for driver properties.  To ensure
625
 * consistent behavior you must call this function rather than the
626
 * consistent behavior you must call this function rather than the
626
 * driver hook directly.
627
 * driver hook directly.
627
 */
628
 */
628
static int
629
static int
629
drm_atomic_plane_get_property(struct drm_plane *plane,
630
drm_atomic_plane_get_property(struct drm_plane *plane,
630
		const struct drm_plane_state *state,
631
		const struct drm_plane_state *state,
631
		struct drm_property *property, uint64_t *val)
632
		struct drm_property *property, uint64_t *val)
632
{
633
{
633
	struct drm_device *dev = plane->dev;
634
	struct drm_device *dev = plane->dev;
634
	struct drm_mode_config *config = &dev->mode_config;
635
	struct drm_mode_config *config = &dev->mode_config;
635
 
636
 
636
	if (property == config->prop_fb_id) {
637
	if (property == config->prop_fb_id) {
637
		*val = (state->fb) ? state->fb->base.id : 0;
638
		*val = (state->fb) ? state->fb->base.id : 0;
638
	} else if (property == config->prop_crtc_id) {
639
	} else if (property == config->prop_crtc_id) {
639
		*val = (state->crtc) ? state->crtc->base.id : 0;
640
		*val = (state->crtc) ? state->crtc->base.id : 0;
640
	} else if (property == config->prop_crtc_x) {
641
	} else if (property == config->prop_crtc_x) {
641
		*val = I642U64(state->crtc_x);
642
		*val = I642U64(state->crtc_x);
642
	} else if (property == config->prop_crtc_y) {
643
	} else if (property == config->prop_crtc_y) {
643
		*val = I642U64(state->crtc_y);
644
		*val = I642U64(state->crtc_y);
644
	} else if (property == config->prop_crtc_w) {
645
	} else if (property == config->prop_crtc_w) {
645
		*val = state->crtc_w;
646
		*val = state->crtc_w;
646
	} else if (property == config->prop_crtc_h) {
647
	} else if (property == config->prop_crtc_h) {
647
		*val = state->crtc_h;
648
		*val = state->crtc_h;
648
	} else if (property == config->prop_src_x) {
649
	} else if (property == config->prop_src_x) {
649
		*val = state->src_x;
650
		*val = state->src_x;
650
	} else if (property == config->prop_src_y) {
651
	} else if (property == config->prop_src_y) {
651
		*val = state->src_y;
652
		*val = state->src_y;
652
	} else if (property == config->prop_src_w) {
653
	} else if (property == config->prop_src_w) {
653
		*val = state->src_w;
654
		*val = state->src_w;
654
	} else if (property == config->prop_src_h) {
655
	} else if (property == config->prop_src_h) {
655
		*val = state->src_h;
656
		*val = state->src_h;
656
	} else if (property == config->rotation_property) {
657
	} else if (property == config->rotation_property) {
657
		*val = state->rotation;
658
		*val = state->rotation;
658
	} else if (plane->funcs->atomic_get_property) {
659
	} else if (plane->funcs->atomic_get_property) {
659
		return plane->funcs->atomic_get_property(plane, state, property, val);
660
		return plane->funcs->atomic_get_property(plane, state, property, val);
660
	} else {
661
	} else {
661
		return -EINVAL;
662
		return -EINVAL;
662
	}
663
	}
663
 
664
 
664
	return 0;
665
	return 0;
665
}
666
}
666
 
667
 
667
static bool
668
static bool
668
plane_switching_crtc(struct drm_atomic_state *state,
669
plane_switching_crtc(struct drm_atomic_state *state,
669
		     struct drm_plane *plane,
670
		     struct drm_plane *plane,
670
		     struct drm_plane_state *plane_state)
671
		     struct drm_plane_state *plane_state)
671
{
672
{
672
	if (!plane->state->crtc || !plane_state->crtc)
673
	if (!plane->state->crtc || !plane_state->crtc)
673
		return false;
674
		return false;
674
 
675
 
675
	if (plane->state->crtc == plane_state->crtc)
676
	if (plane->state->crtc == plane_state->crtc)
676
		return false;
677
		return false;
677
 
678
 
678
	/* This could be refined, but currently there's no helper or driver code
679
	/* This could be refined, but currently there's no helper or driver code
679
	 * to implement direct switching of active planes nor userspace to take
680
	 * to implement direct switching of active planes nor userspace to take
680
	 * advantage of more direct plane switching without the intermediate
681
	 * advantage of more direct plane switching without the intermediate
681
	 * full OFF state.
682
	 * full OFF state.
682
	 */
683
	 */
683
	return true;
684
	return true;
684
}
685
}
685
 
686
 
686
/**
687
/**
687
 * drm_atomic_plane_check - check plane state
688
 * drm_atomic_plane_check - check plane state
688
 * @plane: plane to check
689
 * @plane: plane to check
689
 * @state: plane state to check
690
 * @state: plane state to check
690
 *
691
 *
691
 * Provides core sanity checks for plane state.
692
 * Provides core sanity checks for plane state.
692
 *
693
 *
693
 * RETURNS:
694
 * RETURNS:
694
 * Zero on success, error code on failure
695
 * Zero on success, error code on failure
695
 */
696
 */
696
static int drm_atomic_plane_check(struct drm_plane *plane,
697
static int drm_atomic_plane_check(struct drm_plane *plane,
697
		struct drm_plane_state *state)
698
		struct drm_plane_state *state)
698
{
699
{
699
	unsigned int fb_width, fb_height;
700
	unsigned int fb_width, fb_height;
700
	int ret;
701
	int ret;
701
 
702
 
702
	/* either *both* CRTC and FB must be set, or neither */
703
	/* either *both* CRTC and FB must be set, or neither */
703
	if (WARN_ON(state->crtc && !state->fb)) {
704
	if (WARN_ON(state->crtc && !state->fb)) {
704
		DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
705
		DRM_DEBUG_ATOMIC("CRTC set but no FB\n");
705
		return -EINVAL;
706
		return -EINVAL;
706
	} else if (WARN_ON(state->fb && !state->crtc)) {
707
	} else if (WARN_ON(state->fb && !state->crtc)) {
707
		DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
708
		DRM_DEBUG_ATOMIC("FB set but no CRTC\n");
708
		return -EINVAL;
709
		return -EINVAL;
709
	}
710
	}
710
 
711
 
711
	/* if disabled, we don't care about the rest of the state: */
712
	/* if disabled, we don't care about the rest of the state: */
712
	if (!state->crtc)
713
	if (!state->crtc)
713
		return 0;
714
		return 0;
714
 
715
 
715
	/* Check whether this plane is usable on this CRTC */
716
	/* Check whether this plane is usable on this CRTC */
716
	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
717
	if (!(plane->possible_crtcs & drm_crtc_mask(state->crtc))) {
717
		DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
718
		DRM_DEBUG_ATOMIC("Invalid crtc for plane\n");
718
		return -EINVAL;
719
		return -EINVAL;
719
	}
720
	}
720
 
721
 
721
	/* Check whether this plane supports the fb pixel format. */
722
	/* Check whether this plane supports the fb pixel format. */
722
	ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
723
	ret = drm_plane_check_pixel_format(plane, state->fb->pixel_format);
723
	if (ret) {
724
	if (ret) {
724
		DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
725
		DRM_DEBUG_ATOMIC("Invalid pixel format %s\n",
725
				 drm_get_format_name(state->fb->pixel_format));
726
				 drm_get_format_name(state->fb->pixel_format));
726
		return ret;
727
		return ret;
727
	}
728
	}
728
 
729
 
729
	/* Give drivers some help against integer overflows */
730
	/* Give drivers some help against integer overflows */
730
	if (state->crtc_w > INT_MAX ||
731
	if (state->crtc_w > INT_MAX ||
731
	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
732
	    state->crtc_x > INT_MAX - (int32_t) state->crtc_w ||
732
	    state->crtc_h > INT_MAX ||
733
	    state->crtc_h > INT_MAX ||
733
	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
734
	    state->crtc_y > INT_MAX - (int32_t) state->crtc_h) {
734
		DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
735
		DRM_DEBUG_ATOMIC("Invalid CRTC coordinates %ux%u+%d+%d\n",
735
				 state->crtc_w, state->crtc_h,
736
				 state->crtc_w, state->crtc_h,
736
				 state->crtc_x, state->crtc_y);
737
				 state->crtc_x, state->crtc_y);
737
		return -ERANGE;
738
		return -ERANGE;
738
	}
739
	}
739
 
740
 
740
	fb_width = state->fb->width << 16;
741
	fb_width = state->fb->width << 16;
741
	fb_height = state->fb->height << 16;
742
	fb_height = state->fb->height << 16;
742
 
743
 
743
	/* Make sure source coordinates are inside the fb. */
744
	/* Make sure source coordinates are inside the fb. */
744
	if (state->src_w > fb_width ||
745
	if (state->src_w > fb_width ||
745
	    state->src_x > fb_width - state->src_w ||
746
	    state->src_x > fb_width - state->src_w ||
746
	    state->src_h > fb_height ||
747
	    state->src_h > fb_height ||
747
	    state->src_y > fb_height - state->src_h) {
748
	    state->src_y > fb_height - state->src_h) {
748
		DRM_DEBUG_ATOMIC("Invalid source coordinates "
749
		DRM_DEBUG_ATOMIC("Invalid source coordinates "
749
				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
750
				 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
750
				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
751
				 state->src_w >> 16, ((state->src_w & 0xffff) * 15625) >> 10,
751
				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
752
				 state->src_h >> 16, ((state->src_h & 0xffff) * 15625) >> 10,
752
				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
753
				 state->src_x >> 16, ((state->src_x & 0xffff) * 15625) >> 10,
753
				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
754
				 state->src_y >> 16, ((state->src_y & 0xffff) * 15625) >> 10);
754
		return -ENOSPC;
755
		return -ENOSPC;
755
	}
756
	}
756
 
757
 
757
	if (plane_switching_crtc(state->state, plane, state)) {
758
	if (plane_switching_crtc(state->state, plane, state)) {
758
		DRM_DEBUG_ATOMIC("[PLANE:%d] switching CRTC directly\n",
759
		DRM_DEBUG_ATOMIC("[PLANE:%d] switching CRTC directly\n",
759
				 plane->base.id);
760
				 plane->base.id);
760
		return -EINVAL;
761
		return -EINVAL;
761
	}
762
	}
762
 
763
 
763
	return 0;
764
	return 0;
764
}
765
}
765
 
766
 
766
/**
767
/**
767
 * drm_atomic_get_connector_state - get connector state
768
 * drm_atomic_get_connector_state - get connector state
768
 * @state: global atomic state object
769
 * @state: global atomic state object
769
 * @connector: connector to get state object for
770
 * @connector: connector to get state object for
770
 *
771
 *
771
 * This function returns the connector state for the given connector,
772
 * This function returns the connector state for the given connector,
772
 * allocating it if needed. It will also grab the relevant connector lock to
773
 * allocating it if needed. It will also grab the relevant connector lock to
773
 * make sure that the state is consistent.
774
 * make sure that the state is consistent.
774
 *
775
 *
775
 * Returns:
776
 * Returns:
776
 *
777
 *
777
 * Either the allocated state or the error code encoded into the pointer. When
778
 * Either the allocated state or the error code encoded into the pointer. When
778
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
779
 * the error is EDEADLK then the w/w mutex code has detected a deadlock and the
779
 * entire atomic sequence must be restarted. All other errors are fatal.
780
 * entire atomic sequence must be restarted. All other errors are fatal.
780
 */
781
 */
781
struct drm_connector_state *
782
struct drm_connector_state *
782
drm_atomic_get_connector_state(struct drm_atomic_state *state,
783
drm_atomic_get_connector_state(struct drm_atomic_state *state,
783
			  struct drm_connector *connector)
784
			  struct drm_connector *connector)
784
{
785
{
785
	int ret, index;
786
	int ret, index;
786
	struct drm_mode_config *config = &connector->dev->mode_config;
787
	struct drm_mode_config *config = &connector->dev->mode_config;
787
	struct drm_connector_state *connector_state;
788
	struct drm_connector_state *connector_state;
788
 
789
 
789
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
790
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
790
	if (ret)
791
	if (ret)
791
		return ERR_PTR(ret);
792
		return ERR_PTR(ret);
792
 
793
 
793
	index = drm_connector_index(connector);
794
	index = drm_connector_index(connector);
794
 
795
 
795
	/*
796
	/*
796
	 * Construction of atomic state updates can race with a connector
797
	 * Construction of atomic state updates can race with a connector
797
	 * hot-add which might overflow. In this case flip the table and just
798
	 * hot-add which might overflow. In this case flip the table and just
798
	 * restart the entire ioctl - no one is fast enough to livelock a cpu
799
	 * restart the entire ioctl - no one is fast enough to livelock a cpu
799
	 * with physical hotplug events anyway.
800
	 * with physical hotplug events anyway.
800
	 *
801
	 *
801
	 * Note that we only grab the indexes once we have the right lock to
802
	 * Note that we only grab the indexes once we have the right lock to
802
	 * prevent hotplug/unplugging of connectors. So removal is no problem,
803
	 * prevent hotplug/unplugging of connectors. So removal is no problem,
803
	 * at most the array is a bit too large.
804
	 * at most the array is a bit too large.
804
	 */
805
	 */
805
	if (index >= state->num_connector) {
806
	if (index >= state->num_connector) {
806
		DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
807
		DRM_DEBUG_ATOMIC("Hot-added connector would overflow state array, restarting\n");
807
		return ERR_PTR(-EAGAIN);
808
		return ERR_PTR(-EAGAIN);
808
	}
809
	}
809
 
810
 
810
	if (state->connector_states[index])
811
	if (state->connector_states[index])
811
		return state->connector_states[index];
812
		return state->connector_states[index];
812
 
813
 
813
	connector_state = connector->funcs->atomic_duplicate_state(connector);
814
	connector_state = connector->funcs->atomic_duplicate_state(connector);
814
	if (!connector_state)
815
	if (!connector_state)
815
		return ERR_PTR(-ENOMEM);
816
		return ERR_PTR(-ENOMEM);
816
 
817
 
817
	state->connector_states[index] = connector_state;
818
	state->connector_states[index] = connector_state;
818
	state->connectors[index] = connector;
819
	state->connectors[index] = connector;
819
	connector_state->state = state;
820
	connector_state->state = state;
820
 
821
 
821
	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
822
	DRM_DEBUG_ATOMIC("Added [CONNECTOR:%d] %p state to %p\n",
822
			 connector->base.id, connector_state, state);
823
			 connector->base.id, connector_state, state);
823
 
824
 
824
	if (connector_state->crtc) {
825
	if (connector_state->crtc) {
825
		struct drm_crtc_state *crtc_state;
826
		struct drm_crtc_state *crtc_state;
826
 
827
 
827
		crtc_state = drm_atomic_get_crtc_state(state,
828
		crtc_state = drm_atomic_get_crtc_state(state,
828
						       connector_state->crtc);
829
						       connector_state->crtc);
829
		if (IS_ERR(crtc_state))
830
		if (IS_ERR(crtc_state))
830
			return ERR_CAST(crtc_state);
831
			return ERR_CAST(crtc_state);
831
	}
832
	}
832
 
833
 
833
	return connector_state;
834
	return connector_state;
834
}
835
}
835
EXPORT_SYMBOL(drm_atomic_get_connector_state);
836
EXPORT_SYMBOL(drm_atomic_get_connector_state);
836
 
837
 
837
/**
838
/**
838
 * drm_atomic_connector_set_property - set property on connector.
839
 * drm_atomic_connector_set_property - set property on connector.
839
 * @connector: the drm connector to set a property on
840
 * @connector: the drm connector to set a property on
840
 * @state: the state object to update with the new property value
841
 * @state: the state object to update with the new property value
841
 * @property: the property to set
842
 * @property: the property to set
842
 * @val: the new property value
843
 * @val: the new property value
843
 *
844
 *
844
 * Use this instead of calling connector->atomic_set_property directly.
845
 * Use this instead of calling connector->atomic_set_property directly.
845
 * This function handles generic/core properties and calls out to
846
 * This function handles generic/core properties and calls out to
846
 * driver's ->atomic_set_property() for driver properties.  To ensure
847
 * driver's ->atomic_set_property() for driver properties.  To ensure
847
 * consistent behavior you must call this function rather than the
848
 * consistent behavior you must call this function rather than the
848
 * driver hook directly.
849
 * driver hook directly.
849
 *
850
 *
850
 * RETURNS:
851
 * RETURNS:
851
 * Zero on success, error code on failure
852
 * Zero on success, error code on failure
852
 */
853
 */
853
int drm_atomic_connector_set_property(struct drm_connector *connector,
854
int drm_atomic_connector_set_property(struct drm_connector *connector,
854
		struct drm_connector_state *state, struct drm_property *property,
855
		struct drm_connector_state *state, struct drm_property *property,
855
		uint64_t val)
856
		uint64_t val)
856
{
857
{
857
	struct drm_device *dev = connector->dev;
858
	struct drm_device *dev = connector->dev;
858
	struct drm_mode_config *config = &dev->mode_config;
859
	struct drm_mode_config *config = &dev->mode_config;
859
 
860
 
860
	if (property == config->prop_crtc_id) {
861
	if (property == config->prop_crtc_id) {
861
		struct drm_crtc *crtc = drm_crtc_find(dev, val);
862
		struct drm_crtc *crtc = drm_crtc_find(dev, val);
862
		return drm_atomic_set_crtc_for_connector(state, crtc);
863
		return drm_atomic_set_crtc_for_connector(state, crtc);
863
	} else if (property == config->dpms_property) {
864
	} else if (property == config->dpms_property) {
864
		/* setting DPMS property requires special handling, which
865
		/* setting DPMS property requires special handling, which
865
		 * is done in legacy setprop path for us.  Disallow (for
866
		 * is done in legacy setprop path for us.  Disallow (for
866
		 * now?) atomic writes to DPMS property:
867
		 * now?) atomic writes to DPMS property:
867
		 */
868
		 */
868
		return -EINVAL;
869
		return -EINVAL;
869
	} else if (connector->funcs->atomic_set_property) {
870
	} else if (connector->funcs->atomic_set_property) {
870
		return connector->funcs->atomic_set_property(connector,
871
		return connector->funcs->atomic_set_property(connector,
871
				state, property, val);
872
				state, property, val);
872
	} else {
873
	} else {
873
		return -EINVAL;
874
		return -EINVAL;
874
	}
875
	}
875
}
876
}
876
EXPORT_SYMBOL(drm_atomic_connector_set_property);
877
EXPORT_SYMBOL(drm_atomic_connector_set_property);
877
 
878
 
878
/*
879
/*
879
 * This function handles generic/core properties and calls out to
880
 * This function handles generic/core properties and calls out to
880
 * driver's ->atomic_get_property() for driver properties.  To ensure
881
 * driver's ->atomic_get_property() for driver properties.  To ensure
881
 * consistent behavior you must call this function rather than the
882
 * consistent behavior you must call this function rather than the
882
 * driver hook directly.
883
 * driver hook directly.
883
 */
884
 */
884
static int
885
static int
885
drm_atomic_connector_get_property(struct drm_connector *connector,
886
drm_atomic_connector_get_property(struct drm_connector *connector,
886
		const struct drm_connector_state *state,
887
		const struct drm_connector_state *state,
887
		struct drm_property *property, uint64_t *val)
888
		struct drm_property *property, uint64_t *val)
888
{
889
{
889
	struct drm_device *dev = connector->dev;
890
	struct drm_device *dev = connector->dev;
890
	struct drm_mode_config *config = &dev->mode_config;
891
	struct drm_mode_config *config = &dev->mode_config;
891
 
892
 
892
	if (property == config->prop_crtc_id) {
893
	if (property == config->prop_crtc_id) {
893
		*val = (state->crtc) ? state->crtc->base.id : 0;
894
		*val = (state->crtc) ? state->crtc->base.id : 0;
894
	} else if (property == config->dpms_property) {
895
	} else if (property == config->dpms_property) {
895
		*val = connector->dpms;
896
		*val = connector->dpms;
896
	} else if (connector->funcs->atomic_get_property) {
897
	} else if (connector->funcs->atomic_get_property) {
897
		return connector->funcs->atomic_get_property(connector,
898
		return connector->funcs->atomic_get_property(connector,
898
				state, property, val);
899
				state, property, val);
899
	} else {
900
	} else {
900
		return -EINVAL;
901
		return -EINVAL;
901
	}
902
	}
902
 
903
 
903
	return 0;
904
	return 0;
904
}
905
}
905
 
906
 
906
int drm_atomic_get_property(struct drm_mode_object *obj,
907
int drm_atomic_get_property(struct drm_mode_object *obj,
907
		struct drm_property *property, uint64_t *val)
908
		struct drm_property *property, uint64_t *val)
908
{
909
{
909
	struct drm_device *dev = property->dev;
910
	struct drm_device *dev = property->dev;
910
	int ret;
911
	int ret;
911
 
912
 
912
	switch (obj->type) {
913
	switch (obj->type) {
913
	case DRM_MODE_OBJECT_CONNECTOR: {
914
	case DRM_MODE_OBJECT_CONNECTOR: {
914
		struct drm_connector *connector = obj_to_connector(obj);
915
		struct drm_connector *connector = obj_to_connector(obj);
915
		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
916
		WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
916
		ret = drm_atomic_connector_get_property(connector,
917
		ret = drm_atomic_connector_get_property(connector,
917
				connector->state, property, val);
918
				connector->state, property, val);
918
		break;
919
		break;
919
	}
920
	}
920
	case DRM_MODE_OBJECT_CRTC: {
921
	case DRM_MODE_OBJECT_CRTC: {
921
		struct drm_crtc *crtc = obj_to_crtc(obj);
922
		struct drm_crtc *crtc = obj_to_crtc(obj);
922
		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
923
		WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
923
		ret = drm_atomic_crtc_get_property(crtc,
924
		ret = drm_atomic_crtc_get_property(crtc,
924
				crtc->state, property, val);
925
				crtc->state, property, val);
925
		break;
926
		break;
926
	}
927
	}
927
	case DRM_MODE_OBJECT_PLANE: {
928
	case DRM_MODE_OBJECT_PLANE: {
928
		struct drm_plane *plane = obj_to_plane(obj);
929
		struct drm_plane *plane = obj_to_plane(obj);
929
		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
930
		WARN_ON(!drm_modeset_is_locked(&plane->mutex));
930
		ret = drm_atomic_plane_get_property(plane,
931
		ret = drm_atomic_plane_get_property(plane,
931
				plane->state, property, val);
932
				plane->state, property, val);
932
		break;
933
		break;
933
	}
934
	}
934
	default:
935
	default:
935
		ret = -EINVAL;
936
		ret = -EINVAL;
936
		break;
937
		break;
937
	}
938
	}
938
 
939
 
939
	return ret;
940
	return ret;
940
}
941
}
941
 
942
 
942
/**
943
/**
943
 * drm_atomic_set_crtc_for_plane - set crtc for plane
944
 * drm_atomic_set_crtc_for_plane - set crtc for plane
944
 * @plane_state: the plane whose incoming state to update
945
 * @plane_state: the plane whose incoming state to update
945
 * @crtc: crtc to use for the plane
946
 * @crtc: crtc to use for the plane
946
 *
947
 *
947
 * Changing the assigned crtc for a plane requires us to grab the lock and state
948
 * Changing the assigned crtc for a plane requires us to grab the lock and state
948
 * for the new crtc, as needed. This function takes care of all these details
949
 * for the new crtc, as needed. This function takes care of all these details
949
 * besides updating the pointer in the state object itself.
950
 * besides updating the pointer in the state object itself.
950
 *
951
 *
951
 * Returns:
952
 * Returns:
952
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
953
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
953
 * then the w/w mutex code has detected a deadlock and the entire atomic
954
 * then the w/w mutex code has detected a deadlock and the entire atomic
954
 * sequence must be restarted. All other errors are fatal.
955
 * sequence must be restarted. All other errors are fatal.
955
 */
956
 */
956
int
957
int
957
drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
958
drm_atomic_set_crtc_for_plane(struct drm_plane_state *plane_state,
958
			      struct drm_crtc *crtc)
959
			      struct drm_crtc *crtc)
959
{
960
{
960
	struct drm_plane *plane = plane_state->plane;
961
	struct drm_plane *plane = plane_state->plane;
961
	struct drm_crtc_state *crtc_state;
962
	struct drm_crtc_state *crtc_state;
962
 
963
 
963
	if (plane_state->crtc) {
964
	if (plane_state->crtc) {
964
		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
965
		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
965
						       plane_state->crtc);
966
						       plane_state->crtc);
966
		if (WARN_ON(IS_ERR(crtc_state)))
967
		if (WARN_ON(IS_ERR(crtc_state)))
967
			return PTR_ERR(crtc_state);
968
			return PTR_ERR(crtc_state);
968
 
969
 
969
		crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
970
		crtc_state->plane_mask &= ~(1 << drm_plane_index(plane));
970
	}
971
	}
971
 
972
 
972
	plane_state->crtc = crtc;
973
	plane_state->crtc = crtc;
973
 
974
 
974
	if (crtc) {
975
	if (crtc) {
975
		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
976
		crtc_state = drm_atomic_get_crtc_state(plane_state->state,
976
						       crtc);
977
						       crtc);
977
		if (IS_ERR(crtc_state))
978
		if (IS_ERR(crtc_state))
978
			return PTR_ERR(crtc_state);
979
			return PTR_ERR(crtc_state);
979
		crtc_state->plane_mask |= (1 << drm_plane_index(plane));
980
		crtc_state->plane_mask |= (1 << drm_plane_index(plane));
980
	}
981
	}
981
 
982
 
982
	if (crtc)
983
	if (crtc)
983
		DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
984
		DRM_DEBUG_ATOMIC("Link plane state %p to [CRTC:%d]\n",
984
				 plane_state, crtc->base.id);
985
				 plane_state, crtc->base.id);
985
	else
986
	else
986
		DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
987
		DRM_DEBUG_ATOMIC("Link plane state %p to [NOCRTC]\n",
987
				 plane_state);
988
				 plane_state);
988
 
989
 
989
	return 0;
990
	return 0;
990
}
991
}
991
EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
992
EXPORT_SYMBOL(drm_atomic_set_crtc_for_plane);
992
 
993
 
993
/**
994
/**
994
 * drm_atomic_set_fb_for_plane - set framebuffer for plane
995
 * drm_atomic_set_fb_for_plane - set framebuffer for plane
995
 * @plane_state: atomic state object for the plane
996
 * @plane_state: atomic state object for the plane
996
 * @fb: fb to use for the plane
997
 * @fb: fb to use for the plane
997
 *
998
 *
998
 * Changing the assigned framebuffer for a plane requires us to grab a reference
999
 * Changing the assigned framebuffer for a plane requires us to grab a reference
999
 * to the new fb and drop the reference to the old fb, if there is one. This
1000
 * to the new fb and drop the reference to the old fb, if there is one. This
1000
 * function takes care of all these details besides updating the pointer in the
1001
 * function takes care of all these details besides updating the pointer in the
1001
 * state object itself.
1002
 * state object itself.
1002
 */
1003
 */
1003
void
1004
void
1004
drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1005
drm_atomic_set_fb_for_plane(struct drm_plane_state *plane_state,
1005
			    struct drm_framebuffer *fb)
1006
			    struct drm_framebuffer *fb)
1006
{
1007
{
1007
	if (plane_state->fb)
1008
	if (plane_state->fb)
1008
		drm_framebuffer_unreference(plane_state->fb);
1009
		drm_framebuffer_unreference(plane_state->fb);
1009
	if (fb)
1010
	if (fb)
1010
		drm_framebuffer_reference(fb);
1011
		drm_framebuffer_reference(fb);
1011
	plane_state->fb = fb;
1012
	plane_state->fb = fb;
1012
 
1013
 
1013
	if (fb)
1014
	if (fb)
1014
		DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1015
		DRM_DEBUG_ATOMIC("Set [FB:%d] for plane state %p\n",
1015
				 fb->base.id, plane_state);
1016
				 fb->base.id, plane_state);
1016
	else
1017
	else
1017
		DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1018
		DRM_DEBUG_ATOMIC("Set [NOFB] for plane state %p\n",
1018
				 plane_state);
1019
				 plane_state);
1019
}
1020
}
1020
EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1021
EXPORT_SYMBOL(drm_atomic_set_fb_for_plane);
1021
 
1022
 
1022
/**
1023
/**
1023
 * drm_atomic_set_crtc_for_connector - set crtc for connector
1024
 * drm_atomic_set_crtc_for_connector - set crtc for connector
1024
 * @conn_state: atomic state object for the connector
1025
 * @conn_state: atomic state object for the connector
1025
 * @crtc: crtc to use for the connector
1026
 * @crtc: crtc to use for the connector
1026
 *
1027
 *
1027
 * Changing the assigned crtc for a connector requires us to grab the lock and
1028
 * Changing the assigned crtc for a connector requires us to grab the lock and
1028
 * state for the new crtc, as needed. This function takes care of all these
1029
 * state for the new crtc, as needed. This function takes care of all these
1029
 * details besides updating the pointer in the state object itself.
1030
 * details besides updating the pointer in the state object itself.
1030
 *
1031
 *
1031
 * Returns:
1032
 * Returns:
1032
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1033
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1033
 * then the w/w mutex code has detected a deadlock and the entire atomic
1034
 * then the w/w mutex code has detected a deadlock and the entire atomic
1034
 * sequence must be restarted. All other errors are fatal.
1035
 * sequence must be restarted. All other errors are fatal.
1035
 */
1036
 */
1036
int
1037
int
1037
drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1038
drm_atomic_set_crtc_for_connector(struct drm_connector_state *conn_state,
1038
				  struct drm_crtc *crtc)
1039
				  struct drm_crtc *crtc)
1039
{
1040
{
1040
	struct drm_crtc_state *crtc_state;
1041
	struct drm_crtc_state *crtc_state;
1041
 
1042
 
1042
	if (crtc) {
1043
	if (crtc) {
1043
		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1044
		crtc_state = drm_atomic_get_crtc_state(conn_state->state, crtc);
1044
		if (IS_ERR(crtc_state))
1045
		if (IS_ERR(crtc_state))
1045
			return PTR_ERR(crtc_state);
1046
			return PTR_ERR(crtc_state);
1046
	}
1047
	}
1047
 
1048
 
1048
	conn_state->crtc = crtc;
1049
	conn_state->crtc = crtc;
1049
 
1050
 
1050
	if (crtc)
1051
	if (crtc)
1051
		DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
1052
		DRM_DEBUG_ATOMIC("Link connector state %p to [CRTC:%d]\n",
1052
				 conn_state, crtc->base.id);
1053
				 conn_state, crtc->base.id);
1053
	else
1054
	else
1054
		DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1055
		DRM_DEBUG_ATOMIC("Link connector state %p to [NOCRTC]\n",
1055
				 conn_state);
1056
				 conn_state);
1056
 
1057
 
1057
	return 0;
1058
	return 0;
1058
}
1059
}
1059
EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1060
EXPORT_SYMBOL(drm_atomic_set_crtc_for_connector);
1060
 
1061
 
1061
/**
1062
/**
1062
 * drm_atomic_add_affected_connectors - add connectors for crtc
1063
 * drm_atomic_add_affected_connectors - add connectors for crtc
1063
 * @state: atomic state
1064
 * @state: atomic state
1064
 * @crtc: DRM crtc
1065
 * @crtc: DRM crtc
1065
 *
1066
 *
1066
 * This function walks the current configuration and adds all connectors
1067
 * This function walks the current configuration and adds all connectors
1067
 * currently using @crtc to the atomic configuration @state. Note that this
1068
 * currently using @crtc to the atomic configuration @state. Note that this
1068
 * function must acquire the connection mutex. This can potentially cause
1069
 * function must acquire the connection mutex. This can potentially cause
1069
 * unneeded seralization if the update is just for the planes on one crtc. Hence
1070
 * unneeded seralization if the update is just for the planes on one crtc. Hence
1070
 * drivers and helpers should only call this when really needed (e.g. when a
1071
 * drivers and helpers should only call this when really needed (e.g. when a
1071
 * full modeset needs to happen due to some change).
1072
 * full modeset needs to happen due to some change).
1072
 *
1073
 *
1073
 * Returns:
1074
 * Returns:
1074
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1075
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1075
 * then the w/w mutex code has detected a deadlock and the entire atomic
1076
 * then the w/w mutex code has detected a deadlock and the entire atomic
1076
 * sequence must be restarted. All other errors are fatal.
1077
 * sequence must be restarted. All other errors are fatal.
1077
 */
1078
 */
1078
int
1079
int
1079
drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1080
drm_atomic_add_affected_connectors(struct drm_atomic_state *state,
1080
				   struct drm_crtc *crtc)
1081
				   struct drm_crtc *crtc)
1081
{
1082
{
1082
	struct drm_mode_config *config = &state->dev->mode_config;
1083
	struct drm_mode_config *config = &state->dev->mode_config;
1083
	struct drm_connector *connector;
1084
	struct drm_connector *connector;
1084
	struct drm_connector_state *conn_state;
1085
	struct drm_connector_state *conn_state;
1085
	int ret;
1086
	int ret;
1086
 
1087
 
1087
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1088
	ret = drm_modeset_lock(&config->connection_mutex, state->acquire_ctx);
1088
	if (ret)
1089
	if (ret)
1089
		return ret;
1090
		return ret;
1090
 
1091
 
1091
	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
1092
	DRM_DEBUG_ATOMIC("Adding all current connectors for [CRTC:%d] to %p\n",
1092
			 crtc->base.id, state);
1093
			 crtc->base.id, state);
1093
 
1094
 
1094
	/*
1095
	/*
1095
	 * Changed connectors are already in @state, so only need to look at the
1096
	 * Changed connectors are already in @state, so only need to look at the
1096
	 * current configuration.
1097
	 * current configuration.
1097
	 */
1098
	 */
1098
	drm_for_each_connector(connector, state->dev) {
1099
	drm_for_each_connector(connector, state->dev) {
1099
		if (connector->state->crtc != crtc)
1100
		if (connector->state->crtc != crtc)
1100
			continue;
1101
			continue;
1101
 
1102
 
1102
		conn_state = drm_atomic_get_connector_state(state, connector);
1103
		conn_state = drm_atomic_get_connector_state(state, connector);
1103
		if (IS_ERR(conn_state))
1104
		if (IS_ERR(conn_state))
1104
			return PTR_ERR(conn_state);
1105
			return PTR_ERR(conn_state);
1105
	}
1106
	}
1106
 
1107
 
1107
	return 0;
1108
	return 0;
1108
}
1109
}
1109
EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1110
EXPORT_SYMBOL(drm_atomic_add_affected_connectors);
1110
 
1111
 
1111
/**
1112
/**
1112
 * drm_atomic_add_affected_planes - add planes for crtc
1113
 * drm_atomic_add_affected_planes - add planes for crtc
1113
 * @state: atomic state
1114
 * @state: atomic state
1114
 * @crtc: DRM crtc
1115
 * @crtc: DRM crtc
1115
 *
1116
 *
1116
 * This function walks the current configuration and adds all planes
1117
 * This function walks the current configuration and adds all planes
1117
 * currently used by @crtc to the atomic configuration @state. This is useful
1118
 * currently used by @crtc to the atomic configuration @state. This is useful
1118
 * when an atomic commit also needs to check all currently enabled plane on
1119
 * when an atomic commit also needs to check all currently enabled plane on
1119
 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1120
 * @crtc, e.g. when changing the mode. It's also useful when re-enabling a CRTC
1120
 * to avoid special code to force-enable all planes.
1121
 * to avoid special code to force-enable all planes.
1121
 *
1122
 *
1122
 * Since acquiring a plane state will always also acquire the w/w mutex of the
1123
 * Since acquiring a plane state will always also acquire the w/w mutex of the
1123
 * current CRTC for that plane (if there is any) adding all the plane states for
1124
 * current CRTC for that plane (if there is any) adding all the plane states for
1124
 * a CRTC will not reduce parallism of atomic updates.
1125
 * a CRTC will not reduce parallism of atomic updates.
1125
 *
1126
 *
1126
 * Returns:
1127
 * Returns:
1127
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1128
 * 0 on success or can fail with -EDEADLK or -ENOMEM. When the error is EDEADLK
1128
 * then the w/w mutex code has detected a deadlock and the entire atomic
1129
 * then the w/w mutex code has detected a deadlock and the entire atomic
1129
 * sequence must be restarted. All other errors are fatal.
1130
 * sequence must be restarted. All other errors are fatal.
1130
 */
1131
 */
1131
int
1132
int
1132
drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1133
drm_atomic_add_affected_planes(struct drm_atomic_state *state,
1133
			       struct drm_crtc *crtc)
1134
			       struct drm_crtc *crtc)
1134
{
1135
{
1135
	struct drm_plane *plane;
1136
	struct drm_plane *plane;
1136
 
1137
 
1137
	WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1138
	WARN_ON(!drm_atomic_get_existing_crtc_state(state, crtc));
1138
 
1139
 
1139
	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1140
	drm_for_each_plane_mask(plane, state->dev, crtc->state->plane_mask) {
1140
		struct drm_plane_state *plane_state =
1141
		struct drm_plane_state *plane_state =
1141
			drm_atomic_get_plane_state(state, plane);
1142
			drm_atomic_get_plane_state(state, plane);
1142
 
1143
 
1143
		if (IS_ERR(plane_state))
1144
		if (IS_ERR(plane_state))
1144
			return PTR_ERR(plane_state);
1145
			return PTR_ERR(plane_state);
1145
	}
1146
	}
1146
	return 0;
1147
	return 0;
1147
}
1148
}
1148
EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1149
EXPORT_SYMBOL(drm_atomic_add_affected_planes);
1149
 
1150
 
1150
/**
1151
/**
1151
 * drm_atomic_connectors_for_crtc - count number of connected outputs
1152
 * drm_atomic_connectors_for_crtc - count number of connected outputs
1152
 * @state: atomic state
1153
 * @state: atomic state
1153
 * @crtc: DRM crtc
1154
 * @crtc: DRM crtc
1154
 *
1155
 *
1155
 * This function counts all connectors which will be connected to @crtc
1156
 * This function counts all connectors which will be connected to @crtc
1156
 * according to @state. Useful to recompute the enable state for @crtc.
1157
 * according to @state. Useful to recompute the enable state for @crtc.
1157
 */
1158
 */
1158
int
1159
int
1159
drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1160
drm_atomic_connectors_for_crtc(struct drm_atomic_state *state,
1160
			       struct drm_crtc *crtc)
1161
			       struct drm_crtc *crtc)
1161
{
1162
{
1162
	struct drm_connector *connector;
1163
	struct drm_connector *connector;
1163
	struct drm_connector_state *conn_state;
1164
	struct drm_connector_state *conn_state;
1164
 
1165
 
1165
	int i, num_connected_connectors = 0;
1166
	int i, num_connected_connectors = 0;
1166
 
1167
 
1167
	for_each_connector_in_state(state, connector, conn_state, i) {
1168
	for_each_connector_in_state(state, connector, conn_state, i) {
1168
		if (conn_state->crtc == crtc)
1169
		if (conn_state->crtc == crtc)
1169
			num_connected_connectors++;
1170
			num_connected_connectors++;
1170
	}
1171
	}
1171
 
1172
 
1172
	DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1173
	DRM_DEBUG_ATOMIC("State %p has %i connectors for [CRTC:%d]\n",
1173
			 state, num_connected_connectors, crtc->base.id);
1174
			 state, num_connected_connectors, crtc->base.id);
1174
 
1175
 
1175
	return num_connected_connectors;
1176
	return num_connected_connectors;
1176
}
1177
}
1177
EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1178
EXPORT_SYMBOL(drm_atomic_connectors_for_crtc);
1178
 
1179
 
1179
/**
1180
/**
1180
 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1181
 * drm_atomic_legacy_backoff - locking backoff for legacy ioctls
1181
 * @state: atomic state
1182
 * @state: atomic state
1182
 *
1183
 *
1183
 * This function should be used by legacy entry points which don't understand
1184
 * This function should be used by legacy entry points which don't understand
1184
 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1185
 * -EDEADLK semantics. For simplicity this one will grab all modeset locks after
1185
 * the slowpath completed.
1186
 * the slowpath completed.
1186
 */
1187
 */
1187
void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1188
void drm_atomic_legacy_backoff(struct drm_atomic_state *state)
1188
{
1189
{
1189
	int ret;
1190
	int ret;
1190
 
1191
 
1191
retry:
1192
retry:
1192
	drm_modeset_backoff(state->acquire_ctx);
1193
	drm_modeset_backoff(state->acquire_ctx);
1193
 
1194
 
1194
	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1195
	ret = drm_modeset_lock(&state->dev->mode_config.connection_mutex,
1195
			       state->acquire_ctx);
1196
			       state->acquire_ctx);
1196
	if (ret)
1197
	if (ret)
1197
		goto retry;
1198
		goto retry;
1198
	ret = drm_modeset_lock_all_crtcs(state->dev,
1199
	ret = drm_modeset_lock_all_crtcs(state->dev,
1199
					 state->acquire_ctx);
1200
					 state->acquire_ctx);
1200
	if (ret)
1201
	if (ret)
1201
		goto retry;
1202
		goto retry;
1202
}
1203
}
1203
EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1204
EXPORT_SYMBOL(drm_atomic_legacy_backoff);
1204
 
1205
 
1205
/**
1206
/**
1206
 * drm_atomic_check_only - check whether a given config would work
1207
 * drm_atomic_check_only - check whether a given config would work
1207
 * @state: atomic configuration to check
1208
 * @state: atomic configuration to check
1208
 *
1209
 *
1209
 * Note that this function can return -EDEADLK if the driver needed to acquire
1210
 * Note that this function can return -EDEADLK if the driver needed to acquire
1210
 * more locks but encountered a deadlock. The caller must then do the usual w/w
1211
 * more locks but encountered a deadlock. The caller must then do the usual w/w
1211
 * backoff dance and restart. All other errors are fatal.
1212
 * backoff dance and restart. All other errors are fatal.
1212
 *
1213
 *
1213
 * Returns:
1214
 * Returns:
1214
 * 0 on success, negative error code on failure.
1215
 * 0 on success, negative error code on failure.
1215
 */
1216
 */
1216
int drm_atomic_check_only(struct drm_atomic_state *state)
1217
int drm_atomic_check_only(struct drm_atomic_state *state)
1217
{
1218
{
1218
	struct drm_device *dev = state->dev;
1219
	struct drm_device *dev = state->dev;
1219
	struct drm_mode_config *config = &dev->mode_config;
1220
	struct drm_mode_config *config = &dev->mode_config;
1220
	struct drm_plane *plane;
1221
	struct drm_plane *plane;
1221
	struct drm_plane_state *plane_state;
1222
	struct drm_plane_state *plane_state;
1222
	struct drm_crtc *crtc;
1223
	struct drm_crtc *crtc;
1223
	struct drm_crtc_state *crtc_state;
1224
	struct drm_crtc_state *crtc_state;
1224
	int i, ret = 0;
1225
	int i, ret = 0;
1225
 
1226
 
1226
	DRM_DEBUG_ATOMIC("checking %p\n", state);
1227
	DRM_DEBUG_ATOMIC("checking %p\n", state);
1227
 
1228
 
1228
	for_each_plane_in_state(state, plane, plane_state, i) {
1229
	for_each_plane_in_state(state, plane, plane_state, i) {
1229
		ret = drm_atomic_plane_check(plane, plane_state);
1230
		ret = drm_atomic_plane_check(plane, plane_state);
1230
		if (ret) {
1231
		if (ret) {
1231
			DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1232
			DRM_DEBUG_ATOMIC("[PLANE:%d] atomic core check failed\n",
1232
					 plane->base.id);
1233
					 plane->base.id);
1233
			return ret;
1234
			return ret;
1234
		}
1235
		}
1235
	}
1236
	}
1236
 
1237
 
1237
	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1238
	for_each_crtc_in_state(state, crtc, crtc_state, i) {
1238
		ret = drm_atomic_crtc_check(crtc, crtc_state);
1239
		ret = drm_atomic_crtc_check(crtc, crtc_state);
1239
		if (ret) {
1240
		if (ret) {
1240
			DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1241
			DRM_DEBUG_ATOMIC("[CRTC:%d] atomic core check failed\n",
1241
					 crtc->base.id);
1242
					 crtc->base.id);
1242
			return ret;
1243
			return ret;
1243
		}
1244
		}
1244
	}
1245
	}
1245
 
1246
 
1246
	if (config->funcs->atomic_check)
1247
	if (config->funcs->atomic_check)
1247
		ret = config->funcs->atomic_check(state->dev, state);
1248
		ret = config->funcs->atomic_check(state->dev, state);
1248
 
1249
 
1249
	if (!state->allow_modeset) {
1250
	if (!state->allow_modeset) {
1250
		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1251
		for_each_crtc_in_state(state, crtc, crtc_state, i) {
1251
			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1252
			if (drm_atomic_crtc_needs_modeset(crtc_state)) {
1252
				DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1253
				DRM_DEBUG_ATOMIC("[CRTC:%d] requires full modeset\n",
1253
						 crtc->base.id);
1254
						 crtc->base.id);
1254
				return -EINVAL;
1255
				return -EINVAL;
1255
			}
1256
			}
1256
		}
1257
		}
1257
	}
1258
	}
1258
 
1259
 
1259
	return ret;
1260
	return ret;
1260
}
1261
}
1261
EXPORT_SYMBOL(drm_atomic_check_only);
1262
EXPORT_SYMBOL(drm_atomic_check_only);
1262
 
1263
 
1263
/**
1264
/**
1264
 * drm_atomic_commit - commit configuration atomically
1265
 * drm_atomic_commit - commit configuration atomically
1265
 * @state: atomic configuration to check
1266
 * @state: atomic configuration to check
1266
 *
1267
 *
1267
 * Note that this function can return -EDEADLK if the driver needed to acquire
1268
 * Note that this function can return -EDEADLK if the driver needed to acquire
1268
 * more locks but encountered a deadlock. The caller must then do the usual w/w
1269
 * more locks but encountered a deadlock. The caller must then do the usual w/w
1269
 * backoff dance and restart. All other errors are fatal.
1270
 * backoff dance and restart. All other errors are fatal.
1270
 *
1271
 *
1271
 * Also note that on successful execution ownership of @state is transferred
1272
 * Also note that on successful execution ownership of @state is transferred
1272
 * from the caller of this function to the function itself. The caller must not
1273
 * from the caller of this function to the function itself. The caller must not
1273
 * free or in any other way access @state. If the function fails then the caller
1274
 * free or in any other way access @state. If the function fails then the caller
1274
 * must clean up @state itself.
1275
 * must clean up @state itself.
1275
 *
1276
 *
1276
 * Returns:
1277
 * Returns:
1277
 * 0 on success, negative error code on failure.
1278
 * 0 on success, negative error code on failure.
1278
 */
1279
 */
1279
int drm_atomic_commit(struct drm_atomic_state *state)
1280
int drm_atomic_commit(struct drm_atomic_state *state)
1280
{
1281
{
1281
	struct drm_mode_config *config = &state->dev->mode_config;
1282
	struct drm_mode_config *config = &state->dev->mode_config;
1282
	int ret;
1283
	int ret;
1283
 
1284
 
1284
	ret = drm_atomic_check_only(state);
1285
	ret = drm_atomic_check_only(state);
1285
	if (ret)
1286
	if (ret)
1286
		return ret;
1287
		return ret;
1287
 
1288
 
1288
	DRM_DEBUG_ATOMIC("commiting %p\n", state);
1289
	DRM_DEBUG_ATOMIC("commiting %p\n", state);
1289
 
1290
 
1290
	return config->funcs->atomic_commit(state->dev, state, false);
1291
	return config->funcs->atomic_commit(state->dev, state, false);
1291
}
1292
}
1292
EXPORT_SYMBOL(drm_atomic_commit);
1293
EXPORT_SYMBOL(drm_atomic_commit);
1293
 
1294
 
1294
/**
1295
/**
1295
 * drm_atomic_async_commit - atomic&async configuration commit
1296
 * drm_atomic_async_commit - atomic&async configuration commit
1296
 * @state: atomic configuration to check
1297
 * @state: atomic configuration to check
1297
 *
1298
 *
1298
 * Note that this function can return -EDEADLK if the driver needed to acquire
1299
 * Note that this function can return -EDEADLK if the driver needed to acquire
1299
 * more locks but encountered a deadlock. The caller must then do the usual w/w
1300
 * more locks but encountered a deadlock. The caller must then do the usual w/w
1300
 * backoff dance and restart. All other errors are fatal.
1301
 * backoff dance and restart. All other errors are fatal.
1301
 *
1302
 *
1302
 * Also note that on successful execution ownership of @state is transferred
1303
 * Also note that on successful execution ownership of @state is transferred
1303
 * from the caller of this function to the function itself. The caller must not
1304
 * from the caller of this function to the function itself. The caller must not
1304
 * free or in any other way access @state. If the function fails then the caller
1305
 * free or in any other way access @state. If the function fails then the caller
1305
 * must clean up @state itself.
1306
 * must clean up @state itself.
1306
 *
1307
 *
1307
 * Returns:
1308
 * Returns:
1308
 * 0 on success, negative error code on failure.
1309
 * 0 on success, negative error code on failure.
1309
 */
1310
 */
1310
int drm_atomic_async_commit(struct drm_atomic_state *state)
1311
int drm_atomic_async_commit(struct drm_atomic_state *state)
1311
{
1312
{
1312
	struct drm_mode_config *config = &state->dev->mode_config;
1313
	struct drm_mode_config *config = &state->dev->mode_config;
1313
	int ret;
1314
	int ret;
1314
 
1315
 
1315
	ret = drm_atomic_check_only(state);
1316
	ret = drm_atomic_check_only(state);
1316
	if (ret)
1317
	if (ret)
1317
		return ret;
1318
		return ret;
1318
 
1319
 
1319
	DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1320
	DRM_DEBUG_ATOMIC("commiting %p asynchronously\n", state);
1320
 
1321
 
1321
	return config->funcs->atomic_commit(state->dev, state, true);
1322
	return config->funcs->atomic_commit(state->dev, state, true);
1322
}
1323
}
1323
EXPORT_SYMBOL(drm_atomic_async_commit);
1324
EXPORT_SYMBOL(drm_atomic_async_commit);
1324
 
1325
 
1325
/*
1326
/*
1326
 * The big monstor ioctl
1327
 * The big monstor ioctl
1327
 */
1328
 */
1328
 
1329
 
1329
static struct drm_pending_vblank_event *create_vblank_event(
1330
static struct drm_pending_vblank_event *create_vblank_event(
1330
		struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1331
		struct drm_device *dev, struct drm_file *file_priv, uint64_t user_data)
1331
{
1332
{
1332
	struct drm_pending_vblank_event *e = NULL;
1333
	struct drm_pending_vblank_event *e = NULL;
1333
	unsigned long flags;
1334
	unsigned long flags;
1334
 
1335
 
1335
	spin_lock_irqsave(&dev->event_lock, flags);
1336
	spin_lock_irqsave(&dev->event_lock, flags);
1336
	if (file_priv->event_space < sizeof e->event) {
1337
	if (file_priv->event_space < sizeof e->event) {
1337
		spin_unlock_irqrestore(&dev->event_lock, flags);
1338
		spin_unlock_irqrestore(&dev->event_lock, flags);
1338
		goto out;
1339
		goto out;
1339
	}
1340
	}
1340
	file_priv->event_space -= sizeof e->event;
1341
	file_priv->event_space -= sizeof e->event;
1341
	spin_unlock_irqrestore(&dev->event_lock, flags);
1342
	spin_unlock_irqrestore(&dev->event_lock, flags);
1342
 
1343
 
1343
	e = kzalloc(sizeof *e, GFP_KERNEL);
1344
	e = kzalloc(sizeof *e, GFP_KERNEL);
1344
	if (e == NULL) {
1345
	if (e == NULL) {
1345
		spin_lock_irqsave(&dev->event_lock, flags);
1346
		spin_lock_irqsave(&dev->event_lock, flags);
1346
		file_priv->event_space += sizeof e->event;
1347
		file_priv->event_space += sizeof e->event;
1347
		spin_unlock_irqrestore(&dev->event_lock, flags);
1348
		spin_unlock_irqrestore(&dev->event_lock, flags);
1348
		goto out;
1349
		goto out;
1349
	}
1350
	}
1350
 
1351
 
1351
	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1352
	e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
1352
	e->event.base.length = sizeof e->event;
1353
	e->event.base.length = sizeof e->event;
1353
	e->event.user_data = user_data;
1354
	e->event.user_data = user_data;
1354
	e->base.event = &e->event.base;
1355
	e->base.event = &e->event.base;
1355
	e->base.file_priv = file_priv;
1356
	e->base.file_priv = file_priv;
1356
	e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1357
	e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1357
 
1358
 
1358
out:
1359
out:
1359
	return e;
1360
	return e;
1360
}
1361
}
1361
 
1362
 
1362
static void destroy_vblank_event(struct drm_device *dev,
1363
static void destroy_vblank_event(struct drm_device *dev,
1363
		struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1364
		struct drm_file *file_priv, struct drm_pending_vblank_event *e)
1364
{
1365
{
1365
	unsigned long flags;
1366
	unsigned long flags;
1366
 
1367
 
1367
	spin_lock_irqsave(&dev->event_lock, flags);
1368
	spin_lock_irqsave(&dev->event_lock, flags);
1368
	file_priv->event_space += sizeof e->event;
1369
	file_priv->event_space += sizeof e->event;
1369
	spin_unlock_irqrestore(&dev->event_lock, flags);
1370
	spin_unlock_irqrestore(&dev->event_lock, flags);
1370
	kfree(e);
1371
	kfree(e);
1371
}
1372
}
1372
 
1373
 
1373
static int atomic_set_prop(struct drm_atomic_state *state,
1374
static int atomic_set_prop(struct drm_atomic_state *state,
1374
		struct drm_mode_object *obj, struct drm_property *prop,
1375
		struct drm_mode_object *obj, struct drm_property *prop,
1375
		uint64_t prop_value)
1376
		uint64_t prop_value)
1376
{
1377
{
1377
	struct drm_mode_object *ref;
1378
	struct drm_mode_object *ref;
1378
	int ret;
1379
	int ret;
1379
 
1380
 
1380
	if (!drm_property_change_valid_get(prop, prop_value, &ref))
1381
	if (!drm_property_change_valid_get(prop, prop_value, &ref))
1381
		return -EINVAL;
1382
		return -EINVAL;
1382
 
1383
 
1383
	switch (obj->type) {
1384
	switch (obj->type) {
1384
	case DRM_MODE_OBJECT_CONNECTOR: {
1385
	case DRM_MODE_OBJECT_CONNECTOR: {
1385
		struct drm_connector *connector = obj_to_connector(obj);
1386
		struct drm_connector *connector = obj_to_connector(obj);
1386
		struct drm_connector_state *connector_state;
1387
		struct drm_connector_state *connector_state;
1387
 
1388
 
1388
		connector_state = drm_atomic_get_connector_state(state, connector);
1389
		connector_state = drm_atomic_get_connector_state(state, connector);
1389
		if (IS_ERR(connector_state)) {
1390
		if (IS_ERR(connector_state)) {
1390
			ret = PTR_ERR(connector_state);
1391
			ret = PTR_ERR(connector_state);
1391
			break;
1392
			break;
1392
		}
1393
		}
1393
 
1394
 
1394
		ret = drm_atomic_connector_set_property(connector,
1395
		ret = drm_atomic_connector_set_property(connector,
1395
				connector_state, prop, prop_value);
1396
				connector_state, prop, prop_value);
1396
		break;
1397
		break;
1397
	}
1398
	}
1398
	case DRM_MODE_OBJECT_CRTC: {
1399
	case DRM_MODE_OBJECT_CRTC: {
1399
		struct drm_crtc *crtc = obj_to_crtc(obj);
1400
		struct drm_crtc *crtc = obj_to_crtc(obj);
1400
		struct drm_crtc_state *crtc_state;
1401
		struct drm_crtc_state *crtc_state;
1401
 
1402
 
1402
		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1403
		crtc_state = drm_atomic_get_crtc_state(state, crtc);
1403
		if (IS_ERR(crtc_state)) {
1404
		if (IS_ERR(crtc_state)) {
1404
			ret = PTR_ERR(crtc_state);
1405
			ret = PTR_ERR(crtc_state);
1405
			break;
1406
			break;
1406
		}
1407
		}
1407
 
1408
 
1408
		ret = drm_atomic_crtc_set_property(crtc,
1409
		ret = drm_atomic_crtc_set_property(crtc,
1409
				crtc_state, prop, prop_value);
1410
				crtc_state, prop, prop_value);
1410
		break;
1411
		break;
1411
	}
1412
	}
1412
	case DRM_MODE_OBJECT_PLANE: {
1413
	case DRM_MODE_OBJECT_PLANE: {
1413
		struct drm_plane *plane = obj_to_plane(obj);
1414
		struct drm_plane *plane = obj_to_plane(obj);
1414
		struct drm_plane_state *plane_state;
1415
		struct drm_plane_state *plane_state;
1415
 
1416
 
1416
		plane_state = drm_atomic_get_plane_state(state, plane);
1417
		plane_state = drm_atomic_get_plane_state(state, plane);
1417
		if (IS_ERR(plane_state)) {
1418
		if (IS_ERR(plane_state)) {
1418
			ret = PTR_ERR(plane_state);
1419
			ret = PTR_ERR(plane_state);
1419
			break;
1420
			break;
1420
		}
1421
		}
1421
 
1422
 
1422
		ret = drm_atomic_plane_set_property(plane,
1423
		ret = drm_atomic_plane_set_property(plane,
1423
				plane_state, prop, prop_value);
1424
				plane_state, prop, prop_value);
1424
		break;
1425
		break;
1425
	}
1426
	}
1426
	default:
1427
	default:
1427
		ret = -EINVAL;
1428
		ret = -EINVAL;
1428
		break;
1429
		break;
1429
	}
1430
	}
1430
 
1431
 
1431
	drm_property_change_valid_put(prop, ref);
1432
	drm_property_change_valid_put(prop, ref);
1432
	return ret;
1433
	return ret;
1433
}
1434
}
1434
 
1435
 
1435
/**
1436
/**
1436
 * drm_atomic_update_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1437
 * drm_atomic_update_old_fb -- Unset old_fb pointers and set plane->fb pointers.
1437
 *
1438
 *
1438
 * @dev: drm device to check.
1439
 * @dev: drm device to check.
1439
 * @plane_mask: plane mask for planes that were updated.
1440
 * @plane_mask: plane mask for planes that were updated.
1440
 * @ret: return value, can be -EDEADLK for a retry.
1441
 * @ret: return value, can be -EDEADLK for a retry.
1441
 *
1442
 *
1442
 * Before doing an update plane->old_fb is set to plane->fb,
1443
 * Before doing an update plane->old_fb is set to plane->fb,
1443
 * but before dropping the locks old_fb needs to be set to NULL
1444
 * but before dropping the locks old_fb needs to be set to NULL
1444
 * and plane->fb updated. This is a common operation for each
1445
 * and plane->fb updated. This is a common operation for each
1445
 * atomic update, so this call is split off as a helper.
1446
 * atomic update, so this call is split off as a helper.
1446
 */
1447
 */
1447
void drm_atomic_clean_old_fb(struct drm_device *dev,
1448
void drm_atomic_clean_old_fb(struct drm_device *dev,
1448
			     unsigned plane_mask,
1449
			     unsigned plane_mask,
1449
			     int ret)
1450
			     int ret)
1450
{
1451
{
1451
	struct drm_plane *plane;
1452
	struct drm_plane *plane;
1452
 
1453
 
1453
	/* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1454
	/* if succeeded, fixup legacy plane crtc/fb ptrs before dropping
1454
	 * locks (ie. while it is still safe to deref plane->state).  We
1455
	 * locks (ie. while it is still safe to deref plane->state).  We
1455
	 * need to do this here because the driver entry points cannot
1456
	 * need to do this here because the driver entry points cannot
1456
	 * distinguish between legacy and atomic ioctls.
1457
	 * distinguish between legacy and atomic ioctls.
1457
	 */
1458
	 */
1458
	drm_for_each_plane_mask(plane, dev, plane_mask) {
1459
	drm_for_each_plane_mask(plane, dev, plane_mask) {
1459
		if (ret == 0) {
1460
		if (ret == 0) {
1460
			struct drm_framebuffer *new_fb = plane->state->fb;
1461
			struct drm_framebuffer *new_fb = plane->state->fb;
1461
			if (new_fb)
1462
			if (new_fb)
1462
				drm_framebuffer_reference(new_fb);
1463
				drm_framebuffer_reference(new_fb);
1463
			plane->fb = new_fb;
1464
			plane->fb = new_fb;
1464
			plane->crtc = plane->state->crtc;
1465
			plane->crtc = plane->state->crtc;
1465
 
1466
 
1466
			if (plane->old_fb)
1467
			if (plane->old_fb)
1467
				drm_framebuffer_unreference(plane->old_fb);
1468
				drm_framebuffer_unreference(plane->old_fb);
1468
		}
1469
		}
1469
		plane->old_fb = NULL;
1470
		plane->old_fb = NULL;
1470
	}
1471
	}
1471
}
1472
}
1472
EXPORT_SYMBOL(drm_atomic_clean_old_fb);
1473
EXPORT_SYMBOL(drm_atomic_clean_old_fb);