1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
4 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5 * Copyright 2020 Advanced Micro Devices, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
25 * Authors: Christian König
28 #define pr_fmt(fmt) "[TTM DEVICE] " fmt
30 #include <drm/ttm/ttm_device.h>
31 #include <drm/ttm/ttm_memory.h>
32 #include <drm/ttm/ttm_placement.h>
34 #include "ttm_module.h"
37 * ttm_global_mutex - protecting the global state
39 DEFINE_MUTEX(ttm_global_mutex);
40 unsigned ttm_glob_use_count;
41 struct ttm_global ttm_glob;
42 EXPORT_SYMBOL(ttm_glob);
44 static void ttm_global_release(void)
46 struct ttm_global *glob = &ttm_glob;
48 mutex_lock(&ttm_global_mutex);
49 if (--ttm_glob_use_count > 0)
52 kobject_del(&glob->kobj);
53 kobject_put(&glob->kobj);
54 ttm_mem_global_release(&ttm_mem_glob);
55 __free_page(glob->dummy_read_page);
56 memset(glob, 0, sizeof(*glob));
58 mutex_unlock(&ttm_global_mutex);
61 static int ttm_global_init(void)
63 struct ttm_global *glob = &ttm_glob;
67 mutex_lock(&ttm_global_mutex);
68 if (++ttm_glob_use_count > 1)
71 ret = ttm_mem_global_init(&ttm_mem_glob);
75 spin_lock_init(&glob->lru_lock);
76 glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
78 if (unlikely(glob->dummy_read_page == NULL)) {
83 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
84 INIT_LIST_HEAD(&glob->swap_lru[i]);
85 INIT_LIST_HEAD(&glob->device_list);
86 atomic_set(&glob->bo_count, 0);
88 debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
91 mutex_unlock(&ttm_global_mutex);
95 static void ttm_init_sysman(struct ttm_device *bdev)
97 struct ttm_resource_manager *man = &bdev->sysman;
100 * Initialize the system memory buffer type.
101 * Other types need to be driver / IOCTL initialized.
105 ttm_resource_manager_init(man, 0);
106 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
107 ttm_resource_manager_set_used(man, true);
110 static void ttm_device_delayed_workqueue(struct work_struct *work)
112 struct ttm_device *bdev =
113 container_of(work, struct ttm_device, wq.work);
115 if (!ttm_bo_delayed_delete(bdev, false))
116 schedule_delayed_work(&bdev->wq,
117 ((HZ / 100) < 1) ? 1 : HZ / 100);
123 * @bdev: A pointer to a struct ttm_device to initialize.
124 * @funcs: Function table for the device.
125 * @dev: The core kernel device pointer for DMA mappings and allocations.
126 * @mapping: The address space to use for this bo.
127 * @vma_manager: A pointer to a vma manager.
128 * @use_dma_alloc: If coherent DMA allocation API should be used.
129 * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
131 * Initializes a struct ttm_device:
135 int ttm_device_init(struct ttm_device *bdev, struct ttm_device_funcs *funcs,
136 struct device *dev, struct address_space *mapping,
137 struct drm_vma_offset_manager *vma_manager,
138 bool use_dma_alloc, bool use_dma32)
140 struct ttm_global *glob = &ttm_glob;
143 if (WARN_ON(vma_manager == NULL))
146 ret = ttm_global_init();
152 ttm_init_sysman(bdev);
153 ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
155 bdev->vma_manager = vma_manager;
156 INIT_DELAYED_WORK(&bdev->wq, ttm_device_delayed_workqueue);
157 INIT_LIST_HEAD(&bdev->ddestroy);
158 bdev->dev_mapping = mapping;
159 mutex_lock(&ttm_global_mutex);
160 list_add_tail(&bdev->device_list, &glob->device_list);
161 mutex_unlock(&ttm_global_mutex);
165 EXPORT_SYMBOL(ttm_device_init);
167 void ttm_device_fini(struct ttm_device *bdev)
169 struct ttm_global *glob = &ttm_glob;
170 struct ttm_resource_manager *man;
173 man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
174 ttm_resource_manager_set_used(man, false);
175 ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
177 mutex_lock(&ttm_global_mutex);
178 list_del(&bdev->device_list);
179 mutex_unlock(&ttm_global_mutex);
181 cancel_delayed_work_sync(&bdev->wq);
183 if (ttm_bo_delayed_delete(bdev, true))
184 pr_debug("Delayed destroy list was clean\n");
186 spin_lock(&glob->lru_lock);
187 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
188 if (list_empty(&man->lru[0]))
189 pr_debug("Swap list %d was clean\n", i);
190 spin_unlock(&glob->lru_lock);
192 ttm_pool_fini(&bdev->pool);
193 ttm_global_release();
195 EXPORT_SYMBOL(ttm_device_fini);