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-mapping.h> 15 #include <linux/of.h> 16 #include <linux/of_device.h> 17 18 #include <asm/pgalloc.h> 19 #include <asm/pgtable.h> 20 #include <asm/io.h> 21 #include <asm/io-unit.h> 22 #include <asm/mxcc.h> 23 #include <asm/cacheflush.h> 24 #include <asm/tlbflush.h> 25 #include <asm/dma.h> 26 #include <asm/oplib.h> 27 28 #include "mm_32.h" 29 30 /* #define IOUNIT_DEBUG */ 31 #ifdef IOUNIT_DEBUG 32 #define IOD(x) printk(x) 33 #else 34 #define IOD(x) do { } while (0) 35 #endif 36 37 #define IOPERM (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID) 38 #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM) 39 40 static const struct dma_map_ops iounit_dma_ops; 41 42 static void __init iounit_iommu_init(struct platform_device *op) 43 { 44 struct iounit_struct *iounit; 45 iopte_t __iomem *xpt; 46 iopte_t __iomem *xptend; 47 48 iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC); 49 if (!iounit) { 50 prom_printf("SUN4D: Cannot alloc iounit, halting.\n"); 51 prom_halt(); 52 } 53 54 iounit->limit[0] = IOUNIT_BMAP1_START; 55 iounit->limit[1] = IOUNIT_BMAP2_START; 56 iounit->limit[2] = IOUNIT_BMAPM_START; 57 iounit->limit[3] = IOUNIT_BMAPM_END; 58 iounit->rotor[1] = IOUNIT_BMAP2_START; 59 iounit->rotor[2] = IOUNIT_BMAPM_START; 60 61 xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT"); 62 if (!xpt) { 63 prom_printf("SUN4D: Cannot map External Page Table."); 64 prom_halt(); 65 } 66 67 op->dev.archdata.iommu = iounit; 68 iounit->page_table = xpt; 69 spin_lock_init(&iounit->lock); 70 71 xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t); 72 for (; xpt < xptend; xpt++) 73 sbus_writel(0, xpt); 74 75 op->dev.dma_ops = &iounit_dma_ops; 76 } 77 78 static int __init iounit_init(void) 79 { 80 extern void sun4d_init_sbi_irq(void); 81 struct device_node *dp; 82 83 for_each_node_by_name(dp, "sbi") { 84 struct platform_device *op = of_find_device_by_node(dp); 85 86 iounit_iommu_init(op); 87 of_propagate_archdata(op); 88 } 89 90 sun4d_init_sbi_irq(); 91 92 return 0; 93 } 94 95 subsys_initcall(iounit_init); 96 97 /* One has to hold iounit->lock to call this */ 98 static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size) 99 { 100 int i, j, k, npages; 101 unsigned long rotor, scan, limit; 102 iopte_t iopte; 103 104 npages = ((vaddr & ~PAGE_MASK) + 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(("iounit_get_area(%08lx,%d[%d])=", vaddr, 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 (%08lx,%d)\n", vaddr, size); 129 goto next; 130 } 131 for (k = 1, scan++; k < npages; k++) 132 if (test_bit(scan++, iounit->bmap)) 133 goto nexti; 134 iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1]; 135 scan -= npages; 136 iopte = MKIOPTE(__pa(vaddr & PAGE_MASK)); 137 vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK); 138 for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) { 139 set_bit(scan, iounit->bmap); 140 sbus_writel(iopte_val(iopte), &iounit->page_table[scan]); 141 } 142 IOD(("%08lx\n", vaddr)); 143 return vaddr; 144 } 145 146 static dma_addr_t iounit_map_page(struct device *dev, struct page *page, 147 unsigned long offset, size_t len, enum dma_data_direction dir, 148 unsigned long attrs) 149 { 150 void *vaddr = page_address(page) + offset; 151 struct iounit_struct *iounit = dev->archdata.iommu; 152 unsigned long ret, flags; 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, (unsigned long)vaddr, 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 = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length); 176 sg->dma_length = sg->length; 177 } 178 spin_unlock_irqrestore(&iounit->lock, flags); 179 return nents; 180 } 181 182 static void iounit_unmap_page(struct device *dev, dma_addr_t vaddr, size_t len, 183 enum dma_data_direction dir, unsigned long attrs) 184 { 185 struct iounit_struct *iounit = dev->archdata.iommu; 186 unsigned long flags; 187 188 spin_lock_irqsave(&iounit->lock, flags); 189 len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT; 190 vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT; 191 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); 192 for (len += vaddr; vaddr < len; vaddr++) 193 clear_bit(vaddr, iounit->bmap); 194 spin_unlock_irqrestore(&iounit->lock, flags); 195 } 196 197 static void iounit_unmap_sg(struct device *dev, struct scatterlist *sgl, 198 int nents, enum dma_data_direction dir, unsigned long attrs) 199 { 200 struct iounit_struct *iounit = dev->archdata.iommu; 201 unsigned long flags, vaddr, len; 202 struct scatterlist *sg; 203 int i; 204 205 spin_lock_irqsave(&iounit->lock, flags); 206 for_each_sg(sgl, sg, nents, i) { 207 len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT; 208 vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT; 209 IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr)); 210 for (len += vaddr; vaddr < len; vaddr++) 211 clear_bit(vaddr, iounit->bmap); 212 } 213 spin_unlock_irqrestore(&iounit->lock, flags); 214 } 215 216 #ifdef CONFIG_SBUS 217 static void *iounit_alloc(struct device *dev, size_t len, 218 dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) 219 { 220 struct iounit_struct *iounit = dev->archdata.iommu; 221 unsigned long va, addr, page, end, ret; 222 pgprot_t dvma_prot; 223 iopte_t __iomem *iopte; 224 225 /* XXX So what is maxphys for us and how do drivers know it? */ 226 if (!len || len > 256 * 1024) 227 return NULL; 228 229 len = PAGE_ALIGN(len); 230 va = __get_free_pages(gfp | __GFP_ZERO, get_order(len)); 231 if (!va) 232 return NULL; 233 234 addr = ret = sparc_dma_alloc_resource(dev, len); 235 if (!addr) 236 goto out_free_pages; 237 *dma_handle = addr; 238 239 dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV); 240 end = PAGE_ALIGN((addr + len)); 241 while(addr < end) { 242 page = va; 243 { 244 pgd_t *pgdp; 245 p4d_t *p4dp; 246 pud_t *pudp; 247 pmd_t *pmdp; 248 pte_t *ptep; 249 long i; 250 251 pgdp = pgd_offset(&init_mm, addr); 252 p4dp = p4d_offset(pgdp, addr); 253 pudp = pud_offset(p4dp, addr); 254 pmdp = pmd_offset(pudp, addr); 255 ptep = pte_offset_map(pmdp, addr); 256 257 set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot)); 258 259 i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT); 260 261 iopte = iounit->page_table + i; 262 sbus_writel(iopte_val(MKIOPTE(__pa(page))), iopte); 263 } 264 addr += PAGE_SIZE; 265 va += PAGE_SIZE; 266 } 267 flush_cache_all(); 268 flush_tlb_all(); 269 270 return (void *)ret; 271 272 out_free_pages: 273 free_pages(va, get_order(len)); 274 return NULL; 275 } 276 277 static void iounit_free(struct device *dev, size_t size, void *cpu_addr, 278 dma_addr_t dma_addr, unsigned long attrs) 279 { 280 /* XXX Somebody please fill this in */ 281 } 282 #endif 283 284 static const struct dma_map_ops iounit_dma_ops = { 285 #ifdef CONFIG_SBUS 286 .alloc = iounit_alloc, 287 .free = iounit_free, 288 #endif 289 .map_page = iounit_map_page, 290 .unmap_page = iounit_unmap_page, 291 .map_sg = iounit_map_sg, 292 .unmap_sg = iounit_unmap_sg, 293 }; 294