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