drm/ttm: device naming cleanup
[linux-2.6-microblaze.git] / drivers / gpu / drm / ttm / ttm_device.c
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2
3 /*
4  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
5  * Copyright 2020 Advanced Micro Devices, Inc.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  *
25  * Authors: Christian König
26  */
27
28 #define pr_fmt(fmt) "[TTM DEVICE] " fmt
29
30 #include <drm/ttm/ttm_device.h>
31 #include <drm/ttm/ttm_memory.h>
32 #include <drm/ttm/ttm_placement.h>
33
34 #include "ttm_module.h"
35
36 /**
37  * ttm_global_mutex - protecting the global state
38  */
39 DEFINE_MUTEX(ttm_global_mutex);
40 unsigned ttm_glob_use_count;
41 struct ttm_global ttm_glob;
42 EXPORT_SYMBOL(ttm_glob);
43
44 static void ttm_global_release(void)
45 {
46         struct ttm_global *glob = &ttm_glob;
47
48         mutex_lock(&ttm_global_mutex);
49         if (--ttm_glob_use_count > 0)
50                 goto out;
51
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));
57 out:
58         mutex_unlock(&ttm_global_mutex);
59 }
60
61 static int ttm_global_init(void)
62 {
63         struct ttm_global *glob = &ttm_glob;
64         int ret = 0;
65         unsigned i;
66
67         mutex_lock(&ttm_global_mutex);
68         if (++ttm_glob_use_count > 1)
69                 goto out;
70
71         ret = ttm_mem_global_init(&ttm_mem_glob);
72         if (ret)
73                 goto out;
74
75         spin_lock_init(&glob->lru_lock);
76         glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
77
78         if (unlikely(glob->dummy_read_page == NULL)) {
79                 ret = -ENOMEM;
80                 goto out;
81         }
82
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);
87
88         debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
89                                 &glob->bo_count);
90 out:
91         mutex_unlock(&ttm_global_mutex);
92         return ret;
93 }
94
95 static void ttm_init_sysman(struct ttm_device *bdev)
96 {
97         struct ttm_resource_manager *man = &bdev->sysman;
98
99         /*
100          * Initialize the system memory buffer type.
101          * Other types need to be driver / IOCTL initialized.
102          */
103         man->use_tt = true;
104
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);
108 }
109
110 static void ttm_device_delayed_workqueue(struct work_struct *work)
111 {
112         struct ttm_device *bdev =
113                 container_of(work, struct ttm_device, wq.work);
114
115         if (!ttm_bo_delayed_delete(bdev, false))
116                 schedule_delayed_work(&bdev->wq,
117                                       ((HZ / 100) < 1) ? 1 : HZ / 100);
118 }
119
120 /**
121  * ttm_device_init
122  *
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.
130  *
131  * Initializes a struct ttm_device:
132  * Returns:
133  * !0: Failure.
134  */
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)
139 {
140         struct ttm_global *glob = &ttm_glob;
141         int ret;
142
143         if (WARN_ON(vma_manager == NULL))
144                 return -EINVAL;
145
146         ret = ttm_global_init();
147         if (ret)
148                 return ret;
149
150         bdev->funcs = funcs;
151
152         ttm_init_sysman(bdev);
153         ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
154
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);
162
163         return 0;
164 }
165 EXPORT_SYMBOL(ttm_device_init);
166
167 void ttm_device_fini(struct ttm_device *bdev)
168 {
169         struct ttm_global *glob = &ttm_glob;
170         struct ttm_resource_manager *man;
171         unsigned i;
172
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);
176
177         mutex_lock(&ttm_global_mutex);
178         list_del(&bdev->device_list);
179         mutex_unlock(&ttm_global_mutex);
180
181         cancel_delayed_work_sync(&bdev->wq);
182
183         if (ttm_bo_delayed_delete(bdev, true))
184                 pr_debug("Delayed destroy list was clean\n");
185
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);
191
192         ttm_pool_fini(&bdev->pool);
193         ttm_global_release();
194 }
195 EXPORT_SYMBOL(ttm_device_fini);