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 u64 drm_gem_vram_mmap_offset(struct drm_gem_vram_object *gbo);
81 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo);
82 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag);
83 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo);
84 void *drm_gem_vram_kmap(struct drm_gem_vram_object *gbo, bool map,
85 			bool *is_iomem);
86 void drm_gem_vram_kunmap(struct drm_gem_vram_object *gbo);
87 
88 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
89 				  struct drm_device *dev,
90 				  struct ttm_bo_device *bdev,
91 				  unsigned long pg_align,
92 				  bool interruptible,
93 				  struct drm_mode_create_dumb *args);
94 
95 /*
96  * Helpers for struct ttm_bo_driver
97  */
98 
99 void drm_gem_vram_bo_driver_evict_flags(struct ttm_buffer_object *bo,
100 					struct ttm_placement *pl);
101 
102 int drm_gem_vram_bo_driver_verify_access(struct ttm_buffer_object *bo,
103 					 struct file *filp);
104 
105 extern const struct drm_vram_mm_funcs drm_gem_vram_mm_funcs;
106 
107 /*
108  * Helpers for struct drm_driver
109  */
110 
111 void drm_gem_vram_driver_gem_free_object_unlocked(struct drm_gem_object *gem);
112 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
113 				    struct drm_device *dev,
114 				    struct drm_mode_create_dumb *args);
115 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
116 					 struct drm_device *dev,
117 					 uint32_t handle, uint64_t *offset);
118 
119 /**
120  * define DRM_GEM_VRAM_DRIVER - default callback functions for \
121 	&struct drm_driver
122  *
123  * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize
124  * &struct drm_driver with default functions.
125  */
126 #define DRM_GEM_VRAM_DRIVER \
127 	.gem_free_object_unlocked = \
128 		drm_gem_vram_driver_gem_free_object_unlocked, \
129 	.dumb_create		  = drm_gem_vram_driver_dumb_create, \
130 	.dumb_map_offset	  = drm_gem_vram_driver_dumb_mmap_offset
131 
132 /*
133  * PRIME helpers for struct drm_driver
134  */
135 
136 int drm_gem_vram_driver_gem_prime_pin(struct drm_gem_object *obj);
137 void drm_gem_vram_driver_gem_prime_unpin(struct drm_gem_object *obj);
138 void *drm_gem_vram_driver_gem_prime_vmap(struct drm_gem_object *obj);
139 void drm_gem_vram_driver_gem_prime_vunmap(struct drm_gem_object *obj,
140 					  void *vaddr);
141 int drm_gem_vram_driver_gem_prime_mmap(struct drm_gem_object *obj,
142 				       struct vm_area_struct *vma);
143 
144 #define DRM_GEM_VRAM_DRIVER_PRIME \
145 	.gem_prime_export = drm_gem_prime_export, \
146 	.gem_prime_import = drm_gem_prime_import, \
147 	.gem_prime_pin	  = drm_gem_vram_driver_gem_prime_pin, \
148 	.gem_prime_unpin  = drm_gem_vram_driver_gem_prime_unpin, \
149 	.gem_prime_vmap	  = drm_gem_vram_driver_gem_prime_vmap, \
150 	.gem_prime_vunmap = drm_gem_vram_driver_gem_prime_vunmap, \
151 	.gem_prime_mmap	  = drm_gem_vram_driver_gem_prime_mmap
152 
153 #endif
154