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