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