blob: 2305e7800b45ac22ca4965bff5e92db88bdd6e94 [file] [log] [blame]
Daniel Vetter7520a272016-08-15 16:07:02 +02001/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include <linux/export.h>
24#include <drm/drmP.h>
25#include <drm/drm_auth.h>
26#include <drm/drm_framebuffer.h>
Daniel Vetter941b8ca2017-04-03 10:33:03 +020027#include <drm/drm_atomic.h>
Daniel Vetter7520a272016-08-15 16:07:02 +020028
29#include "drm_crtc_internal.h"
30
31/**
Daniel Vetter750fb8c2016-08-12 22:48:48 +020032 * DOC: overview
33 *
34 * Frame buffers are abstract memory objects that provide a source of pixels to
35 * scanout to a CRTC. Applications explicitly request the creation of frame
36 * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque
37 * handle that can be passed to the KMS CRTC control, plane configuration and
38 * page flip functions.
39 *
40 * Frame buffers rely on the underlying memory manager for allocating backing
41 * storage. When creating a frame buffer applications pass a memory handle
42 * (or a list of memory handles for multi-planar formats) through the
Daniel Vetterea0dd852016-12-29 21:48:26 +010043 * &struct drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace
Daniel Vetter750fb8c2016-08-12 22:48:48 +020044 * buffer management interface this would be a GEM handle. Drivers are however
45 * free to use their own backing storage object handles, e.g. vmwgfx directly
46 * exposes special TTM handles to userspace and so expects TTM handles in the
47 * create ioctl and not GEM handles.
48 *
Daniel Vetterea0dd852016-12-29 21:48:26 +010049 * Framebuffers are tracked with &struct drm_framebuffer. They are published
Daniel Vetter750fb8c2016-08-12 22:48:48 +020050 * using drm_framebuffer_init() - after calling that function userspace can use
51 * and access the framebuffer object. The helper function
52 * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required
53 * metadata fields.
54 *
55 * The lifetime of a drm framebuffer is controlled with a reference count,
Thierry Redinga4a69da2017-02-28 15:46:40 +010056 * drivers can grab additional references with drm_framebuffer_get() and drop
57 * them again with drm_framebuffer_put(). For driver-private framebuffers for
58 * which the last reference is never dropped (e.g. for the fbdev framebuffer
59 * when the struct &struct drm_framebuffer is embedded into the fbdev helper
60 * struct) drivers can manually clean up a framebuffer at module unload time
61 * with drm_framebuffer_unregister_private(). But doing this is not
62 * recommended, and it's better to have a normal free-standing &struct
Daniel Vetterd5745282017-01-25 07:26:45 +010063 * drm_framebuffer.
Daniel Vetter750fb8c2016-08-12 22:48:48 +020064 */
65
Daniel Vetter43968d72016-09-21 10:59:24 +020066int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y,
67 uint32_t src_w, uint32_t src_h,
68 const struct drm_framebuffer *fb)
69{
70 unsigned int fb_width, fb_height;
71
72 fb_width = fb->width << 16;
73 fb_height = fb->height << 16;
74
75 /* Make sure source coordinates are inside the fb. */
76 if (src_w > fb_width ||
77 src_x > fb_width - src_w ||
78 src_h > fb_height ||
79 src_y > fb_height - src_h) {
80 DRM_DEBUG_KMS("Invalid source coordinates "
Ville Syrjälä0338f0d2017-11-01 20:35:33 +020081 "%u.%06ux%u.%06u+%u.%06u+%u.%06u (fb %ux%u)\n",
Daniel Vetter43968d72016-09-21 10:59:24 +020082 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
83 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
84 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
Ville Syrjälä0338f0d2017-11-01 20:35:33 +020085 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10,
86 fb->width, fb->height);
Daniel Vetter43968d72016-09-21 10:59:24 +020087 return -ENOSPC;
88 }
89
90 return 0;
91}
92
Daniel Vetter750fb8c2016-08-12 22:48:48 +020093/**
Daniel Vetter7520a272016-08-15 16:07:02 +020094 * drm_mode_addfb - add an FB to the graphics configuration
95 * @dev: drm device for the ioctl
96 * @data: data pointer for the ioctl
97 * @file_priv: drm file for the ioctl call
98 *
99 * Add a new FB to the specified CRTC, given a user request. This is the
100 * original addfb ioctl which only supported RGB formats.
101 *
102 * Called by the user via ioctl.
103 *
104 * Returns:
105 * Zero on success, negative errno on failure.
106 */
107int drm_mode_addfb(struct drm_device *dev,
108 void *data, struct drm_file *file_priv)
109{
110 struct drm_mode_fb_cmd *or = data;
111 struct drm_mode_fb_cmd2 r = {};
112 int ret;
113
114 /* convert to new format and call new ioctl */
115 r.fb_id = or->fb_id;
116 r.width = or->width;
117 r.height = or->height;
118 r.pitches[0] = or->pitch;
119 r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
120 r.handles[0] = or->handle;
121
122 ret = drm_mode_addfb2(dev, &r, file_priv);
123 if (ret)
124 return ret;
125
126 or->fb_id = r.fb_id;
127
128 return 0;
129}
130
Ville Syrjälä568c5e42017-03-21 20:12:14 +0200131static int fb_plane_width(int width,
132 const struct drm_format_info *format, int plane)
133{
134 if (plane == 0)
135 return width;
136
Ville Syrjälä33f673a2017-03-21 20:12:15 +0200137 return DIV_ROUND_UP(width, format->hsub);
Ville Syrjälä568c5e42017-03-21 20:12:14 +0200138}
139
140static int fb_plane_height(int height,
141 const struct drm_format_info *format, int plane)
142{
143 if (plane == 0)
144 return height;
145
Ville Syrjälä33f673a2017-03-21 20:12:15 +0200146 return DIV_ROUND_UP(height, format->vsub);
Ville Syrjälä568c5e42017-03-21 20:12:14 +0200147}
148
Ville Syrjälä6a0f9eb2017-03-21 20:12:16 +0200149static int framebuffer_check(struct drm_device *dev,
150 const struct drm_mode_fb_cmd2 *r)
Daniel Vetter7520a272016-08-15 16:07:02 +0200151{
Laurent Pinchartd5493492016-10-18 01:41:11 +0300152 const struct drm_format_info *info;
153 int i;
Daniel Vetter7520a272016-08-15 16:07:02 +0200154
Ville Syrjälä6a0f9eb2017-03-21 20:12:16 +0200155 /* check if the format is supported at all */
Laurent Pinchart333d2da2016-10-18 01:41:12 +0300156 info = __drm_format_info(r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN);
Laurent Pinchartd5493492016-10-18 01:41:11 +0300157 if (!info) {
Eric Engestromb3c11ac2016-11-12 01:12:56 +0000158 struct drm_format_name_buf format_name;
159 DRM_DEBUG_KMS("bad framebuffer format %s\n",
160 drm_get_format_name(r->pixel_format,
161 &format_name));
Laurent Pinchartd5493492016-10-18 01:41:11 +0300162 return -EINVAL;
Daniel Vetter7520a272016-08-15 16:07:02 +0200163 }
164
Ville Syrjälä6a0f9eb2017-03-21 20:12:16 +0200165 /* now let the driver pick its own format info */
166 info = drm_get_format_info(dev, r);
167
Ville Syrjälä33f673a2017-03-21 20:12:15 +0200168 if (r->width == 0) {
Daniel Vetter7520a272016-08-15 16:07:02 +0200169 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
170 return -EINVAL;
171 }
172
Ville Syrjälä33f673a2017-03-21 20:12:15 +0200173 if (r->height == 0) {
Daniel Vetter7520a272016-08-15 16:07:02 +0200174 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
175 return -EINVAL;
176 }
177
Laurent Pinchartd5493492016-10-18 01:41:11 +0300178 for (i = 0; i < info->num_planes; i++) {
Ville Syrjälä568c5e42017-03-21 20:12:14 +0200179 unsigned int width = fb_plane_width(r->width, info, i);
180 unsigned int height = fb_plane_height(r->height, info, i);
Laurent Pinchartd5493492016-10-18 01:41:11 +0300181 unsigned int cpp = info->cpp[i];
Daniel Vetter7520a272016-08-15 16:07:02 +0200182
183 if (!r->handles[i]) {
184 DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
185 return -EINVAL;
186 }
187
188 if ((uint64_t) width * cpp > UINT_MAX)
189 return -ERANGE;
190
191 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
192 return -ERANGE;
193
194 if (r->pitches[i] < width * cpp) {
195 DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
196 return -EINVAL;
197 }
198
199 if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
200 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
201 r->modifier[i], i);
202 return -EINVAL;
203 }
204
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200205 if (r->flags & DRM_MODE_FB_MODIFIERS &&
206 r->modifier[i] != r->modifier[0]) {
207 DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
208 r->modifier[i], i);
209 return -EINVAL;
210 }
211
Daniel Vetter7520a272016-08-15 16:07:02 +0200212 /* modifier specific checks: */
213 switch (r->modifier[i]) {
214 case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
215 /* NOTE: the pitch restriction may be lifted later if it turns
216 * out that no hw has this restriction:
217 */
218 if (r->pixel_format != DRM_FORMAT_NV12 ||
219 width % 128 || height % 32 ||
220 r->pitches[i] % 128) {
221 DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
222 return -EINVAL;
223 }
224 break;
225
226 default:
227 break;
228 }
229 }
230
Laurent Pinchartd5493492016-10-18 01:41:11 +0300231 for (i = info->num_planes; i < 4; i++) {
Daniel Vetter7520a272016-08-15 16:07:02 +0200232 if (r->modifier[i]) {
233 DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
234 return -EINVAL;
235 }
236
237 /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
238 if (!(r->flags & DRM_MODE_FB_MODIFIERS))
239 continue;
240
241 if (r->handles[i]) {
242 DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
243 return -EINVAL;
244 }
245
246 if (r->pitches[i]) {
247 DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
248 return -EINVAL;
249 }
250
251 if (r->offsets[i]) {
252 DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
253 return -EINVAL;
254 }
255 }
256
257 return 0;
258}
259
260struct drm_framebuffer *
261drm_internal_framebuffer_create(struct drm_device *dev,
262 const struct drm_mode_fb_cmd2 *r,
263 struct drm_file *file_priv)
264{
265 struct drm_mode_config *config = &dev->mode_config;
266 struct drm_framebuffer *fb;
267 int ret;
268
269 if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
270 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
271 return ERR_PTR(-EINVAL);
272 }
273
274 if ((config->min_width > r->width) || (r->width > config->max_width)) {
275 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
276 r->width, config->min_width, config->max_width);
277 return ERR_PTR(-EINVAL);
278 }
279 if ((config->min_height > r->height) || (r->height > config->max_height)) {
280 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
281 r->height, config->min_height, config->max_height);
282 return ERR_PTR(-EINVAL);
283 }
284
285 if (r->flags & DRM_MODE_FB_MODIFIERS &&
286 !dev->mode_config.allow_fb_modifiers) {
287 DRM_DEBUG_KMS("driver does not support fb modifiers\n");
288 return ERR_PTR(-EINVAL);
289 }
290
Ville Syrjälä6a0f9eb2017-03-21 20:12:16 +0200291 ret = framebuffer_check(dev, r);
Daniel Vetter7520a272016-08-15 16:07:02 +0200292 if (ret)
293 return ERR_PTR(ret);
294
295 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
296 if (IS_ERR(fb)) {
297 DRM_DEBUG_KMS("could not create framebuffer\n");
298 return fb;
299 }
300
301 return fb;
302}
303
304/**
305 * drm_mode_addfb2 - add an FB to the graphics configuration
306 * @dev: drm device for the ioctl
307 * @data: data pointer for the ioctl
308 * @file_priv: drm file for the ioctl call
309 *
310 * Add a new FB to the specified CRTC, given a user request with format. This is
311 * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
312 * and uses fourcc codes as pixel format specifiers.
313 *
314 * Called by the user via ioctl.
315 *
316 * Returns:
317 * Zero on success, negative errno on failure.
318 */
319int drm_mode_addfb2(struct drm_device *dev,
320 void *data, struct drm_file *file_priv)
321{
322 struct drm_mode_fb_cmd2 *r = data;
323 struct drm_framebuffer *fb;
324
325 if (!drm_core_check_feature(dev, DRIVER_MODESET))
326 return -EINVAL;
327
328 fb = drm_internal_framebuffer_create(dev, r, file_priv);
329 if (IS_ERR(fb))
330 return PTR_ERR(fb);
331
332 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
333 r->fb_id = fb->base.id;
334
335 /* Transfer ownership to the filp for reaping on close */
336 mutex_lock(&file_priv->fbs_lock);
337 list_add(&fb->filp_head, &file_priv->fbs);
338 mutex_unlock(&file_priv->fbs_lock);
339
340 return 0;
341}
342
343struct drm_mode_rmfb_work {
344 struct work_struct work;
345 struct list_head fbs;
346};
347
348static void drm_mode_rmfb_work_fn(struct work_struct *w)
349{
350 struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
351
352 while (!list_empty(&arg->fbs)) {
353 struct drm_framebuffer *fb =
354 list_first_entry(&arg->fbs, typeof(*fb), filp_head);
355
356 list_del_init(&fb->filp_head);
357 drm_framebuffer_remove(fb);
358 }
359}
360
361/**
362 * drm_mode_rmfb - remove an FB from the configuration
363 * @dev: drm device for the ioctl
364 * @data: data pointer for the ioctl
365 * @file_priv: drm file for the ioctl call
366 *
367 * Remove the FB specified by the user.
368 *
369 * Called by the user via ioctl.
370 *
371 * Returns:
372 * Zero on success, negative errno on failure.
373 */
374int drm_mode_rmfb(struct drm_device *dev,
375 void *data, struct drm_file *file_priv)
376{
377 struct drm_framebuffer *fb = NULL;
378 struct drm_framebuffer *fbl = NULL;
379 uint32_t *id = data;
380 int found = 0;
381
382 if (!drm_core_check_feature(dev, DRIVER_MODESET))
383 return -EINVAL;
384
385 fb = drm_framebuffer_lookup(dev, *id);
386 if (!fb)
387 return -ENOENT;
388
389 mutex_lock(&file_priv->fbs_lock);
390 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
391 if (fb == fbl)
392 found = 1;
393 if (!found) {
394 mutex_unlock(&file_priv->fbs_lock);
395 goto fail_unref;
396 }
397
398 list_del_init(&fb->filp_head);
399 mutex_unlock(&file_priv->fbs_lock);
400
401 /* drop the reference we picked up in framebuffer lookup */
Thierry Redinga4a69da2017-02-28 15:46:40 +0100402 drm_framebuffer_put(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200403
404 /*
405 * we now own the reference that was stored in the fbs list
406 *
407 * drm_framebuffer_remove may fail with -EINTR on pending signals,
408 * so run this in a separate stack as there's no way to correctly
409 * handle this after the fb is already removed from the lookup table.
410 */
411 if (drm_framebuffer_read_refcount(fb) > 1) {
412 struct drm_mode_rmfb_work arg;
413
414 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
415 INIT_LIST_HEAD(&arg.fbs);
416 list_add_tail(&fb->filp_head, &arg.fbs);
417
418 schedule_work(&arg.work);
419 flush_work(&arg.work);
420 destroy_work_on_stack(&arg.work);
421 } else
Thierry Redinga4a69da2017-02-28 15:46:40 +0100422 drm_framebuffer_put(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200423
424 return 0;
425
426fail_unref:
Thierry Redinga4a69da2017-02-28 15:46:40 +0100427 drm_framebuffer_put(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200428 return -ENOENT;
429}
430
431/**
432 * drm_mode_getfb - get FB info
433 * @dev: drm device for the ioctl
434 * @data: data pointer for the ioctl
435 * @file_priv: drm file for the ioctl call
436 *
437 * Lookup the FB given its ID and return info about it.
438 *
439 * Called by the user via ioctl.
440 *
441 * Returns:
442 * Zero on success, negative errno on failure.
443 */
444int drm_mode_getfb(struct drm_device *dev,
445 void *data, struct drm_file *file_priv)
446{
447 struct drm_mode_fb_cmd *r = data;
448 struct drm_framebuffer *fb;
449 int ret;
450
451 if (!drm_core_check_feature(dev, DRIVER_MODESET))
452 return -EINVAL;
453
454 fb = drm_framebuffer_lookup(dev, r->fb_id);
455 if (!fb)
456 return -ENOENT;
457
458 r->height = fb->height;
459 r->width = fb->width;
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200460 r->depth = fb->format->depth;
Ville Syrjälä272725c2016-12-14 23:32:20 +0200461 r->bpp = fb->format->cpp[0] * 8;
Daniel Vetter7520a272016-08-15 16:07:02 +0200462 r->pitch = fb->pitches[0];
463 if (fb->funcs->create_handle) {
464 if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) ||
465 drm_is_control_client(file_priv)) {
466 ret = fb->funcs->create_handle(fb, file_priv,
467 &r->handle);
468 } else {
469 /* GET_FB() is an unprivileged ioctl so we must not
470 * return a buffer-handle to non-master processes! For
471 * backwards-compatibility reasons, we cannot make
472 * GET_FB() privileged, so just return an invalid handle
473 * for non-masters. */
474 r->handle = 0;
475 ret = 0;
476 }
477 } else {
478 ret = -ENODEV;
479 }
480
Thierry Redinga4a69da2017-02-28 15:46:40 +0100481 drm_framebuffer_put(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200482
483 return ret;
484}
485
486/**
487 * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
488 * @dev: drm device for the ioctl
489 * @data: data pointer for the ioctl
490 * @file_priv: drm file for the ioctl call
491 *
492 * Lookup the FB and flush out the damaged area supplied by userspace as a clip
493 * rectangle list. Generic userspace which does frontbuffer rendering must call
494 * this ioctl to flush out the changes on manual-update display outputs, e.g.
495 * usb display-link, mipi manual update panels or edp panel self refresh modes.
496 *
497 * Modesetting drivers which always update the frontbuffer do not need to
Daniel Vetterd5745282017-01-25 07:26:45 +0100498 * implement the corresponding &drm_framebuffer_funcs.dirty callback.
Daniel Vetter7520a272016-08-15 16:07:02 +0200499 *
500 * Called by the user via ioctl.
501 *
502 * Returns:
503 * Zero on success, negative errno on failure.
504 */
505int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
506 void *data, struct drm_file *file_priv)
507{
508 struct drm_clip_rect __user *clips_ptr;
509 struct drm_clip_rect *clips = NULL;
510 struct drm_mode_fb_dirty_cmd *r = data;
511 struct drm_framebuffer *fb;
512 unsigned flags;
513 int num_clips;
514 int ret;
515
516 if (!drm_core_check_feature(dev, DRIVER_MODESET))
517 return -EINVAL;
518
519 fb = drm_framebuffer_lookup(dev, r->fb_id);
520 if (!fb)
521 return -ENOENT;
522
523 num_clips = r->num_clips;
524 clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
525
526 if (!num_clips != !clips_ptr) {
527 ret = -EINVAL;
528 goto out_err1;
529 }
530
531 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
532
533 /* If userspace annotates copy, clips must come in pairs */
534 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
535 ret = -EINVAL;
536 goto out_err1;
537 }
538
539 if (num_clips && clips_ptr) {
540 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
541 ret = -EINVAL;
542 goto out_err1;
543 }
544 clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
545 if (!clips) {
546 ret = -ENOMEM;
547 goto out_err1;
548 }
549
550 ret = copy_from_user(clips, clips_ptr,
551 num_clips * sizeof(*clips));
552 if (ret) {
553 ret = -EFAULT;
554 goto out_err2;
555 }
556 }
557
558 if (fb->funcs->dirty) {
559 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
560 clips, num_clips);
561 } else {
562 ret = -ENOSYS;
563 }
564
565out_err2:
566 kfree(clips);
567out_err1:
Thierry Redinga4a69da2017-02-28 15:46:40 +0100568 drm_framebuffer_put(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200569
570 return ret;
571}
572
573/**
574 * drm_fb_release - remove and free the FBs on this file
575 * @priv: drm file for the ioctl
576 *
577 * Destroy all the FBs associated with @filp.
578 *
579 * Called by the user via ioctl.
580 *
581 * Returns:
582 * Zero on success, negative errno on failure.
583 */
584void drm_fb_release(struct drm_file *priv)
585{
586 struct drm_framebuffer *fb, *tfb;
587 struct drm_mode_rmfb_work arg;
588
589 INIT_LIST_HEAD(&arg.fbs);
590
591 /*
592 * When the file gets released that means no one else can access the fb
593 * list any more, so no need to grab fpriv->fbs_lock. And we need to
594 * avoid upsetting lockdep since the universal cursor code adds a
595 * framebuffer while holding mutex locks.
596 *
597 * Note that a real deadlock between fpriv->fbs_lock and the modeset
598 * locks is impossible here since no one else but this function can get
599 * at it any more.
600 */
601 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
602 if (drm_framebuffer_read_refcount(fb) > 1) {
603 list_move_tail(&fb->filp_head, &arg.fbs);
604 } else {
605 list_del_init(&fb->filp_head);
606
607 /* This drops the fpriv->fbs reference. */
Thierry Redinga4a69da2017-02-28 15:46:40 +0100608 drm_framebuffer_put(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200609 }
610 }
611
612 if (!list_empty(&arg.fbs)) {
613 INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
614
615 schedule_work(&arg.work);
616 flush_work(&arg.work);
617 destroy_work_on_stack(&arg.work);
618 }
619}
620
621void drm_framebuffer_free(struct kref *kref)
622{
623 struct drm_framebuffer *fb =
624 container_of(kref, struct drm_framebuffer, base.refcount);
625 struct drm_device *dev = fb->dev;
626
627 /*
628 * The lookup idr holds a weak reference, which has not necessarily been
629 * removed at this point. Check for that.
630 */
631 drm_mode_object_unregister(dev, &fb->base);
632
633 fb->funcs->destroy(fb);
634}
635
636/**
637 * drm_framebuffer_init - initialize a framebuffer
638 * @dev: DRM device
639 * @fb: framebuffer to be initialized
640 * @funcs: ... with these functions
641 *
642 * Allocates an ID for the framebuffer's parent mode object, sets its mode
643 * functions & device file and adds it to the master fd list.
644 *
645 * IMPORTANT:
646 * This functions publishes the fb and makes it available for concurrent access
647 * by other users. Which means by this point the fb _must_ be fully set up -
648 * since all the fb attributes are invariant over its lifetime, no further
649 * locking but only correct reference counting is required.
650 *
651 * Returns:
652 * Zero on success, error code on failure.
653 */
654int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
655 const struct drm_framebuffer_funcs *funcs)
656{
657 int ret;
658
Ville Syrjälä570cec32016-11-18 21:52:58 +0200659 if (WARN_ON_ONCE(fb->dev != dev || !fb->format))
Ville Syrjälä95bce762016-11-18 21:52:54 +0200660 return -EINVAL;
661
Daniel Vetter7520a272016-08-15 16:07:02 +0200662 INIT_LIST_HEAD(&fb->filp_head);
Ville Syrjälä95bce762016-11-18 21:52:54 +0200663
Daniel Vetter7520a272016-08-15 16:07:02 +0200664 fb->funcs = funcs;
665
Thierry Reding2135ea72017-02-28 15:46:37 +0100666 ret = __drm_mode_object_add(dev, &fb->base, DRM_MODE_OBJECT_FB,
667 false, drm_framebuffer_free);
Daniel Vetter7520a272016-08-15 16:07:02 +0200668 if (ret)
669 goto out;
670
671 mutex_lock(&dev->mode_config.fb_lock);
672 dev->mode_config.num_fb++;
673 list_add(&fb->head, &dev->mode_config.fb_list);
674 mutex_unlock(&dev->mode_config.fb_lock);
675
676 drm_mode_object_register(dev, &fb->base);
677out:
678 return ret;
679}
680EXPORT_SYMBOL(drm_framebuffer_init);
681
682/**
683 * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
684 * @dev: drm device
685 * @id: id of the fb object
686 *
687 * If successful, this grabs an additional reference to the framebuffer -
688 * callers need to make sure to eventually unreference the returned framebuffer
Thierry Redinga4a69da2017-02-28 15:46:40 +0100689 * again, using drm_framebuffer_put().
Daniel Vetter7520a272016-08-15 16:07:02 +0200690 */
691struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
692 uint32_t id)
693{
694 struct drm_mode_object *obj;
695 struct drm_framebuffer *fb = NULL;
696
697 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB);
698 if (obj)
699 fb = obj_to_fb(obj);
700 return fb;
701}
702EXPORT_SYMBOL(drm_framebuffer_lookup);
703
704/**
705 * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
706 * @fb: fb to unregister
707 *
708 * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
709 * those used for fbdev. Note that the caller must hold a reference of it's own,
710 * i.e. the object may not be destroyed through this call (since it'll lead to a
711 * locking inversion).
Rongrong Zou03e93ac2016-10-31 19:59:56 +0800712 *
713 * NOTE: This function is deprecated. For driver-private framebuffers it is not
714 * recommended to embed a framebuffer struct info fbdev struct, instead, a
Thierry Redinga4a69da2017-02-28 15:46:40 +0100715 * framebuffer pointer is preferred and drm_framebuffer_put() should be called
716 * when the framebuffer is to be cleaned up.
Daniel Vetter7520a272016-08-15 16:07:02 +0200717 */
718void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
719{
720 struct drm_device *dev;
721
722 if (!fb)
723 return;
724
725 dev = fb->dev;
726
727 /* Mark fb as reaped and drop idr ref. */
728 drm_mode_object_unregister(dev, &fb->base);
729}
730EXPORT_SYMBOL(drm_framebuffer_unregister_private);
731
732/**
733 * drm_framebuffer_cleanup - remove a framebuffer object
734 * @fb: framebuffer to remove
735 *
736 * Cleanup framebuffer. This function is intended to be used from the drivers
Daniel Vetterd5745282017-01-25 07:26:45 +0100737 * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up
738 * driver private framebuffers embedded into a larger structure.
Daniel Vetter7520a272016-08-15 16:07:02 +0200739 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100740 * Note that this function does not remove the fb from active usage - if it is
Daniel Vetter7520a272016-08-15 16:07:02 +0200741 * still used anywhere, hilarity can ensue since userspace could call getfb on
742 * the id and get back -EINVAL. Obviously no concern at driver unload time.
743 *
744 * Also, the framebuffer will not be removed from the lookup idr - for
745 * user-created framebuffers this will happen in in the rmfb ioctl. For
746 * driver-private objects (e.g. for fbdev) drivers need to explicitly call
747 * drm_framebuffer_unregister_private.
748 */
749void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
750{
751 struct drm_device *dev = fb->dev;
752
753 mutex_lock(&dev->mode_config.fb_lock);
754 list_del(&fb->head);
755 dev->mode_config.num_fb--;
756 mutex_unlock(&dev->mode_config.fb_lock);
757}
758EXPORT_SYMBOL(drm_framebuffer_cleanup);
759
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200760static int atomic_remove_fb(struct drm_framebuffer *fb)
761{
762 struct drm_modeset_acquire_ctx ctx;
763 struct drm_device *dev = fb->dev;
764 struct drm_atomic_state *state;
765 struct drm_plane *plane;
766 struct drm_connector *conn;
767 struct drm_connector_state *conn_state;
Maarten Lankhorst846c7df2017-11-01 16:04:33 +0100768 int i, ret;
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200769 unsigned plane_mask;
Maarten Lankhorst846c7df2017-11-01 16:04:33 +0100770 bool disable_crtcs = false;
771
772retry_disable:
773 drm_modeset_acquire_init(&ctx, 0);
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200774
775 state = drm_atomic_state_alloc(dev);
Maarten Lankhorst846c7df2017-11-01 16:04:33 +0100776 if (!state) {
777 ret = -ENOMEM;
778 goto out;
779 }
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200780 state->acquire_ctx = &ctx;
781
782retry:
783 plane_mask = 0;
784 ret = drm_modeset_lock_all_ctx(dev, &ctx);
785 if (ret)
786 goto unlock;
787
788 drm_for_each_plane(plane, dev) {
789 struct drm_plane_state *plane_state;
790
791 if (plane->state->fb != fb)
792 continue;
793
794 plane_state = drm_atomic_get_plane_state(state, plane);
795 if (IS_ERR(plane_state)) {
796 ret = PTR_ERR(plane_state);
797 goto unlock;
798 }
799
Maarten Lankhorst846c7df2017-11-01 16:04:33 +0100800 if (disable_crtcs && plane_state->crtc->primary == plane) {
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200801 struct drm_crtc_state *crtc_state;
802
803 crtc_state = drm_atomic_get_existing_crtc_state(state, plane_state->crtc);
804
805 ret = drm_atomic_add_affected_connectors(state, plane_state->crtc);
806 if (ret)
807 goto unlock;
808
809 crtc_state->active = false;
810 ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
811 if (ret)
812 goto unlock;
813 }
814
815 drm_atomic_set_fb_for_plane(plane_state, NULL);
816 ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
817 if (ret)
818 goto unlock;
819
820 plane_mask |= BIT(drm_plane_index(plane));
821
822 plane->old_fb = plane->fb;
823 }
824
Maarten Lankhorst846c7df2017-11-01 16:04:33 +0100825 /* This list is only filled when disable_crtcs is set. */
Maarten Lankhorst0c3eb122017-07-12 10:13:30 +0200826 for_each_new_connector_in_state(state, conn, conn_state, i) {
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200827 ret = drm_atomic_set_crtc_for_connector(conn_state, NULL);
828
829 if (ret)
830 goto unlock;
831 }
832
833 if (plane_mask)
834 ret = drm_atomic_commit(state);
835
836unlock:
837 if (plane_mask)
838 drm_atomic_clean_old_fb(dev, plane_mask, ret);
839
840 if (ret == -EDEADLK) {
Maarten Lankhorst4086d902017-06-29 13:59:54 +0200841 drm_atomic_state_clear(state);
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200842 drm_modeset_backoff(&ctx);
843 goto retry;
844 }
845
846 drm_atomic_state_put(state);
847
Maarten Lankhorst846c7df2017-11-01 16:04:33 +0100848out:
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200849 drm_modeset_drop_locks(&ctx);
850 drm_modeset_acquire_fini(&ctx);
851
Maarten Lankhorst846c7df2017-11-01 16:04:33 +0100852 if (ret == -EINVAL && !disable_crtcs) {
853 disable_crtcs = true;
854 goto retry_disable;
855 }
856
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200857 return ret;
858}
859
860static void legacy_remove_fb(struct drm_framebuffer *fb)
861{
862 struct drm_device *dev = fb->dev;
863 struct drm_crtc *crtc;
864 struct drm_plane *plane;
865
866 drm_modeset_lock_all(dev);
867 /* remove from any CRTC */
868 drm_for_each_crtc(crtc, dev) {
869 if (crtc->primary->fb == fb) {
870 /* should turn off the crtc */
871 if (drm_crtc_force_disable(crtc))
872 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
873 }
874 }
875
876 drm_for_each_plane(plane, dev) {
877 if (plane->fb == fb)
878 drm_plane_force_disable(plane);
879 }
880 drm_modeset_unlock_all(dev);
881}
882
Daniel Vetter7520a272016-08-15 16:07:02 +0200883/**
884 * drm_framebuffer_remove - remove and unreference a framebuffer object
885 * @fb: framebuffer to remove
886 *
887 * Scans all the CRTCs and planes in @dev's mode_config. If they're
888 * using @fb, removes it, setting it to NULL. Then drops the reference to the
889 * passed-in framebuffer. Might take the modeset locks.
890 *
891 * Note that this function optimizes the cleanup away if the caller holds the
892 * last reference to the framebuffer. It is also guaranteed to not take the
893 * modeset locks in this case.
894 */
895void drm_framebuffer_remove(struct drm_framebuffer *fb)
896{
897 struct drm_device *dev;
Daniel Vetter7520a272016-08-15 16:07:02 +0200898
899 if (!fb)
900 return;
901
902 dev = fb->dev;
903
904 WARN_ON(!list_empty(&fb->filp_head));
905
906 /*
907 * drm ABI mandates that we remove any deleted framebuffers from active
908 * useage. But since most sane clients only remove framebuffers they no
909 * longer need, try to optimize this away.
910 *
911 * Since we're holding a reference ourselves, observing a refcount of 1
912 * means that we're the last holder and can skip it. Also, the refcount
913 * can never increase from 1 again, so we don't need any barriers or
914 * locks.
915 *
916 * Note that userspace could try to race with use and instate a new
917 * usage _after_ we've cleared all current ones. End result will be an
918 * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
919 * in this manner.
920 */
921 if (drm_framebuffer_read_refcount(fb) > 1) {
Maarten Lankhorstdb8f6402017-02-21 14:51:41 +0100922 if (drm_drv_uses_atomic_modeset(dev)) {
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200923 int ret = atomic_remove_fb(fb);
Maarten Lankhorstdb8f6402017-02-21 14:51:41 +0100924 WARN(ret, "atomic remove_fb failed with %i\n", ret);
Daniel Vetter941b8ca2017-04-03 10:33:03 +0200925 } else
926 legacy_remove_fb(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200927 }
928
Thierry Redinga4a69da2017-02-28 15:46:40 +0100929 drm_framebuffer_put(fb);
Daniel Vetter7520a272016-08-15 16:07:02 +0200930}
931EXPORT_SYMBOL(drm_framebuffer_remove);
Ville Syrjälä8f8f6a62016-11-18 21:53:05 +0200932
933/**
934 * drm_framebuffer_plane_width - width of the plane given the first plane
935 * @width: width of the first plane
936 * @fb: the framebuffer
937 * @plane: plane index
938 *
939 * Returns:
940 * The width of @plane, given that the width of the first plane is @width.
941 */
942int drm_framebuffer_plane_width(int width,
943 const struct drm_framebuffer *fb, int plane)
944{
945 if (plane >= fb->format->num_planes)
946 return 0;
947
Ville Syrjälä568c5e42017-03-21 20:12:14 +0200948 return fb_plane_width(width, fb->format, plane);
Ville Syrjälä8f8f6a62016-11-18 21:53:05 +0200949}
950EXPORT_SYMBOL(drm_framebuffer_plane_width);
951
952/**
953 * drm_framebuffer_plane_height - height of the plane given the first plane
954 * @height: height of the first plane
955 * @fb: the framebuffer
956 * @plane: plane index
957 *
958 * Returns:
959 * The height of @plane, given that the height of the first plane is @height.
960 */
961int drm_framebuffer_plane_height(int height,
962 const struct drm_framebuffer *fb, int plane)
963{
964 if (plane >= fb->format->num_planes)
965 return 0;
966
Ville Syrjälä568c5e42017-03-21 20:12:14 +0200967 return fb_plane_height(height, fb->format, plane);
Ville Syrjälä8f8f6a62016-11-18 21:53:05 +0200968}
969EXPORT_SYMBOL(drm_framebuffer_plane_height);