blob: a86c9f552fa1420140c6a475ee74b3d84538a880 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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 */
Jens Axboe0912a5d2007-05-14 15:44:38 +020015#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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 */
40extern int viking_mxcc_present;
41BTFIXUPDEF_CALL(void, flush_page_for_dma, unsigned long)
42#define flush_page_for_dma(page) BTFIXUP_CALL(flush_page_for_dma)(page)
43extern int flush_page_for_dma_global;
44static int viking_flush;
45/* viking.S */
46extern void viking_flush_page(unsigned long page);
47extern void viking_mxcc_flush_page(unsigned long page);
48
49/*
50 * Values precomputed according to CPU type.
51 */
52static unsigned int ioperm_noc; /* Consistent mapping iopte flags */
53static 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
David S. Millere0039342008-08-25 22:47:20 -070058void __init iommu_init(struct device_node *parent, struct sbus_bus *sbus)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
David S. Millere0039342008-08-25 22:47:20 -070060 struct of_device *parent_op, *op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct iommu_struct *iommu;
David S. Millere0039342008-08-25 22:47:20 -070062 unsigned int impl, vers;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 unsigned long *bitmap;
David S. Millere0039342008-08-25 22:47:20 -070064 unsigned long tmp;
65
66 parent_op = of_find_device_by_node(parent);
67 if (!parent_op) {
68 prom_printf("Unable to find IOMMU of_device\n");
69 prom_halt();
70 }
71
72 op = of_find_device_by_node(sbus->ofdev.node);
73 if (!op) {
74 prom_printf("Unable to find SBUS of_device\n");
75 prom_halt();
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 iommu = kmalloc(sizeof(struct iommu_struct), GFP_ATOMIC);
79 if (!iommu) {
80 prom_printf("Unable to allocate iommu structure\n");
81 prom_halt();
82 }
David S. Millere0039342008-08-25 22:47:20 -070083
84 iommu->regs = of_ioremap(&parent_op->resource[0], 0, PAGE_SIZE * 3,
85 "iommu_regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (!iommu->regs) {
87 prom_printf("Cannot map IOMMU registers\n");
88 prom_halt();
89 }
90 impl = (iommu->regs->control & IOMMU_CTRL_IMPL) >> 28;
91 vers = (iommu->regs->control & IOMMU_CTRL_VERS) >> 24;
92 tmp = iommu->regs->control;
93 tmp &= ~(IOMMU_CTRL_RNGE);
94 tmp |= (IOMMU_RNGE_256MB | IOMMU_CTRL_ENAB);
95 iommu->regs->control = tmp;
96 iommu_invalidate(iommu->regs);
97 iommu->start = IOMMU_START;
98 iommu->end = 0xffffffff;
99
100 /* Allocate IOMMU page table */
101 /* Stupid alignment constraints give me a headache.
102 We need 256K or 512K or 1M or 2M area aligned to
103 its size and current gfp will fortunately give
104 it to us. */
105 tmp = __get_free_pages(GFP_KERNEL, IOMMU_ORDER);
106 if (!tmp) {
107 prom_printf("Unable to allocate iommu table [0x%08x]\n",
108 IOMMU_NPTES*sizeof(iopte_t));
109 prom_halt();
110 }
111 iommu->page_table = (iopte_t *)tmp;
112
113 /* Initialize new table. */
114 memset(iommu->page_table, 0, IOMMU_NPTES*sizeof(iopte_t));
115 flush_cache_all();
116 flush_tlb_all();
117 iommu->regs->base = __pa((unsigned long) iommu->page_table) >> 4;
118 iommu_invalidate(iommu->regs);
119
120 bitmap = kmalloc(IOMMU_NPTES>>3, GFP_KERNEL);
121 if (!bitmap) {
122 prom_printf("Unable to allocate iommu bitmap [%d]\n",
123 (int)(IOMMU_NPTES>>3));
124 prom_halt();
125 }
126 bit_map_init(&iommu->usemap, bitmap, IOMMU_NPTES);
127 /* To be coherent on HyperSparc, the page color of DVMA
128 * and physical addresses must match.
129 */
130 if (srmmu_modtype == HyperSparc)
131 iommu->usemap.num_colors = vac_cache_size >> PAGE_SHIFT;
132 else
133 iommu->usemap.num_colors = 1;
134
135 printk("IOMMU: impl %d vers %d table 0x%p[%d B] map [%d b]\n",
136 impl, vers, iommu->page_table,
137 (int)(IOMMU_NPTES*sizeof(iopte_t)), (int)IOMMU_NPTES);
138
Robert Reif3ac4c942007-08-10 15:52:06 -0700139 sbus->ofdev.dev.archdata.iommu = iommu;
David S. Millere0039342008-08-25 22:47:20 -0700140 op->dev.archdata.iommu = iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/* This begs to be btfixup-ed by srmmu. */
144/* Flush the iotlb entries to ram. */
145/* This could be better if we didn't have to flush whole pages. */
146static void iommu_flush_iotlb(iopte_t *iopte, unsigned int niopte)
147{
148 unsigned long start;
149 unsigned long end;
150
Bob Breuer3185d4d2006-06-20 00:36:56 -0700151 start = (unsigned long)iopte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 end = PAGE_ALIGN(start + niopte*sizeof(iopte_t));
Bob Breuer3185d4d2006-06-20 00:36:56 -0700153 start &= PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (viking_mxcc_present) {
155 while(start < end) {
156 viking_mxcc_flush_page(start);
157 start += PAGE_SIZE;
158 }
159 } else if (viking_flush) {
160 while(start < end) {
161 viking_flush_page(start);
162 start += PAGE_SIZE;
163 }
164 } else {
165 while(start < end) {
166 __flush_page_to_ram(start);
167 start += PAGE_SIZE;
168 }
169 }
170}
171
172static u32 iommu_get_one(struct page *page, int npages, struct sbus_bus *sbus)
173{
Robert Reif3ac4c942007-08-10 15:52:06 -0700174 struct iommu_struct *iommu = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int ioptex;
176 iopte_t *iopte, *iopte0;
177 unsigned int busa, busa0;
178 int i;
179
180 /* page color = pfn of page */
181 ioptex = bit_map_string_get(&iommu->usemap, npages, page_to_pfn(page));
182 if (ioptex < 0)
183 panic("iommu out");
184 busa0 = iommu->start + (ioptex << PAGE_SHIFT);
185 iopte0 = &iommu->page_table[ioptex];
186
187 busa = busa0;
188 iopte = iopte0;
189 for (i = 0; i < npages; i++) {
190 iopte_val(*iopte) = MKIOPTE(page_to_pfn(page), IOPERM);
191 iommu_invalidate_page(iommu->regs, busa);
192 busa += PAGE_SIZE;
193 iopte++;
194 page++;
195 }
196
197 iommu_flush_iotlb(iopte0, npages);
198
199 return busa0;
200}
201
202static u32 iommu_get_scsi_one(char *vaddr, unsigned int len,
203 struct sbus_bus *sbus)
204{
205 unsigned long off;
206 int npages;
207 struct page *page;
208 u32 busa;
209
210 off = (unsigned long)vaddr & ~PAGE_MASK;
211 npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
212 page = virt_to_page((unsigned long)vaddr & PAGE_MASK);
213 busa = iommu_get_one(page, npages, sbus);
214 return busa + off;
215}
216
217static __u32 iommu_get_scsi_one_noflush(char *vaddr, unsigned long len, struct sbus_bus *sbus)
218{
219 return iommu_get_scsi_one(vaddr, len, sbus);
220}
221
222static __u32 iommu_get_scsi_one_gflush(char *vaddr, unsigned long len, struct sbus_bus *sbus)
223{
224 flush_page_for_dma(0);
225 return iommu_get_scsi_one(vaddr, len, sbus);
226}
227
228static __u32 iommu_get_scsi_one_pflush(char *vaddr, unsigned long len, struct sbus_bus *sbus)
229{
230 unsigned long page = ((unsigned long) vaddr) & PAGE_MASK;
231
232 while(page < ((unsigned long)(vaddr + len))) {
233 flush_page_for_dma(page);
234 page += PAGE_SIZE;
235 }
236 return iommu_get_scsi_one(vaddr, len, sbus);
237}
238
239static void iommu_get_scsi_sgl_noflush(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
240{
241 int n;
242
243 while (sz != 0) {
244 --sz;
245 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
Jens Axboe58b053e2007-10-22 20:02:46 +0200246 sg->dvma_address = iommu_get_one(sg_page(sg), n, sbus) + sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 sg->dvma_length = (__u32) sg->length;
Jens Axboe0912a5d2007-05-14 15:44:38 +0200248 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250}
251
252static void iommu_get_scsi_sgl_gflush(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
253{
254 int n;
255
256 flush_page_for_dma(0);
257 while (sz != 0) {
258 --sz;
259 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
Jens Axboe58b053e2007-10-22 20:02:46 +0200260 sg->dvma_address = iommu_get_one(sg_page(sg), n, sbus) + sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 sg->dvma_length = (__u32) sg->length;
Jens Axboe0912a5d2007-05-14 15:44:38 +0200262 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264}
265
266static void iommu_get_scsi_sgl_pflush(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
267{
268 unsigned long page, oldpage = 0;
269 int n, i;
270
271 while(sz != 0) {
272 --sz;
273
274 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
275
276 /*
277 * We expect unmapped highmem pages to be not in the cache.
278 * XXX Is this a good assumption?
279 * XXX What if someone else unmaps it here and races us?
280 */
Jens Axboe58b053e2007-10-22 20:02:46 +0200281 if ((page = (unsigned long) page_address(sg_page(sg))) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 for (i = 0; i < n; i++) {
283 if (page != oldpage) { /* Already flushed? */
284 flush_page_for_dma(page);
285 oldpage = page;
286 }
287 page += PAGE_SIZE;
288 }
289 }
290
Jens Axboe58b053e2007-10-22 20:02:46 +0200291 sg->dvma_address = iommu_get_one(sg_page(sg), n, sbus) + sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 sg->dvma_length = (__u32) sg->length;
Jens Axboe0912a5d2007-05-14 15:44:38 +0200293 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295}
296
297static void iommu_release_one(u32 busa, int npages, struct sbus_bus *sbus)
298{
Robert Reif3ac4c942007-08-10 15:52:06 -0700299 struct iommu_struct *iommu = sbus->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 int ioptex;
301 int i;
302
Eric Sesterhenn1ae61382006-01-17 15:36:05 -0800303 BUG_ON(busa < iommu->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ioptex = (busa - iommu->start) >> PAGE_SHIFT;
305 for (i = 0; i < npages; i++) {
306 iopte_val(iommu->page_table[ioptex + i]) = 0;
307 iommu_invalidate_page(iommu->regs, busa);
308 busa += PAGE_SIZE;
309 }
310 bit_map_clear(&iommu->usemap, ioptex, npages);
311}
312
313static void iommu_release_scsi_one(__u32 vaddr, unsigned long len, struct sbus_bus *sbus)
314{
315 unsigned long off;
316 int npages;
317
318 off = vaddr & ~PAGE_MASK;
319 npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
320 iommu_release_one(vaddr & PAGE_MASK, npages, sbus);
321}
322
323static void iommu_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus)
324{
325 int n;
326
327 while(sz != 0) {
328 --sz;
329
330 n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
331 iommu_release_one(sg->dvma_address & PAGE_MASK, n, sbus);
332 sg->dvma_address = 0x21212121;
Jens Axboe0912a5d2007-05-14 15:44:38 +0200333 sg = sg_next(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335}
336
337#ifdef CONFIG_SBUS
338static int iommu_map_dma_area(dma_addr_t *pba, unsigned long va,
339 unsigned long addr, int len)
340{
341 unsigned long page, end;
Robert Reif3ac4c942007-08-10 15:52:06 -0700342 struct iommu_struct *iommu = sbus_root->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 iopte_t *iopte = iommu->page_table;
344 iopte_t *first;
345 int ioptex;
346
Eric Sesterhenn1ae61382006-01-17 15:36:05 -0800347 BUG_ON((va & ~PAGE_MASK) != 0);
348 BUG_ON((addr & ~PAGE_MASK) != 0);
349 BUG_ON((len & ~PAGE_MASK) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* page color = physical address */
352 ioptex = bit_map_string_get(&iommu->usemap, len >> PAGE_SHIFT,
353 addr >> PAGE_SHIFT);
354 if (ioptex < 0)
355 panic("iommu out");
356
357 iopte += ioptex;
358 first = iopte;
359 end = addr + len;
360 while(addr < end) {
361 page = va;
362 {
363 pgd_t *pgdp;
364 pmd_t *pmdp;
365 pte_t *ptep;
366
367 if (viking_mxcc_present)
368 viking_mxcc_flush_page(page);
369 else if (viking_flush)
370 viking_flush_page(page);
371 else
372 __flush_page_to_ram(page);
373
374 pgdp = pgd_offset(&init_mm, addr);
375 pmdp = pmd_offset(pgdp, addr);
376 ptep = pte_offset_map(pmdp, addr);
377
378 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
379 }
380 iopte_val(*iopte++) =
381 MKIOPTE(page_to_pfn(virt_to_page(page)), ioperm_noc);
382 addr += PAGE_SIZE;
383 va += PAGE_SIZE;
384 }
385 /* P3: why do we need this?
386 *
387 * DAVEM: Because there are several aspects, none of which
388 * are handled by a single interface. Some cpus are
389 * completely not I/O DMA coherent, and some have
390 * virtually indexed caches. The driver DMA flushing
391 * methods handle the former case, but here during
392 * IOMMU page table modifications, and usage of non-cacheable
393 * cpu mappings of pages potentially in the cpu caches, we have
394 * to handle the latter case as well.
395 */
396 flush_cache_all();
397 iommu_flush_iotlb(first, len >> PAGE_SHIFT);
398 flush_tlb_all();
399 iommu_invalidate(iommu->regs);
400
401 *pba = iommu->start + (ioptex << PAGE_SHIFT);
402 return 0;
403}
404
405static void iommu_unmap_dma_area(unsigned long busa, int len)
406{
Robert Reif3ac4c942007-08-10 15:52:06 -0700407 struct iommu_struct *iommu = sbus_root->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 iopte_t *iopte = iommu->page_table;
409 unsigned long end;
410 int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
411
Eric Sesterhenn1ae61382006-01-17 15:36:05 -0800412 BUG_ON((busa & ~PAGE_MASK) != 0);
413 BUG_ON((len & ~PAGE_MASK) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 iopte += ioptex;
416 end = busa + len;
417 while (busa < end) {
418 iopte_val(*iopte++) = 0;
419 busa += PAGE_SIZE;
420 }
421 flush_tlb_all();
422 iommu_invalidate(iommu->regs);
423 bit_map_clear(&iommu->usemap, ioptex, len >> PAGE_SHIFT);
424}
425
426static struct page *iommu_translate_dvma(unsigned long busa)
427{
Robert Reif3ac4c942007-08-10 15:52:06 -0700428 struct iommu_struct *iommu = sbus_root->ofdev.dev.archdata.iommu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 iopte_t *iopte = iommu->page_table;
430
431 iopte += ((busa - iommu->start) >> PAGE_SHIFT);
432 return pfn_to_page((iopte_val(*iopte) & IOPTE_PAGE) >> (PAGE_SHIFT-4));
433}
434#endif
435
436static char *iommu_lockarea(char *vaddr, unsigned long len)
437{
438 return vaddr;
439}
440
441static void iommu_unlockarea(char *vaddr, unsigned long len)
442{
443}
444
445void __init ld_mmu_iommu(void)
446{
447 viking_flush = (BTFIXUPVAL_CALL(flush_page_for_dma) == (unsigned long)viking_flush_page);
448 BTFIXUPSET_CALL(mmu_lockarea, iommu_lockarea, BTFIXUPCALL_RETO0);
449 BTFIXUPSET_CALL(mmu_unlockarea, iommu_unlockarea, BTFIXUPCALL_NOP);
450
451 if (!BTFIXUPVAL_CALL(flush_page_for_dma)) {
452 /* IO coherent chip */
453 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_noflush, BTFIXUPCALL_RETO0);
454 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_noflush, BTFIXUPCALL_NORM);
455 } else if (flush_page_for_dma_global) {
456 /* flush_page_for_dma flushes everything, no matter of what page is it */
457 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_gflush, BTFIXUPCALL_NORM);
458 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_gflush, BTFIXUPCALL_NORM);
459 } else {
460 BTFIXUPSET_CALL(mmu_get_scsi_one, iommu_get_scsi_one_pflush, BTFIXUPCALL_NORM);
461 BTFIXUPSET_CALL(mmu_get_scsi_sgl, iommu_get_scsi_sgl_pflush, BTFIXUPCALL_NORM);
462 }
463 BTFIXUPSET_CALL(mmu_release_scsi_one, iommu_release_scsi_one, BTFIXUPCALL_NORM);
464 BTFIXUPSET_CALL(mmu_release_scsi_sgl, iommu_release_scsi_sgl, BTFIXUPCALL_NORM);
465
466#ifdef CONFIG_SBUS
467 BTFIXUPSET_CALL(mmu_map_dma_area, iommu_map_dma_area, BTFIXUPCALL_NORM);
468 BTFIXUPSET_CALL(mmu_unmap_dma_area, iommu_unmap_dma_area, BTFIXUPCALL_NORM);
469 BTFIXUPSET_CALL(mmu_translate_dvma, iommu_translate_dvma, BTFIXUPCALL_NORM);
470#endif
471
472 if (viking_mxcc_present || srmmu_modtype == HyperSparc) {
473 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
474 ioperm_noc = IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID;
475 } else {
476 dvma_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV);
477 ioperm_noc = IOPTE_WRITE | IOPTE_VALID;
478 }
479}