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>
23 #include <asm/starfire.h>
25 #include "iommu_common.h"
27 #define MAP_BASE ((u32)0xc0000000)
29 struct sbus_iommu_arena
{
38 struct sbus_iommu_arena arena
;
41 unsigned long strbuf_regs
;
42 unsigned long iommu_regs
;
43 unsigned long sbus_control_reg
;
45 volatile unsigned long strbuf_flushflag
;
48 /* Offsets from iommu_regs */
49 #define SYSIO_IOMMUREG_BASE 0x2400UL
50 #define IOMMU_CONTROL (0x2400UL - 0x2400UL) /* IOMMU control register */
51 #define IOMMU_TSBBASE (0x2408UL - 0x2400UL) /* TSB base address register */
52 #define IOMMU_FLUSH (0x2410UL - 0x2400UL) /* IOMMU flush register */
53 #define IOMMU_VADIAG (0x4400UL - 0x2400UL) /* SBUS virtual address diagnostic */
54 #define IOMMU_TAGCMP (0x4408UL - 0x2400UL) /* TLB tag compare diagnostics */
55 #define IOMMU_LRUDIAG (0x4500UL - 0x2400UL) /* IOMMU LRU queue diagnostics */
56 #define IOMMU_TAGDIAG (0x4580UL - 0x2400UL) /* TLB tag diagnostics */
57 #define IOMMU_DRAMDIAG (0x4600UL - 0x2400UL) /* TLB data RAM diagnostics */
59 #define IOMMU_DRAM_VALID (1UL << 30UL)
61 static void __iommu_flushall(struct sbus_iommu
*iommu
)
63 unsigned long tag
= iommu
->iommu_regs
+ IOMMU_TAGDIAG
;
66 for (entry
= 0; entry
< 16; entry
++) {
70 upa_readq(iommu
->sbus_control_reg
);
73 /* Offsets from strbuf_regs */
74 #define SYSIO_STRBUFREG_BASE 0x2800UL
75 #define STRBUF_CONTROL (0x2800UL - 0x2800UL) /* Control */
76 #define STRBUF_PFLUSH (0x2808UL - 0x2800UL) /* Page flush/invalidate */
77 #define STRBUF_FSYNC (0x2810UL - 0x2800UL) /* Flush synchronization */
78 #define STRBUF_DRAMDIAG (0x5000UL - 0x2800UL) /* data RAM diagnostic */
79 #define STRBUF_ERRDIAG (0x5400UL - 0x2800UL) /* error status diagnostics */
80 #define STRBUF_PTAGDIAG (0x5800UL - 0x2800UL) /* Page tag diagnostics */
81 #define STRBUF_LTAGDIAG (0x5900UL - 0x2800UL) /* Line tag diagnostics */
83 #define STRBUF_TAG_VALID 0x02UL
85 static void sbus_strbuf_flush(struct sbus_iommu
*iommu
, u32 base
, unsigned long npages
, int direction
)
92 upa_writeq(base
+ (n
<< IO_PAGE_SHIFT
),
93 iommu
->strbuf_regs
+ STRBUF_PFLUSH
);
95 /* If the device could not have possibly put dirty data into
96 * the streaming cache, no flush-flag synchronization needs
99 if (direction
== SBUS_DMA_TODEVICE
)
102 iommu
->strbuf_flushflag
= 0UL;
104 /* Whoopee cushion! */
105 upa_writeq(__pa(&iommu
->strbuf_flushflag
),
106 iommu
->strbuf_regs
+ STRBUF_FSYNC
);
107 upa_readq(iommu
->sbus_control_reg
);
110 while (iommu
->strbuf_flushflag
== 0UL) {
118 printk(KERN_WARNING
"sbus_strbuf_flush: flushflag timeout "
119 "vaddr[%08x] npages[%ld]\n",
123 /* Based largely upon the ppc64 iommu allocator. */
124 static long sbus_arena_alloc(struct sbus_iommu
*iommu
, unsigned long npages
)
126 struct sbus_iommu_arena
*arena
= &iommu
->arena
;
127 unsigned long n
, i
, start
, end
, limit
;
130 limit
= arena
->limit
;
135 n
= find_next_zero_bit(arena
->map
, limit
, start
);
137 if (unlikely(end
>= limit
)) {
138 if (likely(pass
< 1)) {
141 __iommu_flushall(iommu
);
145 /* Scanned the whole thing, give up. */
150 for (i
= n
; i
< end
; i
++) {
151 if (test_bit(i
, arena
->map
)) {
157 for (i
= n
; i
< end
; i
++)
158 __set_bit(i
, arena
->map
);
165 static void sbus_arena_free(struct sbus_iommu_arena
*arena
, unsigned long base
, unsigned long npages
)
169 for (i
= base
; i
< (base
+ npages
); i
++)
170 __clear_bit(i
, arena
->map
);
173 static void sbus_iommu_table_init(struct sbus_iommu
*iommu
, unsigned int tsbsize
)
175 unsigned long tsbbase
, order
, sz
, num_tsb_entries
;
177 num_tsb_entries
= tsbsize
/ sizeof(iopte_t
);
179 /* Setup initial software IOMMU state. */
180 spin_lock_init(&iommu
->lock
);
182 /* Allocate and initialize the free area map. */
183 sz
= num_tsb_entries
/ 8;
184 sz
= (sz
+ 7UL) & ~7UL;
185 iommu
->arena
.map
= kzalloc(sz
, GFP_KERNEL
);
186 if (!iommu
->arena
.map
) {
187 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
190 iommu
->arena
.limit
= num_tsb_entries
;
192 /* Now allocate and setup the IOMMU page table itself. */
193 order
= get_order(tsbsize
);
194 tsbbase
= __get_free_pages(GFP_KERNEL
, order
);
196 prom_printf("IOMMU: Error, gfp(tsb) failed.\n");
199 iommu
->page_table
= (iopte_t
*)tsbbase
;
200 memset(iommu
->page_table
, 0, tsbsize
);
203 static inline iopte_t
*alloc_npages(struct sbus_iommu
*iommu
, unsigned long npages
)
207 entry
= sbus_arena_alloc(iommu
, npages
);
208 if (unlikely(entry
< 0))
211 return iommu
->page_table
+ entry
;
214 static inline void free_npages(struct sbus_iommu
*iommu
, dma_addr_t base
, unsigned long npages
)
216 sbus_arena_free(&iommu
->arena
, base
>> IO_PAGE_SHIFT
, npages
);
219 void *sbus_alloc_consistent(struct sbus_dev
*sdev
, size_t size
, dma_addr_t
*dvma_addr
)
221 struct sbus_iommu
*iommu
;
223 unsigned long flags
, order
, first_page
;
227 size
= IO_PAGE_ALIGN(size
);
228 order
= get_order(size
);
232 first_page
= __get_free_pages(GFP_KERNEL
|__GFP_COMP
, order
);
233 if (first_page
== 0UL)
235 memset((char *)first_page
, 0, PAGE_SIZE
<< order
);
237 iommu
= sdev
->bus
->iommu
;
239 spin_lock_irqsave(&iommu
->lock
, flags
);
240 iopte
= alloc_npages(iommu
, size
>> IO_PAGE_SHIFT
);
241 spin_unlock_irqrestore(&iommu
->lock
, flags
);
243 if (unlikely(iopte
== NULL
)) {
244 free_pages(first_page
, order
);
248 *dvma_addr
= (MAP_BASE
+
249 ((iopte
- iommu
->page_table
) << IO_PAGE_SHIFT
));
250 ret
= (void *) first_page
;
251 npages
= size
>> IO_PAGE_SHIFT
;
252 first_page
= __pa(first_page
);
254 iopte_val(*iopte
) = (IOPTE_VALID
| IOPTE_CACHE
|
256 (first_page
& IOPTE_PAGE
));
258 first_page
+= IO_PAGE_SIZE
;
264 void sbus_free_consistent(struct sbus_dev
*sdev
, size_t size
, void *cpu
, dma_addr_t dvma
)
266 struct sbus_iommu
*iommu
;
268 unsigned long flags
, order
, npages
;
270 npages
= IO_PAGE_ALIGN(size
) >> IO_PAGE_SHIFT
;
271 iommu
= sdev
->bus
->iommu
;
272 iopte
= iommu
->page_table
+
273 ((dvma
- MAP_BASE
) >> IO_PAGE_SHIFT
);
275 spin_lock_irqsave(&iommu
->lock
, flags
);
277 free_npages(iommu
, dvma
- MAP_BASE
, npages
);
279 spin_unlock_irqrestore(&iommu
->lock
, flags
);
281 order
= get_order(size
);
283 free_pages((unsigned long)cpu
, order
);
286 dma_addr_t
sbus_map_single(struct sbus_dev
*sdev
, void *ptr
, size_t sz
, int direction
)
288 struct sbus_iommu
*iommu
;
290 unsigned long flags
, npages
, oaddr
;
291 unsigned long i
, base_paddr
;
293 unsigned long iopte_protection
;
295 iommu
= sdev
->bus
->iommu
;
297 if (unlikely(direction
== SBUS_DMA_NONE
))
300 oaddr
= (unsigned long)ptr
;
301 npages
= IO_PAGE_ALIGN(oaddr
+ sz
) - (oaddr
& IO_PAGE_MASK
);
302 npages
>>= IO_PAGE_SHIFT
;
304 spin_lock_irqsave(&iommu
->lock
, flags
);
305 base
= alloc_npages(iommu
, npages
);
306 spin_unlock_irqrestore(&iommu
->lock
, flags
);
311 bus_addr
= (MAP_BASE
+
312 ((base
- iommu
->page_table
) << IO_PAGE_SHIFT
));
313 ret
= bus_addr
| (oaddr
& ~IO_PAGE_MASK
);
314 base_paddr
= __pa(oaddr
& IO_PAGE_MASK
);
316 iopte_protection
= IOPTE_VALID
| IOPTE_STBUF
| IOPTE_CACHE
;
317 if (direction
!= SBUS_DMA_TODEVICE
)
318 iopte_protection
|= IOPTE_WRITE
;
320 for (i
= 0; i
< npages
; i
++, base
++, base_paddr
+= IO_PAGE_SIZE
)
321 iopte_val(*base
) = iopte_protection
| base_paddr
;
326 void sbus_unmap_single(struct sbus_dev
*sdev
, dma_addr_t bus_addr
, size_t sz
, int direction
)
328 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
330 unsigned long flags
, npages
, i
;
332 if (unlikely(direction
== SBUS_DMA_NONE
))
335 npages
= IO_PAGE_ALIGN(bus_addr
+ sz
) - (bus_addr
& IO_PAGE_MASK
);
336 npages
>>= IO_PAGE_SHIFT
;
337 base
= iommu
->page_table
+
338 ((bus_addr
- MAP_BASE
) >> IO_PAGE_SHIFT
);
340 bus_addr
&= IO_PAGE_MASK
;
342 spin_lock_irqsave(&iommu
->lock
, flags
);
343 sbus_strbuf_flush(iommu
, bus_addr
, npages
, direction
);
344 for (i
= 0; i
< npages
; i
++)
345 iopte_val(base
[i
]) = 0UL;
346 free_npages(iommu
, bus_addr
- MAP_BASE
, npages
);
347 spin_unlock_irqrestore(&iommu
->lock
, flags
);
350 #define SG_ENT_PHYS_ADDRESS(SG) \
351 (__pa(page_address((SG)->page)) + (SG)->offset)
353 static inline void fill_sg(iopte_t
*iopte
, struct scatterlist
*sg
,
354 int nused
, int nelems
, unsigned long iopte_protection
)
356 struct scatterlist
*dma_sg
= sg
;
357 struct scatterlist
*sg_end
= sg
+ nelems
;
360 for (i
= 0; i
< nused
; i
++) {
361 unsigned long pteval
= ~0UL;
364 dma_npages
= ((dma_sg
->dma_address
& (IO_PAGE_SIZE
- 1UL)) +
366 ((IO_PAGE_SIZE
- 1UL))) >> IO_PAGE_SHIFT
;
368 unsigned long offset
;
371 /* If we are here, we know we have at least one
372 * more page to map. So walk forward until we
373 * hit a page crossing, and begin creating new
374 * mappings from that spot.
379 tmp
= SG_ENT_PHYS_ADDRESS(sg
);
381 if (((tmp
^ pteval
) >> IO_PAGE_SHIFT
) != 0UL) {
382 pteval
= tmp
& IO_PAGE_MASK
;
383 offset
= tmp
& (IO_PAGE_SIZE
- 1UL);
386 if (((tmp
^ (tmp
+ len
- 1UL)) >> IO_PAGE_SHIFT
) != 0UL) {
387 pteval
= (tmp
+ IO_PAGE_SIZE
) & IO_PAGE_MASK
;
389 len
-= (IO_PAGE_SIZE
- (tmp
& (IO_PAGE_SIZE
- 1UL)));
395 pteval
= iopte_protection
| (pteval
& IOPTE_PAGE
);
397 *iopte
++ = __iopte(pteval
);
398 pteval
+= IO_PAGE_SIZE
;
399 len
-= (IO_PAGE_SIZE
- offset
);
404 pteval
= (pteval
& IOPTE_PAGE
) + len
;
407 /* Skip over any tail mappings we've fully mapped,
408 * adjusting pteval along the way. Stop when we
409 * detect a page crossing event.
411 while (sg
< sg_end
&&
412 (pteval
<< (64 - IO_PAGE_SHIFT
)) != 0UL &&
413 (pteval
== SG_ENT_PHYS_ADDRESS(sg
)) &&
415 (SG_ENT_PHYS_ADDRESS(sg
) + sg
->length
- 1UL)) >> IO_PAGE_SHIFT
) == 0UL) {
416 pteval
+= sg
->length
;
419 if ((pteval
<< (64 - IO_PAGE_SHIFT
)) == 0UL)
421 } while (dma_npages
!= 0);
426 int sbus_map_sg(struct sbus_dev
*sdev
, struct scatterlist
*sglist
, int nelems
, int direction
)
428 struct sbus_iommu
*iommu
;
429 unsigned long flags
, npages
, iopte_protection
;
432 struct scatterlist
*sgtmp
;
435 /* Fast path single entry scatterlists. */
437 sglist
->dma_address
=
438 sbus_map_single(sdev
,
439 (page_address(sglist
->page
) + sglist
->offset
),
440 sglist
->length
, direction
);
441 sglist
->dma_length
= sglist
->length
;
445 iommu
= sdev
->bus
->iommu
;
447 if (unlikely(direction
== SBUS_DMA_NONE
))
450 npages
= prepare_sg(sglist
, nelems
);
452 spin_lock_irqsave(&iommu
->lock
, flags
);
453 base
= alloc_npages(iommu
, npages
);
454 spin_unlock_irqrestore(&iommu
->lock
, flags
);
456 if (unlikely(base
== NULL
))
459 dma_base
= MAP_BASE
+
460 ((base
- iommu
->page_table
) << IO_PAGE_SHIFT
);
462 /* Normalize DVMA addresses. */
466 while (used
&& sgtmp
->dma_length
) {
467 sgtmp
->dma_address
+= dma_base
;
471 used
= nelems
- used
;
473 iopte_protection
= IOPTE_VALID
| IOPTE_STBUF
| IOPTE_CACHE
;
474 if (direction
!= SBUS_DMA_TODEVICE
)
475 iopte_protection
|= IOPTE_WRITE
;
477 fill_sg(base
, sglist
, used
, nelems
, iopte_protection
);
480 verify_sglist(sglist
, nelems
, base
, npages
);
486 void sbus_unmap_sg(struct sbus_dev
*sdev
, struct scatterlist
*sglist
, int nelems
, int direction
)
488 struct sbus_iommu
*iommu
;
490 unsigned long flags
, i
, npages
;
493 if (unlikely(direction
== SBUS_DMA_NONE
))
496 iommu
= sdev
->bus
->iommu
;
498 bus_addr
= sglist
->dma_address
& IO_PAGE_MASK
;
500 for (i
= 1; i
< nelems
; i
++)
501 if (sglist
[i
].dma_length
== 0)
504 npages
= (IO_PAGE_ALIGN(sglist
[i
].dma_address
+ sglist
[i
].dma_length
) -
505 bus_addr
) >> IO_PAGE_SHIFT
;
507 base
= iommu
->page_table
+
508 ((bus_addr
- MAP_BASE
) >> IO_PAGE_SHIFT
);
510 spin_lock_irqsave(&iommu
->lock
, flags
);
511 sbus_strbuf_flush(iommu
, bus_addr
, npages
, direction
);
512 for (i
= 0; i
< npages
; i
++)
513 iopte_val(base
[i
]) = 0UL;
514 free_npages(iommu
, bus_addr
- MAP_BASE
, npages
);
515 spin_unlock_irqrestore(&iommu
->lock
, flags
);
518 void sbus_dma_sync_single_for_cpu(struct sbus_dev
*sdev
, dma_addr_t bus_addr
, size_t sz
, int direction
)
520 struct sbus_iommu
*iommu
;
521 unsigned long flags
, npages
;
523 iommu
= sdev
->bus
->iommu
;
525 npages
= IO_PAGE_ALIGN(bus_addr
+ sz
) - (bus_addr
& IO_PAGE_MASK
);
526 npages
>>= IO_PAGE_SHIFT
;
527 bus_addr
&= IO_PAGE_MASK
;
529 spin_lock_irqsave(&iommu
->lock
, flags
);
530 sbus_strbuf_flush(iommu
, bus_addr
, npages
, direction
);
531 spin_unlock_irqrestore(&iommu
->lock
, flags
);
534 void sbus_dma_sync_single_for_device(struct sbus_dev
*sdev
, dma_addr_t base
, size_t size
, int direction
)
538 void sbus_dma_sync_sg_for_cpu(struct sbus_dev
*sdev
, struct scatterlist
*sglist
, int nelems
, int direction
)
540 struct sbus_iommu
*iommu
;
541 unsigned long flags
, npages
, i
;
544 iommu
= sdev
->bus
->iommu
;
546 bus_addr
= sglist
[0].dma_address
& IO_PAGE_MASK
;
547 for (i
= 0; i
< nelems
; i
++) {
548 if (!sglist
[i
].dma_length
)
552 npages
= (IO_PAGE_ALIGN(sglist
[i
].dma_address
+ sglist
[i
].dma_length
)
553 - bus_addr
) >> IO_PAGE_SHIFT
;
555 spin_lock_irqsave(&iommu
->lock
, flags
);
556 sbus_strbuf_flush(iommu
, bus_addr
, npages
, direction
);
557 spin_unlock_irqrestore(&iommu
->lock
, flags
);
560 void sbus_dma_sync_sg_for_device(struct sbus_dev
*sdev
, struct scatterlist
*sg
, int nents
, int direction
)
564 /* Enable 64-bit DVMA mode for the given device. */
565 void sbus_set_sbus64(struct sbus_dev
*sdev
, int bursts
)
567 struct sbus_iommu
*iommu
= sdev
->bus
->iommu
;
568 int slot
= sdev
->slot
;
569 unsigned long cfg_reg
;
572 cfg_reg
= iommu
->sbus_control_reg
;
600 val
= upa_readq(cfg_reg
);
601 if (val
& (1UL << 14UL)) {
602 /* Extended transfer mode already enabled. */
606 val
|= (1UL << 14UL);
608 if (bursts
& DMA_BURST8
)
610 if (bursts
& DMA_BURST16
)
612 if (bursts
& DMA_BURST32
)
614 if (bursts
& DMA_BURST64
)
616 upa_writeq(val
, cfg_reg
);
619 /* INO number to IMAP register offset for SYSIO external IRQ's.
620 * This should conform to both Sunfire/Wildfire server and Fusion
623 #define SYSIO_IMAP_SLOT0 0x2c04UL
624 #define SYSIO_IMAP_SLOT1 0x2c0cUL
625 #define SYSIO_IMAP_SLOT2 0x2c14UL
626 #define SYSIO_IMAP_SLOT3 0x2c1cUL
627 #define SYSIO_IMAP_SCSI 0x3004UL
628 #define SYSIO_IMAP_ETH 0x300cUL
629 #define SYSIO_IMAP_BPP 0x3014UL
630 #define SYSIO_IMAP_AUDIO 0x301cUL
631 #define SYSIO_IMAP_PFAIL 0x3024UL
632 #define SYSIO_IMAP_KMS 0x302cUL
633 #define SYSIO_IMAP_FLPY 0x3034UL
634 #define SYSIO_IMAP_SHW 0x303cUL
635 #define SYSIO_IMAP_KBD 0x3044UL
636 #define SYSIO_IMAP_MS 0x304cUL
637 #define SYSIO_IMAP_SER 0x3054UL
638 #define SYSIO_IMAP_TIM0 0x3064UL
639 #define SYSIO_IMAP_TIM1 0x306cUL
640 #define SYSIO_IMAP_UE 0x3074UL
641 #define SYSIO_IMAP_CE 0x307cUL
642 #define SYSIO_IMAP_SBERR 0x3084UL
643 #define SYSIO_IMAP_PMGMT 0x308cUL
644 #define SYSIO_IMAP_GFX 0x3094UL
645 #define SYSIO_IMAP_EUPA 0x309cUL
647 #define bogon ((unsigned long) -1)
648 static unsigned long sysio_irq_offsets
[] = {
649 /* SBUS Slot 0 --> 3, level 1 --> 7 */
650 SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
,
651 SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
, SYSIO_IMAP_SLOT0
,
652 SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
,
653 SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
, SYSIO_IMAP_SLOT1
,
654 SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
,
655 SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
, SYSIO_IMAP_SLOT2
,
656 SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
,
657 SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
, SYSIO_IMAP_SLOT3
,
659 /* Onboard devices (not relevant/used on SunFire). */
688 #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
690 /* Convert Interrupt Mapping register pointer to associated
691 * Interrupt Clear register pointer, SYSIO specific version.
693 #define SYSIO_ICLR_UNUSED0 0x3400UL
694 #define SYSIO_ICLR_SLOT0 0x340cUL
695 #define SYSIO_ICLR_SLOT1 0x344cUL
696 #define SYSIO_ICLR_SLOT2 0x348cUL
697 #define SYSIO_ICLR_SLOT3 0x34ccUL
698 static unsigned long sysio_imap_to_iclr(unsigned long imap
)
700 unsigned long diff
= SYSIO_ICLR_UNUSED0
- SYSIO_IMAP_SLOT0
;
704 unsigned int sbus_build_irq(void *buscookie
, unsigned int ino
)
706 struct sbus_bus
*sbus
= (struct sbus_bus
*)buscookie
;
707 struct sbus_iommu
*iommu
= sbus
->iommu
;
708 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
709 unsigned long imap
, iclr
;
712 imap
= sysio_irq_offsets
[ino
];
713 if (imap
== ((unsigned long)-1)) {
714 prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
720 /* SYSIO inconsistency. For external SLOTS, we have to select
721 * the right ICLR register based upon the lower SBUS irq level
725 iclr
= sysio_imap_to_iclr(imap
);
727 int sbus_slot
= (ino
& 0x18)>>3;
729 sbus_level
= ino
& 0x7;
733 iclr
= reg_base
+ SYSIO_ICLR_SLOT0
;
736 iclr
= reg_base
+ SYSIO_ICLR_SLOT1
;
739 iclr
= reg_base
+ SYSIO_ICLR_SLOT2
;
743 iclr
= reg_base
+ SYSIO_ICLR_SLOT3
;
747 iclr
+= ((unsigned long)sbus_level
- 1UL) * 8UL;
749 return build_irq(sbus_level
, iclr
, imap
);
752 /* Error interrupt handling. */
753 #define SYSIO_UE_AFSR 0x0030UL
754 #define SYSIO_UE_AFAR 0x0038UL
755 #define SYSIO_UEAFSR_PPIO 0x8000000000000000UL /* Primary PIO cause */
756 #define SYSIO_UEAFSR_PDRD 0x4000000000000000UL /* Primary DVMA read cause */
757 #define SYSIO_UEAFSR_PDWR 0x2000000000000000UL /* Primary DVMA write cause */
758 #define SYSIO_UEAFSR_SPIO 0x1000000000000000UL /* Secondary PIO is cause */
759 #define SYSIO_UEAFSR_SDRD 0x0800000000000000UL /* Secondary DVMA read cause */
760 #define SYSIO_UEAFSR_SDWR 0x0400000000000000UL /* Secondary DVMA write cause*/
761 #define SYSIO_UEAFSR_RESV1 0x03ff000000000000UL /* Reserved */
762 #define SYSIO_UEAFSR_DOFF 0x0000e00000000000UL /* Doubleword Offset */
763 #define SYSIO_UEAFSR_SIZE 0x00001c0000000000UL /* Bad transfer size 2^SIZE */
764 #define SYSIO_UEAFSR_MID 0x000003e000000000UL /* UPA MID causing the fault */
765 #define SYSIO_UEAFSR_RESV2 0x0000001fffffffffUL /* Reserved */
766 static irqreturn_t
sysio_ue_handler(int irq
, void *dev_id
)
768 struct sbus_bus
*sbus
= dev_id
;
769 struct sbus_iommu
*iommu
= sbus
->iommu
;
770 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
771 unsigned long afsr_reg
, afar_reg
;
772 unsigned long afsr
, afar
, error_bits
;
775 afsr_reg
= reg_base
+ SYSIO_UE_AFSR
;
776 afar_reg
= reg_base
+ SYSIO_UE_AFAR
;
778 /* Latch error status. */
779 afsr
= upa_readq(afsr_reg
);
780 afar
= upa_readq(afar_reg
);
782 /* Clear primary/secondary error status bits. */
784 (SYSIO_UEAFSR_PPIO
| SYSIO_UEAFSR_PDRD
| SYSIO_UEAFSR_PDWR
|
785 SYSIO_UEAFSR_SPIO
| SYSIO_UEAFSR_SDRD
| SYSIO_UEAFSR_SDWR
);
786 upa_writeq(error_bits
, afsr_reg
);
789 printk("SYSIO[%x]: Uncorrectable ECC Error, primary error type[%s]\n",
791 (((error_bits
& SYSIO_UEAFSR_PPIO
) ?
793 ((error_bits
& SYSIO_UEAFSR_PDRD
) ?
795 ((error_bits
& SYSIO_UEAFSR_PDWR
) ?
796 "DVMA Write" : "???")))));
797 printk("SYSIO[%x]: DOFF[%lx] SIZE[%lx] MID[%lx]\n",
799 (afsr
& SYSIO_UEAFSR_DOFF
) >> 45UL,
800 (afsr
& SYSIO_UEAFSR_SIZE
) >> 42UL,
801 (afsr
& SYSIO_UEAFSR_MID
) >> 37UL);
802 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus
->portid
, afar
);
803 printk("SYSIO[%x]: Secondary UE errors [", sbus
->portid
);
805 if (afsr
& SYSIO_UEAFSR_SPIO
) {
809 if (afsr
& SYSIO_UEAFSR_SDRD
) {
811 printk("(DVMA Read)");
813 if (afsr
& SYSIO_UEAFSR_SDWR
) {
815 printk("(DVMA Write)");
824 #define SYSIO_CE_AFSR 0x0040UL
825 #define SYSIO_CE_AFAR 0x0048UL
826 #define SYSIO_CEAFSR_PPIO 0x8000000000000000UL /* Primary PIO cause */
827 #define SYSIO_CEAFSR_PDRD 0x4000000000000000UL /* Primary DVMA read cause */
828 #define SYSIO_CEAFSR_PDWR 0x2000000000000000UL /* Primary DVMA write cause */
829 #define SYSIO_CEAFSR_SPIO 0x1000000000000000UL /* Secondary PIO cause */
830 #define SYSIO_CEAFSR_SDRD 0x0800000000000000UL /* Secondary DVMA read cause */
831 #define SYSIO_CEAFSR_SDWR 0x0400000000000000UL /* Secondary DVMA write cause*/
832 #define SYSIO_CEAFSR_RESV1 0x0300000000000000UL /* Reserved */
833 #define SYSIO_CEAFSR_ESYND 0x00ff000000000000UL /* Syndrome Bits */
834 #define SYSIO_CEAFSR_DOFF 0x0000e00000000000UL /* Double Offset */
835 #define SYSIO_CEAFSR_SIZE 0x00001c0000000000UL /* Bad transfer size 2^SIZE */
836 #define SYSIO_CEAFSR_MID 0x000003e000000000UL /* UPA MID causing the fault */
837 #define SYSIO_CEAFSR_RESV2 0x0000001fffffffffUL /* Reserved */
838 static irqreturn_t
sysio_ce_handler(int irq
, void *dev_id
)
840 struct sbus_bus
*sbus
= dev_id
;
841 struct sbus_iommu
*iommu
= sbus
->iommu
;
842 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
843 unsigned long afsr_reg
, afar_reg
;
844 unsigned long afsr
, afar
, error_bits
;
847 afsr_reg
= reg_base
+ SYSIO_CE_AFSR
;
848 afar_reg
= reg_base
+ SYSIO_CE_AFAR
;
850 /* Latch error status. */
851 afsr
= upa_readq(afsr_reg
);
852 afar
= upa_readq(afar_reg
);
854 /* Clear primary/secondary error status bits. */
856 (SYSIO_CEAFSR_PPIO
| SYSIO_CEAFSR_PDRD
| SYSIO_CEAFSR_PDWR
|
857 SYSIO_CEAFSR_SPIO
| SYSIO_CEAFSR_SDRD
| SYSIO_CEAFSR_SDWR
);
858 upa_writeq(error_bits
, afsr_reg
);
860 printk("SYSIO[%x]: Correctable ECC Error, primary error type[%s]\n",
862 (((error_bits
& SYSIO_CEAFSR_PPIO
) ?
864 ((error_bits
& SYSIO_CEAFSR_PDRD
) ?
866 ((error_bits
& SYSIO_CEAFSR_PDWR
) ?
867 "DVMA Write" : "???")))));
869 /* XXX Use syndrome and afar to print out module string just like
870 * XXX UDB CE trap handler does... -DaveM
872 printk("SYSIO[%x]: DOFF[%lx] ECC Syndrome[%lx] Size[%lx] MID[%lx]\n",
874 (afsr
& SYSIO_CEAFSR_DOFF
) >> 45UL,
875 (afsr
& SYSIO_CEAFSR_ESYND
) >> 48UL,
876 (afsr
& SYSIO_CEAFSR_SIZE
) >> 42UL,
877 (afsr
& SYSIO_CEAFSR_MID
) >> 37UL);
878 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus
->portid
, afar
);
880 printk("SYSIO[%x]: Secondary CE errors [", sbus
->portid
);
882 if (afsr
& SYSIO_CEAFSR_SPIO
) {
886 if (afsr
& SYSIO_CEAFSR_SDRD
) {
888 printk("(DVMA Read)");
890 if (afsr
& SYSIO_CEAFSR_SDWR
) {
892 printk("(DVMA Write)");
901 #define SYSIO_SBUS_AFSR 0x2010UL
902 #define SYSIO_SBUS_AFAR 0x2018UL
903 #define SYSIO_SBAFSR_PLE 0x8000000000000000UL /* Primary Late PIO Error */
904 #define SYSIO_SBAFSR_PTO 0x4000000000000000UL /* Primary SBUS Timeout */
905 #define SYSIO_SBAFSR_PBERR 0x2000000000000000UL /* Primary SBUS Error ACK */
906 #define SYSIO_SBAFSR_SLE 0x1000000000000000UL /* Secondary Late PIO Error */
907 #define SYSIO_SBAFSR_STO 0x0800000000000000UL /* Secondary SBUS Timeout */
908 #define SYSIO_SBAFSR_SBERR 0x0400000000000000UL /* Secondary SBUS Error ACK */
909 #define SYSIO_SBAFSR_RESV1 0x03ff000000000000UL /* Reserved */
910 #define SYSIO_SBAFSR_RD 0x0000800000000000UL /* Primary was late PIO read */
911 #define SYSIO_SBAFSR_RESV2 0x0000600000000000UL /* Reserved */
912 #define SYSIO_SBAFSR_SIZE 0x00001c0000000000UL /* Size of transfer */
913 #define SYSIO_SBAFSR_MID 0x000003e000000000UL /* MID causing the error */
914 #define SYSIO_SBAFSR_RESV3 0x0000001fffffffffUL /* Reserved */
915 static irqreturn_t
sysio_sbus_error_handler(int irq
, void *dev_id
)
917 struct sbus_bus
*sbus
= dev_id
;
918 struct sbus_iommu
*iommu
= sbus
->iommu
;
919 unsigned long afsr_reg
, afar_reg
, reg_base
;
920 unsigned long afsr
, afar
, error_bits
;
923 reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
924 afsr_reg
= reg_base
+ SYSIO_SBUS_AFSR
;
925 afar_reg
= reg_base
+ SYSIO_SBUS_AFAR
;
927 afsr
= upa_readq(afsr_reg
);
928 afar
= upa_readq(afar_reg
);
930 /* Clear primary/secondary error status bits. */
932 (SYSIO_SBAFSR_PLE
| SYSIO_SBAFSR_PTO
| SYSIO_SBAFSR_PBERR
|
933 SYSIO_SBAFSR_SLE
| SYSIO_SBAFSR_STO
| SYSIO_SBAFSR_SBERR
);
934 upa_writeq(error_bits
, afsr_reg
);
937 printk("SYSIO[%x]: SBUS Error, primary error type[%s] read(%d)\n",
939 (((error_bits
& SYSIO_SBAFSR_PLE
) ?
941 ((error_bits
& SYSIO_SBAFSR_PTO
) ?
943 ((error_bits
& SYSIO_SBAFSR_PBERR
) ?
944 "Error Ack" : "???")))),
945 (afsr
& SYSIO_SBAFSR_RD
) ? 1 : 0);
946 printk("SYSIO[%x]: size[%lx] MID[%lx]\n",
948 (afsr
& SYSIO_SBAFSR_SIZE
) >> 42UL,
949 (afsr
& SYSIO_SBAFSR_MID
) >> 37UL);
950 printk("SYSIO[%x]: AFAR[%016lx]\n", sbus
->portid
, afar
);
951 printk("SYSIO[%x]: Secondary SBUS errors [", sbus
->portid
);
953 if (afsr
& SYSIO_SBAFSR_SLE
) {
955 printk("(Late PIO Error)");
957 if (afsr
& SYSIO_SBAFSR_STO
) {
959 printk("(Time Out)");
961 if (afsr
& SYSIO_SBAFSR_SBERR
) {
963 printk("(Error Ack)");
969 /* XXX check iommu/strbuf for further error status XXX */
974 #define ECC_CONTROL 0x0020UL
975 #define SYSIO_ECNTRL_ECCEN 0x8000000000000000UL /* Enable ECC Checking */
976 #define SYSIO_ECNTRL_UEEN 0x4000000000000000UL /* Enable UE Interrupts */
977 #define SYSIO_ECNTRL_CEEN 0x2000000000000000UL /* Enable CE Interrupts */
979 #define SYSIO_UE_INO 0x34
980 #define SYSIO_CE_INO 0x35
981 #define SYSIO_SBUSERR_INO 0x36
983 static void __init
sysio_register_error_handlers(struct sbus_bus
*sbus
)
985 struct sbus_iommu
*iommu
= sbus
->iommu
;
986 unsigned long reg_base
= iommu
->sbus_control_reg
- 0x2000UL
;
990 irq
= sbus_build_irq(sbus
, SYSIO_UE_INO
);
991 if (request_irq(irq
, sysio_ue_handler
,
992 IRQF_SHARED
, "SYSIO UE", sbus
) < 0) {
993 prom_printf("SYSIO[%x]: Cannot register UE interrupt.\n",
998 irq
= sbus_build_irq(sbus
, SYSIO_CE_INO
);
999 if (request_irq(irq
, sysio_ce_handler
,
1000 IRQF_SHARED
, "SYSIO CE", sbus
) < 0) {
1001 prom_printf("SYSIO[%x]: Cannot register CE interrupt.\n",
1006 irq
= sbus_build_irq(sbus
, SYSIO_SBUSERR_INO
);
1007 if (request_irq(irq
, sysio_sbus_error_handler
,
1008 IRQF_SHARED
, "SYSIO SBUS Error", sbus
) < 0) {
1009 prom_printf("SYSIO[%x]: Cannot register SBUS Error interrupt.\n",
1014 /* Now turn the error interrupts on and also enable ECC checking. */
1015 upa_writeq((SYSIO_ECNTRL_ECCEN
|
1018 reg_base
+ ECC_CONTROL
);
1020 control
= upa_readq(iommu
->sbus_control_reg
);
1021 control
|= 0x100UL
; /* SBUS Error Interrupt Enable */
1022 upa_writeq(control
, iommu
->sbus_control_reg
);
1025 /* Boot time initialization. */
1026 static void __init
sbus_iommu_init(int __node
, struct sbus_bus
*sbus
)
1028 struct linux_prom64_registers
*pr
;
1029 struct device_node
*dp
;
1030 struct sbus_iommu
*iommu
;
1035 dp
= of_find_node_by_phandle(__node
);
1037 sbus
->portid
= of_getintprop_default(dp
, "upa-portid", -1);
1039 pr
= of_get_property(dp
, "reg", NULL
);
1041 prom_printf("sbus_iommu_init: Cannot map SYSIO control registers.\n");
1044 regs
= pr
->phys_addr
;
1046 iommu
= kmalloc(sizeof(*iommu
) + SMP_CACHE_BYTES
, GFP_ATOMIC
);
1047 if (iommu
== NULL
) {
1048 prom_printf("sbus_iommu_init: Fatal error, kmalloc(iommu) failed\n");
1052 /* Align on E$ line boundary. */
1053 iommu
= (struct sbus_iommu
*)
1054 (((unsigned long)iommu
+ (SMP_CACHE_BYTES
- 1UL)) &
1055 ~(SMP_CACHE_BYTES
- 1UL));
1057 memset(iommu
, 0, sizeof(*iommu
));
1059 /* Setup spinlock. */
1060 spin_lock_init(&iommu
->lock
);
1062 /* Init register offsets. */
1063 iommu
->iommu_regs
= regs
+ SYSIO_IOMMUREG_BASE
;
1064 iommu
->strbuf_regs
= regs
+ SYSIO_STRBUFREG_BASE
;
1066 /* The SYSIO SBUS control register is used for dummy reads
1067 * in order to ensure write completion.
1069 iommu
->sbus_control_reg
= regs
+ 0x2000UL
;
1071 /* Link into SYSIO software state. */
1072 sbus
->iommu
= iommu
;
1074 printk("SYSIO: UPA portID %x, at %016lx\n",
1075 sbus
->portid
, regs
);
1077 /* Setup for TSB_SIZE=7, TBW_SIZE=0, MMU_DE=1, MMU_EN=1 */
1078 sbus_iommu_table_init(iommu
, IO_TSB_SIZE
);
1080 control
= upa_readq(iommu
->iommu_regs
+ IOMMU_CONTROL
);
1081 control
= ((7UL << 16UL) |
1085 upa_writeq(control
, iommu
->iommu_regs
+ IOMMU_CONTROL
);
1087 /* Clean out any cruft in the IOMMU using
1088 * diagnostic accesses.
1090 for (i
= 0; i
< 16; i
++) {
1091 unsigned long dram
= iommu
->iommu_regs
+ IOMMU_DRAMDIAG
;
1092 unsigned long tag
= iommu
->iommu_regs
+ IOMMU_TAGDIAG
;
1094 dram
+= (unsigned long)i
* 8UL;
1095 tag
+= (unsigned long)i
* 8UL;
1096 upa_writeq(0, dram
);
1099 upa_readq(iommu
->sbus_control_reg
);
1101 /* Give the TSB to SYSIO. */
1102 upa_writeq(__pa(iommu
->page_table
), iommu
->iommu_regs
+ IOMMU_TSBBASE
);
1104 /* Setup streaming buffer, DE=1 SB_EN=1 */
1105 control
= (1UL << 1UL) | (1UL << 0UL);
1106 upa_writeq(control
, iommu
->strbuf_regs
+ STRBUF_CONTROL
);
1108 /* Clear out the tags using diagnostics. */
1109 for (i
= 0; i
< 16; i
++) {
1110 unsigned long ptag
, ltag
;
1112 ptag
= iommu
->strbuf_regs
+ STRBUF_PTAGDIAG
;
1113 ltag
= iommu
->strbuf_regs
+ STRBUF_LTAGDIAG
;
1114 ptag
+= (unsigned long)i
* 8UL;
1115 ltag
+= (unsigned long)i
* 8UL;
1117 upa_writeq(0UL, ptag
);
1118 upa_writeq(0UL, ltag
);
1121 /* Enable DVMA arbitration for all devices/slots. */
1122 control
= upa_readq(iommu
->sbus_control_reg
);
1124 upa_writeq(control
, iommu
->sbus_control_reg
);
1126 /* Now some Xfire specific grot... */
1127 if (this_is_starfire
)
1128 starfire_hookup(sbus
->portid
);
1130 sysio_register_error_handlers(sbus
);
1133 void sbus_fill_device_irq(struct sbus_dev
*sdev
)
1135 struct device_node
*dp
= of_find_node_by_phandle(sdev
->prom_node
);
1136 struct linux_prom_irqs
*irqs
;
1138 irqs
= of_get_property(dp
, "interrupts", NULL
);
1143 unsigned int pri
= irqs
[0].pri
;
1147 pri
+= sdev
->slot
* 8;
1149 sdev
->irqs
[0] = sbus_build_irq(sdev
->bus
, pri
);
1153 void __init
sbus_arch_bus_ranges_init(struct device_node
*pn
, struct sbus_bus
*sbus
)
1157 void __init
sbus_setup_iommu(struct sbus_bus
*sbus
, struct device_node
*dp
)
1159 sbus_iommu_init(dp
->node
, sbus
);
1162 void __init
sbus_setup_arch_props(struct sbus_bus
*sbus
, struct device_node
*dp
)
1166 int __init
sbus_arch_preinit(void)
1171 void __init
sbus_arch_postinit(void)
1173 extern void firetruck_init(void);