Subversion Repositories Kolibri OS

Rev

Rev 6937 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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