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