forked from intel/fpga-runtime-for-opencl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathacl_kernel_if.cpp
More file actions
1975 lines (1739 loc) · 74.7 KB
/
acl_kernel_if.cpp
File metadata and controls
1975 lines (1739 loc) · 74.7 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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2013-2021 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
// System headers.
#include <cassert>
#include <cinttypes>
#include <cstdint>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
// Internal headers.
#include <acl_auto_configure.h>
#include <acl_globals.h>
#include <acl_kernel_if.h>
#include <acl_profiler.h>
#include <acl_shipped_board_cfgs.h>
#include <acl_support.h>
#include <acl_thread.h>
#undef TEST_PROFILING_HARDWARE
#ifdef TEST_PROFILING_HARDWARE
extern int acl_hal_mmd_reset_profile_counters(unsigned int physical_device_id,
unsigned int accel_id);
extern int acl_hal_mmd_get_profile_data(unsigned int physical_device_id,
unsigned int accel_id, uint64_t *data,
unsigned int length);
extern int acl_hal_mmd_disable_profile_counters(unsigned int physical_device_id,
unsigned int accel_id);
extern int acl_hal_mmd_enable_profile_counters(unsigned int physical_device_id,
unsigned int accel_id);
#endif
extern int acl_process_profiler_scan_chain(acl_device_op_t *op);
extern int
acl_process_autorun_profiler_scan_chain(unsigned int physical_device_id,
unsigned int accel_id);
// Versioning: This value must be read from addr 0
// For unit tests to work, this defines must match the one in the unit test
// header file
#define KERNEL_VERSION_ID (0xa0c00001)
#define KERNEL_ROM_VERSION_ID (0xa0c00002)
// Version number in the top 16-bits of the 32-bit status register. Used
// to verify that the hardware and HAL have an identical view of the CSR
// address map.
#define CSR_VERSION_ID_18_1 (3)
#define CSR_VERSION_ID_19_1 (4)
#define CSR_VERSION_ID_23_1 (5)
#define CSR_VERSION_ID CSR_VERSION_ID_23_1
// Address map
// For unit tests to work, these defines must match those in the unit test
// header file
#define OFFSET_VERSION_ID ((dev_addr_t)0x0000)
#define OFFSET_KERNEL_CRA_SEGMENT ((dev_addr_t)0x0020)
#define OFFSET_SW_RESET ((dev_addr_t)0x0030)
#define OFFSET_KERNEL_CRA ((dev_addr_t)0x1000)
#define OFFSET_CONFIGURATION_ROM ((dev_addr_t)0x2000)
// Addressses for Kernel System ROM
#define OFFSET_KERNEL_ROM_LOCATION_MSB (0x3ffffff8)
#define OFFSET_KERNEL_ROM_LOCATION_LSB (0x3ffffffc)
#define OFFSET_KERNEL_MAX_ADDRESS (0x3fffffff)
#define KERNEL_CRA_SEGMENT_SIZE (0x1000)
#define KERNEL_ROM_SIZE_BYTES_READ 4
#define KERNEL_ROM_SIZE_BYTES 8
// Byte offsets into the CRA:
// For CSR version >= 5 byte offsets are pushed back with the proper
// value except for the CSR later on in the runtime execution
#define KERNEL_OFFSET_CSR 0
#define KERNEL_OFFSET_PRINTF_BUFFER_SIZE 0x4
#define KERNEL_OFFSET_CSR_PROFILE_CTRL 0xC
#define KERNEL_OFFSET_CSR_PROFILE_DATA 0x10
#define KERNEL_OFFSET_CSR_PROFILE_START_CYCLE 0x18
#define KERNEL_OFFSET_CSR_PROFILE_STOP_CYCLE 0x20
#define KERNEL_OFFSET_FINISH_COUNTER 0x28
#define KERNEL_OFFSET_INVOCATION_IMAGE 0x30
// CSR version >= 5 byte offsets
#define KERNEL_OFFSET_START_REG 0x8
// Backwards compatibility with CSR_VERSION_ID 3
#define KERNEL_OFFSET_INVOCATION_IMAGE_181 0x28
// Bit positions
#define KERNEL_CSR_START 0
#define KERNEL_CSR_DONE 1
#define KERNEL_CSR_STALLED 3
#define KERNEL_CSR_UNSTALL 4
#define KERNEL_CSR_PROFILE_TEMPORAL_STATUS 5
#define KERNEL_CSR_PROFILE_TEMPORAL_RESET 6
#define KERNEL_CSR_LAST_STATUS_BIT KERNEL_CSR_PROFILE_TEMPORAL_RESET
#define KERNEL_CSR_STATUS_BITS_MASK \
((unsigned)((1 << (KERNEL_CSR_LAST_STATUS_BIT + 1)) - 1))
#define KERNEL_CSR_LMEM_INVALID_BANK 11
#define KERNEL_CSR_LSU_ACTIVE 12
#define KERNEL_CSR_WR_ACTIVE 13
#define KERNEL_CSR_BUSY 14
#define KERNEL_CSR_RUNNING 15
#define KERNEL_CSR_FIRST_VERSION_BIT 16
#define KERNEL_CSR_LAST_VERSION_BIT 31
#define KERNEL_CSR_PROFILE_SHIFT64_BIT 0
#define KERNEL_CSR_PROFILE_RESET_BIT 1
#define KERNEL_CSR_PROFILE_ALLOW_PROFILING_BIT 2
#define KERNEL_CSR_PROFILE_LOAD_BUFFER_BIT 3
#define KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT1 4
#define KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT2 5
#define CONFIGURATION_ROM_BYTES 4096
#define RESET_TIMEOUT (2 * 1000 * 1000 * 1000)
#define ACL_KERNEL_READ_BIT(w, b) (((w) >> (b)) & 1)
#define ACL_KERNEL_READ_BIT_RANGE(w, h, l) \
(((w) >> (l)) & ((1 << ((h) - (l) + 1)) - 1))
#define ACL_KERNEL_SET_BIT(w, b) ((w) |= (1 << (b)))
#define ACL_KERNEL_CLEAR_BIT(w, b) ((w) &= (~(1 << (b))))
#define ACL_KERNEL_GET_BIT(b) (unsigned)(1 << (b))
#define ACL_KERNEL_IF_DEBUG_MSG_VERBOSE(k, verbosity, m, ...) \
if (k->io.printf && (k->io.debug_verbosity) >= verbosity) \
do { \
k->io.printf((m), ##__VA_ARGS__); \
} while (0)
#define ACL_KERNEL_IF_DEBUG_MSG(k, m, ...) \
if (k->io.printf && k->io.debug_verbosity > 0) \
do { \
k->io.printf((m), ##__VA_ARGS__); \
} while (0)
//#define POLLING
//#define POLLING_PERIOD (10000) // polling period in ns
typedef time_ns time_us;
// Function declarations
static int acl_kernel_if_read_32b(acl_kernel_if *kern, unsigned int addr,
unsigned int *val);
static int acl_kernel_if_read_64b(acl_kernel_if *kern, unsigned int addr,
uint64_t *val);
static int acl_kernel_if_write_32b(acl_kernel_if *kern, unsigned int addr,
unsigned int val);
static int acl_kernel_if_write_64b(acl_kernel_if *kern, unsigned int addr,
uint64_t val);
static int acl_kernel_rom_cra_read_32b(acl_kernel_if *kern, unsigned int addr,
unsigned int *val);
static int acl_kernel_rom_cra_read_block(acl_kernel_if *kern, unsigned int addr,
char *config_rom, size_t size);
// **************************************************************************
// **************************** Callback Functions **************************
// **************************************************************************
acl_kernel_update_callback acl_kernel_if_update_fn = NULL;
acl_profile_callback acl_kernel_profile_fn = NULL;
acl_process_printf_buffer_callback acl_process_printf_buffer_fn = NULL;
void acl_kernel_if_register_callbacks(
acl_kernel_update_callback kernel_update,
acl_profile_callback profile_callback,
acl_process_printf_buffer_callback process_printf) {
acl_assert_locked();
acl_kernel_if_update_fn = kernel_update;
acl_kernel_profile_fn = profile_callback;
acl_process_printf_buffer_fn = process_printf;
}
// **************************************************************************
// **************************** Utility Functions ***************************
// **************************************************************************
// Returns 0 on success, -1 on failure
static int check_version_id(acl_kernel_if *kern) {
unsigned int version = 0;
int r;
acl_assert_locked();
r = acl_kernel_if_read_32b(kern, OFFSET_VERSION_ID, &version);
ACL_KERNEL_IF_DEBUG_MSG(kern, "Version ID check, read: %08x\n", version);
if (r != 0 ||
(version != KERNEL_VERSION_ID && version != KERNEL_ROM_VERSION_ID)) {
kern->io.printf(
" HAL Kern: Version mismatch! Expected 0x%x but read 0x%x\n",
KERNEL_VERSION_ID, version);
return -1;
} else
return 0;
}
// Loads auto-discovery config string. Returns 0 if was successful.
static int get_auto_discovery_string(acl_kernel_if *kern, char *config_str) {
unsigned int version, rom_address;
char description_size_msb[KERNEL_ROM_SIZE_BYTES_READ + 1];
char description_size_lsb[KERNEL_ROM_SIZE_BYTES_READ + 1];
unsigned int size_location;
unsigned int size, temp_size, rom_size;
size_t r;
int result;
acl_assert_locked();
kern->cur_segment = 0xffffffff;
version = 0;
acl_kernel_if_read_32b(kern, OFFSET_VERSION_ID, &version);
if (version == KERNEL_ROM_VERSION_ID) {
// Read autodiscovery size
size_location = OFFSET_KERNEL_ROM_LOCATION_MSB;
acl_kernel_rom_cra_read_32b(kern, size_location,
(unsigned int *)description_size_msb);
description_size_msb[KERNEL_ROM_SIZE_BYTES_READ] = '\0';
size = (unsigned int)strtol(description_size_msb, NULL, 16);
size = size << 16;
size_location = OFFSET_KERNEL_ROM_LOCATION_LSB;
acl_kernel_rom_cra_read_32b(kern, size_location,
(unsigned int *)description_size_lsb);
description_size_lsb[KERNEL_ROM_SIZE_BYTES_READ] = '\0';
size += (unsigned int)strtol(description_size_lsb, NULL, 16);
// Find beginning address of ROM
temp_size = size + KERNEL_ROM_SIZE_BYTES;
rom_size = 1;
while (temp_size > 0) {
temp_size = temp_size >> 1;
rom_size = rom_size << 1;
}
rom_address = OFFSET_KERNEL_MAX_ADDRESS - rom_size + 1;
config_str[0] = '\0';
result = acl_kernel_rom_cra_read_block(kern, rom_address, config_str,
(size_t)size);
if (result < 0)
return -1;
// Pad autodiscovery with NULL
*(config_str + (unsigned int)size) = '\0';
// Read is verified above
// Set rom_size and r equal to support old flow and big-endian shuffle
rom_size = size;
r = rom_size;
} else if (version == KERNEL_VERSION_ID) {
rom_size = CONFIGURATION_ROM_BYTES;
r = kern->io.read(&kern->io, (dev_addr_t)OFFSET_CONFIGURATION_ROM,
config_str, (size_t)CONFIGURATION_ROM_BYTES);
} else {
kern->io.printf(" HAL Kern: Version ID incorrect\n");
return -1;
}
ACL_KERNEL_IF_DEBUG_MSG(kern, "Read %zu bytes from kernel auto discovery", r);
return (r == rom_size) ? 0 : -1;
}
int acl_kernel_if_is_valid(acl_kernel_if *kern) {
acl_assert_locked_or_sig();
if (kern == NULL || !acl_bsp_io_is_valid(&kern->io))
return 0;
return 1;
}
time_us acl_kernel_if_get_time_us(acl_kernel_if *kern) {
acl_assert_locked_or_sig();
// Cheaply divide by 1000 by actually dividing by 1024
// Could be Nios II's out there without the div enabled
return kern->io.get_time_ns() >> 10;
}
// **************************************************************************
// *************************** Read/write Functions *************************
// **************************************************************************
// 32-bit read and write calls with error checking
// returns 0 on success, -ve on error
static int acl_kernel_if_read_32b(acl_kernel_if *kern, unsigned int addr,
unsigned int *val) {
size_t size;
size_t r;
acl_assert_locked_or_sig();
if (!acl_kernel_if_is_valid(kern)) {
kern->io.printf("HAL Kern Error: Invalid kernel handle used");
return -1;
}
size = sizeof(unsigned int);
r = kern->io.read(&kern->io, (dev_addr_t)addr, (char *)val, (size_t)size);
if (r < size) {
kern->io.printf(
"HAL Kern Error: Read failed from addr %x, read %zu expected %zu\n",
addr, r, size);
return -1;
}
return 0;
}
// 64-bit read and write calls with error checking
// returns 0 on success, -ve on error
static int acl_kernel_if_read_64b(acl_kernel_if *kern, unsigned int addr,
uint64_t *val) {
int size;
int r;
acl_assert_locked_or_sig();
if (!acl_kernel_if_is_valid(kern)) {
kern->io.printf("HAL Kern Error: Invalid kernel handle used");
return -1;
}
size = sizeof(uint64_t);
r = (int)kern->io.read(&kern->io, (dev_addr_t)addr, (char *)val,
(size_t)size);
if (r < size) {
kern->io.printf(
"HAL Kern Error: Read failed from addr %x, read %d expected %d\n", addr,
r, size);
return -1;
}
return 0;
}
// returns 0 on success, -ve on error
static int acl_kernel_rom_read_block(acl_kernel_if *kern, unsigned int addr,
char *config_rom, size_t size) {
size_t r;
if (!acl_kernel_if_is_valid(kern)) {
kern->io.printf("HAL Kern Error: Invalid kernel handle used");
return -1;
}
r = kern->io.read(&kern->io, (dev_addr_t)addr, config_rom, (size_t)size);
if (r < size) {
kern->io.printf(
"HAL Kern Error: Read failed from addr %x, read %zu expected %zu\n",
addr, r, size);
return -1;
}
return 0;
}
// returns 0 on success, -ve on error
static int acl_kernel_if_write_32b(acl_kernel_if *kern, unsigned int addr,
unsigned int val) {
int size;
int r;
acl_assert_locked_or_sig();
if (!acl_kernel_if_is_valid(kern)) {
kern->io.printf("HAL Kern Error: Invalid kernel handle used");
return -1;
}
size = sizeof(unsigned int);
r = (int)kern->io.write(&kern->io, (dev_addr_t)addr, (char *)&val,
(size_t)size);
if (r < size) {
kern->io.printf("HAL Kern Error: Write failed to addr %x with value %x, "
"wrote %d expected %d\n",
addr, val, r, size);
return -1;
}
return 0;
}
// returns 0 on success, -ve on error
static int acl_kernel_if_write_64b(acl_kernel_if *kern, unsigned int addr,
uint64_t val) {
int size;
int r;
acl_assert_locked();
if (!acl_kernel_if_is_valid(kern)) {
kern->io.printf("HAL Kern Error: Invalid kernel handle used");
return -1;
}
size = sizeof(uint64_t);
r = (int)kern->io.write(&kern->io, (dev_addr_t)addr, (char *)&val,
(size_t)size);
if (r < size) {
kern->io.printf(
"HAL Kern Error: Write failed to addr %x with value %" PRIx64 ", "
"wrote %d, expected %d\n",
addr, val, r, size);
return -1;
}
return 0;
}
// return 0 on success, -ve on error
static int acl_kernel_if_write_block(acl_kernel_if *kern, unsigned int addr,
unsigned int *val, size_t size) {
size_t r;
unsigned int *temp_val = val;
size_t aligned_size = size;
if (!acl_kernel_if_is_valid(kern)) {
kern->io.printf("HAL Kern Error: Invalid kernel handle used");
return -1;
}
// In Windows S5 MMD, block writes to kernel interface must have write size
// divisible by 4 bytes If not divisible by 4, subtract the modulos and add 4
// bytes to the write size.
#ifdef _WIN32
if (size % 4) {
aligned_size = size - (size % 4) + 4;
temp_val = (unsigned int *)acl_malloc(aligned_size + 1);
memcpy(temp_val, val, size);
}
#endif
r = kern->io.write(&kern->io, (dev_addr_t)addr, (char *)temp_val,
(size_t)aligned_size);
#ifdef _WIN32
if (size % 4) {
acl_free(temp_val);
}
#endif
if (r < aligned_size) {
kern->io.printf("HAL Kern Error: Write failed to addr %x with value %x, "
"wrote %zu expected %zu\n",
addr, *val, r, aligned_size);
return -1;
}
return 0;
}
// The kernel cra is segmented (or windowed) using a Qsys address expander.
// We must first write the upper bits of the address we want to access to the
// extender, then perform our read/write operation using the offset within
// that segment as the address.
//
// Sets the segment window bits and returns the offset needed within
static uintptr_t acl_kernel_cra_set_segment(acl_kernel_if *kern,
unsigned int accel_id,
unsigned int addr) {
uintptr_t logical_addr =
kern->accel_csr[accel_id].address + addr - OFFSET_KERNEL_CRA;
uintptr_t segment = logical_addr & ((size_t)0 - (KERNEL_CRA_SEGMENT_SIZE));
uintptr_t segment_offset = logical_addr % KERNEL_CRA_SEGMENT_SIZE;
acl_assert_locked_or_sig();
// The kernel cra master is hardcoded to 30 addr bits, so we can use 32-bit
// interface here.
if (kern->cur_segment != segment) {
acl_kernel_if_write_32b(kern, OFFSET_KERNEL_CRA_SEGMENT,
(unsigned int)segment);
kern->cur_segment = segment;
acl_kernel_if_read_32b(kern, OFFSET_KERNEL_CRA_SEGMENT,
(unsigned int *)&segment);
}
return segment_offset;
}
// Set CRA segment for ROM. No accel_id
static uintptr_t acl_kernel_cra_set_segment_rom(acl_kernel_if *kern,
unsigned int addr) {
uintptr_t segment = addr & ((size_t)0 - (KERNEL_CRA_SEGMENT_SIZE));
uintptr_t segment_offset = addr % KERNEL_CRA_SEGMENT_SIZE;
if (kern->cur_segment != segment) {
acl_kernel_if_write_32b(kern, OFFSET_KERNEL_CRA_SEGMENT,
(unsigned int)segment);
kern->cur_segment = segment;
acl_kernel_if_read_32b(kern, OFFSET_KERNEL_CRA_SEGMENT,
(unsigned int *)&segment);
}
return segment_offset;
}
static int acl_kernel_cra_read(acl_kernel_if *kern, unsigned int accel_id,
unsigned int addr, unsigned int *val) {
assert(kern->cra_ring_root_exist);
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
acl_assert_locked_or_sig();
return acl_kernel_if_read_32b(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset, val);
}
int acl_kernel_cra_read_64b(acl_kernel_if *kern, unsigned int accel_id,
unsigned int addr, uint64_t *val) {
assert(kern->cra_ring_root_exist);
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
acl_assert_locked_or_sig();
return acl_kernel_if_read_64b(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset, val);
}
// Read 32b from kernel ROM
static int acl_kernel_rom_cra_read_32b(acl_kernel_if *kern, unsigned int addr,
unsigned int *val) {
uintptr_t segment_offset = acl_kernel_cra_set_segment_rom(kern, addr);
return acl_kernel_if_read_32b(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset, val);
}
// Read size number of bytes from ROM
static int acl_kernel_rom_cra_read_block(acl_kernel_if *kern, unsigned int addr,
char *config_rom, size_t size) {
uintptr_t segment_offset = acl_kernel_cra_set_segment_rom(kern, addr);
uintptr_t segment = addr & ((size_t)0 - (KERNEL_CRA_SEGMENT_SIZE));
uintptr_t segment_end =
(addr + size) & ((size_t)0 - (KERNEL_CRA_SEGMENT_SIZE));
unsigned int step = 0;
unsigned int transfer = 0;
int r = 0;
if (segment != segment_end) {
while (step < size) {
transfer = ((unsigned int)size > (step + KERNEL_CRA_SEGMENT_SIZE))
? KERNEL_CRA_SEGMENT_SIZE
: (unsigned int)size - step;
r = acl_kernel_rom_read_block(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset,
config_rom, transfer);
config_rom += transfer;
step += transfer;
segment_offset = acl_kernel_cra_set_segment_rom(kern, addr + step);
if (r < 0)
return r;
}
} else {
r = acl_kernel_rom_read_block(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset,
config_rom, size);
}
return r;
}
static int acl_kernel_cra_write(acl_kernel_if *kern, unsigned int accel_id,
unsigned int addr, unsigned int val) {
assert(kern->cra_ring_root_exist);
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
acl_assert_locked_or_sig();
return acl_kernel_if_write_32b(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset, val);
}
static int acl_kernel_cra_write_64b(acl_kernel_if *kern, unsigned int accel_id,
unsigned int addr, uint64_t val) {
assert(kern->cra_ring_root_exist);
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
acl_assert_locked();
return acl_kernel_if_write_64b(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset, val);
}
static int acl_kernel_cra_write_block(acl_kernel_if *kern,
unsigned int accel_id, unsigned int addr,
unsigned int *val, size_t size) {
assert(kern->cra_ring_root_exist);
uintptr_t segment_offset = acl_kernel_cra_set_segment(kern, accel_id, addr);
uintptr_t logical_addr =
kern->accel_csr[accel_id].address + addr - OFFSET_KERNEL_CRA;
uintptr_t segment = logical_addr & ((size_t)0 - (KERNEL_CRA_SEGMENT_SIZE));
uintptr_t logical_addr_end =
kern->accel_csr[accel_id].address + addr + size - OFFSET_KERNEL_CRA;
uintptr_t segment_end =
logical_addr_end & ((size_t)0 - (KERNEL_CRA_SEGMENT_SIZE));
unsigned int step = 0;
if (segment != segment_end) {
ACL_KERNEL_IF_DEBUG_MSG_VERBOSE(
kern, 2, ":: Segment change during block write detected.\n");
while (step < size) {
segment = (logical_addr + step) & ((size_t)0 - (KERNEL_CRA_SEGMENT_SIZE));
if (kern->cur_segment != segment) {
acl_kernel_if_write_block(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset, val,
step);
segment_offset =
acl_kernel_cra_set_segment(kern, accel_id, addr + step);
logical_addr =
kern->accel_csr[accel_id].address + addr + step - OFFSET_KERNEL_CRA;
val += step;
size -= step;
step = 0;
} else {
step += (unsigned)sizeof(int);
}
}
}
return acl_kernel_if_write_block(
kern, (unsigned)OFFSET_KERNEL_CRA + (unsigned)segment_offset, val, size);
}
// Private utility function to issue a command to the profile hardware
static int acl_kernel_if_issue_profile_hw_command(acl_kernel_if *kern,
cl_uint accel_id,
unsigned bit_id,
int set_bit) {
unsigned int profile_ctrl_val;
int status;
acl_assert_locked_or_sig();
assert(acl_kernel_if_is_valid(kern));
status = acl_kernel_cra_read(
kern, accel_id, KERNEL_OFFSET_CSR_PROFILE_CTRL + kern->cra_address_offset,
&profile_ctrl_val);
if (status)
return status;
ACL_KERNEL_IF_DEBUG_MSG(
kern, ":: Issue profile HW command:: Accelerator %d old csr is %x.\n",
accel_id, profile_ctrl_val);
if (set_bit) {
ACL_KERNEL_SET_BIT(profile_ctrl_val, bit_id);
} else { // Clear bit
ACL_KERNEL_CLEAR_BIT(profile_ctrl_val, bit_id);
}
ACL_KERNEL_IF_DEBUG_MSG(
kern, ":: Issue profile HW command:: Accelerator %d new csr is %x.\n",
accel_id, profile_ctrl_val);
status = acl_kernel_cra_write(
kern, accel_id, KERNEL_OFFSET_CSR_PROFILE_CTRL + kern->cra_address_offset,
profile_ctrl_val);
if (status)
return status;
return 0;
}
// Private utility function to issue a command to the profile hardware
// See ip/src/common/lsu_top.v.tmpl, ip/src/common/hld_iord.sv.tmpl, &
// ip/src/common/hld_iowr.sv.tmpl for what 0-3 represents
int acl_kernel_if_set_profile_shared_control(acl_kernel_if *kern,
cl_uint accel_id) {
acl_assert_locked();
ACL_KERNEL_IF_DEBUG_MSG(
kern,
":: Set the shared control for the profile counters:: Accelerator %d.\n",
accel_id);
int return_val = 0;
int set_to = get_env_profile_shared_counter_val();
// Valid control options are 0-3. If set to -1 shared counters are off.
// Anything else the control hasn't been pulled from the ENV variable
if (set_to >= 0 && set_to <= 3) {
if (set_to == 0) {
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT1, 0);
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT2, 0);
} else if (set_to == 1) {
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT1, 1);
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT2, 0);
} else if (set_to == 2) {
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT1, 0);
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT2, 1);
} else if (set_to == 3) {
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT1, 1);
return_val |= acl_kernel_if_issue_profile_hw_command(
kern, accel_id, KERNEL_CSR_PROFILE_SHARED_CONTROL_BIT2, 1);
} else {
ACL_KERNEL_IF_DEBUG_MSG(kern,
":: Setting shared control value - env variable "
"was not in the range 0-3 %d.\n",
accel_id);
}
} else if (set_to == -1) {
ACL_KERNEL_IF_DEBUG_MSG(kern,
":: Not setting shared control value - shared "
"counters not enabled %d.\n",
accel_id);
} else {
ACL_KERNEL_IF_DEBUG_MSG(
kern,
":: Not setting shared control value - env variable was not set %d.\n",
accel_id);
}
return return_val;
}
// **************************************************************************
// ************** Utility Functions that use Read/Write calls ***************
// **************************************************************************
// **************************************************************************
// ***************************** Public Functions ***************************
// **************************************************************************
// This routine queries the PCIe bus for devices that match our ACL supported
// board configurations and populates the acl_system_def and
// ACL_PCIE_DEVICE_DESRIPTION structures in the ACL and HAL layers respectively.
//
// Returns 0 on success, -ve otherwise
int acl_kernel_if_init(acl_kernel_if *kern, acl_bsp_io bsp_io,
acl_system_def_t *sysdef) {
char description_size_msb[KERNEL_ROM_SIZE_BYTES_READ + 1];
char description_size_lsb[KERNEL_ROM_SIZE_BYTES_READ + 1];
unsigned int size_location, version, size;
int result = 0;
acl_assert_locked();
assert(acl_bsp_io_is_valid(&bsp_io));
kern->io = bsp_io;
kern->num_accel = 0;
kern->cur_segment = 0xffffffff;
kern->accel_csr = NULL;
kern->accel_perf_mon = NULL;
kern->accel_num_printfs = NULL;
kern->autorun_profiling_kernel_id = -1;
// The simulator doesn't have any kernel interface information until the aocx
// is loaded, which happens later.
if (acl_platform.offline_mode == ACL_CONTEXT_MPSIM) {
std::string err_msg;
auto parse_result = acl_load_device_def_from_str(
acl_shipped_board_cfgs[0].cfg, sysdef->device[0].autodiscovery_def,
err_msg);
// Fill in definition for all device global memory
// Simulator does not have any global memory interface information until the
// actual aocx is loaded. (Note this is only a problem for simulator not
// hardware run, in hardware run, we can communicate with BSP to query
// memory interface information). In the flow today, the USM device
// allocation call happens before aocx is loaded. The aocx is loaded when
// clCreateProgram is called, which typically happen on first kernel launch
// in sycl runtime. In order to prevent the USM device allocation from
// failing on mutli global memory system, initialize as much global memory
// system as possible for simulation flow. However there are a few downside:
// 1. The address range/size may not be exactly the same as the one that is
// in aocx, but this is not too large of a problem because runtime first fit
// allocation algorithm will fill the lowest address range first. Unless
// user requested more than what is availble.
// 2. it potentially occupied more space than required
// 3. will not error out when user requested a non-existing device global
// memory because we are using ACL_MAX_GLOBAL_MEM for num_global_mem_systems
sysdef->device[0].autodiscovery_def.num_global_mem_systems =
ACL_MAX_GLOBAL_MEM;
for (int i = 0; i < ACL_MAX_GLOBAL_MEM; i++) {
sysdef->device[0].autodiscovery_def.global_mem_defs[i] =
sysdef->device[0].autodiscovery_def.global_mem_defs[0];
}
if (parse_result)
sysdef->num_devices = 1;
// Override the device name to the simulator.
sysdef->device[0].autodiscovery_def.name = ACL_MPSIM_DEVICE_NAME;
return 0;
}
if (check_version_id(kern) != 0) {
kern->io.printf("Hardware version ID differs from version expected by "
"software. Either:\n");
kern->io.printf(" a) Ensure your compiled design was generated by the "
"same ACL build\n");
kern->io.printf(" currently in use, OR\n");
kern->io.printf(
" b) The host can not communicate with the compiled kernel.\n");
assert(0);
}
version = 0;
acl_kernel_if_read_32b(kern, OFFSET_VERSION_ID, &version);
ACL_KERNEL_IF_DEBUG_MSG_VERBOSE(kern, 2, ":: Kernel version 0x%x\n",
version);
size_t config_str_len = 0;
if (version == KERNEL_ROM_VERSION_ID) {
acl_kernel_if_reset(kern);
size_location = OFFSET_KERNEL_ROM_LOCATION_MSB;
result = acl_kernel_rom_cra_read_32b(kern, size_location,
(unsigned int *)description_size_msb);
description_size_msb[KERNEL_ROM_SIZE_BYTES_READ] = '\0';
if (result < 0 || ((unsigned int)*description_size_msb == 0xFFFF)) {
ACL_KERNEL_IF_DEBUG_MSG(
kern, "MSB ROM size not found. No response from PCIe.\n");
return -1;
}
size = (unsigned int)strtol(description_size_msb, NULL, 16);
size = size << 16;
size_location = OFFSET_KERNEL_ROM_LOCATION_LSB;
result = acl_kernel_rom_cra_read_32b(kern, size_location,
(unsigned int *)description_size_lsb);
description_size_lsb[KERNEL_ROM_SIZE_BYTES_READ] = '\0';
if (result < 0 || ((unsigned int)*description_size_lsb == 0xFFFF)) {
ACL_KERNEL_IF_DEBUG_MSG(
kern, "LSB ROM size not found. No response from PCIe.\n");
return -1;
}
size += (unsigned int)strtol(description_size_lsb, NULL, 16);
if ((size == 0) || (size == 0xffffffff)) {
ACL_KERNEL_IF_DEBUG_MSG(kern,
"ROM size is invalid. ROM does not exist\n");
ACL_KERNEL_IF_DEBUG_MSG(kern, "ROM size read was: %i\n", size);
return -1;
}
ACL_KERNEL_IF_DEBUG_MSG_VERBOSE(kern, 2, ":: ROM Size is: 0x%s%s : %i\n",
description_size_msb, description_size_lsb,
size);
config_str_len = size + 1;
} else if (version == KERNEL_VERSION_ID) {
config_str_len = CONFIGURATION_ROM_BYTES + 1;
} else {
kern->io.printf(" HAL Kern: Version ID incorrect\n");
return -1;
}
std::vector<char> config_str(config_str_len);
result |= get_auto_discovery_string(kern, config_str.data());
std::string config_string{config_str.data()};
if (result != 0) {
ACL_KERNEL_IF_DEBUG_MSG(kern, "Failed to read from kernel auto discovery");
return -1;
}
ACL_KERNEL_IF_DEBUG_MSG(kern, "\nPCIE Auto-Discovered Param String: %s\n",
config_str.data());
// Returns 1 if success
std::string auto_config_err_str;
auto load_result = acl_load_device_def_from_str(
config_string, sysdef->device[kern->physical_device_id].autodiscovery_def,
auto_config_err_str);
if (load_result)
++sysdef->num_devices;
result |= load_result ? 0 : -1;
if (result != 0) {
kern->io.printf("%s\n", auto_config_err_str.c_str());
ACL_KERNEL_IF_DEBUG_MSG(kern, "First 16 values:\n ");
for (unsigned i = 0; i < 16; i++)
ACL_KERNEL_IF_DEBUG_MSG(kern, "%02x ", config_str[i]);
ACL_KERNEL_IF_DEBUG_MSG(kern, "\n");
return -1;
}
result = acl_kernel_if_update(
sysdef->device[kern->physical_device_id].autodiscovery_def, kern);
return result;
}
// Given a devdef, update the kernel's internal state
// Returns 0 on success
int acl_kernel_if_update(const acl_device_def_autodiscovery_t &devdef,
acl_kernel_if *kern) {
acl_assert_locked();
// Setup the accelerators
if (kern->io.debug_verbosity > 0) {
for (const auto &acc : devdef.accel) {
ACL_KERNEL_IF_DEBUG_MSG(kern, "Found Kernel {%s}\n",
acc.iface.name.c_str());
// Show info about the arguments themselves
for (const auto &arg : acc.iface.args) {
// These are strictly for debug, but we need a way to keep this up to
// date
static const char *acl_addr_space_names[] = {
"ACL_ARG_ADDR_NONE", "ACL_ARG_ADDR_LOCAL", "ACL_ARG_ADDR_GLOBAL",
"ACL_ARG_ADDR_CONSTANT"};
static const char *acl_arg_categories[] = {
"ACL_ARG_BY_VALUE", "ACL_ARG_MEM_OBJ", "ACL_ARG_SAMPLER"};
ACL_KERNEL_IF_DEBUG_MSG(
kern, " ... param %s (addr_space=%s,category=%s,size=%d)\n",
arg.name.c_str(), acl_addr_space_names[arg.addr_space],
acl_arg_categories[arg.category], arg.size);
}
// Show other info about kernel.
ACL_KERNEL_IF_DEBUG_MSG(kern,
" ... compile work-group size = (%d, %d, %d)\n",
(int)acc.compile_work_group_size[0],
(int)acc.compile_work_group_size[1],
(int)acc.compile_work_group_size[2]);
ACL_KERNEL_IF_DEBUG_MSG(kern, " ... is work-group invariant = %d\n",
acc.is_workgroup_invariant);
ACL_KERNEL_IF_DEBUG_MSG(kern, " ... num vectors lanes = %d\n",
acc.num_vector_lanes);
ACL_KERNEL_IF_DEBUG_MSG(kern, " ... max work-group size = %d\n",
acc.max_work_group_size);
}
}
acl_kernel_if_close(kern);
// Initialize whether the cra_ring_root exist in the design
kern->cra_ring_root_exist = devdef.cra_ring_root_exist;
ACL_KERNEL_IF_DEBUG_MSG(kern, "Number of cra_ring_root : %d\n",
kern->cra_ring_root_exist);
// Setup the PCIe HAL Structures
kern->num_accel = static_cast<unsigned>(devdef.accel.size());
ACL_KERNEL_IF_DEBUG_MSG(kern, "Number of Accelerators : %d\n",
kern->num_accel);
if (kern->num_accel > 0) {
// Allocations for each kernel
kern->accel_csr = (acl_kernel_if_addr_range *)acl_malloc(
kern->num_accel * sizeof(acl_kernel_if_addr_range));
assert(kern->accel_csr);
kern->accel_perf_mon = (acl_kernel_if_addr_range *)acl_malloc(
kern->num_accel * sizeof(acl_kernel_if_addr_range));
assert(kern->accel_perf_mon);
kern->accel_num_printfs =
(unsigned int *)acl_malloc(kern->num_accel * sizeof(unsigned int));
assert(kern->accel_num_printfs);
// The Kernel CSR registers
// The new and improved config ROM give us the address *offsets* from
// the first kernel CSR, and the range of each kernel CSR.
for (unsigned ii = 0; ii < devdef.accel.size(); ++ii) {
kern->accel_csr[ii].address =
OFFSET_KERNEL_CRA + devdef.hal_info[ii].csr.address;
kern->accel_csr[ii].bytes = devdef.hal_info[ii].csr.num_bytes;
ACL_KERNEL_IF_DEBUG_MSG(
kern, "Kernel_%s CSR { 0x%08" PRIxPTR ", 0x%08" PRIxPTR " }\n",
devdef.accel[ii].iface.name.c_str(), kern->accel_csr[ii].address,
kern->accel_csr[ii].bytes);
}
// The Kernel performance monitor registers
for (unsigned ii = 0; ii < devdef.accel.size(); ++ii) {
kern->accel_perf_mon[ii].address =
OFFSET_KERNEL_CRA + devdef.hal_info[ii].perf_mon.address;
kern->accel_perf_mon[ii].bytes = devdef.hal_info[ii].perf_mon.num_bytes;
ACL_KERNEL_IF_DEBUG_MSG(
kern, "Kernel_%s perf_mon { 0x%08" PRIxPTR ", 0x%08" PRIxPTR " }\n",
devdef.accel[ii].iface.name.c_str(), kern->accel_perf_mon[ii].address,
kern->accel_perf_mon[ii].bytes);
// printf info
kern->accel_num_printfs[ii] =
static_cast<unsigned>(devdef.accel[ii].printf_format_info.size());
ACL_KERNEL_IF_DEBUG_MSG(kern, "Kernel_%s(id=%d) num_printfs is %d\n",
devdef.accel[ii].iface.name.c_str(), ii,
kern->accel_num_printfs[ii]);
}
// Find which of the kernels (if any) is the autorun profiling kernel
for (unsigned ii = 0; ii < devdef.accel.size(); ++ii) {
if (devdef.accel[ii].iface.name == ACL_PROFILE_AUTORUN_KERNEL_NAME) {
kern->autorun_profiling_kernel_id = (int)ii;
break;
}
}
kern->streaming_control_signal_names.clear();
kern->streaming_control_signal_names.reserve(devdef.accel.size());
for (const auto &accel : devdef.accel) {
std::optional<acl_streaming_kernel_control_info> signal_names;
if (accel.streaming_control_info_available) {
signal_names = accel.streaming_control_info;
}
kern->streaming_control_signal_names.emplace_back(signal_names);
}
}
// Do reset
acl_kernel_if_reset(kern);
// Set interleaving mode based on autodiscovery
for (unsigned ii = 0; ii < devdef.num_global_mem_systems; ii++) {
if ((unsigned int)devdef.global_mem_defs[ii].config_addr > 0 &&
(unsigned int)devdef.global_mem_defs[ii].num_global_banks > 1) {
ACL_KERNEL_IF_DEBUG_MSG(kern,
"Configuring interleaving for memory %d (%s), "
"burst_interleaved = %d\n",
ii, devdef.global_mem_defs[ii].name.c_str(),
devdef.global_mem_defs[ii].burst_interleaved);
acl_kernel_if_write_32b(
kern, (unsigned int)devdef.global_mem_defs[ii].config_addr,
devdef.global_mem_defs[ii].burst_interleaved ? 0x0 : 0x1u);
}
}
kern->last_kern_update = 0;
// Set up the structures to store state information about the device
if (kern->num_accel > 0) {
kern->accel_job_ids =