blob: c23016748e3fc9f109b100da6664890b7bf9bc9f [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#ifndef __DRM_FRAMEBUFFER_H__
24#define __DRM_FRAMEBUFFER_H__
25
Daniel Vetter7520a272016-08-15 16:07:02 +020026#include <linux/ctype.h>
Sam Ravnborg25131472019-01-08 20:29:30 +010027#include <linux/list.h>
28#include <linux/sched.h>
29
Daniel Vetter949619f2016-08-29 10:27:51 +020030#include <drm/drm_mode_object.h>
Daniel Vetter7520a272016-08-15 16:07:02 +020031
Sam Ravnborg25131472019-01-08 20:29:30 +010032struct drm_clip_rect;
Daniel Vetter7520a272016-08-15 16:07:02 +020033struct drm_device;
Sam Ravnborg25131472019-01-08 20:29:30 +010034struct drm_file;
Laurent Pinchart2471e7a2019-03-13 12:29:05 +020035struct drm_format_info;
Sam Ravnborg25131472019-01-08 20:29:30 +010036struct drm_framebuffer;
37struct drm_gem_object;
Daniel Vetter7520a272016-08-15 16:07:02 +020038
39/**
40 * struct drm_framebuffer_funcs - framebuffer hooks
41 */
42struct drm_framebuffer_funcs {
43 /**
44 * @destroy:
45 *
46 * Clean up framebuffer resources, specifically also unreference the
47 * backing storage. The core guarantees to call this function for every
Daniel Vetterd5745282017-01-25 07:26:45 +010048 * framebuffer successfully created by calling
49 * &drm_mode_config_funcs.fb_create. Drivers must also call
Daniel Vetter7520a272016-08-15 16:07:02 +020050 * drm_framebuffer_cleanup() to release DRM core resources for this
51 * framebuffer.
52 */
53 void (*destroy)(struct drm_framebuffer *framebuffer);
54
55 /**
56 * @create_handle:
57 *
58 * Create a buffer handle in the driver-specific buffer manager (either
Daniel Vetterea0dd852016-12-29 21:48:26 +010059 * GEM or TTM) valid for the passed-in &struct drm_file. This is used by
Daniel Vetter7520a272016-08-15 16:07:02 +020060 * the core to implement the GETFB IOCTL, which returns (for
61 * sufficiently priviledged user) also a native buffer handle. This can
62 * be used for seamless transitions between modesetting clients by
63 * copying the current screen contents to a private buffer and blending
64 * between that and the new contents.
65 *
66 * GEM based drivers should call drm_gem_handle_create() to create the
67 * handle.
68 *
69 * RETURNS:
70 *
71 * 0 on success or a negative error code on failure.
72 */
73 int (*create_handle)(struct drm_framebuffer *fb,
74 struct drm_file *file_priv,
75 unsigned int *handle);
76 /**
77 * @dirty:
78 *
79 * Optional callback for the dirty fb IOCTL.
80 *
81 * Userspace can notify the driver via this callback that an area of the
82 * framebuffer has changed and should be flushed to the display
83 * hardware. This can also be used internally, e.g. by the fbdev
84 * emulation, though that's not the case currently.
85 *
86 * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
87 * for more information as all the semantics and arguments have a one to
88 * one mapping on this function.
89 *
90 * RETURNS:
91 *
92 * 0 on success or a negative error code on failure.
93 */
94 int (*dirty)(struct drm_framebuffer *framebuffer,
95 struct drm_file *file_priv, unsigned flags,
96 unsigned color, struct drm_clip_rect *clips,
97 unsigned num_clips);
98};
99
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200100/**
101 * struct drm_framebuffer - frame buffer object
102 *
103 * Note that the fb is refcounted for the benefit of driver internals,
104 * for example some hw, disabling a CRTC/plane is asynchronous, and
105 * scanout does not actually complete until the next vblank. So some
106 * cleanup (like releasing the reference(s) on the backing GEM bo(s))
107 * should be deferred. In cases like this, the driver would like to
108 * hold a ref to the fb even though it has already been removed from
Thierry Redinga4a69da2017-02-28 15:46:40 +0100109 * userspace perspective. See drm_framebuffer_get() and
110 * drm_framebuffer_put().
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200111 *
112 * The refcount is stored inside the mode object @base.
113 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200114struct drm_framebuffer {
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200115 /**
116 * @dev: DRM device this framebuffer belongs to
Daniel Vetter7520a272016-08-15 16:07:02 +0200117 */
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200118 struct drm_device *dev;
119 /**
Daniel Vetterd5745282017-01-25 07:26:45 +0100120 * @head: Place on the &drm_mode_config.fb_list, access protected by
121 * &drm_mode_config.fb_lock.
Daniel Vetter7520a272016-08-15 16:07:02 +0200122 */
123 struct list_head head;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200124
125 /**
126 * @base: base modeset object structure, contains the reference count.
127 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200128 struct drm_mode_object base;
Maarten Lankhorst8d44e9e2017-12-20 10:35:44 +0100129
130 /**
131 * @comm: Name of the process allocating the fb, used for fb dumping.
132 */
133 char comm[TASK_COMM_LEN];
134
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200135 /**
Ville Syrjäläe14c23c2016-11-18 21:52:55 +0200136 * @format: framebuffer format information
137 */
138 const struct drm_format_info *format;
139 /**
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200140 * @funcs: framebuffer vfunc table
141 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200142 const struct drm_framebuffer_funcs *funcs;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200143 /**
144 * @pitches: Line stride per buffer. For userspace created object this
145 * is copied from drm_mode_fb_cmd2.
146 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200147 unsigned int pitches[4];
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200148 /**
149 * @offsets: Offset from buffer start to the actual pixel data in bytes,
150 * per buffer. For userspace created object this is copied from
151 * drm_mode_fb_cmd2.
152 *
153 * Note that this is a linear offset and does not take into account
154 * tiling or buffer laytou per @modifier. It meant to be used when the
155 * actual pixel data for this framebuffer plane starts at an offset,
156 * e.g. when multiple planes are allocated within the same backing
157 * storage buffer object. For tiled layouts this generally means it
158 * @offsets must at least be tile-size aligned, but hardware often has
159 * stricter requirements.
160 *
161 * This should not be used to specifiy x/y pixel offsets into the buffer
162 * data (even for linear buffers). Specifying an x/y pixel offset is
Daniel Vetterea0dd852016-12-29 21:48:26 +0100163 * instead done through the source rectangle in &struct drm_plane_state.
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200164 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200165 unsigned int offsets[4];
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200166 /**
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200167 * @modifier: Data layout modifier. This is used to describe
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200168 * tiling, or also special layouts (like compression) of auxiliary
169 * buffers. For userspace created object this is copied from
170 * drm_mode_fb_cmd2.
171 */
Ville Syrjäläbae781b2016-11-16 13:33:16 +0200172 uint64_t modifier;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200173 /**
174 * @width: Logical width of the visible area of the framebuffer, in
175 * pixels.
176 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200177 unsigned int width;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200178 /**
179 * @height: Logical height of the visible area of the framebuffer, in
180 * pixels.
181 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200182 unsigned int height;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200183 /**
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200184 * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
185 * DRM_MODE_FB_MODIFIERS.
186 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200187 int flags;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200188 /**
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200189 * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
190 * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
191 * universal plane.
192 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200193 int hot_x;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200194 /**
195 * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor
196 * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
197 * universal plane.
198 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200199 int hot_y;
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200200 /**
Daniel Vetterd5745282017-01-25 07:26:45 +0100201 * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock.
Daniel Vetter750fb8c2016-08-12 22:48:48 +0200202 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200203 struct list_head filp_head;
Noralf Trønnes4c3dbb22017-08-13 15:31:44 +0200204 /**
205 * @obj: GEM objects backing the framebuffer, one per plane (optional).
206 *
207 * This is used by the GEM framebuffer helpers, see e.g.
208 * drm_gem_fb_create().
209 */
210 struct drm_gem_object *obj[4];
Daniel Vetter7520a272016-08-15 16:07:02 +0200211};
212
Daniel Vetterafb21ea62016-08-31 18:09:04 +0200213#define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
214
Daniel Vetter7520a272016-08-15 16:07:02 +0200215int drm_framebuffer_init(struct drm_device *dev,
216 struct drm_framebuffer *fb,
217 const struct drm_framebuffer_funcs *funcs);
218struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
Keith Packard418da172017-03-14 23:25:07 -0700219 struct drm_file *file_priv,
Daniel Vetter7520a272016-08-15 16:07:02 +0200220 uint32_t id);
221void drm_framebuffer_remove(struct drm_framebuffer *fb);
222void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
223void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
224
225/**
Thierry Redinga4a69da2017-02-28 15:46:40 +0100226 * drm_framebuffer_get - acquire a framebuffer reference
227 * @fb: DRM framebuffer
Daniel Vetter7520a272016-08-15 16:07:02 +0200228 *
Thierry Redinga4a69da2017-02-28 15:46:40 +0100229 * This function increments the framebuffer's reference count.
Daniel Vetter7520a272016-08-15 16:07:02 +0200230 */
Thierry Redinga4a69da2017-02-28 15:46:40 +0100231static inline void drm_framebuffer_get(struct drm_framebuffer *fb)
Daniel Vetter7520a272016-08-15 16:07:02 +0200232{
Thierry Redinga4a69da2017-02-28 15:46:40 +0100233 drm_mode_object_get(&fb->base);
Daniel Vetter7520a272016-08-15 16:07:02 +0200234}
235
236/**
Thierry Redinga4a69da2017-02-28 15:46:40 +0100237 * drm_framebuffer_put - release a framebuffer reference
238 * @fb: DRM framebuffer
Daniel Vetter7520a272016-08-15 16:07:02 +0200239 *
Thierry Redinga4a69da2017-02-28 15:46:40 +0100240 * This function decrements the framebuffer's reference count and frees the
241 * framebuffer if the reference count drops to zero.
242 */
243static inline void drm_framebuffer_put(struct drm_framebuffer *fb)
244{
245 drm_mode_object_put(&fb->base);
246}
247
248/**
Daniel Vetter7520a272016-08-15 16:07:02 +0200249 * drm_framebuffer_read_refcount - read the framebuffer reference count.
250 * @fb: framebuffer
251 *
252 * This functions returns the framebuffer's reference count.
253 */
Noralf Trønnes6ff10862017-11-07 20:13:38 +0100254static inline uint32_t drm_framebuffer_read_refcount(const struct drm_framebuffer *fb)
Daniel Vetter7520a272016-08-15 16:07:02 +0200255{
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100256 return kref_read(&fb->base.refcount);
Daniel Vetter7520a272016-08-15 16:07:02 +0200257}
Daniel Vetterafb21ea62016-08-31 18:09:04 +0200258
259/**
Chris Wilson389f78b2016-11-25 15:32:30 +0000260 * drm_framebuffer_assign - store a reference to the fb
261 * @p: location to store framebuffer
262 * @fb: new framebuffer (maybe NULL)
263 *
264 * This functions sets the location to store a reference to the framebuffer,
265 * unreferencing the framebuffer that was previously stored in that location.
266 */
267static inline void drm_framebuffer_assign(struct drm_framebuffer **p,
268 struct drm_framebuffer *fb)
269{
270 if (fb)
Thierry Redinga4a69da2017-02-28 15:46:40 +0100271 drm_framebuffer_get(fb);
Chris Wilson389f78b2016-11-25 15:32:30 +0000272 if (*p)
Thierry Redinga4a69da2017-02-28 15:46:40 +0100273 drm_framebuffer_put(*p);
Chris Wilson389f78b2016-11-25 15:32:30 +0000274 *p = fb;
275}
276
277/*
Daniel Vetterafb21ea62016-08-31 18:09:04 +0200278 * drm_for_each_fb - iterate over all framebuffers
279 * @fb: the loop cursor
280 * @dev: the DRM device
281 *
Daniel Vetterd5745282017-01-25 07:26:45 +0100282 * Iterate over all framebuffers of @dev. User must hold
283 * &drm_mode_config.fb_lock.
Daniel Vetterafb21ea62016-08-31 18:09:04 +0200284 */
285#define drm_for_each_fb(fb, dev) \
286 for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \
287 fb = list_first_entry(&(dev)->mode_config.fb_list, \
288 struct drm_framebuffer, head); \
289 &fb->head != (&(dev)->mode_config.fb_list); \
290 fb = list_next_entry(fb, head))
Ville Syrjälä8f8f6a62016-11-18 21:53:05 +0200291
292int drm_framebuffer_plane_width(int width,
293 const struct drm_framebuffer *fb, int plane);
294int drm_framebuffer_plane_height(int height,
295 const struct drm_framebuffer *fb, int plane);
296
Daniel Vetter7520a272016-08-15 16:07:02 +0200297#endif