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
|
/*
* Copyright © 2017 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <poll.h>
#include <sched.h>
#include "igt.h"
#include "igt_core.h"
#include "igt_perf.h"
#include "igt_sysfs.h"
#include "igt_pm.h"
#include "sw_sync.h"
IGT_TEST_DESCRIPTION("Test the i915 pmu perf interface");
const double tolerance = 0.05f;
const unsigned long batch_duration_ns = 500e6;
static int open_pmu(uint64_t config)
{
int fd;
fd = perf_i915_open(config);
igt_skip_on(fd < 0 && errno == ENODEV);
igt_assert(fd >= 0);
return fd;
}
static int open_group(uint64_t config, int group)
{
int fd;
fd = perf_i915_open_group(config, group);
igt_skip_on(fd < 0 && errno == ENODEV);
igt_assert(fd >= 0);
return fd;
}
static void
init(int gem_fd, const struct intel_execution_engine2 *e, uint8_t sample)
{
int fd, err = 0;
bool exists;
errno = 0;
fd = perf_i915_open(__I915_PMU_ENGINE(e->class, e->instance, sample));
if (fd < 0)
err = errno;
exists = gem_has_engine(gem_fd, e->class, e->instance);
if (intel_gen(intel_get_drm_devid(gem_fd)) < 6 &&
sample == I915_SAMPLE_SEMA)
exists = false;
if (exists) {
igt_assert_eq(err, 0);
igt_assert_fd(fd);
close(fd);
} else {
igt_assert_lt(fd, 0);
igt_assert_eq(err, ENODEV);
}
}
static uint64_t __pmu_read_single(int fd, uint64_t *ts)
{
uint64_t data[2];
igt_assert_eq(read(fd, data, sizeof(data)), sizeof(data));
if (ts)
*ts = data[1];
return data[0];
}
static uint64_t pmu_read_single(int fd)
{
return __pmu_read_single(fd, NULL);
}
static uint64_t pmu_read_multi(int fd, unsigned int num, uint64_t *val)
{
uint64_t buf[2 + num];
unsigned int i;
igt_assert_eq(read(fd, buf, sizeof(buf)), sizeof(buf));
for (i = 0; i < num; i++)
val[i] = buf[2 + i];
return buf[1];
}
#define __assert_within_epsilon(x, ref, tol_up, tol_down) \
igt_assert_f((double)(x) <= (1.0 + (tol_up)) * (double)(ref) && \
(double)(x) >= (1.0 - (tol_down)) * (double)(ref), \
"'%s' != '%s' (%f not within +%f%%/-%f%% tolerance of %f)\n",\
#x, #ref, (double)(x), \
(tol_up) * 100.0, (tol_down) * 100.0, \
(double)(ref))
#define assert_within_epsilon(x, ref, tolerance) \
__assert_within_epsilon(x, ref, tolerance, tolerance)
/*
* Helper for cases where we assert on time spent sleeping (directly or
* indirectly), so make it more robust by ensuring the system sleep time
* is within test tolerance to start with.
*/
static unsigned int measured_usleep(unsigned int usec)
{
struct timespec ts = { };
unsigned int slept;
slept = igt_nsec_elapsed(&ts);
igt_assert(slept == 0);
do {
usleep(usec - slept);
slept = igt_nsec_elapsed(&ts) / 1000;
} while (slept < usec);
return igt_nsec_elapsed(&ts);
}
static unsigned int e2ring(int gem_fd, const struct intel_execution_engine2 *e)
{
return gem_class_instance_to_eb_flags(gem_fd, e->class, e->instance);
}
#define TEST_BUSY (1)
#define FLAG_SYNC (2)
#define TEST_TRAILING_IDLE (4)
#define TEST_RUNTIME_PM (8)
#define FLAG_LONG (16)
#define FLAG_HANG (32)
static igt_spin_t * __spin_poll(int fd, uint32_t ctx, unsigned long flags)
{
if (gem_can_store_dword(fd, flags))
return __igt_spin_batch_new_poll(fd, ctx, flags);
else
return __igt_spin_batch_new(fd, ctx, flags, 0);
}
static unsigned long __spin_wait(int fd, igt_spin_t *spin)
{
struct timespec start = { };
igt_nsec_elapsed(&start);
if (spin->running) {
unsigned long timeout = 0;
while (!READ_ONCE(*spin->running)) {
unsigned long t = igt_nsec_elapsed(&start);
if ((t - timeout) > 250e6) {
timeout = t;
igt_warn("Spinner not running after %.2fms\n",
(double)t / 1e6);
}
}
} else {
igt_debug("__spin_wait - usleep mode\n");
usleep(500e3); /* Better than nothing! */
}
return igt_nsec_elapsed(&start);
}
static igt_spin_t * __spin_sync(int fd, uint32_t ctx, unsigned long flags)
{
igt_spin_t *spin = __spin_poll(fd, ctx, flags);
__spin_wait(fd, spin);
return spin;
}
static igt_spin_t * spin_sync(int fd, uint32_t ctx, unsigned long flags)
{
igt_require_gem(fd);
return __spin_sync(fd, ctx, flags);
}
static void end_spin(int fd, igt_spin_t *spin, unsigned int flags)
{
if (!spin)
return;
igt_spin_batch_end(spin);
if (flags & FLAG_SYNC)
gem_sync(fd, spin->handle);
if (flags & TEST_TRAILING_IDLE) {
unsigned long t, timeout = 0;
struct timespec start = { };
igt_nsec_elapsed(&start);
do {
t = igt_nsec_elapsed(&start);
if (gem_bo_busy(fd, spin->handle) &&
(t - timeout) > 10e6) {
timeout = t;
igt_warn("Spinner not idle after %.2fms\n",
(double)t / 1e6);
}
usleep(1e3);
} while (t < batch_duration_ns / 5);
}
}
static void
single(int gem_fd, const struct intel_execution_engine2 *e, unsigned int flags)
{
unsigned long slept;
igt_spin_t *spin;
uint64_t val;
int fd;
fd = open_pmu(I915_PMU_ENGINE_BUSY(e->class, e->instance));
if (flags & TEST_BUSY)
spin = spin_sync(gem_fd, 0, e2ring(gem_fd, e));
else
spin = NULL;
val = pmu_read_single(fd);
slept = measured_usleep(batch_duration_ns / 1000);
if (flags & TEST_TRAILING_IDLE)
end_spin(gem_fd, spin, flags);
val = pmu_read_single(fd) - val;
if (flags & FLAG_HANG)
igt_force_gpu_reset(gem_fd);
else
end_spin(gem_fd, spin, FLAG_SYNC);
assert_within_epsilon(val, flags & TEST_BUSY ? slept : 0.f, tolerance);
/* Check for idle after hang. */
if (flags & FLAG_HANG) {
gem_quiescent_gpu(gem_fd);
igt_assert(!gem_bo_busy(gem_fd, spin->handle));
val = pmu_read_single(fd);
slept = measured_usleep(batch_duration_ns / 1000);
val = pmu_read_single(fd) - val;
assert_within_epsilon(val, 0, tolerance);
}
igt_spin_batch_free(gem_fd, spin);
close(fd);
gem_quiescent_gpu(gem_fd);
}
static void
busy_start(int gem_fd, const struct intel_execution_engine2 *e)
{
unsigned long slept;
uint64_t val, ts[2];
igt_spin_t *spin;
int fd;
/*
* Defeat the busy stats delayed disable, we need to guarantee we are
* the first user.
*/
sleep(2);
spin = __spin_sync(gem_fd, 0, e2ring(gem_fd, e));
fd = open_pmu(I915_PMU_ENGINE_BUSY(e->class, e->instance));
val = __pmu_read_single(fd, &ts[0]);
slept = measured_usleep(batch_duration_ns / 1000);
val = __pmu_read_single(fd, &ts[1]) - val;
igt_debug("slept=%lu perf=%"PRIu64"\n", slept, ts[1] - ts[0]);
igt_spin_batch_free(gem_fd, spin);
close(fd);
assert_within_epsilon(val, ts[1] - ts[0], tolerance);
gem_quiescent_gpu(gem_fd);
}
/*
* This test has a potentially low rate of catching the issue it is trying to
* catch. Or in other words, quite high rate of false negative successes. We
* will depend on the CI systems running it a lot to detect issues.
*/
static void
busy_double_start(int gem_fd, const struct intel_execution_engine2 *e)
{
unsigned long slept;
uint64_t val, val2, ts[2];
igt_spin_t *spin[2];
uint32_t ctx;
int fd;
ctx = gem_context_create(gem_fd);
/*
* Defeat the busy stats delayed disable, we need to guarantee we are
* the first user.
*/
sleep(2);
/*
* Submit two contexts, with a pause in between targeting the ELSP
* re-submission in execlists mode. Make sure busyness is correctly
* reported with the engine busy, and after the engine went idle.
*/
spin[0] = __spin_sync(gem_fd, 0, e2ring(gem_fd, e));
usleep(500e3);
spin[1] = __igt_spin_batch_new(gem_fd, ctx, e2ring(gem_fd, e), 0);
/*
* Open PMU as fast as possible after the second spin batch in attempt
* to be faster than the driver handling lite-restore.
*/
fd = open_pmu(I915_PMU_ENGINE_BUSY(e->class, e->instance));
val = __pmu_read_single(fd, &ts[0]);
slept = measured_usleep(batch_duration_ns / 1000);
val = __pmu_read_single(fd, &ts[1]) - val;
igt_debug("slept=%lu perf=%"PRIu64"\n", slept, ts[1] - ts[0]);
igt_spin_batch_end(spin[0]);
igt_spin_batch_end(spin[1]);
/* Wait for GPU idle to verify PMU reports idle. */
gem_quiescent_gpu(gem_fd);
val2 = pmu_read_single(fd);
usleep(batch_duration_ns / 1000);
val2 = pmu_read_single(fd) - val2;
igt_info("busy=%"PRIu64" idle=%"PRIu64"\n", val, val2);
igt_spin_batch_free(gem_fd, spin[0]);
igt_spin_batch_free(gem_fd, spin[1]);
close(fd);
gem_context_destroy(gem_fd, ctx);
assert_within_epsilon(val, ts[1] - ts[0], tolerance);
igt_assert_eq(val2, 0);
gem_quiescent_gpu(gem_fd);
}
static void log_busy(unsigned int num_engines, uint64_t *val)
{
char buf[1024];
int rem = sizeof(buf);
unsigned int i;
char *p = buf;
for (i = 0; i < num_engines; i++) {
int len;
len = snprintf(p, rem, "%u=%" PRIu64 "\n", i, val[i]);
igt_assert(len > 0);
rem -= len;
p += len;
}
igt_info("%s", buf);
}
static void
busy_check_all(int gem_fd, const struct intel_execution_engine2 *e,
const unsigned int num_engines, unsigned int flags)
{
const struct intel_execution_engine2 *e_;
uint64_t tval[2][num_engines];
unsigned int busy_idx = 0, i;
uint64_t val[num_engines];
int fd[num_engines];
unsigned long slept;
igt_spin_t *spin;
i = 0;
fd[0] = -1;
for_each_engine_class_instance(gem_fd, e_) {
if (e == e_)
busy_idx = i;
fd[i++] = open_group(I915_PMU_ENGINE_BUSY(e_->class,
e_->instance),
fd[0]);
}
igt_assert_eq(i, num_engines);
spin = spin_sync(gem_fd, 0, e2ring(gem_fd, e));
pmu_read_multi(fd[0], num_engines, tval[0]);
slept = measured_usleep(batch_duration_ns / 1000);
if (flags & TEST_TRAILING_IDLE)
end_spin(gem_fd, spin, flags);
pmu_read_multi(fd[0], num_engines, tval[1]);
end_spin(gem_fd, spin, FLAG_SYNC);
igt_spin_batch_free(gem_fd, spin);
close(fd[0]);
for (i = 0; i < num_engines; i++)
val[i] = tval[1][i] - tval[0][i];
log_busy(num_engines, val);
assert_within_epsilon(val[busy_idx], slept, tolerance);
for (i = 0; i < num_engines; i++) {
if (i == busy_idx)
continue;
assert_within_epsilon(val[i], 0.0f, tolerance);
}
gem_quiescent_gpu(gem_fd);
}
static void
__submit_spin_batch(int gem_fd, igt_spin_t *spin,
const struct intel_execution_engine2 *e,
int offset)
{
struct drm_i915_gem_execbuffer2 eb = spin->execbuf;
eb.flags &= ~(0x3f | I915_EXEC_BSD_MASK);
eb.flags |= e2ring(gem_fd, e) | I915_EXEC_NO_RELOC;
eb.batch_start_offset += offset;
gem_execbuf(gem_fd, &eb);
}
static void
most_busy_check_all(int gem_fd, const struct intel_execution_engine2 *e,
const unsigned int num_engines, unsigned int flags)
{
const struct intel_execution_engine2 *e_;
uint64_t tval[2][num_engines];
uint64_t val[num_engines];
int fd[num_engines];
unsigned long slept;
igt_spin_t *spin = NULL;
unsigned int idle_idx, i;
i = 0;
for_each_engine_class_instance(gem_fd, e_) {
if (e == e_)
idle_idx = i;
else if (spin)
__submit_spin_batch(gem_fd, spin, e_, 64);
else
spin = __spin_poll(gem_fd, 0, e2ring(gem_fd, e_));
val[i++] = I915_PMU_ENGINE_BUSY(e_->class, e_->instance);
}
igt_assert(i == num_engines);
igt_require(spin); /* at least one busy engine */
fd[0] = -1;
for (i = 0; i < num_engines; i++)
fd[i] = open_group(val[i], fd[0]);
/* Small delay to allow engines to start. */
usleep(__spin_wait(gem_fd, spin) * num_engines / 1e3);
pmu_read_multi(fd[0], num_engines, tval[0]);
slept = measured_usleep(batch_duration_ns / 1000);
if (flags & TEST_TRAILING_IDLE)
end_spin(gem_fd, spin, flags);
pmu_read_multi(fd[0], num_engines, tval[1]);
end_spin(gem_fd, spin, FLAG_SYNC);
igt_spin_batch_free(gem_fd, spin);
close(fd[0]);
for (i = 0; i < num_engines; i++)
val[i] = tval[1][i] - tval[0][i];
log_busy(num_engines, val);
for (i = 0; i < num_engines; i++) {
if (i == idle_idx)
assert_within_epsilon(val[i], 0.0f, tolerance);
else
assert_within_epsilon(val[i], slept, tolerance);
}
gem_quiescent_gpu(gem_fd);
}
static void
all_busy_check_all(int gem_fd, const unsigned int num_engines,
unsigned int flags)
{
const struct intel_execution_engine2 *e;
uint64_t tval[2][num_engines];
uint64_t val[num_engines];
int fd[num_engines];
unsigned long slept;
igt_spin_t *spin = NULL;
unsigned int i;
i = 0;
for_each_engine_class_instance(gem_fd, e) {
if (spin)
__submit_spin_batch(gem_fd, spin, e, 64);
else
spin = __spin_poll(gem_fd, 0, e2ring(gem_fd, e));
val[i++] = I915_PMU_ENGINE_BUSY(e->class, e->instance);
}
igt_assert(i == num_engines);
fd[0] = -1;
for (i = 0; i < num_engines; i++)
fd[i] = open_group(val[i], fd[0]);
/* Small delay to allow engines to start. */
usleep(__spin_wait(gem_fd, spin) * num_engines / 1e3);
pmu_read_multi(fd[0], num_engines, tval[0]);
slept = measured_usleep(batch_duration_ns / 1000);
if (flags & TEST_TRAILING_IDLE)
end_spin(gem_fd, spin, flags);
pmu_read_multi(fd[0], num_engines, tval[1]);
end_spin(gem_fd, spin, FLAG_SYNC);
igt_spin_batch_free(gem_fd, spin);
close(fd[0]);
for (i = 0; i < num_engines; i++)
val[i] = tval[1][i] - tval[0][i];
log_busy(num_engines, val);
for (i = 0; i < num_engines; i++)
assert_within_epsilon(val[i], slept, tolerance);
gem_quiescent_gpu(gem_fd);
}
static void
no_sema(int gem_fd, const struct intel_execution_engine2 *e, unsigned int flags)
{
igt_spin_t *spin;
uint64_t val[2][2];
int fd;
fd = open_group(I915_PMU_ENGINE_SEMA(e->class, e->instance), -1);
open_group(I915_PMU_ENGINE_WAIT(e->class, e->instance), fd);
if (flags & TEST_BUSY)
spin = spin_sync(gem_fd, 0, e2ring(gem_fd, e));
else
spin = NULL;
pmu_read_multi(fd, 2, val[0]);
measured_usleep(batch_duration_ns / 1000);
if (flags & TEST_TRAILING_IDLE)
end_spin(gem_fd, spin, flags);
pmu_read_multi(fd, 2, val[1]);
val[0][0] = val[1][0] - val[0][0];
val[0][1] = val[1][1] - val[0][1];
if (spin) {
end_spin(gem_fd, spin, FLAG_SYNC);
igt_spin_batch_free(gem_fd, spin);
}
close(fd);
assert_within_epsilon(val[0][0], 0.0f, tolerance);
assert_within_epsilon(val[0][1], 0.0f, tolerance);
}
#define MI_INSTR(opcode, flags) (((opcode) << 23) | (flags))
#define MI_SEMAPHORE_WAIT MI_INSTR(0x1c, 2) /* GEN8+ */
#define MI_SEMAPHORE_POLL (1<<15)
#define MI_SEMAPHORE_SAD_GTE_SDD (1<<12)
static void
sema_wait(int gem_fd, const struct intel_execution_engine2 *e,
unsigned int flags)
{
struct drm_i915_gem_relocation_entry reloc[2] = {};
struct drm_i915_gem_exec_object2 obj[2] = {};
struct drm_i915_gem_execbuffer2 eb = {};
uint32_t bb_handle, obj_handle;
unsigned long slept;
uint32_t *obj_ptr;
uint32_t batch[16];
uint64_t val[2], ts[2];
int fd;
igt_require(intel_gen(intel_get_drm_devid(gem_fd)) >= 8);
/**
* Setup up a batchbuffer with a polling semaphore wait command which
* will wait on an value in a shared bo to change. This way we are able
* to control how much time we will spend in this bb.
*/
bb_handle = gem_create(gem_fd, 4096);
obj_handle = gem_create(gem_fd, 4096);
obj_ptr = gem_mmap__wc(gem_fd, obj_handle, 0, 4096, PROT_WRITE);
batch[0] = MI_STORE_DWORD_IMM;
batch[1] = sizeof(*obj_ptr);
batch[2] = 0;
batch[3] = 1;
batch[4] = MI_SEMAPHORE_WAIT |
MI_SEMAPHORE_POLL |
MI_SEMAPHORE_SAD_GTE_SDD;
batch[5] = 1;
batch[6] = 0x0;
batch[7] = 0x0;
batch[8] = MI_BATCH_BUFFER_END;
gem_write(gem_fd, bb_handle, 0, batch, sizeof(batch));
reloc[0].target_handle = obj_handle;
reloc[0].offset = 1 * sizeof(uint32_t);
reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
reloc[0].delta = sizeof(*obj_ptr);
reloc[1].target_handle = obj_handle;
reloc[1].offset = 6 * sizeof(uint32_t);
reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
obj[0].handle = obj_handle;
obj[1].handle = bb_handle;
obj[1].relocation_count = 2;
obj[1].relocs_ptr = to_user_pointer(reloc);
eb.buffer_count = 2;
eb.buffers_ptr = to_user_pointer(obj);
eb.flags = e2ring(gem_fd, e);
/**
* Start the semaphore wait PMU and after some known time let the above
* semaphore wait command finish. Then check that the PMU is reporting
* to expected time spent in semaphore wait state.
*/
fd = open_pmu(I915_PMU_ENGINE_SEMA(e->class, e->instance));
val[0] = pmu_read_single(fd);
gem_execbuf(gem_fd, &eb);
do { /* wait for the batch to start executing */
usleep(5e3);
} while (!obj_ptr[1]);
igt_assert_f(igt_wait(pmu_read_single(fd) != val[0], 10, 1),
"sampling failed to start withing 10ms");
val[0] = __pmu_read_single(fd, &ts[0]);
slept = measured_usleep(batch_duration_ns / 1000);
if (flags & TEST_TRAILING_IDLE)
obj_ptr[0] = 1;
val[1] = __pmu_read_single(fd, &ts[1]);
igt_debug("slept %.3fms (perf %.3fms), sampled %.3fms\n",
slept * 1e-6,
(ts[1] - ts[0]) * 1e-6,
(val[1] - val[0]) * 1e-6);
obj_ptr[0] = 1;
gem_sync(gem_fd, bb_handle);
munmap(obj_ptr, 4096);
gem_close(gem_fd, obj_handle);
gem_close(gem_fd, bb_handle);
close(fd);
assert_within_epsilon(val[1] - val[0], slept, tolerance);
}
#define MI_WAIT_FOR_PIPE_C_VBLANK (1<<21)
#define MI_WAIT_FOR_PIPE_B_VBLANK (1<<11)
#define MI_WAIT_FOR_PIPE_A_VBLANK (1<<3)
typedef struct {
igt_display_t display;
struct igt_fb primary_fb;
igt_output_t *output;
enum pipe pipe;
} data_t;
static void prepare_crtc(data_t *data, int fd, igt_output_t *output)
{
drmModeModeInfo *mode;
igt_display_t *display = &data->display;
igt_plane_t *primary;
/* select the pipe we want to use */
igt_output_set_pipe(output, data->pipe);
/* create and set the primary plane fb */
mode = igt_output_get_mode(output);
igt_create_color_fb(fd, mode->hdisplay, mode->vdisplay,
DRM_FORMAT_XRGB8888,
LOCAL_DRM_FORMAT_MOD_NONE,
0.0, 0.0, 0.0,
&data->primary_fb);
primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
igt_plane_set_fb(primary, &data->primary_fb);
igt_display_commit(display);
igt_wait_for_vblank(fd, data->pipe);
}
static void cleanup_crtc(data_t *data, int fd, igt_output_t *output)
{
igt_display_t *display = &data->display;
igt_plane_t *primary;
igt_remove_fb(fd, &data->primary_fb);
primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
igt_plane_set_fb(primary, NULL);
igt_output_set_pipe(output, PIPE_ANY);
igt_display_commit(display);
}
static int wait_vblank(int fd, union drm_wait_vblank *vbl)
{
int err;
err = 0;
if (igt_ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl))
err = -errno;
return err;
}
static void
event_wait(int gem_fd, const struct intel_execution_engine2 *e)
{
struct drm_i915_gem_exec_object2 obj = { };
struct drm_i915_gem_execbuffer2 eb = { };
const uint32_t DERRMR = 0x44050;
const uint32_t FORCEWAKE_MT = 0xa188;
unsigned int valid_tests = 0;
uint32_t batch[16], *b;
uint16_t devid;
igt_output_t *output;
data_t data;
enum pipe p;
int fd;
devid = intel_get_drm_devid(gem_fd);
igt_require(intel_gen(devid) >= 7);
igt_skip_on(IS_VALLEYVIEW(devid) || IS_CHERRYVIEW(devid));
kmstest_set_vt_graphics_mode();
igt_display_init(&data.display, gem_fd);
/**
* We will use the display to render event forwarind so need to
* program the DERRMR register and restore it at exit.
* Note we assume that the default/desired value for DERRMR will always
* be ~0u (all routing disable). To be fancy, we could do a SRM of the
* reg beforehand and then LRM at the end.
*
* We will emit a MI_WAIT_FOR_EVENT listening for vblank events,
* have a background helper to indirectly enable vblank irqs, and
* listen to the recorded time spent in engine wait state as reported
* by the PMU.
*/
obj.handle = gem_create(gem_fd, 4096);
b = batch;
*b++ = MI_LOAD_REGISTER_IMM;
*b++ = FORCEWAKE_MT;
*b++ = 2 << 16 | 2;
*b++ = MI_LOAD_REGISTER_IMM;
*b++ = DERRMR;
*b++ = ~0u;
*b++ = MI_WAIT_FOR_EVENT;
*b++ = MI_LOAD_REGISTER_IMM;
*b++ = DERRMR;
*b++ = ~0u;
*b++ = MI_LOAD_REGISTER_IMM;
*b++ = FORCEWAKE_MT;
*b++ = 2 << 16;
*b++ = MI_BATCH_BUFFER_END;
eb.buffer_count = 1;
eb.buffers_ptr = to_user_pointer(&obj);
eb.flags = e2ring(gem_fd, e) | I915_EXEC_SECURE;
for_each_pipe_with_valid_output(&data.display, p, output) {
struct igt_helper_process waiter = { };
const unsigned int frames = 3;
uint64_t val[2];
batch[6] = MI_WAIT_FOR_EVENT;
switch (p) {
case PIPE_A:
batch[6] |= MI_WAIT_FOR_PIPE_A_VBLANK;
batch[5] = ~(1 << 3);
break;
case PIPE_B:
batch[6] |= MI_WAIT_FOR_PIPE_B_VBLANK;
batch[5] = ~(1 << 11);
break;
case PIPE_C:
batch[6] |= MI_WAIT_FOR_PIPE_C_VBLANK;
batch[5] = ~(1 << 21);
break;
default:
continue;
}
gem_write(gem_fd, obj.handle, 0, batch, sizeof(batch));
data.pipe = p;
prepare_crtc(&data, gem_fd, output);
fd = open_pmu(I915_PMU_ENGINE_WAIT(e->class, e->instance));
val[0] = pmu_read_single(fd);
igt_fork_helper(&waiter) {
const uint32_t pipe_id_flag =
kmstest_get_vbl_flag(data.pipe);
for (;;) {
union drm_wait_vblank vbl = { };
vbl.request.type = DRM_VBLANK_RELATIVE;
vbl.request.type |= pipe_id_flag;
vbl.request.sequence = 1;
igt_assert_eq(wait_vblank(gem_fd, &vbl), 0);
}
}
for (unsigned int frame = 0; frame < frames; frame++) {
gem_execbuf(gem_fd, &eb);
gem_sync(gem_fd, obj.handle);
}
igt_stop_helper(&waiter);
val[1] = pmu_read_single(fd);
close(fd);
cleanup_crtc(&data, gem_fd, output);
valid_tests++;
igt_assert(val[1] - val[0] > 0);
}
gem_close(gem_fd, obj.handle);
igt_require_f(valid_tests,
"no valid crtc/connector combinations found\n");
}
static void
multi_client(int gem_fd, const struct intel_execution_engine2 *e)
{
uint64_t config = I915_PMU_ENGINE_BUSY(e->class, e->instance);
unsigned long slept[2];
uint64_t val[2], ts[2], perf_slept[2];
igt_spin_t *spin;
int fd[2];
gem_quiescent_gpu(gem_fd);
fd[0] = open_pmu(config);
/*
* Second PMU client which is initialized after the first one,
* and exists before it, should not affect accounting as reported
* in the first client.
*/
fd[1] = open_pmu(config);
spin = spin_sync(gem_fd, 0, e2ring(gem_fd, e));
val[0] = val[1] = __pmu_read_single(fd[0], &ts[0]);
slept[1] = measured_usleep(batch_duration_ns / 1000);
val[1] = __pmu_read_single(fd[1], &ts[1]) - val[1];
perf_slept[1] = ts[1] - ts[0];
igt_debug("slept=%lu perf=%"PRIu64"\n", slept[1], perf_slept[1]);
close(fd[1]);
slept[0] = measured_usleep(batch_duration_ns / 1000) + slept[1];
val[0] = __pmu_read_single(fd[0], &ts[1]) - val[0];
perf_slept[0] = ts[1] - ts[0];
igt_debug("slept=%lu perf=%"PRIu64"\n", slept[0], perf_slept[0]);
igt_spin_batch_end(spin);
gem_sync(gem_fd, spin->handle);
igt_spin_batch_free(gem_fd, spin);
close(fd[0]);
assert_within_epsilon(val[0], perf_slept[0], tolerance);
assert_within_epsilon(val[1], perf_slept[1], tolerance);
}
/**
* Tests that i915 PMU corectly errors out in invalid initialization.
* i915 PMU is uncore PMU, thus:
* - sampling period is not supported
* - pid > 0 is not supported since we can't count per-process (we count
* per whole system)
* - cpu != 0 is not supported since i915 PMU only allows running on one cpu
* and that is normally CPU0.
*/
static void invalid_init(void)
{
struct perf_event_attr attr;
#define ATTR_INIT() \
do { \
memset(&attr, 0, sizeof (attr)); \
attr.config = I915_PMU_ENGINE_BUSY(I915_ENGINE_CLASS_RENDER, 0); \
attr.type = i915_type_id(); \
igt_assert(attr.type != 0); \
errno = 0; \
} while(0)
ATTR_INIT();
attr.sample_period = 100;
igt_assert_eq(perf_event_open(&attr, -1, 0, -1, 0), -1);
igt_assert_eq(errno, EINVAL);
ATTR_INIT();
igt_assert_eq(perf_event_open(&attr, 0, 0, -1, 0), -1);
igt_assert_eq(errno, EINVAL);
ATTR_INIT();
igt_assert_eq(perf_event_open(&attr, -1, 1, -1, 0), -1);
igt_assert_eq(errno, EINVAL);
}
static void init_other(unsigned int i, bool valid)
{
int fd;
fd = perf_i915_open(__I915_PMU_OTHER(i));
igt_require(!(fd < 0 && errno == ENODEV));
if (valid) {
igt_assert(fd >= 0);
} else {
igt_assert(fd < 0);
return;
}
close(fd);
}
static void read_other(unsigned int i, bool valid)
{
int fd;
fd = perf_i915_open(__I915_PMU_OTHER(i));
igt_require(!(fd < 0 && errno == ENODEV));
if (valid) {
igt_assert(fd >= 0);
} else {
igt_assert(fd < 0);
return;
}
(void)pmu_read_single(fd);
close(fd);
}
static bool cpu0_hotplug_support(void)
{
return access("/sys/devices/system/cpu/cpu0/online", W_OK) == 0;
}
static void cpu_hotplug(int gem_fd)
{
igt_spin_t *spin[2];
uint64_t ts[2];
uint64_t val;
int link[2];
int fd, ret;
int cur = 0;
char buf;
igt_require(cpu0_hotplug_support());
fd = open_pmu(I915_PMU_ENGINE_BUSY(I915_ENGINE_CLASS_RENDER, 0));
/*
* Create two spinners so test can ensure shorter gaps in engine
* busyness as it is terminating one and re-starting the other.
*/
spin[0] = igt_spin_batch_new(gem_fd, 0, I915_EXEC_RENDER, 0);
spin[1] = __igt_spin_batch_new(gem_fd, 0, I915_EXEC_RENDER, 0);
val = __pmu_read_single(fd, &ts[0]);
ret = pipe2(link, O_NONBLOCK);
igt_assert_eq(ret, 0);
/*
* Toggle online status of all the CPUs in a child process and ensure
* this has not affected busyness stats in the parent.
*/
igt_fork(child, 1) {
int cpu = 0;
close(link[0]);
for (;;) {
char name[128];
int cpufd;
igt_assert_lt(snprintf(name, sizeof(name),
"/sys/devices/system/cpu/cpu%d/online",
cpu), sizeof(name));
cpufd = open(name, O_WRONLY);
if (cpufd == -1) {
igt_assert(cpu > 0);
/*
* Signal parent that we cycled through all
* CPUs and we are done.
*/
igt_assert_eq(write(link[1], "*", 1), 1);
break;
}
/* Offline followed by online a CPU. */
ret = write(cpufd, "0", 2);
if (ret < 0) {
/*
* If we failed to offline a CPU we don't want
* to proceed.
*/
igt_warn("Failed to offline cpu%u! (%d)\n",
cpu, errno);
igt_assert_eq(write(link[1], "s", 1), 1);
break;
}
usleep(1e6);
ret = write(cpufd, "1", 2);
if (ret < 0) {
/*
* Failed to bring a CPU back online is fatal
* for the sanity of a test run so stop further
* testing.
*/
igt_warn("Failed to online cpu%u! (%d)\n",
cpu, errno);
igt_fatal_error();
}
close(cpufd);
cpu++;
}
}
close(link[1]);
/*
* Very long batches can be declared as GPU hangs so emit shorter ones
* until the CPU core shuffler finishes one loop.
*/
for (;;) {
usleep(500e3);
end_spin(gem_fd, spin[cur], 0);
/* Check if the child is signaling completion. */
ret = read(link[0], &buf, 1);
if ( ret == 1 || (ret < 0 && errno != EAGAIN))
break;
igt_spin_batch_free(gem_fd, spin[cur]);
spin[cur] = __igt_spin_batch_new(gem_fd, 0, I915_EXEC_RENDER,
0);
cur ^= 1;
}
val = __pmu_read_single(fd, &ts[1]) - val;
end_spin(gem_fd, spin[0], FLAG_SYNC);
end_spin(gem_fd, spin[1], FLAG_SYNC);
igt_spin_batch_free(gem_fd, spin[0]);
igt_spin_batch_free(gem_fd, spin[1]);
igt_waitchildren();
close(fd);
close(link[0]);
/* Skip if child signals a problem with offlining a CPU. */
igt_skip_on(buf == 's');
assert_within_epsilon(val, ts[1] - ts[0], tolerance);
}
static void
test_interrupts(int gem_fd)
{
const unsigned int test_duration_ms = 1000;
const int target = 30;
igt_spin_t *spin[target];
struct pollfd pfd;
uint64_t idle, busy;
int fence_fd;
int fd;
gem_quiescent_gpu(gem_fd);
fd = open_pmu(I915_PMU_INTERRUPTS);
/* Queue spinning batches. */
for (int i = 0; i < target; i++) {
spin[i] = __igt_spin_batch_new_fence(gem_fd,
0, I915_EXEC_RENDER);
if (i == 0) {
fence_fd = spin[i]->out_fence;
} else {
int old_fd = fence_fd;
fence_fd = sync_fence_merge(old_fd,
spin[i]->out_fence);
close(old_fd);
}
igt_assert(fence_fd >= 0);
}
/* Wait for idle state. */
idle = pmu_read_single(fd);
do {
busy = idle;
usleep(1e3);
idle = pmu_read_single(fd);
} while (idle != busy);
/* Arm batch expiration. */
for (int i = 0; i < target; i++)
igt_spin_batch_set_timeout(spin[i],
(i + 1) * test_duration_ms * 1e6
/ target);
/* Wait for last batch to finish. */
pfd.events = POLLIN;
pfd.fd = fence_fd;
igt_assert_eq(poll(&pfd, 1, 2 * test_duration_ms), 1);
close(fence_fd);
/* Free batches. */
for (int i = 0; i < target; i++)
igt_spin_batch_free(gem_fd, spin[i]);
/* Check at least as many interrupts has been generated. */
busy = pmu_read_single(fd) - idle;
close(fd);
igt_assert_lte(target, busy);
}
static void
test_interrupts_sync(int gem_fd)
{
const unsigned int test_duration_ms = 1000;
const int target = 30;
igt_spin_t *spin[target];
struct pollfd pfd;
uint64_t idle, busy;
int fd;
gem_quiescent_gpu(gem_fd);
fd = open_pmu(I915_PMU_INTERRUPTS);
/* Queue spinning batches. */
for (int i = 0; i < target; i++)
spin[i] = __igt_spin_batch_new_fence(gem_fd, 0, 0);
/* Wait for idle state. */
idle = pmu_read_single(fd);
do {
busy = idle;
usleep(1e3);
idle = pmu_read_single(fd);
} while (idle != busy);
/* Process the batch queue. */
pfd.events = POLLIN;
for (int i = 0; i < target; i++) {
const unsigned int timeout_ms = test_duration_ms / target;
pfd.fd = spin[i]->out_fence;
igt_spin_batch_set_timeout(spin[i], timeout_ms * 1e6);
igt_assert_eq(poll(&pfd, 1, 2 * timeout_ms), 1);
igt_spin_batch_free(gem_fd, spin[i]);
}
/* Check at least as many interrupts has been generated. */
busy = pmu_read_single(fd) - idle;
close(fd);
igt_assert_lte(target, busy);
}
static void
test_frequency(int gem_fd)
{
uint32_t min_freq, max_freq, boost_freq;
uint64_t val[2], start[2], slept;
double min[2], max[2];
igt_spin_t *spin;
int fd, sysfs;
sysfs = igt_sysfs_open(gem_fd, NULL);
igt_require(sysfs >= 0);
min_freq = igt_sysfs_get_u32(sysfs, "gt_RPn_freq_mhz");
max_freq = igt_sysfs_get_u32(sysfs, "gt_RP0_freq_mhz");
boost_freq = igt_sysfs_get_u32(sysfs, "gt_boost_freq_mhz");
igt_info("Frequency: min=%u, max=%u, boost=%u MHz\n",
min_freq, max_freq, boost_freq);
igt_require(min_freq > 0 && max_freq > 0 && boost_freq > 0);
igt_require(max_freq > min_freq);
igt_require(boost_freq > min_freq);
fd = open_group(I915_PMU_REQUESTED_FREQUENCY, -1);
open_group(I915_PMU_ACTUAL_FREQUENCY, fd);
/*
* Set GPU to min frequency and read PMU counters.
*/
igt_require(igt_sysfs_set_u32(sysfs, "gt_min_freq_mhz", min_freq));
igt_require(igt_sysfs_get_u32(sysfs, "gt_min_freq_mhz") == min_freq);
igt_require(igt_sysfs_set_u32(sysfs, "gt_max_freq_mhz", min_freq));
igt_require(igt_sysfs_get_u32(sysfs, "gt_max_freq_mhz") == min_freq);
igt_require(igt_sysfs_set_u32(sysfs, "gt_boost_freq_mhz", min_freq));
igt_require(igt_sysfs_get_u32(sysfs, "gt_boost_freq_mhz") == min_freq);
gem_quiescent_gpu(gem_fd); /* Idle to be sure the change takes effect */
spin = spin_sync(gem_fd, 0, I915_EXEC_RENDER);
slept = pmu_read_multi(fd, 2, start);
measured_usleep(batch_duration_ns / 1000);
slept = pmu_read_multi(fd, 2, val) - slept;
min[0] = 1e9*(val[0] - start[0]) / slept;
min[1] = 1e9*(val[1] - start[1]) / slept;
igt_spin_batch_free(gem_fd, spin);
gem_quiescent_gpu(gem_fd); /* Don't leak busy bo into the next phase */
usleep(1e6);
/*
* Set GPU to max frequency and read PMU counters.
*/
igt_require(igt_sysfs_set_u32(sysfs, "gt_max_freq_mhz", max_freq));
igt_require(igt_sysfs_get_u32(sysfs, "gt_max_freq_mhz") == max_freq);
igt_require(igt_sysfs_set_u32(sysfs, "gt_boost_freq_mhz", boost_freq));
igt_require(igt_sysfs_get_u32(sysfs, "gt_boost_freq_mhz") == boost_freq);
igt_require(igt_sysfs_set_u32(sysfs, "gt_min_freq_mhz", max_freq));
igt_require(igt_sysfs_get_u32(sysfs, "gt_min_freq_mhz") == max_freq);
gem_quiescent_gpu(gem_fd);
spin = spin_sync(gem_fd, 0, I915_EXEC_RENDER);
slept = pmu_read_multi(fd, 2, start);
measured_usleep(batch_duration_ns / 1000);
slept = pmu_read_multi(fd, 2, val) - slept;
max[0] = 1e9*(val[0] - start[0]) / slept;
max[1] = 1e9*(val[1] - start[1]) / slept;
igt_spin_batch_free(gem_fd, spin);
gem_quiescent_gpu(gem_fd);
/*
* Restore min/max.
*/
igt_sysfs_set_u32(sysfs, "gt_min_freq_mhz", min_freq);
if (igt_sysfs_get_u32(sysfs, "gt_min_freq_mhz") != min_freq)
igt_warn("Unable to restore min frequency to saved value [%u MHz], now %u MHz\n",
min_freq, igt_sysfs_get_u32(sysfs, "gt_min_freq_mhz"));
close(fd);
igt_info("Min frequency: requested %.1f, actual %.1f\n",
min[0], min[1]);
igt_info("Max frequency: requested %.1f, actual %.1f\n",
max[0], max[1]);
assert_within_epsilon(min[0], min_freq, tolerance);
/*
* On thermally throttled devices we cannot be sure maximum frequency
* can be reached so use larger tolerance downards.
*/
__assert_within_epsilon(max[0], max_freq, tolerance, 0.15f);
}
static bool wait_for_rc6(int fd)
{
struct timespec tv = {};
uint64_t start, now;
/* First wait for roughly an RC6 Evaluation Interval */
usleep(160 * 1000);
/* Then poll for RC6 to start ticking */
now = pmu_read_single(fd);
do {
start = now;
usleep(5000);
now = pmu_read_single(fd);
if (now - start > 1e6)
return true;
} while (!igt_seconds_elapsed(&tv));
return false;
}
static void
test_rc6(int gem_fd, unsigned int flags)
{
int64_t duration_ns = 2e9;
uint64_t idle, busy, prev, ts[2];
unsigned long slept;
int fd, fw;
gem_quiescent_gpu(gem_fd);
fd = open_pmu(I915_PMU_RC6_RESIDENCY);
if (flags & TEST_RUNTIME_PM) {
drmModeRes *res;
res = drmModeGetResources(gem_fd);
igt_assert(res);
/* force all connectors off */
kmstest_set_vt_graphics_mode();
kmstest_unset_all_crtcs(gem_fd, res);
drmModeFreeResources(res);
igt_require(igt_setup_runtime_pm());
igt_require(igt_wait_for_pm_status(IGT_RUNTIME_PM_STATUS_SUSPENDED));
/*
* Sleep for a bit to see if once woken up estimated RC6 hasn't
* drifted to far in advance of real RC6.
*/
if (flags & FLAG_LONG) {
pmu_read_single(fd);
sleep(5);
pmu_read_single(fd);
}
}
igt_require(wait_for_rc6(fd));
/* While idle check full RC6. */
prev = __pmu_read_single(fd, &ts[0]);
slept = measured_usleep(duration_ns / 1000);
idle = __pmu_read_single(fd, &ts[1]);
igt_debug("slept=%lu perf=%"PRIu64"\n", slept, ts[1] - ts[0]);
assert_within_epsilon(idle - prev, ts[1] - ts[0], tolerance);
/* Wake up device and check no RC6. */
fw = igt_open_forcewake_handle(gem_fd);
igt_assert(fw >= 0);
usleep(1e3); /* wait for the rc6 cycle counter to stop ticking */
prev = pmu_read_single(fd);
usleep(duration_ns / 1000);
busy = pmu_read_single(fd);
close(fw);
close(fd);
assert_within_epsilon(busy - prev, 0.0, tolerance);
}
static void
test_enable_race(int gem_fd, const struct intel_execution_engine2 *e)
{
uint64_t config = I915_PMU_ENGINE_BUSY(e->class, e->instance);
struct igt_helper_process engine_load = { };
const uint32_t bbend = MI_BATCH_BUFFER_END;
struct drm_i915_gem_exec_object2 obj = { };
struct drm_i915_gem_execbuffer2 eb = { };
int fd;
igt_require(gem_has_execlists(gem_fd));
igt_require(gem_has_engine(gem_fd, e->class, e->instance));
obj.handle = gem_create(gem_fd, 4096);
gem_write(gem_fd, obj.handle, 0, &bbend, sizeof(bbend));
eb.buffer_count = 1;
eb.buffers_ptr = to_user_pointer(&obj);
eb.flags = e2ring(gem_fd, e);
/*
* This test is probabilistic so run in a few times to increase the
* chance of hitting the race.
*/
igt_until_timeout(10) {
/*
* Defeat the busy stats delayed disable, we need to guarantee
* we are the first PMU user.
*/
gem_quiescent_gpu(gem_fd);
sleep(2);
/* Apply interrupt-heavy load on the engine. */
igt_fork_helper(&engine_load) {
for (;;)
gem_execbuf(gem_fd, &eb);
}
/* Wait a bit to allow engine load to start. */
usleep(500e3);
/* Enable the PMU. */
fd = open_pmu(config);
/* Stop load and close the PMU. */
igt_stop_helper(&engine_load);
close(fd);
}
/* Cleanup. */
gem_close(gem_fd, obj.handle);
gem_quiescent_gpu(gem_fd);
}
static void __rearm_spin_batch(igt_spin_t *spin)
{
const uint32_t mi_arb_chk = 0x5 << 23;
*spin->batch = mi_arb_chk;
__sync_synchronize();
}
#define __assert_within(x, ref, tol_up, tol_down) \
igt_assert_f((double)(x) <= ((double)(ref) + (tol_up)) && \
(double)(x) >= ((double)(ref) - (tol_down)), \
"%f not within +%f/-%f of %f! ('%s' vs '%s')\n", \
(double)(x), (double)(tol_up), (double)(tol_down), \
(double)(ref), #x, #ref)
#define assert_within(x, ref, tolerance) \
__assert_within(x, ref, tolerance, tolerance)
static void
accuracy(int gem_fd, const struct intel_execution_engine2 *e,
unsigned long target_busy_pct)
{
unsigned long busy_us = 10000 - 100 * (1 + abs(50 - target_busy_pct));
unsigned long idle_us = 100 * (busy_us - target_busy_pct *
busy_us / 100) / target_busy_pct;
const unsigned long min_test_us = 1e6;
const unsigned long pwm_calibration_us = min_test_us;
const unsigned long test_us = min_test_us;
double busy_r, expected;
uint64_t val[2];
uint64_t ts[2];
int link[2];
int fd;
/* Sampling platforms cannot reach the high accuracy criteria. */
igt_require(gem_has_execlists(gem_fd));
while (idle_us < 2500) {
busy_us *= 2;
idle_us *= 2;
}
igt_info("calibration=%lums, test=%lums; ratio=%.2f%% (%luus/%luus)\n",
pwm_calibration_us / 1000, test_us / 1000,
(double)busy_us / (busy_us + idle_us) * 100.0,
busy_us, idle_us);
assert_within_epsilon((double)busy_us / (busy_us + idle_us),
(double)target_busy_pct / 100.0, tolerance);
igt_assert(pipe(link) == 0);
/* Emit PWM pattern on the engine from a child. */
igt_fork(child, 1) {
const unsigned long timeout[] = {
pwm_calibration_us * 1000, test_us * 1000
};
uint64_t total_busy_ns = 0, total_ns = 0;
igt_spin_t *spin;
/* Allocate our spin batch and idle it. */
spin = igt_spin_batch_new(gem_fd, 0, e2ring(gem_fd, e), 0);
igt_spin_batch_end(spin);
gem_sync(gem_fd, spin->handle);
/* 1st pass is calibration, second pass is the test. */
for (int pass = 0; pass < ARRAY_SIZE(timeout); pass++) {
unsigned int target_idle_us = idle_us;
uint64_t busy_ns = 0, idle_ns = 0;
struct timespec start = { };
unsigned long pass_ns = 0;
double avg = 0.0, var = 0.0;
unsigned int n = 0;
igt_nsec_elapsed(&start);
do {
unsigned long loop_ns, loop_busy;
struct timespec _ts = { };
double err, tmp;
/* PWM idle sleep. */
_ts.tv_nsec = target_idle_us * 1000;
nanosleep(&_ts, NULL);
/* Restart the spinbatch. */
__rearm_spin_batch(spin);
__submit_spin_batch(gem_fd, spin, e, 0);
/* PWM busy sleep. */
loop_busy = igt_nsec_elapsed(&start);
_ts.tv_nsec = busy_us * 1000;
nanosleep(&_ts, NULL);
igt_spin_batch_end(spin);
/* Time accounting. */
loop_ns = igt_nsec_elapsed(&start);
loop_busy = loop_ns - loop_busy;
loop_ns -= pass_ns;
busy_ns += loop_busy;
total_busy_ns += loop_busy;
idle_ns += loop_ns - loop_busy;
pass_ns += loop_ns;
total_ns += loop_ns;
/* Re-calibrate. */
err = (double)total_busy_ns / total_ns -
(double)target_busy_pct / 100.0;
target_idle_us = (double)target_idle_us *
(1.0 + err);
/* Running average and variance for debug. */
err = 100.0 * total_busy_ns / total_ns;
tmp = avg;
avg += (err - avg) / ++n;
var += (err - avg) * (err - tmp);
} while (pass_ns < timeout[pass]);
expected = (double)busy_ns / pass_ns;
igt_info("%u: busy %"PRIu64"us, idle %"PRIu64"us -> %.2f%% (target: %lu%%; average=%.2f, variance=%f)\n",
pass, busy_ns / 1000, idle_ns / 1000,
100 * expected, target_busy_pct, avg, var / n);
write(link[1], &expected, sizeof(expected));
}
igt_spin_batch_free(gem_fd, spin);
}
fd = open_pmu(I915_PMU_ENGINE_BUSY(e->class, e->instance));
/* Let the child run. */
read(link[0], &expected, sizeof(expected));
assert_within(100.0 * expected, target_busy_pct, 5);
/* Collect engine busyness for an interesting part of child runtime. */
val[0] = __pmu_read_single(fd, &ts[0]);
read(link[0], &expected, sizeof(expected));
val[1] = __pmu_read_single(fd, &ts[1]);
close(fd);
close(link[1]);
close(link[0]);
igt_waitchildren();
busy_r = (double)(val[1] - val[0]) / (ts[1] - ts[0]);
igt_info("error=%.2f%% (%.2f%% vs %.2f%%)\n",
(busy_r - expected) * 100, 100 * busy_r, 100 * expected);
assert_within(100.0 * busy_r, 100.0 * expected, 2);
}
igt_main
{
const unsigned int num_other_metrics =
I915_PMU_LAST - __I915_PMU_OTHER(0) + 1;
unsigned int num_engines = 0;
int fd = -1;
const struct intel_execution_engine2 *e;
unsigned int i;
igt_fixture {
fd = drm_open_driver_master(DRIVER_INTEL);
igt_require_gem(fd);
igt_require(i915_type_id() > 0);
for_each_engine_class_instance(fd, e)
num_engines++;
}
/**
* Test invalid access via perf API is rejected.
*/
igt_subtest("invalid-init")
invalid_init();
__for_each_engine_class_instance(fd, e) {
const unsigned int pct[] = { 2, 50, 98 };
/**
* Test that a single engine metric can be initialized or it
* is correctly rejected.
*/
igt_subtest_f("init-busy-%s", e->name)
init(fd, e, I915_SAMPLE_BUSY);
igt_subtest_f("init-wait-%s", e->name)
init(fd, e, I915_SAMPLE_WAIT);
igt_subtest_f("init-sema-%s", e->name)
init(fd, e, I915_SAMPLE_SEMA);
igt_subtest_group {
igt_fixture {
gem_require_engine(fd, e->class, e->instance);
}
/**
* Test that engines show no load when idle.
*/
igt_subtest_f("idle-%s", e->name)
single(fd, e, 0);
/**
* Test that a single engine reports load correctly.
*/
igt_subtest_f("busy-%s", e->name)
single(fd, e, TEST_BUSY);
igt_subtest_f("busy-idle-%s", e->name)
single(fd, e, TEST_BUSY | TEST_TRAILING_IDLE);
/**
* Test that when one engine is loaded other report no
* load.
*/
igt_subtest_f("busy-check-all-%s", e->name)
busy_check_all(fd, e, num_engines, TEST_BUSY);
igt_subtest_f("busy-idle-check-all-%s", e->name)
busy_check_all(fd, e, num_engines,
TEST_BUSY | TEST_TRAILING_IDLE);
/**
* Test that when all except one engine are loaded all
* loads are correctly reported.
*/
igt_subtest_f("most-busy-check-all-%s", e->name)
most_busy_check_all(fd, e, num_engines,
TEST_BUSY);
igt_subtest_f("most-busy-idle-check-all-%s", e->name)
most_busy_check_all(fd, e, num_engines,
TEST_BUSY |
TEST_TRAILING_IDLE);
/**
* Test that semphore counters report no activity on
* idle or busy engines.
*/
igt_subtest_f("idle-no-semaphores-%s", e->name)
no_sema(fd, e, 0);
igt_subtest_f("busy-no-semaphores-%s", e->name)
no_sema(fd, e, TEST_BUSY);
igt_subtest_f("busy-idle-no-semaphores-%s", e->name)
no_sema(fd, e, TEST_BUSY | TEST_TRAILING_IDLE);
/**
* Test that semaphore waits are correctly reported.
*/
igt_subtest_f("semaphore-wait-%s", e->name)
sema_wait(fd, e, TEST_BUSY);
igt_subtest_f("semaphore-wait-idle-%s", e->name)
sema_wait(fd, e,
TEST_BUSY | TEST_TRAILING_IDLE);
/**
* Check that two perf clients do not influence each
* others observations.
*/
igt_subtest_f("multi-client-%s", e->name)
multi_client(fd, e);
/**
* Check that reported usage is correct when PMU is
* enabled after the batch is running.
*/
igt_subtest_f("busy-start-%s", e->name)
busy_start(fd, e);
/**
* Check that reported usage is correct when PMU is
* enabled after two batches are running.
*/
igt_subtest_f("busy-double-start-%s", e->name) {
gem_require_contexts(fd);
busy_double_start(fd, e);
}
/**
* Check that the PMU can be safely enabled in face of
* interrupt-heavy engine load.
*/
igt_subtest_f("enable-race-%s", e->name)
test_enable_race(fd, e);
/**
* Check engine busyness accuracy is as expected.
*/
for (i = 0; i < ARRAY_SIZE(pct); i++) {
igt_subtest_f("busy-accuracy-%u-%s",
pct[i], e->name)
accuracy(fd, e, pct[i]);
}
igt_subtest_f("busy-hang-%s", e->name)
single(fd, e, TEST_BUSY | FLAG_HANG);
}
/**
* Test that event waits are correctly reported.
*/
if (e->class == I915_ENGINE_CLASS_RENDER)
igt_subtest_f("event-wait-%s", e->name)
event_wait(fd, e);
}
/**
* Test that when all engines are loaded all loads are
* correctly reported.
*/
igt_subtest("all-busy-check-all")
all_busy_check_all(fd, num_engines, TEST_BUSY);
igt_subtest("all-busy-idle-check-all")
all_busy_check_all(fd, num_engines,
TEST_BUSY | TEST_TRAILING_IDLE);
/**
* Test that non-engine counters can be initialized and read. Apart
* from the invalid metric which should fail.
*/
for (i = 0; i < num_other_metrics + 1; i++) {
igt_subtest_f("other-init-%u", i)
init_other(i, i < num_other_metrics);
igt_subtest_f("other-read-%u", i)
read_other(i, i < num_other_metrics);
}
/**
* Test counters are not affected by CPU offline/online events.
*/
igt_subtest("cpu-hotplug")
cpu_hotplug(fd);
/**
* Test GPU frequency.
*/
igt_subtest("frequency")
test_frequency(fd);
/**
* Test interrupt count reporting.
*/
igt_subtest("interrupts")
test_interrupts(fd);
igt_subtest("interrupts-sync")
test_interrupts_sync(fd);
/**
* Test RC6 residency reporting.
*/
igt_subtest("rc6")
test_rc6(fd, 0);
igt_subtest("rc6-runtime-pm")
test_rc6(fd, TEST_RUNTIME_PM);
igt_subtest("rc6-runtime-pm-long")
test_rc6(fd, TEST_RUNTIME_PM | FLAG_LONG);
/**
* Check render nodes are counted.
*/
igt_subtest_group {
int render_fd = -1;
igt_fixture {
render_fd = drm_open_driver_render(DRIVER_INTEL);
igt_require_gem(render_fd);
gem_quiescent_gpu(fd);
}
__for_each_engine_class_instance(render_fd, e) {
igt_subtest_group {
igt_fixture {
gem_require_engine(render_fd,
e->class,
e->instance);
}
igt_subtest_f("render-node-busy-%s", e->name)
single(render_fd, e, TEST_BUSY);
igt_subtest_f("render-node-busy-idle-%s",
e->name)
single(render_fd, e,
TEST_BUSY | TEST_TRAILING_IDLE);
}
}
igt_fixture {
close(render_fd);
}
}
}
|