summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4u/io/iommu.c
blob: 9a73efda42e9b99fe8b9041f447d3dbc0f5ca808 (plain)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ddi_impldefs.h>
#include <sys/cmn_err.h>
#include <sys/kmem.h>
#include <sys/vmem.h>
#include <sys/sysmacros.h>

#include <sys/ddidmareq.h>
#include <sys/sysiosbus.h>
#include <sys/iommu.h>
#include <sys/iocache.h>
#include <sys/dvma.h>

#include <vm/as.h>
#include <vm/hat.h>
#include <vm/page.h>
#include <vm/hat_sfmmu.h>
#include <sys/machparam.h>
#include <sys/machsystm.h>
#include <sys/vmsystm.h>
#include <sys/iommutsb.h>

/* Useful debugging Stuff */
#include <sys/nexusdebug.h>
#include <sys/debug.h>
/* Bitfield debugging definitions for this file */
#define	IOMMU_GETDVMAPAGES_DEBUG	0x1
#define	IOMMU_DMAMAP_DEBUG		0x2
#define	IOMMU_DMAMCTL_DEBUG		0x4
#define	IOMMU_DMAMCTL_SYNC_DEBUG	0x8
#define	IOMMU_DMAMCTL_HTOC_DEBUG	0x10
#define	IOMMU_DMAMCTL_KVADDR_DEBUG	0x20
#define	IOMMU_DMAMCTL_NEXTWIN_DEBUG	0x40
#define	IOMMU_DMAMCTL_NEXTSEG_DEBUG	0x80
#define	IOMMU_DMAMCTL_MOVWIN_DEBUG	0x100
#define	IOMMU_DMAMCTL_REPWIN_DEBUG	0x200
#define	IOMMU_DMAMCTL_GETERR_DEBUG	0x400
#define	IOMMU_DMAMCTL_COFF_DEBUG	0x800
#define	IOMMU_DMAMCTL_DMA_FREE_DEBUG	0x1000
#define	IOMMU_REGISTERS_DEBUG		0x2000
#define	IOMMU_DMA_SETUP_DEBUG		0x4000
#define	IOMMU_DMA_UNBINDHDL_DEBUG	0x8000
#define	IOMMU_DMA_BINDHDL_DEBUG		0x10000
#define	IOMMU_DMA_WIN_DEBUG		0x20000
#define	IOMMU_DMA_ALLOCHDL_DEBUG	0x40000
#define	IOMMU_DMA_LIM_SETUP_DEBUG	0x80000
#define	IOMMU_FASTDMA_RESERVE		0x100000
#define	IOMMU_FASTDMA_LOAD		0x200000
#define	IOMMU_INTER_INTRA_XFER		0x400000
#define	IOMMU_TTE			0x800000
#define	IOMMU_TLB			0x1000000
#define	IOMMU_FASTDMA_SYNC		0x2000000

/* Turn on if you need to keep track of outstanding IOMMU usage */
/* #define	IO_MEMUSAGE */
/* Turn on to debug IOMMU unmapping code */
/* #define	IO_MEMDEBUG */

static struct dvma_ops iommu_dvma_ops = {
	DVMAO_REV,
	iommu_dvma_kaddr_load,
	iommu_dvma_unload,
	iommu_dvma_sync
};

extern void *sbusp;		/* sbus soft state hook */

#define	DVMA_MAX_CACHE	65536

/*
 * This is the number of pages that a mapping request needs before we force
 * the TLB flush code to use diagnostic registers.  This value was determined
 * through a series of test runs measuring dma mapping settup performance.
 */
int tlb_flush_using_diag = 16;

int sysio_iommu_tsb_sizes[] = {
	IOMMU_TSB_SIZE_8M,
	IOMMU_TSB_SIZE_16M,
	IOMMU_TSB_SIZE_32M,
	IOMMU_TSB_SIZE_64M,
	IOMMU_TSB_SIZE_128M,
	IOMMU_TSB_SIZE_256M,
	IOMMU_TSB_SIZE_512M,
	IOMMU_TSB_SIZE_1G
};

static int iommu_map_window(ddi_dma_impl_t *, off_t, size_t);

int
iommu_init(struct sbus_soft_state *softsp, caddr_t address)
{
	int i;
	char name[40];

#ifdef DEBUG
	debug_info = 1;
#endif

	/*
	 * Simply add each registers offset to the base address
	 * to calculate the already mapped virtual address of
	 * the device register...
	 *
	 * define a macro for the pointer arithmetic; all registers
	 * are 64 bits wide and are defined as uint64_t's.
	 */

#define	REG_ADDR(b, o)	(uint64_t *)((caddr_t)(b) + (o))

	softsp->iommu_ctrl_reg = REG_ADDR(address, OFF_IOMMU_CTRL_REG);
	softsp->tsb_base_addr = REG_ADDR(address, OFF_TSB_BASE_ADDR);
	softsp->iommu_flush_reg = REG_ADDR(address, OFF_IOMMU_FLUSH_REG);
	softsp->iommu_tlb_tag = REG_ADDR(address, OFF_IOMMU_TLB_TAG);
	softsp->iommu_tlb_data = REG_ADDR(address, OFF_IOMMU_TLB_DATA);

#undef REG_ADDR

	mutex_init(&softsp->dma_pool_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&softsp->intr_poll_list_lock, NULL, MUTEX_DEFAULT, NULL);

	/* Set up the DVMA resource sizes */
	if ((softsp->iommu_tsb_cookie = iommu_tsb_alloc(softsp->upa_id)) ==
	    IOMMU_TSB_COOKIE_NONE) {
		cmn_err(CE_WARN, "%s%d: Unable to retrieve IOMMU array.",
		    ddi_driver_name(softsp->dip),
		    ddi_get_instance(softsp->dip));
		return (DDI_FAILURE);
	}
	softsp->soft_tsb_base_addr =
	    iommu_tsb_cookie_to_va(softsp->iommu_tsb_cookie);
	softsp->iommu_dvma_size =
	    iommu_tsb_cookie_to_size(softsp->iommu_tsb_cookie) <<
	    IOMMU_TSB_TO_RNG;
	softsp->iommu_dvma_base = (ioaddr_t)
	    (0 - (ioaddr_t)softsp->iommu_dvma_size);

	(void) snprintf(name, sizeof (name), "%s%d_dvma",
	    ddi_driver_name(softsp->dip), ddi_get_instance(softsp->dip));

	/*
	 * Initialize the DVMA vmem arena.
	 */
	softsp->dvma_arena = vmem_create(name,
	    (void *)(uintptr_t)softsp->iommu_dvma_base,
	    softsp->iommu_dvma_size, PAGESIZE, NULL, NULL, NULL,
	    DVMA_MAX_CACHE, VM_SLEEP);

	/* Set the limit for dvma_reserve() to 1/2 of the total dvma space */
	softsp->dma_reserve = iommu_btop(softsp->iommu_dvma_size >> 1);

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	mutex_init(&softsp->iomemlock, NULL, MUTEX_DEFAULT, NULL);
	softsp->iomem = (struct io_mem_list *)0;
#endif /* DEBUG && IO_MEMUSAGE */
	/*
	 * Get the base address of the TSB table and store it in the hardware
	 */

	/*
	 * We plan on the PROM flushing all TLB entries.  If this is not the
	 * case, this is where we should flush the hardware TLB.
	 */

	/* Set the IOMMU registers */
	(void) iommu_resume_init(softsp);

	/* check the convenient copy of TSB base, and flush write buffers */
	if (*softsp->tsb_base_addr !=
	    va_to_pa((caddr_t)softsp->soft_tsb_base_addr)) {
		iommu_tsb_free(softsp->iommu_tsb_cookie);
		return (DDI_FAILURE);
	}

	softsp->sbus_io_lo_pfn = UINT32_MAX;
	softsp->sbus_io_hi_pfn = 0;
	for (i = 0; i < sysio_pd_getnrng(softsp->dip); i++) {
		struct rangespec *rangep;
		uint64_t addr;
		pfn_t hipfn, lopfn;

		rangep = sysio_pd_getrng(softsp->dip, i);
		addr = (uint64_t)((uint64_t)rangep->rng_bustype << 32);
		addr |= (uint64_t)rangep->rng_offset;
		lopfn = (pfn_t)(addr >> MMU_PAGESHIFT);
		addr += (uint64_t)(rangep->rng_size - 1);
		hipfn = (pfn_t)(addr >> MMU_PAGESHIFT);

		softsp->sbus_io_lo_pfn = (lopfn < softsp->sbus_io_lo_pfn) ?
		    lopfn : softsp->sbus_io_lo_pfn;

		softsp->sbus_io_hi_pfn = (hipfn > softsp->sbus_io_hi_pfn) ?
		    hipfn : softsp->sbus_io_hi_pfn;
	}

	DPRINTF(IOMMU_REGISTERS_DEBUG, ("IOMMU Control reg: %p IOMMU TSB "
	    "base reg: %p IOMMU flush reg: %p TSB base addr %p\n",
	    softsp->iommu_ctrl_reg, softsp->tsb_base_addr,
	    softsp->iommu_flush_reg, softsp->soft_tsb_base_addr));

	return (DDI_SUCCESS);
}

/*
 * function to uninitialize the iommu and release the tsb back to
 * the spare pool.  See startup.c for tsb spare management.
 */

int
iommu_uninit(struct sbus_soft_state *softsp)
{
	vmem_destroy(softsp->dvma_arena);

	/* flip off the IOMMU enable switch */
	*softsp->iommu_ctrl_reg &=
		(TSB_SIZE << TSB_SIZE_SHIFT | IOMMU_DISABLE);

	iommu_tsb_free(softsp->iommu_tsb_cookie);

	return (DDI_SUCCESS);
}

/*
 * Initialize iommu hardware registers when the system is being resumed.
 * (Subset of iommu_init())
 */
int
iommu_resume_init(struct sbus_soft_state *softsp)
{
	int i;
	uint_t tsb_size;
	uint_t tsb_bytes;

	/*
	 * Reset the base address of the TSB table in the hardware
	 */
	*softsp->tsb_base_addr = va_to_pa((caddr_t)softsp->soft_tsb_base_addr);

	/*
	 * Figure out the correct size of the IOMMU TSB entries.  If we
	 * end up with a size smaller than that needed for 8M of IOMMU
	 * space, default the size to 8M.  XXX We could probably panic here
	 */
	i = sizeof (sysio_iommu_tsb_sizes) / sizeof (sysio_iommu_tsb_sizes[0])
	    - 1;

	tsb_bytes = iommu_tsb_cookie_to_size(softsp->iommu_tsb_cookie);

	while (i > 0) {
		if (tsb_bytes >= sysio_iommu_tsb_sizes[i])
			break;
		i--;
	}

	tsb_size = i;

	/* OK, lets flip the "on" switch of the IOMMU */
	*softsp->iommu_ctrl_reg = (uint64_t)(tsb_size << TSB_SIZE_SHIFT
	    | IOMMU_ENABLE | IOMMU_DIAG_ENABLE);

	return (DDI_SUCCESS);
}

void
iommu_tlb_flush(struct sbus_soft_state *softsp, ioaddr_t addr, pgcnt_t npages)
{
	volatile uint64_t tmpreg;
	volatile uint64_t *vaddr_reg, *valid_bit_reg;
	ioaddr_t hiaddr, ioaddr;
	int i, do_flush = 0;

	if (npages == 1) {
		*softsp->iommu_flush_reg = (uint64_t)addr;
		tmpreg = *softsp->sbus_ctrl_reg;
		return;
	}

	hiaddr = addr + (ioaddr_t)(npages * IOMMU_PAGESIZE);
	for (i = 0, vaddr_reg = softsp->iommu_tlb_tag,
	    valid_bit_reg = softsp->iommu_tlb_data;
	    i < IOMMU_TLB_ENTRIES; i++, vaddr_reg++, valid_bit_reg++) {
		tmpreg = *vaddr_reg;
		ioaddr = (ioaddr_t)((tmpreg & IOMMU_TLBTAG_VA_MASK) <<
		    IOMMU_TLBTAG_VA_SHIFT);

		DPRINTF(IOMMU_TLB, ("Vaddr reg 0x%p, "
		    "TLB vaddr reg %lx, IO addr 0x%x "
		    "Base addr 0x%x, Hi addr 0x%x\n",
		    vaddr_reg, tmpreg, ioaddr, addr, hiaddr));

		if (ioaddr >= addr && ioaddr <= hiaddr) {
			tmpreg = *valid_bit_reg;

			DPRINTF(IOMMU_TLB, ("Valid reg addr 0x%p, "
			    "TLB valid reg %lx\n",
			    valid_bit_reg, tmpreg));

			if (tmpreg & IOMMU_TLB_VALID) {
				*softsp->iommu_flush_reg = (uint64_t)ioaddr;
				do_flush = 1;
			}
		}
	}

	if (do_flush)
		tmpreg = *softsp->sbus_ctrl_reg;
}


/*
 * Shorthand defines
 */

#define	ALO		dma_lim->dlim_addr_lo
#define	AHI		dma_lim->dlim_addr_hi
#define	OBJSIZE		dmareq->dmar_object.dmao_size
#define	IOTTE_NDX(vaddr, base) (base + \
		(int)(iommu_btop((vaddr & ~IOMMU_PAGEMASK) - \
		softsp->iommu_dvma_base)))
/*
 * If DDI_DMA_PARTIAL flag is set and the request is for
 * less than MIN_DVMA_WIN_SIZE, it's not worth the hassle so
 * we turn off the DDI_DMA_PARTIAL flag
 */
#define	MIN_DVMA_WIN_SIZE	(128)

/* ARGSUSED */
void
iommu_remove_mappings(ddi_dma_impl_t *mp)
{
#if defined(DEBUG) && defined(IO_MEMDEBUG)
	pgcnt_t npages;
	ioaddr_t ioaddr;
	volatile uint64_t *iotte_ptr;
	ioaddr_t ioaddr = mp->dmai_mapping & ~IOMMU_PAGEOFFSET;
	pgcnt_t npages = mp->dmai_ndvmapages;
	struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;
	struct sbus_soft_state *softsp = mppriv->softsp;

#if defined(IO_MEMUSAGE)
	struct io_mem_list **prevp, *walk;
#endif /* DEBUG && IO_MEMUSAGE */

	ASSERT(softsp != NULL);
	/*
	 * Run thru the mapped entries and free 'em
	 */

	ioaddr = mp->dmai_mapping & ~IOMMU_PAGEOFFSET;
	npages = mp->dmai_ndvmapages;

#if defined(IO_MEMUSAGE)
	mutex_enter(&softsp->iomemlock);
	prevp = &softsp->iomem;
	walk = softsp->iomem;

	while (walk) {
		if (walk->ioaddr == ioaddr) {
			*prevp = walk->next;
			break;
		}

		prevp = &walk->next;
		walk = walk->next;
	}
	mutex_exit(&softsp->iomemlock);

	kmem_free(walk->pfn, sizeof (pfn_t) * (npages + 1));
	kmem_free(walk, sizeof (struct io_mem_list));
#endif /* IO_MEMUSAGE */

	iotte_ptr = IOTTE_NDX(ioaddr, softsp->soft_tsb_base_addr);

	while (npages) {
		DPRINTF(IOMMU_DMAMCTL_DEBUG,
		    ("dma_mctl: freeing ioaddr %x iotte %p\n",
		    ioaddr, iotte_ptr));
		*iotte_ptr = (uint64_t)0;	/* unload tte */
		iommu_tlb_flush(softsp, ioaddr, 1);
		npages--;
		ioaddr += IOMMU_PAGESIZE;
		iotte_ptr++;
	}
#endif /* DEBUG && IO_MEMDEBUG */
}


int
iommu_create_vaddr_mappings(ddi_dma_impl_t *mp, uintptr_t addr)
{
	pfn_t pfn;
	struct as *as = NULL;
	pgcnt_t npages;
	ioaddr_t ioaddr;
	uint_t offset;
	volatile uint64_t *iotte_ptr;
	uint64_t tmp_iotte_flag;
	int rval = DDI_DMA_MAPPED;
	struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;
	struct sbus_soft_state *softsp = mppriv->softsp;
	int diag_tlb_flush;
#if defined(DEBUG) && defined(IO_MEMUSAGE)
	struct io_mem_list *iomemp;
	pfn_t *pfnp;
#endif /* DEBUG && IO_MEMUSAGE */

	ASSERT(softsp != NULL);

	/* Set Valid and Cache for mem xfer */
	tmp_iotte_flag = IOTTE_VALID | IOTTE_CACHE | IOTTE_WRITE | IOTTE_STREAM;

	offset = (uint_t)(mp->dmai_mapping & IOMMU_PAGEOFFSET);
	npages = iommu_btopr(mp->dmai_size + offset);
	ioaddr = (ioaddr_t)(mp->dmai_mapping & ~IOMMU_PAGEOFFSET);
	iotte_ptr = IOTTE_NDX(ioaddr, softsp->soft_tsb_base_addr);
	diag_tlb_flush = npages > tlb_flush_using_diag ? 1 : 0;

	as = mp->dmai_object.dmao_obj.virt_obj.v_as;
	if (as == NULL)
		as = &kas;

	/*
	 * Set the per object bits of the TTE here. We optimize this for
	 * the memory case so that the while loop overhead is minimal.
	 */
	/* Turn on NOSYNC if we need consistent mem */
	if (mp->dmai_rflags & DDI_DMA_CONSISTENT) {
		mp->dmai_rflags |= DMP_NOSYNC;
		tmp_iotte_flag ^= IOTTE_STREAM;
	/* Set streaming mode if not consistent mem */
	} else if (softsp->stream_buf_off) {
		tmp_iotte_flag ^= IOTTE_STREAM;
	}

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	iomemp = kmem_alloc(sizeof (struct io_mem_list), KM_SLEEP);
	iomemp->rdip = mp->dmai_rdip;
	iomemp->ioaddr = ioaddr;
	iomemp->addr = addr;
	iomemp->npages = npages;
	pfnp = iomemp->pfn = kmem_zalloc(sizeof (*pfnp) * (npages + 1),
	    KM_SLEEP);
#endif /* DEBUG && IO_MEMUSAGE */
	/*
	 * Grab the mappings from the dmmu and stick 'em into the
	 * iommu.
	 */
	ASSERT(npages != 0);

	/* If we're going to flush the TLB using diag mode, do it now. */
	if (diag_tlb_flush)
		iommu_tlb_flush(softsp, ioaddr, npages);

	do {
		uint64_t iotte_flag = tmp_iotte_flag;

		/*
		 * Fetch the pfn for the DMA object
		 */

		ASSERT(as);
		pfn = hat_getpfnum(as->a_hat, (caddr_t)addr);
		ASSERT(pfn != PFN_INVALID);

		if (!pf_is_memory(pfn)) {
			/* DVMA'ing to IO space */

			/* Turn off cache bit if set */
			if (iotte_flag & IOTTE_CACHE)
				iotte_flag ^= IOTTE_CACHE;

			/* Turn off stream bit if set */
			if (iotte_flag & IOTTE_STREAM)
				iotte_flag ^= IOTTE_STREAM;

			if (IS_INTRA_SBUS(softsp, pfn)) {
				/* Intra sbus transfer */

				/* Turn on intra flag */
				iotte_flag |= IOTTE_INTRA;

				DPRINTF(IOMMU_INTER_INTRA_XFER, (
				    "Intra xfer pfnum %lx TTE %lx\n",
				    pfn, iotte_flag));
			} else {
				if (pf_is_dmacapable(pfn) == 1) {
					/*EMPTY*/
					DPRINTF(IOMMU_INTER_INTRA_XFER,
					    ("Inter xfer pfnum %lx "
					    "tte hi %lx\n",
					    pfn, iotte_flag));
				} else {
					rval = DDI_DMA_NOMAPPING;
#if defined(DEBUG) && defined(IO_MEMDEBUG)
					goto bad;
#endif /* DEBUG && IO_MEMDEBUG */
				}
			}
		}
		addr += IOMMU_PAGESIZE;

		DPRINTF(IOMMU_TTE, ("vaddr mapping: tte index %p pfn %lx "
		    "tte flag %lx addr %lx ioaddr %x\n",
		    iotte_ptr, pfn, iotte_flag, addr, ioaddr));

		/* Flush the IOMMU TLB before loading a new mapping */
		if (!diag_tlb_flush)
			iommu_tlb_flush(softsp, ioaddr, 1);

		/* Set the hardware IO TTE */
		*iotte_ptr = ((uint64_t)pfn << IOMMU_PAGESHIFT) | iotte_flag;

		ioaddr += IOMMU_PAGESIZE;
		npages--;
		iotte_ptr++;
#if defined(DEBUG) && defined(IO_MEMUSAGE)
		*pfnp = pfn;
		pfnp++;
#endif /* DEBUG && IO_MEMUSAGE */
	} while (npages != 0);

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	mutex_enter(&softsp->iomemlock);
	iomemp->next = softsp->iomem;
	softsp->iomem = iomemp;
	mutex_exit(&softsp->iomemlock);
#endif /* DEBUG && IO_MEMUSAGE */

	return (rval);

#if defined(DEBUG) && defined(IO_MEMDEBUG)
bad:
	/* If we fail a mapping, free up any mapping resources used */
	iommu_remove_mappings(mp);
	return (rval);
#endif /* DEBUG && IO_MEMDEBUG */
}


int
iommu_create_pp_mappings(ddi_dma_impl_t *mp, page_t *pp, page_t **pplist)
{
	pfn_t pfn;
	pgcnt_t npages;
	ioaddr_t ioaddr;
	uint_t offset;
	volatile uint64_t *iotte_ptr;
	uint64_t tmp_iotte_flag;
	struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;
	struct sbus_soft_state *softsp = mppriv->softsp;
	int diag_tlb_flush;
#if defined(DEBUG) && defined(IO_MEMUSAGE)
	struct io_mem_list *iomemp;
	pfn_t *pfnp;
#endif /* DEBUG && IO_MEMUSAGE */
	int rval = DDI_DMA_MAPPED;

	/* Set Valid and Cache for mem xfer */
	tmp_iotte_flag = IOTTE_VALID | IOTTE_CACHE | IOTTE_WRITE | IOTTE_STREAM;

	ASSERT(softsp != NULL);

	offset = (uint_t)(mp->dmai_mapping & IOMMU_PAGEOFFSET);
	npages = iommu_btopr(mp->dmai_size + offset);
	ioaddr = (ioaddr_t)(mp->dmai_mapping & ~IOMMU_PAGEOFFSET);
	iotte_ptr = IOTTE_NDX(ioaddr, softsp->soft_tsb_base_addr);
	diag_tlb_flush = npages > tlb_flush_using_diag ? 1 : 0;

	/*
	 * Set the per object bits of the TTE here. We optimize this for
	 * the memory case so that the while loop overhead is minimal.
	 */
	if (mp->dmai_rflags & DDI_DMA_CONSISTENT) {
		/* Turn on NOSYNC if we need consistent mem */
		mp->dmai_rflags |= DMP_NOSYNC;
		tmp_iotte_flag ^= IOTTE_STREAM;
	} else if (softsp->stream_buf_off) {
		/* Set streaming mode if not consistent mem */
		tmp_iotte_flag ^= IOTTE_STREAM;
	}

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	iomemp = kmem_alloc(sizeof (struct io_mem_list), KM_SLEEP);
	iomemp->rdip = mp->dmai_rdip;
	iomemp->ioaddr = ioaddr;
	iomemp->npages = npages;
	pfnp = iomemp->pfn = kmem_zalloc(sizeof (*pfnp) * (npages + 1),
	    KM_SLEEP);
#endif /* DEBUG && IO_MEMUSAGE */
	/*
	 * Grab the mappings from the dmmu and stick 'em into the
	 * iommu.
	 */
	ASSERT(npages != 0);

	/* If we're going to flush the TLB using diag mode, do it now. */
	if (diag_tlb_flush)
		iommu_tlb_flush(softsp, ioaddr, npages);

	do {
		uint64_t iotte_flag;

		iotte_flag = tmp_iotte_flag;

		if (pp != NULL) {
			pfn = pp->p_pagenum;
			pp = pp->p_next;
		} else {
			pfn = (*pplist)->p_pagenum;
			pplist++;
		}

		DPRINTF(IOMMU_TTE, ("pp mapping TTE index %p pfn %lx "
		    "tte flag %lx ioaddr %x\n", iotte_ptr,
		    pfn, iotte_flag, ioaddr));

		/* Flush the IOMMU TLB before loading a new mapping */
		if (!diag_tlb_flush)
			iommu_tlb_flush(softsp, ioaddr, 1);

		/* Set the hardware IO TTE */
		*iotte_ptr = ((uint64_t)pfn << IOMMU_PAGESHIFT) | iotte_flag;

		ioaddr += IOMMU_PAGESIZE;
		npages--;
		iotte_ptr++;

#if defined(DEBUG) && defined(IO_MEMUSAGE)
		*pfnp = pfn;
		pfnp++;
#endif /* DEBUG && IO_MEMUSAGE */

	} while (npages != 0);

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	mutex_enter(&softsp->iomemlock);
	iomemp->next = softsp->iomem;
	softsp->iomem = iomemp;
	mutex_exit(&softsp->iomemlock);
#endif /* DEBUG && IO_MEMUSAGE */

	return (rval);
}


int
iommu_dma_lim_setup(dev_info_t *dip, dev_info_t *rdip,
    struct sbus_soft_state *softsp, uint_t *burstsizep, uint_t burstsize64,
    uint_t *minxferp, uint_t dma_flags)
{
	struct regspec *rp;

	/* Take care of 64 bit limits. */
	if (!(dma_flags & DDI_DMA_SBUS_64BIT)) {
		/*
		 * return burst size for 32-bit mode
		 */
		*burstsizep &= softsp->sbus_burst_sizes;
		return (DDI_FAILURE);
	}

	/*
	 * check if SBus supports 64 bit and if caller
	 * is child of SBus. No support through bridges
	 */
	if (!softsp->sbus64_burst_sizes || (ddi_get_parent(rdip) != dip)) {
		/*
		 * SBus doesn't support it or bridge. Do 32-bit
		 * xfers
		 */
		*burstsizep &= softsp->sbus_burst_sizes;
		return (DDI_FAILURE);
	}

	rp = ddi_rnumber_to_regspec(rdip, 0);
	if (rp == NULL) {
		*burstsizep &= softsp->sbus_burst_sizes;
		return (DDI_FAILURE);
	}

	/* Check for old-style 64 bit burstsizes */
	if (burstsize64 & SYSIO64_BURST_MASK) {
		/* Scale back burstsizes if Necessary */
		*burstsizep &= (softsp->sbus64_burst_sizes |
		    softsp->sbus_burst_sizes);
	} else {
		/* Get the 64 bit burstsizes. */
		*burstsizep = burstsize64;

		/* Scale back burstsizes if Necessary */
		*burstsizep &= (softsp->sbus64_burst_sizes >>
		    SYSIO64_BURST_SHIFT);
	}

	/*
	 * Set the largest value of the smallest burstsize that the
	 * device or the bus can manage.
	 */
	*minxferp = MAX(*minxferp,
	    (1 << (ddi_ffs(softsp->sbus64_burst_sizes) - 1)));

	return (DDI_SUCCESS);
}


int
iommu_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_attr_t *dma_attr, int (*waitfp)(caddr_t), caddr_t arg,
    ddi_dma_handle_t *handlep)
{
	ioaddr_t addrlow, addrhigh, segalign;
	ddi_dma_impl_t *mp;
	struct dma_impl_priv *mppriv;
	struct sbus_soft_state *softsp = (struct sbus_soft_state *)
	    ddi_get_soft_state(sbusp, ddi_get_instance(dip));

	/*
	 * Setup dma burstsizes and min-xfer counts.
	 */
	(void) iommu_dma_lim_setup(dip, rdip, softsp,
	    &dma_attr->dma_attr_burstsizes,
	    dma_attr->dma_attr_burstsizes, &dma_attr->dma_attr_minxfer,
	    dma_attr->dma_attr_flags);

	if (dma_attr->dma_attr_burstsizes == 0)
		return (DDI_DMA_BADATTR);

	addrlow = (ioaddr_t)dma_attr->dma_attr_addr_lo;
	addrhigh = (ioaddr_t)dma_attr->dma_attr_addr_hi;
	segalign = (ioaddr_t)dma_attr->dma_attr_seg;

	/*
	 * Check sanity for hi and lo address limits
	 */
	if ((addrhigh <= addrlow) ||
	    (addrhigh < (ioaddr_t)softsp->iommu_dvma_base)) {
		return (DDI_DMA_BADATTR);
	}
	if (dma_attr->dma_attr_flags & DDI_DMA_FORCE_PHYSICAL)
		return (DDI_DMA_BADATTR);

	mppriv = kmem_zalloc(sizeof (*mppriv),
	    (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP);

	if (mppriv == NULL) {
		if (waitfp != DDI_DMA_DONTWAIT) {
		    ddi_set_callback(waitfp, arg, &softsp->dvma_call_list_id);
		}
		return (DDI_DMA_NORESOURCES);
	}
	mp = (ddi_dma_impl_t *)mppriv;

	DPRINTF(IOMMU_DMA_ALLOCHDL_DEBUG, ("dma_allochdl: (%s) handle %p "
	    "hi %x lo %x min %x burst %x\n",
	    ddi_get_name(dip), mp, addrhigh, addrlow,
	    dma_attr->dma_attr_minxfer, dma_attr->dma_attr_burstsizes));

	mp->dmai_rdip = rdip;
	mp->dmai_minxfer = (uint_t)dma_attr->dma_attr_minxfer;
	mp->dmai_burstsizes = (uint_t)dma_attr->dma_attr_burstsizes;
	mp->dmai_attr = *dma_attr;
	/* See if the DMA engine has any limit restrictions. */
	if (segalign == (ioaddr_t)UINT32_MAX &&
	    addrhigh == (ioaddr_t)UINT32_MAX &&
	    (dma_attr->dma_attr_align <= IOMMU_PAGESIZE) && addrlow == 0) {
		mp->dmai_rflags |= DMP_NOLIMIT;
	}
	mppriv->softsp = softsp;
	mppriv->phys_sync_flag = va_to_pa((caddr_t)&mppriv->sync_flag);

	*handlep = (ddi_dma_handle_t)mp;
	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
iommu_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
{
	struct dma_impl_priv *mppriv = (struct dma_impl_priv *)handle;
	struct sbus_soft_state *softsp = mppriv->softsp;
	ASSERT(softsp != NULL);

	kmem_free(mppriv, sizeof (*mppriv));

	if (softsp->dvma_call_list_id != 0) {
		ddi_run_callback(&softsp->dvma_call_list_id);
	}
	return (DDI_SUCCESS);
}

static int
check_dma_attr(struct ddi_dma_req *dmareq, ddi_dma_attr_t *dma_attr,
    uint32_t *size)
{
	ioaddr_t addrlow;
	ioaddr_t addrhigh;
	uint32_t segalign;
	uint32_t smask;

	smask = *size - 1;
	segalign = dma_attr->dma_attr_seg;
	if (smask > segalign) {
		if ((dmareq->dmar_flags & DDI_DMA_PARTIAL) == 0)
			return (DDI_DMA_TOOBIG);
		*size = segalign + 1;
	}
	addrlow = (ioaddr_t)dma_attr->dma_attr_addr_lo;
	addrhigh = (ioaddr_t)dma_attr->dma_attr_addr_hi;
	if (addrlow + smask > addrhigh || addrlow + smask < addrlow) {
		if (!((addrlow + dmareq->dmar_object.dmao_size == 0) &&
		    (addrhigh == (ioaddr_t)-1))) {
			if ((dmareq->dmar_flags & DDI_DMA_PARTIAL) == 0)
				return (DDI_DMA_TOOBIG);
			*size = MIN(addrhigh - addrlow + 1, *size);
		}
	}
	return (DDI_DMA_MAPOK);
}

int
iommu_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, struct ddi_dma_req *dmareq,
    ddi_dma_cookie_t *cp, uint_t *ccountp)
{
	page_t *pp;
	uint32_t size;
	ioaddr_t ioaddr;
	uint_t offset;
	uintptr_t addr = 0;
	pgcnt_t npages;
	int rval;
	ddi_dma_attr_t *dma_attr;
	struct sbus_soft_state *softsp;
	struct page **pplist = NULL;
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
	struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;

#ifdef lint
	dip = dip;
	rdip = rdip;
#endif

	if (mp->dmai_inuse)
		return (DDI_DMA_INUSE);

	dma_attr = &mp->dmai_attr;
	size = (uint32_t)dmareq->dmar_object.dmao_size;
	if (!(mp->dmai_rflags & DMP_NOLIMIT)) {
		rval = check_dma_attr(dmareq, dma_attr, &size);
		if (rval != DDI_DMA_MAPOK)
			return (rval);
	}
	mp->dmai_inuse = 1;
	mp->dmai_offset = 0;
	mp->dmai_rflags = (dmareq->dmar_flags & DMP_DDIFLAGS) |
	    (mp->dmai_rflags & DMP_NOLIMIT);

	switch (dmareq->dmar_object.dmao_type) {
	case DMA_OTYP_VADDR:
	case DMA_OTYP_BUFVADDR:
		addr = (uintptr_t)dmareq->dmar_object.dmao_obj.virt_obj.v_addr;
		offset = addr & IOMMU_PAGEOFFSET;
		pplist = dmareq->dmar_object.dmao_obj.virt_obj.v_priv;
		npages = iommu_btopr(OBJSIZE + offset);

		DPRINTF(IOMMU_DMAMAP_DEBUG, ("dma_map vaddr: %lx pages "
		    "req addr %lx off %x OBJSIZE %x\n",
		    npages, addr, offset, OBJSIZE));

		/* We don't need the addr anymore if we have a shadow list */
		if (pplist != NULL)
			addr = NULL;
		pp = NULL;
		break;

	case DMA_OTYP_PAGES:
		pp = dmareq->dmar_object.dmao_obj.pp_obj.pp_pp;
		offset = dmareq->dmar_object.dmao_obj.pp_obj.pp_offset;
		npages = iommu_btopr(OBJSIZE + offset);
		break;

	case DMA_OTYP_PADDR:
	default:
		/*
		 * Not a supported type for this implementation
		 */
		rval = DDI_DMA_NOMAPPING;
		goto bad;
	}

	/* Get our soft state once we know we're mapping an object. */
	softsp = mppriv->softsp;
	ASSERT(softsp != NULL);

	if (mp->dmai_rflags & DDI_DMA_PARTIAL) {
		if (size != OBJSIZE) {
			/*
			 * If the request is for partial mapping arrangement,
			 * the device has to be able to address at least the
			 * size of the window we are establishing.
			 */
			if (size < iommu_ptob(MIN_DVMA_WIN_SIZE)) {
				rval = DDI_DMA_NOMAPPING;
				goto bad;
			}
			npages = iommu_btopr(size + offset);
		}
		/*
		 * If the size requested is less than a moderate amt,
		 * skip the partial mapping stuff- it's not worth the
		 * effort.
		 */
		if (npages > MIN_DVMA_WIN_SIZE) {
			npages = MIN_DVMA_WIN_SIZE + iommu_btopr(offset);
			size = iommu_ptob(MIN_DVMA_WIN_SIZE);
			DPRINTF(IOMMU_DMA_SETUP_DEBUG, ("dma_setup: SZ %x pg "
			    "%lx sz %x\n", OBJSIZE, npages, size));
			if (pplist != NULL) {
				mp->dmai_minfo = (void *)pplist;
				mp->dmai_rflags |= DMP_SHADOW;
			}
		} else {
			mp->dmai_rflags ^= DDI_DMA_PARTIAL;
		}
	} else {
		if (npages >= iommu_btop(softsp->iommu_dvma_size) -
		    MIN_DVMA_WIN_SIZE) {
			rval = DDI_DMA_TOOBIG;
			goto bad;
		}
	}

	/*
	 * save dmareq-object, size and npages into mp
	 */
	mp->dmai_object = dmareq->dmar_object;
	mp->dmai_size = size;
	mp->dmai_ndvmapages = npages;

	if (mp->dmai_rflags & DMP_NOLIMIT) {
		ioaddr = (ioaddr_t)(uintptr_t)vmem_alloc(softsp->dvma_arena,
		    iommu_ptob(npages),
		    dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP);
		if (ioaddr == 0) {
			rval = DDI_DMA_NORESOURCES;
			goto bad;
		}

		/*
		 * If we have a 1 page request and we're working with a page
		 * list, we're going to speed load an IOMMU entry.
		 */
		if (npages == 1 && !addr) {
			uint64_t iotte_flag = IOTTE_VALID | IOTTE_CACHE |
			    IOTTE_WRITE | IOTTE_STREAM;
			volatile uint64_t *iotte_ptr;
			pfn_t pfn;
#if defined(DEBUG) && defined(IO_MEMUSAGE)
			struct io_mem_list *iomemp;
			pfn_t *pfnp;
#endif /* DEBUG && IO_MEMUSAGE */

			iotte_ptr = IOTTE_NDX(ioaddr,
			    softsp->soft_tsb_base_addr);

			if (mp->dmai_rflags & DDI_DMA_CONSISTENT) {
				mp->dmai_rflags |= DMP_NOSYNC;
				iotte_flag ^= IOTTE_STREAM;
			} else if (softsp->stream_buf_off)
				iotte_flag ^= IOTTE_STREAM;

			mp->dmai_rflags ^= DDI_DMA_PARTIAL;

			if (pp != NULL)
				pfn = pp->p_pagenum;
			else
				pfn = (*pplist)->p_pagenum;

			iommu_tlb_flush(softsp, ioaddr, 1);

			*iotte_ptr =
			    ((uint64_t)pfn << IOMMU_PAGESHIFT) | iotte_flag;

			mp->dmai_mapping = (ioaddr_t)(ioaddr + offset);
			mp->dmai_nwin = 0;
			if (cp != NULL) {
				cp->dmac_notused = 0;
				cp->dmac_address = (ioaddr_t)mp->dmai_mapping;
				cp->dmac_size = mp->dmai_size;
				cp->dmac_type = 0;
				*ccountp = 1;
			}

			DPRINTF(IOMMU_TTE, ("speed loading: TTE index %p "
			    "pfn %lx tte flag %lx addr %lx ioaddr %x\n",
			    iotte_ptr, pfn, iotte_flag, addr, ioaddr));

#if defined(DEBUG) && defined(IO_MEMUSAGE)
			iomemp = kmem_alloc(sizeof (struct io_mem_list),
			    KM_SLEEP);
			iomemp->rdip = mp->dmai_rdip;
			iomemp->ioaddr = ioaddr;
			iomemp->addr = addr;
			iomemp->npages = npages;
			pfnp = iomemp->pfn = kmem_zalloc(sizeof (*pfnp) *
			    (npages + 1), KM_SLEEP);
			*pfnp = pfn;
			mutex_enter(&softsp->iomemlock);
			iomemp->next = softsp->iomem;
			softsp->iomem = iomemp;
			mutex_exit(&softsp->iomemlock);
#endif /* DEBUG && IO_MEMUSAGE */

			return (DDI_DMA_MAPPED);
		}
	} else {
		ioaddr = (ioaddr_t)(uintptr_t)vmem_xalloc(softsp->dvma_arena,
		    iommu_ptob(npages),
		    MAX((uint_t)dma_attr->dma_attr_align, IOMMU_PAGESIZE), 0,
		    (uint_t)dma_attr->dma_attr_seg + 1,
		    (void *)(uintptr_t)(ioaddr_t)dma_attr->dma_attr_addr_lo,
		    (void *)(uintptr_t)
			((ioaddr_t)dma_attr->dma_attr_addr_hi + 1),
		    dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP);
	}

	if (ioaddr == 0) {
		if (dmareq->dmar_fp == DDI_DMA_SLEEP)
			rval = DDI_DMA_NOMAPPING;
		else
			rval = DDI_DMA_NORESOURCES;
		goto bad;
	}

	mp->dmai_mapping = ioaddr + offset;
	ASSERT(mp->dmai_mapping >= softsp->iommu_dvma_base);

	/*
	 * At this point we have a range of virtual address allocated
	 * with which we now have to map to the requested object.
	 */
	if (addr) {
		rval = iommu_create_vaddr_mappings(mp,
		    addr & ~IOMMU_PAGEOFFSET);
		if (rval == DDI_DMA_NOMAPPING)
			goto bad_nomap;
	} else {
		rval = iommu_create_pp_mappings(mp, pp, pplist);
		if (rval == DDI_DMA_NOMAPPING)
			goto bad_nomap;
	}

	if (cp) {
		cp->dmac_notused = 0;
		cp->dmac_address = (ioaddr_t)mp->dmai_mapping;
		cp->dmac_size = mp->dmai_size;
		cp->dmac_type = 0;
		*ccountp = 1;
	}
	if (mp->dmai_rflags & DDI_DMA_PARTIAL) {
		size = iommu_ptob(mp->dmai_ndvmapages - iommu_btopr(offset));
		mp->dmai_nwin =
		    (dmareq->dmar_object.dmao_size + (size - 1)) / size;
		return (DDI_DMA_PARTIAL_MAP);
	} else {
		mp->dmai_nwin = 0;
		return (DDI_DMA_MAPPED);
	}

bad_nomap:
	/*
	 * Could not create mmu mappings.
	 */
	if (mp->dmai_rflags & DMP_NOLIMIT) {
		vmem_free(softsp->dvma_arena, (void *)(uintptr_t)ioaddr,
		    iommu_ptob(npages));
	} else {
		vmem_xfree(softsp->dvma_arena, (void *)(uintptr_t)ioaddr,
		    iommu_ptob(npages));
	}

bad:
	if (rval == DDI_DMA_NORESOURCES &&
	    dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
		ddi_set_callback(dmareq->dmar_fp,
		    dmareq->dmar_arg, &softsp->dvma_call_list_id);
	}
	mp->dmai_inuse = 0;
	return (rval);
}

/* ARGSUSED */
int
iommu_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle)
{
	ioaddr_t addr;
	uint_t npages;
	size_t size;
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
	struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;
	struct sbus_soft_state *softsp = mppriv->softsp;
	ASSERT(softsp != NULL);

	addr = (ioaddr_t)(mp->dmai_mapping & ~IOMMU_PAGEOFFSET);
	npages = mp->dmai_ndvmapages;
	size = iommu_ptob(npages);

	DPRINTF(IOMMU_DMA_UNBINDHDL_DEBUG, ("iommu_dma_unbindhdl: "
	    "unbinding addr %x for %x pages\n", addr, mp->dmai_ndvmapages));

	/* sync the entire object */
	if (!(mp->dmai_rflags & DDI_DMA_CONSISTENT)) {
		/* flush stream write buffers */
		sync_stream_buf(softsp, addr, npages, (int *)&mppriv->sync_flag,
		    mppriv->phys_sync_flag);
	}

#if defined(DEBUG) && defined(IO_MEMDEBUG)
	/*
	 * 'Free' the dma mappings.
	 */
	iommu_remove_mappings(mp);
#endif /* DEBUG && IO_MEMDEBUG */

	ASSERT(npages > (uint_t)0);
	if (mp->dmai_rflags & DMP_NOLIMIT)
		vmem_free(softsp->dvma_arena, (void *)(uintptr_t)addr, size);
	else
		vmem_xfree(softsp->dvma_arena, (void *)(uintptr_t)addr, size);

	mp->dmai_ndvmapages = 0;
	mp->dmai_inuse = 0;
	mp->dmai_minfo = NULL;

	if (softsp->dvma_call_list_id != 0)
		ddi_run_callback(&softsp->dvma_call_list_id);

	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
iommu_dma_flush(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, off_t off, size_t len,
    uint_t cache_flags)
{
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
	struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;

	if (!(mp->dmai_rflags & DDI_DMA_CONSISTENT)) {
		sync_stream_buf(mppriv->softsp, mp->dmai_mapping,
		    mp->dmai_ndvmapages, (int *)&mppriv->sync_flag,
		    mppriv->phys_sync_flag);
	}
	return (DDI_SUCCESS);
}

/*ARGSUSED*/
int
iommu_dma_win(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, uint_t win, off_t *offp,
    size_t *lenp, ddi_dma_cookie_t *cookiep, uint_t *ccountp)
{
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
	off_t offset;
	uint_t winsize;
	uint_t newoff;
	int rval;

	offset = mp->dmai_mapping & IOMMU_PAGEOFFSET;
	winsize = iommu_ptob(mp->dmai_ndvmapages - iommu_btopr(offset));

	DPRINTF(IOMMU_DMA_WIN_DEBUG, ("getwin win %d winsize %x\n", win,
	    winsize));

	/*
	 * win is in the range [0 .. dmai_nwin-1]
	 */
	if (win >= mp->dmai_nwin)
		return (DDI_FAILURE);

	newoff = win * winsize;
	if (newoff > mp->dmai_object.dmao_size - mp->dmai_minxfer)
		return (DDI_FAILURE);

	ASSERT(cookiep);
	cookiep->dmac_notused = 0;
	cookiep->dmac_type = 0;
	cookiep->dmac_address = (ioaddr_t)mp->dmai_mapping;
	cookiep->dmac_size = mp->dmai_size;
	*ccountp = 1;
	*offp = (off_t)newoff;
	*lenp = (uint_t)winsize;

	if (newoff == mp->dmai_offset) {
		/*
		 * Nothing to do...
		 */
		return (DDI_SUCCESS);
	}

	if ((rval = iommu_map_window(mp, newoff, winsize)) != DDI_SUCCESS)
		return (rval);

	/*
	 * Set this again in case iommu_map_window() has changed it
	 */
	cookiep->dmac_size = mp->dmai_size;

	return (DDI_SUCCESS);
}

static int
iommu_map_window(ddi_dma_impl_t *mp, off_t newoff, size_t winsize)
{
	uintptr_t addr = 0;
	page_t *pp;
	uint_t flags;
	struct page **pplist = NULL;

#if defined(DEBUG) && defined(IO_MEMDEBUG)
	/* Free mappings for current window */
	iommu_remove_mappings(mp);
#endif /* DEBUG && IO_MEMDEBUG */

	mp->dmai_offset = newoff;
	mp->dmai_size = mp->dmai_object.dmao_size - newoff;
	mp->dmai_size = MIN(mp->dmai_size, winsize);

	if (mp->dmai_object.dmao_type == DMA_OTYP_VADDR ||
	    mp->dmai_object.dmao_type == DMA_OTYP_BUFVADDR) {
		if (mp->dmai_rflags & DMP_SHADOW) {
			pplist = (struct page **)mp->dmai_minfo;
			ASSERT(pplist != NULL);
			pplist = pplist + (newoff >> MMU_PAGESHIFT);
		} else {
			addr = (uintptr_t)
			    mp->dmai_object.dmao_obj.virt_obj.v_addr;
			addr = (addr + newoff) & ~IOMMU_PAGEOFFSET;
		}
		pp = NULL;
	} else {
		pp = mp->dmai_object.dmao_obj.pp_obj.pp_pp;
		flags = 0;
		while (flags < newoff) {
			pp = pp->p_next;
			flags += MMU_PAGESIZE;
		}
	}

	/* Set up mappings for next window */
	if (addr) {
		if (iommu_create_vaddr_mappings(mp, addr) < 0)
			return (DDI_FAILURE);
	} else {
		if (iommu_create_pp_mappings(mp, pp, pplist) < 0)
			return (DDI_FAILURE);
	}

	/*
	 * also invalidate read stream buffer
	 */
	if (!(mp->dmai_rflags & DDI_DMA_CONSISTENT)) {
		struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;

		sync_stream_buf(mppriv->softsp, mp->dmai_mapping,
		    mp->dmai_ndvmapages, (int *)&mppriv->sync_flag,
		    mppriv->phys_sync_flag);
	}

	return (DDI_SUCCESS);

}

int
iommu_dma_map(dev_info_t *dip, dev_info_t *rdip,
    struct ddi_dma_req *dmareq, ddi_dma_handle_t *handlep)
{
	ddi_dma_lim_t *dma_lim = dmareq->dmar_limits;
	ddi_dma_impl_t *mp;
	ddi_dma_attr_t *dma_attr;
	struct dma_impl_priv *mppriv;
	ioaddr_t addrlow, addrhigh;
	ioaddr_t segalign;
	int rval;
	struct sbus_soft_state *softsp =
		(struct sbus_soft_state *)ddi_get_soft_state(sbusp,
		ddi_get_instance(dip));

	addrlow = dma_lim->dlim_addr_lo;
	addrhigh = dma_lim->dlim_addr_hi;
	if ((addrhigh <= addrlow) ||
	    (addrhigh < (ioaddr_t)softsp->iommu_dvma_base)) {
		return (DDI_DMA_NOMAPPING);
	}

	/*
	 * Setup DMA burstsizes and min-xfer counts.
	 */
	(void) iommu_dma_lim_setup(dip, rdip, softsp, &dma_lim->dlim_burstsizes,
		(uint_t)dma_lim->dlim_burstsizes, &dma_lim->dlim_minxfer,
		dmareq->dmar_flags);

	if (dma_lim->dlim_burstsizes == 0)
		return (DDI_DMA_NOMAPPING);
	/*
	 * If not an advisory call, get a DMA handle
	 */
	if (!handlep) {
		return (DDI_DMA_MAPOK);
	}

	mppriv = kmem_zalloc(sizeof (*mppriv),
	    (dmareq->dmar_fp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP);
	if (mppriv == NULL) {
		if (dmareq->dmar_fp != DDI_DMA_DONTWAIT) {
			ddi_set_callback(dmareq->dmar_fp,
			    dmareq->dmar_arg, &softsp->dvma_call_list_id);
		}
		return (DDI_DMA_NORESOURCES);
	}
	mp = (ddi_dma_impl_t *)mppriv;
	mp->dmai_rdip = rdip;
	mp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS;
	mp->dmai_minxfer = dma_lim->dlim_minxfer;
	mp->dmai_burstsizes = dma_lim->dlim_burstsizes;
	mp->dmai_offset = 0;
	mp->dmai_ndvmapages = 0;
	mp->dmai_minfo = 0;
	mp->dmai_inuse = 0;
	segalign = dma_lim->dlim_cntr_max;
	/* See if the DMA engine has any limit restrictions. */
	if (segalign == UINT32_MAX && addrhigh == UINT32_MAX &&
	    addrlow == 0) {
		mp->dmai_rflags |= DMP_NOLIMIT;
	}
	mppriv->softsp = softsp;
	mppriv->phys_sync_flag = va_to_pa((caddr_t)&mppriv->sync_flag);
	dma_attr = &mp->dmai_attr;
	dma_attr->dma_attr_align = 1;
	dma_attr->dma_attr_addr_lo = addrlow;
	dma_attr->dma_attr_addr_hi = addrhigh;
	dma_attr->dma_attr_seg = segalign;
	dma_attr->dma_attr_burstsizes = dma_lim->dlim_burstsizes;
	rval = iommu_dma_bindhdl(dip, rdip, (ddi_dma_handle_t)mp,
		dmareq, NULL, NULL);
	if (rval && (rval != DDI_DMA_PARTIAL_MAP)) {
		kmem_free(mppriv, sizeof (*mppriv));
	} else {
		*handlep = (ddi_dma_handle_t)mp;
	}
	return (rval);
}

/*ARGSUSED*/
int
iommu_dma_mctl(dev_info_t *dip, dev_info_t *rdip,
    ddi_dma_handle_t handle, enum ddi_dma_ctlops request,
    off_t *offp, size_t *lenp, caddr_t *objp, uint_t cache_flags)
{
	ioaddr_t addr;
	uint_t offset;
	pgcnt_t npages;
	size_t size;
	ddi_dma_cookie_t *cp;
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;

	DPRINTF(IOMMU_DMAMCTL_DEBUG, ("dma_mctl: handle %p ", mp));
	switch (request) {
	case DDI_DMA_FREE:
	{
		struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;
		struct sbus_soft_state *softsp = mppriv->softsp;
		ASSERT(softsp != NULL);

		/*
		 * 'Free' the dma mappings.
		 */
		addr = (ioaddr_t)(mp->dmai_mapping & ~IOMMU_PAGEOFFSET);
		npages = mp->dmai_ndvmapages;
		size = iommu_ptob(npages);

		DPRINTF(IOMMU_DMAMCTL_DMA_FREE_DEBUG, ("iommu_dma_mctl dmafree:"
		    "freeing vaddr %x for %x pages.\n", addr,
		    mp->dmai_ndvmapages));
		/* sync the entire object */
		if (!(mp->dmai_rflags & DDI_DMA_CONSISTENT)) {
			/* flush stream write buffers */
			sync_stream_buf(softsp, addr, npages,
			    (int *)&mppriv->sync_flag, mppriv->phys_sync_flag);
		}

#if defined(DEBUG) && defined(IO_MEMDEBUG)
		iommu_remove_mappings(mp);
#endif /* DEBUG && IO_MEMDEBUG */

		ASSERT(npages > (uint_t)0);
		if (mp->dmai_rflags & DMP_NOLIMIT)
			vmem_free(softsp->dvma_arena,
			    (void *)(uintptr_t)addr, size);
		else
			vmem_xfree(softsp->dvma_arena,
			    (void *)(uintptr_t)addr, size);

		kmem_free(mppriv, sizeof (*mppriv));

		if (softsp->dvma_call_list_id != 0)
			ddi_run_callback(&softsp->dvma_call_list_id);

		break;
	}

	case DDI_DMA_SET_SBUS64:
	{
		struct dma_impl_priv *mppriv = (struct dma_impl_priv *)mp;

		return (iommu_dma_lim_setup(dip, rdip, mppriv->softsp,
		    &mp->dmai_burstsizes, (uint_t)*lenp, &mp->dmai_minxfer,
		    DDI_DMA_SBUS_64BIT));
	}

	case DDI_DMA_HTOC:
		DPRINTF(IOMMU_DMAMCTL_HTOC_DEBUG, ("htoc off %lx mapping %lx "
		    "size %x\n", *offp, mp->dmai_mapping,
		    mp->dmai_size));

		if ((uint_t)(*offp) >= mp->dmai_size)
			return (DDI_FAILURE);

		cp = (ddi_dma_cookie_t *)objp;
		cp->dmac_notused = 0;
		cp->dmac_address = (mp->dmai_mapping + (uint_t)(*offp));
		cp->dmac_size =
		    mp->dmai_mapping + mp->dmai_size - cp->dmac_address;
		cp->dmac_type = 0;

		break;

	case DDI_DMA_KVADDR:
		/*
		 * If a physical address mapping has percolated this high,
		 * that is an error (maybe?).
		 */
		if (mp->dmai_rflags & DMP_PHYSADDR) {
			DPRINTF(IOMMU_DMAMCTL_KVADDR_DEBUG, ("kvaddr of phys "
			    "mapping\n"));
			return (DDI_FAILURE);
		}

		return (DDI_FAILURE);

	case DDI_DMA_NEXTWIN:
	{
		ddi_dma_win_t *owin, *nwin;
		uint_t winsize, newoff;
		int rval;

		DPRINTF(IOMMU_DMAMCTL_NEXTWIN_DEBUG, ("nextwin\n"));

		mp = (ddi_dma_impl_t *)handle;
		owin = (ddi_dma_win_t *)offp;
		nwin = (ddi_dma_win_t *)objp;
		if (mp->dmai_rflags & DDI_DMA_PARTIAL) {
			if (*owin == NULL) {
				DPRINTF(IOMMU_DMAMCTL_NEXTWIN_DEBUG,
				    ("nextwin: win == NULL\n"));
				mp->dmai_offset = 0;
				*nwin = (ddi_dma_win_t)mp;
				return (DDI_SUCCESS);
			}

			offset = (uint_t)(mp->dmai_mapping & IOMMU_PAGEOFFSET);
			winsize = iommu_ptob(mp->dmai_ndvmapages -
			    iommu_btopr(offset));

			newoff = (uint_t)(mp->dmai_offset + winsize);
			if (newoff > mp->dmai_object.dmao_size -
			    mp->dmai_minxfer)
				return (DDI_DMA_DONE);

			if ((rval = iommu_map_window(mp, newoff, winsize))
			    != DDI_SUCCESS)
				return (rval);
		} else {
			DPRINTF(IOMMU_DMAMCTL_NEXTWIN_DEBUG, ("nextwin: no "
			    "partial mapping\n"));
			if (*owin != NULL)
				return (DDI_DMA_DONE);
			mp->dmai_offset = 0;
			*nwin = (ddi_dma_win_t)mp;
		}
		break;
	}

	case DDI_DMA_NEXTSEG:
	{
		ddi_dma_seg_t *oseg, *nseg;

		DPRINTF(IOMMU_DMAMCTL_NEXTSEG_DEBUG, ("nextseg:\n"));

		oseg = (ddi_dma_seg_t *)lenp;
		if (*oseg != NULL)
			return (DDI_DMA_DONE);
		nseg = (ddi_dma_seg_t *)objp;
		*nseg = *((ddi_dma_seg_t *)offp);
		break;
	}

	case DDI_DMA_SEGTOC:
	{
		ddi_dma_seg_impl_t *seg;

		seg = (ddi_dma_seg_impl_t *)handle;
		cp = (ddi_dma_cookie_t *)objp;
		cp->dmac_notused = 0;
		cp->dmac_address = (ioaddr_t)seg->dmai_mapping;
		cp->dmac_size = *lenp = seg->dmai_size;
		cp->dmac_type = 0;
		*offp = seg->dmai_offset;
		break;
	}

	case DDI_DMA_MOVWIN:
	{
		uint_t winsize;
		uint_t newoff;
		int rval;

		offset = (uint_t)(mp->dmai_mapping & IOMMU_PAGEOFFSET);
		winsize = iommu_ptob(mp->dmai_ndvmapages - iommu_btopr(offset));

		DPRINTF(IOMMU_DMAMCTL_MOVWIN_DEBUG, ("movwin off %lx len %lx "
		    "winsize %x\n", *offp, *lenp, winsize));

		if ((mp->dmai_rflags & DDI_DMA_PARTIAL) == 0)
			return (DDI_FAILURE);

		if (*lenp != (uint_t)-1 && *lenp != winsize) {
			DPRINTF(IOMMU_DMAMCTL_MOVWIN_DEBUG, ("bad length\n"));
			return (DDI_FAILURE);
		}
		newoff = (uint_t)*offp;
		if (newoff & (winsize - 1)) {
			DPRINTF(IOMMU_DMAMCTL_MOVWIN_DEBUG, ("bad off\n"));
			return (DDI_FAILURE);
		}

		if (newoff == mp->dmai_offset) {
			/*
			 * Nothing to do...
			 */
			break;
		}

		/*
		 * Check out new address...
		 */
		if (newoff > mp->dmai_object.dmao_size - mp->dmai_minxfer) {
			DPRINTF(IOMMU_DMAMCTL_MOVWIN_DEBUG, ("newoff out of "
			    "range\n"));
			return (DDI_FAILURE);
		}

		rval = iommu_map_window(mp, newoff, winsize);
		if (rval != DDI_SUCCESS)
			return (rval);

		if ((cp = (ddi_dma_cookie_t *)objp) != 0) {
			cp->dmac_notused = 0;
			cp->dmac_address = (ioaddr_t)mp->dmai_mapping;
			cp->dmac_size = mp->dmai_size;
			cp->dmac_type = 0;
		}
		*offp = (off_t)newoff;
		*lenp = (uint_t)winsize;
		break;
	}

	case DDI_DMA_REPWIN:
		if ((mp->dmai_rflags & DDI_DMA_PARTIAL) == 0) {
			DPRINTF(IOMMU_DMAMCTL_REPWIN_DEBUG, ("repwin fail\n"));
			return (DDI_FAILURE);
		}

		*offp = (off_t)mp->dmai_offset;

		addr = mp->dmai_ndvmapages -
		    iommu_btopr(mp->dmai_mapping & IOMMU_PAGEOFFSET);

		*lenp = (uint_t)iommu_ptob(addr);

		DPRINTF(IOMMU_DMAMCTL_REPWIN_DEBUG, ("repwin off %lx len %x\n",
		    mp->dmai_offset, mp->dmai_size));

		break;

	case DDI_DMA_GETERR:
		DPRINTF(IOMMU_DMAMCTL_GETERR_DEBUG,
		    ("iommu_dma_mctl: geterr\n"));

		break;

	case DDI_DMA_COFF:
		cp = (ddi_dma_cookie_t *)offp;
		addr = cp->dmac_address;

		if (addr < mp->dmai_mapping ||
		    addr >= mp->dmai_mapping + mp->dmai_size)
			return (DDI_FAILURE);

		*objp = (caddr_t)(addr - mp->dmai_mapping);

		DPRINTF(IOMMU_DMAMCTL_COFF_DEBUG, ("coff off %lx mapping %lx "
		    "size %x\n", (ulong_t)*objp, mp->dmai_mapping,
		    mp->dmai_size));

		break;

	case DDI_DMA_RESERVE:
	{
		struct ddi_dma_req *dmareq = (struct ddi_dma_req *)offp;
		ddi_dma_lim_t *dma_lim;
		ddi_dma_handle_t *handlep;
		uint_t np;
		ioaddr_t ioaddr;
		int i;
		struct fast_dvma *iommu_fast_dvma;
		struct sbus_soft_state *softsp =
		    (struct sbus_soft_state *)ddi_get_soft_state(sbusp,
		    ddi_get_instance(dip));

		/* Some simple sanity checks */
		dma_lim = dmareq->dmar_limits;
		if (dma_lim->dlim_burstsizes == 0) {
			DPRINTF(IOMMU_FASTDMA_RESERVE,
			    ("Reserve: bad burstsizes\n"));
			return (DDI_DMA_BADLIMITS);
		}
		if ((AHI <= ALO) || (AHI < softsp->iommu_dvma_base)) {
			DPRINTF(IOMMU_FASTDMA_RESERVE,
			    ("Reserve: bad limits\n"));
			return (DDI_DMA_BADLIMITS);
		}

		np = dmareq->dmar_object.dmao_size;
		mutex_enter(&softsp->dma_pool_lock);
		if (np > softsp->dma_reserve) {
			mutex_exit(&softsp->dma_pool_lock);
			DPRINTF(IOMMU_FASTDMA_RESERVE,
			    ("Reserve: dma_reserve is exhausted\n"));
			return (DDI_DMA_NORESOURCES);
		}

		softsp->dma_reserve -= np;
		mutex_exit(&softsp->dma_pool_lock);
		mp = kmem_zalloc(sizeof (*mp), KM_SLEEP);
		mp->dmai_rflags = DMP_BYPASSNEXUS;
		mp->dmai_rdip = rdip;
		mp->dmai_minxfer = dma_lim->dlim_minxfer;
		mp->dmai_burstsizes = dma_lim->dlim_burstsizes;

		ioaddr = (ioaddr_t)(uintptr_t)vmem_xalloc(softsp->dvma_arena,
		    iommu_ptob(np), IOMMU_PAGESIZE, 0,
		    dma_lim->dlim_cntr_max + 1,
		    (void *)(uintptr_t)ALO, (void *)(uintptr_t)(AHI + 1),
		    dmareq->dmar_fp == DDI_DMA_SLEEP ? VM_SLEEP : VM_NOSLEEP);

		if (ioaddr == 0) {
			mutex_enter(&softsp->dma_pool_lock);
			softsp->dma_reserve += np;
			mutex_exit(&softsp->dma_pool_lock);
			kmem_free(mp, sizeof (*mp));
			DPRINTF(IOMMU_FASTDMA_RESERVE,
			    ("Reserve: No dvma resources available\n"));
			return (DDI_DMA_NOMAPPING);
		}

		/* create a per request structure */
		iommu_fast_dvma = kmem_alloc(sizeof (struct fast_dvma),
		    KM_SLEEP);

		/*
		 * We need to remember the size of the transfer so that
		 * we can figure the virtual pages to sync when the transfer
		 * is complete.
		 */
		iommu_fast_dvma->pagecnt = kmem_zalloc(np *
		    sizeof (uint_t), KM_SLEEP);

		/* Allocate a streaming cache sync flag for each index */
		iommu_fast_dvma->sync_flag = kmem_zalloc(np *
		    sizeof (int), KM_SLEEP);

		/* Allocate a physical sync flag for each index */
		iommu_fast_dvma->phys_sync_flag =
		    kmem_zalloc(np * sizeof (uint64_t), KM_SLEEP);

		for (i = 0; i < np; i++)
			iommu_fast_dvma->phys_sync_flag[i] = va_to_pa((caddr_t)
			    &iommu_fast_dvma->sync_flag[i]);

		mp->dmai_mapping = ioaddr;
		mp->dmai_ndvmapages = np;
		iommu_fast_dvma->ops = &iommu_dvma_ops;
		iommu_fast_dvma->softsp = (caddr_t)softsp;
		mp->dmai_nexus_private = (caddr_t)iommu_fast_dvma;
		handlep = (ddi_dma_handle_t *)objp;
		*handlep = (ddi_dma_handle_t)mp;

		DPRINTF(IOMMU_FASTDMA_RESERVE,
		    ("Reserve: mapping object %p base addr %lx size %x\n",
		    mp, mp->dmai_mapping, mp->dmai_ndvmapages));

		break;
	}

	case DDI_DMA_RELEASE:
	{
		ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
		uint_t np = npages = mp->dmai_ndvmapages;
		ioaddr_t ioaddr = mp->dmai_mapping;
		volatile uint64_t *iotte_ptr;
		struct fast_dvma *iommu_fast_dvma = (struct fast_dvma *)
		    mp->dmai_nexus_private;
		struct sbus_soft_state *softsp = (struct sbus_soft_state *)
		    iommu_fast_dvma->softsp;

		ASSERT(softsp != NULL);

		/* Unload stale mappings and flush stale tlb's */
		iotte_ptr = IOTTE_NDX(ioaddr, softsp->soft_tsb_base_addr);

		while (npages > (uint_t)0) {
			*iotte_ptr = (uint64_t)0;	/* unload tte */
			iommu_tlb_flush(softsp, ioaddr, 1);

			npages--;
			iotte_ptr++;
			ioaddr += IOMMU_PAGESIZE;
		}

		ioaddr = (ioaddr_t)mp->dmai_mapping;
		mutex_enter(&softsp->dma_pool_lock);
		softsp->dma_reserve += np;
		mutex_exit(&softsp->dma_pool_lock);

		if (mp->dmai_rflags & DMP_NOLIMIT)
			vmem_free(softsp->dvma_arena,
			    (void *)(uintptr_t)ioaddr, iommu_ptob(np));
		else
			vmem_xfree(softsp->dvma_arena,
			    (void *)(uintptr_t)ioaddr, iommu_ptob(np));

		kmem_free(mp, sizeof (*mp));
		kmem_free(iommu_fast_dvma->pagecnt, np * sizeof (uint_t));
		kmem_free(iommu_fast_dvma->sync_flag, np * sizeof (int));
		kmem_free(iommu_fast_dvma->phys_sync_flag, np *
		    sizeof (uint64_t));
		kmem_free(iommu_fast_dvma, sizeof (struct fast_dvma));


		DPRINTF(IOMMU_FASTDMA_RESERVE,
		    ("Release: Base addr %x size %x\n", ioaddr, np));
		/*
		 * Now that we've freed some resource,
		 * if there is anybody waiting for it
		 * try and get them going.
		 */
		if (softsp->dvma_call_list_id != 0)
			ddi_run_callback(&softsp->dvma_call_list_id);

		break;
	}

	default:
		DPRINTF(IOMMU_DMAMCTL_DEBUG, ("iommu_dma_mctl: unknown option "
		    "0%x\n", request));

		return (DDI_FAILURE);
	}
	return (DDI_SUCCESS);
}

/*ARGSUSED*/
void
iommu_dvma_kaddr_load(ddi_dma_handle_t h, caddr_t a, uint_t len, uint_t index,
    ddi_dma_cookie_t *cp)
{
	uintptr_t addr;
	ioaddr_t ioaddr;
	uint_t offset;
	pfn_t pfn;
	int npages;
	volatile uint64_t *iotte_ptr;
	uint64_t iotte_flag = 0;
	struct as *as = NULL;
	extern struct as kas;
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
	struct fast_dvma *iommu_fast_dvma =
	    (struct fast_dvma *)mp->dmai_nexus_private;
	struct sbus_soft_state *softsp = (struct sbus_soft_state *)
	    iommu_fast_dvma->softsp;
#if defined(DEBUG) && defined(IO_MEMUSAGE)
	struct io_mem_list *iomemp;
	pfn_t *pfnp;
#endif /* DEBUG && IO_MEMUSAGE */

	ASSERT(softsp != NULL);

	addr = (uintptr_t)a;
	ioaddr = (ioaddr_t)(mp->dmai_mapping + iommu_ptob(index));
	offset = (uint_t)(addr & IOMMU_PAGEOFFSET);
	iommu_fast_dvma->pagecnt[index] = iommu_btopr(len + offset);
	as = &kas;
	addr &= ~IOMMU_PAGEOFFSET;
	npages = iommu_btopr(len + offset);

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	iomemp = kmem_alloc(sizeof (struct io_mem_list), KM_SLEEP);
	iomemp->rdip = mp->dmai_rdip;
	iomemp->ioaddr = ioaddr;
	iomemp->addr = addr;
	iomemp->npages = npages;
	pfnp = iomemp->pfn = kmem_zalloc(sizeof (*pfnp) * (npages + 1),
	    KM_SLEEP);
#endif /* DEBUG && IO_MEMUSAGE */

	cp->dmac_address = ioaddr | offset;
	cp->dmac_size = len;

	iotte_ptr = IOTTE_NDX(ioaddr, softsp->soft_tsb_base_addr);
	/* read/write and streaming io on */
	iotte_flag = IOTTE_VALID | IOTTE_WRITE | IOTTE_CACHE;

	if (mp->dmai_rflags & DDI_DMA_CONSISTENT)
		mp->dmai_rflags |= DMP_NOSYNC;
	else if (!softsp->stream_buf_off)
		iotte_flag |= IOTTE_STREAM;

	DPRINTF(IOMMU_FASTDMA_LOAD, ("kaddr_load: ioaddr %x "
	    "size %x offset %x index %x kaddr %lx\n",
	    ioaddr, len, offset, index, addr));
	ASSERT(npages > 0);
	do {
		pfn = hat_getpfnum(as->a_hat, (caddr_t)addr);
		if (pfn == PFN_INVALID) {
			DPRINTF(IOMMU_FASTDMA_LOAD, ("kaddr_load: invalid pfn "
			    "from hat_getpfnum()\n"));
		}

		iommu_tlb_flush(softsp, ioaddr, 1);

		/* load tte */
		*iotte_ptr = ((uint64_t)pfn << IOMMU_PAGESHIFT) | iotte_flag;

		npages--;
		iotte_ptr++;

		addr += IOMMU_PAGESIZE;
		ioaddr += IOMMU_PAGESIZE;

#if defined(DEBUG) && defined(IO_MEMUSAGE)
		*pfnp = pfn;
		pfnp++;
#endif /* DEBUG && IO_MEMUSAGE */

	} while (npages > 0);

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	mutex_enter(&softsp->iomemlock);
	iomemp->next = softsp->iomem;
	softsp->iomem = iomemp;
	mutex_exit(&softsp->iomemlock);
#endif /* DEBUG && IO_MEMUSAGE */
}

/*ARGSUSED*/
void
iommu_dvma_unload(ddi_dma_handle_t h, uint_t index, uint_t view)
{
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
	ioaddr_t ioaddr;
	pgcnt_t npages;
	struct fast_dvma *iommu_fast_dvma =
	    (struct fast_dvma *)mp->dmai_nexus_private;
	struct sbus_soft_state *softsp = (struct sbus_soft_state *)
	    iommu_fast_dvma->softsp;
#if defined(DEBUG) && defined(IO_MEMUSAGE)
	struct io_mem_list **prevp, *walk;
#endif /* DEBUG && IO_MEMUSAGE */

	ASSERT(softsp != NULL);

	ioaddr = (ioaddr_t)(mp->dmai_mapping + iommu_ptob(index));
	npages = iommu_fast_dvma->pagecnt[index];

#if defined(DEBUG) && defined(IO_MEMUSAGE)
	mutex_enter(&softsp->iomemlock);
	prevp = &softsp->iomem;
	walk = softsp->iomem;

	while (walk != NULL) {
		if (walk->ioaddr == ioaddr) {
			*prevp = walk->next;
			break;
		}
		prevp = &walk->next;
		walk = walk->next;
	}
	mutex_exit(&softsp->iomemlock);

	kmem_free(walk->pfn, sizeof (pfn_t) * (npages + 1));
	kmem_free(walk, sizeof (struct io_mem_list));
#endif /* DEBUG && IO_MEMUSAGE */

	DPRINTF(IOMMU_FASTDMA_SYNC, ("kaddr_unload: handle %p sync flag "
	    "addr %p sync flag pfn %llx index %x page count %lx\n", mp,
	    &iommu_fast_dvma->sync_flag[index],
	    iommu_fast_dvma->phys_sync_flag[index],
	    index, npages));

	if ((mp->dmai_rflags & DMP_NOSYNC) != DMP_NOSYNC) {
		sync_stream_buf(softsp, ioaddr, npages,
			(int *)&iommu_fast_dvma->sync_flag[index],
			iommu_fast_dvma->phys_sync_flag[index]);
	}
}

/*ARGSUSED*/
void
iommu_dvma_sync(ddi_dma_handle_t h, uint_t index, uint_t view)
{
	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)h;
	ioaddr_t ioaddr;
	uint_t npages;
	struct fast_dvma *iommu_fast_dvma =
	    (struct fast_dvma *)mp->dmai_nexus_private;
	struct sbus_soft_state *softsp = (struct sbus_soft_state *)
	    iommu_fast_dvma->softsp;

	if ((mp->dmai_rflags & DMP_NOSYNC) == DMP_NOSYNC)
		return;

	ASSERT(softsp != NULL);
	ioaddr = (ioaddr_t)(mp->dmai_mapping + iommu_ptob(index));
	npages = iommu_fast_dvma->pagecnt[index];

	DPRINTF(IOMMU_FASTDMA_SYNC, ("kaddr_sync: handle %p, "
	    "sync flag addr %p, sync flag pfn %llx\n", mp,
	    &iommu_fast_dvma->sync_flag[index],
	    iommu_fast_dvma->phys_sync_flag[index]));

	sync_stream_buf(softsp, ioaddr, npages,
	    (int *)&iommu_fast_dvma->sync_flag[index],
	    iommu_fast_dvma->phys_sync_flag[index]);
}