sparc: Remove generic SBUS probing layer.
[safe/jmp/linux-2.6] / 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/kernel.h>
11 #include <linux/init.h>
12 #include <linux/mm.h>
13 #include <linux/slab.h>
14 #include <linux/highmem.h>      /* pte_offset_map => kmap_atomic */
15 #include <linux/scatterlist.h>
16
17 #include <asm/pgalloc.h>
18 #include <asm/pgtable.h>
19 #include <asm/sbus.h>
20 #include <asm/io.h>
21 #include <asm/mxcc.h>
22 #include <asm/mbus.h>
23 #include <asm/cacheflush.h>
24 #include <asm/tlbflush.h>
25 #include <asm/bitext.h>
26 #include <asm/iommu.h>
27 #include <asm/dma.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 static void __init sbus_iommu_init(struct of_device *op)
59 {
60         struct iommu_struct *iommu;
61         unsigned int impl, vers;
62         unsigned long *bitmap;
63         unsigned long tmp;
64
65         iommu = kmalloc(sizeof(struct iommu_struct), GFP_ATOMIC);
66         if (!iommu) {
67                 prom_printf("Unable to allocate iommu structure\n");
68                 prom_halt();
69         }
70
71         iommu->regs = of_ioremap(&op->resource[0], 0, PAGE_SIZE * 3,
72                                  "iommu_regs");
73         if (!iommu->regs) {
74                 prom_printf("Cannot map IOMMU registers\n");
75                 prom_halt();
76         }
77         impl = (iommu->regs->control & IOMMU_CTRL_IMPL) >> 28;
78         vers = (iommu->regs->control & IOMMU_CTRL_VERS) >> 24;
79         tmp = iommu->regs->control;
80         tmp &= ~(IOMMU_CTRL_RNGE);
81         tmp |= (IOMMU_RNGE_256MB | IOMMU_CTRL_ENAB);
82         iommu->regs->control = tmp;
83         iommu_invalidate(iommu->regs);
84         iommu->start = IOMMU_START;
85         iommu->end = 0xffffffff;
86
87         /* Allocate IOMMU page table */
88         /* Stupid alignment constraints give me a headache. 
89            We need 256K or 512K or 1M or 2M area aligned to
90            its size and current gfp will fortunately give
91            it to us. */
92         tmp = __get_free_pages(GFP_KERNEL, IOMMU_ORDER);
93         if (!tmp) {
94                 prom_printf("Unable to allocate iommu table [0x%08x]\n",
95                             IOMMU_NPTES*sizeof(iopte_t));
96                 prom_halt();
97         }
98         iommu->page_table = (iopte_t *)tmp;
99
100         /* Initialize new table. */
101         memset(iommu->page_table, 0, IOMMU_NPTES*sizeof(iopte_t));
102         flush_cache_all();
103         flush_tlb_all();
104         iommu->regs->base = __pa((unsigned long) iommu->page_table) >> 4;
105         iommu_invalidate(iommu->regs);
106
107         bitmap = kmalloc(IOMMU_NPTES>>3, GFP_KERNEL);
108         if (!bitmap) {
109                 prom_printf("Unable to allocate iommu bitmap [%d]\n",
110                             (int)(IOMMU_NPTES>>3));
111                 prom_halt();
112         }
113         bit_map_init(&iommu->usemap, bitmap, IOMMU_NPTES);
114         /* To be coherent on HyperSparc, the page color of DVMA
115          * and physical addresses must match.
116          */
117         if (srmmu_modtype == HyperSparc)
118                 iommu->usemap.num_colors = vac_cache_size >> PAGE_SHIFT;
119         else
120                 iommu->usemap.num_colors = 1;
121
122         printk(KERN_INFO "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         op->dev.archdata.iommu = iommu;
127 }
128
129 static int __init iommu_init(void)
130 {
131         struct device_node *dp;
132
133         for_each_node_by_name(dp, "iommu") {
134                 struct of_device *op = of_find_device_by_node(dp);
135
136                 sbus_iommu_init(op);
137                 of_propagate_archdata(op);
138         }
139
140         return 0;
141 }
142
143 subsys_initcall(iommu_init);
144
145 /* This begs to be btfixup-ed by srmmu. */
146 /* Flush the iotlb entries to ram. */
147 /* This could be better if we didn't have to flush whole pages. */
148 static void iommu_flush_iotlb(iopte_t *iopte, unsigned int niopte)
149 {
150         unsigned long start;
151         unsigned long end;
152
153         start = (unsigned long)iopte;
154         end = PAGE_ALIGN(start + niopte*sizeof(iopte_t));
155         start &= PAGE_MASK;
156         if (viking_mxcc_present) {
157                 while(start < end) {
158                         viking_mxcc_flush_page(start);
159                         start += PAGE_SIZE;
160                 }
161         } else if (viking_flush) {
162                 while(start < end) {
163                         viking_flush_page(start);
164                         start += PAGE_SIZE;
165                 }
166         } else {
167                 while(start < end) {
168                         __flush_page_to_ram(start);
169                         start += PAGE_SIZE;
170                 }
171         }
172 }
173
174 static u32 iommu_get_one(struct device *dev, struct page *page, int npages)
175 {
176         struct iommu_struct *iommu = dev->archdata.iommu;
177         int ioptex;
178         iopte_t *iopte, *iopte0;
179         unsigned int busa, busa0;
180         int i;
181
182         /* page color = pfn of page */
183         ioptex = bit_map_string_get(&iommu->usemap, npages, page_to_pfn(page));
184         if (ioptex < 0)
185                 panic("iommu out");
186         busa0 = iommu->start + (ioptex << PAGE_SHIFT);
187         iopte0 = &iommu->page_table[ioptex];
188
189         busa = busa0;
190         iopte = iopte0;
191         for (i = 0; i < npages; i++) {
192                 iopte_val(*iopte) = MKIOPTE(page_to_pfn(page), IOPERM);
193                 iommu_invalidate_page(iommu->regs, busa);
194                 busa += PAGE_SIZE;
195                 iopte++;
196                 page++;
197         }
198
199         iommu_flush_iotlb(iopte0, npages);
200
201         return busa0;
202 }
203
204 static u32 iommu_get_scsi_one(struct device *dev, char *vaddr, unsigned int len)
205 {
206         unsigned long off;
207         int npages;
208         struct page *page;
209         u32 busa;
210
211         off = (unsigned long)vaddr & ~PAGE_MASK;
212         npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
213         page = virt_to_page((unsigned long)vaddr & PAGE_MASK);
214         busa = iommu_get_one(dev, page, npages);
215         return busa + off;
216 }
217
218 static __u32 iommu_get_scsi_one_noflush(struct device *dev, char *vaddr, unsigned long len)
219 {
220         return iommu_get_scsi_one(dev, vaddr, len);
221 }
222
223 static __u32 iommu_get_scsi_one_gflush(struct device *dev, char *vaddr, unsigned long len)
224 {
225         flush_page_for_dma(0);
226         return iommu_get_scsi_one(dev, vaddr, len);
227 }
228
229 static __u32 iommu_get_scsi_one_pflush(struct device *dev, char *vaddr, unsigned long len)
230 {
231         unsigned long page = ((unsigned long) vaddr) & PAGE_MASK;
232
233         while(page < ((unsigned long)(vaddr + len))) {
234                 flush_page_for_dma(page);
235                 page += PAGE_SIZE;
236         }
237         return iommu_get_scsi_one(dev, vaddr, len);
238 }
239
240 static void iommu_get_scsi_sgl_noflush(struct device *dev, struct scatterlist *sg, int sz)
241 {
242         int n;
243
244         while (sz != 0) {
245                 --sz;
246                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
247                 sg->dvma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset;
248                 sg->dvma_length = (__u32) sg->length;
249                 sg = sg_next(sg);
250         }
251 }
252
253 static void iommu_get_scsi_sgl_gflush(struct device *dev, struct scatterlist *sg, int sz)
254 {
255         int n;
256
257         flush_page_for_dma(0);
258         while (sz != 0) {
259                 --sz;
260                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
261                 sg->dvma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset;
262                 sg->dvma_length = (__u32) sg->length;
263                 sg = sg_next(sg);
264         }
265 }
266
267 static void iommu_get_scsi_sgl_pflush(struct device *dev, struct scatterlist *sg, int sz)
268 {
269         unsigned long page, oldpage = 0;
270         int n, i;
271
272         while(sz != 0) {
273                 --sz;
274
275                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
276
277                 /*
278                  * We expect unmapped highmem pages to be not in the cache.
279                  * XXX Is this a good assumption?
280                  * XXX What if someone else unmaps it here and races us?
281                  */
282                 if ((page = (unsigned long) page_address(sg_page(sg))) != 0) {
283                         for (i = 0; i < n; i++) {
284                                 if (page != oldpage) {  /* Already flushed? */
285                                         flush_page_for_dma(page);
286                                         oldpage = page;
287                                 }
288                                 page += PAGE_SIZE;
289                         }
290                 }
291
292                 sg->dvma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset;
293                 sg->dvma_length = (__u32) sg->length;
294                 sg = sg_next(sg);
295         }
296 }
297
298 static void iommu_release_one(struct device *dev, u32 busa, int npages)
299 {
300         struct iommu_struct *iommu = dev->archdata.iommu;
301         int ioptex;
302         int i;
303
304         BUG_ON(busa < iommu->start);
305         ioptex = (busa - iommu->start) >> PAGE_SHIFT;
306         for (i = 0; i < npages; i++) {
307                 iopte_val(iommu->page_table[ioptex + i]) = 0;
308                 iommu_invalidate_page(iommu->regs, busa);
309                 busa += PAGE_SIZE;
310         }
311         bit_map_clear(&iommu->usemap, ioptex, npages);
312 }
313
314 static void iommu_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len)
315 {
316         unsigned long off;
317         int npages;
318
319         off = vaddr & ~PAGE_MASK;
320         npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
321         iommu_release_one(dev, vaddr & PAGE_MASK, npages);
322 }
323
324 static void iommu_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz)
325 {
326         int n;
327
328         while(sz != 0) {
329                 --sz;
330
331                 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
332                 iommu_release_one(dev, sg->dvma_address & PAGE_MASK, n);
333                 sg->dvma_address = 0x21212121;
334                 sg = sg_next(sg);
335         }
336 }
337
338 #ifdef CONFIG_SBUS
339 static int iommu_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va,
340                               unsigned long addr, int len)
341 {
342         struct iommu_struct *iommu = dev->archdata.iommu;
343         unsigned long page, end;
344         iopte_t *iopte = iommu->page_table;
345         iopte_t *first;
346         int ioptex;
347
348         BUG_ON((va & ~PAGE_MASK) != 0);
349         BUG_ON((addr & ~PAGE_MASK) != 0);
350         BUG_ON((len & ~PAGE_MASK) != 0);
351
352         /* page color = physical address */
353         ioptex = bit_map_string_get(&iommu->usemap, len >> PAGE_SHIFT,
354                 addr >> PAGE_SHIFT);
355         if (ioptex < 0)
356                 panic("iommu out");
357
358         iopte += ioptex;
359         first = iopte;
360         end = addr + len;
361         while(addr < end) {
362                 page = va;
363                 {
364                         pgd_t *pgdp;
365                         pmd_t *pmdp;
366                         pte_t *ptep;
367
368                         if (viking_mxcc_present)
369                                 viking_mxcc_flush_page(page);
370                         else if (viking_flush)
371                                 viking_flush_page(page);
372                         else
373                                 __flush_page_to_ram(page);
374
375                         pgdp = pgd_offset(&init_mm, addr);
376                         pmdp = pmd_offset(pgdp, addr);
377                         ptep = pte_offset_map(pmdp, addr);
378
379                         set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
380                 }
381                 iopte_val(*iopte++) =
382                     MKIOPTE(page_to_pfn(virt_to_page(page)), ioperm_noc);
383                 addr += PAGE_SIZE;
384                 va += PAGE_SIZE;
385         }
386         /* P3: why do we need this?
387          *
388          * DAVEM: Because there are several aspects, none of which
389          *        are handled by a single interface.  Some cpus are
390          *        completely not I/O DMA coherent, and some have
391          *        virtually indexed caches.  The driver DMA flushing
392          *        methods handle the former case, but here during
393          *        IOMMU page table modifications, and usage of non-cacheable
394          *        cpu mappings of pages potentially in the cpu caches, we have
395          *        to handle the latter case as well.
396          */
397         flush_cache_all();
398         iommu_flush_iotlb(first, len >> PAGE_SHIFT);
399         flush_tlb_all();
400         iommu_invalidate(iommu->regs);
401
402         *pba = iommu->start + (ioptex << PAGE_SHIFT);
403         return 0;
404 }
405
406 static void iommu_unmap_dma_area(struct device *dev, unsigned long busa, int len)
407 {
408         struct iommu_struct *iommu = dev->archdata.iommu;
409         iopte_t *iopte = iommu->page_table;
410         unsigned long end;
411         int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
412
413         BUG_ON((busa & ~PAGE_MASK) != 0);
414         BUG_ON((len & ~PAGE_MASK) != 0);
415
416         iopte += ioptex;
417         end = busa + len;
418         while (busa < end) {
419                 iopte_val(*iopte++) = 0;
420                 busa += PAGE_SIZE;
421         }
422         flush_tlb_all();
423         iommu_invalidate(iommu->regs);
424         bit_map_clear(&iommu->usemap, ioptex, len >> PAGE_SHIFT);
425 }
426 #endif
427
428 static char *iommu_lockarea(char *vaddr, unsigned long len)
429 {
430         return vaddr;
431 }
432
433 static void iommu_unlockarea(char *vaddr, unsigned long len)
434 {
435 }
436
437 void __init ld_mmu_iommu(void)
438 {
439         viking_flush = (BTFIXUPVAL_CALL(flush_page_for_dma) == (unsigned long)viking_flush_page);
440         BTFIXUPSET_CALL(mmu_lockarea, iommu_lockarea, BTFIXUPCALL_RETO0);
441         BTFIXUPSET_CALL(mmu_unlockarea, iommu_unlockarea, BTFIXUPCALL_NOP);
442
443         if (!BTFIXUPVAL_CALL(flush_page_for_dma)) {
444                 /* IO coherent chip */
445                 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_noflush, BTFIXUPCALL_RETO0);
446                 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_noflush, BTFIXUPCALL_NORM);
447         } else if (flush_page_for_dma_global) {
448                 /* flush_page_for_dma flushes everything, no matter of what page is it */
449                 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_gflush, BTFIXUPCALL_NORM);
450                 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_gflush, BTFIXUPCALL_NORM);
451         } else {
452                 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_pflush, BTFIXUPCALL_NORM);
453                 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_pflush, BTFIXUPCALL_NORM);
454         }
455         BTFIXUPSET_CALL(mmu_release_scsi_one, iommu_release_scsi_one, BTFIXUPCALL_NORM);
456         BTFIXUPSET_CALL(mmu_release_scsi_sgl, iommu_release_scsi_sgl, BTFIXUPCALL_NORM);
457
458 #ifdef CONFIG_SBUS
459         BTFIXUPSET_CALL(mmu_map_dma_area, iommu_map_dma_area, BTFIXUPCALL_NORM);
460         BTFIXUPSET_CALL(mmu_unmap_dma_area, iommu_unmap_dma_area, BTFIXUPCALL_NORM);
461 #endif
462
463         if (viking_mxcc_present || srmmu_modtype == HyperSparc) {
464                 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
465                 ioperm_noc = IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID;
466         } else {
467                 dvma_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV);
468                 ioperm_noc = IOPTE_WRITE | IOPTE_VALID;
469         }
470 }