1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #ifndef DRM_VRAM_MM_HELPER_H
4 #define DRM_VRAM_MM_HELPER_H
5
6 #include <drm/drm_file.h>
7 #include <drm/drm_ioctl.h>
8 #include <drm/ttm/ttm_bo_driver.h>
9
10 struct drm_device;
11
12 /**
13 * struct drm_vram_mm_funcs - Callback functions for &struct drm_vram_mm
14 * @evict_flags: Provides an implementation for struct \
15 &ttm_bo_driver.evict_flags
16 * @verify_access: Provides an implementation for \
17 struct &ttm_bo_driver.verify_access
18 *
19 * These callback function integrate VRAM MM with TTM buffer objects. New
20 * functions can be added if necessary.
21 */
22 struct drm_vram_mm_funcs {
23 void (*evict_flags)(struct ttm_buffer_object *bo,
24 struct ttm_placement *placement);
25 int (*verify_access)(struct ttm_buffer_object *bo, struct file *filp);
26 };
27
28 /**
29 * struct drm_vram_mm - An instance of VRAM MM
30 * @vram_base: Base address of the managed video memory
31 * @vram_size: Size of the managed video memory in bytes
32 * @bdev: The TTM BO device.
33 * @funcs: TTM BO functions
34 *
35 * The fields &struct drm_vram_mm.vram_base and
36 * &struct drm_vram_mm.vrm_size are managed by VRAM MM, but are
37 * available for public read access. Use the field
38 * &struct drm_vram_mm.bdev to access the TTM BO device.
39 */
40 struct drm_vram_mm {
41 uint64_t vram_base;
42 size_t vram_size;
43
44 struct ttm_bo_device bdev;
45
46 const struct drm_vram_mm_funcs *funcs;
47 };
48
49 /**
50 * drm_vram_mm_of_bdev() - \
51 Returns the container of type &struct ttm_bo_device for field bdev.
52 * @bdev: the TTM BO device
53 *
54 * Returns:
55 * The containing instance of &struct drm_vram_mm
56 */
drm_vram_mm_of_bdev(struct ttm_bo_device * bdev)57 static inline struct drm_vram_mm *drm_vram_mm_of_bdev(
58 struct ttm_bo_device *bdev)
59 {
60 return container_of(bdev, struct drm_vram_mm, bdev);
61 }
62
63 int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
64 uint64_t vram_base, size_t vram_size,
65 const struct drm_vram_mm_funcs *funcs);
66 void drm_vram_mm_cleanup(struct drm_vram_mm *vmm);
67
68 int drm_vram_mm_mmap(struct file *filp, struct vm_area_struct *vma,
69 struct drm_vram_mm *vmm);
70
71 /*
72 * Helpers for integration with struct drm_device
73 */
74
75 struct drm_vram_mm *drm_vram_helper_alloc_mm(
76 struct drm_device *dev, uint64_t vram_base, size_t vram_size,
77 const struct drm_vram_mm_funcs *funcs);
78 void drm_vram_helper_release_mm(struct drm_device *dev);
79
80 /*
81 * Helpers for &struct file_operations
82 */
83
84 int drm_vram_mm_file_operations_mmap(
85 struct file *filp, struct vm_area_struct *vma);
86
87 /**
88 * define DRM_VRAM_MM_FILE_OPERATIONS - default callback functions for \
89 &struct file_operations
90 *
91 * Drivers that use VRAM MM can use this macro to initialize
92 * &struct file_operations with default functions.
93 */
94 #define DRM_VRAM_MM_FILE_OPERATIONS \
95 .llseek = no_llseek, \
96 .read = drm_read, \
97 .poll = drm_poll, \
98 .unlocked_ioctl = drm_ioctl, \
99 .compat_ioctl = drm_compat_ioctl, \
100 .mmap = drm_vram_mm_file_operations_mmap, \
101 .open = drm_open, \
102 .release = drm_release \
103
104 #endif
105