proc: Fix proc_sys_prune_dcache to hold a sb reference
[cris-mirror.git] / drivers / gpu / drm / drm_blend.c
bloba0d0d684328822d7166f881b3470700a49a870f1
1 /*
2 * Copyright (C) 2016 Samsung Electronics Co.Ltd
3 * Authors:
4 * Marek Szyprowski <m.szyprowski@samsung.com>
6 * DRM core plane blending related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
26 #include <drm/drmP.h>
27 #include <drm/drm_atomic.h>
28 #include <drm/drm_blend.h>
29 #include <linux/export.h>
30 #include <linux/slab.h>
31 #include <linux/sort.h>
33 #include "drm_crtc_internal.h"
35 /**
36 * DOC: overview
38 * The basic plane composition model supported by standard plane properties only
39 * has a source rectangle (in logical pixels within the &drm_framebuffer), with
40 * sub-pixel accuracy, which is scaled up to a pixel-aligned destination
41 * rectangle in the visible area of a &drm_crtc. The visible area of a CRTC is
42 * defined by the horizontal and vertical visible pixels (stored in @hdisplay
43 * and @vdisplay) of the requested mode (stored in &drm_crtc_state.mode). These
44 * two rectangles are both stored in the &drm_plane_state.
46 * For the atomic ioctl the following standard (atomic) properties on the plane object
47 * encode the basic plane composition model:
49 * SRC_X:
50 * X coordinate offset for the source rectangle within the
51 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
52 * SRC_Y:
53 * Y coordinate offset for the source rectangle within the
54 * &drm_framebuffer, in 16.16 fixed point. Must be positive.
55 * SRC_W:
56 * Width for the source rectangle within the &drm_framebuffer, in 16.16
57 * fixed point. SRC_X plus SRC_W must be within the width of the source
58 * framebuffer. Must be positive.
59 * SRC_H:
60 * Height for the source rectangle within the &drm_framebuffer, in 16.16
61 * fixed point. SRC_Y plus SRC_H must be within the height of the source
62 * framebuffer. Must be positive.
63 * CRTC_X:
64 * X coordinate offset for the destination rectangle. Can be negative.
65 * CRTC_Y:
66 * Y coordinate offset for the destination rectangle. Can be negative.
67 * CRTC_W:
68 * Width for the destination rectangle. CRTC_X plus CRTC_W can extend past
69 * the currently visible horizontal area of the &drm_crtc.
70 * CRTC_H:
71 * Height for the destination rectangle. CRTC_Y plus CRTC_H can extend past
72 * the currently visible vertical area of the &drm_crtc.
73 * FB_ID:
74 * Mode object ID of the &drm_framebuffer this plane should scan out.
75 * CRTC_ID:
76 * Mode object ID of the &drm_crtc this plane should be connected to.
78 * Note that the source rectangle must fully lie within the bounds of the
79 * &drm_framebuffer. The destination rectangle can lie outside of the visible
80 * area of the current mode of the CRTC. It must be apprpriately clipped by the
81 * driver, which can be done by calling drm_plane_helper_check_update(). Drivers
82 * are also allowed to round the subpixel sampling positions appropriately, but
83 * only to the next full pixel. No pixel outside of the source rectangle may
84 * ever be sampled, which is important when applying more sophisticated
85 * filtering than just a bilinear one when scaling. The filtering mode when
86 * scaling is unspecified.
88 * On top of this basic transformation additional properties can be exposed by
89 * the driver:
91 * - Rotation is set up with drm_plane_create_rotation_property(). It adds a
92 * rotation and reflection step between the source and destination rectangles.
93 * Without this property the rectangle is only scaled, but not rotated or
94 * reflected.
96 * - Z position is set up with drm_plane_create_zpos_immutable_property() and
97 * drm_plane_create_zpos_property(). It controls the visibility of overlapping
98 * planes. Without this property the primary plane is always below the cursor
99 * plane, and ordering between all other planes is undefined.
101 * Note that all the property extensions described here apply either to the
102 * plane or the CRTC (e.g. for the background color, which currently is not
103 * exposed and assumed to be black).
107 * drm_plane_create_rotation_property - create a new rotation property
108 * @plane: drm plane
109 * @rotation: initial value of the rotation property
110 * @supported_rotations: bitmask of supported rotations and reflections
112 * This creates a new property with the selected support for transformations.
114 * Since a rotation by 180° degress is the same as reflecting both along the x
115 * and the y axis the rotation property is somewhat redundant. Drivers can use
116 * drm_rotation_simplify() to normalize values of this property.
118 * The property exposed to userspace is a bitmask property (see
119 * drm_property_create_bitmask()) called "rotation" and has the following
120 * bitmask enumaration values:
122 * DRM_ROTATE_0:
123 * "rotate-0"
124 * DRM_ROTATE_90:
125 * "rotate-90"
126 * DRM_ROTATE_180:
127 * "rotate-180"
128 * DRM_ROTATE_270:
129 * "rotate-270"
130 * DRM_REFLECT_X:
131 * "reflect-x"
132 * DRM_REFELCT_Y:
133 * "reflect-y"
135 * Rotation is the specified amount in degrees in counter clockwise direction,
136 * the X and Y axis are within the source rectangle, i.e. the X/Y axis before
137 * rotation. After reflection, the rotation is applied to the image sampled from
138 * the source rectangle, before scaling it to fit the destination rectangle.
140 int drm_plane_create_rotation_property(struct drm_plane *plane,
141 unsigned int rotation,
142 unsigned int supported_rotations)
144 static const struct drm_prop_enum_list props[] = {
145 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
146 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
147 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
148 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
149 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
150 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
152 struct drm_property *prop;
154 WARN_ON((supported_rotations & DRM_ROTATE_MASK) == 0);
155 WARN_ON(!is_power_of_2(rotation & DRM_ROTATE_MASK));
156 WARN_ON(rotation & ~supported_rotations);
158 prop = drm_property_create_bitmask(plane->dev, 0, "rotation",
159 props, ARRAY_SIZE(props),
160 supported_rotations);
161 if (!prop)
162 return -ENOMEM;
164 drm_object_attach_property(&plane->base, prop, rotation);
166 if (plane->state)
167 plane->state->rotation = rotation;
169 plane->rotation_property = prop;
171 return 0;
173 EXPORT_SYMBOL(drm_plane_create_rotation_property);
176 * drm_rotation_simplify() - Try to simplify the rotation
177 * @rotation: Rotation to be simplified
178 * @supported_rotations: Supported rotations
180 * Attempt to simplify the rotation to a form that is supported.
181 * Eg. if the hardware supports everything except DRM_REFLECT_X
182 * one could call this function like this:
184 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
185 * DRM_ROTATE_90 | DRM_ROTATE_180 |
186 * DRM_ROTATE_270 | DRM_REFLECT_Y);
188 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
189 * transforms the hardware supports, this function may not
190 * be able to produce a supported transform, so the caller should
191 * check the result afterwards.
193 unsigned int drm_rotation_simplify(unsigned int rotation,
194 unsigned int supported_rotations)
196 if (rotation & ~supported_rotations) {
197 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
198 rotation = (rotation & DRM_REFLECT_MASK) |
199 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
202 return rotation;
204 EXPORT_SYMBOL(drm_rotation_simplify);
207 * drm_plane_create_zpos_property - create mutable zpos property
208 * @plane: drm plane
209 * @zpos: initial value of zpos property
210 * @min: minimal possible value of zpos property
211 * @max: maximal possible value of zpos property
213 * This function initializes generic mutable zpos property and enables support
214 * for it in drm core. Drivers can then attach this property to planes to enable
215 * support for configurable planes arrangement during blending operation.
216 * Once mutable zpos property has been enabled, the DRM core will automatically
217 * calculate &drm_plane_state.normalized_zpos values. Usually min should be set
218 * to 0 and max to maximal number of planes for given crtc - 1.
220 * If zpos of some planes cannot be changed (like fixed background or
221 * cursor/topmost planes), driver should adjust min/max values and assign those
222 * planes immutable zpos property with lower or higher values (for more
223 * information, see drm_plane_create_zpos_immutable_property() function). In such
224 * case driver should also assign proper initial zpos values for all planes in
225 * its plane_reset() callback, so the planes will be always sorted properly.
227 * See also drm_atomic_normalize_zpos().
229 * The property exposed to userspace is called "zpos".
231 * Returns:
232 * Zero on success, negative errno on failure.
234 int drm_plane_create_zpos_property(struct drm_plane *plane,
235 unsigned int zpos,
236 unsigned int min, unsigned int max)
238 struct drm_property *prop;
240 prop = drm_property_create_range(plane->dev, 0, "zpos", min, max);
241 if (!prop)
242 return -ENOMEM;
244 drm_object_attach_property(&plane->base, prop, zpos);
246 plane->zpos_property = prop;
248 if (plane->state) {
249 plane->state->zpos = zpos;
250 plane->state->normalized_zpos = zpos;
253 return 0;
255 EXPORT_SYMBOL(drm_plane_create_zpos_property);
258 * drm_plane_create_zpos_immutable_property - create immuttable zpos property
259 * @plane: drm plane
260 * @zpos: value of zpos property
262 * This function initializes generic immutable zpos property and enables
263 * support for it in drm core. Using this property driver lets userspace
264 * to get the arrangement of the planes for blending operation and notifies
265 * it that the hardware (or driver) doesn't support changing of the planes'
266 * order. For mutable zpos see drm_plane_create_zpos_property().
268 * The property exposed to userspace is called "zpos".
270 * Returns:
271 * Zero on success, negative errno on failure.
273 int drm_plane_create_zpos_immutable_property(struct drm_plane *plane,
274 unsigned int zpos)
276 struct drm_property *prop;
278 prop = drm_property_create_range(plane->dev, DRM_MODE_PROP_IMMUTABLE,
279 "zpos", zpos, zpos);
280 if (!prop)
281 return -ENOMEM;
283 drm_object_attach_property(&plane->base, prop, zpos);
285 plane->zpos_property = prop;
287 if (plane->state) {
288 plane->state->zpos = zpos;
289 plane->state->normalized_zpos = zpos;
292 return 0;
294 EXPORT_SYMBOL(drm_plane_create_zpos_immutable_property);
296 static int drm_atomic_state_zpos_cmp(const void *a, const void *b)
298 const struct drm_plane_state *sa = *(struct drm_plane_state **)a;
299 const struct drm_plane_state *sb = *(struct drm_plane_state **)b;
301 if (sa->zpos != sb->zpos)
302 return sa->zpos - sb->zpos;
303 else
304 return sa->plane->base.id - sb->plane->base.id;
307 static int drm_atomic_helper_crtc_normalize_zpos(struct drm_crtc *crtc,
308 struct drm_crtc_state *crtc_state)
310 struct drm_atomic_state *state = crtc_state->state;
311 struct drm_device *dev = crtc->dev;
312 int total_planes = dev->mode_config.num_total_plane;
313 struct drm_plane_state **states;
314 struct drm_plane *plane;
315 int i, n = 0;
316 int ret = 0;
318 DRM_DEBUG_ATOMIC("[CRTC:%d:%s] calculating normalized zpos values\n",
319 crtc->base.id, crtc->name);
321 states = kmalloc_array(total_planes, sizeof(*states), GFP_TEMPORARY);
322 if (!states)
323 return -ENOMEM;
326 * Normalization process might create new states for planes which
327 * normalized_zpos has to be recalculated.
329 drm_for_each_plane_mask(plane, dev, crtc_state->plane_mask) {
330 struct drm_plane_state *plane_state =
331 drm_atomic_get_plane_state(state, plane);
332 if (IS_ERR(plane_state)) {
333 ret = PTR_ERR(plane_state);
334 goto done;
336 states[n++] = plane_state;
337 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] processing zpos value %d\n",
338 plane->base.id, plane->name,
339 plane_state->zpos);
342 sort(states, n, sizeof(*states), drm_atomic_state_zpos_cmp, NULL);
344 for (i = 0; i < n; i++) {
345 plane = states[i]->plane;
347 states[i]->normalized_zpos = i;
348 DRM_DEBUG_ATOMIC("[PLANE:%d:%s] normalized zpos value %d\n",
349 plane->base.id, plane->name, i);
351 crtc_state->zpos_changed = true;
353 done:
354 kfree(states);
355 return ret;
359 * drm_atomic_normalize_zpos - calculate normalized zpos values for all crtcs
360 * @dev: DRM device
361 * @state: atomic state of DRM device
363 * This function calculates normalized zpos value for all modified planes in
364 * the provided atomic state of DRM device.
366 * For every CRTC this function checks new states of all planes assigned to
367 * it and calculates normalized zpos value for these planes. Planes are compared
368 * first by their zpos values, then by plane id (if zpos is equal). The plane
369 * with lowest zpos value is at the bottom. The &drm_plane_state.normalized_zpos
370 * is then filled with unique values from 0 to number of active planes in crtc
371 * minus one.
373 * RETURNS
374 * Zero for success or -errno
376 int drm_atomic_normalize_zpos(struct drm_device *dev,
377 struct drm_atomic_state *state)
379 struct drm_crtc *crtc;
380 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
381 struct drm_plane *plane;
382 struct drm_plane_state *old_plane_state, *new_plane_state;
383 int i, ret = 0;
385 for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) {
386 crtc = new_plane_state->crtc;
387 if (!crtc)
388 continue;
389 if (old_plane_state->zpos != new_plane_state->zpos) {
390 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
391 new_crtc_state->zpos_changed = true;
395 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
396 if (old_crtc_state->plane_mask != new_crtc_state->plane_mask ||
397 new_crtc_state->zpos_changed) {
398 ret = drm_atomic_helper_crtc_normalize_zpos(crtc,
399 new_crtc_state);
400 if (ret)
401 return ret;
404 return 0;
406 EXPORT_SYMBOL(drm_atomic_normalize_zpos);