drm_framebuffer.c revision 1.4 1 1.4 riastrad /* $NetBSD: drm_framebuffer.c,v 1.4 2021/12/19 01:00:09 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright (c) 2016 Intel Corporation
5 1.1 riastrad *
6 1.1 riastrad * Permission to use, copy, modify, distribute, and sell this software and its
7 1.1 riastrad * documentation for any purpose is hereby granted without fee, provided that
8 1.1 riastrad * the above copyright notice appear in all copies and that both that copyright
9 1.1 riastrad * notice and this permission notice appear in supporting documentation, and
10 1.1 riastrad * that the name of the copyright holders not be used in advertising or
11 1.1 riastrad * publicity pertaining to distribution of the software without specific,
12 1.1 riastrad * written prior permission. The copyright holders make no representations
13 1.1 riastrad * about the suitability of this software for any purpose. It is provided "as
14 1.1 riastrad * is" without express or implied warranty.
15 1.1 riastrad *
16 1.1 riastrad * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 1.1 riastrad * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 1.1 riastrad * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 1.1 riastrad * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 1.1 riastrad * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 1.1 riastrad * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 1.1 riastrad * OF THIS SOFTWARE.
23 1.1 riastrad */
24 1.1 riastrad
25 1.1 riastrad #include <sys/cdefs.h>
26 1.4 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_framebuffer.c,v 1.4 2021/12/19 01:00:09 riastradh Exp $");
27 1.1 riastrad
28 1.1 riastrad #include <linux/export.h>
29 1.1 riastrad #include <linux/uaccess.h>
30 1.1 riastrad
31 1.1 riastrad #include <drm/drm_atomic.h>
32 1.1 riastrad #include <drm/drm_atomic_uapi.h>
33 1.1 riastrad #include <drm/drm_auth.h>
34 1.1 riastrad #include <drm/drm_debugfs.h>
35 1.1 riastrad #include <drm/drm_drv.h>
36 1.1 riastrad #include <drm/drm_file.h>
37 1.1 riastrad #include <drm/drm_fourcc.h>
38 1.1 riastrad #include <drm/drm_framebuffer.h>
39 1.1 riastrad #include <drm/drm_print.h>
40 1.1 riastrad #include <drm/drm_util.h>
41 1.1 riastrad
42 1.1 riastrad #include "drm_crtc_internal.h"
43 1.1 riastrad #include "drm_internal.h"
44 1.1 riastrad
45 1.1 riastrad /**
46 1.1 riastrad * DOC: overview
47 1.1 riastrad *
48 1.1 riastrad * Frame buffers are abstract memory objects that provide a source of pixels to
49 1.1 riastrad * scanout to a CRTC. Applications explicitly request the creation of frame
50 1.1 riastrad * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque
51 1.1 riastrad * handle that can be passed to the KMS CRTC control, plane configuration and
52 1.1 riastrad * page flip functions.
53 1.1 riastrad *
54 1.1 riastrad * Frame buffers rely on the underlying memory manager for allocating backing
55 1.1 riastrad * storage. When creating a frame buffer applications pass a memory handle
56 1.1 riastrad * (or a list of memory handles for multi-planar formats) through the
57 1.1 riastrad * &struct drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace
58 1.1 riastrad * buffer management interface this would be a GEM handle. Drivers are however
59 1.1 riastrad * free to use their own backing storage object handles, e.g. vmwgfx directly
60 1.1 riastrad * exposes special TTM handles to userspace and so expects TTM handles in the
61 1.1 riastrad * create ioctl and not GEM handles.
62 1.1 riastrad *
63 1.1 riastrad * Framebuffers are tracked with &struct drm_framebuffer. They are published
64 1.1 riastrad * using drm_framebuffer_init() - after calling that function userspace can use
65 1.1 riastrad * and access the framebuffer object. The helper function
66 1.1 riastrad * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required
67 1.1 riastrad * metadata fields.
68 1.1 riastrad *
69 1.1 riastrad * The lifetime of a drm framebuffer is controlled with a reference count,
70 1.1 riastrad * drivers can grab additional references with drm_framebuffer_get() and drop
71 1.1 riastrad * them again with drm_framebuffer_put(). For driver-private framebuffers for
72 1.1 riastrad * which the last reference is never dropped (e.g. for the fbdev framebuffer
73 1.1 riastrad * when the struct &struct drm_framebuffer is embedded into the fbdev helper
74 1.1 riastrad * struct) drivers can manually clean up a framebuffer at module unload time
75 1.1 riastrad * with drm_framebuffer_unregister_private(). But doing this is not
76 1.1 riastrad * recommended, and it's better to have a normal free-standing &struct
77 1.1 riastrad * drm_framebuffer.
78 1.1 riastrad */
79 1.1 riastrad
80 1.1 riastrad int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y,
81 1.1 riastrad uint32_t src_w, uint32_t src_h,
82 1.1 riastrad const struct drm_framebuffer *fb)
83 1.1 riastrad {
84 1.1 riastrad unsigned int fb_width, fb_height;
85 1.1 riastrad
86 1.1 riastrad fb_width = fb->width << 16;
87 1.1 riastrad fb_height = fb->height << 16;
88 1.1 riastrad
89 1.1 riastrad /* Make sure source coordinates are inside the fb. */
90 1.1 riastrad if (src_w > fb_width ||
91 1.1 riastrad src_x > fb_width - src_w ||
92 1.1 riastrad src_h > fb_height ||
93 1.1 riastrad src_y > fb_height - src_h) {
94 1.1 riastrad DRM_DEBUG_KMS("Invalid source coordinates "
95 1.1 riastrad "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
96 1.1 riastrad src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
97 1.1 riastrad src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
98 1.1 riastrad src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
99 1.1 riastrad src_y >> 16, ((src_y & 0xffff) * 15625) >> 10,
100 1.1 riastrad fb->width, fb->height);
101 1.1 riastrad return -ENOSPC;
102 1.1 riastrad }
103 1.1 riastrad
104 1.1 riastrad return 0;
105 1.1 riastrad }
106 1.1 riastrad
107 1.1 riastrad /**
108 1.1 riastrad * drm_mode_addfb - add an FB to the graphics configuration
109 1.1 riastrad * @dev: drm device for the ioctl
110 1.1 riastrad * @or: pointer to request structure
111 1.1 riastrad * @file_priv: drm file
112 1.1 riastrad *
113 1.1 riastrad * Add a new FB to the specified CRTC, given a user request. This is the
114 1.1 riastrad * original addfb ioctl which only supported RGB formats.
115 1.1 riastrad *
116 1.1 riastrad * Called by the user via ioctl, or by an in-kernel client.
117 1.1 riastrad *
118 1.1 riastrad * Returns:
119 1.1 riastrad * Zero on success, negative errno on failure.
120 1.1 riastrad */
121 1.1 riastrad int drm_mode_addfb(struct drm_device *dev, struct drm_mode_fb_cmd *or,
122 1.1 riastrad struct drm_file *file_priv)
123 1.1 riastrad {
124 1.1 riastrad struct drm_mode_fb_cmd2 r = {};
125 1.1 riastrad int ret;
126 1.1 riastrad
127 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
128 1.1 riastrad return -EOPNOTSUPP;
129 1.1 riastrad
130 1.1 riastrad r.pixel_format = drm_driver_legacy_fb_format(dev, or->bpp, or->depth);
131 1.1 riastrad if (r.pixel_format == DRM_FORMAT_INVALID) {
132 1.1 riastrad DRM_DEBUG("bad {bpp:%d, depth:%d}\n", or->bpp, or->depth);
133 1.1 riastrad return -EINVAL;
134 1.1 riastrad }
135 1.1 riastrad
136 1.1 riastrad /* convert to new format and call new ioctl */
137 1.1 riastrad r.fb_id = or->fb_id;
138 1.1 riastrad r.width = or->width;
139 1.1 riastrad r.height = or->height;
140 1.1 riastrad r.pitches[0] = or->pitch;
141 1.1 riastrad r.handles[0] = or->handle;
142 1.1 riastrad
143 1.1 riastrad ret = drm_mode_addfb2(dev, &r, file_priv);
144 1.1 riastrad if (ret)
145 1.1 riastrad return ret;
146 1.1 riastrad
147 1.1 riastrad or->fb_id = r.fb_id;
148 1.1 riastrad
149 1.1 riastrad return 0;
150 1.1 riastrad }
151 1.1 riastrad
152 1.1 riastrad int drm_mode_addfb_ioctl(struct drm_device *dev,
153 1.1 riastrad void *data, struct drm_file *file_priv)
154 1.1 riastrad {
155 1.1 riastrad return drm_mode_addfb(dev, data, file_priv);
156 1.1 riastrad }
157 1.1 riastrad
158 1.1 riastrad static int fb_plane_width(int width,
159 1.1 riastrad const struct drm_format_info *format, int plane)
160 1.1 riastrad {
161 1.1 riastrad if (plane == 0)
162 1.1 riastrad return width;
163 1.1 riastrad
164 1.1 riastrad return DIV_ROUND_UP(width, format->hsub);
165 1.1 riastrad }
166 1.1 riastrad
167 1.1 riastrad static int fb_plane_height(int height,
168 1.1 riastrad const struct drm_format_info *format, int plane)
169 1.1 riastrad {
170 1.1 riastrad if (plane == 0)
171 1.1 riastrad return height;
172 1.1 riastrad
173 1.1 riastrad return DIV_ROUND_UP(height, format->vsub);
174 1.1 riastrad }
175 1.1 riastrad
176 1.1 riastrad static int framebuffer_check(struct drm_device *dev,
177 1.1 riastrad const struct drm_mode_fb_cmd2 *r)
178 1.1 riastrad {
179 1.1 riastrad const struct drm_format_info *info;
180 1.1 riastrad int i;
181 1.1 riastrad
182 1.1 riastrad /* check if the format is supported at all */
183 1.1 riastrad info = __drm_format_info(r->pixel_format);
184 1.1 riastrad if (!info) {
185 1.1 riastrad struct drm_format_name_buf format_name;
186 1.1 riastrad
187 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer format %s\n",
188 1.1 riastrad drm_get_format_name(r->pixel_format,
189 1.1 riastrad &format_name));
190 1.1 riastrad return -EINVAL;
191 1.1 riastrad }
192 1.1 riastrad
193 1.1 riastrad /* now let the driver pick its own format info */
194 1.1 riastrad info = drm_get_format_info(dev, r);
195 1.1 riastrad
196 1.1 riastrad if (r->width == 0) {
197 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
198 1.1 riastrad return -EINVAL;
199 1.1 riastrad }
200 1.1 riastrad
201 1.1 riastrad if (r->height == 0) {
202 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
203 1.1 riastrad return -EINVAL;
204 1.1 riastrad }
205 1.1 riastrad
206 1.1 riastrad for (i = 0; i < info->num_planes; i++) {
207 1.1 riastrad unsigned int width = fb_plane_width(r->width, info, i);
208 1.1 riastrad unsigned int height = fb_plane_height(r->height, info, i);
209 1.1 riastrad unsigned int block_size = info->char_per_block[i];
210 1.1 riastrad u64 min_pitch = drm_format_info_min_pitch(info, i, width);
211 1.1 riastrad
212 1.1 riastrad if (!block_size && (r->modifier[i] == DRM_FORMAT_MOD_LINEAR)) {
213 1.1 riastrad DRM_DEBUG_KMS("Format requires non-linear modifier for plane %d\n", i);
214 1.1 riastrad return -EINVAL;
215 1.1 riastrad }
216 1.1 riastrad
217 1.1 riastrad if (!r->handles[i]) {
218 1.1 riastrad DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
219 1.1 riastrad return -EINVAL;
220 1.1 riastrad }
221 1.1 riastrad
222 1.1 riastrad if (min_pitch > UINT_MAX)
223 1.1 riastrad return -ERANGE;
224 1.1 riastrad
225 1.1 riastrad if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
226 1.1 riastrad return -ERANGE;
227 1.1 riastrad
228 1.1 riastrad if (block_size && r->pitches[i] < min_pitch) {
229 1.1 riastrad DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
230 1.1 riastrad return -EINVAL;
231 1.1 riastrad }
232 1.1 riastrad
233 1.1 riastrad if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
234 1.1 riastrad DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
235 1.1 riastrad r->modifier[i], i);
236 1.1 riastrad return -EINVAL;
237 1.1 riastrad }
238 1.1 riastrad
239 1.1 riastrad if (r->flags & DRM_MODE_FB_MODIFIERS &&
240 1.1 riastrad r->modifier[i] != r->modifier[0]) {
241 1.1 riastrad DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
242 1.1 riastrad r->modifier[i], i);
243 1.1 riastrad return -EINVAL;
244 1.1 riastrad }
245 1.1 riastrad
246 1.1 riastrad /* modifier specific checks: */
247 1.1 riastrad switch (r->modifier[i]) {
248 1.1 riastrad case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
249 1.1 riastrad /* NOTE: the pitch restriction may be lifted later if it turns
250 1.1 riastrad * out that no hw has this restriction:
251 1.1 riastrad */
252 1.1 riastrad if (r->pixel_format != DRM_FORMAT_NV12 ||
253 1.1 riastrad width % 128 || height % 32 ||
254 1.1 riastrad r->pitches[i] % 128) {
255 1.1 riastrad DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
256 1.1 riastrad return -EINVAL;
257 1.1 riastrad }
258 1.1 riastrad break;
259 1.1 riastrad
260 1.1 riastrad default:
261 1.1 riastrad break;
262 1.1 riastrad }
263 1.1 riastrad }
264 1.1 riastrad
265 1.1 riastrad for (i = info->num_planes; i < 4; i++) {
266 1.1 riastrad if (r->modifier[i]) {
267 1.1 riastrad DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
268 1.1 riastrad return -EINVAL;
269 1.1 riastrad }
270 1.1 riastrad
271 1.1 riastrad /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
272 1.1 riastrad if (!(r->flags & DRM_MODE_FB_MODIFIERS))
273 1.1 riastrad continue;
274 1.1 riastrad
275 1.1 riastrad if (r->handles[i]) {
276 1.1 riastrad DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
277 1.1 riastrad return -EINVAL;
278 1.1 riastrad }
279 1.1 riastrad
280 1.1 riastrad if (r->pitches[i]) {
281 1.1 riastrad DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
282 1.1 riastrad return -EINVAL;
283 1.1 riastrad }
284 1.1 riastrad
285 1.1 riastrad if (r->offsets[i]) {
286 1.1 riastrad DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
287 1.1 riastrad return -EINVAL;
288 1.1 riastrad }
289 1.1 riastrad }
290 1.1 riastrad
291 1.1 riastrad return 0;
292 1.1 riastrad }
293 1.1 riastrad
294 1.1 riastrad struct drm_framebuffer *
295 1.1 riastrad drm_internal_framebuffer_create(struct drm_device *dev,
296 1.1 riastrad const struct drm_mode_fb_cmd2 *r,
297 1.1 riastrad struct drm_file *file_priv)
298 1.1 riastrad {
299 1.1 riastrad struct drm_mode_config *config = &dev->mode_config;
300 1.1 riastrad struct drm_framebuffer *fb;
301 1.1 riastrad int ret;
302 1.1 riastrad
303 1.1 riastrad if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
304 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
305 1.1 riastrad return ERR_PTR(-EINVAL);
306 1.1 riastrad }
307 1.1 riastrad
308 1.1 riastrad if ((config->min_width > r->width) || (r->width > config->max_width)) {
309 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
310 1.1 riastrad r->width, config->min_width, config->max_width);
311 1.1 riastrad return ERR_PTR(-EINVAL);
312 1.1 riastrad }
313 1.1 riastrad if ((config->min_height > r->height) || (r->height > config->max_height)) {
314 1.1 riastrad DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
315 1.1 riastrad r->height, config->min_height, config->max_height);
316 1.1 riastrad return ERR_PTR(-EINVAL);
317 1.1 riastrad }
318 1.1 riastrad
319 1.1 riastrad if (r->flags & DRM_MODE_FB_MODIFIERS &&
320 1.1 riastrad !dev->mode_config.allow_fb_modifiers) {
321 1.1 riastrad DRM_DEBUG_KMS("driver does not support fb modifiers\n");
322 1.1 riastrad return ERR_PTR(-EINVAL);
323 1.1 riastrad }
324 1.1 riastrad
325 1.1 riastrad ret = framebuffer_check(dev, r);
326 1.1 riastrad if (ret)
327 1.1 riastrad return ERR_PTR(ret);
328 1.1 riastrad
329 1.1 riastrad fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
330 1.1 riastrad if (IS_ERR(fb)) {
331 1.1 riastrad DRM_DEBUG_KMS("could not create framebuffer\n");
332 1.1 riastrad return fb;
333 1.1 riastrad }
334 1.1 riastrad
335 1.1 riastrad return fb;
336 1.1 riastrad }
337 1.1 riastrad EXPORT_SYMBOL_FOR_TESTS_ONLY(drm_internal_framebuffer_create);
338 1.1 riastrad
339 1.1 riastrad /**
340 1.1 riastrad * drm_mode_addfb2 - add an FB to the graphics configuration
341 1.1 riastrad * @dev: drm device for the ioctl
342 1.1 riastrad * @data: data pointer for the ioctl
343 1.1 riastrad * @file_priv: drm file for the ioctl call
344 1.1 riastrad *
345 1.1 riastrad * Add a new FB to the specified CRTC, given a user request with format. This is
346 1.1 riastrad * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
347 1.1 riastrad * and uses fourcc codes as pixel format specifiers.
348 1.1 riastrad *
349 1.1 riastrad * Called by the user via ioctl.
350 1.1 riastrad *
351 1.1 riastrad * Returns:
352 1.1 riastrad * Zero on success, negative errno on failure.
353 1.1 riastrad */
354 1.1 riastrad int drm_mode_addfb2(struct drm_device *dev,
355 1.1 riastrad void *data, struct drm_file *file_priv)
356 1.1 riastrad {
357 1.1 riastrad struct drm_mode_fb_cmd2 *r = data;
358 1.1 riastrad struct drm_framebuffer *fb;
359 1.1 riastrad
360 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
361 1.1 riastrad return -EOPNOTSUPP;
362 1.1 riastrad
363 1.1 riastrad fb = drm_internal_framebuffer_create(dev, r, file_priv);
364 1.1 riastrad if (IS_ERR(fb))
365 1.1 riastrad return PTR_ERR(fb);
366 1.1 riastrad
367 1.1 riastrad DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
368 1.1 riastrad r->fb_id = fb->base.id;
369 1.1 riastrad
370 1.1 riastrad /* Transfer ownership to the filp for reaping on close */
371 1.1 riastrad mutex_lock(&file_priv->fbs_lock);
372 1.1 riastrad list_add(&fb->filp_head, &file_priv->fbs);
373 1.1 riastrad mutex_unlock(&file_priv->fbs_lock);
374 1.1 riastrad
375 1.1 riastrad return 0;
376 1.1 riastrad }
377 1.1 riastrad
378 1.1 riastrad int drm_mode_addfb2_ioctl(struct drm_device *dev,
379 1.1 riastrad void *data, struct drm_file *file_priv)
380 1.1 riastrad {
381 1.1 riastrad #ifdef __BIG_ENDIAN
382 1.1 riastrad if (!dev->mode_config.quirk_addfb_prefer_host_byte_order) {
383 1.1 riastrad /*
384 1.1 riastrad * Drivers must set the
385 1.1 riastrad * quirk_addfb_prefer_host_byte_order quirk to make
386 1.1 riastrad * the drm_mode_addfb() compat code work correctly on
387 1.1 riastrad * bigendian machines.
388 1.1 riastrad *
389 1.1 riastrad * If they don't they interpret pixel_format values
390 1.1 riastrad * incorrectly for bug compatibility, which in turn
391 1.1 riastrad * implies the ADDFB2 ioctl does not work correctly
392 1.1 riastrad * then. So block it to make userspace fallback to
393 1.1 riastrad * ADDFB.
394 1.1 riastrad */
395 1.1 riastrad DRM_DEBUG_KMS("addfb2 broken on bigendian");
396 1.1 riastrad return -EOPNOTSUPP;
397 1.1 riastrad }
398 1.1 riastrad #endif
399 1.1 riastrad return drm_mode_addfb2(dev, data, file_priv);
400 1.1 riastrad }
401 1.1 riastrad
402 1.1 riastrad struct drm_mode_rmfb_work {
403 1.1 riastrad struct work_struct work;
404 1.1 riastrad struct list_head fbs;
405 1.1 riastrad };
406 1.1 riastrad
407 1.1 riastrad static void drm_mode_rmfb_work_fn(struct work_struct *w)
408 1.1 riastrad {
409 1.1 riastrad struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
410 1.1 riastrad
411 1.1 riastrad while (!list_empty(&arg->fbs)) {
412 1.1 riastrad struct drm_framebuffer *fb =
413 1.1 riastrad list_first_entry(&arg->fbs, typeof(*fb), filp_head);
414 1.1 riastrad
415 1.1 riastrad list_del_init(&fb->filp_head);
416 1.1 riastrad drm_framebuffer_remove(fb);
417 1.1 riastrad }
418 1.1 riastrad }
419 1.1 riastrad
420 1.1 riastrad /**
421 1.1 riastrad * drm_mode_rmfb - remove an FB from the configuration
422 1.1 riastrad * @dev: drm device
423 1.1 riastrad * @fb_id: id of framebuffer to remove
424 1.1 riastrad * @file_priv: drm file
425 1.1 riastrad *
426 1.1 riastrad * Remove the specified FB.
427 1.1 riastrad *
428 1.1 riastrad * Called by the user via ioctl, or by an in-kernel client.
429 1.1 riastrad *
430 1.1 riastrad * Returns:
431 1.1 riastrad * Zero on success, negative errno on failure.
432 1.1 riastrad */
433 1.1 riastrad int drm_mode_rmfb(struct drm_device *dev, u32 fb_id,
434 1.1 riastrad struct drm_file *file_priv)
435 1.1 riastrad {
436 1.1 riastrad struct drm_framebuffer *fb = NULL;
437 1.1 riastrad struct drm_framebuffer *fbl = NULL;
438 1.1 riastrad int found = 0;
439 1.1 riastrad
440 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
441 1.1 riastrad return -EOPNOTSUPP;
442 1.1 riastrad
443 1.1 riastrad fb = drm_framebuffer_lookup(dev, file_priv, fb_id);
444 1.1 riastrad if (!fb)
445 1.1 riastrad return -ENOENT;
446 1.1 riastrad
447 1.1 riastrad mutex_lock(&file_priv->fbs_lock);
448 1.1 riastrad list_for_each_entry(fbl, &file_priv->fbs, filp_head)
449 1.1 riastrad if (fb == fbl)
450 1.1 riastrad found = 1;
451 1.1 riastrad if (!found) {
452 1.1 riastrad mutex_unlock(&file_priv->fbs_lock);
453 1.1 riastrad goto fail_unref;
454 1.1 riastrad }
455 1.1 riastrad
456 1.1 riastrad list_del_init(&fb->filp_head);
457 1.1 riastrad mutex_unlock(&file_priv->fbs_lock);
458 1.1 riastrad
459 1.1 riastrad /* drop the reference we picked up in framebuffer lookup */
460 1.1 riastrad drm_framebuffer_put(fb);
461 1.1 riastrad
462 1.1 riastrad /*
463 1.1 riastrad * we now own the reference that was stored in the fbs list
464 1.1 riastrad *
465 1.1 riastrad * drm_framebuffer_remove may fail with -EINTR on pending signals,
466 1.1 riastrad * so run this in a separate stack as there's no way to correctly
467 1.1 riastrad * handle this after the fb is already removed from the lookup table.
468 1.1 riastrad */
469 1.1 riastrad if (drm_framebuffer_read_refcount(fb) > 1) {
470 1.1 riastrad struct drm_mode_rmfb_work arg;
471 1.1 riastrad
472 1.1 riastrad INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
473 1.1 riastrad INIT_LIST_HEAD(&arg.fbs);
474 1.1 riastrad list_add_tail(&fb->filp_head, &arg.fbs);
475 1.1 riastrad
476 1.1 riastrad schedule_work(&arg.work);
477 1.1 riastrad flush_work(&arg.work);
478 1.1 riastrad destroy_work_on_stack(&arg.work);
479 1.1 riastrad } else
480 1.1 riastrad drm_framebuffer_put(fb);
481 1.1 riastrad
482 1.1 riastrad return 0;
483 1.1 riastrad
484 1.1 riastrad fail_unref:
485 1.1 riastrad drm_framebuffer_put(fb);
486 1.1 riastrad return -ENOENT;
487 1.1 riastrad }
488 1.1 riastrad
489 1.1 riastrad int drm_mode_rmfb_ioctl(struct drm_device *dev,
490 1.1 riastrad void *data, struct drm_file *file_priv)
491 1.1 riastrad {
492 1.1 riastrad uint32_t *fb_id = data;
493 1.1 riastrad
494 1.1 riastrad return drm_mode_rmfb(dev, *fb_id, file_priv);
495 1.1 riastrad }
496 1.1 riastrad
497 1.1 riastrad /**
498 1.1 riastrad * drm_mode_getfb - get FB info
499 1.1 riastrad * @dev: drm device for the ioctl
500 1.1 riastrad * @data: data pointer for the ioctl
501 1.1 riastrad * @file_priv: drm file for the ioctl call
502 1.1 riastrad *
503 1.1 riastrad * Lookup the FB given its ID and return info about it.
504 1.1 riastrad *
505 1.1 riastrad * Called by the user via ioctl.
506 1.1 riastrad *
507 1.1 riastrad * Returns:
508 1.1 riastrad * Zero on success, negative errno on failure.
509 1.1 riastrad */
510 1.1 riastrad int drm_mode_getfb(struct drm_device *dev,
511 1.1 riastrad void *data, struct drm_file *file_priv)
512 1.1 riastrad {
513 1.1 riastrad struct drm_mode_fb_cmd *r = data;
514 1.1 riastrad struct drm_framebuffer *fb;
515 1.1 riastrad int ret;
516 1.1 riastrad
517 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
518 1.1 riastrad return -EOPNOTSUPP;
519 1.1 riastrad
520 1.1 riastrad fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
521 1.1 riastrad if (!fb)
522 1.1 riastrad return -ENOENT;
523 1.1 riastrad
524 1.1 riastrad /* Multi-planar framebuffers need getfb2. */
525 1.1 riastrad if (fb->format->num_planes > 1) {
526 1.1 riastrad ret = -EINVAL;
527 1.1 riastrad goto out;
528 1.1 riastrad }
529 1.1 riastrad
530 1.1 riastrad if (!fb->funcs->create_handle) {
531 1.1 riastrad ret = -ENODEV;
532 1.1 riastrad goto out;
533 1.1 riastrad }
534 1.1 riastrad
535 1.1 riastrad r->height = fb->height;
536 1.1 riastrad r->width = fb->width;
537 1.1 riastrad r->depth = fb->format->depth;
538 1.1 riastrad r->bpp = fb->format->cpp[0] * 8;
539 1.1 riastrad r->pitch = fb->pitches[0];
540 1.1 riastrad
541 1.1 riastrad /* GET_FB() is an unprivileged ioctl so we must not return a
542 1.1 riastrad * buffer-handle to non-master processes! For
543 1.1 riastrad * backwards-compatibility reasons, we cannot make GET_FB() privileged,
544 1.1 riastrad * so just return an invalid handle for non-masters.
545 1.1 riastrad */
546 1.1 riastrad if (!drm_is_current_master(file_priv) && !capable(CAP_SYS_ADMIN)) {
547 1.1 riastrad r->handle = 0;
548 1.1 riastrad ret = 0;
549 1.1 riastrad goto out;
550 1.1 riastrad }
551 1.1 riastrad
552 1.1 riastrad ret = fb->funcs->create_handle(fb, file_priv, &r->handle);
553 1.1 riastrad
554 1.1 riastrad out:
555 1.1 riastrad drm_framebuffer_put(fb);
556 1.1 riastrad
557 1.1 riastrad return ret;
558 1.1 riastrad }
559 1.1 riastrad
560 1.1 riastrad /**
561 1.1 riastrad * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
562 1.1 riastrad * @dev: drm device for the ioctl
563 1.1 riastrad * @data: data pointer for the ioctl
564 1.1 riastrad * @file_priv: drm file for the ioctl call
565 1.1 riastrad *
566 1.1 riastrad * Lookup the FB and flush out the damaged area supplied by userspace as a clip
567 1.1 riastrad * rectangle list. Generic userspace which does frontbuffer rendering must call
568 1.1 riastrad * this ioctl to flush out the changes on manual-update display outputs, e.g.
569 1.1 riastrad * usb display-link, mipi manual update panels or edp panel self refresh modes.
570 1.1 riastrad *
571 1.1 riastrad * Modesetting drivers which always update the frontbuffer do not need to
572 1.1 riastrad * implement the corresponding &drm_framebuffer_funcs.dirty callback.
573 1.1 riastrad *
574 1.1 riastrad * Called by the user via ioctl.
575 1.1 riastrad *
576 1.1 riastrad * Returns:
577 1.1 riastrad * Zero on success, negative errno on failure.
578 1.1 riastrad */
579 1.1 riastrad int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
580 1.1 riastrad void *data, struct drm_file *file_priv)
581 1.1 riastrad {
582 1.1 riastrad struct drm_clip_rect __user *clips_ptr;
583 1.1 riastrad struct drm_clip_rect *clips = NULL;
584 1.1 riastrad struct drm_mode_fb_dirty_cmd *r = data;
585 1.1 riastrad struct drm_framebuffer *fb;
586 1.1 riastrad unsigned flags;
587 1.1 riastrad int num_clips;
588 1.1 riastrad int ret;
589 1.1 riastrad
590 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_MODESET))
591 1.1 riastrad return -EOPNOTSUPP;
592 1.1 riastrad
593 1.1 riastrad fb = drm_framebuffer_lookup(dev, file_priv, r->fb_id);
594 1.1 riastrad if (!fb)
595 1.1 riastrad return -ENOENT;
596 1.1 riastrad
597 1.1 riastrad num_clips = r->num_clips;
598 1.1 riastrad clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
599 1.1 riastrad
600 1.1 riastrad if (!num_clips != !clips_ptr) {
601 1.1 riastrad ret = -EINVAL;
602 1.1 riastrad goto out_err1;
603 1.1 riastrad }
604 1.1 riastrad
605 1.1 riastrad flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
606 1.1 riastrad
607 1.1 riastrad /* If userspace annotates copy, clips must come in pairs */
608 1.1 riastrad if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
609 1.1 riastrad ret = -EINVAL;
610 1.1 riastrad goto out_err1;
611 1.1 riastrad }
612 1.1 riastrad
613 1.1 riastrad if (num_clips && clips_ptr) {
614 1.1 riastrad if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
615 1.1 riastrad ret = -EINVAL;
616 1.1 riastrad goto out_err1;
617 1.1 riastrad }
618 1.1 riastrad clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
619 1.1 riastrad if (!clips) {
620 1.1 riastrad ret = -ENOMEM;
621 1.1 riastrad goto out_err1;
622 1.1 riastrad }
623 1.1 riastrad
624 1.1 riastrad ret = copy_from_user(clips, clips_ptr,
625 1.1 riastrad num_clips * sizeof(*clips));
626 1.1 riastrad if (ret) {
627 1.1 riastrad ret = -EFAULT;
628 1.1 riastrad goto out_err2;
629 1.1 riastrad }
630 1.1 riastrad }
631 1.1 riastrad
632 1.1 riastrad if (fb->funcs->dirty) {
633 1.1 riastrad ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
634 1.1 riastrad clips, num_clips);
635 1.1 riastrad } else {
636 1.1 riastrad ret = -ENOSYS;
637 1.1 riastrad }
638 1.1 riastrad
639 1.1 riastrad out_err2:
640 1.1 riastrad kfree(clips);
641 1.1 riastrad out_err1:
642 1.1 riastrad drm_framebuffer_put(fb);
643 1.1 riastrad
644 1.1 riastrad return ret;
645 1.1 riastrad }
646 1.1 riastrad
647 1.1 riastrad /**
648 1.1 riastrad * drm_fb_release - remove and free the FBs on this file
649 1.1 riastrad * @priv: drm file for the ioctl
650 1.1 riastrad *
651 1.1 riastrad * Destroy all the FBs associated with @filp.
652 1.1 riastrad *
653 1.1 riastrad * Called by the user via ioctl.
654 1.1 riastrad *
655 1.1 riastrad * Returns:
656 1.1 riastrad * Zero on success, negative errno on failure.
657 1.1 riastrad */
658 1.1 riastrad void drm_fb_release(struct drm_file *priv)
659 1.1 riastrad {
660 1.1 riastrad struct drm_framebuffer *fb, *tfb;
661 1.1 riastrad struct drm_mode_rmfb_work arg;
662 1.1 riastrad
663 1.1 riastrad INIT_LIST_HEAD(&arg.fbs);
664 1.1 riastrad
665 1.1 riastrad /*
666 1.1 riastrad * When the file gets released that means no one else can access the fb
667 1.1 riastrad * list any more, so no need to grab fpriv->fbs_lock. And we need to
668 1.1 riastrad * avoid upsetting lockdep since the universal cursor code adds a
669 1.1 riastrad * framebuffer while holding mutex locks.
670 1.1 riastrad *
671 1.1 riastrad * Note that a real deadlock between fpriv->fbs_lock and the modeset
672 1.1 riastrad * locks is impossible here since no one else but this function can get
673 1.1 riastrad * at it any more.
674 1.1 riastrad */
675 1.1 riastrad list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
676 1.1 riastrad if (drm_framebuffer_read_refcount(fb) > 1) {
677 1.1 riastrad list_move_tail(&fb->filp_head, &arg.fbs);
678 1.1 riastrad } else {
679 1.1 riastrad list_del_init(&fb->filp_head);
680 1.1 riastrad
681 1.1 riastrad /* This drops the fpriv->fbs reference. */
682 1.1 riastrad drm_framebuffer_put(fb);
683 1.1 riastrad }
684 1.1 riastrad }
685 1.1 riastrad
686 1.1 riastrad if (!list_empty(&arg.fbs)) {
687 1.1 riastrad INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
688 1.1 riastrad
689 1.1 riastrad schedule_work(&arg.work);
690 1.1 riastrad flush_work(&arg.work);
691 1.1 riastrad destroy_work_on_stack(&arg.work);
692 1.1 riastrad }
693 1.1 riastrad }
694 1.1 riastrad
695 1.1 riastrad void drm_framebuffer_free(struct kref *kref)
696 1.1 riastrad {
697 1.1 riastrad struct drm_framebuffer *fb =
698 1.1 riastrad container_of(kref, struct drm_framebuffer, base.refcount);
699 1.1 riastrad struct drm_device *dev = fb->dev;
700 1.1 riastrad
701 1.1 riastrad /*
702 1.1 riastrad * The lookup idr holds a weak reference, which has not necessarily been
703 1.1 riastrad * removed at this point. Check for that.
704 1.1 riastrad */
705 1.1 riastrad drm_mode_object_unregister(dev, &fb->base);
706 1.1 riastrad
707 1.1 riastrad fb->funcs->destroy(fb);
708 1.1 riastrad }
709 1.1 riastrad
710 1.1 riastrad /**
711 1.1 riastrad * drm_framebuffer_init - initialize a framebuffer
712 1.1 riastrad * @dev: DRM device
713 1.1 riastrad * @fb: framebuffer to be initialized
714 1.1 riastrad * @funcs: ... with these functions
715 1.1 riastrad *
716 1.1 riastrad * Allocates an ID for the framebuffer's parent mode object, sets its mode
717 1.1 riastrad * functions & device file and adds it to the master fd list.
718 1.1 riastrad *
719 1.1 riastrad * IMPORTANT:
720 1.1 riastrad * This functions publishes the fb and makes it available for concurrent access
721 1.1 riastrad * by other users. Which means by this point the fb _must_ be fully set up -
722 1.1 riastrad * since all the fb attributes are invariant over its lifetime, no further
723 1.1 riastrad * locking but only correct reference counting is required.
724 1.1 riastrad *
725 1.1 riastrad * Returns:
726 1.1 riastrad * Zero on success, error code on failure.
727 1.1 riastrad */
728 1.1 riastrad int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
729 1.1 riastrad const struct drm_framebuffer_funcs *funcs)
730 1.1 riastrad {
731 1.1 riastrad int ret;
732 1.1 riastrad
733 1.1 riastrad if (WARN_ON_ONCE(fb->dev != dev || !fb->format))
734 1.1 riastrad return -EINVAL;
735 1.1 riastrad
736 1.1 riastrad INIT_LIST_HEAD(&fb->filp_head);
737 1.1 riastrad
738 1.1 riastrad fb->funcs = funcs;
739 1.3 riastrad #ifdef __NetBSD__
740 1.3 riastrad strlcpy(fb->comm, curproc->p_comm, sizeof fb->comm);
741 1.3 riastrad #else
742 1.1 riastrad strcpy(fb->comm, current->comm);
743 1.3 riastrad #endif
744 1.1 riastrad
745 1.1 riastrad ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB,
746 1.1 riastrad false, drm_framebuffer_free);
747 1.1 riastrad if (ret)
748 1.1 riastrad goto out;
749 1.1 riastrad
750 1.1 riastrad mutex_lock(&dev->mode_config.fb_lock);
751 1.1 riastrad dev->mode_config.num_fb++;
752 1.1 riastrad list_add(&fb->head, &dev->mode_config.fb_list);
753 1.1 riastrad mutex_unlock(&dev->mode_config.fb_lock);
754 1.1 riastrad
755 1.1 riastrad drm_mode_object_register(dev, &fb->base);
756 1.1 riastrad out:
757 1.1 riastrad return ret;
758 1.1 riastrad }
759 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_init);
760 1.1 riastrad
761 1.1 riastrad /**
762 1.1 riastrad * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
763 1.1 riastrad * @dev: drm device
764 1.1 riastrad * @file_priv: drm file to check for lease against.
765 1.1 riastrad * @id: id of the fb object
766 1.1 riastrad *
767 1.1 riastrad * If successful, this grabs an additional reference to the framebuffer -
768 1.1 riastrad * callers need to make sure to eventually unreference the returned framebuffer
769 1.1 riastrad * again, using drm_framebuffer_put().
770 1.1 riastrad */
771 1.1 riastrad struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
772 1.1 riastrad struct drm_file *file_priv,
773 1.1 riastrad uint32_t id)
774 1.1 riastrad {
775 1.1 riastrad struct drm_mode_object *obj;
776 1.1 riastrad struct drm_framebuffer *fb = NULL;
777 1.1 riastrad
778 1.1 riastrad obj = __drm_mode_object_find(dev, file_priv, id, DRM_MODE_OBJECT_FB);
779 1.1 riastrad if (obj)
780 1.1 riastrad fb = obj_to_fb(obj);
781 1.1 riastrad return fb;
782 1.1 riastrad }
783 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_lookup);
784 1.1 riastrad
785 1.1 riastrad /**
786 1.1 riastrad * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
787 1.1 riastrad * @fb: fb to unregister
788 1.1 riastrad *
789 1.1 riastrad * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
790 1.1 riastrad * those used for fbdev. Note that the caller must hold a reference of its own,
791 1.1 riastrad * i.e. the object may not be destroyed through this call (since it'll lead to a
792 1.1 riastrad * locking inversion).
793 1.1 riastrad *
794 1.1 riastrad * NOTE: This function is deprecated. For driver-private framebuffers it is not
795 1.1 riastrad * recommended to embed a framebuffer struct info fbdev struct, instead, a
796 1.1 riastrad * framebuffer pointer is preferred and drm_framebuffer_put() should be called
797 1.1 riastrad * when the framebuffer is to be cleaned up.
798 1.1 riastrad */
799 1.1 riastrad void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
800 1.1 riastrad {
801 1.1 riastrad struct drm_device *dev;
802 1.1 riastrad
803 1.1 riastrad if (!fb)
804 1.1 riastrad return;
805 1.1 riastrad
806 1.1 riastrad dev = fb->dev;
807 1.1 riastrad
808 1.1 riastrad /* Mark fb as reaped and drop idr ref. */
809 1.1 riastrad drm_mode_object_unregister(dev, &fb->base);
810 1.1 riastrad }
811 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_unregister_private);
812 1.1 riastrad
813 1.1 riastrad /**
814 1.1 riastrad * drm_framebuffer_cleanup - remove a framebuffer object
815 1.1 riastrad * @fb: framebuffer to remove
816 1.1 riastrad *
817 1.1 riastrad * Cleanup framebuffer. This function is intended to be used from the drivers
818 1.1 riastrad * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up
819 1.1 riastrad * driver private framebuffers embedded into a larger structure.
820 1.1 riastrad *
821 1.1 riastrad * Note that this function does not remove the fb from active usage - if it is
822 1.1 riastrad * still used anywhere, hilarity can ensue since userspace could call getfb on
823 1.1 riastrad * the id and get back -EINVAL. Obviously no concern at driver unload time.
824 1.1 riastrad *
825 1.1 riastrad * Also, the framebuffer will not be removed from the lookup idr - for
826 1.1 riastrad * user-created framebuffers this will happen in in the rmfb ioctl. For
827 1.1 riastrad * driver-private objects (e.g. for fbdev) drivers need to explicitly call
828 1.1 riastrad * drm_framebuffer_unregister_private.
829 1.1 riastrad */
830 1.1 riastrad void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
831 1.1 riastrad {
832 1.1 riastrad struct drm_device *dev = fb->dev;
833 1.1 riastrad
834 1.1 riastrad mutex_lock(&dev->mode_config.fb_lock);
835 1.1 riastrad list_del(&fb->head);
836 1.1 riastrad dev->mode_config.num_fb--;
837 1.1 riastrad mutex_unlock(&dev->mode_config.fb_lock);
838 1.1 riastrad }
839 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_cleanup);
840 1.1 riastrad
841 1.1 riastrad static int atomic_remove_fb(struct drm_framebuffer *fb)
842 1.1 riastrad {
843 1.1 riastrad struct drm_modeset_acquire_ctx ctx;
844 1.1 riastrad struct drm_device *dev = fb->dev;
845 1.1 riastrad struct drm_atomic_state *state;
846 1.1 riastrad struct drm_plane *plane;
847 1.1 riastrad struct drm_connector *conn __maybe_unused;
848 1.1 riastrad struct drm_connector_state *conn_state;
849 1.1 riastrad int i, ret;
850 1.1 riastrad unsigned plane_mask;
851 1.1 riastrad bool disable_crtcs = false;
852 1.1 riastrad
853 1.1 riastrad retry_disable:
854 1.1 riastrad drm_modeset_acquire_init(&ctx, 0);
855 1.1 riastrad
856 1.1 riastrad state = drm_atomic_state_alloc(dev);
857 1.1 riastrad if (!state) {
858 1.1 riastrad ret = -ENOMEM;
859 1.1 riastrad goto out;
860 1.1 riastrad }
861 1.1 riastrad state->acquire_ctx = &ctx;
862 1.1 riastrad
863 1.1 riastrad retry:
864 1.1 riastrad plane_mask = 0;
865 1.1 riastrad ret = drm_modeset_lock_all_ctx(dev, &ctx);
866 1.1 riastrad if (ret)
867 1.1 riastrad goto unlock;
868 1.1 riastrad
869 1.1 riastrad drm_for_each_plane(plane, dev) {
870 1.1 riastrad struct drm_plane_state *plane_state;
871 1.1 riastrad
872 1.1 riastrad if (plane->state->fb != fb)
873 1.1 riastrad continue;
874 1.1 riastrad
875 1.1 riastrad plane_state = drm_atomic_get_plane_state(state, plane);
876 1.1 riastrad if (IS_ERR(plane_state)) {
877 1.1 riastrad ret = PTR_ERR(plane_state);
878 1.1 riastrad goto unlock;
879 1.1 riastrad }
880 1.1 riastrad
881 1.1 riastrad if (disable_crtcs && plane_state->crtc->primary == plane) {
882 1.1 riastrad struct drm_crtc_state *crtc_state;
883 1.1 riastrad
884 1.1 riastrad crtc_state = drm_atomic_get_existing_crtc_state(state, plane_state->crtc);
885 1.1 riastrad
886 1.1 riastrad ret = drm_atomic_add_affected_connectors(state, plane_state->crtc);
887 1.1 riastrad if (ret)
888 1.1 riastrad goto unlock;
889 1.1 riastrad
890 1.1 riastrad crtc_state->active = false;
891 1.1 riastrad ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
892 1.1 riastrad if (ret)
893 1.1 riastrad goto unlock;
894 1.1 riastrad }
895 1.1 riastrad
896 1.1 riastrad drm_atomic_set_fb_for_plane(plane_state, NULL);
897 1.1 riastrad ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
898 1.1 riastrad if (ret)
899 1.1 riastrad goto unlock;
900 1.1 riastrad
901 1.1 riastrad plane_mask |= drm_plane_mask(plane);
902 1.1 riastrad }
903 1.1 riastrad
904 1.1 riastrad /* This list is only filled when disable_crtcs is set. */
905 1.1 riastrad for_each_new_connector_in_state(state, conn, conn_state, i) {
906 1.1 riastrad ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
907 1.1 riastrad
908 1.1 riastrad if (ret)
909 1.1 riastrad goto unlock;
910 1.1 riastrad }
911 1.1 riastrad
912 1.1 riastrad if (plane_mask)
913 1.1 riastrad ret = drm_atomic_commit(state);
914 1.1 riastrad
915 1.1 riastrad unlock:
916 1.1 riastrad if (ret == -EDEADLK) {
917 1.1 riastrad drm_atomic_state_clear(state);
918 1.1 riastrad drm_modeset_backoff(&ctx);
919 1.1 riastrad goto retry;
920 1.1 riastrad }
921 1.1 riastrad
922 1.1 riastrad drm_atomic_state_put(state);
923 1.1 riastrad
924 1.1 riastrad out:
925 1.1 riastrad drm_modeset_drop_locks(&ctx);
926 1.1 riastrad drm_modeset_acquire_fini(&ctx);
927 1.1 riastrad
928 1.1 riastrad if (ret == -EINVAL && !disable_crtcs) {
929 1.1 riastrad disable_crtcs = true;
930 1.1 riastrad goto retry_disable;
931 1.1 riastrad }
932 1.1 riastrad
933 1.1 riastrad return ret;
934 1.1 riastrad }
935 1.1 riastrad
936 1.1 riastrad static void legacy_remove_fb(struct drm_framebuffer *fb)
937 1.1 riastrad {
938 1.1 riastrad struct drm_device *dev = fb->dev;
939 1.1 riastrad struct drm_crtc *crtc;
940 1.1 riastrad struct drm_plane *plane;
941 1.1 riastrad
942 1.1 riastrad drm_modeset_lock_all(dev);
943 1.1 riastrad /* remove from any CRTC */
944 1.1 riastrad drm_for_each_crtc(crtc, dev) {
945 1.1 riastrad if (crtc->primary->fb == fb) {
946 1.1 riastrad /* should turn off the crtc */
947 1.1 riastrad if (drm_crtc_force_disable(crtc))
948 1.1 riastrad DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
949 1.1 riastrad }
950 1.1 riastrad }
951 1.1 riastrad
952 1.1 riastrad drm_for_each_plane(plane, dev) {
953 1.1 riastrad if (plane->fb == fb)
954 1.1 riastrad drm_plane_force_disable(plane);
955 1.1 riastrad }
956 1.1 riastrad drm_modeset_unlock_all(dev);
957 1.1 riastrad }
958 1.1 riastrad
959 1.1 riastrad /**
960 1.1 riastrad * drm_framebuffer_remove - remove and unreference a framebuffer object
961 1.1 riastrad * @fb: framebuffer to remove
962 1.1 riastrad *
963 1.1 riastrad * Scans all the CRTCs and planes in @dev's mode_config. If they're
964 1.1 riastrad * using @fb, removes it, setting it to NULL. Then drops the reference to the
965 1.1 riastrad * passed-in framebuffer. Might take the modeset locks.
966 1.1 riastrad *
967 1.1 riastrad * Note that this function optimizes the cleanup away if the caller holds the
968 1.1 riastrad * last reference to the framebuffer. It is also guaranteed to not take the
969 1.1 riastrad * modeset locks in this case.
970 1.1 riastrad */
971 1.1 riastrad void drm_framebuffer_remove(struct drm_framebuffer *fb)
972 1.1 riastrad {
973 1.1 riastrad struct drm_device *dev;
974 1.1 riastrad
975 1.1 riastrad if (!fb)
976 1.1 riastrad return;
977 1.1 riastrad
978 1.1 riastrad dev = fb->dev;
979 1.1 riastrad
980 1.1 riastrad WARN_ON(!list_empty(&fb->filp_head));
981 1.1 riastrad
982 1.1 riastrad /*
983 1.1 riastrad * drm ABI mandates that we remove any deleted framebuffers from active
984 1.1 riastrad * useage. But since most sane clients only remove framebuffers they no
985 1.1 riastrad * longer need, try to optimize this away.
986 1.1 riastrad *
987 1.1 riastrad * Since we're holding a reference ourselves, observing a refcount of 1
988 1.1 riastrad * means that we're the last holder and can skip it. Also, the refcount
989 1.1 riastrad * can never increase from 1 again, so we don't need any barriers or
990 1.1 riastrad * locks.
991 1.1 riastrad *
992 1.1 riastrad * Note that userspace could try to race with use and instate a new
993 1.1 riastrad * usage _after_ we've cleared all current ones. End result will be an
994 1.1 riastrad * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
995 1.1 riastrad * in this manner.
996 1.1 riastrad */
997 1.1 riastrad if (drm_framebuffer_read_refcount(fb) > 1) {
998 1.1 riastrad if (drm_drv_uses_atomic_modeset(dev)) {
999 1.1 riastrad int ret = atomic_remove_fb(fb);
1000 1.1 riastrad WARN(ret, "atomic remove_fb failed with %i\n", ret);
1001 1.1 riastrad } else
1002 1.1 riastrad legacy_remove_fb(fb);
1003 1.1 riastrad }
1004 1.1 riastrad
1005 1.1 riastrad drm_framebuffer_put(fb);
1006 1.1 riastrad }
1007 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_remove);
1008 1.1 riastrad
1009 1.1 riastrad /**
1010 1.1 riastrad * drm_framebuffer_plane_width - width of the plane given the first plane
1011 1.1 riastrad * @width: width of the first plane
1012 1.1 riastrad * @fb: the framebuffer
1013 1.1 riastrad * @plane: plane index
1014 1.1 riastrad *
1015 1.1 riastrad * Returns:
1016 1.1 riastrad * The width of @plane, given that the width of the first plane is @width.
1017 1.1 riastrad */
1018 1.1 riastrad int drm_framebuffer_plane_width(int width,
1019 1.1 riastrad const struct drm_framebuffer *fb, int plane)
1020 1.1 riastrad {
1021 1.1 riastrad if (plane >= fb->format->num_planes)
1022 1.1 riastrad return 0;
1023 1.1 riastrad
1024 1.1 riastrad return fb_plane_width(width, fb->format, plane);
1025 1.1 riastrad }
1026 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_plane_width);
1027 1.1 riastrad
1028 1.1 riastrad /**
1029 1.1 riastrad * drm_framebuffer_plane_height - height of the plane given the first plane
1030 1.1 riastrad * @height: height of the first plane
1031 1.1 riastrad * @fb: the framebuffer
1032 1.1 riastrad * @plane: plane index
1033 1.1 riastrad *
1034 1.1 riastrad * Returns:
1035 1.1 riastrad * The height of @plane, given that the height of the first plane is @height.
1036 1.1 riastrad */
1037 1.1 riastrad int drm_framebuffer_plane_height(int height,
1038 1.1 riastrad const struct drm_framebuffer *fb, int plane)
1039 1.1 riastrad {
1040 1.1 riastrad if (plane >= fb->format->num_planes)
1041 1.1 riastrad return 0;
1042 1.1 riastrad
1043 1.1 riastrad return fb_plane_height(height, fb->format, plane);
1044 1.1 riastrad }
1045 1.1 riastrad EXPORT_SYMBOL(drm_framebuffer_plane_height);
1046 1.1 riastrad
1047 1.1 riastrad void drm_framebuffer_print_info(struct drm_printer *p, unsigned int indent,
1048 1.1 riastrad const struct drm_framebuffer *fb)
1049 1.1 riastrad {
1050 1.1 riastrad struct drm_format_name_buf format_name;
1051 1.1 riastrad unsigned int i;
1052 1.1 riastrad
1053 1.1 riastrad drm_printf_indent(p, indent, "allocated by = %s\n", fb->comm);
1054 1.1 riastrad drm_printf_indent(p, indent, "refcount=%u\n",
1055 1.1 riastrad drm_framebuffer_read_refcount(fb));
1056 1.1 riastrad drm_printf_indent(p, indent, "format=%s\n",
1057 1.1 riastrad drm_get_format_name(fb->format->format, &format_name));
1058 1.4 riastrad drm_printf_indent(p, indent, "modifier=0x%"PRIx64"\n", fb->modifier);
1059 1.1 riastrad drm_printf_indent(p, indent, "size=%ux%u\n", fb->width, fb->height);
1060 1.1 riastrad drm_printf_indent(p, indent, "layers:\n");
1061 1.1 riastrad
1062 1.1 riastrad for (i = 0; i < fb->format->num_planes; i++) {
1063 1.1 riastrad drm_printf_indent(p, indent + 1, "size[%u]=%dx%d\n", i,
1064 1.1 riastrad drm_framebuffer_plane_width(fb->width, fb, i),
1065 1.1 riastrad drm_framebuffer_plane_height(fb->height, fb, i));
1066 1.1 riastrad drm_printf_indent(p, indent + 1, "pitch[%u]=%u\n", i, fb->pitches[i]);
1067 1.1 riastrad drm_printf_indent(p, indent + 1, "offset[%u]=%u\n", i, fb->offsets[i]);
1068 1.1 riastrad drm_printf_indent(p, indent + 1, "obj[%u]:%s\n", i,
1069 1.1 riastrad fb->obj[i] ? "" : "(null)");
1070 1.1 riastrad if (fb->obj[i])
1071 1.1 riastrad drm_gem_print_info(p, indent + 2, fb->obj[i]);
1072 1.1 riastrad }
1073 1.1 riastrad }
1074 1.1 riastrad
1075 1.1 riastrad #ifdef CONFIG_DEBUG_FS
1076 1.1 riastrad static int drm_framebuffer_info(struct seq_file *m, void *data)
1077 1.1 riastrad {
1078 1.1 riastrad struct drm_info_node *node = m->private;
1079 1.1 riastrad struct drm_device *dev = node->minor->dev;
1080 1.1 riastrad struct drm_printer p = drm_seq_file_printer(m);
1081 1.1 riastrad struct drm_framebuffer *fb;
1082 1.1 riastrad
1083 1.1 riastrad mutex_lock(&dev->mode_config.fb_lock);
1084 1.1 riastrad drm_for_each_fb(fb, dev) {
1085 1.1 riastrad drm_printf(&p, "framebuffer[%u]:\n", fb->base.id);
1086 1.1 riastrad drm_framebuffer_print_info(&p, 1, fb);
1087 1.1 riastrad }
1088 1.1 riastrad mutex_unlock(&dev->mode_config.fb_lock);
1089 1.1 riastrad
1090 1.1 riastrad return 0;
1091 1.1 riastrad }
1092 1.1 riastrad
1093 1.1 riastrad static const struct drm_info_list drm_framebuffer_debugfs_list[] = {
1094 1.1 riastrad { "framebuffer", drm_framebuffer_info, 0 },
1095 1.1 riastrad };
1096 1.1 riastrad
1097 1.1 riastrad int drm_framebuffer_debugfs_init(struct drm_minor *minor)
1098 1.1 riastrad {
1099 1.1 riastrad return drm_debugfs_create_files(drm_framebuffer_debugfs_list,
1100 1.1 riastrad ARRAY_SIZE(drm_framebuffer_debugfs_list),
1101 1.1 riastrad minor->debugfs_root, minor);
1102 1.1 riastrad }
1103 1.1 riastrad #endif
1104