sparc32: Kill mmu_translate_dvma and implementations.
[deliverable/linux.git] / arch / sparc / mm / io-unit.c
CommitLineData
88278ca2 1/*
1da177e4
LT
2 * io-unit.c: IO-UNIT specific routines for memory management.
3 *
4 * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5 */
6
1da177e4
LT
7#include <linux/kernel.h>
8#include <linux/init.h>
9#include <linux/slab.h>
10#include <linux/spinlock.h>
11#include <linux/mm.h>
12#include <linux/highmem.h> /* pte_offset_map => kmap_atomic */
13#include <linux/bitops.h>
0912a5db 14#include <linux/scatterlist.h>
1da177e4 15
1da177e4
LT
16#include <asm/pgalloc.h>
17#include <asm/pgtable.h>
18#include <asm/sbus.h>
19#include <asm/io.h>
20#include <asm/io-unit.h>
21#include <asm/mxcc.h>
22#include <asm/cacheflush.h>
23#include <asm/tlbflush.h>
24#include <asm/dma.h>
d4accd60 25#include <asm/oplib.h>
1da177e4
LT
26
27/* #define IOUNIT_DEBUG */
28#ifdef IOUNIT_DEBUG
29#define IOD(x) printk(x)
30#else
31#define IOD(x) do { } while (0)
32#endif
33
34#define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
35#define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
36
e0039348 37void __init iounit_init(struct sbus_bus *sbus)
1da177e4 38{
e0039348 39 struct device_node *dp = sbus->ofdev.node;
1da177e4 40 struct iounit_struct *iounit;
e0039348
DM
41 iopte_t *xpt, *xptend;
42 struct of_device *op;
43
44 op = of_find_device_by_node(dp);
45 if (!op) {
46 prom_printf("SUN4D: Cannot find SBI of_device.\n");
47 prom_halt();
48 }
1da177e4 49
c80892d1 50 iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
d4accd60
DM
51 if (!iounit) {
52 prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
53 prom_halt();
54 }
1da177e4 55
1da177e4
LT
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;
62
e0039348
DM
63 xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
64 if (!xpt) {
65 prom_printf("SUN4D: Cannot map External Page Table.");
66 prom_halt();
1da177e4 67 }
1da177e4 68
3ac4c949 69 sbus->ofdev.dev.archdata.iommu = iounit;
e0039348 70 op->dev.archdata.iommu = iounit;
1da177e4 71 iounit->page_table = xpt;
2f72ba43 72 spin_lock_init(&iounit->lock);
1da177e4
LT
73
74 for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
75 xpt < xptend;)
76 iopte_val(*xpt++) = 0;
77}
78
79/* One has to hold iounit->lock to call this */
80static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
81{
82 int i, j, k, npages;
83 unsigned long rotor, scan, limit;
84 iopte_t iopte;
85
86 npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
87
88 /* A tiny bit of magic ingredience :) */
89 switch (npages) {
90 case 1: i = 0x0231; break;
91 case 2: i = 0x0132; break;
92 default: i = 0x0213; break;
93 }
94
95 IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
96
97next: j = (i & 15);
98 rotor = iounit->rotor[j - 1];
99 limit = iounit->limit[j];
100 scan = rotor;
101nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
102 if (scan + npages > limit) {
103 if (limit != rotor) {
104 limit = rotor;
105 scan = iounit->limit[j - 1];
106 goto nexti;
107 }
108 i >>= 4;
109 if (!(i & 15))
110 panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
111 goto next;
112 }
113 for (k = 1, scan++; k < npages; k++)
114 if (test_bit(scan++, iounit->bmap))
115 goto nexti;
116 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
117 scan -= npages;
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;
123 }
124 IOD(("%08lx\n", vaddr));
125 return vaddr;
126}
127
260489fa 128static __u32 iounit_get_scsi_one(struct device *dev, char *vaddr, unsigned long len)
1da177e4 129{
260489fa 130 struct iounit_struct *iounit = dev->archdata.iommu;
1da177e4 131 unsigned long ret, flags;
1da177e4
LT
132
133 spin_lock_irqsave(&iounit->lock, flags);
134 ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
135 spin_unlock_irqrestore(&iounit->lock, flags);
136 return ret;
137}
138
260489fa 139static void iounit_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
1da177e4 140{
260489fa 141 struct iounit_struct *iounit = dev->archdata.iommu;
1da177e4 142 unsigned long flags;
1da177e4
LT
143
144 /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
145 spin_lock_irqsave(&iounit->lock, flags);
146 while (sz != 0) {
147 --sz;
6ee4e28b 148 sg->dvma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
0912a5db
JA
149 sg->dvma_length = sg->length;
150 sg = sg_next(sg);
1da177e4
LT
151 }
152 spin_unlock_irqrestore(&iounit->lock, flags);
153}
154
260489fa 155static void iounit_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
1da177e4 156{
260489fa 157 struct iounit_struct *iounit = dev->archdata.iommu;
1da177e4 158 unsigned long flags;
1da177e4
LT
159
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);
167}
168
260489fa 169static void iounit_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
1da177e4 170{
260489fa 171 struct iounit_struct *iounit = dev->archdata.iommu;
1da177e4
LT
172 unsigned long flags;
173 unsigned long vaddr, len;
1da177e4
LT
174
175 spin_lock_irqsave(&iounit->lock, flags);
176 while (sz != 0) {
177 --sz;
0912a5db
JA
178 len = ((sg->dvma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
179 vaddr = (sg->dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
1da177e4
LT
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);
0912a5db 183 sg = sg_next(sg);
1da177e4
LT
184 }
185 spin_unlock_irqrestore(&iounit->lock, flags);
186}
187
188#ifdef CONFIG_SBUS
189static int iounit_map_dma_area(dma_addr_t *pba, unsigned long va, __u32 addr, int len)
190{
191 unsigned long page, end;
192 pgprot_t dvma_prot;
193 iopte_t *iopte;
194 struct sbus_bus *sbus;
195
196 *pba = addr;
197
198 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
199 end = PAGE_ALIGN((addr + len));
200 while(addr < end) {
201 page = va;
202 {
203 pgd_t *pgdp;
204 pmd_t *pmdp;
205 pte_t *ptep;
206 long i;
207
208 pgdp = pgd_offset(&init_mm, addr);
209 pmdp = pmd_offset(pgdp, addr);
210 ptep = pte_offset_map(pmdp, addr);
211
212 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
213
214 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
215
216 for_each_sbus(sbus) {
3ac4c949 217 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
1da177e4
LT
218
219 iopte = (iopte_t *)(iounit->page_table + i);
220 *iopte = MKIOPTE(__pa(page));
221 }
222 }
223 addr += PAGE_SIZE;
224 va += PAGE_SIZE;
225 }
226 flush_cache_all();
227 flush_tlb_all();
228
229 return 0;
230}
231
232static void iounit_unmap_dma_area(unsigned long addr, int len)
233{
234 /* XXX Somebody please fill this in */
235}
1da177e4
LT
236#endif
237
238static char *iounit_lockarea(char *vaddr, unsigned long len)
239{
240/* FIXME: Write this */
241 return vaddr;
242}
243
244static void iounit_unlockarea(char *vaddr, unsigned long len)
245{
246/* FIXME: Write this */
247}
248
249void __init ld_mmu_iounit(void)
250{
251 BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0);
252 BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP);
253
254 BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM);
255 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM);
256 BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM);
257 BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM);
258
259#ifdef CONFIG_SBUS
260 BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM);
261 BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM);
1da177e4
LT
262#endif
263}
264
265__u32 iounit_map_dma_init(struct sbus_bus *sbus, int size)
266{
267 int i, j, k, npages;
268 unsigned long rotor, scan, limit;
269 unsigned long flags;
270 __u32 ret;
3ac4c949 271 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
1da177e4
LT
272
273 npages = (size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
274 i = 0x0213;
275 spin_lock_irqsave(&iounit->lock, flags);
276next: j = (i & 15);
277 rotor = iounit->rotor[j - 1];
278 limit = iounit->limit[j];
279 scan = rotor;
280nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan);
281 if (scan + npages > limit) {
282 if (limit != rotor) {
283 limit = rotor;
284 scan = iounit->limit[j - 1];
285 goto nexti;
286 }
287 i >>= 4;
288 if (!(i & 15))
289 panic("iounit_map_dma_init: Couldn't find free iopte slots for %d bytes\n", size);
290 goto next;
291 }
292 for (k = 1, scan++; k < npages; k++)
293 if (test_bit(scan++, iounit->bmap))
294 goto nexti;
295 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
296 scan -= npages;
297 ret = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT);
298 for (k = 0; k < npages; k++, scan++)
299 set_bit(scan, iounit->bmap);
300 spin_unlock_irqrestore(&iounit->lock, flags);
301 return ret;
302}
303
304__u32 iounit_map_dma_page(__u32 vaddr, void *addr, struct sbus_bus *sbus)
305{
306 int scan = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
3ac4c949 307 struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu;
1da177e4
LT
308
309 iounit->page_table[scan] = MKIOPTE(__pa(((unsigned long)addr) & PAGE_MASK));
310 return vaddr + (((unsigned long)addr) & ~PAGE_MASK);
311}
This page took 0.537983 seconds and 5 git commands to generate.