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