2 * Copyright (C) 2014 Intel Corporation
4 * DRM universal plane helper functions
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:
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
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
26 #include <linux/list.h>
28 #include <drm/drm_plane_helper.h>
29 #include <drm/drm_rect.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_uapi.h>
32 #include <drm/drm_crtc_helper.h>
33 #include <drm/drm_encoder.h>
34 #include <drm/drm_atomic_helper.h>
36 #define SUBPIXEL_MASK 0xffff
41 * This helper library has two parts. The first part has support to implement
42 * primary plane support on top of the normal CRTC configuration interface.
43 * Since the legacy &drm_mode_config_funcs.set_config interface ties the primary
44 * plane together with the CRTC state this does not allow userspace to disable
45 * the primary plane itself. The default primary plane only expose XRBG8888 and
46 * ARGB8888 as valid pixel formats for the attached framebuffer.
48 * Drivers are highly recommended to implement proper support for primary
49 * planes, and newly merged drivers must not rely upon these transitional
52 * The second part also implements transitional helpers which allow drivers to
53 * gradually switch to the atomic helper infrastructure for plane updates. Once
54 * that switch is complete drivers shouldn't use these any longer, instead using
55 * the proper legacy implementations for update and disable plane hooks provided
56 * by the atomic helpers.
58 * Again drivers are strongly urged to switch to the new interfaces.
60 * The plane helpers share the function table structures with other helpers,
61 * specifically also the atomic helpers. See &struct drm_plane_helper_funcs for
66 * Returns the connectors currently associated with a CRTC. This function
67 * should be called twice: once with a NULL connector list to retrieve
68 * the list size, and once with the properly allocated list to be filled in.
70 static int get_connectors_for_crtc(struct drm_crtc *crtc,
71 struct drm_connector **connector_list,
74 struct drm_device *dev = crtc->dev;
75 struct drm_connector *connector;
76 struct drm_connector_list_iter conn_iter;
80 * Note: Once we change the plane hooks to more fine-grained locking we
81 * need to grab the connection_mutex here to be able to make these
84 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
86 drm_connector_list_iter_begin(dev, &conn_iter);
87 drm_for_each_connector_iter(connector, &conn_iter) {
88 if (connector->encoder && connector->encoder->crtc == crtc) {
89 if (connector_list != NULL && count < num_connectors)
90 *(connector_list++) = connector;
95 drm_connector_list_iter_end(&conn_iter);
101 * drm_plane_helper_check_update() - Check plane update for validity
102 * @plane: plane object to update
103 * @crtc: owning CRTC of owning plane
104 * @fb: framebuffer to flip onto plane
105 * @src: source coordinates in 16.16 fixed point
106 * @dst: integer destination coordinates
107 * @rotation: plane rotation
108 * @min_scale: minimum @src:@dest scaling factor in 16.16 fixed point
109 * @max_scale: maximum @src:@dest scaling factor in 16.16 fixed point
110 * @can_position: is it legal to position the plane such that it
111 * doesn't cover the entire crtc? This will generally
112 * only be false for primary planes.
113 * @can_update_disabled: can the plane be updated while the crtc
115 * @visible: output parameter indicating whether plane is still visible after
118 * Checks that a desired plane update is valid. Drivers that provide
119 * their own plane handling rather than helper-provided implementations may
120 * still wish to call this function to avoid duplication of error checking
124 * Zero if update appears valid, error code on failure
126 int drm_plane_helper_check_update(struct drm_plane *plane,
127 struct drm_crtc *crtc,
128 struct drm_framebuffer *fb,
129 struct drm_rect *src,
130 struct drm_rect *dst,
131 unsigned int rotation,
135 bool can_update_disabled,
138 struct drm_plane_state plane_state = {
144 .src_w = drm_rect_width(src),
145 .src_h = drm_rect_height(src),
148 .crtc_w = drm_rect_width(dst),
149 .crtc_h = drm_rect_height(dst),
150 .rotation = rotation,
153 struct drm_crtc_state crtc_state = {
155 .enable = crtc->enabled,
160 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
161 min_scale, max_scale,
163 can_update_disabled);
167 *src = plane_state.src;
168 *dst = plane_state.dst;
169 *visible = plane_state.visible;
173 EXPORT_SYMBOL(drm_plane_helper_check_update);
175 static int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
176 struct drm_framebuffer *fb,
177 int crtc_x, int crtc_y,
178 unsigned int crtc_w, unsigned int crtc_h,
179 uint32_t src_x, uint32_t src_y,
180 uint32_t src_w, uint32_t src_h,
181 struct drm_modeset_acquire_ctx *ctx)
183 struct drm_mode_set set = {
190 struct drm_rect src = {
196 struct drm_rect dest = {
199 .x2 = crtc_x + crtc_w,
200 .y2 = crtc_y + crtc_h,
202 struct drm_connector **connector_list;
203 int num_connectors, ret;
206 ret = drm_plane_helper_check_update(plane, crtc, fb,
209 DRM_PLANE_HELPER_NO_SCALING,
210 DRM_PLANE_HELPER_NO_SCALING,
211 false, false, &visible);
217 * Primary plane isn't visible. Note that unless a driver
218 * provides their own disable function, this will just
219 * wind up returning -EINVAL to userspace.
221 return plane->funcs->disable_plane(plane, ctx);
223 /* Find current connectors for CRTC */
224 num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
225 BUG_ON(num_connectors == 0);
226 connector_list = kcalloc(num_connectors, sizeof(*connector_list),
230 get_connectors_for_crtc(crtc, connector_list, num_connectors);
232 set.connectors = connector_list;
233 set.num_connectors = num_connectors;
236 * We call set_config() directly here rather than using
237 * drm_mode_set_config_internal. We're reprogramming the same
238 * connectors that were already in use, so we shouldn't need the extra
239 * cross-CRTC fb refcounting to accomodate stealing connectors.
240 * drm_mode_setplane() already handles the basic refcounting for the
241 * framebuffers involved in this operation.
243 ret = crtc->funcs->set_config(&set, ctx);
245 kfree(connector_list);
249 static int drm_primary_helper_disable(struct drm_plane *plane,
250 struct drm_modeset_acquire_ctx *ctx)
256 * drm_primary_helper_destroy() - Helper for primary plane destruction
257 * @plane: plane to destroy
259 * Provides a default plane destroy handler for primary planes. This handler
260 * is called during CRTC destruction. We disable the primary plane, remove
261 * it from the DRM plane list, and deallocate the plane structure.
263 void drm_primary_helper_destroy(struct drm_plane *plane)
265 drm_plane_cleanup(plane);
268 EXPORT_SYMBOL(drm_primary_helper_destroy);
270 const struct drm_plane_funcs drm_primary_helper_funcs = {
271 .update_plane = drm_primary_helper_update,
272 .disable_plane = drm_primary_helper_disable,
273 .destroy = drm_primary_helper_destroy,
275 EXPORT_SYMBOL(drm_primary_helper_funcs);