2018-05-02 15:46:21 +02:00
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2009-06-10 15:20:19 +02:00
/**************************************************************************
*
* Copyright ( c ) 2006 - 2009 VMware , Inc . , Palo Alto , CA . , USA
* All Rights Reserved .
*
* 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 , sub license , 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 ( including the
* next paragraph ) 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 NON - INFRINGEMENT . IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS , AUTHORS AND / OR ITS SUPPLIERS 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 : Thomas Hellstrom < thellstrom - at - vmware - dot - com >
*/
2012-03-16 21:43:50 -07:00
# define pr_fmt(fmt) "[TTM] " fmt
2012-10-02 18:01:07 +01:00
# include <drm/ttm/ttm_bo_driver.h>
# include <drm/ttm/ttm_placement.h>
2009-06-10 15:20:19 +02:00
# include <linux/jiffies.h>
# include <linux/slab.h>
# include <linux/sched.h>
# include <linux/mm.h>
# include <linux/file.h>
# include <linux/module.h>
2011-07-26 16:09:06 -07:00
# include <linux/atomic.h>
2019-08-11 10:06:32 +02:00
# include <linux/dma-resv.h>
2009-06-10 15:20:19 +02:00
2020-11-17 15:44:07 +01:00
# include "ttm_module.h"
2009-12-09 21:55:10 +01:00
static void ttm_bo_mem_space_debug ( struct ttm_buffer_object * bo ,
struct ttm_placement * placement )
{
2018-11-30 18:15:22 +01:00
struct drm_printer p = drm_debug_printer ( TTM_PFX ) ;
2020-08-04 12:56:31 +10:00
struct ttm_resource_manager * man ;
2020-09-10 13:39:41 +02:00
int i , mem_type ;
2009-12-09 21:55:10 +01:00
for ( i = 0 ; i < placement - > num_placement ; i + + ) {
2020-09-10 13:39:41 +02:00
mem_type = placement - > placement [ i ] . mem_type ;
2018-11-30 18:15:22 +01:00
drm_printf ( & p , " placement[%d]=0x%08X (%d) \n " ,
i , placement - > placement [ i ] . flags , mem_type ) ;
2020-08-04 12:56:09 +10:00
man = ttm_manager_type ( bo - > bdev , mem_type ) ;
2020-08-04 12:56:31 +10:00
ttm_resource_manager_debug ( man , & p ) ;
2009-12-09 21:55:10 +01:00
}
}
2022-01-24 11:07:15 +01:00
/**
* ttm_bo_move_to_lru_tail
*
* @ bo : The buffer object .
*
* Move this BO to the tail of all lru lists used to lookup and reserve an
* object . This function must be called with struct ttm_global : : lru_lock
* held , and is used to make a BO less likely to be considered for eviction .
*/
void ttm_bo_move_to_lru_tail ( struct ttm_buffer_object * bo )
2016-01-11 15:35:20 +01:00
{
2021-07-16 14:00:10 +02:00
dma_resv_assert_held ( bo - > base . resv ) ;
2018-08-06 17:05:30 +08:00
2021-07-16 14:00:10 +02:00
if ( bo - > resource )
2022-01-24 11:07:15 +01:00
ttm_resource_move_to_lru_tail ( bo - > resource ) ;
2016-01-11 15:35:20 +01:00
}
EXPORT_SYMBOL ( ttm_bo_move_to_lru_tail ) ;
2022-01-24 11:07:15 +01:00
/**
* ttm_bo_set_bulk_move - update BOs bulk move object
*
* @ bo : The buffer object .
*
* Update the BOs bulk move object , making sure that resources are added / removed
* as well . A bulk move allows to move many resource on the LRU at once ,
* resulting in much less overhead of maintaining the LRU .
* The only requirement is that the resources stay together on the LRU and are
* never separated . This is enforces by setting the bulk_move structure on a BO .
* ttm_lru_bulk_move_tail ( ) should be used to move all resources to the tail of
* their LRU list .
*/
void ttm_bo_set_bulk_move ( struct ttm_buffer_object * bo ,
struct ttm_lru_bulk_move * bulk )
{
dma_resv_assert_held ( bo - > base . resv ) ;
if ( bo - > bulk_move = = bulk )
return ;
spin_lock ( & bo - > bdev - > lru_lock ) ;
2022-06-13 09:37:03 +02:00
if ( bo - > resource )
ttm_resource_del_bulk_move ( bo - > resource , bo ) ;
2022-01-24 11:07:15 +01:00
bo - > bulk_move = bulk ;
2022-06-13 09:37:03 +02:00
if ( bo - > resource )
ttm_resource_add_bulk_move ( bo - > resource , bo ) ;
2022-01-24 11:07:15 +01:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
}
EXPORT_SYMBOL ( ttm_bo_set_bulk_move ) ;
2009-06-10 15:20:19 +02:00
static int ttm_bo_handle_move_mem ( struct ttm_buffer_object * bo ,
2020-08-04 12:56:32 +10:00
struct ttm_resource * mem , bool evict ,
2020-10-29 13:58:52 +10:00
struct ttm_operation_ctx * ctx ,
struct ttm_place * hop )
2009-06-10 15:20:19 +02:00
{
2020-10-01 14:51:40 +02:00
struct ttm_device * bdev = bo - > bdev ;
2022-02-17 10:43:16 +01:00
bool old_use_tt , new_use_tt ;
2020-07-15 14:52:05 +02:00
int ret ;
2009-06-10 15:20:19 +02:00
2022-02-17 10:43:16 +01:00
old_use_tt = bo - > resource & &
ttm_manager_type ( bdev , bo - > resource - > mem_type ) - > use_tt ;
new_use_tt = ttm_manager_type ( bdev , mem - > mem_type ) - > use_tt ;
2021-04-12 15:11:47 +02:00
2019-09-30 15:12:54 +02:00
ttm_bo_unmap_virtual ( bo ) ;
2009-06-10 15:20:19 +02:00
/*
* Create and bind a ttm if required .
*/
2022-02-17 10:43:16 +01:00
if ( new_use_tt ) {
2020-07-21 09:58:13 +02:00
/* Zero init the new TTM structure if the old location should
* have used one as well .
*/
2022-02-17 10:43:16 +01:00
ret = ttm_tt_create ( bo , old_use_tt ) ;
2020-06-24 15:15:20 +02:00
if ( ret )
goto out_err ;
2009-06-10 15:20:19 +02:00
2020-10-20 11:03:17 +10:00
if ( mem - > mem_type ! = TTM_PL_SYSTEM ) {
ret = ttm_tt_populate ( bo - > bdev , bo - > ttm , ctx ) ;
if ( ret )
goto out_err ;
}
2009-06-10 15:20:19 +02:00
}
2021-11-16 15:20:45 +01:00
ret = dma_resv_reserve_fences ( bo - > base . resv , 1 ) ;
if ( ret )
goto out_err ;
2020-10-01 14:51:40 +02:00
ret = bdev - > funcs - > move ( bo , evict , ctx , mem , hop ) ;
2020-10-29 13:58:52 +10:00
if ( ret ) {
if ( ret = = - EMULTIHOP )
return ret ;
drm/ttm: fix two regressions since move_notify changes
Both changes in dc97b3409a790d2a21aac6e5cdb99558b5944119 cause serious
regressions in the nouveau driver.
move_notify() was originally able to presume that bo->mem is the old node,
and new_mem is the new node. The above commit moves the call to
move_notify() to after move() has been done, which means that now, sometimes,
new_mem isn't the new node at all, bo->mem is, and new_mem points at a
stale, possibly-just-been-killed-by-move node.
This is clearly not a good situation. This patch reverts this change, and
replaces it with a cleanup in the move() failure path instead.
The second issue is that the call to move_notify() from cleanup_memtype_use()
causes the TTM ghost objects to get passed into the driver. This is clearly
bad as the driver knows nothing about these "fake" TTM BOs, and ends up
accessing uninitialised memory.
I worked around this in nouveau's move_notify() hook by ensuring the BO
destructor was nouveau's. I don't particularly like this solution, and
would rather TTM never pass the driver these objects. However, I don't
clearly understand the reason why we're calling move_notify() here anyway
and am happy to work around the problem in nouveau instead of breaking the
behaviour expected by other drivers.
Signed-off-by: Ben Skeggs <bskeggs@redhat.com>
Reviewed-by: Thomas Hellstrom <thellstrom@vmware.com>
Cc: Jerome Glisse <j.glisse@gmail.com>
Signed-off-by: Dave Airlie <airlied@redhat.com>
2012-01-25 15:34:22 +10:00
goto out_err ;
2020-10-29 13:58:52 +10:00
}
2011-11-18 11:47:03 -05:00
2020-12-09 15:07:50 +01:00
ctx - > bytes_moved + = bo - > base . size ;
2009-06-10 15:20:19 +02:00
return 0 ;
out_err :
2022-02-17 10:43:16 +01:00
if ( ! old_use_tt )
2020-09-15 11:02:12 +10:00
ttm_bo_tt_destroy ( bo ) ;
2009-06-10 15:20:19 +02:00
return ret ;
}
2020-11-16 17:41:01 +00:00
/*
2010-10-19 09:01:00 +02:00
* Call bo : : reserved .
2010-09-30 12:36:45 +02:00
* Will release GPU memory type usage on destruction .
2010-10-19 09:01:00 +02:00
* This is the place to put in driver specific hooks to release
* driver private resources .
* Will release the bo : : reserved lock .
2010-09-30 12:36:45 +02:00
*/
static void ttm_bo_cleanup_memtype_use ( struct ttm_buffer_object * bo )
{
2020-10-01 14:51:40 +02:00
if ( bo - > bdev - > funcs - > delete_mem_notify )
bo - > bdev - > funcs - > delete_mem_notify ( bo ) ;
2011-11-18 11:47:03 -05:00
2020-09-15 11:02:12 +10:00
ttm_bo_tt_destroy ( bo ) ;
2021-04-15 09:52:58 +02:00
ttm_resource_free ( bo , & bo - > resource ) ;
2010-09-30 12:36:45 +02:00
}
2017-07-20 20:55:06 +02:00
static int ttm_bo_individualize_resv ( struct ttm_buffer_object * bo )
{
int r ;
2019-08-05 16:01:12 +02:00
if ( bo - > base . resv = = & bo - > base . _resv )
2017-07-20 20:55:06 +02:00
return 0 ;
2019-08-11 10:06:32 +02:00
BUG_ON ( ! dma_resv_trylock ( & bo - > base . _resv ) ) ;
2017-07-20 20:55:06 +02:00
2019-08-11 10:06:32 +02:00
r = dma_resv_copy_fences ( & bo - > base . _resv , bo - > base . resv ) ;
2019-11-11 14:42:13 +01:00
dma_resv_unlock ( & bo - > base . _resv ) ;
2019-11-11 15:16:56 +01:00
if ( r )
return r ;
if ( bo - > type ! = ttm_bo_type_sg ) {
/* This works because the BO is about to be destroyed and nobody
* reference it any more . The only tricky case is the trylock on
* the resv object while holding the lru_lock .
*/
2020-10-06 17:26:42 +02:00
spin_lock ( & bo - > bdev - > lru_lock ) ;
2019-11-11 15:16:56 +01:00
bo - > base . resv = & bo - > base . _resv ;
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2019-11-11 15:16:56 +01:00
}
2017-07-20 20:55:06 +02:00
return r ;
}
2014-04-02 17:14:48 +02:00
static void ttm_bo_flush_all_fences ( struct ttm_buffer_object * bo )
{
2020-02-10 14:41:39 +01:00
struct dma_resv * resv = & bo - > base . _resv ;
2021-06-15 15:57:22 +02:00
struct dma_resv_iter cursor ;
2016-10-25 13:00:45 +01:00
struct dma_fence * fence ;
2014-04-02 17:14:48 +02:00
2021-11-09 11:08:18 +01:00
dma_resv_iter_begin ( & cursor , resv , DMA_RESV_USAGE_BOOKKEEP ) ;
2021-06-15 15:57:22 +02:00
dma_resv_for_each_fence_unlocked ( & cursor , fence ) {
2014-04-02 17:14:48 +02:00
if ( ! fence - > ops - > signaled )
2016-10-25 13:00:45 +01:00
dma_fence_enable_sw_signaling ( fence ) ;
2014-04-02 17:14:48 +02:00
}
2021-06-15 15:57:22 +02:00
dma_resv_iter_end ( & cursor ) ;
2014-04-02 17:14:48 +02:00
}
2010-10-19 09:01:01 +02:00
/**
2021-04-16 15:37:08 +01:00
* ttm_bo_cleanup_refs
2019-11-11 14:42:13 +01:00
* If bo idle , remove from lru lists , and unref .
* If not idle , block if possible .
2010-10-19 09:01:01 +02:00
*
2012-11-29 11:36:54 +00:00
* Must be called with lru_lock and reservation held , this function
2017-11-08 14:57:45 +01:00
* will drop the lru lock and optionally the reservation lock before returning .
2012-11-29 11:36:54 +00:00
*
2020-11-16 17:41:01 +00:00
* @ bo : The buffer object to clean - up
* @ interruptible : Any sleeps should occur interruptibly .
* @ no_wait_gpu : Never wait for gpu . Return - EBUSY instead .
* @ unlock_resv : Unlock the reservation lock as well .
2010-10-19 09:01:01 +02:00
*/
2017-11-08 14:57:45 +01:00
static int ttm_bo_cleanup_refs ( struct ttm_buffer_object * bo ,
bool interruptible , bool no_wait_gpu ,
bool unlock_resv )
2010-10-19 09:01:01 +02:00
{
2019-11-11 13:52:03 +01:00
struct dma_resv * resv = & bo - > base . _resv ;
2012-11-29 11:36:54 +00:00
int ret ;
2010-10-19 09:01:01 +02:00
2021-11-09 11:08:18 +01:00
if ( dma_resv_test_signaled ( resv , DMA_RESV_USAGE_BOOKKEEP ) )
2017-07-20 20:55:06 +02:00
ret = 0 ;
else
ret = - EBUSY ;
2010-10-19 09:01:01 +02:00
2012-11-29 11:36:54 +00:00
if ( ret & & ! no_wait_gpu ) {
2014-05-14 15:42:29 +02:00
long lret ;
2017-11-08 14:38:34 +01:00
2017-11-08 14:57:45 +01:00
if ( unlock_resv )
2019-08-11 10:06:32 +02:00
dma_resv_unlock ( bo - > base . resv ) ;
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2014-05-14 15:42:29 +02:00
2021-11-09 11:08:18 +01:00
lret = dma_resv_wait_timeout ( resv , DMA_RESV_USAGE_BOOKKEEP ,
2021-11-09 11:08:18 +01:00
interruptible ,
2021-06-02 13:01:15 +02:00
30 * HZ ) ;
2014-05-14 15:42:29 +02:00
if ( lret < 0 )
return lret ;
else if ( lret = = 0 )
return - EBUSY ;
2012-10-22 12:51:26 +00:00
2020-10-06 17:26:42 +02:00
spin_lock ( & bo - > bdev - > lru_lock ) ;
2019-08-11 10:06:32 +02:00
if ( unlock_resv & & ! dma_resv_trylock ( bo - > base . resv ) ) {
2017-11-08 14:57:45 +01:00
/*
* We raced , and lost , someone else holds the reservation now ,
* and is probably busy in ttm_bo_cleanup_memtype_use .
*
* Even if it ' s not the case , because we finished waiting any
* delayed destruction would succeed , so just return success
* here .
*/
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2012-11-29 11:36:54 +00:00
return 0 ;
}
2017-11-08 14:57:45 +01:00
ret = 0 ;
2014-01-21 13:07:01 +01:00
}
2009-06-10 15:20:19 +02:00
2012-11-29 11:36:54 +00:00
if ( ret | | unlikely ( list_empty ( & bo - > ddestroy ) ) ) {
2017-11-08 14:57:45 +01:00
if ( unlock_resv )
2019-08-11 10:06:32 +02:00
dma_resv_unlock ( bo - > base . resv ) ;
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2012-11-29 11:36:54 +00:00
return ret ;
2009-06-10 15:20:19 +02:00
}
2010-10-19 09:01:01 +02:00
list_del_init ( & bo - > ddestroy ) ;
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2010-10-19 09:01:01 +02:00
ttm_bo_cleanup_memtype_use ( bo ) ;
2017-11-08 14:57:45 +01:00
if ( unlock_resv )
2019-08-11 10:06:32 +02:00
dma_resv_unlock ( bo - > base . resv ) ;
2010-10-19 09:01:01 +02:00
2019-11-11 14:42:13 +01:00
ttm_bo_put ( bo ) ;
2010-10-19 09:01:01 +02:00
return 0 ;
2009-06-10 15:20:19 +02:00
}
2020-11-16 17:41:01 +00:00
/*
2009-06-10 15:20:19 +02:00
* Traverse the delayed list , and call ttm_bo_cleanup_refs on all
* encountered buffers .
*/
2020-10-01 14:51:40 +02:00
bool ttm_bo_delayed_delete ( struct ttm_device * bdev , bool remove_all )
2009-06-10 15:20:19 +02:00
{
2017-11-15 13:20:09 +01:00
struct list_head removed ;
bool empty ;
2010-01-20 20:01:30 +01:00
2017-11-15 13:20:09 +01:00
INIT_LIST_HEAD ( & removed ) ;
2009-06-10 15:20:19 +02:00
2020-10-06 17:26:42 +02:00
spin_lock ( & bdev - > lru_lock ) ;
2017-11-15 13:20:09 +01:00
while ( ! list_empty ( & bdev - > ddestroy ) ) {
struct ttm_buffer_object * bo ;
2013-01-15 14:56:37 +01:00
2017-11-15 13:20:09 +01:00
bo = list_first_entry ( & bdev - > ddestroy , struct ttm_buffer_object ,
ddestroy ) ;
list_move_tail ( & bo - > ddestroy , & removed ) ;
2019-11-11 14:42:13 +01:00
if ( ! ttm_bo_get_unless_zero ( bo ) )
continue ;
2012-11-29 11:36:54 +00:00
2019-08-05 16:01:12 +02:00
if ( remove_all | | bo - > base . resv ! = & bo - > base . _resv ) {
2020-10-06 17:26:42 +02:00
spin_unlock ( & bdev - > lru_lock ) ;
2019-08-11 10:06:32 +02:00
dma_resv_lock ( bo - > base . resv , NULL ) ;
2010-01-20 20:01:30 +01:00
2020-10-06 17:26:42 +02:00
spin_lock ( & bdev - > lru_lock ) ;
2017-12-15 13:36:49 +01:00
ttm_bo_cleanup_refs ( bo , false , ! remove_all , true ) ;
2019-08-11 10:06:32 +02:00
} else if ( dma_resv_trylock ( bo - > base . resv ) ) {
2017-12-15 13:36:49 +01:00
ttm_bo_cleanup_refs ( bo , false , ! remove_all , true ) ;
2017-12-21 19:04:15 +01:00
} else {
2020-10-06 17:26:42 +02:00
spin_unlock ( & bdev - > lru_lock ) ;
2017-12-15 13:36:49 +01:00
}
2009-06-10 15:20:19 +02:00
2019-11-11 14:42:13 +01:00
ttm_bo_put ( bo ) ;
2020-10-06 17:26:42 +02:00
spin_lock ( & bdev - > lru_lock ) ;
2009-06-10 15:20:19 +02:00
}
2017-11-15 13:20:09 +01:00
list_splice_tail ( & removed , & bdev - > ddestroy ) ;
empty = list_empty ( & bdev - > ddestroy ) ;
2020-10-06 17:26:42 +02:00
spin_unlock ( & bdev - > lru_lock ) ;
2017-11-15 13:20:09 +01:00
return empty ;
2009-06-10 15:20:19 +02:00
}
static void ttm_bo_release ( struct kref * kref )
{
struct ttm_buffer_object * bo =
container_of ( kref , struct ttm_buffer_object , kref ) ;
2020-10-01 14:51:40 +02:00
struct ttm_device * bdev = bo - > bdev ;
2019-11-11 14:42:13 +01:00
int ret ;
2009-06-10 15:20:19 +02:00
2021-04-14 16:02:16 +02:00
WARN_ON_ONCE ( bo - > pin_count ) ;
2022-01-24 11:07:15 +01:00
WARN_ON_ONCE ( bo - > bulk_move ) ;
2021-04-14 16:02:16 +02:00
2019-11-11 14:42:13 +01:00
if ( ! bo - > deleted ) {
ret = ttm_bo_individualize_resv ( bo ) ;
if ( ret ) {
/* Last resort, if we fail to allocate memory for the
* fences block for the BO to become idle
*/
2021-11-09 11:08:18 +01:00
dma_resv_wait_timeout ( bo - > base . resv ,
2021-11-09 11:08:18 +01:00
DMA_RESV_USAGE_BOOKKEEP , false ,
2021-06-02 13:01:15 +02:00
30 * HZ ) ;
2019-11-11 14:42:13 +01:00
}
2020-02-10 13:04:25 +01:00
2020-10-01 14:51:40 +02:00
if ( bo - > bdev - > funcs - > release_notify )
bo - > bdev - > funcs - > release_notify ( bo ) ;
2020-02-10 13:04:25 +01:00
drm_vma_offset_remove ( bdev - > vma_manager , & bo - > base . vma_node ) ;
2021-04-12 15:11:47 +02:00
ttm_mem_io_free ( bdev , bo - > resource ) ;
2019-11-11 14:42:13 +01:00
}
2021-11-09 11:08:18 +01:00
if ( ! dma_resv_test_signaled ( bo - > base . resv , DMA_RESV_USAGE_BOOKKEEP ) | |
2020-03-30 15:45:01 +02:00
! dma_resv_trylock ( bo - > base . resv ) ) {
2019-11-11 14:42:13 +01:00
/* The BO is not idle, resurrect it for delayed destroy */
ttm_bo_flush_all_fences ( bo ) ;
bo - > deleted = true ;
2020-10-06 17:26:42 +02:00
spin_lock ( & bo - > bdev - > lru_lock ) ;
2019-11-11 14:42:13 +01:00
/*
2020-09-21 16:06:36 +02:00
* Make pinned bos immediately available to
2019-11-11 14:42:13 +01:00
* shrinkers , now that they are queued for
* destruction .
2021-03-03 15:47:14 +01:00
*
* FIXME : QXL is triggering this . Can be removed when the
* driver is fixed .
2019-11-11 14:42:13 +01:00
*/
2021-04-14 16:02:16 +02:00
if ( bo - > pin_count ) {
2020-09-21 13:05:54 +02:00
bo - > pin_count = 0 ;
2022-01-24 11:07:15 +01:00
ttm_resource_move_to_lru_tail ( bo - > resource ) ;
2019-11-11 14:42:13 +01:00
}
kref_init ( & bo - > kref ) ;
list_add_tail ( & bo - > ddestroy , & bdev - > ddestroy ) ;
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2019-11-11 14:42:13 +01:00
schedule_delayed_work ( & bdev - > wq ,
( ( HZ / 100 ) < 1 ) ? 1 : HZ / 100 ) ;
return ;
}
2020-10-06 17:26:42 +02:00
spin_lock ( & bo - > bdev - > lru_lock ) ;
2019-11-11 14:42:13 +01:00
list_del ( & bo - > ddestroy ) ;
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2019-11-11 14:42:13 +01:00
ttm_bo_cleanup_memtype_use ( bo ) ;
2020-03-30 15:45:01 +02:00
dma_resv_unlock ( bo - > base . resv ) ;
2019-11-11 14:42:13 +01:00
2020-10-01 14:51:40 +02:00
atomic_dec ( & ttm_glob . bo_count ) ;
2019-11-11 14:42:13 +01:00
bo - > destroy ( bo ) ;
2009-06-10 15:20:19 +02:00
}
2018-06-21 15:21:35 +02:00
void ttm_bo_put ( struct ttm_buffer_object * bo )
{
kref_put ( & bo - > kref , ttm_bo_release ) ;
}
EXPORT_SYMBOL ( ttm_bo_put ) ;
2020-10-01 14:51:40 +02:00
int ttm_bo_lock_delayed_workqueue ( struct ttm_device * bdev )
2010-04-26 16:00:09 -04:00
{
return cancel_delayed_work_sync ( & bdev - > wq ) ;
}
EXPORT_SYMBOL ( ttm_bo_lock_delayed_workqueue ) ;
2020-10-01 14:51:40 +02:00
void ttm_bo_unlock_delayed_workqueue ( struct ttm_device * bdev , int resched )
2010-04-26 16:00:09 -04:00
{
if ( resched )
schedule_delayed_work ( & bdev - > wq ,
( ( HZ / 100 ) < 1 ) ? 1 : HZ / 100 ) ;
}
EXPORT_SYMBOL ( ttm_bo_unlock_delayed_workqueue ) ;
2021-06-22 12:23:39 -04:00
static int ttm_bo_bounce_temp_buffer ( struct ttm_buffer_object * bo ,
struct ttm_resource * * mem ,
struct ttm_operation_ctx * ctx ,
struct ttm_place * hop )
{
struct ttm_placement hop_placement ;
struct ttm_resource * hop_mem ;
int ret ;
hop_placement . num_placement = hop_placement . num_busy_placement = 1 ;
hop_placement . placement = hop_placement . busy_placement = hop ;
/* find space in the bounce domain */
ret = ttm_bo_mem_space ( bo , & hop_placement , & hop_mem , ctx ) ;
if ( ret )
return ret ;
/* move to the bounce domain */
ret = ttm_bo_handle_move_mem ( bo , hop_mem , false , ctx , NULL ) ;
if ( ret ) {
ttm_resource_free ( bo , & hop_mem ) ;
return ret ;
}
return 0 ;
}
2017-04-12 16:48:39 +02:00
static int ttm_bo_evict ( struct ttm_buffer_object * bo ,
struct ttm_operation_ctx * ctx )
2009-06-10 15:20:19 +02:00
{
2020-10-01 14:51:40 +02:00
struct ttm_device * bdev = bo - > bdev ;
2021-04-15 09:52:58 +02:00
struct ttm_resource * evict_mem ;
2009-12-08 15:33:32 +01:00
struct ttm_placement placement ;
2020-10-29 13:58:52 +10:00
struct ttm_place hop ;
2009-12-08 15:33:32 +01:00
int ret = 0 ;
2009-06-10 15:20:19 +02:00
2020-10-29 13:58:52 +10:00
memset ( & hop , 0 , sizeof ( hop ) ) ;
2019-08-11 10:06:32 +02:00
dma_resv_assert_held ( bo - > base . resv ) ;
2009-06-10 15:20:19 +02:00
2018-02-20 15:35:21 +01:00
placement . num_placement = 0 ;
placement . num_busy_placement = 0 ;
2020-10-01 14:51:40 +02:00
bdev - > funcs - > evict_flags ( bo , & placement ) ;
2018-02-20 15:35:21 +01:00
2020-07-23 10:58:12 +02:00
if ( ! placement . num_placement & & ! placement . num_busy_placement ) {
2021-06-02 10:38:13 +02:00
ret = ttm_bo_wait ( bo , true , false ) ;
if ( ret )
return ret ;
2020-07-23 10:58:12 +02:00
2021-06-02 10:38:13 +02:00
/*
* Since we ' ve already synced , this frees backing store
* immediately .
*/
return ttm_bo_pipeline_gutting ( bo ) ;
2020-07-23 10:58:12 +02:00
}
2018-02-20 15:35:21 +01:00
2017-04-12 16:48:39 +02:00
ret = ttm_bo_mem_space ( bo , & placement , & evict_mem , ctx ) ;
2009-06-10 15:20:19 +02:00
if ( ret ) {
2009-12-09 21:55:10 +01:00
if ( ret ! = - ERESTARTSYS ) {
2012-03-16 21:43:50 -07:00
pr_err ( " Failed to find memory space for buffer 0x%p eviction \n " ,
bo ) ;
2009-12-09 21:55:10 +01:00
ttm_bo_mem_space_debug ( bo , & placement ) ;
}
2009-06-10 15:20:19 +02:00
goto out ;
}
2021-06-22 12:23:39 -04:00
bounce :
2021-04-15 09:52:58 +02:00
ret = ttm_bo_handle_move_mem ( bo , evict_mem , true , ctx , & hop ) ;
2021-06-22 12:23:39 -04:00
if ( ret = = - EMULTIHOP ) {
ret = ttm_bo_bounce_temp_buffer ( bo , & evict_mem , ctx , & hop ) ;
if ( ret ) {
2012-03-16 21:43:50 -07:00
pr_err ( " Buffer eviction failed \n " ) ;
2021-06-22 12:23:39 -04:00
ttm_resource_free ( bo , & evict_mem ) ;
goto out ;
}
/* try and move to final place now. */
goto bounce ;
2009-06-10 15:20:19 +02:00
}
2009-12-08 15:33:32 +01:00
out :
return ret ;
}
2016-08-30 17:26:04 +02:00
bool ttm_bo_eviction_valuable ( struct ttm_buffer_object * bo ,
const struct ttm_place * place )
{
2022-08-20 00:32:59 -07:00
struct ttm_resource * res = bo - > resource ;
struct ttm_device * bdev = bo - > bdev ;
2021-06-02 10:38:14 +02:00
dma_resv_assert_held ( bo - > base . resv ) ;
if ( bo - > resource - > mem_type = = TTM_PL_SYSTEM )
return true ;
2016-08-30 17:26:04 +02:00
/* Don't evict this BO if it's outside of the
* requested placement range
*/
2022-08-20 00:32:59 -07:00
return ttm_resource_intersects ( bdev , res , place , bo - > base . size ) ;
2016-08-30 17:26:04 +02:00
}
EXPORT_SYMBOL ( ttm_bo_eviction_valuable ) ;
2020-11-16 17:41:01 +00:00
/*
2017-12-21 17:42:52 +08:00
* Check the target bo is allowable to be evicted or swapout , including cases :
*
* a . if share same reservation object with ctx - > resv , have assumption
* reservation objects should already be locked , so not lock again and
* return true directly when either the opreation allow_reserved_eviction
* or the target bo already is in delayed free list ;
*
* b . Otherwise , trylock it .
*/
static bool ttm_bo_evict_swapout_allowable ( struct ttm_buffer_object * bo ,
2021-06-02 10:38:14 +02:00
struct ttm_operation_ctx * ctx ,
const struct ttm_place * place ,
bool * locked , bool * busy )
2017-12-21 17:42:52 +08:00
{
bool ret = false ;
2019-08-05 16:01:12 +02:00
if ( bo - > base . resv = = ctx - > resv ) {
2019-08-11 10:06:32 +02:00
dma_resv_assert_held ( bo - > base . resv ) ;
2020-11-02 13:16:13 +01:00
if ( ctx - > allow_res_evict )
2017-12-21 17:42:52 +08:00
ret = true ;
2019-05-22 09:51:47 +02:00
* locked = false ;
if ( busy )
* busy = false ;
2017-12-21 17:42:52 +08:00
} else {
2019-08-11 10:06:32 +02:00
ret = dma_resv_trylock ( bo - > base . resv ) ;
2019-05-22 09:51:47 +02:00
* locked = ret ;
if ( busy )
* busy = ! ret ;
2017-12-21 17:42:52 +08:00
}
2021-11-10 12:31:49 +08:00
if ( ret & & place & & ( bo - > resource - > mem_type ! = place - > mem_type | |
! bo - > bdev - > funcs - > eviction_valuable ( bo , place ) ) ) {
2021-06-02 10:38:14 +02:00
ret = false ;
if ( * locked ) {
dma_resv_unlock ( bo - > base . resv ) ;
* locked = false ;
}
}
2017-12-21 17:42:52 +08:00
return ret ;
}
2019-05-22 09:51:47 +02:00
/**
* ttm_mem_evict_wait_busy - wait for a busy BO to become available
*
* @ busy_bo : BO which couldn ' t be locked with trylock
* @ ctx : operation context
* @ ticket : acquire ticket
*
* Try to lock a busy buffer object to avoid failing eviction .
*/
static int ttm_mem_evict_wait_busy ( struct ttm_buffer_object * busy_bo ,
struct ttm_operation_ctx * ctx ,
struct ww_acquire_ctx * ticket )
{
int r ;
if ( ! busy_bo | | ! ticket )
return - EBUSY ;
if ( ctx - > interruptible )
2019-08-11 10:06:32 +02:00
r = dma_resv_lock_interruptible ( busy_bo - > base . resv ,
2019-05-22 09:51:47 +02:00
ticket ) ;
else
2019-08-11 10:06:32 +02:00
r = dma_resv_lock ( busy_bo - > base . resv , ticket ) ;
2019-05-22 09:51:47 +02:00
/*
* TODO : It would be better to keep the BO locked until allocation is at
* least tried one more time , but that would mean a much larger rework
* of TTM .
*/
if ( ! r )
2019-08-11 10:06:32 +02:00
dma_resv_unlock ( busy_bo - > base . resv ) ;
2019-05-22 09:51:47 +02:00
2019-06-26 02:32:43 -04:00
return r = = - EDEADLK ? - EBUSY : r ;
2019-05-22 09:51:47 +02:00
}
2020-10-01 14:51:40 +02:00
int ttm_mem_evict_first ( struct ttm_device * bdev ,
2020-08-03 16:25:15 +02:00
struct ttm_resource_manager * man ,
const struct ttm_place * place ,
struct ttm_operation_ctx * ctx ,
struct ww_acquire_ctx * ticket )
2009-12-08 15:33:32 +01:00
{
2019-05-22 09:51:47 +02:00
struct ttm_buffer_object * bo = NULL , * busy_bo = NULL ;
2021-06-08 11:13:21 +02:00
struct ttm_resource_cursor cursor ;
2021-07-16 14:00:10 +02:00
struct ttm_resource * res ;
2017-11-08 15:55:44 +01:00
bool locked = false ;
int ret ;
2009-06-10 15:20:19 +02:00
2021-03-25 16:06:57 +01:00
spin_lock ( & bdev - > lru_lock ) ;
2021-06-08 11:13:21 +02:00
ttm_resource_manager_for_each_res ( man , & cursor , res ) {
bool busy ;
if ( ! ttm_bo_evict_swapout_allowable ( res - > bo , ctx , place ,
& locked , & busy ) ) {
if ( busy & & ! busy_bo & & ticket ! =
dma_resv_locking_ctx ( res - > bo - > base . resv ) )
busy_bo = res - > bo ;
continue ;
}
2019-05-22 09:51:47 +02:00
2021-06-08 11:13:21 +02:00
if ( ttm_bo_get_unless_zero ( res - > bo ) ) {
2021-07-16 14:00:10 +02:00
bo = res - > bo ;
2017-01-10 14:08:28 +01:00
break ;
2014-10-09 15:02:59 +09:00
}
2021-06-08 11:13:21 +02:00
if ( locked )
dma_resv_unlock ( res - > bo - > base . resv ) ;
2012-11-28 11:25:43 +00:00
}
2017-11-08 15:55:44 +01:00
if ( ! bo ) {
2019-11-11 14:42:13 +01:00
if ( busy_bo & & ! ttm_bo_get_unless_zero ( busy_bo ) )
busy_bo = NULL ;
2021-03-25 16:06:57 +01:00
spin_unlock ( & bdev - > lru_lock ) ;
2019-05-22 09:51:47 +02:00
ret = ttm_mem_evict_wait_busy ( busy_bo , ctx , ticket ) ;
if ( busy_bo )
2019-11-11 14:42:13 +01:00
ttm_bo_put ( busy_bo ) ;
2019-05-22 09:51:47 +02:00
return ret ;
2009-12-02 18:33:46 +01:00
}
2019-11-11 14:42:13 +01:00
if ( bo - > deleted ) {
2017-04-12 16:48:39 +02:00
ret = ttm_bo_cleanup_refs ( bo , ctx - > interruptible ,
ctx - > no_wait_gpu , locked ) ;
2019-11-11 14:42:13 +01:00
ttm_bo_put ( bo ) ;
2012-10-22 12:51:26 +00:00
return ret ;
2010-10-19 09:01:01 +02:00
}
2021-03-25 16:06:57 +01:00
spin_unlock ( & bdev - > lru_lock ) ;
2009-12-02 18:33:46 +01:00
2017-04-12 16:48:39 +02:00
ret = ttm_bo_evict ( bo , ctx ) ;
2019-09-19 12:56:15 +02:00
if ( locked )
2017-11-08 15:55:44 +01:00
ttm_bo_unreserve ( bo ) ;
2021-11-10 12:31:48 +08:00
else
ttm_bo_move_to_lru_tail_unlocked ( bo ) ;
2009-12-02 18:33:46 +01:00
2019-11-11 14:42:13 +01:00
ttm_bo_put ( bo ) ;
2009-06-10 15:20:19 +02:00
return ret ;
}
2022-02-15 17:29:59 +01:00
/**
* ttm_bo_pin - Pin the buffer object .
* @ bo : The buffer object to pin
*
* Make sure the buffer is not evicted any more during memory pressure .
* @ bo must be unpinned again by calling ttm_bo_unpin ( ) .
*/
void ttm_bo_pin ( struct ttm_buffer_object * bo )
{
dma_resv_assert_held ( bo - > base . resv ) ;
WARN_ON_ONCE ( ! kref_read ( & bo - > kref ) ) ;
2022-06-13 09:37:03 +02:00
spin_lock ( & bo - > bdev - > lru_lock ) ;
if ( bo - > resource )
ttm_resource_del_bulk_move ( bo - > resource , bo ) ;
+ + bo - > pin_count ;
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2022-02-15 17:29:59 +01:00
}
EXPORT_SYMBOL ( ttm_bo_pin ) ;
/**
* ttm_bo_unpin - Unpin the buffer object .
* @ bo : The buffer object to unpin
*
* Allows the buffer object to be evicted again during memory pressure .
*/
void ttm_bo_unpin ( struct ttm_buffer_object * bo )
{
dma_resv_assert_held ( bo - > base . resv ) ;
WARN_ON_ONCE ( ! kref_read ( & bo - > kref ) ) ;
2022-01-24 11:07:15 +01:00
if ( WARN_ON_ONCE ( ! bo - > pin_count ) )
return ;
2022-06-13 09:37:03 +02:00
spin_lock ( & bo - > bdev - > lru_lock ) ;
- - bo - > pin_count ;
if ( bo - > resource )
ttm_resource_add_bulk_move ( bo - > resource , bo ) ;
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2022-02-15 17:29:59 +01:00
}
EXPORT_SYMBOL ( ttm_bo_unpin ) ;
2020-11-16 17:41:01 +00:00
/*
2021-11-23 11:30:35 +01:00
* Add the last move fence to the BO as kernel dependency and reserve a new
* fence slot .
2016-06-15 13:44:03 +02:00
*/
static int ttm_bo_add_move_fence ( struct ttm_buffer_object * bo ,
2020-08-04 12:56:31 +10:00
struct ttm_resource_manager * man ,
2020-08-04 12:56:32 +10:00
struct ttm_resource * mem ,
2019-09-16 10:20:47 -05:00
bool no_wait_gpu )
2016-06-15 13:44:03 +02:00
{
2016-10-25 13:00:45 +01:00
struct dma_fence * fence ;
2016-06-15 13:44:03 +02:00
int ret ;
spin_lock ( & man - > move_lock ) ;
2016-10-25 13:00:45 +01:00
fence = dma_fence_get ( man - > move ) ;
2016-06-15 13:44:03 +02:00
spin_unlock ( & man - > move_lock ) ;
2019-09-16 10:20:47 -05:00
if ( ! fence )
return 0 ;
2016-06-15 13:44:03 +02:00
2020-06-13 20:30:25 +08:00
if ( no_wait_gpu ) {
2021-02-26 22:43:04 -05:00
ret = dma_fence_is_signaled ( fence ) ? 0 : - EBUSY ;
2020-06-13 20:30:25 +08:00
dma_fence_put ( fence ) ;
2021-02-26 22:43:04 -05:00
return ret ;
2020-06-13 20:30:25 +08:00
}
2016-06-15 13:44:03 +02:00
2021-11-26 14:12:42 +01:00
dma_resv_add_fence ( bo - > base . resv , fence , DMA_RESV_USAGE_KERNEL ) ;
2019-09-16 10:20:47 -05:00
2021-11-16 15:20:45 +01:00
ret = dma_resv_reserve_fences ( bo - > base . resv , 1 ) ;
2022-04-13 09:21:33 +01:00
dma_fence_put ( fence ) ;
return ret ;
2016-06-15 13:44:03 +02:00
}
2020-11-16 17:41:01 +00:00
/*
2009-06-10 15:20:19 +02:00
* Repeatedly evict memory from the LRU for @ mem_type until we create enough
* space , or we ' ve evicted everything and there isn ' t enough space .
*/
2009-12-08 15:33:32 +01:00
static int ttm_bo_mem_force_space ( struct ttm_buffer_object * bo ,
2019-05-13 17:34:29 +02:00
const struct ttm_place * place ,
2021-04-15 09:52:58 +02:00
struct ttm_resource * * mem ,
2019-05-13 17:34:29 +02:00
struct ttm_operation_ctx * ctx )
2009-06-10 15:20:19 +02:00
{
2020-10-01 14:51:40 +02:00
struct ttm_device * bdev = bo - > bdev ;
2021-04-15 09:52:58 +02:00
struct ttm_resource_manager * man ;
2019-07-31 09:41:50 +02:00
struct ww_acquire_ctx * ticket ;
2009-06-10 15:20:19 +02:00
int ret ;
2021-06-07 19:10:45 +02:00
man = ttm_manager_type ( bdev , place - > mem_type ) ;
2019-08-11 10:06:32 +02:00
ticket = dma_resv_locking_ctx ( bo - > base . resv ) ;
2009-06-10 15:20:19 +02:00
do {
2020-08-03 16:25:15 +02:00
ret = ttm_resource_alloc ( bo , place , mem ) ;
2020-06-16 14:33:23 +02:00
if ( likely ( ! ret ) )
2009-06-10 15:20:19 +02:00
break ;
2020-06-16 14:33:23 +02:00
if ( unlikely ( ret ! = - ENOSPC ) )
return ret ;
2020-08-04 12:55:57 +10:00
ret = ttm_mem_evict_first ( bdev , man , place , ctx ,
2019-07-31 09:41:50 +02:00
ticket ) ;
2009-06-10 15:20:19 +02:00
if ( unlikely ( ret ! = 0 ) )
return ret ;
} while ( 1 ) ;
2019-05-13 17:34:29 +02:00
2021-04-15 09:52:58 +02:00
return ttm_bo_add_move_fence ( bo , man , * mem , ctx - > no_wait_gpu ) ;
2019-05-13 17:34:29 +02:00
}
2020-11-16 17:41:01 +00:00
/*
2009-06-10 15:20:19 +02:00
* Creates space for memory region @ mem according to its type .
*
* This function first searches for free space in compatible memory types in
* the priority order defined by the driver . If free space isn ' t found , then
* ttm_bo_mem_force_space is attempted in priority order to evict and find
* space .
*/
int ttm_bo_mem_space ( struct ttm_buffer_object * bo ,
2009-12-08 15:33:32 +01:00
struct ttm_placement * placement ,
2021-04-15 09:52:58 +02:00
struct ttm_resource * * mem ,
2017-04-12 15:33:00 +02:00
struct ttm_operation_ctx * ctx )
2009-06-10 15:20:19 +02:00
{
2020-10-01 14:51:40 +02:00
struct ttm_device * bdev = bo - > bdev ;
2009-06-10 15:20:19 +02:00
bool type_found = false ;
2009-12-08 15:33:32 +01:00
int i , ret ;
2009-06-10 15:20:19 +02:00
2021-11-16 15:20:45 +01:00
ret = dma_resv_reserve_fences ( bo - > base . resv , 1 ) ;
2016-06-15 13:44:03 +02:00
if ( unlikely ( ret ) )
return ret ;
2009-12-14 14:51:35 +10:00
for ( i = 0 ; i < placement - > num_placement ; + + i ) {
2014-08-27 13:16:04 +02:00
const struct ttm_place * place = & placement - > placement [ i ] ;
2020-08-04 12:56:31 +10:00
struct ttm_resource_manager * man ;
2014-08-27 13:16:04 +02:00
2021-04-15 09:52:58 +02:00
man = ttm_manager_type ( bdev , place - > mem_type ) ;
if ( ! man | | ! ttm_resource_manager_used ( man ) )
2020-09-10 13:39:41 +02:00
continue ;
2009-06-10 15:20:19 +02:00
2015-09-14 01:24:41 -07:00
type_found = true ;
2020-08-03 16:25:15 +02:00
ret = ttm_resource_alloc ( bo , place , mem ) ;
2020-06-16 14:33:23 +02:00
if ( ret = = - ENOSPC )
continue ;
2015-09-14 01:24:41 -07:00
if ( unlikely ( ret ) )
2019-05-13 17:58:23 +02:00
goto error ;
2016-06-15 13:44:03 +02:00
2021-04-15 09:52:58 +02:00
ret = ttm_bo_add_move_fence ( bo , man , * mem , ctx - > no_wait_gpu ) ;
2019-09-16 10:20:47 -05:00
if ( unlikely ( ret ) ) {
2020-08-03 16:25:15 +02:00
ttm_resource_free ( bo , mem ) ;
2019-09-16 10:20:47 -05:00
if ( ret = = - EBUSY )
continue ;
goto error ;
2016-06-15 13:44:03 +02:00
}
2019-09-16 10:20:47 -05:00
return 0 ;
2009-06-10 15:20:19 +02:00
}
2009-12-14 14:51:35 +10:00
for ( i = 0 ; i < placement - > num_busy_placement ; + + i ) {
2014-08-27 13:16:04 +02:00
const struct ttm_place * place = & placement - > busy_placement [ i ] ;
2021-04-15 09:52:58 +02:00
struct ttm_resource_manager * man ;
2014-08-27 13:16:04 +02:00
2021-04-15 09:52:58 +02:00
man = ttm_manager_type ( bdev , place - > mem_type ) ;
if ( ! man | | ! ttm_resource_manager_used ( man ) )
2020-09-10 13:39:41 +02:00
continue ;
2009-06-10 15:20:19 +02:00
2015-09-14 01:24:41 -07:00
type_found = true ;
2019-05-13 17:34:29 +02:00
ret = ttm_bo_mem_force_space ( bo , place , mem , ctx ) ;
2020-07-06 17:32:55 +02:00
if ( likely ( ! ret ) )
2009-06-10 15:20:19 +02:00
return 0 ;
2019-05-13 17:34:29 +02:00
2019-05-13 15:36:08 +02:00
if ( ret & & ret ! = - EBUSY )
2019-05-13 17:58:23 +02:00
goto error ;
2009-06-10 15:20:19 +02:00
}
2015-09-14 01:24:41 -07:00
2019-05-13 17:58:23 +02:00
ret = - ENOMEM ;
2015-09-14 01:24:41 -07:00
if ( ! type_found ) {
2017-02-28 04:55:54 -08:00
pr_err ( TTM_PFX " No compatible memory type found \n " ) ;
2019-05-13 17:58:23 +02:00
ret = - EINVAL ;
2015-09-14 01:24:41 -07:00
}
2019-05-13 17:58:23 +02:00
error :
return ret ;
2009-06-10 15:20:19 +02:00
}
EXPORT_SYMBOL ( ttm_bo_mem_space ) ;
2014-01-06 22:12:58 +05:30
static int ttm_bo_move_buffer ( struct ttm_buffer_object * bo ,
2017-04-12 16:48:39 +02:00
struct ttm_placement * placement ,
struct ttm_operation_ctx * ctx )
2009-06-10 15:20:19 +02:00
{
2021-04-15 09:52:58 +02:00
struct ttm_resource * mem ;
2020-10-29 13:58:52 +10:00
struct ttm_place hop ;
2021-02-16 19:03:52 +01:00
int ret ;
2009-06-10 15:20:19 +02:00
2019-08-11 10:06:32 +02:00
dma_resv_assert_held ( bo - > base . resv ) ;
2009-06-10 15:20:19 +02:00
/*
* Determine where to move the buffer .
2020-10-29 13:58:52 +10:00
*
* If driver determines move is going to need
* an extra step then it will return - EMULTIHOP
* and the buffer will be moved to the temporary
* stop and the driver will be called to make
* the second hop .
2009-06-10 15:20:19 +02:00
*/
2017-04-12 16:48:39 +02:00
ret = ttm_bo_mem_space ( bo , placement , & mem , ctx ) ;
2009-06-10 15:20:19 +02:00
if ( ret )
2020-10-29 13:58:52 +10:00
return ret ;
2021-02-19 12:25:47 +08:00
bounce :
2021-04-15 09:52:58 +02:00
ret = ttm_bo_handle_move_mem ( bo , mem , false , ctx , & hop ) ;
2020-10-29 13:58:52 +10:00
if ( ret = = - EMULTIHOP ) {
ret = ttm_bo_bounce_temp_buffer ( bo , & mem , ctx , & hop ) ;
if ( ret )
2021-02-19 12:25:47 +08:00
goto out ;
2020-10-29 13:58:52 +10:00
/* try and move to final place now. */
goto bounce ;
}
2021-02-19 12:25:47 +08:00
out :
2020-07-06 17:32:55 +02:00
if ( ret )
2020-08-03 16:25:15 +02:00
ttm_resource_free ( bo , & mem ) ;
2009-06-10 15:20:19 +02:00
return ret ;
}
2009-12-10 17:16:27 +01:00
int ttm_bo_validate ( struct ttm_buffer_object * bo ,
2017-04-12 14:24:39 +02:00
struct ttm_placement * placement ,
struct ttm_operation_ctx * ctx )
2009-06-10 15:20:19 +02:00
{
int ret ;
2019-08-11 10:06:32 +02:00
dma_resv_assert_held ( bo - > base . resv ) ;
2018-03-15 16:48:20 +01:00
/*
* Remove the backing store if no placement is given .
*/
2021-06-02 10:38:13 +02:00
if ( ! placement - > num_placement & & ! placement - > num_busy_placement )
return ttm_bo_pipeline_gutting ( bo ) ;
2018-03-15 16:48:20 +01:00
2009-06-10 15:20:19 +02:00
/*
* Check whether we need to move buffer .
*/
2022-02-17 10:43:16 +01:00
if ( ! bo - > resource | | ! ttm_resource_compat ( bo - > resource , placement ) ) {
2017-04-12 16:48:39 +02:00
ret = ttm_bo_move_buffer ( bo , placement , ctx ) ;
2009-12-08 15:33:32 +01:00
if ( ret )
2009-06-10 15:20:19 +02:00
return ret ;
}
2020-08-12 13:03:49 +10:00
/*
* We might need to add a TTM .
*/
2022-08-09 02:56:23 -07:00
if ( ! bo - > resource | | bo - > resource - > mem_type = = TTM_PL_SYSTEM ) {
2020-08-12 13:03:49 +10:00
ret = ttm_tt_create ( bo , true ) ;
if ( ret )
return ret ;
}
2009-06-10 15:20:19 +02:00
return 0 ;
}
2009-12-10 17:16:27 +01:00
EXPORT_SYMBOL ( ttm_bo_validate ) ;
2009-06-10 15:20:19 +02:00
2022-02-18 14:32:53 +01:00
/**
* ttm_bo_init_reserved
*
* @ bdev : Pointer to a ttm_device struct .
* @ bo : Pointer to a ttm_buffer_object to be initialized .
* @ type : Requested type of buffer object .
* @ placement : Initial placement for buffer object .
* @ alignment : Data alignment in pages .
* @ ctx : TTM operation context for memory allocation .
* @ sg : Scatter - gather table .
* @ resv : Pointer to a dma_resv , or NULL to let ttm allocate one .
* @ destroy : Destroy function . Use NULL for kfree ( ) .
*
* This function initializes a pre - allocated struct ttm_buffer_object .
* As this object may be part of a larger structure , this function ,
* together with the @ destroy function , enables driver - specific objects
* derived from a ttm_buffer_object .
*
* On successful return , the caller owns an object kref to @ bo . The kref and
* list_kref are usually set to 1 , but note that in some situations , other
* tasks may already be holding references to @ bo as well .
* Furthermore , if resv = = NULL , the buffer ' s reservation lock will be held ,
* and it is the caller ' s responsibility to call ttm_bo_unreserve .
*
* If a failure occurs , the function will call the @ destroy function . Thus ,
* after a failure , dereferencing @ bo is illegal and will likely cause memory
* corruption .
*
* Returns
* - ENOMEM : Out of memory .
* - EINVAL : Invalid placement flags .
* - ERESTARTSYS : Interrupted by signal while sleeping waiting for resources .
*/
int ttm_bo_init_reserved ( struct ttm_device * bdev , struct ttm_buffer_object * bo ,
enum ttm_bo_type type , struct ttm_placement * placement ,
uint32_t alignment , struct ttm_operation_ctx * ctx ,
struct sg_table * sg , struct dma_resv * resv ,
2017-02-16 10:56:40 +01:00
void ( * destroy ) ( struct ttm_buffer_object * ) )
2009-06-10 15:20:19 +02:00
{
2021-02-16 19:03:52 +01:00
static const struct ttm_place sys_mem = { . mem_type = TTM_PL_SYSTEM } ;
2021-04-15 09:52:58 +02:00
int ret ;
2011-11-11 15:42:57 -05:00
2009-06-10 15:20:19 +02:00
kref_init ( & bo - > kref ) ;
INIT_LIST_HEAD ( & bo - > ddestroy ) ;
bo - > bdev = bdev ;
bo - > type = type ;
2022-02-18 14:32:53 +01:00
bo - > page_alignment = alignment ;
bo - > destroy = destroy ;
2020-09-21 13:05:54 +02:00
bo - > pin_count = 0 ;
2012-04-02 11:46:06 +01:00
bo - > sg = sg ;
2022-01-24 11:07:15 +01:00
bo - > bulk_move = NULL ;
2022-02-18 14:32:53 +01:00
if ( resv )
2019-08-05 16:01:11 +02:00
bo - > base . resv = resv ;
2022-02-18 14:32:53 +01:00
else
2019-08-05 16:01:11 +02:00
bo - > base . resv = & bo - > base . _resv ;
2020-10-01 14:51:40 +02:00
atomic_inc ( & ttm_glob . bo_count ) ;
2009-06-10 15:20:19 +02:00
2021-04-15 09:52:58 +02:00
ret = ttm_resource_alloc ( bo , & sys_mem , & bo - > resource ) ;
if ( unlikely ( ret ) ) {
ttm_bo_put ( bo ) ;
return ret ;
}
2009-06-10 15:20:19 +02:00
/*
* For ttm_bo_type_device buffers , allocate
* address space from the device .
*/
2022-02-18 14:32:53 +01:00
if ( bo - > type = = ttm_bo_type_device | | bo - > type = = ttm_bo_type_sg ) {
2019-09-05 09:05:02 +02:00
ret = drm_vma_offset_add ( bdev - > vma_manager , & bo - > base . vma_node ,
2022-02-18 14:32:53 +01:00
PFN_UP ( bo - > base . size ) ) ;
if ( ret )
goto err_put ;
}
2009-06-10 15:20:19 +02:00
2014-01-09 11:03:15 +01:00
/* passed reservation objects should already be locked,
* since otherwise lockdep will be angered in radeon .
*/
2022-02-18 14:32:53 +01:00
if ( ! resv )
WARN_ON ( ! dma_resv_trylock ( bo - > base . resv ) ) ;
else
dma_resv_assert_held ( resv ) ;
2009-06-10 15:20:19 +02:00
2022-02-18 14:32:53 +01:00
ret = ttm_bo_validate ( bo , placement , ctx ) ;
if ( unlikely ( ret ) )
goto err_unlock ;
2009-06-10 15:20:19 +02:00
2022-02-18 14:32:53 +01:00
return 0 ;
2017-02-16 10:56:40 +01:00
2022-02-18 14:32:53 +01:00
err_unlock :
if ( ! resv )
dma_resv_unlock ( bo - > base . resv ) ;
2017-02-14 09:37:12 +01:00
2022-02-18 14:32:53 +01:00
err_put :
ttm_bo_put ( bo ) ;
2009-06-10 15:20:19 +02:00
return ret ;
}
2017-02-16 10:56:40 +01:00
EXPORT_SYMBOL ( ttm_bo_init_reserved ) ;
2022-02-18 14:32:53 +01:00
/**
* ttm_bo_init_validate
*
* @ bdev : Pointer to a ttm_device struct .
* @ bo : Pointer to a ttm_buffer_object to be initialized .
* @ type : Requested type of buffer object .
* @ placement : Initial placement for buffer object .
* @ alignment : Data alignment in pages .
* @ interruptible : If needing to sleep to wait for GPU resources ,
* sleep interruptible .
* pinned in physical memory . If this behaviour is not desired , this member
* holds a pointer to a persistent shmem object . Typically , this would
* point to the shmem object backing a GEM object if TTM is used to back a
* GEM user interface .
* @ sg : Scatter - gather table .
* @ resv : Pointer to a dma_resv , or NULL to let ttm allocate one .
* @ destroy : Destroy function . Use NULL for kfree ( ) .
*
* This function initializes a pre - allocated struct ttm_buffer_object .
* As this object may be part of a larger structure , this function ,
* together with the @ destroy function ,
* enables driver - specific objects derived from a ttm_buffer_object .
*
* On successful return , the caller owns an object kref to @ bo . The kref and
* list_kref are usually set to 1 , but note that in some situations , other
* tasks may already be holding references to @ bo as well .
*
* If a failure occurs , the function will call the @ destroy function , Thus ,
* after a failure , dereferencing @ bo is illegal and will likely cause memory
* corruption .
*
* Returns
* - ENOMEM : Out of memory .
* - EINVAL : Invalid placement flags .
* - ERESTARTSYS : Interrupted by signal while sleeping waiting for resources .
*/
int ttm_bo_init_validate ( struct ttm_device * bdev , struct ttm_buffer_object * bo ,
enum ttm_bo_type type , struct ttm_placement * placement ,
uint32_t alignment , bool interruptible ,
struct sg_table * sg , struct dma_resv * resv ,
void ( * destroy ) ( struct ttm_buffer_object * ) )
2017-02-16 10:56:40 +01:00
{
2017-04-12 14:41:43 +02:00
struct ttm_operation_ctx ctx = { interruptible , false } ;
2017-02-16 10:56:40 +01:00
int ret ;
2022-02-18 14:32:53 +01:00
ret = ttm_bo_init_reserved ( bdev , bo , type , placement , alignment , & ctx ,
sg , resv , destroy ) ;
2017-02-16 10:56:40 +01:00
if ( ret )
return ret ;
if ( ! resv )
ttm_bo_unreserve ( bo ) ;
return 0 ;
}
2022-02-18 14:32:53 +01:00
EXPORT_SYMBOL ( ttm_bo_init_validate ) ;
2009-06-10 15:20:19 +02:00
/*
* buffer object vm functions .
*/
2010-11-11 09:41:57 +01:00
void ttm_bo_unmap_virtual ( struct ttm_buffer_object * bo )
{
2020-10-01 14:51:40 +02:00
struct ttm_device * bdev = bo - > bdev ;
2010-11-11 09:41:57 +01:00
2019-09-30 15:12:54 +02:00
drm_vma_node_unmap ( & bo - > base . vma_node , bdev - > dev_mapping ) ;
2021-04-12 15:11:47 +02:00
ttm_mem_io_free ( bdev , bo - > resource ) ;
2009-06-10 15:20:19 +02:00
}
2009-06-24 09:48:08 +10:00
EXPORT_SYMBOL ( ttm_bo_unmap_virtual ) ;
2009-06-10 15:20:19 +02:00
int ttm_bo_wait ( struct ttm_buffer_object * bo ,
2016-04-06 11:12:04 +02:00
bool interruptible , bool no_wait )
2009-06-10 15:20:19 +02:00
{
2016-11-07 16:16:15 -05:00
long timeout = 15 * HZ ;
if ( no_wait ) {
2021-11-09 11:08:18 +01:00
if ( dma_resv_test_signaled ( bo - > base . resv , DMA_RESV_USAGE_BOOKKEEP ) )
2016-11-07 16:16:15 -05:00
return 0 ;
else
return - EBUSY ;
}
2014-04-02 17:14:48 +02:00
2021-11-09 11:08:18 +01:00
timeout = dma_resv_wait_timeout ( bo - > base . resv , DMA_RESV_USAGE_BOOKKEEP ,
2021-11-09 11:08:18 +01:00
interruptible , timeout ) ;
2014-04-02 17:14:48 +02:00
if ( timeout < 0 )
return timeout ;
if ( timeout = = 0 )
return - EBUSY ;
return 0 ;
2009-06-10 15:20:19 +02:00
}
EXPORT_SYMBOL ( ttm_bo_wait ) ;
2020-10-06 13:35:32 +02:00
int ttm_bo_swapout ( struct ttm_buffer_object * bo , struct ttm_operation_ctx * ctx ,
gfp_t gfp_flags )
2009-06-10 15:20:19 +02:00
{
2021-06-02 10:38:14 +02:00
struct ttm_place place ;
2017-12-21 17:42:53 +08:00
bool locked ;
2020-10-06 13:35:32 +02:00
int ret ;
2019-11-11 14:42:13 +01:00
2021-06-02 10:38:14 +02:00
/*
* While the bo may already reside in SYSTEM placement , set
* SYSTEM as new placement to cover also the move further below .
* The driver may use the fact that we ' re moving from SYSTEM
* as an indication that we ' re about to swap out .
*/
memset ( & place , 0 , sizeof ( place ) ) ;
2021-12-02 11:29:08 +01:00
place . mem_type = bo - > resource - > mem_type ;
2021-06-02 10:38:14 +02:00
if ( ! ttm_bo_evict_swapout_allowable ( bo , ctx , & place , & locked , NULL ) )
2020-10-06 13:35:32 +02:00
return - EBUSY ;
2012-11-29 11:36:54 +00:00
2021-05-28 14:34:38 +02:00
if ( ! bo - > ttm | | ! ttm_tt_is_populated ( bo - > ttm ) | |
2021-09-29 14:26:27 +01:00
bo - > ttm - > page_flags & TTM_TT_FLAG_EXTERNAL | |
bo - > ttm - > page_flags & TTM_TT_FLAG_SWAPPED | |
2021-05-28 14:34:38 +02:00
! ttm_bo_get_unless_zero ( bo ) ) {
2020-10-06 13:35:32 +02:00
if ( locked )
dma_resv_unlock ( bo - > base . resv ) ;
return - EBUSY ;
2012-11-28 11:25:42 +00:00
}
2010-10-19 09:01:01 +02:00
2019-11-11 14:42:13 +01:00
if ( bo - > deleted ) {
2021-09-07 12:08:32 +08:00
ret = ttm_bo_cleanup_refs ( bo , false , false , locked ) ;
2019-11-11 14:42:13 +01:00
ttm_bo_put ( bo ) ;
2021-09-07 12:08:32 +08:00
return ret = = - EBUSY ? - ENOSPC : ret ;
2009-06-10 15:20:19 +02:00
}
2020-10-06 13:35:32 +02:00
/* TODO: Cleanup the locking */
2020-10-06 17:26:42 +02:00
spin_unlock ( & bo - > bdev - > lru_lock ) ;
2009-06-10 15:20:19 +02:00
2020-10-06 13:35:32 +02:00
/*
2016-06-06 10:17:57 +02:00
* Move to system cached
2009-06-10 15:20:19 +02:00
*/
2021-04-12 15:11:47 +02:00
if ( bo - > resource - > mem_type ! = TTM_PL_SYSTEM ) {
2017-04-12 16:48:39 +02:00
struct ttm_operation_ctx ctx = { false , false } ;
2021-04-15 09:52:58 +02:00
struct ttm_resource * evict_mem ;
2021-06-02 10:38:14 +02:00
struct ttm_place hop ;
2020-10-29 13:58:52 +10:00
memset ( & hop , 0 , sizeof ( hop ) ) ;
2021-12-02 11:29:08 +01:00
place . mem_type = TTM_PL_SYSTEM ;
2021-04-13 20:17:51 +02:00
ret = ttm_resource_alloc ( bo , & place , & evict_mem ) ;
if ( unlikely ( ret ) )
goto out ;
2009-06-10 15:20:19 +02:00
2021-04-15 09:52:58 +02:00
ret = ttm_bo_handle_move_mem ( bo , evict_mem , true , & ctx , & hop ) ;
2020-10-29 13:58:52 +10:00
if ( unlikely ( ret ! = 0 ) ) {
WARN ( ret = = - EMULTIHOP , " Unexpected multihop in swaput - likely driver bug. \n " ) ;
2009-06-10 15:20:19 +02:00
goto out ;
2020-10-29 13:58:52 +10:00
}
2009-06-10 15:20:19 +02:00
}
2020-10-06 13:35:32 +02:00
/*
2016-06-06 10:17:57 +02:00
* Make sure BO is idle .
*/
ret = ttm_bo_wait ( bo , false , false ) ;
if ( unlikely ( ret ! = 0 ) )
goto out ;
2009-06-10 15:20:19 +02:00
ttm_bo_unmap_virtual ( bo ) ;
2020-10-06 13:35:32 +02:00
/*
2009-06-10 15:20:19 +02:00
* Swap out . Buffer will be swapped in again as soon as
* anyone tries to access a ttm page .
*/
2020-10-01 14:51:40 +02:00
if ( bo - > bdev - > funcs - > swap_notify )
bo - > bdev - > funcs - > swap_notify ( bo ) ;
2010-01-13 22:28:40 +01:00
2021-06-02 10:38:14 +02:00
if ( ttm_tt_is_populated ( bo - > ttm ) )
ret = ttm_tt_swapout ( bo - > bdev , bo - > ttm , gfp_flags ) ;
2009-06-10 15:20:19 +02:00
out :
2020-10-06 13:35:32 +02:00
/*
2009-06-10 15:20:19 +02:00
* Unreserve without putting on LRU to avoid swapping out an
* already swapped buffer .
*/
2018-01-17 23:54:07 -05:00
if ( locked )
2019-08-11 10:06:32 +02:00
dma_resv_unlock ( bo - > base . resv ) ;
2019-11-11 14:42:13 +01:00
ttm_bo_put ( bo ) ;
2021-09-07 12:08:32 +08:00
return ret = = - EBUSY ? - ENOSPC : ret ;
2009-06-10 15:20:19 +02:00
}
2020-09-15 11:02:12 +10:00
void ttm_bo_tt_destroy ( struct ttm_buffer_object * bo )
{
2020-09-15 11:04:08 +10:00
if ( bo - > ttm = = NULL )
return ;
2020-09-15 11:34:51 +10:00
2021-07-28 15:05:52 +02:00
ttm_tt_unpopulate ( bo - > bdev , bo - > ttm ) ;
2020-09-15 11:02:12 +10:00
ttm_tt_destroy ( bo - > bdev , bo - > ttm ) ;
bo - > ttm = NULL ;
}