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 51 : AicpuQueueEventProcess() : initPipeline_(BindQueueInitStatus::UNINIT), qsPid_(0), pipelineQueueId_(0U),
110 51 : type_(CpType::MASTER), curPid_(drvDeviceGetBareTgid()) {}
111 :
112 51 : ~AicpuQueueEventProcess() = default;
113 :
114 : // Prohibit copy constructor, copy assignment, move constructor, move assignment
115 : AicpuQueueEventProcess(AicpuQueueEventProcess const&) = delete;
116 : AicpuQueueEventProcess& operator=(AicpuQueueEventProcess const&) = delete;
117 : AicpuQueueEventProcess(AicpuQueueEventProcess&&) = delete;
118 : AicpuQueueEventProcess& operator=(AicpuQueueEventProcess&&) = delete;
119 :
120 : /**
121 : * @ingroup AicpuQueueEventProcess
122 : * @brief process queue event
123 : * @param [in] event: queue event
124 : * @param [out] needRes: whether need response
125 : * @return AICPU_SCHEDULE_OK: success
126 : */
127 : int32_t DoProcessDrvMsg(const event_info &event, bool &needRes);
128 :
129 : /**
130 : * @ingroup AicpuQueueEventProcess
131 : * @brief process queue event
132 : * @param [in] event: queue event
133 : * @param [out] callback: callback msg return from queue evnet process
134 : * @param [out] qsProcMsgRsp: qs process result
135 : * @param [out] isRes: is qs response
136 : * @return AICPU_SCHEDULE_OK: success
137 : */
138 : int32_t DoProcessQsMsg(const event_info &event,
139 : std::shared_ptr<CallbackMsg> &callback,
140 : const bqs::QsProcMsgRspDstAicpu *&qsProcMsgRsp,
141 : bool &isRes);
142 :
143 : /**
144 : * @ingroup AicpuQueueEventProcess
145 : * @brief process acl bind queue init event
146 : * @param [in] event: queue event from driver send by acl
147 : * @return AICPU_SCHEDULE_OK: success
148 : */
149 : int32_t ProcessBindQueueInit(const event_info &event);
150 :
151 : /**
152 : * @ingroup AicpuQueueEventProcess
153 : * @brief process qs bind queue init result
154 : * @param [in] event: queue event from driver send by qs
155 : * @param [out] callback: callback msg from bind queue init
156 : * @param [out] qsProcMsgRsp: qs process result
157 : * @return AICPU_SCHEDULE_OK: success
158 : */
159 : int32_t ProcessBindQueueInitRet(const event_info &event, std::shared_ptr<CallbackMsg> &callback,
160 : const bqs::QsProcMsgRspDstAicpu ** const qsProcMsgRsp);
161 :
162 : /**
163 : * @ingroup AicpuQueueEventProcess
164 : * @brief process acl query queue num event
165 : * @param [in] event: queue event from driver send by acl
166 : * @return AICPU_SCHEDULE_OK: success
167 : */
168 : int32_t ProcessQueryQueueNum(const event_info &event);
169 :
170 : /**
171 : * @ingroup AicpuQueueEventProcess
172 : * @brief send event to qs
173 : * @param [in] msg: event msg will sent to qs
174 : * @param [in] msgLen: msg len
175 : * @param [in] drvSubeventId: subevent id for event
176 : * @return AICPU_SCHEDULE_OK: success
177 : */
178 : int32_t SendEventToQs(char_t * const msg, const size_t msgLen,
179 : const bqs::QueueSubEventType drvSubeventId) const;
180 :
181 : /**
182 : * @ingroup AicpuQueueEventProcess
183 : * @brief alloc mbuf for data and enqueue to cp and qs pipline queue
184 : * @param [in] data: need copy from svm to mbuf data
185 : * @param [in] size: data size
186 : * @param [out] buff: point to alloc mbuf pointer
187 : * @return AICPU_SCHEDULE_OK: success
188 : */
189 : int32_t AllocMbufAndEnqueue(const bqs::QsRouteHead * const data, const size_t size, Mbuf ** const buff);
190 :
191 : /**
192 : * @ingroup AicpuQueueEventProcess
193 : * @brief response event to acl
194 : * @param [in] event: event info which need response
195 : * @param [in] msg: return to acl msg
196 : * @param [in] len: msg len
197 : * @return AICPU_SCHEDULE_OK: success
198 : */
199 : int32_t ResponseEvent(const event_info &event, const char_t * const msg, const size_t len) const;
200 :
201 : /**
202 : * @ingroup AicpuQueueEventProcess
203 : * @brief query qs pid and init qs pid
204 : * @return AICPU_SCHEDULE_OK: success
205 : */
206 : int32_t QueryQsPid();
207 :
208 : /**
209 : * @ingroup AicpuQueueEventProcess
210 : * @brief copy result from mbuf to svm
211 : * @param [in] callback: callback which event need to copy result
212 : * @return AICPU_SCHEDULE_OK: success
213 : */
214 : int32_t CopyResult(const std::shared_ptr<CallbackMsg> &callback) const;
215 :
216 : /**
217 : * @ingroup AicpuQueueEventProcess
218 : * @brief check qs process event result
219 : * @param [in] event: event from driver send by qs
220 : * @param [out] callback: callback msg from event info
221 : * @param [out] qsProcMsgRsp: qs process result
222 : * @return AICPU_SCHEDULE_OK: success
223 : */
224 : int32_t ProcessQsRet(const event_info &event, std::shared_ptr<CallbackMsg> &callback,
225 : const bqs::QsProcMsgRspDstAicpu ** const qsProcMsgRsp);
226 :
227 : /**
228 : * @ingroup AicpuQueueEventProcess
229 : * @brief process grant queue, grant queue to other aicpusd process
230 : * @param [in] event: event from driver send by acl
231 : * @return AICPU_SCHEDULE_OK: success
232 : */
233 : int32_t GrantQueue(const event_info &event);
234 :
235 : /**
236 : * @ingroup AicpuQueueEventProcess
237 : * @brief process attach queue,check group and queue authority
238 : * @param [in] event: event from driver send by acl
239 : * @return AICPU_SCHEDULE_OK: success
240 : */
241 : int32_t AttachQueue(const event_info &event);
242 :
243 : /**
244 : * @ingroup AicpuQueueEventProcess
245 : * @brief share group with other process
246 : * @param [in] groupName: group name to be share
247 : * @param [in] pid: the process pid to be share
248 : * @return AICPU_SCHEDULE_OK: success
249 : */
250 : int32_t ShareGroupWithProcess(const std::string &groupName, const pid_t &pid) const;
251 :
252 : /**
253 : * @ingroup AicpuQueueEventProcess
254 : * @brief get or create group for current process
255 : * @param [out] outGroupName: group name
256 : * @return AICPU_SCHEDULE_OK: success
257 : */
258 : int32_t GetOrCreateGroup(std::string &outGroupName);
259 :
260 : /**
261 : * @ingroup AicpuQueueEventProcess
262 : * @brief create group for current process
263 : * @param [out] outGroupName: group name
264 : * @return AICPU_SCHEDULE_OK: success
265 : */
266 : int32_t CreateGroupForMaster(std::string &outGroupName, const char_t * const inGroupName = nullptr,
267 : const uint64_t size = 0U, const uint32_t allocFlag = 0U);
268 :
269 : /**
270 : * @ingroup AicpuQueueEventProcess
271 : * @brief attach group for current process
272 : * @param [in] grpInfos: group info
273 : * @param [out] outGroupName: group name
274 : * @return AICPU_SCHEDULE_OK: success
275 : */
276 : int32_t AttachGroupForSlave(const std::map<std::string, GroupShareAttr> &grpInfos,
277 : std::string &outGroupName);
278 :
279 : /**
280 : * @ingroup AicpuQueueEventProcess
281 : * @brief create and add callback msg
282 : * @param [in] event: which event need to add callback
283 : * @param [in] mbuf: save data for event msg
284 : * @param [in] userData: user data
285 : * @param [out] callback: callback msg
286 : * @return AICPU_SCHEDULE_OK: success
287 : */
288 : int32_t CreateAndAddCallbackMsg(const event_info &event, Mbuf * const buff,
289 : const uint64_t userData, std::shared_ptr<CallbackMsg> &callback);
290 : /**
291 : * @ingroup AicpuQueueEventProcess
292 : * @brief save callback msg
293 : * @param [in] userData: key: callback event addr
294 : * @param [in] callback: callback msg
295 : * @return AICPU_SCHEDULE_OK: success
296 : */
297 : int32_t AddCallback(uint64_t userData, std::shared_ptr<CallbackMsg> &callback);
298 :
299 : /**
300 : * @ingroup AicpuQueueEventProcess
301 : * @brief get and delete callback msg
302 : * @param [in] userData: key: callback event addr
303 : * @param [in] callback: callback msg
304 : * @return AICPU_SCHEDULE_OK: success
305 : */
306 : int32_t GetAndDeleteCallback(const uint64_t userData, std::shared_ptr<CallbackMsg> &callback);
307 :
308 : /**
309 : * @ingroup AicpuQueueEventProcess
310 : * @brief add queue auth to qs, read to src queue, write to dst queue
311 : * @param [in] queueRoute: queue route array addr
312 : * @param [in] routeNum: queue route array size
313 : * @return AICPU_SCHEDULE_OK: success
314 : */
315 : int32_t AddQueueAuthToQs(const bqs::QueueRoute * const queueRoute, const uint32_t routeNum);
316 :
317 : /**
318 : * @ingroup AicpuQueueEventProcess
319 : * @brief check qs process event result
320 : * @param [in] event: event from driver send by qs
321 : * @param [out] callback: callback msg from event info
322 : * @param [out] qsProcMsgRsp: qs process result
323 : * @return AICPU_SCHEDULE_OK: success
324 : */
325 : int32_t ProcessQsRetWithMbuf(const event_info &event, std::shared_ptr<CallbackMsg> &callback,
326 : const bqs::QsProcMsgRspDstAicpu ** const qsProcMsgRsp);
327 :
328 : /**
329 : * @ingroup AicpuQueueEventProcess
330 : * @brief process acl queue event
331 : * @param [in] event: event info
332 : * @param [out] msg: queue event msg
333 : * @param [out] routeHead: queue event msg head
334 : * @return AICPU_SCHEDULE_OK: success
335 : */
336 : int32_t CheckAndInitParamWithMbuf(const event_info &event,
337 : const bqs::QueueRouteList *&msg,
338 : bqs::QsRouteHead *&routeHead) const;
339 :
340 : /**
341 : * @ingroup AicpuQueueEventProcess
342 : * @brief process acl queue event
343 : * @param [in] event: queue event from driver send by acl
344 : * @param [in] drvSubeventId: subevent id
345 : * @return AICPU_SCHEDULE_OK: success
346 : */
347 : int32_t ProcessQueueEventWithMbuf(const event_info &event, const bqs::QueueSubEventType drvSubeventId);
348 :
349 : /**
350 : * @ingroup
351 : * @brief parse queue event msg
352 : * @param [in] event: queue event
353 : * @param [out] msg: will parse from event info
354 : * @param [in] isSyncEvent: if sync event, msg need add offset
355 : * @return AICPU_SCHEDULE_OK: success
356 : */
357 : int32_t ParseQueueEventMessage(const event_info &event, const char_t *&msg, const size_t msgSize,
358 : const bool isSyncEvent = false) const;
359 :
360 : void DoProcessProxyMsg(const event_info &event, ProxyMsgRsp &rsp);
361 : int32_t ProxyCreateGroup(const event_info &event);
362 : int32_t ProxyAllocMbuf(const event_info &event, Mbuf **mbufPtr, void **dataPptr) const;
363 : int32_t ProxyFreeMbuf(const event_info &event) const;
364 : int32_t ProxyCopyQMbuf(const event_info &event) const;
365 : int32_t ProxyAddGroup(const event_info &event) const;
366 : int32_t ProxyAllocCache(const event_info &event) const;
367 : int32_t DoAllocCache(const char_t* const groupName, GrpCacheAllocPara* const allocPar) const;
368 :
369 : private:
370 : BindQueueInitStatus initPipeline_; // init cp and qs pipeline status
371 : pid_t qsPid_; // qs pid
372 : uint32_t pipelineQueueId_; // cp and qs pipeline queue id
373 : CpType type_; // cp type master or slave
374 : std::string grpName_; // group name
375 : pid_t curPid_; // current process pid
376 : SpinLock lockGroup_; // lock for group name
377 : SpinLock lockEnqueue_; // lock for enqueue
378 : std::atomic_flag lockInit_ = ATOMIC_FLAG_INIT; // lock for bind queue init
379 : std::mutex lockCallback_; // lock for callback
380 : std::unordered_map<uint64_t, std::shared_ptr<CallbackMsg>> callbacks_; // save userData,callback map
381 : std::set<uint32_t> grantedSrcQueueSet_; // src queue set already been granted to avoid grant twice
382 : std::set<uint32_t> grantedDstQueueSet_; // src queue set already been granted to avoid grant twice
383 : };
384 : }
385 :
386 : #endif
|