drm_vma_manager.h revision 1.1.1.2 1 /* $NetBSD: drm_vma_manager.h,v 1.1.1.2 2018/08/27 01:35:00 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 (at) 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/fs.h>
30 #include <linux/mm.h>
31 #include <linux/module.h>
32 #include <linux/rbtree.h>
33 #include <linux/spinlock.h>
34 #include <linux/types.h>
35
36 struct drm_vma_offset_file {
37 struct rb_node vm_rb;
38 struct file *vm_filp;
39 unsigned long vm_count;
40 };
41
42 struct drm_vma_offset_node {
43 rwlock_t vm_lock;
44 struct drm_mm_node vm_node;
45 struct rb_node vm_rb;
46 struct rb_root vm_files;
47 };
48
49 struct drm_vma_offset_manager {
50 rwlock_t vm_lock;
51 struct rb_root vm_addr_space_rb;
52 struct drm_mm vm_addr_space_mm;
53 };
54
55 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
56 unsigned long page_offset, unsigned long size);
57 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
58
59 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
60 unsigned long start,
61 unsigned long pages);
62 int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
63 struct drm_vma_offset_node *node, unsigned long pages);
64 void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
65 struct drm_vma_offset_node *node);
66
67 int drm_vma_node_allow(struct drm_vma_offset_node *node, struct file *filp);
68 void drm_vma_node_revoke(struct drm_vma_offset_node *node, struct file *filp);
69 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
70 struct file *filp);
71
72 /**
73 * drm_vma_offset_exact_lookup_locked() - Look up node by exact address
74 * @mgr: Manager object
75 * @start: Start address (page-based, not byte-based)
76 * @pages: Size of object (page-based)
77 *
78 * Same as drm_vma_offset_lookup_locked() but does not allow any offset into the node.
79 * It only returns the exact object with the given start address.
80 *
81 * RETURNS:
82 * Node at exact start address @start.
83 */
84 static inline struct drm_vma_offset_node *
85 drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *mgr,
86 unsigned long start,
87 unsigned long pages)
88 {
89 struct drm_vma_offset_node *node;
90
91 node = drm_vma_offset_lookup_locked(mgr, start, pages);
92 return (node && node->vm_node.start == start) ? node : NULL;
93 }
94
95 /**
96 * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
97 * @mgr: Manager object
98 *
99 * Lock VMA manager for extended lookups. Only locked VMA function calls
100 * are allowed while holding this lock. All other contexts are blocked from VMA
101 * until the lock is released via drm_vma_offset_unlock_lookup().
102 *
103 * Use this if you need to take a reference to the objects returned by
104 * drm_vma_offset_lookup_locked() before releasing this lock again.
105 *
106 * This lock must not be used for anything else than extended lookups. You must
107 * not call any other VMA helpers while holding this lock.
108 *
109 * Note: You're in atomic-context while holding this lock!
110 */
111 static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
112 {
113 read_lock(&mgr->vm_lock);
114 }
115
116 /**
117 * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
118 * @mgr: Manager object
119 *
120 * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
121 */
122 static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
123 {
124 read_unlock(&mgr->vm_lock);
125 }
126
127 /**
128 * drm_vma_node_reset() - Initialize or reset node object
129 * @node: Node to initialize or reset
130 *
131 * Reset a node to its initial state. This must be called before using it with
132 * any VMA offset manager.
133 *
134 * This must not be called on an already allocated node, or you will leak
135 * memory.
136 */
137 static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
138 {
139 memset(node, 0, sizeof(*node));
140 node->vm_files = RB_ROOT;
141 rwlock_init(&node->vm_lock);
142 }
143
144 /**
145 * drm_vma_node_start() - Return start address for page-based addressing
146 * @node: Node to inspect
147 *
148 * Return the start address of the given node. This can be used as offset into
149 * the linear VM space that is provided by the VMA offset manager. Note that
150 * this can only be used for page-based addressing. If you need a proper offset
151 * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
152 * drm_vma_node_offset_addr() helper instead.
153 *
154 * RETURNS:
155 * Start address of @node for page-based addressing. 0 if the node does not
156 * have an offset allocated.
157 */
158 static inline unsigned long drm_vma_node_start(struct drm_vma_offset_node *node)
159 {
160 return node->vm_node.start;
161 }
162
163 /**
164 * drm_vma_node_size() - Return size (page-based)
165 * @node: Node to inspect
166 *
167 * Return the size as number of pages for the given node. This is the same size
168 * that was passed to drm_vma_offset_add(). If no offset is allocated for the
169 * node, this is 0.
170 *
171 * RETURNS:
172 * Size of @node as number of pages. 0 if the node does not have an offset
173 * allocated.
174 */
175 static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
176 {
177 return node->vm_node.size;
178 }
179
180 /**
181 * drm_vma_node_has_offset() - Check whether node is added to offset manager
182 * @node: Node to be checked
183 *
184 * RETURNS:
185 * true iff the node was previously allocated an offset and added to
186 * an vma offset manager.
187 */
188 static inline bool drm_vma_node_has_offset(struct drm_vma_offset_node *node)
189 {
190 return drm_mm_node_allocated(&node->vm_node);
191 }
192
193 /**
194 * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
195 * @node: Linked offset node
196 *
197 * Same as drm_vma_node_start() but returns the address as a valid offset that
198 * can be used for user-space mappings during mmap().
199 * This must not be called on unlinked nodes.
200 *
201 * RETURNS:
202 * Offset of @node for byte-based addressing. 0 if the node does not have an
203 * object allocated.
204 */
205 static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
206 {
207 return ((__u64)node->vm_node.start) << PAGE_SHIFT;
208 }
209
210 /**
211 * drm_vma_node_unmap() - Unmap offset node
212 * @node: Offset node
213 * @file_mapping: Address space to unmap @node from
214 *
215 * Unmap all userspace mappings for a given offset node. The mappings must be
216 * associated with the @file_mapping address-space. If no offset exists
217 * nothing is done.
218 *
219 * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
220 * is not called on this node concurrently.
221 */
222 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
223 struct address_space *file_mapping)
224 {
225 if (drm_vma_node_has_offset(node))
226 unmap_mapping_range(file_mapping,
227 drm_vma_node_offset_addr(node),
228 drm_vma_node_size(node) << PAGE_SHIFT, 1);
229 }
230
231 /**
232 * drm_vma_node_verify_access() - Access verification helper for TTM
233 * @node: Offset node
234 * @filp: Open-file
235 *
236 * This checks whether @filp is granted access to @node. It is the same as
237 * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
238 * verify_access() callbacks.
239 *
240 * RETURNS:
241 * 0 if access is granted, -EACCES otherwise.
242 */
243 static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
244 struct file *filp)
245 {
246 return drm_vma_node_is_allowed(node, filp) ? 0 : -EACCES;
247 }
248
249 #endif /* __DRM_VMA_MANAGER_H__ */
250