1 /* $Id: sbus.c,v 1.19 2002/01/23 11:27:32 davem Exp $
2 * sbus.c: UltraSparc SBUS controller support.
4 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
7 #include <linux/kernel.h>
8 #include <linux/types.h>
10 #include <linux/spinlock.h>
11 #include <linux/slab.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
19 #include <asm/cache.h>
22 #include <asm/starfire.h>
24 #include "iommu_common.h"
26 /* These should be allocated on an SMP_CACHE_BYTES
27 * aligned boundary for optimal performance.
29 * On SYSIO, using an 8K page size we have 1GB of SBUS
30 * DMA space mapped. We divide this space into equally
31 * sized clusters. We allocate a DMA mapping from the
32 * cluster that matches the order of the allocation, or
33 * if the order is greater than the number of clusters,
34 * we try to allocate from the last cluster.
38 #define ONE_GIG (1UL * 1024UL * 1024UL * 1024UL)
39 #define CLUSTER_SIZE (ONE_GIG / NCLUSTERS)
40 #define CLUSTER_MASK (CLUSTER_SIZE - 1)
41 #define CLUSTER_NPAGES (CLUSTER_SIZE >> IO_PAGE_SHIFT)
42 #define MAP_BASE ((u32)0xc0000000)
45 /*0x00*/spinlock_t lock
;
47 /*0x08*/iopte_t
*page_table
;
48 /*0x10*/unsigned long strbuf_regs
;
49 /*0x18*/unsigned long iommu_regs
;
50 /*0x20*/unsigned long sbus_control_reg
;
52 /*0x28*/volatile unsigned long strbuf_flushflag
;
54 /* If NCLUSTERS is ever decresed to 4 or lower,
55 * you must increase the size of the type of
56 * these counters. You have been duly warned. -DaveM
61 } alloc_info
[NCLUSTERS
];
63 /* The lowest used consistent mapping entry. Since
64 * we allocate consistent maps out of cluster 0 this
65 * is relative to the beginning of closter 0.
67 /*0x50*/u32 lowest_consistent_map
;
70 /* Offsets from iommu_regs */
71 #define SYSIO_IOMMUREG_BASE 0x2400UL
72 #define IOMMU_CONTROL (0x2400UL - 0x2400UL) /* IOMMU control register */
73 #define IOMMU_TSBBASE (0x2408UL - 0x2400UL) /* TSB base address register */
74 #define IOMMU_FLUSH (0x2410UL - 0x2400UL) /* IOMMU flush register */
75 #define IOMMU_VADIAG (0x4400UL - 0x2400UL) /* SBUS virtual address diagnostic */
76 #define IOMMU_TAGCMP (0x4408UL - 0x2400UL) /* TLB tag compare diagnostics */
77 #define IOMMU_LRUDIAG (0x4500UL - 0x2400UL) /* IOMMU LRU queue diagnostics */
78 #define IOMMU_TAGDIAG (0x4580UL - 0x2400UL) /* TLB tag diagnostics */
79 #define IOMMU_DRAMDIAG (0x4600UL - 0x2400UL) /* TLB data RAM diagnostics */
81 #define IOMMU_DRAM_VALID (1UL << 30UL)
83 static void __iommu_flushall(struct sbus_iommu
*iommu
)
85 unsigned long tag
= iommu
->iommu_regs
+ IOMMU_TAGDIAG
;
88 for (entry
= 0; entry
< 16; entry
++) {
92 upa_readq(iommu
->sbus_control_reg
);
94 for (entry
= 0; entry
< NCLUSTERS
; entry
++) {
95 iommu
->alloc_info
[entry
].flush
=
96 iommu
->alloc_info
[entry
].next
;
100 static void iommu_flush(struct sbus_iommu
*iommu
, u32 base
, unsigned long npages
)
103 upa_writeq(base
+ (npages
<< IO_PAGE_SHIFT
),
104 iommu
->iommu_regs
+ IOMMU_FLUSH
);
105 upa_readq(iommu
->sbus_control_reg
);
108 /* Offsets from strbuf_regs */
109 #define SYSIO_STRBUFREG_BASE 0x2800UL
110 #define STRBUF_CONTROL (0x2800UL - 0x2800UL) /* Control */
111 #define STRBUF_PFLUSH (0x2808UL - 0x2800UL) /* Page flush/invalidate */
112 #define STRBUF_FSYNC (0x2810UL - 0x2800UL) /* Flush synchronization */
113 #define STRBUF_DRAMDIAG (0x5000UL - 0x2800UL) /* data RAM diagnostic */
114 #define STRBUF_ERRDIAG (0x5400UL - 0x2800UL) /* error status diagnostics */
115 #define STRBUF_PTAGDIAG (0x5800UL - 0x2800UL) /* Page tag diagnostics */
116 #define STRBUF_LTAGDIAG (0x5900UL - 0x2800UL) /* Line tag diagnostics */
118 #define STRBUF_TAG_VALID 0x02UL
120 static void sbus_strbuf_flush(struct sbus_iommu
*iommu
, u32 base
, unsigned long npages
, int direction
)
127 upa_writeq(base
+ (n
<< IO_PAGE_SHIFT
),
128 iommu
->strbuf_regs
+ STRBUF_PFLUSH
);
130 /* If the device could not have possibly put dirty data into
131 * the streaming cache, no flush-flag synchronization needs
134 if (direction
== SBUS_DMA_TODEVICE
)
137 iommu
->strbuf_flushflag
= 0UL;
139 /* Whoopee cushion! */
140 upa_writeq(__pa(&iommu
->strbuf_flushflag
),
141 iommu
->strbuf_regs
+ STRBUF_FSYNC
);
142 upa_readq(iommu
->sbus_control_reg
);
145 while (iommu
->strbuf_flushflag
== 0UL) {
153 printk(KERN_WARNING
"sbus_strbuf_flush: flushflag timeout "
154 "vaddr[%08x] npages[%ld]\n",
158 static iopte_t
*alloc_streaming_cluster(struct sbus_iommu
*iommu
, unsigned long npages
)
160 iopte_t
*iopte
, *limit
, *first
, *cluster
;
161 unsigned long cnum
, ent
, nent
, flush_point
, found
;
165 while ((1UL << cnum
) < npages
)
167 if(cnum
>= NCLUSTERS
) {
168 nent
= 1UL << (cnum
- NCLUSTERS
);
169 cnum
= NCLUSTERS
- 1;
171 iopte
= iommu
->page_table
+ (cnum
* CLUSTER_NPAGES
);
174 limit
= (iommu
->page_table
+
175 iommu
->lowest_consistent_map
);
177 limit
= (iopte
+ CLUSTER_NPAGES
);
179 iopte
+= ((ent
= iommu
->alloc_info
[cnum
].next
) << cnum
);
180 flush_point
= iommu
->alloc_info
[cnum
].flush
;
186 if (iopte_val(*iopte
) == 0UL) {
191 /* Used cluster in the way */
199 iopte
+= (1 << cnum
);
201 if (iopte
>= limit
) {
202 iopte
= (iommu
->page_table
+ (cnum
* CLUSTER_NPAGES
));
205 /* Multiple cluster allocations must not wrap */
209 if (ent
== flush_point
)
210 __iommu_flushall(iommu
);
215 /* ent/iopte points to the last cluster entry we're going to use,
216 * so save our place for the next allocation.
218 if ((iopte
+ (1 << cnum
)) >= limit
)
222 iommu
->alloc_info
[cnum
].next
= ent
;
223 if (ent
== flush_point
)
224 __iommu_flushall(iommu
);
226 /* I've got your streaming cluster right here buddy boy... */
230 printk(KERN_EMERG
"sbus: alloc_streaming_cluster of npages(%ld) failed!\n",
235 static void free_streaming_cluster(struct sbus_iommu
*iommu
, u32 base
, unsigned long npages
)
237 unsigned long cnum
, ent
, nent
;
242 while ((1UL << cnum
) < npages
)
244 if(cnum
>= NCLUSTERS
) {
245 nent
= 1UL << (cnum
- NCLUSTERS
);
246 cnum
= NCLUSTERS
- 1;
248 ent
= (base
& CLUSTER_MASK
) >> (IO_PAGE_SHIFT
+ cnum
);
249 iopte
= iommu
->page_table
+ ((base
- MAP_BASE
) >> IO_PAGE_SHIFT
);
251 iopte_val(*iopte
) = 0UL;
255 /* If the global flush might not have caught this entry,
256 * adjust the flush point such that we will flush before
257 * ever trying to reuse it.
259 #define between(X,Y,Z) (((Z) - (Y)) >= ((X) - (Y)))
260 if (between(ent
, iommu
->alloc_info
[cnum
].next
, iommu
->alloc_info
[cnum
].flush
))
261 iommu
->alloc_info
[cnum
].flush
= ent
;
265 /* We allocate consistent mappings from the end of cluster zero. */
266 static iopte_t
*alloc_consistent_cluster(struct sbus_iommu
*iommu
, unsigned long npages
)
270 iopte
= iommu
->page_table
+ (1 * CLUSTER_NPAGES
);
271 while (iopte
> iommu
->page_table
) {
273 if (!(iopte_val(*iopte
) & IOPTE_VALID
)) {
274 unsigned long tmp
= npages
;
278 if (iopte_val(*iopte
) & IOPTE_VALID
)
282 u32 entry
= (iopte
- iommu
->page_table
);
284 if (entry
< iommu
->lowest_consistent_map
)
285 iommu
->lowest_consistent_map
= entry
;
293 static void free_consistent_cluster(struct sbus_iommu
*iommu
, u32 base
, unsigned long npages
)
295 iopte_t
*iopte
= iommu
->page_table
+ ((base
- MAP_BASE
) >> IO_PAGE_SHIFT
);
297 if ((iopte
- iommu
->page_table
) == iommu
->lowest_consistent_map
) {
298 iopte_t
*walk
= iopte
+ npages
;
301 limit
= iommu
->page_table
+ CLUSTER_NPAGES
;
302 while (walk
< limit
) {
303 if (iopte_val(*walk
) != 0UL)
307 iommu
->lowest_consistent_map
=
308 (walk
- iommu
->page_table
);
312 *iopte
++ = __iopte(0UL);
315 void *sbus_alloc_consistent(struct sbus_dev
*sdev
, size_t size
, dma_addr_t
*dvma_addr
)
317 unsigned long order
, first_page
, flags
;
318 struct sbus_iommu
*iommu
;
323 if (size
<= 0 || sdev
== NULL
|| dvma_addr
== NULL
)
326 size
= IO_PAGE_ALIGN(size
);
327 order
= get_order(size
);
330 first_page
= __get_free_pages(GFP_KERNEL
|__GFP_COMP
, order
);
331 if (first_page
== 0UL)
333 memset((char *)first_page
, 0, PAGE_SIZE
<< order
);
335 iommu
= sdev
->bus
->iommu
;
337 spin_lock_irqsave(&iommu
->lock
, flags
);
338 iopte
= alloc_consistent_cluster(iommu
, size
>> IO_PAGE_SHIFT
);
340 spin_unlock_irqrestore(&iommu
->lock
, flags
);
341 free_pages(first_page
, order
);
345 /* Ok, we're committed at this point. */
346 *dvma_addr
= MAP_BASE
+ ((iopte
- iommu
->page_table
) << IO_PAGE_SHIFT
);
347 ret
= (void *) first_page
;
348 npages
= size
>> IO_PAGE_SHIFT
;
350 *iopte
++ = __iopte(IOPTE_VALID
| IOPTE_CACHE
| IOPTE_WRITE
|
351 (__pa(first_page
) & IOPTE_PAGE
));
352 first_page
+= IO_PAGE_SIZE
;
354 iommu_flush(iommu
, *dvma_addr
, size
>> IO_PAGE_SHIFT
);
355 spin_unlock_irqrestore(&iommu
->lock
, flags
);
360 void sbus_free_consistent(struct sbus_dev
*sdev
, size_t size
, void *cpu
, dma_addr_t dvma
)
362 unsigned long order
, npages
;
363 struct sbus_iommu
*iommu
;
365 if (size
<= 0 || sdev
== NULL
|| cpu
== NULL
)
368 npages
= IO_PAGE_ALIGN(size
) >> IO_PAGE_SHIFT
;
369 iommu
= sdev
->bus
->iommu
;
371 spin_lock_irq(&iommu
->lock
);
372 free_consistent_cluster(iommu
, dvma
, npages
);
373 iommu_flush(iommu
, dvma
, npages
);
374 spin_unlock_irq(&iommu
->lock
);
376 order
= get_order(size
);
378 free_pages((unsigned long)cpu
, order
);
381 dma_addr_t
sbus_map_single(struct sbus_dev
*sdev
, void *ptr
, size_t size
, int dir
)
383 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
384 unsigned long npages
, pbase
, flags
;
386 u32 dma_base
, offset
;
387 unsigned long iopte_bits
;
389 if (dir
== SBUS_DMA_NONE
)
392 pbase
= (unsigned long) ptr
;
393 offset
= (u32
) (pbase
& ~IO_PAGE_MASK
);
394 size
= (IO_PAGE_ALIGN(pbase
+ size
) - (pbase
& IO_PAGE_MASK
));
395 pbase
= (unsigned long) __pa(pbase
& IO_PAGE_MASK
);
397 spin_lock_irqsave(&iommu
->lock
, flags
);
398 npages
= size
>> IO_PAGE_SHIFT
;
399 iopte
= alloc_streaming_cluster(iommu
, npages
);
402 dma_base
= MAP_BASE
+ ((iopte
- iommu
->page_table
) << IO_PAGE_SHIFT
);
403 npages
= size
>> IO_PAGE_SHIFT
;
404 iopte_bits
= IOPTE_VALID
| IOPTE_STBUF
| IOPTE_CACHE
;
405 if (dir
!= SBUS_DMA_TODEVICE
)
406 iopte_bits
|= IOPTE_WRITE
;
408 *iopte
++ = __iopte(iopte_bits
| (pbase
& IOPTE_PAGE
));
409 pbase
+= IO_PAGE_SIZE
;
411 npages
= size
>> IO_PAGE_SHIFT
;
412 spin_unlock_irqrestore(&iommu
->lock
, flags
);
414 return (dma_base
| offset
);
417 spin_unlock_irqrestore(&iommu
->lock
, flags
);
422 void sbus_unmap_single(struct sbus_dev
*sdev
, dma_addr_t dma_addr
, size_t size
, int direction
)
424 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
425 u32 dma_base
= dma_addr
& IO_PAGE_MASK
;
428 size
= (IO_PAGE_ALIGN(dma_addr
+ size
) - dma_base
);
430 spin_lock_irqsave(&iommu
->lock
, flags
);
431 free_streaming_cluster(iommu
, dma_base
, size
>> IO_PAGE_SHIFT
);
432 sbus_strbuf_flush(iommu
, dma_base
, size
>> IO_PAGE_SHIFT
, direction
);
433 spin_unlock_irqrestore(&iommu
->lock
, flags
);
436 #define SG_ENT_PHYS_ADDRESS(SG) \
437 (__pa(page_address((SG)->page)) + (SG)->offset)
439 static inline void fill_sg(iopte_t
*iopte
, struct scatterlist
*sg
, int nused
, int nelems
, unsigned long iopte_bits
)
441 struct scatterlist
*dma_sg
= sg
;
442 struct scatterlist
*sg_end
= sg
+ nelems
;
445 for (i
= 0; i
< nused
; i
++) {
446 unsigned long pteval
= ~0UL;
449 dma_npages
= ((dma_sg
->dma_address
& (IO_PAGE_SIZE
- 1UL)) +
451 ((IO_PAGE_SIZE
- 1UL))) >> IO_PAGE_SHIFT
;
453 unsigned long offset
;
456 /* If we are here, we know we have at least one
457 * more page to map. So walk forward until we
458 * hit a page crossing, and begin creating new
459 * mappings from that spot.
464 tmp
= (unsigned long) SG_ENT_PHYS_ADDRESS(sg
);
466 if (((tmp
^ pteval
) >> IO_PAGE_SHIFT
) != 0UL) {
467 pteval
= tmp
& IO_PAGE_MASK
;
468 offset
= tmp
& (IO_PAGE_SIZE
- 1UL);
471 if (((tmp
^ (tmp
+ len
- 1UL)) >> IO_PAGE_SHIFT
) != 0UL) {
472 pteval
= (tmp
+ IO_PAGE_SIZE
) & IO_PAGE_MASK
;
474 len
-= (IO_PAGE_SIZE
- (tmp
& (IO_PAGE_SIZE
- 1UL)));
480 pteval
= ((pteval
& IOPTE_PAGE
) | iopte_bits
);
482 *iopte
++ = __iopte(pteval
);
483 pteval
+= IO_PAGE_SIZE
;
484 len
-= (IO_PAGE_SIZE
- offset
);
489 pteval
= (pteval
& IOPTE_PAGE
) + len
;
492 /* Skip over any tail mappings we've fully mapped,
493 * adjusting pteval along the way. Stop when we
494 * detect a page crossing event.
496 while (sg
< sg_end
&&
497 (pteval
<< (64 - IO_PAGE_SHIFT
)) != 0UL &&
498 (pteval
== SG_ENT_PHYS_ADDRESS(sg
)) &&
500 (SG_ENT_PHYS_ADDRESS(sg
) + sg
->length
- 1UL)) >> IO_PAGE_SHIFT
) == 0UL) {
501 pteval
+= sg
->length
;
504 if ((pteval
<< (64 - IO_PAGE_SHIFT
)) == 0UL)
506 } while (dma_npages
!= 0);
511 int sbus_map_sg(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int nents
, int dir
)
513 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
514 unsigned long flags
, npages
;
517 struct scatterlist
*sgtmp
;
519 unsigned long iopte_bits
;
521 if (dir
== SBUS_DMA_NONE
)
524 /* Fast path single entry scatterlists. */
527 sbus_map_single(sdev
,
528 (page_address(sg
->page
) + sg
->offset
),
530 sg
->dma_length
= sg
->length
;
534 npages
= prepare_sg(sg
, nents
);
536 spin_lock_irqsave(&iommu
->lock
, flags
);
537 iopte
= alloc_streaming_cluster(iommu
, npages
);
540 dma_base
= MAP_BASE
+ ((iopte
- iommu
->page_table
) << IO_PAGE_SHIFT
);
542 /* Normalize DVMA addresses. */
546 while (used
&& sgtmp
->dma_length
) {
547 sgtmp
->dma_address
+= dma_base
;
553 iopte_bits
= IOPTE_VALID
| IOPTE_STBUF
| IOPTE_CACHE
;
554 if (dir
!= SBUS_DMA_TODEVICE
)
555 iopte_bits
|= IOPTE_WRITE
;
557 fill_sg(iopte
, sg
, used
, nents
, iopte_bits
);
559 verify_sglist(sg
, nents
, iopte
, npages
);
561 spin_unlock_irqrestore(&iommu
->lock
, flags
);
566 spin_unlock_irqrestore(&iommu
->lock
, flags
);
571 void sbus_unmap_sg(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int nents
, int direction
)
573 unsigned long size
, flags
;
574 struct sbus_iommu
*iommu
;
578 /* Fast path single entry scatterlists. */
580 sbus_unmap_single(sdev
, sg
->dma_address
, sg
->dma_length
, direction
);
584 dvma_base
= sg
[0].dma_address
& IO_PAGE_MASK
;
585 for (i
= 0; i
< nents
; i
++) {
586 if (sg
[i
].dma_length
== 0)
590 size
= IO_PAGE_ALIGN(sg
[i
].dma_address
+ sg
[i
].dma_length
) - dvma_base
;
592 iommu
= sdev
->bus
->iommu
;
593 spin_lock_irqsave(&iommu
->lock
, flags
);
594 free_streaming_cluster(iommu
, dvma_base
, size
>> IO_PAGE_SHIFT
);
595 sbus_strbuf_flush(iommu
, dvma_base
, size
>> IO_PAGE_SHIFT
, direction
);
596 spin_unlock_irqrestore(&iommu
->lock
, flags
);
599 void sbus_dma_sync_single_for_cpu(struct sbus_dev
*sdev
, dma_addr_t base
, size_t size
, int direction
)
601 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
604 size
= (IO_PAGE_ALIGN(base
+ size
) - (base
& IO_PAGE_MASK
));
606 spin_lock_irqsave(&iommu
->lock
, flags
);
607 sbus_strbuf_flush(iommu
, base
& IO_PAGE_MASK
, size
>> IO_PAGE_SHIFT
, direction
);
608 spin_unlock_irqrestore(&iommu
->lock
, flags
);
611 void sbus_dma_sync_single_for_device(struct sbus_dev
*sdev
, dma_addr_t base
, size_t size
, int direction
)
615 void sbus_dma_sync_sg_for_cpu(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int nents
, int direction
)
617 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
618 unsigned long flags
, size
;
622 base
= sg
[0].dma_address
& IO_PAGE_MASK
;
623 for (i
= 0; i
< nents
; i
++) {
624 if (sg
[i
].dma_length
== 0)
628 size
= IO_PAGE_ALIGN(sg
[i
].dma_address
+ sg
[i
].dma_length
) - base
;
630 spin_lock_irqsave(&iommu
->lock
, flags
);
631 sbus_strbuf_flush(iommu
, base
, size
>> IO_PAGE_SHIFT
, direction
);
632 spin_unlock_irqrestore(&iommu
->lock
, flags
);
635 void sbus_dma_sync_sg_for_device(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int nents
, int direction
)
639 /* Enable 64-bit DVMA mode for the given device. */
640 void sbus_set_sbus64(struct sbus_dev
*sdev
, int bursts
)
642 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
643 int slot
= sdev
->slot
;
644 unsigned long cfg_reg
;
647 cfg_reg
= iommu
->sbus_control_reg
;
675 val
= upa_readq(cfg_reg
);
676 if (val
& (1UL << 14UL)) {
677 /* Extended transfer mode already enabled. */
681 val
|= (1UL << 14UL);
683 if (bursts
& DMA_BURST8
)
685 if (bursts
& DMA_BURST16
)
687 if (bursts
& DMA_BURST32
)
689 if (bursts
& DMA_BURST64
)
691 upa_writeq(val
, cfg_reg
);
694 /* SBUS SYSIO INO number to Sparc PIL level. */
695 static unsigned char sysio_ino_to_pil
[] = {
696 0, 4, 4, 7, 5, 7, 8, 9, /* SBUS slot 0 */
697 0, 4, 4, 7, 5, 7, 8, 9, /* SBUS slot 1 */
698 0, 4, 4, 7, 5, 7, 8, 9, /* SBUS slot 2 */
699 0, 4, 4, 7, 5, 7, 8, 9, /* SBUS slot 3 */
700 4, /* Onboard SCSI */
701 5, /* Onboard Ethernet */
702 /*XXX*/ 8, /* Onboard BPP */
705 /*XXX*/15, /* PowerFail */
708 12, /* Zilog Serial Channels (incl. Keyboard/Mouse lines) */
710 0, /* Spare Hardware (bogon for now) */
711 0, /* Keyboard (bogon for now) */
712 0, /* Mouse (bogon for now) */
713 0, /* Serial (bogon for now) */
714 0, 0, /* Bogon, Bogon */
717 0, 0, /* Bogon, Bogon */
718 15, /* Uncorrectable SBUS Error */
719 15, /* Correctable SBUS Error */
721 /*XXX*/ 0, /* Power Management (bogon for now) */
724 /* INO number to IMAP register offset for SYSIO external IRQ's.
725 * This should conform to both Sunfire/Wildfire server and Fusion
728 #define SYSIO_IMAP_SLOT0 0x2c04UL
729 #define SYSIO_IMAP_SLOT1 0x2c0cUL
730 #define SYSIO_IMAP_SLOT2 0x2c14UL
731 #define SYSIO_IMAP_SLOT3 0x2c1cUL
732 #define SYSIO_IMAP_SCSI 0x3004UL
733 #define SYSIO_IMAP_ETH 0x300cUL
734 #define SYSIO_IMAP_BPP 0x3014UL
735 #define SYSIO_IMAP_AUDIO 0x301cUL
736 #define SYSIO_IMAP_PFAIL 0x3024UL
737 #define SYSIO_IMAP_KMS 0x302cUL
738 #define SYSIO_IMAP_FLPY 0x3034UL
739 #define SYSIO_IMAP_SHW 0x303cUL
740 #define SYSIO_IMAP_KBD 0x3044UL
741 #define SYSIO_IMAP_MS 0x304cUL
742 #define SYSIO_IMAP_SER 0x3054UL
743 #define SYSIO_IMAP_TIM0 0x3064UL
744 #define SYSIO_IMAP_TIM1 0x306cUL
745 #define SYSIO_IMAP_UE 0x3074UL
746 #define SYSIO_IMAP_CE 0x307cUL
747 #define SYSIO_IMAP_SBERR 0x3084UL
748 #define SYSIO_IMAP_PMGMT 0x308cUL
749 #define SYSIO_IMAP_GFX 0x3094UL
750 #define SYSIO_IMAP_EUPA 0x309cUL
752 #define bogon ((unsigned long) -1)
753 static unsigned long sysio_irq_offsets
[] = {
754 /* SBUS Slot 0 --> 3, level 1 --> 7 */
755 SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
,
756 SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
,
757 SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
,
758 SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
,
759 SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
,
760 SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
,
761 SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
,
762 SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
,
764 /* Onboard devices (not relevant/used on SunFire). */
793 #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
795 /* Convert Interrupt Mapping register pointer to associated
796 * Interrupt Clear register pointer, SYSIO specific version.
798 #define SYSIO_ICLR_UNUSED0 0x3400UL
799 #define SYSIO_ICLR_SLOT0 0x340cUL
800 #define SYSIO_ICLR_SLOT1 0x344cUL
801 #define SYSIO_ICLR_SLOT2 0x348cUL
802 #define SYSIO_ICLR_SLOT3 0x34ccUL
803 static unsigned long sysio_imap_to_iclr(unsigned long imap
)
805 unsigned long diff
= SYSIO_ICLR_UNUSED0
- SYSIO_IMAP_SLOT0
;
809 unsigned int sbus_build_irq(void *buscookie
, unsigned int ino
)
811 struct sbus_bus
*sbus
= (struct sbus_bus
*)buscookie
;
812 struct sbus_iommu
*iommu
= sbus
->iommu
;
813 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
814 unsigned long imap
, iclr
;
815 int pil
, sbus_level
= 0;
817 pil
= sysio_ino_to_pil
[ino
];
819 printk("sbus_irq_build: Bad SYSIO INO[%x]\n", ino
);
820 panic("Bad SYSIO IRQ translations...");
823 if (PIL_RESERVED(pil
))
826 imap
= sysio_irq_offsets
[ino
];
827 if (imap
== ((unsigned long)-1)) {
828 prom_printf("get_irq_translations: Bad SYSIO INO[%x] cpu[%d]\n",
834 /* SYSIO inconsistency. For external SLOTS, we have to select
835 * the right ICLR register based upon the lower SBUS irq level
839 iclr
= sysio_imap_to_iclr(imap
);
841 int sbus_slot
= (ino
& 0x18)>>3;
843 sbus_level
= ino
& 0x7;
847 iclr
= reg_base
+ SYSIO_ICLR_SLOT0
;
850 iclr
= reg_base
+ SYSIO_ICLR_SLOT1
;
853 iclr
= reg_base
+ SYSIO_ICLR_SLOT2
;
857 iclr
= reg_base
+ SYSIO_ICLR_SLOT3
;
861 iclr
+= ((unsigned long)sbus_level
- 1UL) * 8UL;
863 return build_irq(pil
, sbus_level
, iclr
, imap
);
866 /* Error interrupt handling. */
867 #define SYSIO_UE_AFSR 0x0030UL
868 #define SYSIO_UE_AFAR 0x0038UL
869 #define SYSIO_UEAFSR_PPIO 0x8000000000000000UL /* Primary PIO cause */
870 #define SYSIO_UEAFSR_PDRD 0x4000000000000000UL /* Primary DVMA read cause */
871 #define SYSIO_UEAFSR_PDWR 0x2000000000000000UL /* Primary DVMA write cause */
872 #define SYSIO_UEAFSR_SPIO 0x1000000000000000UL /* Secondary PIO is cause */
873 #define SYSIO_UEAFSR_SDRD 0x0800000000000000UL /* Secondary DVMA read cause */
874 #define SYSIO_UEAFSR_SDWR 0x0400000000000000UL /* Secondary DVMA write cause*/
875 #define SYSIO_UEAFSR_RESV1 0x03ff000000000000UL /* Reserved */
876 #define SYSIO_UEAFSR_DOFF 0x0000e00000000000UL /* Doubleword Offset */
877 #define SYSIO_UEAFSR_SIZE 0x00001c0000000000UL /* Bad transfer size 2^SIZE */
878 #define SYSIO_UEAFSR_MID 0x000003e000000000UL /* UPA MID causing the fault */
879 #define SYSIO_UEAFSR_RESV2 0x0000001fffffffffUL /* Reserved */
880 static irqreturn_t
sysio_ue_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
882 struct sbus_bus
*sbus
= dev_id
;
883 struct sbus_iommu
*iommu
= sbus
->iommu
;
884 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
885 unsigned long afsr_reg
, afar_reg
;
886 unsigned long afsr
, afar
, error_bits
;
889 afsr_reg
= reg_base
+ SYSIO_UE_AFSR
;
890 afar_reg
= reg_base
+ SYSIO_UE_AFAR
;
892 /* Latch error status. */
893 afsr
= upa_readq(afsr_reg
);
894 afar
= upa_readq(afar_reg
);
896 /* Clear primary/secondary error status bits. */
898 (SYSIO_UEAFSR_PPIO
| SYSIO_UEAFSR_PDRD
| SYSIO_UEAFSR_PDWR
|
899 SYSIO_UEAFSR_SPIO
| SYSIO_UEAFSR_SDRD
| SYSIO_UEAFSR_SDWR
);
900 upa_writeq(error_bits
, afsr_reg
);
903 printk("SYSIO[%x]: Uncorrectable ECC Error, primary error type[%s]\n",
905 (((error_bits
& SYSIO_UEAFSR_PPIO
) ?
907 ((error_bits
& SYSIO_UEAFSR_PDRD
) ?
909 ((error_bits
& SYSIO_UEAFSR_PDWR
) ?
910 "DVMA Write" : "???")))));
911 printk("SYSIO[%x]: DOFF[%lx] SIZE[%lx] MID[%lx]\n",
913 (afsr
& SYSIO_UEAFSR_DOFF
) >> 45UL,
914 (afsr
& SYSIO_UEAFSR_SIZE
) >> 42UL,
915 (afsr
& SYSIO_UEAFSR_MID
) >> 37UL);
916 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus
->portid
, afar
);
917 printk("SYSIO[%x]: Secondary UE errors [", sbus
->portid
);
919 if (afsr
& SYSIO_UEAFSR_SPIO
) {
923 if (afsr
& SYSIO_UEAFSR_SDRD
) {
925 printk("(DVMA Read)");
927 if (afsr
& SYSIO_UEAFSR_SDWR
) {
929 printk("(DVMA Write)");
938 #define SYSIO_CE_AFSR 0x0040UL
939 #define SYSIO_CE_AFAR 0x0048UL
940 #define SYSIO_CEAFSR_PPIO 0x8000000000000000UL /* Primary PIO cause */
941 #define SYSIO_CEAFSR_PDRD 0x4000000000000000UL /* Primary DVMA read cause */
942 #define SYSIO_CEAFSR_PDWR 0x2000000000000000UL /* Primary DVMA write cause */
943 #define SYSIO_CEAFSR_SPIO 0x1000000000000000UL /* Secondary PIO cause */
944 #define SYSIO_CEAFSR_SDRD 0x0800000000000000UL /* Secondary DVMA read cause */
945 #define SYSIO_CEAFSR_SDWR 0x0400000000000000UL /* Secondary DVMA write cause*/
946 #define SYSIO_CEAFSR_RESV1 0x0300000000000000UL /* Reserved */
947 #define SYSIO_CEAFSR_ESYND 0x00ff000000000000UL /* Syndrome Bits */
948 #define SYSIO_CEAFSR_DOFF 0x0000e00000000000UL /* Double Offset */
949 #define SYSIO_CEAFSR_SIZE 0x00001c0000000000UL /* Bad transfer size 2^SIZE */
950 #define SYSIO_CEAFSR_MID 0x000003e000000000UL /* UPA MID causing the fault */
951 #define SYSIO_CEAFSR_RESV2 0x0000001fffffffffUL /* Reserved */
952 static irqreturn_t
sysio_ce_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
954 struct sbus_bus
*sbus
= dev_id
;
955 struct sbus_iommu
*iommu
= sbus
->iommu
;
956 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
957 unsigned long afsr_reg
, afar_reg
;
958 unsigned long afsr
, afar
, error_bits
;
961 afsr_reg
= reg_base
+ SYSIO_CE_AFSR
;
962 afar_reg
= reg_base
+ SYSIO_CE_AFAR
;
964 /* Latch error status. */
965 afsr
= upa_readq(afsr_reg
);
966 afar
= upa_readq(afar_reg
);
968 /* Clear primary/secondary error status bits. */
970 (SYSIO_CEAFSR_PPIO
| SYSIO_CEAFSR_PDRD
| SYSIO_CEAFSR_PDWR
|
971 SYSIO_CEAFSR_SPIO
| SYSIO_CEAFSR_SDRD
| SYSIO_CEAFSR_SDWR
);
972 upa_writeq(error_bits
, afsr_reg
);
974 printk("SYSIO[%x]: Correctable ECC Error, primary error type[%s]\n",
976 (((error_bits
& SYSIO_CEAFSR_PPIO
) ?
978 ((error_bits
& SYSIO_CEAFSR_PDRD
) ?
980 ((error_bits
& SYSIO_CEAFSR_PDWR
) ?
981 "DVMA Write" : "???")))));
983 /* XXX Use syndrome and afar to print out module string just like
984 * XXX UDB CE trap handler does... -DaveM
986 printk("SYSIO[%x]: DOFF[%lx] ECC Syndrome[%lx] Size[%lx] MID[%lx]\n",
988 (afsr
& SYSIO_CEAFSR_DOFF
) >> 45UL,
989 (afsr
& SYSIO_CEAFSR_ESYND
) >> 48UL,
990 (afsr
& SYSIO_CEAFSR_SIZE
) >> 42UL,
991 (afsr
& SYSIO_CEAFSR_MID
) >> 37UL);
992 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus
->portid
, afar
);
994 printk("SYSIO[%x]: Secondary CE errors [", sbus
->portid
);
996 if (afsr
& SYSIO_CEAFSR_SPIO
) {
1000 if (afsr
& SYSIO_CEAFSR_SDRD
) {
1002 printk("(DVMA Read)");
1004 if (afsr
& SYSIO_CEAFSR_SDWR
) {
1006 printk("(DVMA Write)");
1015 #define SYSIO_SBUS_AFSR 0x2010UL
1016 #define SYSIO_SBUS_AFAR 0x2018UL
1017 #define SYSIO_SBAFSR_PLE 0x8000000000000000UL /* Primary Late PIO Error */
1018 #define SYSIO_SBAFSR_PTO 0x4000000000000000UL /* Primary SBUS Timeout */
1019 #define SYSIO_SBAFSR_PBERR 0x2000000000000000UL /* Primary SBUS Error ACK */
1020 #define SYSIO_SBAFSR_SLE 0x1000000000000000UL /* Secondary Late PIO Error */
1021 #define SYSIO_SBAFSR_STO 0x0800000000000000UL /* Secondary SBUS Timeout */
1022 #define SYSIO_SBAFSR_SBERR 0x0400000000000000UL /* Secondary SBUS Error ACK */
1023 #define SYSIO_SBAFSR_RESV1 0x03ff000000000000UL /* Reserved */
1024 #define SYSIO_SBAFSR_RD 0x0000800000000000UL /* Primary was late PIO read */
1025 #define SYSIO_SBAFSR_RESV2 0x0000600000000000UL /* Reserved */
1026 #define SYSIO_SBAFSR_SIZE 0x00001c0000000000UL /* Size of transfer */
1027 #define SYSIO_SBAFSR_MID 0x000003e000000000UL /* MID causing the error */
1028 #define SYSIO_SBAFSR_RESV3 0x0000001fffffffffUL /* Reserved */
1029 static irqreturn_t
sysio_sbus_error_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
1031 struct sbus_bus
*sbus
= dev_id
;
1032 struct sbus_iommu
*iommu
= sbus
->iommu
;
1033 unsigned long afsr_reg
, afar_reg
, reg_base
;
1034 unsigned long afsr
, afar
, error_bits
;
1037 reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
1038 afsr_reg
= reg_base
+ SYSIO_SBUS_AFSR
;
1039 afar_reg
= reg_base
+ SYSIO_SBUS_AFAR
;
1041 afsr
= upa_readq(afsr_reg
);
1042 afar
= upa_readq(afar_reg
);
1044 /* Clear primary/secondary error status bits. */
1046 (SYSIO_SBAFSR_PLE
| SYSIO_SBAFSR_PTO
| SYSIO_SBAFSR_PBERR
|
1047 SYSIO_SBAFSR_SLE
| SYSIO_SBAFSR_STO
| SYSIO_SBAFSR_SBERR
);
1048 upa_writeq(error_bits
, afsr_reg
);
1050 /* Log the error. */
1051 printk("SYSIO[%x]: SBUS Error, primary error type[%s] read(%d)\n",
1053 (((error_bits
& SYSIO_SBAFSR_PLE
) ?
1055 ((error_bits
& SYSIO_SBAFSR_PTO
) ?
1057 ((error_bits
& SYSIO_SBAFSR_PBERR
) ?
1058 "Error Ack" : "???")))),
1059 (afsr
& SYSIO_SBAFSR_RD
) ? 1 : 0);
1060 printk("SYSIO[%x]: size[%lx] MID[%lx]\n",
1062 (afsr
& SYSIO_SBAFSR_SIZE
) >> 42UL,
1063 (afsr
& SYSIO_SBAFSR_MID
) >> 37UL);
1064 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus
->portid
, afar
);
1065 printk("SYSIO[%x]: Secondary SBUS errors [", sbus
->portid
);
1067 if (afsr
& SYSIO_SBAFSR_SLE
) {
1069 printk("(Late PIO Error)");
1071 if (afsr
& SYSIO_SBAFSR_STO
) {
1073 printk("(Time Out)");
1075 if (afsr
& SYSIO_SBAFSR_SBERR
) {
1077 printk("(Error Ack)");
1083 /* XXX check iommu/strbuf for further error status XXX */
1088 #define ECC_CONTROL 0x0020UL
1089 #define SYSIO_ECNTRL_ECCEN 0x8000000000000000UL /* Enable ECC Checking */
1090 #define SYSIO_ECNTRL_UEEN 0x4000000000000000UL /* Enable UE Interrupts */
1091 #define SYSIO_ECNTRL_CEEN 0x2000000000000000UL /* Enable CE Interrupts */
1093 #define SYSIO_UE_INO 0x34
1094 #define SYSIO_CE_INO 0x35
1095 #define SYSIO_SBUSERR_INO 0x36
1097 static void __init
sysio_register_error_handlers(struct sbus_bus
*sbus
)
1099 struct sbus_iommu
*iommu
= sbus
->iommu
;
1100 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
1104 irq
= sbus_build_irq(sbus
, SYSIO_UE_INO
);
1105 if (request_irq(irq
, sysio_ue_handler
,
1106 SA_SHIRQ
, "SYSIO UE", sbus
) < 0) {
1107 prom_printf("SYSIO[%x]: Cannot register UE interrupt.\n",
1112 irq
= sbus_build_irq(sbus
, SYSIO_CE_INO
);
1113 if (request_irq(irq
, sysio_ce_handler
,
1114 SA_SHIRQ
, "SYSIO CE", sbus
) < 0) {
1115 prom_printf("SYSIO[%x]: Cannot register CE interrupt.\n",
1120 irq
= sbus_build_irq(sbus
, SYSIO_SBUSERR_INO
);
1121 if (request_irq(irq
, sysio_sbus_error_handler
,
1122 SA_SHIRQ
, "SYSIO SBUS Error", sbus
) < 0) {
1123 prom_printf("SYSIO[%x]: Cannot register SBUS Error interrupt.\n",
1128 /* Now turn the error interrupts on and also enable ECC checking. */
1129 upa_writeq((SYSIO_ECNTRL_ECCEN
|
1132 reg_base
+ ECC_CONTROL
);
1134 control
= upa_readq(iommu
->sbus_control_reg
);
1135 control
|= 0x100UL
; /* SBUS Error Interrupt Enable */
1136 upa_writeq(control
, iommu
->sbus_control_reg
);
1139 /* Boot time initialization. */
1140 void __init
sbus_iommu_init(int prom_node
, struct sbus_bus
*sbus
)
1142 struct linux_prom64_registers rprop
;
1143 struct sbus_iommu
*iommu
;
1144 unsigned long regs
, tsb_base
;
1148 sbus
->portid
= prom_getintdefault(sbus
->prom_node
,
1151 err
= prom_getproperty(prom_node
, "reg",
1152 (char *)&rprop
, sizeof(rprop
));
1154 prom_printf("sbus_iommu_init: Cannot map SYSIO control registers.\n");
1157 regs
= rprop
.phys_addr
;
1159 iommu
= kmalloc(sizeof(*iommu
) + SMP_CACHE_BYTES
, GFP_ATOMIC
);
1160 if (iommu
== NULL
) {
1161 prom_printf("sbus_iommu_init: Fatal error, kmalloc(iommu) failed\n");
1165 /* Align on E$ line boundary. */
1166 iommu
= (struct sbus_iommu
*)
1167 (((unsigned long)iommu
+ (SMP_CACHE_BYTES
- 1UL)) &
1168 ~(SMP_CACHE_BYTES
- 1UL));
1170 memset(iommu
, 0, sizeof(*iommu
));
1172 /* We start with no consistent mappings. */
1173 iommu
->lowest_consistent_map
= CLUSTER_NPAGES
;
1175 for (i
= 0; i
< NCLUSTERS
; i
++) {
1176 iommu
->alloc_info
[i
].flush
= 0;
1177 iommu
->alloc_info
[i
].next
= 0;
1180 /* Setup spinlock. */
1181 spin_lock_init(&iommu
->lock
);
1183 /* Init register offsets. */
1184 iommu
->iommu_regs
= regs
+ SYSIO_IOMMUREG_BASE
;
1185 iommu
->strbuf_regs
= regs
+ SYSIO_STRBUFREG_BASE
;
1187 /* The SYSIO SBUS control register is used for dummy reads
1188 * in order to ensure write completion.
1190 iommu
->sbus_control_reg
= regs
+ 0x2000UL
;
1192 /* Link into SYSIO software state. */
1193 sbus
->iommu
= iommu
;
1195 printk("SYSIO: UPA portID %x, at %016lx\n",
1196 sbus
->portid
, regs
);
1198 /* Setup for TSB_SIZE=7, TBW_SIZE=0, MMU_DE=1, MMU_EN=1 */
1199 control
= upa_readq(iommu
->iommu_regs
+ IOMMU_CONTROL
);
1200 control
= ((7UL << 16UL) |
1205 /* Using the above configuration we need 1MB iommu page
1206 * table (128K ioptes * 8 bytes per iopte). This is
1207 * page order 7 on UltraSparc.
1209 tsb_base
= __get_free_pages(GFP_ATOMIC
, get_order(IO_TSB_SIZE
));
1210 if (tsb_base
== 0UL) {
1211 prom_printf("sbus_iommu_init: Fatal error, cannot alloc TSB table.\n");
1215 iommu
->page_table
= (iopte_t
*) tsb_base
;
1216 memset(iommu
->page_table
, 0, IO_TSB_SIZE
);
1218 upa_writeq(control
, iommu
->iommu_regs
+ IOMMU_CONTROL
);
1220 /* Clean out any cruft in the IOMMU using
1221 * diagnostic accesses.
1223 for (i
= 0; i
< 16; i
++) {
1224 unsigned long dram
= iommu
->iommu_regs
+ IOMMU_DRAMDIAG
;
1225 unsigned long tag
= iommu
->iommu_regs
+ IOMMU_TAGDIAG
;
1227 dram
+= (unsigned long)i
* 8UL;
1228 tag
+= (unsigned long)i
* 8UL;
1229 upa_writeq(0, dram
);
1232 upa_readq(iommu
->sbus_control_reg
);
1234 /* Give the TSB to SYSIO. */
1235 upa_writeq(__pa(tsb_base
), iommu
->iommu_regs
+ IOMMU_TSBBASE
);
1237 /* Setup streaming buffer, DE=1 SB_EN=1 */
1238 control
= (1UL << 1UL) | (1UL << 0UL);
1239 upa_writeq(control
, iommu
->strbuf_regs
+ STRBUF_CONTROL
);
1241 /* Clear out the tags using diagnostics. */
1242 for (i
= 0; i
< 16; i
++) {
1243 unsigned long ptag
, ltag
;
1245 ptag
= iommu
->strbuf_regs
+ STRBUF_PTAGDIAG
;
1246 ltag
= iommu
->strbuf_regs
+ STRBUF_LTAGDIAG
;
1247 ptag
+= (unsigned long)i
* 8UL;
1248 ltag
+= (unsigned long)i
* 8UL;
1250 upa_writeq(0UL, ptag
);
1251 upa_writeq(0UL, ltag
);
1254 /* Enable DVMA arbitration for all devices/slots. */
1255 control
= upa_readq(iommu
->sbus_control_reg
);
1257 upa_writeq(control
, iommu
->sbus_control_reg
);
1259 /* Now some Xfire specific grot... */
1260 if (this_is_starfire
)
1261 sbus
->starfire_cookie
= starfire_hookup(sbus
->portid
);
1263 sbus
->starfire_cookie
= NULL
;
1265 sysio_register_error_handlers(sbus
);