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 CORE_AICPUSD_DRV_MANAGER_H
12 : #define CORE_AICPUSD_DRV_MANAGER_H
13 : #include <string>
14 : #include <vector>
15 : #include <sys/types.h>
16 : #include "ascend_hal.h"
17 : #include "feature_ctrl.h"
18 :
19 : namespace AicpuSchedule {
20 : class AicpuDrvManager {
21 : public:
22 : static AicpuDrvManager& GetInstance();
23 :
24 : int32_t GetNormalAicpuInfo(const uint32_t deviceId);
25 :
26 : /**
27 : * @ingroup AicpuDrvManager
28 : * @brief it is used to init.
29 : * @param [in] deviceId : device id.
30 : * @param [in] hostPid : host pid
31 : * @param [in] vfId : vf id
32 : * @param [in] hasThread : has thread flag.
33 : * @return AICPU_SCHEDULE_OK: success, other: error code
34 : */
35 : int32_t InitDrvMgr(const uint32_t deviceId, const pid_t hostPid, const uint32_t vfId, const bool hasThread);
36 :
37 : /**
38 : * @ingroup AicpuDrvManager
39 : * @brief it is used to init driver sched module.
40 : * @param [in] grpId : the group id.
41 : * @return AICPU_SCHEDULE_OK: success, other: error code
42 : */
43 : int32_t InitDrvSchedModule(const uint32_t grpId);
44 :
45 68 : uint32_t GetDeviceId() const { return deviceId_; }
46 :
47 11 : uint32_t GetUniqueVfId() const { return uniqueVfId_; }
48 :
49 25 : pid_t GetHostPid() const
50 : {
51 25 : if (isInit_) {
52 20 : return hostPid_;
53 : }
54 5 : return drvDeviceGetBareTgid();
55 : }
56 :
57 5 : uint32_t GetGroupId() const { return groupId_; }
58 :
59 18 : uint32_t GetVfId() const { return vfId_; }
60 :
61 : bool HasThread() const { return hasThread_; }
62 :
63 : uint32_t GetAicpuNum() const;
64 :
65 6 : uint32_t GetAicpuPhysIndex(const uint32_t aicpuLogIndex) const
66 : {
67 6 : if (FeatureCtrl::IsAosCore()) {
68 : // connot overflow
69 0 : return ((aicpuBaseId_ + aicpuNum_) * deviceId_) + aicpuBaseId_ + aicpuLogIndex;
70 : } else {
71 6 : return (coreNumPerDev_ * deviceId_) + aicpuIdVec_[aicpuLogIndex];
72 : }
73 : }
74 :
75 2 : uint32_t GetAicpuPhysIndexInVfMode(const uint32_t aicpuLogIndex, const uint32_t deviceId) const
76 : {
77 2 : if (FeatureCtrl::IsVfModeDie1(deviceId)) {
78 0 : return coreNumPerDev_ + aicpuIdVec_[static_cast<size_t>(aicpuLogIndex)];
79 : }
80 2 : return aicpuIdVec_[static_cast<size_t>(aicpuLogIndex)];
81 : }
82 :
83 : void GetDcpuRange(uint32_t& dcpuBase, uint32_t& dcpuNum) const
84 : {
85 : dcpuBase = dcpuBase_;
86 : dcpuNum = dcpuNum_;
87 : }
88 :
89 : int32_t CheckBindHostPid() const;
90 :
91 : int32_t GetCcpuInfo(const uint32_t deviceId);
92 :
93 15 : std::vector<uint32_t> GetCcpuList() { return ccpuIdVec_; }
94 :
95 9 : uint32_t GetCcpuNum() const { return static_cast<uint32_t>(ccpuIdVec_.size()); }
96 :
97 5 : uint32_t GetCcpuPhysIndex(const uint32_t ccpuLogIndex) const
98 : {
99 5 : return (coreNumPerDev_ * deviceId_) + ccpuIdVec_[ccpuLogIndex];
100 : }
101 :
102 12 : bool GetSafeVerifyFlag() const { return needSafeVerify_; }
103 :
104 : private:
105 3 : AicpuDrvManager()
106 3 : : deviceId_(0U),
107 3 : coreNumPerDev_(0U),
108 3 : hostPid_(0),
109 3 : vfId_(0U),
110 3 : groupId_(0U),
111 3 : hasThread_(false),
112 3 : isInit_(false),
113 3 : aicpuNum_(0U),
114 3 : aicpuBaseId_(0U),
115 6 : aicpuIdVec_({}),
116 3 : dcpuBase_(0U),
117 3 : dcpuNum_(0U),
118 3 : uniqueVfId_(0U),
119 3 : needSafeVerify_(true)
120 3 : {}
121 :
122 3 : ~AicpuDrvManager() = default;
123 :
124 : AicpuDrvManager(const AicpuDrvManager&) = delete;
125 :
126 : AicpuDrvManager& operator=(const AicpuDrvManager&) = delete;
127 :
128 : void InitSocType(const uint32_t deviceId);
129 :
130 : void SetSafeVerifyFlag(const uint32_t deviceId);
131 :
132 : // the id of the chip.
133 : uint32_t deviceId_;
134 :
135 : // core num of device
136 : uint32_t coreNumPerDev_;
137 :
138 : // the pid of host process
139 : pid_t hostPid_;
140 :
141 : // vf id
142 : uint32_t vfId_;
143 :
144 : // the id is used to group in process.
145 : uint32_t groupId_;
146 :
147 : // it is used to mark which is in call mode or multi-thread mode.
148 : bool hasThread_;
149 :
150 : // init function called or not, lhisi is will be false
151 : bool isInit_;
152 :
153 : // aicpu core num
154 : uint32_t aicpuNum_;
155 :
156 : // aicpu base index
157 : uint32_t aicpuBaseId_;
158 :
159 : // aicpu index
160 : std::vector<uint32_t> aicpuIdVec_;
161 :
162 : // dcpu base index
163 : uint32_t dcpuBase_;
164 :
165 : // dcpu number
166 : uint32_t dcpuNum_;
167 :
168 : // calc by deviceId and vfId for vfId uniqueization
169 : uint32_t uniqueVfId_;
170 :
171 : std::vector<uint32_t> ccpuIdVec_;
172 :
173 : bool needSafeVerify_;
174 : };
175 : } // namespace AicpuSchedule
176 : #endif // CORE_AICPUSD_DRV_MANAGER_H
|