Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2020 Advanced Micro Devices, Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Christian König
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  * ttm_lru_bulk_move_init - initialize a bulk move structure
0034  * @bulk: the structure to init
0035  *
0036  * For now just memset the structure to zero.
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  * ttm_lru_bulk_move_tail - bulk move range of resources to the LRU tail.
0046  *
0047  * @bulk: bulk move structure
0048  *
0049  * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
0050  * resource order never changes. Should be called with &ttm_device.lru_lock held.
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 /* Return the bulk move pos object for this resource */
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 /* Move the resource to the tail of the bulk move range */
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 /* Add the resource to a bulk_move cursor */
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 /* Remove the resource from a bulk_move range */
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 /* Add the resource to a bulk move if the BO is configured for it */
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 /* Remove the resource from a bulk move if the BO is configured for it */
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 /* Move a resource to the LRU or bulk tail */
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  * ttm_resource_init - resource object constructure
0167  * @bo: buffer object this resources is allocated for
0168  * @place: placement of the resource
0169  * @res: the resource object to inistilize
0170  *
0171  * Initialize a new resource object. Counterpart of ttm_resource_fini().
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  * ttm_resource_fini - resource destructor
0202  * @man: the resource manager this resource belongs to
0203  * @res: the resource to clean up
0204  *
0205  * Should be used by resource manager backends to clean up the TTM resource
0206  * objects before freeing the underlying structure. Makes sure the resource is
0207  * removed from the LRU before destruction.
0208  * Counterpart of ttm_resource_init().
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  * ttm_resource_compat - check if resource is compatible with placement
0282  *
0283  * @res: the resource to check
0284  * @placement: the placement to check against
0285  *
0286  * Returns true if the placement is compatible.
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  * ttm_resource_manager_init
0315  *
0316  * @man: memory manager object to init
0317  * @bdev: ttm device this manager belongs to
0318  * @size: size of managed resources in arbitrary units
0319  *
0320  * Initialise core parts of a manager object.
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  * ttm_resource_manager_evict_all
0341  *
0342  * @bdev - device to use
0343  * @man - manager to use
0344  *
0345  * Evict all the objects out of a memory manager until it is empty.
0346  * Part of memory manager cleanup sequence.
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      * Can't use standard list traversal since we're unlocking.
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  * ttm_resource_manager_usage
0394  *
0395  * @man: A memory manager object.
0396  *
0397  * Return how many resources are currently used.
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  * ttm_resource_manager_debug
0412  *
0413  * @man: manager type to dump.
0414  * @p: printer to use for debug.
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  * ttm_resource_manager_first
0430  *
0431  * @man: resource manager to iterate over
0432  * @cursor: cursor to record the position
0433  *
0434  * Returns the first resource from the resource manager.
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  * ttm_resource_manager_next
0454  *
0455  * @man: resource manager to iterate over
0456  * @cursor: cursor to record the position
0457  * @res: the current resource pointer
0458  *
0459  * Returns the next resource from the resource manager.
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  * ttm_kmap_iter_iomap_init - Initialize a struct ttm_kmap_iter_iomap
0524  * @iter_io: The struct ttm_kmap_iter_iomap to initialize.
0525  * @iomap: The struct io_mapping representing the underlying linear io_memory.
0526  * @st: sg_table into @iomap, representing the memory of the struct
0527  * ttm_resource.
0528  * @start: Offset that needs to be subtracted from @st to make
0529  * sg_dma_address(st->sgl) - @start == 0 for @iomap start.
0530  *
0531  * Return: Pointer to the embedded struct ttm_kmap_iter.
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  * DOC: Linear io iterator
0551  *
0552  * This code should die in the not too near future. Best would be if we could
0553  * make io-mapping use memremap for all io memory, and have memremap
0554  * implement a kmap_local functionality. We could then strip a huge amount of
0555  * code. These linear io iterators are implemented to mimic old functionality,
0556  * and they don't use kmap_local semantics at all internally. Rather ioremap or
0557  * friends, and at least on 32-bit they add global TLB flushes and points
0558  * of failure.
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  * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory
0579  * @iter_io: The iterator to initialize
0580  * @bdev: The TTM device
0581  * @mem: The ttm resource representing the iomap.
0582  *
0583  * This function is for internal TTM use only. It sets up a memcpy kmap iterator
0584  * pointing at a linear chunk of io memory.
0585  *
0586  * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on
0587  * failure.
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         /* If uncached requested or if mapping cached or wc failed */
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  * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory
0646  * @iter_io: The iterator to initialize
0647  * @bdev: The TTM device
0648  * @mem: The ttm resource representing the iomap.
0649  *
0650  * This function is for internal TTM use only. It cleans up a memcpy kmap
0651  * iterator initialized by ttm_kmap_iter_linear_io_init.
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  * ttm_resource_manager_create_debugfs - Create debugfs entry for specified
0684  * resource manager.
0685  * @man: The TTM resource manager for which the debugfs stats file be creates
0686  * @parent: debugfs directory in which the file will reside
0687  * @name: The filename to create.
0688  *
0689  * This function setups up a debugfs file that can be used to look
0690  * at debug statistics of the specified ttm_resource_manager.
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);