File: cap.c

package info (click to toggle)
intel-cmt-cat 25.04-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,724 kB
  • sloc: ansic: 52,004; python: 11,324; makefile: 2,197; perl: 1,165; javascript: 37; sh: 23
file content (1391 lines) | stat: -rw-r--r-- 41,641 bytes parent folder | download | duplicates (2)
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
/*
 * BSD LICENSE
 *
 * Copyright(c) 2014-2023 Intel Corporation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in
 *     the documentation and/or other materials provided with the
 *     distribution.
 *   * Neither the name of Intel Corporation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @brief Host implementation of PQoS API / capabilities.
 *
 * This module is responsible for PQoS management and capability
 * functionalities.
 *
 * Management functions include:
 * - management includes initializing and shutting down all other sub-modules
 *   including: monitoring, allocation, log, cpuinfo and machine
 *
 * Capability functions:
 * - monitoring detection, this is to discover all monitoring event types.
 * - LLC allocation detection, this is to discover last level cache
 *   allocation feature.
 * - A new targeted function has to be implemented to discover new allocation
 *   technology.
 */

#include "cap.h"

#include "allocation.h"
#include "api.h"
#include "cpu_registers.h"
#include "cpuinfo.h"
#include "hw_cap.h"
#include "iordt.h"
#include "lock.h"
#include "log.h"
#include "machine.h"
#include "monitoring.h"
#include "os_cap.h"
#include "resctrl.h"
#include "resctrl_alloc.h"
#include "utils.h"

#include <stdlib.h>
#include <string.h>

/**
 * ---------------------------------------
 * Local macros
 * ---------------------------------------
 */

#define PROC_CPUINFO "/proc/cpuinfo"

/**
 * ---------------------------------------
 * Local data types
 * ---------------------------------------
 */

/**
 * ---------------------------------------
 * Local data structures
 * ---------------------------------------
 */

/**
 * This structure is initialized in this module. Then other sub-modules get this
 * structure in order to retrieve capability and CPU information.
 */
static struct pqos_sysconfig m_sysconf;

/**
 * Library initialization status.
 */
static int m_init_done = 0;

/**
 * Interface status
 *   0  PQOS_INTER_MSR
 *   1  PQOS_INTER_OS
 *   2  PQOS_INTER_OS_RESCTRL_MON
 */
static enum pqos_interface m_interface = PQOS_INTER_MSR;

/**
 * ---------------------------------------
 * Local functions
 * ---------------------------------------
 */

enum pqos_interface
_pqos_get_inter(void)
{
        return m_interface;
}

PQOS_STATIC void
_pqos_set_inter(const enum pqos_interface iface)
{
        m_interface = iface;
}

/**
 * ---------------------------------------
 * Function for library initialization
 * ---------------------------------------
 */

int
_pqos_check_init(const int expect)
{
        if (m_init_done && (!expect)) {
                LOG_ERROR("PQoS library already initialized\n");
                return PQOS_RETVAL_INIT;
        }

        if ((!m_init_done) && expect) {
                LOG_ERROR("PQoS library not initialized\n");
                return PQOS_RETVAL_INIT;
        }

        return PQOS_RETVAL_OK;
}

/**
 * @brief Discovers support of L3 CAT
 *
 * @param[out] r_cap place to store L3 CAT capabilities structure
 * @param[in] cpu detected cpu topology
 * @param[in] iface Selected interface
 *
 * @return Operation status
 * @retval PQOS_RETVAL_OK success
 */
PQOS_STATIC int
cap_l3ca_discover(struct pqos_cap_l3ca **r_cap,
                  const struct pqos_cpuinfo *cpu,
                  const enum pqos_interface iface)
{
        struct pqos_cap_l3ca *cap = NULL;
        int ret;

        if (r_cap == NULL || cpu == NULL)
                return PQOS_RETVAL_PARAM;

        cap = (struct pqos_cap_l3ca *)malloc(sizeof(*cap));
        if (cap == NULL)
                return PQOS_RETVAL_RESOURCE;

        switch (iface) {
        case PQOS_INTER_MSR:
                ret = hw_cap_l3ca_discover(cap, cpu);
                break;
#ifdef __linux__
        case PQOS_INTER_OS:
        case PQOS_INTER_OS_RESCTRL_MON:
                ret = os_cap_l3ca_discover(cap, cpu);
                break;
#endif
        default:
                ret = PQOS_RETVAL_RESOURCE;
                break;
        }

        if (ret == PQOS_RETVAL_OK)
                *r_cap = cap;
        else
                free(cap);

        return ret;
}

/**
 * @brief Discovers support of L2 CAT
 *
 * @param[out] r_cap place to store L2 CAT capabilities structure
 * @param[in] cpu detected cpu topology
 * @param[in] iface Selected interface
 *
 * @return Operation status
 * @retval PQOS_RETVAL_OK success
 */
PQOS_STATIC int
cap_l2ca_discover(struct pqos_cap_l2ca **r_cap,
                  const struct pqos_cpuinfo *cpu,
                  const enum pqos_interface iface)
{
        struct pqos_cap_l2ca *cap = NULL;
        int ret;

        if (r_cap == NULL || cpu == NULL)
                return PQOS_RETVAL_PARAM;

        cap = (struct pqos_cap_l2ca *)malloc(sizeof(*cap));
        if (cap == NULL)
                return PQOS_RETVAL_RESOURCE;

        switch (iface) {
        case PQOS_INTER_MSR:
                ret = hw_cap_l2ca_discover(cap, cpu);
                break;
#ifdef __linux__
        case PQOS_INTER_OS:
        case PQOS_INTER_OS_RESCTRL_MON:
                ret = os_cap_l2ca_discover(cap, cpu);
                break;
#endif
        default:
                ret = PQOS_RETVAL_RESOURCE;
                break;
        }

        if (ret == PQOS_RETVAL_OK)
                *r_cap = cap;
        else
                free(cap);

        return ret;
}

/**
 * @brief Discovers support of MBA
 *
 * @param[out] r_cap place to store MBA capabilities structure
 * @param[in] cpu detected cpu topology
 * @param[in] iface Selected interface
 *
 * @return Operation status
 * @retval PQOS_RETVAL_OK success
 */
PQOS_STATIC int
cap_mba_discover(struct pqos_cap_mba **r_cap,
                 const struct pqos_cpuinfo *cpu,
                 const enum pqos_interface iface)
{
        struct pqos_cap_mba *cap = NULL;
        int ret;

        if (r_cap == NULL || cpu == NULL)
                return PQOS_RETVAL_PARAM;

        cap = (struct pqos_cap_mba *)malloc(sizeof(*cap));
        if (cap == NULL)
                return PQOS_RETVAL_RESOURCE;

        switch (iface) {
        case PQOS_INTER_MSR:
                if (cpu->vendor == PQOS_VENDOR_AMD)
                        ret = amd_cap_mba_discover(cap, cpu);
                else
                        ret = hw_cap_mba_discover(cap, cpu);
                break;
#ifdef __linux__
        case PQOS_INTER_OS:
        case PQOS_INTER_OS_RESCTRL_MON:
                ret = os_cap_mba_discover(cap, cpu);
                break;
#endif
        default:
                ret = PQOS_RETVAL_RESOURCE;
                break;
        }

        if (ret == PQOS_RETVAL_OK)
                *r_cap = cap;
        else
                free(cap);

        return ret;
}

/**
 * @brief Discovers support of SMBA
 *
 * @param[out] r_cap place to store SMBA capabilities structure
 * @param[in] cpu detected cpu topology
 * @param[in] iface Selected interface
 *
 * @return Operation status
 * @retval PQOS_RETVAL_OK success
 */
static int
cap_smba_discover(struct pqos_cap_mba **r_cap,
                  const struct pqos_cpuinfo *cpu,
                  const enum pqos_interface iface)
{
        struct pqos_cap_mba *cap = NULL;
        int ret = PQOS_RETVAL_OK;

        cap = (struct pqos_cap_mba *)malloc(sizeof(*cap));
        if (cap == NULL)
                return PQOS_RETVAL_RESOURCE;

        switch (iface) {
        case PQOS_INTER_MSR:
                if (cpu->vendor == PQOS_VENDOR_AMD)
                        ret = amd_cap_smba_discover(cap, cpu);
                else
                        ret = PQOS_RETVAL_RESOURCE;
                break;
#ifdef __linux__
        case PQOS_INTER_OS:
        case PQOS_INTER_OS_RESCTRL_MON:
                ret = os_cap_smba_discover(cap, cpu);
                break;
#endif
        default:
                ret = PQOS_RETVAL_RESOURCE;
                break;
        }

        if (ret == PQOS_RETVAL_OK)
                *r_cap = cap;
        else
                free(cap);

        return ret;
}

/**
 * @brief Runs detection of platform monitoring and allocation capabilities
 *
 * @param p_cap place to store allocated capabilities structure
 * @param cpu detected cpu topology
 * @param inter selected pqos interface
 *
 * @return Operation status
 * @retval PQOS_RETVAL_OK success
 */
PQOS_STATIC int
discover_capabilities(struct pqos_cap **p_cap,
                      const struct pqos_cpuinfo *cpu,
                      enum pqos_interface inter)
{
        struct pqos_cap_mon *det_mon = NULL;
        struct pqos_cap_l3ca *det_l3ca = NULL;
        struct pqos_cap_l2ca *det_l2ca = NULL;
        struct pqos_cap_mba *det_mba = NULL;
        struct pqos_cap_mba *det_smba = NULL;
        struct pqos_cap *_cap = NULL;
        unsigned sz = 0;
        int ret = PQOS_RETVAL_RESOURCE;

        if (p_cap == NULL || cpu == NULL)
                return PQOS_RETVAL_PARAM;
        /**
         * Monitoring init
         */
        if (inter == PQOS_INTER_MSR)
                ret = hw_cap_mon_discover(&det_mon, cpu);
#ifdef __linux__
        else if (inter == PQOS_INTER_OS || inter == PQOS_INTER_OS_RESCTRL_MON)
                ret = os_cap_mon_discover(&det_mon, cpu);
#endif
        switch (ret) {
        case PQOS_RETVAL_OK:
                LOG_INFO("Monitoring capability detected\n");
                sz += sizeof(struct pqos_capability);
                break;
        case PQOS_RETVAL_RESOURCE:
                LOG_INFO("Monitoring capability not detected\n");
                break;
        default:
                LOG_ERROR("Error encounter in monitoring discovery!\n");
                ret = PQOS_RETVAL_ERROR;
                goto error_exit;
        }

        /**
         * L3 Cache allocation init
         */
        ret = cap_l3ca_discover(&det_l3ca, cpu, inter);
        switch (ret) {
        case PQOS_RETVAL_OK:
                LOG_INFO("L3CA capability detected\n");
                LOG_INFO("L3 CAT details: CDP support=%d, CDP on=%d, "
                         "#COS=%u, #ways=%u, ways contention bit-mask 0x%lx\n",
                         det_l3ca->cdp, det_l3ca->cdp_on, det_l3ca->num_classes,
                         det_l3ca->num_ways,
                         (unsigned long)det_l3ca->way_contention);
                LOG_INFO("L3 CAT details: cache size %u bytes, "
                         "way size %u bytes\n",
                         det_l3ca->way_size * det_l3ca->num_ways,
                         det_l3ca->way_size);
                LOG_INFO("L3 CAT details: I/O RDT support=%d, I/O RDT on=%d\n",
                         det_l3ca->iordt, det_l3ca->iordt_on);
                sz += sizeof(struct pqos_capability);
                break;
        case PQOS_RETVAL_RESOURCE:
                LOG_INFO("L3CA capability not detected\n");
                break;
        default:
                LOG_ERROR("Fatal error encounter in L3 CAT discovery!\n");
                ret = PQOS_RETVAL_ERROR;
                goto error_exit;
        }

        /**
         * L2 Cache allocation init
         */
        ret = cap_l2ca_discover(&det_l2ca, cpu, inter);
        switch (ret) {
        case PQOS_RETVAL_OK:
                LOG_INFO("L2CA capability detected\n");
                LOG_INFO("L2 CAT details: CDP support=%d, CDP on=%d, "
                         "#COS=%u, #ways=%u, ways contention bit-mask 0x%lx\n",
                         det_l2ca->cdp, det_l2ca->cdp_on, det_l2ca->num_classes,
                         det_l2ca->num_ways,
                         (unsigned long)det_l2ca->way_contention);
                LOG_INFO("L2 CAT details: cache size %u bytes, way size %u "
                         "bytes\n",
                         det_l2ca->way_size * det_l2ca->num_ways,
                         det_l2ca->way_size);
                sz += sizeof(struct pqos_capability);
                break;
        case PQOS_RETVAL_RESOURCE:
                LOG_INFO("L2CA capability not detected\n");
                break;
        default:
                LOG_ERROR("Fatal error encounter in L2 CAT discovery!\n");
                ret = PQOS_RETVAL_ERROR;
                goto error_exit;
        }

        /**
         * Memory bandwidth allocation init
         */
        ret = cap_mba_discover(&det_mba, cpu, inter);
        switch (ret) {
        case PQOS_RETVAL_OK:
                LOG_INFO("MBA capability detected\n");
                LOG_INFO("MBA details: "
                         "#COS=%u, %slinear, max=%u, step=%u\n",
                         det_mba->num_classes, det_mba->is_linear ? "" : "non-",
                         det_mba->throttle_max, det_mba->throttle_step);
                sz += sizeof(struct pqos_capability);
                break;
        case PQOS_RETVAL_RESOURCE:
                LOG_INFO("MBA capability not detected\n");
                break;
        default:
                LOG_ERROR("Fatal error encounter in MBA discovery!\n");
                ret = PQOS_RETVAL_ERROR;
                goto error_exit;
        }

        /**
         * Slow Memory bandwidth allocation init
         */
        ret = cap_smba_discover(&det_smba, cpu, inter);
        switch (ret) {
        case PQOS_RETVAL_OK:
                LOG_INFO("SMBA capability detected\n");
                LOG_INFO("SMBA details: "
                         "#COS=%u, %slinear, max=%u, step=%u\n",
                         det_smba->num_classes,
                         det_smba->is_linear ? "" : "non-",
                         det_smba->throttle_max, det_smba->throttle_step);
                sz += sizeof(struct pqos_capability);
                break;
        case PQOS_RETVAL_RESOURCE:
                LOG_INFO("SMBA capability not detected\n");
                break;
        default:
                LOG_ERROR("Fatal error encounter in SMBA discovery!\n");
                ret = PQOS_RETVAL_ERROR;
                goto error_exit;
        }

        if (sz == 0) {
                LOG_ERROR("No Platform QoS capability discovered\n");
                ret = PQOS_RETVAL_ERROR;
                goto error_exit;
        }

        sz += sizeof(struct pqos_cap);
        _cap = (struct pqos_cap *)malloc(sz);
        if (_cap == NULL) {
                LOG_ERROR("Allocation error in %s()\n", __func__);
                ret = PQOS_RETVAL_ERROR;
                goto error_exit;
        }

        memset(_cap, 0, sz);
        _cap->mem_size = sz;
        _cap->version = PQOS_VERSION;

        if (det_mon != NULL) {
                _cap->capabilities[_cap->num_cap].type = PQOS_CAP_TYPE_MON;
                _cap->capabilities[_cap->num_cap].u.mon = det_mon;
                _cap->num_cap++;
                ret = PQOS_RETVAL_OK;
        }

        if (det_l3ca != NULL) {
                _cap->capabilities[_cap->num_cap].type = PQOS_CAP_TYPE_L3CA;
                _cap->capabilities[_cap->num_cap].u.l3ca = det_l3ca;
                _cap->num_cap++;
                ret = PQOS_RETVAL_OK;
        }

        if (det_l2ca != NULL) {
                _cap->capabilities[_cap->num_cap].type = PQOS_CAP_TYPE_L2CA;
                _cap->capabilities[_cap->num_cap].u.l2ca = det_l2ca;
                _cap->num_cap++;
                ret = PQOS_RETVAL_OK;
        }

        if (det_mba != NULL) {
                _cap->capabilities[_cap->num_cap].type = PQOS_CAP_TYPE_MBA;
                _cap->capabilities[_cap->num_cap].u.mba = det_mba;
                _cap->num_cap++;
                ret = PQOS_RETVAL_OK;
#ifdef __linux__
                /**
                 * Check status of MBA CTRL
                 */
                if (inter == PQOS_INTER_OS ||
                    inter == PQOS_INTER_OS_RESCTRL_MON) {
                        ret = os_cap_get_mba_ctrl(_cap, cpu, &det_mba->ctrl,
                                                  &det_mba->ctrl_on);
                        if (ret != PQOS_RETVAL_OK)
                                goto error_exit;
                }
#endif
        }

        if (det_smba != NULL) {
                _cap->capabilities[_cap->num_cap].type = PQOS_CAP_TYPE_SMBA;
                _cap->capabilities[_cap->num_cap].u.smba = det_smba;
                _cap->num_cap++;
                ret = PQOS_RETVAL_OK;
        }

        (*p_cap) = _cap;

error_exit:
        if (ret != PQOS_RETVAL_OK) {
                if (det_mon != NULL)
                        free(det_mon);
                if (det_l3ca != NULL)
                        free(det_l3ca);
                if (det_l2ca != NULL)
                        free(det_l2ca);
                if (det_mba != NULL)
                        free(det_mba);
                if (_cap != NULL)
                        free(_cap);
        }

        return ret;
}

/**
 * @brief Converts enumeration value into string
 *
 * @param interface interface
 *
 * @return converted enumeration value into string
 */
PQOS_STATIC const char *
_cap_interface_to_string(enum pqos_interface interface)
{
        switch (interface) {
        case PQOS_INTER_MSR:
                return "MSR";
        case PQOS_INTER_OS:
                return "OS";
        case PQOS_INTER_OS_RESCTRL_MON:
                return "OS_RESCTRL_MON";
        case PQOS_INTER_AUTO:
                return "AUTO";
        default:
                return "Unknown";
        }
}

/**
 * @brief Detects interface
 *
 * @param requested_interface requested interface
 * @param interface detected interface
 *
 * @return Operation status
 * @retval PQOS_RETVAL_OK success
 */
PQOS_STATIC int
discover_interface(enum pqos_interface requested_interface,
                   enum pqos_interface *interface)
{
        char *environment = NULL;

        LOG_INFO("Requested interface: %s\n",
                 _cap_interface_to_string(requested_interface));

        if (interface == NULL)
                return PQOS_RETVAL_PARAM;
#ifdef __linux__
        if (requested_interface != PQOS_INTER_MSR &&
            requested_interface != PQOS_INTER_OS &&
            requested_interface != PQOS_INTER_OS_RESCTRL_MON &&
            requested_interface != PQOS_INTER_AUTO)
#else
        if (requested_interface != PQOS_INTER_MSR &&
            requested_interface != PQOS_INTER_AUTO)
#endif
                return PQOS_RETVAL_PARAM;

        environment = getenv("RDT_IFACE");
        if (environment != NULL) {
                if (strncasecmp(environment, "OS", 2) == 0) {
                        if (requested_interface != PQOS_INTER_OS &&
                            requested_interface != PQOS_INTER_AUTO) {
                                LOG_ERROR("Interface initialization error!\n"
                                          "Your system has been restricted "
                                          "to use the OS interface only!\n");
                                return PQOS_RETVAL_ERROR;
                        }

                        *interface = PQOS_INTER_OS;
                } else if (strncasecmp(environment, "MSR", 3) == 0) {
                        if (requested_interface != PQOS_INTER_MSR &&
                            requested_interface != PQOS_INTER_AUTO) {
                                LOG_ERROR("Interface initialization error!\n"
                                          "Your system has been restricted "
                                          "to use the MSR interface only!\n");
                                return PQOS_RETVAL_ERROR;
                        }

                        *interface = PQOS_INTER_MSR;
                } else {
                        LOG_ERROR("Interface initialization error!\n"
                                  "Invalid interface enforcement selection.\n");
                        return PQOS_RETVAL_ERROR;
                }
        } else if (requested_interface == PQOS_INTER_AUTO) {
#ifdef __linux__
                if (resctrl_is_supported() == PQOS_RETVAL_OK)
                        *interface = PQOS_INTER_OS;
                else
                        *interface = PQOS_INTER_MSR;
#else
                *interface = PQOS_INTER_MSR;
#endif
        } else {
                *interface = requested_interface;
        }

        LOG_INFO("Selected interface: %s\n",
                 _cap_interface_to_string(*interface));
        return PQOS_RETVAL_OK;
}

/*
 * =======================================
 * =======================================
 *
 * initialize and shutdown
 *
 * =======================================
 * =======================================
 */
int
pqos_init(const struct pqos_config *config)
{
        int ret = PQOS_RETVAL_OK;
        unsigned i = 0, max_core = 0;
        int cat_init = 0, mon_init = 0;
        struct pqos_cap *cap = NULL;
        struct pqos_cpuinfo *cpu = NULL;
        struct pqos_devinfo *dev = NULL;
        enum pqos_interface interface;

        if (config == NULL)
                return PQOS_RETVAL_PARAM;

        if (lock_init() != 0) {
                fprintf(stderr, "API lock initialization error!\n");
                return PQOS_RETVAL_ERROR;
        }

        lock_get();

        ret = _pqos_check_init(0);
        if (ret != PQOS_RETVAL_OK) {
                lock_release();
                lock_fini();
                return ret;
        }

        memset(&m_sysconf, 0, sizeof(m_sysconf));

        ret = log_init(config->fd_log, config->callback_log,
                       config->context_log, config->verbose);
        if (ret != LOG_RETVAL_OK) {
                fprintf(stderr, "log_init() error\n");
                goto init_error;
        }

        ret = discover_interface(config->interface, &interface);
        if (ret != PQOS_RETVAL_OK) {
                LOG_ERROR("Cannot select the interface!\n");
                goto log_init_error;
        }

        /**
         * Topology not provided through config.
         * CPU discovery done through internal mechanism.
         */
        ret = cpuinfo_init(interface, &cpu);
        if (ret != 0 || cpu == NULL) {
                LOG_ERROR("cpuinfo_init() error %d\n", ret);
                ret = PQOS_RETVAL_ERROR;
                goto log_init_error;
        }

        /**
         * Find max core id in the topology
         */
        for (i = 0; i < cpu->num_cores; i++)
                if (cpu->cores[i].lcore > max_core)
                        max_core = cpu->cores[i].lcore;

        ret = machine_init(max_core);
        if (ret != PQOS_RETVAL_OK) {
                LOG_ERROR("machine_init() error %d\n", ret);
                goto cpuinfo_init_error;
        }

        if (hw_detect_hybrid())
                LOG_WARN(
                    "Hybrid part with L2 CAT support detected.\n"
                    "      L2 CAT on hybrid parts is not yet supported in "
                    "pqos\n"
                    "      tools and may not behave as expected and cause\n"
                    "      performance degradation. For more information, "
                    "see:\n"
                    "      "
                    "https://github.com/intel/intel-cmt-cat/issues/272\n");

#ifdef __linux__
        if (interface == PQOS_INTER_OS ||
            interface == PQOS_INTER_OS_RESCTRL_MON) {
                ret = os_cap_init(interface);
                if (ret != PQOS_RETVAL_OK) {
                        LOG_ERROR("os_cap_init() error %d\n", ret);
                        goto machine_init_error;
                }
        } else if (access(RESCTRL_PATH "/cpus", F_OK) == 0)
                LOG_WARN("resctl filesystem mounted! Using MSR "
                         "interface may corrupt resctrl filesystem "
                         "and cause unexpected behaviour\n");
#endif

        ret = discover_capabilities(&cap, cpu, interface);
        if (ret != PQOS_RETVAL_OK) {
                LOG_ERROR("discover_capabilities() error %d\n", ret);
                goto machine_init_error;
        }

        ret = _pqos_utils_init(interface);
        if (ret != PQOS_RETVAL_OK) {
                fprintf(stderr, "Utils initialization error!\n");
                goto machine_init_error;
        }

        ret = api_init(interface, cpu->vendor);
        if (ret != PQOS_RETVAL_OK) {
                LOG_ERROR("lock_init() error %d\n", ret);
                goto machine_init_error;
        }

        _pqos_set_inter(interface);

        ret = pqos_alloc_init(cpu, cap, config);
        switch (ret) {
        case PQOS_RETVAL_BUSY:
                LOG_ERROR("OS allocation init error!\n");
                goto machine_init_error;
        case PQOS_RETVAL_OK:
                LOG_DEBUG("allocation init OK\n");
                cat_init = 1;
                break;
        default:
                LOG_ERROR("allocation init error %d\n", ret);
                break;
        }

        /**
         * If monitoring capability has been discovered
         * then get max RMID supported by a CPU socket
         * and allocate memory for RMID table
         */
        ret = pqos_mon_init(cpu, cap, config);
        switch (ret) {
        case PQOS_RETVAL_RESOURCE:
                LOG_DEBUG("monitoring init aborted: feature not present\n");
                break;
        case PQOS_RETVAL_OK:
                LOG_DEBUG("monitoring init OK\n");
                mon_init = 1;
                break;
        case PQOS_RETVAL_ERROR:
        default:
                LOG_ERROR("monitoring init error %d\n", ret);
                break;
        }

        if (cat_init == 0 && mon_init == 0) {
                LOG_ERROR("None of detected capabilities could be "
                          "initialized!\n");
                ret = PQOS_RETVAL_ERROR;
                goto machine_init_error;
        }

        ret = iordt_init(cap, &dev);
        switch (ret) {
        case PQOS_RETVAL_RESOURCE:
                LOG_DEBUG("I/O RDT init aborted: feature not present\n");
                break;
        case PQOS_RETVAL_OK:
                LOG_DEBUG("I/O RDT init OK\n");
                break;
        case PQOS_RETVAL_ERROR:
        default:
                LOG_ERROR("I/O RDT init error %d\n", ret);
                break;
        }

        if (ret == PQOS_RETVAL_RESOURCE)
                ret = PQOS_RETVAL_OK;

        if (ret != PQOS_RETVAL_OK)
                iordt_fini();
machine_init_error:
        if (ret != PQOS_RETVAL_OK)
                (void)machine_fini();
cpuinfo_init_error:
        if (ret != PQOS_RETVAL_OK)
                (void)cpuinfo_fini();
log_init_error:
        if (ret != PQOS_RETVAL_OK)
                (void)log_fini();
init_error:
        if (ret != PQOS_RETVAL_OK) {
                if (cap != NULL) {
                        for (i = 0; i < cap->num_cap; i++)
                                free(cap->capabilities[i].u.generic_ptr);
                        free(cap);
                }
        }

        if (ret == PQOS_RETVAL_OK) {
                m_init_done = 1;
                m_sysconf.cap = cap;
                m_sysconf.cpu = cpu;
                m_sysconf.dev = dev;
        }

        lock_release();

        if (ret != PQOS_RETVAL_OK)
                lock_fini();

        return ret;
}

int
pqos_fini(void)
{
        int ret = PQOS_RETVAL_OK;
        int retval = PQOS_RETVAL_OK;
        unsigned i = 0;

        lock_get();

        ret = _pqos_check_init(1);
        if (ret != PQOS_RETVAL_OK) {
                lock_release();
                lock_fini();
                return ret;
        }

        pqos_mon_fini();
        pqos_alloc_fini();

        ret = iordt_fini();
        if (ret != 0) {
                retval = PQOS_RETVAL_ERROR;
                LOG_ERROR("iordt_fini() error %d\n", ret);
        }

        ret = cpuinfo_fini();
        if (ret != 0) {
                retval = PQOS_RETVAL_ERROR;
                LOG_ERROR("cpuinfo_fini() error %d\n", ret);
        }

        ret = machine_fini();
        if (ret != PQOS_RETVAL_OK) {
                retval = ret;
                LOG_ERROR("machine_fini() error %d\n", ret);
        }

        ret = log_fini();
        if (ret != PQOS_RETVAL_OK)
                retval = ret;

        if (m_sysconf.cap)
                for (i = 0; i < m_sysconf.cap->num_cap; i++)
                        free(m_sysconf.cap->capabilities[i].u.generic_ptr);
        free(m_sysconf.cap);
        m_sysconf.cap = NULL;
        m_sysconf.cpu = NULL;
        m_sysconf.dev = NULL;

        m_init_done = 0;

        lock_release();

        if (lock_fini() != 0)
                retval = PQOS_RETVAL_ERROR;

        return retval;
}

/**
 * =======================================
 * =======================================
 *
 * capabilities
 *
 * =======================================
 * =======================================
 */

int
pqos_cap_get(const struct pqos_cap **cap, const struct pqos_cpuinfo **cpu)
{
        int ret = PQOS_RETVAL_OK;

        if (cap == NULL && cpu == NULL)
                return PQOS_RETVAL_PARAM;

        lock_get();

        ret = _pqos_check_init(1);
        if (ret != PQOS_RETVAL_OK) {
                lock_release();
                return ret;
        }

        if (cap != NULL) {
                *cap = _pqos_get_cap();
                ASSERT(*cap != NULL);
        }
        if (cpu != NULL) {
                *cpu = _pqos_get_cpu();
                ASSERT(*cpu != NULL);
        }

        lock_release();
        return PQOS_RETVAL_OK;
}

int
pqos_sysconfig_get(const struct pqos_sysconfig **sysconf)
{
        int ret = PQOS_RETVAL_OK;

        if (sysconf == NULL)
                return PQOS_RETVAL_PARAM;

        lock_get();

        ret = _pqos_check_init(1);
        if (ret != PQOS_RETVAL_OK) {
                lock_release();
                return ret;
        }

        *sysconf = _pqos_get_sysconfig();

        lock_release();
        return PQOS_RETVAL_OK;
}

void
_pqos_cap_l3cdp_change(const enum pqos_cdp_config cdp)
{
        struct pqos_cap_l3ca *l3_cap = NULL;
        struct pqos_cap_l3ca cap;
        int ret;
        unsigned i;
        enum pqos_interface interface = _pqos_get_inter();

        ASSERT(cdp == PQOS_REQUIRE_CDP_ON || cdp == PQOS_REQUIRE_CDP_OFF ||
               cdp == PQOS_REQUIRE_CDP_ANY);
        ASSERT(m_sysconf.cap != NULL);

        if (m_sysconf.cap == NULL)
                return;

        for (i = 0; i < m_sysconf.cap->num_cap && l3_cap == NULL; i++)
                if (m_sysconf.cap->capabilities[i].type == PQOS_CAP_TYPE_L3CA)
                        l3_cap = m_sysconf.cap->capabilities[i].u.l3ca;

        if (l3_cap == NULL)
                return;

        switch (interface) {
        case PQOS_INTER_MSR:
                ret = hw_cap_l3ca_discover(&cap, m_sysconf.cpu);
                break;
#ifdef __linux__
        case PQOS_INTER_OS: /* fall through */
        case PQOS_INTER_OS_RESCTRL_MON:
                ret = os_cap_l3ca_discover(&cap, m_sysconf.cpu);
                break;
#endif
        default:
                ret = PQOS_RETVAL_RESOURCE;
                break;
        }

        if (ret == PQOS_RETVAL_OK) {
                *l3_cap = cap;
                return;
        }

        if (cdp == PQOS_REQUIRE_CDP_ON && !l3_cap->cdp_on) {
                /* turn on */
                l3_cap->cdp_on = 1;
                l3_cap->num_classes = l3_cap->num_classes / 2;
        }

        if (cdp == PQOS_REQUIRE_CDP_OFF && l3_cap->cdp_on) {
                /* turn off */
                l3_cap->cdp_on = 0;
                l3_cap->num_classes = l3_cap->num_classes * 2;
        }
}

void
_pqos_cap_l3iordt_change(const enum pqos_iordt_config iordt)
{
        struct pqos_cap_l3ca *l3_cap = NULL;
        struct pqos_cap *cap = m_sysconf.cap;
        unsigned i;

        ASSERT(iordt == PQOS_REQUIRE_IORDT_ON ||
               iordt == PQOS_REQUIRE_IORDT_OFF ||
               iordt == PQOS_REQUIRE_IORDT_ANY);
        ASSERT(m_sysconf.cap != NULL);

        if (cap == NULL)
                return;

        for (i = 0; i < cap->num_cap && l3_cap == NULL; i++)
                if (cap->capabilities[i].type == PQOS_CAP_TYPE_L3CA)
                        l3_cap = cap->capabilities[i].u.l3ca;

        if (l3_cap == NULL)
                return;

        /* turn on */
        if (iordt == PQOS_REQUIRE_IORDT_ON && !l3_cap->iordt_on)
                l3_cap->iordt_on = 1;

        /* turn off */
        if (iordt == PQOS_REQUIRE_IORDT_OFF && l3_cap->iordt_on)
                l3_cap->iordt_on = 0;
}

void
_pqos_cap_l2cdp_change(const enum pqos_cdp_config cdp)
{
        struct pqos_cap_l2ca *l2_cap = NULL;
        struct pqos_cap_l2ca cap;
        unsigned i;
        int ret;
        enum pqos_interface interface = _pqos_get_inter();

        ASSERT(cdp == PQOS_REQUIRE_CDP_ON || cdp == PQOS_REQUIRE_CDP_OFF ||
               cdp == PQOS_REQUIRE_CDP_ANY);
        ASSERT(m_sysconf.cap != NULL);

        if (m_sysconf.cap == NULL)
                return;

        for (i = 0; i < m_sysconf.cap->num_cap && l2_cap == NULL; i++)
                if (m_sysconf.cap->capabilities[i].type == PQOS_CAP_TYPE_L2CA)
                        l2_cap = m_sysconf.cap->capabilities[i].u.l2ca;

        if (l2_cap == NULL)
                return;

        switch (interface) {
        case PQOS_INTER_MSR:
                ret = hw_cap_l2ca_discover(&cap, m_sysconf.cpu);
                break;
#ifdef __linux__
        case PQOS_INTER_OS:
        case PQOS_INTER_OS_RESCTRL_MON:
                ret = os_cap_l2ca_discover(&cap, m_sysconf.cpu);
                break;
#endif
        default:
                ret = PQOS_RETVAL_RESOURCE;
                break;
        }

        if (ret == PQOS_RETVAL_OK) {
                *l2_cap = cap;
                return;
        }

        if (cdp == PQOS_REQUIRE_CDP_ON && !l2_cap->cdp_on) {
                /* turn on */
                l2_cap->cdp_on = 1;
                l2_cap->num_classes = l2_cap->num_classes / 2;
        }

        if (cdp == PQOS_REQUIRE_CDP_OFF && l2_cap->cdp_on) {
                /* turn off */
                l2_cap->cdp_on = 0;
                l2_cap->num_classes = l2_cap->num_classes * 2;
        }
}

void
_pqos_cap_mba_change(const enum pqos_mba_config cfg)
{
        struct pqos_cap_mba *mba_cap = NULL;
        unsigned i;
#ifdef __linux__
        enum pqos_interface interface = _pqos_get_inter();
#endif

        ASSERT(cfg == PQOS_MBA_DEFAULT || cfg == PQOS_MBA_CTRL ||
               cfg == PQOS_MBA_ANY);
        ASSERT(m_sysconf.cap != NULL);

        if (m_sysconf.cap == NULL)
                return;

        for (i = 0; i < m_sysconf.cap->num_cap && mba_cap == NULL; i++)
                if (m_sysconf.cap->capabilities[i].type == PQOS_CAP_TYPE_MBA)
                        mba_cap = m_sysconf.cap->capabilities[i].u.mba;

        if (mba_cap == NULL)
                return;

#ifdef __linux__
        /* refresh number of classes */
        if (interface == PQOS_INTER_OS ||
            interface == PQOS_INTER_OS_RESCTRL_MON) {
                int ret;
                unsigned num_classes;

                ret = resctrl_alloc_get_num_closids(&num_classes);
                if (ret == PQOS_RETVAL_OK)
                        mba_cap->num_classes = num_classes;
        }
#endif

        if (cfg == PQOS_MBA_DEFAULT)
                mba_cap->ctrl_on = 0;
        else if (cfg == PQOS_MBA_CTRL) {
#ifdef __linux__
                if (interface != PQOS_INTER_MSR)
                        mba_cap->ctrl = 1;
#endif
                mba_cap->ctrl_on = 1;
        }
}

void
_pqos_cap_mon_iordt_change(const enum pqos_iordt_config iordt)
{
        struct pqos_cap_mon *mon_cap = NULL;
        unsigned i;

        ASSERT(iordt == PQOS_REQUIRE_IORDT_ON ||
               iordt == PQOS_REQUIRE_IORDT_OFF ||
               iordt == PQOS_REQUIRE_IORDT_ANY);
        ASSERT(m_sysconf.cap != NULL);

        if (m_sysconf.cap == NULL)
                return;

        for (i = 0; i < m_sysconf.cap->num_cap && mon_cap == NULL; i++)
                if (m_sysconf.cap->capabilities[i].type == PQOS_CAP_TYPE_MON)
                        mon_cap = m_sysconf.cap->capabilities[i].u.mon;

        if (mon_cap == NULL)
                return;

        /* turn on */
        if (iordt == PQOS_REQUIRE_IORDT_ON && !mon_cap->iordt_on)
                mon_cap->iordt_on = 1;

        /* turn off */
        if (iordt == PQOS_REQUIRE_IORDT_OFF && mon_cap->iordt_on)
                mon_cap->iordt_on = 0;
}

void
_pqos_cap_mon_snc_change(const enum pqos_snc_config cfg)
{
        struct pqos_cap_mon *mon_cap = NULL;
        unsigned i;

        ASSERT(cfg == PQOS_REQUIRE_SNC_LOCAL || cfg == PQOS_REQUIRE_SNC_TOTAL ||
               cfg == PQOS_REQUIRE_SNC_ANY);
        ASSERT(m_sysconf.cap != NULL);

        if (m_sysconf.cap == NULL)
                return;

        for (i = 0; i < m_sysconf.cap->num_cap && mon_cap == NULL; i++)
                if (m_sysconf.cap->capabilities[i].type == PQOS_CAP_TYPE_MON)
                        mon_cap = m_sysconf.cap->capabilities[i].u.mon;

        if (mon_cap == NULL)
                return;

        if (mon_cap->snc_num == 1)
                return;

        if (cfg == PQOS_REQUIRE_SNC_TOTAL)
                mon_cap->snc_mode = PQOS_SNC_TOTAL;

        if (cfg == PQOS_REQUIRE_SNC_LOCAL)
                mon_cap->snc_mode = PQOS_SNC_LOCAL;
}

const struct pqos_sysconfig *
_pqos_get_sysconfig(void)
{
        return &m_sysconf;
}

void
_pqos_cap_smba_change(const enum pqos_mba_config cfg)
{
        struct pqos_cap_mba *smba_cap = NULL;
        unsigned i;
#ifdef __linux__
        enum pqos_interface interface = _pqos_get_inter();
#endif
        ASSERT(cfg == PQOS_MBA_DEFAULT || cfg == PQOS_MBA_CTRL ||
               cfg == PQOS_MBA_ANY);
        ASSERT(m_sysconf.cap != NULL);

        if (m_sysconf.cap == NULL)
                return;

        for (i = 0; i < m_sysconf.cap->num_cap && smba_cap == NULL; i++)
                if (m_sysconf.cap->capabilities[i].type == PQOS_CAP_TYPE_SMBA)
                        smba_cap = m_sysconf.cap->capabilities[i].u.smba;

        if (smba_cap == NULL)
                return;

#ifdef __linux__
        /* refresh number of classes */
        if (interface == PQOS_INTER_OS ||
            interface == PQOS_INTER_OS_RESCTRL_MON) {
                int ret;
                unsigned num_classes;

                ret = resctrl_alloc_get_num_closids(&num_classes);
                if (ret == PQOS_RETVAL_OK)
                        smba_cap->num_classes = num_classes;
        }
#endif

        if (cfg == PQOS_MBA_DEFAULT)
                smba_cap->ctrl_on = 0;
        else if (cfg == PQOS_MBA_CTRL) {
#ifdef __linux__
                if (interface != PQOS_INTER_MSR)
                        smba_cap->ctrl = 1;
#endif
                smba_cap->ctrl_on = 1;
        }
}

const struct pqos_cap *
_pqos_get_cap(void)
{
        ASSERT(m_sysconf.cap != NULL);
        return m_sysconf.cap;
}

const struct pqos_cpuinfo *
_pqos_get_cpu(void)
{
        ASSERT(m_sysconf.cpu != NULL);
        return m_sysconf.cpu;
}

const struct pqos_devinfo *
_pqos_get_dev(void)
{
        return m_sysconf.dev;
}

int
cap_get_l3ca_non_contignous(void)
{
        const struct pqos_capability *l3cap =
            _pqos_cap_get_type(PQOS_CAP_TYPE_L3CA);

        /* If L3 capability is not available, return contiguous CBM support */
        if (l3cap == NULL)
                return !PQOS_CPUID_CAT_NON_CONTIGUOUS_CBM_SUPPORT;

        return l3cap->u.l3ca->non_contiguous_cbm;
}

int
cap_get_l2ca_non_contignous(void)
{
        const struct pqos_capability *l2cap =
            _pqos_cap_get_type(PQOS_CAP_TYPE_L2CA);

        /* If L2 capability is not available, return contiguous CBM support */
        if (l2cap == NULL)
                return !PQOS_CPUID_CAT_NON_CONTIGUOUS_CBM_SUPPORT;

        return l2cap->u.l2ca->non_contiguous_cbm;
}

/**
 * =======================================
 * =======================================
 *
 * interface
 *
 * =======================================
 * =======================================
 */

int
pqos_inter_get(enum pqos_interface *interface)
{
        int ret = PQOS_RETVAL_OK;

        if (interface == NULL)
                return PQOS_RETVAL_PARAM;

        lock_get();

        ret = _pqos_check_init(1);
        if (ret != PQOS_RETVAL_OK) {
                lock_release();
                return ret;
        }

        *interface = _pqos_get_inter();

        lock_release();
        return PQOS_RETVAL_OK;
}