1 /* 2 * Copyright 2016 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 #include <drm/ttm/ttm_range_manager.h> 26 27 #include "amdgpu.h" 28 29 #define GART_ENTRY_WITHOUT_BO_COLOR 1 30 31 static inline struct amdgpu_gtt_mgr * 32 to_gtt_mgr(struct ttm_resource_manager *man) 33 { 34 return container_of(man, struct amdgpu_gtt_mgr, manager); 35 } 36 37 /** 38 * DOC: mem_info_gtt_total 39 * 40 * The amdgpu driver provides a sysfs API for reporting current total size of 41 * the GTT. 42 * The file mem_info_gtt_total is used for this, and returns the total size of 43 * the GTT block, in bytes 44 */ 45 static ssize_t amdgpu_mem_info_gtt_total_show(struct device *dev, 46 struct device_attribute *attr, 47 char *buf) 48 { 49 struct drm_device *ddev = dev_get_drvdata(dev); 50 struct amdgpu_device *adev = drm_to_adev(ddev); 51 struct ttm_resource_manager *man; 52 53 man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT); 54 return sysfs_emit(buf, "%llu\n", man->size); 55 } 56 57 /** 58 * DOC: mem_info_gtt_used 59 * 60 * The amdgpu driver provides a sysfs API for reporting current total amount of 61 * used GTT. 62 * The file mem_info_gtt_used is used for this, and returns the current used 63 * size of the GTT block, in bytes 64 */ 65 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev, 66 struct device_attribute *attr, 67 char *buf) 68 { 69 struct drm_device *ddev = dev_get_drvdata(dev); 70 struct amdgpu_device *adev = drm_to_adev(ddev); 71 struct ttm_resource_manager *man = &adev->mman.gtt_mgr.manager; 72 73 return sysfs_emit(buf, "%llu\n", ttm_resource_manager_usage(man)); 74 } 75 76 static DEVICE_ATTR(mem_info_gtt_total, S_IRUGO, 77 amdgpu_mem_info_gtt_total_show, NULL); 78 static DEVICE_ATTR(mem_info_gtt_used, S_IRUGO, 79 amdgpu_mem_info_gtt_used_show, NULL); 80 81 static struct attribute *amdgpu_gtt_mgr_attributes[] = { 82 &dev_attr_mem_info_gtt_total.attr, 83 &dev_attr_mem_info_gtt_used.attr, 84 NULL 85 }; 86 87 const struct attribute_group amdgpu_gtt_mgr_attr_group = { 88 .attrs = amdgpu_gtt_mgr_attributes 89 }; 90 91 /** 92 * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space 93 * 94 * @res: the mem object to check 95 * 96 * Check if a mem object has already address space allocated. 97 */ 98 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *res) 99 { 100 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); 101 102 return drm_mm_node_allocated(&node->mm_nodes[0]); 103 } 104 105 /** 106 * amdgpu_gtt_mgr_new - allocate a new node 107 * 108 * @man: TTM memory type manager 109 * @tbo: TTM BO we need this range for 110 * @place: placement flags and restrictions 111 * @res: the resulting mem object 112 * 113 * Dummy, allocate the node but no space for it yet. 114 */ 115 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man, 116 struct ttm_buffer_object *tbo, 117 const struct ttm_place *place, 118 struct ttm_resource **res) 119 { 120 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 121 uint32_t num_pages = PFN_UP(tbo->base.size); 122 struct ttm_range_mgr_node *node; 123 int r; 124 125 node = kzalloc_flex(*node, mm_nodes, 1); 126 if (!node) 127 return -ENOMEM; 128 129 ttm_resource_init(tbo, place, &node->base); 130 if (!(place->flags & TTM_PL_FLAG_TEMPORARY) && 131 ttm_resource_manager_usage(man) > man->size) { 132 r = -ENOSPC; 133 goto err_free; 134 } 135 136 if (place->lpfn) { 137 spin_lock(&mgr->lock); 138 r = drm_mm_insert_node_in_range(&mgr->mm, &node->mm_nodes[0], 139 num_pages, tbo->page_alignment, 140 0, place->fpfn, place->lpfn, 141 DRM_MM_INSERT_BEST); 142 spin_unlock(&mgr->lock); 143 if (unlikely(r)) 144 goto err_free; 145 146 node->base.start = node->mm_nodes[0].start; 147 } else { 148 node->mm_nodes[0].start = 0; 149 node->mm_nodes[0].size = PFN_UP(node->base.size); 150 node->base.start = AMDGPU_BO_INVALID_OFFSET; 151 } 152 153 *res = &node->base; 154 return 0; 155 156 err_free: 157 ttm_resource_fini(man, &node->base); 158 kfree(node); 159 return r; 160 } 161 162 /** 163 * amdgpu_gtt_mgr_del - free ranges 164 * 165 * @man: TTM memory type manager 166 * @res: TTM memory object 167 * 168 * Free the allocated GTT again. 169 */ 170 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man, 171 struct ttm_resource *res) 172 { 173 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res); 174 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 175 176 spin_lock(&mgr->lock); 177 if (drm_mm_node_allocated(&node->mm_nodes[0])) 178 drm_mm_remove_node(&node->mm_nodes[0]); 179 spin_unlock(&mgr->lock); 180 181 ttm_resource_fini(man, res); 182 kfree(node); 183 } 184 185 /** 186 * amdgpu_gtt_mgr_alloc_entries - alloc GART entries without GTT bo 187 * 188 * @mgr: The GTT manager object 189 * @mm_node: The drm mm node to return the new allocation node information 190 * @num_pages: The number of pages for the new allocation 191 * @mode: The new allocation mode 192 * 193 * Helper to dynamic alloc GART entries to map memory not accociated with 194 * GTT BO, for example VRAM BO physical memory, remote physical memory. 195 */ 196 int amdgpu_gtt_mgr_alloc_entries(struct amdgpu_gtt_mgr *mgr, 197 struct drm_mm_node *mm_node, 198 u64 num_pages, 199 enum drm_mm_insert_mode mode) 200 { 201 struct amdgpu_device *adev = container_of(mgr, typeof(*adev), mman.gtt_mgr); 202 u32 alignment = 0; 203 int r; 204 205 /* Align to TLB L2 cache entry size to work around "V bit HW bug" */ 206 if (adev->asic_type == CHIP_TAHITI) { 207 alignment = 32 * 1024 / AMDGPU_GPU_PAGE_SIZE; 208 num_pages = ALIGN(num_pages, alignment); 209 } 210 211 spin_lock(&mgr->lock); 212 r = drm_mm_insert_node_in_range(&mgr->mm, mm_node, num_pages, 213 alignment, GART_ENTRY_WITHOUT_BO_COLOR, 0, 214 adev->gmc.gart_size >> PAGE_SHIFT, 215 mode); 216 spin_unlock(&mgr->lock); 217 return r; 218 } 219 220 /** 221 * amdgpu_gtt_mgr_free_entries - free GART entries not accocaited with GTT bo 222 * 223 * @mgr: The GTT manager object 224 * @mm_node: The drm mm node to free 225 */ 226 void amdgpu_gtt_mgr_free_entries(struct amdgpu_gtt_mgr *mgr, 227 struct drm_mm_node *mm_node) 228 { 229 spin_lock(&mgr->lock); 230 if (drm_mm_node_allocated(mm_node)) 231 drm_mm_remove_node(mm_node); 232 spin_unlock(&mgr->lock); 233 } 234 235 /** 236 * amdgpu_gtt_mgr_recover - re-init gart 237 * 238 * @mgr: amdgpu_gtt_mgr pointer 239 * 240 * Re-init the gart for each known BO in the GTT. 241 */ 242 void amdgpu_gtt_mgr_recover(struct amdgpu_gtt_mgr *mgr) 243 { 244 struct ttm_range_mgr_node *node; 245 struct drm_mm_node *mm_node; 246 struct amdgpu_device *adev; 247 248 adev = container_of(mgr, typeof(*adev), mman.gtt_mgr); 249 spin_lock(&mgr->lock); 250 drm_mm_for_each_node(mm_node, &mgr->mm) { 251 if (mm_node->color == GART_ENTRY_WITHOUT_BO_COLOR) 252 continue; 253 254 node = container_of(mm_node, typeof(*node), mm_nodes[0]); 255 amdgpu_ttm_recover_gart(node->base.bo); 256 } 257 spin_unlock(&mgr->lock); 258 } 259 260 /** 261 * amdgpu_gtt_mgr_intersects - test for intersection 262 * 263 * @man: Our manager object 264 * @res: The resource to test 265 * @place: The place for the new allocation 266 * @size: The size of the new allocation 267 * 268 * Simplified intersection test, only interesting if we need GART or not. 269 */ 270 static bool amdgpu_gtt_mgr_intersects(struct ttm_resource_manager *man, 271 struct ttm_resource *res, 272 const struct ttm_place *place, 273 size_t size) 274 { 275 return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res); 276 } 277 278 /** 279 * amdgpu_gtt_mgr_compatible - test for compatibility 280 * 281 * @man: Our manager object 282 * @res: The resource to test 283 * @place: The place for the new allocation 284 * @size: The size of the new allocation 285 * 286 * Simplified compatibility test. 287 */ 288 static bool amdgpu_gtt_mgr_compatible(struct ttm_resource_manager *man, 289 struct ttm_resource *res, 290 const struct ttm_place *place, 291 size_t size) 292 { 293 return !place->lpfn || amdgpu_gtt_mgr_has_gart_addr(res); 294 } 295 296 /** 297 * amdgpu_gtt_mgr_debug - dump VRAM table 298 * 299 * @man: TTM memory type manager 300 * @printer: DRM printer to use 301 * 302 * Dump the table content using printk. 303 */ 304 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man, 305 struct drm_printer *printer) 306 { 307 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 308 309 spin_lock(&mgr->lock); 310 drm_mm_print(&mgr->mm, printer); 311 spin_unlock(&mgr->lock); 312 } 313 314 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = { 315 .alloc = amdgpu_gtt_mgr_new, 316 .free = amdgpu_gtt_mgr_del, 317 .intersects = amdgpu_gtt_mgr_intersects, 318 .compatible = amdgpu_gtt_mgr_compatible, 319 .debug = amdgpu_gtt_mgr_debug 320 }; 321 322 /** 323 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM 324 * 325 * @adev: amdgpu_device pointer 326 * @gtt_size: maximum size of GTT 327 * 328 * Allocate and initialize the GTT manager. 329 */ 330 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size) 331 { 332 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 333 struct ttm_resource_manager *man = &mgr->manager; 334 335 man->use_tt = true; 336 man->func = &amdgpu_gtt_mgr_func; 337 338 ttm_resource_manager_init(man, &adev->mman.bdev, gtt_size); 339 340 drm_mm_init(&mgr->mm, 0, adev->gmc.gart_size >> PAGE_SHIFT); 341 spin_lock_init(&mgr->lock); 342 343 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager); 344 ttm_resource_manager_set_used(man, true); 345 return 0; 346 } 347 348 /** 349 * amdgpu_gtt_mgr_fini - free and destroy GTT manager 350 * 351 * @adev: amdgpu_device pointer 352 * 353 * Destroy and free the GTT manager, returns -EBUSY if ranges are still 354 * allocated inside it. 355 */ 356 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev) 357 { 358 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 359 struct ttm_resource_manager *man = &mgr->manager; 360 int ret; 361 362 ttm_resource_manager_set_used(man, false); 363 364 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man); 365 if (ret) 366 return; 367 368 spin_lock(&mgr->lock); 369 drm_mm_takedown(&mgr->mm); 370 spin_unlock(&mgr->lock); 371 372 ttm_resource_manager_cleanup(man); 373 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL); 374 } 375