Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | 046e26a | 2008-08-27 04:54:04 -0700 | [diff] [blame^] | 58 | static void __init sbus_iommu_init(struct of_device *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct iommu_struct *iommu; |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 61 | unsigned int impl, vers; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | unsigned long *bitmap; |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 63 | unsigned long tmp; |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 70 | |
David S. Miller | 046e26a | 2008-08-27 04:54:04 -0700 | [diff] [blame^] | 71 | iommu->regs = of_ioremap(&op->resource[0], 0, PAGE_SIZE * 3, |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 72 | "iommu_regs"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | 046e26a | 2008-08-27 04:54:04 -0700 | [diff] [blame^] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 126 | op->dev.archdata.iommu = iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
David S. Miller | 046e26a | 2008-08-27 04:54:04 -0700 | [diff] [blame^] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Bob Breuer | 3185d4d | 2006-06-20 00:36:56 -0700 | [diff] [blame] | 153 | start = (unsigned long)iopte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | end = PAGE_ALIGN(start + niopte*sizeof(iopte_t)); |
Bob Breuer | 3185d4d | 2006-06-20 00:36:56 -0700 | [diff] [blame] | 155 | start &= PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 174 | static u32 iommu_get_one(struct device *dev, struct page *page, int npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 176 | struct iommu_struct *iommu = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 204 | static u32 iommu_get_scsi_one(struct device *dev, char *vaddr, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 214 | busa = iommu_get_one(dev, page, npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | return busa + off; |
| 216 | } |
| 217 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 218 | static __u32 iommu_get_scsi_one_noflush(struct device *dev, char *vaddr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 220 | return iommu_get_scsi_one(dev, vaddr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 223 | static __u32 iommu_get_scsi_one_gflush(struct device *dev, char *vaddr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | flush_page_for_dma(0); |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 226 | return iommu_get_scsi_one(dev, vaddr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 229 | static __u32 iommu_get_scsi_one_pflush(struct device *dev, char *vaddr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 237 | return iommu_get_scsi_one(dev, vaddr, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 240 | static void iommu_get_scsi_sgl_noflush(struct device *dev, struct scatterlist *sg, int sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | int n; |
| 243 | |
| 244 | while (sz != 0) { |
| 245 | --sz; |
| 246 | n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT; |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 247 | sg->dvma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | sg->dvma_length = (__u32) sg->length; |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 249 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 253 | static void iommu_get_scsi_sgl_gflush(struct device *dev, struct scatterlist *sg, int sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 261 | sg->dvma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | sg->dvma_length = (__u32) sg->length; |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 263 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 267 | static void iommu_get_scsi_sgl_pflush(struct device *dev, struct scatterlist *sg, int sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 282 | if ((page = (unsigned long) page_address(sg_page(sg))) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 292 | sg->dvma_address = iommu_get_one(dev, sg_page(sg), n) + sg->offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | sg->dvma_length = (__u32) sg->length; |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 294 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 298 | static void iommu_release_one(struct device *dev, u32 busa, int npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 300 | struct iommu_struct *iommu = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | int ioptex; |
| 302 | int i; |
| 303 | |
Eric Sesterhenn | 1ae6138 | 2006-01-17 15:36:05 -0800 | [diff] [blame] | 304 | BUG_ON(busa < iommu->start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 314 | static void iommu_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { |
| 316 | unsigned long off; |
| 317 | int npages; |
| 318 | |
| 319 | off = vaddr & ~PAGE_MASK; |
| 320 | npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT; |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 321 | iommu_release_one(dev, vaddr & PAGE_MASK, npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 324 | static void iommu_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | int n; |
| 327 | |
| 328 | while(sz != 0) { |
| 329 | --sz; |
| 330 | |
| 331 | n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT; |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 332 | iommu_release_one(dev, sg->dvma_address & PAGE_MASK, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | sg->dvma_address = 0x21212121; |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 334 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | #ifdef CONFIG_SBUS |
David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 339 | static int iommu_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va, |
| 340 | unsigned long addr, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 342 | struct iommu_struct *iommu = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | unsigned long page, end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | iopte_t *iopte = iommu->page_table; |
| 345 | iopte_t *first; |
| 346 | int ioptex; |
| 347 | |
Eric Sesterhenn | 1ae6138 | 2006-01-17 15:36:05 -0800 | [diff] [blame] | 348 | BUG_ON((va & ~PAGE_MASK) != 0); |
| 349 | BUG_ON((addr & ~PAGE_MASK) != 0); |
| 350 | BUG_ON((len & ~PAGE_MASK) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 406 | static void iommu_unmap_dma_area(struct device *dev, unsigned long busa, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 408 | struct iommu_struct *iommu = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | iopte_t *iopte = iommu->page_table; |
| 410 | unsigned long end; |
| 411 | int ioptex = (busa - iommu->start) >> PAGE_SHIFT; |
| 412 | |
Eric Sesterhenn | 1ae6138 | 2006-01-17 15:36:05 -0800 | [diff] [blame] | 413 | BUG_ON((busa & ~PAGE_MASK) != 0); |
| 414 | BUG_ON((len & ~PAGE_MASK) != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |