drm/ttm: move swapout logic around v3
[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 <linux/mm.h>
31
32 #include <drm/ttm/ttm_device.h>
33 #include <drm/ttm/ttm_tt.h>
34 #include <drm/ttm/ttm_placement.h>
35 #include <drm/ttm/ttm_bo_api.h>
36
37 #include "ttm_module.h"
38
39 /**
40  * ttm_global_mutex - protecting the global state
41  */
42 DEFINE_MUTEX(ttm_global_mutex);
43 unsigned ttm_glob_use_count;
44 struct ttm_global ttm_glob;
45 EXPORT_SYMBOL(ttm_glob);
46
47 static void ttm_global_release(void)
48 {
49         struct ttm_global *glob = &ttm_glob;
50
51         mutex_lock(&ttm_global_mutex);
52         if (--ttm_glob_use_count > 0)
53                 goto out;
54
55         ttm_pool_mgr_fini();
56         ttm_tt_mgr_fini();
57
58         __free_page(glob->dummy_read_page);
59         memset(glob, 0, sizeof(*glob));
60 out:
61         mutex_unlock(&ttm_global_mutex);
62 }
63
64 static int ttm_global_init(void)
65 {
66         struct ttm_global *glob = &ttm_glob;
67         unsigned long num_pages;
68         struct sysinfo si;
69         int ret = 0;
70         unsigned i;
71
72         mutex_lock(&ttm_global_mutex);
73         if (++ttm_glob_use_count > 1)
74                 goto out;
75
76         si_meminfo(&si);
77
78         /* Limit the number of pages in the pool to about 50% of the total
79          * system memory.
80          */
81         num_pages = ((u64)si.totalram * si.mem_unit) >> PAGE_SHIFT;
82         ttm_pool_mgr_init(num_pages * 50 / 100);
83         ttm_tt_mgr_init();
84
85         spin_lock_init(&glob->lru_lock);
86         glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
87
88         if (unlikely(glob->dummy_read_page == NULL)) {
89                 ret = -ENOMEM;
90                 goto out;
91         }
92
93         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
94                 INIT_LIST_HEAD(&glob->swap_lru[i]);
95         INIT_LIST_HEAD(&glob->device_list);
96         atomic_set(&glob->bo_count, 0);
97
98         debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
99                                 &glob->bo_count);
100 out:
101         mutex_unlock(&ttm_global_mutex);
102         return ret;
103 }
104
105 /**
106  * A buffer object shrink method that tries to swap out the first
107  * buffer object on the global::swap_lru list.
108  */
109 int ttm_global_swapout(struct ttm_operation_ctx *ctx, gfp_t gfp_flags)
110 {
111         struct ttm_global *glob = &ttm_glob;
112         struct ttm_buffer_object *bo;
113         unsigned i;
114         int ret;
115
116         spin_lock(&glob->lru_lock);
117         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) {
118                 list_for_each_entry(bo, &glob->swap_lru[i], swap) {
119                         uint32_t num_pages = bo->ttm->num_pages;
120
121                         ret = ttm_bo_swapout(bo, ctx, gfp_flags);
122                         /* ttm_bo_swapout has dropped the lru_lock */
123                         if (!ret)
124                                 return num_pages;
125                         if (ret != -EBUSY)
126                                 return ret;
127                 }
128         }
129         spin_unlock(&glob->lru_lock);
130         return 0;
131 }
132 EXPORT_SYMBOL(ttm_global_swapout);
133
134 static void ttm_init_sysman(struct ttm_device *bdev)
135 {
136         struct ttm_resource_manager *man = &bdev->sysman;
137
138         /*
139          * Initialize the system memory buffer type.
140          * Other types need to be driver / IOCTL initialized.
141          */
142         man->use_tt = true;
143
144         ttm_resource_manager_init(man, 0);
145         ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
146         ttm_resource_manager_set_used(man, true);
147 }
148
149 static void ttm_device_delayed_workqueue(struct work_struct *work)
150 {
151         struct ttm_device *bdev =
152                 container_of(work, struct ttm_device, wq.work);
153
154         if (!ttm_bo_delayed_delete(bdev, false))
155                 schedule_delayed_work(&bdev->wq,
156                                       ((HZ / 100) < 1) ? 1 : HZ / 100);
157 }
158
159 /**
160  * ttm_device_init
161  *
162  * @bdev: A pointer to a struct ttm_device to initialize.
163  * @funcs: Function table for the device.
164  * @dev: The core kernel device pointer for DMA mappings and allocations.
165  * @mapping: The address space to use for this bo.
166  * @vma_manager: A pointer to a vma manager.
167  * @use_dma_alloc: If coherent DMA allocation API should be used.
168  * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
169  *
170  * Initializes a struct ttm_device:
171  * Returns:
172  * !0: Failure.
173  */
174 int ttm_device_init(struct ttm_device *bdev, struct ttm_device_funcs *funcs,
175                     struct device *dev, struct address_space *mapping,
176                     struct drm_vma_offset_manager *vma_manager,
177                     bool use_dma_alloc, bool use_dma32)
178 {
179         struct ttm_global *glob = &ttm_glob;
180         int ret;
181
182         if (WARN_ON(vma_manager == NULL))
183                 return -EINVAL;
184
185         ret = ttm_global_init();
186         if (ret)
187                 return ret;
188
189         bdev->funcs = funcs;
190
191         ttm_init_sysman(bdev);
192         ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
193
194         bdev->vma_manager = vma_manager;
195         INIT_DELAYED_WORK(&bdev->wq, ttm_device_delayed_workqueue);
196         INIT_LIST_HEAD(&bdev->ddestroy);
197         bdev->dev_mapping = mapping;
198         mutex_lock(&ttm_global_mutex);
199         list_add_tail(&bdev->device_list, &glob->device_list);
200         mutex_unlock(&ttm_global_mutex);
201
202         return 0;
203 }
204 EXPORT_SYMBOL(ttm_device_init);
205
206 void ttm_device_fini(struct ttm_device *bdev)
207 {
208         struct ttm_global *glob = &ttm_glob;
209         struct ttm_resource_manager *man;
210         unsigned i;
211
212         man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
213         ttm_resource_manager_set_used(man, false);
214         ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
215
216         mutex_lock(&ttm_global_mutex);
217         list_del(&bdev->device_list);
218         mutex_unlock(&ttm_global_mutex);
219
220         cancel_delayed_work_sync(&bdev->wq);
221
222         if (ttm_bo_delayed_delete(bdev, true))
223                 pr_debug("Delayed destroy list was clean\n");
224
225         spin_lock(&glob->lru_lock);
226         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
227                 if (list_empty(&man->lru[0]))
228                         pr_debug("Swap list %d was clean\n", i);
229         spin_unlock(&glob->lru_lock);
230
231         ttm_pool_fini(&bdev->pool);
232         ttm_global_release();
233 }
234 EXPORT_SYMBOL(ttm_device_fini);