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 : #include "adump_dsmi.h"
11 : #include "runtime/dev.h"
12 : #include "error_codes/rt_error_codes.h"
13 : #include "ascend_hal.h"
14 : #include "adx_log.h"
15 : #include "sys_utils.h"
16 :
17 : #if !defined(ADUMP_SOC_HOST) || ADUMP_SOC_HOST == 1
18 : __attribute__((weak)) drvError_t halGetVdevNum(uint32_t *num_dev);
19 : __attribute__((weak)) drvError_t halGetVdevIDs(uint32_t *devices, uint32_t len);
20 : __attribute__((weak)) drvError_t halGetChipInfo(uint32_t devId, halChipInfo *chipInfo);
21 : __attribute__((weak)) drvError_t halGetAPIVersion(int *halAPIVersion);
22 : #endif
23 :
24 : namespace Adx {
25 375 : uint32_t AdumpDsmi::DrvGetDevNum()
26 : {
27 375 : int32_t numDev = 0;
28 375 : rtError_t ret = rtGetDeviceCount(&numDev);
29 375 : IDE_CTRL_VALUE_WARN(ret != ACL_ERROR_RT_FEATURE_NOT_SUPPORT, return 0,
30 : "Driver doesn't support rtGetDeviceCount interface, ret=%d", static_cast<int32_t>(ret));
31 375 : IDE_CTRL_VALUE_FAILED((ret == RT_ERROR_NONE) && (numDev >= 0) && (numDev <= DEV_NUM), return 0,
32 : "Failed to get device count, ret=%d, num=%d", static_cast<int32_t>(ret), numDev);
33 :
34 375 : IDE_LOGD("Succeeded to get device count, numDev=%d", numDev);
35 375 : return static_cast<uint32_t>(numDev);
36 : }
37 :
38 375 : bool AdumpDsmi::DrvGetDevIds(uint32_t numDevices, std::vector<uint32_t> &devIds)
39 : {
40 375 : devIds.clear();
41 375 : if (numDevices > DEV_NUM) {
42 0 : return false;
43 : }
44 375 : uint32_t devices[DEV_NUM] = { 0 };
45 375 : rtError_t ret = rtGetDeviceIDs(devices, numDevices);
46 375 : IDE_CTRL_VALUE_FAILED(ret == RT_ERROR_NONE, return false, "Failed to rtGetDeviceIDs, ret=%d",
47 : static_cast<int32_t>(ret));
48 3404 : for (uint32_t i = 0; i < numDevices; ++i) {
49 3032 : if (DrvGetDeviceStatus(devices[i])) {
50 3032 : devIds.push_back(devices[i]);
51 : }
52 : }
53 :
54 372 : IDE_LOGD("Succeeded to rtGetDeviceIDs, numDevices=%u, device list num=%zu", numDevices, devIds.size());
55 372 : return true;
56 : }
57 :
58 3032 : bool AdumpDsmi::DrvGetDeviceStatus(const uint32_t deviceId)
59 : {
60 3032 : rtDevStatus_t deviceStatus = RT_DEV_STATUS_INITING;
61 3032 : rtError_t ret = rtGetDeviceStatus(deviceId, &deviceStatus);
62 3032 : IDE_CTRL_VALUE_WARN(ret != ACL_ERROR_RT_FEATURE_NOT_SUPPORT, return true,
63 : "Driver doesn't support rtGetDeviceStatus interface, ret=%d", static_cast<int32_t>(ret));
64 :
65 3032 : IDE_CTRL_VALUE_FAILED(ret == RT_ERROR_NONE, return false, "Failed to get device %u status, ret=%d.",
66 : deviceId, static_cast<int32_t>(ret));
67 :
68 3032 : IDE_CTRL_VALUE_WARN(deviceStatus != RT_DEV_STATUS_COMMUNICATION_LOST, return false,
69 : "Device %u status is communication lost.", deviceId);
70 3032 : return true;
71 : }
72 :
73 374 : std::vector<uint32_t> AdumpDsmi::DrvGetDeviceList()
74 : {
75 374 : std::vector<uint32_t> devList;
76 374 : uint32_t devNum = DrvGetDevNum();
77 374 : if ((devNum > 0) && !DrvGetDevIds(devNum, devList)) {
78 3 : IDE_LOGW("Get device id list abnormally, device num: %u.", devNum);
79 : }
80 374 : if (devList.empty()) {
81 3 : devList.push_back(0);
82 : }
83 374 : return devList;
84 0 : }
85 :
86 374 : bool AdumpDsmi::DrvGetPlatformType(uint32_t &platformType)
87 : {
88 374 : IDE_LOGD("Start to get chip type.");
89 374 : std::vector<uint32_t> devList = DrvGetDeviceList();
90 :
91 374 : int64_t versionInfo = 0;
92 374 : uint64_t chipId = 0;
93 374 : rtError_t ret = ACL_ERROR_RT_NO_DEVICE;
94 374 : for (auto &devId : devList) {
95 374 : ret = rtGetDeviceInfo(devId, static_cast<int32_t>(MODULE_TYPE_SYSTEM),
96 : static_cast<int32_t>(INFO_TYPE_VERSION), &versionInfo);
97 374 : if (ret == RT_ERROR_NONE) {
98 374 : chipId = ((static_cast<uint64_t>(versionInfo) >> 8) & 0xff); // 8:shift 8 bits, get the low 8 bits(0xff)
99 374 : break;
100 0 : } else if (ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
101 0 : IDE_LOGW("Driver doesn't support device type version by rtGetDeviceInfo interface, ret=%d"
102 : ", set PlatformType::HELPER_DEVICE_TYPE", static_cast<int32_t>(ret));
103 0 : chipId = DEFAULT_CHIP_TYPE; // tmp set default chip type for helper device
104 0 : break;
105 : }
106 : }
107 374 : IDE_CTRL_VALUE_FAILED((ret == RT_ERROR_NONE) || (ret == ACL_ERROR_RT_FEATURE_NOT_SUPPORT), return false,
108 : "Failed to get chip id, ret is %d.", static_cast<int32_t>(ret));
109 :
110 374 : platformType = static_cast<uint32_t>(chipId);
111 374 : IDE_LOGD("Success to get chip id: %u.", chipId);
112 374 : return true;
113 374 : }
114 :
115 : /**
116 : * @ingroup driver
117 : * @brief Get current platform information
118 : * @attention null
119 : * @param [out] *platformInfo 0 Means currently on the Device side, 1/Means currently on the host side
120 : * @return true for success, false for fail
121 : */
122 110 : bool AdumpDsmi::DrvGetPlatformInfo(uint32_t &platformInfo)
123 : {
124 110 : rtRunMode info = RT_RUN_MODE_RESERVED;
125 110 : rtError_t ret = rtGetRunMode(&info);
126 110 : if (ret != RT_ERROR_NONE) {
127 20 : if (ret != ACL_ERROR_RT_FEATURE_NOT_SUPPORT) {
128 20 : IDE_LOGE("Failed to rtGetRunMode, ret=%d", static_cast<int32_t>(ret));
129 20 : return false;
130 : }
131 : }
132 90 : platformInfo = static_cast<uint32_t>(info);
133 90 : return true;
134 : }
135 :
136 : #if !defined(ADUMP_SOC_HOST) || ADUMP_SOC_HOST == 1
137 6 : int32_t AdumpDsmi::DrvGetAPIVersion()
138 : {
139 6 : int32_t halAPIVersion = 0;
140 6 : if (halGetAPIVersion) {
141 6 : int32_t ret = halGetAPIVersion(&halAPIVersion);
142 6 : if (ret != DRV_ERROR_NONE) {
143 0 : IDE_LOGW("Unable to get driver version from halGetAPIVersion, return code is %d.", ret);
144 0 : return -1;
145 : }
146 6 : IDE_LOGD("Received current driver version %d.", halAPIVersion);
147 6 : return halAPIVersion;
148 : }
149 : // If halGetAPIVersion is nullptr, it means that the function is not supported.
150 0 : return halAPIVersion;
151 : }
152 : #endif
153 : }
|