1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2020 Advanced Micro Devices, Inc.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors: Christian König
24  */
25 
26 #ifndef __AMDGPU_RES_CURSOR_H__
27 #define __AMDGPU_RES_CURSOR_H__
28 
29 #include <drm/drm_mm.h>
30 #include <drm/ttm/ttm_resource.h>
31 #include <drm/ttm/ttm_range_manager.h>
32 
33 /* state back for walking over vram_mgr and gtt_mgr allocations */
34 struct amdgpu_res_cursor {
35 	uint64_t		start;
36 	uint64_t		size;
37 	uint64_t		remaining;
38 	struct drm_mm_node	*node;
39 };
40 
41 /**
42  * amdgpu_res_first - initialize a amdgpu_res_cursor
43  *
44  * @res: TTM resource object to walk
45  * @start: Start of the range
46  * @size: Size of the range
47  * @cur: cursor object to initialize
48  *
49  * Start walking over the range of allocations between @start and @size.
50  */
amdgpu_res_first(struct ttm_resource * res,uint64_t start,uint64_t size,struct amdgpu_res_cursor * cur)51 static inline void amdgpu_res_first(struct ttm_resource *res,
52 				    uint64_t start, uint64_t size,
53 				    struct amdgpu_res_cursor *cur)
54 {
55 	struct drm_mm_node *node;
56 
57 	if (!res || res->mem_type == TTM_PL_SYSTEM) {
58 		cur->start = start;
59 		cur->size = size;
60 		cur->remaining = size;
61 		cur->node = NULL;
62 		WARN_ON(res && start + size > res->num_pages << PAGE_SHIFT);
63 		return;
64 	}
65 
66 	BUG_ON(start + size > res->num_pages << PAGE_SHIFT);
67 
68 	node = to_ttm_range_mgr_node(res)->mm_nodes;
69 	while (start >= node->size << PAGE_SHIFT)
70 		start -= node++->size << PAGE_SHIFT;
71 
72 	cur->start = (node->start << PAGE_SHIFT) + start;
73 	cur->size = min((node->size << PAGE_SHIFT) - start, size);
74 	cur->remaining = size;
75 	cur->node = node;
76 }
77 
78 /**
79  * amdgpu_res_next - advance the cursor
80  *
81  * @cur: the cursor to advance
82  * @size: number of bytes to move forward
83  *
84  * Move the cursor @size bytes forwrad, walking to the next node if necessary.
85  */
amdgpu_res_next(struct amdgpu_res_cursor * cur,uint64_t size)86 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
87 {
88 	struct drm_mm_node *node = cur->node;
89 
90 	BUG_ON(size > cur->remaining);
91 
92 	cur->remaining -= size;
93 	if (!cur->remaining)
94 		return;
95 
96 	cur->size -= size;
97 	if (cur->size) {
98 		cur->start += size;
99 		return;
100 	}
101 
102 	cur->node = ++node;
103 	cur->start = node->start << PAGE_SHIFT;
104 	cur->size = min(node->size << PAGE_SHIFT, cur->remaining);
105 }
106 
107 #endif
108