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