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>
31 #include <drm/ttm/ttm_range_manager.h>
33 #include "amdgpu_vram_mgr.h"
35 /* state back for walking over vram_mgr and gtt_mgr allocations */
36 struct amdgpu_res_cursor {
45 * amdgpu_res_first - initialize a amdgpu_res_cursor
47 * @res: TTM resource object to walk
48 * @start: Start of the range
49 * @size: Size of the range
50 * @cur: cursor object to initialize
52 * Start walking over the range of allocations between @start and @size.
54 static inline void amdgpu_res_first(struct ttm_resource *res,
55 uint64_t start, uint64_t size,
56 struct amdgpu_res_cursor *cur)
58 struct drm_buddy_block *block;
59 struct list_head *head, *next;
60 struct drm_mm_node *node;
65 BUG_ON(start + size > res->size);
67 cur->mem_type = res->mem_type;
69 switch (cur->mem_type) {
71 head = &to_amdgpu_vram_mgr_resource(res)->blocks;
73 block = list_first_entry_or_null(head,
74 struct drm_buddy_block,
79 while (start >= amdgpu_vram_mgr_block_size(block)) {
80 start -= amdgpu_vram_mgr_block_size(block);
82 next = block->link.next;
84 block = list_entry(next, struct drm_buddy_block, link);
87 cur->start = amdgpu_vram_mgr_block_start(block) + start;
88 cur->size = min(amdgpu_vram_mgr_block_size(block) - start, size);
89 cur->remaining = size;
93 case AMDGPU_PL_DOORBELL:
94 node = to_ttm_range_mgr_node(res)->mm_nodes;
95 while (start >= node->size << PAGE_SHIFT)
96 start -= node++->size << PAGE_SHIFT;
98 cur->start = (node->start << PAGE_SHIFT) + start;
99 cur->size = min((node->size << PAGE_SHIFT) - start, size);
100 cur->remaining = size;
112 cur->remaining = size;
114 WARN_ON(res && start + size > res->size);
118 * amdgpu_res_next - advance the cursor
120 * @cur: the cursor to advance
121 * @size: number of bytes to move forward
123 * Move the cursor @size bytes forwrad, walking to the next node if necessary.
125 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
127 struct drm_buddy_block *block;
128 struct drm_mm_node *node;
129 struct list_head *next;
131 BUG_ON(size > cur->remaining);
133 cur->remaining -= size;
143 switch (cur->mem_type) {
147 next = block->link.next;
148 block = list_entry(next, struct drm_buddy_block, link);
151 cur->start = amdgpu_vram_mgr_block_start(block);
152 cur->size = min(amdgpu_vram_mgr_block_size(block), cur->remaining);
155 case AMDGPU_PL_DOORBELL:
159 cur->start = node->start << PAGE_SHIFT;
160 cur->size = min(node->size << PAGE_SHIFT, cur->remaining);
168 * amdgpu_res_cleared - check if blocks are cleared
170 * @cur: the cursor to extract the block
172 * Check if the @cur block is cleared
174 static inline bool amdgpu_res_cleared(struct amdgpu_res_cursor *cur)
176 struct drm_buddy_block *block;
178 switch (cur->mem_type) {
182 if (!amdgpu_vram_mgr_is_cleared(block))