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