Subversion Repositories Kolibri OS

Rev

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

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