1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #ifndef DRM_GEM_VRAM_HELPER_H
4 #define DRM_GEM_VRAM_HELPER_H
5 
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>
12 
13 #include <linux/kernel.h> /* for container_of() */
14 
15 struct drm_mode_create_dumb;
16 struct drm_plane;
17 struct drm_plane_state;
18 struct drm_simple_display_pipe;
19 struct filp;
20 struct vm_area_struct;
21 
22 #define DRM_GEM_VRAM_PL_FLAG_SYSTEM	(1 << 0)
23 #define DRM_GEM_VRAM_PL_FLAG_VRAM	(1 << 1)
24 #define DRM_GEM_VRAM_PL_FLAG_TOPDOWN	(1 << 2)
25 
26 /*
27  * Buffer-object helpers
28  */
29 
30 /**
31  * struct drm_gem_vram_object - GEM object backed by VRAM
32  * @gem:	GEM object
33  * @bo:		TTM buffer object
34  * @kmap:	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.
38  * @pin_count:	Pin counter
39  *
40  * The type struct drm_gem_vram_object represents a GEM object that is
41  * backed by VRAM. It can be used for simple framebuffer devices with
42  * dedicated memory. The buffer object can be evicted to system memory if
43  * video memory becomes scarce.
44  *
45  * GEM VRAM objects perform reference counting for pin and mapping
46  * operations. So a buffer object that has been pinned N times with
47  * drm_gem_vram_pin() must be unpinned N times with
48  * drm_gem_vram_unpin(). The same applies to pairs of
49  * drm_gem_vram_kmap() and drm_gem_vram_kunmap(), as well as pairs of
50  * drm_gem_vram_vmap() and drm_gem_vram_vunmap().
51  */
52 struct drm_gem_vram_object {
53 	struct ttm_buffer_object bo;
54 	struct ttm_bo_kmap_obj kmap;
55 
56 	/**
57 	 * @kmap_use_count:
58 	 *
59 	 * Reference count on the virtual address.
60 	 * The address are un-mapped when the count reaches zero.
61 	 */
62 	unsigned int kmap_use_count;
63 
64 	/* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */
65 	struct ttm_placement placement;
66 	struct ttm_place placements[2];
67 
68 	int pin_count;
69 };
70 
71 /**
72  * Returns the container of type &struct drm_gem_vram_object
73  * for field bo.
74  * @bo:		the VRAM buffer object
75  * Returns:	The containing GEM VRAM object
76  */
drm_gem_vram_of_bo(struct ttm_buffer_object * bo)77 static inline struct drm_gem_vram_object *drm_gem_vram_of_bo(
78 	struct ttm_buffer_object *bo)
79 {
80 	return container_of(bo, struct drm_gem_vram_object, bo);
81 }
82 
83 /**
84  * Returns the container of type &struct drm_gem_vram_object
85  * for field gem.
86  * @gem:	the GEM object
87  * Returns:	The containing GEM VRAM object
88  */
drm_gem_vram_of_gem(struct drm_gem_object * gem)89 static inline struct drm_gem_vram_object *drm_gem_vram_of_gem(
90 	struct drm_gem_object *gem)
91 {
92 	return container_of(gem, struct drm_gem_vram_object, bo.base);
93 }
94 
95 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
96 						size_t size,
97 						unsigned long pg_align);
98 void drm_gem_vram_put(struct drm_gem_vram_object *gbo);
99 u64 drm_gem_vram_mmap_offset(struct drm_gem_vram_object *gbo);
100 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo);
101 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag);
102 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo);
103 void *drm_gem_vram_vmap(struct drm_gem_vram_object *gbo);
104 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, void *vaddr);
105 
106 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
107 				  struct drm_device *dev,
108 				  unsigned long pg_align,
109 				  unsigned long pitch_align,
110 				  struct drm_mode_create_dumb *args);
111 
112 /*
113  * Helpers for struct drm_driver
114  */
115 
116 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
117 				    struct drm_device *dev,
118 				    struct drm_mode_create_dumb *args);
119 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
120 					 struct drm_device *dev,
121 					 uint32_t handle, uint64_t *offset);
122 
123 /*
124  * Helpers for struct drm_plane_helper_funcs
125  */
126 int
127 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
128 				     struct drm_plane_state *new_state);
129 void
130 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
131 				     struct drm_plane_state *old_state);
132 
133 /*
134  * Helpers for struct drm_simple_display_pipe_funcs
135  */
136 
137 int drm_gem_vram_simple_display_pipe_prepare_fb(
138 	struct drm_simple_display_pipe *pipe,
139 	struct drm_plane_state *new_state);
140 
141 void drm_gem_vram_simple_display_pipe_cleanup_fb(
142 	struct drm_simple_display_pipe *pipe,
143 	struct drm_plane_state *old_state);
144 
145 /**
146  * define DRM_GEM_VRAM_DRIVER - default callback functions for \
147 	&struct drm_driver
148  *
149  * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize
150  * &struct drm_driver with default functions.
151  */
152 #define DRM_GEM_VRAM_DRIVER \
153 	.debugfs_init             = drm_vram_mm_debugfs_init, \
154 	.dumb_create		  = drm_gem_vram_driver_dumb_create, \
155 	.dumb_map_offset	  = drm_gem_vram_driver_dumb_mmap_offset, \
156 	.gem_prime_mmap		  = drm_gem_prime_mmap
157 
158 /*
159  *  VRAM memory manager
160  */
161 
162 /**
163  * struct drm_vram_mm - An instance of VRAM MM
164  * @vram_base:	Base address of the managed video memory
165  * @vram_size:	Size of the managed video memory in bytes
166  * @bdev:	The TTM BO device.
167  * @funcs:	TTM BO functions
168  *
169  * The fields &struct drm_vram_mm.vram_base and
170  * &struct drm_vram_mm.vrm_size are managed by VRAM MM, but are
171  * available for public read access. Use the field
172  * &struct drm_vram_mm.bdev to access the TTM BO device.
173  */
174 struct drm_vram_mm {
175 	uint64_t vram_base;
176 	size_t vram_size;
177 
178 	struct ttm_bo_device bdev;
179 };
180 
181 /**
182  * drm_vram_mm_of_bdev() - \
183 	Returns the container of type &struct ttm_bo_device for field bdev.
184  * @bdev:	the TTM BO device
185  *
186  * Returns:
187  * The containing instance of &struct drm_vram_mm
188  */
drm_vram_mm_of_bdev(struct ttm_bo_device * bdev)189 static inline struct drm_vram_mm *drm_vram_mm_of_bdev(
190 	struct ttm_bo_device *bdev)
191 {
192 	return container_of(bdev, struct drm_vram_mm, bdev);
193 }
194 
195 void drm_vram_mm_debugfs_init(struct drm_minor *minor);
196 
197 /*
198  * Helpers for integration with struct drm_device
199  */
200 
201 struct drm_vram_mm *drm_vram_helper_alloc_mm(
202 	struct drm_device *dev, uint64_t vram_base, size_t vram_size);
203 void drm_vram_helper_release_mm(struct drm_device *dev);
204 
205 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
206 			  size_t vram_size);
207 
208 /*
209  * Mode-config helpers
210  */
211 
212 enum drm_mode_status
213 drm_vram_helper_mode_valid(struct drm_device *dev,
214 			   const struct drm_display_mode *mode);
215 
216 #endif
217