1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #ifndef DRM_GEM_VRAM_HELPER_H
4 #define DRM_GEM_VRAM_HELPER_H
6 #include <drm/drm_file.h>
7 #include <drm/drm_gem.h>
8 #include <drm/drm_ioctl.h>
9 #include <drm/drm_modes.h>
10 #include <drm/ttm/ttm_bo_api.h>
11 #include <drm/ttm/ttm_bo_driver.h>
13 #include <linux/dma-buf-map.h>
14 #include <linux/kernel.h> /* for container_of() */
16 struct drm_mode_create_dumb
;
18 struct drm_plane_state
;
19 struct drm_simple_display_pipe
;
21 struct vm_area_struct
;
23 #define DRM_GEM_VRAM_PL_FLAG_SYSTEM (1 << 0)
24 #define DRM_GEM_VRAM_PL_FLAG_VRAM (1 << 1)
25 #define DRM_GEM_VRAM_PL_FLAG_TOPDOWN (1 << 2)
28 * Buffer-object helpers
32 * struct drm_gem_vram_object - GEM object backed by VRAM
33 * @bo: TTM buffer object
34 * @map: Mapping information for @bo
35 * @placement: TTM placement information. Supported placements are \
36 %TTM_PL_VRAM and %TTM_PL_SYSTEM
37 * @placements: TTM placement information.
39 * The type struct drm_gem_vram_object represents a GEM object that is
40 * backed by VRAM. It can be used for simple framebuffer devices with
41 * dedicated memory. The buffer object can be evicted to system memory if
42 * video memory becomes scarce.
44 * GEM VRAM objects perform reference counting for pin and mapping
45 * operations. So a buffer object that has been pinned N times with
46 * drm_gem_vram_pin() must be unpinned N times with
47 * drm_gem_vram_unpin(). The same applies to pairs of
48 * drm_gem_vram_kmap() and drm_gem_vram_kunmap(), as well as pairs of
49 * drm_gem_vram_vmap() and drm_gem_vram_vunmap().
51 struct drm_gem_vram_object
{
52 struct ttm_buffer_object bo
;
53 struct dma_buf_map map
;
58 * Reference count on the virtual address.
59 * The address are un-mapped when the count reaches zero.
61 unsigned int vmap_use_count
;
63 /* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */
64 struct ttm_placement placement
;
65 struct ttm_place placements
[2];
69 * drm_gem_vram_of_bo - Returns the container of type
70 * &struct drm_gem_vram_object for field bo.
71 * @bo: the VRAM buffer object
72 * Returns: The containing GEM VRAM object
74 static inline struct drm_gem_vram_object
*drm_gem_vram_of_bo(
75 struct ttm_buffer_object
*bo
)
77 return container_of(bo
, struct drm_gem_vram_object
, bo
);
81 * drm_gem_vram_of_gem - Returns the container of type
82 * &struct drm_gem_vram_object for field gem.
83 * @gem: the GEM object
84 * Returns: The containing GEM VRAM object
86 static inline struct drm_gem_vram_object
*drm_gem_vram_of_gem(
87 struct drm_gem_object
*gem
)
89 return container_of(gem
, struct drm_gem_vram_object
, bo
.base
);
92 struct drm_gem_vram_object
*drm_gem_vram_create(struct drm_device
*dev
,
94 unsigned long pg_align
);
95 void drm_gem_vram_put(struct drm_gem_vram_object
*gbo
);
96 u64
drm_gem_vram_mmap_offset(struct drm_gem_vram_object
*gbo
);
97 s64
drm_gem_vram_offset(struct drm_gem_vram_object
*gbo
);
98 int drm_gem_vram_pin(struct drm_gem_vram_object
*gbo
, unsigned long pl_flag
);
99 int drm_gem_vram_unpin(struct drm_gem_vram_object
*gbo
);
100 int drm_gem_vram_vmap(struct drm_gem_vram_object
*gbo
, struct dma_buf_map
*map
);
101 void drm_gem_vram_vunmap(struct drm_gem_vram_object
*gbo
, struct dma_buf_map
*map
);
103 int drm_gem_vram_fill_create_dumb(struct drm_file
*file
,
104 struct drm_device
*dev
,
105 unsigned long pg_align
,
106 unsigned long pitch_align
,
107 struct drm_mode_create_dumb
*args
);
110 * Helpers for struct drm_driver
113 int drm_gem_vram_driver_dumb_create(struct drm_file
*file
,
114 struct drm_device
*dev
,
115 struct drm_mode_create_dumb
*args
);
116 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file
*file
,
117 struct drm_device
*dev
,
118 uint32_t handle
, uint64_t *offset
);
121 * Helpers for struct drm_plane_helper_funcs
124 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane
*plane
,
125 struct drm_plane_state
*new_state
);
127 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane
*plane
,
128 struct drm_plane_state
*old_state
);
131 * Helpers for struct drm_simple_display_pipe_funcs
134 int drm_gem_vram_simple_display_pipe_prepare_fb(
135 struct drm_simple_display_pipe
*pipe
,
136 struct drm_plane_state
*new_state
);
138 void drm_gem_vram_simple_display_pipe_cleanup_fb(
139 struct drm_simple_display_pipe
*pipe
,
140 struct drm_plane_state
*old_state
);
143 * define DRM_GEM_VRAM_DRIVER - default callback functions for \
146 * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize
147 * &struct drm_driver with default functions.
149 #define DRM_GEM_VRAM_DRIVER \
150 .debugfs_init = drm_vram_mm_debugfs_init, \
151 .dumb_create = drm_gem_vram_driver_dumb_create, \
152 .dumb_map_offset = drm_gem_vram_driver_dumb_mmap_offset, \
153 .gem_prime_mmap = drm_gem_prime_mmap
156 * VRAM memory manager
160 * struct drm_vram_mm - An instance of VRAM MM
161 * @vram_base: Base address of the managed video memory
162 * @vram_size: Size of the managed video memory in bytes
163 * @bdev: The TTM BO device.
164 * @funcs: TTM BO functions
166 * The fields &struct drm_vram_mm.vram_base and
167 * &struct drm_vram_mm.vrm_size are managed by VRAM MM, but are
168 * available for public read access. Use the field
169 * &struct drm_vram_mm.bdev to access the TTM BO device.
175 struct ttm_bo_device bdev
;
179 * drm_vram_mm_of_bdev() - \
180 Returns the container of type &struct ttm_bo_device for field bdev.
181 * @bdev: the TTM BO device
184 * The containing instance of &struct drm_vram_mm
186 static inline struct drm_vram_mm
*drm_vram_mm_of_bdev(
187 struct ttm_bo_device
*bdev
)
189 return container_of(bdev
, struct drm_vram_mm
, bdev
);
192 void drm_vram_mm_debugfs_init(struct drm_minor
*minor
);
195 * Helpers for integration with struct drm_device
198 struct drm_vram_mm
*drm_vram_helper_alloc_mm(
199 struct drm_device
*dev
, uint64_t vram_base
, size_t vram_size
);
200 void drm_vram_helper_release_mm(struct drm_device
*dev
);
202 int drmm_vram_helper_init(struct drm_device
*dev
, uint64_t vram_base
,
206 * Mode-config helpers
210 drm_vram_helper_mode_valid(struct drm_device
*dev
,
211 const struct drm_display_mode
*mode
);