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