1
2
3
4
5
6
7#include <linux/kernel.h>
8#include <linux/types.h>
9#include <linux/mm.h>
10#include <linux/spinlock.h>
11#include <linux/slab.h>
12#include <linux/init.h>
13
14#include <asm/page.h>
15#include <asm/sbus.h>
16#include <asm/io.h>
17#include <asm/upa.h>
18#include <asm/cache.h>
19#include <asm/dma.h>
20#include <asm/irq.h>
21#include <asm/starfire.h>
22
23#include "iommu_common.h"
24
25
26
27
28
29
30
31
32
33
34
35
36#define NCLUSTERS 8UL
37#define ONE_GIG (1UL * 1024UL * 1024UL * 1024UL)
38#define CLUSTER_SIZE (ONE_GIG / NCLUSTERS)
39#define CLUSTER_MASK (CLUSTER_SIZE - 1)
40#define CLUSTER_NPAGES (CLUSTER_SIZE >> IO_PAGE_SHIFT)
41#define MAP_BASE ((u32)0xc0000000)
42
43struct sbus_iommu {
44spinlock_t lock;
45
46iopte_t *page_table;
47unsigned long strbuf_regs;
48unsigned long iommu_regs;
49unsigned long sbus_control_reg;
50
51volatile unsigned long strbuf_flushflag;
52
53
54
55
56
57struct {
58 u16 next;
59 u16 flush;
60 } alloc_info[NCLUSTERS];
61
62
63
64
65
66u32 lowest_consistent_map;
67};
68
69
70#define SYSIO_IOMMUREG_BASE 0x2400UL
71#define IOMMU_CONTROL (0x2400UL - 0x2400UL)
72#define IOMMU_TSBBASE (0x2408UL - 0x2400UL)
73#define IOMMU_FLUSH (0x2410UL - 0x2400UL)
74#define IOMMU_VADIAG (0x4400UL - 0x2400UL)
75#define IOMMU_TAGCMP (0x4408UL - 0x2400UL)
76#define IOMMU_LRUDIAG (0x4500UL - 0x2400UL)
77#define IOMMU_TAGDIAG (0x4580UL - 0x2400UL)
78#define IOMMU_DRAMDIAG (0x4600UL - 0x2400UL)
79
80#define IOMMU_DRAM_VALID (1UL << 30UL)
81
82static void __iommu_flushall(struct sbus_iommu *iommu)
83{
84 unsigned long tag = iommu->iommu_regs + IOMMU_TAGDIAG;
85 int entry;
86
87 for (entry = 0; entry < 16; entry++) {
88 upa_writeq(0, tag);
89 tag += 8UL;
90 }
91 upa_readq(iommu->sbus_control_reg);
92
93 for (entry = 0; entry < NCLUSTERS; entry++) {
94 iommu->alloc_info[entry].flush =
95 iommu->alloc_info[entry].next;
96 }
97}
98
99static void iommu_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages)
100{
101 while (npages--)
102 upa_writeq(base + (npages << IO_PAGE_SHIFT),
103 iommu->iommu_regs + IOMMU_FLUSH);
104 upa_readq(iommu->sbus_control_reg);
105}
106
107
108#define SYSIO_STRBUFREG_BASE 0x2800UL
109#define STRBUF_CONTROL (0x2800UL - 0x2800UL)
110#define STRBUF_PFLUSH (0x2808UL - 0x2800UL)
111#define STRBUF_FSYNC (0x2810UL - 0x2800UL)
112#define STRBUF_DRAMDIAG (0x5000UL - 0x2800UL)
113#define STRBUF_ERRDIAG (0x5400UL - 0x2800UL)
114#define STRBUF_PTAGDIAG (0x5800UL - 0x2800UL)
115#define STRBUF_LTAGDIAG (0x5900UL - 0x2800UL)
116
117#define STRBUF_TAG_VALID 0x02UL
118
119static void strbuf_flush(struct sbus_iommu *iommu, u32 base, unsigned long npages)
120{
121 iommu->strbuf_flushflag = 0UL;
122 while (npages--)
123 upa_writeq(base + (npages << IO_PAGE_SHIFT),
124 iommu->strbuf_regs + STRBUF_PFLUSH);
125
126
127 upa_writeq(__pa(&iommu->strbuf_flushflag),
128 iommu->strbuf_regs + STRBUF_FSYNC);
129 upa_readq(iommu->sbus_control_reg);
130 while (iommu->strbuf_flushflag == 0UL)
131 membar("#LoadLoad");
132}
133
134static iopte_t *alloc_streaming_cluster(struct sbus_iommu *iommu, unsigned long npages)
135{
136 iopte_t *iopte, *limit, *first, *cluster;
137 unsigned long cnum, ent, nent, flush_point, found;
138
139 cnum = 0;
140 nent = 1;
141 while ((1UL << cnum) < npages)
142 cnum++;
143 if(cnum >= NCLUSTERS) {
144 nent = 1UL << (cnum - NCLUSTERS);
145 cnum = NCLUSTERS - 1;
146 }
147 iopte = iommu->page_table + (cnum * CLUSTER_NPAGES);
148
149 if (cnum == 0)
150 limit = (iommu->page_table +
151 iommu->lowest_consistent_map);
152 else
153 limit = (iopte + CLUSTER_NPAGES);
154
155 iopte += ((ent = iommu->alloc_info[cnum].next) << cnum);
156 flush_point = iommu->alloc_info[cnum].flush;
157
158 first = iopte;
159 cluster = NULL;
160 found = 0;
161 for (;;) {
162 if (iopte_val(*iopte) == 0UL) {
163 found++;
164 if (!cluster)
165 cluster = iopte;
166 } else {
167
168 cluster = NULL;
169 found = 0;
170 }
171
172 if (found == nent)
173 break;
174
175 iopte += (1 << cnum);
176 ent++;
177 if (iopte >= limit) {
178 iopte = (iommu->page_table + (cnum * CLUSTER_NPAGES));
179 ent = 0;
180
181
182 cluster = NULL;
183 found = 0;
184 }
185 if (ent == flush_point)
186 __iommu_flushall(iommu);
187 if (iopte == first)
188 goto bad;
189 }
190
191
192
193
194 if ((iopte + (1 << cnum)) >= limit)
195 ent = 0;
196 else
197 ent = ent + 1;
198 iommu->alloc_info[cnum].next = ent;
199 if (ent == flush_point)
200 __iommu_flushall(iommu);
201
202
203 return cluster;
204
205bad:
206 printk(KERN_EMERG "sbus: alloc_streaming_cluster of npages(%ld) failed!\n",
207 npages);
208 return NULL;
209}
210
211static void free_streaming_cluster(struct sbus_iommu *iommu, u32 base, unsigned long npages)
212{
213 unsigned long cnum, ent, nent;
214 iopte_t *iopte;
215
216 cnum = 0;
217 nent = 1;
218 while ((1UL << cnum) < npages)
219 cnum++;
220 if(cnum >= NCLUSTERS) {
221 nent = 1UL << (cnum - NCLUSTERS);
222 cnum = NCLUSTERS - 1;
223 }
224 ent = (base & CLUSTER_MASK) >> (IO_PAGE_SHIFT + cnum);
225 iopte = iommu->page_table + ((base - MAP_BASE) >> IO_PAGE_SHIFT);
226 do {
227 iopte_val(*iopte) = 0UL;
228 iopte += 1 << cnum;
229 } while(--nent);
230
231
232
233
234
235#define between(X,Y,Z) (((Z) - (Y)) >= ((X) - (Y)))
236 if (between(ent, iommu->alloc_info[cnum].next, iommu->alloc_info[cnum].flush))
237 iommu->alloc_info[cnum].flush = ent;
238#undef between
239}
240
241
242static iopte_t *alloc_consistent_cluster(struct sbus_iommu *iommu, unsigned long npages)
243{
244 iopte_t *iopte;
245
246 iopte = iommu->page_table + (1 * CLUSTER_NPAGES);
247 while (iopte > iommu->page_table) {
248 iopte--;
249 if (!(iopte_val(*iopte) & IOPTE_VALID)) {
250 unsigned long tmp = npages;
251
252 while (--tmp) {
253 iopte--;
254 if (iopte_val(*iopte) & IOPTE_VALID)
255 break;
256 }
257 if (tmp == 0) {
258 u32 entry = (iopte - iommu->page_table);
259
260 if (entry < iommu->lowest_consistent_map)
261 iommu->lowest_consistent_map = entry;
262 return iopte;
263 }
264 }
265 }
266 return NULL;
267}
268
269static void free_consistent_cluster(struct sbus_iommu *iommu, u32 base, unsigned long npages)
270{
271 iopte_t *iopte = iommu->page_table + ((base - MAP_BASE) >> IO_PAGE_SHIFT);
272
273 if ((iopte - iommu->page_table) == iommu->lowest_consistent_map) {
274 iopte_t *walk = iopte + npages;
275 iopte_t *limit;
276
277 limit = iommu->page_table + CLUSTER_NPAGES;
278 while (walk < limit) {
279 if (iopte_val(*walk) != 0UL)
280 break;
281 walk++;
282 }
283 iommu->lowest_consistent_map =
284 (walk - iommu->page_table);
285 }
286
287 while (npages--)
288 *iopte++ = __iopte(0UL);
289}
290
291void *sbus_alloc_consistent(struct sbus_dev *sdev, size_t size, dma_addr_t *dvma_addr)
292{
293 unsigned long order, first_page, flags;
294 struct sbus_iommu *iommu;
295 iopte_t *iopte;
296 void *ret;
297 int npages;
298
299 if (size <= 0 || sdev == NULL || dvma_addr == NULL)
300 return NULL;
301
302 size = IO_PAGE_ALIGN(size);
303 order = get_order(size);
304 if (order >= 10)
305 return NULL;
306 first_page = __get_free_pages(GFP_KERNEL, order);
307 if (first_page == 0UL)
308 return NULL;
309 memset((char *)first_page, 0, PAGE_SIZE << order);
310
311 iommu = sdev->bus->iommu;
312
313 spin_lock_irqsave(&iommu->lock, flags);
314 iopte = alloc_consistent_cluster(iommu, size >> IO_PAGE_SHIFT);
315 if (iopte == NULL) {
316 spin_unlock_irqrestore(&iommu->lock, flags);
317 free_pages(first_page, order);
318 return NULL;
319 }
320
321
322 *dvma_addr = MAP_BASE + ((iopte - iommu->page_table) << IO_PAGE_SHIFT);
323 ret = (void *) first_page;
324 npages = size >> IO_PAGE_SHIFT;
325 while (npages--) {
326 *iopte++ = __iopte(IOPTE_VALID | IOPTE_CACHE | IOPTE_WRITE |
327 (__pa(first_page) & IOPTE_PAGE));
328 first_page += IO_PAGE_SIZE;
329 }
330 iommu_flush(iommu, *dvma_addr, size >> IO_PAGE_SHIFT);
331 spin_unlock_irqrestore(&iommu->lock, flags);
332
333 return ret;
334}
335
336void sbus_free_consistent(struct sbus_dev *sdev, size_t size, void *cpu, dma_addr_t dvma)
337{
338 unsigned long order, npages;
339 struct sbus_iommu *iommu;
340
341 if (size <= 0 || sdev == NULL || cpu == NULL)
342 return;
343
344 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
345 iommu = sdev->bus->iommu;
346
347 spin_lock_irq(&iommu->lock);
348 free_consistent_cluster(iommu, dvma, npages);
349 iommu_flush(iommu, dvma, npages);
350 spin_unlock_irq(&iommu->lock);
351
352 order = get_order(size);
353 if (order < 10)
354 free_pages((unsigned long)cpu, order);
355}
356
357dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *ptr, size_t size, int dir)
358{
359 struct sbus_iommu *iommu = sdev->bus->iommu;
360 unsigned long npages, pbase, flags;
361 iopte_t *iopte;
362 u32 dma_base, offset;
363 unsigned long iopte_bits;
364
365 if (dir == SBUS_DMA_NONE)
366 BUG();
367
368 pbase = (unsigned long) ptr;
369 offset = (u32) (pbase & ~IO_PAGE_MASK);
370 size = (IO_PAGE_ALIGN(pbase + size) - (pbase & IO_PAGE_MASK));
371 pbase = (unsigned long) __pa(pbase & IO_PAGE_MASK);
372
373 spin_lock_irqsave(&iommu->lock, flags);
374 npages = size >> IO_PAGE_SHIFT;
375 iopte = alloc_streaming_cluster(iommu, npages);
376 if (iopte == NULL)
377 goto bad;
378 dma_base = MAP_BASE + ((iopte - iommu->page_table) << IO_PAGE_SHIFT);
379 npages = size >> IO_PAGE_SHIFT;
380 iopte_bits = IOPTE_VALID | IOPTE_STBUF | IOPTE_CACHE;
381 if (dir != SBUS_DMA_TODEVICE)
382 iopte_bits |= IOPTE_WRITE;
383 while (npages--) {
384 *iopte++ = __iopte(iopte_bits | (pbase & IOPTE_PAGE));
385 pbase += IO_PAGE_SIZE;
386 }
387 npages = size >> IO_PAGE_SHIFT;
388 spin_unlock_irqrestore(&iommu->lock, flags);
389
390 return (dma_base | offset);
391
392bad:
393 spin_unlock_irqrestore(&iommu->lock, flags);
394 BUG();
395 return 0;
396}
397
398void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t dma_addr, size_t size, int direction)
399{
400 struct sbus_iommu *iommu = sdev->bus->iommu;
401 u32 dma_base = dma_addr & IO_PAGE_MASK;
402 unsigned long flags;
403
404 size = (IO_PAGE_ALIGN(dma_addr + size) - dma_base);
405
406 spin_lock_irqsave(&iommu->lock, flags);
407 free_streaming_cluster(iommu, dma_base, size >> IO_PAGE_SHIFT);
408 strbuf_flush(iommu, dma_base, size >> IO_PAGE_SHIFT);
409 spin_unlock_irqrestore(&iommu->lock, flags);
410}
411
412#define SG_ENT_PHYS_ADDRESS(SG) \
413 ((SG)->address ? \
414 __pa((SG)->address) : \
415 (__pa(page_address((SG)->page)) + (SG)->offset))
416
417static inline void fill_sg(iopte_t *iopte, struct scatterlist *sg, int nused, int nelems, unsigned long iopte_bits)
418{
419 struct scatterlist *dma_sg = sg;
420 struct scatterlist *sg_end = sg + nelems;
421 int i;
422
423 for (i = 0; i < nused; i++) {
424 unsigned long pteval = ~0UL;
425 u32 dma_npages;
426
427 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
428 dma_sg->dma_length +
429 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
430 do {
431 unsigned long offset;
432 signed int len;
433
434
435
436
437
438
439 for (;;) {
440 unsigned long tmp;
441
442 tmp = (unsigned long) SG_ENT_PHYS_ADDRESS(sg);
443 len = sg->length;
444 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
445 pteval = tmp & IO_PAGE_MASK;
446 offset = tmp & (IO_PAGE_SIZE - 1UL);
447 break;
448 }
449 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
450 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
451 offset = 0UL;
452 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
453 break;
454 }
455 sg++;
456 }
457
458 pteval = ((pteval & IOPTE_PAGE) | iopte_bits);
459 while (len > 0) {
460 *iopte++ = __iopte(pteval);
461 pteval += IO_PAGE_SIZE;
462 len -= (IO_PAGE_SIZE - offset);
463 offset = 0;
464 dma_npages--;
465 }
466
467 pteval = (pteval & IOPTE_PAGE) + len;
468 sg++;
469
470
471
472
473
474 while (sg < sg_end &&
475 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
476 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
477 ((pteval ^
478 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
479 pteval += sg->length;
480 sg++;
481 }
482 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
483 pteval = ~0UL;
484 } while (dma_npages != 0);
485 dma_sg++;
486 }
487}
488
489int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int nents, int dir)
490{
491 struct sbus_iommu *iommu = sdev->bus->iommu;
492 unsigned long flags, npages;
493 iopte_t *iopte;
494 u32 dma_base;
495 struct scatterlist *sgtmp;
496 int used;
497 unsigned long iopte_bits;
498
499 if (dir == SBUS_DMA_NONE)
500 BUG();
501
502
503 if (nents == 1) {
504 sg->dma_address =
505 sbus_map_single(sdev,
506 (sg->address ?
507 sg->address :
508 (page_address(sg->page) + sg->offset)),
509 sg->length, dir);
510 sg->dma_length = sg->length;
511 return 1;
512 }
513
514 npages = prepare_sg(sg, nents);
515
516 spin_lock_irqsave(&iommu->lock, flags);
517 iopte = alloc_streaming_cluster(iommu, npages);
518 if (iopte == NULL)
519 goto bad;
520 dma_base = MAP_BASE + ((iopte - iommu->page_table) << IO_PAGE_SHIFT);
521
522
523 sgtmp = sg;
524 used = nents;
525
526 while (used && sgtmp->dma_length) {
527 sgtmp->dma_address += dma_base;
528 sgtmp++;
529 used--;
530 }
531 used = nents - used;
532
533 iopte_bits = IOPTE_VALID | IOPTE_STBUF | IOPTE_CACHE;
534 if (dir != SBUS_DMA_TODEVICE)
535 iopte_bits |= IOPTE_WRITE;
536
537 fill_sg(iopte, sg, used, nents, iopte_bits);
538#ifdef VERIFY_SG
539 verify_sglist(sg, nents, iopte, npages);
540#endif
541 spin_unlock_irqrestore(&iommu->lock, flags);
542
543 return used;
544
545bad:
546 spin_unlock_irqrestore(&iommu->lock, flags);
547 BUG();
548 return 0;
549}
550
551void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int nents, int direction)
552{
553 unsigned long size, flags;
554 struct sbus_iommu *iommu;
555 u32 dvma_base;
556 int i;
557
558
559 if (nents == 1) {
560 sbus_unmap_single(sdev, sg->dma_address, sg->dma_length, direction);
561 return;
562 }
563
564 dvma_base = sg[0].dma_address & IO_PAGE_MASK;
565 for (i = 0; i < nents; i++) {
566 if (sg[i].dma_length == 0)
567 break;
568 }
569 i--;
570 size = IO_PAGE_ALIGN(sg[i].dma_address + sg[i].dma_length) - dvma_base;
571
572 iommu = sdev->bus->iommu;
573 spin_lock_irqsave(&iommu->lock, flags);
574 free_streaming_cluster(iommu, dvma_base, size >> IO_PAGE_SHIFT);
575 strbuf_flush(iommu, dvma_base, size >> IO_PAGE_SHIFT);
576 spin_unlock_irqrestore(&iommu->lock, flags);
577}
578
579void sbus_dma_sync_single(struct sbus_dev *sdev, dma_addr_t base, size_t size, int direction)
580{
581 struct sbus_iommu *iommu = sdev->bus->iommu;
582 unsigned long flags;
583
584 size = (IO_PAGE_ALIGN(base + size) - (base & IO_PAGE_MASK));
585
586 spin_lock_irqsave(&iommu->lock, flags);
587 strbuf_flush(iommu, base & IO_PAGE_MASK, size >> IO_PAGE_SHIFT);
588 spin_unlock_irqrestore(&iommu->lock, flags);
589}
590
591void sbus_dma_sync_sg(struct sbus_dev *sdev, struct scatterlist *sg, int nents, int direction)
592{
593 struct sbus_iommu *iommu = sdev->bus->iommu;
594 unsigned long flags, size;
595 u32 base;
596 int i;
597
598 base = sg[0].dma_address & IO_PAGE_MASK;
599 for (i = 0; i < nents; i++) {
600 if (sg[i].dma_length == 0)
601 break;
602 }
603 i--;
604 size = IO_PAGE_ALIGN(sg[i].dma_address + sg[i].dma_length) - base;
605
606 spin_lock_irqsave(&iommu->lock, flags);
607 strbuf_flush(iommu, base, size >> IO_PAGE_SHIFT);
608 spin_unlock_irqrestore(&iommu->lock, flags);
609}
610
611
612void sbus_set_sbus64(struct sbus_dev *sdev, int bursts)
613{
614 struct sbus_iommu *iommu = sdev->bus->iommu;
615 int slot = sdev->slot;
616 unsigned long cfg_reg;
617 u64 val;
618
619 cfg_reg = iommu->sbus_control_reg;
620 switch (slot) {
621 case 0:
622 cfg_reg += 0x20UL;
623 break;
624 case 1:
625 cfg_reg += 0x28UL;
626 break;
627 case 2:
628 cfg_reg += 0x30UL;
629 break;
630 case 3:
631 cfg_reg += 0x38UL;
632 break;
633 case 13:
634 cfg_reg += 0x40UL;
635 break;
636 case 14:
637 cfg_reg += 0x48UL;
638 break;
639 case 15:
640 cfg_reg += 0x50UL;
641 break;
642
643 default:
644 return;
645 };
646
647 val = upa_readq(cfg_reg);
648 if (val & (1UL << 14UL)) {
649
650 return;
651 }
652
653 val |= (1UL << 14UL);
654
655 if (bursts & DMA_BURST8)
656 val |= (1UL << 1UL);
657 if (bursts & DMA_BURST16)
658 val |= (1UL << 2UL);
659 if (bursts & DMA_BURST32)
660 val |= (1UL << 3UL);
661 if (bursts & DMA_BURST64)
662 val |= (1UL << 4UL);
663 upa_writeq(val, cfg_reg);
664}
665
666
667static unsigned char sysio_ino_to_pil[] = {
668 0, 4, 4, 7, 5, 7, 8, 9,
669 0, 4, 4, 7, 5, 7, 8, 9,
670 0, 4, 4, 7, 5, 7, 8, 9,
671 0, 4, 4, 7, 5, 7, 8, 9,
672 4,
673 5,
674 8,
675 0,
676 13,
67715,
678 0,
679 0,
680 12,
681 11,
682 0,
683 0,
684 0,
685 0,
686 0, 0,
687 10,
688 11,
689 0, 0,
690 15,
691 15,
692 15,
693 0,
694};
695
696
697
698
699
700#define SYSIO_IMAP_SLOT0 0x2c04UL
701#define SYSIO_IMAP_SLOT1 0x2c0cUL
702#define SYSIO_IMAP_SLOT2 0x2c14UL
703#define SYSIO_IMAP_SLOT3 0x2c1cUL
704#define SYSIO_IMAP_SCSI 0x3004UL
705#define SYSIO_IMAP_ETH 0x300cUL
706#define SYSIO_IMAP_BPP 0x3014UL
707#define SYSIO_IMAP_AUDIO 0x301cUL
708#define SYSIO_IMAP_PFAIL 0x3024UL
709#define SYSIO_IMAP_KMS 0x302cUL
710#define SYSIO_IMAP_FLPY 0x3034UL
711#define SYSIO_IMAP_SHW 0x303cUL
712#define SYSIO_IMAP_KBD 0x3044UL
713#define SYSIO_IMAP_MS 0x304cUL
714#define SYSIO_IMAP_SER 0x3054UL
715#define SYSIO_IMAP_TIM0 0x3064UL
716#define SYSIO_IMAP_TIM1 0x306cUL
717#define SYSIO_IMAP_UE 0x3074UL
718#define SYSIO_IMAP_CE 0x307cUL
719#define SYSIO_IMAP_SBERR 0x3084UL
720#define SYSIO_IMAP_PMGMT 0x308cUL
721#define SYSIO_IMAP_GFX 0x3094UL
722#define SYSIO_IMAP_EUPA 0x309cUL
723
724#define bogon ((unsigned long) -1)
725static unsigned long sysio_irq_offsets[] = {
726
727 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
728 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
729 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
730 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
731 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
732 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
733 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
734 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
735
736
737 SYSIO_IMAP_SCSI,
738 SYSIO_IMAP_ETH,
739 SYSIO_IMAP_BPP,
740 bogon,
741 SYSIO_IMAP_AUDIO,
742 SYSIO_IMAP_PFAIL,
743 bogon,
744 bogon,
745 SYSIO_IMAP_KMS,
746 SYSIO_IMAP_FLPY,
747 SYSIO_IMAP_SHW,
748 SYSIO_IMAP_KBD,
749 SYSIO_IMAP_MS,
750 SYSIO_IMAP_SER,
751 bogon,
752 bogon,
753 SYSIO_IMAP_TIM0,
754 SYSIO_IMAP_TIM1,
755 bogon,
756 bogon,
757 SYSIO_IMAP_UE,
758 SYSIO_IMAP_CE,
759 SYSIO_IMAP_SBERR,
760 SYSIO_IMAP_PMGMT,
761};
762
763#undef bogon
764
765#define NUM_SYSIO_OFFSETS (sizeof(sysio_irq_offsets) / sizeof(sysio_irq_offsets[0]))
766
767
768
769
770#define SYSIO_ICLR_UNUSED0 0x3400UL
771#define SYSIO_ICLR_SLOT0 0x340cUL
772#define SYSIO_ICLR_SLOT1 0x344cUL
773#define SYSIO_ICLR_SLOT2 0x348cUL
774#define SYSIO_ICLR_SLOT3 0x34ccUL
775static unsigned long sysio_imap_to_iclr(unsigned long imap)
776{
777 unsigned long diff = SYSIO_ICLR_UNUSED0 - SYSIO_IMAP_SLOT0;
778 return imap + diff;
779}
780
781unsigned int sbus_build_irq(void *buscookie, unsigned int ino)
782{
783 struct sbus_bus *sbus = (struct sbus_bus *)buscookie;
784 struct sbus_iommu *iommu = sbus->iommu;
785 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL;
786 unsigned long imap, iclr;
787 int pil, sbus_level = 0;
788
789 pil = sysio_ino_to_pil[ino];
790 if (!pil) {
791 printk("sbus_irq_build: Bad SYSIO INO[%x]\n", ino);
792 panic("Bad SYSIO IRQ translations...");
793 }
794
795 if (PIL_RESERVED(pil))
796 BUG();
797
798 imap = sysio_irq_offsets[ino];
799 if (imap == ((unsigned long)-1)) {
800 prom_printf("get_irq_translations: Bad SYSIO INO[%x] cpu[%d]\n",
801 ino, pil);
802 prom_halt();
803 }
804 imap += reg_base;
805
806
807
808
809
810 if (ino >= 0x20) {
811 iclr = sysio_imap_to_iclr(imap);
812 } else {
813 int sbus_slot = (ino & 0x18)>>3;
814
815 sbus_level = ino & 0x7;
816
817 switch(sbus_slot) {
818 case 0:
819 iclr = reg_base + SYSIO_ICLR_SLOT0;
820 break;
821 case 1:
822 iclr = reg_base + SYSIO_ICLR_SLOT1;
823 break;
824 case 2:
825 iclr = reg_base + SYSIO_ICLR_SLOT2;
826 break;
827 default:
828 case 3:
829 iclr = reg_base + SYSIO_ICLR_SLOT3;
830 break;
831 };
832
833 iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
834 }
835 return build_irq(pil, sbus_level, iclr, imap);
836}
837
838
839#define SYSIO_UE_AFSR 0x0030UL
840#define SYSIO_UE_AFAR 0x0038UL
841#define SYSIO_UEAFSR_PPIO 0x8000000000000000
842#define SYSIO_UEAFSR_PDRD 0x4000000000000000
843#define SYSIO_UEAFSR_PDWR 0x2000000000000000
844#define SYSIO_UEAFSR_SPIO 0x1000000000000000
845#define SYSIO_UEAFSR_SDRD 0x0800000000000000
846#define SYSIO_UEAFSR_SDWR 0x0400000000000000
847#define SYSIO_UEAFSR_RESV1 0x03ff000000000000
848#define SYSIO_UEAFSR_DOFF 0x0000e00000000000
849#define SYSIO_UEAFSR_SIZE 0x00001c0000000000
850#define SYSIO_UEAFSR_MID 0x000003e000000000
851#define SYSIO_UEAFSR_RESV2 0x0000001fffffffff
852static void sysio_ue_handler(int irq, void *dev_id, struct pt_regs *regs)
853{
854 struct sbus_bus *sbus = dev_id;
855 struct sbus_iommu *iommu = sbus->iommu;
856 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL;
857 unsigned long afsr_reg, afar_reg;
858 unsigned long afsr, afar, error_bits;
859 int reported;
860
861 afsr_reg = reg_base + SYSIO_UE_AFSR;
862 afar_reg = reg_base + SYSIO_UE_AFAR;
863
864
865 afsr = upa_readq(afsr_reg);
866 afar = upa_readq(afar_reg);
867
868
869 error_bits = afsr &
870 (SYSIO_UEAFSR_PPIO | SYSIO_UEAFSR_PDRD | SYSIO_UEAFSR_PDWR |
871 SYSIO_UEAFSR_SPIO | SYSIO_UEAFSR_SDRD | SYSIO_UEAFSR_SDWR);
872 upa_writeq(error_bits, afsr_reg);
873
874
875 printk("SYSIO[%x]: Uncorrectable ECC Error, primary error type[%s]\n",
876 sbus->portid,
877 (((error_bits & SYSIO_UEAFSR_PPIO) ?
878 "PIO" :
879 ((error_bits & SYSIO_UEAFSR_PDRD) ?
880 "DVMA Read" :
881 ((error_bits & SYSIO_UEAFSR_PDWR) ?
882 "DVMA Write" : "???")))));
883 printk("SYSIO[%x]: DOFF[%lx] SIZE[%lx] MID[%lx]\n",
884 sbus->portid,
885 (afsr & SYSIO_UEAFSR_DOFF) >> 45UL,
886 (afsr & SYSIO_UEAFSR_SIZE) >> 42UL,
887 (afsr & SYSIO_UEAFSR_MID) >> 37UL);
888 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus->portid, afar);
889 printk("SYSIO[%x]: Secondary UE errors [", sbus->portid);
890 reported = 0;
891 if (afsr & SYSIO_UEAFSR_SPIO) {
892 reported++;
893 printk("(PIO)");
894 }
895 if (afsr & SYSIO_UEAFSR_SDRD) {
896 reported++;
897 printk("(DVMA Read)");
898 }
899 if (afsr & SYSIO_UEAFSR_SDWR) {
900 reported++;
901 printk("(DVMA Write)");
902 }
903 if (!reported)
904 printk("(none)");
905 printk("]\n");
906}
907
908#define SYSIO_CE_AFSR 0x0040UL
909#define SYSIO_CE_AFAR 0x0048UL
910#define SYSIO_CEAFSR_PPIO 0x8000000000000000
911#define SYSIO_CEAFSR_PDRD 0x4000000000000000
912#define SYSIO_CEAFSR_PDWR 0x2000000000000000
913#define SYSIO_CEAFSR_SPIO 0x1000000000000000
914#define SYSIO_CEAFSR_SDRD 0x0800000000000000
915#define SYSIO_CEAFSR_SDWR 0x0400000000000000
916#define SYSIO_CEAFSR_RESV1 0x0300000000000000
917#define SYSIO_CEAFSR_ESYND 0x00ff000000000000
918#define SYSIO_CEAFSR_DOFF 0x0000e00000000000
919#define SYSIO_CEAFSR_SIZE 0x00001c0000000000
920#define SYSIO_CEAFSR_MID 0x000003e000000000
921#define SYSIO_CEAFSR_RESV2 0x0000001fffffffff
922static void sysio_ce_handler(int irq, void *dev_id, struct pt_regs *regs)
923{
924 struct sbus_bus *sbus = dev_id;
925 struct sbus_iommu *iommu = sbus->iommu;
926 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL;
927 unsigned long afsr_reg, afar_reg;
928 unsigned long afsr, afar, error_bits;
929 int reported;
930
931 afsr_reg = reg_base + SYSIO_CE_AFSR;
932 afar_reg = reg_base + SYSIO_CE_AFAR;
933
934
935 afsr = upa_readq(afsr_reg);
936 afar = upa_readq(afar_reg);
937
938
939 error_bits = afsr &
940 (SYSIO_CEAFSR_PPIO | SYSIO_CEAFSR_PDRD | SYSIO_CEAFSR_PDWR |
941 SYSIO_CEAFSR_SPIO | SYSIO_CEAFSR_SDRD | SYSIO_CEAFSR_SDWR);
942 upa_writeq(error_bits, afsr_reg);
943
944 printk("SYSIO[%x]: Correctable ECC Error, primary error type[%s]\n",
945 sbus->portid,
946 (((error_bits & SYSIO_CEAFSR_PPIO) ?
947 "PIO" :
948 ((error_bits & SYSIO_CEAFSR_PDRD) ?
949 "DVMA Read" :
950 ((error_bits & SYSIO_CEAFSR_PDWR) ?
951 "DVMA Write" : "???")))));
952
953
954
955
956 printk("SYSIO[%x]: DOFF[%lx] ECC Syndrome[%lx] Size[%lx] MID[%lx]\n",
957 sbus->portid,
958 (afsr & SYSIO_CEAFSR_DOFF) >> 45UL,
959 (afsr & SYSIO_CEAFSR_ESYND) >> 48UL,
960 (afsr & SYSIO_CEAFSR_SIZE) >> 42UL,
961 (afsr & SYSIO_CEAFSR_MID) >> 37UL);
962 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus->portid, afar);
963
964 printk("SYSIO[%x]: Secondary CE errors [", sbus->portid);
965 reported = 0;
966 if (afsr & SYSIO_CEAFSR_SPIO) {
967 reported++;
968 printk("(PIO)");
969 }
970 if (afsr & SYSIO_CEAFSR_SDRD) {
971 reported++;
972 printk("(DVMA Read)");
973 }
974 if (afsr & SYSIO_CEAFSR_SDWR) {
975 reported++;
976 printk("(DVMA Write)");
977 }
978 if (!reported)
979 printk("(none)");
980 printk("]\n");
981}
982
983#define SYSIO_SBUS_AFSR 0x2010UL
984#define SYSIO_SBUS_AFAR 0x2018UL
985#define SYSIO_SBAFSR_PLE 0x8000000000000000
986#define SYSIO_SBAFSR_PTO 0x4000000000000000
987#define SYSIO_SBAFSR_PBERR 0x2000000000000000
988#define SYSIO_SBAFSR_SLE 0x1000000000000000
989#define SYSIO_SBAFSR_STO 0x0800000000000000
990#define SYSIO_SBAFSR_SBERR 0x0400000000000000
991#define SYSIO_SBAFSR_RESV1 0x03ff000000000000
992#define SYSIO_SBAFSR_RD 0x0000800000000000
993#define SYSIO_SBAFSR_RESV2 0x0000600000000000
994#define SYSIO_SBAFSR_SIZE 0x00001c0000000000
995#define SYSIO_SBAFSR_MID 0x000003e000000000
996#define SYSIO_SBAFSR_RESV3 0x0000001fffffffff
997static void sysio_sbus_error_handler(int irq, void *dev_id, struct pt_regs *regs)
998{
999 struct sbus_bus *sbus = dev_id;
1000 struct sbus_iommu *iommu = sbus->iommu;
1001 unsigned long afsr_reg, afar_reg, reg_base;
1002 unsigned long afsr, afar, error_bits;
1003 int reported;
1004
1005 reg_base = iommu->sbus_control_reg - 0x2000UL;
1006 afsr_reg = reg_base + SYSIO_SBUS_AFSR;
1007 afar_reg = reg_base + SYSIO_SBUS_AFAR;
1008
1009 afsr = upa_readq(afsr_reg);
1010 afar = upa_readq(afar_reg);
1011
1012
1013 error_bits = afsr &
1014 (SYSIO_SBAFSR_PLE | SYSIO_SBAFSR_PTO | SYSIO_SBAFSR_PBERR |
1015 SYSIO_SBAFSR_SLE | SYSIO_SBAFSR_STO | SYSIO_SBAFSR_SBERR);
1016 upa_writeq(error_bits, afsr_reg);
1017
1018
1019 printk("SYSIO[%x]: SBUS Error, primary error type[%s] read(%d)\n",
1020 sbus->portid,
1021 (((error_bits & SYSIO_SBAFSR_PLE) ?
1022 "Late PIO Error" :
1023 ((error_bits & SYSIO_SBAFSR_PTO) ?
1024 "Time Out" :
1025 ((error_bits & SYSIO_SBAFSR_PBERR) ?
1026 "Error Ack" : "???")))),
1027 (afsr & SYSIO_SBAFSR_RD) ? 1 : 0);
1028 printk("SYSIO[%x]: size[%lx] MID[%lx]\n",
1029 sbus->portid,
1030 (afsr & SYSIO_SBAFSR_SIZE) >> 42UL,
1031 (afsr & SYSIO_SBAFSR_MID) >> 37UL);
1032 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus->portid, afar);
1033 printk("SYSIO[%x]: Secondary SBUS errors [", sbus->portid);
1034 reported = 0;
1035 if (afsr & SYSIO_SBAFSR_SLE) {
1036 reported++;
1037 printk("(Late PIO Error)");
1038 }
1039 if (afsr & SYSIO_SBAFSR_STO) {
1040 reported++;
1041 printk("(Time Out)");
1042 }
1043 if (afsr & SYSIO_SBAFSR_SBERR) {
1044 reported++;
1045 printk("(Error Ack)");
1046 }
1047 if (!reported)
1048 printk("(none)");
1049 printk("]\n");
1050
1051
1052}
1053
1054#define ECC_CONTROL 0x0020UL
1055#define SYSIO_ECNTRL_ECCEN 0x8000000000000000
1056#define SYSIO_ECNTRL_UEEN 0x4000000000000000
1057#define SYSIO_ECNTRL_CEEN 0x2000000000000000
1058
1059#define SYSIO_UE_INO 0x34
1060#define SYSIO_CE_INO 0x35
1061#define SYSIO_SBUSERR_INO 0x36
1062
1063static void __init sysio_register_error_handlers(struct sbus_bus *sbus)
1064{
1065 struct sbus_iommu *iommu = sbus->iommu;
1066 unsigned long reg_base = iommu->sbus_control_reg - 0x2000UL;
1067 unsigned int irq;
1068 u64 control;
1069
1070 irq = sbus_build_irq(sbus, SYSIO_UE_INO);
1071 if (request_irq(irq, sysio_ue_handler,
1072 SA_SHIRQ, "SYSIO UE", sbus) < 0) {
1073 prom_printf("SYSIO[%x]: Cannot register UE interrupt.\n",
1074 sbus->portid);
1075 prom_halt();
1076 }
1077
1078 irq = sbus_build_irq(sbus, SYSIO_CE_INO);
1079 if (request_irq(irq, sysio_ce_handler,
1080 SA_SHIRQ, "SYSIO CE", sbus) < 0) {
1081 prom_printf("SYSIO[%x]: Cannot register CE interrupt.\n",
1082 sbus->portid);
1083 prom_halt();
1084 }
1085
1086 irq = sbus_build_irq(sbus, SYSIO_SBUSERR_INO);
1087 if (request_irq(irq, sysio_sbus_error_handler,
1088 SA_SHIRQ, "SYSIO SBUS Error", sbus) < 0) {
1089 prom_printf("SYSIO[%x]: Cannot register SBUS Error interrupt.\n",
1090 sbus->portid);
1091 prom_halt();
1092 }
1093
1094
1095 upa_writeq((SYSIO_ECNTRL_ECCEN |
1096 SYSIO_ECNTRL_UEEN |
1097 SYSIO_ECNTRL_CEEN),
1098 reg_base + ECC_CONTROL);
1099
1100 control = upa_readq(iommu->sbus_control_reg);
1101 control |= 0x100UL;
1102 upa_writeq(control, iommu->sbus_control_reg);
1103}
1104
1105
1106void __init sbus_iommu_init(int prom_node, struct sbus_bus *sbus)
1107{
1108 struct linux_prom64_registers rprop;
1109 struct sbus_iommu *iommu;
1110 unsigned long regs, tsb_base;
1111 u64 control;
1112 int err, i;
1113
1114 sbus->portid = prom_getintdefault(sbus->prom_node,
1115 "upa-portid", -1);
1116
1117 err = prom_getproperty(prom_node, "reg",
1118 (char *)&rprop, sizeof(rprop));
1119 if (err < 0) {
1120 prom_printf("sbus_iommu_init: Cannot map SYSIO control registers.\n");
1121 prom_halt();
1122 }
1123 regs = rprop.phys_addr;
1124
1125 iommu = kmalloc(sizeof(*iommu) + SMP_CACHE_BYTES, GFP_ATOMIC);
1126 if (iommu == NULL) {
1127 prom_printf("sbus_iommu_init: Fatal error, kmalloc(iommu) failed\n");
1128 prom_halt();
1129 }
1130
1131
1132 iommu = (struct sbus_iommu *)
1133 (((unsigned long)iommu + (SMP_CACHE_BYTES - 1UL)) &
1134 ~(SMP_CACHE_BYTES - 1UL));
1135
1136 memset(iommu, 0, sizeof(*iommu));
1137
1138
1139 iommu->lowest_consistent_map = CLUSTER_NPAGES;
1140
1141 for (i = 0; i < NCLUSTERS; i++) {
1142 iommu->alloc_info[i].flush = 0;
1143 iommu->alloc_info[i].next = 0;
1144 }
1145
1146
1147 spin_lock_init(&iommu->lock);
1148
1149
1150 iommu->iommu_regs = regs + SYSIO_IOMMUREG_BASE;
1151 iommu->strbuf_regs = regs + SYSIO_STRBUFREG_BASE;
1152
1153
1154
1155
1156 iommu->sbus_control_reg = regs + 0x2000UL;
1157
1158
1159 sbus->iommu = iommu;
1160
1161 printk("SYSIO: UPA portID %x, at %016lx\n",
1162 sbus->portid, regs);
1163
1164
1165 control = upa_readq(iommu->iommu_regs + IOMMU_CONTROL);
1166 control = ((7UL << 16UL) |
1167 (0UL << 2UL) |
1168 (1UL << 1UL) |
1169 (1UL << 0UL));
1170
1171
1172
1173
1174
1175 tsb_base = __get_free_pages(GFP_ATOMIC, get_order(IO_TSB_SIZE));
1176 if (tsb_base == 0UL) {
1177 prom_printf("sbus_iommu_init: Fatal error, cannot alloc TSB table.\n");
1178 prom_halt();
1179 }
1180
1181 iommu->page_table = (iopte_t *) tsb_base;
1182 memset(iommu->page_table, 0, IO_TSB_SIZE);
1183
1184 upa_writeq(control, iommu->iommu_regs + IOMMU_CONTROL);
1185
1186
1187
1188
1189 for (i = 0; i < 16; i++) {
1190 unsigned long dram = iommu->iommu_regs + IOMMU_DRAMDIAG;
1191 unsigned long tag = iommu->iommu_regs + IOMMU_TAGDIAG;
1192
1193 dram += (unsigned long)i * 8UL;
1194 tag += (unsigned long)i * 8UL;
1195 upa_writeq(0, dram);
1196 upa_writeq(0, tag);
1197 }
1198 upa_readq(iommu->sbus_control_reg);
1199
1200
1201 upa_writeq(__pa(tsb_base), iommu->iommu_regs + IOMMU_TSBBASE);
1202
1203
1204 control = (1UL << 1UL) | (1UL << 0UL);
1205 upa_writeq(control, iommu->strbuf_regs + STRBUF_CONTROL);
1206
1207
1208 for (i = 0; i < 16; i++) {
1209 unsigned long ptag, ltag;
1210
1211 ptag = iommu->strbuf_regs + STRBUF_PTAGDIAG;
1212 ltag = iommu->strbuf_regs + STRBUF_LTAGDIAG;
1213 ptag += (unsigned long)i * 8UL;
1214 ltag += (unsigned long)i * 8UL;
1215
1216 upa_writeq(0UL, ptag);
1217 upa_writeq(0UL, ltag);
1218 }
1219
1220
1221 control = upa_readq(iommu->sbus_control_reg);
1222 control |= 0x3fUL;
1223 upa_writeq(control, iommu->sbus_control_reg);
1224
1225
1226 if (this_is_starfire)
1227 sbus->starfire_cookie = starfire_hookup(sbus->portid);
1228 else
1229 sbus->starfire_cookie = NULL;
1230
1231 sysio_register_error_handlers(sbus);
1232}
1233