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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
/*****************************************************************************
 *
 *    Copyright (c) 2016-2026 by Bitmain Technologies Inc. All rights reserved.
 *
 *    The material in this file is confidential and contains trade secrets
 *    of Bitmain Technologies Inc. This is proprietary information owned by
 *    Bitmain Technologies Inc. No part of this work may be disclosed,
 *    reproduced, copied, transmitted, or used in any way for any purpose,
 *    without the express written permission of Bitmain Technologies Inc.
 *
 *****************************************************************************/

/**************************************************************************
 * bmlib_runtime defines interfaces that operate TPU devices.
 * The functions can be divided into serveral categories.
 * 1) device handle creation and destroy
 * 2) memory help functions
 * 3) global memory allocation and free
 * 4) data transfer between host and device
 * 5) data transfer within device memory
 * 6) api send and synchronization
 * 7) global memory map and coherence
 * 8) trace and profile
 * 9) power management
 * 10) miscellaneous functions
 *************************************************************************/

#ifndef BMLIB_RUNTIME_H_
#define BMLIB_RUNTIME_H_
#if defined(_WIN32) && !defined(__MINGW32__)
    #include <vadefs.h>
    #define DECL_EXPORT __declspec(dllexport)
    #define DECL_IMPORT __declspec(dllimport)
#else
	#include <stdbool.h>
	#include <stddef.h>
	#include <stdarg.h>
    #define DECL_EXPORT
    #define DECL_IMPORT
#endif

#if defined(__cplusplus)
extern "C" {
#endif

typedef enum {
  MODULE_CDMA = 0,
  MODULE_GDMA = 1,
  MODULE_TPU = 2,
  MODULE_SMMU = 3,
  MODULE_SRAM = 4,
  MODULE_END = 5
} MODULE_ID;

#define BM_MEM_ADDR_NULL (0xfffffffff)

#ifndef BM_MEM_DESC_T_
#define BM_MEM_DESC_T_
/* BM function return code definitions */
typedef enum {
  BM_SUCCESS = 0,
  BM_ERR_DEVNOTREADY = 1, /* Device not ready yet */
  BM_ERR_FAILURE = 2,     /* General failure */
  BM_ERR_TIMEOUT = 3,     /* Timeout */
  BM_ERR_PARAM = 4,       /* Parameters invalid */
  BM_ERR_NOMEM = 5,       /* Not enough memory */
  BM_ERR_DATA = 6,        /* Data error */
  BM_ERR_BUSY = 7,        /* Busy */
  BM_ERR_NOFEATURE = 8,   /* Not supported yet */
  BM_NOT_SUPPORTED = 9
} bm_status_t;

/* BM memory type definitions */
typedef enum {
  BM_MEM_TYPE_DEVICE = 0,
  BM_MEM_TYPE_HOST = 1,
  BM_MEM_TYPE_SYSTEM = 2,
  BM_MEM_TYPE_INT8_DEVICE = 3,
  BM_MEM_TYPE_INVALID = 4
} bm_mem_type_t;

typedef enum {
  PERF_MONITOR_GDMA = 0,
  PERF_MONITOR_TPU = 1
} PERF_MONITOR_ID;

typedef enum {
  BMCPU_IDLE    = 0,
  BMCPU_RUNNING = 1,
  BMCPU_FAULT   = 2
} bm_cpu_status_t;

/*
* bm performace monitor
*/
typedef struct bm_perf_monitor {
  long long buffer_start_addr; /*buffer address to store perf data*/
  int buffer_size; /*buffer size*/
  PERF_MONITOR_ID monitor_id; /*PERF_MONITOR_GDMA or PERF_MONITOR_TPU*/
} bm_perf_monitor_t;

typedef union {
  struct {
    bm_mem_type_t mem_type : 3;
    unsigned int gmem_heapid : 3;
    unsigned int reserved : 26;
  } u;
  unsigned int rawflags;
} bm_mem_flags_t;

/* BM memory descriptor definition*/
typedef struct bm_mem_desc {
  union {
    struct {
#ifdef __linux__
      unsigned long device_addr;
#else
      unsigned long long device_addr;
#endif
      unsigned int reserved;
      int dmabuf_fd;
    } device;

    struct {
      void *system_addr;
      unsigned int reserved0;
      int reserved1;
    } system;
  } u;

  bm_mem_flags_t flags;
  unsigned int size;
} bm_mem_desc_t;

typedef struct bm_mem_desc bm_device_mem_t;
typedef struct bm_mem_desc bm_system_mem_t;

typedef struct sg_mem_desc {
  union {
    struct {
#ifdef __linux__
      unsigned long device_addr;
#else
      unsigned long long device_addr;
#endif
      unsigned int reserved;
      int dmabuf_fd;
    } device;

    struct {
      void *system_addr;
      unsigned int reserved0;
      int reserved1;
    } system;
  } u;

  bm_mem_flags_t flags;
  unsigned long long size;
} sg_mem_desc_t;

typedef struct sg_mem_desc sg_device_mem_t;
typedef struct sg_mem_desc sg_system_mem_t;
#endif

struct bm_context;
typedef struct bm_context *bm_handle_t;

#define MD5SUM_LEN 16
#define LIB_MAX_NAME_LEN 64
#define FUNC_MAX_NAME_LEN 64

typedef struct bm_module
{
  // void *lib_handle;
  char lib_name[LIB_MAX_NAME_LEN];
  unsigned char md5[MD5SUM_LEN];
}bm_module;

typedef struct bm_module *tpu_kernel_module_t;
typedef int tpu_kernel_function_t;

/**
 * @name    tpu_kernel_load_module_file
 * @brief   To load dyn file
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  module_file     dyn file
 * @retval  dyn lib ptr
 */
tpu_kernel_module_t tpu_kernel_load_module_file(bm_handle_t handle, const char *module_file);

/**
 * @name    tpu_kernel_load_module_file_key
 * @brief   To load dyn file with key
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  module_file     dyn file
 * @param [in]  key             identification str
 * @param [in]  size            key size
 * @retval  dyn lib ptr
 */
tpu_kernel_module_t tpu_kernel_load_module_file_key(bm_handle_t handle, const char *module_file, const char *key, int size);

/**
 * @name    tpu_kernel_unload_module
 * @brief   To unload dyn file
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  p_module        dyn lib ptr
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
bm_status_t tpu_kernel_unload_module(bm_handle_t handle, tpu_kernel_module_t p_module);

/**
 * @name    tpu_kernel_free_module
 * @brief   To free p_module when not use
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  p_module        dyn lib ptr
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
bm_status_t tpu_kernel_free_module(bm_handle_t handle, tpu_kernel_module_t p_module);

/**
 * @name    tpu_kernel_load_module
 * @brief   To load dyn module
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  data            dyn module
 * @param [in]  length          dyn module size
 * @retval  dyn lib ptr
 */
tpu_kernel_module_t tpu_kernel_load_module(bm_handle_t handle, const char *data, size_t length);

/**
 * @name    tpu_kernel_get_function
 * @brief   To get function from lib
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  module          dyn module
 * @param [in]  function        funtion name
 * @retval  function id
 */
tpu_kernel_function_t tpu_kernel_get_function(bm_handle_t handle, tpu_kernel_module_t module, const char *function);

/**
 * @name    tpu_kernel_launch
 * @brief   To launch function with sync
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  function        function id
 * @param [in]  args            funtion args
 * @param [in]  size            args size
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
bm_status_t tpu_kernel_launch(bm_handle_t handle, tpu_kernel_function_t function, void *args, size_t size);

/**
 * @name    tpu_kernel_launch_async
 * @brief   To launch function with async
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  function        function id
 * @param [in]  args            funtion args
 * @param [in]  size            args size
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
bm_status_t tpu_kernel_launch_async(bm_handle_t handle, tpu_kernel_function_t function, void *args, size_t size);

/**
 * @name    tpu_kernel_launch_async_multi_cores
 * @brief   To launch function with async for multi cores
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  func_name       function name
 * @param [in]  api_param       funtion params
 * @param [in]  api_size        params size
 * @param [in]  core_list       list of core ids
 * @param [in]  core_num        number of cores
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
bm_status_t tpu_kernel_launch_async_multi_cores(bm_handle_t handle, const char *func_name, const void *api_param,
                                                size_t api_size, const int* core_list, const int core_num);

/**
 * @name    tpu_kernel_launch_sync_multi_cores
 * @brief   To launch function with sync for multi cores
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @param [in]  func_name       function name
 * @param [in]  api_param       funtion params
 * @param [in]  api_size        params size
 * @param [in]  core_list       list of core ids
 * @param [in]  core_num        number of cores
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
bm_status_t tpu_kernel_launch_sync_multi_cores(bm_handle_t handle, const char *func_name, const void *api_param,
                                              size_t api_size, const int* core_list, const int core_num);

/**
 * @name    tpu_kernel_sync
 * @brief   To sync
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle          The device handle
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
bm_status_t tpu_kernel_sync(bm_handle_t handle);
void show_md5(unsigned char md5[]);

DECL_EXPORT void bmlib_log(const char *tag, int level, const char *fmt, ...);

#ifndef USING_CMODEL
#define BM_CHECK_RET(call)                                                    \
  do {                                                                        \
    bm_status_t ret = (bm_status_t)call;                                                   \
    if (ret != BM_SUCCESS) {                                                  \
      bmlib_log("BM_CHECK",16,"BM_CHECK_RET fail %s: %s: %d\n", __FILE__, __func__, __LINE__); \
      return ret;                                                             \
    }                                                                         \
  } while (0)
#else
#define BM_CHECK_RET(call)                     \
  do {                                         \
    bm_status_t ret = call;                    \
    if (ret != BM_SUCCESS) {                   \
      bmlib_log("BM_CHECK",16,"BM_CHECK_RET failed %d\n", ret);\
      ASSERT(0);                               \
      exit(-ret);                              \
    }                                          \
  } while (0)
#endif

/*******************handle releated functions *********************************/
/**
 * @name    bm_dev_getcount
 * @brief   To get the number of sophon devices in system.
 *          If N is got, valid devid is [0, N-1]
 * @ingroup bmlib_runtime
 *
 * @param [out] count  The result number of sophon devices
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_dev_getcount(int *count);

/**
 * @name    bm_dev_query
 * @brief   To query if a device is present
 * @ingroup bmlib_runtime
 *
 * @param [in] devid  The id of the device to query
 * @retval  BM_SUCCESS Device is present
 *          Other code Devcie is not present
 */
DECL_EXPORT bm_status_t bm_dev_query(int devid);

/**
 * @name    bm_dev_request
 * @brief   To create a handle for the given device
 * @ingroup bmlib_runtime
 *
 * @param [out] handle  The created handle
 * @param [in]  devid   Specify on which device to create handle
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_dev_request(bm_handle_t *handle, int devid);

/**
 * @name    bm_get_devid
 * @brief   To get device index for the given handle
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The given handle
 * @retval  int  device index that the handle points to.
 */
DECL_EXPORT int bm_get_devid(bm_handle_t handle);

/**
 * @name    bm_dev_free
 * @brief   To free a handle
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The handle to free
 */
DECL_EXPORT void bm_dev_free(bm_handle_t handle);

/*******************memory help functions ************************************/
/**
 * @name    bm_mem_get_type
 * @brief   To get a memory descriptor's type
 * @ingroup bmlib_runtime
 *
 * @param [in]  mem  The memory descriptor queried
 * @retval  BM_MEM_TYPE_DEVICE  Device global memory
 * @retval  BM_MEM_TYPE_SYSTEM  Host user memory
 */
DECL_EXPORT bm_mem_type_t bm_mem_get_type(struct bm_mem_desc mem);

/**
 * @name    sg_mem_get_type
 * @brief   To get a memory descriptor's type
 * @ingroup bmlib_runtime
 *
 * @param [in]  mem  The memory descriptor queried
 * @retval  BM_MEM_TYPE_DEVICE  Device global memory
 * @retval  BM_MEM_TYPE_SYSTEM  Host user memory
 */
DECL_EXPORT bm_mem_type_t sg_mem_get_type(struct sg_mem_desc mem);

/**
 * @name    bm_mem_get_device_addr
 * @brief   To get a device memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in]  mem  The device memory descriptor queried
 * @retval  unsigned long long  The device memory address
 */
DECL_EXPORT unsigned long long bm_mem_get_device_addr(struct bm_mem_desc mem);

/**
 * @name    sg_mem_get_device_addr
 * @brief   To get a device memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in]  mem  The device memory descriptor queried
 * @retval  unsigned long long  The device memory address
 */
DECL_EXPORT unsigned long long sg_mem_get_device_addr(struct sg_mem_desc mem);

/**
 * @name    bm_mem_set_device_addr
 * @brief   To set a device memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in]  pmem   The device memory descriptor pointer
 * @param ]in]  addr  The new device address of the device memory
 */
DECL_EXPORT void bm_mem_set_device_addr(struct bm_mem_desc* pmem, unsigned long long addr);

/**
 * @name    sg_mem_set_device_addr
 * @brief   To set a device memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in]  pmem   The device memory descriptor pointer
 * @param ]in]  addr  The new device address of the device memory
 */
DECL_EXPORT void sg_mem_set_device_addr(struct sg_mem_desc* pmem, unsigned long long addr);

/**
 * @name    bm_mem_get_device_size
 * @brief   To get a device memory descriptor's size
 * @ingroup bmlib_runtime
 *
 * @param [in]  mem      The device memory descriptor queried
 * @retval unsigned int  The device memory's size in bytes
 */
DECL_EXPORT unsigned int bm_mem_get_device_size(struct bm_mem_desc mem);

/**
 * @name    sg_mem_get_device_size
 * @brief   To get a device memory descriptor's size
 * @ingroup bmlib_runtime
 *
 * @param [in]  mem      The device memory descriptor queried
 * @retval unsigned int  The device memory's size in bytes
 */
DECL_EXPORT unsigned long long sg_mem_get_device_size(struct sg_mem_desc mem);

/**
 * @name    bm_mem_set_device_size
 * @brief   To set a device memory descriptor's size
 * @ingroup bmlib_runtime
 *
 * @param [out]  pmem  The device memory descriptor pointer
 * @param [in]  size  The new device memory size (in bytes) of the device memory
 */
DECL_EXPORT void bm_mem_set_device_size(struct bm_mem_desc* pmem, unsigned int size);

/**
 * @name    sg_mem_set_device_size
 * @brief   To set a device memory descriptor's size
 * @ingroup bmlib_runtime
 *
 * @param [out]  pmem  The device memory descriptor pointer
 * @param [in]  size  The new device memory size (in bytes) of the device memory
 */
DECL_EXPORT void sg_mem_set_device_size(struct sg_mem_desc* pmem, unsigned long long size);

/**
 * @name    bm_set_device_mem
 * @brief   To fill in a device memory descriptor with size and address
 * @ingroup bmlib_runtime
 *
 * @param [in] pmem  The device memory descriptor pointer
 * @param [in]  size  The device memory descriptor's size
 * @param [in]  addr  The device memory descriptor's address
 */
DECL_EXPORT void bm_set_device_mem(bm_device_mem_t* pmem, unsigned int size,
                       unsigned long long addr);

/**
 * @name    sg_set_device_mem
 * @brief   To fill in a device memory descriptor with size and address
 * @ingroup bmlib_runtime
 *
 * @param [in] pmem  The device memory descriptor pointer
 * @param [in]  size  The device memory descriptor's size
 * @param [in]  addr  The device memory descriptor's address
 */
DECL_EXPORT void sg_set_device_mem(sg_device_mem_t* pmem, unsigned long long size,
                       unsigned long long addr);

/**
 * @name    bm_mem_from_device
 * @brief   To create a device memory descriptor from address and size
 * @ingroup bmlib_runtime
 *
 * @param [in] device_addr The device memory address
 * @param [in] len         The device memory size
 * @retval bm_device_mem_t The device memory descriptor created
 */
DECL_EXPORT bm_device_mem_t bm_mem_from_device(unsigned long long device_addr,
                                   unsigned int len);

/**
 * @name    sg_mem_from_device
 * @brief   To create a device memory descriptor from address and size
 * @ingroup bmlib_runtime
 *
 * @param [in] device_addr The device memory address
 * @param [in] len         The device memory size
 * @retval bm_device_mem_t The device memory descriptor created
 */
DECL_EXPORT sg_device_mem_t sg_mem_from_device(unsigned long long device_addr,
                                   unsigned long long len);

/**
 * @name    bm_mem_get_system_addr
 * @brief   To get a system memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in] mem  The system memory descriptor
 * @retval void *   The system memory descriptor's address
 */
DECL_EXPORT void *bm_mem_get_system_addr(struct bm_mem_desc mem);

/**
 * @name    sg_mem_get_system_addr
 * @brief   To get a system memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in] mem  The system memory descriptor
 * @retval void *   The system memory descriptor's address
 */
DECL_EXPORT void *sg_mem_get_system_addr(struct sg_mem_desc mem);

/**
 * @name    bm_mem_set_system_addr
 * @brief   To set a system memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in]  pmem  The system memory descriptor pointer
 * @param [in]   addr The system memory address
 */
DECL_EXPORT void bm_mem_set_system_addr(struct bm_mem_desc* pmem, void *addr);

/**
 * @name    sg_mem_set_system_addr
 * @brief   To set a system memory descriptor's address
 * @ingroup bmlib_runtime
 *
 * @param [in]  pmem  The system memory descriptor pointer
 * @param [in]   addr The system memory address
 */
DECL_EXPORT void sg_mem_set_system_addr(struct sg_mem_desc* pmem, void *addr);

/**
 * @name    bm_mem_from_system
 * @brief   To create a system memory descriptor with the given system address
 * @ingroup bmlib_runtime
 *
 * @param [in]  system_addr  The system address in the descriptor
 * @retval  bm_system_mem_t  The system memory descriptor created
 */
DECL_EXPORT bm_system_mem_t bm_mem_from_system(void *system_addr);

/*******************memory alloc and free functions ***************************/
/**
 * @name    bm_mem_null
 * @brief   Return an illegal device memory descriptor
 * @ingroup bmlib_runtime
 *
 * @retval  bm_device_mem_t  An invalid device memory descriptor
 */
DECL_EXPORT bm_device_mem_t bm_mem_null(void);
#define BM_MEM_NULL (bm_mem_null())

/**
 * @name    bm_malloc_neuron_device
 * @brief   To malloc device memory according to a tensor shape
 *          (each neuron is 32 bits)
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result devcie memory descriptor
 * @param [in]  n, c, h, w  The shape of the input tensor
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_malloc_neuron_device(bm_handle_t handle, bm_device_mem_t *pmem,
                                    int n, int c, int h, int w);

/**
 * @name    sg_malloc_neuron_device
 * @brief   To malloc device memory according to a tensor shape
 *          (each neuron is 32 bits)
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result devcie memory descriptor
 * @param [in]  n, c, h, w  The shape of the input tensor
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_malloc_neuron_device(bm_handle_t handle, sg_device_mem_t *pmem,
                                    unsigned long long n, unsigned long long c,
                                    unsigned long long h, unsigned long long w);

/**
 * @name    bm_malloc_device_dword
 * @brief   To malloc device memory in size of dword (32 bits)
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]   count  The number of dwords(32bits) to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_malloc_device_dword(bm_handle_t handle, bm_device_mem_t *pmem,
                                   int count);

/**
 * @name    sg_malloc_device_dword
 * @brief   To malloc device memory in size of dword (32 bits)
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]   count  The number of dwords(32bits) to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_malloc_device_dword(bm_handle_t handle, sg_device_mem_t *pmem,
                                   unsigned long long count);

/**
 * @name    bm_malloc_device_byte
 * @brief   To malloc device memory in size of byte
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]   size   The number of bytes to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_malloc_device_byte(bm_handle_t handle, bm_device_mem_t *pmem,
                                  unsigned int size);

/**
 * @name    sg_malloc_device_byte
 * @brief   To malloc device memory in size of byte
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]   size   The number of bytes to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_malloc_device_byte(bm_handle_t handle, sg_device_mem_t *pmem,
                                  unsigned long long size);

/**
 * @name    bm_malloc_device_byte_heap
 * @brief   To malloc device memory in size of byte within the specified heap
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]  heap_id The heap where to allocate  0/1/2
 * @param [in]   size   The number of bytes to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_malloc_device_byte_heap(bm_handle_t handle, bm_device_mem_t *pmem,
                                  int heap_id, unsigned int size);

/**
 * @name    sg_malloc_device_byte_heap
 * @brief   To malloc device memory in size of byte within the specified heap
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]  heap_id The heap where to allocate  0/1/2
 * @param [in]   size   The number of bytes to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_malloc_device_byte_heap(bm_handle_t handle, sg_device_mem_t *pmem,
                                  int heap_id, unsigned long long size);

/**
 * @name    bm_malloc_device_byte_heap_mask
 * @brief   To malloc device memory in size of byte within the specified heaps
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]  heap_id_mask The mask which heaps allocate from. each bit indicate one heap
 * @param [in]   size   The number of bytes to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_malloc_device_byte_heap_mask(bm_handle_t handle, bm_device_mem_t *pmem,
                                  int heap_id_mask, unsigned int size);

/**
 * @name    sg_malloc_device_byte_heap_mask
 * @brief   To malloc device memory in size of byte within the specified heaps
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  pmem   The result device memory descriptor
 * @param [in]  heap_id_mask The mask which heaps allocate from. each bit indicate one heap
 * @param [in]   size   The number of bytes to allocate
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_malloc_device_byte_heap_mask(bm_handle_t handle, sg_device_mem_t *pmem,
                                  int heap_id_mask, unsigned long long size);

/**
 * @name    bm_free_device
 * @brief   To free device memory
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  mem     The device memory descriptor to free
 */
DECL_EXPORT void bm_free_device(bm_handle_t handle, bm_device_mem_t mem);

/**
 * @name    sg_free_device
 * @brief   To free device memory
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  mem     The device memory descriptor to free
 */
DECL_EXPORT void sg_free_device(bm_handle_t handle, sg_device_mem_t mem);

/**
 * @name    bm_gmem_arm_reserved_request
 * @brief   To obtain the address of global memory reserved for arm926
 * @param [in]  handle  The device handle
 *
 * @retval unsigned long long  The absolute address of gmem reserved for arm926
 */
DECL_EXPORT unsigned long long bm_gmem_arm_reserved_request(bm_handle_t handle);

/**
 * @name    bm_gmem_arm_reserved_release
 * @brief   To release the global memory reserved for arm926
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 */
DECL_EXPORT void bm_gmem_arm_reserved_release(bm_handle_t handle);

/*******************memory copy functions *************************************/
/**
 * @name    bm_memcpy_s2d
 * @brief   To copy data from system memory to device memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in] dst     The destination memory (device memory descriptor )
 * @param [in] src     The source memory (system memory, a void* pointer)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_s2d(bm_handle_t handle, bm_device_mem_t dst, void *src);

/**
 * @name    bm_memcpy_p2p
 * @brief   To copy data from one chip to another chip
 * @ingroup bmlib_runtime
 *
 * @param [in] handle_src The source device handle
 * @param [in] src        The source memory (device memory descriptor )
 * @param [in] handle_dst The destination device handle
 * @param [in] dst        The destination memory (device memory descriptor )
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_p2p(bm_handle_t handle_src, bm_device_mem_t src, bm_handle_t handle_dst,bm_device_mem_t dst);

/**
 * @name    sg_memcpy_s2d
 * @brief   To copy data from system memory to device memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in] dst     The destination memory (device memory descriptor )
 * @param [in] src     The source memory (system memory, a void* pointer)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_memcpy_s2d(bm_handle_t handle, sg_device_mem_t dst, void *src);

/**
 * @name    bm_memcpy_s2d_partial_offset
 * @brief   To copy specified bytes of data from system memory to device memory
 *          with an offset in device memory address.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (device memory descriptor)
 * @param [in]  src    The source memory (system memory, a void* pointer)
 * @param [in] size    The size of data to copy (in bytes)
 * @param [in] offset  The offset of the device memory address
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_s2d_partial_offset(bm_handle_t handle,
                                         bm_device_mem_t dst, void *src,
                                         unsigned int size,
                                         unsigned int offset);

/**
 * @name    sg_memcpy_s2d_partial_offset
 * @brief   To copy specified bytes of data from system memory to device memory
 *          with an offset in device memory address.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (device memory descriptor)
 * @param [in]  src    The source memory (system memory, a void* pointer)
 * @param [in] size    The size of data to copy (in bytes)
 * @param [in] offset  The offset of the device memory address
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_memcpy_s2d_partial_offset(bm_handle_t handle,
                                         sg_device_mem_t dst, void *src,
                                         unsigned long long size,
                                         unsigned long long offset);

/**
 * @name    bm_memcpy_s2d_partial
 * @brief   To copy specified bytes of data from system memory to device memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (device memory descriptor)
 * @param [in]  src    The source memory (system memory, a void* pointer)
 * @param [in] size    The size of data to copy (in bytes)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_s2d_partial(bm_handle_t handle, bm_device_mem_t dst,
                                  void *src, unsigned int size);

/**
 * @name    sg_memcpy_s2d_partial
 * @brief   To copy specified bytes of data from system memory to device memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (device memory descriptor)
 * @param [in]  src    The source memory (system memory, a void* pointer)
 * @param [in] size    The size of data to copy (in bytes)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_memcpy_s2d_partial(bm_handle_t handle, sg_device_mem_t dst,
                                  void *src, unsigned long long size);

/**
 * @name    bm_memcpy_d2s
 * @brief   To copy data from device memory to system memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (system memory, a void* pointer)
 * @param [in]  src    The source memory (device memory descriptor)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2s(bm_handle_t handle, void *dst, bm_device_mem_t src);

/**
 * @name    sg_memcpy_d2s
 * @brief   To copy data from device memory to system memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (system memory, a void* pointer)
 * @param [in]  src    The source memory (device memory descriptor)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_memcpy_d2s(bm_handle_t handle, void *dst, sg_device_mem_t src);

/**
 * @name    bm_memcpy_d2s_partial_offset
 * @brief   To copy specified bytes of data from device memory to system memory
 *          with an offset in device memory address.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (system memory, a void* pointer)
 * @param [in]  src    The source memory (device memory descriptor)
 * @param [in] size    The size of data to copy (in bytes)
 * @param [in] offset  The offset of the device memory address
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2s_partial_offset(bm_handle_t handle, void *dst,
                                         bm_device_mem_t src, unsigned int size,
                                         unsigned int offset);

/**
 * @name    sg_memcpy_d2s_partial_offset
 * @brief   To copy specified bytes of data from device memory to system memory
 *          with an offset in device memory address.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (system memory, a void* pointer)
 * @param [in]  src    The source memory (device memory descriptor)
 * @param [in] size    The size of data to copy (in bytes)
 * @param [in] offset  The offset of the device memory address
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_memcpy_d2s_partial_offset(bm_handle_t handle, void *dst,
                                         sg_device_mem_t src, unsigned long long size,
                                         unsigned long long offset);

/**
 * @name    bm_memcpy_d2s_partial
 * @brief   To copy specified bytes of data from device memory to system memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (system memory, a void* pointer)
 * @param [in]  src    The source memory (device memory descriptor)
 * @param [in] size    The size of data to copy (in bytes)
 *
 * @retval  BM_SUCCESS  Data transfer succeeds.
 *          Other code  Data transfer fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2s_partial(bm_handle_t handle, void *dst,
                                  bm_device_mem_t src, unsigned int size);

/**
 * @name    sg_memcpy_d2s_partial
 * @brief   To copy specified bytes of data from device memory to system memory
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in]  dst    The destination memory (system memory, a void* pointer)
 * @param [in]  src    The source memory (device memory descriptor)
 * @param [in] size    The size of data to copy (in bytes)
 *
 * @retval  BM_SUCCESS  Data transfer succeeds.
 *          Other code  Data transfer fails.
 */
DECL_EXPORT bm_status_t sg_memcpy_d2s_partial(bm_handle_t handle, void *dst,
                                  sg_device_mem_t src, unsigned long long size);

/**
 * @name    bm_memcpy_d2d
 * @brief   To copy specified dwords of data from one piece of device memory
 *          to another piece of device memory within one device. Both source
 *          and destination offsets can be specified.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle     The device handle
 * @param [in]  dst       The destination device memory
 * @param [in] dst_offset The offset of destination device memory address
 * @param [in]  src       The source device memory
 * @param [in] src_offset The offset of source device memory address
 * @param [in]  len       Length of data to copy (in DWORD 4 bytes)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2d(bm_handle_t handle, bm_device_mem_t dst,
                          int dst_offset, bm_device_mem_t src, int src_offset,
                          int len);

/**
 * @name    bm_memcpy_d2d_with_core
 * @brief   To copy specified dwords of data from one piece of device memory
 *          to another piece of device memory within one device. Both source
 *          and destination offsets can be specified.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle     The device handle
 * @param [in]  dst       The destination device memory
 * @param [in] dst_offset The offset of destination device memory address
 * @param [in]  src       The source device memory
 * @param [in] src_offset The offset of source device memory address
 * @param [in]  len       Length of data to copy (in DWORD 4 bytes)
 * @param [in] core_id    The core id to copy
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2d_with_core(bm_handle_t handle, bm_device_mem_t dst,
                          int dst_offset, bm_device_mem_t src, int src_offset,
                          int len, int core_id);

/**
 * @name    bm_memcpy_d2d_byte
 * @brief   To copy specified bytes of data from one piece of device memory
 *          to another piece of device memory within one device. Both source
 *          and destination offsets can be specified.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle     The device handle
 * @param [in]  dst       The destination device memory
 * @param [in] dst_offset The offset of destination device memory address (in bytes)
 * @param [in]  src       The source device memory
 * @param [in] src_offset The offset of source device memory address (in bytes)
 * @param [in]  size      Size of data to copy (in bytes)
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2d_byte(bm_handle_t handle, bm_device_mem_t dst,
                               size_t dst_offset, bm_device_mem_t src,
                               size_t src_offset, size_t size);

/**
 * @name    bm_memcpy_d2d_byte_with_core
 * @brief   To copy specified bytes of data from one piece of device memory
 *          to another piece of device memory within one device. Both source
 *          and destination offsets can be specified.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle     The device handle
 * @param [in]  dst       The destination device memory
 * @param [in] dst_offset The offset of destination device memory address (in bytes)
 * @param [in]  src       The source device memory
 * @param [in] src_offset The offset of source device memory address (in bytes)
 * @param [in]  size      Size of data to copy (in bytes)
 * @param [in] core_id    The core id to copy
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2d_byte_with_core(bm_handle_t handle, bm_device_mem_t dst,
                               size_t dst_offset, bm_device_mem_t src,
                               size_t src_offset, size_t size, int core_id);

/**
 * @name    bm_memcpy_d2d_stride
 * @brief   To copy specified data from one piece of device memory
 *          to another piece of device memory within one device. Both source
 *          and destination offsets can be specified.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle      The device handle
 * @param [in] dst         The destination device memory
 * @param [in] dst_stride  The data stride of destination data
 * @param [in] src         The source device memory
 * @param [in] src_stride  The data stride of source data
 * @param [in] count       Count of data to copy
 * @param [in] format_size Data format byte size, such as sizeof(uint8_t), sizeof(float), etc.
 *                         format_size only support 1/2/4.
 *
 * dst_stride MUST be 1, EXCEPT: dst_stride == 4 && src_stride == 1 && format_size ==1
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2d_stride(bm_handle_t     handle,
                                 bm_device_mem_t dst,
                                 int             dst_stride,
                                 bm_device_mem_t src,
                                 int             src_stride,
                                 int             count,
                                 int             format_size);

/**
 * @name    bm_memcpy_d2d_stride
 * @brief   To copy specified data from one piece of device memory
 *          to another piece of device memory within one device. Both source
 *          and destination offsets can be specified.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle      The device handle
 * @param [in] dst         The destination device memory
 * @param [in] dst_stride  The data stride of destination data
 * @param [in] src         The source device memory
 * @param [in] src_stride  The data stride of source data
 * @param [in] count       Count of data to copy
 * @param [in] format_size Data format byte size, such as sizeof(uint8_t), sizeof(float), etc.
 *                         format_size only support 1/2/4.
 * @param [in] core_id     The core id to copy.
 *
 * dst_stride MUST be 1, EXCEPT: dst_stride == 4 && src_stride == 1 && format_size ==1
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_d2d_stride_with_core(bm_handle_t     handle,
                                 bm_device_mem_t dst,
                                 int             dst_stride,
                                 bm_device_mem_t src,
                                 int             src_stride,
                                 int             count,
                                 int             format_size,
                                 int             core_id);

/**
 * @name    bm_memcpy_c2c
 * @brief   To copy data from one chip to another chip.
 *          (Used in multi-chip card scenario)
 * @ingroup bmlib_runtime
 *
 * @param [in] src_handle The source device handle
 * @param [in] dst_handle The destination device handle
 * @param [in] src        The source device memory descriptor
 * @param [in] dst        The destination device memory descriptor
 * @param [in] force_dst_cdma If use the CDMA engine of the destination device
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memcpy_c2c(bm_handle_t src_handle, bm_handle_t dst_handle,
                          bm_device_mem_t src, bm_device_mem_t dst,
                          bool force_dst_cdma);

/**
 * @name    bm_memset_device
 * @brief   To fill in specified device memory with the given value
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   value  The value used to fill. (int type)
 * @param [in]  mem     The device memory which will be filled in
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memset_device(bm_handle_t handle, const int value,
                             bm_device_mem_t mem);

/**
 * @name    bm_memset_device_ext
 * @brief   To fill in specified device memory with the given value and mode
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   value  The pointer of value used to fill
 * @param [in]   mode   The valid bytes of *value
 * @param [in]  mem     The device memory which will be filled in
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_memset_device_ext(bm_handle_t handle, void* value, int mode,
                             bm_device_mem_t mem);

/**
 * @name    bm_mem_convert_system_to_device_neuron
 * @brief   To malloc a piece of device memory according to the shape of
 *          neuron(in DWORD 4 bytes); copy neuron from system memory to
 *          device memory if need_copy is true.
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory descriptor
 * @param [in]  sys_mem The system memory descriptor
 * @param [in]  need_copy If copy from system to device is needed
 * @param [in]  n,c,h,w  Neuron shape size
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_convert_system_to_device_neuron(bm_handle_t handle,
                                                   struct bm_mem_desc *dev_mem,
                                                   struct bm_mem_desc sys_mem,
                                                   bool need_copy, int n, int c,
                                                   int h, int w);

/**
 * @name    bm_mem_convert_system_to_device_neuron_byte
 * @brief   To malloc a piece of device memory according to the shape of
 *          neuron(in bytes); copy neuron from system memory to
 *          device memory if need_copy is true.
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory descriptor
 * @param [in]  sys_mem The system memory descriptor
 * @param [in]  need_copy If copy from system to device is needed
 * @param [in]  n,c,h,w  Neuron shape size
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_convert_system_to_device_neuron_byte(
    bm_handle_t handle, struct bm_mem_desc *dev_mem, struct bm_mem_desc sys_mem,
    bool need_copy, int n, int c, int h, int w);

/**
 * @name    bm_mem_convert_system_to_device_coeff
 * @brief   To malloc a piece of device memory according to the size of
 *          coefficient (in DWORD 4 bytes); copy coefficient from system
 *          memory to device memory if need_copy is true.
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory descriptor
 * @param [in]  sys_mem The system memory descriptor
 * @param [in]  need_copy If copy from system to device is needed
 * @param [in]  coeff_count Coefficient size
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_convert_system_to_device_coeff(bm_handle_t handle,
                                                  struct bm_mem_desc *dev_mem,
                                                  struct bm_mem_desc sys_mem,
                                                  bool need_copy,
                                                  int coeff_count);
/**
 * @name    bm_mem_convert_system_to_device_coeff_byte
 * @brief   To malloc a piece of device memory according to the size of
 *          coefficient (in bytes); copy coefficient from system
 *          memory to device memory if need_copy is true.
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory descriptor
 * @param [in]  sys_mem The system memory descriptor
 * @param [in]  need_copy If copy from system to device is needed
 * @param [in]  coeff_count Coefficient size
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_convert_system_to_device_coeff_byte(
    bm_handle_t handle, struct bm_mem_desc *dev_mem, struct bm_mem_desc sys_mem,
    bool need_copy, int coeff_count);

/*******************memory map functions *************************************/
/**
 * @name    bm_mem_mmap_device_mem
 * @brief   To map a piece of device memory to user space with cache enabled.
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory to map
 * @param [out] vmem    The virtual address of the mapped device memory
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_mmap_device_mem(bm_handle_t handle, bm_device_mem_t *dmem,

        unsigned long long *vmem);

/**
 * @name    sg_mem_mmap_device_mem
 * @brief   To map a piece of device memory to user space with cache enabled.
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory to map
 * @param [out] vmem    The virtual address of the mapped device memory
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_mem_mmap_device_mem(bm_handle_t handle, sg_device_mem_t *dmem,
        unsigned long long *vmem);

/*******************memory map functions *************************************/
/**
 * @name    bm_mem_mmap_device_mem_no_cache
 * @brief   To map a piece of device memory to user space with cache disabled.
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory to map
 * @param [out] vmem    The virtual address of the mapped device memory
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_mmap_device_mem_no_cache(bm_handle_t handle, bm_device_mem_t *dmem,

        unsigned long long *vmem);

/**
 * @name    sg_mem_mmap_device_mem_no_cache
 * @brief   To map a piece of device memory to user space with cache disabled.
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  dev_mem The device memory to map
 * @param [out] vmem    The virtual address of the mapped device memory
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_mem_mmap_device_mem_no_cache(bm_handle_t handle, sg_device_mem_t *dmem,
        unsigned long long *vmem);

/**
 * @name    bm_mem_vir_to_phy
 * @brief   To get device mem address through the mapped virtual address .
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  vmem    The virtual address of the mapped device memory
 * @param [out]  dev_mem The device memory address
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_vir_to_phy(bm_handle_t handle, unsigned long long vmem,
        unsigned long long *device_mem);
/**
 * @name    bm_mem_invalidate_device_mem
 * @brief   To invalidate a piece of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to invalidate
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */

DECL_EXPORT bm_status_t bm_mem_invalidate_device_mem(bm_handle_t handle,
                                         bm_device_mem_t *dmem);

/**
 * @name    sg_mem_invalidate_device_mem
 * @brief   To invalidate a piece of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to invalidate
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */

DECL_EXPORT bm_status_t sg_mem_invalidate_device_mem(bm_handle_t handle,
                                         sg_device_mem_t *dmem);

/**
 * @name    bm_mem_invalidate_partial_device_mem
 * @brief   To invalidate part of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to invalidate
 * @param [in]  offset  The offset of device memory address
 * @param [in]  len     The length of memory to invalidate in bytes
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_invalidate_partial_device_mem(bm_handle_t handle,
                                                 bm_device_mem_t *dmem,
                                                 unsigned int offset,
                                                 unsigned int len);

/**
 * @name    sg_mem_invalidate_partial_device_mem
 * @brief   To invalidate part of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to invalidate
 * @param [in]  offset  The offset of device memory address
 * @param [in]  len     The length of memory to invalidate in bytes
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_mem_invalidate_partial_device_mem(bm_handle_t handle,
                                                 sg_device_mem_t *dmem,
                                                 unsigned long long offset,
                                                 unsigned long long len);

/**
 * @name    bm_mem_flush_device_mem
 * @brief   To flush a piece of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to flush
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_flush_device_mem(bm_handle_t handle, bm_device_mem_t *dmem);

/**
 * @name    sg_mem_flush_device_mem
 * @brief   To flush a piece of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to flush
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_mem_flush_device_mem(bm_handle_t handle, sg_device_mem_t *dmem);

/**
 * @name    bm_mem_flush_partial_device_mem
 * @brief   To flush part of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to flush
 * @param [in]  offset  The offset of device memory address
 * @param [in]  len     The length of memory to flush in bytes
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_flush_partial_device_mem(bm_handle_t handle,
                                            bm_device_mem_t *dmem,
                                            unsigned int offset,
                                            unsigned int len);

/**
 * @name    sg_mem_flush_partial_device_mem
 * @brief   To flush part of mapped device memory to maintain
 *          cache coherence
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   dmem   The device memory to flush
 * @param [in]  offset  The offset of device memory address
 * @param [in]  len     The length of memory to flush in bytes
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_mem_flush_partial_device_mem(bm_handle_t handle,
                                            sg_device_mem_t *dmem,
                                            unsigned long long offset,
                                            unsigned long long len);

/**
 * @name    bm_mem_unmap_device_mem
 * @brief   To unmap a piece of mapped device memory
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   vmem   The virtual address of the mapped device memory
 * @param [in]  size    The size of unmapped memory
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_mem_unmap_device_mem(bm_handle_t handle, void *vmem, int size);

/**
 * @name    sg_mem_unmap_device_mem
 * @brief   To unmap a piece of mapped device memory
 *          (only valid in SoC mode; Not supported in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   vmem   The virtual address of the mapped device memory
 * @param [in]  size    The size of unmapped memory
 *
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t sg_mem_unmap_device_mem(bm_handle_t handle, void *vmem, unsigned long long size);

/*******************api(kernel) functions *************************************/
/**
 * @name    bm_flush
 * @brief   To synchronize APIs of the current thread. The thread will block
 *          until all the outstanding APIs of the current thread are finished.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 */
DECL_EXPORT void bm_flush(bm_handle_t handle);

/**
 * @name    bm_device_sync
 * @brief   To synchronize APIs of the device. The thread will block
 *          until all the outstanding APIs of the device are finished.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle   The device handle
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_device_sync(bm_handle_t handle);

/**
 * @name    bm_handle_sync
 * @brief   To synchronize APIs of the handle. The thread will block
 *          until all the outstanding APIs of the handle are finished.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle   The device handle
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_handle_sync(bm_handle_t handle);

/**
 * @name    bm_handle_sync_from_core
 * @brief   To synchronize APIs of the handle. The thread will block
 *          until all the outstanding APIs of the handle are finished.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle   The device handle
 * @param [in] core_id  The core id
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_handle_sync_from_core(bm_handle_t handle, int core_id);

/**
 * @name    bm_thread_sync
 * @brief   To synchronize APIs of the current thread. The thread will block
 *          until all the outstanding APIs of the current thread are finished.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @retval  BM_SUCCESS Succeeds.
 *          Other code Fails.
 */
DECL_EXPORT bm_status_t bm_thread_sync(bm_handle_t handle);

/**
 * @name    bm_thread_sync_from_core
 * @brief   To synchronize APIs of the current thread. The thread will block
 *          until all the outstanding APIs of the current thread are finished.
 * @ingroup bmlib_runtime
 *
 * @param [in] handle  The device handle
 * @param [in] core_id The core id
 * @retval  BM_SUCCESS Succeeds.
 *          Other code Fails.
 */
DECL_EXPORT bm_status_t bm_thread_sync_from_core(bm_handle_t handle, int core_id);

/*******************trace and profile releated functions **********************/
typedef struct bm_profile {
#ifdef __linux__
  unsigned long cdma_in_time;
  unsigned long cdma_in_counter;
  unsigned long cdma_out_time;
  unsigned long cdma_out_counter;
  unsigned long tpu_process_time;
  unsigned long tpu1_process_time;
  unsigned long sent_api_counter;
  unsigned long completed_api_counter;
#else
  unsigned long long cdma_in_time;
  unsigned long long cdma_in_counter;
  unsigned long long cdma_out_time;
  unsigned long long cdma_out_counter;
  unsigned long long tpu_process_time;
  unsigned long long tpu1_process_time;
  unsigned long long sent_api_counter;
  unsigned long long completed_api_counter;
#endif
} bm_profile_t;
/**
 * @name    bm_get_profile
 * @brief   To get the profile data at the moment
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out] profile The result profile data
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_profile(bm_handle_t handle, bm_profile_t *profile);

typedef struct bootloader_version{
	char *bl1_version;
	char *bl2_version;
	char *bl31_version;
	char *uboot_version;
} boot_loader_version;

/**
 * @name    bm_get_boot_loader_version
 * @brief   To get the boot_loader_version
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out] version The result version data
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_boot_loader_version(bm_handle_t handle, boot_loader_version *version);

/**
 * @name    bm_get_vpu_instant_usage
 * @brief   To get vpu usage
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out] smi_attr The result vpu usage
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_vpu_instant_usage(bm_handle_t handle, int *vpu_usage);

/**
 * @name    bm_get_jpu_core_usage
 * @brief   To get the jpu usage
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out] smi_attr The result jpu usage
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_jpu_core_usage(bm_handle_t handle, int *jpu_usage);

/**
 * @name    bm_get_vpp_instant_usage
 * @brief   To get the vpp usage
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out] smi_attr The result vpp usage
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_vpp_instant_usage(bm_handle_t handle, int *vpp_usage);
/**
 * @name    bm_get_last_api_process_time_us
 * @brief   This function is abandoned.
 */
#ifdef __linux__
DECL_EXPORT bm_status_t bm_get_last_api_process_time_us(bm_handle_t handle,
                                            unsigned long *time_us);
#else
DECL_EXPORT bm_status_t bm_get_last_api_process_time_us(bm_handle_t handle,
											unsigned long long *time_us);
#endif
/*******************tpu clock and module reset releated functions *************/

/**
 * @name    bm_set_clk_tpu_freq
 * @brief   To set the clock frequency of TPU (only valid in PCIE mode).
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]   freq   The TPU target frequency
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_set_clk_tpu_freq(bm_handle_t handle, int freq);

/**
 * @name    bm_get_clk_tpu_freq
 * @brief   To get the clock frequency of TPU
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out]  freq   The current TPU frequency
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_clk_tpu_freq(bm_handle_t handle, int *freq);

/*******************misc functions ********************************************/
struct bm_misc_info {
  int pcie_soc_mode;  /*0---pcie; 1---soc*/
  int ddr_ecc_enable; /*0---disable; 1---enable*/
  long long ddr0a_size;
  long long ddr0b_size;
  long long ddr1_size;
  long long ddr2_size;
  unsigned int chipid;
#define BM1682_CHIPID_BIT_MASK (0X1 << 0)
#define BM1684_CHIPID_BIT_MASK (0X1 << 1)
#define BM1686_CHIPID_BIT_MASK (0X1 << 2)
#ifdef __linux__
  unsigned long chipid_bit_mask;
#else
	unsigned long long chipid_bit_mask;
#endif
  unsigned int driver_version;
  int domain_bdf;
  int board_version; /*hardware board version [23:16]-mcu sw version, [15:8]-board type, [7:0]-hw version*/
  int a53_enable;
  int dyn_enable;
};

/**
 * @name    bm_get_misc_info
 * @brief   To get miscellaneous information of the device
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle     The device handle
 * @param [out] pmisc_info The fetched misc info
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_misc_info(bm_handle_t handle, struct bm_misc_info *pmisc_info);

/**
 * @name    bm_get_chipid
 * @brief   To get the chipid of the device. (0x1682 / 0x1684 / 0x168?)
 * @ingroup bmlib_runtime
 *
 * @param [in] handle    The device handle
 * @param [out] p_chipid The chip id of the device
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_chipid(bm_handle_t handle, unsigned int *p_chipid);

#define BMLIB_LOG_QUIET    -8
#define BMLIB_LOG_PANIC     0
#define BMLIB_LOG_FATAL     8
#define BMLIB_LOG_ERROR    16
#define BMLIB_LOG_WARNING  24
#define BMLIB_LOG_INFO     32
#define BMLIB_LOG_VERBOSE  40
#define BMLIB_LOG_DEBUG    48
#define BMLIB_LOG_TRACE    56

/**
 * @name    bmlib_log_get_level
 * @brief   To get the bmlib log level
 * @ingroup bmlib_log
 *
 * @param void
 * @retval  The level of bmlib log level
 */
DECL_EXPORT int  bmlib_log_get_level(void);

/**
 * @name    bmlib_log_set_level
 * @brief   To set the bmlib log level
 * @ingroup bmlib_log
 *
 * @param [in] level    The level of bmlib log level
 * @retval  void
 */
DECL_EXPORT void bmlib_log_set_level(int level);

/**
 * @name    bmlib_log_set_callback
 * @brief   To set callback to get bmlib log
 * @ingroup bmlib_log
 *
 * @param [in]  callback     The callback function to get bmlib log
 * @retval  void
 */
DECL_EXPORT void bmlib_log_set_callback(void (*callback)(const char*, int, const char*, va_list args));

/**
 * @name    bm_set_debug_mode
 * @brief   To set the debug mode for firmware log for tpu
 * @ingroup bmlib_log
 *
 * @param [in]  handle  The device handle
 * @param [in]  mode    The debug mode of fw log, 0/1 for disable/enable log
 * @retval  void
 */
DECL_EXPORT void bm_set_debug_mode(bm_handle_t handle, int mode);

/**
 * @name    bmlib_api_dbg_callback
 * @brief   To set debug callback to get firmware log
 * @ingroup bmlib_log
 *
 * @param [in]  bmlib_api_dbg_callback  callback to get firmware log
 * @retval  void
 */
typedef void (*bmlib_api_dbg_callback)(int, int, int, const char*);
// api, result, duratioin, log, third int for api duration for future
DECL_EXPORT void bmlib_set_api_dbg_callback(bmlib_api_dbg_callback callback);

/**
 * @name    bmcpu_get_cpu_status
 * @brief   Get bmcpu status
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @retval  BMCPU_RUNNING  bmcpu is running.
 *          Other code  Fails.
 */
DECL_EXPORT bm_cpu_status_t bmcpu_get_cpu_status(bm_handle_t handle);

/**
 * @name    bmcpu_start_cpu
 * @brief   Start cpu in pcie mode
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  boot_file       Fip file
 * @param [in]  core_file       Itb file
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_start_cpu(bm_handle_t handle, char *boot_file, char *core_file);

/**
 * @name    bmcpu_open_process
 * @brief   Open a process to do some work
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  flags           Process flags
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  >= 0 process handle
 *          < 0  Other code Fails.
 */
DECL_EXPORT int bmcpu_open_process(bm_handle_t handle, unsigned int flags, int timeout);

/**
 * @name    bmcpu_load_library
 * @brief   Load a share library(so) to specific process
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  library_file    Library file path
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_load_library(bm_handle_t handle, int process_handle, char *library_file, int timeout);

/**
 * @name    bmcpu_unload_library
 * @brief   Load a share library(so) to specific process
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  library_file    Library file path
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_unload_library(bm_handle_t handle, int process_handle, char *library_file, int timeout);

/**
 * @name    bmcpu_exec_function
 * @brief   Execute specific function in specific process
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  function_name   Function name
 * @param [in]  function_param  Function parameters
 * @param [in]  param_size      Parameters size in bytes
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  0   success.
 *          >0  code fails from bmlib
 *          <0  code fails from function
 */
DECL_EXPORT int bmcpu_exec_function(bm_handle_t handle,
                     int process_handle,
                     char *function_name,
                     void *function_param,
                     unsigned int param_size,
                     int timeout);

#define BMCPU_EXEC_OPT_NO_FLUSH_CACHE     1
/**
 * @name    bmcpu_exec_function_ext
 * @brief   Execute specific function in specific process
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  function_name   Function name
 * @param [in]  function_param  Function parameters
 * @param [in]  param_size      Parameters size in bytes
 * @param [in]  opt             exec options
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  0   success.
 *          >0  code fails from bmlib
 *          <0  code fails from function
 */
DECL_EXPORT int bmcpu_exec_function_ext(bm_handle_t  handle,
                            int process_handle,
                            char *function_name,
                            void *function_param,
                            unsigned int param_size,
                            unsigned int opt,
                            int timeout);

/**
 * @name    bmcpu_exec_function_async
 * @brief   Execute specific function in specific process asynchronous
 *          user should use bm_query_exec_function_result to query result
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  function_name   Function name
 * @param [in]  function_param  Function param
 * @param [in]  param_size      Param size in bytes
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_exec_function_async(bm_handle_t handle,
                                   int process_handle,
                                   char *function_name,
                                   void *function_param,
                                   unsigned int param_size,
                                   unsigned long long *api_handle);

/**
 * @name    bmcpu_exec_function_async_ext
 * @brief   Execute specific function in specific process asynchronous
 *          user should use bm_query_exec_function_result to query result
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  function_name   Function name
 * @param [in]  function_param  Function param
 * @param [in]  param_size      Param size in bytes
 * @param [in]  opt             exec options
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_exec_function_async_ext(bm_handle_t handle,
                                          int process_handle,
                                          char *function_name,
                                          void *function_param,
                                          unsigned int param_size,
                                          unsigned int opt,
                                          unsigned long long *api_handle);

/**
 * @name    bmcpu_query_exec_function_result
 * @brief   Query result from function called by bm_exec_function
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  api_handle      Api handle return by bm_exec_function_async
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  0   success.
 *          >0  code fails from bmlib
 *          <0  code fails from function
 */
DECL_EXPORT int bmcpu_query_exec_function_result(bm_handle_t handle, unsigned long long api_handle, int timeout);

/**
 * @name    bmcpu_map_phys_addr
 * @brief   Map physical address in specific process
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  phys_addr       Physical address
 * @param [in]  size            Map size in bytes
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  >0  virtual address
 *          0   fails
 */
DECL_EXPORT void *bmcpu_map_phys_addr(bm_handle_t handle, int process_handle, void *phys_addr, unsigned int size, int timeout);

/**
 * @name    bmcpu_unmap_phys_addr
 * @brief   Unmap physical address in specific process
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  phys_addr       Physical address
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  <0  fail
 *          0   success
 */
DECL_EXPORT bm_status_t bmcpu_unmap_phys_addr(bm_handle_t handle, int process_handle, void *phys_addr, int timeout);

/**
 * @name    bmcpu_close_process
 * @brief   Close process
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_close_process(bm_handle_t handle, int process_handle, int timeout);

/**
 * @name    bmcpu_reset_cpu
 * @brief   Reset cpu in pcie mode
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_reset_cpu(bm_handle_t handle);

/**
 * @name    bm_enable_perf_monitor
 * @brief   enable perf monitor to get gdma and tpu performance data
 * @ingroup bmlib_perf
 *
 * @param [in]  handle         The device handle
 * @param [in]  perf_monitor   The monitor to perf
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_enable_perf_monitor(bm_handle_t handle, bm_perf_monitor_t *perf_monitor);

/**
 * @name    bm_disable_perf_monitor
 * @brief   disable perf monitor to get gdma and tpu performance data
 * @ingroup bmlib_perf
 *
 * @param [in]  handle         The device handle
 * @param [in]  perf_monitor   The monitor to perf
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_disable_perf_monitor(bm_handle_t handle, bm_perf_monitor_t *perf_monitor);

/**
 * @name    bmcpu_set_log
 * @brief   Set cpu log options
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  log_level       0: DEBUG  1:INFO 2:WARN 3:ERROR 4:FATAL
 * @param [in]  log_to_console  1: YES  0: No
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_set_log(bm_handle_t handle, unsigned int log_level,  unsigned int log_to_console, int timeout);

/**
 * @name    bmcpu_get_log
 * @brief   Get cpu log file
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @param [in]  process_handle  Process handle
 * @param [in]  log_file        save log as file
 * @param [in]  timeout         Timeout value in millisecond, -1 means default value of this device
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_get_log(bm_handle_t handle, int process_handle, char *log_file, int timeout);

/**
 * @name    bmcpu_sync_time
 * @brief   Sync device cpu time with host
 * @ingroup bmlib_log
 *
 * @param [in]  handle          The device handle
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmcpu_sync_time(bm_handle_t handle);

/*******************trace and profile releated functions **********************/
struct bm_heap_stat {
  unsigned int mem_total;
  unsigned int mem_avail;
  unsigned int mem_used;
};

typedef struct bm_heap_stat_byte {
  unsigned int  heap_id;
  unsigned long long mem_total;
  unsigned long long mem_avail;
  unsigned long long mem_used;
  unsigned long long mem_start_addr;
} bm_heap_stat_byte_t;

typedef struct bm_dev_stat {
  int mem_total;
  int mem_used;
  int tpu_util;
  int heap_num;
  struct bm_heap_stat heap_stat[4];
} bm_dev_stat_t;

/**
 * @name    bm_get_stat
 * @brief   To get the stat data at the moment
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [out] profile The result stat data
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_stat(bm_handle_t handle, bm_dev_stat_t *stat);

/**
 * @name    bm_get_gmem_heap_id
 * @brief   To get the heap id of allocated global memory
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  pmem The allocted global memory
 * @param [out] heapid The result of get heap id
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */

DECL_EXPORT bm_status_t bm_get_gmem_heap_id(bm_handle_t handle, bm_device_mem_t *pmem, unsigned int *heapid);

/**
 * @name    sg_get_gmem_heap_id
 * @brief   To get the heap id of allocated global memory
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  pmem The allocted global memory
 * @param [out] heapid The result of get heap id
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */

DECL_EXPORT bm_status_t sg_get_gmem_heap_id(bm_handle_t handle, sg_device_mem_t *pmem, unsigned int *heapid);

/**
 * @name    bm_get_gmem_total_heap_num
 * @brief   To get the total heap num of global memory
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  heap_num The result of get total num
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_gmem_total_heap_num(bm_handle_t handle, unsigned int *heap_num);

/**
 * @name    bm_get_gmem_heap_stat_byte_by_id
 * @brief   To get the heap stat by heap id
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  heap_id The heap index to get heap status
 * @param [out] pheap_byte The result of get heap status
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_gmem_heap_stat_byte_by_id(bm_handle_t handle, bm_heap_stat_byte_t *pheap_byte, unsigned int heap_id);

DECL_EXPORT bm_status_t bm_load_firmware(
        bm_handle_t  handle,
        const char  *firmware_tcm,
        const char  *firmware_ddr);

#define bmkernel_load_firmware okkernel_load_firmware
DECL_EXPORT bm_status_t okkernel_load_firmware(
        bm_handle_t  handle,
        const char  *firmware_tcm,
        const char  *firmware_ddr);

DECL_EXPORT bm_status_t okkernel_launch_async(
        bm_handle_t   handle,
        const char   *func_name,
        const void   *args,
        unsigned int  size);

DECL_EXPORT bm_status_t okkernel_launch_sync(
        bm_handle_t   handle,
        const char   *func_name,
        const void   *args,
        unsigned int  size);

DECL_EXPORT bm_status_t tpu_kernel_launch_sync(
        bm_handle_t   handle,
        const char   *func_name,
        const void   *args,
        unsigned int  size);

DECL_EXPORT bm_status_t okkernel_sync(bm_handle_t handle);

/**
 * @name    bmkernel_launch
 * @brief   send api to device and launch function
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  api cmd struct pointer
 * @param [in]  api cmd length
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmkernel_launch(bm_handle_t handle, const void *args,
                            unsigned int size);

/**
 * @name    bmkernel_load_lookup_table
 * @brief   load lookup table to l2-sram
 * @ingroup bmlib_runtime
 *
 * @param [in]  handle  The device handle
 * @param [in]  table which loaded to l2-sram
 * @param [in]  table size
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bmkernel_load_lookup_table(bm_handle_t handle, const void* table, unsigned int size);

/*******************device management api functions ********************************************/
/**
 * @name    bm_get_tpu_current
 * @brief   get tpu current
 * @ingroup bmlib_runtime
 *
 * @param [in]   handle     The device handle
 * @param [out]  tpuc(mA)   The pointer for tpu current
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_tpu_current(bm_handle_t handle, unsigned int *tpuc);

/**
 * @name    bm_get_board_max_power
 * @brief   get board support max power
 * @ingroup bmlib_runtime
 *
 * @param [in]   handle  The device handle
 * @param [out]  maxp    The pointer for maxp
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_board_max_power(bm_handle_t handle, unsigned int *maxp);

/**
 * @name    bm_get_board_power
 * @brief   get board power
 * @ingroup bmlib_runtime
 *
 * @param [in]   handle    The device handle
 * @param [out]  boardp    The pointer for boardp
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_board_power(bm_handle_t handle, unsigned int *boardp);

/**
 * @name    bm_get_fan_speed
 * @brief   get board fan speed
 * @ingroup bmlib_runtime
 *
 * @param [in]   handle The device handle
 * @param [out]  fan    The pointer for fan speed
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_fan_speed(bm_handle_t handle, unsigned int *fan);

/**
 * @name    bm_get_ecc_correct_num
 * @brief   get ecc_correct_num
 * @ingroup device management api
 *
 * @param [in]   handle  The device handle
 * @param [out]  ecc_correct_num
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
#ifdef __linux__
DECL_EXPORT bm_status_t bm_get_ecc_correct_num(bm_handle_t handle, unsigned long *ecc_correct_num);
#else
DECL_EXPORT bm_status_t bm_get_ecc_correct_num(bm_handle_t handle, unsigned long long *ecc_correct_num);
#endif
/**
 * @name    bm_get_12v_atx
 * @brief   get atx_12v
 * @ingroup device management api
 *
 * @param [in]   handle  The device handle
 * @param [out]  atx_12v
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_12v_atx(bm_handle_t handle, int *atx_12v);

/**
 * @name    bm_get_product_sn
 * @brief   get SE5 sn
 * @ingroup device management api
 *
 * @param [out]  product_sn
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_product_sn(char *product_sn);

/**
 * @name    bm_get_sn
 * @brief   get sn
 * @ingroup device management api
 *
 * @param [in]   handle  The device handle
 * @param [out]  sn
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_sn(bm_handle_t handle, char *sn);

/**
 * @name    bm_get_status
 * @brief   get chip status
 * @ingroup device management api
 *
 * @param [in]   handle  The device handle
 * @param [out]  status  The board error status, each bit represents an error state
 *  status == 0x0, borad is nornal, staus > 0, borad is abnormal;
 *  bit0 == 1, tpu is hang
 *  bit1 == 1, pcie link abnormal
 *  bit2 == 1, board temperature is too high
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_status(bm_handle_t handle, int *status);

/**
 * @name    bm_get_tpu_maxclk
 * @brief   get tpu_maxclk
 * @ingroup device management api
 *
 * @param [in]   handle  The device handle
 * @param [out]  tpu_maxclk
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_tpu_maxclk(bm_handle_t handle, unsigned int *tpu_maxclk);

/**
 * @name    bm_get_tpu_minclk
 * @brief   get tpu_minclk
 * @ingroup device management api
 *
 * @param [in]   handle  The device handle
 * @param [out]  tpu_minclk
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_tpu_minclk(bm_handle_t handle, unsigned int *tpu_minclk);

/**
 * @name    bm_get_driver_version
 * @brief   get driver version
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  driver_version
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_driver_version(bm_handle_t handle, int *driver_version);

/**
 * @name    bm_get_board_name
 * @brief   get device board name
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  board_name
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_board_name(bm_handle_t handle, char *name);

/**
 * @name    bm_get_board_temp
 * @brief   get board temperature
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  board_temp
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_board_temp(bm_handle_t handle, unsigned int *board_temp);

/**
 * @name    bm_get_chip_temp
 * @brief   get chip temperature
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  chip_temp
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_chip_temp(bm_handle_t handle, unsigned int *chip_temp);

/**
 * @name    bm_get_tpu_power
 * @brief   get TPU power
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  tpu_power
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_tpu_power(bm_handle_t handle, float *tpu_power);

/**
 * @name    bm_get_tpu_volt
 * @brief   get TPU voltage
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  tpu_volt
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_tpu_volt(bm_handle_t handle, unsigned int *tpu_volt);

/**
 * @name    bm_get_card_id
 * @brief   get card id
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  card_id
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_card_id(bm_handle_t handle, unsigned int *card_id);

/**
 * @name    bm_get_card_num
 * @brief   get card number
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  card_id
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_card_num(unsigned int *card_num);

/**
 * @name    bm_get_chip_num_from_card
 * @brief   get chip number and start chip id from card
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  chip_num
 * @param [out]  dev_start_index
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_chip_num_from_card(unsigned int card_id, unsigned int *chip_num, unsigned int *dev_start_index);

/**
 * @name    bm_get_dynfreq_status
 * @brief   get chip dynamic freq status
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [out]  dynfreq_status
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_dynfreq_status(bm_handle_t handle, int *dynfreq_status);

/**
 * @name    bm_change_dynfreq_status
 * @brief   change(enable/disable) chip dynamic freq status
 * @ingroup device management api
 *
 * @param [in]   handle The device handle
 * @param [in]   new_status
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_change_dynfreq_status(bm_handle_t handle, int new_status);

/**
 * @name    bm_get_tpu_scalar_num
 * @brief   To get the core number of TPU scalar
 * @ingroup bmlib_runtime
 *
 * @param [in] handle    The device handle
 * @param [out] core_num The core number of TPU scalar
 * @retval  BM_SUCCESS  Succeeds.
 *          Other code  Fails.
 */
DECL_EXPORT bm_status_t bm_get_tpu_scalar_num(bm_handle_t handle, unsigned int *core_num);

#define  bm_get_tpu_core_num bm_get_tpu_scalar_num

#if defined(__cplusplus)
}
#endif

#endif /* BM_RUNTIME_H_ */