]>
Commit | Line | Data |
---|---|---|
fe3078fa DH |
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/module.h> | |
28 | #include <linux/rbtree.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/types.h> | |
31 | ||
32 | struct drm_vma_offset_node { | |
33 | struct drm_mm_node vm_node; | |
34 | struct rb_node vm_rb; | |
35 | }; | |
36 | ||
37 | struct drm_vma_offset_manager { | |
38 | rwlock_t vm_lock; | |
39 | struct rb_root vm_addr_space_rb; | |
40 | struct drm_mm vm_addr_space_mm; | |
41 | }; | |
42 | ||
43 | void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr, | |
44 | unsigned long page_offset, unsigned long size); | |
45 | void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr); | |
46 | ||
47 | struct drm_vma_offset_node *drm_vma_offset_lookup(struct drm_vma_offset_manager *mgr, | |
48 | unsigned long start, | |
49 | unsigned long pages); | |
50 | struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr, | |
51 | unsigned long start, | |
52 | unsigned long pages); | |
53 | int drm_vma_offset_add(struct drm_vma_offset_manager *mgr, | |
54 | struct drm_vma_offset_node *node, unsigned long pages); | |
55 | void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr, | |
56 | struct drm_vma_offset_node *node); | |
57 | ||
58 | /** | |
59 | * drm_vma_offset_exact_lookup() - Look up node by exact address | |
60 | * @mgr: Manager object | |
61 | * @start: Start address (page-based, not byte-based) | |
62 | * @pages: Size of object (page-based) | |
63 | * | |
64 | * Same as drm_vma_offset_lookup() but does not allow any offset into the node. | |
65 | * It only returns the exact object with the given start address. | |
66 | * | |
67 | * RETURNS: | |
68 | * Node at exact start address @start. | |
69 | */ | |
70 | static inline struct drm_vma_offset_node * | |
71 | drm_vma_offset_exact_lookup(struct drm_vma_offset_manager *mgr, | |
72 | unsigned long start, | |
73 | unsigned long pages) | |
74 | { | |
75 | struct drm_vma_offset_node *node; | |
76 | ||
77 | node = drm_vma_offset_lookup(mgr, start, pages); | |
78 | return (node && node->vm_node.start == start) ? node : NULL; | |
79 | } | |
80 | ||
81 | /** | |
82 | * drm_vma_offset_lock_lookup() - Lock lookup for extended private use | |
83 | * @mgr: Manager object | |
84 | * | |
85 | * Lock VMA manager for extended lookups. Only *_locked() VMA function calls | |
86 | * are allowed while holding this lock. All other contexts are blocked from VMA | |
87 | * until the lock is released via drm_vma_offset_unlock_lookup(). | |
88 | * | |
89 | * Use this if you need to take a reference to the objects returned by | |
90 | * drm_vma_offset_lookup_locked() before releasing this lock again. | |
91 | * | |
92 | * This lock must not be used for anything else than extended lookups. You must | |
93 | * not call any other VMA helpers while holding this lock. | |
94 | * | |
95 | * Note: You're in atomic-context while holding this lock! | |
96 | * | |
97 | * Example: | |
98 | * drm_vma_offset_lock_lookup(mgr); | |
99 | * node = drm_vma_offset_lookup_locked(mgr); | |
100 | * if (node) | |
101 | * kref_get_unless_zero(container_of(node, sth, entr)); | |
102 | * drm_vma_offset_unlock_lookup(mgr); | |
103 | */ | |
104 | static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr) | |
105 | { | |
106 | read_lock(&mgr->vm_lock); | |
107 | } | |
108 | ||
109 | /** | |
110 | * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use | |
111 | * @mgr: Manager object | |
112 | * | |
113 | * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information. | |
114 | */ | |
115 | static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr) | |
116 | { | |
117 | read_unlock(&mgr->vm_lock); | |
118 | } | |
119 | ||
120 | /** | |
121 | * drm_vma_node_reset() - Initialize or reset node object | |
122 | * @node: Node to initialize or reset | |
123 | * | |
124 | * Reset a node to its initial state. This must be called if @node isn't | |
125 | * already cleared (eg., via kzalloc) before using it with any VMA offset | |
126 | * manager. | |
127 | * | |
128 | * This must not be called on an already allocated node, or you will leak | |
129 | * memory. | |
130 | */ | |
131 | static inline void drm_vma_node_reset(struct drm_vma_offset_node *node) | |
132 | { | |
133 | memset(node, 0, sizeof(*node)); | |
134 | } | |
135 | ||
136 | /** | |
137 | * drm_vma_node_start() - Return start address for page-based addressing | |
138 | * @node: Node to inspect | |
139 | * | |
140 | * Return the start address of the given node. This can be used as offset into | |
141 | * the linear VM space that is provided by the VMA offset manager. Note that | |
142 | * this can only be used for page-based addressing. If you need a proper offset | |
143 | * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the | |
144 | * drm_vma_node_offset_addr() helper instead. | |
145 | * | |
146 | * RETURNS: | |
147 | * Start address of @node for page-based addressing. 0 if the node does not | |
148 | * have an offset allocated. | |
149 | */ | |
150 | static inline unsigned long drm_vma_node_start(struct drm_vma_offset_node *node) | |
151 | { | |
152 | return node->vm_node.start; | |
153 | } | |
154 | ||
155 | /** | |
156 | * drm_vma_node_size() - Return size (page-based) | |
157 | * @node: Node to inspect | |
158 | * | |
159 | * Return the size as number of pages for the given node. This is the same size | |
160 | * that was passed to drm_vma_offset_add(). If no offset is allocated for the | |
161 | * node, this is 0. | |
162 | * | |
163 | * RETURNS: | |
164 | * Size of @node as number of pages. 0 if the node does not have an offset | |
165 | * allocated. | |
166 | */ | |
167 | static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node) | |
168 | { | |
169 | return node->vm_node.size; | |
170 | } | |
171 | ||
172 | /** | |
173 | * drm_vma_node_has_offset() - Check whether node is added to offset manager | |
174 | * @node: Node to be checked | |
175 | * | |
176 | * RETURNS: | |
177 | * true iff the node was previously allocated an offset and added to | |
178 | * an vma offset manager. | |
179 | */ | |
180 | static inline bool drm_vma_node_has_offset(struct drm_vma_offset_node *node) | |
181 | { | |
182 | return drm_mm_node_allocated(&node->vm_node); | |
183 | } | |
184 | ||
185 | /** | |
186 | * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps | |
187 | * @node: Linked offset node | |
188 | * | |
189 | * Same as drm_vma_node_start() but returns the address as a valid offset that | |
190 | * can be used for user-space mappings during mmap(). | |
191 | * This must not be called on unlinked nodes. | |
192 | * | |
193 | * RETURNS: | |
194 | * Offset of @node for byte-based addressing. 0 if the node does not have an | |
195 | * object allocated. | |
196 | */ | |
197 | static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node) | |
198 | { | |
199 | return ((__u64)node->vm_node.start) << PAGE_SHIFT; | |
200 | } | |
201 | ||
202 | #endif /* __DRM_VMA_MANAGER_H__ */ |