1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
*/
#include <linux/bitfield.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/qcom_scm.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include "arm-smmu.h"
#include "arm-smmu-debug.h"
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#define ARM_SMMU_IMPL_DEF1 6
#define IMPL_DEF1_MICRO_MMU_CTRL 0
#define MICRO_MMU_CTRL_LOCAL_HALT_REQ BIT(2)
#define MICRO_MMU_CTRL_IDLE BIT(3)
/* Definitions for implementation-defined registers */
#define ACTLR_QCOM_OSH BIT(28)
#define ACTLR_QCOM_ISH BIT(29)
#define ACTLR_QCOM_NSH BIT(30)
struct arm_smmu_impl_def_reg {
u32 offset;
u32 value;
};
struct qsmmuv2_archdata {
spinlock_t atos_lock;
struct arm_smmu_impl_def_reg *impl_def_attach_registers;
unsigned int num_impl_def_attach_registers;
struct arm_smmu_device smmu;
};
#define to_qsmmuv2_archdata(smmu) \
container_of(smmu, struct qsmmuv2_archdata, smmu)
static int qsmmuv2_wait_for_halt(struct arm_smmu_device *smmu)
{
void __iomem *reg = arm_smmu_page(smmu, ARM_SMMU_IMPL_DEF1);
struct device *dev = smmu->dev;
u32 tmp;
if (readl_poll_timeout_atomic(reg + IMPL_DEF1_MICRO_MMU_CTRL, tmp,
(tmp & MICRO_MMU_CTRL_IDLE), 0, 30000)) {
dev_err(dev, "Couldn't halt SMMU!\n");
return -EBUSY;
}
return 0;
}
static int __qsmmuv2_halt(struct arm_smmu_device *smmu, bool wait)
{
u32 val;
val = arm_smmu_readl(smmu, ARM_SMMU_IMPL_DEF1,
IMPL_DEF1_MICRO_MMU_CTRL);
val |= MICRO_MMU_CTRL_LOCAL_HALT_REQ;
arm_smmu_writel(smmu, ARM_SMMU_IMPL_DEF1, IMPL_DEF1_MICRO_MMU_CTRL,
val);
return wait ? qsmmuv2_wait_for_halt(smmu) : 0;
}
static int qsmmuv2_halt(struct arm_smmu_device *smmu)
{
return __qsmmuv2_halt(smmu, true);
}
static int qsmmuv2_halt_nowait(struct arm_smmu_device *smmu)
{
return __qsmmuv2_halt(smmu, false);
}
static void qsmmuv2_resume(struct arm_smmu_device *smmu)
{
u32 val;
val = arm_smmu_readl(smmu, ARM_SMMU_IMPL_DEF1,
IMPL_DEF1_MICRO_MMU_CTRL);
val &= ~MICRO_MMU_CTRL_LOCAL_HALT_REQ;
arm_smmu_writel(smmu, ARM_SMMU_IMPL_DEF1, IMPL_DEF1_MICRO_MMU_CTRL,
val);
}
static phys_addr_t __qsmmuv2_iova_to_phys_hard(
struct arm_smmu_domain *smmu_domain,
dma_addr_t iova)
{
struct arm_smmu_device *smmu = smmu_domain->smmu;
struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
struct device *dev = smmu->dev;
int idx = cfg->cbndx;
void __iomem *reg;
u32 tmp;
u64 phys;
unsigned long va;
/* ATS1 registers can only be written atomically */
va = iova & ~0xfffUL;
if (cfg->fmt == ARM_SMMU_CTX_FMT_AARCH64)
arm_smmu_cb_writeq(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
else
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ATS1PR, va);
reg = arm_smmu_page(smmu, ARM_SMMU_CB(smmu, idx));
if (readl_poll_timeout_atomic(reg + ARM_SMMU_CB_ATSR, tmp,
!(tmp & ATSR_ACTIVE), 5, 50)) {
dev_err(dev, "iova to phys timed out on %pad.\n", &iova);
phys = 0;
return phys;
}
phys = arm_smmu_cb_readq(smmu, idx, ARM_SMMU_CB_PAR);
if (phys & CB_PAR_F) {
dev_err(dev, "translation fault!\n");
dev_err(dev, "PAR = 0x%llx\n", phys);
phys = 0;
} else {
phys = (phys & (PHYS_MASK & ~0xfffULL)) | (iova & 0xfff);
}
return phys;
}
static phys_addr_t qsmmuv2_iova_to_phys_hard(
struct arm_smmu_domain *smmu_domain,
dma_addr_t iova,
unsigned long trans_flags)
{
struct arm_smmu_device *smmu = smmu_domain->smmu;
struct qsmmuv2_archdata *data = to_qsmmuv2_archdata(smmu);
int idx = smmu_domain->cfg.cbndx;
phys_addr_t phys = 0;
unsigned long flags;
u32 sctlr, sctlr_orig, fsr;
spin_lock_irqsave(&data->atos_lock, flags);
qsmmuv2_halt_nowait(smmu);
/* disable stall mode momentarily */
sctlr_orig = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_SCTLR);
sctlr = sctlr_orig & ~(SCTLR_CFCFG);
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, sctlr);
/* clear FSR to allow ATOS to log any faults */
fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
if (fsr & FSR_FAULT) {
/* Clear pending interrupts */
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
/*
* Barrier required to ensure that the FSR is cleared
* before resuming SMMU operation.
*/
wmb();
/*
* TBU halt takes care of resuming any stalled transcation.
* Kept it here for completeness sake.
*/
if (fsr & FSR_SS)
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_RESUME,
RESUME_TERMINATE);
}
qsmmuv2_wait_for_halt(smmu);
phys = __qsmmuv2_iova_to_phys_hard(smmu_domain, iova);
/* restore SCTLR */
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, sctlr_orig);
qsmmuv2_resume(smmu);
spin_unlock_irqrestore(&data->atos_lock, flags);
return phys;
}
static void qsmmuv2_tlb_sync_timeout(struct arm_smmu_device *smmu)
{
dev_err_ratelimited(smmu->dev,
"TLB sync timed out -- SMMU may be deadlocked\n");
BUG_ON(IS_ENABLED(CONFIG_IOMMU_TLBSYNC_DEBUG));
}
static int qsmmuv2_device_reset(struct arm_smmu_device *smmu)
{
struct qsmmuv2_archdata *data = to_qsmmuv2_archdata(smmu);
struct arm_smmu_impl_def_reg *regs = data->impl_def_attach_registers;
u32 i;
/* Program implementation defined registers */
qsmmuv2_halt(smmu);
for (i = 0; i < data->num_impl_def_attach_registers; ++i)
arm_smmu_gr0_write(smmu, regs[i].offset, regs[i].value);
qsmmuv2_resume(smmu);
return 0;
}
static void qsmmuv2_init_cb(struct arm_smmu_domain *smmu_domain,
struct device *dev)
{
struct arm_smmu_device *smmu = smmu_domain->smmu;
int idx = smmu_domain->cfg.cbndx;
const struct iommu_flush_ops *tlb;
u32 val;
tlb = smmu_domain->pgtbl_info[0].pgtbl_cfg.tlb;
val = ACTLR_QCOM_ISH | ACTLR_QCOM_OSH | ACTLR_QCOM_NSH;
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ACTLR, val);
/*
* Flush the context bank after modifying ACTLR to ensure there
* are no cache entries with stale state
*/
tlb->tlb_flush_all(smmu_domain);
}
static int arm_smmu_parse_impl_def_registers(struct arm_smmu_device *smmu)
{
struct device *dev = smmu->dev;
struct qsmmuv2_archdata *data = to_qsmmuv2_archdata(smmu);
int i, ntuples, ret;
u32 *tuples;
struct arm_smmu_impl_def_reg *regs, *regit;
if (!of_find_property(dev->of_node, "attach-impl-defs", &ntuples))
return 0;
ntuples /= sizeof(u32);
if (ntuples % 2) {
dev_err(dev,
"Invalid number of attach-impl-defs registers: %d\n",
ntuples);
return -EINVAL;
}
regs = devm_kzalloc(dev, sizeof(*data->impl_def_attach_registers) *
ntuples, GFP_KERNEL);
if (!regs)
return -ENOMEM;
tuples = kzalloc(sizeof(u32) * ntuples * 2, GFP_KERNEL);
if (!tuples)
return -ENOMEM;
ret = of_property_read_u32_array(dev->of_node, "attach-impl-defs",
tuples, ntuples);
if (ret) {
kfree(tuples);
return ret;
}
for (i = 0, regit = regs; i < ntuples; i += 2, ++regit) {
regit->offset = tuples[i];
regit->value = tuples[i + 1];
}
kfree(tuples);
data->impl_def_attach_registers = regs;
data->num_impl_def_attach_registers = ntuples / 2;
return 0;
}
static const struct arm_smmu_impl qsmmuv2_impl = {
.init_context_bank = qsmmuv2_init_cb,
.iova_to_phys_hard = qsmmuv2_iova_to_phys_hard,
.tlb_sync_timeout = qsmmuv2_tlb_sync_timeout,
.reset = qsmmuv2_device_reset,
};
struct qcom_smmu {
struct arm_smmu_device smmu;
};
static int qcom_sdm845_smmu500_cfg_probe(struct arm_smmu_device *smmu)
{
u32 s2cr;
u32 smr;
int i;
for (i = 0; i < smmu->num_mapping_groups; i++) {
smr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_SMR(i));
s2cr = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_S2CR(i));
smmu->smrs[i].mask = FIELD_GET(SMR_MASK, smr);
smmu->smrs[i].id = FIELD_GET(SMR_ID, smr);
if (smmu->features & ARM_SMMU_FEAT_EXIDS)
smmu->smrs[i].valid = FIELD_GET(S2CR_EXIDVALID, s2cr);
else
smmu->smrs[i].valid = FIELD_GET(SMR_VALID, smr);
smmu->s2crs[i].group = NULL;
smmu->s2crs[i].count = 0;
smmu->s2crs[i].type = FIELD_GET(S2CR_TYPE, s2cr);
smmu->s2crs[i].privcfg = FIELD_GET(S2CR_PRIVCFG, s2cr);
smmu->s2crs[i].cbndx = FIELD_GET(S2CR_CBNDX, s2cr);
if (!smmu->smrs[i].valid)
continue;
smmu->s2crs[i].pinned = true;
bitmap_set(smmu->context_map, smmu->s2crs[i].cbndx, 1);
}
return 0;
}
static int qcom_sdm845_smmu500_reset(struct arm_smmu_device *smmu)
{
int ret;
arm_mmu500_reset(smmu);
/*
* To address performance degradation in non-real time clients,
* such as USB and UFS, turn off wait-for-safe on sdm845 based boards,
* such as MTP and db845, whose firmwares implement secure monitor
* call handlers to turn on/off the wait-for-safe logic.
*/
ret = qcom_scm_qsmmu500_wait_safe_toggle(0);
if (ret)
dev_warn(smmu->dev, "Failed to turn off SAFE logic\n");
return ret;
}
static const struct arm_smmu_impl qcom_smmu_impl = {
.cfg_probe = qcom_sdm845_smmu500_cfg_probe,
.reset = qcom_sdm845_smmu500_reset,
};
#define TCU_HW_VERSION_HLOS1 (0x18)
#define DEBUG_SID_HALT_REG 0x0
#define DEBUG_SID_HALT_REQ BIT(16)
#define DEBUG_SID_HALT_SID GENMASK(9, 0)
#define DEBUG_VA_ADDR_REG 0x8
#define DEBUG_TXN_TRIGG_REG 0x18
#define DEBUG_TXN_AXPROT GENMASK(8, 6)
#define DEBUG_TXN_AXCACHE GENMASK(5, 2)
#define DEBUG_TXN_WRITE BIT(1)
#define DEBUG_TXN_AXPROT_PRIV 0x1
#define DEBUG_TXN_AXPROT_UNPRIV 0x0
#define DEBUG_TXN_AXPROT_NSEC 0x2
#define DEBUG_TXN_AXPROT_SEC 0x0
#define DEBUG_TXN_AXPROT_INST 0x4
#define DEBUG_TXN_AXPROT_DATA 0x0
#define DEBUG_TXN_READ (0x0 << 1)
#define DEBUG_TXN_TRIGGER BIT(0)
#define DEBUG_SR_HALT_ACK_REG 0x20
#define DEBUG_SR_HALT_ACK_VAL (0x1 << 1)
#define DEBUG_SR_ECATS_RUNNING_VAL (0x1 << 0)
#define DEBUG_PAR_REG 0x28
#define DEBUG_PAR_PA GENMASK_ULL(47, 12)
#define DEBUG_PAR_FAULT_VAL BIT(0)
#define DEBUG_AXUSER_REG 0x30
#define DEBUG_AXUSER_CDMID GENMASK_ULL(43, 36)
#define DEBUG_AXUSER_CDMID_VAL 255
#define TBU_DBG_TIMEOUT_US 100
struct actlr_setting {
struct arm_smmu_smr smr;
u32 actlr;
};
struct qsmmuv500_archdata {
struct list_head tbus;
void __iomem *tcu_base;
u32 version;
struct actlr_setting *actlrs;
u32 actlr_tbl_size;
struct work_struct outstanding_tnx_work;
spinlock_t atos_lock;
struct arm_smmu_device smmu;
};
#define to_qsmmuv500_archdata(smmu) \
container_of(smmu, struct qsmmuv500_archdata, smmu)
struct qsmmuv500_group_iommudata {
bool has_actlr;
u32 actlr;
};
#define to_qsmmuv500_group_iommudata(group) \
((struct qsmmuv500_group_iommudata *) \
(iommu_group_get_iommudata(group)))
static struct qsmmuv500_tbu_device *qsmmuv500_find_tbu(
struct arm_smmu_device *smmu, u32 sid);
/*
* Provides mutually exclusive access to the registers used by the
* outstanding transaction snapshot feature and the transaction
* snapshot capture feature.
*/
static DEFINE_MUTEX(capture_reg_lock);
static DEFINE_SPINLOCK(testbus_lock);
#ifdef CONFIG_IOMMU_DEBUGFS
static struct dentry *debugfs_capturebus_dir;
#endif
#ifdef CONFIG_ARM_SMMU_TESTBUS_DEBUGFS
static struct dentry *debugfs_testbus_dir;
static ssize_t arm_smmu_debug_testbus_read(struct file *file,
char __user *ubuf, size_t count, loff_t *offset,
enum testbus_sel tbu, enum testbus_ops ops)
{
char buf[100];
ssize_t retval;
size_t buflen;
int buf_len = sizeof(buf);
if (*offset)
return 0;
memset(buf, 0, buf_len);
if (tbu == SEL_TBU) {
struct qsmmuv500_tbu_device *tbu = file->private_data;
void __iomem *tbu_base = tbu->base;
long val;
arm_smmu_power_on(tbu->pwr);
if (ops == TESTBUS_SELECT)
val = arm_smmu_debug_tbu_testbus_select(tbu_base,
READ, 0);
else
val = arm_smmu_debug_tbu_testbus_output(tbu_base);
arm_smmu_power_off(tbu->smmu, tbu->pwr);
scnprintf(buf, buf_len, "0x%0x\n", val);
} else {
struct arm_smmu_device *smmu = file->private_data;
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
phys_addr_t phys_addr = smmu->phys_addr;
void __iomem *tcu_base = data->tcu_base;
arm_smmu_power_on(smmu->pwr);
if (ops == TESTBUS_SELECT) {
scnprintf(buf, buf_len, "TCU clk testbus sel: 0x%0x\n",
arm_smmu_debug_tcu_testbus_select(phys_addr,
tcu_base, CLK_TESTBUS, READ, 0));
scnprintf(buf + strlen(buf), buf_len - strlen(buf),
"TCU testbus sel : 0x%0x\n",
arm_smmu_debug_tcu_testbus_select(phys_addr,
tcu_base, PTW_AND_CACHE_TESTBUS,
READ, 0));
} else {
scnprintf(buf, buf_len, "0x%0x\n",
arm_smmu_debug_tcu_testbus_output(phys_addr));
}
arm_smmu_power_off(smmu, smmu->pwr);
}
buflen = min(count, strlen(buf));
if (copy_to_user(ubuf, buf, buflen)) {
pr_err_ratelimited("Couldn't copy_to_user\n");
retval = -EFAULT;
} else {
*offset = 1;
retval = buflen;
}
return retval;
}
static ssize_t arm_smmu_debug_tcu_testbus_sel_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *offset)
{
struct arm_smmu_device *smmu = file->private_data;
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
void __iomem *tcu_base = data->tcu_base;
phys_addr_t phys_addr = smmu->phys_addr;
char *comma;
char buf[100];
u64 sel, val;
if (count >= 100) {
pr_err_ratelimited("Value too large\n");
return -EINVAL;
}
memset(buf, 0, 100);
if (copy_from_user(buf, ubuf, count)) {
pr_err_ratelimited("Couldn't copy from user\n");
return -EFAULT;
}
comma = strnchr(buf, count, ',');
if (!comma)
goto invalid_format;
/* split up the words */
*comma = '\0';
if (kstrtou64(buf, 0, &sel))
goto invalid_format;
if (sel != 1 && sel != 2)
goto invalid_format;
if (kstrtou64(comma + 1, 0, &val))
goto invalid_format;
arm_smmu_power_on(smmu->pwr);
if (sel == 1)
arm_smmu_debug_tcu_testbus_select(phys_addr,
tcu_base, CLK_TESTBUS, WRITE, val);
else if (sel == 2)
arm_smmu_debug_tcu_testbus_select(phys_addr,
tcu_base, PTW_AND_CACHE_TESTBUS, WRITE, val);
arm_smmu_power_off(smmu, smmu->pwr);
return count;
invalid_format:
pr_err_ratelimited("Invalid format. Expected: <1, testbus select> for tcu CLK testbus (or) <2, testbus select> for tcu PTW/CACHE testbuses\n");
return -EINVAL;
}
static ssize_t arm_smmu_debug_tcu_testbus_sel_read(struct file *file,
char __user *ubuf, size_t count, loff_t *offset)
{
return arm_smmu_debug_testbus_read(file, ubuf,
count, offset, SEL_TCU, TESTBUS_SELECT);
}
static const struct file_operations arm_smmu_debug_tcu_testbus_sel_fops = {
.open = simple_open,
.write = arm_smmu_debug_tcu_testbus_sel_write,
.read = arm_smmu_debug_tcu_testbus_sel_read,
};
static ssize_t arm_smmu_debug_tcu_testbus_read(struct file *file,
char __user *ubuf, size_t count, loff_t *offset)
{
return arm_smmu_debug_testbus_read(file, ubuf,
count, offset, SEL_TCU, TESTBUS_OUTPUT);
}
static const struct file_operations arm_smmu_debug_tcu_testbus_fops = {
.open = simple_open,
.read = arm_smmu_debug_tcu_testbus_read,
};
static int qsmmuv500_tcu_testbus_init(struct arm_smmu_device *smmu)
{
struct dentry *testbus_dir;
if (!iommu_debugfs_dir)
return 0;
if (!debugfs_testbus_dir) {
debugfs_testbus_dir = debugfs_create_dir("testbus",
iommu_debugfs_dir);
if (IS_ERR(debugfs_testbus_dir)) {
pr_err_ratelimited("Couldn't create iommu/testbus debugfs directory\n");
return -ENODEV;
}
}
testbus_dir = debugfs_create_dir(dev_name(smmu->dev),
debugfs_testbus_dir);
if (IS_ERR(testbus_dir)) {
pr_err_ratelimited("Couldn't create iommu/testbus/%s debugfs directory\n",
dev_name(smmu->dev));
goto err;
}
if (IS_ERR(debugfs_create_file("tcu_testbus_sel", 0400,
testbus_dir, smmu,
&arm_smmu_debug_tcu_testbus_sel_fops))) {
pr_err_ratelimited("Couldn't create iommu/testbus/%s/tcu_testbus_sel debugfs file\n",
dev_name(smmu->dev));
goto err_rmdir;
}
if (IS_ERR(debugfs_create_file("tcu_testbus_output", 0400,
testbus_dir, smmu,
&arm_smmu_debug_tcu_testbus_fops))) {
pr_err_ratelimited("Couldn't create iommu/testbus/%s/tcu_testbus_output debugfs file\n",
dev_name(smmu->dev));
goto err_rmdir;
}
return 0;
err_rmdir:
debugfs_remove_recursive(testbus_dir);
err:
return 0;
}
static ssize_t arm_smmu_debug_tbu_testbus_sel_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *offset)
{
struct qsmmuv500_tbu_device *tbu = file->private_data;
void __iomem *tbu_base = tbu->base;
u64 val;
if (kstrtoull_from_user(ubuf, count, 0, &val)) {
pr_err_ratelimited("Invalid format for tbu testbus select\n");
return -EINVAL;
}
arm_smmu_power_on(tbu->pwr);
arm_smmu_debug_tbu_testbus_select(tbu_base, WRITE, val);
arm_smmu_power_off(tbu->smmu, tbu->pwr);
return count;
}
static ssize_t arm_smmu_debug_tbu_testbus_sel_read(struct file *file,
char __user *ubuf, size_t count, loff_t *offset)
{
return arm_smmu_debug_testbus_read(file, ubuf,
count, offset, SEL_TBU, TESTBUS_SELECT);
}
static const struct file_operations arm_smmu_debug_tbu_testbus_sel_fops = {
.open = simple_open,
.write = arm_smmu_debug_tbu_testbus_sel_write,
.read = arm_smmu_debug_tbu_testbus_sel_read,
};
static ssize_t arm_smmu_debug_tbu_testbus_read(struct file *file,
char __user *ubuf, size_t count, loff_t *offset)
{
return arm_smmu_debug_testbus_read(file, ubuf,
count, offset, SEL_TBU, TESTBUS_OUTPUT);
}
static const struct file_operations arm_smmu_debug_tbu_testbus_fops = {
.open = simple_open,
.read = arm_smmu_debug_tbu_testbus_read,
};
static int qsmmuv500_tbu_testbus_init(struct qsmmuv500_tbu_device *tbu)
{
struct dentry *testbus_dir;
if (!iommu_debugfs_dir)
return 0;
if (!debugfs_testbus_dir) {
debugfs_testbus_dir = debugfs_create_dir("testbus",
iommu_debugfs_dir);
if (IS_ERR(debugfs_testbus_dir)) {
pr_err_ratelimited("Couldn't create iommu/testbus debugfs directory\n");
return -ENODEV;
}
}
testbus_dir = debugfs_create_dir(dev_name(tbu->dev),
debugfs_testbus_dir);
if (IS_ERR(testbus_dir)) {
pr_err_ratelimited("Couldn't create iommu/testbus/%s debugfs directory\n",
dev_name(tbu->dev));
goto err;
}
if (IS_ERR(debugfs_create_file("tbu_testbus_sel", 0400,
testbus_dir, tbu,
&arm_smmu_debug_tbu_testbus_sel_fops))) {
pr_err_ratelimited("Couldn't create iommu/testbus/%s/tbu_testbus_sel debugfs file\n",
dev_name(tbu->dev));
goto err_rmdir;
}
if (IS_ERR(debugfs_create_file("tbu_testbus_output", 0400,
testbus_dir, tbu,
&arm_smmu_debug_tbu_testbus_fops))) {
pr_err_ratelimited("Couldn't create iommu/testbus/%s/tbu_testbus_output debugfs file\n",
dev_name(tbu->dev));
goto err_rmdir;
}
return 0;
err_rmdir:
debugfs_remove_recursive(testbus_dir);
err:
return 0;
}
#else
static int qsmmuv500_tcu_testbus_init(struct arm_smmu_device *smmu)
{
return 0;
}
static int qsmmuv500_tbu_testbus_init(struct qsmmuv500_tbu_device *tbu)
{
return 0;
}
#endif
static void arm_smmu_testbus_dump(struct arm_smmu_device *smmu, u16 sid)
{
if (smmu->model == QCOM_SMMUV500 &&
IS_ENABLED(CONFIG_ARM_SMMU_TESTBUS_DUMP)) {
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
struct qsmmuv500_tbu_device *tbu;
tbu = qsmmuv500_find_tbu(smmu, sid);
spin_lock(&testbus_lock);
if (tbu)
arm_smmu_debug_dump_tbu_testbus(tbu->dev,
tbu->base,
tbu_testbus_sel);
else
arm_smmu_debug_dump_tcu_testbus(smmu->dev,
smmu->phys_addr,
data->tcu_base,
tcu_testbus_sel);
spin_unlock(&testbus_lock);
}
}
static void qsmmuv500_log_outstanding_transactions(struct work_struct *work)
{
struct qsmmuv500_tbu_device *tbu = NULL;
u64 outstanding_tnxs;
u64 tcr_cntl_val, res;
struct qsmmuv500_archdata *data = container_of(work,
struct qsmmuv500_archdata,
outstanding_tnx_work);
struct arm_smmu_device *smmu = &data->smmu;
void __iomem *base;
if (!mutex_trylock(&capture_reg_lock)) {
dev_warn_ratelimited(smmu->dev,
"Tnx snapshot regs in use, not dumping OT tnxs.\n");
goto bug;
}
if (arm_smmu_power_on(smmu->pwr)) {
dev_err_ratelimited(smmu->dev,
"%s: Failed to power on SMMU.\n",
__func__);
goto unlock;
}
list_for_each_entry(tbu, &data->tbus, list) {
if (arm_smmu_power_on(tbu->pwr)) {
dev_err_ratelimited(tbu->dev,
"%s: Failed to power on TBU.\n",
__func__);
continue;
}
base = tbu->base;
tcr_cntl_val = readq_relaxed(base + TNX_TCR_CNTL);
/* Write 1 into MATCH_MASK_UPD of TNX_TCR_CNTL */
writeq_relaxed(tcr_cntl_val | TNX_TCR_CNTL_MATCH_MASK_UPD,
base + TNX_TCR_CNTL);
/*
* Simultaneously write 0 into MATCH_MASK_UPD, 0 into
* ALWAYS_CAPTURE, 0 into MATCH_MASK_VALID, and 1 into
* TBU_OT_CAPTURE_EN of TNX_TCR_CNTL
*/
tcr_cntl_val &= ~(TNX_TCR_CNTL_MATCH_MASK_UPD |
TNX_TCR_CNTL_ALWAYS_CAPTURE |
TNX_TCR_CNTL_MATCH_MASK_VALID);
writeq_relaxed(tcr_cntl_val | TNX_TCR_CNTL_TBU_OT_CAPTURE_EN,
base + TNX_TCR_CNTL);
/* Poll for CAPTURE1_VALID to become 1 on TNX_TCR_CNTL_2 */
if (readq_poll_timeout_atomic(base + TNX_TCR_CNTL_2, res,
res & TNX_TCR_CNTL_2_CAP1_VALID,
0, TBU_DBG_TIMEOUT_US)) {
dev_err_ratelimited(tbu->dev,
"Timeout on TNX snapshot poll\n");
goto poll_timeout;
}
/* Read Register CAPTURE1_SNAPSHOT_1 */
outstanding_tnxs = readq_relaxed(base + CAPTURE1_SNAPSHOT_1);
dev_err_ratelimited(tbu->dev,
"Outstanding Transaction Bitmap: 0x%llx\n",
outstanding_tnxs);
poll_timeout:
/* Write TBU_OT_CAPTURE_EN to 0 of TNX_TCR_CNTL */
writeq_relaxed(tcr_cntl_val & ~TNX_TCR_CNTL_TBU_OT_CAPTURE_EN,
tbu->base + TNX_TCR_CNTL);
arm_smmu_power_off(smmu, tbu->pwr);
}
arm_smmu_power_off(smmu, smmu->pwr);
unlock:
mutex_unlock(&capture_reg_lock);
bug:
BUG_ON(IS_ENABLED(CONFIG_IOMMU_TLBSYNC_DEBUG));
}
static ssize_t arm_smmu_debug_capturebus_snapshot_read(struct file *file,
char __user *ubuf, size_t count, loff_t *offset)
{
struct qsmmuv500_tbu_device *tbu = file->private_data;
struct arm_smmu_device *smmu = tbu->smmu;
void __iomem *tbu_base = tbu->base;
u64 snapshot[NO_OF_CAPTURE_POINTS][REGS_PER_CAPTURE_POINT];
char buf[400];
ssize_t retval;
size_t buflen;
int buf_len = sizeof(buf);
int i, j;
if (*offset)
return 0;
memset(buf, 0, buf_len);
if (arm_smmu_power_on(smmu->pwr))
return -EINVAL;
if (arm_smmu_power_on(tbu->pwr)) {
arm_smmu_power_off(smmu, smmu->pwr);
return -EINVAL;
}
if (!mutex_trylock(&capture_reg_lock)) {
dev_warn_ratelimited(smmu->dev,
"capture bus regs in use, not dumping it.\n");
return -EBUSY;
}
arm_smmu_debug_get_capture_snapshot(tbu_base, snapshot);
mutex_unlock(&capture_reg_lock);
arm_smmu_power_off(tbu->smmu, tbu->pwr);
arm_smmu_power_off(smmu, smmu->pwr);
for (i = 0; i < NO_OF_CAPTURE_POINTS ; ++i) {
for (j = 0; j < REGS_PER_CAPTURE_POINT; ++j) {
scnprintf(buf + strlen(buf), buf_len - strlen(buf),
"Capture_%d_Snapshot_%d : 0x%0llx\n",
i+1, j+1, snapshot[i][j]);
}
}
buflen = min(count, strlen(buf));
if (copy_to_user(ubuf, buf, buflen)) {
dev_err_ratelimited(smmu->dev, "Couldn't copy_to_user\n");
retval = -EFAULT;
} else {
*offset = 1;
retval = buflen;
}
return retval;
}
static const struct file_operations arm_smmu_debug_capturebus_snapshot_fops = {
.open = simple_open,
.read = arm_smmu_debug_capturebus_snapshot_read,
};
static ssize_t arm_smmu_debug_capturebus_config_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *offset)
{
struct qsmmuv500_tbu_device *tbu = file->private_data;
struct arm_smmu_device *smmu = tbu->smmu;
void __iomem *tbu_base = tbu->base;
char *comma1, *comma2;
char buf[100];
u64 sel, mask, match, val;
if (count >= sizeof(buf)) {
dev_err_ratelimited(smmu->dev, "Input too large\n");
goto invalid_format;
}
memset(buf, 0, sizeof(buf));
if (copy_from_user(buf, ubuf, count)) {
dev_err_ratelimited(smmu->dev, "Couldn't copy from user\n");
return -EFAULT;
}
comma1 = strnchr(buf, count, ',');
if (!comma1)
goto invalid_format;
*comma1 = '\0';
if (kstrtou64(buf, 0, &sel))
goto invalid_format;
if (sel > 4) {
goto invalid_format;
} else if (sel == 4) {
if (kstrtou64(comma1 + 1, 0, &val))
goto invalid_format;
goto program_capturebus;
}
comma2 = strnchr(comma1 + 1, count, ',');
if (!comma2)
goto invalid_format;
/* split up the words */
*comma2 = '\0';
if (kstrtou64(comma1 + 1, 0, &mask))
goto invalid_format;
if (kstrtou64(comma2 + 1, 0, &match))
goto invalid_format;
program_capturebus:
if (arm_smmu_power_on(smmu->pwr))
return -EINVAL;
if (arm_smmu_power_on(tbu->pwr)) {
arm_smmu_power_off(smmu, smmu->pwr);
return -EINVAL;
}
if (!mutex_trylock(&capture_reg_lock)) {
dev_warn_ratelimited(smmu->dev,
"capture bus regs in use, not configuring it.\n");
return -EBUSY;
}
if (sel == 4)
arm_smmu_debug_set_tnx_tcr_cntl(tbu_base, val);
else
arm_smmu_debug_set_mask_and_match(tbu_base, sel, mask, match);
mutex_unlock(&capture_reg_lock);
arm_smmu_power_off(tbu->smmu, tbu->pwr);
arm_smmu_power_off(smmu, smmu->pwr);
return count;
invalid_format:
dev_err_ratelimited(smmu->dev, "Invalid format\n");
dev_err_ratelimited(smmu->dev,
"Expected:<1/2/3,Mask,Match> <4,TNX_TCR_CNTL>\n");
return -EINVAL;
}
static ssize_t arm_smmu_debug_capturebus_config_read(struct file *file,
char __user *ubuf, size_t count, loff_t *offset)
{
struct qsmmuv500_tbu_device *tbu = file->private_data;
struct arm_smmu_device *smmu = tbu->smmu;
void __iomem *tbu_base = tbu->base;
u64 val;
u64 mask[NO_OF_MASK_AND_MATCH], match[NO_OF_MASK_AND_MATCH];
char buf[400];
ssize_t retval;
size_t buflen;
int buf_len = sizeof(buf);
int i;
if (*offset)
return 0;
memset(buf, 0, buf_len);
if (arm_smmu_power_on(smmu->pwr))
return -EINVAL;
if (arm_smmu_power_on(tbu->pwr)) {
arm_smmu_power_off(smmu, smmu->pwr);
return -EINVAL;
}
if (!mutex_trylock(&capture_reg_lock)) {
dev_warn_ratelimited(smmu->dev,
"capture bus regs in use, not configuring it.\n");
return -EBUSY;
}
arm_smmu_debug_get_mask_and_match(tbu_base,
mask, match);
val = arm_smmu_debug_get_tnx_tcr_cntl(tbu_base);
mutex_unlock(&capture_reg_lock);
arm_smmu_power_off(tbu->smmu, tbu->pwr);
arm_smmu_power_off(smmu, smmu->pwr);
for (i = 0; i < NO_OF_MASK_AND_MATCH; ++i) {
scnprintf(buf + strlen(buf), buf_len - strlen(buf),
"Mask_%d : 0x%0llx\t", i+1, mask[i]);
scnprintf(buf + strlen(buf), buf_len - strlen(buf),
"Match_%d : 0x%0llx\n", i+1, match[i]);
}
scnprintf(buf + strlen(buf), buf_len - strlen(buf), "0x%0lx\n", val);
buflen = min(count, strlen(buf));
if (copy_to_user(ubuf, buf, buflen)) {
dev_err_ratelimited(smmu->dev, "Couldn't copy_to_user\n");
retval = -EFAULT;
} else {
*offset = 1;
retval = buflen;
}
return retval;
}
static const struct file_operations arm_smmu_debug_capturebus_config_fops = {
.open = simple_open,
.write = arm_smmu_debug_capturebus_config_write,
.read = arm_smmu_debug_capturebus_config_read,
};
#ifdef CONFIG_IOMMU_DEBUGFS
static int qsmmuv500_capturebus_init(struct qsmmuv500_tbu_device *tbu)
{
struct dentry *capturebus_dir;
if (!iommu_debugfs_dir)
return 0;
if (!debugfs_capturebus_dir) {
debugfs_capturebus_dir = debugfs_create_dir(
"capturebus", iommu_debugfs_dir);
if (IS_ERR(debugfs_capturebus_dir)) {
dev_err_ratelimited(tbu->dev, "Couldn't create iommu/capturebus debugfs directory\n");
return PTR_ERR(debugfs_capturebus_dir);
}
}
capturebus_dir = debugfs_create_dir(dev_name(tbu->dev),
debugfs_capturebus_dir);
if (IS_ERR(capturebus_dir)) {
dev_err_ratelimited(tbu->dev, "Couldn't create iommu/capturebus/%s debugfs directory\n",
dev_name(tbu->dev));
goto err;
}
if (IS_ERR(debugfs_create_file("config", 0400, capturebus_dir, tbu,
&arm_smmu_debug_capturebus_config_fops))) {
dev_err_ratelimited(tbu->dev, "Couldn't create iommu/capturebus/%s/config debugfs file\n",
dev_name(tbu->dev));
goto err_rmdir;
}
if (IS_ERR(debugfs_create_file("snapshot", 0400, capturebus_dir, tbu,
&arm_smmu_debug_capturebus_snapshot_fops))) {
dev_err_ratelimited(tbu->dev, "Couldn't create iommu/capturebus/%s/snapshot debugfs file\n",
dev_name(tbu->dev));
goto err_rmdir;
}
return 0;
err_rmdir:
debugfs_remove_recursive(capturebus_dir);
err:
return -ENODEV;
}
#else
static int qsmmuv500_capturebus_init(struct qsmmuv500_tbu_device *tbu)
{
return 0;
}
#endif
static irqreturn_t arm_smmu_debug_capture_bus_match(int irq, void *dev)
{
struct qsmmuv500_tbu_device *tbu = dev;
struct arm_smmu_device *smmu = tbu->smmu;
void __iomem *tbu_base = tbu->base;
u64 mask[NO_OF_MASK_AND_MATCH], match[NO_OF_MASK_AND_MATCH];
u64 snapshot[NO_OF_CAPTURE_POINTS][REGS_PER_CAPTURE_POINT];
int i, j;
u64 val;
if (arm_smmu_power_on(smmu->pwr))
return IRQ_NONE;
if (arm_smmu_power_on(tbu->pwr)) {
arm_smmu_power_off(smmu, smmu->pwr);
return IRQ_NONE;
}
if (!mutex_trylock(&capture_reg_lock)) {
dev_warn_ratelimited(smmu->dev,
"capture bus regs in use, not dumping it.\n");
return IRQ_NONE;
}
val = arm_smmu_debug_get_tnx_tcr_cntl(tbu_base);
arm_smmu_debug_get_mask_and_match(tbu_base, mask, match);
arm_smmu_debug_get_capture_snapshot(tbu_base, snapshot);
arm_smmu_debug_clear_intr_and_validbits(tbu_base);
mutex_unlock(&capture_reg_lock);
arm_smmu_power_off(tbu->smmu, tbu->pwr);
arm_smmu_power_off(smmu, smmu->pwr);
dev_info(tbu->dev, "TNX_TCR_CNTL : 0x%0llx\n", val);
for (i = 0; i < NO_OF_MASK_AND_MATCH; ++i) {
dev_info(tbu->dev,
"Mask_%d : 0x%0llx\n", i+1, mask[i]);
dev_info(tbu->dev,
"Match_%d : 0x%0llx\n", i+1, match[i]);
}
for (i = 0; i < NO_OF_CAPTURE_POINTS ; ++i) {
for (j = 0; j < REGS_PER_CAPTURE_POINT; ++j) {
dev_info(tbu->dev,
"Capture_%d_Snapshot_%d : 0x%0llx\n",
i+1, j+1, snapshot[i][j]);
}
}
return IRQ_HANDLED;
}
static void qsmmuv500_tlb_sync_timeout(struct arm_smmu_device *smmu)
{
u32 sync_inv_ack, tbu_pwr_status, sync_inv_progress;
u32 tbu_inv_pending = 0, tbu_sync_pending = 0;
u32 tbu_inv_acked = 0, tbu_sync_acked = 0;
u32 tcu_inv_pending = 0, tcu_sync_pending = 0;
unsigned long tbu_ids = 0;
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
int ret;
static DEFINE_RATELIMIT_STATE(_rs,
DEFAULT_RATELIMIT_INTERVAL,
DEFAULT_RATELIMIT_BURST);
dev_err_ratelimited(smmu->dev,
"TLB sync timed out -- SMMU may be deadlocked\n");
sync_inv_ack = arm_smmu_readl(smmu,
ARM_SMMU_IMPL_DEF0,
ARM_SMMU_STATS_SYNC_INV_TBU_ACK);
ret = qcom_scm_io_readl((unsigned long)(smmu->phys_addr +
ARM_SMMU_TBU_PWR_STATUS), &tbu_pwr_status);
if (ret) {
dev_err_ratelimited(smmu->dev,
"SCM read of TBU power status fails: %d\n",
ret);
goto out;
}
ret = qcom_scm_io_readl((unsigned long)(smmu->phys_addr +
ARM_SMMU_MMU2QSS_AND_SAFE_WAIT_CNTR),
&sync_inv_progress);
if (ret) {
dev_err_ratelimited(smmu->dev,
"SCM read of TBU sync/inv prog fails: %d\n",
ret);
goto out;
}
if (sync_inv_ack) {
tbu_inv_pending = FIELD_GET(TBU_INV_REQ, sync_inv_ack);
tbu_inv_acked = FIELD_GET(TBU_INV_ACK, sync_inv_ack);
tbu_sync_pending = FIELD_GET(TBU_SYNC_REQ, sync_inv_ack);
tbu_sync_acked = FIELD_GET(TBU_SYNC_ACK, sync_inv_ack);
}
if (tbu_pwr_status) {
if (tbu_sync_pending)
tbu_ids = tbu_pwr_status & ~tbu_sync_acked;
else if (tbu_inv_pending)
tbu_ids = tbu_pwr_status & ~tbu_inv_acked;
}
tcu_inv_pending = FIELD_GET(TCU_INV_IN_PRGSS, sync_inv_progress);
tcu_sync_pending = FIELD_GET(TCU_SYNC_IN_PRGSS, sync_inv_progress);
if (__ratelimit(&_rs)) {
unsigned long tbu_id;
dev_err(smmu->dev,
"TBU ACK 0x%x TBU PWR 0x%x TCU sync_inv 0x%x\n",
sync_inv_ack, tbu_pwr_status, sync_inv_progress);
dev_err(smmu->dev,
"TCU invalidation %s, TCU sync %s\n",
tcu_inv_pending?"pending":"completed",
tcu_sync_pending?"pending":"completed");
for_each_set_bit(tbu_id, &tbu_ids, sizeof(tbu_ids) *
BITS_PER_BYTE) {
struct qsmmuv500_tbu_device *tbu;
tbu = qsmmuv500_find_tbu(smmu,
(u16)(tbu_id << TBUID_SHIFT));
if (tbu) {
dev_err(smmu->dev,
"TBU %s ack pending for TBU %s, %s\n",
tbu_sync_pending?"sync" : "inv",
dev_name(tbu->dev),
tbu_sync_pending ?
"check pending transactions on TBU"
: "check for TBU power status");
arm_smmu_testbus_dump(smmu,
(u16)(tbu_id << TBUID_SHIFT));
}
}
/*dump TCU testbus*/
arm_smmu_testbus_dump(smmu, U16_MAX);
}
if (tcu_sync_pending) {
schedule_work(&data->outstanding_tnx_work);
return;
}
out:
BUG_ON(IS_ENABLED(CONFIG_IOMMU_TLBSYNC_DEBUG));
}
static void qsmmuv500_device_remove(struct arm_smmu_device *smmu)
{
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
cancel_work_sync(&data->outstanding_tnx_work);
}
static bool arm_smmu_fwspec_match_smr(struct iommu_fwspec *fwspec,
struct arm_smmu_smr *smr)
{
struct arm_smmu_smr *smr2;
struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
int i, idx;
for_each_cfg_sme(fwspec, i, idx) {
smr2 = &smmu->smrs[idx];
/* Continue if table entry does not match */
if ((smr->id ^ smr2->id) & ~(smr->mask | smr2->mask))
continue;
return true;
}
return false;
}
static int qsmmuv500_tbu_halt(struct qsmmuv500_tbu_device *tbu,
struct arm_smmu_domain *smmu_domain)
{
unsigned long flags;
struct arm_smmu_device *smmu = smmu_domain->smmu;
int idx = smmu_domain->cfg.cbndx;
u32 halt, fsr, status;
void __iomem *tbu_base;
if (of_property_read_bool(tbu->dev->of_node,
"qcom,opt-out-tbu-halting")) {
dev_notice(tbu->dev, "TBU opted-out for halting!\n");
return -EBUSY;
}
spin_lock_irqsave(&tbu->halt_lock, flags);
if (tbu->halt_count) {
tbu->halt_count++;
spin_unlock_irqrestore(&tbu->halt_lock, flags);
return 0;
}
tbu_base = tbu->base;
halt = readl_relaxed(tbu_base + DEBUG_SID_HALT_REG);
halt |= DEBUG_SID_HALT_REQ;
writel_relaxed(halt, tbu_base + DEBUG_SID_HALT_REG);
fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
if ((fsr & FSR_FAULT) && (fsr & FSR_SS)) {
u32 sctlr_orig, sctlr;
/*
* We are in a fault; Our request to halt the bus will not
* complete until transactions in front of us (such as the fault
* itself) have completed. Disable iommu faults and terminate
* any existing transactions.
*/
sctlr_orig = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_SCTLR);
sctlr = sctlr_orig & ~(SCTLR_CFCFG | SCTLR_CFIE);
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, sctlr);
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
/*
* Barrier required to ensure that the FSR is cleared
* before resuming SMMU operation
*/
wmb();
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_RESUME,
RESUME_TERMINATE);
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, sctlr_orig);
}
if (readl_poll_timeout_atomic(tbu_base + DEBUG_SR_HALT_ACK_REG, status,
(status & DEBUG_SR_HALT_ACK_VAL),
0, TBU_DBG_TIMEOUT_US)) {
dev_err(tbu->dev, "Couldn't halt TBU!\n");
halt = readl_relaxed(tbu_base + DEBUG_SID_HALT_REG);
halt &= ~DEBUG_SID_HALT_REQ;
writel_relaxed(halt, tbu_base + DEBUG_SID_HALT_REG);
spin_unlock_irqrestore(&tbu->halt_lock, flags);
return -ETIMEDOUT;
}
tbu->halt_count = 1;
spin_unlock_irqrestore(&tbu->halt_lock, flags);
return 0;
}
static void qsmmuv500_tbu_resume(struct qsmmuv500_tbu_device *tbu)
{
unsigned long flags;
u32 val;
void __iomem *base;
spin_lock_irqsave(&tbu->halt_lock, flags);
if (!tbu->halt_count) {
WARN(1, "%s: bad tbu->halt_count", dev_name(tbu->dev));
spin_unlock_irqrestore(&tbu->halt_lock, flags);
return;
} else if (tbu->halt_count > 1) {
tbu->halt_count--;
spin_unlock_irqrestore(&tbu->halt_lock, flags);
return;
}
base = tbu->base;
val = readl_relaxed(base + DEBUG_SID_HALT_REG);
val &= ~DEBUG_SID_HALT_REQ;
writel_relaxed(val, base + DEBUG_SID_HALT_REG);
tbu->halt_count = 0;
spin_unlock_irqrestore(&tbu->halt_lock, flags);
}
static struct qsmmuv500_tbu_device *qsmmuv500_find_tbu(
struct arm_smmu_device *smmu, u32 sid)
{
struct qsmmuv500_tbu_device *tbu = NULL;
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
list_for_each_entry(tbu, &data->tbus, list) {
if (tbu->sid_start <= sid &&
sid < tbu->sid_start + tbu->num_sids)
return tbu;
}
return NULL;
}
static int qsmmuv500_ecats_lock(struct arm_smmu_domain *smmu_domain,
struct qsmmuv500_tbu_device *tbu,
unsigned long *flags)
__acquires(&smmu->atos_lock)
{
struct arm_smmu_device *smmu = tbu->smmu;
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
u32 val;
spin_lock_irqsave(&data->atos_lock, *flags);
/* The status register is not accessible on version 1.0 */
if (data->version == 0x01000000)
return 0;
if (readl_poll_timeout_atomic(tbu->status_reg,
val, (val == 0x1), 0,
TBU_DBG_TIMEOUT_US)) {
dev_err(tbu->dev, "ECATS hw busy!\n");
spin_unlock_irqrestore(&data->atos_lock, *flags);
return -ETIMEDOUT;
}
return 0;
}
static void qsmmuv500_ecats_unlock(struct arm_smmu_domain *smmu_domain,
struct qsmmuv500_tbu_device *tbu,
unsigned long *flags)
__releases(&smmu->atos_lock)
{
struct arm_smmu_device *smmu = tbu->smmu;
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
/* The status register is not accessible on version 1.0 */
if (data->version != 0x01000000)
writel_relaxed(0, tbu->status_reg);
spin_unlock_irqrestore(&data->atos_lock, *flags);
}
/*
* Zero means failure.
*/
static phys_addr_t qsmmuv500_iova_to_phys(
struct arm_smmu_domain *smmu_domain, dma_addr_t iova, u32 sid,
unsigned long trans_flags)
{
struct arm_smmu_device *smmu = smmu_domain->smmu;
struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
struct qsmmuv500_tbu_device *tbu;
int ret;
phys_addr_t phys = 0;
u64 val, fsr;
long iova_ext_bits = (s64)iova >> smmu->va_size;
bool split_tables = test_bit(DOMAIN_ATTR_SPLIT_TABLES,
smmu_domain->attributes);
unsigned long flags;
int idx = cfg->cbndx;
u32 sctlr_orig, sctlr;
int needs_redo = 0;
ktime_t timeout;
if (iova_ext_bits && split_tables)
iova_ext_bits = ~iova_ext_bits;
if (iova_ext_bits) {
dev_err_ratelimited(smmu->dev, "ECATS: address out of bounds: %pad\n",
&iova);
return 0;
}
tbu = qsmmuv500_find_tbu(smmu, sid);
if (!tbu)
return 0;
ret = arm_smmu_power_on(tbu->pwr);
if (ret)
return 0;
ret = qsmmuv500_tbu_halt(tbu, smmu_domain);
if (ret)
goto out_power_off;
/*
* ECATS can trigger the fault interrupt, so disable it temporarily
* and check for an interrupt manually.
*/
sctlr_orig = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_SCTLR);
sctlr = sctlr_orig & ~(SCTLR_CFCFG | SCTLR_CFIE);
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, sctlr);
fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
if (fsr & FSR_FAULT) {
/* Clear pending interrupts */
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
/*
* Barrier required to ensure that the FSR is cleared
* before resuming SMMU operation.
*/
wmb();
/*
* TBU halt takes care of resuming any stalled transcation.
* Kept it here for completeness sake.
*/
if (fsr & FSR_SS)
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_RESUME,
RESUME_TERMINATE);
}
/* Only one concurrent atos operation */
ret = qsmmuv500_ecats_lock(smmu_domain, tbu, &flags);
if (ret)
goto out_resume;
redo:
/* Set address and stream-id */
val = readq_relaxed(tbu->base + DEBUG_SID_HALT_REG);
val &= ~DEBUG_SID_HALT_SID;
val |= FIELD_PREP(DEBUG_SID_HALT_SID, sid);
writeq_relaxed(val, tbu->base + DEBUG_SID_HALT_REG);
writeq_relaxed(iova, tbu->base + DEBUG_VA_ADDR_REG);
val = FIELD_PREP(DEBUG_AXUSER_CDMID, DEBUG_AXUSER_CDMID_VAL);
writeq_relaxed(val, tbu->base + DEBUG_AXUSER_REG);
/* Write-back Read and Write-Allocate */
val = FIELD_PREP(DEBUG_TXN_AXCACHE, 0xF);
/* Non-secure Access */
val |= FIELD_PREP(DEBUG_TXN_AXPROT, DEBUG_TXN_AXPROT_NSEC);
/* Write or Read Access */
if (trans_flags & IOMMU_TRANS_WRITE)
val |= DEBUG_TXN_WRITE;
/* Priviledged or Unpriviledged Access */
if (trans_flags & IOMMU_TRANS_PRIV)
val |= FIELD_PREP(DEBUG_TXN_AXPROT, DEBUG_TXN_AXPROT_PRIV);
/* Data or Instruction Access */
if (trans_flags & IOMMU_TRANS_INST)
val |= FIELD_PREP(DEBUG_TXN_AXPROT, DEBUG_TXN_AXPROT_INST);
val |= DEBUG_TXN_TRIGGER;
writeq_relaxed(val, tbu->base + DEBUG_TXN_TRIGG_REG);
ret = 0;
timeout = ktime_add_us(ktime_get(), TBU_DBG_TIMEOUT_US);
for (;;) {
val = readl_relaxed(tbu->base + DEBUG_SR_HALT_ACK_REG);
if (!(val & DEBUG_SR_ECATS_RUNNING_VAL))
break;
val = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
if (val & FSR_FAULT)
break;
if (ktime_compare(ktime_get(), timeout) > 0) {
dev_err_ratelimited(tbu->dev, "ECATS translation timed out!\n");
ret = -ETIMEDOUT;
break;
}
}
val = readq_relaxed(tbu->base + DEBUG_PAR_REG);
fsr = arm_smmu_cb_read(smmu, idx, ARM_SMMU_CB_FSR);
if (val & DEBUG_PAR_FAULT_VAL) {
dev_err(tbu->dev, "ECATS generated a fault interrupt! FSR = %llx, SID=0x%x\n",
fsr, sid);
dev_err(tbu->dev, "ECATS translation failed! PAR = %llx\n",
val);
/* Clear pending interrupts */
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_FSR, fsr);
/*
* Barrier required to ensure that the FSR is cleared
* before resuming SMMU operation.
*/
wmb();
if (fsr & FSR_SS)
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_RESUME,
RESUME_TERMINATE);
ret = -EINVAL;
}
phys = FIELD_GET(DEBUG_PAR_PA, val);
if (ret < 0)
phys = 0;
/* Reset hardware */
writeq_relaxed(0, tbu->base + DEBUG_TXN_TRIGG_REG);
writeq_relaxed(0, tbu->base + DEBUG_VA_ADDR_REG);
val = readl_relaxed(tbu->base + DEBUG_SID_HALT_REG);
val &= ~DEBUG_SID_HALT_SID;
writel_relaxed(val, tbu->base + DEBUG_SID_HALT_REG);
/*
* After a failed translation, the next successful translation will
* incorrectly be reported as a failure.
*/
if (!phys && needs_redo++ < 2)
goto redo;
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_SCTLR, sctlr_orig);
qsmmuv500_ecats_unlock(smmu_domain, tbu, &flags);
out_resume:
qsmmuv500_tbu_resume(tbu);
out_power_off:
/* Read to complete prior write transcations */
val = readl_relaxed(tbu->base + DEBUG_SR_HALT_ACK_REG);
/* Wait for read to complete before off */
rmb();
arm_smmu_power_off(tbu->smmu, tbu->pwr);
return phys;
}
static phys_addr_t qsmmuv500_iova_to_phys_hard(
struct arm_smmu_domain *smmu_domain,
dma_addr_t iova,
unsigned long trans_flags)
{
u16 sid;
struct msm_iommu_domain *msm_domain = &smmu_domain->domain;
struct arm_smmu_cfg *cfg = &smmu_domain->cfg;
struct arm_smmu_device *smmu = smmu_domain->smmu;
struct iommu_fwspec *fwspec;
u32 frsynra;
/* Check to see if the domain is associated with the test
* device. If the domain belongs to the test device, then
* pick the SID from fwspec.
*/
if (msm_domain->is_debug_domain) {
fwspec = dev_iommu_fwspec_get(smmu_domain->dev);
sid = (u16)fwspec->ids[0];
} else {
/* If the domain belongs to an actual device, read
* SID from the corresponding frsynra register
*/
frsynra = arm_smmu_gr1_read(smmu,
ARM_SMMU_GR1_CBFRSYNRA(cfg->cbndx));
sid = FIELD_GET(CBFRSYNRA_SID, frsynra);
}
return qsmmuv500_iova_to_phys(smmu_domain, iova, sid, trans_flags);
}
static void qsmmuv500_release_group_iommudata(void *data)
{
kfree(data);
}
/* If a device has a valid actlr, it must match */
static int qsmmuv500_device_group(struct device *dev,
struct iommu_group *group)
{
struct iommu_fwspec *fwspec = dev_iommu_fwspec_get(dev);
struct arm_smmu_device *smmu = fwspec_smmu(fwspec);
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
struct qsmmuv500_group_iommudata *iommudata;
u32 actlr, i;
struct arm_smmu_smr *smr;
iommudata = to_qsmmuv500_group_iommudata(group);
if (!iommudata) {
iommudata = kzalloc(sizeof(*iommudata), GFP_KERNEL);
if (!iommudata)
return -ENOMEM;
iommu_group_set_iommudata(group, iommudata,
qsmmuv500_release_group_iommudata);
}
for (i = 0; i < data->actlr_tbl_size; i++) {
smr = &data->actlrs[i].smr;
actlr = data->actlrs[i].actlr;
if (!arm_smmu_fwspec_match_smr(fwspec, smr))
continue;
if (!iommudata->has_actlr) {
iommudata->actlr = actlr;
iommudata->has_actlr = true;
} else if (iommudata->actlr != actlr) {
return -EINVAL;
}
}
return 0;
}
static void qsmmuv500_init_cb(struct arm_smmu_domain *smmu_domain,
struct device *dev)
{
struct arm_smmu_device *smmu = smmu_domain->smmu;
struct qsmmuv500_group_iommudata *iommudata =
to_qsmmuv500_group_iommudata(dev->iommu_group);
int idx = smmu_domain->cfg.cbndx;
const struct iommu_flush_ops *tlb;
if (!iommudata->has_actlr)
return;
tlb = smmu_domain->pgtbl_info[0].pgtbl_cfg.tlb;
arm_smmu_cb_write(smmu, idx, ARM_SMMU_CB_ACTLR, iommudata->actlr);
/*
* Flush the context bank after modifying ACTLR to ensure there
* are no cache entries with stale state
*/
tlb->tlb_flush_all(smmu_domain);
}
static int qsmmuv500_tbu_register(struct device *dev, void *cookie)
{
struct resource *res;
struct qsmmuv500_tbu_device *tbu;
struct qsmmuv500_archdata *data = cookie;
struct platform_device *pdev = to_platform_device(dev);
int i, err, num_irqs = 0;
if (!dev->driver) {
dev_err(dev, "TBU failed probe, QSMMUV500 cannot continue!\n");
return -EINVAL;
}
tbu = dev_get_drvdata(dev);
INIT_LIST_HEAD(&tbu->list);
tbu->smmu = &data->smmu;
list_add(&tbu->list, &data->tbus);
while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, num_irqs)))
num_irqs++;
tbu->irqs = devm_kzalloc(dev, sizeof(*tbu->irqs) * num_irqs,
GFP_KERNEL);
if (!tbu->irqs)
return -ENOMEM;
for (i = 0; i < num_irqs; ++i) {
int irq = platform_get_irq(pdev, i);
if (irq < 0) {
dev_err(dev, "failed to get irq index %d\n", i);
return -ENODEV;
}
tbu->irqs[i] = irq;
err = devm_request_threaded_irq(tbu->dev, tbu->irqs[i],
NULL, arm_smmu_debug_capture_bus_match,
IRQF_ONESHOT | IRQF_SHARED,
"capture bus", tbu);
if (err) {
dev_err(dev, "failed to request capture bus irq%d (%u)\n",
i, tbu->irqs[i]);
return err;
}
}
qsmmuv500_tbu_testbus_init(tbu);
qsmmuv500_capturebus_init(tbu);
return 0;
}
static int qsmmuv500_read_actlr_tbl(struct qsmmuv500_archdata *data)
{
int len, i;
struct device *dev = data->smmu.dev;
struct actlr_setting *actlrs;
const __be32 *cell;
cell = of_get_property(dev->of_node, "qcom,actlr", NULL);
if (!cell)
return 0;
len = of_property_count_elems_of_size(dev->of_node, "qcom,actlr",
sizeof(u32) * 3);
if (len < 0)
return 0;
actlrs = devm_kzalloc(dev, sizeof(*actlrs) * len, GFP_KERNEL);
if (!actlrs)
return -ENOMEM;
for (i = 0; i < len; i++) {
actlrs[i].smr.id = of_read_number(cell++, 1);
actlrs[i].smr.mask = of_read_number(cell++, 1);
actlrs[i].actlr = of_read_number(cell++, 1);
}
data->actlrs = actlrs;
data->actlr_tbl_size = len;
return 0;
}
static int qsmmuv500_cfg_probe(struct arm_smmu_device *smmu)
{
struct qsmmuv500_archdata *data = to_qsmmuv500_archdata(smmu);
u32 val;
data->version = readl_relaxed(data->tcu_base + TCU_HW_VERSION_HLOS1);
val = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sACR);
val &= ~ARM_MMU500_ACR_CACHE_LOCK;
arm_smmu_gr0_write(smmu, ARM_SMMU_GR0_sACR, val);
val = arm_smmu_gr0_read(smmu, ARM_SMMU_GR0_sACR);
/*
* Modifiying the nonsecure copy of the sACR register is only
* allowed if permission is given in the secure sACR register.
* Attempt to detect if we were able to update the value.
*/
WARN_ON(val & ARM_MMU500_ACR_CACHE_LOCK);
return 0;
}
static const struct arm_smmu_impl qsmmuv500_impl = {
.cfg_probe = qsmmuv500_cfg_probe,
.init_context_bank = qsmmuv500_init_cb,
.iova_to_phys_hard = qsmmuv500_iova_to_phys_hard,
.tlb_sync_timeout = qsmmuv500_tlb_sync_timeout,
.device_remove = qsmmuv500_device_remove,
.device_group = qsmmuv500_device_group,
};
struct arm_smmu_device *qsmmuv500_impl_init(struct arm_smmu_device *smmu)
{
struct resource *res;
struct device *dev = smmu->dev;
struct qsmmuv500_archdata *data;
struct platform_device *pdev;
int ret;
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if (!data)
return ERR_PTR(-ENOMEM);
INIT_LIST_HEAD(&data->tbus);
pdev = to_platform_device(dev);
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tcu-base");
if (!res) {
dev_err(dev, "Unable to get the tcu-base\n");
return ERR_PTR(-EINVAL);
}
data->tcu_base = devm_ioremap_resource(dev, res);
if (IS_ERR(data->tcu_base))
return ERR_CAST(data->tcu_base);
spin_lock_init(&data->atos_lock);
data->smmu = *smmu;
data->smmu.impl = &qsmmuv500_impl;
qsmmuv500_tcu_testbus_init(&data->smmu);
ret = qsmmuv500_read_actlr_tbl(data);
if (ret)
return ERR_PTR(ret);
INIT_WORK(&data->outstanding_tnx_work,
qsmmuv500_log_outstanding_transactions);
ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
if (ret)
return ERR_PTR(ret);
/* Attempt to register child devices */
ret = device_for_each_child(dev, data, qsmmuv500_tbu_register);
if (ret)
return ERR_PTR(-EPROBE_DEFER);
devm_kfree(smmu->dev, smmu);
return &data->smmu;
}
struct arm_smmu_device *qcom_smmu_impl_init(struct arm_smmu_device *smmu)
{
struct qcom_smmu *qsmmu;
qsmmu = devm_kzalloc(smmu->dev, sizeof(*qsmmu), GFP_KERNEL);
if (!qsmmu)
return ERR_PTR(-ENOMEM);
qsmmu->smmu = *smmu;
qsmmu->smmu.impl = &qcom_smmu_impl;
devm_kfree(smmu->dev, smmu);
return &qsmmu->smmu;
}
struct arm_smmu_device *qsmmuv2_impl_init(struct arm_smmu_device *smmu)
{
struct device *dev = smmu->dev;
struct qsmmuv2_archdata *data;
struct platform_device *pdev;
int ret;
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
if (!data)
return ERR_PTR(-ENOMEM);
pdev = to_platform_device(dev);
spin_lock_init(&data->atos_lock);
data->smmu = *smmu;
data->smmu.impl = &qsmmuv2_impl;
ret = arm_smmu_parse_impl_def_registers(&data->smmu);
if (ret)
return ERR_PTR(ret);
devm_kfree(smmu->dev, smmu);
return &data->smmu;
}
|