/* * Copyright 2020 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * Authors: Christian König
*/
/* Detach the cursor from the bulk move list*/ staticvoid
ttm_resource_cursor_clear_bulk(struct ttm_resource_cursor *cursor)
{
lockdep_assert_held(&cursor->man->bdev->lru_lock);
/* Move the cursor to the end of the bulk move list it's in */ staticvoid ttm_resource_cursor_move_bulk_tail(struct ttm_lru_bulk_move *bulk, struct ttm_resource_cursor *cursor)
{ struct ttm_lru_bulk_move_pos *pos;
if (WARN_ON_ONCE(bulk != cursor->bulk)) {
list_del_init(&cursor->bulk_link); return;
}
pos = &bulk->pos[cursor->mem_type][cursor->priority]; if (pos->last)
list_move(&cursor->hitch.link, &pos->last->lru.link);
ttm_resource_cursor_clear_bulk(cursor);
}
/* Move all cursors attached to a bulk move to its end */ staticvoid ttm_bulk_move_adjust_cursors(struct ttm_lru_bulk_move *bulk)
{ struct ttm_resource_cursor *cursor, *next;
/* Remove a cursor from an empty bulk move list */ staticvoid ttm_bulk_move_drop_cursors(struct ttm_lru_bulk_move *bulk)
{ struct ttm_resource_cursor *cursor, *next;
/** * ttm_resource_cursor_init() - Initialize a struct ttm_resource_cursor * @cursor: The cursor to initialize. * @man: The resource manager. * * Initialize the cursor before using it for iteration.
*/ void ttm_resource_cursor_init(struct ttm_resource_cursor *cursor, struct ttm_resource_manager *man)
{
cursor->priority = 0;
cursor->man = man;
ttm_lru_item_init(&cursor->hitch, TTM_LRU_HITCH);
INIT_LIST_HEAD(&cursor->bulk_link);
INIT_LIST_HEAD(&cursor->hitch.link);
}
/** * ttm_resource_cursor_fini() - Finalize the LRU list cursor usage * @cursor: The struct ttm_resource_cursor to finalize. * * The function pulls the LRU list cursor off any lists it was previusly * attached to. Needs to be called with the LRU lock held. The function * can be called multiple times after eachother.
*/ void ttm_resource_cursor_fini(struct ttm_resource_cursor *cursor)
{
lockdep_assert_held(&cursor->man->bdev->lru_lock);
list_del_init(&cursor->hitch.link);
ttm_resource_cursor_clear_bulk(cursor);
}
/** * ttm_lru_bulk_move_init - initialize a bulk move structure * @bulk: the structure to init * * For now just memset the structure to zero.
*/ void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk)
{
memset(bulk, 0, sizeof(*bulk));
INIT_LIST_HEAD(&bulk->cursor_list);
}
EXPORT_SYMBOL(ttm_lru_bulk_move_init);
/** * ttm_lru_bulk_move_fini - finalize a bulk move structure * @bdev: The struct ttm_device * @bulk: the structure to finalize * * Sanity checks that bulk moves don't have any * resources left and hence no cursors attached.
*/ void ttm_lru_bulk_move_fini(struct ttm_device *bdev, struct ttm_lru_bulk_move *bulk)
{
spin_lock(&bdev->lru_lock);
ttm_bulk_move_drop_cursors(bulk);
spin_unlock(&bdev->lru_lock);
}
EXPORT_SYMBOL(ttm_lru_bulk_move_fini);
/** * ttm_lru_bulk_move_tail - bulk move range of resources to the LRU tail. * * @bulk: bulk move structure * * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that * resource order never changes. Should be called with &ttm_device.lru_lock held.
*/ void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk)
{ unsigned i, j;
ttm_bulk_move_adjust_cursors(bulk); for (i = 0; i < TTM_NUM_MEM_TYPES; ++i) { for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j) { struct ttm_lru_bulk_move_pos *pos = &bulk->pos[i][j]; struct ttm_resource_manager *man;
/* Return the bulk move pos object for this resource */ staticstruct ttm_lru_bulk_move_pos *
ttm_lru_bulk_move_pos(struct ttm_lru_bulk_move *bulk, struct ttm_resource *res)
{ return &bulk->pos[res->mem_type][res->bo->priority];
}
/* Return the previous resource on the list (skip over non-resource list items) */ staticstruct ttm_resource *ttm_lru_prev_res(struct ttm_resource *cur)
{ struct ttm_lru_item *lru = &cur->lru;
do {
lru = list_prev_entry(lru, link);
} while (!ttm_lru_item_is_res(lru));
return ttm_lru_item_to_res(lru);
}
/* Return the next resource on the list (skip over non-resource list items) */ staticstruct ttm_resource *ttm_lru_next_res(struct ttm_resource *cur)
{ struct ttm_lru_item *lru = &cur->lru;
do {
lru = list_next_entry(lru, link);
} while (!ttm_lru_item_is_res(lru));
return ttm_lru_item_to_res(lru);
}
/* Move the resource to the tail of the bulk move range */ staticvoid ttm_lru_bulk_move_pos_tail(struct ttm_lru_bulk_move_pos *pos, struct ttm_resource *res)
{ if (pos->last != res) { if (pos->first == res)
pos->first = ttm_lru_next_res(res);
list_move(&res->lru.link, &pos->last->lru.link);
pos->last = res;
}
}
/* Add the resource to a bulk_move cursor */ staticvoid ttm_lru_bulk_move_add(struct ttm_lru_bulk_move *bulk, struct ttm_resource *res)
{ struct ttm_lru_bulk_move_pos *pos = ttm_lru_bulk_move_pos(bulk, res);
staticbool ttm_resource_is_swapped(struct ttm_resource *res, struct ttm_buffer_object *bo)
{ /* * Take care when creating a new resource for a bo, that it is not considered * swapped if it's not the current resource for the bo and is thus logically * associated with the ttm_tt. Think a VRAM resource created to move a * swapped-out bo to VRAM.
*/ if (bo->resource != res || !bo->ttm) returnfalse;
/* Add the resource to a bulk move if the BO is configured for it */ void ttm_resource_add_bulk_move(struct ttm_resource *res, struct ttm_buffer_object *bo)
{ if (bo->bulk_move && !ttm_resource_unevictable(res, bo))
ttm_lru_bulk_move_add(bo->bulk_move, res);
}
/* Remove the resource from a bulk move if the BO is configured for it */ void ttm_resource_del_bulk_move(struct ttm_resource *res, struct ttm_buffer_object *bo)
{ if (bo->bulk_move && !ttm_resource_unevictable(res, bo))
ttm_lru_bulk_move_del(bo->bulk_move, res);
}
/* Move a resource to the LRU or bulk tail */ void ttm_resource_move_to_lru_tail(struct ttm_resource *res)
{ struct ttm_buffer_object *bo = res->bo; struct ttm_device *bdev = bo->bdev;
lockdep_assert_held(&bo->bdev->lru_lock);
if (ttm_resource_unevictable(res, bo)) {
list_move_tail(&res->lru.link, &bdev->unevictable);
man = ttm_manager_type(bo->bdev, place->mem_type);
spin_lock(&bo->bdev->lru_lock); if (ttm_resource_unevictable(res, bo))
list_add_tail(&res->lru.link, &bo->bdev->unevictable); else
list_add_tail(&res->lru.link, &man->lru[bo->priority]);
man->usage += res->size;
spin_unlock(&bo->bdev->lru_lock);
}
EXPORT_SYMBOL(ttm_resource_init);
/** * ttm_resource_fini - resource destructor * @man: the resource manager this resource belongs to * @res: the resource to clean up * * Should be used by resource manager backends to clean up the TTM resource * objects before freeing the underlying structure. Makes sure the resource is * removed from the LRU before destruction. * Counterpart of ttm_resource_init().
*/ void ttm_resource_fini(struct ttm_resource_manager *man, struct ttm_resource *res)
{ struct ttm_device *bdev = man->bdev;
pool = (*res)->css;
man = ttm_manager_type(bo->bdev, (*res)->mem_type);
man->func->free(man, *res);
*res = NULL; if (man->cg)
dmem_cgroup_uncharge(pool, bo->base.size);
}
EXPORT_SYMBOL(ttm_resource_free);
/** * ttm_resource_intersects - test for intersection * * @bdev: TTM device structure * @res: The resource to test * @place: The placement to test * @size: How many bytes the new allocation needs. * * Test if @res intersects with @place and @size. Used for testing if evictions * are valueable or not. * * Returns true if the res placement intersects with @place and @size.
*/ bool ttm_resource_intersects(struct ttm_device *bdev, struct ttm_resource *res, conststruct ttm_place *place,
size_t size)
{ struct ttm_resource_manager *man;
if (!res) returnfalse;
man = ttm_manager_type(bdev, res->mem_type); if (!place || !man->func->intersects) returntrue;
/** * ttm_resource_compatible - check if resource is compatible with placement * * @res: the resource to check * @placement: the placement to check against * @evicting: true if the caller is doing evictions * * Returns true if the placement is compatible.
*/ bool ttm_resource_compatible(struct ttm_resource *res, struct ttm_placement *placement, bool evicting)
{ struct ttm_buffer_object *bo = res->bo; struct ttm_device *bdev = bo->bdev; unsigned i;
if (res->placement & TTM_PL_FLAG_TEMPORARY) returnfalse;
for (i = 0; i < placement->num_placement; i++) { conststruct ttm_place *place = &placement->placement[i]; struct ttm_resource_manager *man;
if (res->mem_type != place->mem_type) continue;
if (place->flags & (evicting ? TTM_PL_FLAG_DESIRED :
TTM_PL_FLAG_FALLBACK)) continue;
if (place->flags & TTM_PL_FLAG_CONTIGUOUS &&
!(res->placement & TTM_PL_FLAG_CONTIGUOUS)) continue;
man = ttm_manager_type(bdev, res->mem_type); if (man->func->compatible &&
!man->func->compatible(man, res, place, bo->base.size)) continue;
for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
INIT_LIST_HEAD(&man->lru[i]);
man->move = NULL;
}
EXPORT_SYMBOL(ttm_resource_manager_init);
/* * ttm_resource_manager_evict_all * * @bdev - device to use * @man - manager to use * * Evict all the objects out of a memory manager until it is empty. * Part of memory manager cleanup sequence.
*/ int ttm_resource_manager_evict_all(struct ttm_device *bdev, struct ttm_resource_manager *man)
{ struct ttm_operation_ctx ctx = {
.interruptible = false,
.no_wait_gpu = false,
}; struct dma_fence *fence; int ret;
do {
ret = ttm_bo_evict_first(bdev, man, &ctx);
cond_resched();
} while (!ret);
/** * ttm_resource_manager_first() - Start iterating over the resources * of a resource manager * @cursor: cursor to record the position * * Initializes the cursor and starts iterating. When done iterating, * the caller must explicitly call ttm_resource_cursor_fini(). * * Return: The first resource from the resource manager.
*/ struct ttm_resource *
ttm_resource_manager_first(struct ttm_resource_cursor *cursor)
{ struct ttm_resource_manager *man = cursor->man;
/** * ttm_resource_manager_next() - Continue iterating over the resource manager * resources * @cursor: cursor to record the position * * Return: the next resource from the resource manager.
*/ struct ttm_resource *
ttm_resource_manager_next(struct ttm_resource_cursor *cursor)
{ struct ttm_resource_manager *man = cursor->man; struct ttm_lru_item *lru;
/** * ttm_lru_first_res_or_null() - Return the first resource on an lru list * @head: The list head of the lru list. * * Return: Pointer to the first resource on the lru list or NULL if * there is none.
*/ struct ttm_resource *ttm_lru_first_res_or_null(struct list_head *head)
{ struct ttm_lru_item *lru;
list_for_each_entry(lru, head, link) { if (ttm_lru_item_is_res(lru)) return ttm_lru_item_to_res(lru);
}
/** * DOC: Linear io iterator * * This code should die in the not too near future. Best would be if we could * make io-mapping use memremap for all io memory, and have memremap * implement a kmap_local functionality. We could then strip a huge amount of * code. These linear io iterators are implemented to mimic old functionality, * and they don't use kmap_local semantics at all internally. Rather ioremap or * friends, and at least on 32-bit they add global TLB flushes and points * of failure.
*/
/** * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory * @iter_io: The iterator to initialize * @bdev: The TTM device * @mem: The ttm resource representing the iomap. * * This function is for internal TTM use only. It sets up a memcpy kmap iterator * pointing at a linear chunk of io memory. * * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on * failure.
*/ struct ttm_kmap_iter *
ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io, struct ttm_device *bdev, struct ttm_resource *mem)
{ int ret;
ret = ttm_mem_io_reserve(bdev, mem); if (ret) goto out_err; if (!mem->bus.is_iomem) {
ret = -EINVAL; goto out_io_free;
}
/* If uncached requested or if mapping cached or wc failed */ if (iosys_map_is_null(&iter_io->dmap))
iosys_map_set_vaddr_iomem(&iter_io->dmap,
ioremap(mem->bus.offset,
mem->size));
if (iosys_map_is_null(&iter_io->dmap)) {
ret = -ENOMEM; goto out_io_free;
}
}
/** * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory * @iter_io: The iterator to initialize * @bdev: The TTM device * @mem: The ttm resource representing the iomap. * * This function is for internal TTM use only. It cleans up a memcpy kmap * iterator initialized by ttm_kmap_iter_linear_io_init.
*/ void
ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io, struct ttm_device *bdev, struct ttm_resource *mem)
{ if (iter_io->needs_unmap && iosys_map_is_set(&iter_io->dmap)) { if (iter_io->dmap.is_iomem)
iounmap(iter_io->dmap.vaddr_iomem); else
memunmap(iter_io->dmap.vaddr);
}
/** * ttm_resource_manager_create_debugfs - Create debugfs entry for specified * resource manager. * @man: The TTM resource manager for which the debugfs stats file be creates * @parent: debugfs directory in which the file will reside * @name: The filename to create. * * This function setups up a debugfs file that can be used to look * at debug statistics of the specified ttm_resource_manager.
*/ void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man, struct dentry * parent, constchar *name)
{ #ifdefined(CONFIG_DEBUG_FS)
debugfs_create_file(name, 0444, parent, man, &ttm_resource_manager_fops); #endif
}
EXPORT_SYMBOL(ttm_resource_manager_create_debugfs);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.