Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "mem_mapping_manager.h"
12 : #include "dlhal_function.h"
13 :
14 : namespace hccl {
15 0 : MemMappingManager& MemMappingManager::GetInstance(s32 deviceLogicID)
16 : {
17 0 : static MemMappingManager instance[MAX_DEV_NUM];
18 0 : if (deviceLogicID == HOST_DEVICE_ID) {
19 0 : return instance[DEFAULT_DEVICE_LOGIC_ID];
20 : }
21 :
22 0 : if (static_cast<u32>(deviceLogicID) >= MAX_DEV_NUM || deviceLogicID <= HOST_DEVICE_ID) {
23 0 : HCCL_WARNING("[Get][Instance]deviceLogicID[%d] is invalid", deviceLogicID);
24 0 : return instance[DEFAULT_DEVICE_LOGIC_ID];
25 : }
26 0 : return instance[deviceLogicID];
27 : }
28 0 : MemMappingManager::~MemMappingManager() {}
29 : // 获取映射后的devVa,先去map找,找不到则新建映射关系
30 0 : HcclResult MemMappingManager::GetDevVA(s32 deviceLogicID, void* addr, u64 size, void*& devVA)
31 : {
32 0 : std::unique_lock<std::mutex> lockMapping(mappedHostToDevMutex_);
33 0 : if (!DlHalFunction::GetInstance().DlHalFunctionIsInit()) {
34 0 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
35 0 : HCCL_INFO("[MemMappingManager] hal function init success.");
36 : }
37 :
38 0 : CHK_RET(MapMem(deviceLogicID, addr, size, devVA));
39 0 : HCCL_INFO("[MemMappingManager][GetDevVA]addr[%p] size[%llu] mapping success, devVa[%p]", addr, size, devVA);
40 0 : return HCCL_SUCCESS;
41 0 : }
42 :
43 0 : bool MemMappingManager::IsRequireMapping(void* addr, u64 size, void*& devVA)
44 : {
45 0 : u64 userAddr = reinterpret_cast<u64>(addr);
46 0 : u64 userSize = size;
47 0 : if (mappedHostToDevMap_.size() == 0) {
48 0 : return true;
49 : }
50 :
51 0 : auto iter = SearchMappingMap(userAddr, userSize);
52 0 : if (iter != mappedHostToDevMap_.end()) {
53 0 : u64 tmpDva = reinterpret_cast<u64>(iter->second.devVA) + userAddr - iter->first.addr;
54 0 : devVA = reinterpret_cast<void*>(static_cast<uintptr_t>(tmpDva));
55 0 : iter->second.ref.Ref();
56 0 : return false;
57 : }
58 :
59 0 : return true;
60 : }
61 :
62 0 : MemMappingManager::HostMappingIter MemMappingManager::SearchMappingMap(u64 userAddr, u64 userSize)
63 : {
64 0 : for (auto iter = mappedHostToDevMap_.begin(); iter != mappedHostToDevMap_.end(); ++iter) {
65 0 : if ((userAddr >= iter->first.addr) && (userAddr + userSize <= iter->first.size + iter->first.addr)) {
66 0 : return iter;
67 : }
68 : }
69 0 : return mappedHostToDevMap_.end();
70 : }
71 :
72 : } // namespace hccl
|