xref: /openbsd-src/sys/dev/pci/drm/include/drm/drm_framebuffer.h (revision 1bb76ff151c0aba8e3312a604e4cd2e5195cf4b7)
17f4dd379Sjsg /*
27f4dd379Sjsg  * Copyright (c) 2016 Intel Corporation
37f4dd379Sjsg  *
47f4dd379Sjsg  * Permission to use, copy, modify, distribute, and sell this software and its
57f4dd379Sjsg  * documentation for any purpose is hereby granted without fee, provided that
67f4dd379Sjsg  * the above copyright notice appear in all copies and that both that copyright
77f4dd379Sjsg  * notice and this permission notice appear in supporting documentation, and
87f4dd379Sjsg  * that the name of the copyright holders not be used in advertising or
97f4dd379Sjsg  * publicity pertaining to distribution of the software without specific,
107f4dd379Sjsg  * written prior permission.  The copyright holders make no representations
117f4dd379Sjsg  * about the suitability of this software for any purpose.  It is provided "as
127f4dd379Sjsg  * is" without express or implied warranty.
137f4dd379Sjsg  *
147f4dd379Sjsg  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
157f4dd379Sjsg  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
167f4dd379Sjsg  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
177f4dd379Sjsg  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
187f4dd379Sjsg  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
197f4dd379Sjsg  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
207f4dd379Sjsg  * OF THIS SOFTWARE.
217f4dd379Sjsg  */
227f4dd379Sjsg 
237f4dd379Sjsg #ifndef __DRM_FRAMEBUFFER_H__
247f4dd379Sjsg #define __DRM_FRAMEBUFFER_H__
257f4dd379Sjsg 
267f4dd379Sjsg #include <linux/ctype.h>
27c349dbc7Sjsg #include <linux/list.h>
28c349dbc7Sjsg #include <linux/sched.h>
29c349dbc7Sjsg 
305ca02815Sjsg #include <drm/drm_fourcc.h>
317f4dd379Sjsg #include <drm/drm_mode_object.h>
327f4dd379Sjsg 
33c349dbc7Sjsg struct drm_clip_rect;
347f4dd379Sjsg struct drm_device;
35c349dbc7Sjsg struct drm_file;
36c349dbc7Sjsg struct drm_framebuffer;
37c349dbc7Sjsg struct drm_gem_object;
387f4dd379Sjsg 
397f4dd379Sjsg /**
407f4dd379Sjsg  * struct drm_framebuffer_funcs - framebuffer hooks
417f4dd379Sjsg  */
427f4dd379Sjsg struct drm_framebuffer_funcs {
437f4dd379Sjsg 	/**
447f4dd379Sjsg 	 * @destroy:
457f4dd379Sjsg 	 *
467f4dd379Sjsg 	 * Clean up framebuffer resources, specifically also unreference the
477f4dd379Sjsg 	 * backing storage. The core guarantees to call this function for every
487f4dd379Sjsg 	 * framebuffer successfully created by calling
497f4dd379Sjsg 	 * &drm_mode_config_funcs.fb_create. Drivers must also call
507f4dd379Sjsg 	 * drm_framebuffer_cleanup() to release DRM core resources for this
517f4dd379Sjsg 	 * framebuffer.
527f4dd379Sjsg 	 */
537f4dd379Sjsg 	void (*destroy)(struct drm_framebuffer *framebuffer);
547f4dd379Sjsg 
557f4dd379Sjsg 	/**
567f4dd379Sjsg 	 * @create_handle:
577f4dd379Sjsg 	 *
587f4dd379Sjsg 	 * Create a buffer handle in the driver-specific buffer manager (either
597f4dd379Sjsg 	 * GEM or TTM) valid for the passed-in &struct drm_file. This is used by
607f4dd379Sjsg 	 * the core to implement the GETFB IOCTL, which returns (for
617f4dd379Sjsg 	 * sufficiently priviledged user) also a native buffer handle. This can
627f4dd379Sjsg 	 * be used for seamless transitions between modesetting clients by
637f4dd379Sjsg 	 * copying the current screen contents to a private buffer and blending
647f4dd379Sjsg 	 * between that and the new contents.
657f4dd379Sjsg 	 *
667f4dd379Sjsg 	 * GEM based drivers should call drm_gem_handle_create() to create the
677f4dd379Sjsg 	 * handle.
687f4dd379Sjsg 	 *
697f4dd379Sjsg 	 * RETURNS:
707f4dd379Sjsg 	 *
717f4dd379Sjsg 	 * 0 on success or a negative error code on failure.
727f4dd379Sjsg 	 */
737f4dd379Sjsg 	int (*create_handle)(struct drm_framebuffer *fb,
747f4dd379Sjsg 			     struct drm_file *file_priv,
757f4dd379Sjsg 			     unsigned int *handle);
767f4dd379Sjsg 	/**
777f4dd379Sjsg 	 * @dirty:
787f4dd379Sjsg 	 *
797f4dd379Sjsg 	 * Optional callback for the dirty fb IOCTL.
807f4dd379Sjsg 	 *
817f4dd379Sjsg 	 * Userspace can notify the driver via this callback that an area of the
827f4dd379Sjsg 	 * framebuffer has changed and should be flushed to the display
837f4dd379Sjsg 	 * hardware. This can also be used internally, e.g. by the fbdev
847f4dd379Sjsg 	 * emulation, though that's not the case currently.
857f4dd379Sjsg 	 *
867f4dd379Sjsg 	 * See documentation in drm_mode.h for the struct drm_mode_fb_dirty_cmd
877f4dd379Sjsg 	 * for more information as all the semantics and arguments have a one to
887f4dd379Sjsg 	 * one mapping on this function.
897f4dd379Sjsg 	 *
90c349dbc7Sjsg 	 * Atomic drivers should use drm_atomic_helper_dirtyfb() to implement
91c349dbc7Sjsg 	 * this hook.
92c349dbc7Sjsg 	 *
937f4dd379Sjsg 	 * RETURNS:
947f4dd379Sjsg 	 *
957f4dd379Sjsg 	 * 0 on success or a negative error code on failure.
967f4dd379Sjsg 	 */
977f4dd379Sjsg 	int (*dirty)(struct drm_framebuffer *framebuffer,
987f4dd379Sjsg 		     struct drm_file *file_priv, unsigned flags,
997f4dd379Sjsg 		     unsigned color, struct drm_clip_rect *clips,
1007f4dd379Sjsg 		     unsigned num_clips);
1017f4dd379Sjsg };
1027f4dd379Sjsg 
1037f4dd379Sjsg /**
1047f4dd379Sjsg  * struct drm_framebuffer - frame buffer object
1057f4dd379Sjsg  *
1067f4dd379Sjsg  * Note that the fb is refcounted for the benefit of driver internals,
1077f4dd379Sjsg  * for example some hw, disabling a CRTC/plane is asynchronous, and
1087f4dd379Sjsg  * scanout does not actually complete until the next vblank.  So some
1097f4dd379Sjsg  * cleanup (like releasing the reference(s) on the backing GEM bo(s))
1107f4dd379Sjsg  * should be deferred.  In cases like this, the driver would like to
1117f4dd379Sjsg  * hold a ref to the fb even though it has already been removed from
1127f4dd379Sjsg  * userspace perspective. See drm_framebuffer_get() and
1137f4dd379Sjsg  * drm_framebuffer_put().
1147f4dd379Sjsg  *
1157f4dd379Sjsg  * The refcount is stored inside the mode object @base.
1167f4dd379Sjsg  */
1177f4dd379Sjsg struct drm_framebuffer {
1187f4dd379Sjsg 	/**
1197f4dd379Sjsg 	 * @dev: DRM device this framebuffer belongs to
1207f4dd379Sjsg 	 */
1217f4dd379Sjsg 	struct drm_device *dev;
1227f4dd379Sjsg 	/**
1237f4dd379Sjsg 	 * @head: Place on the &drm_mode_config.fb_list, access protected by
1247f4dd379Sjsg 	 * &drm_mode_config.fb_lock.
1257f4dd379Sjsg 	 */
1267f4dd379Sjsg 	struct list_head head;
1277f4dd379Sjsg 
1287f4dd379Sjsg 	/**
1297f4dd379Sjsg 	 * @base: base modeset object structure, contains the reference count.
1307f4dd379Sjsg 	 */
1317f4dd379Sjsg 	struct drm_mode_object base;
1327f4dd379Sjsg 
1337f4dd379Sjsg 	/**
1347f4dd379Sjsg 	 * @comm: Name of the process allocating the fb, used for fb dumping.
1357f4dd379Sjsg 	 */
1367f4dd379Sjsg 	char comm[TASK_COMM_LEN];
1377f4dd379Sjsg 
1387f4dd379Sjsg 	/**
1397f4dd379Sjsg 	 * @format: framebuffer format information
1407f4dd379Sjsg 	 */
1417f4dd379Sjsg 	const struct drm_format_info *format;
1427f4dd379Sjsg 	/**
1437f4dd379Sjsg 	 * @funcs: framebuffer vfunc table
1447f4dd379Sjsg 	 */
1457f4dd379Sjsg 	const struct drm_framebuffer_funcs *funcs;
1467f4dd379Sjsg 	/**
1477f4dd379Sjsg 	 * @pitches: Line stride per buffer. For userspace created object this
1487f4dd379Sjsg 	 * is copied from drm_mode_fb_cmd2.
1497f4dd379Sjsg 	 */
1505ca02815Sjsg 	unsigned int pitches[DRM_FORMAT_MAX_PLANES];
1517f4dd379Sjsg 	/**
1527f4dd379Sjsg 	 * @offsets: Offset from buffer start to the actual pixel data in bytes,
1537f4dd379Sjsg 	 * per buffer. For userspace created object this is copied from
1547f4dd379Sjsg 	 * drm_mode_fb_cmd2.
1557f4dd379Sjsg 	 *
1567f4dd379Sjsg 	 * Note that this is a linear offset and does not take into account
157*1bb76ff1Sjsg 	 * tiling or buffer layout per @modifier. It is meant to be used when
158*1bb76ff1Sjsg 	 * the actual pixel data for this framebuffer plane starts at an offset,
1597f4dd379Sjsg 	 * e.g. when multiple planes are allocated within the same backing
160*1bb76ff1Sjsg 	 * storage buffer object. For tiled layouts this generally means its
1617f4dd379Sjsg 	 * @offsets must at least be tile-size aligned, but hardware often has
1627f4dd379Sjsg 	 * stricter requirements.
1637f4dd379Sjsg 	 *
1647f4dd379Sjsg 	 * This should not be used to specifiy x/y pixel offsets into the buffer
1657f4dd379Sjsg 	 * data (even for linear buffers). Specifying an x/y pixel offset is
1667f4dd379Sjsg 	 * instead done through the source rectangle in &struct drm_plane_state.
1677f4dd379Sjsg 	 */
1685ca02815Sjsg 	unsigned int offsets[DRM_FORMAT_MAX_PLANES];
1697f4dd379Sjsg 	/**
1707f4dd379Sjsg 	 * @modifier: Data layout modifier. This is used to describe
1717f4dd379Sjsg 	 * tiling, or also special layouts (like compression) of auxiliary
1727f4dd379Sjsg 	 * buffers. For userspace created object this is copied from
1737f4dd379Sjsg 	 * drm_mode_fb_cmd2.
1747f4dd379Sjsg 	 */
1757f4dd379Sjsg 	uint64_t modifier;
1767f4dd379Sjsg 	/**
1777f4dd379Sjsg 	 * @width: Logical width of the visible area of the framebuffer, in
1787f4dd379Sjsg 	 * pixels.
1797f4dd379Sjsg 	 */
1807f4dd379Sjsg 	unsigned int width;
1817f4dd379Sjsg 	/**
1827f4dd379Sjsg 	 * @height: Logical height of the visible area of the framebuffer, in
1837f4dd379Sjsg 	 * pixels.
1847f4dd379Sjsg 	 */
1857f4dd379Sjsg 	unsigned int height;
1867f4dd379Sjsg 	/**
1877f4dd379Sjsg 	 * @flags: Framebuffer flags like DRM_MODE_FB_INTERLACED or
1887f4dd379Sjsg 	 * DRM_MODE_FB_MODIFIERS.
1897f4dd379Sjsg 	 */
1907f4dd379Sjsg 	int flags;
1917f4dd379Sjsg 	/**
1927f4dd379Sjsg 	 * @hot_x: X coordinate of the cursor hotspot. Used by the legacy cursor
1937f4dd379Sjsg 	 * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
1947f4dd379Sjsg 	 * universal plane.
1957f4dd379Sjsg 	 */
1967f4dd379Sjsg 	int hot_x;
1977f4dd379Sjsg 	/**
1987f4dd379Sjsg 	 * @hot_y: Y coordinate of the cursor hotspot. Used by the legacy cursor
1997f4dd379Sjsg 	 * IOCTL when the driver supports cursor through a DRM_PLANE_TYPE_CURSOR
2007f4dd379Sjsg 	 * universal plane.
2017f4dd379Sjsg 	 */
2027f4dd379Sjsg 	int hot_y;
2037f4dd379Sjsg 	/**
2047f4dd379Sjsg 	 * @filp_head: Placed on &drm_file.fbs, protected by &drm_file.fbs_lock.
2057f4dd379Sjsg 	 */
2067f4dd379Sjsg 	struct list_head filp_head;
2077f4dd379Sjsg 	/**
2087f4dd379Sjsg 	 * @obj: GEM objects backing the framebuffer, one per plane (optional).
2097f4dd379Sjsg 	 *
2107f4dd379Sjsg 	 * This is used by the GEM framebuffer helpers, see e.g.
2117f4dd379Sjsg 	 * drm_gem_fb_create().
2127f4dd379Sjsg 	 */
2135ca02815Sjsg 	struct drm_gem_object *obj[DRM_FORMAT_MAX_PLANES];
2147f4dd379Sjsg };
2157f4dd379Sjsg 
2167f4dd379Sjsg #define obj_to_fb(x) container_of(x, struct drm_framebuffer, base)
2177f4dd379Sjsg 
2187f4dd379Sjsg int drm_framebuffer_init(struct drm_device *dev,
2197f4dd379Sjsg 			 struct drm_framebuffer *fb,
2207f4dd379Sjsg 			 const struct drm_framebuffer_funcs *funcs);
2217f4dd379Sjsg struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
2227f4dd379Sjsg 					       struct drm_file *file_priv,
2237f4dd379Sjsg 					       uint32_t id);
2247f4dd379Sjsg void drm_framebuffer_remove(struct drm_framebuffer *fb);
2257f4dd379Sjsg void drm_framebuffer_cleanup(struct drm_framebuffer *fb);
2267f4dd379Sjsg void drm_framebuffer_unregister_private(struct drm_framebuffer *fb);
2277f4dd379Sjsg 
2287f4dd379Sjsg /**
2297f4dd379Sjsg  * drm_framebuffer_get - acquire a framebuffer reference
2307f4dd379Sjsg  * @fb: DRM framebuffer
2317f4dd379Sjsg  *
2327f4dd379Sjsg  * This function increments the framebuffer's reference count.
2337f4dd379Sjsg  */
drm_framebuffer_get(struct drm_framebuffer * fb)2347f4dd379Sjsg static inline void drm_framebuffer_get(struct drm_framebuffer *fb)
2357f4dd379Sjsg {
2367f4dd379Sjsg 	drm_mode_object_get(&fb->base);
2377f4dd379Sjsg }
2387f4dd379Sjsg 
2397f4dd379Sjsg /**
2407f4dd379Sjsg  * drm_framebuffer_put - release a framebuffer reference
2417f4dd379Sjsg  * @fb: DRM framebuffer
2427f4dd379Sjsg  *
2437f4dd379Sjsg  * This function decrements the framebuffer's reference count and frees the
2447f4dd379Sjsg  * framebuffer if the reference count drops to zero.
2457f4dd379Sjsg  */
drm_framebuffer_put(struct drm_framebuffer * fb)2467f4dd379Sjsg static inline void drm_framebuffer_put(struct drm_framebuffer *fb)
2477f4dd379Sjsg {
2487f4dd379Sjsg 	drm_mode_object_put(&fb->base);
2497f4dd379Sjsg }
2507f4dd379Sjsg 
2517f4dd379Sjsg /**
2527f4dd379Sjsg  * drm_framebuffer_read_refcount - read the framebuffer reference count.
2537f4dd379Sjsg  * @fb: framebuffer
2547f4dd379Sjsg  *
2557f4dd379Sjsg  * This functions returns the framebuffer's reference count.
2567f4dd379Sjsg  */
drm_framebuffer_read_refcount(const struct drm_framebuffer * fb)2577f4dd379Sjsg static inline uint32_t drm_framebuffer_read_refcount(const struct drm_framebuffer *fb)
2587f4dd379Sjsg {
2597f4dd379Sjsg 	return kref_read(&fb->base.refcount);
2607f4dd379Sjsg }
2617f4dd379Sjsg 
2627f4dd379Sjsg /**
2637f4dd379Sjsg  * drm_framebuffer_assign - store a reference to the fb
2647f4dd379Sjsg  * @p: location to store framebuffer
2657f4dd379Sjsg  * @fb: new framebuffer (maybe NULL)
2667f4dd379Sjsg  *
2677f4dd379Sjsg  * This functions sets the location to store a reference to the framebuffer,
2687f4dd379Sjsg  * unreferencing the framebuffer that was previously stored in that location.
2697f4dd379Sjsg  */
drm_framebuffer_assign(struct drm_framebuffer ** p,struct drm_framebuffer * fb)2707f4dd379Sjsg static inline void drm_framebuffer_assign(struct drm_framebuffer **p,
2717f4dd379Sjsg 					  struct drm_framebuffer *fb)
2727f4dd379Sjsg {
2737f4dd379Sjsg 	if (fb)
2747f4dd379Sjsg 		drm_framebuffer_get(fb);
2757f4dd379Sjsg 	if (*p)
2767f4dd379Sjsg 		drm_framebuffer_put(*p);
2777f4dd379Sjsg 	*p = fb;
2787f4dd379Sjsg }
2797f4dd379Sjsg 
2807f4dd379Sjsg /*
2817f4dd379Sjsg  * drm_for_each_fb - iterate over all framebuffers
2827f4dd379Sjsg  * @fb: the loop cursor
2837f4dd379Sjsg  * @dev: the DRM device
2847f4dd379Sjsg  *
2857f4dd379Sjsg  * Iterate over all framebuffers of @dev. User must hold
2867f4dd379Sjsg  * &drm_mode_config.fb_lock.
2877f4dd379Sjsg  */
2887f4dd379Sjsg #define drm_for_each_fb(fb, dev) \
2897f4dd379Sjsg 	for (WARN_ON(!mutex_is_locked(&(dev)->mode_config.fb_lock)),		\
2907f4dd379Sjsg 	     fb = list_first_entry(&(dev)->mode_config.fb_list,	\
2917f4dd379Sjsg 					  struct drm_framebuffer, head);	\
2927f4dd379Sjsg 	     &fb->head != (&(dev)->mode_config.fb_list);			\
2937f4dd379Sjsg 	     fb = list_next_entry(fb, head))
2947f4dd379Sjsg 
2957f4dd379Sjsg int drm_framebuffer_plane_width(int width,
2967f4dd379Sjsg 				const struct drm_framebuffer *fb, int plane);
2977f4dd379Sjsg int drm_framebuffer_plane_height(int height,
2987f4dd379Sjsg 				 const struct drm_framebuffer *fb, int plane);
2997f4dd379Sjsg 
300ad8b1aafSjsg /**
301ad8b1aafSjsg  * struct drm_afbc_framebuffer - a special afbc frame buffer object
302ad8b1aafSjsg  *
303ad8b1aafSjsg  * A derived class of struct drm_framebuffer, dedicated for afbc use cases.
304ad8b1aafSjsg  */
305ad8b1aafSjsg struct drm_afbc_framebuffer {
306ad8b1aafSjsg 	/**
307ad8b1aafSjsg 	 * @base: base framebuffer structure.
308ad8b1aafSjsg 	 */
309ad8b1aafSjsg 	struct drm_framebuffer base;
310ad8b1aafSjsg 	/**
311ad8b1aafSjsg 	 * @block_width: width of a single afbc block
312ad8b1aafSjsg 	 */
313ad8b1aafSjsg 	u32 block_width;
314ad8b1aafSjsg 	/**
315ad8b1aafSjsg 	 * @block_height: height of a single afbc block
316ad8b1aafSjsg 	 */
317ad8b1aafSjsg 	u32 block_height;
318ad8b1aafSjsg 	/**
319ad8b1aafSjsg 	 * @aligned_width: aligned frame buffer width
320ad8b1aafSjsg 	 */
321ad8b1aafSjsg 	u32 aligned_width;
322ad8b1aafSjsg 	/**
323ad8b1aafSjsg 	 * @aligned_height: aligned frame buffer height
324ad8b1aafSjsg 	 */
325ad8b1aafSjsg 	u32 aligned_height;
326ad8b1aafSjsg 	/**
327ad8b1aafSjsg 	 * @offset: offset of the first afbc header
328ad8b1aafSjsg 	 */
329ad8b1aafSjsg 	u32 offset;
330ad8b1aafSjsg 	/**
331ad8b1aafSjsg 	 * @afbc_size: minimum size of afbc buffer
332ad8b1aafSjsg 	 */
333ad8b1aafSjsg 	u32 afbc_size;
334ad8b1aafSjsg };
335ad8b1aafSjsg 
336ad8b1aafSjsg #define fb_to_afbc_fb(x) container_of(x, struct drm_afbc_framebuffer, base)
337ad8b1aafSjsg 
3387f4dd379Sjsg #endif
339