blob: fe4d1fb2376cd488d22974a2710d2b2571fb6122 [file] [log] [blame]
Matt Roperc103d1c2014-04-01 15:22:35 -07001/*
2 * Copyright (C) 2014 Intel Corporation
3 *
4 * DRM universal plane helper functions
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
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
11 * Software is furnished to do so, subject to the following conditions:
12 *
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
15 * Software.
16 *
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,
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
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
23 * SOFTWARE.
24 */
25
26#include <linux/list.h>
27#include <drm/drmP.h>
Matt Roper7daf8d52014-05-29 08:06:52 -070028#include <drm/drm_plane_helper.h>
Matt Roperc103d1c2014-04-01 15:22:35 -070029#include <drm/drm_rect.h>
Thierry Redingf220e622014-05-13 12:47:42 +020030#include <drm/drm_plane_helper.h>
Matt Roperc103d1c2014-04-01 15:22:35 -070031
32#define SUBPIXEL_MASK 0xffff
33
34/*
35 * This is the minimal list of formats that seem to be safe for modeset use
36 * with all current DRM drivers. Most hardware can actually support more
37 * formats than this and drivers may specify a more accurate list when
38 * creating the primary plane. However drivers that still call
39 * drm_plane_init() will use this minimal format list as the default.
40 */
Thierry Reding233fd4e2014-05-13 16:58:35 +020041static const uint32_t safe_modeset_formats[] = {
42 DRM_FORMAT_XRGB8888,
43 DRM_FORMAT_ARGB8888,
Matt Roperc103d1c2014-04-01 15:22:35 -070044};
45
46/*
47 * Returns the connectors currently associated with a CRTC. This function
48 * should be called twice: once with a NULL connector list to retrieve
49 * the list size, and once with the properly allocated list to be filled in.
50 */
51static int get_connectors_for_crtc(struct drm_crtc *crtc,
52 struct drm_connector **connector_list,
53 int num_connectors)
54{
55 struct drm_device *dev = crtc->dev;
56 struct drm_connector *connector;
57 int count = 0;
58
Daniel Vetter6e9f7982014-05-29 23:54:47 +020059 /*
60 * Note: Once we change the plane hooks to more fine-grained locking we
61 * need to grab the connection_mutex here to be able to make these
62 * checks.
63 */
Rob Clark51fd3712013-11-19 12:10:12 -050064 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Daniel Vetter6e9f7982014-05-29 23:54:47 +020065
Matt Roperc103d1c2014-04-01 15:22:35 -070066 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
67 if (connector->encoder && connector->encoder->crtc == crtc) {
68 if (connector_list != NULL && count < num_connectors)
69 *(connector_list++) = connector;
70
71 count++;
72 }
73
74 return count;
75}
76
77/**
Matt Roper7daf8d52014-05-29 08:06:52 -070078 * drm_plane_helper_check_update() - Check plane update for validity
79 * @plane: plane object to update
80 * @crtc: owning CRTC of owning plane
81 * @fb: framebuffer to flip onto plane
82 * @src: source coordinates in 16.16 fixed point
83 * @dest: integer destination coordinates
84 * @clip: integer clipping coordinates
85 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
86 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
87 * @can_position: is it legal to position the plane such that it
88 * doesn't cover the entire crtc? This will generally
89 * only be false for primary planes.
90 * @can_update_disabled: can the plane be updated while the crtc
91 * is disabled?
92 * @visible: output parameter indicating whether plane is still visible after
93 * clipping
94 *
95 * Checks that a desired plane update is valid. Drivers that provide
96 * their own plane handling rather than helper-provided implementations may
97 * still wish to call this function to avoid duplication of error checking
98 * code.
99 *
100 * RETURNS:
101 * Zero if update appears valid, error code on failure
102 */
103int drm_plane_helper_check_update(struct drm_plane *plane,
104 struct drm_crtc *crtc,
105 struct drm_framebuffer *fb,
106 struct drm_rect *src,
107 struct drm_rect *dest,
108 const struct drm_rect *clip,
109 int min_scale,
110 int max_scale,
111 bool can_position,
112 bool can_update_disabled,
113 bool *visible)
114{
115 int hscale, vscale;
116
117 if (!crtc->enabled && !can_update_disabled) {
118 DRM_DEBUG_KMS("Cannot update plane of a disabled CRTC.\n");
119 return -EINVAL;
120 }
121
122 /* Check scaling */
123 hscale = drm_rect_calc_hscale(src, dest, min_scale, max_scale);
124 vscale = drm_rect_calc_vscale(src, dest, min_scale, max_scale);
125 if (hscale < 0 || vscale < 0) {
126 DRM_DEBUG_KMS("Invalid scaling of plane\n");
127 return -ERANGE;
128 }
129
Gustavo Padovan083fe3b2014-10-24 19:00:17 +0100130 if (!fb) {
131 *visible = false;
132 return 0;
133 }
134
Matt Roper7daf8d52014-05-29 08:06:52 -0700135 *visible = drm_rect_clip_scaled(src, dest, clip, hscale, vscale);
136 if (!*visible)
137 /*
138 * Plane isn't visible; some drivers can handle this
139 * so we just return success here. Drivers that can't
140 * (including those that use the primary plane helper's
141 * update function) will return an error from their
142 * update_plane handler.
143 */
144 return 0;
145
146 if (!can_position && !drm_rect_equals(dest, clip)) {
147 DRM_DEBUG_KMS("Plane must cover entire CRTC\n");
148 return -EINVAL;
149 }
150
151 return 0;
152}
153EXPORT_SYMBOL(drm_plane_helper_check_update);
154
155/**
Matt Roperc103d1c2014-04-01 15:22:35 -0700156 * drm_primary_helper_update() - Helper for primary plane update
157 * @plane: plane object to update
158 * @crtc: owning CRTC of owning plane
159 * @fb: framebuffer to flip onto plane
160 * @crtc_x: x offset of primary plane on crtc
161 * @crtc_y: y offset of primary plane on crtc
162 * @crtc_w: width of primary plane rectangle on crtc
163 * @crtc_h: height of primary plane rectangle on crtc
164 * @src_x: x offset of @fb for panning
165 * @src_y: y offset of @fb for panning
166 * @src_w: width of source rectangle in @fb
167 * @src_h: height of source rectangle in @fb
168 *
169 * Provides a default plane update handler for primary planes. This is handler
170 * is called in response to a userspace SetPlane operation on the plane with a
171 * non-NULL framebuffer. We call the driver's modeset handler to update the
172 * framebuffer.
173 *
174 * SetPlane() on a primary plane of a disabled CRTC is not supported, and will
175 * return an error.
176 *
177 * Note that we make some assumptions about hardware limitations that may not be
178 * true for all hardware --
179 * 1) Primary plane cannot be repositioned.
180 * 2) Primary plane cannot be scaled.
181 * 3) Primary plane must cover the entire CRTC.
182 * 4) Subpixel positioning is not supported.
183 * Drivers for hardware that don't have these restrictions can provide their
184 * own implementation rather than using this helper.
185 *
186 * RETURNS:
187 * Zero on success, error code on failure
188 */
189int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
190 struct drm_framebuffer *fb,
191 int crtc_x, int crtc_y,
192 unsigned int crtc_w, unsigned int crtc_h,
193 uint32_t src_x, uint32_t src_y,
194 uint32_t src_w, uint32_t src_h)
195{
196 struct drm_mode_set set = {
197 .crtc = crtc,
198 .fb = fb,
199 .mode = &crtc->mode,
200 .x = src_x >> 16,
201 .y = src_y >> 16,
202 };
Matt Roper7daf8d52014-05-29 08:06:52 -0700203 struct drm_rect src = {
204 .x1 = src_x,
205 .y1 = src_y,
206 .x2 = src_x + src_w,
207 .y2 = src_y + src_h,
208 };
Matt Roperc103d1c2014-04-01 15:22:35 -0700209 struct drm_rect dest = {
210 .x1 = crtc_x,
211 .y1 = crtc_y,
212 .x2 = crtc_x + crtc_w,
213 .y2 = crtc_y + crtc_h,
214 };
Matt Roper7daf8d52014-05-29 08:06:52 -0700215 const struct drm_rect clip = {
Matt Roperc103d1c2014-04-01 15:22:35 -0700216 .x2 = crtc->mode.hdisplay,
217 .y2 = crtc->mode.vdisplay,
218 };
219 struct drm_connector **connector_list;
Matt Roperc103d1c2014-04-01 15:22:35 -0700220 int num_connectors, ret;
Matt Roper7daf8d52014-05-29 08:06:52 -0700221 bool visible;
Matt Roperc103d1c2014-04-01 15:22:35 -0700222
Matt Roper7daf8d52014-05-29 08:06:52 -0700223 ret = drm_plane_helper_check_update(plane, crtc, fb,
224 &src, &dest, &clip,
225 DRM_PLANE_HELPER_NO_SCALING,
226 DRM_PLANE_HELPER_NO_SCALING,
227 false, false, &visible);
Matt Roperc103d1c2014-04-01 15:22:35 -0700228 if (ret)
229 return ret;
230
Matt Roper7daf8d52014-05-29 08:06:52 -0700231 if (!visible)
232 /*
233 * Primary plane isn't visible. Note that unless a driver
234 * provides their own disable function, this will just
235 * wind up returning -EINVAL to userspace.
236 */
237 return plane->funcs->disable_plane(plane);
238
Matt Roperc103d1c2014-04-01 15:22:35 -0700239 /* Find current connectors for CRTC */
240 num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
241 BUG_ON(num_connectors == 0);
242 connector_list = kzalloc(num_connectors * sizeof(*connector_list),
243 GFP_KERNEL);
244 if (!connector_list)
245 return -ENOMEM;
246 get_connectors_for_crtc(crtc, connector_list, num_connectors);
247
248 set.connectors = connector_list;
249 set.num_connectors = num_connectors;
250
251 /*
Daniel Vetter0fe27f02014-04-23 17:34:06 +0200252 * We call set_config() directly here rather than using
Matt Roperc103d1c2014-04-01 15:22:35 -0700253 * drm_mode_set_config_internal. We're reprogramming the same
254 * connectors that were already in use, so we shouldn't need the extra
255 * cross-CRTC fb refcounting to accomodate stealing connectors.
256 * drm_mode_setplane() already handles the basic refcounting for the
257 * framebuffers involved in this operation.
258 */
Matt Roperc103d1c2014-04-01 15:22:35 -0700259 ret = crtc->funcs->set_config(&set);
Matt Roperc103d1c2014-04-01 15:22:35 -0700260
261 kfree(connector_list);
262 return ret;
263}
264EXPORT_SYMBOL(drm_primary_helper_update);
265
266/**
267 * drm_primary_helper_disable() - Helper for primary plane disable
268 * @plane: plane to disable
269 *
270 * Provides a default plane disable handler for primary planes. This is handler
271 * is called in response to a userspace SetPlane operation on the plane with a
Daniel Vetterb6ccd7b2014-04-15 10:02:43 +0200272 * NULL framebuffer parameter. It unconditionally fails the disable call with
273 * -EINVAL the only way to disable the primary plane without driver support is
274 * to disable the entier CRTC. Which does not match the plane ->disable hook.
Matt Roperc103d1c2014-04-01 15:22:35 -0700275 *
276 * Note that some hardware may be able to disable the primary plane without
277 * disabling the whole CRTC. Drivers for such hardware should provide their
278 * own disable handler that disables just the primary plane (and they'll likely
279 * need to provide their own update handler as well to properly re-enable a
280 * disabled primary plane).
281 *
282 * RETURNS:
Daniel Vetterb6ccd7b2014-04-15 10:02:43 +0200283 * Unconditionally returns -EINVAL.
Matt Roperc103d1c2014-04-01 15:22:35 -0700284 */
285int drm_primary_helper_disable(struct drm_plane *plane)
286{
Daniel Vetterb6ccd7b2014-04-15 10:02:43 +0200287 return -EINVAL;
Matt Roperc103d1c2014-04-01 15:22:35 -0700288}
289EXPORT_SYMBOL(drm_primary_helper_disable);
290
291/**
292 * drm_primary_helper_destroy() - Helper for primary plane destruction
293 * @plane: plane to destroy
294 *
295 * Provides a default plane destroy handler for primary planes. This handler
296 * is called during CRTC destruction. We disable the primary plane, remove
297 * it from the DRM plane list, and deallocate the plane structure.
298 */
299void drm_primary_helper_destroy(struct drm_plane *plane)
300{
Matt Roperc103d1c2014-04-01 15:22:35 -0700301 drm_plane_cleanup(plane);
302 kfree(plane);
303}
304EXPORT_SYMBOL(drm_primary_helper_destroy);
305
306const struct drm_plane_funcs drm_primary_helper_funcs = {
307 .update_plane = drm_primary_helper_update,
308 .disable_plane = drm_primary_helper_disable,
309 .destroy = drm_primary_helper_destroy,
310};
311EXPORT_SYMBOL(drm_primary_helper_funcs);
312
313/**
314 * drm_primary_helper_create_plane() - Create a generic primary plane
315 * @dev: drm device
316 * @formats: pixel formats supported, or NULL for a default safe list
317 * @num_formats: size of @formats; ignored if @formats is NULL
318 *
319 * Allocates and initializes a primary plane that can be used with the primary
320 * plane helpers. Drivers that wish to use driver-specific plane structures or
321 * provide custom handler functions may perform their own allocation and
322 * initialization rather than calling this function.
323 */
324struct drm_plane *drm_primary_helper_create_plane(struct drm_device *dev,
325 const uint32_t *formats,
326 int num_formats)
327{
328 struct drm_plane *primary;
329 int ret;
330
331 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
332 if (primary == NULL) {
333 DRM_DEBUG_KMS("Failed to allocate primary plane\n");
334 return NULL;
335 }
336
337 if (formats == NULL) {
338 formats = safe_modeset_formats;
339 num_formats = ARRAY_SIZE(safe_modeset_formats);
340 }
341
342 /* possible_crtc's will be filled in later by crtc_init */
Matt Roper3281cc72014-06-30 15:37:51 -0700343 ret = drm_universal_plane_init(dev, primary, 0,
344 &drm_primary_helper_funcs,
345 formats, num_formats,
346 DRM_PLANE_TYPE_PRIMARY);
Matt Roperc103d1c2014-04-01 15:22:35 -0700347 if (ret) {
348 kfree(primary);
349 primary = NULL;
350 }
351
352 return primary;
353}
354EXPORT_SYMBOL(drm_primary_helper_create_plane);
355
Matt Ropere13161a2014-04-01 15:22:38 -0700356/**
357 * drm_crtc_init - Legacy CRTC initialization function
358 * @dev: DRM device
359 * @crtc: CRTC object to init
360 * @funcs: callbacks for the new CRTC
361 *
362 * Initialize a CRTC object with a default helper-provided primary plane and no
363 * cursor plane.
364 *
365 * Returns:
366 * Zero on success, error code on failure.
367 */
368int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
369 const struct drm_crtc_funcs *funcs)
370{
371 struct drm_plane *primary;
372
373 primary = drm_primary_helper_create_plane(dev, NULL, 0);
374 return drm_crtc_init_with_planes(dev, crtc, primary, NULL, funcs);
375}
376EXPORT_SYMBOL(drm_crtc_init);