8bb61dd26437912d828facac30c8a88a7cc1e1da
[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         ttm_mem_global_release(&ttm_mem_glob);
53         __free_page(glob->dummy_read_page);
54         memset(glob, 0, sizeof(*glob));
55 out:
56         mutex_unlock(&ttm_global_mutex);
57 }
58
59 static int ttm_global_init(void)
60 {
61         struct ttm_global *glob = &ttm_glob;
62         int ret = 0;
63         unsigned i;
64
65         mutex_lock(&ttm_global_mutex);
66         if (++ttm_glob_use_count > 1)
67                 goto out;
68
69         ret = ttm_mem_global_init(&ttm_mem_glob);
70         if (ret)
71                 goto out;
72
73         spin_lock_init(&glob->lru_lock);
74         glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32);
75
76         if (unlikely(glob->dummy_read_page == NULL)) {
77                 ret = -ENOMEM;
78                 goto out;
79         }
80
81         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
82                 INIT_LIST_HEAD(&glob->swap_lru[i]);
83         INIT_LIST_HEAD(&glob->device_list);
84         atomic_set(&glob->bo_count, 0);
85
86         debugfs_create_atomic_t("buffer_objects", 0444, ttm_debugfs_root,
87                                 &glob->bo_count);
88 out:
89         mutex_unlock(&ttm_global_mutex);
90         return ret;
91 }
92
93 static void ttm_init_sysman(struct ttm_device *bdev)
94 {
95         struct ttm_resource_manager *man = &bdev->sysman;
96
97         /*
98          * Initialize the system memory buffer type.
99          * Other types need to be driver / IOCTL initialized.
100          */
101         man->use_tt = true;
102
103         ttm_resource_manager_init(man, 0);
104         ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, man);
105         ttm_resource_manager_set_used(man, true);
106 }
107
108 static void ttm_device_delayed_workqueue(struct work_struct *work)
109 {
110         struct ttm_device *bdev =
111                 container_of(work, struct ttm_device, wq.work);
112
113         if (!ttm_bo_delayed_delete(bdev, false))
114                 schedule_delayed_work(&bdev->wq,
115                                       ((HZ / 100) < 1) ? 1 : HZ / 100);
116 }
117
118 /**
119  * ttm_device_init
120  *
121  * @bdev: A pointer to a struct ttm_device to initialize.
122  * @funcs: Function table for the device.
123  * @dev: The core kernel device pointer for DMA mappings and allocations.
124  * @mapping: The address space to use for this bo.
125  * @vma_manager: A pointer to a vma manager.
126  * @use_dma_alloc: If coherent DMA allocation API should be used.
127  * @use_dma32: If we should use GFP_DMA32 for device memory allocations.
128  *
129  * Initializes a struct ttm_device:
130  * Returns:
131  * !0: Failure.
132  */
133 int ttm_device_init(struct ttm_device *bdev, struct ttm_device_funcs *funcs,
134                     struct device *dev, struct address_space *mapping,
135                     struct drm_vma_offset_manager *vma_manager,
136                     bool use_dma_alloc, bool use_dma32)
137 {
138         struct ttm_global *glob = &ttm_glob;
139         int ret;
140
141         if (WARN_ON(vma_manager == NULL))
142                 return -EINVAL;
143
144         ret = ttm_global_init();
145         if (ret)
146                 return ret;
147
148         bdev->funcs = funcs;
149
150         ttm_init_sysman(bdev);
151         ttm_pool_init(&bdev->pool, dev, use_dma_alloc, use_dma32);
152
153         bdev->vma_manager = vma_manager;
154         INIT_DELAYED_WORK(&bdev->wq, ttm_device_delayed_workqueue);
155         INIT_LIST_HEAD(&bdev->ddestroy);
156         bdev->dev_mapping = mapping;
157         mutex_lock(&ttm_global_mutex);
158         list_add_tail(&bdev->device_list, &glob->device_list);
159         mutex_unlock(&ttm_global_mutex);
160
161         return 0;
162 }
163 EXPORT_SYMBOL(ttm_device_init);
164
165 void ttm_device_fini(struct ttm_device *bdev)
166 {
167         struct ttm_global *glob = &ttm_glob;
168         struct ttm_resource_manager *man;
169         unsigned i;
170
171         man = ttm_manager_type(bdev, TTM_PL_SYSTEM);
172         ttm_resource_manager_set_used(man, false);
173         ttm_set_driver_manager(bdev, TTM_PL_SYSTEM, NULL);
174
175         mutex_lock(&ttm_global_mutex);
176         list_del(&bdev->device_list);
177         mutex_unlock(&ttm_global_mutex);
178
179         cancel_delayed_work_sync(&bdev->wq);
180
181         if (ttm_bo_delayed_delete(bdev, true))
182                 pr_debug("Delayed destroy list was clean\n");
183
184         spin_lock(&glob->lru_lock);
185         for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i)
186                 if (list_empty(&man->lru[0]))
187                         pr_debug("Swap list %d was clean\n", i);
188         spin_unlock(&glob->lru_lock);
189
190         ttm_pool_fini(&bdev->pool);
191         ttm_global_release();
192 }
193 EXPORT_SYMBOL(ttm_device_fini);