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 */ 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) { 58 cur->start = start; 59 cur->size = size; 60 cur->remaining = size; 61 cur->node = NULL; 62 return; 63 } 64 65 BUG_ON(start + size > res->num_pages << PAGE_SHIFT); 66 67 node = to_ttm_range_mgr_node(res)->mm_nodes; 68 while (start >= node->size << PAGE_SHIFT) 69 start -= node++->size << PAGE_SHIFT; 70 71 cur->start = (node->start << PAGE_SHIFT) + start; 72 cur->size = min((node->size << PAGE_SHIFT) - start, size); 73 cur->remaining = size; 74 cur->node = node; 75 } 76 77 /** 78 * amdgpu_res_next - advance the cursor 79 * 80 * @cur: the cursor to advance 81 * @size: number of bytes to move forward 82 * 83 * Move the cursor @size bytes forwrad, walking to the next node if necessary. 84 */ 85 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size) 86 { 87 struct drm_mm_node *node = cur->node; 88 89 BUG_ON(size > cur->remaining); 90 91 cur->remaining -= size; 92 if (!cur->remaining) 93 return; 94 95 cur->size -= size; 96 if (cur->size) { 97 cur->start += size; 98 return; 99 } 100 101 cur->node = ++node; 102 cur->start = node->start << PAGE_SHIFT; 103 cur->size = min(node->size << PAGE_SHIFT, cur->remaining); 104 } 105 106 #endif 107