1 1.1 riastrad /* $NetBSD: drm_framebuffer.h,v 1.2 2021/12/18 23:45:46 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 #ifndef __DRM_FRAMEBUFFER_H__ 26 1.1 riastrad #define __DRM_FRAMEBUFFER_H__ 27 1.1 riastrad 28 1.1 riastrad #include <linux/ctype.h> 29 1.1 riastrad #include <linux/list.h> 30 1.1 riastrad #include <linux/sched.h> 31 1.1 riastrad 32 1.1 riastrad #include <drm/drm_mode_object.h> 33 1.1 riastrad 34 1.1 riastrad struct drm_clip_rect; 35 1.1 riastrad struct drm_device; 36 1.1 riastrad struct drm_file; 37 1.1 riastrad struct drm_format_info; 38 1.1 riastrad struct drm_framebuffer; 39 1.1 riastrad struct drm_gem_object; 40 1.1 riastrad 41 1.1 riastrad /** 42 1.1 riastrad * struct drm_framebuffer_funcs - framebuffer hooks 43 1.1 riastrad */ 44 1.1 riastrad struct drm_framebuffer_funcs { 45 1.1 riastrad /** 46 1.1 riastrad * @destroy: 47 1.1 riastrad * 48 1.1 riastrad * Clean up framebuffer resources, specifically also unreference the 49 1.1 riastrad * backing storage. The core guarantees to call this function for every 50 1.1 riastrad * framebuffer successfully created by calling 51 1.1 riastrad * &drm_mode_config_funcs.fb_create. Drivers must also call 52 1.1 riastrad * drm_framebuffer_cleanup() to release DRM core resources for this 53 1.1 riastrad * framebuffer. 54 1.1 riastrad */ 55 1.1 riastrad void (*destroy)(struct drm_framebuffer *framebuffer); 56 1.1 riastrad 57 1.1 riastrad /** 58 1.1 riastrad * @create_handle: 59 1.1 riastrad * 60 1.1 riastrad * Create a buffer handle in the driver-specific buffer manager (either 61 1.1 riastrad * GEM or TTM) valid for the passed-in &struct drm_file. This is used by 62 1.1 riastrad * the core to implement the GETFB IOCTL, which returns (for 63 1.1 riastrad * sufficiently priviledged user) also a native buffer handle. This can 64 1.1 riastrad * be used for seamless transitions between modesetting clients by 65 1.1 riastrad * copying the current screen contents to a private buffer and blending 66 1.1 riastrad * between that and the new contents. 67 1.1 riastrad * 68 1.1 riastrad * GEM based drivers should call drm_gem_handle_create() to create the 69 1.1 riastrad * handle. 70 1.1 riastrad * 71 1.1 riastrad * RETURNS: 72 1.1 riastrad * 73 1.1 riastrad * 0 on success or a negative error code on failure. 74 1.1 riastrad */ 75 1.1 riastrad int (*create_handle)(struct drm_framebuffer *fb, 76 1.1 riastrad struct drm_file *file_priv, 77 1.1 riastrad unsigned int *handle); 78 1.1 riastrad /** 79 1.1 riastrad * @dirty: 80 1.1 riastrad * 81 1.1 riastrad * Optional callback for the dirty fb IOCTL. 82 1.1 riastrad * 83 1.1 riastrad * Userspace can notify the driver via this callback that an area of the 84 1.1 riastrad * framebuffer has changed and should be flushed to the display 85 1.1 riastrad * hardware. This can also be used internally, e.g. by the fbdev 86 1.1 riastrad * emulation, though that's not the case currently. 87 1.1 riastrad * 88 1.1 riastrad * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd 89 1.1 riastrad * for more information as all the semantics and arguments have a one to 90 1.1 riastrad * one mapping on this function. 91 1.1 riastrad * 92 1.1 riastrad * Atomic drivers should use drm_atomic_helper_dirtyfb() to implement 93 1.1 riastrad * this hook. 94 1.1 riastrad * 95 1.1 riastrad * RETURNS: 96 1.1 riastrad * 97 1.1 riastrad * 0 on success or a negative error code on failure. 98 1.1 riastrad */ 99 1.1 riastrad int (*dirty)(struct drm_framebuffer *framebuffer, 100 1.1 riastrad struct drm_file *file_priv, unsigned flags, 101 1.1 riastrad unsigned color, struct drm_clip_rect *clips, 102 1.1 riastrad unsigned num_clips); 103 1.1 riastrad }; 104 1.1 riastrad 105 1.1 riastrad /** 106 1.1 riastrad * struct drm_framebuffer - frame buffer object 107 1.1 riastrad * 108 1.1 riastrad * Note that the fb is refcounted for the benefit of driver internals, 109 1.1 riastrad * for example some hw, disabling a CRTC/plane is asynchronous, and 110 1.1 riastrad * scanout does not actually complete until the next vblank. So some 111 1.1 riastrad * cleanup (like releasing the reference(s) on the backing GEM bo(s)) 112 1.1 riastrad * should be deferred. In cases like this, the driver would like to 113 1.1 riastrad * hold a ref to the fb even though it has already been removed from 114 1.1 riastrad * userspace perspective. See drm_framebuffer_get() and 115 1.1 riastrad * drm_framebuffer_put(). 116 1.1 riastrad * 117 1.1 riastrad * The refcount is stored inside the mode object @base. 118 1.1 riastrad */ 119 1.1 riastrad struct drm_framebuffer { 120 1.1 riastrad /** 121 1.1 riastrad * @dev: DRM device this framebuffer belongs to 122 1.1 riastrad */ 123 1.1 riastrad struct drm_device *dev; 124 1.1 riastrad /** 125 1.1 riastrad * @head: Place on the &drm_mode_config.fb_list, access protected by 126 1.1 riastrad * &drm_mode_config.fb_lock. 127 1.1 riastrad */ 128 1.1 riastrad struct list_head head; 129 1.1 riastrad 130 1.1 riastrad /** 131 1.1 riastrad * @base: base modeset object structure, contains the reference count. 132 1.1 riastrad */ 133 1.1 riastrad struct drm_mode_object base; 134 1.1 riastrad 135 1.1 riastrad /** 136 1.1 riastrad * @comm: Name of the process allocating the fb, used for fb dumping. 137 1.1 riastrad */ 138 1.1 riastrad char comm[TASK_COMM_LEN]; 139 1.1 riastrad 140 1.1 riastrad /** 141 1.1 riastrad * @format: framebuffer format information 142 1.1 riastrad */ 143 1.1 riastrad const struct drm_format_info *format; 144 1.1 riastrad /** 145 1.1 riastrad * @funcs: framebuffer vfunc table 146 1.1 riastrad */ 147 1.1 riastrad const struct drm_framebuffer_funcs *funcs; 148 1.1 riastrad /** 149 1.1 riastrad * @pitches: Line stride per buffer. For userspace created object this 150 1.1 riastrad * is copied from drm_mode_fb_cmd2. 151 1.1 riastrad */ 152 1.1 riastrad unsigned int pitches[4]; 153 1.1 riastrad /** 154 1.1 riastrad * @offsets: Offset from buffer start to the actual pixel data in bytes, 155 1.1 riastrad * per buffer. For userspace created object this is copied from 156 1.1 riastrad * drm_mode_fb_cmd2. 157 1.1 riastrad * 158 1.1 riastrad * Note that this is a linear offset and does not take into account 159 1.1 riastrad * tiling or buffer laytou per @modifier. It meant to be used when the 160 1.1 riastrad * actual pixel data for this framebuffer plane starts at an offset, 161 1.1 riastrad * e.g. when multiple planes are allocated within the same backing 162 1.1 riastrad * storage buffer object. For tiled layouts this generally means it 163 1.1 riastrad * @offsets must at least be tile-size aligned, but hardware often has 164 1.1 riastrad * stricter requirements. 165 1.1 riastrad * 166 1.1 riastrad * This should not be used to specifiy x/y pixel offsets into the buffer 167 1.1 riastrad * data (even for linear buffers). Specifying an x/y pixel offset is 168 1.1 riastrad * instead done through the source rectangle in &struct drm_plane_state. 169 1.1 riastrad */ 170 1.1 riastrad unsigned int offsets[4]; 171 1.1 riastrad /** 172 1.1 riastrad * @modifier: Data layout modifier. This is used to describe 173 1.1 riastrad * tiling, or also special layouts (like compression) of auxiliary 174 1.1 riastrad * buffers. For userspace created object this is copied from 175 1.1 riastrad * drm_mode_fb_cmd2. 176 1.1 riastrad */ 177 1.1 riastrad uint64_t modifier; 178 1.1 riastrad /** 179 1.1 riastrad * @width: Logical width of the visible area of the framebuffer, in 180 1.1 riastrad * pixels. 181 1.1 riastrad */ 182 1.1 riastrad unsigned int width; 183 1.1 riastrad /** 184 1.1 riastrad * @height: Logical height of the visible area of the framebuffer, in 185 1.1 riastrad * pixels. 186 1.1 riastrad */ 187 1.1 riastrad unsigned int height; 188 1.1 riastrad /** 189 1.1 riastrad * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or 190 1.1 riastrad * DRM_MODE_FB_MODIFIERS. 191 1.1 riastrad */ 192 1.1 riastrad int flags; 193 1.1 riastrad /** 194 1.1 riastrad * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor 195 1.1 riastrad * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR 196 1.1 riastrad * universal plane. 197 1.1 riastrad */ 198 1.1 riastrad int hot_x; 199 1.1 riastrad /** 200 1.1 riastrad * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor 201 1.1 riastrad * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR 202 1.1 riastrad * universal plane. 203 1.1 riastrad */ 204 1.1 riastrad int hot_y; 205 1.1 riastrad /** 206 1.1 riastrad * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock. 207 1.1 riastrad */ 208 1.1 riastrad struct list_head filp_head; 209 1.1 riastrad /** 210 1.1 riastrad * @obj: GEM objects backing the framebuffer, one per plane (optional). 211 1.1 riastrad * 212 1.1 riastrad * This is used by the GEM framebuffer helpers, see e.g. 213 1.1 riastrad * drm_gem_fb_create(). 214 1.1 riastrad */ 215 1.1 riastrad struct drm_gem_object *obj[4]; 216 1.1 riastrad }; 217 1.1 riastrad 218 1.1 riastrad #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base) 219 1.1 riastrad 220 1.1 riastrad int drm_framebuffer_init(struct drm_device *dev, 221 1.1 riastrad struct drm_framebuffer *fb, 222 1.1 riastrad const struct drm_framebuffer_funcs *funcs); 223 1.1 riastrad struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev, 224 1.1 riastrad struct drm_file *file_priv, 225 1.1 riastrad uint32_t id); 226 1.1 riastrad void drm_framebuffer_remove(struct drm_framebuffer *fb); 227 1.1 riastrad void drm_framebuffer_cleanup(struct drm_framebuffer *fb); 228 1.1 riastrad void drm_framebuffer_unregister_private(struct drm_framebuffer *fb); 229 1.1 riastrad 230 1.1 riastrad /** 231 1.1 riastrad * drm_framebuffer_get - acquire a framebuffer reference 232 1.1 riastrad * @fb: DRM framebuffer 233 1.1 riastrad * 234 1.1 riastrad * This function increments the framebuffer's reference count. 235 1.1 riastrad */ 236 1.1 riastrad static inline void drm_framebuffer_get(struct drm_framebuffer *fb) 237 1.1 riastrad { 238 1.1 riastrad drm_mode_object_get(&fb->base); 239 1.1 riastrad } 240 1.1 riastrad 241 1.1 riastrad /** 242 1.1 riastrad * drm_framebuffer_put - release a framebuffer reference 243 1.1 riastrad * @fb: DRM framebuffer 244 1.1 riastrad * 245 1.1 riastrad * This function decrements the framebuffer's reference count and frees the 246 1.1 riastrad * framebuffer if the reference count drops to zero. 247 1.1 riastrad */ 248 1.1 riastrad static inline void drm_framebuffer_put(struct drm_framebuffer *fb) 249 1.1 riastrad { 250 1.1 riastrad drm_mode_object_put(&fb->base); 251 1.1 riastrad } 252 1.1 riastrad 253 1.1 riastrad /** 254 1.1 riastrad * drm_framebuffer_read_refcount - read the framebuffer reference count. 255 1.1 riastrad * @fb: framebuffer 256 1.1 riastrad * 257 1.1 riastrad * This functions returns the framebuffer's reference count. 258 1.1 riastrad */ 259 1.1 riastrad static inline uint32_t drm_framebuffer_read_refcount(const struct drm_framebuffer *fb) 260 1.1 riastrad { 261 1.1 riastrad return kref_read(&fb->base.refcount); 262 1.1 riastrad } 263 1.1 riastrad 264 1.1 riastrad /** 265 1.1 riastrad * drm_framebuffer_assign - store a reference to the fb 266 1.1 riastrad * @p: location to store framebuffer 267 1.1 riastrad * @fb: new framebuffer (maybe NULL) 268 1.1 riastrad * 269 1.1 riastrad * This functions sets the location to store a reference to the framebuffer, 270 1.1 riastrad * unreferencing the framebuffer that was previously stored in that location. 271 1.1 riastrad */ 272 1.1 riastrad static inline void drm_framebuffer_assign(struct drm_framebuffer **p, 273 1.1 riastrad struct drm_framebuffer *fb) 274 1.1 riastrad { 275 1.1 riastrad if (fb) 276 1.1 riastrad drm_framebuffer_get(fb); 277 1.1 riastrad if (*p) 278 1.1 riastrad drm_framebuffer_put(*p); 279 1.1 riastrad *p = fb; 280 1.1 riastrad } 281 1.1 riastrad 282 1.1 riastrad /* 283 1.1 riastrad * drm_for_each_fb - iterate over all framebuffers 284 1.1 riastrad * @fb: the loop cursor 285 1.1 riastrad * @dev: the DRM device 286 1.1 riastrad * 287 1.1 riastrad * Iterate over all framebuffers of @dev. User must hold 288 1.1 riastrad * &drm_mode_config.fb_lock. 289 1.1 riastrad */ 290 1.1 riastrad #define drm_for_each_fb(fb, dev) \ 291 1.1 riastrad for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \ 292 1.1 riastrad fb = list_first_entry(&(dev)->mode_config.fb_list, \ 293 1.1 riastrad struct drm_framebuffer, head); \ 294 1.1 riastrad &fb->head != (&(dev)->mode_config.fb_list); \ 295 1.1 riastrad fb = list_next_entry(fb, head)) 296 1.1 riastrad 297 1.1 riastrad int drm_framebuffer_plane_width(int width, 298 1.1 riastrad const struct drm_framebuffer *fb, int plane); 299 1.1 riastrad int drm_framebuffer_plane_height(int height, 300 1.1 riastrad const struct drm_framebuffer *fb, int plane); 301 1.1 riastrad 302 1.1 riastrad #endif 303