2 * io-unit.c: IO-UNIT specific routines for memory management.
4 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/spinlock.h>
12 #include <linux/highmem.h> /* pte_offset_map => kmap_atomic */
13 #include <linux/bitops.h>
14 #include <linux/scatterlist.h>
16 #include <asm/pgalloc.h>
17 #include <asm/pgtable.h>
20 #include <asm/io-unit.h>
22 #include <asm/cacheflush.h>
23 #include <asm/tlbflush.h>
25 #include <asm/oplib.h>
27 /* #define IOUNIT_DEBUG */
29 #define IOD(x) printk(x)
31 #define IOD(x) do { } while (0)
34 #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
35 #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
37 void __init iounit_init(struct sbus_bus *sbus)
39 struct device_node *dp = sbus->ofdev.node;
40 struct iounit_struct *iounit;
41 iopte_t *xpt, *xptend;
44 op = of_find_device_by_node(dp);
46 prom_printf("SUN4D: Cannot find SBI of_device.\n");
50 iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
52 prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
56 iounit->limit[0] = IOUNIT_BMAP1_START;
57 iounit->limit[1] = IOUNIT_BMAP2_START;
58 iounit->limit[2] = IOUNIT_BMAPM_START;
59 iounit->limit[3] = IOUNIT_BMAPM_END;
60 iounit->rotor[1] = IOUNIT_BMAP2_START;
61 iounit->rotor[2] = IOUNIT_BMAPM_START;
63 xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
65 prom_printf("SUN4D: Cannot map External Page Table.");
69 sbus->ofdev.dev.archdata.iommu = iounit;
70 op->dev.archdata.iommu = iounit;
71 iounit->page_table = xpt;
72 spin_lock_init(&iounit->lock);
74 for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
76 iopte_val(*xpt++) = 0;
79 /* One has to hold iounit->lock to call this */
80 static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
83 unsigned long rotor, scan, limit;
86 npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
88 /* A tiny bit of magic ingredience :) */
90 case 1: i = 0x0231; break;
91 case 2: i = 0x0132; break;
92 default: i = 0x0213; break;
95 IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
98 rotor = iounit->rotor[j - 1];
99 limit = iounit->limit[j];
101 nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
102 if (scan + npages > limit) {
103 if (limit != rotor) {
105 scan = iounit->limit[j - 1];
110 panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
113 for (k = 1, scan++; k < npages; k++)
114 if (test_bit(scan++, iounit->bmap))
116 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
118 iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
119 vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
120 for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
121 set_bit(scan, iounit->bmap);
122 iounit->page_table[scan] = iopte;
124 IOD(("%08lx\n", vaddr));
128 static __u32 iounit_get_scsi_one(struct device *dev, char *vaddr, unsigned long len)
130 struct iounit_struct *iounit = dev->archdata.iommu;
131 unsigned long ret, flags;
133 spin_lock_irqsave(&iounit->lock, flags);
134 ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
135 spin_unlock_irqrestore(&iounit->lock, flags);
139 static void iounit_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
141 struct iounit_struct *iounit = dev->archdata.iommu;
144 /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
145 spin_lock_irqsave(&iounit->lock, flags);
148 sg->dvma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
149 sg->dvma_length = sg->length;
152 spin_unlock_irqrestore(&iounit->lock, flags);
155 static void iounit_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
157 struct iounit_struct *iounit = dev->archdata.iommu;
160 spin_lock_irqsave(&iounit->lock, flags);
161 len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
162 vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
163 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
164 for (len += vaddr; vaddr < len; vaddr++)
165 clear_bit(vaddr, iounit->bmap);
166 spin_unlock_irqrestore(&iounit->lock, flags);
169 static void iounit_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
171 struct iounit_struct *iounit = dev->archdata.iommu;
173 unsigned long vaddr, len;
175 spin_lock_irqsave(&iounit->lock, flags);
178 len = ((sg->dvma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
179 vaddr = (sg->dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
180 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
181 for (len += vaddr; vaddr < len; vaddr++)
182 clear_bit(vaddr, iounit->bmap);
185 spin_unlock_irqrestore(&iounit->lock, flags);
189 static int iounit_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va, __u32 addr, int len)
191 struct iounit_struct *iounit = dev->archdata.iommu;
192 unsigned long page, end;
198 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
199 end = PAGE_ALIGN((addr + len));
208 pgdp = pgd_offset(&init_mm, addr);
209 pmdp = pmd_offset(pgdp, addr);
210 ptep = pte_offset_map(pmdp, addr);
212 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
214 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
216 iopte = (iopte_t *)(iounit->page_table + i);
217 *iopte = MKIOPTE(__pa(page));
228 static void iounit_unmap_dma_area(struct device *dev, unsigned long addr, int len)
230 /* XXX Somebody please fill this in */
234 static char *iounit_lockarea(char *vaddr, unsigned long len)
236 /* FIXME: Write this */
240 static void iounit_unlockarea(char *vaddr, unsigned long len)
242 /* FIXME: Write this */
245 void __init ld_mmu_iounit(void)
247 BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0);
248 BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP);
250 BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM);
251 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM);
252 BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM);
253 BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM);
256 BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM);
257 BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM);
261 __u32 iounit_map_dma_init(struct sbus_bus *sbus, int size)
264 unsigned long rotor, scan, limit;
267 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
269 npages = (size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
271 spin_lock_irqsave(&iounit->lock, flags);
273 rotor = iounit->rotor[j - 1];
274 limit = iounit->limit[j];
276 nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
277 if (scan + npages > limit) {
278 if (limit != rotor) {
280 scan = iounit->limit[j - 1];
285 panic("iounit_map_dma_init: Couldn't find free iopte slots for %d bytes\n", size);
288 for (k = 1, scan++; k < npages; k++)
289 if (test_bit(scan++, iounit->bmap))
291 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
293 ret = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT);
294 for (k = 0; k < npages; k++, scan++)
295 set_bit(scan, iounit->bmap);
296 spin_unlock_irqrestore(&iounit->lock, flags);
300 __u32 iounit_map_dma_page(__u32 vaddr, void *addr, struct sbus_bus *sbus)
302 int scan = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
303 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
305 iounit->page_table[scan] = MKIOPTE(__pa(((unsigned long)addr) & PAGE_MASK));
306 return vaddr + (((unsigned long)addr) & ~PAGE_MASK);