1 /* 2 * Copyright 2020 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Christian König 23 */ 24 25 #ifndef _TTM_RESOURCE_H_ 26 #define _TTM_RESOURCE_H_ 27 28 #include <linux/types.h> 29 #include <linux/mutex.h> 30 #include <linux/dma-fence.h> 31 #include <drm/drm_print.h> 32 33 #define TTM_MAX_BO_PRIORITY 4U 34 35 struct ttm_bo_device; 36 struct ttm_resource_manager; 37 struct ttm_resource; 38 struct ttm_place; 39 struct ttm_buffer_object; 40 41 struct ttm_resource_manager_func { 42 /** 43 * struct ttm_resource_manager_func member alloc 44 * 45 * @man: Pointer to a memory type manager. 46 * @bo: Pointer to the buffer object we're allocating space for. 47 * @placement: Placement details. 48 * @flags: Additional placement flags. 49 * @mem: Pointer to a struct ttm_resource to be filled in. 50 * 51 * This function should allocate space in the memory type managed 52 * by @man. Placement details if 53 * applicable are given by @placement. If successful, 54 * @mem::mm_node should be set to a non-null value, and 55 * @mem::start should be set to a value identifying the beginning 56 * of the range allocated, and the function should return zero. 57 * If the memory region accommodate the buffer object, @mem::mm_node 58 * should be set to NULL, and the function should return 0. 59 * If a system error occurred, preventing the request to be fulfilled, 60 * the function should return a negative error code. 61 * 62 * Note that @mem::mm_node will only be dereferenced by 63 * struct ttm_resource_manager functions and optionally by the driver, 64 * which has knowledge of the underlying type. 65 * 66 * This function may not be called from within atomic context, so 67 * an implementation can and must use either a mutex or a spinlock to 68 * protect any data structures managing the space. 69 */ 70 int (*alloc)(struct ttm_resource_manager *man, 71 struct ttm_buffer_object *bo, 72 const struct ttm_place *place, 73 struct ttm_resource *mem); 74 75 /** 76 * struct ttm_resource_manager_func member free 77 * 78 * @man: Pointer to a memory type manager. 79 * @mem: Pointer to a struct ttm_resource to be filled in. 80 * 81 * This function frees memory type resources previously allocated 82 * and that are identified by @mem::mm_node and @mem::start. May not 83 * be called from within atomic context. 84 */ 85 void (*free)(struct ttm_resource_manager *man, 86 struct ttm_resource *mem); 87 88 /** 89 * struct ttm_resource_manager_func member debug 90 * 91 * @man: Pointer to a memory type manager. 92 * @printer: Prefix to be used in printout to identify the caller. 93 * 94 * This function is called to print out the state of the memory 95 * type manager to aid debugging of out-of-memory conditions. 96 * It may not be called from within atomic context. 97 */ 98 void (*debug)(struct ttm_resource_manager *man, 99 struct drm_printer *printer); 100 }; 101 102 /** 103 * struct ttm_resource_manager 104 * 105 * @use_type: The memory type is enabled. 106 * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory 107 * managed by this memory type. 108 * @gpu_offset: If used, the GPU offset of the first managed page of 109 * fixed memory or the first managed location in an aperture. 110 * @size: Size of the managed region. 111 * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX, 112 * as defined in ttm_placement_common.h 113 * @default_caching: The default caching policy used for a buffer object 114 * placed in this memory type if the user doesn't provide one. 115 * @func: structure pointer implementing the range manager. See above 116 * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures 117 * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions 118 * reserved by the TTM vm system. 119 * @io_reserve_lru: Optional lru list for unreserving io mem regions. 120 * @move_lock: lock for move fence 121 * static information. bdev::driver::io_mem_free is never used. 122 * @lru: The lru list for this memory type. 123 * @move: The fence of the last pipelined move operation. 124 * 125 * This structure is used to identify and manage memory types for a device. 126 */ 127 struct ttm_resource_manager { 128 /* 129 * No protection. Constant from start. 130 */ 131 bool use_type; 132 bool use_tt; 133 uint64_t size; 134 uint32_t available_caching; 135 uint32_t default_caching; 136 const struct ttm_resource_manager_func *func; 137 struct mutex io_reserve_mutex; 138 bool use_io_reserve_lru; 139 spinlock_t move_lock; 140 141 /* 142 * Protected by @io_reserve_mutex: 143 */ 144 145 struct list_head io_reserve_lru; 146 147 /* 148 * Protected by the global->lru_lock. 149 */ 150 151 struct list_head lru[TTM_MAX_BO_PRIORITY]; 152 153 /* 154 * Protected by @move_lock. 155 */ 156 struct dma_fence *move; 157 }; 158 159 /** 160 * struct ttm_bus_placement 161 * 162 * @addr: mapped virtual address 163 * @base: bus base address 164 * @is_iomem: is this io memory ? 165 * @offset: offset from the base address 166 * @io_reserved_vm: The VM system has a refcount in @io_reserved_count 167 * @io_reserved_count: Refcounting the numbers of callers to ttm_mem_io_reserve 168 * 169 * Structure indicating the bus placement of an object. 170 */ 171 struct ttm_bus_placement { 172 void *addr; 173 phys_addr_t base; 174 unsigned long offset; 175 bool is_iomem; 176 bool io_reserved_vm; 177 uint64_t io_reserved_count; 178 }; 179 180 /** 181 * struct ttm_resource 182 * 183 * @mm_node: Memory manager node. 184 * @size: Requested size of memory region. 185 * @num_pages: Actual size of memory region in pages. 186 * @page_alignment: Page alignment. 187 * @placement: Placement flags. 188 * @bus: Placement on io bus accessible to the CPU 189 * 190 * Structure indicating the placement and space resources used by a 191 * buffer object. 192 */ 193 struct ttm_resource { 194 void *mm_node; 195 unsigned long start; 196 unsigned long size; 197 unsigned long num_pages; 198 uint32_t page_alignment; 199 uint32_t mem_type; 200 uint32_t placement; 201 struct ttm_bus_placement bus; 202 }; 203 204 /** 205 * ttm_resource_manager_set_used 206 * 207 * @man: A memory manager object. 208 * @used: usage state to set. 209 * 210 * Set the manager in use flag. If disabled the manager is no longer 211 * used for object placement. 212 */ 213 static inline void 214 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used) 215 { 216 man->use_type = used; 217 } 218 219 /** 220 * ttm_resource_manager_used 221 * 222 * @man: Manager to get used state for 223 * 224 * Get the in use flag for a manager. 225 * Returns: 226 * true is used, false if not. 227 */ 228 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man) 229 { 230 return man->use_type; 231 } 232 233 /** 234 * ttm_resource_manager_cleanup 235 * 236 * @man: A memory manager object. 237 * 238 * Cleanup the move fences from the memory manager object. 239 */ 240 static inline void 241 ttm_resource_manager_cleanup(struct ttm_resource_manager *man) 242 { 243 dma_fence_put(man->move); 244 man->move = NULL; 245 } 246 247 int ttm_resource_alloc(struct ttm_buffer_object *bo, 248 const struct ttm_place *place, 249 struct ttm_resource *res); 250 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource *res); 251 252 void ttm_resource_manager_init(struct ttm_resource_manager *man, 253 unsigned long p_size); 254 255 int ttm_resource_manager_force_list_clean(struct ttm_bo_device *bdev, 256 struct ttm_resource_manager *man); 257 258 void ttm_resource_manager_debug(struct ttm_resource_manager *man, 259 struct drm_printer *p); 260 261 #endif 262