| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <linux/module.h> |
| #include <linux/sched.h> |
| #include <linux/kernel.h> |
| #include <linux/errno.h> |
| #include <linux/types.h> |
| #include <linux/ioport.h> |
| #include <linux/mm.h> |
| #include <linux/slab.h> |
| #include <linux/pci.h> /* struct pci_dev */ |
| #include <linux/proc_fs.h> |
| #include <linux/seq_file.h> |
| #include <linux/scatterlist.h> |
| #include <linux/dma-map-ops.h> |
| #include <linux/of_device.h> |
| |
| #include <asm/io.h> |
| #include <asm/vaddrs.h> |
| #include <asm/oplib.h> |
| #include <asm/prom.h> |
| #include <asm/page.h> |
| #include <asm/pgalloc.h> |
| #include <asm/dma.h> |
| #include <asm/iommu.h> |
| #include <asm/io-unit.h> |
| #include <asm/leon.h> |
| |
| |
| |
| |
| static inline void dma_make_coherent(unsigned long pa, unsigned long len) |
| { |
| <------>if (sparc_cpu_model == sparc_leon) { |
| <------><------>if (!sparc_leon3_snooping_enabled()) |
| <------><------><------>leon_flush_dcache_all(); |
| <------>} |
| } |
| |
| static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz); |
| static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, |
| unsigned long size, char *name); |
| static void _sparc_free_io(struct resource *res); |
| |
| static void register_proc_sparc_ioport(void); |
| |
| |
| static struct resource _sparc_dvma = { |
| <------>.name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1 |
| }; |
| |
| struct resource sparc_iomap = { |
| <------>.name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1 |
| }; |
| |
| |
| |
| |
| |
| |
| |
| #define XNMLN 15 |
| #define XNRES 10 |
| |
| struct xresource { |
| <------>struct resource xres; |
| <------>int xflag; |
| <------>char xname[XNMLN+1]; |
| }; |
| |
| static struct xresource xresv[XNRES]; |
| |
| static struct xresource *xres_alloc(void) { |
| <------>struct xresource *xrp; |
| <------>int n; |
| |
| <------>xrp = xresv; |
| <------>for (n = 0; n < XNRES; n++) { |
| <------><------>if (xrp->xflag == 0) { |
| <------><------><------>xrp->xflag = 1; |
| <------><------><------>return xrp; |
| <------><------>} |
| <------><------>xrp++; |
| <------>} |
| <------>return NULL; |
| } |
| |
| static void xres_free(struct xresource *xrp) { |
| <------>xrp->xflag = 0; |
| } |
| |
| |
| |
| |
| |
| |
| |
| void __iomem *ioremap(phys_addr_t offset, size_t size) |
| { |
| <------>char name[14]; |
| |
| <------>sprintf(name, "phys_%08x", (u32)offset); |
| <------>return _sparc_alloc_io(0, (unsigned long)offset, size, name); |
| } |
| EXPORT_SYMBOL(ioremap); |
| |
| |
| |
| |
| void iounmap(volatile void __iomem *virtual) |
| { |
| <------>unsigned long vaddr = (unsigned long) virtual & PAGE_MASK; |
| <------>struct resource *res; |
| |
| <------> |
| <------> * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case. |
| <------> * This probably warrants some sort of hashing. |
| <------>*/ |
| <------>if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) { |
| <------><------>printk("free_io/iounmap: cannot free %lx\n", vaddr); |
| <------><------>return; |
| <------>} |
| <------>_sparc_free_io(res); |
| |
| <------>if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) { |
| <------><------>xres_free((struct xresource *)res); |
| <------>} else { |
| <------><------>kfree(res); |
| <------>} |
| } |
| EXPORT_SYMBOL(iounmap); |
| |
| void __iomem *of_ioremap(struct resource *res, unsigned long offset, |
| <------><------><------> unsigned long size, char *name) |
| { |
| <------>return _sparc_alloc_io(res->flags & 0xF, |
| <------><------><------> res->start + offset, |
| <------><------><------> size, name); |
| } |
| EXPORT_SYMBOL(of_ioremap); |
| |
| void of_iounmap(struct resource *res, void __iomem *base, unsigned long size) |
| { |
| <------>iounmap(base); |
| } |
| EXPORT_SYMBOL(of_iounmap); |
| |
| |
| |
| |
| static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, |
| unsigned long size, char *name) |
| { |
| <------>static int printed_full; |
| <------>struct xresource *xres; |
| <------>struct resource *res; |
| <------>char *tack; |
| <------>int tlen; |
| <------>void __iomem *va; |
| |
| <------>if (name == NULL) name = "???"; |
| |
| <------>if ((xres = xres_alloc()) != NULL) { |
| <------><------>tack = xres->xname; |
| <------><------>res = &xres->xres; |
| <------>} else { |
| <------><------>if (!printed_full) { |
| <------><------><------>printk("ioremap: done with statics, switching to malloc\n"); |
| <------><------><------>printed_full = 1; |
| <------><------>} |
| <------><------>tlen = strlen(name); |
| <------><------>tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL); |
| <------><------>if (tack == NULL) return NULL; |
| <------><------>memset(tack, 0, sizeof(struct resource)); |
| <------><------>res = (struct resource *) tack; |
| <------><------>tack += sizeof (struct resource); |
| <------>} |
| |
| <------>strlcpy(tack, name, XNMLN+1); |
| <------>res->name = tack; |
| |
| <------>va = _sparc_ioremap(res, busno, phys, size); |
| <------> |
| <------>return va; |
| } |
| |
| |
| |
| static void __iomem * |
| _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz) |
| { |
| <------>unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK); |
| |
| <------>if (allocate_resource(&sparc_iomap, res, |
| <------> (offset + sz + PAGE_SIZE-1) & PAGE_MASK, |
| <------> sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) { |
| <------><------> |
| <------><------>prom_printf("alloc_io_res(%s): cannot occupy\n", |
| <------><------> (res->name != NULL)? res->name: "???"); |
| <------><------>prom_halt(); |
| <------>} |
| |
| <------>pa &= PAGE_MASK; |
| <------>srmmu_mapiorange(bus, pa, res->start, resource_size(res)); |
| |
| <------>return (void __iomem *)(unsigned long)(res->start + offset); |
| } |
| |
| |
| |
| |
| static void _sparc_free_io(struct resource *res) |
| { |
| <------>unsigned long plen; |
| |
| <------>plen = resource_size(res); |
| <------>BUG_ON((plen & (PAGE_SIZE-1)) != 0); |
| <------>srmmu_unmapiorange(res->start, plen); |
| <------>release_resource(res); |
| } |
| |
| unsigned long sparc_dma_alloc_resource(struct device *dev, size_t len) |
| { |
| <------>struct resource *res; |
| |
| <------>res = kzalloc(sizeof(*res), GFP_KERNEL); |
| <------>if (!res) |
| <------><------>return 0; |
| <------>res->name = dev->of_node->full_name; |
| |
| <------>if (allocate_resource(&_sparc_dvma, res, len, _sparc_dvma.start, |
| <------><------><------> _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { |
| <------><------>printk("%s: cannot occupy 0x%zx", __func__, len); |
| <------><------>kfree(res); |
| <------><------>return 0; |
| <------>} |
| |
| <------>return res->start; |
| } |
| |
| bool sparc_dma_free_resource(void *cpu_addr, size_t size) |
| { |
| <------>unsigned long addr = (unsigned long)cpu_addr; |
| <------>struct resource *res; |
| |
| <------>res = lookup_resource(&_sparc_dvma, addr); |
| <------>if (!res) { |
| <------><------>printk("%s: cannot free %p\n", __func__, cpu_addr); |
| <------><------>return false; |
| <------>} |
| |
| <------>if ((addr & (PAGE_SIZE - 1)) != 0) { |
| <------><------>printk("%s: unaligned va %p\n", __func__, cpu_addr); |
| <------><------>return false; |
| <------>} |
| |
| <------>size = PAGE_ALIGN(size); |
| <------>if (resource_size(res) != size) { |
| <------><------>printk("%s: region 0x%lx asked 0x%zx\n", |
| <------><------><------>__func__, (long)resource_size(res), size); |
| <------><------>return false; |
| <------>} |
| |
| <------>release_resource(res); |
| <------>kfree(res); |
| <------>return true; |
| } |
| |
| #ifdef CONFIG_SBUS |
| |
| void sbus_set_sbus64(struct device *dev, int x) |
| { |
| <------>printk("sbus_set_sbus64: unsupported\n"); |
| } |
| EXPORT_SYMBOL(sbus_set_sbus64); |
| |
| static int __init sparc_register_ioport(void) |
| { |
| <------>register_proc_sparc_ioport(); |
| |
| <------>return 0; |
| } |
| |
| arch_initcall(sparc_register_ioport); |
| |
| #endif |
| |
| |
| |
| |
| |
| void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, |
| <------><------>gfp_t gfp, unsigned long attrs) |
| { |
| <------>unsigned long addr; |
| <------>void *va; |
| |
| <------>if (!size || size > 256 * 1024) |
| <------><------>return NULL; |
| |
| <------>size = PAGE_ALIGN(size); |
| <------>va = (void *) __get_free_pages(gfp | __GFP_ZERO, get_order(size)); |
| <------>if (!va) { |
| <------><------>printk("%s: no %zd pages\n", __func__, size >> PAGE_SHIFT); |
| <------><------>return NULL; |
| <------>} |
| |
| <------>addr = sparc_dma_alloc_resource(dev, size); |
| <------>if (!addr) |
| <------><------>goto err_nomem; |
| |
| <------>srmmu_mapiorange(0, virt_to_phys(va), addr, size); |
| |
| <------>*dma_handle = virt_to_phys(va); |
| <------>return (void *)addr; |
| |
| err_nomem: |
| <------>free_pages((unsigned long)va, get_order(size)); |
| <------>return NULL; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| void arch_dma_free(struct device *dev, size_t size, void *cpu_addr, |
| <------><------>dma_addr_t dma_addr, unsigned long attrs) |
| { |
| <------>size = PAGE_ALIGN(size); |
| |
| <------>if (!sparc_dma_free_resource(cpu_addr, size)) |
| <------><------>return; |
| |
| <------>dma_make_coherent(dma_addr, size); |
| <------>srmmu_unmapiorange((unsigned long)cpu_addr, size); |
| <------>free_pages((unsigned long)phys_to_virt(dma_addr), get_order(size)); |
| } |
| |
| |
| |
| void arch_sync_dma_for_cpu(phys_addr_t paddr, size_t size, |
| <------><------>enum dma_data_direction dir) |
| { |
| <------>if (dir != PCI_DMA_TODEVICE) |
| <------><------>dma_make_coherent(paddr, PAGE_ALIGN(size)); |
| } |
| |
| #ifdef CONFIG_PROC_FS |
| |
| static int sparc_io_proc_show(struct seq_file *m, void *v) |
| { |
| <------>struct resource *root = m->private, *r; |
| <------>const char *nm; |
| |
| <------>for (r = root->child; r != NULL; r = r->sibling) { |
| <------><------>if ((nm = r->name) == NULL) nm = "???"; |
| <------><------>seq_printf(m, "%016llx-%016llx: %s\n", |
| <------><------><------><------>(unsigned long long)r->start, |
| <------><------><------><------>(unsigned long long)r->end, nm); |
| <------>} |
| |
| <------>return 0; |
| } |
| #endif |
| |
| static void register_proc_sparc_ioport(void) |
| { |
| #ifdef CONFIG_PROC_FS |
| <------>proc_create_single_data("io_map", 0, NULL, sparc_io_proc_show, |
| <------><------><------>&sparc_iomap); |
| <------>proc_create_single_data("dvma_map", 0, NULL, sparc_io_proc_show, |
| <------><------><------>&_sparc_dvma); |
| #endif |
| } |
| |