aboutsummaryrefslogtreecommitdiffstats
path: root/arch/sparc64/kernel/sbus.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/sparc64/kernel/sbus.c')
-rw-r--r--arch/sparc64/kernel/sbus.c717
1 files changed, 325 insertions, 392 deletions
diff --git a/arch/sparc64/kernel/sbus.c b/arch/sparc64/kernel/sbus.c
index 01d6d869ea2b..3b05428cc909 100644
--- a/arch/sparc64/kernel/sbus.c
+++ b/arch/sparc64/kernel/sbus.c
@@ -24,48 +24,11 @@
24 24
25#include "iommu_common.h" 25#include "iommu_common.h"
26 26
27/* These should be allocated on an SMP_CACHE_BYTES
28 * aligned boundary for optimal performance.
29 *
30 * On SYSIO, using an 8K page size we have 1GB of SBUS
31 * DMA space mapped. We divide this space into equally
32 * sized clusters. We allocate a DMA mapping from the
33 * cluster that matches the order of the allocation, or
34 * if the order is greater than the number of clusters,
35 * we try to allocate from the last cluster.
36 */
37
38#define NCLUSTERS 8UL
39#define ONE_GIG (1UL * 1024UL * 1024UL * 1024UL)
40#define CLUSTER_SIZE (ONE_GIG / NCLUSTERS)
41#define CLUSTER_MASK (CLUSTER_SIZE - 1)
42#define CLUSTER_NPAGES (CLUSTER_SIZE >> IO_PAGE_SHIFT)
43#define MAP_BASE ((u32)0xc0000000) 27#define MAP_BASE ((u32)0xc0000000)
44 28
45struct sbus_iommu { 29struct sbus_info {
46/*0x00*/spinlock_t lock; 30 struct iommu iommu;
47 31 struct strbuf strbuf;
48/*0x08*/iopte_t *page_table;
49/*0x10*/unsigned long strbuf_regs;
50/*0x18*/unsigned long iommu_regs;
51/*0x20*/unsigned long sbus_control_reg;
52
53/*0x28*/volatile unsigned long strbuf_flushflag;
54
55 /* If NCLUSTERS is ever decresed to 4 or lower,
56 * you must increase the size of the type of
57 * these counters. You have been duly warned. -DaveM
58 */
59/*0x30*/struct {
60 u16 next;
61 u16 flush;
62 } alloc_info[NCLUSTERS];
63
64 /* The lowest used consistent mapping entry. Since
65 * we allocate consistent maps out of cluster 0 this
66 * is relative to the beginning of closter 0.
67 */
68/*0x50*/u32 lowest_consistent_map;
69}; 32};
70 33
71/* Offsets from iommu_regs */ 34/* Offsets from iommu_regs */
@@ -81,29 +44,17 @@ struct sbus_iommu {
81 44
82#define IOMMU_DRAM_VALID (1UL << 30UL) 45#define IOMMU_DRAM_VALID (1UL << 30UL)
83 46
84static void __iommu_flushall(struct sbus_iommu *iommu) 47static void __iommu_flushall(struct iommu *iommu)
85{ 48{
86 unsigned long tag = iommu->iommu_regs + IOMMU_TAGDIAG; 49 unsigned long tag;
87 int entry; 50 int entry;
88 51
52 tag = iommu->iommu_control + (IOMMU_TAGDIAG - IOMMU_CONTROL);
89 for (entry = 0; entry < 16; entry++) { 53 for (entry = 0; entry < 16; entry++) {
90 upa_writeq(0, tag); 54 upa_writeq(0, tag);
91 tag += 8UL; 55 tag += 8UL;
92 } 56 }
93 upa_readq(iommu->sbus_control_reg); 57 upa_readq(iommu->write_complete_reg);
94
95 for (entry = 0; entry < NCLUSTERS; entry++) {
96 iommu->alloc_info[entry].flush =
97 iommu->alloc_info[entry].next;
98 }
99}
100
101static void iommu_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages)
102{
103 while (npages--)
104 upa_writeq(base + (npages << IO_PAGE_SHIFT),
105 iommu->iommu_regs + IOMMU_FLUSH);
106 upa_readq(iommu->sbus_control_reg);
107} 58}
108 59
109/* Offsets from strbuf_regs */ 60/* Offsets from strbuf_regs */
@@ -118,15 +69,14 @@ static void iommu_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages
118 69
119#define STRBUF_TAG_VALID 0x02UL 70#define STRBUF_TAG_VALID 0x02UL
120 71
121static void sbus_strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages, int direction) 72static void sbus_strbuf_flush(struct iommu *iommu, struct strbuf *strbuf, u32 base, unsigned long npages, int direction)
122{ 73{
123 unsigned long n; 74 unsigned long n;
124 int limit; 75 int limit;
125 76
126 n = npages; 77 n = npages;
127 while (n--) 78 while (n--)
128 upa_writeq(base + (n << IO_PAGE_SHIFT), 79 upa_writeq(base + (n << IO_PAGE_SHIFT), strbuf->strbuf_pflush);
129 iommu->strbuf_regs + STRBUF_PFLUSH);
130 80
131 /* If the device could not have possibly put dirty data into 81 /* If the device could not have possibly put dirty data into
132 * the streaming cache, no flush-flag synchronization needs 82 * the streaming cache, no flush-flag synchronization needs
@@ -135,15 +85,14 @@ static void sbus_strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long
135 if (direction == SBUS_DMA_TODEVICE) 85 if (direction == SBUS_DMA_TODEVICE)
136 return; 86 return;
137 87
138 iommu->strbuf_flushflag = 0UL; 88 *(strbuf->strbuf_flushflag) = 0UL;
139 89
140 /* Whoopee cushion! */ 90 /* Whoopee cushion! */
141 upa_writeq(__pa(&iommu->strbuf_flushflag), 91 upa_writeq(strbuf->strbuf_flushflag_pa, strbuf->strbuf_fsync);
142 iommu->strbuf_regs + STRBUF_FSYNC); 92 upa_readq(iommu->write_complete_reg);
143 upa_readq(iommu->sbus_control_reg);
144 93
145 limit = 100000; 94 limit = 100000;
146 while (iommu->strbuf_flushflag == 0UL) { 95 while (*(strbuf->strbuf_flushflag) == 0UL) {
147 limit--; 96 limit--;
148 if (!limit) 97 if (!limit)
149 break; 98 break;
@@ -156,288 +105,247 @@ static void sbus_strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long
156 base, npages); 105 base, npages);
157} 106}
158 107
159static iopte_t *alloc_streaming_cluster(struct sbus_iommu *iommu, unsigned long npages) 108/* Based largely upon the ppc64 iommu allocator. */
109static long sbus_arena_alloc(struct iommu *iommu, unsigned long npages)
160{ 110{
161 iopte_t *iopte, *limit, *first, *cluster; 111 struct iommu_arena *arena = &iommu->arena;
162 unsigned long cnum, ent, nent, flush_point, found; 112 unsigned long n, i, start, end, limit;
163 113 int pass;
164 cnum = 0; 114
165 nent = 1; 115 limit = arena->limit;
166 while ((1UL << cnum) < npages) 116 start = arena->hint;
167 cnum++; 117 pass = 0;
168 if(cnum >= NCLUSTERS) { 118
169 nent = 1UL << (cnum - NCLUSTERS); 119again:
170 cnum = NCLUSTERS - 1; 120 n = find_next_zero_bit(arena->map, limit, start);
171 } 121 end = n + npages;
172 iopte = iommu->page_table + (cnum * CLUSTER_NPAGES); 122 if (unlikely(end >= limit)) {
173 123 if (likely(pass < 1)) {
174 if (cnum == 0) 124 limit = start;
175 limit = (iommu->page_table + 125 start = 0;
176 iommu->lowest_consistent_map); 126 __iommu_flushall(iommu);
177 else 127 pass++;
178 limit = (iopte + CLUSTER_NPAGES); 128 goto again;
179
180 iopte += ((ent = iommu->alloc_info[cnum].next) << cnum);
181 flush_point = iommu->alloc_info[cnum].flush;
182
183 first = iopte;
184 cluster = NULL;
185 found = 0;
186 for (;;) {
187 if (iopte_val(*iopte) == 0UL) {
188 found++;
189 if (!cluster)
190 cluster = iopte;
191 } else { 129 } else {
192 /* Used cluster in the way */ 130 /* Scanned the whole thing, give up. */
193 cluster = NULL; 131 return -1;
194 found = 0;
195 } 132 }
133 }
196 134
197 if (found == nent) 135 for (i = n; i < end; i++) {
198 break; 136 if (test_bit(i, arena->map)) {
199 137 start = i + 1;
200 iopte += (1 << cnum); 138 goto again;
201 ent++;
202 if (iopte >= limit) {
203 iopte = (iommu->page_table + (cnum * CLUSTER_NPAGES));
204 ent = 0;
205
206 /* Multiple cluster allocations must not wrap */
207 cluster = NULL;
208 found = 0;
209 } 139 }
210 if (ent == flush_point)
211 __iommu_flushall(iommu);
212 if (iopte == first)
213 goto bad;
214 } 140 }
215 141
216 /* ent/iopte points to the last cluster entry we're going to use, 142 for (i = n; i < end; i++)
217 * so save our place for the next allocation. 143 __set_bit(i, arena->map);
218 */ 144
219 if ((iopte + (1 << cnum)) >= limit) 145 arena->hint = end;
220 ent = 0; 146
221 else 147 return n;
222 ent = ent + 1;
223 iommu->alloc_info[cnum].next = ent;
224 if (ent == flush_point)
225 __iommu_flushall(iommu);
226
227 /* I've got your streaming cluster right here buddy boy... */
228 return cluster;
229
230bad:
231 printk(KERN_EMERG "sbus: alloc_streaming_cluster of npages(%ld) failed!\n",
232 npages);
233 return NULL;
234} 148}
235 149
236static void free_streaming_cluster(struct sbus_iommu *iommu, u32 base, unsigned long npages) 150static void sbus_arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
237{ 151{
238 unsigned long cnum, ent, nent; 152 unsigned long i;
239 iopte_t *iopte;
240 153
241 cnum = 0; 154 for (i = base; i < (base + npages); i++)
242 nent = 1; 155 __clear_bit(i, arena->map);
243 while ((1UL << cnum) < npages)
244 cnum++;
245 if(cnum >= NCLUSTERS) {
246 nent = 1UL << (cnum - NCLUSTERS);
247 cnum = NCLUSTERS - 1;
248 }
249 ent = (base & CLUSTER_MASK) >> (IO_PAGE_SHIFT + cnum);
250 iopte = iommu->page_table + ((base - MAP_BASE) >> IO_PAGE_SHIFT);
251 do {
252 iopte_val(*iopte) = 0UL;
253 iopte += 1 << cnum;
254 } while(--nent);
255
256 /* If the global flush might not have caught this entry,
257 * adjust the flush point such that we will flush before
258 * ever trying to reuse it.
259 */
260#define between(X,Y,Z) (((Z) - (Y)) >= ((X) - (Y)))
261 if (between(ent, iommu->alloc_info[cnum].next, iommu->alloc_info[cnum].flush))
262 iommu->alloc_info[cnum].flush = ent;
263#undef between
264} 156}
265 157
266/* We allocate consistent mappings from the end of cluster zero. */ 158static void sbus_iommu_table_init(struct iommu *iommu, unsigned int tsbsize)
267static iopte_t *alloc_consistent_cluster(struct sbus_iommu *iommu, unsigned long npages)
268{ 159{
269 iopte_t *iopte; 160 unsigned long tsbbase, order, sz, num_tsb_entries;
270 161
271 iopte = iommu->page_table + (1 * CLUSTER_NPAGES); 162 num_tsb_entries = tsbsize / sizeof(iopte_t);
272 while (iopte > iommu->page_table) {
273 iopte--;
274 if (!(iopte_val(*iopte) & IOPTE_VALID)) {
275 unsigned long tmp = npages;
276 163
277 while (--tmp) { 164 /* Setup initial software IOMMU state. */
278 iopte--; 165 spin_lock_init(&iommu->lock);
279 if (iopte_val(*iopte) & IOPTE_VALID) 166 iommu->page_table_map_base = MAP_BASE;
280 break; 167
281 } 168 /* Allocate and initialize the free area map. */
282 if (tmp == 0) { 169 sz = num_tsb_entries / 8;
283 u32 entry = (iopte - iommu->page_table); 170 sz = (sz + 7UL) & ~7UL;
171 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
172 if (!iommu->arena.map) {
173 prom_printf("SBUS_IOMMU: Error, kmalloc(arena.map) failed.\n");
174 prom_halt();
175 }
176 iommu->arena.limit = num_tsb_entries;
284 177
285 if (entry < iommu->lowest_consistent_map) 178 /* Now allocate and setup the IOMMU page table itself. */
286 iommu->lowest_consistent_map = entry; 179 order = get_order(tsbsize);
287 return iopte; 180 tsbbase = __get_free_pages(GFP_KERNEL, order);
288 } 181 if (!tsbbase) {
289 } 182 prom_printf("IOMMU: Error, gfp(tsb) failed.\n");
183 prom_halt();
290 } 184 }
291 return NULL; 185 iommu->page_table = (iopte_t *)tsbbase;
186 memset(iommu->page_table, 0, tsbsize);
292} 187}
293 188
294static void free_consistent_cluster(struct sbus_iommu *iommu, u32 base, unsigned long npages) 189static inline iopte_t *alloc_npages(struct iommu *iommu, unsigned long npages)
295{ 190{
296 iopte_t *iopte = iommu->page_table + ((base - MAP_BASE) >> IO_PAGE_SHIFT); 191 long entry;
297 192
298 if ((iopte - iommu->page_table) == iommu->lowest_consistent_map) { 193 entry = sbus_arena_alloc(iommu, npages);
299 iopte_t *walk = iopte + npages; 194 if (unlikely(entry < 0))
300 iopte_t *limit; 195 return NULL;
301 196
302 limit = iommu->page_table + CLUSTER_NPAGES; 197 return iommu->page_table + entry;
303 while (walk < limit) { 198}
304 if (iopte_val(*walk) != 0UL)
305 break;
306 walk++;
307 }
308 iommu->lowest_consistent_map =
309 (walk - iommu->page_table);
310 }
311 199
312 while (npages--) 200static inline void free_npages(struct iommu *iommu, dma_addr_t base, unsigned long npages)
313 *iopte++ = __iopte(0UL); 201{
202 sbus_arena_free(&iommu->arena, base >> IO_PAGE_SHIFT, npages);
314} 203}
315 204
316void *sbus_alloc_consistent(struct sbus_dev *sdev, size_t size, dma_addr_t *dvma_addr) 205void *sbus_alloc_consistent(struct sbus_dev *sdev, size_t size, dma_addr_t *dvma_addr)
317{ 206{
318 unsigned long order, first_page, flags; 207 struct sbus_info *info;
319 struct sbus_iommu *iommu; 208 struct iommu *iommu;
320 iopte_t *iopte; 209 iopte_t *iopte;
210 unsigned long flags, order, first_page;
321 void *ret; 211 void *ret;
322 int npages; 212 int npages;
323 213
324 if (size <= 0 || sdev == NULL || dvma_addr == NULL)
325 return NULL;
326
327 size = IO_PAGE_ALIGN(size); 214 size = IO_PAGE_ALIGN(size);
328 order = get_order(size); 215 order = get_order(size);
329 if (order >= 10) 216 if (order >= 10)
330 return NULL; 217 return NULL;
218
331 first_page = __get_free_pages(GFP_KERNEL|__GFP_COMP, order); 219 first_page = __get_free_pages(GFP_KERNEL|__GFP_COMP, order);
332 if (first_page == 0UL) 220 if (first_page == 0UL)
333 return NULL; 221 return NULL;
334 memset((char *)first_page, 0, PAGE_SIZE << order); 222 memset((char *)first_page, 0, PAGE_SIZE << order);
335 223
336 iommu = sdev->bus->iommu; 224 info = sdev->bus->iommu;
225 iommu = &info->iommu;
337 226
338 spin_lock_irqsave(&iommu->lock, flags); 227 spin_lock_irqsave(&iommu->lock, flags);
339 iopte = alloc_consistent_cluster(iommu, size >> IO_PAGE_SHIFT); 228 iopte = alloc_npages(iommu, size >> IO_PAGE_SHIFT);
340 if (iopte == NULL) { 229 spin_unlock_irqrestore(&iommu->lock, flags);
341 spin_unlock_irqrestore(&iommu->lock, flags); 230
231 if (unlikely(iopte == NULL)) {
342 free_pages(first_page, order); 232 free_pages(first_page, order);
343 return NULL; 233 return NULL;
344 } 234 }
345 235
346 /* Ok, we're committed at this point. */ 236 *dvma_addr = (iommu->page_table_map_base +
347 *dvma_addr = MAP_BASE + ((iopte - iommu->page_table) << IO_PAGE_SHIFT); 237 ((iopte - iommu->page_table) << IO_PAGE_SHIFT));
348 ret = (void *) first_page; 238 ret = (void *) first_page;
349 npages = size >> IO_PAGE_SHIFT; 239 npages = size >> IO_PAGE_SHIFT;
240 first_page = __pa(first_page);
350 while (npages--) { 241 while (npages--) {
351 *iopte++ = __iopte(IOPTE_VALID | IOPTE_CACHE | IOPTE_WRITE | 242 iopte_val(*iopte) = (IOPTE_VALID | IOPTE_CACHE |
352 (__pa(first_page) & IOPTE_PAGE)); 243 IOPTE_WRITE |
244 (first_page & IOPTE_PAGE));
245 iopte++;
353 first_page += IO_PAGE_SIZE; 246 first_page += IO_PAGE_SIZE;
354 } 247 }
355 iommu_flush(iommu, *dvma_addr, size >> IO_PAGE_SHIFT);
356 spin_unlock_irqrestore(&iommu->lock, flags);
357 248
358 return ret; 249 return ret;
359} 250}
360 251
361void sbus_free_consistent(struct sbus_dev *sdev, size_t size, void *cpu, dma_addr_t dvma) 252void sbus_free_consistent(struct sbus_dev *sdev, size_t size, void *cpu, dma_addr_t dvma)
362{ 253{
363 unsigned long order, npages; 254 struct sbus_info *info;
364 struct sbus_iommu *iommu; 255 struct iommu *iommu;
365 256 iopte_t *iopte;
366 if (size <= 0 || sdev == NULL || cpu == NULL) 257 unsigned long flags, order, npages;
367 return;
368 258
369 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT; 259 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
370 iommu = sdev->bus->iommu; 260 info = sdev->bus->iommu;
261 iommu = &info->iommu;
262 iopte = iommu->page_table +
263 ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
371 264
372 spin_lock_irq(&iommu->lock); 265 spin_lock_irqsave(&iommu->lock, flags);
373 free_consistent_cluster(iommu, dvma, npages); 266
374 iommu_flush(iommu, dvma, npages); 267 free_npages(iommu, dvma - iommu->page_table_map_base, npages);
375 spin_unlock_irq(&iommu->lock); 268
269 spin_unlock_irqrestore(&iommu->lock, flags);
376 270
377 order = get_order(size); 271 order = get_order(size);
378 if (order < 10) 272 if (order < 10)
379 free_pages((unsigned long)cpu, order); 273 free_pages((unsigned long)cpu, order);
380} 274}
381 275
382dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *ptr, size_t size, int dir) 276dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *ptr, size_t sz, int direction)
383{ 277{
384 struct sbus_iommu *iommu = sdev->bus->iommu; 278 struct sbus_info *info;
385 unsigned long npages, pbase, flags; 279 struct iommu *iommu;
386 iopte_t *iopte; 280 iopte_t *base;
387 u32 dma_base, offset; 281 unsigned long flags, npages, oaddr;
388 unsigned long iopte_bits; 282 unsigned long i, base_paddr;
389 283 u32 bus_addr, ret;
390 if (dir == SBUS_DMA_NONE) 284 unsigned long iopte_protection;
285
286 info = sdev->bus->iommu;
287 iommu = &info->iommu;
288
289 if (unlikely(direction == SBUS_DMA_NONE))
391 BUG(); 290 BUG();
392 291
393 pbase = (unsigned long) ptr; 292 oaddr = (unsigned long)ptr;
394 offset = (u32) (pbase & ~IO_PAGE_MASK); 293 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
395 size = (IO_PAGE_ALIGN(pbase + size) - (pbase & IO_PAGE_MASK)); 294 npages >>= IO_PAGE_SHIFT;
396 pbase = (unsigned long) __pa(pbase & IO_PAGE_MASK);
397 295
398 spin_lock_irqsave(&iommu->lock, flags); 296 spin_lock_irqsave(&iommu->lock, flags);
399 npages = size >> IO_PAGE_SHIFT; 297 base = alloc_npages(iommu, npages);
400 iopte = alloc_streaming_cluster(iommu, npages);
401 if (iopte == NULL)
402 goto bad;
403 dma_base = MAP_BASE + ((iopte - iommu->page_table) << IO_PAGE_SHIFT);
404 npages = size >> IO_PAGE_SHIFT;
405 iopte_bits = IOPTE_VALID | IOPTE_STBUF | IOPTE_CACHE;
406 if (dir != SBUS_DMA_TODEVICE)
407 iopte_bits |= IOPTE_WRITE;
408 while (npages--) {
409 *iopte++ = __iopte(iopte_bits | (pbase & IOPTE_PAGE));
410 pbase += IO_PAGE_SIZE;
411 }
412 npages = size >> IO_PAGE_SHIFT;
413 spin_unlock_irqrestore(&iommu->lock, flags); 298 spin_unlock_irqrestore(&iommu->lock, flags);
414 299
415 return (dma_base | offset); 300 if (unlikely(!base))
301 BUG();
416 302
417bad: 303 bus_addr = (iommu->page_table_map_base +
418 spin_unlock_irqrestore(&iommu->lock, flags); 304 ((base - iommu->page_table) << IO_PAGE_SHIFT));
419 BUG(); 305 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
420 return 0; 306 base_paddr = __pa(oaddr & IO_PAGE_MASK);
307
308 iopte_protection = IOPTE_VALID | IOPTE_STBUF | IOPTE_CACHE;
309 if (direction != SBUS_DMA_TODEVICE)
310 iopte_protection |= IOPTE_WRITE;
311
312 for (i = 0; i < npages; i++, base++, base_paddr += IO_PAGE_SIZE)
313 iopte_val(*base) = iopte_protection | base_paddr;
314
315 return ret;
421} 316}
422 317
423void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t dma_addr, size_t size, int direction) 318void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t bus_addr, size_t sz, int direction)
424{ 319{
425 struct sbus_iommu *iommu = sdev->bus->iommu; 320 struct sbus_info *info = sdev->bus->iommu;
426 u32 dma_base = dma_addr & IO_PAGE_MASK; 321 struct iommu *iommu = &info->iommu;
427 unsigned long flags; 322 struct strbuf *strbuf = &info->strbuf;
323 iopte_t *base;
324 unsigned long flags, npages, i;
325
326 if (unlikely(direction == SBUS_DMA_NONE))
327 BUG();
428 328
429 size = (IO_PAGE_ALIGN(dma_addr + size) - dma_base); 329 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
330 npages >>= IO_PAGE_SHIFT;
331 base = iommu->page_table +
332 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
333
334 bus_addr &= IO_PAGE_MASK;
430 335
431 spin_lock_irqsave(&iommu->lock, flags); 336 spin_lock_irqsave(&iommu->lock, flags);
432 free_streaming_cluster(iommu, dma_base, size >> IO_PAGE_SHIFT); 337 sbus_strbuf_flush(iommu, strbuf, bus_addr, npages, direction);
433 sbus_strbuf_flush(iommu, dma_base, size >> IO_PAGE_SHIFT, direction); 338 for (i = 0; i < npages; i++)
339 iopte_val(base[i]) = 0UL;
340 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
434 spin_unlock_irqrestore(&iommu->lock, flags); 341 spin_unlock_irqrestore(&iommu->lock, flags);
435} 342}
436 343
437#define SG_ENT_PHYS_ADDRESS(SG) \ 344#define SG_ENT_PHYS_ADDRESS(SG) \
438 (__pa(page_address((SG)->page)) + (SG)->offset) 345 (__pa(page_address((SG)->page)) + (SG)->offset)
439 346
440static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg, int nused, int nelems, unsigned long iopte_bits) 347static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg,
348 int nused, int nelems, unsigned long iopte_protection)
441{ 349{
442 struct scatterlist *dma_sg = sg; 350 struct scatterlist *dma_sg = sg;
443 struct scatterlist *sg_end = sg + nelems; 351 struct scatterlist *sg_end = sg + nelems;
@@ -462,7 +370,7 @@ static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg, int nused, in
462 for (;;) { 370 for (;;) {
463 unsigned long tmp; 371 unsigned long tmp;
464 372
465 tmp = (unsigned long) SG_ENT_PHYS_ADDRESS(sg); 373 tmp = SG_ENT_PHYS_ADDRESS(sg);
466 len = sg->length; 374 len = sg->length;
467 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) { 375 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
468 pteval = tmp & IO_PAGE_MASK; 376 pteval = tmp & IO_PAGE_MASK;
@@ -478,7 +386,7 @@ static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg, int nused, in
478 sg++; 386 sg++;
479 } 387 }
480 388
481 pteval = ((pteval & IOPTE_PAGE) | iopte_bits); 389 pteval = iopte_protection | (pteval & IOPTE_PAGE);
482 while (len > 0) { 390 while (len > 0) {
483 *iopte++ = __iopte(pteval); 391 *iopte++ = __iopte(pteval);
484 pteval += IO_PAGE_SIZE; 392 pteval += IO_PAGE_SIZE;
@@ -509,103 +417,121 @@ static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg, int nused, in
509 } 417 }
510} 418}
511 419
512int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int nents, int dir) 420int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sglist, int nelems, int direction)
513{ 421{
514 struct sbus_iommu *iommu = sdev->bus->iommu; 422 struct sbus_info *info;
515 unsigned long flags, npages; 423 struct iommu *iommu;
516 iopte_t *iopte; 424 unsigned long flags, npages, iopte_protection;
425 iopte_t *base;
517 u32 dma_base; 426 u32 dma_base;
518 struct scatterlist *sgtmp; 427 struct scatterlist *sgtmp;
519 int used; 428 int used;
520 unsigned long iopte_bits;
521
522 if (dir == SBUS_DMA_NONE)
523 BUG();
524 429
525 /* Fast path single entry scatterlists. */ 430 /* Fast path single entry scatterlists. */
526 if (nents == 1) { 431 if (nelems == 1) {
527 sg->dma_address = 432 sglist->dma_address =
528 sbus_map_single(sdev, 433 sbus_map_single(sdev,
529 (page_address(sg->page) + sg->offset), 434 (page_address(sglist->page) + sglist->offset),
530 sg->length, dir); 435 sglist->length, direction);
531 sg->dma_length = sg->length; 436 sglist->dma_length = sglist->length;
532 return 1; 437 return 1;
533 } 438 }
534 439
535 npages = prepare_sg(sg, nents); 440 info = sdev->bus->iommu;
441 iommu = &info->iommu;
442
443 if (unlikely(direction == SBUS_DMA_NONE))
444 BUG();
445
446 npages = prepare_sg(sglist, nelems);
536 447
537 spin_lock_irqsave(&iommu->lock, flags); 448 spin_lock_irqsave(&iommu->lock, flags);
538 iopte = alloc_streaming_cluster(iommu, npages); 449 base = alloc_npages(iommu, npages);
539 if (iopte == NULL) 450 spin_unlock_irqrestore(&iommu->lock, flags);
540 goto bad; 451
541 dma_base = MAP_BASE + ((iopte - iommu->page_table) << IO_PAGE_SHIFT); 452 if (unlikely(base == NULL))
453 BUG();
454
455 dma_base = iommu->page_table_map_base +
456 ((base - iommu->page_table) << IO_PAGE_SHIFT);
542 457
543 /* Normalize DVMA addresses. */ 458 /* Normalize DVMA addresses. */
544 sgtmp = sg; 459 used = nelems;
545 used = nents;
546 460
461 sgtmp = sglist;
547 while (used && sgtmp->dma_length) { 462 while (used && sgtmp->dma_length) {
548 sgtmp->dma_address += dma_base; 463 sgtmp->dma_address += dma_base;
549 sgtmp++; 464 sgtmp++;
550 used--; 465 used--;
551 } 466 }
552 used = nents - used; 467 used = nelems - used;
468
469 iopte_protection = IOPTE_VALID | IOPTE_STBUF | IOPTE_CACHE;
470 if (direction != SBUS_DMA_TODEVICE)
471 iopte_protection |= IOPTE_WRITE;
553 472
554 iopte_bits = IOPTE_VALID | IOPTE_STBUF | IOPTE_CACHE; 473 fill_sg(base, sglist, used, nelems, iopte_protection);
555 if (dir != SBUS_DMA_TODEVICE)
556 iopte_bits |= IOPTE_WRITE;
557 474
558 fill_sg(iopte, sg, used, nents, iopte_bits);
559#ifdef VERIFY_SG 475#ifdef VERIFY_SG
560 verify_sglist(sg, nents, iopte, npages); 476 verify_sglist(sglist, nelems, base, npages);
561#endif 477#endif
562 spin_unlock_irqrestore(&iommu->lock, flags);
563 478
564 return used; 479 return used;
565
566bad:
567 spin_unlock_irqrestore(&iommu->lock, flags);
568 BUG();
569 return 0;
570} 480}
571 481
572void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int nents, int direction) 482void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sglist, int nelems, int direction)
573{ 483{
574 unsigned long size, flags; 484 struct sbus_info *info;
575 struct sbus_iommu *iommu; 485 struct iommu *iommu;
576 u32 dvma_base; 486 struct strbuf *strbuf;
577 int i; 487 iopte_t *base;
488 unsigned long flags, i, npages;
489 u32 bus_addr;
490
491 if (unlikely(direction == SBUS_DMA_NONE))
492 BUG();
578 493
579 /* Fast path single entry scatterlists. */ 494 info = sdev->bus->iommu;
580 if (nents == 1) { 495 iommu = &info->iommu;
581 sbus_unmap_single(sdev, sg->dma_address, sg->dma_length, direction); 496 strbuf = &info->strbuf;
582 return;
583 }
584 497
585 dvma_base = sg[0].dma_address & IO_PAGE_MASK; 498 bus_addr = sglist->dma_address & IO_PAGE_MASK;
586 for (i = 0; i < nents; i++) { 499
587 if (sg[i].dma_length == 0) 500 for (i = 1; i < nelems; i++)
501 if (sglist[i].dma_length == 0)
588 break; 502 break;
589 }
590 i--; 503 i--;
591 size = IO_PAGE_ALIGN(sg[i].dma_address + sg[i].dma_length) - dvma_base; 504 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
505 bus_addr) >> IO_PAGE_SHIFT;
506
507 base = iommu->page_table +
508 ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
592 509
593 iommu = sdev->bus->iommu;
594 spin_lock_irqsave(&iommu->lock, flags); 510 spin_lock_irqsave(&iommu->lock, flags);
595 free_streaming_cluster(iommu, dvma_base, size >> IO_PAGE_SHIFT); 511 sbus_strbuf_flush(iommu, strbuf, bus_addr, npages, direction);
596 sbus_strbuf_flush(iommu, dvma_base, size >> IO_PAGE_SHIFT, direction); 512 for (i = 0; i < npages; i++)
513 iopte_val(base[i]) = 0UL;
514 free_npages(iommu, bus_addr - iommu->page_table_map_base, npages);
597 spin_unlock_irqrestore(&iommu->lock, flags); 515 spin_unlock_irqrestore(&iommu->lock, flags);
598} 516}
599 517
600void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t base, size_t size, int direction) 518void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t bus_addr, size_t sz, int direction)
601{ 519{
602 struct sbus_iommu *iommu = sdev->bus->iommu; 520 struct sbus_info *info;
603 unsigned long flags; 521 struct iommu *iommu;
522 struct strbuf *strbuf;
523 unsigned long flags, npages;
524
525 info = sdev->bus->iommu;
526 iommu = &info->iommu;
527 strbuf = &info->strbuf;
604 528
605 size = (IO_PAGE_ALIGN(base + size) - (base & IO_PAGE_MASK)); 529 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
530 npages >>= IO_PAGE_SHIFT;
531 bus_addr &= IO_PAGE_MASK;
606 532
607 spin_lock_irqsave(&iommu->lock, flags); 533 spin_lock_irqsave(&iommu->lock, flags);
608 sbus_strbuf_flush(iommu, base & IO_PAGE_MASK, size >> IO_PAGE_SHIFT, direction); 534 sbus_strbuf_flush(iommu, strbuf, bus_addr, npages, direction);
609 spin_unlock_irqrestore(&iommu->lock, flags); 535 spin_unlock_irqrestore(&iommu->lock, flags);
610} 536}
611 537
@@ -613,23 +539,29 @@ void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t base, siz
613{ 539{
614} 540}
615 541
616void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int nents, int direction) 542void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sglist, int nelems, int direction)
617{ 543{
618 struct sbus_iommu *iommu = sdev->bus->iommu; 544 struct sbus_info *info;
619 unsigned long flags, size; 545 struct iommu *iommu;
620 u32 base; 546 struct strbuf *strbuf;
621 int i; 547 unsigned long flags, npages, i;
622 548 u32 bus_addr;
623 base = sg[0].dma_address & IO_PAGE_MASK; 549
624 for (i = 0; i < nents; i++) { 550 info = sdev->bus->iommu;
625 if (sg[i].dma_length == 0) 551 iommu = &info->iommu;
552 strbuf = &info->strbuf;
553
554 bus_addr = sglist[0].dma_address & IO_PAGE_MASK;
555 for (i = 0; i < nelems; i++) {
556 if (!sglist[i].dma_length)
626 break; 557 break;
627 } 558 }
628 i--; 559 i--;
629 size = IO_PAGE_ALIGN(sg[i].dma_address + sg[i].dma_length) - base; 560 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length)
561 - bus_addr) >> IO_PAGE_SHIFT;
630 562
631 spin_lock_irqsave(&iommu->lock, flags); 563 spin_lock_irqsave(&iommu->lock, flags);
632 sbus_strbuf_flush(iommu, base, size >> IO_PAGE_SHIFT, direction); 564 sbus_strbuf_flush(iommu, strbuf, bus_addr, npages, direction);
633 spin_unlock_irqrestore(&iommu->lock, flags); 565 spin_unlock_irqrestore(&iommu->lock, flags);
634} 566}
635 567
@@ -640,12 +572,13 @@ void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg,
640/* Enable 64-bit DVMA mode for the given device. */ 572/* Enable 64-bit DVMA mode for the given device. */
641void sbus_set_sbus64(struct sbus_dev *sdev, int bursts) 573void sbus_set_sbus64(struct sbus_dev *sdev, int bursts)
642{ 574{
643 struct sbus_iommu *iommu = sdev->bus->iommu; 575 struct sbus_info *info = sdev->bus->iommu;
576 struct iommu *iommu = &info->iommu;
644 int slot = sdev->slot; 577 int slot = sdev->slot;
645 unsigned long cfg_reg; 578 unsigned long cfg_reg;
646 u64 val; 579 u64 val;
647 580
648 cfg_reg = iommu->sbus_control_reg; 581 cfg_reg = iommu->write_complete_reg;
649 switch (slot) { 582 switch (slot) {
650 case 0: 583 case 0:
651 cfg_reg += 0x20UL; 584 cfg_reg += 0x20UL;
@@ -780,8 +713,9 @@ static unsigned long sysio_imap_to_iclr(unsigned long imap)
780unsigned int sbus_build_irq(void *buscookie, unsigned int ino) 713unsigned int sbus_build_irq(void *buscookie, unsigned int ino)
781{ 714{
782 struct sbus_bus *sbus = (struct sbus_bus *)buscookie; 715 struct sbus_bus *sbus = (struct sbus_bus *)buscookie;
783 struct sbus_iommu *iommu = sbus->iommu; 716 struct sbus_info *info = sbus->iommu;
784 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL; 717 struct iommu *iommu = &info->iommu;
718 unsigned long reg_base = iommu->write_complete_reg - 0x2000UL;
785 unsigned long imap, iclr; 719 unsigned long imap, iclr;
786 int sbus_level = 0; 720 int sbus_level = 0;
787 721
@@ -842,8 +776,9 @@ unsigned int sbus_build_irq(void *buscookie, unsigned int ino)
842static irqreturn_t sysio_ue_handler(int irq, void *dev_id) 776static irqreturn_t sysio_ue_handler(int irq, void *dev_id)
843{ 777{
844 struct sbus_bus *sbus = dev_id; 778 struct sbus_bus *sbus = dev_id;
845 struct sbus_iommu *iommu = sbus->iommu; 779 struct sbus_info *info = sbus->iommu;
846 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL; 780 struct iommu *iommu = &info->iommu;
781 unsigned long reg_base = iommu->write_complete_reg - 0x2000UL;
847 unsigned long afsr_reg, afar_reg; 782 unsigned long afsr_reg, afar_reg;
848 unsigned long afsr, afar, error_bits; 783 unsigned long afsr, afar, error_bits;
849 int reported; 784 int reported;
@@ -914,8 +849,9 @@ static irqreturn_t sysio_ue_handler(int irq, void *dev_id)
914static irqreturn_t sysio_ce_handler(int irq, void *dev_id) 849static irqreturn_t sysio_ce_handler(int irq, void *dev_id)
915{ 850{
916 struct sbus_bus *sbus = dev_id; 851 struct sbus_bus *sbus = dev_id;
917 struct sbus_iommu *iommu = sbus->iommu; 852 struct sbus_info *info = sbus->iommu;
918 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL; 853 struct iommu *iommu = &info->iommu;
854 unsigned long reg_base = iommu->write_complete_reg - 0x2000UL;
919 unsigned long afsr_reg, afar_reg; 855 unsigned long afsr_reg, afar_reg;
920 unsigned long afsr, afar, error_bits; 856 unsigned long afsr, afar, error_bits;
921 int reported; 857 int reported;
@@ -991,12 +927,13 @@ static irqreturn_t sysio_ce_handler(int irq, void *dev_id)
991static irqreturn_t sysio_sbus_error_handler(int irq, void *dev_id) 927static irqreturn_t sysio_sbus_error_handler(int irq, void *dev_id)
992{ 928{
993 struct sbus_bus *sbus = dev_id; 929 struct sbus_bus *sbus = dev_id;
994 struct sbus_iommu *iommu = sbus->iommu; 930 struct sbus_info *info = sbus->iommu;
931 struct iommu *iommu = &info->iommu;
995 unsigned long afsr_reg, afar_reg, reg_base; 932 unsigned long afsr_reg, afar_reg, reg_base;
996 unsigned long afsr, afar, error_bits; 933 unsigned long afsr, afar, error_bits;
997 int reported; 934 int reported;
998 935
999 reg_base = iommu->sbus_control_reg - 0x2000UL; 936 reg_base = iommu->write_complete_reg - 0x2000UL;
1000 afsr_reg = reg_base + SYSIO_SBUS_AFSR; 937 afsr_reg = reg_base + SYSIO_SBUS_AFSR;
1001 afar_reg = reg_base + SYSIO_SBUS_AFAR; 938 afar_reg = reg_base + SYSIO_SBUS_AFAR;
1002 939
@@ -1058,8 +995,9 @@ static irqreturn_t sysio_sbus_error_handler(int irq, void *dev_id)
1058 995
1059static void __init sysio_register_error_handlers(struct sbus_bus *sbus) 996static void __init sysio_register_error_handlers(struct sbus_bus *sbus)
1060{ 997{
1061 struct sbus_iommu *iommu = sbus->iommu; 998 struct sbus_info *info = sbus->iommu;
1062 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL; 999 struct iommu *iommu = &info->iommu;
1000 unsigned long reg_base = iommu->write_complete_reg - 0x2000UL;
1063 unsigned int irq; 1001 unsigned int irq;
1064 u64 control; 1002 u64 control;
1065 1003
@@ -1093,18 +1031,20 @@ static void __init sysio_register_error_handlers(struct sbus_bus *sbus)
1093 SYSIO_ECNTRL_CEEN), 1031 SYSIO_ECNTRL_CEEN),
1094 reg_base + ECC_CONTROL); 1032 reg_base + ECC_CONTROL);
1095 1033
1096 control = upa_readq(iommu->sbus_control_reg); 1034 control = upa_readq(iommu->write_complete_reg);
1097 control |= 0x100UL; /* SBUS Error Interrupt Enable */ 1035 control |= 0x100UL; /* SBUS Error Interrupt Enable */
1098 upa_writeq(control, iommu->sbus_control_reg); 1036 upa_writeq(control, iommu->write_complete_reg);
1099} 1037}
1100 1038
1101/* Boot time initialization. */ 1039/* Boot time initialization. */
1102static void __init sbus_iommu_init(int __node, struct sbus_bus *sbus) 1040static void __init sbus_iommu_init(int __node, struct sbus_bus *sbus)
1103{ 1041{
1104 struct linux_prom64_registers *pr; 1042 const struct linux_prom64_registers *pr;
1105 struct device_node *dp; 1043 struct device_node *dp;
1106 struct sbus_iommu *iommu; 1044 struct sbus_info *info;
1107 unsigned long regs, tsb_base; 1045 struct iommu *iommu;
1046 struct strbuf *strbuf;
1047 unsigned long regs, reg_base;
1108 u64 control; 1048 u64 control;
1109 int i; 1049 int i;
1110 1050
@@ -1119,94 +1059,87 @@ static void __init sbus_iommu_init(int __node, struct sbus_bus *sbus)
1119 } 1059 }
1120 regs = pr->phys_addr; 1060 regs = pr->phys_addr;
1121 1061
1122 iommu = kmalloc(sizeof(*iommu) + SMP_CACHE_BYTES, GFP_ATOMIC); 1062 info = kzalloc(sizeof(*info), GFP_ATOMIC);
1123 if (iommu == NULL) { 1063 if (info == NULL) {
1124 prom_printf("sbus_iommu_init: Fatal error, kmalloc(iommu) failed\n"); 1064 prom_printf("sbus_iommu_init: Fatal error, "
1065 "kmalloc(info) failed\n");
1125 prom_halt(); 1066 prom_halt();
1126 } 1067 }
1127 1068
1128 /* Align on E$ line boundary. */ 1069 iommu = &info->iommu;
1129 iommu = (struct sbus_iommu *) 1070 strbuf = &info->strbuf;
1130 (((unsigned long)iommu + (SMP_CACHE_BYTES - 1UL)) &
1131 ~(SMP_CACHE_BYTES - 1UL));
1132 1071
1133 memset(iommu, 0, sizeof(*iommu)); 1072 reg_base = regs + SYSIO_IOMMUREG_BASE;
1073 iommu->iommu_control = reg_base + IOMMU_CONTROL;
1074 iommu->iommu_tsbbase = reg_base + IOMMU_TSBBASE;
1075 iommu->iommu_flush = reg_base + IOMMU_FLUSH;
1134 1076
1135 /* We start with no consistent mappings. */ 1077 reg_base = regs + SYSIO_STRBUFREG_BASE;
1136 iommu->lowest_consistent_map = CLUSTER_NPAGES; 1078 strbuf->strbuf_control = reg_base + STRBUF_CONTROL;
1079 strbuf->strbuf_pflush = reg_base + STRBUF_PFLUSH;
1080 strbuf->strbuf_fsync = reg_base + STRBUF_FSYNC;
1137 1081
1138 for (i = 0; i < NCLUSTERS; i++) { 1082 strbuf->strbuf_enabled = 1;
1139 iommu->alloc_info[i].flush = 0;
1140 iommu->alloc_info[i].next = 0;
1141 }
1142 1083
1143 /* Setup spinlock. */ 1084 strbuf->strbuf_flushflag = (volatile unsigned long *)
1144 spin_lock_init(&iommu->lock); 1085 ((((unsigned long)&strbuf->__flushflag_buf[0])
1145 1086 + 63UL)
1146 /* Init register offsets. */ 1087 & ~63UL);
1147 iommu->iommu_regs = regs + SYSIO_IOMMUREG_BASE; 1088 strbuf->strbuf_flushflag_pa = (unsigned long)
1148 iommu->strbuf_regs = regs + SYSIO_STRBUFREG_BASE; 1089 __pa(strbuf->strbuf_flushflag);
1149 1090
1150 /* The SYSIO SBUS control register is used for dummy reads 1091 /* The SYSIO SBUS control register is used for dummy reads
1151 * in order to ensure write completion. 1092 * in order to ensure write completion.
1152 */ 1093 */
1153 iommu->sbus_control_reg = regs + 0x2000UL; 1094 iommu->write_complete_reg = regs + 0x2000UL;
1154 1095
1155 /* Link into SYSIO software state. */ 1096 /* Link into SYSIO software state. */
1156 sbus->iommu = iommu; 1097 sbus->iommu = info;
1157 1098
1158 printk("SYSIO: UPA portID %x, at %016lx\n", 1099 printk("SYSIO: UPA portID %x, at %016lx\n",
1159 sbus->portid, regs); 1100 sbus->portid, regs);
1160 1101
1161 /* Setup for TSB_SIZE=7, TBW_SIZE=0, MMU_DE=1, MMU_EN=1 */ 1102 /* Setup for TSB_SIZE=7, TBW_SIZE=0, MMU_DE=1, MMU_EN=1 */
1162 control = upa_readq(iommu->iommu_regs + IOMMU_CONTROL); 1103 sbus_iommu_table_init(iommu, IO_TSB_SIZE);
1104
1105 control = upa_readq(iommu->iommu_control);
1163 control = ((7UL << 16UL) | 1106 control = ((7UL << 16UL) |
1164 (0UL << 2UL) | 1107 (0UL << 2UL) |
1165 (1UL << 1UL) | 1108 (1UL << 1UL) |
1166 (1UL << 0UL)); 1109 (1UL << 0UL));
1167 1110 upa_writeq(control, iommu->iommu_control);
1168 /* Using the above configuration we need 1MB iommu page
1169 * table (128K ioptes * 8 bytes per iopte). This is
1170 * page order 7 on UltraSparc.
1171 */
1172 tsb_base = __get_free_pages(GFP_ATOMIC, get_order(IO_TSB_SIZE));
1173 if (tsb_base == 0UL) {
1174 prom_printf("sbus_iommu_init: Fatal error, cannot alloc TSB table.\n");
1175 prom_halt();
1176 }
1177
1178 iommu->page_table = (iopte_t *) tsb_base;
1179 memset(iommu->page_table, 0, IO_TSB_SIZE);
1180
1181 upa_writeq(control, iommu->iommu_regs + IOMMU_CONTROL);
1182 1111
1183 /* Clean out any cruft in the IOMMU using 1112 /* Clean out any cruft in the IOMMU using
1184 * diagnostic accesses. 1113 * diagnostic accesses.
1185 */ 1114 */
1186 for (i = 0; i < 16; i++) { 1115 for (i = 0; i < 16; i++) {
1187 unsigned long dram = iommu->iommu_regs + IOMMU_DRAMDIAG; 1116 unsigned long dram, tag;
1188 unsigned long tag = iommu->iommu_regs + IOMMU_TAGDIAG; 1117
1118 dram = iommu->iommu_control + (IOMMU_DRAMDIAG - IOMMU_CONTROL);
1119 tag = iommu->iommu_control + (IOMMU_TAGDIAG - IOMMU_CONTROL);
1189 1120
1190 dram += (unsigned long)i * 8UL; 1121 dram += (unsigned long)i * 8UL;
1191 tag += (unsigned long)i * 8UL; 1122 tag += (unsigned long)i * 8UL;
1192 upa_writeq(0, dram); 1123 upa_writeq(0, dram);
1193 upa_writeq(0, tag); 1124 upa_writeq(0, tag);
1194 } 1125 }
1195 upa_readq(iommu->sbus_control_reg); 1126 upa_readq(iommu->write_complete_reg);
1196 1127
1197 /* Give the TSB to SYSIO. */ 1128 /* Give the TSB to SYSIO. */
1198 upa_writeq(__pa(tsb_base), iommu->iommu_regs + IOMMU_TSBBASE); 1129 upa_writeq(__pa(iommu->page_table), iommu->iommu_tsbbase);
1199 1130
1200 /* Setup streaming buffer, DE=1 SB_EN=1 */ 1131 /* Setup streaming buffer, DE=1 SB_EN=1 */
1201 control = (1UL << 1UL) | (1UL << 0UL); 1132 control = (1UL << 1UL) | (1UL << 0UL);
1202 upa_writeq(control, iommu->strbuf_regs + STRBUF_CONTROL); 1133 upa_writeq(control, strbuf->strbuf_control);
1203 1134
1204 /* Clear out the tags using diagnostics. */ 1135 /* Clear out the tags using diagnostics. */
1205 for (i = 0; i < 16; i++) { 1136 for (i = 0; i < 16; i++) {
1206 unsigned long ptag, ltag; 1137 unsigned long ptag, ltag;
1207 1138
1208 ptag = iommu->strbuf_regs + STRBUF_PTAGDIAG; 1139 ptag = strbuf->strbuf_control +
1209 ltag = iommu->strbuf_regs + STRBUF_LTAGDIAG; 1140 (STRBUF_PTAGDIAG - STRBUF_CONTROL);
1141 ltag = strbuf->strbuf_control +
1142 (STRBUF_LTAGDIAG - STRBUF_CONTROL);
1210 ptag += (unsigned long)i * 8UL; 1143 ptag += (unsigned long)i * 8UL;
1211 ltag += (unsigned long)i * 8UL; 1144 ltag += (unsigned long)i * 8UL;
1212 1145
@@ -1215,9 +1148,9 @@ static void __init sbus_iommu_init(int __node, struct sbus_bus *sbus)
1215 } 1148 }
1216 1149
1217 /* Enable DVMA arbitration for all devices/slots. */ 1150 /* Enable DVMA arbitration for all devices/slots. */
1218 control = upa_readq(iommu->sbus_control_reg); 1151 control = upa_readq(iommu->write_complete_reg);
1219 control |= 0x3fUL; 1152 control |= 0x3fUL;
1220 upa_writeq(control, iommu->sbus_control_reg); 1153 upa_writeq(control, iommu->write_complete_reg);
1221 1154
1222 /* Now some Xfire specific grot... */ 1155 /* Now some Xfire specific grot... */
1223 if (this_is_starfire) 1156 if (this_is_starfire)
@@ -1229,7 +1162,7 @@ static void __init sbus_iommu_init(int __node, struct sbus_bus *sbus)
1229void sbus_fill_device_irq(struct sbus_dev *sdev) 1162void sbus_fill_device_irq(struct sbus_dev *sdev)
1230{ 1163{
1231 struct device_node *dp = of_find_node_by_phandle(sdev->prom_node); 1164 struct device_node *dp = of_find_node_by_phandle(sdev->prom_node);
1232 struct linux_prom_irqs *irqs; 1165 const struct linux_prom_irqs *irqs;
1233 1166
1234 irqs = of_get_property(dp, "interrupts", NULL); 1167 irqs = of_get_property(dp, "interrupts", NULL);
1235 if (!irqs) { 1168 if (!irqs) {