Merge remote-tracking branch 'iommu/next'
[deliverable/linux.git] / drivers / gpu / drm / drm_vma_manager.c
1 /*
2 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
3 * Copyright (c) 2012 David Airlie <airlied@linux.ie>
4 * Copyright (c) 2013 David Herrmann <dh.herrmann@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <drm/drmP.h>
26 #include <drm/drm_mm.h>
27 #include <drm/drm_vma_manager.h>
28 #include <linux/fs.h>
29 #include <linux/mm.h>
30 #include <linux/module.h>
31 #include <linux/rbtree.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/types.h>
35
36 /**
37 * DOC: vma offset manager
38 *
39 * The vma-manager is responsible to map arbitrary driver-dependent memory
40 * regions into the linear user address-space. It provides offsets to the
41 * caller which can then be used on the address_space of the drm-device. It
42 * takes care to not overlap regions, size them appropriately and to not
43 * confuse mm-core by inconsistent fake vm_pgoff fields.
44 * Drivers shouldn't use this for object placement in VMEM. This manager should
45 * only be used to manage mappings into linear user-space VMs.
46 *
47 * We use drm_mm as backend to manage object allocations. But it is highly
48 * optimized for alloc/free calls, not lookups. Hence, we use an rb-tree to
49 * speed up offset lookups.
50 *
51 * You must not use multiple offset managers on a single address_space.
52 * Otherwise, mm-core will be unable to tear down memory mappings as the VM will
53 * no longer be linear.
54 *
55 * This offset manager works on page-based addresses. That is, every argument
56 * and return code (with the exception of drm_vma_node_offset_addr()) is given
57 * in number of pages, not number of bytes. That means, object sizes and offsets
58 * must always be page-aligned (as usual).
59 * If you want to get a valid byte-based user-space address for a given offset,
60 * please see drm_vma_node_offset_addr().
61 *
62 * Additionally to offset management, the vma offset manager also handles access
63 * management. For every open-file context that is allowed to access a given
64 * node, you must call drm_vma_node_allow(). Otherwise, an mmap() call on this
65 * open-file with the offset of the node will fail with -EACCES. To revoke
66 * access again, use drm_vma_node_revoke(). However, the caller is responsible
67 * for destroying already existing mappings, if required.
68 */
69
70 /**
71 * drm_vma_offset_manager_init - Initialize new offset-manager
72 * @mgr: Manager object
73 * @page_offset: Offset of available memory area (page-based)
74 * @size: Size of available address space range (page-based)
75 *
76 * Initialize a new offset-manager. The offset and area size available for the
77 * manager are given as @page_offset and @size. Both are interpreted as
78 * page-numbers, not bytes.
79 *
80 * Adding/removing nodes from the manager is locked internally and protected
81 * against concurrent access. However, node allocation and destruction is left
82 * for the caller. While calling into the vma-manager, a given node must
83 * always be guaranteed to be referenced.
84 */
85 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
86 unsigned long page_offset, unsigned long size)
87 {
88 rwlock_init(&mgr->vm_lock);
89 drm_mm_init(&mgr->vm_addr_space_mm, page_offset, size);
90 }
91 EXPORT_SYMBOL(drm_vma_offset_manager_init);
92
93 /**
94 * drm_vma_offset_manager_destroy() - Destroy offset manager
95 * @mgr: Manager object
96 *
97 * Destroy an object manager which was previously created via
98 * drm_vma_offset_manager_init(). The caller must remove all allocated nodes
99 * before destroying the manager. Otherwise, drm_mm will refuse to free the
100 * requested resources.
101 *
102 * The manager must not be accessed after this function is called.
103 */
104 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr)
105 {
106 /* take the lock to protect against buggy drivers */
107 write_lock(&mgr->vm_lock);
108 drm_mm_takedown(&mgr->vm_addr_space_mm);
109 write_unlock(&mgr->vm_lock);
110 }
111 EXPORT_SYMBOL(drm_vma_offset_manager_destroy);
112
113 /**
114 * drm_vma_offset_lookup_locked() - Find node in offset space
115 * @mgr: Manager object
116 * @start: Start address for object (page-based)
117 * @pages: Size of object (page-based)
118 *
119 * Find a node given a start address and object size. This returns the _best_
120 * match for the given node. That is, @start may point somewhere into a valid
121 * region and the given node will be returned, as long as the node spans the
122 * whole requested area (given the size in number of pages as @pages).
123 *
124 * Note that before lookup the vma offset manager lookup lock must be acquired
125 * with drm_vma_offset_lock_lookup(). See there for an example. This can then be
126 * used to implement weakly referenced lookups using kref_get_unless_zero().
127 *
128 * Example:
129 *
130 * ::
131 *
132 * drm_vma_offset_lock_lookup(mgr);
133 * node = drm_vma_offset_lookup_locked(mgr);
134 * if (node)
135 * kref_get_unless_zero(container_of(node, sth, entr));
136 * drm_vma_offset_unlock_lookup(mgr);
137 *
138 * RETURNS:
139 * Returns NULL if no suitable node can be found. Otherwise, the best match
140 * is returned. It's the caller's responsibility to make sure the node doesn't
141 * get destroyed before the caller can access it.
142 */
143 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
144 unsigned long start,
145 unsigned long pages)
146 {
147 struct drm_mm_node *node, *best;
148 struct rb_node *iter;
149 unsigned long offset;
150
151 iter = mgr->vm_addr_space_mm.interval_tree.rb_node;
152 best = NULL;
153
154 while (likely(iter)) {
155 node = rb_entry(iter, struct drm_mm_node, rb);
156 offset = node->start;
157 if (start >= offset) {
158 iter = iter->rb_right;
159 best = node;
160 if (start == offset)
161 break;
162 } else {
163 iter = iter->rb_left;
164 }
165 }
166
167 /* verify that the node spans the requested area */
168 if (best) {
169 offset = best->start + best->size;
170 if (offset < start + pages)
171 best = NULL;
172 }
173
174 if (!best)
175 return NULL;
176
177 return container_of(best, struct drm_vma_offset_node, vm_node);
178 }
179 EXPORT_SYMBOL(drm_vma_offset_lookup_locked);
180
181 /**
182 * drm_vma_offset_add() - Add offset node to manager
183 * @mgr: Manager object
184 * @node: Node to be added
185 * @pages: Allocation size visible to user-space (in number of pages)
186 *
187 * Add a node to the offset-manager. If the node was already added, this does
188 * nothing and return 0. @pages is the size of the object given in number of
189 * pages.
190 * After this call succeeds, you can access the offset of the node until it
191 * is removed again.
192 *
193 * If this call fails, it is safe to retry the operation or call
194 * drm_vma_offset_remove(), anyway. However, no cleanup is required in that
195 * case.
196 *
197 * @pages is not required to be the same size as the underlying memory object
198 * that you want to map. It only limits the size that user-space can map into
199 * their address space.
200 *
201 * RETURNS:
202 * 0 on success, negative error code on failure.
203 */
204 int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
205 struct drm_vma_offset_node *node, unsigned long pages)
206 {
207 int ret;
208
209 write_lock(&mgr->vm_lock);
210
211 if (drm_mm_node_allocated(&node->vm_node)) {
212 ret = 0;
213 goto out_unlock;
214 }
215
216 ret = drm_mm_insert_node(&mgr->vm_addr_space_mm, &node->vm_node,
217 pages, 0, DRM_MM_SEARCH_DEFAULT);
218 if (ret)
219 goto out_unlock;
220
221 out_unlock:
222 write_unlock(&mgr->vm_lock);
223 return ret;
224 }
225 EXPORT_SYMBOL(drm_vma_offset_add);
226
227 /**
228 * drm_vma_offset_remove() - Remove offset node from manager
229 * @mgr: Manager object
230 * @node: Node to be removed
231 *
232 * Remove a node from the offset manager. If the node wasn't added before, this
233 * does nothing. After this call returns, the offset and size will be 0 until a
234 * new offset is allocated via drm_vma_offset_add() again. Helper functions like
235 * drm_vma_node_start() and drm_vma_node_offset_addr() will return 0 if no
236 * offset is allocated.
237 */
238 void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
239 struct drm_vma_offset_node *node)
240 {
241 write_lock(&mgr->vm_lock);
242
243 if (drm_mm_node_allocated(&node->vm_node)) {
244 drm_mm_remove_node(&node->vm_node);
245 memset(&node->vm_node, 0, sizeof(node->vm_node));
246 }
247
248 write_unlock(&mgr->vm_lock);
249 }
250 EXPORT_SYMBOL(drm_vma_offset_remove);
251
252 /**
253 * drm_vma_node_allow - Add open-file to list of allowed users
254 * @node: Node to modify
255 * @filp: Open file to add
256 *
257 * Add @filp to the list of allowed open-files for this node. If @filp is
258 * already on this list, the ref-count is incremented.
259 *
260 * The list of allowed-users is preserved across drm_vma_offset_add() and
261 * drm_vma_offset_remove() calls. You may even call it if the node is currently
262 * not added to any offset-manager.
263 *
264 * You must remove all open-files the same number of times as you added them
265 * before destroying the node. Otherwise, you will leak memory.
266 *
267 * This is locked against concurrent access internally.
268 *
269 * RETURNS:
270 * 0 on success, negative error code on internal failure (out-of-mem)
271 */
272 int drm_vma_node_allow(struct drm_vma_offset_node *node, struct file *filp)
273 {
274 struct rb_node **iter;
275 struct rb_node *parent = NULL;
276 struct drm_vma_offset_file *new, *entry;
277 int ret = 0;
278
279 /* Preallocate entry to avoid atomic allocations below. It is quite
280 * unlikely that an open-file is added twice to a single node so we
281 * don't optimize for this case. OOM is checked below only if the entry
282 * is actually used. */
283 new = kmalloc(sizeof(*entry), GFP_KERNEL);
284
285 write_lock(&node->vm_lock);
286
287 iter = &node->vm_files.rb_node;
288
289 while (likely(*iter)) {
290 parent = *iter;
291 entry = rb_entry(*iter, struct drm_vma_offset_file, vm_rb);
292
293 if (filp == entry->vm_filp) {
294 entry->vm_count++;
295 goto unlock;
296 } else if (filp > entry->vm_filp) {
297 iter = &(*iter)->rb_right;
298 } else {
299 iter = &(*iter)->rb_left;
300 }
301 }
302
303 if (!new) {
304 ret = -ENOMEM;
305 goto unlock;
306 }
307
308 new->vm_filp = filp;
309 new->vm_count = 1;
310 rb_link_node(&new->vm_rb, parent, iter);
311 rb_insert_color(&new->vm_rb, &node->vm_files);
312 new = NULL;
313
314 unlock:
315 write_unlock(&node->vm_lock);
316 kfree(new);
317 return ret;
318 }
319 EXPORT_SYMBOL(drm_vma_node_allow);
320
321 /**
322 * drm_vma_node_revoke - Remove open-file from list of allowed users
323 * @node: Node to modify
324 * @filp: Open file to remove
325 *
326 * Decrement the ref-count of @filp in the list of allowed open-files on @node.
327 * If the ref-count drops to zero, remove @filp from the list. You must call
328 * this once for every drm_vma_node_allow() on @filp.
329 *
330 * This is locked against concurrent access internally.
331 *
332 * If @filp is not on the list, nothing is done.
333 */
334 void drm_vma_node_revoke(struct drm_vma_offset_node *node, struct file *filp)
335 {
336 struct drm_vma_offset_file *entry;
337 struct rb_node *iter;
338
339 write_lock(&node->vm_lock);
340
341 iter = node->vm_files.rb_node;
342 while (likely(iter)) {
343 entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
344 if (filp == entry->vm_filp) {
345 if (!--entry->vm_count) {
346 rb_erase(&entry->vm_rb, &node->vm_files);
347 kfree(entry);
348 }
349 break;
350 } else if (filp > entry->vm_filp) {
351 iter = iter->rb_right;
352 } else {
353 iter = iter->rb_left;
354 }
355 }
356
357 write_unlock(&node->vm_lock);
358 }
359 EXPORT_SYMBOL(drm_vma_node_revoke);
360
361 /**
362 * drm_vma_node_is_allowed - Check whether an open-file is granted access
363 * @node: Node to check
364 * @filp: Open-file to check for
365 *
366 * Search the list in @node whether @filp is currently on the list of allowed
367 * open-files (see drm_vma_node_allow()).
368 *
369 * This is locked against concurrent access internally.
370 *
371 * RETURNS:
372 * true iff @filp is on the list
373 */
374 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
375 struct file *filp)
376 {
377 struct drm_vma_offset_file *entry;
378 struct rb_node *iter;
379
380 read_lock(&node->vm_lock);
381
382 iter = node->vm_files.rb_node;
383 while (likely(iter)) {
384 entry = rb_entry(iter, struct drm_vma_offset_file, vm_rb);
385 if (filp == entry->vm_filp)
386 break;
387 else if (filp > entry->vm_filp)
388 iter = iter->rb_right;
389 else
390 iter = iter->rb_left;
391 }
392
393 read_unlock(&node->vm_lock);
394
395 return iter;
396 }
397 EXPORT_SYMBOL(drm_vma_node_is_allowed);
This page took 0.03846 seconds and 5 git commands to generate.