0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #include <linux/iosys-map.h>
0026 #include <linux/io-mapping.h>
0027 #include <linux/scatterlist.h>
0028
0029 #include <drm/ttm/ttm_resource.h>
0030 #include <drm/ttm/ttm_bo_driver.h>
0031
0032
0033
0034
0035
0036
0037
0038 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk)
0039 {
0040 memset(bulk, 0, sizeof(*bulk));
0041 }
0042 EXPORT_SYMBOL(ttm_lru_bulk_move_init);
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk)
0053 {
0054 unsigned i, j;
0055
0056 for (i = 0; i < TTM_NUM_MEM_TYPES; ++i) {
0057 for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) {
0058 struct ttm_lru_bulk_move_pos *pos = &bulk->pos[i][j];
0059 struct ttm_resource_manager *man;
0060
0061 if (!pos->first)
0062 continue;
0063
0064 lockdep_assert_held(&pos->first->bo->bdev->lru_lock);
0065 dma_resv_assert_held(pos->first->bo->base.resv);
0066 dma_resv_assert_held(pos->last->bo->base.resv);
0067
0068 man = ttm_manager_type(pos->first->bo->bdev, i);
0069 list_bulk_move_tail(&man->lru[j], &pos->first->lru,
0070 &pos->last->lru);
0071 }
0072 }
0073 }
0074 EXPORT_SYMBOL(ttm_lru_bulk_move_tail);
0075
0076
0077 static struct ttm_lru_bulk_move_pos *
0078 ttm_lru_bulk_move_pos(struct ttm_lru_bulk_move *bulk, struct ttm_resource *res)
0079 {
0080 return &bulk->pos[res->mem_type][res->bo->priority];
0081 }
0082
0083
0084 static void ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos,
0085 struct ttm_resource *res)
0086 {
0087 if (pos->last != res) {
0088 list_move(&res->lru, &pos->last->lru);
0089 pos->last = res;
0090 }
0091 }
0092
0093
0094 static void ttm_lru_bulk_move_add(struct ttm_lru_bulk_move *bulk,
0095 struct ttm_resource *res)
0096 {
0097 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
0098
0099 if (!pos->first) {
0100 pos->first = res;
0101 pos->last = res;
0102 } else {
0103 ttm_lru_bulk_move_pos_tail(pos, res);
0104 }
0105 }
0106
0107
0108 static void ttm_lru_bulk_move_del(struct ttm_lru_bulk_move *bulk,
0109 struct ttm_resource *res)
0110 {
0111 struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
0112
0113 if (unlikely(pos->first == res && pos->last == res)) {
0114 pos->first = NULL;
0115 pos->last = NULL;
0116 } else if (pos->first == res) {
0117 pos->first = list_next_entry(res, lru);
0118 } else if (pos->last == res) {
0119 pos->last = list_prev_entry(res, lru);
0120 } else {
0121 list_move(&res->lru, &pos->last->lru);
0122 }
0123 }
0124
0125
0126 void ttm_resource_add_bulk_move(struct ttm_resource *res,
0127 struct ttm_buffer_object *bo)
0128 {
0129 if (bo->bulk_move && !bo->pin_count)
0130 ttm_lru_bulk_move_add(bo->bulk_move, res);
0131 }
0132
0133
0134 void ttm_resource_del_bulk_move(struct ttm_resource *res,
0135 struct ttm_buffer_object *bo)
0136 {
0137 if (bo->bulk_move && !bo->pin_count)
0138 ttm_lru_bulk_move_del(bo->bulk_move, res);
0139 }
0140
0141
0142 void ttm_resource_move_to_lru_tail(struct ttm_resource *res)
0143 {
0144 struct ttm_buffer_object *bo = res->bo;
0145 struct ttm_device *bdev = bo->bdev;
0146
0147 lockdep_assert_held(&bo->bdev->lru_lock);
0148
0149 if (bo->pin_count) {
0150 list_move_tail(&res->lru, &bdev->pinned);
0151
0152 } else if (bo->bulk_move) {
0153 struct ttm_lru_bulk_move_pos *pos =
0154 ttm_lru_bulk_move_pos(bo->bulk_move, res);
0155
0156 ttm_lru_bulk_move_pos_tail(pos, res);
0157 } else {
0158 struct ttm_resource_manager *man;
0159
0160 man = ttm_manager_type(bdev, res->mem_type);
0161 list_move_tail(&res->lru, &man->lru[bo->priority]);
0162 }
0163 }
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 void ttm_resource_init(struct ttm_buffer_object *bo,
0174 const struct ttm_place *place,
0175 struct ttm_resource *res)
0176 {
0177 struct ttm_resource_manager *man;
0178
0179 res->start = 0;
0180 res->num_pages = PFN_UP(bo->base.size);
0181 res->mem_type = place->mem_type;
0182 res->placement = place->flags;
0183 res->bus.addr = NULL;
0184 res->bus.offset = 0;
0185 res->bus.is_iomem = false;
0186 res->bus.caching = ttm_cached;
0187 res->bo = bo;
0188
0189 man = ttm_manager_type(bo->bdev, place->mem_type);
0190 spin_lock(&bo->bdev->lru_lock);
0191 if (bo->pin_count)
0192 list_add_tail(&res->lru, &bo->bdev->pinned);
0193 else
0194 list_add_tail(&res->lru, &man->lru[bo->priority]);
0195 man->usage += res->num_pages << PAGE_SHIFT;
0196 spin_unlock(&bo->bdev->lru_lock);
0197 }
0198 EXPORT_SYMBOL(ttm_resource_init);
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210 void ttm_resource_fini(struct ttm_resource_manager *man,
0211 struct ttm_resource *res)
0212 {
0213 struct ttm_device *bdev = man->bdev;
0214
0215 spin_lock(&bdev->lru_lock);
0216 list_del_init(&res->lru);
0217 man->usage -= res->num_pages << PAGE_SHIFT;
0218 spin_unlock(&bdev->lru_lock);
0219 }
0220 EXPORT_SYMBOL(ttm_resource_fini);
0221
0222 int ttm_resource_alloc(struct ttm_buffer_object *bo,
0223 const struct ttm_place *place,
0224 struct ttm_resource **res_ptr)
0225 {
0226 struct ttm_resource_manager *man =
0227 ttm_manager_type(bo->bdev, place->mem_type);
0228 int ret;
0229
0230 ret = man->func->alloc(man, bo, place, res_ptr);
0231 if (ret)
0232 return ret;
0233
0234 spin_lock(&bo->bdev->lru_lock);
0235 ttm_resource_add_bulk_move(*res_ptr, bo);
0236 spin_unlock(&bo->bdev->lru_lock);
0237 return 0;
0238 }
0239
0240 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res)
0241 {
0242 struct ttm_resource_manager *man;
0243
0244 if (!*res)
0245 return;
0246
0247 spin_lock(&bo->bdev->lru_lock);
0248 ttm_resource_del_bulk_move(*res, bo);
0249 spin_unlock(&bo->bdev->lru_lock);
0250 man = ttm_manager_type(bo->bdev, (*res)->mem_type);
0251 man->func->free(man, *res);
0252 *res = NULL;
0253 }
0254 EXPORT_SYMBOL(ttm_resource_free);
0255
0256 static bool ttm_resource_places_compat(struct ttm_resource *res,
0257 const struct ttm_place *places,
0258 unsigned num_placement)
0259 {
0260 unsigned i;
0261
0262 if (res->placement & TTM_PL_FLAG_TEMPORARY)
0263 return false;
0264
0265 for (i = 0; i < num_placement; i++) {
0266 const struct ttm_place *heap = &places[i];
0267
0268 if (res->start < heap->fpfn || (heap->lpfn &&
0269 (res->start + res->num_pages) > heap->lpfn))
0270 continue;
0271
0272 if ((res->mem_type == heap->mem_type) &&
0273 (!(heap->flags & TTM_PL_FLAG_CONTIGUOUS) ||
0274 (res->placement & TTM_PL_FLAG_CONTIGUOUS)))
0275 return true;
0276 }
0277 return false;
0278 }
0279
0280
0281
0282
0283
0284
0285
0286
0287
0288 bool ttm_resource_compat(struct ttm_resource *res,
0289 struct ttm_placement *placement)
0290 {
0291 if (ttm_resource_places_compat(res, placement->placement,
0292 placement->num_placement))
0293 return true;
0294
0295 if ((placement->busy_placement != placement->placement ||
0296 placement->num_busy_placement > placement->num_placement) &&
0297 ttm_resource_places_compat(res, placement->busy_placement,
0298 placement->num_busy_placement))
0299 return true;
0300
0301 return false;
0302 }
0303 EXPORT_SYMBOL(ttm_resource_compat);
0304
0305 void ttm_resource_set_bo(struct ttm_resource *res,
0306 struct ttm_buffer_object *bo)
0307 {
0308 spin_lock(&bo->bdev->lru_lock);
0309 res->bo = bo;
0310 spin_unlock(&bo->bdev->lru_lock);
0311 }
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 void ttm_resource_manager_init(struct ttm_resource_manager *man,
0323 struct ttm_device *bdev,
0324 uint64_t size)
0325 {
0326 unsigned i;
0327
0328 spin_lock_init(&man->move_lock);
0329 man->bdev = bdev;
0330 man->size = size;
0331 man->usage = 0;
0332
0333 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
0334 INIT_LIST_HEAD(&man->lru[i]);
0335 man->move = NULL;
0336 }
0337 EXPORT_SYMBOL(ttm_resource_manager_init);
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
0349 struct ttm_resource_manager *man)
0350 {
0351 struct ttm_operation_ctx ctx = {
0352 .interruptible = false,
0353 .no_wait_gpu = false,
0354 .force_alloc = true
0355 };
0356 struct dma_fence *fence;
0357 int ret;
0358 unsigned i;
0359
0360
0361
0362
0363
0364 spin_lock(&bdev->lru_lock);
0365 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
0366 while (!list_empty(&man->lru[i])) {
0367 spin_unlock(&bdev->lru_lock);
0368 ret = ttm_mem_evict_first(bdev, man, NULL, &ctx,
0369 NULL);
0370 if (ret)
0371 return ret;
0372 spin_lock(&bdev->lru_lock);
0373 }
0374 }
0375 spin_unlock(&bdev->lru_lock);
0376
0377 spin_lock(&man->move_lock);
0378 fence = dma_fence_get(man->move);
0379 spin_unlock(&man->move_lock);
0380
0381 if (fence) {
0382 ret = dma_fence_wait(fence, false);
0383 dma_fence_put(fence);
0384 if (ret)
0385 return ret;
0386 }
0387
0388 return 0;
0389 }
0390 EXPORT_SYMBOL(ttm_resource_manager_evict_all);
0391
0392
0393
0394
0395
0396
0397
0398
0399 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man)
0400 {
0401 uint64_t usage;
0402
0403 spin_lock(&man->bdev->lru_lock);
0404 usage = man->usage;
0405 spin_unlock(&man->bdev->lru_lock);
0406 return usage;
0407 }
0408 EXPORT_SYMBOL(ttm_resource_manager_usage);
0409
0410
0411
0412
0413
0414
0415
0416 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
0417 struct drm_printer *p)
0418 {
0419 drm_printf(p, " use_type: %d\n", man->use_type);
0420 drm_printf(p, " use_tt: %d\n", man->use_tt);
0421 drm_printf(p, " size: %llu\n", man->size);
0422 drm_printf(p, " usage: %llu\n", ttm_resource_manager_usage(man));
0423 if (man->func->debug)
0424 man->func->debug(man, p);
0425 }
0426 EXPORT_SYMBOL(ttm_resource_manager_debug);
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436 struct ttm_resource *
0437 ttm_resource_manager_first(struct ttm_resource_manager *man,
0438 struct ttm_resource_cursor *cursor)
0439 {
0440 struct ttm_resource *res;
0441
0442 lockdep_assert_held(&man->bdev->lru_lock);
0443
0444 for (cursor->priority = 0; cursor->priority < TTM_MAX_BO_PRIORITY;
0445 ++cursor->priority)
0446 list_for_each_entry(res, &man->lru[cursor->priority], lru)
0447 return res;
0448
0449 return NULL;
0450 }
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461 struct ttm_resource *
0462 ttm_resource_manager_next(struct ttm_resource_manager *man,
0463 struct ttm_resource_cursor *cursor,
0464 struct ttm_resource *res)
0465 {
0466 lockdep_assert_held(&man->bdev->lru_lock);
0467
0468 list_for_each_entry_continue(res, &man->lru[cursor->priority], lru)
0469 return res;
0470
0471 for (++cursor->priority; cursor->priority < TTM_MAX_BO_PRIORITY;
0472 ++cursor->priority)
0473 list_for_each_entry(res, &man->lru[cursor->priority], lru)
0474 return res;
0475
0476 return NULL;
0477 }
0478
0479 static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter,
0480 struct iosys_map *dmap,
0481 pgoff_t i)
0482 {
0483 struct ttm_kmap_iter_iomap *iter_io =
0484 container_of(iter, typeof(*iter_io), base);
0485 void __iomem *addr;
0486
0487 retry:
0488 while (i >= iter_io->cache.end) {
0489 iter_io->cache.sg = iter_io->cache.sg ?
0490 sg_next(iter_io->cache.sg) : iter_io->st->sgl;
0491 iter_io->cache.i = iter_io->cache.end;
0492 iter_io->cache.end += sg_dma_len(iter_io->cache.sg) >>
0493 PAGE_SHIFT;
0494 iter_io->cache.offs = sg_dma_address(iter_io->cache.sg) -
0495 iter_io->start;
0496 }
0497
0498 if (i < iter_io->cache.i) {
0499 iter_io->cache.end = 0;
0500 iter_io->cache.sg = NULL;
0501 goto retry;
0502 }
0503
0504 addr = io_mapping_map_local_wc(iter_io->iomap, iter_io->cache.offs +
0505 (((resource_size_t)i - iter_io->cache.i)
0506 << PAGE_SHIFT));
0507 iosys_map_set_vaddr_iomem(dmap, addr);
0508 }
0509
0510 static void ttm_kmap_iter_iomap_unmap_local(struct ttm_kmap_iter *iter,
0511 struct iosys_map *map)
0512 {
0513 io_mapping_unmap_local(map->vaddr_iomem);
0514 }
0515
0516 static const struct ttm_kmap_iter_ops ttm_kmap_iter_io_ops = {
0517 .map_local = ttm_kmap_iter_iomap_map_local,
0518 .unmap_local = ttm_kmap_iter_iomap_unmap_local,
0519 .maps_tt = false,
0520 };
0521
0522
0523
0524
0525
0526
0527
0528
0529
0530
0531
0532
0533 struct ttm_kmap_iter *
0534 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
0535 struct io_mapping *iomap,
0536 struct sg_table *st,
0537 resource_size_t start)
0538 {
0539 iter_io->base.ops = &ttm_kmap_iter_io_ops;
0540 iter_io->iomap = iomap;
0541 iter_io->st = st;
0542 iter_io->start = start;
0543 memset(&iter_io->cache, 0, sizeof(iter_io->cache));
0544
0545 return &iter_io->base;
0546 }
0547 EXPORT_SYMBOL(ttm_kmap_iter_iomap_init);
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561 static void ttm_kmap_iter_linear_io_map_local(struct ttm_kmap_iter *iter,
0562 struct iosys_map *dmap,
0563 pgoff_t i)
0564 {
0565 struct ttm_kmap_iter_linear_io *iter_io =
0566 container_of(iter, typeof(*iter_io), base);
0567
0568 *dmap = iter_io->dmap;
0569 iosys_map_incr(dmap, i * PAGE_SIZE);
0570 }
0571
0572 static const struct ttm_kmap_iter_ops ttm_kmap_iter_linear_io_ops = {
0573 .map_local = ttm_kmap_iter_linear_io_map_local,
0574 .maps_tt = false,
0575 };
0576
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589 struct ttm_kmap_iter *
0590 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
0591 struct ttm_device *bdev,
0592 struct ttm_resource *mem)
0593 {
0594 int ret;
0595
0596 ret = ttm_mem_io_reserve(bdev, mem);
0597 if (ret)
0598 goto out_err;
0599 if (!mem->bus.is_iomem) {
0600 ret = -EINVAL;
0601 goto out_io_free;
0602 }
0603
0604 if (mem->bus.addr) {
0605 iosys_map_set_vaddr(&iter_io->dmap, mem->bus.addr);
0606 iter_io->needs_unmap = false;
0607 } else {
0608 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
0609
0610 iter_io->needs_unmap = true;
0611 memset(&iter_io->dmap, 0, sizeof(iter_io->dmap));
0612 if (mem->bus.caching == ttm_write_combined)
0613 iosys_map_set_vaddr_iomem(&iter_io->dmap,
0614 ioremap_wc(mem->bus.offset,
0615 bus_size));
0616 else if (mem->bus.caching == ttm_cached)
0617 iosys_map_set_vaddr(&iter_io->dmap,
0618 memremap(mem->bus.offset, bus_size,
0619 MEMREMAP_WB |
0620 MEMREMAP_WT |
0621 MEMREMAP_WC));
0622
0623
0624 if (iosys_map_is_null(&iter_io->dmap))
0625 iosys_map_set_vaddr_iomem(&iter_io->dmap,
0626 ioremap(mem->bus.offset,
0627 bus_size));
0628
0629 if (iosys_map_is_null(&iter_io->dmap)) {
0630 ret = -ENOMEM;
0631 goto out_io_free;
0632 }
0633 }
0634
0635 iter_io->base.ops = &ttm_kmap_iter_linear_io_ops;
0636 return &iter_io->base;
0637
0638 out_io_free:
0639 ttm_mem_io_free(bdev, mem);
0640 out_err:
0641 return ERR_PTR(ret);
0642 }
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653 void
0654 ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
0655 struct ttm_device *bdev,
0656 struct ttm_resource *mem)
0657 {
0658 if (iter_io->needs_unmap && iosys_map_is_set(&iter_io->dmap)) {
0659 if (iter_io->dmap.is_iomem)
0660 iounmap(iter_io->dmap.vaddr_iomem);
0661 else
0662 memunmap(iter_io->dmap.vaddr);
0663 }
0664
0665 ttm_mem_io_free(bdev, mem);
0666 }
0667
0668 #if defined(CONFIG_DEBUG_FS)
0669
0670 static int ttm_resource_manager_show(struct seq_file *m, void *unused)
0671 {
0672 struct ttm_resource_manager *man =
0673 (struct ttm_resource_manager *)m->private;
0674 struct drm_printer p = drm_seq_file_printer(m);
0675 ttm_resource_manager_debug(man, &p);
0676 return 0;
0677 }
0678 DEFINE_SHOW_ATTRIBUTE(ttm_resource_manager);
0679
0680 #endif
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
0693 struct dentry * parent,
0694 const char *name)
0695 {
0696 #if defined(CONFIG_DEBUG_FS)
0697 debugfs_create_file(name, 0444, parent, man, &ttm_resource_manager_fops);
0698 #endif
0699 }
0700 EXPORT_SYMBOL(ttm_resource_manager_create_debugfs);