17520a277SDaniel Vetter /*
27520a277SDaniel Vetter * Copyright (c) 2016 Intel Corporation
37520a277SDaniel Vetter *
47520a277SDaniel Vetter * Permission to use, copy, modify, distribute, and sell this software and its
57520a277SDaniel Vetter * documentation for any purpose is hereby granted without fee, provided that
67520a277SDaniel Vetter * the above copyright notice appear in all copies and that both that copyright
77520a277SDaniel Vetter * notice and this permission notice appear in supporting documentation, and
87520a277SDaniel Vetter * that the name of the copyright holders not be used in advertising or
97520a277SDaniel Vetter * publicity pertaining to distribution of the software without specific,
107520a277SDaniel Vetter * written prior permission. The copyright holders make no representations
117520a277SDaniel Vetter * about the suitability of this software for any purpose. It is provided "as
127520a277SDaniel Vetter * is" without express or implied warranty.
137520a277SDaniel Vetter *
147520a277SDaniel Vetter * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
157520a277SDaniel Vetter * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
167520a277SDaniel Vetter * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
177520a277SDaniel Vetter * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
187520a277SDaniel Vetter * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
197520a277SDaniel Vetter * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
207520a277SDaniel Vetter * OF THIS SOFTWARE.
217520a277SDaniel Vetter */
227520a277SDaniel Vetter
237520a277SDaniel Vetter #ifndef __DRM_FRAMEBUFFER_H__
247520a277SDaniel Vetter #define __DRM_FRAMEBUFFER_H__
257520a277SDaniel Vetter
267520a277SDaniel Vetter #include <linux/ctype.h>
272513147dSSam Ravnborg #include <linux/list.h>
282513147dSSam Ravnborg #include <linux/sched.h>
292513147dSSam Ravnborg
30279cc2e9SThomas Zimmermann #include <drm/drm_fourcc.h>
31949619f3SDaniel Vetter #include <drm/drm_mode_object.h>
327520a277SDaniel Vetter
332513147dSSam Ravnborg struct drm_clip_rect;
347520a277SDaniel Vetter struct drm_device;
352513147dSSam Ravnborg struct drm_file;
362513147dSSam Ravnborg struct drm_framebuffer;
372513147dSSam Ravnborg struct drm_gem_object;
387520a277SDaniel Vetter
397520a277SDaniel Vetter /**
407520a277SDaniel Vetter * struct drm_framebuffer_funcs - framebuffer hooks
417520a277SDaniel Vetter */
427520a277SDaniel Vetter struct drm_framebuffer_funcs {
437520a277SDaniel Vetter /**
447520a277SDaniel Vetter * @destroy:
457520a277SDaniel Vetter *
467520a277SDaniel Vetter * Clean up framebuffer resources, specifically also unreference the
477520a277SDaniel Vetter * backing storage. The core guarantees to call this function for every
48d574528aSDaniel Vetter * framebuffer successfully created by calling
49d574528aSDaniel Vetter * &drm_mode_config_funcs.fb_create. Drivers must also call
507520a277SDaniel Vetter * drm_framebuffer_cleanup() to release DRM core resources for this
517520a277SDaniel Vetter * framebuffer.
527520a277SDaniel Vetter */
537520a277SDaniel Vetter void (*destroy)(struct drm_framebuffer *framebuffer);
547520a277SDaniel Vetter
557520a277SDaniel Vetter /**
567520a277SDaniel Vetter * @create_handle:
577520a277SDaniel Vetter *
587520a277SDaniel Vetter * Create a buffer handle in the driver-specific buffer manager (either
59ea0dd85aSDaniel Vetter * GEM or TTM) valid for the passed-in &struct drm_file. This is used by
607520a277SDaniel Vetter * the core to implement the GETFB IOCTL, which returns (for
617520a277SDaniel Vetter * sufficiently priviledged user) also a native buffer handle. This can
627520a277SDaniel Vetter * be used for seamless transitions between modesetting clients by
637520a277SDaniel Vetter * copying the current screen contents to a private buffer and blending
647520a277SDaniel Vetter * between that and the new contents.
657520a277SDaniel Vetter *
667520a277SDaniel Vetter * GEM based drivers should call drm_gem_handle_create() to create the
677520a277SDaniel Vetter * handle.
687520a277SDaniel Vetter *
697520a277SDaniel Vetter * RETURNS:
707520a277SDaniel Vetter *
717520a277SDaniel Vetter * 0 on success or a negative error code on failure.
727520a277SDaniel Vetter */
737520a277SDaniel Vetter int (*create_handle)(struct drm_framebuffer *fb,
747520a277SDaniel Vetter struct drm_file *file_priv,
757520a277SDaniel Vetter unsigned int *handle);
767520a277SDaniel Vetter /**
777520a277SDaniel Vetter * @dirty:
787520a277SDaniel Vetter *
797520a277SDaniel Vetter * Optional callback for the dirty fb IOCTL.
807520a277SDaniel Vetter *
817520a277SDaniel Vetter * Userspace can notify the driver via this callback that an area of the
827520a277SDaniel Vetter * framebuffer has changed and should be flushed to the display
837520a277SDaniel Vetter * hardware. This can also be used internally, e.g. by the fbdev
847520a277SDaniel Vetter * emulation, though that's not the case currently.
857520a277SDaniel Vetter *
867520a277SDaniel Vetter * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
877520a277SDaniel Vetter * for more information as all the semantics and arguments have a one to
887520a277SDaniel Vetter * one mapping on this function.
897520a277SDaniel Vetter *
90ecf79e7cSDaniel Vetter * Atomic drivers should use drm_atomic_helper_dirtyfb() to implement
91ecf79e7cSDaniel Vetter * this hook.
92ecf79e7cSDaniel Vetter *
937520a277SDaniel Vetter * RETURNS:
947520a277SDaniel Vetter *
957520a277SDaniel Vetter * 0 on success or a negative error code on failure.
967520a277SDaniel Vetter */
977520a277SDaniel Vetter int (*dirty)(struct drm_framebuffer *framebuffer,
987520a277SDaniel Vetter struct drm_file *file_priv, unsigned flags,
997520a277SDaniel Vetter unsigned color, struct drm_clip_rect *clips,
1007520a277SDaniel Vetter unsigned num_clips);
1017520a277SDaniel Vetter };
1027520a277SDaniel Vetter
103750fb8c4SDaniel Vetter /**
104750fb8c4SDaniel Vetter * struct drm_framebuffer - frame buffer object
105750fb8c4SDaniel Vetter *
1067520a277SDaniel Vetter * Note that the fb is refcounted for the benefit of driver internals,
1077520a277SDaniel Vetter * for example some hw, disabling a CRTC/plane is asynchronous, and
1087520a277SDaniel Vetter * scanout does not actually complete until the next vblank. So some
1097520a277SDaniel Vetter * cleanup (like releasing the reference(s) on the backing GEM bo(s))
1107520a277SDaniel Vetter * should be deferred. In cases like this, the driver would like to
1117520a277SDaniel Vetter * hold a ref to the fb even though it has already been removed from
112a4a69da0SThierry Reding * userspace perspective. See drm_framebuffer_get() and
113a4a69da0SThierry Reding * drm_framebuffer_put().
114750fb8c4SDaniel Vetter *
115750fb8c4SDaniel Vetter * The refcount is stored inside the mode object @base.
1167520a277SDaniel Vetter */
117750fb8c4SDaniel Vetter struct drm_framebuffer {
118750fb8c4SDaniel Vetter /**
119750fb8c4SDaniel Vetter * @dev: DRM device this framebuffer belongs to
120750fb8c4SDaniel Vetter */
121750fb8c4SDaniel Vetter struct drm_device *dev;
122750fb8c4SDaniel Vetter /**
123d574528aSDaniel Vetter * @head: Place on the &drm_mode_config.fb_list, access protected by
124d574528aSDaniel Vetter * &drm_mode_config.fb_lock.
1257520a277SDaniel Vetter */
1267520a277SDaniel Vetter struct list_head head;
127750fb8c4SDaniel Vetter
128750fb8c4SDaniel Vetter /**
129750fb8c4SDaniel Vetter * @base: base modeset object structure, contains the reference count.
130750fb8c4SDaniel Vetter */
1317520a277SDaniel Vetter struct drm_mode_object base;
1328d44e9e6SMaarten Lankhorst
1338d44e9e6SMaarten Lankhorst /**
1348d44e9e6SMaarten Lankhorst * @comm: Name of the process allocating the fb, used for fb dumping.
1358d44e9e6SMaarten Lankhorst */
1368d44e9e6SMaarten Lankhorst char comm[TASK_COMM_LEN];
1378d44e9e6SMaarten Lankhorst
138750fb8c4SDaniel Vetter /**
139e14c23c6SVille Syrjälä * @format: framebuffer format information
140e14c23c6SVille Syrjälä */
141e14c23c6SVille Syrjälä const struct drm_format_info *format;
142e14c23c6SVille Syrjälä /**
143750fb8c4SDaniel Vetter * @funcs: framebuffer vfunc table
144750fb8c4SDaniel Vetter */
1457520a277SDaniel Vetter const struct drm_framebuffer_funcs *funcs;
146750fb8c4SDaniel Vetter /**
147750fb8c4SDaniel Vetter * @pitches: Line stride per buffer. For userspace created object this
148750fb8c4SDaniel Vetter * is copied from drm_mode_fb_cmd2.
149750fb8c4SDaniel Vetter */
150279cc2e9SThomas Zimmermann unsigned int pitches[DRM_FORMAT_MAX_PLANES];
151750fb8c4SDaniel Vetter /**
152750fb8c4SDaniel Vetter * @offsets: Offset from buffer start to the actual pixel data in bytes,
153750fb8c4SDaniel Vetter * per buffer. For userspace created object this is copied from
154750fb8c4SDaniel Vetter * drm_mode_fb_cmd2.
155750fb8c4SDaniel Vetter *
156750fb8c4SDaniel Vetter * Note that this is a linear offset and does not take into account
157*6de74523SGeert Uytterhoeven * tiling or buffer layout per @modifier. It is meant to be used when
158*6de74523SGeert Uytterhoeven * the actual pixel data for this framebuffer plane starts at an offset,
159750fb8c4SDaniel Vetter * e.g. when multiple planes are allocated within the same backing
160*6de74523SGeert Uytterhoeven * storage buffer object. For tiled layouts this generally means its
161750fb8c4SDaniel Vetter * @offsets must at least be tile-size aligned, but hardware often has
162750fb8c4SDaniel Vetter * stricter requirements.
163750fb8c4SDaniel Vetter *
164750fb8c4SDaniel Vetter * This should not be used to specifiy x/y pixel offsets into the buffer
165750fb8c4SDaniel Vetter * data (even for linear buffers). Specifying an x/y pixel offset is
166ea0dd85aSDaniel Vetter * instead done through the source rectangle in &struct drm_plane_state.
167750fb8c4SDaniel Vetter */
168279cc2e9SThomas Zimmermann unsigned int offsets[DRM_FORMAT_MAX_PLANES];
169750fb8c4SDaniel Vetter /**
170bae781b2SVille Syrjälä * @modifier: Data layout modifier. This is used to describe
171750fb8c4SDaniel Vetter * tiling, or also special layouts (like compression) of auxiliary
172750fb8c4SDaniel Vetter * buffers. For userspace created object this is copied from
173750fb8c4SDaniel Vetter * drm_mode_fb_cmd2.
174750fb8c4SDaniel Vetter */
175bae781b2SVille Syrjälä uint64_t modifier;
176750fb8c4SDaniel Vetter /**
177750fb8c4SDaniel Vetter * @width: Logical width of the visible area of the framebuffer, in
178750fb8c4SDaniel Vetter * pixels.
179750fb8c4SDaniel Vetter */
1807520a277SDaniel Vetter unsigned int width;
181750fb8c4SDaniel Vetter /**
182750fb8c4SDaniel Vetter * @height: Logical height of the visible area of the framebuffer, in
183750fb8c4SDaniel Vetter * pixels.
184750fb8c4SDaniel Vetter */
1857520a277SDaniel Vetter unsigned int height;
186750fb8c4SDaniel Vetter /**
187750fb8c4SDaniel Vetter * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
188750fb8c4SDaniel Vetter * DRM_MODE_FB_MODIFIERS.
189750fb8c4SDaniel Vetter */
1907520a277SDaniel Vetter int flags;
191750fb8c4SDaniel Vetter /**
192750fb8c4SDaniel Vetter * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
193750fb8c4SDaniel Vetter * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
194750fb8c4SDaniel Vetter * universal plane.
195750fb8c4SDaniel Vetter */
1967520a277SDaniel Vetter int hot_x;
197750fb8c4SDaniel Vetter /**
198750fb8c4SDaniel Vetter * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor
199750fb8c4SDaniel Vetter * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
200750fb8c4SDaniel Vetter * universal plane.
201750fb8c4SDaniel Vetter */
2027520a277SDaniel Vetter int hot_y;
203750fb8c4SDaniel Vetter /**
204d574528aSDaniel Vetter * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock.
205750fb8c4SDaniel Vetter */
2067520a277SDaniel Vetter struct list_head filp_head;
2074c3dbb2cSNoralf Trønnes /**
2084c3dbb2cSNoralf Trønnes * @obj: GEM objects backing the framebuffer, one per plane (optional).
2094c3dbb2cSNoralf Trønnes *
2104c3dbb2cSNoralf Trønnes * This is used by the GEM framebuffer helpers, see e.g.
2114c3dbb2cSNoralf Trønnes * drm_gem_fb_create().
2124c3dbb2cSNoralf Trønnes */
213279cc2e9SThomas Zimmermann struct drm_gem_object *obj[DRM_FORMAT_MAX_PLANES];
2147520a277SDaniel Vetter };
2157520a277SDaniel Vetter
216afb21ea6SDaniel Vetter #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
217afb21ea6SDaniel Vetter
2187520a277SDaniel Vetter int drm_framebuffer_init(struct drm_device *dev,
2197520a277SDaniel Vetter struct drm_framebuffer *fb,
2207520a277SDaniel Vetter const struct drm_framebuffer_funcs *funcs);
2217520a277SDaniel Vetter struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
222418da172SKeith Packard struct drm_file *file_priv,
2237520a277SDaniel Vetter uint32_t id);
2247520a277SDaniel Vetter void drm_framebuffer_remove(struct drm_framebuffer *fb);
2257520a277SDaniel Vetter void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
2267520a277SDaniel Vetter void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
2277520a277SDaniel Vetter
2287520a277SDaniel Vetter /**
229a4a69da0SThierry Reding * drm_framebuffer_get - acquire a framebuffer reference
230a4a69da0SThierry Reding * @fb: DRM framebuffer
2317520a277SDaniel Vetter *
232a4a69da0SThierry Reding * This function increments the framebuffer's reference count.
2337520a277SDaniel Vetter */
drm_framebuffer_get(struct drm_framebuffer * fb)234a4a69da0SThierry Reding static inline void drm_framebuffer_get(struct drm_framebuffer *fb)
2357520a277SDaniel Vetter {
236a4a69da0SThierry Reding drm_mode_object_get(&fb->base);
2377520a277SDaniel Vetter }
2387520a277SDaniel Vetter
2397520a277SDaniel Vetter /**
240a4a69da0SThierry Reding * drm_framebuffer_put - release a framebuffer reference
241a4a69da0SThierry Reding * @fb: DRM framebuffer
2427520a277SDaniel Vetter *
243a4a69da0SThierry Reding * This function decrements the framebuffer's reference count and frees the
244a4a69da0SThierry Reding * framebuffer if the reference count drops to zero.
245a4a69da0SThierry Reding */
drm_framebuffer_put(struct drm_framebuffer * fb)246a4a69da0SThierry Reding static inline void drm_framebuffer_put(struct drm_framebuffer *fb)
247a4a69da0SThierry Reding {
248a4a69da0SThierry Reding drm_mode_object_put(&fb->base);
249a4a69da0SThierry Reding }
250a4a69da0SThierry Reding
251a4a69da0SThierry Reding /**
2527520a277SDaniel Vetter * drm_framebuffer_read_refcount - read the framebuffer reference count.
2537520a277SDaniel Vetter * @fb: framebuffer
2547520a277SDaniel Vetter *
2557520a277SDaniel Vetter * This functions returns the framebuffer's reference count.
2567520a277SDaniel Vetter */
drm_framebuffer_read_refcount(const struct drm_framebuffer * fb)2576ff1086eSNoralf Trønnes static inline uint32_t drm_framebuffer_read_refcount(const struct drm_framebuffer *fb)
2587520a277SDaniel Vetter {
2592c935bc5SPeter Zijlstra return kref_read(&fb->base.refcount);
2607520a277SDaniel Vetter }
261afb21ea6SDaniel Vetter
262afb21ea6SDaniel Vetter /**
263389f78b3SChris Wilson * drm_framebuffer_assign - store a reference to the fb
264389f78b3SChris Wilson * @p: location to store framebuffer
265389f78b3SChris Wilson * @fb: new framebuffer (maybe NULL)
266389f78b3SChris Wilson *
267389f78b3SChris Wilson * This functions sets the location to store a reference to the framebuffer,
268389f78b3SChris Wilson * unreferencing the framebuffer that was previously stored in that location.
269389f78b3SChris Wilson */
drm_framebuffer_assign(struct drm_framebuffer ** p,struct drm_framebuffer * fb)270389f78b3SChris Wilson static inline void drm_framebuffer_assign(struct drm_framebuffer **p,
271389f78b3SChris Wilson struct drm_framebuffer *fb)
272389f78b3SChris Wilson {
273389f78b3SChris Wilson if (fb)
274a4a69da0SThierry Reding drm_framebuffer_get(fb);
275389f78b3SChris Wilson if (*p)
276a4a69da0SThierry Reding drm_framebuffer_put(*p);
277389f78b3SChris Wilson *p = fb;
278389f78b3SChris Wilson }
279389f78b3SChris Wilson
280389f78b3SChris Wilson /*
281afb21ea6SDaniel Vetter * drm_for_each_fb - iterate over all framebuffers
282afb21ea6SDaniel Vetter * @fb: the loop cursor
283afb21ea6SDaniel Vetter * @dev: the DRM device
284afb21ea6SDaniel Vetter *
285d574528aSDaniel Vetter * Iterate over all framebuffers of @dev. User must hold
286d574528aSDaniel Vetter * &drm_mode_config.fb_lock.
287afb21ea6SDaniel Vetter */
288afb21ea6SDaniel Vetter #define drm_for_each_fb(fb, dev) \
289afb21ea6SDaniel Vetter for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)), \
290afb21ea6SDaniel Vetter fb = list_first_entry(&(dev)->mode_config.fb_list, \
291afb21ea6SDaniel Vetter struct drm_framebuffer, head); \
292afb21ea6SDaniel Vetter &fb->head != (&(dev)->mode_config.fb_list); \
293afb21ea6SDaniel Vetter fb = list_next_entry(fb, head))
2948f8f6a6cSVille Syrjälä
2958f8f6a6cSVille Syrjälä int drm_framebuffer_plane_width(int width,
2968f8f6a6cSVille Syrjälä const struct drm_framebuffer *fb, int plane);
2978f8f6a6cSVille Syrjälä int drm_framebuffer_plane_height(int height,
2988f8f6a6cSVille Syrjälä const struct drm_framebuffer *fb, int plane);
2998f8f6a6cSVille Syrjälä
30055f7f727SAndrzej Pietrasiewicz /**
30155f7f727SAndrzej Pietrasiewicz * struct drm_afbc_framebuffer - a special afbc frame buffer object
30255f7f727SAndrzej Pietrasiewicz *
30355f7f727SAndrzej Pietrasiewicz * A derived class of struct drm_framebuffer, dedicated for afbc use cases.
30455f7f727SAndrzej Pietrasiewicz */
30555f7f727SAndrzej Pietrasiewicz struct drm_afbc_framebuffer {
30655f7f727SAndrzej Pietrasiewicz /**
30755f7f727SAndrzej Pietrasiewicz * @base: base framebuffer structure.
30855f7f727SAndrzej Pietrasiewicz */
30955f7f727SAndrzej Pietrasiewicz struct drm_framebuffer base;
31055f7f727SAndrzej Pietrasiewicz /**
311303973aaSSam Ravnborg * @block_width: width of a single afbc block
31255f7f727SAndrzej Pietrasiewicz */
31355f7f727SAndrzej Pietrasiewicz u32 block_width;
31455f7f727SAndrzej Pietrasiewicz /**
315303973aaSSam Ravnborg * @block_height: height of a single afbc block
31655f7f727SAndrzej Pietrasiewicz */
31755f7f727SAndrzej Pietrasiewicz u32 block_height;
31855f7f727SAndrzej Pietrasiewicz /**
31955f7f727SAndrzej Pietrasiewicz * @aligned_width: aligned frame buffer width
32055f7f727SAndrzej Pietrasiewicz */
32155f7f727SAndrzej Pietrasiewicz u32 aligned_width;
32255f7f727SAndrzej Pietrasiewicz /**
32355f7f727SAndrzej Pietrasiewicz * @aligned_height: aligned frame buffer height
32455f7f727SAndrzej Pietrasiewicz */
32555f7f727SAndrzej Pietrasiewicz u32 aligned_height;
32655f7f727SAndrzej Pietrasiewicz /**
32755f7f727SAndrzej Pietrasiewicz * @offset: offset of the first afbc header
32855f7f727SAndrzej Pietrasiewicz */
32955f7f727SAndrzej Pietrasiewicz u32 offset;
33055f7f727SAndrzej Pietrasiewicz /**
33155f7f727SAndrzej Pietrasiewicz * @afbc_size: minimum size of afbc buffer
33255f7f727SAndrzej Pietrasiewicz */
33355f7f727SAndrzej Pietrasiewicz u32 afbc_size;
33455f7f727SAndrzej Pietrasiewicz };
33555f7f727SAndrzej Pietrasiewicz
33655f7f727SAndrzej Pietrasiewicz #define fb_to_afbc_fb(x) container_of(x, struct drm_afbc_framebuffer, base)
33755f7f727SAndrzej Pietrasiewicz
3387520a277SDaniel Vetter #endif
339