Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * io-unit.c:  IO-UNIT specific routines for memory management.
0004  *
0005  * Copyright (C) 1997,1998 Jakub Jelinek    (jj@sunsite.mff.cuni.cz)
0006  */
0007  
0008 #include <linux/kernel.h>
0009 #include <linux/init.h>
0010 #include <linux/slab.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/mm.h>
0013 #include <linux/bitops.h>
0014 #include <linux/dma-map-ops.h>
0015 #include <linux/of.h>
0016 #include <linux/of_device.h>
0017 
0018 #include <asm/io.h>
0019 #include <asm/io-unit.h>
0020 #include <asm/mxcc.h>
0021 #include <asm/cacheflush.h>
0022 #include <asm/tlbflush.h>
0023 #include <asm/dma.h>
0024 #include <asm/oplib.h>
0025 
0026 #include "mm_32.h"
0027 
0028 /* #define IOUNIT_DEBUG */
0029 #ifdef IOUNIT_DEBUG
0030 #define IOD(x) printk(x)
0031 #else
0032 #define IOD(x) do { } while (0)
0033 #endif
0034 
0035 #define IOPERM        (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
0036 #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
0037 
0038 static const struct dma_map_ops iounit_dma_ops;
0039 
0040 static void __init iounit_iommu_init(struct platform_device *op)
0041 {
0042     struct iounit_struct *iounit;
0043     iopte_t __iomem *xpt;
0044     iopte_t __iomem *xptend;
0045 
0046     iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
0047     if (!iounit) {
0048         prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
0049         prom_halt();
0050     }
0051 
0052     iounit->limit[0] = IOUNIT_BMAP1_START;
0053     iounit->limit[1] = IOUNIT_BMAP2_START;
0054     iounit->limit[2] = IOUNIT_BMAPM_START;
0055     iounit->limit[3] = IOUNIT_BMAPM_END;
0056     iounit->rotor[1] = IOUNIT_BMAP2_START;
0057     iounit->rotor[2] = IOUNIT_BMAPM_START;
0058 
0059     xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
0060     if (!xpt) {
0061         prom_printf("SUN4D: Cannot map External Page Table.");
0062         prom_halt();
0063     }
0064     
0065     op->dev.archdata.iommu = iounit;
0066     iounit->page_table = xpt;
0067     spin_lock_init(&iounit->lock);
0068 
0069     xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
0070     for (; xpt < xptend; xpt++)
0071         sbus_writel(0, xpt);
0072 
0073     op->dev.dma_ops = &iounit_dma_ops;
0074 }
0075 
0076 static int __init iounit_init(void)
0077 {
0078     extern void sun4d_init_sbi_irq(void);
0079     struct device_node *dp;
0080 
0081     for_each_node_by_name(dp, "sbi") {
0082         struct platform_device *op = of_find_device_by_node(dp);
0083 
0084         iounit_iommu_init(op);
0085         of_propagate_archdata(op);
0086     }
0087 
0088     sun4d_init_sbi_irq();
0089 
0090     return 0;
0091 }
0092 
0093 subsys_initcall(iounit_init);
0094 
0095 /* One has to hold iounit->lock to call this */
0096 static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
0097 {
0098     int i, j, k, npages;
0099     unsigned long rotor, scan, limit;
0100     iopte_t iopte;
0101 
0102         npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
0103 
0104     /* A tiny bit of magic ingredience :) */
0105     switch (npages) {
0106     case 1: i = 0x0231; break;
0107     case 2: i = 0x0132; break;
0108     default: i = 0x0213; break;
0109     }
0110     
0111     IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
0112     
0113 next:   j = (i & 15);
0114     rotor = iounit->rotor[j - 1];
0115     limit = iounit->limit[j];
0116     scan = rotor;
0117 nexti:  scan = find_next_zero_bit(iounit->bmap, limit, scan);
0118     if (scan + npages > limit) {
0119         if (limit != rotor) {
0120             limit = rotor;
0121             scan = iounit->limit[j - 1];
0122             goto nexti;
0123         }
0124         i >>= 4;
0125         if (!(i & 15))
0126             panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
0127         goto next;
0128     }
0129     for (k = 1, scan++; k < npages; k++)
0130         if (test_bit(scan++, iounit->bmap))
0131             goto nexti;
0132     iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
0133     scan -= npages;
0134     iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
0135     vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
0136     for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
0137         set_bit(scan, iounit->bmap);
0138         sbus_writel(iopte_val(iopte), &iounit->page_table[scan]);
0139     }
0140     IOD(("%08lx\n", vaddr));
0141     return vaddr;
0142 }
0143 
0144 static dma_addr_t iounit_map_page(struct device *dev, struct page *page,
0145         unsigned long offset, size_t len, enum dma_data_direction dir,
0146         unsigned long attrs)
0147 {
0148     void *vaddr = page_address(page) + offset;
0149     struct iounit_struct *iounit = dev->archdata.iommu;
0150     unsigned long ret, flags;
0151     
0152     /* XXX So what is maxphys for us and how do drivers know it? */
0153     if (!len || len > 256 * 1024)
0154         return DMA_MAPPING_ERROR;
0155 
0156     spin_lock_irqsave(&iounit->lock, flags);
0157     ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
0158     spin_unlock_irqrestore(&iounit->lock, flags);
0159     return ret;
0160 }
0161 
0162 static int iounit_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
0163         enum dma_data_direction dir, unsigned long attrs)
0164 {
0165     struct iounit_struct *iounit = dev->archdata.iommu;
0166     struct scatterlist *sg;
0167     unsigned long flags;
0168     int i;
0169 
0170     /* FIXME: Cache some resolved pages - often several sg entries are to the same page */
0171     spin_lock_irqsave(&iounit->lock, flags);
0172     for_each_sg(sgl, sg, nents, i) {
0173         sg->dma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
0174         sg->dma_length = sg->length;
0175     }
0176     spin_unlock_irqrestore(&iounit->lock, flags);
0177     return nents;
0178 }
0179 
0180 static void iounit_unmap_page(struct device *dev, dma_addr_t vaddr, size_t len,
0181         enum dma_data_direction dir, unsigned long attrs)
0182 {
0183     struct iounit_struct *iounit = dev->archdata.iommu;
0184     unsigned long flags;
0185     
0186     spin_lock_irqsave(&iounit->lock, flags);
0187     len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
0188     vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
0189     IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
0190     for (len += vaddr; vaddr < len; vaddr++)
0191         clear_bit(vaddr, iounit->bmap);
0192     spin_unlock_irqrestore(&iounit->lock, flags);
0193 }
0194 
0195 static void iounit_unmap_sg(struct device *dev, struct scatterlist *sgl,
0196         int nents, enum dma_data_direction dir, unsigned long attrs)
0197 {
0198     struct iounit_struct *iounit = dev->archdata.iommu;
0199     unsigned long flags, vaddr, len;
0200     struct scatterlist *sg;
0201     int i;
0202 
0203     spin_lock_irqsave(&iounit->lock, flags);
0204     for_each_sg(sgl, sg, nents, i) {
0205         len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
0206         vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
0207         IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
0208         for (len += vaddr; vaddr < len; vaddr++)
0209             clear_bit(vaddr, iounit->bmap);
0210     }
0211     spin_unlock_irqrestore(&iounit->lock, flags);
0212 }
0213 
0214 #ifdef CONFIG_SBUS
0215 static void *iounit_alloc(struct device *dev, size_t len,
0216         dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
0217 {
0218     struct iounit_struct *iounit = dev->archdata.iommu;
0219     unsigned long va, addr, page, end, ret;
0220     pgprot_t dvma_prot;
0221     iopte_t __iomem *iopte;
0222 
0223     /* XXX So what is maxphys for us and how do drivers know it? */
0224     if (!len || len > 256 * 1024)
0225         return NULL;
0226 
0227     len = PAGE_ALIGN(len);
0228     va = __get_free_pages(gfp | __GFP_ZERO, get_order(len));
0229     if (!va)
0230         return NULL;
0231 
0232     addr = ret = sparc_dma_alloc_resource(dev, len);
0233     if (!addr)
0234         goto out_free_pages;
0235     *dma_handle = addr;
0236 
0237     dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
0238     end = PAGE_ALIGN((addr + len));
0239     while(addr < end) {
0240         page = va;
0241         {
0242             pmd_t *pmdp;
0243             pte_t *ptep;
0244             long i;
0245 
0246             pmdp = pmd_off_k(addr);
0247             ptep = pte_offset_map(pmdp, addr);
0248 
0249             set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
0250 
0251             i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
0252 
0253             iopte = iounit->page_table + i;
0254             sbus_writel(iopte_val(MKIOPTE(__pa(page))), iopte);
0255         }
0256         addr += PAGE_SIZE;
0257         va += PAGE_SIZE;
0258     }
0259     flush_cache_all();
0260     flush_tlb_all();
0261 
0262     return (void *)ret;
0263 
0264 out_free_pages:
0265     free_pages(va, get_order(len));
0266     return NULL;
0267 }
0268 
0269 static void iounit_free(struct device *dev, size_t size, void *cpu_addr,
0270         dma_addr_t dma_addr, unsigned long attrs)
0271 {
0272     /* XXX Somebody please fill this in */
0273 }
0274 #endif
0275 
0276 static const struct dma_map_ops iounit_dma_ops = {
0277 #ifdef CONFIG_SBUS
0278     .alloc          = iounit_alloc,
0279     .free           = iounit_free,
0280 #endif
0281     .map_page       = iounit_map_page,
0282     .unmap_page     = iounit_unmap_page,
0283     .map_sg         = iounit_map_sg,
0284     .unmap_sg       = iounit_unmap_sg,
0285 };