upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / arch / sparc / mm / iommu.c
1 /*
2  * iommu.c:  IOMMU specific routines for memory management.
3  *
4  * Copyright (C) 1995 David S. Miller  (davem@caip.rutgers.edu)
5  * Copyright (C) 1995,2002 Pete Zaitcev     (zaitcev@yahoo.com)
6  * Copyright (C) 1996 Eddie C. Dost    (ecd@skynet.be)
7  * Copyright (C) 1997,1998 Jakub Jelinek    (jj@sunsite.mff.cuni.cz)
8  */
9  
10 #include <linux/config.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
15 #include <linux/highmem.h>      /* pte_offset_map => kmap_atomic */
16
17 #include <asm/scatterlist.h>
18 #include <asm/pgalloc.h>
19 #include <asm/pgtable.h>
20 #include <asm/sbus.h>
21 #include <asm/io.h>
22 #include <asm/mxcc.h>
23 #include <asm/mbus.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
26 #include <asm/bitext.h>
27 #include <asm/iommu.h>
28 #include <asm/dma.h>
29
30 /*
31  * This can be sized dynamically, but we will do this
32  * only when we have a guidance about actual I/O pressures.
33  */
34 #define IOMMU_RNGE      IOMMU_RNGE_256MB
35 #define IOMMU_START     0xF0000000
36 #define IOMMU_WINSIZE   (256*1024*1024U)
37 #define IOMMU_NPTES     (IOMMU_WINSIZE/PAGE_SIZE)       /* 64K PTEs, 265KB */
38 #define IOMMU_ORDER     6                               /* 4096 * (1<<6) */
39
40 /* srmmu.c */
41 extern int viking_mxcc_present;
42 BTFIXUPDEF_CALL(void, flush_page_for_dma, unsigned long)
43 #define flush_page_for_dma(page) BTFIXUP_CALL(flush_page_for_dma)(page)
44 extern int flush_page_for_dma_global;
45 static int viking_flush;
46 /* viking.S */
47 extern void viking_flush_page(unsigned long page);
48 extern void viking_mxcc_flush_page(unsigned long page);
49
50 /*
51  * Values precomputed according to CPU type.
52  */
53 static unsigned int ioperm_noc;         /* Consistent mapping iopte flags */
54 static pgprot_t dvma_prot;              /* Consistent mapping pte flags */
55
56 #define IOPERM        (IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID)
57 #define MKIOPTE(pfn, perm) (((((pfn)<<8) & IOPTE_PAGE) | (perm)) & ~IOPTE_WAZ)
58
59 void __init
60 iommu_init(int iommund, struct sbus_bus *sbus)
61 {
62         unsigned int impl, vers;
63         unsigned long tmp;
64         struct iommu_struct *iommu;
65         struct linux_prom_registers iommu_promregs[PROMREG_MAX];
66         struct resource r;
67         unsigned long *bitmap;
68
69         iommu = kmalloc(sizeof(struct iommu_struct), GFP_ATOMIC);
70         if (!iommu) {
71                 prom_printf("Unable to allocate iommu structure\n");
72                 prom_halt();
73         }
74         prom_getproperty(iommund, "reg", (void *) iommu_promregs,
75                          sizeof(iommu_promregs));
76         memset(&r, 0, sizeof(r));
77         r.flags = iommu_promregs[0].which_io;
78         r.start = iommu_promregs[0].phys_addr;
79         iommu->regs = (struct iommu_regs *)
80                 sbus_ioremap(&r, 0, PAGE_SIZE * 3, "iommu_regs");
81         if(!iommu->regs) {
82                 prom_printf("Cannot map IOMMU registers\n");
83                 prom_halt();
84         }
85         impl = (iommu->regs->control & IOMMU_CTRL_IMPL) >> 28;
86         vers = (iommu->regs->control & IOMMU_CTRL_VERS) >> 24;
87         tmp = iommu->regs->control;
88         tmp &= ~(IOMMU_CTRL_RNGE);
89         tmp |= (IOMMU_RNGE_256MB | IOMMU_CTRL_ENAB);
90         iommu->regs->control = tmp;
91         iommu_invalidate(iommu->regs);
92         iommu->start = IOMMU_START;
93         iommu->end = 0xffffffff;
94
95         /* Allocate IOMMU page table */
96         /* Stupid alignment constraints give me a headache. 
97            We need 256K or 512K or 1M or 2M area aligned to
98            its size and current gfp will fortunately give
99            it to us. */
100         tmp = __get_free_pages(GFP_KERNEL, IOMMU_ORDER);
101         if (!tmp) {
102                 prom_printf("Unable to allocate iommu table [0x%08x]\n",
103                             IOMMU_NPTES*sizeof(iopte_t));
104                 prom_halt();
105         }
106         iommu->page_table = (iopte_t *)tmp;
107
108         /* Initialize new table. */
109         memset(iommu->page_table, 0, IOMMU_NPTES*sizeof(iopte_t));
110         flush_cache_all();
111         flush_tlb_all();
112         iommu->regs->base = __pa((unsigned long) iommu->page_table) >> 4;
113         iommu_invalidate(iommu->regs);
114
115         bitmap = kmalloc(IOMMU_NPTES>>3, GFP_KERNEL);
116         if (!bitmap) {
117                 prom_printf("Unable to allocate iommu bitmap [%d]\n",
118                             (int)(IOMMU_NPTES>>3));
119                 prom_halt();
120         }
121         bit_map_init(&iommu->usemap, bitmap, IOMMU_NPTES);
122
123         printk("IOMMU: impl %d vers %d table 0x%p[%d B] map [%d b]\n",
124             impl, vers, iommu->page_table,
125             (int)(IOMMU_NPTES*sizeof(iopte_t)), (int)IOMMU_NPTES);
126
127         sbus->iommu = iommu;
128 }
129
130 /* This begs to be btfixup-ed by srmmu. */
131 static void iommu_viking_flush_iotlb(iopte_t *iopte, unsigned int niopte)
132 {
133         unsigned long start;
134         unsigned long end;
135
136         start = (unsigned long)iopte & PAGE_MASK;
137         end = PAGE_ALIGN(start + niopte*sizeof(iopte_t));
138         if (viking_mxcc_present) {
139                 while(start < end) {
140                         viking_mxcc_flush_page(start);
141                         start += PAGE_SIZE;
142                 }
143         } else if (viking_flush) {
144                 while(start < end) {
145                         viking_flush_page(start);
146                         start += PAGE_SIZE;
147                 }
148         }
149 }
150
151 static u32 iommu_get_one(struct page *page, int npages, struct sbus_bus *sbus)
152 {
153         struct iommu_struct *iommu = sbus->iommu;
154         int ioptex;
155         iopte_t *iopte, *iopte0;
156         unsigned int busa, busa0;
157         int i;
158
159         ioptex = bit_map_string_get(&iommu->usemap, npages, 1);
160         if (ioptex < 0)
161                 panic("iommu out");
162         busa0 = iommu->start + (ioptex << PAGE_SHIFT);
163         iopte0 = &iommu->page_table[ioptex];
164
165         busa = busa0;
166         iopte = iopte0;
167         for (i = 0; i < npages; i++) {
168                 iopte_val(*iopte) = MKIOPTE(page_to_pfn(page), IOPERM);
169                 iommu_invalidate_page(iommu->regs, busa);
170                 busa += PAGE_SIZE;
171                 iopte++;
172                 page++;
173         }
174
175         iommu_viking_flush_iotlb(iopte0, npages);
176         flush_cache_all(); // hack to fix dma errors with hypersparc
177
178         return busa0;
179 }
180
181 static u32 iommu_get_scsi_one(char *vaddr, unsigned int len,
182     struct sbus_bus *sbus)
183 {
184         unsigned long off;
185         int npages;
186         struct page *page;
187         u32 busa;
188
189         off = (unsigned long)vaddr & ~PAGE_MASK;
190         npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
191         page = virt_to_page((unsigned long)vaddr & PAGE_MASK);
192         busa = iommu_get_one(page, npages, sbus);
193         return busa + off;
194 }
195
196 static __u32 iommu_get_scsi_one_noflush(char *vaddr, unsigned long len, struct sbus_bus *sbus)
197 {
198         return iommu_get_scsi_one(vaddr, len, sbus);
199 }
200
201 static __u32 iommu_get_scsi_one_gflush(char *vaddr, unsigned long len, struct sbus_bus *sbus)
202 {
203         flush_page_for_dma(0);
204         return iommu_get_scsi_one(vaddr, len, sbus);
205 }
206
207 static __u32 iommu_get_scsi_one_pflush(char *vaddr, unsigned long len, struct sbus_bus *sbus)
208 {
209         unsigned long page = ((unsigned long) vaddr) & PAGE_MASK;
210
211         while(page < ((unsigned long)(vaddr + len))) {
212                 flush_page_for_dma(page);
213                 page += PAGE_SIZE;
214         }
215         return iommu_get_scsi_one(vaddr, len, sbus);
216 }
217
218 static void iommu_get_scsi_sgl_noflush(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
219 {
220         int n;
221
222         while (sz != 0) {
223                 --sz;
224                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
225                 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset;
226                 sg->dvma_length = (__u32) sg->length;
227                 sg++;
228         }
229 }
230
231 static void iommu_get_scsi_sgl_gflush(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
232 {
233         int n;
234
235         flush_page_for_dma(0);
236         while (sz != 0) {
237                 --sz;
238                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
239                 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset;
240                 sg->dvma_length = (__u32) sg->length;
241                 sg++;
242         }
243 }
244
245 static void iommu_get_scsi_sgl_pflush(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
246 {
247         unsigned long page, oldpage = 0;
248         int n, i;
249
250         while(sz != 0) {
251                 --sz;
252
253                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
254
255                 /*
256                  * We expect unmapped highmem pages to be not in the cache.
257                  * XXX Is this a good assumption?
258                  * XXX What if someone else unmaps it here and races us?
259                  */
260                 if ((page = (unsigned long) page_address(sg->page)) != 0) {
261                         for (i = 0; i < n; i++) {
262                                 if (page != oldpage) {  /* Already flushed? */
263                                         flush_page_for_dma(page);
264                                         oldpage = page;
265                                 }
266                                 page += PAGE_SIZE;
267                         }
268                 }
269
270                 sg->dvma_address = iommu_get_one(sg->page, n, sbus) + sg->offset;
271                 sg->dvma_length = (__u32) sg->length;
272                 sg++;
273         }
274 }
275
276 static void iommu_release_one(u32 busa, int npages, struct sbus_bus *sbus)
277 {
278         struct iommu_struct *iommu = sbus->iommu;
279         int ioptex;
280         int i;
281
282         if (busa < iommu->start)
283                 BUG();
284         ioptex = (busa - iommu->start) >> PAGE_SHIFT;
285         for (i = 0; i < npages; i++) {
286                 iopte_val(iommu->page_table[ioptex + i]) = 0;
287                 iommu_invalidate_page(iommu->regs, busa);
288                 busa += PAGE_SIZE;
289         }
290         bit_map_clear(&iommu->usemap, ioptex, npages);
291 }
292
293 static void iommu_release_scsi_one(__u32 vaddr, unsigned long len, struct sbus_bus *sbus)
294 {
295         unsigned long off;
296         int npages;
297
298         off = vaddr & ~PAGE_MASK;
299         npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
300         iommu_release_one(vaddr & PAGE_MASK, npages, sbus);
301 }
302
303 static void iommu_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
304 {
305         int n;
306
307         while(sz != 0) {
308                 --sz;
309
310                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
311                 iommu_release_one(sg->dvma_address & PAGE_MASK, n, sbus);
312                 sg->dvma_address = 0x21212121;
313                 sg++;
314         }
315 }
316
317 #ifdef CONFIG_SBUS
318 static int iommu_map_dma_area(dma_addr_t *pba, unsigned long va,
319     unsigned long addr, int len)
320 {
321         unsigned long page, end;
322         struct iommu_struct *iommu = sbus_root->iommu;
323         iopte_t *iopte = iommu->page_table;
324         iopte_t *first;
325         int ioptex;
326
327         if ((va & ~PAGE_MASK) != 0) BUG();
328         if ((addr & ~PAGE_MASK) != 0) BUG();
329         if ((len & ~PAGE_MASK) != 0) BUG();
330
331         ioptex = bit_map_string_get(&iommu->usemap, len >> PAGE_SHIFT, 1);
332         if (ioptex < 0)
333                 panic("iommu out");
334
335         iopte += ioptex;
336         first = iopte;
337         end = addr + len;
338         while(addr < end) {
339                 page = va;
340                 {
341                         pgd_t *pgdp;
342                         pmd_t *pmdp;
343                         pte_t *ptep;
344
345                         if (viking_mxcc_present)
346                                 viking_mxcc_flush_page(page);
347                         else if (viking_flush)
348                                 viking_flush_page(page);
349                         else
350                                 __flush_page_to_ram(page);
351
352                         pgdp = pgd_offset(&init_mm, addr);
353                         pmdp = pmd_offset(pgdp, addr);
354                         ptep = pte_offset_map(pmdp, addr);
355
356                         set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
357                 }
358                 iopte_val(*iopte++) =
359                     MKIOPTE(page_to_pfn(virt_to_page(page)), ioperm_noc);
360                 addr += PAGE_SIZE;
361                 va += PAGE_SIZE;
362         }
363         /* P3: why do we need this?
364          *
365          * DAVEM: Because there are several aspects, none of which
366          *        are handled by a single interface.  Some cpus are
367          *        completely not I/O DMA coherent, and some have
368          *        virtually indexed caches.  The driver DMA flushing
369          *        methods handle the former case, but here during
370          *        IOMMU page table modifications, and usage of non-cacheable
371          *        cpu mappings of pages potentially in the cpu caches, we have
372          *        to handle the latter case as well.
373          */
374         flush_cache_all();
375         iommu_viking_flush_iotlb(first, len >> PAGE_SHIFT);
376         flush_tlb_all();
377         iommu_invalidate(iommu->regs);
378
379         *pba = iommu->start + (ioptex << PAGE_SHIFT);
380         return 0;
381 }
382
383 static void iommu_unmap_dma_area(unsigned long busa, int len)
384 {
385         struct iommu_struct *iommu = sbus_root->iommu;
386         iopte_t *iopte = iommu->page_table;
387         unsigned long end;
388         int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
389
390         if ((busa & ~PAGE_MASK) != 0) BUG();
391         if ((len & ~PAGE_MASK) != 0) BUG();
392
393         iopte += ioptex;
394         end = busa + len;
395         while (busa < end) {
396                 iopte_val(*iopte++) = 0;
397                 busa += PAGE_SIZE;
398         }
399         flush_tlb_all();
400         iommu_invalidate(iommu->regs);
401         bit_map_clear(&iommu->usemap, ioptex, len >> PAGE_SHIFT);
402 }
403
404 static struct page *iommu_translate_dvma(unsigned long busa)
405 {
406         struct iommu_struct *iommu = sbus_root->iommu;
407         iopte_t *iopte = iommu->page_table;
408
409         iopte += ((busa - iommu->start) >> PAGE_SHIFT);
410         return pfn_to_page((iopte_val(*iopte) & IOPTE_PAGE) >> (PAGE_SHIFT-4));
411 }
412 #endif
413
414 static char *iommu_lockarea(char *vaddr, unsigned long len)
415 {
416         return vaddr;
417 }
418
419 static void iommu_unlockarea(char *vaddr, unsigned long len)
420 {
421 }
422
423 void __init ld_mmu_iommu(void)
424 {
425         viking_flush = (BTFIXUPVAL_CALL(flush_page_for_dma) == (unsigned long)viking_flush_page);
426         BTFIXUPSET_CALL(mmu_lockarea, iommu_lockarea, BTFIXUPCALL_RETO0);
427         BTFIXUPSET_CALL(mmu_unlockarea, iommu_unlockarea, BTFIXUPCALL_NOP);
428
429         if (!BTFIXUPVAL_CALL(flush_page_for_dma)) {
430                 /* IO coherent chip */
431                 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_noflush, BTFIXUPCALL_RETO0);
432                 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_noflush, BTFIXUPCALL_NORM);
433         } else if (flush_page_for_dma_global) {
434                 /* flush_page_for_dma flushes everything, no matter of what page is it */
435                 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_gflush, BTFIXUPCALL_NORM);
436                 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_gflush, BTFIXUPCALL_NORM);
437         } else {
438                 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_pflush, BTFIXUPCALL_NORM);
439                 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_pflush, BTFIXUPCALL_NORM);
440         }
441         BTFIXUPSET_CALL(mmu_release_scsi_one, iommu_release_scsi_one, BTFIXUPCALL_NORM);
442         BTFIXUPSET_CALL(mmu_release_scsi_sgl, iommu_release_scsi_sgl, BTFIXUPCALL_NORM);
443
444 #ifdef CONFIG_SBUS
445         BTFIXUPSET_CALL(mmu_map_dma_area, iommu_map_dma_area, BTFIXUPCALL_NORM);
446         BTFIXUPSET_CALL(mmu_unmap_dma_area, iommu_unmap_dma_area, BTFIXUPCALL_NORM);
447         BTFIXUPSET_CALL(mmu_translate_dvma, iommu_translate_dvma, BTFIXUPCALL_NORM);
448 #endif
449
450         if (viking_mxcc_present || srmmu_modtype == HyperSparc) {
451                 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
452                 ioperm_noc = IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID;
453         } else {
454                 dvma_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV);
455                 ioperm_noc = IOPTE_WRITE | IOPTE_VALID;
456         }
457 }