ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sparc / mm / io-unit.c
1 /* $Id: io-unit.c,v 1.24 2001/12/17 07:05:09 davem Exp $
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  
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/mm.h>
13 #include <linux/highmem.h>      /* pte_offset_map => kmap_atomic */
14
15 #include <asm/scatterlist.h>
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/bitops.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
25
26 /* #define IOUNIT_DEBUG */
27 #ifdef IOUNIT_DEBUG
28 #define IOD(x) printk(x)
29 #else
30 #define IOD(x) do { } while (0)
31 #endif
32
33 #define IOPERM        (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
34 #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
35
36 void __init
37 iounit_init(int sbi_node, int io_node, struct sbus_bus *sbus)
38 {
39         iopte_t *xpt, *xptend;
40         struct iounit_struct *iounit;
41         struct linux_prom_registers iommu_promregs[PROMREG_MAX];
42         struct resource r;
43
44         iounit = kmalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
45
46         memset(iounit, 0, sizeof(*iounit));
47         iounit->limit[0] = IOUNIT_BMAP1_START;
48         iounit->limit[1] = IOUNIT_BMAP2_START;
49         iounit->limit[2] = IOUNIT_BMAPM_START;
50         iounit->limit[3] = IOUNIT_BMAPM_END;
51         iounit->rotor[1] = IOUNIT_BMAP2_START;
52         iounit->rotor[2] = IOUNIT_BMAPM_START;
53
54         prom_getproperty(sbi_node, "reg", (void *) iommu_promregs,
55                          sizeof(iommu_promregs));
56         prom_apply_generic_ranges(io_node, 0, iommu_promregs, 3);
57         memset(&r, 0, sizeof(r));
58         r.flags = iommu_promregs[2].which_io;
59         r.start = iommu_promregs[2].phys_addr;
60         xpt = (iopte_t *) sbus_ioremap(&r, 0, PAGE_SIZE * 16, "XPT");
61         if(!xpt) panic("Cannot map External Page Table.");
62         
63         sbus->iommu = (struct iommu_struct *)iounit;
64         iounit->page_table = xpt;
65         
66         for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
67              xpt < xptend;)
68                 iopte_val(*xpt++) = 0;
69 }
70
71 /* One has to hold iounit->lock to call this */
72 static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
73 {
74         int i, j, k, npages;
75         unsigned long rotor, scan, limit;
76         iopte_t iopte;
77
78         npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
79
80         /* A tiny bit of magic ingredience :) */
81         switch (npages) {
82         case 1: i = 0x0231; break;
83         case 2: i = 0x0132; break;
84         default: i = 0x0213; break;
85         }
86         
87         IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
88         
89 next:   j = (i & 15);
90         rotor = iounit->rotor[j - 1];
91         limit = iounit->limit[j];
92         scan = rotor;
93 nexti:  scan = find_next_zero_bit(iounit->bmap, limit, scan);
94         if (scan + npages > limit) {
95                 if (limit != rotor) {
96                         limit = rotor;
97                         scan = iounit->limit[j - 1];
98                         goto nexti;
99                 }
100                 i >>= 4;
101                 if (!(i & 15))
102                         panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
103                 goto next;
104         }
105         for (k = 1, scan++; k < npages; k++)
106                 if (test_bit(scan++, iounit->bmap))
107                         goto nexti;
108         iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
109         scan -= npages;
110         iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
111         vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
112         for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
113                 set_bit(scan, iounit->bmap);
114                 iounit->page_table[scan] = iopte;
115         }
116         IOD(("%08lx\n", vaddr));
117         return vaddr;
118 }
119
120 static __u32 iounit_get_scsi_one(char *vaddr, unsigned long len, struct sbus_bus *sbus)
121 {
122         unsigned long ret, flags;
123         struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
124         
125         spin_lock_irqsave(&iounit->lock, flags);
126         ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
127         spin_unlock_irqrestore(&iounit->lock, flags);
128         return ret;
129 }
130
131 static void iounit_get_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
132 {
133         unsigned long flags;
134         struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
135
136         /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
137         spin_lock_irqsave(&iounit->lock, flags);
138         while (sz != 0) {
139                 --sz;
140                 sg[sz].dvma_address = iounit_get_area(iounit, (unsigned long)page_address(sg[sz].page) + sg[sz].offset, sg[sz].length);
141                 sg[sz].dvma_length = sg[sz].length;
142         }
143         spin_unlock_irqrestore(&iounit->lock, flags);
144 }
145
146 static void iounit_release_scsi_one(__u32 vaddr, unsigned long len, struct sbus_bus *sbus)
147 {
148         unsigned long flags;
149         struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
150         
151         spin_lock_irqsave(&iounit->lock, flags);
152         len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
153         vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
154         IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
155         for (len += vaddr; vaddr < len; vaddr++)
156                 clear_bit(vaddr, iounit->bmap);
157         spin_unlock_irqrestore(&iounit->lock, flags);
158 }
159
160 static void iounit_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
161 {
162         unsigned long flags;
163         unsigned long vaddr, len;
164         struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
165
166         spin_lock_irqsave(&iounit->lock, flags);
167         while (sz != 0) {
168                 --sz;
169                 len = ((sg[sz].dvma_address & ~PAGE_MASK) + sg[sz].length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
170                 vaddr = (sg[sz].dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
171                 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
172                 for (len += vaddr; vaddr < len; vaddr++)
173                         clear_bit(vaddr, iounit->bmap);
174         }
175         spin_unlock_irqrestore(&iounit->lock, flags);
176 }
177
178 #ifdef CONFIG_SBUS
179 static int iounit_map_dma_area(dma_addr_t *pba, unsigned long va, __u32 addr, int len)
180 {
181         unsigned long page, end;
182         pgprot_t dvma_prot;
183         iopte_t *iopte;
184         struct sbus_bus *sbus;
185
186         *pba = addr;
187
188         dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
189         end = PAGE_ALIGN((addr + len));
190         while(addr < end) {
191                 page = va;
192                 {
193                         pgd_t *pgdp;
194                         pmd_t *pmdp;
195                         pte_t *ptep;
196                         long i;
197
198                         pgdp = pgd_offset(init_task.mm, addr);
199                         pmdp = pmd_offset(pgdp, addr);
200                         ptep = pte_offset_map(pmdp, addr);
201
202                         set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
203                         
204                         i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
205
206                         for_each_sbus(sbus) {
207                                 struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
208
209                                 iopte = (iopte_t *)(iounit->page_table + i);
210                                 *iopte = MKIOPTE(__pa(page));
211                         }
212                 }
213                 addr += PAGE_SIZE;
214                 va += PAGE_SIZE;
215         }
216         flush_cache_all();
217         flush_tlb_all();
218
219         return 0;
220 }
221
222 static void iounit_unmap_dma_area(unsigned long addr, int len)
223 {
224         /* XXX Somebody please fill this in */
225 }
226
227 /* XXX We do not pass sbus device here, bad. */
228 static struct page *iounit_translate_dvma(unsigned long addr)
229 {
230         struct sbus_bus *sbus = sbus_root;      /* They are all the same */
231         struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
232         int i;
233         iopte_t *iopte;
234
235         i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
236         iopte = (iopte_t *)(iounit->page_table + i);
237         return pfn_to_page(iopte_val(*iopte) >> (PAGE_SHIFT-4)); /* XXX sun4d guru, help */
238 }
239 #endif
240
241 static char *iounit_lockarea(char *vaddr, unsigned long len)
242 {
243 /* FIXME: Write this */
244         return vaddr;
245 }
246
247 static void iounit_unlockarea(char *vaddr, unsigned long len)
248 {
249 /* FIXME: Write this */
250 }
251
252 void __init ld_mmu_iounit(void)
253 {
254         BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0);
255         BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP);
256
257         BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM);
258         BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM);
259         BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM);
260         BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM);
261
262 #ifdef CONFIG_SBUS
263         BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM);
264         BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM);
265         BTFIXUPSET_CALL(mmu_translate_dvma, iounit_translate_dvma, BTFIXUPCALL_NORM);
266 #endif
267 }
268
269 __u32 iounit_map_dma_init(struct sbus_bus *sbus, int size)
270 {
271         int i, j, k, npages;
272         unsigned long rotor, scan, limit;
273         unsigned long flags;
274         __u32 ret;
275         struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
276
277         npages = (size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
278         i = 0x0213;
279         spin_lock_irqsave(&iounit->lock, flags);
280 next:   j = (i & 15);
281         rotor = iounit->rotor[j - 1];
282         limit = iounit->limit[j];
283         scan = rotor;
284 nexti:  scan = find_next_zero_bit(iounit->bmap, limit, scan);
285         if (scan + npages > limit) {
286                 if (limit != rotor) {
287                         limit = rotor;
288                         scan = iounit->limit[j - 1];
289                         goto nexti;
290                 }
291                 i >>= 4;
292                 if (!(i & 15))
293                         panic("iounit_map_dma_init: Couldn't find free iopte slots for %d bytes\n", size);
294                 goto next;
295         }
296         for (k = 1, scan++; k < npages; k++)
297                 if (test_bit(scan++, iounit->bmap))
298                         goto nexti;
299         iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
300         scan -= npages;
301         ret = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT);
302         for (k = 0; k < npages; k++, scan++)
303                 set_bit(scan, iounit->bmap);
304         spin_unlock_irqrestore(&iounit->lock, flags);
305         return ret;
306 }
307
308 __u32 iounit_map_dma_page(__u32 vaddr, void *addr, struct sbus_bus *sbus)
309 {
310         int scan = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
311         struct iounit_struct *iounit = (struct iounit_struct *)sbus->iommu;
312         
313         iounit->page_table[scan] = MKIOPTE(__pa(((unsigned long)addr) & PAGE_MASK));
314         return vaddr + (((unsigned long)addr) & ~PAGE_MASK);
315 }