Merge tag 'topic/drm-misc-2016-08-12' of git://anongit.freedesktop.org/drm-intel...
[cascardo/linux.git] / include / drm / drm_vma_manager.h
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/fs.h>
28 #include <linux/mm.h>
29 #include <linux/module.h>
30 #include <linux/rbtree.h>
31 #include <linux/spinlock.h>
32 #include <linux/types.h>
33
34 struct drm_vma_offset_file {
35         struct rb_node vm_rb;
36         struct file *vm_filp;
37         unsigned long vm_count;
38 };
39
40 struct drm_vma_offset_node {
41         rwlock_t vm_lock;
42         struct drm_mm_node vm_node;
43         struct rb_root vm_files;
44 };
45
46 struct drm_vma_offset_manager {
47         rwlock_t vm_lock;
48         struct drm_mm vm_addr_space_mm;
49 };
50
51 void drm_vma_offset_manager_init(struct drm_vma_offset_manager *mgr,
52                                  unsigned long page_offset, unsigned long size);
53 void drm_vma_offset_manager_destroy(struct drm_vma_offset_manager *mgr);
54
55 struct drm_vma_offset_node *drm_vma_offset_lookup_locked(struct drm_vma_offset_manager *mgr,
56                                                            unsigned long start,
57                                                            unsigned long pages);
58 int drm_vma_offset_add(struct drm_vma_offset_manager *mgr,
59                        struct drm_vma_offset_node *node, unsigned long pages);
60 void drm_vma_offset_remove(struct drm_vma_offset_manager *mgr,
61                            struct drm_vma_offset_node *node);
62
63 int drm_vma_node_allow(struct drm_vma_offset_node *node, struct file *filp);
64 void drm_vma_node_revoke(struct drm_vma_offset_node *node, struct file *filp);
65 bool drm_vma_node_is_allowed(struct drm_vma_offset_node *node,
66                              struct file *filp);
67
68 /**
69  * drm_vma_offset_exact_lookup_locked() - Look up node by exact address
70  * @mgr: Manager object
71  * @start: Start address (page-based, not byte-based)
72  * @pages: Size of object (page-based)
73  *
74  * Same as drm_vma_offset_lookup_locked() but does not allow any offset into the node.
75  * It only returns the exact object with the given start address.
76  *
77  * RETURNS:
78  * Node at exact start address @start.
79  */
80 static inline struct drm_vma_offset_node *
81 drm_vma_offset_exact_lookup_locked(struct drm_vma_offset_manager *mgr,
82                                    unsigned long start,
83                                    unsigned long pages)
84 {
85         struct drm_vma_offset_node *node;
86
87         node = drm_vma_offset_lookup_locked(mgr, start, pages);
88         return (node && node->vm_node.start == start) ? node : NULL;
89 }
90
91 /**
92  * drm_vma_offset_lock_lookup() - Lock lookup for extended private use
93  * @mgr: Manager object
94  *
95  * Lock VMA manager for extended lookups. Only locked VMA function calls
96  * are allowed while holding this lock. All other contexts are blocked from VMA
97  * until the lock is released via drm_vma_offset_unlock_lookup().
98  *
99  * Use this if you need to take a reference to the objects returned by
100  * drm_vma_offset_lookup_locked() before releasing this lock again.
101  *
102  * This lock must not be used for anything else than extended lookups. You must
103  * not call any other VMA helpers while holding this lock.
104  *
105  * Note: You're in atomic-context while holding this lock!
106  */
107 static inline void drm_vma_offset_lock_lookup(struct drm_vma_offset_manager *mgr)
108 {
109         read_lock(&mgr->vm_lock);
110 }
111
112 /**
113  * drm_vma_offset_unlock_lookup() - Unlock lookup for extended private use
114  * @mgr: Manager object
115  *
116  * Release lookup-lock. See drm_vma_offset_lock_lookup() for more information.
117  */
118 static inline void drm_vma_offset_unlock_lookup(struct drm_vma_offset_manager *mgr)
119 {
120         read_unlock(&mgr->vm_lock);
121 }
122
123 /**
124  * drm_vma_node_reset() - Initialize or reset node object
125  * @node: Node to initialize or reset
126  *
127  * Reset a node to its initial state. This must be called before using it with
128  * any VMA offset manager.
129  *
130  * This must not be called on an already allocated node, or you will leak
131  * memory.
132  */
133 static inline void drm_vma_node_reset(struct drm_vma_offset_node *node)
134 {
135         memset(node, 0, sizeof(*node));
136         node->vm_files = RB_ROOT;
137         rwlock_init(&node->vm_lock);
138 }
139
140 /**
141  * drm_vma_node_start() - Return start address for page-based addressing
142  * @node: Node to inspect
143  *
144  * Return the start address of the given node. This can be used as offset into
145  * the linear VM space that is provided by the VMA offset manager. Note that
146  * this can only be used for page-based addressing. If you need a proper offset
147  * for user-space mappings, you must apply "<< PAGE_SHIFT" or use the
148  * drm_vma_node_offset_addr() helper instead.
149  *
150  * RETURNS:
151  * Start address of @node for page-based addressing. 0 if the node does not
152  * have an offset allocated.
153  */
154 static inline unsigned long drm_vma_node_start(struct drm_vma_offset_node *node)
155 {
156         return node->vm_node.start;
157 }
158
159 /**
160  * drm_vma_node_size() - Return size (page-based)
161  * @node: Node to inspect
162  *
163  * Return the size as number of pages for the given node. This is the same size
164  * that was passed to drm_vma_offset_add(). If no offset is allocated for the
165  * node, this is 0.
166  *
167  * RETURNS:
168  * Size of @node as number of pages. 0 if the node does not have an offset
169  * allocated.
170  */
171 static inline unsigned long drm_vma_node_size(struct drm_vma_offset_node *node)
172 {
173         return node->vm_node.size;
174 }
175
176 /**
177  * drm_vma_node_offset_addr() - Return sanitized offset for user-space mmaps
178  * @node: Linked offset node
179  *
180  * Same as drm_vma_node_start() but returns the address as a valid offset that
181  * can be used for user-space mappings during mmap().
182  * This must not be called on unlinked nodes.
183  *
184  * RETURNS:
185  * Offset of @node for byte-based addressing. 0 if the node does not have an
186  * object allocated.
187  */
188 static inline __u64 drm_vma_node_offset_addr(struct drm_vma_offset_node *node)
189 {
190         return ((__u64)node->vm_node.start) << PAGE_SHIFT;
191 }
192
193 /**
194  * drm_vma_node_unmap() - Unmap offset node
195  * @node: Offset node
196  * @file_mapping: Address space to unmap @node from
197  *
198  * Unmap all userspace mappings for a given offset node. The mappings must be
199  * associated with the @file_mapping address-space. If no offset exists
200  * nothing is done.
201  *
202  * This call is unlocked. The caller must guarantee that drm_vma_offset_remove()
203  * is not called on this node concurrently.
204  */
205 static inline void drm_vma_node_unmap(struct drm_vma_offset_node *node,
206                                       struct address_space *file_mapping)
207 {
208         if (drm_mm_node_allocated(&node->vm_node))
209                 unmap_mapping_range(file_mapping,
210                                     drm_vma_node_offset_addr(node),
211                                     drm_vma_node_size(node) << PAGE_SHIFT, 1);
212 }
213
214 /**
215  * drm_vma_node_verify_access() - Access verification helper for TTM
216  * @node: Offset node
217  * @filp: Open-file
218  *
219  * This checks whether @filp is granted access to @node. It is the same as
220  * drm_vma_node_is_allowed() but suitable as drop-in helper for TTM
221  * verify_access() callbacks.
222  *
223  * RETURNS:
224  * 0 if access is granted, -EACCES otherwise.
225  */
226 static inline int drm_vma_node_verify_access(struct drm_vma_offset_node *node,
227                                              struct file *filp)
228 {
229         return drm_vma_node_is_allowed(node, filp) ? 0 : -EACCES;
230 }
231
232 #endif /* __DRM_VMA_MANAGER_H__ */