drm_plane_helper.c revision 1.1.1.3 1 /* $NetBSD: drm_plane_helper.c,v 1.1.1.3 2021/12/18 20:11:03 riastradh Exp $ */
2
3 /*
4 * Copyright (C) 2014 Intel Corporation
5 *
6 * DRM universal plane helper functions
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: drm_plane_helper.c,v 1.1.1.3 2021/12/18 20:11:03 riastradh Exp $");
30
31 #include <linux/list.h>
32
33 #include <drm/drm_atomic.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/drm_atomic_uapi.h>
36 #include <drm/drm_crtc_helper.h>
37 #include <drm/drm_device.h>
38 #include <drm/drm_encoder.h>
39 #include <drm/drm_plane_helper.h>
40 #include <drm/drm_rect.h>
41
42 #define SUBPIXEL_MASK 0xffff
43
44 /**
45 * DOC: overview
46 *
47 * This helper library has two parts. The first part has support to implement
48 * primary plane support on top of the normal CRTC configuration interface.
49 * Since the legacy &drm_mode_config_funcs.set_config interface ties the primary
50 * plane together with the CRTC state this does not allow userspace to disable
51 * the primary plane itself. The default primary plane only expose XRBG8888 and
52 * ARGB8888 as valid pixel formats for the attached framebuffer.
53 *
54 * Drivers are highly recommended to implement proper support for primary
55 * planes, and newly merged drivers must not rely upon these transitional
56 * helpers.
57 *
58 * The second part also implements transitional helpers which allow drivers to
59 * gradually switch to the atomic helper infrastructure for plane updates. Once
60 * that switch is complete drivers shouldn't use these any longer, instead using
61 * the proper legacy implementations for update and disable plane hooks provided
62 * by the atomic helpers.
63 *
64 * Again drivers are strongly urged to switch to the new interfaces.
65 *
66 * The plane helpers share the function table structures with other helpers,
67 * specifically also the atomic helpers. See &struct drm_plane_helper_funcs for
68 * the details.
69 */
70
71 /*
72 * Returns the connectors currently associated with a CRTC. This function
73 * should be called twice: once with a NULL connector list to retrieve
74 * the list size, and once with the properly allocated list to be filled in.
75 */
76 static int get_connectors_for_crtc(struct drm_crtc *crtc,
77 struct drm_connector **connector_list,
78 int num_connectors)
79 {
80 struct drm_device *dev = crtc->dev;
81 struct drm_connector *connector;
82 struct drm_connector_list_iter conn_iter;
83 int count = 0;
84
85 /*
86 * Note: Once we change the plane hooks to more fine-grained locking we
87 * need to grab the connection_mutex here to be able to make these
88 * checks.
89 */
90 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
91
92 drm_connector_list_iter_begin(dev, &conn_iter);
93 drm_for_each_connector_iter(connector, &conn_iter) {
94 if (connector->encoder && connector->encoder->crtc == crtc) {
95 if (connector_list != NULL && count < num_connectors)
96 *(connector_list++) = connector;
97
98 count++;
99 }
100 }
101 drm_connector_list_iter_end(&conn_iter);
102
103 return count;
104 }
105
106 static int drm_plane_helper_check_update(struct drm_plane *plane,
107 struct drm_crtc *crtc,
108 struct drm_framebuffer *fb,
109 struct drm_rect *src,
110 struct drm_rect *dst,
111 unsigned int rotation,
112 int min_scale,
113 int max_scale,
114 bool can_position,
115 bool can_update_disabled,
116 bool *visible)
117 {
118 struct drm_plane_state plane_state = {
119 .plane = plane,
120 .crtc = crtc,
121 .fb = fb,
122 .src_x = src->x1,
123 .src_y = src->y1,
124 .src_w = drm_rect_width(src),
125 .src_h = drm_rect_height(src),
126 .crtc_x = dst->x1,
127 .crtc_y = dst->y1,
128 .crtc_w = drm_rect_width(dst),
129 .crtc_h = drm_rect_height(dst),
130 .rotation = rotation,
131 .visible = *visible,
132 };
133 struct drm_crtc_state crtc_state = {
134 .crtc = crtc,
135 .enable = crtc->enabled,
136 .mode = crtc->mode,
137 };
138 int ret;
139
140 ret = drm_atomic_helper_check_plane_state(&plane_state, &crtc_state,
141 min_scale, max_scale,
142 can_position,
143 can_update_disabled);
144 if (ret)
145 return ret;
146
147 *src = plane_state.src;
148 *dst = plane_state.dst;
149 *visible = plane_state.visible;
150
151 return 0;
152 }
153
154 static int drm_primary_helper_update(struct drm_plane *plane, struct drm_crtc *crtc,
155 struct drm_framebuffer *fb,
156 int crtc_x, int crtc_y,
157 unsigned int crtc_w, unsigned int crtc_h,
158 uint32_t src_x, uint32_t src_y,
159 uint32_t src_w, uint32_t src_h,
160 struct drm_modeset_acquire_ctx *ctx)
161 {
162 struct drm_mode_set set = {
163 .crtc = crtc,
164 .fb = fb,
165 .mode = &crtc->mode,
166 .x = src_x >> 16,
167 .y = src_y >> 16,
168 };
169 struct drm_rect src = {
170 .x1 = src_x,
171 .y1 = src_y,
172 .x2 = src_x + src_w,
173 .y2 = src_y + src_h,
174 };
175 struct drm_rect dest = {
176 .x1 = crtc_x,
177 .y1 = crtc_y,
178 .x2 = crtc_x + crtc_w,
179 .y2 = crtc_y + crtc_h,
180 };
181 struct drm_connector **connector_list;
182 int num_connectors, ret;
183 bool visible;
184
185 ret = drm_plane_helper_check_update(plane, crtc, fb,
186 &src, &dest,
187 DRM_MODE_ROTATE_0,
188 DRM_PLANE_HELPER_NO_SCALING,
189 DRM_PLANE_HELPER_NO_SCALING,
190 false, false, &visible);
191 if (ret)
192 return ret;
193
194 if (!visible)
195 /*
196 * Primary plane isn't visible. Note that unless a driver
197 * provides their own disable function, this will just
198 * wind up returning -EINVAL to userspace.
199 */
200 return plane->funcs->disable_plane(plane, ctx);
201
202 /* Find current connectors for CRTC */
203 num_connectors = get_connectors_for_crtc(crtc, NULL, 0);
204 BUG_ON(num_connectors == 0);
205 connector_list = kcalloc(num_connectors, sizeof(*connector_list),
206 GFP_KERNEL);
207 if (!connector_list)
208 return -ENOMEM;
209 get_connectors_for_crtc(crtc, connector_list, num_connectors);
210
211 set.connectors = connector_list;
212 set.num_connectors = num_connectors;
213
214 /*
215 * We call set_config() directly here rather than using
216 * drm_mode_set_config_internal. We're reprogramming the same
217 * connectors that were already in use, so we shouldn't need the extra
218 * cross-CRTC fb refcounting to accomodate stealing connectors.
219 * drm_mode_setplane() already handles the basic refcounting for the
220 * framebuffers involved in this operation.
221 */
222 ret = crtc->funcs->set_config(&set, ctx);
223
224 kfree(connector_list);
225 return ret;
226 }
227
228 static int drm_primary_helper_disable(struct drm_plane *plane,
229 struct drm_modeset_acquire_ctx *ctx)
230 {
231 return -EINVAL;
232 }
233
234 /**
235 * drm_primary_helper_destroy() - Helper for primary plane destruction
236 * @plane: plane to destroy
237 *
238 * Provides a default plane destroy handler for primary planes. This handler
239 * is called during CRTC destruction. We disable the primary plane, remove
240 * it from the DRM plane list, and deallocate the plane structure.
241 */
242 void drm_primary_helper_destroy(struct drm_plane *plane)
243 {
244 drm_plane_cleanup(plane);
245 kfree(plane);
246 }
247 EXPORT_SYMBOL(drm_primary_helper_destroy);
248
249 const struct drm_plane_funcs drm_primary_helper_funcs = {
250 .update_plane = drm_primary_helper_update,
251 .disable_plane = drm_primary_helper_disable,
252 .destroy = drm_primary_helper_destroy,
253 };
254 EXPORT_SYMBOL(drm_primary_helper_funcs);
255