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 ->fb_create() in
44 	 * &drm_mode_config_funcs. 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_reference() and
105  * drm_framebuffer_unreference().
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 dev->mode_config.fb_list, access protected by
116 	 * dev->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 	 * @funcs: framebuffer vfunc table
126 	 */
127 	const struct drm_framebuffer_funcs *funcs;
128 	/**
129 	 * @pitches: Line stride per buffer. For userspace created object this
130 	 * is copied from drm_mode_fb_cmd2.
131 	 */
132 	unsigned int pitches[4];
133 	/**
134 	 * @offsets: Offset from buffer start to the actual pixel data in bytes,
135 	 * per buffer. For userspace created object this is copied from
136 	 * drm_mode_fb_cmd2.
137 	 *
138 	 * Note that this is a linear offset and does not take into account
139 	 * tiling or buffer laytou per @modifier. It meant to be used when the
140 	 * actual pixel data for this framebuffer plane starts at an offset,
141 	 * e.g.  when multiple planes are allocated within the same backing
142 	 * storage buffer object. For tiled layouts this generally means it
143 	 * @offsets must at least be tile-size aligned, but hardware often has
144 	 * stricter requirements.
145 	 *
146 	 * This should not be used to specifiy x/y pixel offsets into the buffer
147 	 * data (even for linear buffers). Specifying an x/y pixel offset is
148 	 * instead done through the source rectangle in struct &drm_plane_state.
149 	 */
150 	unsigned int offsets[4];
151 	/**
152 	 * @modifier: Data layout modifier, per buffer. This is used to describe
153 	 * tiling, or also special layouts (like compression) of auxiliary
154 	 * buffers. For userspace created object this is copied from
155 	 * drm_mode_fb_cmd2.
156 	 */
157 	uint64_t modifier[4];
158 	/**
159 	 * @width: Logical width of the visible area of the framebuffer, in
160 	 * pixels.
161 	 */
162 	unsigned int width;
163 	/**
164 	 * @height: Logical height of the visible area of the framebuffer, in
165 	 * pixels.
166 	 */
167 	unsigned int height;
168 	/**
169 	 * @depth: Depth in bits per pixel for RGB formats. 0 for everything
170 	 * else. Legacy information derived from @pixel_format, it's suggested to use
171 	 * the DRM FOURCC codes and helper functions directly instead.
172 	 */
173 	unsigned int depth;
174 	/**
175 	 * @bits_per_pixel: Storage used bits per pixel for RGB formats. 0 for
176 	 * everything else. Legacy information derived from @pixel_format, it's
177 	 * suggested to use the DRM FOURCC codes and helper functions directly
178 	 * instead.
179 	 */
180 	int bits_per_pixel;
181 	/**
182 	 * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
183 	 * DRM_MODE_FB_MODIFIERS.
184 	 */
185 	int flags;
186 	/**
187 	 * @pixel_format: DRM FOURCC code describing the pixel format.
188 	 */
189 	uint32_t pixel_format; /* fourcc format */
190 	/**
191 	 * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
192 	 * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
193 	 * universal plane.
194 	 */
195 	int hot_x;
196 	/**
197 	 * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor
198 	 * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
199 	 * universal plane.
200 	 */
201 	int hot_y;
202 	/**
203 	 * @filp_head: Placed on struct &drm_file fbs list_head, protected by
204 	 * fbs_lock in the same structure.
205 	 */
206 	struct list_head filp_head;
207 };
208 
209 #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
210 
211 int drm_framebuffer_init(struct drm_device *dev,
212 			 struct drm_framebuffer *fb,
213 			 const struct drm_framebuffer_funcs *funcs);
214 struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
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 /**
221  * drm_framebuffer_reference - incr the fb refcnt
222  * @fb: framebuffer
223  *
224  * This functions increments the fb's refcount.
225  */
226 static inline void drm_framebuffer_reference(struct drm_framebuffer *fb)
227 {
228 	drm_mode_object_reference(&fb->base);
229 }
230 
231 /**
232  * drm_framebuffer_unreference - unref a framebuffer
233  * @fb: framebuffer to unref
234  *
235  * This functions decrements the fb's refcount and frees it if it drops to zero.
236  */
237 static inline void drm_framebuffer_unreference(struct drm_framebuffer *fb)
238 {
239 	drm_mode_object_unreference(&fb->base);
240 }
241 
242 /**
243  * drm_framebuffer_read_refcount - read the framebuffer reference count.
244  * @fb: framebuffer
245  *
246  * This functions returns the framebuffer's reference count.
247  */
248 static inline uint32_t drm_framebuffer_read_refcount(struct drm_framebuffer *fb)
249 {
250 	return atomic_read(&fb->base.refcount.refcount);
251 }
252 
253 /**
254  * drm_for_each_fb - iterate over all framebuffers
255  * @fb: the loop cursor
256  * @dev: the DRM device
257  *
258  * Iterate over all framebuffers of @dev. User must hold the fb_lock from
259  * &drm_mode_config.
260  */
261 #define drm_for_each_fb(fb, dev) \
262 	for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)),		\
263 	     fb = list_first_entry(&(dev)->mode_config.fb_list,	\
264 					  struct drm_framebuffer, head);	\
265 	     &fb->head != (&(dev)->mode_config.fb_list);			\
266 	     fb = list_next_entry(fb, head))
267 #endif
268