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 : #ifndef MEM_MAPPING_MANAGER_H
12 : #define MEM_MAPPING_MANAGER_H
13 :
14 : #include <atomic>
15 : #include <mutex>
16 : #include "hccl_common.h"
17 : #include "hccl_inner_common.h"
18 : #include "dlhal_function.h"
19 :
20 : namespace hccl {
21 : class MemMappingManager {
22 : public:
23 : ~MemMappingManager();
24 :
25 : static MemMappingManager& GetInstance(s32 deviceLogicID);
26 :
27 : HcclResult GetDevVA(s32 deviceLogicID, void* addr, u64 size, void*& devVA);
28 : HcclResult ReleaseDevVA(s32 deviceLogicID, void* addr, u64 size);
29 :
30 : // delete copy and move constructors and assign operators
31 : MemMappingManager(MemMappingManager const&) = delete; // Copy construct
32 : MemMappingManager(MemMappingManager&&) = delete; // Move construct
33 : MemMappingManager& operator=(MemMappingManager const&) = delete; // Copy assign
34 : MemMappingManager& operator=(MemMappingManager&&) = delete; // Move assign
35 :
36 : private:
37 : struct HostMappingKey {
38 : u64 addr = 0;
39 : u64 size = 0;
40 :
41 0 : HostMappingKey(u64 addr, u64 size) : addr(addr), size(size) {}
42 :
43 : bool operator==(const HostMappingKey& that) const
44 : {
45 : return ((this->addr == that.addr) && (this->size == that.size));
46 : }
47 :
48 : bool operator!=(const HostMappingKey& that) const
49 : {
50 : return (this->addr != that.addr) || (this->size != that.size);
51 : }
52 :
53 0 : bool operator<(const HostMappingKey& that) const
54 : {
55 0 : return (addr < that.addr) || (addr == that.addr && size < that.size);
56 : }
57 : };
58 : struct HostMappingInfo {
59 : void* devVA = nullptr;
60 : Referenced ref;
61 : drvRegisterTpye registerTpye;
62 : };
63 : using HostMappingIter = std::map<MemMappingManager::HostMappingKey, MemMappingManager::HostMappingInfo>::iterator;
64 :
65 0 : MemMappingManager() {}
66 : HcclResult MapMem(s32 deviceLogicID, void* addr, u64 size, void*& devVA);
67 : bool IsRequireMapping(void* addr, u64 size, void*& devVA);
68 : HostMappingIter SearchMappingMap(u64 userAddr, u64 userSize);
69 :
70 : std::mutex mappedHostToDevMutex_;
71 : std::map<HostMappingKey, HostMappingInfo> mappedHostToDevMap_;
72 : };
73 : } // namespace hccl
74 : #endif // MEM_MAPPING_MANAGER_H
|