Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/radeon: Use the drm suballocation manager implementation.

Use the generic suballocation helper for radeon.

v3:
- Select the suballoc helper in Kconfig (Thomas).

Signed-off-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Co-developed-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Signed-off-by: Thomas Hellström <thomas.hellstrom@linux.intel.com>
Reviewed-by: Christian König <christian.koenig@amd.com>
Acked-by: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
Acked-by: Alex Deucher <alexdeucher@gmail.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20230224095152.30134-4-thomas.hellstrom@linux.intel.com

authored by

Maarten Lankhorst and committed by
Thomas Hellström
254986e3 c103a23f

+59 -358
+1
drivers/gpu/drm/radeon/Kconfig
··· 8 8 select DRM_DISPLAY_DP_HELPER 9 9 select DRM_DISPLAY_HELPER 10 10 select DRM_KMS_HELPER 11 + select DRM_SUBALLOC_HELPER 11 12 select DRM_TTM 12 13 select DRM_TTM_HELPER 13 14 select SND_HDA_COMPONENT if SND_HDA_CORE
+8 -47
drivers/gpu/drm/radeon/radeon.h
··· 79 79 80 80 #include <drm/drm_gem.h> 81 81 #include <drm/drm_audio_component.h> 82 + #include <drm/drm_suballoc.h> 82 83 83 84 #include "radeon_family.h" 84 85 #include "radeon_mode.h" ··· 512 511 }; 513 512 #define gem_to_radeon_bo(gobj) container_of((gobj), struct radeon_bo, tbo.base) 514 513 515 - /* sub-allocation manager, it has to be protected by another lock. 516 - * By conception this is an helper for other part of the driver 517 - * like the indirect buffer or semaphore, which both have their 518 - * locking. 519 - * 520 - * Principe is simple, we keep a list of sub allocation in offset 521 - * order (first entry has offset == 0, last entry has the highest 522 - * offset). 523 - * 524 - * When allocating new object we first check if there is room at 525 - * the end total_size - (last_object_offset + last_object_size) >= 526 - * alloc_size. If so we allocate new object there. 527 - * 528 - * When there is not enough room at the end, we start waiting for 529 - * each sub object until we reach object_offset+object_size >= 530 - * alloc_size, this object then become the sub object we return. 531 - * 532 - * Alignment can't be bigger than page size. 533 - * 534 - * Hole are not considered for allocation to keep things simple. 535 - * Assumption is that there won't be hole (all object on same 536 - * alignment). 537 - */ 538 514 struct radeon_sa_manager { 539 - wait_queue_head_t wq; 540 - struct radeon_bo *bo; 541 - struct list_head *hole; 542 - struct list_head flist[RADEON_NUM_RINGS]; 543 - struct list_head olist; 544 - unsigned size; 545 - uint64_t gpu_addr; 546 - void *cpu_ptr; 547 - uint32_t domain; 548 - uint32_t align; 549 - }; 550 - 551 - struct radeon_sa_bo; 552 - 553 - /* sub-allocation buffer */ 554 - struct radeon_sa_bo { 555 - struct list_head olist; 556 - struct list_head flist; 557 - struct radeon_sa_manager *manager; 558 - unsigned soffset; 559 - unsigned eoffset; 560 - struct radeon_fence *fence; 515 + struct drm_suballoc_manager base; 516 + struct radeon_bo *bo; 517 + uint64_t gpu_addr; 518 + void *cpu_ptr; 519 + u32 domain; 561 520 }; 562 521 563 522 /* ··· 548 587 * Semaphores. 549 588 */ 550 589 struct radeon_semaphore { 551 - struct radeon_sa_bo *sa_bo; 590 + struct drm_suballoc *sa_bo; 552 591 signed waiters; 553 592 uint64_t gpu_addr; 554 593 }; ··· 777 816 */ 778 817 779 818 struct radeon_ib { 780 - struct radeon_sa_bo *sa_bo; 819 + struct drm_suballoc *sa_bo; 781 820 uint32_t length_dw; 782 821 uint64_t gpu_addr; 783 822 uint32_t *ptr;
+5 -7
drivers/gpu/drm/radeon/radeon_ib.c
··· 61 61 { 62 62 int r; 63 63 64 - r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256); 64 + r = radeon_sa_bo_new(&rdev->ring_tmp_bo, &ib->sa_bo, size, 256); 65 65 if (r) { 66 66 dev_err(rdev->dev, "failed to get a new IB (%d)\n", r); 67 67 return r; ··· 77 77 /* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address 78 78 * space and soffset is the offset inside the pool bo 79 79 */ 80 - ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET; 80 + ib->gpu_addr = drm_suballoc_soffset(ib->sa_bo) + RADEON_VA_IB_OFFSET; 81 81 } else { 82 82 ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo); 83 83 } ··· 97 97 void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib) 98 98 { 99 99 radeon_sync_free(rdev, &ib->sync, ib->fence); 100 - radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence); 100 + radeon_sa_bo_free(&ib->sa_bo, ib->fence); 101 101 radeon_fence_unref(&ib->fence); 102 102 } 103 103 ··· 201 201 202 202 if (rdev->family >= CHIP_BONAIRE) { 203 203 r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo, 204 - RADEON_IB_POOL_SIZE*64*1024, 205 - RADEON_GPU_PAGE_SIZE, 204 + RADEON_IB_POOL_SIZE*64*1024, 256, 206 205 RADEON_GEM_DOMAIN_GTT, 207 206 RADEON_GEM_GTT_WC); 208 207 } else { ··· 209 210 * to the command stream checking 210 211 */ 211 212 r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo, 212 - RADEON_IB_POOL_SIZE*64*1024, 213 - RADEON_GPU_PAGE_SIZE, 213 + RADEON_IB_POOL_SIZE*64*1024, 256, 214 214 RADEON_GEM_DOMAIN_GTT, 0); 215 215 } 216 216 if (r) {
+16 -11
drivers/gpu/drm/radeon/radeon_object.h
··· 169 169 /* 170 170 * sub allocation 171 171 */ 172 - 173 - static inline uint64_t radeon_sa_bo_gpu_addr(struct radeon_sa_bo *sa_bo) 172 + static inline struct radeon_sa_manager * 173 + to_radeon_sa_manager(struct drm_suballoc_manager *manager) 174 174 { 175 - return sa_bo->manager->gpu_addr + sa_bo->soffset; 175 + return container_of(manager, struct radeon_sa_manager, base); 176 176 } 177 177 178 - static inline void * radeon_sa_bo_cpu_addr(struct radeon_sa_bo *sa_bo) 178 + static inline uint64_t radeon_sa_bo_gpu_addr(struct drm_suballoc *sa_bo) 179 179 { 180 - return sa_bo->manager->cpu_ptr + sa_bo->soffset; 180 + return to_radeon_sa_manager(sa_bo->manager)->gpu_addr + 181 + drm_suballoc_soffset(sa_bo); 182 + } 183 + 184 + static inline void *radeon_sa_bo_cpu_addr(struct drm_suballoc *sa_bo) 185 + { 186 + return to_radeon_sa_manager(sa_bo->manager)->cpu_ptr + 187 + drm_suballoc_soffset(sa_bo); 181 188 } 182 189 183 190 extern int radeon_sa_bo_manager_init(struct radeon_device *rdev, ··· 197 190 struct radeon_sa_manager *sa_manager); 198 191 extern int radeon_sa_bo_manager_suspend(struct radeon_device *rdev, 199 192 struct radeon_sa_manager *sa_manager); 200 - extern int radeon_sa_bo_new(struct radeon_device *rdev, 201 - struct radeon_sa_manager *sa_manager, 202 - struct radeon_sa_bo **sa_bo, 203 - unsigned size, unsigned align); 204 - extern void radeon_sa_bo_free(struct radeon_device *rdev, 205 - struct radeon_sa_bo **sa_bo, 193 + extern int radeon_sa_bo_new(struct radeon_sa_manager *sa_manager, 194 + struct drm_suballoc **sa_bo, 195 + unsigned int size, unsigned int align); 196 + extern void radeon_sa_bo_free(struct drm_suballoc **sa_bo, 206 197 struct radeon_fence *fence); 207 198 #if defined(CONFIG_DEBUG_FS) 208 199 extern void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager,
+27 -291
drivers/gpu/drm/radeon/radeon_sa.c
··· 44 44 45 45 #include "radeon.h" 46 46 47 - static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo); 48 - static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager); 49 - 50 47 int radeon_sa_bo_manager_init(struct radeon_device *rdev, 51 48 struct radeon_sa_manager *sa_manager, 52 - unsigned size, u32 align, u32 domain, u32 flags) 49 + unsigned int size, u32 sa_align, u32 domain, 50 + u32 flags) 53 51 { 54 - int i, r; 52 + int r; 55 53 56 - init_waitqueue_head(&sa_manager->wq); 57 - sa_manager->bo = NULL; 58 - sa_manager->size = size; 59 - sa_manager->domain = domain; 60 - sa_manager->align = align; 61 - sa_manager->hole = &sa_manager->olist; 62 - INIT_LIST_HEAD(&sa_manager->olist); 63 - for (i = 0; i < RADEON_NUM_RINGS; ++i) { 64 - INIT_LIST_HEAD(&sa_manager->flist[i]); 65 - } 66 - 67 - r = radeon_bo_create(rdev, size, align, true, 54 + r = radeon_bo_create(rdev, size, RADEON_GPU_PAGE_SIZE, true, 68 55 domain, flags, NULL, NULL, &sa_manager->bo); 69 56 if (r) { 70 57 dev_err(rdev->dev, "(%d) failed to allocate bo for manager\n", r); 71 58 return r; 72 59 } 60 + 61 + sa_manager->domain = domain; 62 + 63 + drm_suballoc_manager_init(&sa_manager->base, size, sa_align); 73 64 74 65 return r; 75 66 } ··· 68 77 void radeon_sa_bo_manager_fini(struct radeon_device *rdev, 69 78 struct radeon_sa_manager *sa_manager) 70 79 { 71 - struct radeon_sa_bo *sa_bo, *tmp; 72 - 73 - if (!list_empty(&sa_manager->olist)) { 74 - sa_manager->hole = &sa_manager->olist, 75 - radeon_sa_bo_try_free(sa_manager); 76 - if (!list_empty(&sa_manager->olist)) { 77 - dev_err(rdev->dev, "sa_manager is not empty, clearing anyway\n"); 78 - } 79 - } 80 - list_for_each_entry_safe(sa_bo, tmp, &sa_manager->olist, olist) { 81 - radeon_sa_bo_remove_locked(sa_bo); 82 - } 80 + drm_suballoc_manager_fini(&sa_manager->base); 83 81 radeon_bo_unref(&sa_manager->bo); 84 - sa_manager->size = 0; 85 82 } 86 83 87 84 int radeon_sa_bo_manager_start(struct radeon_device *rdev, ··· 118 139 return r; 119 140 } 120 141 121 - static void radeon_sa_bo_remove_locked(struct radeon_sa_bo *sa_bo) 142 + int radeon_sa_bo_new(struct radeon_sa_manager *sa_manager, 143 + struct drm_suballoc **sa_bo, 144 + unsigned int size, unsigned int align) 122 145 { 123 - struct radeon_sa_manager *sa_manager = sa_bo->manager; 124 - if (sa_manager->hole == &sa_bo->olist) { 125 - sa_manager->hole = sa_bo->olist.prev; 146 + struct drm_suballoc *sa = drm_suballoc_new(&sa_manager->base, size, 147 + GFP_KERNEL, true, align); 148 + 149 + if (IS_ERR(sa)) { 150 + *sa_bo = NULL; 151 + return PTR_ERR(sa); 126 152 } 127 - list_del_init(&sa_bo->olist); 128 - list_del_init(&sa_bo->flist); 129 - radeon_fence_unref(&sa_bo->fence); 130 - kfree(sa_bo); 131 - } 132 153 133 - static void radeon_sa_bo_try_free(struct radeon_sa_manager *sa_manager) 134 - { 135 - struct radeon_sa_bo *sa_bo, *tmp; 136 - 137 - if (sa_manager->hole->next == &sa_manager->olist) 138 - return; 139 - 140 - sa_bo = list_entry(sa_manager->hole->next, struct radeon_sa_bo, olist); 141 - list_for_each_entry_safe_from(sa_bo, tmp, &sa_manager->olist, olist) { 142 - if (sa_bo->fence == NULL || !radeon_fence_signaled(sa_bo->fence)) { 143 - return; 144 - } 145 - radeon_sa_bo_remove_locked(sa_bo); 146 - } 147 - } 148 - 149 - static inline unsigned radeon_sa_bo_hole_soffset(struct radeon_sa_manager *sa_manager) 150 - { 151 - struct list_head *hole = sa_manager->hole; 152 - 153 - if (hole != &sa_manager->olist) { 154 - return list_entry(hole, struct radeon_sa_bo, olist)->eoffset; 155 - } 154 + *sa_bo = sa; 156 155 return 0; 157 156 } 158 157 159 - static inline unsigned radeon_sa_bo_hole_eoffset(struct radeon_sa_manager *sa_manager) 160 - { 161 - struct list_head *hole = sa_manager->hole; 162 - 163 - if (hole->next != &sa_manager->olist) { 164 - return list_entry(hole->next, struct radeon_sa_bo, olist)->soffset; 165 - } 166 - return sa_manager->size; 167 - } 168 - 169 - static bool radeon_sa_bo_try_alloc(struct radeon_sa_manager *sa_manager, 170 - struct radeon_sa_bo *sa_bo, 171 - unsigned size, unsigned align) 172 - { 173 - unsigned soffset, eoffset, wasted; 174 - 175 - soffset = radeon_sa_bo_hole_soffset(sa_manager); 176 - eoffset = radeon_sa_bo_hole_eoffset(sa_manager); 177 - wasted = (align - (soffset % align)) % align; 178 - 179 - if ((eoffset - soffset) >= (size + wasted)) { 180 - soffset += wasted; 181 - 182 - sa_bo->manager = sa_manager; 183 - sa_bo->soffset = soffset; 184 - sa_bo->eoffset = soffset + size; 185 - list_add(&sa_bo->olist, sa_manager->hole); 186 - INIT_LIST_HEAD(&sa_bo->flist); 187 - sa_manager->hole = &sa_bo->olist; 188 - return true; 189 - } 190 - return false; 191 - } 192 - 193 - /** 194 - * radeon_sa_event - Check if we can stop waiting 195 - * 196 - * @sa_manager: pointer to the sa_manager 197 - * @size: number of bytes we want to allocate 198 - * @align: alignment we need to match 199 - * 200 - * Check if either there is a fence we can wait for or 201 - * enough free memory to satisfy the allocation directly 202 - */ 203 - static bool radeon_sa_event(struct radeon_sa_manager *sa_manager, 204 - unsigned size, unsigned align) 205 - { 206 - unsigned soffset, eoffset, wasted; 207 - int i; 208 - 209 - for (i = 0; i < RADEON_NUM_RINGS; ++i) { 210 - if (!list_empty(&sa_manager->flist[i])) { 211 - return true; 212 - } 213 - } 214 - 215 - soffset = radeon_sa_bo_hole_soffset(sa_manager); 216 - eoffset = radeon_sa_bo_hole_eoffset(sa_manager); 217 - wasted = (align - (soffset % align)) % align; 218 - 219 - if ((eoffset - soffset) >= (size + wasted)) { 220 - return true; 221 - } 222 - 223 - return false; 224 - } 225 - 226 - static bool radeon_sa_bo_next_hole(struct radeon_sa_manager *sa_manager, 227 - struct radeon_fence **fences, 228 - unsigned *tries) 229 - { 230 - struct radeon_sa_bo *best_bo = NULL; 231 - unsigned i, soffset, best, tmp; 232 - 233 - /* if hole points to the end of the buffer */ 234 - if (sa_manager->hole->next == &sa_manager->olist) { 235 - /* try again with its beginning */ 236 - sa_manager->hole = &sa_manager->olist; 237 - return true; 238 - } 239 - 240 - soffset = radeon_sa_bo_hole_soffset(sa_manager); 241 - /* to handle wrap around we add sa_manager->size */ 242 - best = sa_manager->size * 2; 243 - /* go over all fence list and try to find the closest sa_bo 244 - * of the current last 245 - */ 246 - for (i = 0; i < RADEON_NUM_RINGS; ++i) { 247 - struct radeon_sa_bo *sa_bo; 248 - 249 - fences[i] = NULL; 250 - 251 - if (list_empty(&sa_manager->flist[i])) { 252 - continue; 253 - } 254 - 255 - sa_bo = list_first_entry(&sa_manager->flist[i], 256 - struct radeon_sa_bo, flist); 257 - 258 - if (!radeon_fence_signaled(sa_bo->fence)) { 259 - fences[i] = sa_bo->fence; 260 - continue; 261 - } 262 - 263 - /* limit the number of tries each ring gets */ 264 - if (tries[i] > 2) { 265 - continue; 266 - } 267 - 268 - tmp = sa_bo->soffset; 269 - if (tmp < soffset) { 270 - /* wrap around, pretend it's after */ 271 - tmp += sa_manager->size; 272 - } 273 - tmp -= soffset; 274 - if (tmp < best) { 275 - /* this sa bo is the closest one */ 276 - best = tmp; 277 - best_bo = sa_bo; 278 - } 279 - } 280 - 281 - if (best_bo) { 282 - ++tries[best_bo->fence->ring]; 283 - sa_manager->hole = best_bo->olist.prev; 284 - 285 - /* we knew that this one is signaled, 286 - so it's save to remote it */ 287 - radeon_sa_bo_remove_locked(best_bo); 288 - return true; 289 - } 290 - return false; 291 - } 292 - 293 - int radeon_sa_bo_new(struct radeon_device *rdev, 294 - struct radeon_sa_manager *sa_manager, 295 - struct radeon_sa_bo **sa_bo, 296 - unsigned size, unsigned align) 297 - { 298 - struct radeon_fence *fences[RADEON_NUM_RINGS]; 299 - unsigned tries[RADEON_NUM_RINGS]; 300 - int i, r; 301 - 302 - BUG_ON(align > sa_manager->align); 303 - BUG_ON(size > sa_manager->size); 304 - 305 - *sa_bo = kmalloc(sizeof(struct radeon_sa_bo), GFP_KERNEL); 306 - if ((*sa_bo) == NULL) { 307 - return -ENOMEM; 308 - } 309 - (*sa_bo)->manager = sa_manager; 310 - (*sa_bo)->fence = NULL; 311 - INIT_LIST_HEAD(&(*sa_bo)->olist); 312 - INIT_LIST_HEAD(&(*sa_bo)->flist); 313 - 314 - spin_lock(&sa_manager->wq.lock); 315 - do { 316 - for (i = 0; i < RADEON_NUM_RINGS; ++i) 317 - tries[i] = 0; 318 - 319 - do { 320 - radeon_sa_bo_try_free(sa_manager); 321 - 322 - if (radeon_sa_bo_try_alloc(sa_manager, *sa_bo, 323 - size, align)) { 324 - spin_unlock(&sa_manager->wq.lock); 325 - return 0; 326 - } 327 - 328 - /* see if we can skip over some allocations */ 329 - } while (radeon_sa_bo_next_hole(sa_manager, fences, tries)); 330 - 331 - for (i = 0; i < RADEON_NUM_RINGS; ++i) 332 - radeon_fence_ref(fences[i]); 333 - 334 - spin_unlock(&sa_manager->wq.lock); 335 - r = radeon_fence_wait_any(rdev, fences, false); 336 - for (i = 0; i < RADEON_NUM_RINGS; ++i) 337 - radeon_fence_unref(&fences[i]); 338 - spin_lock(&sa_manager->wq.lock); 339 - /* if we have nothing to wait for block */ 340 - if (r == -ENOENT) { 341 - r = wait_event_interruptible_locked( 342 - sa_manager->wq, 343 - radeon_sa_event(sa_manager, size, align) 344 - ); 345 - } 346 - 347 - } while (!r); 348 - 349 - spin_unlock(&sa_manager->wq.lock); 350 - kfree(*sa_bo); 351 - *sa_bo = NULL; 352 - return r; 353 - } 354 - 355 - void radeon_sa_bo_free(struct radeon_device *rdev, struct radeon_sa_bo **sa_bo, 158 + void radeon_sa_bo_free(struct drm_suballoc **sa_bo, 356 159 struct radeon_fence *fence) 357 160 { 358 - struct radeon_sa_manager *sa_manager; 359 - 360 161 if (sa_bo == NULL || *sa_bo == NULL) { 361 162 return; 362 163 } 363 164 364 - sa_manager = (*sa_bo)->manager; 365 - spin_lock(&sa_manager->wq.lock); 366 - if (fence && !radeon_fence_signaled(fence)) { 367 - (*sa_bo)->fence = radeon_fence_ref(fence); 368 - list_add_tail(&(*sa_bo)->flist, 369 - &sa_manager->flist[fence->ring]); 370 - } else { 371 - radeon_sa_bo_remove_locked(*sa_bo); 372 - } 373 - wake_up_all_locked(&sa_manager->wq); 374 - spin_unlock(&sa_manager->wq.lock); 165 + if (fence) 166 + drm_suballoc_free(*sa_bo, &fence->base); 167 + else 168 + drm_suballoc_free(*sa_bo, NULL); 169 + 375 170 *sa_bo = NULL; 376 171 } 377 172 ··· 153 400 void radeon_sa_bo_dump_debug_info(struct radeon_sa_manager *sa_manager, 154 401 struct seq_file *m) 155 402 { 156 - struct radeon_sa_bo *i; 403 + struct drm_printer p = drm_seq_file_printer(m); 157 404 158 - spin_lock(&sa_manager->wq.lock); 159 - list_for_each_entry(i, &sa_manager->olist, olist) { 160 - uint64_t soffset = i->soffset + sa_manager->gpu_addr; 161 - uint64_t eoffset = i->eoffset + sa_manager->gpu_addr; 162 - if (&i->olist == sa_manager->hole) { 163 - seq_printf(m, ">"); 164 - } else { 165 - seq_printf(m, " "); 166 - } 167 - seq_printf(m, "[0x%010llx 0x%010llx] size %8lld", 168 - soffset, eoffset, eoffset - soffset); 169 - if (i->fence) { 170 - seq_printf(m, " protected by 0x%016llx on ring %d", 171 - i->fence->seq, i->fence->ring); 172 - } 173 - seq_printf(m, "\n"); 174 - } 175 - spin_unlock(&sa_manager->wq.lock); 405 + drm_suballoc_dump_debug_info(&sa_manager->base, &p, sa_manager->gpu_addr); 176 406 } 177 407 #endif
+2 -2
drivers/gpu/drm/radeon/radeon_semaphore.c
··· 40 40 if (*semaphore == NULL) { 41 41 return -ENOMEM; 42 42 } 43 - r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, 43 + r = radeon_sa_bo_new(&rdev->ring_tmp_bo, 44 44 &(*semaphore)->sa_bo, 8, 8); 45 45 if (r) { 46 46 kfree(*semaphore); ··· 100 100 dev_err(rdev->dev, "semaphore %p has more waiters than signalers," 101 101 " hardware lockup imminent!\n", *semaphore); 102 102 } 103 - radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence); 103 + radeon_sa_bo_free(&(*semaphore)->sa_bo, fence); 104 104 kfree(*semaphore); 105 105 *semaphore = NULL; 106 106 }