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 : {
30 0 : }
31 : // 获取映射后的devVa,先去map找,找不到则新建映射关系
32 0 : HcclResult MemMappingManager::GetDevVA(s32 deviceLogicID, void *addr, u64 size, void *&devVA)
33 : {
34 0 : std::unique_lock<std::mutex> lockMapping(mappedHostToDevMutex_);
35 0 : if (!DlHalFunction::GetInstance().DlHalFunctionIsInit()) {
36 0 : CHK_RET(DlHalFunction::GetInstance().DlHalFunctionInit());
37 0 : HCCL_INFO("[MemMappingManager] hal function init success.");
38 : }
39 :
40 0 : CHK_RET(MapMem(deviceLogicID, addr, size, devVA));
41 0 : HCCL_INFO("[MemMappingManager][GetDevVA]addr[%p] size[%llu] mapping success, devVa[%p]",
42 : addr, size, devVA);
43 0 : return HCCL_SUCCESS;
44 0 : }
45 :
46 0 : bool MemMappingManager::IsRequireMapping(void *addr, u64 size, void *&devVA)
47 : {
48 0 : u64 userAddr = reinterpret_cast<u64>(addr);
49 0 : u64 userSize = size;
50 0 : if (mappedHostToDevMap_.size() == 0) {
51 0 : return true;
52 : }
53 :
54 0 : auto iter = SearchMappingMap(userAddr, userSize);
55 0 : if (iter != mappedHostToDevMap_.end()) {
56 0 : u64 tmpDva = reinterpret_cast<u64>(iter->second.devVA) + userAddr - iter->first.addr;
57 0 : devVA = reinterpret_cast<void*>(static_cast<uintptr_t>(tmpDva));
58 0 : iter->second.ref.Ref();
59 0 : return false;
60 : }
61 :
62 0 : return true;
63 : }
64 :
65 0 : MemMappingManager::HostMappingIter MemMappingManager::SearchMappingMap(u64 userAddr, u64 userSize)
66 : {
67 0 : for (auto iter = mappedHostToDevMap_.begin(); iter != mappedHostToDevMap_.end(); ++iter) {
68 0 : if ((userAddr >= iter->first.addr) &&
69 0 : (userAddr + userSize <= iter->first.size + iter->first.addr)) {
70 0 : return iter;
71 : }
72 : }
73 0 : return mappedHostToDevMap_.end();
74 : }
75 :
76 : }
|