Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2/**************************************************************************
3 *
4 * Copyright (c) 2007-2010 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28/*
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 */
31
32#include <drm/ttm/ttm_device.h>
33#include <drm/ttm/ttm_placement.h>
34#include <drm/ttm/ttm_range_manager.h>
35#include <drm/ttm/ttm_bo_api.h>
36#include <drm/drm_mm.h>
37#include <linux/slab.h>
38#include <linux/spinlock.h>
39
40/*
41 * Currently we use a spinlock for the lock, but a mutex *may* be
42 * more appropriate to reduce scheduling latency if the range manager
43 * ends up with very fragmented allocation patterns.
44 */
45
46struct ttm_range_manager {
47 struct ttm_resource_manager manager;
48 struct drm_mm mm;
49 spinlock_t lock;
50};
51
52static inline struct ttm_range_manager *
53to_range_manager(struct ttm_resource_manager *man)
54{
55 return container_of(man, struct ttm_range_manager, manager);
56}
57
58static int ttm_range_man_alloc(struct ttm_resource_manager *man,
59 struct ttm_buffer_object *bo,
60 const struct ttm_place *place,
61 struct ttm_resource **res)
62{
63 struct ttm_range_manager *rman = to_range_manager(man);
64 struct ttm_range_mgr_node *node;
65 struct drm_mm *mm = &rman->mm;
66 enum drm_mm_insert_mode mode;
67 unsigned long lpfn;
68 int ret;
69
70 lpfn = place->lpfn;
71 if (!lpfn)
72 lpfn = man->size;
73
74 node = kzalloc(struct_size(node, mm_nodes, 1), GFP_KERNEL);
75 if (!node)
76 return -ENOMEM;
77
78 mode = DRM_MM_INSERT_BEST;
79 if (place->flags & TTM_PL_FLAG_TOPDOWN)
80 mode = DRM_MM_INSERT_HIGH;
81
82 ttm_resource_init(bo, place, &node->base);
83
84 spin_lock(&rman->lock);
85 ret = drm_mm_insert_node_in_range(mm, &node->mm_nodes[0],
86 node->base.num_pages,
87 bo->page_alignment, 0,
88 place->fpfn, lpfn, mode);
89 spin_unlock(&rman->lock);
90
91 if (unlikely(ret)) {
92 kfree(node);
93 return ret;
94 }
95
96 node->base.start = node->mm_nodes[0].start;
97 *res = &node->base;
98 return 0;
99}
100
101static void ttm_range_man_free(struct ttm_resource_manager *man,
102 struct ttm_resource *res)
103{
104 struct ttm_range_mgr_node *node = to_ttm_range_mgr_node(res);
105 struct ttm_range_manager *rman = to_range_manager(man);
106
107 spin_lock(&rman->lock);
108 drm_mm_remove_node(&node->mm_nodes[0]);
109 spin_unlock(&rman->lock);
110
111 kfree(node);
112}
113
114static void ttm_range_man_debug(struct ttm_resource_manager *man,
115 struct drm_printer *printer)
116{
117 struct ttm_range_manager *rman = to_range_manager(man);
118
119 spin_lock(&rman->lock);
120 drm_mm_print(&rman->mm, printer);
121 spin_unlock(&rman->lock);
122}
123
124static const struct ttm_resource_manager_func ttm_range_manager_func = {
125 .alloc = ttm_range_man_alloc,
126 .free = ttm_range_man_free,
127 .debug = ttm_range_man_debug
128};
129
130/**
131 * ttm_range_man_init_nocheck - Initialise a generic range manager for the
132 * selected memory type.
133 *
134 * @bdev: ttm device
135 * @type: memory manager type
136 * @use_tt: if the memory manager uses tt
137 * @p_size: size of area to be managed in pages.
138 *
139 * The range manager is installed for this device in the type slot.
140 *
141 * Return: %0 on success or a negative error code on failure
142 */
143int ttm_range_man_init_nocheck(struct ttm_device *bdev,
144 unsigned type, bool use_tt,
145 unsigned long p_size)
146{
147 struct ttm_resource_manager *man;
148 struct ttm_range_manager *rman;
149
150 rman = kzalloc(sizeof(*rman), GFP_KERNEL);
151 if (!rman)
152 return -ENOMEM;
153
154 man = &rman->manager;
155 man->use_tt = use_tt;
156
157 man->func = &ttm_range_manager_func;
158
159 ttm_resource_manager_init(man, p_size);
160
161 drm_mm_init(&rman->mm, 0, p_size);
162 spin_lock_init(&rman->lock);
163
164 ttm_set_driver_manager(bdev, type, &rman->manager);
165 ttm_resource_manager_set_used(man, true);
166 return 0;
167}
168EXPORT_SYMBOL(ttm_range_man_init_nocheck);
169
170/**
171 * ttm_range_man_fini_nocheck - Remove the generic range manager from a slot
172 * and tear it down.
173 *
174 * @bdev: ttm device
175 * @type: memory manager type
176 *
177 * Return: %0 on success or a negative error code on failure
178 */
179int ttm_range_man_fini_nocheck(struct ttm_device *bdev,
180 unsigned type)
181{
182 struct ttm_resource_manager *man = ttm_manager_type(bdev, type);
183 struct ttm_range_manager *rman = to_range_manager(man);
184 struct drm_mm *mm = &rman->mm;
185 int ret;
186
187 if (!man)
188 return 0;
189
190 ttm_resource_manager_set_used(man, false);
191
192 ret = ttm_resource_manager_evict_all(bdev, man);
193 if (ret)
194 return ret;
195
196 spin_lock(&rman->lock);
197 drm_mm_clean(mm);
198 drm_mm_takedown(mm);
199 spin_unlock(&rman->lock);
200
201 ttm_resource_manager_cleanup(man);
202 ttm_set_driver_manager(bdev, type, NULL);
203 kfree(rman);
204 return 0;
205}
206EXPORT_SYMBOL(ttm_range_man_fini_nocheck);