15ca02815Sjsg /* SPDX-License-Identifier: GPL-2.0 OR MIT */
25ca02815Sjsg
35ca02815Sjsg /*
45ca02815Sjsg * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
55ca02815Sjsg * Copyright 2020 Advanced Micro Devices, Inc.
65ca02815Sjsg *
75ca02815Sjsg * Permission is hereby granted, free of charge, to any person obtaining a
85ca02815Sjsg * copy of this software and associated documentation files (the "Software"),
95ca02815Sjsg * to deal in the Software without restriction, including without limitation
105ca02815Sjsg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
115ca02815Sjsg * and/or sell copies of the Software, and to permit persons to whom the
125ca02815Sjsg * Software is furnished to do so, subject to the following conditions:
135ca02815Sjsg *
145ca02815Sjsg * The above copyright notice and this permission notice shall be included in
155ca02815Sjsg * all copies or substantial portions of the Software.
165ca02815Sjsg *
175ca02815Sjsg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
185ca02815Sjsg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
195ca02815Sjsg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
205ca02815Sjsg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
215ca02815Sjsg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
225ca02815Sjsg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
235ca02815Sjsg * OTHER DEALINGS IN THE SOFTWARE.
245ca02815Sjsg *
255ca02815Sjsg * Authors: Christian König
265ca02815Sjsg */
275ca02815Sjsg
285ca02815Sjsg #define pr_fmt(fmt) "[TTM DEVICE] " fmt
295ca02815Sjsg
305ca02815Sjsg #include <linux/mm.h>
315ca02815Sjsg
32*f005ef32Sjsg #include <drm/ttm/ttm_bo.h>
335ca02815Sjsg #include <drm/ttm/ttm_device.h>
345ca02815Sjsg #include <drm/ttm/ttm_tt.h>
355ca02815Sjsg #include <drm/ttm/ttm_placement.h>
365ca02815Sjsg
375ca02815Sjsg #include "ttm_module.h"
385ca02815Sjsg
395ca02815Sjsg /*
405ca02815Sjsg * ttm_global_mutex - protecting the global state
415ca02815Sjsg */
425ca02815Sjsg static DEFINE_MUTEX(ttm_global_mutex);
435ca02815Sjsg static unsigned ttm_glob_use_count;
445ca02815Sjsg struct ttm_global ttm_glob;
455ca02815Sjsg EXPORT_SYMBOL(ttm_glob);
465ca02815Sjsg
475ca02815Sjsg struct dentry *ttm_debugfs_root;
485ca02815Sjsg
ttm_global_release(void)495ca02815Sjsg static void ttm_global_release(void)
505ca02815Sjsg {
515ca02815Sjsg struct ttm_global *glob = &ttm_glob;
525ca02815Sjsg
535ca02815Sjsg mutex_lock(&ttm_global_mutex);
545ca02815Sjsg if (--ttm_glob_use_count > 0)
555ca02815Sjsg goto out;
565ca02815Sjsg
575ca02815Sjsg ttm_pool_mgr_fini();
585ca02815Sjsg debugfs_remove(ttm_debugfs_root);
595ca02815Sjsg
605ca02815Sjsg __free_page(glob->dummy_read_page);
615ca02815Sjsg memset(glob, 0, sizeof(*glob));
625ca02815Sjsg out:
635ca02815Sjsg mutex_unlock(&ttm_global_mutex);
645ca02815Sjsg }
655ca02815Sjsg
ttm_global_init(void)665ca02815Sjsg static int ttm_global_init(void)
675ca02815Sjsg {
685ca02815Sjsg struct ttm_global *glob = &ttm_glob;
695ca02815Sjsg unsigned long num_pages, num_dma32;
705ca02815Sjsg int ret = 0;
715ca02815Sjsg
725ca02815Sjsg mutex_lock(&ttm_global_mutex);
735ca02815Sjsg if (++ttm_glob_use_count > 1)
745ca02815Sjsg goto out;
755ca02815Sjsg
765ca02815Sjsg ttm_debugfs_root = debugfs_create_dir("ttm", NULL);
775ca02815Sjsg if (IS_ERR(ttm_debugfs_root)) {
785ca02815Sjsg ttm_debugfs_root = NULL;
795ca02815Sjsg }
805ca02815Sjsg
815ca02815Sjsg /* Limit the number of pages in the pool to about 50% of the total
825ca02815Sjsg * system memory.
835ca02815Sjsg */
845ca02815Sjsg num_pages = physmem;
855ca02815Sjsg num_pages /= 2;
865ca02815Sjsg
875ca02815Sjsg /* But for DMA32 we limit ourself to only use 2GiB maximum. */
885ca02815Sjsg num_dma32 = physmem;
895ca02815Sjsg num_dma32 = min(num_dma32, 2UL << (30 - PAGE_SHIFT));
905ca02815Sjsg
915ca02815Sjsg ttm_pool_mgr_init(num_pages);
925ca02815Sjsg ttm_tt_mgr_init(num_pages, num_dma32);
935ca02815Sjsg
945ca02815Sjsg glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
955ca02815Sjsg
965ca02815Sjsg if (unlikely(glob->dummy_read_page == NULL)) {
975ca02815Sjsg ret = -ENOMEM;
985ca02815Sjsg goto out;
995ca02815Sjsg }
1005ca02815Sjsg
1015ca02815Sjsg INIT_LIST_HEAD(&glob->device_list);
1025ca02815Sjsg atomic_set(&glob->bo_count, 0);
1035ca02815Sjsg
1045ca02815Sjsg debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
1055ca02815Sjsg &glob->bo_count);
1065ca02815Sjsg out:
1075ca02815Sjsg if (ret && ttm_debugfs_root)
1085ca02815Sjsg debugfs_remove(ttm_debugfs_root);
1095ca02815Sjsg if (ret)
1105ca02815Sjsg --ttm_glob_use_count;
1115ca02815Sjsg mutex_unlock(&ttm_global_mutex);
1125ca02815Sjsg return ret;
1135ca02815Sjsg }
1145ca02815Sjsg
1155ca02815Sjsg /*
1165ca02815Sjsg * A buffer object shrink method that tries to swap out the first
1175ca02815Sjsg * buffer object on the global::swap_lru list.
1185ca02815Sjsg */
ttm_global_swapout(struct ttm_operation_ctx * ctx,gfp_t gfp_flags)1195ca02815Sjsg int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
1205ca02815Sjsg {
1215ca02815Sjsg struct ttm_global *glob = &ttm_glob;
1225ca02815Sjsg struct ttm_device *bdev;
1235ca02815Sjsg int ret = 0;
1245ca02815Sjsg
1255ca02815Sjsg mutex_lock(&ttm_global_mutex);
1265ca02815Sjsg list_for_each_entry(bdev, &glob->device_list, device_list) {
1275ca02815Sjsg ret = ttm_device_swapout(bdev, ctx, gfp_flags);
1285ca02815Sjsg if (ret > 0) {
1295ca02815Sjsg list_move_tail(&bdev->device_list, &glob->device_list);
1305ca02815Sjsg break;
1315ca02815Sjsg }
1325ca02815Sjsg }
1335ca02815Sjsg mutex_unlock(&ttm_global_mutex);
1345ca02815Sjsg return ret;
1355ca02815Sjsg }
1365ca02815Sjsg
ttm_device_swapout(struct ttm_device * bdev,struct ttm_operation_ctx * ctx,gfp_t gfp_flags)1375ca02815Sjsg int ttm_device_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
1385ca02815Sjsg gfp_t gfp_flags)
1395ca02815Sjsg {
1401bb76ff1Sjsg struct ttm_resource_cursor cursor;
1415ca02815Sjsg struct ttm_resource_manager *man;
1421bb76ff1Sjsg struct ttm_resource *res;
1431bb76ff1Sjsg unsigned i;
1445ca02815Sjsg int ret;
1455ca02815Sjsg
1465ca02815Sjsg spin_lock(&bdev->lru_lock);
1475ca02815Sjsg for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
1485ca02815Sjsg man = ttm_manager_type(bdev, i);
1495ca02815Sjsg if (!man || !man->use_tt)
1505ca02815Sjsg continue;
1515ca02815Sjsg
1521bb76ff1Sjsg ttm_resource_manager_for_each_res(man, &cursor, res) {
1531bb76ff1Sjsg struct ttm_buffer_object *bo = res->bo;
1541bb76ff1Sjsg uint32_t num_pages;
1555ca02815Sjsg
15652e4ee52Sjsg if (!bo || bo->resource != res)
1571bb76ff1Sjsg continue;
1581bb76ff1Sjsg
1591bb76ff1Sjsg num_pages = PFN_UP(bo->base.size);
1605ca02815Sjsg ret = ttm_bo_swapout(bo, ctx, gfp_flags);
1615ca02815Sjsg /* ttm_bo_swapout has dropped the lru_lock */
1625ca02815Sjsg if (!ret)
1635ca02815Sjsg return num_pages;
1645ca02815Sjsg if (ret != -EBUSY)
1655ca02815Sjsg return ret;
1665ca02815Sjsg }
1675ca02815Sjsg }
1685ca02815Sjsg spin_unlock(&bdev->lru_lock);
1695ca02815Sjsg return 0;
1705ca02815Sjsg }
1715ca02815Sjsg EXPORT_SYMBOL(ttm_device_swapout);
1725ca02815Sjsg
1735ca02815Sjsg /**
1745ca02815Sjsg * ttm_device_init
1755ca02815Sjsg *
1765ca02815Sjsg * @bdev: A pointer to a struct ttm_device to initialize.
1775ca02815Sjsg * @funcs: Function table for the device.
1785ca02815Sjsg * @dev: The core kernel device pointer for DMA mappings and allocations.
1795ca02815Sjsg * @mapping: The address space to use for this bo.
1805ca02815Sjsg * @vma_manager: A pointer to a vma manager.
1815ca02815Sjsg * @use_dma_alloc: If coherent DMA allocation API should be used.
1825ca02815Sjsg * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
1835ca02815Sjsg *
1845ca02815Sjsg * Initializes a struct ttm_device:
1855ca02815Sjsg * Returns:
1865ca02815Sjsg * !0: Failure.
1875ca02815Sjsg */
ttm_device_init(struct ttm_device * bdev,const struct ttm_device_funcs * funcs,struct device * dev,struct address_space * mapping,struct drm_vma_offset_manager * vma_manager,bool use_dma_alloc,bool use_dma32)188*f005ef32Sjsg int ttm_device_init(struct ttm_device *bdev, const struct ttm_device_funcs *funcs,
1895ca02815Sjsg struct device *dev, struct address_space *mapping,
1905ca02815Sjsg struct drm_vma_offset_manager *vma_manager,
1915ca02815Sjsg bool use_dma_alloc, bool use_dma32)
1925ca02815Sjsg {
1935ca02815Sjsg struct ttm_global *glob = &ttm_glob;
1945ca02815Sjsg int ret;
1955ca02815Sjsg
1965ca02815Sjsg if (WARN_ON(vma_manager == NULL))
1975ca02815Sjsg return -EINVAL;
1985ca02815Sjsg
1995ca02815Sjsg ret = ttm_global_init();
2005ca02815Sjsg if (ret)
2015ca02815Sjsg return ret;
2025ca02815Sjsg
203*f005ef32Sjsg bdev->wq = alloc_workqueue("ttm", WQ_MEM_RECLAIM | WQ_HIGHPRI, 16);
204*f005ef32Sjsg if (!bdev->wq) {
205*f005ef32Sjsg ttm_global_release();
206*f005ef32Sjsg return -ENOMEM;
207*f005ef32Sjsg }
208*f005ef32Sjsg
2095ca02815Sjsg bdev->funcs = funcs;
2105ca02815Sjsg
2115ca02815Sjsg ttm_sys_man_init(bdev);
212*f005ef32Sjsg ttm_pool_init(&bdev->pool, dev, NUMA_NO_NODE, use_dma_alloc, use_dma32);
2135ca02815Sjsg
2145ca02815Sjsg bdev->vma_manager = vma_manager;
2155ca02815Sjsg mtx_init(&bdev->lru_lock, IPL_NONE);
2161bb76ff1Sjsg INIT_LIST_HEAD(&bdev->pinned);
2175ca02815Sjsg bdev->dev_mapping = mapping;
2185ca02815Sjsg mutex_lock(&ttm_global_mutex);
2195ca02815Sjsg list_add_tail(&bdev->device_list, &glob->device_list);
2205ca02815Sjsg mutex_unlock(&ttm_global_mutex);
2215ca02815Sjsg
2225ca02815Sjsg return 0;
2235ca02815Sjsg }
2245ca02815Sjsg EXPORT_SYMBOL(ttm_device_init);
2255ca02815Sjsg
ttm_device_fini(struct ttm_device * bdev)2265ca02815Sjsg void ttm_device_fini(struct ttm_device *bdev)
2275ca02815Sjsg {
2285ca02815Sjsg struct ttm_resource_manager *man;
2295ca02815Sjsg unsigned i;
2305ca02815Sjsg
2315ca02815Sjsg mutex_lock(&ttm_global_mutex);
2325ca02815Sjsg list_del(&bdev->device_list);
2335ca02815Sjsg mutex_unlock(&ttm_global_mutex);
2345ca02815Sjsg
235*f005ef32Sjsg drain_workqueue(bdev->wq);
236*f005ef32Sjsg destroy_workqueue(bdev->wq);
2375ca02815Sjsg
238b0170149Sjsg man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
239b0170149Sjsg ttm_resource_manager_set_used(man, false);
240b0170149Sjsg ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
241b0170149Sjsg
2425ca02815Sjsg spin_lock(&bdev->lru_lock);
2435ca02815Sjsg for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
2445ca02815Sjsg if (list_empty(&man->lru[0]))
2455ca02815Sjsg pr_debug("Swap list %d was clean\n", i);
2465ca02815Sjsg spin_unlock(&bdev->lru_lock);
2475ca02815Sjsg
2485ca02815Sjsg ttm_pool_fini(&bdev->pool);
2495ca02815Sjsg ttm_global_release();
2505ca02815Sjsg }
2515ca02815Sjsg EXPORT_SYMBOL(ttm_device_fini);
2521bb76ff1Sjsg
ttm_device_clear_lru_dma_mappings(struct ttm_device * bdev,struct list_head * list)2531bb76ff1Sjsg static void ttm_device_clear_lru_dma_mappings(struct ttm_device *bdev,
2541bb76ff1Sjsg struct list_head *list)
2551bb76ff1Sjsg {
2561bb76ff1Sjsg struct ttm_resource *res;
2571bb76ff1Sjsg
2581bb76ff1Sjsg spin_lock(&bdev->lru_lock);
2591bb76ff1Sjsg while ((res = list_first_entry_or_null(list, typeof(*res), lru))) {
2601bb76ff1Sjsg struct ttm_buffer_object *bo = res->bo;
2611bb76ff1Sjsg
2621bb76ff1Sjsg /* Take ref against racing releases once lru_lock is unlocked */
2631bb76ff1Sjsg if (!ttm_bo_get_unless_zero(bo))
2641bb76ff1Sjsg continue;
2651bb76ff1Sjsg
2661bb76ff1Sjsg list_del_init(&res->lru);
2671bb76ff1Sjsg spin_unlock(&bdev->lru_lock);
2681bb76ff1Sjsg
2691bb76ff1Sjsg if (bo->ttm)
2701bb76ff1Sjsg ttm_tt_unpopulate(bo->bdev, bo->ttm);
2711bb76ff1Sjsg
2721bb76ff1Sjsg ttm_bo_put(bo);
2731bb76ff1Sjsg spin_lock(&bdev->lru_lock);
2741bb76ff1Sjsg }
2751bb76ff1Sjsg spin_unlock(&bdev->lru_lock);
2761bb76ff1Sjsg }
2771bb76ff1Sjsg
ttm_device_clear_dma_mappings(struct ttm_device * bdev)2781bb76ff1Sjsg void ttm_device_clear_dma_mappings(struct ttm_device *bdev)
2791bb76ff1Sjsg {
2801bb76ff1Sjsg struct ttm_resource_manager *man;
2811bb76ff1Sjsg unsigned int i, j;
2821bb76ff1Sjsg
2831bb76ff1Sjsg ttm_device_clear_lru_dma_mappings(bdev, &bdev->pinned);
2841bb76ff1Sjsg
2851bb76ff1Sjsg for (i = TTM_PL_SYSTEM; i < TTM_NUM_MEM_TYPES; ++i) {
2861bb76ff1Sjsg man = ttm_manager_type(bdev, i);
2871bb76ff1Sjsg if (!man || !man->use_tt)
2881bb76ff1Sjsg continue;
2891bb76ff1Sjsg
2901bb76ff1Sjsg for (j = 0; j < TTM_MAX_BO_PRIORITY; ++j)
2911bb76ff1Sjsg ttm_device_clear_lru_dma_mappings(bdev, &man->lru[j]);
2921bb76ff1Sjsg }
2931bb76ff1Sjsg }
2941bb76ff1Sjsg EXPORT_SYMBOL(ttm_device_clear_dma_mappings);
295