Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 1 | /* |
| 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> |
| 27 | |
| 28 | #include "drm_crtc_internal.h" |
| 29 | |
| 30 | /** |
Daniel Vetter | 750fb8c | 2016-08-12 22:48:48 +0200 | [diff] [blame] | 31 | * DOC: overview |
| 32 | * |
| 33 | * Frame buffers are abstract memory objects that provide a source of pixels to |
| 34 | * scanout to a CRTC. Applications explicitly request the creation of frame |
| 35 | * buffers through the DRM_IOCTL_MODE_ADDFB(2) ioctls and receive an opaque |
| 36 | * handle that can be passed to the KMS CRTC control, plane configuration and |
| 37 | * page flip functions. |
| 38 | * |
| 39 | * Frame buffers rely on the underlying memory manager for allocating backing |
| 40 | * storage. When creating a frame buffer applications pass a memory handle |
| 41 | * (or a list of memory handles for multi-planar formats) through the |
Daniel Vetter | ea0dd85 | 2016-12-29 21:48:26 +0100 | [diff] [blame] | 42 | * &struct drm_mode_fb_cmd2 argument. For drivers using GEM as their userspace |
Daniel Vetter | 750fb8c | 2016-08-12 22:48:48 +0200 | [diff] [blame] | 43 | * buffer management interface this would be a GEM handle. Drivers are however |
| 44 | * free to use their own backing storage object handles, e.g. vmwgfx directly |
| 45 | * exposes special TTM handles to userspace and so expects TTM handles in the |
| 46 | * create ioctl and not GEM handles. |
| 47 | * |
Daniel Vetter | ea0dd85 | 2016-12-29 21:48:26 +0100 | [diff] [blame] | 48 | * Framebuffers are tracked with &struct drm_framebuffer. They are published |
Daniel Vetter | 750fb8c | 2016-08-12 22:48:48 +0200 | [diff] [blame] | 49 | * using drm_framebuffer_init() - after calling that function userspace can use |
| 50 | * and access the framebuffer object. The helper function |
| 51 | * drm_helper_mode_fill_fb_struct() can be used to pre-fill the required |
| 52 | * metadata fields. |
| 53 | * |
| 54 | * The lifetime of a drm framebuffer is controlled with a reference count, |
| 55 | * drivers can grab additional references with drm_framebuffer_reference() and |
| 56 | * drop them again with drm_framebuffer_unreference(). For driver-private |
| 57 | * framebuffers for which the last reference is never dropped (e.g. for the |
Daniel Vetter | ea0dd85 | 2016-12-29 21:48:26 +0100 | [diff] [blame] | 58 | * fbdev framebuffer when the struct &struct drm_framebuffer is embedded into |
Daniel Vetter | 750fb8c | 2016-08-12 22:48:48 +0200 | [diff] [blame] | 59 | * the fbdev helper struct) drivers can manually clean up a framebuffer at |
| 60 | * module unload time with drm_framebuffer_unregister_private(). But doing this |
Daniel Vetter | d574528 | 2017-01-25 07:26:45 +0100 | [diff] [blame^] | 61 | * is not recommended, and it's better to have a normal free-standing &struct |
| 62 | * drm_framebuffer. |
Daniel Vetter | 750fb8c | 2016-08-12 22:48:48 +0200 | [diff] [blame] | 63 | */ |
| 64 | |
Daniel Vetter | 43968d7 | 2016-09-21 10:59:24 +0200 | [diff] [blame] | 65 | int drm_framebuffer_check_src_coords(uint32_t src_x, uint32_t src_y, |
| 66 | uint32_t src_w, uint32_t src_h, |
| 67 | const struct drm_framebuffer *fb) |
| 68 | { |
| 69 | unsigned int fb_width, fb_height; |
| 70 | |
| 71 | fb_width = fb->width << 16; |
| 72 | fb_height = fb->height << 16; |
| 73 | |
| 74 | /* Make sure source coordinates are inside the fb. */ |
| 75 | if (src_w > fb_width || |
| 76 | src_x > fb_width - src_w || |
| 77 | src_h > fb_height || |
| 78 | src_y > fb_height - src_h) { |
| 79 | DRM_DEBUG_KMS("Invalid source coordinates " |
| 80 | "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n", |
| 81 | src_w >> 16, ((src_w & 0xffff) * 15625) >> 10, |
| 82 | src_h >> 16, ((src_h & 0xffff) * 15625) >> 10, |
| 83 | src_x >> 16, ((src_x & 0xffff) * 15625) >> 10, |
| 84 | src_y >> 16, ((src_y & 0xffff) * 15625) >> 10); |
| 85 | return -ENOSPC; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
Daniel Vetter | 750fb8c | 2016-08-12 22:48:48 +0200 | [diff] [blame] | 91 | /** |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 92 | * drm_mode_addfb - add an FB to the graphics configuration |
| 93 | * @dev: drm device for the ioctl |
| 94 | * @data: data pointer for the ioctl |
| 95 | * @file_priv: drm file for the ioctl call |
| 96 | * |
| 97 | * Add a new FB to the specified CRTC, given a user request. This is the |
| 98 | * original addfb ioctl which only supported RGB formats. |
| 99 | * |
| 100 | * Called by the user via ioctl. |
| 101 | * |
| 102 | * Returns: |
| 103 | * Zero on success, negative errno on failure. |
| 104 | */ |
| 105 | int drm_mode_addfb(struct drm_device *dev, |
| 106 | void *data, struct drm_file *file_priv) |
| 107 | { |
| 108 | struct drm_mode_fb_cmd *or = data; |
| 109 | struct drm_mode_fb_cmd2 r = {}; |
| 110 | int ret; |
| 111 | |
| 112 | /* convert to new format and call new ioctl */ |
| 113 | r.fb_id = or->fb_id; |
| 114 | r.width = or->width; |
| 115 | r.height = or->height; |
| 116 | r.pitches[0] = or->pitch; |
| 117 | r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth); |
| 118 | r.handles[0] = or->handle; |
| 119 | |
| 120 | ret = drm_mode_addfb2(dev, &r, file_priv); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
| 124 | or->fb_id = r.fb_id; |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 129 | static int framebuffer_check(const struct drm_mode_fb_cmd2 *r) |
| 130 | { |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 131 | const struct drm_format_info *info; |
| 132 | int i; |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 133 | |
Laurent Pinchart | 333d2da | 2016-10-18 01:41:12 +0300 | [diff] [blame] | 134 | info = __drm_format_info(r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN); |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 135 | if (!info) { |
Eric Engestrom | b3c11ac | 2016-11-12 01:12:56 +0000 | [diff] [blame] | 136 | struct drm_format_name_buf format_name; |
| 137 | DRM_DEBUG_KMS("bad framebuffer format %s\n", |
| 138 | drm_get_format_name(r->pixel_format, |
| 139 | &format_name)); |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 140 | return -EINVAL; |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 143 | if (r->width == 0 || r->width % info->hsub) { |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 144 | DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width); |
| 145 | return -EINVAL; |
| 146 | } |
| 147 | |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 148 | if (r->height == 0 || r->height % info->vsub) { |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 149 | DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height); |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 153 | for (i = 0; i < info->num_planes; i++) { |
| 154 | unsigned int width = r->width / (i != 0 ? info->hsub : 1); |
| 155 | unsigned int height = r->height / (i != 0 ? info->vsub : 1); |
| 156 | unsigned int cpp = info->cpp[i]; |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 157 | |
| 158 | if (!r->handles[i]) { |
| 159 | DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i); |
| 160 | return -EINVAL; |
| 161 | } |
| 162 | |
| 163 | if ((uint64_t) width * cpp > UINT_MAX) |
| 164 | return -ERANGE; |
| 165 | |
| 166 | if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX) |
| 167 | return -ERANGE; |
| 168 | |
| 169 | if (r->pitches[i] < width * cpp) { |
| 170 | DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i); |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | |
| 174 | if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) { |
| 175 | DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n", |
| 176 | r->modifier[i], i); |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Ville Syrjälä | bae781b | 2016-11-16 13:33:16 +0200 | [diff] [blame] | 180 | if (r->flags & DRM_MODE_FB_MODIFIERS && |
| 181 | r->modifier[i] != r->modifier[0]) { |
| 182 | DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n", |
| 183 | r->modifier[i], i); |
| 184 | return -EINVAL; |
| 185 | } |
| 186 | |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 187 | /* modifier specific checks: */ |
| 188 | switch (r->modifier[i]) { |
| 189 | case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE: |
| 190 | /* NOTE: the pitch restriction may be lifted later if it turns |
| 191 | * out that no hw has this restriction: |
| 192 | */ |
| 193 | if (r->pixel_format != DRM_FORMAT_NV12 || |
| 194 | width % 128 || height % 32 || |
| 195 | r->pitches[i] % 128) { |
| 196 | DRM_DEBUG_KMS("bad modifier data for plane %d\n", i); |
| 197 | return -EINVAL; |
| 198 | } |
| 199 | break; |
| 200 | |
| 201 | default: |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | |
Laurent Pinchart | d549349 | 2016-10-18 01:41:11 +0300 | [diff] [blame] | 206 | for (i = info->num_planes; i < 4; i++) { |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 207 | if (r->modifier[i]) { |
| 208 | DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i); |
| 209 | return -EINVAL; |
| 210 | } |
| 211 | |
| 212 | /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */ |
| 213 | if (!(r->flags & DRM_MODE_FB_MODIFIERS)) |
| 214 | continue; |
| 215 | |
| 216 | if (r->handles[i]) { |
| 217 | DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i); |
| 218 | return -EINVAL; |
| 219 | } |
| 220 | |
| 221 | if (r->pitches[i]) { |
| 222 | DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | if (r->offsets[i]) { |
| 227 | DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i); |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | struct drm_framebuffer * |
| 236 | drm_internal_framebuffer_create(struct drm_device *dev, |
| 237 | const struct drm_mode_fb_cmd2 *r, |
| 238 | struct drm_file *file_priv) |
| 239 | { |
| 240 | struct drm_mode_config *config = &dev->mode_config; |
| 241 | struct drm_framebuffer *fb; |
| 242 | int ret; |
| 243 | |
| 244 | if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) { |
| 245 | DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags); |
| 246 | return ERR_PTR(-EINVAL); |
| 247 | } |
| 248 | |
| 249 | if ((config->min_width > r->width) || (r->width > config->max_width)) { |
| 250 | DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n", |
| 251 | r->width, config->min_width, config->max_width); |
| 252 | return ERR_PTR(-EINVAL); |
| 253 | } |
| 254 | if ((config->min_height > r->height) || (r->height > config->max_height)) { |
| 255 | DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n", |
| 256 | r->height, config->min_height, config->max_height); |
| 257 | return ERR_PTR(-EINVAL); |
| 258 | } |
| 259 | |
| 260 | if (r->flags & DRM_MODE_FB_MODIFIERS && |
| 261 | !dev->mode_config.allow_fb_modifiers) { |
| 262 | DRM_DEBUG_KMS("driver does not support fb modifiers\n"); |
| 263 | return ERR_PTR(-EINVAL); |
| 264 | } |
| 265 | |
| 266 | ret = framebuffer_check(r); |
| 267 | if (ret) |
| 268 | return ERR_PTR(ret); |
| 269 | |
| 270 | fb = dev->mode_config.funcs->fb_create(dev, file_priv, r); |
| 271 | if (IS_ERR(fb)) { |
| 272 | DRM_DEBUG_KMS("could not create framebuffer\n"); |
| 273 | return fb; |
| 274 | } |
| 275 | |
| 276 | return fb; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * drm_mode_addfb2 - add an FB to the graphics configuration |
| 281 | * @dev: drm device for the ioctl |
| 282 | * @data: data pointer for the ioctl |
| 283 | * @file_priv: drm file for the ioctl call |
| 284 | * |
| 285 | * Add a new FB to the specified CRTC, given a user request with format. This is |
| 286 | * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers |
| 287 | * and uses fourcc codes as pixel format specifiers. |
| 288 | * |
| 289 | * Called by the user via ioctl. |
| 290 | * |
| 291 | * Returns: |
| 292 | * Zero on success, negative errno on failure. |
| 293 | */ |
| 294 | int drm_mode_addfb2(struct drm_device *dev, |
| 295 | void *data, struct drm_file *file_priv) |
| 296 | { |
| 297 | struct drm_mode_fb_cmd2 *r = data; |
| 298 | struct drm_framebuffer *fb; |
| 299 | |
| 300 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | fb = drm_internal_framebuffer_create(dev, r, file_priv); |
| 304 | if (IS_ERR(fb)) |
| 305 | return PTR_ERR(fb); |
| 306 | |
| 307 | DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id); |
| 308 | r->fb_id = fb->base.id; |
| 309 | |
| 310 | /* Transfer ownership to the filp for reaping on close */ |
| 311 | mutex_lock(&file_priv->fbs_lock); |
| 312 | list_add(&fb->filp_head, &file_priv->fbs); |
| 313 | mutex_unlock(&file_priv->fbs_lock); |
| 314 | |
| 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | struct drm_mode_rmfb_work { |
| 319 | struct work_struct work; |
| 320 | struct list_head fbs; |
| 321 | }; |
| 322 | |
| 323 | static void drm_mode_rmfb_work_fn(struct work_struct *w) |
| 324 | { |
| 325 | struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work); |
| 326 | |
| 327 | while (!list_empty(&arg->fbs)) { |
| 328 | struct drm_framebuffer *fb = |
| 329 | list_first_entry(&arg->fbs, typeof(*fb), filp_head); |
| 330 | |
| 331 | list_del_init(&fb->filp_head); |
| 332 | drm_framebuffer_remove(fb); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * drm_mode_rmfb - remove an FB from the configuration |
| 338 | * @dev: drm device for the ioctl |
| 339 | * @data: data pointer for the ioctl |
| 340 | * @file_priv: drm file for the ioctl call |
| 341 | * |
| 342 | * Remove the FB specified by the user. |
| 343 | * |
| 344 | * Called by the user via ioctl. |
| 345 | * |
| 346 | * Returns: |
| 347 | * Zero on success, negative errno on failure. |
| 348 | */ |
| 349 | int drm_mode_rmfb(struct drm_device *dev, |
| 350 | void *data, struct drm_file *file_priv) |
| 351 | { |
| 352 | struct drm_framebuffer *fb = NULL; |
| 353 | struct drm_framebuffer *fbl = NULL; |
| 354 | uint32_t *id = data; |
| 355 | int found = 0; |
| 356 | |
| 357 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 358 | return -EINVAL; |
| 359 | |
| 360 | fb = drm_framebuffer_lookup(dev, *id); |
| 361 | if (!fb) |
| 362 | return -ENOENT; |
| 363 | |
| 364 | mutex_lock(&file_priv->fbs_lock); |
| 365 | list_for_each_entry(fbl, &file_priv->fbs, filp_head) |
| 366 | if (fb == fbl) |
| 367 | found = 1; |
| 368 | if (!found) { |
| 369 | mutex_unlock(&file_priv->fbs_lock); |
| 370 | goto fail_unref; |
| 371 | } |
| 372 | |
| 373 | list_del_init(&fb->filp_head); |
| 374 | mutex_unlock(&file_priv->fbs_lock); |
| 375 | |
| 376 | /* drop the reference we picked up in framebuffer lookup */ |
| 377 | drm_framebuffer_unreference(fb); |
| 378 | |
| 379 | /* |
| 380 | * we now own the reference that was stored in the fbs list |
| 381 | * |
| 382 | * drm_framebuffer_remove may fail with -EINTR on pending signals, |
| 383 | * so run this in a separate stack as there's no way to correctly |
| 384 | * handle this after the fb is already removed from the lookup table. |
| 385 | */ |
| 386 | if (drm_framebuffer_read_refcount(fb) > 1) { |
| 387 | struct drm_mode_rmfb_work arg; |
| 388 | |
| 389 | INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); |
| 390 | INIT_LIST_HEAD(&arg.fbs); |
| 391 | list_add_tail(&fb->filp_head, &arg.fbs); |
| 392 | |
| 393 | schedule_work(&arg.work); |
| 394 | flush_work(&arg.work); |
| 395 | destroy_work_on_stack(&arg.work); |
| 396 | } else |
| 397 | drm_framebuffer_unreference(fb); |
| 398 | |
| 399 | return 0; |
| 400 | |
| 401 | fail_unref: |
| 402 | drm_framebuffer_unreference(fb); |
| 403 | return -ENOENT; |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * drm_mode_getfb - get FB info |
| 408 | * @dev: drm device for the ioctl |
| 409 | * @data: data pointer for the ioctl |
| 410 | * @file_priv: drm file for the ioctl call |
| 411 | * |
| 412 | * Lookup the FB given its ID and return info about it. |
| 413 | * |
| 414 | * Called by the user via ioctl. |
| 415 | * |
| 416 | * Returns: |
| 417 | * Zero on success, negative errno on failure. |
| 418 | */ |
| 419 | int drm_mode_getfb(struct drm_device *dev, |
| 420 | void *data, struct drm_file *file_priv) |
| 421 | { |
| 422 | struct drm_mode_fb_cmd *r = data; |
| 423 | struct drm_framebuffer *fb; |
| 424 | int ret; |
| 425 | |
| 426 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 427 | return -EINVAL; |
| 428 | |
| 429 | fb = drm_framebuffer_lookup(dev, r->fb_id); |
| 430 | if (!fb) |
| 431 | return -ENOENT; |
| 432 | |
| 433 | r->height = fb->height; |
| 434 | r->width = fb->width; |
Ville Syrjälä | b00c600 | 2016-12-14 23:31:35 +0200 | [diff] [blame] | 435 | r->depth = fb->format->depth; |
Ville Syrjälä | 272725c | 2016-12-14 23:32:20 +0200 | [diff] [blame] | 436 | r->bpp = fb->format->cpp[0] * 8; |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 437 | r->pitch = fb->pitches[0]; |
| 438 | if (fb->funcs->create_handle) { |
| 439 | if (drm_is_current_master(file_priv) || capable(CAP_SYS_ADMIN) || |
| 440 | drm_is_control_client(file_priv)) { |
| 441 | ret = fb->funcs->create_handle(fb, file_priv, |
| 442 | &r->handle); |
| 443 | } else { |
| 444 | /* GET_FB() is an unprivileged ioctl so we must not |
| 445 | * return a buffer-handle to non-master processes! For |
| 446 | * backwards-compatibility reasons, we cannot make |
| 447 | * GET_FB() privileged, so just return an invalid handle |
| 448 | * for non-masters. */ |
| 449 | r->handle = 0; |
| 450 | ret = 0; |
| 451 | } |
| 452 | } else { |
| 453 | ret = -ENODEV; |
| 454 | } |
| 455 | |
| 456 | drm_framebuffer_unreference(fb); |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB |
| 463 | * @dev: drm device for the ioctl |
| 464 | * @data: data pointer for the ioctl |
| 465 | * @file_priv: drm file for the ioctl call |
| 466 | * |
| 467 | * Lookup the FB and flush out the damaged area supplied by userspace as a clip |
| 468 | * rectangle list. Generic userspace which does frontbuffer rendering must call |
| 469 | * this ioctl to flush out the changes on manual-update display outputs, e.g. |
| 470 | * usb display-link, mipi manual update panels or edp panel self refresh modes. |
| 471 | * |
| 472 | * Modesetting drivers which always update the frontbuffer do not need to |
Daniel Vetter | d574528 | 2017-01-25 07:26:45 +0100 | [diff] [blame^] | 473 | * implement the corresponding &drm_framebuffer_funcs.dirty callback. |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 474 | * |
| 475 | * Called by the user via ioctl. |
| 476 | * |
| 477 | * Returns: |
| 478 | * Zero on success, negative errno on failure. |
| 479 | */ |
| 480 | int drm_mode_dirtyfb_ioctl(struct drm_device *dev, |
| 481 | void *data, struct drm_file *file_priv) |
| 482 | { |
| 483 | struct drm_clip_rect __user *clips_ptr; |
| 484 | struct drm_clip_rect *clips = NULL; |
| 485 | struct drm_mode_fb_dirty_cmd *r = data; |
| 486 | struct drm_framebuffer *fb; |
| 487 | unsigned flags; |
| 488 | int num_clips; |
| 489 | int ret; |
| 490 | |
| 491 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | fb = drm_framebuffer_lookup(dev, r->fb_id); |
| 495 | if (!fb) |
| 496 | return -ENOENT; |
| 497 | |
| 498 | num_clips = r->num_clips; |
| 499 | clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr; |
| 500 | |
| 501 | if (!num_clips != !clips_ptr) { |
| 502 | ret = -EINVAL; |
| 503 | goto out_err1; |
| 504 | } |
| 505 | |
| 506 | flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags; |
| 507 | |
| 508 | /* If userspace annotates copy, clips must come in pairs */ |
| 509 | if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) { |
| 510 | ret = -EINVAL; |
| 511 | goto out_err1; |
| 512 | } |
| 513 | |
| 514 | if (num_clips && clips_ptr) { |
| 515 | if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) { |
| 516 | ret = -EINVAL; |
| 517 | goto out_err1; |
| 518 | } |
| 519 | clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL); |
| 520 | if (!clips) { |
| 521 | ret = -ENOMEM; |
| 522 | goto out_err1; |
| 523 | } |
| 524 | |
| 525 | ret = copy_from_user(clips, clips_ptr, |
| 526 | num_clips * sizeof(*clips)); |
| 527 | if (ret) { |
| 528 | ret = -EFAULT; |
| 529 | goto out_err2; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | if (fb->funcs->dirty) { |
| 534 | ret = fb->funcs->dirty(fb, file_priv, flags, r->color, |
| 535 | clips, num_clips); |
| 536 | } else { |
| 537 | ret = -ENOSYS; |
| 538 | } |
| 539 | |
| 540 | out_err2: |
| 541 | kfree(clips); |
| 542 | out_err1: |
| 543 | drm_framebuffer_unreference(fb); |
| 544 | |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * drm_fb_release - remove and free the FBs on this file |
| 550 | * @priv: drm file for the ioctl |
| 551 | * |
| 552 | * Destroy all the FBs associated with @filp. |
| 553 | * |
| 554 | * Called by the user via ioctl. |
| 555 | * |
| 556 | * Returns: |
| 557 | * Zero on success, negative errno on failure. |
| 558 | */ |
| 559 | void drm_fb_release(struct drm_file *priv) |
| 560 | { |
| 561 | struct drm_framebuffer *fb, *tfb; |
| 562 | struct drm_mode_rmfb_work arg; |
| 563 | |
| 564 | INIT_LIST_HEAD(&arg.fbs); |
| 565 | |
| 566 | /* |
| 567 | * When the file gets released that means no one else can access the fb |
| 568 | * list any more, so no need to grab fpriv->fbs_lock. And we need to |
| 569 | * avoid upsetting lockdep since the universal cursor code adds a |
| 570 | * framebuffer while holding mutex locks. |
| 571 | * |
| 572 | * Note that a real deadlock between fpriv->fbs_lock and the modeset |
| 573 | * locks is impossible here since no one else but this function can get |
| 574 | * at it any more. |
| 575 | */ |
| 576 | list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) { |
| 577 | if (drm_framebuffer_read_refcount(fb) > 1) { |
| 578 | list_move_tail(&fb->filp_head, &arg.fbs); |
| 579 | } else { |
| 580 | list_del_init(&fb->filp_head); |
| 581 | |
| 582 | /* This drops the fpriv->fbs reference. */ |
| 583 | drm_framebuffer_unreference(fb); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | if (!list_empty(&arg.fbs)) { |
| 588 | INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn); |
| 589 | |
| 590 | schedule_work(&arg.work); |
| 591 | flush_work(&arg.work); |
| 592 | destroy_work_on_stack(&arg.work); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | void drm_framebuffer_free(struct kref *kref) |
| 597 | { |
| 598 | struct drm_framebuffer *fb = |
| 599 | container_of(kref, struct drm_framebuffer, base.refcount); |
| 600 | struct drm_device *dev = fb->dev; |
| 601 | |
| 602 | /* |
| 603 | * The lookup idr holds a weak reference, which has not necessarily been |
| 604 | * removed at this point. Check for that. |
| 605 | */ |
| 606 | drm_mode_object_unregister(dev, &fb->base); |
| 607 | |
| 608 | fb->funcs->destroy(fb); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * drm_framebuffer_init - initialize a framebuffer |
| 613 | * @dev: DRM device |
| 614 | * @fb: framebuffer to be initialized |
| 615 | * @funcs: ... with these functions |
| 616 | * |
| 617 | * Allocates an ID for the framebuffer's parent mode object, sets its mode |
| 618 | * functions & device file and adds it to the master fd list. |
| 619 | * |
| 620 | * IMPORTANT: |
| 621 | * This functions publishes the fb and makes it available for concurrent access |
| 622 | * by other users. Which means by this point the fb _must_ be fully set up - |
| 623 | * since all the fb attributes are invariant over its lifetime, no further |
| 624 | * locking but only correct reference counting is required. |
| 625 | * |
| 626 | * Returns: |
| 627 | * Zero on success, error code on failure. |
| 628 | */ |
| 629 | int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb, |
| 630 | const struct drm_framebuffer_funcs *funcs) |
| 631 | { |
| 632 | int ret; |
| 633 | |
Ville Syrjälä | 570cec3 | 2016-11-18 21:52:58 +0200 | [diff] [blame] | 634 | if (WARN_ON_ONCE(fb->dev != dev || !fb->format)) |
Ville Syrjälä | 95bce76 | 2016-11-18 21:52:54 +0200 | [diff] [blame] | 635 | return -EINVAL; |
| 636 | |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 637 | INIT_LIST_HEAD(&fb->filp_head); |
Ville Syrjälä | 95bce76 | 2016-11-18 21:52:54 +0200 | [diff] [blame] | 638 | |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 639 | fb->funcs = funcs; |
| 640 | |
| 641 | ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB, |
| 642 | false, drm_framebuffer_free); |
| 643 | if (ret) |
| 644 | goto out; |
| 645 | |
| 646 | mutex_lock(&dev->mode_config.fb_lock); |
| 647 | dev->mode_config.num_fb++; |
| 648 | list_add(&fb->head, &dev->mode_config.fb_list); |
| 649 | mutex_unlock(&dev->mode_config.fb_lock); |
| 650 | |
| 651 | drm_mode_object_register(dev, &fb->base); |
| 652 | out: |
| 653 | return ret; |
| 654 | } |
| 655 | EXPORT_SYMBOL(drm_framebuffer_init); |
| 656 | |
| 657 | /** |
| 658 | * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference |
| 659 | * @dev: drm device |
| 660 | * @id: id of the fb object |
| 661 | * |
| 662 | * If successful, this grabs an additional reference to the framebuffer - |
| 663 | * callers need to make sure to eventually unreference the returned framebuffer |
| 664 | * again, using @drm_framebuffer_unreference. |
| 665 | */ |
| 666 | struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, |
| 667 | uint32_t id) |
| 668 | { |
| 669 | struct drm_mode_object *obj; |
| 670 | struct drm_framebuffer *fb = NULL; |
| 671 | |
| 672 | obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_FB); |
| 673 | if (obj) |
| 674 | fb = obj_to_fb(obj); |
| 675 | return fb; |
| 676 | } |
| 677 | EXPORT_SYMBOL(drm_framebuffer_lookup); |
| 678 | |
| 679 | /** |
| 680 | * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr |
| 681 | * @fb: fb to unregister |
| 682 | * |
| 683 | * Drivers need to call this when cleaning up driver-private framebuffers, e.g. |
| 684 | * those used for fbdev. Note that the caller must hold a reference of it's own, |
| 685 | * i.e. the object may not be destroyed through this call (since it'll lead to a |
| 686 | * locking inversion). |
Rongrong Zou | 03e93ac | 2016-10-31 19:59:56 +0800 | [diff] [blame] | 687 | * |
| 688 | * NOTE: This function is deprecated. For driver-private framebuffers it is not |
| 689 | * recommended to embed a framebuffer struct info fbdev struct, instead, a |
| 690 | * framebuffer pointer is preferred and drm_framebuffer_unreference() should be |
| 691 | * called when the framebuffer is to be cleaned up. |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 692 | */ |
| 693 | void drm_framebuffer_unregister_private(struct drm_framebuffer *fb) |
| 694 | { |
| 695 | struct drm_device *dev; |
| 696 | |
| 697 | if (!fb) |
| 698 | return; |
| 699 | |
| 700 | dev = fb->dev; |
| 701 | |
| 702 | /* Mark fb as reaped and drop idr ref. */ |
| 703 | drm_mode_object_unregister(dev, &fb->base); |
| 704 | } |
| 705 | EXPORT_SYMBOL(drm_framebuffer_unregister_private); |
| 706 | |
| 707 | /** |
| 708 | * drm_framebuffer_cleanup - remove a framebuffer object |
| 709 | * @fb: framebuffer to remove |
| 710 | * |
| 711 | * Cleanup framebuffer. This function is intended to be used from the drivers |
Daniel Vetter | d574528 | 2017-01-25 07:26:45 +0100 | [diff] [blame^] | 712 | * &drm_framebuffer_funcs.destroy callback. It can also be used to clean up |
| 713 | * driver private framebuffers embedded into a larger structure. |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 714 | * |
Daniel Vetter | d574528 | 2017-01-25 07:26:45 +0100 | [diff] [blame^] | 715 | * Note that this function does not remove the fb from active usage - if it is |
Daniel Vetter | 7520a27 | 2016-08-15 16:07:02 +0200 | [diff] [blame] | 716 | * still used anywhere, hilarity can ensue since userspace could call getfb on |
| 717 | * the id and get back -EINVAL. Obviously no concern at driver unload time. |
| 718 | * |
| 719 | * Also, the framebuffer will not be removed from the lookup idr - for |
| 720 | * user-created framebuffers this will happen in in the rmfb ioctl. For |
| 721 | * driver-private objects (e.g. for fbdev) drivers need to explicitly call |
| 722 | * drm_framebuffer_unregister_private. |
| 723 | */ |
| 724 | void drm_framebuffer_cleanup(struct drm_framebuffer *fb) |
| 725 | { |
| 726 | struct drm_device *dev = fb->dev; |
| 727 | |
| 728 | mutex_lock(&dev->mode_config.fb_lock); |
| 729 | list_del(&fb->head); |
| 730 | dev->mode_config.num_fb--; |
| 731 | mutex_unlock(&dev->mode_config.fb_lock); |
| 732 | } |
| 733 | EXPORT_SYMBOL(drm_framebuffer_cleanup); |
| 734 | |
| 735 | /** |
| 736 | * drm_framebuffer_remove - remove and unreference a framebuffer object |
| 737 | * @fb: framebuffer to remove |
| 738 | * |
| 739 | * Scans all the CRTCs and planes in @dev's mode_config. If they're |
| 740 | * using @fb, removes it, setting it to NULL. Then drops the reference to the |
| 741 | * passed-in framebuffer. Might take the modeset locks. |
| 742 | * |
| 743 | * Note that this function optimizes the cleanup away if the caller holds the |
| 744 | * last reference to the framebuffer. It is also guaranteed to not take the |
| 745 | * modeset locks in this case. |
| 746 | */ |
| 747 | void drm_framebuffer_remove(struct drm_framebuffer *fb) |
| 748 | { |
| 749 | struct drm_device *dev; |
| 750 | struct drm_crtc *crtc; |
| 751 | struct drm_plane *plane; |
| 752 | |
| 753 | if (!fb) |
| 754 | return; |
| 755 | |
| 756 | dev = fb->dev; |
| 757 | |
| 758 | WARN_ON(!list_empty(&fb->filp_head)); |
| 759 | |
| 760 | /* |
| 761 | * drm ABI mandates that we remove any deleted framebuffers from active |
| 762 | * useage. But since most sane clients only remove framebuffers they no |
| 763 | * longer need, try to optimize this away. |
| 764 | * |
| 765 | * Since we're holding a reference ourselves, observing a refcount of 1 |
| 766 | * means that we're the last holder and can skip it. Also, the refcount |
| 767 | * can never increase from 1 again, so we don't need any barriers or |
| 768 | * locks. |
| 769 | * |
| 770 | * Note that userspace could try to race with use and instate a new |
| 771 | * usage _after_ we've cleared all current ones. End result will be an |
| 772 | * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot |
| 773 | * in this manner. |
| 774 | */ |
| 775 | if (drm_framebuffer_read_refcount(fb) > 1) { |
| 776 | drm_modeset_lock_all(dev); |
| 777 | /* remove from any CRTC */ |
| 778 | drm_for_each_crtc(crtc, dev) { |
| 779 | if (crtc->primary->fb == fb) { |
| 780 | /* should turn off the crtc */ |
| 781 | if (drm_crtc_force_disable(crtc)) |
| 782 | DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | drm_for_each_plane(plane, dev) { |
| 787 | if (plane->fb == fb) |
| 788 | drm_plane_force_disable(plane); |
| 789 | } |
| 790 | drm_modeset_unlock_all(dev); |
| 791 | } |
| 792 | |
| 793 | drm_framebuffer_unreference(fb); |
| 794 | } |
| 795 | EXPORT_SYMBOL(drm_framebuffer_remove); |
Ville Syrjälä | 8f8f6a6 | 2016-11-18 21:53:05 +0200 | [diff] [blame] | 796 | |
| 797 | /** |
| 798 | * drm_framebuffer_plane_width - width of the plane given the first plane |
| 799 | * @width: width of the first plane |
| 800 | * @fb: the framebuffer |
| 801 | * @plane: plane index |
| 802 | * |
| 803 | * Returns: |
| 804 | * The width of @plane, given that the width of the first plane is @width. |
| 805 | */ |
| 806 | int drm_framebuffer_plane_width(int width, |
| 807 | const struct drm_framebuffer *fb, int plane) |
| 808 | { |
| 809 | if (plane >= fb->format->num_planes) |
| 810 | return 0; |
| 811 | |
| 812 | if (plane == 0) |
| 813 | return width; |
| 814 | |
| 815 | return width / fb->format->hsub; |
| 816 | } |
| 817 | EXPORT_SYMBOL(drm_framebuffer_plane_width); |
| 818 | |
| 819 | /** |
| 820 | * drm_framebuffer_plane_height - height of the plane given the first plane |
| 821 | * @height: height of the first plane |
| 822 | * @fb: the framebuffer |
| 823 | * @plane: plane index |
| 824 | * |
| 825 | * Returns: |
| 826 | * The height of @plane, given that the height of the first plane is @height. |
| 827 | */ |
| 828 | int drm_framebuffer_plane_height(int height, |
| 829 | const struct drm_framebuffer *fb, int plane) |
| 830 | { |
| 831 | if (plane >= fb->format->num_planes) |
| 832 | return 0; |
| 833 | |
| 834 | if (plane == 0) |
| 835 | return height; |
| 836 | |
| 837 | return height / fb->format->vsub; |
| 838 | } |
| 839 | EXPORT_SYMBOL(drm_framebuffer_plane_height); |