blob: 72bf408f887fc9de8e597bad49e766f45b94cd0f [file] [log] [blame]
David Herrmannfe3078f2013-07-24 21:06:15 +02001#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>
David Herrmann51335df2013-07-24 21:10:03 +020027#include <linux/mm.h>
David Herrmannfe3078f2013-07-24 21:06:15 +020028#include <linux/module.h>
29#include <linux/rbtree.h>
30#include <linux/spinlock.h>
31#include <linux/types.h>
32
David Herrmannd9a1f0b2016-09-01 14:48:33 +020033struct drm_file;
34
David Herrmann88d7ebe2013-08-25 18:28:57 +020035struct drm_vma_offset_file {
36 struct rb_node vm_rb;
David Herrmannd9a1f0b2016-09-01 14:48:33 +020037 struct drm_file *vm_tag;
David Herrmann88d7ebe2013-08-25 18:28:57 +020038 unsigned long vm_count;
39};
40
David Herrmannfe3078f2013-07-24 21:06:15 +020041struct drm_vma_offset_node {
David Herrmann88d7ebe2013-08-25 18:28:57 +020042 rwlock_t vm_lock;
David Herrmannfe3078f2013-07-24 21:06:15 +020043 struct drm_mm_node vm_node;
David Herrmann88d7ebe2013-08-25 18:28:57 +020044 struct rb_root vm_files;
Chris Wilson91b712c2018-07-12 19:53:13 +010045 bool readonly:1;
David Herrmannfe3078f2013-07-24 21:06:15 +020046};
47
48struct drm_vma_offset_manager {
49 rwlock_t vm_lock;
David Herrmannfe3078f2013-07-24 21:06:15 +020050 struct drm_mm vm_addr_space_mm;
51};
52
53void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
54 unsigned long page_offset, unsigned long size);
55void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
56
David Herrmannfe3078f2013-07-24 21:06:15 +020057struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
58 unsigned long start,
59 unsigned long pages);
60int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
61 struct drm_vma_offset_node *node, unsigned long pages);
62void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
63 struct drm_vma_offset_node *node);
64
David Herrmannd9a1f0b2016-09-01 14:48:33 +020065int drm_vma_node_allow(struct drm_vma_offset_node *node, struct drm_file *tag);
66void drm_vma_node_revoke(struct drm_vma_offset_node *node,
67 struct drm_file *tag);
David Herrmann88d7ebe2013-08-25 18:28:57 +020068bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
David Herrmannd9a1f0b2016-09-01 14:48:33 +020069 struct drm_file *tag);
David Herrmann88d7ebe2013-08-25 18:28:57 +020070
David Herrmannfe3078f2013-07-24 21:06:15 +020071/**
Daniel Vetter2225cfe2015-10-15 11:33:43 +020072 * drm_vma_offset_exact_lookup_locked() - Look up node by exact address
David Herrmannfe3078f2013-07-24 21:06:15 +020073 * @mgr: Manager object
74 * @start: Start address (page-based, not byte-based)
75 * @pages: Size of object (page-based)
76 *
Daniel Vetter2225cfe2015-10-15 11:33:43 +020077 * Same as drm_vma_offset_lookup_locked() but does not allow any offset into the node.
David Herrmannfe3078f2013-07-24 21:06:15 +020078 * It only returns the exact object with the given start address.
79 *
80 * RETURNS:
81 * Node at exact start address @start.
82 */
83static inline struct drm_vma_offset_node *
Daniel Vetter2225cfe2015-10-15 11:33:43 +020084drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *mgr,
85 unsigned long start,
86 unsigned long pages)
David Herrmannfe3078f2013-07-24 21:06:15 +020087{
88 struct drm_vma_offset_node *node;
89
Daniel Vetter2225cfe2015-10-15 11:33:43 +020090 node = drm_vma_offset_lookup_locked(mgr, start, pages);
David Herrmannfe3078f2013-07-24 21:06:15 +020091 return (node && node->vm_node.start == start) ? node : NULL;
92}
93
94/**
95 * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
96 * @mgr: Manager object
97 *
Lukas Wunnerf71a6d62015-10-12 09:10:33 +020098 * Lock VMA manager for extended lookups. Only locked VMA function calls
David Herrmannfe3078f2013-07-24 21:06:15 +020099 * are allowed while holding this lock. All other contexts are blocked from VMA
100 * until the lock is released via drm_vma_offset_unlock_lookup().
101 *
102 * Use this if you need to take a reference to the objects returned by
103 * drm_vma_offset_lookup_locked() before releasing this lock again.
104 *
105 * This lock must not be used for anything else than extended lookups. You must
106 * not call any other VMA helpers while holding this lock.
107 *
108 * Note: You're in atomic-context while holding this lock!
David Herrmannfe3078f2013-07-24 21:06:15 +0200109 */
110static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
111{
112 read_lock(&mgr->vm_lock);
113}
114
115/**
116 * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
117 * @mgr: Manager object
118 *
119 * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
120 */
121static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
122{
123 read_unlock(&mgr->vm_lock);
124}
125
126/**
127 * drm_vma_node_reset() - Initialize or reset node object
128 * @node: Node to initialize or reset
129 *
David Herrmann88d7ebe2013-08-25 18:28:57 +0200130 * Reset a node to its initial state. This must be called before using it with
131 * any VMA offset manager.
David Herrmannfe3078f2013-07-24 21:06:15 +0200132 *
133 * This must not be called on an already allocated node, or you will leak
134 * memory.
135 */
136static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
137{
138 memset(node, 0, sizeof(*node));
David Herrmann88d7ebe2013-08-25 18:28:57 +0200139 node->vm_files = RB_ROOT;
140 rwlock_init(&node->vm_lock);
David Herrmannfe3078f2013-07-24 21:06:15 +0200141}
142
143/**
144 * drm_vma_node_start() - Return start address for page-based addressing
145 * @node: Node to inspect
146 *
147 * Return the start address of the given node. This can be used as offset into
148 * the linear VM space that is provided by the VMA offset manager. Note that
149 * this can only be used for page-based addressing. If you need a proper offset
150 * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
151 * drm_vma_node_offset_addr() helper instead.
152 *
153 * RETURNS:
154 * Start address of @node for page-based addressing. 0 if the node does not
155 * have an offset allocated.
156 */
157static inline unsigned long drm_vma_node_start(struct drm_vma_offset_node *node)
158{
159 return node->vm_node.start;
160}
161
162/**
163 * drm_vma_node_size() - Return size (page-based)
164 * @node: Node to inspect
165 *
166 * Return the size as number of pages for the given node. This is the same size
167 * that was passed to drm_vma_offset_add(). If no offset is allocated for the
168 * node, this is 0.
169 *
170 * RETURNS:
171 * Size of @node as number of pages. 0 if the node does not have an offset
172 * allocated.
173 */
174static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
175{
176 return node->vm_node.size;
177}
178
179/**
David Herrmannfe3078f2013-07-24 21:06:15 +0200180 * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
181 * @node: Linked offset node
182 *
183 * Same as drm_vma_node_start() but returns the address as a valid offset that
184 * can be used for user-space mappings during mmap().
185 * This must not be called on unlinked nodes.
186 *
187 * RETURNS:
188 * Offset of @node for byte-based addressing. 0 if the node does not have an
189 * object allocated.
190 */
191static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
192{
193 return ((__u64)node->vm_node.start) << PAGE_SHIFT;
194}
195
David Herrmann51335df2013-07-24 21:10:03 +0200196/**
197 * drm_vma_node_unmap() - Unmap offset node
198 * @node: Offset node
199 * @file_mapping: Address space to unmap @node from
200 *
201 * Unmap all userspace mappings for a given offset node. The mappings must be
David Herrmann44d847b2013-08-13 19:10:30 +0200202 * associated with the @file_mapping address-space. If no offset exists
203 * nothing is done.
David Herrmann51335df2013-07-24 21:10:03 +0200204 *
205 * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
206 * is not called on this node concurrently.
207 */
208static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
209 struct address_space *file_mapping)
210{
Daniel Vetterf74418a2016-03-30 11:40:52 +0200211 if (drm_mm_node_allocated(&node->vm_node))
David Herrmann51335df2013-07-24 21:10:03 +0200212 unmap_mapping_range(file_mapping,
213 drm_vma_node_offset_addr(node),
214 drm_vma_node_size(node) << PAGE_SHIFT, 1);
215}
216
David Herrmann88d7ebe2013-08-25 18:28:57 +0200217/**
218 * drm_vma_node_verify_access() - Access verification helper for TTM
219 * @node: Offset node
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200220 * @tag: Tag of file to check
David Herrmann88d7ebe2013-08-25 18:28:57 +0200221 *
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200222 * This checks whether @tag is granted access to @node. It is the same as
David Herrmann88d7ebe2013-08-25 18:28:57 +0200223 * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
224 * verify_access() callbacks.
225 *
226 * RETURNS:
227 * 0 if access is granted, -EACCES otherwise.
228 */
229static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200230 struct drm_file *tag)
David Herrmann88d7ebe2013-08-25 18:28:57 +0200231{
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200232 return drm_vma_node_is_allowed(node, tag) ? 0 : -EACCES;
David Herrmann88d7ebe2013-08-25 18:28:57 +0200233}
234
David Herrmannfe3078f2013-07-24 21:06:15 +0200235#endif /* __DRM_VMA_MANAGER_H__ */