-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdevice.cpp
More file actions
430 lines (377 loc) · 14.6 KB
/
device.cpp
File metadata and controls
430 lines (377 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
* The MIT License (MIT)
* Copyright (c) 2025-2026 Advanced Micro Devices, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file device.cpp
* @brief Device class implementation.
*/
#include <vrt/device.hpp>
#include <unistd.h>
#include <cctype>
#include <cerrno>
#include <chrono>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <limits>
#include <thread>
#include <vrtd/bar.hpp>
#include <vrt/utils/filesystem_cache.hpp>
namespace vrt {
namespace impl {
namespace {
// Normalize a user-supplied BDF to board-level "DDDD:BB:DD" for vrtd lookup.
// Strips function digit (.F) if present, prepends domain 0000: if missing.
std::string normalizeBdfForVrtd(const std::string& bdf) {
std::string result = bdf;
// Strip function digit
auto dot = result.rfind('.');
if (dot != std::string::npos) {
std::cerr << "Warning: BDF '" << bdf
<< "' contains a PF function number; "
<< "stripping " << result.substr(dot)
<< " — use board address instead"
<< std::endl;
result = result.substr(0, dot);
}
// Prepend domain if missing (only one colon means short BDF)
const auto firstColon = result.find(':');
const auto lastColon = result.rfind(':');
if (firstColon == std::string::npos || firstColon == lastColon) {
result = "0000:" + result;
}
return result;
}
// Normalize a user-supplied BDF to board-level "BB:DD" (no domain, no function).
std::string normalizeBdfLegacy(const std::string& bdf) {
std::string result = bdf;
// Strip function digit
auto dot = result.rfind('.');
if (dot != std::string::npos) {
result = result.substr(0, dot);
}
// Strip domain
const auto firstColon = result.find(':');
const auto lastColon = result.rfind(':');
if (firstColon != std::string::npos && firstColon != lastColon) {
result = result.substr(firstColon + 1);
}
return result;
}
std::string shellQuote(const std::string& value) {
std::string quoted = "'";
for (char c : value) {
if (c == '\'') {
quoted += "'\\''";
} else {
quoted += c;
}
}
quoted += "'";
return quoted;
}
std::string makeExecFromBinaryDirCommand(const std::string& execPath) {
const std::filesystem::path path(execPath);
const std::string dir = path.parent_path().string();
const std::string file = path.filename().string();
if (dir.empty() || file.empty()) {
return shellQuote(execPath);
}
return "cd " + shellQuote(dir) + " && exec ./" + shellQuote(file);
}
bool parseEmuArgIndex(const std::string& argName, std::size_t& outIdx) {
if (argName.size() < 4 || argName.rfind("arg", 0) != 0) {
return false;
}
std::size_t value = 0;
bool hasDigit = false;
for (std::size_t i = 3; i < argName.size(); ++i) {
unsigned char c = static_cast<unsigned char>(argName[i]);
if (!std::isdigit(c)) {
return false;
}
hasDigit = true;
value = value * 10 + static_cast<std::size_t>(c - '0');
}
if (!hasDigit) {
return false;
}
outIdx = value;
return true;
}
void applyEmuManifestToKernels(const std::string& manifestPath, std::map<std::string, Kernel>& kernels) {
if (manifestPath.empty()) {
throw std::runtime_error("EMU manifest missing from vrtbin");
}
std::ifstream in(manifestPath);
if (!in.is_open()) {
throw std::runtime_error("EMU manifest path unreadable: " + manifestPath);
}
Json::Value root;
Json::Reader reader;
if (!reader.parse(in, root) || !root.isObject()) {
throw std::runtime_error("Failed to parse EMU manifest: " + manifestPath);
}
std::size_t appliedCallKinds = 0;
std::size_t appliedFetchRoutes = 0;
const Json::Value manifestKernels = root["kernels"];
if (!manifestKernels.isArray()) {
throw std::runtime_error("EMU manifest missing required array: kernels");
}
if (manifestKernels.isArray()) {
for (const auto& k : manifestKernels) {
if (!k.isObject()) continue;
const std::string instance = k.get("instance", "").asString();
if (instance.empty()) continue;
auto it = kernels.find(instance);
if (it == kernels.end()) continue;
std::vector<std::string> kinds;
const Json::Value callArgs = k["call_args"];
if (callArgs.isArray()) {
for (const auto& ca : callArgs) {
if (!ca.isObject()) continue;
const std::string argName = ca.get("arg", "").asString();
const std::string kind = ca.get("kind", "").asString();
std::size_t idx = 0;
if (kind.empty() || !parseEmuArgIndex(argName, idx)) continue;
if (idx >= kinds.size()) {
kinds.resize(idx + 1);
}
kinds[idx] = kind;
}
}
if (!kinds.empty()) {
it->second.setEmuCallArgKinds(kinds);
appliedCallKinds += 1;
}
}
}
std::map<std::string, std::map<uint32_t, std::string>> fetchRoutesByKernel;
const Json::Value fetch = root["fetch"];
if (!fetch.isObject()) {
throw std::runtime_error("EMU manifest missing required object: fetch");
}
const Json::Value fetchScalar = fetch["scalar"];
if (!fetchScalar.isArray()) {
throw std::runtime_error("EMU manifest missing required array: fetch.scalar");
}
if (fetchScalar.isArray()) {
for (const auto& route : fetchScalar) {
if (!route.isObject()) continue;
const std::string functionName = route.get("function", "").asString();
const std::string argName = route.get("arg", "").asString();
if (functionName.empty() || argName.empty()) continue;
const Json::Value source = route["source"];
if (!source.isObject()) continue;
const Json::Value regOff = source["register_offset"];
if (!regOff.isUInt() && !regOff.isInt()) continue;
const uint32_t offset = regOff.asUInt();
fetchRoutesByKernel[functionName][offset] = argName;
}
}
for (auto& kv : fetchRoutesByKernel) {
auto it = kernels.find(kv.first);
if (it == kernels.end()) continue;
it->second.setEmuFetchScalarArgByOffset(kv.second);
appliedFetchRoutes += kv.second.size();
}
utils::Logger::log(utils::LogLevel::DEBUG, __PRETTY_FUNCTION__,
"Applied EMU manifest metadata: call-kind kernels={}, fetch routes={}",
appliedCallKinds, appliedFetchRoutes);
}
} // namespace
Device::Device(const std::string& bdf, const std::string& vrtbinPath, bool program,
ProgramType programType)
: vrtbin(vrtbinPath, bdf) {
this->bdf = normalizeBdfLegacy(bdf);
this->bdfFull = normalizeBdfForVrtd(bdf);
this->allocator = new Allocator();
this->systemMap = this->vrtbin.getSystemMapPath();
this->pdiPath = this->vrtbin.getPdiPath();
this->pdiPaths = this->vrtbin.getPdiPaths();
this->programType = programType;
this->zmqServer = std::make_shared<ZmqServer>();
this->platform = vrtbin.getPlatform();
if (platform == Platform::HARDWARE) {
vrtdSession = std::make_shared<vrtd::Session>();
vrtdDevice = vrtdSession->getDeviceByBdf(bdfFull);
if (program) {
programDevice();
}
parseSystemMap();
if (program && !kernels.empty()) {
sleep(1); // wait for device to be ready after programming before accessing BAR
}
if (vrtdDevice.has_value()) {
if (clockFreq > CLOCK_MAX_FREQ) {
utils::Logger::log(utils::LogLevel::WARN, __PRETTY_FUNCTION__,
"Clock frequency {} exceeds maximum frequency {}", clockFreq, CLOCK_MAX_FREQ);
vrtdDevice->setUserClockRate(static_cast<uint32_t>(CLOCK_MAX_FREQ));
}
}
} else if (platform == Platform::EMULATION) {
parseSystemMap();
applyEmuManifestToKernels(this->vrtbin.getEmulationManifest(), kernels);
std::string emulationExecPath = this->vrtbin.getEmulationExec();
if (emulationExecPath.empty()) {
throw std::runtime_error("Emulation executable vpp_emu not found in vrtbin");
}
if (::access(emulationExecPath.c_str(), X_OK) != 0) {
throw std::runtime_error("Emulation executable is not runnable: " + emulationExecPath +
" (" + std::strerror(errno) + ")");
}
const std::string emuCommand = makeExecFromBinaryDirCommand(emulationExecPath);
runtimeThread = std::thread([emuCommand]() { std::system(emuCommand.c_str()); });
} else {
parseSystemMap();
std::string simulationExecPath = this->vrtbin.getSimulationExec();
if (simulationExecPath.empty()) {
throw std::runtime_error("Simulation executable vpp_sim not found in vrtbin");
}
if (::access(simulationExecPath.c_str(), X_OK) != 0) {
throw std::runtime_error("Simulation executable is not runnable: " +
simulationExecPath + " (" + std::strerror(errno) + ")");
}
const std::string simCommand = makeExecFromBinaryDirCommand(simulationExecPath);
runtimeThread = std::thread([simCommand]() { std::system(simCommand.c_str()); });
Json::Value command;
command["command"] = "start";
zmqServer->sendCommand(command);
}
if (platform == Platform::HARDWARE && vrtdDevice.has_value()) {
for (auto& qdmaCon : qdmaConnections) {
qdmaIntfs.emplace_back(new QdmaIntf(*vrtdDevice, qdmaCon.getQid(),
qdmaCon.getDirection()));
}
}
}
Device::~Device() {
cleanup();
delete allocator;
allocator = nullptr;
}
void Device::parseSystemMap() {
XMLParser parser(systemMap);
parser.parseXML();
clockFreq = parser.getClockFrequency();
this->platform = parser.getPlatform();
kernels = parser.getKernels();
std::optional<vrtd::Bar> barHandle = std::nullopt;
if (platform == Platform::HARDWARE && vrtdDevice.has_value()) {
barHandle = vrtdDevice->getBar(bar);
}
for (auto&kernel : kernels) {
kernel.second.setPlatform(platform);
kernel.second.setVrtdBar(barHandle);
kernel.second.setServer(zmqServer);
}
this->qdmaConnections = parser.getQdmaConnections();
}
Kernel Device::getKernel(const std::string& name) {
auto it = kernels.find(name);
if (it == kernels.end()) {
throw std::runtime_error("Kernel '" + name + "' not found in system_map metadata");
}
return it->second;
}
void Device::cleanup() {
if (cleanupDone) {
return;
}
cleanupDone = true;
if (platform == Platform::HARDWARE) {
for (auto qdmaIntf_ : qdmaIntfs) {
delete qdmaIntf_;
}
qdmaIntfs.clear();
} else if (platform == Platform::EMULATION || platform == Platform::SIMULATION) {
Json::Value exit;
exit["command"] = "exit";
zmqServer->sendCommand(exit);
}
if (runtimeThread.joinable()) {
runtimeThread.join();
}
}
std::string Device::getBdf() { return bdf; }
void Device::programDevice() {
if (pdiPaths.empty() && !pdiPath.empty()) {
pdiPaths.push_back(pdiPath);
}
if (pdiPaths.empty()) {
throw std::runtime_error("No PDI files found for programming");
}
for (const auto& pdi : pdiPaths) {
utils::Logger::log(utils::LogLevel::INFO, __PRETTY_FUNCTION__,
"Programming PDI via vrtd design writer {}", pdi);
getVrtdDevice().designWriteFile(pdi);
}
}
void Device::setFrequency(uint64_t freq) {
if (platform == Platform::HARDWARE) {
if (freq > clockFreq) {
utils::Logger::log(utils::LogLevel::WARN, __PRETTY_FUNCTION__,
"Setting frequency {}, which is higher than max frequency {}", freq,
clockFreq);
}
if (freq > std::numeric_limits<uint32_t>::max()) {
throw std::runtime_error("Requested frequency exceeds vrtd clock API limits");
}
getVrtdDevice().setUserClockRate(static_cast<uint32_t>(freq));
}
}
uint64_t Device::getFrequency() {
if (platform == Platform::HARDWARE) {
return getVrtdDevice().getUserClockRate();
} else {
return 0;
}
}
uint64_t Device::getMaxFrequency() {
if (platform == Platform::HARDWARE) {
return clockFreq;
} else {
return 0;
}
}
void Device::findPlatform() {
XMLParser parser(systemMap);
parser.parseXML();
this->platform = parser.getPlatform();
}
Platform Device::getPlatform() { return platform; }
std::shared_ptr<ZmqServer> Device::getZmqServer() { return zmqServer; }
std::vector<QdmaConnection> Device::getQdmaConnections() { return qdmaConnections; }
Allocator* Device::getAllocator() { return allocator; }
vrtd::Device& Device::getVrtdDevice() {
if (!vrtdDevice.has_value()) {
throw std::runtime_error("vrtd device not initialized");
}
return *vrtdDevice;
}
const vrtd::Device& Device::getVrtdDevice() const {
if (!vrtdDevice.has_value()) {
throw std::runtime_error("vrtd device not initialized");
}
return *vrtdDevice;
}
std::vector<QdmaIntf*> Device::getQdmaInterfaces() { return qdmaIntfs; }
} // namespace impl
} // namespace vrt