1 /* $NetBSD: drm_vma_manager.h,v 1.3 2021/12/18 23:45:46 riastradh Exp $ */
2
3 #ifndef __DRM_VMA_MANAGER_H__
4 #define __DRM_VMA_MANAGER_H__
5
6 /*
7 * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #include <drm/drm_mm.h>
29 #include <linux/mm.h>
30 #include <linux/rbtree.h>
31 #include <linux/spinlock.h>
32 #include <linux/types.h>
33
34 /* We make up offsets for buffer objects so we can recognize them at
35 * mmap time. pgoff in mmap is an unsigned long, so we need to make sure
36 * that the faked up offset will fit
37 */
38 #if BITS_PER_LONG == 64
39 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFFUL >> PAGE_SHIFT) + 1)
40 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFFUL >> PAGE_SHIFT) * 256)
41 #else
42 #define DRM_FILE_PAGE_OFFSET_START ((0xFFFFFFFUL >> PAGE_SHIFT) + 1)
43 #define DRM_FILE_PAGE_OFFSET_SIZE ((0xFFFFFFFUL >> PAGE_SHIFT) * 16)
44 #endif
45
46 struct drm_file;
47
48 struct drm_vma_offset_file {
49 struct rb_node vm_rb;
50 struct drm_file *vm_tag;
51 unsigned long vm_count;
52 };
53
54 struct drm_vma_offset_node {
55 rwlock_t vm_lock;
56 struct drm_mm_node vm_node;
57 struct rb_root vm_files;
58 bool readonly:1;
59 };
60
61 struct drm_vma_offset_manager {
62 rwlock_t vm_lock;
63 struct drm_mm vm_addr_space_mm;
64 };
65
66 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
67 unsigned long page_offset, unsigned long size);
68 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
69
70 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
71 unsigned long start,
72 unsigned long pages);
73 int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
74 struct drm_vma_offset_node *node, unsigned long pages);
75 void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
76 struct drm_vma_offset_node *node);
77
78 int drm_vma_node_allow(struct drm_vma_offset_node *node, struct drm_file *tag);
79 void drm_vma_node_revoke(struct drm_vma_offset_node *node,
80 struct drm_file *tag);
81 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
82 struct drm_file *tag);
83
84 /**
85 * drm_vma_offset_exact_lookup_locked() - Look up node by exact address
86 * @mgr: Manager object
87 * @start: Start address (page-based, not byte-based)
88 * @pages: Size of object (page-based)
89 *
90 * Same as drm_vma_offset_lookup_locked() but does not allow any offset into the node.
91 * It only returns the exact object with the given start address.
92 *
93 * RETURNS:
94 * Node at exact start address @start.
95 */
96 static inline struct drm_vma_offset_node *
drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager * mgr,unsigned long start,unsigned long pages)97 drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *mgr,
98 unsigned long start,
99 unsigned long pages)
100 {
101 struct drm_vma_offset_node *node;
102
103 node = drm_vma_offset_lookup_locked(mgr, start, pages);
104 return (node && node->vm_node.start == start) ? node : NULL;
105 }
106
107 /**
108 * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
109 * @mgr: Manager object
110 *
111 * Lock VMA manager for extended lookups. Only locked VMA function calls
112 * are allowed while holding this lock. All other contexts are blocked from VMA
113 * until the lock is released via drm_vma_offset_unlock_lookup().
114 *
115 * Use this if you need to take a reference to the objects returned by
116 * drm_vma_offset_lookup_locked() before releasing this lock again.
117 *
118 * This lock must not be used for anything else than extended lookups. You must
119 * not call any other VMA helpers while holding this lock.
120 *
121 * Note: You're in atomic-context while holding this lock!
122 */
drm_vma_offset_lock_lookup(struct drm_vma_offset_manager * mgr)123 static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
124 {
125 read_lock(&mgr->vm_lock);
126 }
127
128 /**
129 * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
130 * @mgr: Manager object
131 *
132 * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
133 */
drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager * mgr)134 static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
135 {
136 read_unlock(&mgr->vm_lock);
137 }
138
139 /**
140 * drm_vma_node_reset() - Initialize or reset node object
141 * @node: Node to initialize or reset
142 *
143 * Reset a node to its initial state. This must be called before using it with
144 * any VMA offset manager.
145 *
146 * This must not be called on an already allocated node, or you will leak
147 * memory.
148 */
drm_vma_node_reset(struct drm_vma_offset_node * node)149 static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
150 {
151 memset(node, 0, sizeof(*node));
152 node->vm_files = RB_ROOT;
153 rwlock_init(&node->vm_lock);
154 }
155
156 /**
157 * drm_vma_node_start() - Return start address for page-based addressing
158 * @node: Node to inspect
159 *
160 * Return the start address of the given node. This can be used as offset into
161 * the linear VM space that is provided by the VMA offset manager. Note that
162 * this can only be used for page-based addressing. If you need a proper offset
163 * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
164 * drm_vma_node_offset_addr() helper instead.
165 *
166 * RETURNS:
167 * Start address of @node for page-based addressing. 0 if the node does not
168 * have an offset allocated.
169 */
drm_vma_node_start(const struct drm_vma_offset_node * node)170 static inline unsigned long drm_vma_node_start(const struct drm_vma_offset_node *node)
171 {
172 return node->vm_node.start;
173 }
174
175 /**
176 * drm_vma_node_size() - Return size (page-based)
177 * @node: Node to inspect
178 *
179 * Return the size as number of pages for the given node. This is the same size
180 * that was passed to drm_vma_offset_add(). If no offset is allocated for the
181 * node, this is 0.
182 *
183 * RETURNS:
184 * Size of @node as number of pages. 0 if the node does not have an offset
185 * allocated.
186 */
drm_vma_node_size(struct drm_vma_offset_node * node)187 static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
188 {
189 return node->vm_node.size;
190 }
191
192 /**
193 * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
194 * @node: Linked offset node
195 *
196 * Same as drm_vma_node_start() but returns the address as a valid offset that
197 * can be used for user-space mappings during mmap().
198 * This must not be called on unlinked nodes.
199 *
200 * RETURNS:
201 * Offset of @node for byte-based addressing. 0 if the node does not have an
202 * object allocated.
203 */
drm_vma_node_offset_addr(struct drm_vma_offset_node * node)204 static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
205 {
206 return ((__u64)node->vm_node.start) << PAGE_SHIFT;
207 }
208
209 /**
210 * drm_vma_node_unmap() - Unmap offset node
211 * @node: Offset node
212 * @file_mapping: Address space to unmap @node from
213 *
214 * Unmap all userspace mappings for a given offset node. The mappings must be
215 * associated with the @file_mapping address-space. If no offset exists
216 * nothing is done.
217 *
218 * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
219 * is not called on this node concurrently.
220 */
drm_vma_node_unmap(struct drm_vma_offset_node * node,struct address_space * file_mapping)221 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
222 struct address_space *file_mapping)
223 {
224 if (drm_mm_node_allocated(&node->vm_node))
225 unmap_mapping_range(file_mapping,
226 drm_vma_node_offset_addr(node),
227 drm_vma_node_size(node) << PAGE_SHIFT, 1);
228 }
229
230 /**
231 * drm_vma_node_verify_access() - Access verification helper for TTM
232 * @node: Offset node
233 * @tag: Tag of file to check
234 *
235 * This checks whether @tag is granted access to @node. It is the same as
236 * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
237 * verify_access() callbacks.
238 *
239 * RETURNS:
240 * 0 if access is granted, -EACCES otherwise.
241 */
drm_vma_node_verify_access(struct drm_vma_offset_node * node,struct drm_file * tag)242 static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
243 struct drm_file *tag)
244 {
245 return drm_vma_node_is_allowed(node, tag) ? 0 : -EACCES;
246 }
247
248 #endif /* __DRM_VMA_MANAGER_H__ */
249