Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ioport.c:  Simple io mapping allocator.
0004  *
0005  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
0006  * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
0007  *
0008  * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
0009  *
0010  * 2000/01/29
0011  * <rth> zait: as long as pci_alloc_consistent produces something addressable, 
0012  *  things are ok.
0013  * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
0014  *  pointer into the big page mapping
0015  * <rth> zait: so what?
0016  * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
0017  * <zaitcev> Hmm
0018  * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
0019  *  So far so good.
0020  * <zaitcev> Now, driver calls pci_free_consistent(with result of
0021  *  remap_it_my_way()).
0022  * <zaitcev> How do you find the address to pass to free_pages()?
0023  * <rth> zait: walk the page tables?  It's only two or three level after all.
0024  * <rth> zait: you have to walk them anyway to remove the mapping.
0025  * <zaitcev> Hmm
0026  * <zaitcev> Sounds reasonable
0027  */
0028 
0029 #include <linux/module.h>
0030 #include <linux/sched.h>
0031 #include <linux/kernel.h>
0032 #include <linux/errno.h>
0033 #include <linux/types.h>
0034 #include <linux/ioport.h>
0035 #include <linux/mm.h>
0036 #include <linux/slab.h>
0037 #include <linux/pci.h>      /* struct pci_dev */
0038 #include <linux/proc_fs.h>
0039 #include <linux/seq_file.h>
0040 #include <linux/scatterlist.h>
0041 #include <linux/dma-map-ops.h>
0042 #include <linux/of_device.h>
0043 
0044 #include <asm/io.h>
0045 #include <asm/vaddrs.h>
0046 #include <asm/oplib.h>
0047 #include <asm/prom.h>
0048 #include <asm/page.h>
0049 #include <asm/pgalloc.h>
0050 #include <asm/dma.h>
0051 #include <asm/iommu.h>
0052 #include <asm/io-unit.h>
0053 #include <asm/leon.h>
0054 
0055 static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
0056 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
0057     unsigned long size, char *name);
0058 static void _sparc_free_io(struct resource *res);
0059 
0060 static void register_proc_sparc_ioport(void);
0061 
0062 /* This points to the next to use virtual memory for DVMA mappings */
0063 static struct resource _sparc_dvma = {
0064     .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
0065 };
0066 /* This points to the start of I/O mappings, cluable from outside. */
0067 /*ext*/ struct resource sparc_iomap = {
0068     .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
0069 };
0070 
0071 /*
0072  * Our mini-allocator...
0073  * Boy this is gross! We need it because we must map I/O for
0074  * timers and interrupt controller before the kmalloc is available.
0075  */
0076 
0077 #define XNMLN  15
0078 #define XNRES  10   /* SS-10 uses 8 */
0079 
0080 struct xresource {
0081     struct resource xres;   /* Must be first */
0082     int xflag;      /* 1 == used */
0083     char xname[XNMLN+1];
0084 };
0085 
0086 static struct xresource xresv[XNRES];
0087 
0088 static struct xresource *xres_alloc(void) {
0089     struct xresource *xrp;
0090     int n;
0091 
0092     xrp = xresv;
0093     for (n = 0; n < XNRES; n++) {
0094         if (xrp->xflag == 0) {
0095             xrp->xflag = 1;
0096             return xrp;
0097         }
0098         xrp++;
0099     }
0100     return NULL;
0101 }
0102 
0103 static void xres_free(struct xresource *xrp) {
0104     xrp->xflag = 0;
0105 }
0106 
0107 /*
0108  * These are typically used in PCI drivers
0109  * which are trying to be cross-platform.
0110  *
0111  * Bus type is always zero on IIep.
0112  */
0113 void __iomem *ioremap(phys_addr_t offset, size_t size)
0114 {
0115     char name[14];
0116 
0117     sprintf(name, "phys_%08x", (u32)offset);
0118     return _sparc_alloc_io(0, (unsigned long)offset, size, name);
0119 }
0120 EXPORT_SYMBOL(ioremap);
0121 
0122 /*
0123  * Complementary to ioremap().
0124  */
0125 void iounmap(volatile void __iomem *virtual)
0126 {
0127     unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
0128     struct resource *res;
0129 
0130     /*
0131      * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
0132      * This probably warrants some sort of hashing.
0133     */
0134     if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) {
0135         printk("free_io/iounmap: cannot free %lx\n", vaddr);
0136         return;
0137     }
0138     _sparc_free_io(res);
0139 
0140     if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
0141         xres_free((struct xresource *)res);
0142     } else {
0143         kfree(res);
0144     }
0145 }
0146 EXPORT_SYMBOL(iounmap);
0147 
0148 void __iomem *of_ioremap(struct resource *res, unsigned long offset,
0149              unsigned long size, char *name)
0150 {
0151     return _sparc_alloc_io(res->flags & 0xF,
0152                    res->start + offset,
0153                    size, name);
0154 }
0155 EXPORT_SYMBOL(of_ioremap);
0156 
0157 void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
0158 {
0159     iounmap(base);
0160 }
0161 EXPORT_SYMBOL(of_iounmap);
0162 
0163 /*
0164  * Meat of mapping
0165  */
0166 static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
0167     unsigned long size, char *name)
0168 {
0169     static int printed_full;
0170     struct xresource *xres;
0171     struct resource *res;
0172     char *tack;
0173     int tlen;
0174     void __iomem *va;   /* P3 diag */
0175 
0176     if (name == NULL) name = "???";
0177 
0178     if ((xres = xres_alloc()) != NULL) {
0179         tack = xres->xname;
0180         res = &xres->xres;
0181     } else {
0182         if (!printed_full) {
0183             printk("ioremap: done with statics, switching to malloc\n");
0184             printed_full = 1;
0185         }
0186         tlen = strlen(name);
0187         tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
0188         if (tack == NULL) return NULL;
0189         memset(tack, 0, sizeof(struct resource));
0190         res = (struct resource *) tack;
0191         tack += sizeof (struct resource);
0192     }
0193 
0194     strlcpy(tack, name, XNMLN+1);
0195     res->name = tack;
0196 
0197     va = _sparc_ioremap(res, busno, phys, size);
0198     /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
0199     return va;
0200 }
0201 
0202 /*
0203  */
0204 static void __iomem *
0205 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
0206 {
0207     unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
0208 
0209     if (allocate_resource(&sparc_iomap, res,
0210         (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
0211         sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
0212         /* Usually we cannot see printks in this case. */
0213         prom_printf("alloc_io_res(%s): cannot occupy\n",
0214             (res->name != NULL)? res->name: "???");
0215         prom_halt();
0216     }
0217 
0218     pa &= PAGE_MASK;
0219     srmmu_mapiorange(bus, pa, res->start, resource_size(res));
0220 
0221     return (void __iomem *)(unsigned long)(res->start + offset);
0222 }
0223 
0224 /*
0225  * Complementary to _sparc_ioremap().
0226  */
0227 static void _sparc_free_io(struct resource *res)
0228 {
0229     unsigned long plen;
0230 
0231     plen = resource_size(res);
0232     BUG_ON((plen & (PAGE_SIZE-1)) != 0);
0233     srmmu_unmapiorange(res->start, plen);
0234     release_resource(res);
0235 }
0236 
0237 unsigned long sparc_dma_alloc_resource(struct device *dev, size_t len)
0238 {
0239     struct resource *res;
0240 
0241     res = kzalloc(sizeof(*res), GFP_KERNEL);
0242     if (!res)
0243         return 0;
0244     res->name = dev->of_node->full_name;
0245 
0246     if (allocate_resource(&_sparc_dvma, res, len, _sparc_dvma.start,
0247                   _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
0248         printk("%s: cannot occupy 0x%zx", __func__, len);
0249         kfree(res);
0250         return 0;
0251     }
0252 
0253     return res->start;
0254 }
0255 
0256 bool sparc_dma_free_resource(void *cpu_addr, size_t size)
0257 {
0258     unsigned long addr = (unsigned long)cpu_addr;
0259     struct resource *res;
0260 
0261     res = lookup_resource(&_sparc_dvma, addr);
0262     if (!res) {
0263         printk("%s: cannot free %p\n", __func__, cpu_addr);
0264         return false;
0265     }
0266 
0267     if ((addr & (PAGE_SIZE - 1)) != 0) {
0268         printk("%s: unaligned va %p\n", __func__, cpu_addr);
0269         return false;
0270     }
0271 
0272     size = PAGE_ALIGN(size);
0273     if (resource_size(res) != size) {
0274         printk("%s: region 0x%lx asked 0x%zx\n",
0275             __func__, (long)resource_size(res), size);
0276         return false;
0277     }
0278 
0279     release_resource(res);
0280     kfree(res);
0281     return true;
0282 }
0283 
0284 #ifdef CONFIG_SBUS
0285 
0286 void sbus_set_sbus64(struct device *dev, int x)
0287 {
0288     printk("sbus_set_sbus64: unsupported\n");
0289 }
0290 EXPORT_SYMBOL(sbus_set_sbus64);
0291 
0292 static int __init sparc_register_ioport(void)
0293 {
0294     register_proc_sparc_ioport();
0295 
0296     return 0;
0297 }
0298 
0299 arch_initcall(sparc_register_ioport);
0300 
0301 #endif /* CONFIG_SBUS */
0302 
0303 /*
0304  * IIep is write-through, not flushing on cpu to device transfer.
0305  *
0306  * On LEON systems without cache snooping, the entire D-CACHE must be flushed to
0307  * make DMA to cacheable memory coherent.
0308  */
0309 void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size,
0310         enum dma_data_direction dir)
0311 {
0312     if (dir != DMA_TO_DEVICE &&
0313         sparc_cpu_model == sparc_leon &&
0314         !sparc_leon3_snooping_enabled())
0315         leon_flush_dcache_all();
0316 }
0317 
0318 #ifdef CONFIG_PROC_FS
0319 
0320 static int sparc_io_proc_show(struct seq_file *m, void *v)
0321 {
0322     struct resource *root = m->private, *r;
0323     const char *nm;
0324 
0325     for (r = root->child; r != NULL; r = r->sibling) {
0326         if ((nm = r->name) == NULL) nm = "???";
0327         seq_printf(m, "%016llx-%016llx: %s\n",
0328                 (unsigned long long)r->start,
0329                 (unsigned long long)r->end, nm);
0330     }
0331 
0332     return 0;
0333 }
0334 #endif /* CONFIG_PROC_FS */
0335 
0336 static void register_proc_sparc_ioport(void)
0337 {
0338 #ifdef CONFIG_PROC_FS
0339     proc_create_single_data("io_map", 0, NULL, sparc_io_proc_show,
0340             &sparc_iomap);
0341     proc_create_single_data("dvma_map", 0, NULL, sparc_io_proc_show,
0342             &_sparc_dvma);
0343 #endif
0344 }