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_QUEUE_EVENT_PROCESS_H
12 : #define CORE_AICPUSD_QUEUE_EVENT_PROCESS_H
13 :
14 : #include <memory>
15 : #include <mutex>
16 : #include <string>
17 : #include <unordered_map>
18 : #include <set>
19 : #include "aicpusd_drv_manager.h"
20 : #include "aicpusd_common.h"
21 : #include "aicpusd_util.h"
22 : #include "ascend_hal.h"
23 : #include "ascend_hal_define.h"
24 : #include "qs_client.h"
25 :
26 : namespace AicpuSchedule {
27 : constexpr uint32_t PROXY_SUBEVENT_CREATE_GROUP = 0U;
28 : constexpr uint32_t PROXY_SUBEVENT_ALLOC_MBUF = 1U;
29 : constexpr uint32_t PROXY_SUBEVENT_FREE_MBUF = 2U;
30 : constexpr uint32_t PROXY_SUBEVENT_COPY_QMBUF = 3U;
31 : constexpr uint32_t PROXY_SUBEVENT_ADD_GROUP = 4U;
32 : constexpr uint32_t PROXY_SUBEVENT_ALLOC_CACHE = 5U;
33 :
34 : struct ProxyMsgRsp {
35 : uint64_t mbufAddr;
36 : uint64_t dataAddr;
37 : int32_t retCode;
38 : char_t rsv[12];
39 : };
40 :
41 : struct ProxyMsgCreateGroup {
42 : uint64_t size; // max buf size in group, in KB
43 : char_t groupName[16];
44 : int64_t allocSize; // alloc size when create group, 0: alloc by size, -1: not alloc, >0: alloc by allocSize
45 : };
46 :
47 : struct ProxyMsgAllocMbuf {
48 : uint64_t size;
49 : char_t rsv[24];
50 : };
51 :
52 : struct ProxyMsgFreeMbuf {
53 : uint64_t mbufAddr;
54 : char_t rsv[24];
55 : };
56 :
57 : struct ProxyMsgCopyQMbuf {
58 : uint64_t destAddr;
59 : uint32_t destLen;
60 : uint32_t queueId;
61 : char_t rsv[16];
62 : };
63 :
64 : struct ProxyMsgAddGroup {
65 : uint32_t admin : 1; /* admin permission, can add other proc to grp */
66 : uint32_t read : 1; /* rsv, not supported */
67 : uint32_t write : 1; /* read and write permission */
68 : uint32_t alloc : 1;
69 : int32_t pid;
70 : char_t groupName[16];
71 : char_t rsv[8];
72 : };
73 : #pragma pack(push, 1)
74 : struct ProxyMsgAllocCache {
75 : uint64_t memSize;
76 : uint32_t allocMaxSize;
77 : char_t rsv[20];
78 : };
79 : #pragma pack(pop)
80 :
81 : class AicpuQueueEventProcess {
82 : public:
83 : /**
84 : * @ingroup AicpuQueueEventProcess
85 : * @brief get AicpuQueueEventProcess Singleton
86 : * @return AicpuQueueEventProcess Singleton
87 : */
88 : static AicpuQueueEventProcess& GetInstance();
89 :
90 : /**
91 : * @ingroup AicpuQueueEventProcess
92 : * @brief process event from driver
93 : * @param [in] event: queue event from driver send by acl
94 : * @return AICPU_SCHEDULE_OK: success
95 : */
96 : int32_t ProcessDrvMsg(const event_info& event);
97 :
98 : /**
99 : * @ingroup AicpuQueueEventProcess
100 : * @brief process event from acl or qs
101 : * @param [in] event: queue event from acl or qs
102 : * @return AICPU_SCHEDULE_OK: success
103 : */
104 : int32_t ProcessQsMsg(const event_info& event);
105 :
106 : int32_t ProcessProxyMsg(const event_info& event);
107 :
108 : private:
109 58 : AicpuQueueEventProcess()
110 58 : : initPipeline_(BindQueueInitStatus::UNINIT),
111 58 : qsPid_(0),
112 58 : pipelineQueueId_(0U),
113 58 : type_(CpType::MASTER),
114 58 : curPid_(drvDeviceGetBareTgid())
115 58 : {}
116 :
117 58 : ~AicpuQueueEventProcess() = default;
118 :
119 : // Prohibit copy constructor, copy assignment, move constructor, move assignment
120 : AicpuQueueEventProcess(AicpuQueueEventProcess const&) = delete;
121 : AicpuQueueEventProcess& operator=(AicpuQueueEventProcess const&) = delete;
122 : AicpuQueueEventProcess(AicpuQueueEventProcess&&) = delete;
123 : AicpuQueueEventProcess& operator=(AicpuQueueEventProcess&&) = delete;
124 :
125 : /**
126 : * @ingroup AicpuQueueEventProcess
127 : * @brief process queue event
128 : * @param [in] event: queue event
129 : * @param [out] needRes: whether need response
130 : * @return AICPU_SCHEDULE_OK: success
131 : */
132 : int32_t DoProcessDrvMsg(const event_info& event, bool& needRes);
133 :
134 : /**
135 : * @ingroup AicpuQueueEventProcess
136 : * @brief process queue event
137 : * @param [in] event: queue event
138 : * @param [out] callback: callback msg return from queue evnet process
139 : * @param [out] qsProcMsgRsp: qs process result
140 : * @param [out] isRes: is qs response
141 : * @return AICPU_SCHEDULE_OK: success
142 : */
143 : int32_t DoProcessQsMsg(
144 : const event_info& event, std::shared_ptr<CallbackMsg>& callback, const bqs::QsProcMsgRspDstAicpu*& qsProcMsgRsp,
145 : bool& isRes);
146 :
147 : /**
148 : * @ingroup AicpuQueueEventProcess
149 : * @brief process acl bind queue init event
150 : * @param [in] event: queue event from driver send by acl
151 : * @return AICPU_SCHEDULE_OK: success
152 : */
153 : int32_t ProcessBindQueueInit(const event_info& event);
154 :
155 : /**
156 : * @ingroup AicpuQueueEventProcess
157 : * @brief process qs bind queue init result
158 : * @param [in] event: queue event from driver send by qs
159 : * @param [out] callback: callback msg from bind queue init
160 : * @param [out] qsProcMsgRsp: qs process result
161 : * @return AICPU_SCHEDULE_OK: success
162 : */
163 : int32_t ProcessBindQueueInitRet(
164 : const event_info& event, std::shared_ptr<CallbackMsg>& callback,
165 : const bqs::QsProcMsgRspDstAicpu** const qsProcMsgRsp);
166 :
167 : /**
168 : * @ingroup AicpuQueueEventProcess
169 : * @brief process acl query queue num event
170 : * @param [in] event: queue event from driver send by acl
171 : * @return AICPU_SCHEDULE_OK: success
172 : */
173 : int32_t ProcessQueryQueueNum(const event_info& event);
174 :
175 : /**
176 : * @ingroup AicpuQueueEventProcess
177 : * @brief send event to qs
178 : * @param [in] msg: event msg will sent to qs
179 : * @param [in] msgLen: msg len
180 : * @param [in] drvSubeventId: subevent id for event
181 : * @return AICPU_SCHEDULE_OK: success
182 : */
183 : int32_t SendEventToQs(char_t* const msg, const size_t msgLen, const bqs::QueueSubEventType drvSubeventId) const;
184 :
185 : /**
186 : * @ingroup AicpuQueueEventProcess
187 : * @brief alloc mbuf for data and enqueue to cp and qs pipline queue
188 : * @param [in] data: need copy from svm to mbuf data
189 : * @param [in] size: data size
190 : * @param [out] buff: point to alloc mbuf pointer
191 : * @return AICPU_SCHEDULE_OK: success
192 : */
193 : int32_t AllocMbufAndEnqueue(const bqs::QsRouteHead* const data, const size_t size, Mbuf** const buff);
194 :
195 : /**
196 : * @ingroup AicpuQueueEventProcess
197 : * @brief response event to acl
198 : * @param [in] event: event info which need response
199 : * @param [in] msg: return to acl msg
200 : * @param [in] len: msg len
201 : * @return AICPU_SCHEDULE_OK: success
202 : */
203 : int32_t ResponseEvent(const event_info& event, const char_t* const msg, const size_t len) const;
204 :
205 : /**
206 : * @ingroup AicpuQueueEventProcess
207 : * @brief query qs pid and init qs pid
208 : * @return AICPU_SCHEDULE_OK: success
209 : */
210 : int32_t QueryQsPid();
211 :
212 : /**
213 : * @ingroup AicpuQueueEventProcess
214 : * @brief copy result from mbuf to svm
215 : * @param [in] callback: callback which event need to copy result
216 : * @return AICPU_SCHEDULE_OK: success
217 : */
218 : int32_t CopyResult(const std::shared_ptr<CallbackMsg>& callback) const;
219 :
220 : /**
221 : * @ingroup AicpuQueueEventProcess
222 : * @brief check qs process event result
223 : * @param [in] event: event from driver send by qs
224 : * @param [out] callback: callback msg from event info
225 : * @param [out] qsProcMsgRsp: qs process result
226 : * @return AICPU_SCHEDULE_OK: success
227 : */
228 : int32_t ProcessQsRet(
229 : const event_info& event, std::shared_ptr<CallbackMsg>& callback,
230 : const bqs::QsProcMsgRspDstAicpu** const qsProcMsgRsp);
231 :
232 : /**
233 : * @ingroup AicpuQueueEventProcess
234 : * @brief process grant queue, grant queue to other aicpusd process
235 : * @param [in] event: event from driver send by acl
236 : * @return AICPU_SCHEDULE_OK: success
237 : */
238 : int32_t GrantQueue(const event_info& event);
239 :
240 : /**
241 : * @ingroup AicpuQueueEventProcess
242 : * @brief process attach queue,check group and queue authority
243 : * @param [in] event: event from driver send by acl
244 : * @return AICPU_SCHEDULE_OK: success
245 : */
246 : int32_t AttachQueue(const event_info& event);
247 :
248 : /**
249 : * @ingroup AicpuQueueEventProcess
250 : * @brief share group with other process
251 : * @param [in] groupName: group name to be share
252 : * @param [in] pid: the process pid to be share
253 : * @return AICPU_SCHEDULE_OK: success
254 : */
255 : int32_t ShareGroupWithProcess(const std::string& groupName, const pid_t& pid) const;
256 :
257 : /**
258 : * @ingroup AicpuQueueEventProcess
259 : * @brief get or create group for current process
260 : * @param [out] outGroupName: group name
261 : * @return AICPU_SCHEDULE_OK: success
262 : */
263 : int32_t GetOrCreateGroup(std::string& outGroupName);
264 :
265 : /**
266 : * @ingroup AicpuQueueEventProcess
267 : * @brief create group for current process
268 : * @param [out] outGroupName: group name
269 : * @return AICPU_SCHEDULE_OK: success
270 : */
271 : int32_t CreateGroupForMaster(
272 : std::string& outGroupName, const char_t* const inGroupName = nullptr, const uint64_t size = 0U,
273 : const uint32_t allocFlag = 0U);
274 :
275 : /**
276 : * @ingroup AicpuQueueEventProcess
277 : * @brief attach group for current process
278 : * @param [in] grpInfos: group info
279 : * @param [out] outGroupName: group name
280 : * @return AICPU_SCHEDULE_OK: success
281 : */
282 : int32_t AttachGroupForSlave(const std::map<std::string, GroupShareAttr>& grpInfos, std::string& outGroupName);
283 :
284 : /**
285 : * @ingroup AicpuQueueEventProcess
286 : * @brief create and add callback msg
287 : * @param [in] event: which event need to add callback
288 : * @param [in] mbuf: save data for event msg
289 : * @param [in] userData: user data
290 : * @param [out] callback: callback msg
291 : * @return AICPU_SCHEDULE_OK: success
292 : */
293 : int32_t CreateAndAddCallbackMsg(
294 : const event_info& event, Mbuf* const buff, const uint64_t userData, std::shared_ptr<CallbackMsg>& callback);
295 : /**
296 : * @ingroup AicpuQueueEventProcess
297 : * @brief save callback msg
298 : * @param [in] userData: key: callback event addr
299 : * @param [in] callback: callback msg
300 : * @return AICPU_SCHEDULE_OK: success
301 : */
302 : int32_t AddCallback(uint64_t userData, std::shared_ptr<CallbackMsg>& callback);
303 :
304 : /**
305 : * @ingroup AicpuQueueEventProcess
306 : * @brief get and delete callback msg
307 : * @param [in] userData: key: callback event addr
308 : * @param [in] callback: callback msg
309 : * @return AICPU_SCHEDULE_OK: success
310 : */
311 : int32_t GetAndDeleteCallback(const uint64_t userData, std::shared_ptr<CallbackMsg>& callback);
312 :
313 : /**
314 : * @ingroup AicpuQueueEventProcess
315 : * @brief add queue auth to qs, read to src queue, write to dst queue
316 : * @param [in] queueRoute: queue route array addr
317 : * @param [in] routeNum: queue route array size
318 : * @return AICPU_SCHEDULE_OK: success
319 : */
320 : int32_t AddQueueAuthToQs(const bqs::QueueRoute* const queueRoute, const uint32_t routeNum);
321 :
322 : /**
323 : * @ingroup AicpuQueueEventProcess
324 : * @brief check qs process event result
325 : * @param [in] event: event from driver send by qs
326 : * @param [out] callback: callback msg from event info
327 : * @param [out] qsProcMsgRsp: qs process result
328 : * @return AICPU_SCHEDULE_OK: success
329 : */
330 : int32_t ProcessQsRetWithMbuf(
331 : const event_info& event, std::shared_ptr<CallbackMsg>& callback,
332 : const bqs::QsProcMsgRspDstAicpu** const qsProcMsgRsp);
333 :
334 : /**
335 : * @ingroup AicpuQueueEventProcess
336 : * @brief process acl queue event
337 : * @param [in] event: event info
338 : * @param [out] msg: queue event msg
339 : * @param [out] routeHead: queue event msg head
340 : * @return AICPU_SCHEDULE_OK: success
341 : */
342 : int32_t CheckAndInitParamWithMbuf(
343 : const event_info& event, const bqs::QueueRouteList*& msg, bqs::QsRouteHead*& routeHead) const;
344 :
345 : /**
346 : * @ingroup AicpuQueueEventProcess
347 : * @brief process acl queue event
348 : * @param [in] event: queue event from driver send by acl
349 : * @param [in] drvSubeventId: subevent id
350 : * @return AICPU_SCHEDULE_OK: success
351 : */
352 : int32_t ProcessQueueEventWithMbuf(const event_info& event, const bqs::QueueSubEventType drvSubeventId);
353 :
354 : /**
355 : * @ingroup
356 : * @brief parse queue event msg
357 : * @param [in] event: queue event
358 : * @param [out] msg: will parse from event info
359 : * @param [in] isSyncEvent: if sync event, msg need add offset
360 : * @return AICPU_SCHEDULE_OK: success
361 : */
362 : int32_t ParseQueueEventMessage(
363 : const event_info& event, const char_t*& msg, const size_t msgSize, const bool isSyncEvent = false) const;
364 :
365 : void DoProcessProxyMsg(const event_info& event, ProxyMsgRsp& rsp);
366 : int32_t ProxyCreateGroup(const event_info& event);
367 : int32_t ProxyAllocMbuf(const event_info& event, Mbuf** mbufPtr, void** dataPptr) const;
368 : int32_t ProxyFreeMbuf(const event_info& event) const;
369 : int32_t ProxyCopyQMbuf(const event_info& event) const;
370 : int32_t ProxyAddGroup(const event_info& event) const;
371 : int32_t ProxyAllocCache(const event_info& event) const;
372 : int32_t DoAllocCache(const char_t* const groupName, GrpCacheAllocPara* const allocPar) const;
373 :
374 : private:
375 : BindQueueInitStatus initPipeline_; // init cp and qs pipeline status
376 : pid_t qsPid_; // qs pid
377 : uint32_t pipelineQueueId_; // cp and qs pipeline queue id
378 : CpType type_; // cp type master or slave
379 : std::string grpName_; // group name
380 : pid_t curPid_; // current process pid
381 : SpinLock lockGroup_; // lock for group name
382 : SpinLock lockEnqueue_; // lock for enqueue
383 : std::atomic_flag lockInit_ = ATOMIC_FLAG_INIT; // lock for bind queue init
384 : std::mutex lockCallback_; // lock for callback
385 : std::unordered_map<uint64_t, std::shared_ptr<CallbackMsg>> callbacks_; // save userData,callback map
386 : std::set<uint32_t> grantedSrcQueueSet_; // src queue set already been granted to avoid grant twice
387 : std::set<uint32_t> grantedDstQueueSet_; // src queue set already been granted to avoid grant twice
388 : };
389 : } // namespace AicpuSchedule
390 :
391 : #endif
|