Adrian Bunk | 88278ca | 2008-05-19 16:53:02 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * io-unit.c: IO-UNIT specific routines for memory management. |
| 3 | * |
| 4 | * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/kernel.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/highmem.h> /* pte_offset_map => kmap_atomic */ |
| 13 | #include <linux/bitops.h> |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 14 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/pgalloc.h> |
| 17 | #include <asm/pgtable.h> |
| 18 | #include <asm/sbus.h> |
| 19 | #include <asm/io.h> |
| 20 | #include <asm/io-unit.h> |
| 21 | #include <asm/mxcc.h> |
| 22 | #include <asm/cacheflush.h> |
| 23 | #include <asm/tlbflush.h> |
| 24 | #include <asm/dma.h> |
David S. Miller | d4accd6 | 2006-11-30 17:11:26 -0800 | [diff] [blame] | 25 | #include <asm/oplib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* #define IOUNIT_DEBUG */ |
| 28 | #ifdef IOUNIT_DEBUG |
| 29 | #define IOD(x) printk(x) |
| 30 | #else |
| 31 | #define IOD(x) do { } while (0) |
| 32 | #endif |
| 33 | |
| 34 | #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID) |
| 35 | #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM) |
| 36 | |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame^] | 37 | void __init iounit_init(struct sbus_bus *sbus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame^] | 39 | struct device_node *dp = sbus->ofdev.node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct iounit_struct *iounit; |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame^] | 41 | iopte_t *xpt, *xptend; |
| 42 | struct of_device *op; |
| 43 | |
| 44 | op = of_find_device_by_node(dp); |
| 45 | if (!op) { |
| 46 | prom_printf("SUN4D: Cannot find SBI of_device.\n"); |
| 47 | prom_halt(); |
| 48 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Yan Burman | c80892d | 2006-11-30 17:07:04 -0800 | [diff] [blame] | 50 | iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC); |
David S. Miller | d4accd6 | 2006-11-30 17:11:26 -0800 | [diff] [blame] | 51 | if (!iounit) { |
| 52 | prom_printf("SUN4D: Cannot alloc iounit, halting.\n"); |
| 53 | prom_halt(); |
| 54 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | iounit->limit[0] = IOUNIT_BMAP1_START; |
| 57 | iounit->limit[1] = IOUNIT_BMAP2_START; |
| 58 | iounit->limit[2] = IOUNIT_BMAPM_START; |
| 59 | iounit->limit[3] = IOUNIT_BMAPM_END; |
| 60 | iounit->rotor[1] = IOUNIT_BMAP2_START; |
| 61 | iounit->rotor[2] = IOUNIT_BMAPM_START; |
| 62 | |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame^] | 63 | xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT"); |
| 64 | if (!xpt) { |
| 65 | prom_printf("SUN4D: Cannot map External Page Table."); |
| 66 | prom_halt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 69 | sbus->ofdev.dev.archdata.iommu = iounit; |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame^] | 70 | op->dev.archdata.iommu = iounit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | iounit->page_table = xpt; |
Raymond Burns | 2f72ba4 | 2006-07-17 21:40:27 -0700 | [diff] [blame] | 72 | spin_lock_init(&iounit->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | for (xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t); |
| 75 | xpt < xptend;) |
| 76 | iopte_val(*xpt++) = 0; |
| 77 | } |
| 78 | |
| 79 | /* One has to hold iounit->lock to call this */ |
| 80 | static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size) |
| 81 | { |
| 82 | int i, j, k, npages; |
| 83 | unsigned long rotor, scan, limit; |
| 84 | iopte_t iopte; |
| 85 | |
| 86 | npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT; |
| 87 | |
| 88 | /* A tiny bit of magic ingredience :) */ |
| 89 | switch (npages) { |
| 90 | case 1: i = 0x0231; break; |
| 91 | case 2: i = 0x0132; break; |
| 92 | default: i = 0x0213; break; |
| 93 | } |
| 94 | |
| 95 | IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages)); |
| 96 | |
| 97 | next: j = (i & 15); |
| 98 | rotor = iounit->rotor[j - 1]; |
| 99 | limit = iounit->limit[j]; |
| 100 | scan = rotor; |
| 101 | nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan); |
| 102 | if (scan + npages > limit) { |
| 103 | if (limit != rotor) { |
| 104 | limit = rotor; |
| 105 | scan = iounit->limit[j - 1]; |
| 106 | goto nexti; |
| 107 | } |
| 108 | i >>= 4; |
| 109 | if (!(i & 15)) |
| 110 | panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size); |
| 111 | goto next; |
| 112 | } |
| 113 | for (k = 1, scan++; k < npages; k++) |
| 114 | if (test_bit(scan++, iounit->bmap)) |
| 115 | goto nexti; |
| 116 | iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1]; |
| 117 | scan -= npages; |
| 118 | iopte = MKIOPTE(__pa(vaddr & PAGE_MASK)); |
| 119 | vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK); |
| 120 | for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) { |
| 121 | set_bit(scan, iounit->bmap); |
| 122 | iounit->page_table[scan] = iopte; |
| 123 | } |
| 124 | IOD(("%08lx\n", vaddr)); |
| 125 | return vaddr; |
| 126 | } |
| 127 | |
| 128 | static __u32 iounit_get_scsi_one(char *vaddr, unsigned long len, struct sbus_bus *sbus) |
| 129 | { |
| 130 | unsigned long ret, flags; |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 131 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
| 133 | spin_lock_irqsave(&iounit->lock, flags); |
| 134 | ret = iounit_get_area(iounit, (unsigned long)vaddr, len); |
| 135 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | static void iounit_get_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus) |
| 140 | { |
| 141 | unsigned long flags; |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 142 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | /* FIXME: Cache some resolved pages - often several sg entries are to the same page */ |
| 145 | spin_lock_irqsave(&iounit->lock, flags); |
| 146 | while (sz != 0) { |
| 147 | --sz; |
David S. Miller | 6ee4e28 | 2007-10-27 00:54:14 -0700 | [diff] [blame] | 148 | sg->dvma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length); |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 149 | sg->dvma_length = sg->length; |
| 150 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 153 | } |
| 154 | |
| 155 | static void iounit_release_scsi_one(__u32 vaddr, unsigned long len, struct sbus_bus *sbus) |
| 156 | { |
| 157 | unsigned long flags; |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 158 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | spin_lock_irqsave(&iounit->lock, flags); |
| 161 | len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT; |
| 162 | vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT; |
| 163 | IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); |
| 164 | for (len += vaddr; vaddr < len; vaddr++) |
| 165 | clear_bit(vaddr, iounit->bmap); |
| 166 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 167 | } |
| 168 | |
| 169 | static void iounit_release_scsi_sgl(struct scatterlist *sg, int sz, struct sbus_bus *sbus) |
| 170 | { |
| 171 | unsigned long flags; |
| 172 | unsigned long vaddr, len; |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 173 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | spin_lock_irqsave(&iounit->lock, flags); |
| 176 | while (sz != 0) { |
| 177 | --sz; |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 178 | len = ((sg->dvma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT; |
| 179 | vaddr = (sg->dvma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); |
| 181 | for (len += vaddr; vaddr < len; vaddr++) |
| 182 | clear_bit(vaddr, iounit->bmap); |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 183 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 186 | } |
| 187 | |
| 188 | #ifdef CONFIG_SBUS |
| 189 | static int iounit_map_dma_area(dma_addr_t *pba, unsigned long va, __u32 addr, int len) |
| 190 | { |
| 191 | unsigned long page, end; |
| 192 | pgprot_t dvma_prot; |
| 193 | iopte_t *iopte; |
| 194 | struct sbus_bus *sbus; |
| 195 | |
| 196 | *pba = addr; |
| 197 | |
| 198 | dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV); |
| 199 | end = PAGE_ALIGN((addr + len)); |
| 200 | while(addr < end) { |
| 201 | page = va; |
| 202 | { |
| 203 | pgd_t *pgdp; |
| 204 | pmd_t *pmdp; |
| 205 | pte_t *ptep; |
| 206 | long i; |
| 207 | |
| 208 | pgdp = pgd_offset(&init_mm, addr); |
| 209 | pmdp = pmd_offset(pgdp, addr); |
| 210 | ptep = pte_offset_map(pmdp, addr); |
| 211 | |
| 212 | set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot)); |
| 213 | |
| 214 | i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT); |
| 215 | |
| 216 | for_each_sbus(sbus) { |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 217 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | iopte = (iopte_t *)(iounit->page_table + i); |
| 220 | *iopte = MKIOPTE(__pa(page)); |
| 221 | } |
| 222 | } |
| 223 | addr += PAGE_SIZE; |
| 224 | va += PAGE_SIZE; |
| 225 | } |
| 226 | flush_cache_all(); |
| 227 | flush_tlb_all(); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static void iounit_unmap_dma_area(unsigned long addr, int len) |
| 233 | { |
| 234 | /* XXX Somebody please fill this in */ |
| 235 | } |
| 236 | |
| 237 | /* XXX We do not pass sbus device here, bad. */ |
| 238 | static struct page *iounit_translate_dvma(unsigned long addr) |
| 239 | { |
| 240 | struct sbus_bus *sbus = sbus_root; /* They are all the same */ |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 241 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | int i; |
| 243 | iopte_t *iopte; |
| 244 | |
| 245 | i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT); |
| 246 | iopte = (iopte_t *)(iounit->page_table + i); |
| 247 | return pfn_to_page(iopte_val(*iopte) >> (PAGE_SHIFT-4)); /* XXX sun4d guru, help */ |
| 248 | } |
| 249 | #endif |
| 250 | |
| 251 | static char *iounit_lockarea(char *vaddr, unsigned long len) |
| 252 | { |
| 253 | /* FIXME: Write this */ |
| 254 | return vaddr; |
| 255 | } |
| 256 | |
| 257 | static void iounit_unlockarea(char *vaddr, unsigned long len) |
| 258 | { |
| 259 | /* FIXME: Write this */ |
| 260 | } |
| 261 | |
| 262 | void __init ld_mmu_iounit(void) |
| 263 | { |
| 264 | BTFIXUPSET_CALL(mmu_lockarea, iounit_lockarea, BTFIXUPCALL_RETO0); |
| 265 | BTFIXUPSET_CALL(mmu_unlockarea, iounit_unlockarea, BTFIXUPCALL_NOP); |
| 266 | |
| 267 | BTFIXUPSET_CALL(mmu_get_scsi_one, iounit_get_scsi_one, BTFIXUPCALL_NORM); |
| 268 | BTFIXUPSET_CALL(mmu_get_scsi_sgl, iounit_get_scsi_sgl, BTFIXUPCALL_NORM); |
| 269 | BTFIXUPSET_CALL(mmu_release_scsi_one, iounit_release_scsi_one, BTFIXUPCALL_NORM); |
| 270 | BTFIXUPSET_CALL(mmu_release_scsi_sgl, iounit_release_scsi_sgl, BTFIXUPCALL_NORM); |
| 271 | |
| 272 | #ifdef CONFIG_SBUS |
| 273 | BTFIXUPSET_CALL(mmu_map_dma_area, iounit_map_dma_area, BTFIXUPCALL_NORM); |
| 274 | BTFIXUPSET_CALL(mmu_unmap_dma_area, iounit_unmap_dma_area, BTFIXUPCALL_NORM); |
| 275 | BTFIXUPSET_CALL(mmu_translate_dvma, iounit_translate_dvma, BTFIXUPCALL_NORM); |
| 276 | #endif |
| 277 | } |
| 278 | |
| 279 | __u32 iounit_map_dma_init(struct sbus_bus *sbus, int size) |
| 280 | { |
| 281 | int i, j, k, npages; |
| 282 | unsigned long rotor, scan, limit; |
| 283 | unsigned long flags; |
| 284 | __u32 ret; |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 285 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | npages = (size + (PAGE_SIZE-1)) >> PAGE_SHIFT; |
| 288 | i = 0x0213; |
| 289 | spin_lock_irqsave(&iounit->lock, flags); |
| 290 | next: j = (i & 15); |
| 291 | rotor = iounit->rotor[j - 1]; |
| 292 | limit = iounit->limit[j]; |
| 293 | scan = rotor; |
| 294 | nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan); |
| 295 | if (scan + npages > limit) { |
| 296 | if (limit != rotor) { |
| 297 | limit = rotor; |
| 298 | scan = iounit->limit[j - 1]; |
| 299 | goto nexti; |
| 300 | } |
| 301 | i >>= 4; |
| 302 | if (!(i & 15)) |
| 303 | panic("iounit_map_dma_init: Couldn't find free iopte slots for %d bytes\n", size); |
| 304 | goto next; |
| 305 | } |
| 306 | for (k = 1, scan++; k < npages; k++) |
| 307 | if (test_bit(scan++, iounit->bmap)) |
| 308 | goto nexti; |
| 309 | iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1]; |
| 310 | scan -= npages; |
| 311 | ret = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT); |
| 312 | for (k = 0; k < npages; k++, scan++) |
| 313 | set_bit(scan, iounit->bmap); |
| 314 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | __u32 iounit_map_dma_page(__u32 vaddr, void *addr, struct sbus_bus *sbus) |
| 319 | { |
| 320 | int scan = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT; |
Robert Reif | 3ac4c94 | 2007-08-10 15:52:06 -0700 | [diff] [blame] | 321 | struct iounit_struct *iounit = sbus->ofdev.dev.archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
| 323 | iounit->page_table[scan] = MKIOPTE(__pa(((unsigned long)addr) & PAGE_MASK)); |
| 324 | return vaddr + (((unsigned long)addr) & ~PAGE_MASK); |
| 325 | } |