1 #ifndef __DRM_VMA_MANAGER_H__
2 #define __DRM_VMA_MANAGER_H__
5 * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include <drm/drm_mm.h>
28 #include <linux/module.h>
29 #include <linux/rbtree.h>
30 #include <linux/spinlock.h>
31 #include <linux/types.h>
33 struct drm_vma_offset_file {
36 unsigned long vm_count;
39 struct drm_vma_offset_node {
41 struct drm_mm_node vm_node;
43 struct rb_root vm_files;
46 struct drm_vma_offset_manager {
48 struct rb_root vm_addr_space_rb;
49 struct drm_mm vm_addr_space_mm;
52 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
53 unsigned long page_offset, unsigned long size);
54 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
56 struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr,
59 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
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);
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,
73 * drm_vma_offset_exact_lookup() - 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)
78 * Same as drm_vma_offset_lookup() but does not allow any offset into the node.
79 * It only returns the exact object with the given start address.
82 * Node at exact start address @start.
84 static inline struct drm_vma_offset_node *
85 drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *mgr,
89 struct drm_vma_offset_node *node;
91 node = drm_vma_offset_lookup(mgr, start, pages);
92 return (node && node->vm_node.start == start) ? node : NULL;
96 * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
97 * @mgr: Manager object
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().
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.
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.
109 * Note: You're in atomic-context while holding this lock!
112 * drm_vma_offset_lock_lookup(mgr);
113 * node = drm_vma_offset_lookup_locked(mgr);
115 * kref_get_unless_zero(container_of(node, sth, entr));
116 * drm_vma_offset_unlock_lookup(mgr);
118 static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
120 lockmgr(&mgr->vm_lock, LK_EXCLUSIVE);
124 * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
125 * @mgr: Manager object
127 * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
129 static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
131 lockmgr(&mgr->vm_lock, LK_RELEASE);
135 * drm_vma_node_reset() - Initialize or reset node object
136 * @node: Node to initialize or reset
138 * Reset a node to its initial state. This must be called before using it with
139 * any VMA offset manager.
141 * This must not be called on an already allocated node, or you will leak
144 static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
146 memset(node, 0, sizeof(*node));
147 node->vm_files = RB_ROOT;
148 lockinit(&node->vm_lock, "vmlock", 0, LK_CANRECURSE);
152 * drm_vma_node_start() - Return start address for page-based addressing
153 * @node: Node to inspect
155 * Return the start address of the given node. This can be used as offset into
156 * the linear VM space that is provided by the VMA offset manager. Note that
157 * this can only be used for page-based addressing. If you need a proper offset
158 * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
159 * drm_vma_node_offset_addr() helper instead.
162 * Start address of @node for page-based addressing. 0 if the node does not
163 * have an offset allocated.
165 static inline unsigned long drm_vma_node_start(struct drm_vma_offset_node *node)
167 return node->vm_node.start;
171 * drm_vma_node_size() - Return size (page-based)
172 * @node: Node to inspect
174 * Return the size as number of pages for the given node. This is the same size
175 * that was passed to drm_vma_offset_add(). If no offset is allocated for the
179 * Size of @node as number of pages. 0 if the node does not have an offset
182 static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
184 return node->vm_node.size;
188 * drm_vma_node_has_offset() - Check whether node is added to offset manager
189 * @node: Node to be checked
192 * true iff the node was previously allocated an offset and added to
193 * an vma offset manager.
195 static inline bool drm_vma_node_has_offset(struct drm_vma_offset_node *node)
197 return drm_mm_node_allocated(&node->vm_node);
201 * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
202 * @node: Linked offset node
204 * Same as drm_vma_node_start() but returns the address as a valid offset that
205 * can be used for user-space mappings during mmap().
206 * This must not be called on unlinked nodes.
209 * Offset of @node for byte-based addressing. 0 if the node does not have an
212 static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
214 return ((__u64)node->vm_node.start) << PAGE_SHIFT;
219 * drm_vma_node_unmap() - Unmap offset node
221 * @file_mapping: Address space to unmap @node from
223 * Unmap all userspace mappings for a given offset node. The mappings must be
224 * associated with the @file_mapping address-space. If no offset exists
227 * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
228 * is not called on this node concurrently.
230 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
231 struct address_space *file_mapping)
233 if (drm_vma_node_has_offset(node))
234 unmap_mapping_range(file_mapping,
235 drm_vma_node_offset_addr(node),
236 drm_vma_node_size(node) << PAGE_SHIFT, 1);
241 * drm_vma_node_verify_access() - Access verification helper for TTM
245 * This checks whether @filp is granted access to @node. It is the same as
246 * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
247 * verify_access() callbacks.
250 * 0 if access is granted, -EACCES otherwise.
252 static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
255 return drm_vma_node_is_allowed(node, filp) ? 0 : -EACCES;
258 #endif /* __DRM_VMA_MANAGER_H__ */