Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Adrian Bunk | 88278ca | 2008-05-19 16:53:02 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * io-unit.c: IO-UNIT specific routines for memory management. |
| 4 | * |
| 5 | * Copyright (C) 1997,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz) |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/highmem.h> /* pte_offset_map => kmap_atomic */ |
| 14 | #include <linux/bitops.h> |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
David S. Miller | 9dc6923 | 2008-08-27 19:54:01 -0700 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/pgalloc.h> |
| 20 | #include <asm/pgtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/io.h> |
| 22 | #include <asm/io-unit.h> |
| 23 | #include <asm/mxcc.h> |
| 24 | #include <asm/cacheflush.h> |
| 25 | #include <asm/tlbflush.h> |
| 26 | #include <asm/dma.h> |
David S. Miller | d4accd6 | 2006-11-30 17:11:26 -0800 | [diff] [blame] | 27 | #include <asm/oplib.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Sam Ravnborg | 1918660 | 2014-05-16 23:25:41 +0200 | [diff] [blame] | 29 | #include "mm_32.h" |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | /* #define IOUNIT_DEBUG */ |
| 32 | #ifdef IOUNIT_DEBUG |
| 33 | #define IOD(x) printk(x) |
| 34 | #else |
| 35 | #define IOD(x) do { } while (0) |
| 36 | #endif |
| 37 | |
| 38 | #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID) |
| 39 | #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM) |
| 40 | |
Grant Likely | cd4cd73 | 2010-07-22 16:04:30 -0600 | [diff] [blame] | 41 | static void __init iounit_iommu_init(struct platform_device *op) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | struct iounit_struct *iounit; |
Sam Ravnborg | 1918660 | 2014-05-16 23:25:41 +0200 | [diff] [blame] | 44 | iopte_t __iomem *xpt; |
| 45 | iopte_t __iomem *xptend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Yan Burman | c80892d | 2006-11-30 17:07:04 -0800 | [diff] [blame] | 47 | iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC); |
David S. Miller | d4accd6 | 2006-11-30 17:11:26 -0800 | [diff] [blame] | 48 | if (!iounit) { |
| 49 | prom_printf("SUN4D: Cannot alloc iounit, halting.\n"); |
| 50 | prom_halt(); |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | iounit->limit[0] = IOUNIT_BMAP1_START; |
| 54 | iounit->limit[1] = IOUNIT_BMAP2_START; |
| 55 | iounit->limit[2] = IOUNIT_BMAPM_START; |
| 56 | iounit->limit[3] = IOUNIT_BMAPM_END; |
| 57 | iounit->rotor[1] = IOUNIT_BMAP2_START; |
| 58 | iounit->rotor[2] = IOUNIT_BMAPM_START; |
| 59 | |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 60 | xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT"); |
| 61 | if (!xpt) { |
| 62 | prom_printf("SUN4D: Cannot map External Page Table."); |
| 63 | prom_halt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 66 | op->dev.archdata.iommu = iounit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | iounit->page_table = xpt; |
Raymond Burns | 2f72ba4 | 2006-07-17 21:40:27 -0700 | [diff] [blame] | 68 | spin_lock_init(&iounit->lock); |
Sam Ravnborg | 1918660 | 2014-05-16 23:25:41 +0200 | [diff] [blame] | 69 | |
| 70 | xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t); |
| 71 | for (; xpt < xptend; xpt++) |
| 72 | sbus_writel(0, xpt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
| 74 | |
David S. Miller | 046e26a | 2008-08-27 04:54:04 -0700 | [diff] [blame] | 75 | static int __init iounit_init(void) |
| 76 | { |
| 77 | extern void sun4d_init_sbi_irq(void); |
| 78 | struct device_node *dp; |
| 79 | |
| 80 | for_each_node_by_name(dp, "sbi") { |
Grant Likely | cd4cd73 | 2010-07-22 16:04:30 -0600 | [diff] [blame] | 81 | struct platform_device *op = of_find_device_by_node(dp); |
David S. Miller | 046e26a | 2008-08-27 04:54:04 -0700 | [diff] [blame] | 82 | |
| 83 | iounit_iommu_init(op); |
| 84 | of_propagate_archdata(op); |
| 85 | } |
| 86 | |
| 87 | sun4d_init_sbi_irq(); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | subsys_initcall(iounit_init); |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* One has to hold iounit->lock to call this */ |
| 95 | static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size) |
| 96 | { |
| 97 | int i, j, k, npages; |
| 98 | unsigned long rotor, scan, limit; |
| 99 | iopte_t iopte; |
| 100 | |
| 101 | npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT; |
| 102 | |
| 103 | /* A tiny bit of magic ingredience :) */ |
| 104 | switch (npages) { |
| 105 | case 1: i = 0x0231; break; |
| 106 | case 2: i = 0x0132; break; |
| 107 | default: i = 0x0213; break; |
| 108 | } |
| 109 | |
| 110 | IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages)); |
| 111 | |
| 112 | next: j = (i & 15); |
| 113 | rotor = iounit->rotor[j - 1]; |
| 114 | limit = iounit->limit[j]; |
| 115 | scan = rotor; |
| 116 | nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan); |
| 117 | if (scan + npages > limit) { |
| 118 | if (limit != rotor) { |
| 119 | limit = rotor; |
| 120 | scan = iounit->limit[j - 1]; |
| 121 | goto nexti; |
| 122 | } |
| 123 | i >>= 4; |
| 124 | if (!(i & 15)) |
| 125 | panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size); |
| 126 | goto next; |
| 127 | } |
| 128 | for (k = 1, scan++; k < npages; k++) |
| 129 | if (test_bit(scan++, iounit->bmap)) |
| 130 | goto nexti; |
| 131 | iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1]; |
| 132 | scan -= npages; |
| 133 | iopte = MKIOPTE(__pa(vaddr & PAGE_MASK)); |
| 134 | vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK); |
| 135 | for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) { |
| 136 | set_bit(scan, iounit->bmap); |
Sam Ravnborg | 6e6e418 | 2016-04-22 19:41:06 +0200 | [diff] [blame] | 137 | sbus_writel(iopte_val(iopte), &iounit->page_table[scan]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | IOD(("%08lx\n", vaddr)); |
| 140 | return vaddr; |
| 141 | } |
| 142 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 143 | static __u32 iounit_get_scsi_one(struct device *dev, char *vaddr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 145 | struct iounit_struct *iounit = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | unsigned long ret, flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | spin_lock_irqsave(&iounit->lock, flags); |
| 149 | ret = iounit_get_area(iounit, (unsigned long)vaddr, len); |
| 150 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 151 | return ret; |
| 152 | } |
| 153 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 154 | static void iounit_get_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 156 | struct iounit_struct *iounit = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | /* FIXME: Cache some resolved pages - often several sg entries are to the same page */ |
| 160 | spin_lock_irqsave(&iounit->lock, flags); |
| 161 | while (sz != 0) { |
| 162 | --sz; |
Robert Reif | aa83a26 | 2008-12-11 20:24:58 -0800 | [diff] [blame] | 163 | sg->dma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length); |
| 164 | sg->dma_length = sg->length; |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 165 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 168 | } |
| 169 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 170 | static void iounit_release_scsi_one(struct device *dev, __u32 vaddr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 172 | struct iounit_struct *iounit = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | spin_lock_irqsave(&iounit->lock, flags); |
| 176 | len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT; |
| 177 | vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT; |
| 178 | IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); |
| 179 | for (len += vaddr; vaddr < len; vaddr++) |
| 180 | clear_bit(vaddr, iounit->bmap); |
| 181 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 182 | } |
| 183 | |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 184 | static void iounit_release_scsi_sgl(struct device *dev, struct scatterlist *sg, int sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 186 | struct iounit_struct *iounit = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | unsigned long flags; |
| 188 | unsigned long vaddr, len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | spin_lock_irqsave(&iounit->lock, flags); |
| 191 | while (sz != 0) { |
| 192 | --sz; |
Robert Reif | aa83a26 | 2008-12-11 20:24:58 -0800 | [diff] [blame] | 193 | len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT; |
| 194 | vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); |
| 196 | for (len += vaddr; vaddr < len; vaddr++) |
| 197 | clear_bit(vaddr, iounit->bmap); |
Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 198 | sg = sg_next(sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | spin_unlock_irqrestore(&iounit->lock, flags); |
| 201 | } |
| 202 | |
| 203 | #ifdef CONFIG_SBUS |
David S. Miller | d894d96 | 2012-05-13 13:57:05 -0700 | [diff] [blame] | 204 | static int iounit_map_dma_area(struct device *dev, dma_addr_t *pba, unsigned long va, unsigned long addr, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 206 | struct iounit_struct *iounit = dev->archdata.iommu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | unsigned long page, end; |
| 208 | pgprot_t dvma_prot; |
Sam Ravnborg | 1918660 | 2014-05-16 23:25:41 +0200 | [diff] [blame] | 209 | iopte_t __iomem *iopte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | *pba = addr; |
| 212 | |
| 213 | dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV); |
| 214 | end = PAGE_ALIGN((addr + len)); |
| 215 | while(addr < end) { |
| 216 | page = va; |
| 217 | { |
| 218 | pgd_t *pgdp; |
| 219 | pmd_t *pmdp; |
| 220 | pte_t *ptep; |
| 221 | long i; |
| 222 | |
| 223 | pgdp = pgd_offset(&init_mm, addr); |
| 224 | pmdp = pmd_offset(pgdp, addr); |
| 225 | ptep = pte_offset_map(pmdp, addr); |
| 226 | |
| 227 | set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot)); |
| 228 | |
| 229 | i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT); |
| 230 | |
Sam Ravnborg | 1918660 | 2014-05-16 23:25:41 +0200 | [diff] [blame] | 231 | iopte = iounit->page_table + i; |
Sam Ravnborg | 6e6e418 | 2016-04-22 19:41:06 +0200 | [diff] [blame] | 232 | sbus_writel(iopte_val(MKIOPTE(__pa(page))), iopte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | addr += PAGE_SIZE; |
| 235 | va += PAGE_SIZE; |
| 236 | } |
| 237 | flush_cache_all(); |
| 238 | flush_tlb_all(); |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 243 | static void iounit_unmap_dma_area(struct device *dev, unsigned long addr, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
| 245 | /* XXX Somebody please fill this in */ |
| 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | #endif |
| 248 | |
David S. Miller | d894d96 | 2012-05-13 13:57:05 -0700 | [diff] [blame] | 249 | static const struct sparc32_dma_ops iounit_dma_ops = { |
| 250 | .get_scsi_one = iounit_get_scsi_one, |
| 251 | .get_scsi_sgl = iounit_get_scsi_sgl, |
| 252 | .release_scsi_one = iounit_release_scsi_one, |
| 253 | .release_scsi_sgl = iounit_release_scsi_sgl, |
| 254 | #ifdef CONFIG_SBUS |
| 255 | .map_dma_area = iounit_map_dma_area, |
| 256 | .unmap_dma_area = iounit_unmap_dma_area, |
| 257 | #endif |
| 258 | }; |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | void __init ld_mmu_iounit(void) |
| 261 | { |
David S. Miller | d894d96 | 2012-05-13 13:57:05 -0700 | [diff] [blame] | 262 | sparc32_dma_ops = &iounit_dma_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |