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 ttm_buffer_object bo;
40 struct ttm_bo_kmap_obj kmap;
41
42 /* Supported placements are %TTM_PL_VRAM and %TTM_PL_SYSTEM */
43 struct ttm_placement placement;
44 struct ttm_place placements[2];
45
46 int pin_count;
47 };
48
49 /**
50 * Returns the container of type &struct drm_gem_vram_object
51 * for field bo.
52 * @bo: the VRAM buffer object
53 * Returns: The containing GEM VRAM object
54 */
drm_gem_vram_of_bo(struct ttm_buffer_object * bo)55 static inline struct drm_gem_vram_object *drm_gem_vram_of_bo(
56 struct ttm_buffer_object *bo)
57 {
58 return container_of(bo, struct drm_gem_vram_object, bo);
59 }
60
61 /**
62 * Returns the container of type &struct drm_gem_vram_object
63 * for field gem.
64 * @gem: the GEM object
65 * Returns: The containing GEM VRAM object
66 */
drm_gem_vram_of_gem(struct drm_gem_object * gem)67 static inline struct drm_gem_vram_object *drm_gem_vram_of_gem(
68 struct drm_gem_object *gem)
69 {
70 return container_of(gem, struct drm_gem_vram_object, bo.base);
71 }
72
73 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
74 struct ttm_bo_device *bdev,
75 size_t size,
76 unsigned long pg_align,
77 bool interruptible);
78 void drm_gem_vram_put(struct drm_gem_vram_object *gbo);
79 u64 drm_gem_vram_mmap_offset(struct drm_gem_vram_object *gbo);
80 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo);
81 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag);
82 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo);
83 void *drm_gem_vram_kmap(struct drm_gem_vram_object *gbo, bool map,
84 bool *is_iomem);
85 void drm_gem_vram_kunmap(struct drm_gem_vram_object *gbo);
86
87 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
88 struct drm_device *dev,
89 struct ttm_bo_device *bdev,
90 unsigned long pg_align,
91 bool interruptible,
92 struct drm_mode_create_dumb *args);
93
94 /*
95 * Helpers for struct ttm_bo_driver
96 */
97
98 void drm_gem_vram_bo_driver_evict_flags(struct ttm_buffer_object *bo,
99 struct ttm_placement *pl);
100
101 int drm_gem_vram_bo_driver_verify_access(struct ttm_buffer_object *bo,
102 struct file *filp);
103
104 extern const struct drm_vram_mm_funcs drm_gem_vram_mm_funcs;
105
106 /*
107 * Helpers for struct drm_driver
108 */
109
110 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
111 struct drm_device *dev,
112 struct drm_mode_create_dumb *args);
113 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
114 struct drm_device *dev,
115 uint32_t handle, uint64_t *offset);
116
117 /**
118 * define DRM_GEM_VRAM_DRIVER - default callback functions for \
119 &struct drm_driver
120 *
121 * Drivers that use VRAM MM and GEM VRAM can use this macro to initialize
122 * &struct drm_driver with default functions.
123 */
124 #define DRM_GEM_VRAM_DRIVER \
125 .dumb_create = drm_gem_vram_driver_dumb_create, \
126 .dumb_map_offset = drm_gem_vram_driver_dumb_mmap_offset, \
127 .gem_prime_mmap = drm_gem_prime_mmap
128
129 #endif
130