xref: /openbmc/linux/include/drm/drm_gem_vram_helper.h (revision abade675e02e1b73da0c20ffaf08fbe309038298)
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_gem.h>
7 #include <drm/ttm/ttm_bo_api.h>
8 #include <drm/ttm/ttm_placement.h>
9 #include <linux/kernel.h> /* for container_of() */
10 
11 struct drm_mode_create_dumb;
12 struct drm_vram_mm_funcs;
13 struct filp;
14 struct vm_area_struct;
15 
16 #define DRM_GEM_VRAM_PL_FLAG_VRAM	TTM_PL_FLAG_VRAM
17 #define DRM_GEM_VRAM_PL_FLAG_SYSTEM	TTM_PL_FLAG_SYSTEM
18 
19 /*
20  * Buffer-object helpers
21  */
22 
23 /**
24  * struct drm_gem_vram_object - GEM object backed by VRAM
25  * @gem:	GEM object
26  * @bo:		TTM buffer object
27  * @kmap:	Mapping information for @bo
28  * @placement:	TTM placement information. Supported placements are \
29 	%TTM_PL_VRAM and %TTM_PL_SYSTEM
30  * @placements:	TTM placement information.
31  * @pin_count:	Pin counter
32  *
33  * The type struct drm_gem_vram_object represents a GEM object that is
34  * backed by VRAM. It can be used for simple framebuffer devices with
35  * dedicated memory. The buffer object can be evicted to system memory if
36  * video memory becomes scarce.
37  */
38 struct drm_gem_vram_object {
39 	struct drm_gem_object gem;
40 	struct ttm_buffer_object bo;
41 	struct ttm_bo_kmap_obj kmap;
42 
43 	/* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */
44 	struct ttm_placement placement;
45 	struct ttm_place placements[2];
46 
47 	int pin_count;
48 };
49 
50 /**
51  * Returns the container of type &struct drm_gem_vram_object
52  * for field bo.
53  * @bo:		the VRAM buffer object
54  * Returns:	The containing GEM VRAM object
55  */
56 static inline struct drm_gem_vram_object *drm_gem_vram_of_bo(
57 	struct ttm_buffer_object *bo)
58 {
59 	return container_of(bo, struct drm_gem_vram_object, bo);
60 }
61 
62 /**
63  * Returns the container of type &struct drm_gem_vram_object
64  * for field gem.
65  * @gem:	the GEM object
66  * Returns:	The containing GEM VRAM object
67  */
68 static inline struct drm_gem_vram_object *drm_gem_vram_of_gem(
69 	struct drm_gem_object *gem)
70 {
71 	return container_of(gem, struct drm_gem_vram_object, gem);
72 }
73 
74 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
75 						struct ttm_bo_device *bdev,
76 						size_t size,
77 						unsigned long pg_align,
78 						bool interruptible);
79 void drm_gem_vram_put(struct drm_gem_vram_object *gbo);
80 int drm_gem_vram_lock(struct drm_gem_vram_object *gbo, bool no_wait);
81 void drm_gem_vram_unlock(struct drm_gem_vram_object *gbo);
82 u64 drm_gem_vram_mmap_offset(struct drm_gem_vram_object *gbo);
83 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo);
84 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag);
85 int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
86 			      unsigned long pl_flag);
87 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo);
88 int drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo);
89 void *drm_gem_vram_kmap_at(struct drm_gem_vram_object *gbo, bool map,
90 			   bool *is_iomem, struct ttm_bo_kmap_obj *kmap);
91 void *drm_gem_vram_kmap(struct drm_gem_vram_object *gbo, bool map,
92 			bool *is_iomem);
93 void drm_gem_vram_kunmap_at(struct drm_gem_vram_object *gbo,
94 			    struct ttm_bo_kmap_obj *kmap);
95 void drm_gem_vram_kunmap(struct drm_gem_vram_object *gbo);
96 
97 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
98 				  struct drm_device *dev,
99 				  struct ttm_bo_device *bdev,
100 				  unsigned long pg_align,
101 				  bool interruptible,
102 				  struct drm_mode_create_dumb *args);
103 
104 /*
105  * Helpers for struct ttm_bo_driver
106  */
107 
108 void drm_gem_vram_bo_driver_evict_flags(struct ttm_buffer_object *bo,
109 					struct ttm_placement *pl);
110 
111 int drm_gem_vram_bo_driver_verify_access(struct ttm_buffer_object *bo,
112 					 struct file *filp);
113 
114 extern const struct drm_vram_mm_funcs drm_gem_vram_mm_funcs;
115 
116 /*
117  * Helpers for struct drm_driver
118  */
119 
120 void drm_gem_vram_driver_gem_free_object_unlocked(struct drm_gem_object *gem);
121 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
122 				    struct drm_device *dev,
123 				    struct drm_mode_create_dumb *args);
124 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
125 					 struct drm_device *dev,
126 					 uint32_t handle, uint64_t *offset);
127 
128 /**
129  * define DRM_GEM_VRAM_DRIVER - default callback functions for \
130 	&struct drm_driver
131  *
132  * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize
133  * &struct drm_driver with default functions.
134  */
135 #define DRM_GEM_VRAM_DRIVER \
136 	.gem_free_object_unlocked = \
137 		drm_gem_vram_driver_gem_free_object_unlocked, \
138 	.dumb_create		  = drm_gem_vram_driver_dumb_create, \
139 	.dumb_map_offset	  = drm_gem_vram_driver_dumb_mmap_offset
140 
141 /*
142  * PRIME helpers for struct drm_driver
143  */
144 
145 int drm_gem_vram_driver_gem_prime_pin(struct drm_gem_object *obj);
146 void drm_gem_vram_driver_gem_prime_unpin(struct drm_gem_object *obj);
147 void *drm_gem_vram_driver_gem_prime_vmap(struct drm_gem_object *obj);
148 void drm_gem_vram_driver_gem_prime_vunmap(struct drm_gem_object *obj,
149 					  void *vaddr);
150 int drm_gem_vram_driver_gem_prime_mmap(struct drm_gem_object *obj,
151 				       struct vm_area_struct *vma);
152 
153 #define DRM_GEM_VRAM_DRIVER_PRIME \
154 	.gem_prime_export = drm_gem_prime_export, \
155 	.gem_prime_import = drm_gem_prime_import, \
156 	.gem_prime_pin	  = drm_gem_vram_driver_gem_prime_pin, \
157 	.gem_prime_unpin  = drm_gem_vram_driver_gem_prime_unpin, \
158 	.gem_prime_vmap	  = drm_gem_vram_driver_gem_prime_vmap, \
159 	.gem_prime_vunmap = drm_gem_vram_driver_gem_prime_vunmap, \
160 	.gem_prime_mmap	  = drm_gem_vram_driver_gem_prime_mmap
161 
162 #endif
163