Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /*
0003  * Copyright 2020 Advanced Micro Devices, Inc.
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the "Software"),
0007  * to deal in the Software without restriction, including without limitation
0008  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0009  * and/or sell copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included in
0013  * all copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0018  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0019  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0020  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0021  * OTHER DEALINGS IN THE SOFTWARE.
0022  *
0023  * Authors: Christian König
0024  */
0025 
0026 #ifndef __AMDGPU_RES_CURSOR_H__
0027 #define __AMDGPU_RES_CURSOR_H__
0028 
0029 #include <drm/drm_mm.h>
0030 #include <drm/ttm/ttm_resource.h>
0031 #include <drm/ttm/ttm_range_manager.h>
0032 
0033 #include "amdgpu_vram_mgr.h"
0034 
0035 /* state back for walking over vram_mgr and gtt_mgr allocations */
0036 struct amdgpu_res_cursor {
0037     uint64_t        start;
0038     uint64_t        size;
0039     uint64_t        remaining;
0040     void            *node;
0041     uint32_t        mem_type;
0042 };
0043 
0044 /**
0045  * amdgpu_res_first - initialize a amdgpu_res_cursor
0046  *
0047  * @res: TTM resource object to walk
0048  * @start: Start of the range
0049  * @size: Size of the range
0050  * @cur: cursor object to initialize
0051  *
0052  * Start walking over the range of allocations between @start and @size.
0053  */
0054 static inline void amdgpu_res_first(struct ttm_resource *res,
0055                     uint64_t start, uint64_t size,
0056                     struct amdgpu_res_cursor *cur)
0057 {
0058     struct drm_buddy_block *block;
0059     struct list_head *head, *next;
0060     struct drm_mm_node *node;
0061 
0062     if (!res)
0063         goto fallback;
0064 
0065     BUG_ON(start + size > res->num_pages << PAGE_SHIFT);
0066 
0067     cur->mem_type = res->mem_type;
0068 
0069     switch (cur->mem_type) {
0070     case TTM_PL_VRAM:
0071         head = &to_amdgpu_vram_mgr_resource(res)->blocks;
0072 
0073         block = list_first_entry_or_null(head,
0074                          struct drm_buddy_block,
0075                          link);
0076         if (!block)
0077             goto fallback;
0078 
0079         while (start >= amdgpu_vram_mgr_block_size(block)) {
0080             start -= amdgpu_vram_mgr_block_size(block);
0081 
0082             next = block->link.next;
0083             if (next != head)
0084                 block = list_entry(next, struct drm_buddy_block, link);
0085         }
0086 
0087         cur->start = amdgpu_vram_mgr_block_start(block) + start;
0088         cur->size = min(amdgpu_vram_mgr_block_size(block) - start, size);
0089         cur->remaining = size;
0090         cur->node = block;
0091         break;
0092     case TTM_PL_TT:
0093         node = to_ttm_range_mgr_node(res)->mm_nodes;
0094         while (start >= node->size << PAGE_SHIFT)
0095             start -= node++->size << PAGE_SHIFT;
0096 
0097         cur->start = (node->start << PAGE_SHIFT) + start;
0098         cur->size = min((node->size << PAGE_SHIFT) - start, size);
0099         cur->remaining = size;
0100         cur->node = node;
0101         break;
0102     default:
0103         goto fallback;
0104     }
0105 
0106     return;
0107 
0108 fallback:
0109     cur->start = start;
0110     cur->size = size;
0111     cur->remaining = size;
0112     cur->node = NULL;
0113     WARN_ON(res && start + size > res->num_pages << PAGE_SHIFT);
0114     return;
0115 }
0116 
0117 /**
0118  * amdgpu_res_next - advance the cursor
0119  *
0120  * @cur: the cursor to advance
0121  * @size: number of bytes to move forward
0122  *
0123  * Move the cursor @size bytes forwrad, walking to the next node if necessary.
0124  */
0125 static inline void amdgpu_res_next(struct amdgpu_res_cursor *cur, uint64_t size)
0126 {
0127     struct drm_buddy_block *block;
0128     struct drm_mm_node *node;
0129     struct list_head *next;
0130 
0131     BUG_ON(size > cur->remaining);
0132 
0133     cur->remaining -= size;
0134     if (!cur->remaining)
0135         return;
0136 
0137     cur->size -= size;
0138     if (cur->size) {
0139         cur->start += size;
0140         return;
0141     }
0142 
0143     switch (cur->mem_type) {
0144     case TTM_PL_VRAM:
0145         block = cur->node;
0146 
0147         next = block->link.next;
0148         block = list_entry(next, struct drm_buddy_block, link);
0149 
0150         cur->node = block;
0151         cur->start = amdgpu_vram_mgr_block_start(block);
0152         cur->size = min(amdgpu_vram_mgr_block_size(block), cur->remaining);
0153         break;
0154     case TTM_PL_TT:
0155         node = cur->node;
0156 
0157         cur->node = ++node;
0158         cur->start = node->start << PAGE_SHIFT;
0159         cur->size = min(node->size << PAGE_SHIFT, cur->remaining);
0160         break;
0161     default:
0162         return;
0163     }
0164 }
0165 
0166 #endif