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