forked from Xilinx/XRT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXBUtilities.cpp
More file actions
972 lines (837 loc) · 34.8 KB
/
XBUtilities.cpp
File metadata and controls
972 lines (837 loc) · 34.8 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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2019-2022 Xilinx, Inc
// Copyright (C) 2022-2026 Advanced Micro Devices, Inc. All rights reserved.
// ------ I N C L U D E F I L E S -------------------------------------------
#include "XBUtilities.h"
#include "XBUtilitiesCore.h"
// Local - Include Files
#include "common/error.h"
#include "common/info_vmr.h"
#include "common/utils.h"
#include "common/message.h"
#include "common/system.h"
#include "common/sysinfo.h"
#include "common/smi.h"
#include "common/module_loader.h"
#include "xrt/detail/version-slim.h"
// 3rd Party Library - Include Files
#include <boost/algorithm/string/split.hpp>
#include <boost/format.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <boost/tokenizer.hpp>
// System - Include Files
#include <iostream>
#include <map>
#include <regex>
#include <filesystem>
#ifdef _WIN32
# pragma warning( disable : 4189 4100 4996)
# pragma comment(lib, "Ws2_32.lib")
/* need to link the lib for the following to work */
# define be32toh ntohl
#else
# include <unistd.h> // SUDO check
#endif
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#define PALIGN(p, a) ((char *)(ALIGN((unsigned long long)(p), (a))))
#define GET_CELL(p) (p += 4, *((const uint32_t *)(p-4)))
// ------ C O N S T A N T V A R I A B L E S ---------------------------------
static const uint32_t FDT_BEGIN_NODE = 0x1;
static const uint32_t FDT_PROP = 0x3;
static const uint32_t FDT_END = 0x9;
// ------ L O C A L F U N C T I O N S A N D S T R U C T S ------------------
struct fdt_header {
uint32_t magic;
uint32_t totalsize;
uint32_t off_dt_struct;
uint32_t off_dt_strings;
uint32_t off_mem_rsvmap;
uint32_t version;
uint32_t last_comp_version;
uint32_t boot_cpuid_phys;
uint32_t size_dt_strings;
uint32_t size_dt_struct;
};
namespace xq = xrt_core::query;
// ------ F U N C T I O N S ---------------------------------------------------
std::string
XBUtilities::Timer::format_time(std::chrono::duration<double> duration)
{
auto hours = std::chrono::duration_cast<std::chrono::hours>(duration);
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration);
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
std::string formatted_time;
if (hours.count() != 0)
formatted_time += std::to_string(hours.count()) + "h ";
if (hours.count() != 0 || minutes.count() != 0)
formatted_time += std::to_string(minutes.count() % 60) + "m ";
formatted_time += std::to_string(seconds.count() % 60) + "s";
return formatted_time;
}
boost::property_tree::ptree
XBUtilities::get_available_devices(bool inUserDomain)
{
xrt_core::device_collection deviceCollection;
collect_devices(std::set<std::string> {"_all_"}, inUserDomain, deviceCollection);
boost::property_tree::ptree pt;
for (const auto & device : deviceCollection) {
boost::property_tree::ptree pt_dev;
pt_dev.put("bdf", xrt_core::query::pcie_bdf::to_string(xrt_core::device_query<xrt_core::query::pcie_bdf>(device)));
const auto device_class = xrt_core::device_query_default<xrt_core::query::device_class>(device, xrt_core::query::device_class::type::alveo);
pt_dev.put("device_class", xrt_core::query::device_class::enum_to_str(device_class));
//user pf doesn't have mfg node. Also if user pf is loaded, it means that the card is not is mfg mode
const auto is_mfg = xrt_core::device_query_default<xrt_core::query::is_mfg>(device, false);
//if factory mode
if (is_mfg) {
auto mGoldenVer = xrt_core::device_query<xrt_core::query::mfg_ver>(device);
std::string vbnv = "xilinx_" + xrt_core::device_query<xrt_core::query::board_name>(device) + "_GOLDEN_"+ std::to_string(mGoldenVer);
pt_dev.put("vbnv", vbnv);
pt_dev.put("id", "n/a");
pt_dev.put("instance","n/a");
}
else {
switch (device_class) {
case xrt_core::query::device_class::type::alveo:
pt_dev.put("vbnv", xrt_core::device_query<xrt_core::query::rom_vbnv>(device));
break;
case xrt_core::query::device_class::type::ryzen:
pt_dev.put("name", xrt_core::device_query<xrt_core::query::rom_vbnv>(device));
break;
}
if (device_class == xrt_core::query::device_class::type::ryzen) {
try { // Ryzen/NPU: derive UUID from PCIe BDF
pt_dev.put("id", xrt_core::query::pcie_bdf::to_uuid(
xrt_core::device_query<xrt_core::query::pcie_bdf>(device)).to_string());
}
catch(...) {}
}
else {
try { //1RP
pt_dev.put("id", xrt_core::query::rom_time_since_epoch::to_string(
xrt_core::device_query<xrt_core::query::rom_time_since_epoch>(device)));
}
catch(...) {}
try { //2RP - overwrites 1RP if available
auto logic_uuids = xrt_core::device_query<xrt_core::query::logic_uuids>(device);
if (!logic_uuids.empty())
pt_dev.put("id", xrt_core::query::interface_uuids::to_uuid_upper_string(logic_uuids[0]));
}
catch(...) {}
}
try {
const auto fw_ver = xrt_core::device_query<xq::firmware_version>(device, xq::firmware_version::firmware_type::npu_firmware);
std::string version = "N/A";
if (fw_ver.major != 0 || fw_ver.minor != 0 || fw_ver.patch != 0 || fw_ver.build != 0) {
version = boost::str(boost::format("%u.%u.%u.%u")
% fw_ver.major % fw_ver.minor % fw_ver.patch % fw_ver.build);
}
pt_dev.put("firmware_version", version);
}
catch(...) {
// The npu firmware wasn't added
}
try {
const auto cert_fw_ver = xrt_core::device_query<xq::cert_firmware_version>(device);
std::string version = "N/A";
if (cert_fw_ver.major != 0 || cert_fw_ver.minor != 0 || cert_fw_ver.hotfix != 0 || cert_fw_ver.build != 0) {
version = boost::str(boost::format("%u.%u.%u.%u")
% cert_fw_ver.major % cert_fw_ver.minor % cert_fw_ver.hotfix % cert_fw_ver.build);
}
pt_dev.put("cert_firmware_version", version);
}
catch(...) {
// The CERT firmware wasn't added
}
try {
const auto aie_tiles = xrt_core::device_query<xq::aie_tiles_stats>(device);
std::string topology = boost::str(boost::format("%ux%u") % aie_tiles.rows % aie_tiles.cols);
pt_dev.put("aie_topology", topology);
}
catch (...) {
// AIE topology wasn't added
}
try {
// Map hardware type to AIE architecture version string
const auto& pcie_id = xrt_core::device_query<xrt_core::query::pcie_id>(device);
xrt_core::smi::smi_hardware_config smi_hrdw;
auto hardware_type = smi_hrdw.get_hardware_type(pcie_id);
switch (hardware_type) {
case xrt_core::smi::smi_hardware_config::hardware_type::phx:
pt_dev.put("aie_architecture_version", "aie2");
break;
case xrt_core::smi::smi_hardware_config::hardware_type::stxA0:
case xrt_core::smi::smi_hardware_config::hardware_type::stxB0:
case xrt_core::smi::smi_hardware_config::hardware_type::stxH:
case xrt_core::smi::smi_hardware_config::hardware_type::krk1:
pt_dev.put("aie_architecture_version", "aie2p");
break;
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f0:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f1:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f2:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f3:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_B01:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_B02:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_B03:
pt_dev.put("aie_architecture_version", "aie4");
break;
case xrt_core::smi::smi_hardware_config::hardware_type::aie2ps:
pt_dev.put("aie_architecture_version", "aie2ps");
break;
default:
pt_dev.put("aie_architecture_version", "N/A");
break;
}
}
catch (...) {
// AIE architecture version wasn't added
}
try {
auto instance = xrt_core::device_query<xrt_core::query::instance>(device);
std::string pf = device->is_userpf() ? "user" : "mgmt";
pt_dev.put("instance",boost::str(boost::format("%s(inst=%d)") % pf % instance));
}
catch(const xrt_core::query::exception&) {
// The instance wasn't added
}
}
pt_dev.put("is_ready", xrt_core::device_query_default<xrt_core::query::is_ready>(device, true));
pt.push_back(std::make_pair("", pt_dev));
}
return pt;
}
std::string
XBUtilities::str_available_devs(bool _inUserDomain)
{
//gather available devices for user to pick from
std::stringstream available_devs;
available_devs << "\n Available devices:\n";
boost::property_tree::ptree available_devices = XBUtilities::get_available_devices(_inUserDomain);
for (auto& kd : available_devices) {
boost::property_tree::ptree& dev = kd.second;
if (boost::iequals(dev.get<std::string>("device_class"), xrt_core::query::device_class::enum_to_str(xrt_core::query::device_class::type::alveo)))
available_devs << boost::format(" [%s] : %s\n") % dev.get<std::string>("bdf") % dev.get<std::string>("vbnv");
if (boost::iequals(dev.get<std::string>("device_class"), xrt_core::query::device_class::enum_to_str(xrt_core::query::device_class::type::ryzen)))
available_devs << boost::format(" [%s] : %s\n") % dev.get<std::string>("bdf") % dev.get<std::string>("name");
}
return available_devs.str();
}
/*
* Parse the bdf passed in by the user to an index
* DDDD:BB:DD.F where domain and function are optional
*/
static uint16_t
bdf2index(const std::string& bdfstr, bool _inUserDomain)
{
if (!std::regex_match(bdfstr,std::regex("[A-Za-z0-9:.]+")))
throw std::runtime_error("Invalid BDF format. Please specify valid BDF" + XBUtilities::str_available_devs(_inUserDomain));
std::vector<std::string> tokens;
boost::split(tokens, bdfstr, boost::is_any_of(":"));
int radix = 16;
uint16_t domain = 0;
uint16_t bus = 0;
uint16_t dev = 0;
uint16_t func = std::numeric_limits<uint16_t>::max();
// check if we have 2-3 tokens: domain, bus, device.function
// domain is optional
if (tokens.size() <= 1 || tokens.size() > 3)
throw std::runtime_error(boost::str(boost::format("Invalid BDF '%s'. Please spcify the BDF using 'DDDD:BB:DD.F' format") % bdfstr) + XBUtilities::str_available_devs(_inUserDomain));
std::reverse(std::begin(tokens), std::end(tokens));
try {
//check if func was specified. func is optional
auto pos_of_func = tokens[0].find('.');
if (pos_of_func != std::string::npos) {
dev = static_cast<uint16_t>(std::stoi(std::string(tokens[0].substr(0, pos_of_func)), nullptr, radix));
func = static_cast<uint16_t>(std::stoi(std::string(tokens[0].substr(pos_of_func+1)), nullptr, radix));
}
else
dev = static_cast<uint16_t>(std::stoi(std::string(tokens[0]), nullptr, radix));
bus = static_cast<uint16_t>(std::stoi(std::string(tokens[1]), nullptr, radix));
// domain is not mandatory if it is "0000"
if(tokens.size() > 2)
domain = static_cast<uint16_t>(std::stoi(std::string(tokens[2]), nullptr, radix));
} catch (const std::invalid_argument&) {
throw std::runtime_error(boost::str(boost::format("Invalid BDF '%s'") % bdfstr) + XBUtilities::str_available_devs(_inUserDomain));
}
// Iterate through the available devices to find a BDF match
// This must not open any devices! Doing do would slow down the software
// quite a bit and cause other undesirable side affects
auto devices = _inUserDomain ? xrt_core::get_total_devices(true).first : xrt_core::get_total_devices(false).first;
for (decltype(devices) i = 0; i < devices; i++) {
auto bdf = xrt_core::get_bdf_info(i, _inUserDomain);
//if the user specifies func, compare
//otherwise safely ignore
auto cmp_func = [bdf](uint16_t func)
{
if (func != std::numeric_limits<uint16_t>::max())
return func == std::get<3>(bdf);
return true;
};
if (domain == std::get<0>(bdf) && bus == std::get<1>(bdf) && dev == std::get<2>(bdf) && cmp_func(func))
return static_cast<uint16_t>(i);
}
throw std::runtime_error(boost::str(boost::format("Specified device BDF '%s' not found") % bdfstr) + XBUtilities::str_available_devs(_inUserDomain));
}
static std::shared_ptr<xrt_core::device>
get_device_internal(xrt_core::device::id_type index, bool in_user_domain)
{
static std::mutex mutex;
std::lock_guard guard(mutex);
if (in_user_domain) {
static std::vector<std::shared_ptr<xrt_core::device>> user_devices(xrt_core::get_total_devices(true).first, nullptr);
if (user_devices.size() <= index )
throw std::runtime_error("no device present with index " + std::to_string(index));
if (!user_devices[index])
user_devices[index] = xrt_core::get_userpf_device(index);
return user_devices[index];
}
static std::vector<std::shared_ptr<xrt_core::device>> mgmt_devices(xrt_core::get_total_devices(false).first, nullptr);
if (mgmt_devices.size() <= index )
throw std::runtime_error("no device present with index " + std::to_string(index));
if (!mgmt_devices[index])
mgmt_devices[index] = xrt_core::get_mgmtpf_device(index);
return mgmt_devices[index];
}
/*
* Map the string passed in by the user to a valid index
* Supports pcie and edge devices
*/
static uint16_t
str2index(const std::string& str, bool _inUserDomain)
{
//throw an error if no devices are present
uint64_t devices = _inUserDomain ? xrt_core::get_total_devices(true).first : xrt_core::get_total_devices(false).first;
if (devices == 0)
throw std::runtime_error("No devices found");
try {
int idx(boost::lexical_cast<int>(str));
auto device = get_device_internal(idx, _inUserDomain);
auto bdf = xrt_core::device_query<xrt_core::query::pcie_bdf>(device);
if (std::get<0>(bdf) == 0 && std::get<1>(bdf) == 0 && std::get<2>(bdf) == 0)
return std::get<3>(bdf);
} catch (...) {
/* not an edge device so safe to ignore this error */
}
return bdf2index(str, _inUserDomain);
}
void
XBUtilities::xrt_version_cmp(bool isUserDomain)
{
boost::property_tree::ptree pt_xrt;
xrt_core::sysinfo::get_xrt_info(pt_xrt);
const boost::property_tree::ptree empty_ptree;
std::string xrt_version = pt_xrt.get<std::string>("version", "<unknown>");
const boost::property_tree::ptree& available_drivers = pt_xrt.get_child("drivers", empty_ptree);
const std::string expected_drv_name = isUserDomain ? "xocl" : "xclmgmt";
for(const auto& drv : available_drivers) {
const boost::property_tree::ptree& driver = drv.second;
const std::string drv_name = driver.get<std::string>("name", "<unknown>");
const std::string drv_version = driver.get<std::string>("version", "<unknown>");
if (drv_name.compare(expected_drv_name) == 0 && drv_version.compare("unknown") != 0 && xrt_version.compare(drv_version) != 0) {
const auto & warnMsg = boost::str(boost::format("WARNING: Unexpected %s version (%s) was found. Expected %s, to match XRT tools.") % expected_drv_name % drv_version % xrt_version);
std::cout << warnMsg << std::endl;
}
}
}
void
XBUtilities::collect_devices( const std::set<std::string> &_deviceBDFs,
bool _inUserDomain,
xrt_core::device_collection &_deviceCollection)
{
// -- If the collection is empty then do nothing
if (_deviceBDFs.empty())
return;
// -- Collect all of devices if the "all" option is used...anywhere in the collection
if (_deviceBDFs.find("_all_") != _deviceBDFs.end()) {
xrt_core::device::id_type total = 0;
try {
// If there are no devices in the server a runtime exception is thrown in mgmt.cpp probe()
total = (xrt_core::device::id_type) xrt_core::get_total_devices(_inUserDomain /*isUser*/).first;
} catch (...) {
/* Do nothing */
}
// No devices found
if (total == 0)
return;
// Now collect the devices and add them to the collection
for(xrt_core::device::id_type index = 0; index < total; ++index) {
try {
_deviceCollection.push_back(get_device_internal(index, _inUserDomain));
} catch (const std::runtime_error& e) {
throw std::runtime_error(e.what());
}
}
return;
}
// -- Collect the devices by name
for (const auto & deviceBDF : _deviceBDFs) {
auto index = str2index(deviceBDF, _inUserDomain); // Can throw
_deviceCollection.push_back(get_device_internal(index, _inUserDomain));
}
}
static void
check_versal_boot(const std::shared_ptr<xrt_core::device> &device)
{
std::vector<std::string> warnings;
try {
const auto is_default = xrt_core::vmr::get_vmr_status(device.get(), xrt_core::vmr::vmr_status_type::has_fpt);
if (!is_default)
warnings.push_back("Versal Platform is NOT migrated");
} catch (const xrt_core::error& e) {
warnings.push_back(e.what());
}
try {
const auto is_default = xrt_core::vmr::get_vmr_status(device.get(), xrt_core::vmr::vmr_status_type::boot_on_default);
if (!is_default)
warnings.push_back("Versal Platform is NOT in default boot");
} catch (const xrt_core::error& e) {
warnings.push_back(e.what());
}
try {
const std::string unavail = "N/A";
const std::string zeroes = "0.0.0";
auto cur_ver = xrt_core::device_query_default<xrt_core::query::hwmon_sdm_active_msp_ver>(device, unavail);
auto exp_ver = xrt_core::device_query_default<xrt_core::query::hwmon_sdm_target_msp_ver>(device, unavail);
cur_ver = (boost::equals(cur_ver, zeroes)) ? unavail : cur_ver;
exp_ver = (boost::equals(exp_ver, zeroes)) ? unavail : exp_ver;
if ((boost::equals(cur_ver, unavail) || boost::equals(exp_ver, unavail)) && !boost::equals(cur_ver, exp_ver))
warnings.push_back("SC version data missing. Upgrade your shell");
else if (!boost::equals(cur_ver, exp_ver))
warnings.push_back(boost::str(boost::format("Invalid SC version. Expected: %s Current: %s") % exp_ver % cur_ver));
} catch (const xrt_core::error& e) {
warnings.push_back(e.what());
}
if (warnings.empty())
return;
const std::string star_line = "***********************************************************";
std::cout << star_line << "\n";
std::cout << "* WARNING WARNING WARNING *\n";
// Print all warnings
for (const auto& warning : warnings) {
// Subtract the:
// 1. Side stars
// 2. Single space next to the side star
const size_t available_space = star_line.size() - 2 - 2;
// Account for strings who are larger than the star line
size_t warning_index = 0;
while (warning_index < warning.size()) {
// Extract the largest possible string from the warning
const auto warning_msg = warning.substr(warning_index, available_space);
// Update the index so the next substring is valid
warning_index += warning_msg.size();
const auto side_spaces = available_space - warning_msg.size();
// The left side should be larger than the right if there is an imbalance
const size_t left_spaces = (side_spaces % 2 == 0) ? side_spaces / 2 : (side_spaces / 2) + 1;
const size_t right_spaces = side_spaces / 2;
std::cout << "* " << std::string(left_spaces, ' ') << warning_msg << std::string(right_spaces, ' ') << " *\n";
}
}
std::cout << star_line << "\n";
}
std::shared_ptr<xrt_core::device>
XBUtilities::get_device( const std::string &deviceBDF, bool in_user_domain)
{
// -- If the deviceBDF is empty then do nothing
if (deviceBDF.empty())
throw std::runtime_error("Please specify a device using --device option" + XBUtilities::str_available_devs(in_user_domain));
// -- Collect the devices by name
auto index = str2index(deviceBDF, in_user_domain); // Can throw
std::shared_ptr<xrt_core::device> device;
device = get_device_internal(index, in_user_domain);
if (xrt_core::device_query_default<xq::is_versal>(device, false))
check_versal_boot(device);
return device;
}
static std::string
deviceMapping(const xrt_core::query::device_class::type type)
{
switch (type) {
case xrt_core::query::device_class::type::alveo:
return "alveo";
case xrt_core::query::device_class::type::ryzen:
return "aie";
}
return "";
}
std::string
XBUtilities::get_device_class(const std::string &deviceBDF, bool in_user_domain)
{
if (deviceBDF.empty())
return "";
std::shared_ptr<xrt_core::device> device = get_device(boost::algorithm::to_lower_copy(deviceBDF), in_user_domain);
auto device_class = xrt_core::device_query_default<xrt_core::query::device_class>(device, xrt_core::query::device_class::type::alveo);
return deviceMapping(device_class);
}
void
XBUtilities::can_proceed_or_throw(const std::string& info, const std::string& error)
{
std::cout << info << "\n";
if (!XBUtilities::can_proceed(getForce()))
throw xrt_core::system_error(ECANCELED, error);
}
void
XBUtilities::print_exception(const std::system_error& e)
{
try {
// Remove the type of error from the message.
const std::string msg = std::regex_replace(e.what(), std::regex(std::string(": ") + e.code().message()), "");
if ((!msg.empty()) && (!boost::icontains(msg, "operation canceled")))
std::cerr << boost::format("ERROR: %s\n") % msg;
}
catch (const std::exception&)
{
// exception can occur while formatting message, print normal message
std::cerr << e.what() << std::endl;
}
}
std::vector<char>
XBUtilities::get_axlf_section(const std::string& filename, axlf_section_kind kind)
{
std::ifstream in(filename, std::ios::binary);
if (!in.is_open())
throw std::runtime_error(boost::str(boost::format("Can't open %s") % filename));
// Read axlf from dsabin file to find out number of sections in total.
axlf a;
size_t sz = sizeof (axlf);
in.read(reinterpret_cast<char *>(&a), sz);
if (!in.good())
throw std::runtime_error(boost::str(boost::format("Can't read axlf from %s") % filename));
// Reread axlf from dsabin file, including all sections headers.
// Sanity check for number of sections coming from user input file
if (a.m_header.m_numSections > 10000)
throw std::runtime_error("Incorrect file passed in");
sz = sizeof (axlf) + sizeof (axlf_section_header) * (a.m_header.m_numSections - 1);
std::vector<char> top(sz);
in.seekg(0);
in.read(top.data(), sz);
if (!in.good())
throw std::runtime_error(boost::str(boost::format("Can't read axlf and section headers from %s") % filename));
const axlf *ap = reinterpret_cast<const axlf *>(top.data());
auto section = ::xclbin::get_axlf_section(ap, kind);
if (!section)
throw std::runtime_error("Section not found");
std::vector<char> buf(section->m_sectionSize);
in.seekg(section->m_sectionOffset);
in.read(buf.data(), section->m_sectionSize);
return buf;
}
std::vector<std::string>
XBUtilities::get_uuids(const void *dtbuf)
{
std::vector<std::string> uuids;
struct fdt_header *bph = (struct fdt_header *)dtbuf;
uint32_t version = be32toh(bph->version);
uint32_t off_dt = be32toh(bph->off_dt_struct);
const char *p_struct = (const char *)dtbuf + off_dt;
uint32_t off_str = be32toh(bph->off_dt_strings);
const char *p_strings = (const char *)dtbuf + off_str;
const char *p, *s;
uint32_t tag;
int sz;
p = p_struct;
uuids.clear();
while ((tag = be32toh(GET_CELL(p))) != FDT_END) {
if (tag == FDT_BEGIN_NODE) {
s = p;
p = PALIGN(p + strlen(s) + 1, 4);
continue;
}
if (tag != FDT_PROP)
continue;
sz = be32toh(GET_CELL(p));
s = p_strings + be32toh(GET_CELL(p));
if (version < 16 && sz >= 8)
p = PALIGN(p, 8);
if (!strcmp(s, "logic_uuid")) {
uuids.insert(uuids.begin(), std::string(p));
}
else if (!strcmp(s, "interface_uuid")) {
uuids.push_back(std::string(p));
}
p = PALIGN(p + sz, 4);
}
return uuids;
}
static const std::map<std::string, xrt_core::query::reset_type> reset_map = {
{ "hot", xrt_core::query::reset_type(xrt_core::query::reset_key::hot, "HOT Reset", "", "mgmt_reset", "Please make sure xocl driver is unloaded.", "1") },
{ "kernel", xrt_core::query::reset_type(xrt_core::query::reset_key::kernel, "KERNEL Reset", "", "mgmt_reset", "Please make sure no application is currently running.", "2") },
{ "ert", xrt_core::query::reset_type(xrt_core::query::reset_key::ert, "ERT Reset", "", "mgmt_reset", "", "3") },
{ "ecc", xrt_core::query::reset_type(xrt_core::query::reset_key::ecc, "ECC Reset", "", "ecc_reset", "", "4") },
{ "soft-kernel", xrt_core::query::reset_type(xrt_core::query::reset_key::soft_kernel, "SOFT KERNEL Reset", "", "mgmt_reset", "", "5") },
{ "aie", xrt_core::query::reset_type(xrt_core::query::reset_key::aie, "AIE Reset", "", "mgmt_reset", "", "6") },
{ "user", xrt_core::query::reset_type(xrt_core::query::reset_key::user, "HOT Reset", "", "", "", "") },
};
xrt_core::query::reset_type
XBUtilities::str_to_reset_obj(const std::string& str)
{
auto it = reset_map.find(str);
if (it != reset_map.end())
return it->second;
throw xrt_core::error(str + " is invalid. Please specify a valid reset type");
}
std::string
XBUtilities::string_to_UUID(std::string str)
{
//make sure that a UUID is passed in
assert(str.length() == 32);
std::string uuid = "";
//positions to insert hyphens
//before: 00000000000000000000000000000000
std::vector<int> pos = {8, 4, 4, 4};
//before: 00000000-0000-0000-0000-000000000000
for(auto const p : pos) {
std::string token = str.substr(0, p);
boost::to_upper(token);
uuid.append(token + "-");
str.erase(0, p);
}
boost::to_upper(str);
uuid.append(str);
return uuid;
}
static const std::map<uint64_t, std::string> oemid_map = {
{0x10da, "Xilinx"},
{0x02a2, "Dell"},
{0x12a1, "IBM"},
{0xb85c, "HP"},
{0x2a7c, "Super Micro"},
{0x4a66, "Lenovo"},
{0xbd80, "Inspur"},
{0x12eb, "Amazon"},
{0x2b79, "Google"}
};
std::string
XBUtilities::parse_oem_id(const std::string& oemid)
{
uint64_t oem_id_val = 0;
std::stringstream ss;
try {
oem_id_val = std::stoul(oemid, nullptr, 16);
} catch (const std::exception&) {
//failed to parse oemid to hex value, ignore erros and print original value
}
auto oemstr = oemid_map.find(oem_id_val);
return oemstr != oemid_map.end() ? oemstr->second : "N/A";
}
static const std::map<std::string, std::string> clock_map = {
{"DATA_CLK", "Data"},
{"KERNEL_CLK", "Kernel"},
{"SYSTEM_CLK", "System"},
};
std::string
XBUtilities::parse_clock_id(const std::string& id)
{
auto clock_str = clock_map.find(id);
if (clock_str != clock_map.end())
return clock_str->second;
throw xrt_core::error(std::errc::invalid_argument);
}
uint64_t
XBUtilities::string_to_base_units(std::string str, const unit& conversion_unit)
{
boost::algorithm::trim(str);
if(str.empty())
throw xrt_core::error(std::errc::invalid_argument);
int factor;
switch(conversion_unit) {
case unit::bytes :
factor = 1024;
break;
case unit::Hertz :
factor = 1000;
break;
default :
throw xrt_core::error(std::errc::invalid_argument);
}
std::string units = "B";
if(std::isalpha(str.back())) {
units = str.back();
str.pop_back();
}
uint64_t unit_value = 0;
boost::to_upper(units);
if(units.compare("B") == 0)
unit_value = 1;
else if(units.compare("K") == 0)
unit_value = factor;
else if(units.compare("M") == 0)
unit_value = factor * factor;
else if(units.compare("G") == 0)
unit_value = factor * factor * factor;
else
throw xrt_core::error(std::errc::invalid_argument);
boost::algorithm::trim(str);
uint64_t size = 0;
try {
size = std::stoll(str);
}
catch (const std::exception&) {
//out of range, invalid argument ex
throw xrt_core::error(std::errc::invalid_argument);
}
size *= unit_value;
return size;
}
std::string
XBUtilities::
get_xrt_pretty_version()
{
std::stringstream ss;
boost::property_tree::ptree pt_xrt;
xrt_core::sysinfo::get_xrt_info(pt_xrt);
const boost::property_tree::ptree available_devices = XBUtilities::get_available_devices(true);
XBUtilities::fill_xrt_versions(pt_xrt, ss, available_devices);
return ss.str();
}
void
XBUtilities::
fill_xrt_versions(const boost::property_tree::ptree& pt_xrt,
std::stringstream& output,
const boost::property_tree::ptree& available_devices)
{
boost::property_tree::ptree empty_ptree;
output << boost::format(" %-20s : %s\n") % "Version" % pt_xrt.get<std::string>("version", "N/A");
auto branch = pt_xrt.get<std::string>("branch", "N/A");
auto hash = pt_xrt.get<std::string>("hash", "N/A");
auto build_hash_date = pt_xrt.get<std::string>("build_hash_date", "N/A");
if (!branch.empty() && !boost::iequals(branch, "N/A"))
output << boost::format(" %-20s : %s\n") % "Branch" % branch;
if (!hash.empty() && !boost::iequals(hash, "N/A"))
output << boost::format(" %-20s : %s\n") % "Hash" % hash;
if (!build_hash_date.empty() && !boost::iequals(build_hash_date, "N/A"))
output << boost::format(" %-20s : %s\n") % "Hash Date" % build_hash_date;
const boost::property_tree::ptree& available_drivers = pt_xrt.get_child("drivers", empty_ptree);
for(auto& drv : available_drivers) {
const boost::property_tree::ptree& driver = drv.second;
auto drv_name = driver.get<std::string>("name", "N/A");
auto drv_hash = driver.get<std::string>("hash", "N/A");
auto drv_ver = driver.get<std::string>("version", "N/A");
// If driver is in-tree then use OS kernel version
if (drv_ver == "unknown" || drv_ver == "N/A") {
static auto pt_os = xrt_core::sysinfo::get_os_info();
drv_ver = pt_os.get<std::string>("release", "N/A");
}
std::string drv_label = boost::iequals(drv_name, "N/A") ? drv_name : drv_name.append(" Version");
if (drv_hash == "unknown" || drv_hash == "N/A")
output << boost::format(" %-20s : %s\n") % drv_label % drv_ver;
else
output << boost::format(" %-20s : %s, %s\n") % drv_label % drv_ver % drv_hash;
}
if (available_devices.empty())
return;
try {
const boost::property_tree::ptree& dev = available_devices.begin()->second;
auto fw_ver = dev.get<std::string>("firmware_version", "N/A");
auto device_class = dev.get<std::string>("device_class", "");
if (device_class == xrt_core::query::device_class::enum_to_str(xrt_core::query::device_class::type::ryzen)) {
if (fw_ver != "N/A")
output << boost::format(" %-20s : %s\n") % "NPU Firmware Version" % fw_ver;
auto cert_fw_ver = dev.get<std::string>("cert_firmware_version", "N/A");
if (cert_fw_ver != "N/A")
output << boost::format(" %-20s: %s\n") % "CERT Firmware Version" % cert_fw_ver;
}
else
output << boost::format(" %-20s : %s\n") % "Firmware Version" % fw_ver;
}
catch (...) {
//no device available
}
}
xrt_core::runner::artifacts_repository
XBUtilities::
extract_artifacts_from_archive(const xrt_core::archive* archive,
const std::vector<std::string>& artifact_names)
{
xrt_core::runner::artifacts_repository artifacts_repo;
for (const auto& artifact_name : artifact_names) {
try {
artifacts_repo.add_data(artifact_name, archive->data(artifact_name));
} catch (const std::exception& /*e*/) {
//Ignore files that are not found
}
}
return artifacts_repo;
}
void
XBUtilities::
report_missing_archive(const xrt_core::device* device)
{
std::string archive_path = xrt_core::device_query<xrt_core::query::archive_path>(device);
std::string xrt_version = xrt_build_version;
std::string install_path = get_archive_install_path(xrt_version);
// Extract archive name from archive path (e.g., "amdxdna/bins/xrt_smi_phx.a" -> "xrt_smi_phx.a")
std::filesystem::path arch_path(archive_path);
std::string archive_name = arch_path.filename().string();
XBUtilities::error(boost::str(boost::format(
"Archive not found: %s\n"
"To install the required archive, run:\n"
" smi_install_archive.sh %s %s\n"
"Or download manually from:\n"
" https://github.com/Xilinx/VTD/tree/main/archive\n"
"And place it in: %s/")
% archive_path
% archive_name
% xrt_version
% install_path));
}
std::unique_ptr<xrt_core::archive>
XBUtilities::
open_archive(const xrt_core::device* device)
{
// Archives only applicable to Ryzen devices
if (xrt_core::device_query<xrt_core::query::device_class>(device) == xrt_core::query::device_class::type::alveo)
return nullptr;
std::unique_ptr<xrt_core::archive> archive;
try {
std::string archive_path = xrt_core::device_query<xrt_core::query::archive_path>(device);
std::string full_archive_path = xrt_core::environment::platform_path(archive_path).string();
archive = std::make_unique<xrt_core::archive>(full_archive_path);
} catch (const std::exception& e) {
report_missing_archive(device);
throw std::runtime_error(e.what());
}
return archive;
}
// Helper function to determine if hardware type is STRX or NPU3
bool
XBUtilities::
is_strix_hardware(xrt_core::smi::smi_hardware_config::hardware_type hw_type)
{
switch (hw_type) {
case xrt_core::smi::smi_hardware_config::hardware_type::stxA0:
case xrt_core::smi::smi_hardware_config::hardware_type::stxB0:
case xrt_core::smi::smi_hardware_config::hardware_type::stxH:
case xrt_core::smi::smi_hardware_config::hardware_type::krk1:
case xrt_core::smi::smi_hardware_config::hardware_type::phx:
return true;
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f0:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f1:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f2:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_f3:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_B01:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_B02:
case xrt_core::smi::smi_hardware_config::hardware_type::npu3_B03:
return false;
default:
throw std::runtime_error("Unsupported hardware type");
}
}
std::string
XBUtilities::
get_archive_install_path(const std::string& xrt_version)
{
// NOLINTNEXTLINE(concurrency-mt-unsafe) - called only during error reporting, not performance critical
const char* home = std::getenv("HOME");
if (!home || !*home)
throw std::runtime_error("HOME environment variable is not set");
return std::string(home) + "/.local/share/xrt/" + xrt_version + "/amdxdna/bins";
}
void
XBUtilities::
printAdvancedDisclaimer()
{
std::cout << "-------------------------------------------------------------------------\n";
std::cout << " DISCLAIMER (xrt-smi --advanced) \n";
std::cout << "You are running a developer command that may change system configuration.\n";
std::cout << " Continue only if you understand the risks. \n";
std::cout << "-------------------------------------------------------------------------\n";
}