1 // SPDX-License-Identifier: GPL-2.0 2 /* 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 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/bitops.h> 14 #include <linux/dma-map-ops.h> 15 #include <linux/of.h> 16 #include <linux/of_platform.h> 17 #include <linux/platform_device.h> 18 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> 25 #include <asm/oplib.h> 26 27 #include "mm_32.h" 28 29 /* #define IOUNIT_DEBUG */ 30 #ifdef IOUNIT_DEBUG 31 #define IOD(x) printk(x) 32 #else 33 #define IOD(x) do { } while (0) 34 #endif 35 36 #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID) 37 #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM) 38 39 static const struct dma_map_ops iounit_dma_ops; 40 41 static void __init iounit_iommu_init(struct platform_device *op) 42 { 43 struct iounit_struct *iounit; 44 iopte_t __iomem *xpt; 45 iopte_t __iomem *xptend; 46 47 iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC); 48 if (!iounit) { 49 prom_printf("SUN4D: Cannot alloc iounit, halting.\n"); 50 prom_halt(); 51 } 52 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 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(); 64 } 65 66 op->dev.archdata.iommu = iounit; 67 iounit->page_table = xpt; 68 spin_lock_init(&iounit->lock); 69 70 xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t); 71 for (; xpt < xptend; xpt++) 72 sbus_writel(0, xpt); 73 74 op->dev.dma_ops = &iounit_dma_ops; 75 } 76 77 static int __init iounit_init(void) 78 { 79 extern void sun4d_init_sbi_irq(void); 80 struct device_node *dp; 81 82 for_each_node_by_name(dp, "sbi") { 83 struct platform_device *op = of_find_device_by_node(dp); 84 85 iounit_iommu_init(op); 86 of_propagate_archdata(op); 87 } 88 89 sun4d_init_sbi_irq(); 90 91 return 0; 92 } 93 94 subsys_initcall(iounit_init); 95 96 /* One has to hold iounit->lock to call this */ 97 static dma_addr_t iounit_get_area(struct iounit_struct *iounit, 98 phys_addr_t phys, int size) 99 { 100 int i, j, k, npages; 101 unsigned long rotor, scan, limit; 102 iopte_t iopte; 103 104 npages = (offset_in_page(phys) + size + (PAGE_SIZE - 1)) >> PAGE_SHIFT; 105 106 /* A tiny bit of magic ingredience :) */ 107 switch (npages) { 108 case 1: i = 0x0231; break; 109 case 2: i = 0x0132; break; 110 default: i = 0x0213; break; 111 } 112 113 IOD(("%s(%pa,%d[%d])=", __func__, &phys, size, npages)); 114 115 next: j = (i & 15); 116 rotor = iounit->rotor[j - 1]; 117 limit = iounit->limit[j]; 118 scan = rotor; 119 nexti: scan = find_next_zero_bit(iounit->bmap, limit, scan); 120 if (scan + npages > limit) { 121 if (limit != rotor) { 122 limit = rotor; 123 scan = iounit->limit[j - 1]; 124 goto nexti; 125 } 126 i >>= 4; 127 if (!(i & 15)) 128 panic("iounit_get_area: Couldn't find free iopte slots for (%pa,%d)\n", 129 &phys, size); 130 goto next; 131 } 132 for (k = 1, scan++; k < npages; k++) 133 if (test_bit(scan++, iounit->bmap)) 134 goto nexti; 135 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1]; 136 scan -= npages; 137 iopte = MKIOPTE(phys & PAGE_MASK); 138 phys = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + offset_in_page(phys); 139 for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) { 140 set_bit(scan, iounit->bmap); 141 sbus_writel(iopte_val(iopte), &iounit->page_table[scan]); 142 } 143 IOD(("%pa\n", &phys)); 144 return phys; 145 } 146 147 static dma_addr_t iounit_map_phys(struct device *dev, phys_addr_t phys, 148 size_t len, enum dma_data_direction dir, unsigned long attrs) 149 { 150 struct iounit_struct *iounit = dev->archdata.iommu; 151 unsigned long flags; 152 dma_addr_t ret; 153 154 /* XXX So what is maxphys for us and how do drivers know it? */ 155 if (!len || len > 256 * 1024) 156 return DMA_MAPPING_ERROR; 157 158 spin_lock_irqsave(&iounit->lock, flags); 159 ret = iounit_get_area(iounit, phys, len); 160 spin_unlock_irqrestore(&iounit->lock, flags); 161 return ret; 162 } 163 164 static int iounit_map_sg(struct device *dev, struct scatterlist *sgl, int nents, 165 enum dma_data_direction dir, unsigned long attrs) 166 { 167 struct iounit_struct *iounit = dev->archdata.iommu; 168 struct scatterlist *sg; 169 unsigned long flags; 170 int i; 171 172 /* FIXME: Cache some resolved pages - often several sg entries are to the same page */ 173 spin_lock_irqsave(&iounit->lock, flags); 174 for_each_sg(sgl, sg, nents, i) { 175 sg->dma_address = 176 iounit_get_area(iounit, sg_phys(sg), sg->length); 177 sg->dma_length = sg->length; 178 } 179 spin_unlock_irqrestore(&iounit->lock, flags); 180 return nents; 181 } 182 183 static void iounit_unmap_phys(struct device *dev, dma_addr_t vaddr, size_t len, 184 enum dma_data_direction dir, unsigned long attrs) 185 { 186 struct iounit_struct *iounit = dev->archdata.iommu; 187 unsigned long flags; 188 189 spin_lock_irqsave(&iounit->lock, flags); 190 len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT; 191 vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT; 192 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); 193 for (len += vaddr; vaddr < len; vaddr++) 194 clear_bit(vaddr, iounit->bmap); 195 spin_unlock_irqrestore(&iounit->lock, flags); 196 } 197 198 static void iounit_unmap_sg(struct device *dev, struct scatterlist *sgl, 199 int nents, enum dma_data_direction dir, unsigned long attrs) 200 { 201 struct iounit_struct *iounit = dev->archdata.iommu; 202 unsigned long flags, vaddr, len; 203 struct scatterlist *sg; 204 int i; 205 206 spin_lock_irqsave(&iounit->lock, flags); 207 for_each_sg(sgl, sg, nents, i) { 208 len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT; 209 vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT; 210 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); 211 for (len += vaddr; vaddr < len; vaddr++) 212 clear_bit(vaddr, iounit->bmap); 213 } 214 spin_unlock_irqrestore(&iounit->lock, flags); 215 } 216 217 #ifdef CONFIG_SBUS 218 static void *iounit_alloc(struct device *dev, size_t len, 219 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) 220 { 221 struct iounit_struct *iounit = dev->archdata.iommu; 222 unsigned long va, addr, page, end, ret; 223 pgprot_t dvma_prot; 224 iopte_t __iomem *iopte; 225 226 /* XXX So what is maxphys for us and how do drivers know it? */ 227 if (!len || len > 256 * 1024) 228 return NULL; 229 230 len = PAGE_ALIGN(len); 231 va = __get_free_pages(gfp | __GFP_ZERO, get_order(len)); 232 if (!va) 233 return NULL; 234 235 addr = ret = sparc_dma_alloc_resource(dev, len); 236 if (!addr) 237 goto out_free_pages; 238 *dma_handle = addr; 239 240 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV); 241 end = PAGE_ALIGN((addr + len)); 242 while(addr < end) { 243 page = va; 244 { 245 pmd_t *pmdp; 246 pte_t *ptep; 247 long i; 248 249 pmdp = pmd_off_k(addr); 250 ptep = pte_offset_kernel(pmdp, addr); 251 252 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot)); 253 254 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT); 255 256 iopte = iounit->page_table + i; 257 sbus_writel(iopte_val(MKIOPTE(__pa(page))), iopte); 258 } 259 addr += PAGE_SIZE; 260 va += PAGE_SIZE; 261 } 262 flush_cache_all(); 263 flush_tlb_all(); 264 265 return (void *)ret; 266 267 out_free_pages: 268 free_pages(va, get_order(len)); 269 return NULL; 270 } 271 272 static void iounit_free(struct device *dev, size_t size, void *cpu_addr, 273 dma_addr_t dma_addr, unsigned long attrs) 274 { 275 /* XXX Somebody please fill this in */ 276 } 277 #endif 278 279 static const struct dma_map_ops iounit_dma_ops = { 280 #ifdef CONFIG_SBUS 281 .alloc = iounit_alloc, 282 .free = iounit_free, 283 #endif 284 .map_phys = iounit_map_phys, 285 .unmap_phys = iounit_unmap_phys, 286 .map_sg = iounit_map_sg, 287 .unmap_sg = iounit_unmap_sg, 288 }; 289