aboutsummaryrefslogtreecommitdiffstats
path: root/security/tomoyo/.gitignore
blob: 5caf1a6f59078d653f967341eb99fa962d730a8d (plain) (blame)
1
2
builtin-policy.h
policy/
pan class='sha1'>067bc2193
0dbf3f8e3
3eb739820
128cf4517















3eb739820
128cf4517







3eb739820
128cf4517
3eb739820
128cf4517
3eb739820
128cf4517




3eb739820
128cf4517






04d9e5a86
128cf4517










3eb739820




128cf4517



3eb739820

128cf4517








04d9e5a86








128cf4517















067bc2193
128cf4517






















83fdd8c8b
128cf4517

3eb739820
5fe851069
128cf4517





























65c4e67ad
128cf4517



















42edf20a3
128cf4517
971516676
128cf4517







ab07e9f87
128cf4517
ab07e9f87
128cf4517



a6dbae971
128cf4517







128cf4517

4e60f296e
128cf4517



128cf4517























1fce2195d
128cf4517


128cf4517
067bc2193





















1fce2195d




42edf20a3
1fce2195d



42edf20a3
1fce2195d

















0ef8c1e1b

1fce2195d





128cf4517






42edf20a3
128cf4517




42edf20a3
128cf4517




42edf20a3
128cf4517
1fce2195d
128cf4517

1fce2195d



128cf4517


982d85e6c




42edf20a3
982d85e6c
42edf20a3
982d85e6c


1fce2195d







7f3e23f8c
1fce2195d
7f3e23f8c

1fce2195d


128cf4517







42edf20a3
128cf4517



42edf20a3
128cf4517

42edf20a3
128cf4517
1fce2195d
128cf4517












a6dbae971
128cf4517




128cf4517
































3eb739820
128cf4517

128cf4517

83fdd8c8b
5fe851069
eb9b5cc23


128cf4517





971516676





























128cf4517














971516676















128cf4517























971516676





























128cf4517









65c4e67ad
128cf4517


9d20b4601
128cf4517





1fce2195d
128cf4517
128cf4517






971516676









65c4e67ad
971516676

















ab07e9f87
128cf4517







42edf20a3
128cf4517







42edf20a3
128cf4517

42edf20a3
128cf4517
42edf20a3
128cf4517

04d9e5a86



d42541ff0

04d9e5a86

d42541ff0

04d9e5a86

d42541ff0

04d9e5a86

d42541ff0

04d9e5a86

d42541ff0

04d9e5a86

d42541ff0

04d9e5a86

d42541ff0


04d9e5a86


128cf4517




04d9e5a86
128cf4517
42edf20a3
128cf4517

04d9e5a86



d42541ff0
04d9e5a86


128cf4517
128cf4517
d42541ff0







ab07e9f87
42edf20a3
ab07e9f87
128cf4517

42edf20a3
128cf4517









42edf20a3
128cf4517


42edf20a3
128cf4517






971516676

128cf4517
42edf20a3
128cf4517




971516676
128cf4517
971516676




















128cf4517
42edf20a3
128cf4517


067bc2193





0dbf3f8e3
067bc2193



d987d6892
128cf4517




067bc2193
3eb739820
128cf4517

42edf20a3
410329b17

128cf4517



067bc2193



























128cf4517

3eb739820
128cf4517



3eb739820
128cf4517







22c5861cf

65c4e67ad

128cf4517
128cf4517


410329b17
128cf4517





42edf20a3
d987d6892
128cf4517











42edf20a3
128cf4517

9d20b4601
42edf20a3
128cf4517





65c4e67ad


128cf4517




42edf20a3
128cf4517







65c4e67ad









128cf4517




42edf20a3
128cf4517




42edf20a3
128cf4517


128cf4517






















3eb739820
128cf4517





3eb739820
128cf4517

3eb739820
128cf4517
3eb739820


128cf4517
3eb739820


128cf4517
3eb739820
128cf4517
3eb739820







128cf4517
3eb739820
128cf4517























5fe851069



128cf4517






5fe851069






















04d9e5a86




5fe851069
04d9e5a86
5fe851069
04d9e5a86
5fe851069
















65c4e67ad
5fe851069



















83fdd8c8b


5fe851069














































04d9e5a86

32232a71e



04d9e5a86
5fe851069
04d9e5a86
5fe851069
04d9e5a86
5fe851069
















65c4e67ad
5fe851069



















83fdd8c8b




5fe851069






















128cf4517








6640ecb8a
128cf4517


6640ecb8a
128cf4517

















3eb739820
128cf4517
3eb739820
128cf4517
3eb739820

eb9b5cc23

3eb739820
eb9b5cc23
128cf4517


04d9e5a86



eb9b5cc23
3eb739820
04d9e5a86
128cf4517




eb9b5cc23

7298260c8





eb9b5cc23
128cf4517









65c4e67ad
128cf4517



83fdd8c8b

128cf4517


















3eb739820
128cf4517




83fdd8c8b

128cf4517


83fdd8c8b


128cf4517
83fdd8c8b
eb9b5cc23




128cf4517


















971516676
128cf4517
971516676















971516676
971516676






















































61daa77b8
971516676










61daa77b8
971516676
61daa77b8



971516676
61daa77b8
971516676


971516676

7298260c8




971516676











65c4e67ad
971516676





























































128cf4517







128cf4517







128cf4517










42edf20a3
128cf4517





a6dbae971
42edf20a3
128cf4517






















e4eb6f8ac
128cf4517















2b7e74fca
128cf4517


04d9e5a86







32232a71e
04d9e5a86




128cf4517







c77da8866
04d9e5a86
1fce2195d
128cf4517
































04d9e5a86
897263df1








1fce2195d










128cf4517


04d9e5a86
128cf4517



















128cf4517


4e60f296e





128cf4517
128cf4517
128cf4517
0ef8c1e1b

128cf4517


1fce2195d
128cf4517












04d9e5a86
128cf4517





a6dbae971
128cf4517
42edf20a3
ab07e9f87

128cf4517




04d9e5a86
1fce2195d










04d9e5a86

128cf4517



971516676
5fe851069

f069180d6
5fe851069




78ed31e28
128cf4517




971516676
5fe851069

d987d6892

128cf4517


d42541ff0



ab07e9f87

d42541ff0

ab07e9f87

d42541ff0



128cf4517





















ab07e9f87



128cf4517






ab07e9f87

128cf4517

ab07e9f87





128cf4517



128cf4517




128cf4517







83fdd8c8b

128cf4517

3eb739820
128cf4517
128cf4517






128cf4517




3eb739820
128cf4517
128cf4517
83fdd8c8b



128cf4517


128cf4517


128cf4517



503df6e16
128cf4517














fb45c368e










128cf4517






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011


                                                       
                                                                     














                                                                            













                                  
                       
                                   
                          
                        
                                              
                                             








                                                         


                                                         






                                                         
                                                    
                                                   
                                                       















                                                       
                                                       







                                                     
                                                     
                                                 
                                                    
                                                  
                                                    




                                                       
                                                         






                                                         
                                                    










                                                         




                                                          



                                                  

                                                    








                                                    








                                                                                                                             















                                                                             
                                                   






















                                                                          
                                      

                                 
                               
                                      





























                                                                           
                                                                



















                                                              
                                     
                                     
                                       







                                                       
                                                                     
                                            
                                        



                                                  
                     







                                                    

                                                    
                                  



                                             























                                                                              
                                         


                                                                            
 





















                                                                   




                                                             
                                                 



                                                                        
                                                      

















                                                                             

                                           





                                                  






                                                                  
                                                 




                                                                  
                                                                      




                                                
                                                      
 
                                                

 



                                               


                            




                                                               
                                                         
                                                               
                                                              


                                                               







                                                                                         
                      
                                                               

                            


                                                        







                                                     
                                                 



                                                               
                                                              

                              
                                                      
 
                                                  












                                                                     
                                




                                                  
































                                                                             
                                                                  

                                                 

                                                                    
                                                                                
                                                                                


                                                                     





                                                                     





























                                                                                        














                                                               















                                                                               























                                                                            





























                                                                               









                                                                             
                                                            


                               
                                                    





                                                                         
                                                
 






                                                             









                                                                             
                                                            

















                                                                              
                                                 







                                                                         
                                                 







                                                                    
                                                              

                                                 
                                                         
         
                                                      

 



                                                                                               

                                                                       

                                                    

                                                                               

                                             

                                                                                

                                             

                                                                            

                                            

                                                                         

                                              

                                                                           

                        


                                                                               


         




                                                       
                                
 
                                                 

                                                         



                                                                 
                                              


                                                                         
                                                   
                                                                                      







                                                                                
                                                
                                                              
                                   

         
                                                      









                                                                            
                                                 


                                                                        
                                                      






                                                              

                             
 
                                                 




                                                          
                         
                                        




















                                                                                 
    
                                                      


               





                                                                 
                                               



                                                           
                                                       




                                                              
                                         
                                 

                      
                                                 

                                             



                                   



























                                                                      

                                                         
                                                             



                                                                       
                                                                     







                                                              

                                                      

                                                           
         


                                 
             





                                                                    
                                                      
                 











                                                               
                                                 

                                                     
                                  
                                                              





                                                                  


                                                                           




                                                                           
                                                                      







                                                                 









                                                                             




                                                                           
                                                                      




                                                                    
                                                      


                   






















                                                                     
                             





                                                               
                                          

                                                                              
                                
                              


                                                            
                                          


                                                            
                    
                                                             
         







                                                    
            
                                                     























                                                                                



                                                              






                                                                          






















                                                                    




                                                             
                                      
                                                 
                                                       
                                                 
















                                                                         
                                              



















                                                                        


                                                               














































                                                                           

                                                         



                                                              
 
                                      
                                                 
                                                       
                                                 
















                                                                         
                                              



















                                                                        




                                                        






















                                                         








                                                                          
                                                             


                                               
                                             

















                                                                             
                                            
                                     
                                                      
                                                               

                                         

                                                              
 
                                      


                                               



                                                             
 
                                                               
                                                 




                                                                

                                                            





                                                                
 









                                                                         
                                              



                                               

                               


















                                                                                
                                                                           




                                                          

                                                                     


                                                          


                                                                       
                 
 




                                                                            


















                                                                       
                                
                                                        















                                                                              
                            






















































                                                                                 
                                      










                                                               
                                                                
 



                                                             
                                                               
                                                 


                                                       

                                                            




                                                                











                                                                            
                                              





























































                                                                              







                                                              







                                                              










                                                               
                                                 





                                                              
                           
                                                      






















                                                                            
                                                  















                                                                      
            


          







                                                                                               
                                                                      




                                                         







                                                        
                               
                                
                                                       
































                                                                                         
 








                                                                     










                                                                         


                                                                                  
                                               



















                                                                            


                                                





                                                                  
 
                                        
 

                                       


                                                                   
                      












                                                                                
 





                                                         
                                   
 
                                               

                                                              




                                                     
 










                                                               

                                                         



                                                         
                                                        

                                                        
 




                                                                  
                                     




                                                                       
                                                                         

                                                                         

                                                                     


                                                                     



                                                                               

                                                                  

                                           

                                                                              



                                     





















                                                                        



                                                                   






                                                            

                                      

                                                    





                                                            



                                           




                                                      







                                                                           

                                                                 

                                                                            
                                                                         
         






                                                      




                                                                          
                                                                         
                                                                           
                                                                           



                                                                            


                                                               


                 



                                                       
                                                                                














                                                                   










                                                            






                                                             
/*
 * DMA driver for Nvidia's Tegra186 GPC DMA controller.
 *
 * Copyright (c) 2014-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
#include <linux/err.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_dma.h>
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/slab.h>
#include <linux/tegra_pm_domains.h>
#include <linux/version.h>
#include <linux/reset.h>
#include <linux/platform/tegra/tegra-mc-sid.h>
#include <dt-bindings/memory/tegra-swgroup.h>

#include "dmaengine.h"

/* CSR register */
#define TEGRA_GPCDMA_CHAN_CSR			0x00
#define TEGRA_GPCDMA_CSR_ENB			BIT(31)
#define TEGRA_GPCDMA_CSR_IE_EOC			BIT(30)
#define TEGRA_GPCDMA_CSR_ONCE			BIT(27)
#define TEGRA_GPCDMA_CSR_FC_MODE_NO_MMIO	(0 << 24)
#define TEGRA_GPCDMA_CSR_FC_MODE_ONE_MMIO	(1 << 24)
#define TEGRA_GPCDMA_CSR_FC_MODE_TWO_MMIO	(2 << 24)
#define TEGRA_GPCDMA_CSR_FC_MODE_FOUR_MMIO	(3 << 24)
#define TEGRA_GPCDMA_CSR_DMA_IO2MEM_NO_FC	(0 << 21)
#define TEGRA_GPCDMA_CSR_DMA_IO2MEM_FC		(1 << 21)
#define TEGRA_GPCDMA_CSR_DMA_MEM2IO_NO_FC	(2 << 21)
#define TEGRA_GPCDMA_CSR_DMA_MEM2IO_FC		(3 << 21)
#define TEGRA_GPCDMA_CSR_DMA_MEM2MEM		(4 << 21)
#define TEGRA_GPCDMA_CSR_DMA_FIXED_PAT		(6 << 21)
#define TEGRA_GPCDMA_CSR_REQ_SEL_SHIFT		16
#define TEGRA_GPCDMA_CSR_REQ_SEL_MASK		0x1F
#define TEGRA_GPCDMA_CSR_REQ_SEL_UNUSED		0x4
#define TEGRA_GPCDMA_CSR_IRQ_MASK		BIT(15)
#define TEGRA_GPCDMA_CSR_WEIGHT_SHIFT		10

/* STATUS register */
#define TEGRA_GPCDMA_CHAN_STATUS		0x004
#define TEGRA_GPCDMA_STATUS_BUSY		BIT(31)
#define TEGRA_GPCDMA_STATUS_ISE_EOC		BIT(30)
#define TEGRA_GPCDMA_STATUS_PING_PONG		BIT(28)
#define TEGRA_GPCDMA_STATUS_DMA_ACTIVITY	BIT(27)
#define TEGRA_GPCDMA_STATUS_CHANNEL_PAUSE	BIT(26)
#define TEGRA_GPCDMA_STATUS_CHANNEL_RX		BIT(25)
#define TEGRA_GPCDMA_STATUS_CHANNEL_TX		BIT(24)
#define TEGRA_GPCDMA_STATUS_IRQ_INTR_STA	BIT(23)
#define TEGRA_GPCDMA_STATUS_IRQ_STA		BIT(21)
#define TEGRA_GPCDMA_STATUS_IRQ_TRIG_STA	BIT(20)

#define TEGRA_GPCDMA_CHAN_CSRE			0x008
#define TEGRA_GPCDMA_CHAN_CSRE_PAUSE		BIT(31)

/* Source address */
#define TEGRA_GPCDMA_CHAN_SRC_PTR		0x00C

/* Destination address */
#define TEGRA_GPCDMA_CHAN_DST_PTR		0x010

/* High address pointer */
#define TEGRA_GPCDMA_CHAN_HIGH_ADDR_PTR		0x014
#define TEGRA_GPCDMA_HIGH_ADDR_SCR_PTR_SHIFT	0
#define TEGRA_GPCDMA_HIGH_ADDR_SCR_PTR_MASK	0xFF
#define TEGRA_GPCDMA_HIGH_ADDR_DST_PTR_SHIFT	16
#define TEGRA_GPCDMA_HIGH_ADDR_DST_PTR_MASK	0xFF

/* MC sequence register */
#define TEGRA_GPCDMA_CHAN_MCSEQ		0x18
#define TEGRA_GPCDMA_MCSEQ_DATA_SWAP		BIT(31)
#define TEGRA_GPCDMA_MCSEQ_REQ_COUNT_SHIFT	25
#define TEGRA_GPCDMA_MCSEQ_BURST_2		(0 << 23)
#define TEGRA_GPCDMA_MCSEQ_BURST_16		(3 << 23)
#define TEGRA_GPCDMA_MCSEQ_WRAP1_SHIFT		20
#define TEGRA_GPCDMA_MCSEQ_WRAP0_SHIFT		17
#define TEGRA_GPCDMA_MCSEQ_WRAP_NONE		0
#define TEGRA_GPCDMA_MCSEQ_MC_PROT_SHIFT	14
#define TEGRA_GPCDMA_MCSEQ_STREAM_ID1_SHIFT	7
#define TEGRA_GPCDMA_MCSEQ_STREAM_ID0_SHIFT	0
#define TEGRA_GPCDMA_MCSEQ_STREAM_ID_MASK	0x7F


/* MMIO sequence register */
#define TEGRA_GPCDMA_CHAN_MMIOSEQ		0x01c
#define TEGRA_GPCDMA_MMIOSEQ_DBL_BUF		BIT(31)
#define TEGRA_GPCDMA_MMIOSEQ_BUS_WIDTH_8	(0 << 28)
#define TEGRA_GPCDMA_MMIOSEQ_BUS_WIDTH_16	(1 << 28)
#define TEGRA_GPCDMA_MMIOSEQ_BUS_WIDTH_32	(2 << 28)
#define TEGRA_GPCDMA_MMIOSEQ_BUS_WIDTH_64	(3 << 28)
#define TEGRA_GPCDMA_MMIOSEQ_BUS_WIDTH_128	(4 << 28)
#define TEGRA_GPCDMA_MMIOSEQ_DATA_SWAP		BIT(27)
#define TEGRA_GPCDMA_MMIOSEQ_BURST_1		(0 << 23)
#define TEGRA_GPCDMA_MMIOSEQ_BURST_2		(1 << 23)
#define TEGRA_GPCDMA_MMIOSEQ_BURST_4		(3 << 23)
#define TEGRA_GPCDMA_MMIOSEQ_BURST_8		(7 << 23)
#define TEGRA_GPCDMA_MMIOSEQ_BURST_16		(15 << 23)
#define TEGRA_GPCDMA_MMIOSEQ_MASTER_ID_SHIFT	19
#define TEGRA_GPCDMA_MMIOSEQ_WRAP_WORD_SHIFT	16
#define TEGRA_GPCDMA_MMIOSEQ_MMIO_PROT_SHIFT	7

/* Channel WCOUNT */
#define TEGRA_GPCDMA_CHAN_WCOUNT		0x20

/* Transfer count */
#define TEGRA_GPCDMA_CHAN_XFER_COUNT		0x24

/* DMA byte count status */
#define TEGRA_GPCDMA_CHAN_DMA_BYTE_STATUS	0x28

/* Error Status Register */
#define TEGRA_GPCDMA_CHAN_ERR_STATUS		0x30
#define TEGRA_GPCDMA_CHAN_ERR_TYPE_SHIFT	(8)
#define TEGRA_GPCDMA_CHAN_ERR_TYPE_MASK		(0xF)
#define TEGRA_GPCDMA_CHAN_ERR_TYPE(err)		((err >> TEGRA_GPCDMA_CHAN_ERR_TYPE_SHIFT) & TEGRA_GPCDMA_CHAN_ERR_TYPE_MASK)
#define TEGRA_DMA_BM_FIFO_FULL_ERR		(0xF)
#define TEGRA_DMA_PERIPH_FIFO_FULL_ERR		(0xE)
#define TEGRA_DMA_PERIPH_ID_ERR			(0xD)
#define TEGRA_DMA_STREAM_ID_ERR			(0xC)
#define TEGRA_DMA_MC_SLAVE_ERR			(0xB)
#define TEGRA_DMA_MMIO_SLAVE_ERR		(0xA)

/* Fixed Pattern */
#define TEGRA_GPCDMA_CHAN_FIXED_PATTERN		0x34

#define TEGRA_GPCDMA_CHAN_TZ			0x38
#define TEGRA_GPCDMA_CHAN_TZ_MMIO_PROT_1	BIT(0)
#define TEGRA_GPCDMA_CHAN_TZ_MC_PROT_1		BIT(1)

#define TEGRA_GPCDMA_CHAN_SPARE			0x3c
#define TEGRA_GPCDMA_CHAN_SPARE_EN_LEGACY_FC	BIT(16)

/*
 * If any burst is in flight and DMA paused then this is the time to complete
 * on-flight burst and update DMA status register.
 */
#define TEGRA_GPCDMA_BURST_COMPLETE_TIME	20
#define TEGRA_GPCDMA_BURST_COMPLETION_TIMEOUT	100

/* Channel base address offset from GPCDMA base address */
#define TEGRA_GPCDMA_CHANNEL_BASE_ADD_OFFSET	0x10000

struct tegra_dma;

/*
 * tegra_dma_chip_data Tegra chip specific DMA data
 * @nr_channels: Number of channels available in the controller.
 * @channel_reg_size: Channel register size.
 * @max_dma_count: Maximum DMA transfer count supported by DMA controller.
 */
struct tegra_dma_chip_data {
	int nr_channels;
	int channel_reg_size;
	int max_dma_count;
};

/* DMA channel registers */
struct tegra_dma_channel_regs {
	unsigned long	csr;
	unsigned long	src_ptr;
	unsigned long	dst_ptr;
	unsigned long	high_addr_ptr;
	unsigned long	mc_seq;
	unsigned long	mmio_seq;
	unsigned long	wcount;
	unsigned long	fixed_pattern;
};

/*
 * tegra_dma_sg_req: Dma request details to configure hardware. This
 * contains the details for one transfer to configure DMA hw.
 * The client's request for data transfer can be broken into multiple
 * sub-transfer as per requester details and hw support.
 * This sub transfer get added in the list of transfer and point to Tegra
 * DMA descriptor which manages the transfer details.
 */
struct tegra_dma_sg_req {
	struct tegra_dma_channel_regs	ch_regs;
	int				req_len;
	bool				configured;
	bool				skipped;
	bool				last_sg;
	bool				half_done;
	struct list_head		node;
	struct tegra_dma_desc		*dma_desc;
};

/*
 * tegra_dma_desc: Tegra DMA descriptors which manages the client requests.
 * This descriptor keep track of transfer status, callbacks and request
 * counts etc.
 */
struct tegra_dma_desc {
	struct dma_async_tx_descriptor	txd;
	int				bytes_requested;
	int				bytes_transferred;
	u64				total_bytes_transferred;
	enum dma_status			dma_status;
	struct list_head		node;
	struct list_head		tx_list;
	struct list_head		cb_node;
	int				cb_count;
};

struct tegra_dma_channel;

typedef void (*dma_isr_handler)(struct tegra_dma_channel *tdc,
				bool to_terminate);

/* tegra_dma_channel: Channel specific information */
struct tegra_dma_channel {
	struct dma_chan		dma_chan;
	char			name[30];
	bool			config_init;
	int			id;
	int			irq;
	unsigned long		chan_base_offset;
	raw_spinlock_t		lock;
	bool			busy;
	bool			cyclic;
	struct tegra_dma	*tdma;

	/* Different lists for managing the requests */
	struct list_head	free_sg_req;
	struct list_head	pending_sg_req;
	struct list_head	free_dma_desc;
	struct list_head	cb_desc;

	/* ISR handler and tasklet for bottom half of isr handling */
	dma_isr_handler		isr_handler;
	struct tasklet_struct	tasklet;
	dma_async_tx_callback	callback;
	void			*callback_param;

	/* Channel-slave specific configuration */
	int slave_id;
	struct dma_slave_config dma_sconfig;
	struct tegra_dma_channel_regs	channel_reg;
};

/* tegra_dma: Tegra DMA specific information */
struct tegra_dma {
	struct dma_device		dma_dev;
	struct device			*dev;
	void __iomem			*base_addr;
	const struct tegra_dma_chip_data *chip_data;
	struct reset_control *rst;
	/* Last member of the structure */
	struct tegra_dma_channel channels[0];
};

static inline void tdc_write(struct tegra_dma_channel *tdc,
		u32 reg, u32 val)
{
	writel(val, tdc->tdma->base_addr + tdc->chan_base_offset + reg);
}

static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg)
{
	return readl(tdc->tdma->base_addr + tdc->chan_base_offset + reg);
}

static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc)
{
	return container_of(dc, struct tegra_dma_channel, dma_chan);
}

static inline struct tegra_dma_desc *txd_to_tegra_dma_desc(
		struct dma_async_tx_descriptor *td)
{
	return container_of(td, struct tegra_dma_desc, txd);
}

static inline struct device *tdc2dev(struct tegra_dma_channel *tdc)
{
	return tdc->dma_chan.device->dev;
}

static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);

static void tegra_dma_dump_chan_regs(struct tegra_dma_channel *tdc)
{
	pr_info("DMA Channel %d name %s register dump:\n",
		tdc->id, tdc->name);
	pr_info("CSR %x STA %x CSRE %x SRC %x DST %x\n",
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_CSR),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_CSRE),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_SRC_PTR),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_DST_PTR)
	);
	pr_info("MCSEQ %x IOSEQ %x WCNT %x XFER %x BSTA %x\n",
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_MCSEQ),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_MMIOSEQ),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_WCOUNT),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_XFER_COUNT),
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_DMA_BYTE_STATUS)
	);
	pr_info("DMA ERR_STA %x\n",
		tdc_read(tdc, TEGRA_GPCDMA_CHAN_ERR_STATUS));
}

static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
		struct tegra_dma_desc *dma_desc)
{
	unsigned long flags;

	raw_spin_lock_irqsave(&tdc->lock, flags);
	if (!list_empty(&dma_desc->tx_list))
		list_splice_init(&dma_desc->tx_list, &tdc->free_sg_req);
	dma_desc->txd.flags = DMA_CTRL_ACK;
	list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
	raw_spin_unlock_irqrestore(&tdc->lock, flags);
}

static struct tegra_dma_desc *tegra_dma_desc_alloc(
		struct tegra_dma_channel *tdc, bool prealloc)
{
	struct tegra_dma_desc *dma_desc;

	BUG_ON(tdc2dev(tdc) == NULL);

	dma_desc = devm_kzalloc(tdc2dev(tdc), sizeof(*dma_desc), GFP_ATOMIC);
	if (!dma_desc) {
		dev_err(tdc2dev(tdc), "dma_desc alloc failed\n");
		return NULL;
	}

	dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan);
	dma_desc->txd.tx_submit = tegra_dma_tx_submit;

	INIT_LIST_HEAD(&dma_desc->tx_list);

	if (prealloc)
		tegra_dma_desc_put(tdc, dma_desc);

	return dma_desc;
}

/* Get DMA desc from free list, if not there then allocate it.  */
static struct tegra_dma_desc *tegra_dma_desc_get(
		struct tegra_dma_channel *tdc)
{
	struct tegra_dma_desc *dma_desc;
	unsigned long flags;

	raw_spin_lock_irqsave(&tdc->lock, flags);

	/* Do not allocate if desc are waiting for ack */
	list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
		if (async_tx_test_ack(&dma_desc->txd)) {
			list_del(&dma_desc->node);
			raw_spin_unlock_irqrestore(&tdc->lock, flags);
			dma_desc->txd.flags = 0;
			return dma_desc;
		}
	}

	raw_spin_unlock_irqrestore(&tdc->lock, flags);

	return tegra_dma_desc_alloc(tdc, false);
}

static void tegra_dma_sg_req_put(
		struct tegra_dma_channel *tdc,
		struct tegra_dma_sg_req *sgreq,
		bool lock)
{
	unsigned long flags;

	if (lock) {
		/*
		 * to make sparse happy, I call lock and unlock
		 * on the same level, without conditionals
		 */
		raw_spin_lock_irqsave(&tdc->lock, flags);
		list_add_tail(&sgreq->node, &tdc->free_sg_req);
		raw_spin_unlock_irqrestore(&tdc->lock, flags);
	} else {
		list_add_tail(&sgreq->node, &tdc->free_sg_req);
	}
}

static struct tegra_dma_sg_req *tegra_dma_sg_req_alloc(
		struct tegra_dma_channel *tdc,
		bool prealloc)
{
	struct tegra_dma_sg_req *sg_req = NULL;
	sg_req = devm_kzalloc(tdc2dev(tdc), sizeof(struct tegra_dma_sg_req), GFP_ATOMIC);
	if (!sg_req) {
		dev_err(tdc2dev(tdc), "sg_req alloc failed\n");
		return NULL;
	}
	if (prealloc)
		tegra_dma_sg_req_put(tdc, sg_req, true);
	return sg_req;
}

static struct tegra_dma_sg_req *tegra_dma_sg_req_get(
		struct tegra_dma_channel *tdc)
{
	struct tegra_dma_sg_req *sg_req = NULL;
	unsigned long flags;

	raw_spin_lock_irqsave(&tdc->lock, flags);
	if (!list_empty(&tdc->free_sg_req)) {
		sg_req = list_first_entry(&tdc->free_sg_req,
					typeof(*sg_req), node);
		list_del(&sg_req->node);
		raw_spin_unlock_irqrestore(&tdc->lock, flags);
		return sg_req;
	}
	raw_spin_unlock_irqrestore(&tdc->lock, flags);

	return tegra_dma_sg_req_alloc(tdc, false);
}

static int tegra_dma_slave_config(struct dma_chan *dc,
		struct dma_slave_config *sconfig)
{
	struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);

	if (!list_empty(&tdc->pending_sg_req)) {
		dev_err(tdc2dev(tdc), "Configuration not allowed\n");
		return -EBUSY;
	}

	memcpy(&tdc->dma_sconfig, sconfig, sizeof(*sconfig));
	if (tdc->slave_id == -1)
		tdc->slave_id = sconfig->slave_id;
	tdc->config_init = true;
	return 0;
}

static void tegra_dma_resume(struct tegra_dma_channel *tdc)
{
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSRE, 0);
}

static void tegra_dma_stop(struct tegra_dma_channel *tdc)
{
	u32 csr;
	u32 status;

	/* Disable interrupts */
	csr = tdc_read(tdc, TEGRA_GPCDMA_CHAN_CSR);
	csr &= ~TEGRA_GPCDMA_CSR_IE_EOC;
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSR, csr);

	/* Disable DMA */
	csr &= ~TEGRA_GPCDMA_CSR_ENB;
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSR, csr);

	/* Clear interrupt status if it is there */
	status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);
	if (status & TEGRA_GPCDMA_STATUS_ISE_EOC) {
		dev_dbg(tdc2dev(tdc), "%s():clearing interrupt\n", __func__);
		tdc_write(tdc, TEGRA_GPCDMA_CHAN_STATUS, status);
	}
	tdc->busy = false;
}

static void tegra_dma_start(struct tegra_dma_channel *tdc,
		struct tegra_dma_sg_req *sg_req)
{
	struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs;

	tdc_write(tdc, TEGRA_GPCDMA_CHAN_WCOUNT, ch_regs->wcount);

	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSR, 0);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_SRC_PTR, ch_regs->src_ptr);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_DST_PTR, ch_regs->dst_ptr);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_HIGH_ADDR_PTR, ch_regs->high_addr_ptr);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_FIXED_PATTERN, ch_regs->fixed_pattern);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_MMIOSEQ, ch_regs->mmio_seq);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_MCSEQ, ch_regs->mc_seq);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSR, ch_regs->csr);

	/* Start DMA */
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSR,
				ch_regs->csr | TEGRA_GPCDMA_CSR_ENB);
}

static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
		struct tegra_dma_sg_req *nsg_req)
{
	unsigned long status;

	status  = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);

	/*
	 * If interrupt is pending then do nothing as the ISR will handle
	 * the programing for new request.
	 */
	if (status & TEGRA_GPCDMA_STATUS_ISE_EOC) {
		dev_err(tdc2dev(tdc),
			"Skipping new configuration as interrupt is pending\n");
		nsg_req->skipped = true;
		tegra_dma_resume(tdc);
		return;
	}

	/* Safe to program new configuration */
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_SRC_PTR, nsg_req->ch_regs.src_ptr);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_DST_PTR, nsg_req->ch_regs.dst_ptr);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_HIGH_ADDR_PTR, nsg_req->ch_regs.high_addr_ptr);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_WCOUNT, nsg_req->ch_regs.wcount);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSR,
		nsg_req->ch_regs.csr | TEGRA_GPCDMA_CSR_ENB);
	nsg_req->configured = true;
	nsg_req->skipped = false;
}

static void tdc_start_head_req(struct tegra_dma_channel *tdc)
{
	struct tegra_dma_sg_req *sg_req;

	if (list_empty(&tdc->pending_sg_req))
		return;

	sg_req = list_first_entry(&tdc->pending_sg_req,
					typeof(*sg_req), node);
	tegra_dma_start(tdc, sg_req);
	sg_req->configured = true;
	sg_req->skipped = false;
	tdc->busy = true;
}

static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
{
	struct tegra_dma_sg_req *hsgreq;
	struct tegra_dma_sg_req *hnsgreq;

	if (list_empty(&tdc->pending_sg_req))
		return;

	hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
	if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
		hnsgreq = list_first_entry(&hsgreq->node,
					typeof(*hnsgreq), node);
		tegra_dma_configure_for_next(tdc, hnsgreq);
	}
}

static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
{
	struct tegra_dma_sg_req *sgreq;
	struct tegra_dma_desc *dma_desc;

	while (!list_empty(&tdc->pending_sg_req)) {
		sgreq = list_first_entry(&tdc->pending_sg_req,
						typeof(*sgreq), node);
		list_move_tail(&sgreq->node, &tdc->free_sg_req);
		if (sgreq->last_sg) {
			dma_desc = sgreq->dma_desc;
			dma_desc->dma_status = DMA_ERROR;
			list_add_tail(&dma_desc->node, &tdc->free_dma_desc);

			/* Add in cb list if it is not there. */
			if (!dma_desc->cb_count)
				list_add_tail(&dma_desc->cb_node,
							&tdc->cb_desc);
			dma_desc->cb_count++;
		}
	}
	tdc->isr_handler = NULL;
}

static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
		struct tegra_dma_sg_req *last_sg_req, bool to_terminate)
{
	struct tegra_dma_sg_req *hsgreq = NULL;

	if (list_empty(&tdc->pending_sg_req)) {
		dev_err(tdc2dev(tdc), "Dma is running without req\n");
		tegra_dma_stop(tdc);
		return false;
	}

	/*
	 * Check that head req on list should be in flight.
	 * If it is not in flight then abort transfer as
	 * looping of transfer can not continue.
	 */
	hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
	if (!hsgreq->configured && !hsgreq->skipped) {
		tegra_dma_stop(tdc);
		dev_err(tdc2dev(tdc), "Error in dma transfer, aborting dma\n");
		tegra_dma_abort_all(tdc);
		return false;
	}

	/* Configure next request */
	if (!to_terminate)
		tdc_configure_next_head_desc(tdc);
	return true;
}

static void handle_once_dma_done(struct tegra_dma_channel *tdc,
	bool to_terminate)
{
	struct tegra_dma_sg_req *sgreq;
	struct tegra_dma_desc *dma_desc;

	tdc->busy = false;
	sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
	dma_desc = sgreq->dma_desc;
	dma_desc->bytes_transferred += sgreq->req_len;
	dma_desc->total_bytes_transferred += sgreq->req_len;

	list_del(&sgreq->node);
	if (sgreq->last_sg) {
		dma_desc->dma_status = DMA_COMPLETE;
		dma_cookie_complete(&dma_desc->txd);
		if (!dma_desc->cb_count)
			list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
		dma_desc->cb_count++;
		list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
	}
	tegra_dma_sg_req_put(tdc, sgreq, false);

	if (to_terminate || list_empty(&tdc->pending_sg_req))
		return;

	tdc_start_head_req(tdc);
	return;
}

static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
		bool to_terminate)
{
	struct tegra_dma_sg_req *sgreq;
	struct tegra_dma_desc *dma_desc;
	bool st;

	sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
	dma_desc = sgreq->dma_desc;
	dma_desc->bytes_transferred += sgreq->req_len;
	dma_desc->total_bytes_transferred += sgreq->req_len;

	/* Callback need to be call */
	if (!dma_desc->cb_count)
		list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
	dma_desc->cb_count++;

	/* If not last req then put at end of pending list */
	if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
		list_move_tail(&sgreq->node, &tdc->pending_sg_req);
		sgreq->configured = false;
		sgreq->skipped = false;
		st = handle_continuous_head_request(tdc, sgreq, to_terminate);
		if (!st)
			dma_desc->dma_status = DMA_ERROR;
	}
	return;
}

static void tegra_dma_tasklet(unsigned long data)
{
	struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data;
	dma_async_tx_callback callback = NULL;
	void *callback_param = NULL;
	struct tegra_dma_desc *dma_desc;
	unsigned long flags;
	int cb_count;

	raw_spin_lock_irqsave(&tdc->lock, flags);
	while (!list_empty(&tdc->cb_desc)) {
		dma_desc  = list_first_entry(&tdc->cb_desc,
					typeof(*dma_desc), cb_node);
		list_del(&dma_desc->cb_node);
		callback = dma_desc->txd.callback;
		callback_param = dma_desc->txd.callback_param;
		cb_count = dma_desc->cb_count;
		dma_desc->cb_count = 0;
		raw_spin_unlock_irqrestore(&tdc->lock, flags);
		while (cb_count-- && callback)
			callback(callback_param);
		raw_spin_lock_irqsave(&tdc->lock, flags);
	}
	raw_spin_unlock_irqrestore(&tdc->lock, flags);
}

static void tegra_dma_chan_decode_error(struct tegra_dma_channel *tdc, unsigned int err_status)
{
	switch(TEGRA_GPCDMA_CHAN_ERR_TYPE(err_status)) {
		case TEGRA_DMA_BM_FIFO_FULL_ERR:
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d bm fifo full\n", tdc->id);
		break;
		case TEGRA_DMA_PERIPH_FIFO_FULL_ERR:
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d peripheral fifo full\n", tdc->id);
		break;
		case TEGRA_DMA_PERIPH_ID_ERR:
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d illegal peripheral id\n", tdc->id);
		break;
		case TEGRA_DMA_STREAM_ID_ERR:
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d illegal stream id\n", tdc->id);
		break;
		case TEGRA_DMA_MC_SLAVE_ERR:
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d mc slave error\n", tdc->id);
		break;
		case TEGRA_DMA_MMIO_SLAVE_ERR:
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d mmio slave error\n", tdc->id);
		break;
		default:
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d security violation %x\n", tdc->id,
				err_status);
	}
}

static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
{
	struct tegra_dma_channel *tdc = dev_id;
	unsigned long status;
	unsigned long flags;
	unsigned int err_status;

	raw_spin_lock_irqsave(&tdc->lock, flags);

	status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);
	err_status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_ERR_STATUS);

	if (err_status) {
		tegra_dma_chan_decode_error(tdc, err_status);
		tegra_dma_dump_chan_regs(tdc);
		tdc_write(tdc, TEGRA_GPCDMA_CHAN_ERR_STATUS, 0xFFFFFFFF);
	}

	if (status & TEGRA_GPCDMA_STATUS_ISE_EOC) {
		tdc_write(tdc, TEGRA_GPCDMA_CHAN_STATUS, TEGRA_GPCDMA_STATUS_ISE_EOC);
		if (tdc->isr_handler)
			tdc->isr_handler(tdc, false);
		else {
			dev_err(tdc->tdma->dev,
				"GPCDMA CH%d: status %lx ISR handler absent!\n",
				tdc->id, status);
			tegra_dma_dump_chan_regs(tdc);
		}
		tasklet_schedule(&tdc->tasklet);
		raw_spin_unlock_irqrestore(&tdc->lock, flags);
		return IRQ_HANDLED;
	}

	raw_spin_unlock_irqrestore(&tdc->lock, flags);
	return IRQ_NONE;
}

static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *txd)
{
	struct tegra_dma_desc *dma_desc = txd_to_tegra_dma_desc(txd);
	struct tegra_dma_channel *tdc = to_tegra_dma_chan(txd->chan);
	unsigned long flags;
	dma_cookie_t cookie;

	raw_spin_lock_irqsave(&tdc->lock, flags);
	dma_desc->dma_status = DMA_IN_PROGRESS;
	cookie = dma_cookie_assign(&dma_desc->txd);
	list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req);
	raw_spin_unlock_irqrestore(&tdc->lock, flags);
	return cookie;
}

static void tegra_dma_issue_pending(struct dma_chan *dc)
{
	struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
	unsigned long flags;
	unsigned long status;
	int count;

	raw_spin_lock_irqsave(&tdc->lock, flags);
	if (list_empty(&tdc->pending_sg_req)) {
		dev_err(tdc2dev(tdc), "No DMA request\n");
		goto end;
	}

	if (!tdc->busy) {
		tdc_start_head_req(tdc);
		/* Continuous mode: Configure next req */
		if (tdc->cyclic) {
			/*
			 * For cyclic dma transfers, program the second transfer
			 * parameters as soon as the first dma transfer is
			 * started inorder for the dma controller to trigger the
			 * second transfer with the correct parameters. Poll
			 * for the channel busy bit and start the transfer.
			 */
			count = 20;
			do {
				status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);
				if (status & TEGRA_GPCDMA_STATUS_BUSY)
					break;
				udelay(1);
				count--;
			} while(count);
			tdc_configure_next_head_desc(tdc);
		}
	}

end:
	raw_spin_unlock_irqrestore(&tdc->lock, flags);
	return;
}

static void tegra_dma_reset_client(struct tegra_dma_channel *tdc)
{
	uint32_t csr = tdc_read(tdc, TEGRA_GPCDMA_CHAN_CSR);

	csr &= ~(TEGRA_GPCDMA_CSR_REQ_SEL_MASK <<
			TEGRA_GPCDMA_CSR_REQ_SEL_SHIFT);
	csr |= (TEGRA_GPCDMA_CSR_REQ_SEL_UNUSED
			<< TEGRA_GPCDMA_CSR_REQ_SEL_SHIFT);
	tdc_write(tdc, TEGRA_GPCDMA_CHAN_CSR, csr);
}

static int tegra_dma_terminate_all(struct dma_chan *dc)
{
	struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
	struct tegra_dma_sg_req *sgreq;
	struct tegra_dma_desc *dma_desc;
	unsigned long flags;
	unsigned long status, burst_time;
	unsigned long wcount = 0;
	bool was_busy;

	raw_spin_lock_irqsave(&tdc->lock, flags);
	if (list_empty(&tdc->pending_sg_req))
		goto empty_cblist;

	if (!tdc->busy)
		goto skip_dma_stop;

	/* Before Reading DMA status to figure out number
	 * of bytes transferred by DMA channel:
	 * Change the client associated with the DMA channel
	 * to stop DMA engine from starting any more bursts for
	 * the given client and wait for in flight bursts to complete
	 */
	tegra_dma_reset_client(tdc);

	/* Wait for in flight data transfer to finish */
	udelay(TEGRA_GPCDMA_BURST_COMPLETE_TIME);

	/* If TX/RX path is still active wait till it becomes
	 * inactive */
	burst_time = 0;
	while (burst_time < TEGRA_GPCDMA_BURST_COMPLETION_TIMEOUT) {
		status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);
		if (status & (TEGRA_GPCDMA_STATUS_CHANNEL_TX |
			TEGRA_GPCDMA_STATUS_CHANNEL_RX)) {
			udelay(5);
			burst_time += 5;
		} else
			break;
	}

	if (burst_time >= TEGRA_GPCDMA_BURST_COMPLETION_TIMEOUT) {
		pr_err("Timeout waiting for DMA burst completion!\n");
		tegra_dma_dump_chan_regs(tdc);
	}

	status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);
	wcount = tdc_read(tdc, TEGRA_GPCDMA_CHAN_XFER_COUNT);
	if (status & TEGRA_GPCDMA_STATUS_ISE_EOC) {
		dev_dbg(tdc2dev(tdc), "%s():handling isr\n", __func__);
		tdc->isr_handler(tdc, true);
		status = tdc_read(tdc, TEGRA_GPCDMA_CHAN_STATUS);
		wcount = tdc_read(tdc, TEGRA_GPCDMA_CHAN_XFER_COUNT);
	}

	was_busy = tdc->busy;