1 // SPDX-License-Identifier: MIT
3 * Copyright © 2021 Intel Corporation
6 #include <linux/slab.h>
8 #include <drm/ttm/ttm_placement.h>
9 #include <drm/ttm/ttm_bo.h>
11 #include <drm/drm_buddy.h>
13 #include "i915_ttm_buddy_manager.h"
17 struct i915_ttm_buddy_manager {
18 struct ttm_resource_manager manager;
20 struct list_head reserved;
22 unsigned long visible_size;
23 unsigned long visible_avail;
24 unsigned long visible_reserved;
25 u64 default_page_size;
28 static struct i915_ttm_buddy_manager *
29 to_buddy_manager(struct ttm_resource_manager *man)
31 return container_of(man, struct i915_ttm_buddy_manager, manager);
34 static int i915_ttm_buddy_man_alloc(struct ttm_resource_manager *man,
35 struct ttm_buffer_object *bo,
36 const struct ttm_place *place,
37 struct ttm_resource **res)
39 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
40 struct i915_ttm_buddy_resource *bman_res;
41 struct drm_buddy *mm = &bman->mm;
42 unsigned long n_pages, lpfn;
51 bman_res = kzalloc(sizeof(*bman_res), GFP_KERNEL);
55 ttm_resource_init(bo, place, &bman_res->base);
56 INIT_LIST_HEAD(&bman_res->blocks);
59 if (place->flags & TTM_PL_FLAG_TOPDOWN)
60 bman_res->flags |= DRM_BUDDY_TOPDOWN_ALLOCATION;
62 if (place->flags & TTM_PL_FLAG_CONTIGUOUS)
63 bman_res->flags |= DRM_BUDDY_CONTIGUOUS_ALLOCATION;
65 if (place->fpfn || lpfn != man->size)
66 bman_res->flags |= DRM_BUDDY_RANGE_ALLOCATION;
68 GEM_BUG_ON(!bman_res->base.size);
69 size = bman_res->base.size;
71 min_page_size = bman->default_page_size;
72 if (bo->page_alignment)
73 min_page_size = bo->page_alignment << PAGE_SHIFT;
75 GEM_BUG_ON(min_page_size < mm->chunk_size);
76 GEM_BUG_ON(!IS_ALIGNED(size, min_page_size));
78 if (size > lpfn << PAGE_SHIFT) {
83 n_pages = size >> ilog2(mm->chunk_size);
85 mutex_lock(&bman->lock);
86 if (lpfn <= bman->visible_size && n_pages > bman->visible_avail) {
87 mutex_unlock(&bman->lock);
92 err = drm_buddy_alloc_blocks(mm, (u64)place->fpfn << PAGE_SHIFT,
93 (u64)lpfn << PAGE_SHIFT,
94 (u64)n_pages << PAGE_SHIFT,
101 if (lpfn <= bman->visible_size) {
102 bman_res->used_visible_size = PFN_UP(bman_res->base.size);
104 struct drm_buddy_block *block;
106 list_for_each_entry(block, &bman_res->blocks, link) {
107 unsigned long start =
108 drm_buddy_block_offset(block) >> PAGE_SHIFT;
110 if (start < bman->visible_size) {
111 unsigned long end = start +
112 (drm_buddy_block_size(mm, block) >> PAGE_SHIFT);
114 bman_res->used_visible_size +=
115 min(end, bman->visible_size) - start;
120 if (bman_res->used_visible_size)
121 bman->visible_avail -= bman_res->used_visible_size;
123 mutex_unlock(&bman->lock);
125 *res = &bman_res->base;
129 drm_buddy_free_list(mm, &bman_res->blocks, 0);
130 mutex_unlock(&bman->lock);
132 ttm_resource_fini(man, &bman_res->base);
137 static void i915_ttm_buddy_man_free(struct ttm_resource_manager *man,
138 struct ttm_resource *res)
140 struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
141 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
143 mutex_lock(&bman->lock);
144 drm_buddy_free_list(&bman->mm, &bman_res->blocks, 0);
145 bman->visible_avail += bman_res->used_visible_size;
146 mutex_unlock(&bman->lock);
148 ttm_resource_fini(man, res);
152 static bool i915_ttm_buddy_man_intersects(struct ttm_resource_manager *man,
153 struct ttm_resource *res,
154 const struct ttm_place *place,
157 struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
158 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
159 struct drm_buddy *mm = &bman->mm;
160 struct drm_buddy_block *block;
162 if (!place->fpfn && !place->lpfn)
165 GEM_BUG_ON(!place->lpfn);
168 * If we just want something mappable then we can quickly check
169 * if the current victim resource is using any of the CPU
173 place->lpfn == i915_ttm_buddy_man_visible_size(man))
174 return bman_res->used_visible_size > 0;
176 /* Check each drm buddy block individually */
177 list_for_each_entry(block, &bman_res->blocks, link) {
179 drm_buddy_block_offset(block) >> PAGE_SHIFT;
180 unsigned long lpfn = fpfn +
181 (drm_buddy_block_size(mm, block) >> PAGE_SHIFT);
183 if (place->fpfn < lpfn && place->lpfn > fpfn)
190 static bool i915_ttm_buddy_man_compatible(struct ttm_resource_manager *man,
191 struct ttm_resource *res,
192 const struct ttm_place *place,
195 struct i915_ttm_buddy_resource *bman_res = to_ttm_buddy_resource(res);
196 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
197 struct drm_buddy *mm = &bman->mm;
198 struct drm_buddy_block *block;
200 if (!place->fpfn && !place->lpfn)
203 GEM_BUG_ON(!place->lpfn);
206 place->lpfn == i915_ttm_buddy_man_visible_size(man))
207 return bman_res->used_visible_size == PFN_UP(res->size);
209 /* Check each drm buddy block individually */
210 list_for_each_entry(block, &bman_res->blocks, link) {
212 drm_buddy_block_offset(block) >> PAGE_SHIFT;
213 unsigned long lpfn = fpfn +
214 (drm_buddy_block_size(mm, block) >> PAGE_SHIFT);
216 if (fpfn < place->fpfn || lpfn > place->lpfn)
223 static void i915_ttm_buddy_man_debug(struct ttm_resource_manager *man,
224 struct drm_printer *printer)
226 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
227 struct drm_buddy_block *block;
229 mutex_lock(&bman->lock);
230 drm_printf(printer, "default_page_size: %lluKiB\n",
231 bman->default_page_size >> 10);
232 drm_printf(printer, "visible_avail: %lluMiB\n",
233 (u64)bman->visible_avail << PAGE_SHIFT >> 20);
234 drm_printf(printer, "visible_size: %lluMiB\n",
235 (u64)bman->visible_size << PAGE_SHIFT >> 20);
236 drm_printf(printer, "visible_reserved: %lluMiB\n",
237 (u64)bman->visible_reserved << PAGE_SHIFT >> 20);
239 drm_buddy_print(&bman->mm, printer);
241 drm_printf(printer, "reserved:\n");
242 list_for_each_entry(block, &bman->reserved, link)
243 drm_buddy_block_print(&bman->mm, block, printer);
244 mutex_unlock(&bman->lock);
247 static const struct ttm_resource_manager_func i915_ttm_buddy_manager_func = {
248 .alloc = i915_ttm_buddy_man_alloc,
249 .free = i915_ttm_buddy_man_free,
250 .intersects = i915_ttm_buddy_man_intersects,
251 .compatible = i915_ttm_buddy_man_compatible,
252 .debug = i915_ttm_buddy_man_debug,
256 * i915_ttm_buddy_man_init - Setup buddy allocator based ttm manager
257 * @bdev: The ttm device
258 * @type: Memory type we want to manage
259 * @use_tt: Set use_tt for the manager
260 * @size: The size in bytes to manage
261 * @visible_size: The CPU visible size in bytes to manage
262 * @default_page_size: The default minimum page size in bytes for allocations,
263 * this must be at least as large as @chunk_size, and can be overridden by
264 * setting the BO page_alignment, to be larger or smaller as needed.
265 * @chunk_size: The minimum page size in bytes for our allocations i.e
268 * Note that the starting address is assumed to be zero here, since this
269 * simplifies keeping the property where allocated blocks having natural
270 * power-of-two alignment. So long as the real starting address is some large
271 * power-of-two, or naturally start from zero, then this should be fine. Also
272 * the &i915_ttm_buddy_man_reserve interface can be used to preserve alignment
273 * if say there is some unusable range from the start of the region. We can
274 * revisit this in the future and make the interface accept an actual starting
275 * offset and let it take care of the rest.
277 * Note that if the @size is not aligned to the @chunk_size then we perform the
278 * required rounding to get the usable size. The final size in pages can be
279 * taken from &ttm_resource_manager.size.
281 * Return: 0 on success, negative error code on failure.
283 int i915_ttm_buddy_man_init(struct ttm_device *bdev,
284 unsigned int type, bool use_tt,
285 u64 size, u64 visible_size, u64 default_page_size,
288 struct ttm_resource_manager *man;
289 struct i915_ttm_buddy_manager *bman;
292 bman = kzalloc(sizeof(*bman), GFP_KERNEL);
296 err = drm_buddy_init(&bman->mm, size, chunk_size);
300 mutex_init(&bman->lock);
301 INIT_LIST_HEAD(&bman->reserved);
302 GEM_BUG_ON(default_page_size < chunk_size);
303 bman->default_page_size = default_page_size;
304 bman->visible_size = visible_size >> PAGE_SHIFT;
305 bman->visible_avail = bman->visible_size;
307 man = &bman->manager;
308 man->use_tt = use_tt;
309 man->func = &i915_ttm_buddy_manager_func;
310 ttm_resource_manager_init(man, bdev, bman->mm.size >> PAGE_SHIFT);
312 ttm_resource_manager_set_used(man, true);
313 ttm_set_driver_manager(bdev, type, man);
323 * i915_ttm_buddy_man_fini - Destroy the buddy allocator ttm manager
324 * @bdev: The ttm device
325 * @type: Memory type we want to manage
327 * Note that if we reserved anything with &i915_ttm_buddy_man_reserve, this will
328 * also be freed for us here.
330 * Return: 0 on success, negative error code on failure.
332 int i915_ttm_buddy_man_fini(struct ttm_device *bdev, unsigned int type)
334 struct ttm_resource_manager *man = ttm_manager_type(bdev, type);
335 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
336 struct drm_buddy *mm = &bman->mm;
339 ttm_resource_manager_set_used(man, false);
341 ret = ttm_resource_manager_evict_all(bdev, man);
345 ttm_set_driver_manager(bdev, type, NULL);
347 mutex_lock(&bman->lock);
348 drm_buddy_free_list(mm, &bman->reserved, 0);
350 bman->visible_avail += bman->visible_reserved;
351 WARN_ON_ONCE(bman->visible_avail != bman->visible_size);
352 mutex_unlock(&bman->lock);
354 ttm_resource_manager_cleanup(man);
361 * i915_ttm_buddy_man_reserve - Reserve address range
362 * @man: The buddy allocator ttm manager
363 * @start: The offset in bytes, where the region start is assumed to be zero
364 * @size: The size in bytes
366 * Note that the starting address for the region is always assumed to be zero.
368 * Return: 0 on success, negative error code on failure.
370 int i915_ttm_buddy_man_reserve(struct ttm_resource_manager *man,
373 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
374 struct drm_buddy *mm = &bman->mm;
375 unsigned long fpfn = start >> PAGE_SHIFT;
376 unsigned long flags = 0;
379 flags |= DRM_BUDDY_RANGE_ALLOCATION;
381 mutex_lock(&bman->lock);
382 ret = drm_buddy_alloc_blocks(mm, start,
384 size, mm->chunk_size,
388 if (fpfn < bman->visible_size) {
389 unsigned long lpfn = fpfn + (size >> PAGE_SHIFT);
390 unsigned long visible = min(lpfn, bman->visible_size) - fpfn;
392 bman->visible_reserved += visible;
393 bman->visible_avail -= visible;
395 mutex_unlock(&bman->lock);
401 * i915_ttm_buddy_man_visible_size - Return the size of the CPU visible portion
403 * @man: The buddy allocator ttm manager
405 u64 i915_ttm_buddy_man_visible_size(struct ttm_resource_manager *man)
407 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
409 return bman->visible_size;
413 * i915_ttm_buddy_man_avail - Query the avail tracking for the manager.
415 * @man: The buddy allocator ttm manager
416 * @avail: The total available memory in pages for the entire manager.
417 * @visible_avail: The total available memory in pages for the CPU visible
418 * portion. Note that this will always give the same value as @avail on
419 * configurations that don't have a small BAR.
421 void i915_ttm_buddy_man_avail(struct ttm_resource_manager *man,
422 u64 *avail, u64 *visible_avail)
424 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
426 mutex_lock(&bman->lock);
427 *avail = bman->mm.avail >> PAGE_SHIFT;
428 *visible_avail = bman->visible_avail;
429 mutex_unlock(&bman->lock);
432 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
433 void i915_ttm_buddy_man_force_visible_size(struct ttm_resource_manager *man,
436 struct i915_ttm_buddy_manager *bman = to_buddy_manager(man);
438 bman->visible_size = size;