xref: /openbmc/linux/include/drm/drm_framebuffer.h (revision b78412b8)
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>
28 #include <drm/drm_mode_object.h>
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
43 	 * framebuffer successfully created by calling
44 	 * &drm_mode_config_funcs.fb_create. Drivers must also call
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
54 	 * GEM or TTM) valid for the passed-in &struct drm_file. This is used by
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 
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
104  * userspace perspective. See drm_framebuffer_get() and
105  * drm_framebuffer_put().
106  *
107  * The refcount is stored inside the mode object @base.
108  */
109 struct drm_framebuffer {
110 	/**
111 	 * @dev: DRM device this framebuffer belongs to
112 	 */
113 	struct drm_device *dev;
114 	/**
115 	 * @head: Place on the &drm_mode_config.fb_list, access protected by
116 	 * &drm_mode_config.fb_lock.
117 	 */
118 	struct list_head head;
119 
120 	/**
121 	 * @base: base modeset object structure, contains the reference count.
122 	 */
123 	struct drm_mode_object base;
124 	/**
125 	 * @format: framebuffer format information
126 	 */
127 	const struct drm_format_info *format;
128 	/**
129 	 * @funcs: framebuffer vfunc table
130 	 */
131 	const struct drm_framebuffer_funcs *funcs;
132 	/**
133 	 * @pitches: Line stride per buffer. For userspace created object this
134 	 * is copied from drm_mode_fb_cmd2.
135 	 */
136 	unsigned int pitches[4];
137 	/**
138 	 * @offsets: Offset from buffer start to the actual pixel data in bytes,
139 	 * per buffer. For userspace created object this is copied from
140 	 * drm_mode_fb_cmd2.
141 	 *
142 	 * Note that this is a linear offset and does not take into account
143 	 * tiling or buffer laytou per @modifier. It meant to be used when the
144 	 * actual pixel data for this framebuffer plane starts at an offset,
145 	 * e.g.  when multiple planes are allocated within the same backing
146 	 * storage buffer object. For tiled layouts this generally means it
147 	 * @offsets must at least be tile-size aligned, but hardware often has
148 	 * stricter requirements.
149 	 *
150 	 * This should not be used to specifiy x/y pixel offsets into the buffer
151 	 * data (even for linear buffers). Specifying an x/y pixel offset is
152 	 * instead done through the source rectangle in &struct drm_plane_state.
153 	 */
154 	unsigned int offsets[4];
155 	/**
156 	 * @modifier: Data layout modifier. This is used to describe
157 	 * tiling, or also special layouts (like compression) of auxiliary
158 	 * buffers. For userspace created object this is copied from
159 	 * drm_mode_fb_cmd2.
160 	 */
161 	uint64_t modifier;
162 	/**
163 	 * @width: Logical width of the visible area of the framebuffer, in
164 	 * pixels.
165 	 */
166 	unsigned int width;
167 	/**
168 	 * @height: Logical height of the visible area of the framebuffer, in
169 	 * pixels.
170 	 */
171 	unsigned int height;
172 	/**
173 	 * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
174 	 * DRM_MODE_FB_MODIFIERS.
175 	 */
176 	int flags;
177 	/**
178 	 * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
179 	 * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
180 	 * universal plane.
181 	 */
182 	int hot_x;
183 	/**
184 	 * @hot_y: Y 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 	 */
188 	int hot_y;
189 	/**
190 	 * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock.
191 	 */
192 	struct list_head filp_head;
193 	/**
194 	 * @obj: GEM objects backing the framebuffer, one per plane (optional).
195 	 *
196 	 * This is used by the GEM framebuffer helpers, see e.g.
197 	 * drm_gem_fb_create().
198 	 */
199 	struct drm_gem_object *obj[4];
200 };
201 
202 #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
203 
204 int drm_framebuffer_init(struct drm_device *dev,
205 			 struct drm_framebuffer *fb,
206 			 const struct drm_framebuffer_funcs *funcs);
207 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
208 					       uint32_t id);
209 void drm_framebuffer_remove(struct drm_framebuffer *fb);
210 void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
211 void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
212 
213 /**
214  * drm_framebuffer_get - acquire a framebuffer reference
215  * @fb: DRM framebuffer
216  *
217  * This function increments the framebuffer's reference count.
218  */
219 static inline void drm_framebuffer_get(struct drm_framebuffer *fb)
220 {
221 	drm_mode_object_get(&fb->base);
222 }
223 
224 /**
225  * drm_framebuffer_put - release a framebuffer reference
226  * @fb: DRM framebuffer
227  *
228  * This function decrements the framebuffer's reference count and frees the
229  * framebuffer if the reference count drops to zero.
230  */
231 static inline void drm_framebuffer_put(struct drm_framebuffer *fb)
232 {
233 	drm_mode_object_put(&fb->base);
234 }
235 
236 /**
237  * drm_framebuffer_reference - acquire a framebuffer reference
238  * @fb: DRM framebuffer
239  *
240  * This is a compatibility alias for drm_framebuffer_get() and should not be
241  * used by new code.
242  */
243 static inline void drm_framebuffer_reference(struct drm_framebuffer *fb)
244 {
245 	drm_framebuffer_get(fb);
246 }
247 
248 /**
249  * drm_framebuffer_unreference - release a framebuffer reference
250  * @fb: DRM framebuffer
251  *
252  * This is a compatibility alias for drm_framebuffer_put() and should not be
253  * used by new code.
254  */
255 static inline void drm_framebuffer_unreference(struct drm_framebuffer *fb)
256 {
257 	drm_framebuffer_put(fb);
258 }
259 
260 /**
261  * drm_framebuffer_read_refcount - read the framebuffer reference count.
262  * @fb: framebuffer
263  *
264  * This functions returns the framebuffer's reference count.
265  */
266 static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb)
267 {
268 	return kref_read(&fb->base.refcount);
269 }
270 
271 /**
272  * drm_framebuffer_assign - store a reference to the fb
273  * @p: location to store framebuffer
274  * @fb: new framebuffer (maybe NULL)
275  *
276  * This functions sets the location to store a reference to the framebuffer,
277  * unreferencing the framebuffer that was previously stored in that location.
278  */
279 static inline void drm_framebuffer_assign(struct drm_framebuffer **p,
280 					  struct drm_framebuffer *fb)
281 {
282 	if (fb)
283 		drm_framebuffer_get(fb);
284 	if (*p)
285 		drm_framebuffer_put(*p);
286 	*p = fb;
287 }
288 
289 /*
290  * drm_for_each_fb - iterate over all framebuffers
291  * @fb: the loop cursor
292  * @dev: the DRM device
293  *
294  * Iterate over all framebuffers of @dev. User must hold
295  * &drm_mode_config.fb_lock.
296  */
297 #define drm_for_each_fb(fb, dev) \
298 	for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)),		\
299 	     fb = list_first_entry(&(dev)->mode_config.fb_list,	\
300 					  struct drm_framebuffer, head);	\
301 	     &fb->head != (&(dev)->mode_config.fb_list);			\
302 	     fb = list_next_entry(fb, head))
303 
304 int drm_framebuffer_plane_width(int width,
305 				const struct drm_framebuffer *fb, int plane);
306 int drm_framebuffer_plane_height(int height,
307 				 const struct drm_framebuffer *fb, int plane);
308 
309 #endif
310