Subversion Repositories Kolibri OS

Rev

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

Rev 5271 Rev 6084
1
/*
1
/*
2
 * Copyright (C) 2014 Intel Corporation
2
 * Copyright (C) 2014 Intel Corporation
3
 *
3
 *
4
 * DRM universal plane helper functions
4
 * DRM universal plane helper functions
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice (including the next
13
 * The above copyright notice and this permission notice (including the next
14
 * paragraph) shall be included in all copies or substantial portions of the
14
 * paragraph) shall be included in all copies or substantial portions of the
15
 * Software.
15
 * Software.
16
 *
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
23
 * SOFTWARE.
24
 */
24
 */
25
 
25
 
26
#include 
26
#include 
27
#include 
27
#include 
28
#include 
28
#include 
29
#include 
29
#include 
30
#include 
30
#include 
31
#include 
31
#include 
32
#include 
32
#include 
33
 
33
 
34
#define SUBPIXEL_MASK 0xffff
34
#define SUBPIXEL_MASK 0xffff
35
 
35
 
36
/**
36
/**
37
 * DOC: overview
37
 * DOC: overview
38
 *
38
 *
39
 * This helper library has two parts. The first part has support to implement
39
 * This helper library has two parts. The first part has support to implement
40
 * primary plane support on top of the normal CRTC configuration interface.
40
 * primary plane support on top of the normal CRTC configuration interface.
41
 * Since the legacy ->set_config interface ties the primary plane together with
41
 * Since the legacy ->set_config interface ties the primary plane together with
42
 * the CRTC state this does not allow userspace to disable the primary plane
42
 * the CRTC state this does not allow userspace to disable the primary plane
43
 * itself.  To avoid too much duplicated code use
43
 * itself.  To avoid too much duplicated code use
44
 * drm_plane_helper_check_update() which can be used to enforce the same
44
 * drm_plane_helper_check_update() which can be used to enforce the same
45
 * restrictions as primary planes had thus. The default primary plane only
45
 * restrictions as primary planes had thus. The default primary plane only
46
 * expose XRBG8888 and ARGB8888 as valid pixel formats for the attached
46
 * expose XRBG8888 and ARGB8888 as valid pixel formats for the attached
47
 * framebuffer.
47
 * framebuffer.
48
 *
48
 *
49
 * Drivers are highly recommended to implement proper support for primary
49
 * Drivers are highly recommended to implement proper support for primary
50
 * planes, and newly merged drivers must not rely upon these transitional
50
 * planes, and newly merged drivers must not rely upon these transitional
51
 * helpers.
51
 * helpers.
52
 *
52
 *
53
 * The second part also implements transitional helpers which allow drivers to
53
 * The second part also implements transitional helpers which allow drivers to
54
 * gradually switch to the atomic helper infrastructure for plane updates. Once
54
 * gradually switch to the atomic helper infrastructure for plane updates. Once
55
 * that switch is complete drivers shouldn't use these any longer, instead using
55
 * that switch is complete drivers shouldn't use these any longer, instead using
56
 * the proper legacy implementations for update and disable plane hooks provided
56
 * the proper legacy implementations for update and disable plane hooks provided
57
 * by the atomic helpers.
57
 * by the atomic helpers.
58
 *
58
 *
59
 * Again drivers are strongly urged to switch to the new interfaces.
59
 * Again drivers are strongly urged to switch to the new interfaces.
60
 */
60
 */
61
 
61
 
62
/*
62
/*
63
 * This is the minimal list of formats that seem to be safe for modeset use
63
 * This is the minimal list of formats that seem to be safe for modeset use
64
 * with all current DRM drivers.  Most hardware can actually support more
64
 * with all current DRM drivers.  Most hardware can actually support more
65
 * formats than this and drivers may specify a more accurate list when
65
 * formats than this and drivers may specify a more accurate list when
66
 * creating the primary plane.  However drivers that still call
66
 * creating the primary plane.  However drivers that still call
67
 * drm_plane_init() will use this minimal format list as the default.
67
 * drm_plane_init() will use this minimal format list as the default.
68
 */
68
 */
69
static const uint32_t safe_modeset_formats[] = {
69
static const uint32_t safe_modeset_formats[] = {
70
	DRM_FORMAT_XRGB8888,
70
	DRM_FORMAT_XRGB8888,
71
	DRM_FORMAT_ARGB8888,
71
	DRM_FORMAT_ARGB8888,
72
};
72
};
73
 
73
 
74
/*
74
/*
75
 * Returns the connectors currently associated with a CRTC.  This function
75
 * Returns the connectors currently associated with a CRTC.  This function
76
 * should be called twice:  once with a NULL connector list to retrieve
76
 * should be called twice:  once with a NULL connector list to retrieve
77
 * the list size, and once with the properly allocated list to be filled in.
77
 * the list size, and once with the properly allocated list to be filled in.
78
 */
78
 */
79
static int get_connectors_for_crtc(struct drm_crtc *crtc,
79
static int get_connectors_for_crtc(struct drm_crtc *crtc,
80
				   struct drm_connector **connector_list,
80
				   struct drm_connector **connector_list,
81
				   int num_connectors)
81
				   int num_connectors)
82
{
82
{
83
	struct drm_device *dev = crtc->dev;
83
	struct drm_device *dev = crtc->dev;
84
	struct drm_connector *connector;
84
	struct drm_connector *connector;
85
	int count = 0;
85
	int count = 0;
86
 
86
 
87
	/*
87
	/*
88
	 * Note: Once we change the plane hooks to more fine-grained locking we
88
	 * Note: Once we change the plane hooks to more fine-grained locking we
89
	 * need to grab the connection_mutex here to be able to make these
89
	 * need to grab the connection_mutex here to be able to make these
90
	 * checks.
90
	 * checks.
91
	 */
91
	 */
92
	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
92
	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
93
 
93
 
94
	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
94
	drm_for_each_connector(connector, dev) {
95
		if (connector->encoder && connector->encoder->crtc == crtc) {
95
		if (connector->encoder && connector->encoder->crtc == crtc) {
96
			if (connector_list != NULL && count < num_connectors)
96
			if (connector_list != NULL && count < num_connectors)
97
				*(connector_list++) = connector;
97
				*(connector_list++) = connector;
98
 
98
 
99
			count++;
99
			count++;
100
		}
100
		}
-
 
101
	}
101
 
102
 
102
	return count;
103
	return count;
103
}
104
}
104
 
105
 
105
/**
106
/**
106
 * drm_plane_helper_check_update() - Check plane update for validity
107
 * drm_plane_helper_check_update() - Check plane update for validity
107
 * @plane: plane object to update
108
 * @plane: plane object to update
108
 * @crtc: owning CRTC of owning plane
109
 * @crtc: owning CRTC of owning plane
109
 * @fb: framebuffer to flip onto plane
110
 * @fb: framebuffer to flip onto plane
110
 * @src: source coordinates in 16.16 fixed point
111
 * @src: source coordinates in 16.16 fixed point
111
 * @dest: integer destination coordinates
112
 * @dest: integer destination coordinates
112
 * @clip: integer clipping coordinates
113
 * @clip: integer clipping coordinates
113
 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
114
 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
114
 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
115
 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
115
 * @can_position: is it legal to position the plane such that it
116
 * @can_position: is it legal to position the plane such that it
116
 *                doesn't cover the entire crtc?  This will generally
117
 *                doesn't cover the entire crtc?  This will generally
117
 *                only be false for primary planes.
118
 *                only be false for primary planes.
118
 * @can_update_disabled: can the plane be updated while the crtc
119
 * @can_update_disabled: can the plane be updated while the crtc
119
 *                       is disabled?
120
 *                       is disabled?
120
 * @visible: output parameter indicating whether plane is still visible after
121
 * @visible: output parameter indicating whether plane is still visible after
121
 *           clipping
122
 *           clipping
122
 *
123
 *
123
 * Checks that a desired plane update is valid.  Drivers that provide
124
 * Checks that a desired plane update is valid.  Drivers that provide
124
 * their own plane handling rather than helper-provided implementations may
125
 * their own plane handling rather than helper-provided implementations may
125
 * still wish to call this function to avoid duplication of error checking
126
 * still wish to call this function to avoid duplication of error checking
126
 * code.
127
 * code.
127
 *
128
 *
128
 * RETURNS:
129
 * RETURNS:
129
 * Zero if update appears valid, error code on failure
130
 * Zero if update appears valid, error code on failure
130
 */
131
 */
131
int drm_plane_helper_check_update(struct drm_plane *plane,
132
int drm_plane_helper_check_update(struct drm_plane *plane,
132
				    struct drm_crtc *crtc,
133
				    struct drm_crtc *crtc,
133
				    struct drm_framebuffer *fb,
134
				    struct drm_framebuffer *fb,
134
				    struct drm_rect *src,
135
				    struct drm_rect *src,
135
				    struct drm_rect *dest,
136
				    struct drm_rect *dest,
136
				    const struct drm_rect *clip,
137
				    const struct drm_rect *clip,
137
				    int min_scale,
138
				    int min_scale,
138
				    int max_scale,
139
				    int max_scale,
139
				    bool can_position,
140
				    bool can_position,
140
				    bool can_update_disabled,
141
				    bool can_update_disabled,
141
				    bool *visible)
142
				    bool *visible)
142
{
143
{
143
	int hscale, vscale;
144
	int hscale, vscale;
-
 
145
 
-
 
146
	if (!fb) {
-
 
147
		*visible = false;
-
 
148
		return 0;
-
 
149
	}
-
 
150
 
-
 
151
	/* crtc should only be NULL when disabling (i.e., !fb) */
-
 
152
	if (WARN_ON(!crtc)) {
-
 
153
		*visible = false;
-
 
154
		return 0;
-
 
155
	}
144
 
156
 
145
	if (!crtc->enabled && !can_update_disabled) {
157
	if (!crtc->enabled && !can_update_disabled) {
146
		DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
158
		DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
147
		return -EINVAL;
159
		return -EINVAL;
148
	}
160
	}
149
 
161
 
150
	/* Check scaling */
162
	/* Check scaling */
151
	hscale = drm_rect_calc_hscale(src, dest, min_scale, max_scale);
163
	hscale = drm_rect_calc_hscale(src, dest, min_scale, max_scale);
152
	vscale = drm_rect_calc_vscale(src, dest, min_scale, max_scale);
164
	vscale = drm_rect_calc_vscale(src, dest, min_scale, max_scale);
153
	if (hscale < 0 || vscale < 0) {
165
	if (hscale < 0 || vscale < 0) {
154
		DRM_DEBUG_KMS("Invalid scaling of plane\n");
166
		DRM_DEBUG_KMS("Invalid scaling of plane\n");
155
		return -ERANGE;
167
		return -ERANGE;
156
	}
168
	}
157
 
-
 
158
	if (!fb) {
-
 
159
		*visible = false;
-
 
160
		return 0;
-
 
161
	}
-
 
162
 
169
 
163
	*visible = drm_rect_clip_scaled(src, dest, clip, hscale, vscale);
170
	*visible = drm_rect_clip_scaled(src, dest, clip, hscale, vscale);
164
	if (!*visible)
171
	if (!*visible)
165
		/*
172
		/*
166
		 * Plane isn't visible; some drivers can handle this
173
		 * Plane isn't visible; some drivers can handle this
167
		 * so we just return success here.  Drivers that can't
174
		 * so we just return success here.  Drivers that can't
168
		 * (including those that use the primary plane helper's
175
		 * (including those that use the primary plane helper's
169
		 * update function) will return an error from their
176
		 * update function) will return an error from their
170
		 * update_plane handler.
177
		 * update_plane handler.
171
		 */
178
		 */
172
		return 0;
179
		return 0;
173
 
180
 
174
	if (!can_position && !drm_rect_equals(dest, clip)) {
181
	if (!can_position && !drm_rect_equals(dest, clip)) {
175
		DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
182
		DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
176
		return -EINVAL;
183
		return -EINVAL;
177
	}
184
	}
178
 
185
 
179
	return 0;
186
	return 0;
180
}
187
}
181
EXPORT_SYMBOL(drm_plane_helper_check_update);
188
EXPORT_SYMBOL(drm_plane_helper_check_update);
182
 
189
 
183
/**
190
/**
184
 * drm_primary_helper_update() - Helper for primary plane update
191
 * drm_primary_helper_update() - Helper for primary plane update
185
 * @plane: plane object to update
192
 * @plane: plane object to update
186
 * @crtc: owning CRTC of owning plane
193
 * @crtc: owning CRTC of owning plane
187
 * @fb: framebuffer to flip onto plane
194
 * @fb: framebuffer to flip onto plane
188
 * @crtc_x: x offset of primary plane on crtc
195
 * @crtc_x: x offset of primary plane on crtc
189
 * @crtc_y: y offset of primary plane on crtc
196
 * @crtc_y: y offset of primary plane on crtc
190
 * @crtc_w: width of primary plane rectangle on crtc
197
 * @crtc_w: width of primary plane rectangle on crtc
191
 * @crtc_h: height of primary plane rectangle on crtc
198
 * @crtc_h: height of primary plane rectangle on crtc
192
 * @src_x: x offset of @fb for panning
199
 * @src_x: x offset of @fb for panning
193
 * @src_y: y offset of @fb for panning
200
 * @src_y: y offset of @fb for panning
194
 * @src_w: width of source rectangle in @fb
201
 * @src_w: width of source rectangle in @fb
195
 * @src_h: height of source rectangle in @fb
202
 * @src_h: height of source rectangle in @fb
196
 *
203
 *
197
 * Provides a default plane update handler for primary planes.  This is handler
204
 * Provides a default plane update handler for primary planes.  This is handler
198
 * is called in response to a userspace SetPlane operation on the plane with a
205
 * is called in response to a userspace SetPlane operation on the plane with a
199
 * non-NULL framebuffer.  We call the driver's modeset handler to update the
206
 * non-NULL framebuffer.  We call the driver's modeset handler to update the
200
 * framebuffer.
207
 * framebuffer.
201
 *
208
 *
202
 * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
209
 * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
203
 * return an error.
210
 * return an error.
204
 *
211
 *
205
 * Note that we make some assumptions about hardware limitations that may not be
212
 * Note that we make some assumptions about hardware limitations that may not be
206
 * true for all hardware --
213
 * true for all hardware --
207
 *   1) Primary plane cannot be repositioned.
214
 *   1) Primary plane cannot be repositioned.
208
 *   2) Primary plane cannot be scaled.
215
 *   2) Primary plane cannot be scaled.
209
 *   3) Primary plane must cover the entire CRTC.
216
 *   3) Primary plane must cover the entire CRTC.
210
 *   4) Subpixel positioning is not supported.
217
 *   4) Subpixel positioning is not supported.
211
 * Drivers for hardware that don't have these restrictions can provide their
218
 * Drivers for hardware that don't have these restrictions can provide their
212
 * own implementation rather than using this helper.
219
 * own implementation rather than using this helper.
213
 *
220
 *
214
 * RETURNS:
221
 * RETURNS:
215
 * Zero on success, error code on failure
222
 * Zero on success, error code on failure
216
 */
223
 */
217
int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
224
int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
218
			      struct drm_framebuffer *fb,
225
			      struct drm_framebuffer *fb,
219
			      int crtc_x, int crtc_y,
226
			      int crtc_x, int crtc_y,
220
			      unsigned int crtc_w, unsigned int crtc_h,
227
			      unsigned int crtc_w, unsigned int crtc_h,
221
			      uint32_t src_x, uint32_t src_y,
228
			      uint32_t src_x, uint32_t src_y,
222
			      uint32_t src_w, uint32_t src_h)
229
			      uint32_t src_w, uint32_t src_h)
223
{
230
{
224
	struct drm_mode_set set = {
231
	struct drm_mode_set set = {
225
		.crtc = crtc,
232
		.crtc = crtc,
226
		.fb = fb,
233
		.fb = fb,
227
		.mode = &crtc->mode,
234
		.mode = &crtc->mode,
228
		.x = src_x >> 16,
235
		.x = src_x >> 16,
229
		.y = src_y >> 16,
236
		.y = src_y >> 16,
230
	};
237
	};
231
	struct drm_rect src = {
238
	struct drm_rect src = {
232
		.x1 = src_x,
239
		.x1 = src_x,
233
		.y1 = src_y,
240
		.y1 = src_y,
234
		.x2 = src_x + src_w,
241
		.x2 = src_x + src_w,
235
		.y2 = src_y + src_h,
242
		.y2 = src_y + src_h,
236
	};
243
	};
237
	struct drm_rect dest = {
244
	struct drm_rect dest = {
238
		.x1 = crtc_x,
245
		.x1 = crtc_x,
239
		.y1 = crtc_y,
246
		.y1 = crtc_y,
240
		.x2 = crtc_x + crtc_w,
247
		.x2 = crtc_x + crtc_w,
241
		.y2 = crtc_y + crtc_h,
248
		.y2 = crtc_y + crtc_h,
242
	};
249
	};
243
	const struct drm_rect clip = {
250
	const struct drm_rect clip = {
244
		.x2 = crtc->mode.hdisplay,
251
		.x2 = crtc->mode.hdisplay,
245
		.y2 = crtc->mode.vdisplay,
252
		.y2 = crtc->mode.vdisplay,
246
	};
253
	};
247
	struct drm_connector **connector_list;
254
	struct drm_connector **connector_list;
248
	int num_connectors, ret;
255
	int num_connectors, ret;
249
	bool visible;
256
	bool visible;
250
 
257
 
251
	ret = drm_plane_helper_check_update(plane, crtc, fb,
258
	ret = drm_plane_helper_check_update(plane, crtc, fb,
252
					    &src, &dest, &clip,
259
					    &src, &dest, &clip,
253
					    DRM_PLANE_HELPER_NO_SCALING,
260
					    DRM_PLANE_HELPER_NO_SCALING,
254
					    DRM_PLANE_HELPER_NO_SCALING,
261
					    DRM_PLANE_HELPER_NO_SCALING,
255
					    false, false, &visible);
262
					    false, false, &visible);
256
	if (ret)
263
	if (ret)
257
		return ret;
264
		return ret;
258
 
265
 
259
	if (!visible)
266
	if (!visible)
260
		/*
267
		/*
261
		 * Primary plane isn't visible.  Note that unless a driver
268
		 * Primary plane isn't visible.  Note that unless a driver
262
		 * provides their own disable function, this will just
269
		 * provides their own disable function, this will just
263
		 * wind up returning -EINVAL to userspace.
270
		 * wind up returning -EINVAL to userspace.
264
		 */
271
		 */
265
		return plane->funcs->disable_plane(plane);
272
		return plane->funcs->disable_plane(plane);
266
 
273
 
267
	/* Find current connectors for CRTC */
274
	/* Find current connectors for CRTC */
268
	num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
275
	num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
269
	BUG_ON(num_connectors == 0);
276
	BUG_ON(num_connectors == 0);
270
	connector_list = kzalloc(num_connectors * sizeof(*connector_list),
277
	connector_list = kzalloc(num_connectors * sizeof(*connector_list),
271
				 GFP_KERNEL);
278
				 GFP_KERNEL);
272
	if (!connector_list)
279
	if (!connector_list)
273
		return -ENOMEM;
280
		return -ENOMEM;
274
	get_connectors_for_crtc(crtc, connector_list, num_connectors);
281
	get_connectors_for_crtc(crtc, connector_list, num_connectors);
275
 
282
 
276
	set.connectors = connector_list;
283
	set.connectors = connector_list;
277
	set.num_connectors = num_connectors;
284
	set.num_connectors = num_connectors;
278
 
285
 
279
	/*
286
	/*
280
	 * We call set_config() directly here rather than using
287
	 * We call set_config() directly here rather than using
281
	 * drm_mode_set_config_internal.  We're reprogramming the same
288
	 * drm_mode_set_config_internal.  We're reprogramming the same
282
	 * connectors that were already in use, so we shouldn't need the extra
289
	 * connectors that were already in use, so we shouldn't need the extra
283
	 * cross-CRTC fb refcounting to accomodate stealing connectors.
290
	 * cross-CRTC fb refcounting to accomodate stealing connectors.
284
	 * drm_mode_setplane() already handles the basic refcounting for the
291
	 * drm_mode_setplane() already handles the basic refcounting for the
285
	 * framebuffers involved in this operation.
292
	 * framebuffers involved in this operation.
286
	 */
293
	 */
287
	ret = crtc->funcs->set_config(&set);
294
	ret = crtc->funcs->set_config(&set);
288
 
295
 
289
	kfree(connector_list);
296
	kfree(connector_list);
290
	return ret;
297
	return ret;
291
}
298
}
292
EXPORT_SYMBOL(drm_primary_helper_update);
299
EXPORT_SYMBOL(drm_primary_helper_update);
293
 
300
 
294
/**
301
/**
295
 * drm_primary_helper_disable() - Helper for primary plane disable
302
 * drm_primary_helper_disable() - Helper for primary plane disable
296
 * @plane: plane to disable
303
 * @plane: plane to disable
297
 *
304
 *
298
 * Provides a default plane disable handler for primary planes.  This is handler
305
 * Provides a default plane disable handler for primary planes.  This is handler
299
 * is called in response to a userspace SetPlane operation on the plane with a
306
 * is called in response to a userspace SetPlane operation on the plane with a
300
 * NULL framebuffer parameter.  It unconditionally fails the disable call with
307
 * NULL framebuffer parameter.  It unconditionally fails the disable call with
301
 * -EINVAL the only way to disable the primary plane without driver support is
308
 * -EINVAL the only way to disable the primary plane without driver support is
302
 * to disable the entier CRTC. Which does not match the plane ->disable hook.
309
 * to disable the entier CRTC. Which does not match the plane ->disable hook.
303
 *
310
 *
304
 * Note that some hardware may be able to disable the primary plane without
311
 * Note that some hardware may be able to disable the primary plane without
305
 * disabling the whole CRTC.  Drivers for such hardware should provide their
312
 * disabling the whole CRTC.  Drivers for such hardware should provide their
306
 * own disable handler that disables just the primary plane (and they'll likely
313
 * own disable handler that disables just the primary plane (and they'll likely
307
 * need to provide their own update handler as well to properly re-enable a
314
 * need to provide their own update handler as well to properly re-enable a
308
 * disabled primary plane).
315
 * disabled primary plane).
309
 *
316
 *
310
 * RETURNS:
317
 * RETURNS:
311
 * Unconditionally returns -EINVAL.
318
 * Unconditionally returns -EINVAL.
312
 */
319
 */
313
int drm_primary_helper_disable(struct drm_plane *plane)
320
int drm_primary_helper_disable(struct drm_plane *plane)
314
{
321
{
315
	return -EINVAL;
322
	return -EINVAL;
316
}
323
}
317
EXPORT_SYMBOL(drm_primary_helper_disable);
324
EXPORT_SYMBOL(drm_primary_helper_disable);
318
 
325
 
319
/**
326
/**
320
 * drm_primary_helper_destroy() - Helper for primary plane destruction
327
 * drm_primary_helper_destroy() - Helper for primary plane destruction
321
 * @plane: plane to destroy
328
 * @plane: plane to destroy
322
 *
329
 *
323
 * Provides a default plane destroy handler for primary planes.  This handler
330
 * Provides a default plane destroy handler for primary planes.  This handler
324
 * is called during CRTC destruction.  We disable the primary plane, remove
331
 * is called during CRTC destruction.  We disable the primary plane, remove
325
 * it from the DRM plane list, and deallocate the plane structure.
332
 * it from the DRM plane list, and deallocate the plane structure.
326
 */
333
 */
327
void drm_primary_helper_destroy(struct drm_plane *plane)
334
void drm_primary_helper_destroy(struct drm_plane *plane)
328
{
335
{
329
	drm_plane_cleanup(plane);
336
	drm_plane_cleanup(plane);
330
	kfree(plane);
337
	kfree(plane);
331
}
338
}
332
EXPORT_SYMBOL(drm_primary_helper_destroy);
339
EXPORT_SYMBOL(drm_primary_helper_destroy);
333
 
340
 
334
const struct drm_plane_funcs drm_primary_helper_funcs = {
341
const struct drm_plane_funcs drm_primary_helper_funcs = {
335
	.update_plane = drm_primary_helper_update,
342
	.update_plane = drm_primary_helper_update,
336
	.disable_plane = drm_primary_helper_disable,
343
	.disable_plane = drm_primary_helper_disable,
337
	.destroy = drm_primary_helper_destroy,
344
	.destroy = drm_primary_helper_destroy,
338
};
345
};
339
EXPORT_SYMBOL(drm_primary_helper_funcs);
346
EXPORT_SYMBOL(drm_primary_helper_funcs);
340
 
-
 
341
/**
-
 
342
 * drm_primary_helper_create_plane() - Create a generic primary plane
-
 
343
 * @dev: drm device
-
 
344
 * @formats: pixel formats supported, or NULL for a default safe list
-
 
345
 * @num_formats: size of @formats; ignored if @formats is NULL
-
 
346
 *
-
 
347
 * Allocates and initializes a primary plane that can be used with the primary
-
 
348
 * plane helpers.  Drivers that wish to use driver-specific plane structures or
-
 
349
 * provide custom handler functions may perform their own allocation and
-
 
350
 * initialization rather than calling this function.
-
 
351
 */
347
 
352
struct drm_plane *drm_primary_helper_create_plane(struct drm_device *dev,
-
 
353
						  const uint32_t *formats,
-
 
354
						  int num_formats)
348
static struct drm_plane *create_primary_plane(struct drm_device *dev)
355
{
349
{
356
	struct drm_plane *primary;
350
	struct drm_plane *primary;
357
	int ret;
351
	int ret;
358
 
352
 
359
	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
353
	primary = kzalloc(sizeof(*primary), GFP_KERNEL);
360
	if (primary == NULL) {
354
	if (primary == NULL) {
361
		DRM_DEBUG_KMS("Failed to allocate primary plane\n");
355
		DRM_DEBUG_KMS("Failed to allocate primary plane\n");
362
		return NULL;
356
		return NULL;
363
	}
357
	}
364
 
358
 
365
	if (formats == NULL) {
359
	/*
366
		formats = safe_modeset_formats;
360
	 * Remove the format_default field from drm_plane when dropping
367
		num_formats = ARRAY_SIZE(safe_modeset_formats);
361
	 * this helper.
-
 
362
	 */
368
	}
363
	primary->format_default = true;
369
 
364
 
370
	/* possible_crtc's will be filled in later by crtc_init */
365
	/* possible_crtc's will be filled in later by crtc_init */
371
	ret = drm_universal_plane_init(dev, primary, 0,
366
	ret = drm_universal_plane_init(dev, primary, 0,
372
				       &drm_primary_helper_funcs,
367
				       &drm_primary_helper_funcs,
373
			     formats, num_formats,
368
				       safe_modeset_formats,
-
 
369
				       ARRAY_SIZE(safe_modeset_formats),
374
			     DRM_PLANE_TYPE_PRIMARY);
370
				       DRM_PLANE_TYPE_PRIMARY);
375
	if (ret) {
371
	if (ret) {
376
		kfree(primary);
372
		kfree(primary);
377
		primary = NULL;
373
		primary = NULL;
378
	}
374
	}
379
 
375
 
380
	return primary;
376
	return primary;
381
}
377
}
382
EXPORT_SYMBOL(drm_primary_helper_create_plane);
-
 
383
 
378
 
384
/**
379
/**
385
 * drm_crtc_init - Legacy CRTC initialization function
380
 * drm_crtc_init - Legacy CRTC initialization function
386
 * @dev: DRM device
381
 * @dev: DRM device
387
 * @crtc: CRTC object to init
382
 * @crtc: CRTC object to init
388
 * @funcs: callbacks for the new CRTC
383
 * @funcs: callbacks for the new CRTC
389
 *
384
 *
390
 * Initialize a CRTC object with a default helper-provided primary plane and no
385
 * Initialize a CRTC object with a default helper-provided primary plane and no
391
 * cursor plane.
386
 * cursor plane.
392
 *
387
 *
393
 * Returns:
388
 * Returns:
394
 * Zero on success, error code on failure.
389
 * Zero on success, error code on failure.
395
 */
390
 */
396
int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
391
int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
397
		  const struct drm_crtc_funcs *funcs)
392
		  const struct drm_crtc_funcs *funcs)
398
{
393
{
399
	struct drm_plane *primary;
394
	struct drm_plane *primary;
400
 
395
 
401
	primary = drm_primary_helper_create_plane(dev, NULL, 0);
396
	primary = create_primary_plane(dev);
402
	return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs);
397
	return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs);
403
}
398
}
404
EXPORT_SYMBOL(drm_crtc_init);
399
EXPORT_SYMBOL(drm_crtc_init);
405
 
400
 
406
int drm_plane_helper_commit(struct drm_plane *plane,
401
int drm_plane_helper_commit(struct drm_plane *plane,
407
			    struct drm_plane_state *plane_state,
402
			    struct drm_plane_state *plane_state,
408
			    struct drm_framebuffer *old_fb)
403
			    struct drm_framebuffer *old_fb)
409
{
404
{
410
	struct drm_plane_helper_funcs *plane_funcs;
405
	const struct drm_plane_helper_funcs *plane_funcs;
411
	struct drm_crtc *crtc[2];
406
	struct drm_crtc *crtc[2];
412
	struct drm_crtc_helper_funcs *crtc_funcs[2];
407
	const struct drm_crtc_helper_funcs *crtc_funcs[2];
413
	int i, ret = 0;
408
	int i, ret = 0;
414
 
409
 
415
	plane_funcs = plane->helper_private;
410
	plane_funcs = plane->helper_private;
416
 
411
 
417
	/* Since this is a transitional helper we can't assume that plane->state
412
	/* Since this is a transitional helper we can't assume that plane->state
418
	 * is always valid. Hence we need to use plane->crtc instead of
413
	 * is always valid. Hence we need to use plane->crtc instead of
419
	 * plane->state->crtc as the old crtc. */
414
	 * plane->state->crtc as the old crtc. */
420
	crtc[0] = plane->crtc;
415
	crtc[0] = plane->crtc;
421
	crtc[1] = crtc[0] != plane_state->crtc ? plane_state->crtc : NULL;
416
	crtc[1] = crtc[0] != plane_state->crtc ? plane_state->crtc : NULL;
422
 
417
 
423
	for (i = 0; i < 2; i++)
418
	for (i = 0; i < 2; i++)
424
		crtc_funcs[i] = crtc[i] ? crtc[i]->helper_private : NULL;
419
		crtc_funcs[i] = crtc[i] ? crtc[i]->helper_private : NULL;
425
 
420
 
426
	if (plane_funcs->atomic_check) {
421
	if (plane_funcs->atomic_check) {
427
		ret = plane_funcs->atomic_check(plane, plane_state);
422
		ret = plane_funcs->atomic_check(plane, plane_state);
428
		if (ret)
423
		if (ret)
429
			goto out;
424
			goto out;
430
	}
425
	}
431
 
426
 
-
 
427
	if (plane_funcs->prepare_fb && plane_state->fb &&
432
	if (plane_funcs->prepare_fb && plane_state->fb) {
428
	    plane_state->fb != old_fb) {
-
 
429
		ret = plane_funcs->prepare_fb(plane,
433
		ret = plane_funcs->prepare_fb(plane, plane_state->fb);
430
					      plane_state);
434
		if (ret)
431
		if (ret)
435
			goto out;
432
			goto out;
436
	}
433
	}
437
 
434
 
438
	/* Point of no return, commit sw state. */
435
	/* Point of no return, commit sw state. */
439
	swap(plane->state, plane_state);
436
	swap(plane->state, plane_state);
440
 
437
 
441
	for (i = 0; i < 2; i++) {
438
	for (i = 0; i < 2; i++) {
442
		if (crtc_funcs[i] && crtc_funcs[i]->atomic_begin)
439
		if (crtc_funcs[i] && crtc_funcs[i]->atomic_begin)
443
			crtc_funcs[i]->atomic_begin(crtc[i]);
440
			crtc_funcs[i]->atomic_begin(crtc[i], crtc[i]->state);
444
	}
441
	}
-
 
442
 
-
 
443
	/*
-
 
444
	 * Drivers may optionally implement the ->atomic_disable callback, so
-
 
445
	 * special-case that here.
-
 
446
	 */
-
 
447
	if (drm_atomic_plane_disabling(plane, plane_state) &&
-
 
448
	    plane_funcs->atomic_disable)
-
 
449
		plane_funcs->atomic_disable(plane, plane_state);
445
 
450
	else
446
	plane_funcs->atomic_update(plane, plane_state);
451
		plane_funcs->atomic_update(plane, plane_state);
447
 
452
 
448
	for (i = 0; i < 2; i++) {
453
	for (i = 0; i < 2; i++) {
449
		if (crtc_funcs[i] && crtc_funcs[i]->atomic_flush)
454
		if (crtc_funcs[i] && crtc_funcs[i]->atomic_flush)
450
			crtc_funcs[i]->atomic_flush(crtc[i]);
455
			crtc_funcs[i]->atomic_flush(crtc[i], crtc[i]->state);
451
	}
456
	}
-
 
457
 
-
 
458
	/*
-
 
459
	 * If we only moved the plane and didn't change fb's, there's no need to
-
 
460
	 * wait for vblank.
-
 
461
	 */
-
 
462
	if (plane->state->fb == old_fb)
-
 
463
		goto out;
452
 
464
 
453
	for (i = 0; i < 2; i++) {
465
	for (i = 0; i < 2; i++) {
454
		if (!crtc[i])
466
		if (!crtc[i])
455
			continue;
467
			continue;
-
 
468
 
-
 
469
		if (crtc[i]->cursor == plane)
-
 
470
			continue;
456
 
471
 
457
		/* There's no other way to figure out whether the crtc is running. */
472
		/* There's no other way to figure out whether the crtc is running. */
458
		ret = drm_crtc_vblank_get(crtc[i]);
473
		ret = drm_crtc_vblank_get(crtc[i]);
459
		if (ret == 0) {
474
		if (ret == 0) {
460
			drm_crtc_wait_one_vblank(crtc[i]);
475
			drm_crtc_wait_one_vblank(crtc[i]);
461
			drm_crtc_vblank_put(crtc[i]);
476
			drm_crtc_vblank_put(crtc[i]);
462
		}
477
		}
463
 
478
 
464
		ret = 0;
479
		ret = 0;
465
	}
480
	}
466
 
481
 
467
	if (plane_funcs->cleanup_fb && old_fb)
482
	if (plane_funcs->cleanup_fb)
468
		plane_funcs->cleanup_fb(plane, old_fb);
483
		plane_funcs->cleanup_fb(plane, plane_state);
469
out:
484
out:
470
	if (plane_state) {
485
	if (plane_state) {
471
		if (plane->funcs->atomic_destroy_state)
486
		if (plane->funcs->atomic_destroy_state)
472
			plane->funcs->atomic_destroy_state(plane, plane_state);
487
			plane->funcs->atomic_destroy_state(plane, plane_state);
473
		else
488
		else
474
			drm_atomic_helper_plane_destroy_state(plane, plane_state);
489
			drm_atomic_helper_plane_destroy_state(plane, plane_state);
475
	}
490
	}
476
 
491
 
477
	return ret;
492
	return ret;
478
}
493
}
479
 
494
 
480
/**
495
/**
481
 * drm_plane_helper_update() - Helper for primary plane update
496
 * drm_plane_helper_update() - Transitional helper for plane update
482
 * @plane: plane object to update
497
 * @plane: plane object to update
483
 * @crtc: owning CRTC of owning plane
498
 * @crtc: owning CRTC of owning plane
484
 * @fb: framebuffer to flip onto plane
499
 * @fb: framebuffer to flip onto plane
485
 * @crtc_x: x offset of primary plane on crtc
500
 * @crtc_x: x offset of primary plane on crtc
486
 * @crtc_y: y offset of primary plane on crtc
501
 * @crtc_y: y offset of primary plane on crtc
487
 * @crtc_w: width of primary plane rectangle on crtc
502
 * @crtc_w: width of primary plane rectangle on crtc
488
 * @crtc_h: height of primary plane rectangle on crtc
503
 * @crtc_h: height of primary plane rectangle on crtc
489
 * @src_x: x offset of @fb for panning
504
 * @src_x: x offset of @fb for panning
490
 * @src_y: y offset of @fb for panning
505
 * @src_y: y offset of @fb for panning
491
 * @src_w: width of source rectangle in @fb
506
 * @src_w: width of source rectangle in @fb
492
 * @src_h: height of source rectangle in @fb
507
 * @src_h: height of source rectangle in @fb
493
 *
508
 *
494
 * Provides a default plane update handler using the atomic plane update
509
 * Provides a default plane update handler using the atomic plane update
495
 * functions. It is fully left to the driver to check plane constraints and
510
 * functions. It is fully left to the driver to check plane constraints and
496
 * handle corner-cases like a fully occluded or otherwise invisible plane.
511
 * handle corner-cases like a fully occluded or otherwise invisible plane.
497
 *
512
 *
498
 * This is useful for piecewise transitioning of a driver to the atomic helpers.
513
 * This is useful for piecewise transitioning of a driver to the atomic helpers.
499
 *
514
 *
500
 * RETURNS:
515
 * RETURNS:
501
 * Zero on success, error code on failure
516
 * Zero on success, error code on failure
502
 */
517
 */
503
int drm_plane_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
518
int drm_plane_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
504
			    struct drm_framebuffer *fb,
519
			    struct drm_framebuffer *fb,
505
			    int crtc_x, int crtc_y,
520
			    int crtc_x, int crtc_y,
506
			    unsigned int crtc_w, unsigned int crtc_h,
521
			    unsigned int crtc_w, unsigned int crtc_h,
507
			    uint32_t src_x, uint32_t src_y,
522
			    uint32_t src_x, uint32_t src_y,
508
			    uint32_t src_w, uint32_t src_h)
523
			    uint32_t src_w, uint32_t src_h)
509
{
524
{
510
	struct drm_plane_state *plane_state;
525
	struct drm_plane_state *plane_state;
511
 
526
 
512
	if (plane->funcs->atomic_duplicate_state)
527
	if (plane->funcs->atomic_duplicate_state)
513
		plane_state = plane->funcs->atomic_duplicate_state(plane);
528
		plane_state = plane->funcs->atomic_duplicate_state(plane);
-
 
529
	else {
514
	else if (plane->state)
530
		if (!plane->state)
-
 
531
			drm_atomic_helper_plane_reset(plane);
-
 
532
 
515
		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
533
		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
516
	else
534
	}
517
		plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
-
 
518
	if (!plane_state)
535
	if (!plane_state)
519
		return -ENOMEM;
536
		return -ENOMEM;
-
 
537
	plane_state->plane = plane;
520
 
538
 
521
	plane_state->crtc = crtc;
539
	plane_state->crtc = crtc;
522
	drm_atomic_set_fb_for_plane(plane_state, fb);
540
	drm_atomic_set_fb_for_plane(plane_state, fb);
523
	plane_state->crtc_x = crtc_x;
541
	plane_state->crtc_x = crtc_x;
524
	plane_state->crtc_y = crtc_y;
542
	plane_state->crtc_y = crtc_y;
525
	plane_state->crtc_h = crtc_h;
543
	plane_state->crtc_h = crtc_h;
526
	plane_state->crtc_w = crtc_w;
544
	plane_state->crtc_w = crtc_w;
527
	plane_state->src_x = src_x;
545
	plane_state->src_x = src_x;
528
	plane_state->src_y = src_y;
546
	plane_state->src_y = src_y;
529
	plane_state->src_h = src_h;
547
	plane_state->src_h = src_h;
530
	plane_state->src_w = src_w;
548
	plane_state->src_w = src_w;
531
 
549
 
532
	return drm_plane_helper_commit(plane, plane_state, plane->fb);
550
	return drm_plane_helper_commit(plane, plane_state, plane->fb);
533
}
551
}
534
EXPORT_SYMBOL(drm_plane_helper_update);
552
EXPORT_SYMBOL(drm_plane_helper_update);
535
 
553
 
536
/**
554
/**
537
 * drm_plane_helper_disable() - Helper for primary plane disable
555
 * drm_plane_helper_disable() - Transitional helper for plane disable
538
 * @plane: plane to disable
556
 * @plane: plane to disable
539
 *
557
 *
540
 * Provides a default plane disable handler using the atomic plane update
558
 * Provides a default plane disable handler using the atomic plane update
541
 * functions. It is fully left to the driver to check plane constraints and
559
 * functions. It is fully left to the driver to check plane constraints and
542
 * handle corner-cases like a fully occluded or otherwise invisible plane.
560
 * handle corner-cases like a fully occluded or otherwise invisible plane.
543
 *
561
 *
544
 * This is useful for piecewise transitioning of a driver to the atomic helpers.
562
 * This is useful for piecewise transitioning of a driver to the atomic helpers.
545
 *
563
 *
546
 * RETURNS:
564
 * RETURNS:
547
 * Zero on success, error code on failure
565
 * Zero on success, error code on failure
548
 */
566
 */
549
int drm_plane_helper_disable(struct drm_plane *plane)
567
int drm_plane_helper_disable(struct drm_plane *plane)
550
{
568
{
551
	struct drm_plane_state *plane_state;
569
	struct drm_plane_state *plane_state;
552
 
570
 
553
	/* crtc helpers love to call disable functions for already disabled hw
571
	/* crtc helpers love to call disable functions for already disabled hw
554
	 * functions. So cope with that. */
572
	 * functions. So cope with that. */
555
	if (!plane->crtc)
573
	if (!plane->crtc)
556
		return 0;
574
		return 0;
557
 
575
 
558
	if (plane->funcs->atomic_duplicate_state)
576
	if (plane->funcs->atomic_duplicate_state)
559
		plane_state = plane->funcs->atomic_duplicate_state(plane);
577
		plane_state = plane->funcs->atomic_duplicate_state(plane);
-
 
578
	else {
560
	else if (plane->state)
579
		if (!plane->state)
-
 
580
			drm_atomic_helper_plane_reset(plane);
-
 
581
 
561
		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
582
		plane_state = drm_atomic_helper_plane_duplicate_state(plane);
562
	else
583
	}
563
		plane_state = kzalloc(sizeof(*plane_state), GFP_KERNEL);
-
 
564
	if (!plane_state)
584
	if (!plane_state)
565
		return -ENOMEM;
585
		return -ENOMEM;
-
 
586
	plane_state->plane = plane;
566
 
587
 
567
	plane_state->crtc = NULL;
588
	plane_state->crtc = NULL;
568
	drm_atomic_set_fb_for_plane(plane_state, NULL);
589
	drm_atomic_set_fb_for_plane(plane_state, NULL);
569
 
590
 
570
	return drm_plane_helper_commit(plane, plane_state, plane->fb);
591
	return drm_plane_helper_commit(plane, plane_state, plane->fb);
571
}
592
}
572
EXPORT_SYMBOL(drm_plane_helper_disable);
593
EXPORT_SYMBOL(drm_plane_helper_disable);