Merge tag '5.13-rc-smb3-part2' of git://git.samba.org/sfrench/cifs-2.6
[linux-2.6-microblaze.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_res_cursor.h
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
32 /* state back for walking over vram_mgr and gtt_mgr allocations */
33 struct amdgpu_res_cursor {
34         uint64_t                start;
35         uint64_t                size;
36         uint64_t                remaining;
37         struct drm_mm_node      *node;
38 };
39
40 /**
41  * amdgpu_res_first - initialize a amdgpu_res_cursor
42  *
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
47  *
48  * Start walking over the range of allocations between @start and @size.
49  */
50 static inline void amdgpu_res_first(struct ttm_resource *res,
51                                     uint64_t start, uint64_t size,
52                                     struct amdgpu_res_cursor *cur)
53 {
54         struct drm_mm_node *node;
55
56         if (!res || !res->mm_node) {
57                 cur->start = start;
58                 cur->size = size;
59                 cur->remaining = size;
60                 cur->node = NULL;
61                 return;
62         }
63
64         BUG_ON(start + size > res->num_pages << PAGE_SHIFT);
65
66         node = res->mm_node;
67         while (start >= node->size << PAGE_SHIFT)
68                 start -= node++->size << PAGE_SHIFT;
69
70         cur->start = (node->start << PAGE_SHIFT) + start;
71         cur->size = min((node->size << PAGE_SHIFT) - start, size);
72         cur->remaining = size;
73         cur->node = node;
74 }
75
76 /**
77  * amdgpu_res_next - advance the cursor
78  *
79  * @cur: the cursor to advance
80  * @size: number of bytes to move forward
81  *
82  * Move the cursor @size bytes forwrad, walking to the next node if necessary.
83  */
84 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
85 {
86         struct drm_mm_node *node = cur->node;
87
88         BUG_ON(size > cur->remaining);
89
90         cur->remaining -= size;
91         if (!cur->remaining)
92                 return;
93
94         cur->size -= size;
95         if (cur->size) {
96                 cur->start += size;
97                 return;
98         }
99
100         cur->node = ++node;
101         cur->start = node->start << PAGE_SHIFT;
102         cur->size = min(node->size << PAGE_SHIFT, cur->remaining);
103 }
104
105 #endif