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 "amdgpu.h" 26 27 static inline struct amdgpu_gtt_mgr *to_gtt_mgr(struct ttm_resource_manager *man) 28 { 29 return container_of(man, struct amdgpu_gtt_mgr, manager); 30 } 31 32 struct amdgpu_gtt_node { 33 struct drm_mm_node node; 34 struct ttm_buffer_object *tbo; 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, char *buf) 47 { 48 struct drm_device *ddev = dev_get_drvdata(dev); 49 struct amdgpu_device *adev = drm_to_adev(ddev); 50 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT); 51 52 return sysfs_emit(buf, "%llu\n", man->size * PAGE_SIZE); 53 } 54 55 /** 56 * DOC: mem_info_gtt_used 57 * 58 * The amdgpu driver provides a sysfs API for reporting current total amount of 59 * used GTT. 60 * The file mem_info_gtt_used is used for this, and returns the current used 61 * size of the GTT block, in bytes 62 */ 63 static ssize_t amdgpu_mem_info_gtt_used_show(struct device *dev, 64 struct device_attribute *attr, char *buf) 65 { 66 struct drm_device *ddev = dev_get_drvdata(dev); 67 struct amdgpu_device *adev = drm_to_adev(ddev); 68 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT); 69 70 return sysfs_emit(buf, "%llu\n", amdgpu_gtt_mgr_usage(man)); 71 } 72 73 static DEVICE_ATTR(mem_info_gtt_total, S_IRUGO, 74 amdgpu_mem_info_gtt_total_show, NULL); 75 static DEVICE_ATTR(mem_info_gtt_used, S_IRUGO, 76 amdgpu_mem_info_gtt_used_show, NULL); 77 78 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func; 79 /** 80 * amdgpu_gtt_mgr_init - init GTT manager and DRM MM 81 * 82 * @adev: amdgpu_device pointer 83 * @gtt_size: maximum size of GTT 84 * 85 * Allocate and initialize the GTT manager. 86 */ 87 int amdgpu_gtt_mgr_init(struct amdgpu_device *adev, uint64_t gtt_size) 88 { 89 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 90 struct ttm_resource_manager *man = &mgr->manager; 91 uint64_t start, size; 92 int ret; 93 94 man->use_tt = true; 95 man->func = &amdgpu_gtt_mgr_func; 96 97 ttm_resource_manager_init(man, gtt_size >> PAGE_SHIFT); 98 99 start = AMDGPU_GTT_MAX_TRANSFER_SIZE * AMDGPU_GTT_NUM_TRANSFER_WINDOWS; 100 size = (adev->gmc.gart_size >> PAGE_SHIFT) - start; 101 drm_mm_init(&mgr->mm, start, size); 102 spin_lock_init(&mgr->lock); 103 atomic64_set(&mgr->available, gtt_size >> PAGE_SHIFT); 104 105 ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_total); 106 if (ret) { 107 DRM_ERROR("Failed to create device file mem_info_gtt_total\n"); 108 return ret; 109 } 110 ret = device_create_file(adev->dev, &dev_attr_mem_info_gtt_used); 111 if (ret) { 112 DRM_ERROR("Failed to create device file mem_info_gtt_used\n"); 113 return ret; 114 } 115 116 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, &mgr->manager); 117 ttm_resource_manager_set_used(man, true); 118 return 0; 119 } 120 121 /** 122 * amdgpu_gtt_mgr_fini - free and destroy GTT manager 123 * 124 * @adev: amdgpu_device pointer 125 * 126 * Destroy and free the GTT manager, returns -EBUSY if ranges are still 127 * allocated inside it. 128 */ 129 void amdgpu_gtt_mgr_fini(struct amdgpu_device *adev) 130 { 131 struct amdgpu_gtt_mgr *mgr = &adev->mman.gtt_mgr; 132 struct ttm_resource_manager *man = &mgr->manager; 133 int ret; 134 135 ttm_resource_manager_set_used(man, false); 136 137 ret = ttm_resource_manager_evict_all(&adev->mman.bdev, man); 138 if (ret) 139 return; 140 141 spin_lock(&mgr->lock); 142 drm_mm_takedown(&mgr->mm); 143 spin_unlock(&mgr->lock); 144 145 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_total); 146 device_remove_file(adev->dev, &dev_attr_mem_info_gtt_used); 147 148 ttm_resource_manager_cleanup(man); 149 ttm_set_driver_manager(&adev->mman.bdev, TTM_PL_TT, NULL); 150 } 151 152 /** 153 * amdgpu_gtt_mgr_has_gart_addr - Check if mem has address space 154 * 155 * @mem: the mem object to check 156 * 157 * Check if a mem object has already address space allocated. 158 */ 159 bool amdgpu_gtt_mgr_has_gart_addr(struct ttm_resource *mem) 160 { 161 return mem->mm_node != NULL; 162 } 163 164 /** 165 * amdgpu_gtt_mgr_new - allocate a new node 166 * 167 * @man: TTM memory type manager 168 * @tbo: TTM BO we need this range for 169 * @place: placement flags and restrictions 170 * @mem: the resulting mem object 171 * 172 * Dummy, allocate the node but no space for it yet. 173 */ 174 static int amdgpu_gtt_mgr_new(struct ttm_resource_manager *man, 175 struct ttm_buffer_object *tbo, 176 const struct ttm_place *place, 177 struct ttm_resource *mem) 178 { 179 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 180 struct amdgpu_gtt_node *node; 181 int r; 182 183 spin_lock(&mgr->lock); 184 if ((&tbo->mem == mem || tbo->mem.mem_type != TTM_PL_TT) && 185 atomic64_read(&mgr->available) < mem->num_pages) { 186 spin_unlock(&mgr->lock); 187 return -ENOSPC; 188 } 189 atomic64_sub(mem->num_pages, &mgr->available); 190 spin_unlock(&mgr->lock); 191 192 if (!place->lpfn) { 193 mem->mm_node = NULL; 194 mem->start = AMDGPU_BO_INVALID_OFFSET; 195 return 0; 196 } 197 198 node = kzalloc(sizeof(*node), GFP_KERNEL); 199 if (!node) { 200 r = -ENOMEM; 201 goto err_out; 202 } 203 204 node->tbo = tbo; 205 206 spin_lock(&mgr->lock); 207 r = drm_mm_insert_node_in_range(&mgr->mm, &node->node, mem->num_pages, 208 mem->page_alignment, 0, place->fpfn, 209 place->lpfn, DRM_MM_INSERT_BEST); 210 spin_unlock(&mgr->lock); 211 212 if (unlikely(r)) 213 goto err_free; 214 215 mem->mm_node = node; 216 mem->start = node->node.start; 217 218 return 0; 219 220 err_free: 221 kfree(node); 222 223 err_out: 224 atomic64_add(mem->num_pages, &mgr->available); 225 226 return r; 227 } 228 229 /** 230 * amdgpu_gtt_mgr_del - free ranges 231 * 232 * @man: TTM memory type manager 233 * @mem: TTM memory object 234 * 235 * Free the allocated GTT again. 236 */ 237 static void amdgpu_gtt_mgr_del(struct ttm_resource_manager *man, 238 struct ttm_resource *mem) 239 { 240 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 241 struct amdgpu_gtt_node *node = mem->mm_node; 242 243 if (node) { 244 spin_lock(&mgr->lock); 245 drm_mm_remove_node(&node->node); 246 spin_unlock(&mgr->lock); 247 kfree(node); 248 } 249 250 atomic64_add(mem->num_pages, &mgr->available); 251 } 252 253 /** 254 * amdgpu_gtt_mgr_usage - return usage of GTT domain 255 * 256 * @man: TTM memory type manager 257 * 258 * Return how many bytes are used in the GTT domain 259 */ 260 uint64_t amdgpu_gtt_mgr_usage(struct ttm_resource_manager *man) 261 { 262 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 263 s64 result = man->size - atomic64_read(&mgr->available); 264 265 return (result > 0 ? result : 0) * PAGE_SIZE; 266 } 267 268 int amdgpu_gtt_mgr_recover(struct ttm_resource_manager *man) 269 { 270 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 271 struct amdgpu_gtt_node *node; 272 struct drm_mm_node *mm_node; 273 int r = 0; 274 275 spin_lock(&mgr->lock); 276 drm_mm_for_each_node(mm_node, &mgr->mm) { 277 node = container_of(mm_node, struct amdgpu_gtt_node, node); 278 r = amdgpu_ttm_recover_gart(node->tbo); 279 if (r) 280 break; 281 } 282 spin_unlock(&mgr->lock); 283 284 return r; 285 } 286 287 /** 288 * amdgpu_gtt_mgr_debug - dump VRAM table 289 * 290 * @man: TTM memory type manager 291 * @printer: DRM printer to use 292 * 293 * Dump the table content using printk. 294 */ 295 static void amdgpu_gtt_mgr_debug(struct ttm_resource_manager *man, 296 struct drm_printer *printer) 297 { 298 struct amdgpu_gtt_mgr *mgr = to_gtt_mgr(man); 299 300 spin_lock(&mgr->lock); 301 drm_mm_print(&mgr->mm, printer); 302 spin_unlock(&mgr->lock); 303 304 drm_printf(printer, "man size:%llu pages, gtt available:%lld pages, usage:%lluMB\n", 305 man->size, (u64)atomic64_read(&mgr->available), 306 amdgpu_gtt_mgr_usage(man) >> 20); 307 } 308 309 static const struct ttm_resource_manager_func amdgpu_gtt_mgr_func = { 310 .alloc = amdgpu_gtt_mgr_new, 311 .free = amdgpu_gtt_mgr_del, 312 .debug = amdgpu_gtt_mgr_debug 313 }; 314