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