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