1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2020 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Christian König
26 #ifndef __AMDGPU_RES_CURSOR_H__
27 #define __AMDGPU_RES_CURSOR_H__
29 #include <drm/drm_mm.h>
30 #include <drm/ttm/ttm_resource.h>
32 /* state back for walking over vram_mgr and gtt_mgr allocations */
33 struct amdgpu_res_cursor {
37 struct drm_mm_node *node;
41 * amdgpu_res_first - initialize a amdgpu_res_cursor
43 * @res: TTM resource object to walk
44 * @start: Start of the range
45 * @size: Size of the range
46 * @cur: cursor object to initialize
48 * Start walking over the range of allocations between @start and @size.
50 static inline void amdgpu_res_first(struct ttm_resource *res,
51 uint64_t start, uint64_t size,
52 struct amdgpu_res_cursor *cur)
54 struct drm_mm_node *node;
56 if (!res || !res->mm_node) {
59 cur->remaining = size;
64 BUG_ON(start + size > res->num_pages << PAGE_SHIFT);
67 while (start > node->size << PAGE_SHIFT)
68 start -= node++->size << PAGE_SHIFT;
70 cur->start = (node->start << PAGE_SHIFT) + start;
71 cur->size = min((node->size << PAGE_SHIFT) - start, size);
72 cur->remaining = size;
77 * amdgpu_res_next - advance the cursor
79 * @cur: the cursor to advance
80 * @size: number of bytes to move forward
82 * Move the cursor @size bytes forwrad, walking to the next node if necessary.
84 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
86 struct drm_mm_node *node = cur->node;
88 BUG_ON(size > cur->remaining);
90 cur->remaining -= size;
101 cur->start = node->start << PAGE_SHIFT;
102 cur->size = min(node->size << PAGE_SHIFT, cur->remaining);