17520a277SDaniel Vetter /* 27520a277SDaniel Vetter * Copyright (c) 2016 Intel Corporation 37520a277SDaniel Vetter * 47520a277SDaniel Vetter * Permission to use, copy, modify, distribute, and sell this software and its 57520a277SDaniel Vetter * documentation for any purpose is hereby granted without fee, provided that 67520a277SDaniel Vetter * the above copyright notice appear in all copies and that both that copyright 77520a277SDaniel Vetter * notice and this permission notice appear in supporting documentation, and 87520a277SDaniel Vetter * that the name of the copyright holders not be used in advertising or 97520a277SDaniel Vetter * publicity pertaining to distribution of the software without specific, 107520a277SDaniel Vetter * written prior permission. The copyright holders make no representations 117520a277SDaniel Vetter * about the suitability of this software for any purpose. It is provided "as 127520a277SDaniel Vetter * is" without express or implied warranty. 137520a277SDaniel Vetter * 147520a277SDaniel Vetter * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 157520a277SDaniel Vetter * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 167520a277SDaniel Vetter * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 177520a277SDaniel Vetter * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 187520a277SDaniel Vetter * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 197520a277SDaniel Vetter * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 207520a277SDaniel Vetter * OF THIS SOFTWARE. 217520a277SDaniel Vetter */ 227520a277SDaniel Vetter 237520a277SDaniel Vetter #ifndef __DRM_FRAMEBUFFER_H__ 247520a277SDaniel Vetter #define __DRM_FRAMEBUFFER_H__ 257520a277SDaniel Vetter 267520a277SDaniel Vetter #include <linux/list.h> 277520a277SDaniel Vetter #include <linux/ctype.h> 287520a277SDaniel Vetter #include <drm/drm_modeset.h> 297520a277SDaniel Vetter 307520a277SDaniel Vetter struct drm_framebuffer; 317520a277SDaniel Vetter struct drm_file; 327520a277SDaniel Vetter struct drm_device; 337520a277SDaniel Vetter 347520a277SDaniel Vetter /** 357520a277SDaniel Vetter * struct drm_framebuffer_funcs - framebuffer hooks 367520a277SDaniel Vetter */ 377520a277SDaniel Vetter struct drm_framebuffer_funcs { 387520a277SDaniel Vetter /** 397520a277SDaniel Vetter * @destroy: 407520a277SDaniel Vetter * 417520a277SDaniel Vetter * Clean up framebuffer resources, specifically also unreference the 427520a277SDaniel Vetter * backing storage. The core guarantees to call this function for every 437520a277SDaniel Vetter * framebuffer successfully created by ->fb_create() in 447520a277SDaniel Vetter * &drm_mode_config_funcs. Drivers must also call 457520a277SDaniel Vetter * drm_framebuffer_cleanup() to release DRM core resources for this 467520a277SDaniel Vetter * framebuffer. 477520a277SDaniel Vetter */ 487520a277SDaniel Vetter void (*destroy)(struct drm_framebuffer *framebuffer); 497520a277SDaniel Vetter 507520a277SDaniel Vetter /** 517520a277SDaniel Vetter * @create_handle: 527520a277SDaniel Vetter * 537520a277SDaniel Vetter * Create a buffer handle in the driver-specific buffer manager (either 547520a277SDaniel Vetter * GEM or TTM) valid for the passed-in struct &drm_file. This is used by 557520a277SDaniel Vetter * the core to implement the GETFB IOCTL, which returns (for 567520a277SDaniel Vetter * sufficiently priviledged user) also a native buffer handle. This can 577520a277SDaniel Vetter * be used for seamless transitions between modesetting clients by 587520a277SDaniel Vetter * copying the current screen contents to a private buffer and blending 597520a277SDaniel Vetter * between that and the new contents. 607520a277SDaniel Vetter * 617520a277SDaniel Vetter * GEM based drivers should call drm_gem_handle_create() to create the 627520a277SDaniel Vetter * handle. 637520a277SDaniel Vetter * 647520a277SDaniel Vetter * RETURNS: 657520a277SDaniel Vetter * 667520a277SDaniel Vetter * 0 on success or a negative error code on failure. 677520a277SDaniel Vetter */ 687520a277SDaniel Vetter int (*create_handle)(struct drm_framebuffer *fb, 697520a277SDaniel Vetter struct drm_file *file_priv, 707520a277SDaniel Vetter unsigned int *handle); 717520a277SDaniel Vetter /** 727520a277SDaniel Vetter * @dirty: 737520a277SDaniel Vetter * 747520a277SDaniel Vetter * Optional callback for the dirty fb IOCTL. 757520a277SDaniel Vetter * 767520a277SDaniel Vetter * Userspace can notify the driver via this callback that an area of the 777520a277SDaniel Vetter * framebuffer has changed and should be flushed to the display 787520a277SDaniel Vetter * hardware. This can also be used internally, e.g. by the fbdev 797520a277SDaniel Vetter * emulation, though that's not the case currently. 807520a277SDaniel Vetter * 817520a277SDaniel Vetter * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd 827520a277SDaniel Vetter * for more information as all the semantics and arguments have a one to 837520a277SDaniel Vetter * one mapping on this function. 847520a277SDaniel Vetter * 857520a277SDaniel Vetter * RETURNS: 867520a277SDaniel Vetter * 877520a277SDaniel Vetter * 0 on success or a negative error code on failure. 887520a277SDaniel Vetter */ 897520a277SDaniel Vetter int (*dirty)(struct drm_framebuffer *framebuffer, 907520a277SDaniel Vetter struct drm_file *file_priv, unsigned flags, 917520a277SDaniel Vetter unsigned color, struct drm_clip_rect *clips, 927520a277SDaniel Vetter unsigned num_clips); 937520a277SDaniel Vetter }; 947520a277SDaniel Vetter 957520a277SDaniel Vetter struct drm_framebuffer { 967520a277SDaniel Vetter struct drm_device *dev; 977520a277SDaniel Vetter /* 987520a277SDaniel Vetter * Note that the fb is refcounted for the benefit of driver internals, 997520a277SDaniel Vetter * for example some hw, disabling a CRTC/plane is asynchronous, and 1007520a277SDaniel Vetter * scanout does not actually complete until the next vblank. So some 1017520a277SDaniel Vetter * cleanup (like releasing the reference(s) on the backing GEM bo(s)) 1027520a277SDaniel Vetter * should be deferred. In cases like this, the driver would like to 1037520a277SDaniel Vetter * hold a ref to the fb even though it has already been removed from 1047520a277SDaniel Vetter * userspace perspective. 1057520a277SDaniel Vetter * The refcount is stored inside the mode object. 1067520a277SDaniel Vetter */ 1077520a277SDaniel Vetter /* 1087520a277SDaniel Vetter * Place on the dev->mode_config.fb_list, access protected by 1097520a277SDaniel Vetter * dev->mode_config.fb_lock. 1107520a277SDaniel Vetter */ 1117520a277SDaniel Vetter struct list_head head; 1127520a277SDaniel Vetter struct drm_mode_object base; 1137520a277SDaniel Vetter const struct drm_framebuffer_funcs *funcs; 1147520a277SDaniel Vetter unsigned int pitches[4]; 1157520a277SDaniel Vetter unsigned int offsets[4]; 1167520a277SDaniel Vetter uint64_t modifier[4]; 1177520a277SDaniel Vetter unsigned int width; 1187520a277SDaniel Vetter unsigned int height; 1197520a277SDaniel Vetter /* depth can be 15 or 16 */ 1207520a277SDaniel Vetter unsigned int depth; 1217520a277SDaniel Vetter int bits_per_pixel; 1227520a277SDaniel Vetter int flags; 1237520a277SDaniel Vetter uint32_t pixel_format; /* fourcc format */ 1247520a277SDaniel Vetter int hot_x; 1257520a277SDaniel Vetter int hot_y; 1267520a277SDaniel Vetter struct list_head filp_head; 1277520a277SDaniel Vetter }; 1287520a277SDaniel Vetter 1297520a277SDaniel Vetter int drm_framebuffer_init(struct drm_device *dev, 1307520a277SDaniel Vetter struct drm_framebuffer *fb, 1317520a277SDaniel Vetter const struct drm_framebuffer_funcs *funcs); 1327520a277SDaniel Vetter struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, 1337520a277SDaniel Vetter uint32_t id); 1347520a277SDaniel Vetter void drm_framebuffer_remove(struct drm_framebuffer *fb); 1357520a277SDaniel Vetter void drm_framebuffer_cleanup(struct drm_framebuffer *fb); 1367520a277SDaniel Vetter void drm_framebuffer_unregister_private(struct drm_framebuffer *fb); 1377520a277SDaniel Vetter 1387520a277SDaniel Vetter /** 1397520a277SDaniel Vetter * drm_framebuffer_reference - incr the fb refcnt 1407520a277SDaniel Vetter * @fb: framebuffer 1417520a277SDaniel Vetter * 1427520a277SDaniel Vetter * This functions increments the fb's refcount. 1437520a277SDaniel Vetter */ 1447520a277SDaniel Vetter static inline void drm_framebuffer_reference(struct drm_framebuffer *fb) 1457520a277SDaniel Vetter { 1467520a277SDaniel Vetter drm_mode_object_reference(&fb->base); 1477520a277SDaniel Vetter } 1487520a277SDaniel Vetter 1497520a277SDaniel Vetter /** 1507520a277SDaniel Vetter * drm_framebuffer_unreference - unref a framebuffer 1517520a277SDaniel Vetter * @fb: framebuffer to unref 1527520a277SDaniel Vetter * 1537520a277SDaniel Vetter * This functions decrements the fb's refcount and frees it if it drops to zero. 1547520a277SDaniel Vetter */ 1557520a277SDaniel Vetter static inline void drm_framebuffer_unreference(struct drm_framebuffer *fb) 1567520a277SDaniel Vetter { 1577520a277SDaniel Vetter drm_mode_object_unreference(&fb->base); 1587520a277SDaniel Vetter } 1597520a277SDaniel Vetter 1607520a277SDaniel Vetter /** 1617520a277SDaniel Vetter * drm_framebuffer_read_refcount - read the framebuffer reference count. 1627520a277SDaniel Vetter * @fb: framebuffer 1637520a277SDaniel Vetter * 1647520a277SDaniel Vetter * This functions returns the framebuffer's reference count. 1657520a277SDaniel Vetter */ 1667520a277SDaniel Vetter static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb) 1677520a277SDaniel Vetter { 1687520a277SDaniel Vetter return atomic_read(&fb->base.refcount.refcount); 1697520a277SDaniel Vetter } 1707520a277SDaniel Vetter #endif 171