xref: /openbmc/linux/arch/sparc/mm/io-unit.c (revision 263291fa)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
288278ca2SAdrian Bunk /*
31da177e4SLinus Torvalds  * io-unit.c:  IO-UNIT specific routines for memory management.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1997,1998 Jakub Jelinek    (jj@sunsite.mff.cuni.cz)
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/init.h>
101da177e4SLinus Torvalds #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/spinlock.h>
121da177e4SLinus Torvalds #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/bitops.h>
140a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
159dc69230SDavid S. Miller #include <linux/of.h>
16*263291faSRob Herring #include <linux/of_platform.h>
17*263291faSRob Herring #include <linux/platform_device.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <asm/io.h>
201da177e4SLinus Torvalds #include <asm/io-unit.h>
211da177e4SLinus Torvalds #include <asm/mxcc.h>
221da177e4SLinus Torvalds #include <asm/cacheflush.h>
231da177e4SLinus Torvalds #include <asm/tlbflush.h>
241da177e4SLinus Torvalds #include <asm/dma.h>
25d4accd60SDavid S. Miller #include <asm/oplib.h>
261da177e4SLinus Torvalds 
271918660bSSam Ravnborg #include "mm_32.h"
281918660bSSam Ravnborg 
291da177e4SLinus Torvalds /* #define IOUNIT_DEBUG */
301da177e4SLinus Torvalds #ifdef IOUNIT_DEBUG
311da177e4SLinus Torvalds #define IOD(x) printk(x)
321da177e4SLinus Torvalds #else
331da177e4SLinus Torvalds #define IOD(x) do { } while (0)
341da177e4SLinus Torvalds #endif
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define IOPERM        (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
371da177e4SLinus Torvalds #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
381da177e4SLinus Torvalds 
39255a69a9SChristoph Hellwig static const struct dma_map_ops iounit_dma_ops;
40255a69a9SChristoph Hellwig 
iounit_iommu_init(struct platform_device * op)41cd4cd730SGrant Likely static void __init iounit_iommu_init(struct platform_device *op)
421da177e4SLinus Torvalds {
431da177e4SLinus Torvalds 	struct iounit_struct *iounit;
441918660bSSam Ravnborg 	iopte_t __iomem *xpt;
451918660bSSam Ravnborg 	iopte_t __iomem *xptend;
461da177e4SLinus Torvalds 
47c80892d1SYan Burman 	iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
48d4accd60SDavid S. Miller 	if (!iounit) {
49d4accd60SDavid S. Miller 		prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
50d4accd60SDavid S. Miller 		prom_halt();
51d4accd60SDavid S. Miller 	}
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	iounit->limit[0] = IOUNIT_BMAP1_START;
541da177e4SLinus Torvalds 	iounit->limit[1] = IOUNIT_BMAP2_START;
551da177e4SLinus Torvalds 	iounit->limit[2] = IOUNIT_BMAPM_START;
561da177e4SLinus Torvalds 	iounit->limit[3] = IOUNIT_BMAPM_END;
571da177e4SLinus Torvalds 	iounit->rotor[1] = IOUNIT_BMAP2_START;
581da177e4SLinus Torvalds 	iounit->rotor[2] = IOUNIT_BMAPM_START;
591da177e4SLinus Torvalds 
60e0039348SDavid S. Miller 	xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
61e0039348SDavid S. Miller 	if (!xpt) {
62e0039348SDavid S. Miller 		prom_printf("SUN4D: Cannot map External Page Table.");
63e0039348SDavid S. Miller 		prom_halt();
641da177e4SLinus Torvalds 	}
651da177e4SLinus Torvalds 
66e0039348SDavid S. Miller 	op->dev.archdata.iommu = iounit;
671da177e4SLinus Torvalds 	iounit->page_table = xpt;
682f72ba43SRaymond Burns 	spin_lock_init(&iounit->lock);
691da177e4SLinus Torvalds 
701918660bSSam Ravnborg 	xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
711918660bSSam Ravnborg 	for (; xpt < xptend; xpt++)
721918660bSSam Ravnborg 		sbus_writel(0, xpt);
73255a69a9SChristoph Hellwig 
74255a69a9SChristoph Hellwig 	op->dev.dma_ops = &iounit_dma_ops;
751da177e4SLinus Torvalds }
761da177e4SLinus Torvalds 
iounit_init(void)77046e26a8SDavid S. Miller static int __init iounit_init(void)
78046e26a8SDavid S. Miller {
79046e26a8SDavid S. Miller 	extern void sun4d_init_sbi_irq(void);
80046e26a8SDavid S. Miller 	struct device_node *dp;
81046e26a8SDavid S. Miller 
82046e26a8SDavid S. Miller 	for_each_node_by_name(dp, "sbi") {
83cd4cd730SGrant Likely 		struct platform_device *op = of_find_device_by_node(dp);
84046e26a8SDavid S. Miller 
85046e26a8SDavid S. Miller 		iounit_iommu_init(op);
86046e26a8SDavid S. Miller 		of_propagate_archdata(op);
87046e26a8SDavid S. Miller 	}
88046e26a8SDavid S. Miller 
89046e26a8SDavid S. Miller 	sun4d_init_sbi_irq();
90046e26a8SDavid S. Miller 
91046e26a8SDavid S. Miller 	return 0;
92046e26a8SDavid S. Miller }
93046e26a8SDavid S. Miller 
94046e26a8SDavid S. Miller subsys_initcall(iounit_init);
95046e26a8SDavid S. Miller 
961da177e4SLinus Torvalds /* One has to hold iounit->lock to call this */
iounit_get_area(struct iounit_struct * iounit,unsigned long vaddr,int size)971da177e4SLinus Torvalds static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	int i, j, k, npages;
1001da177e4SLinus Torvalds 	unsigned long rotor, scan, limit;
1011da177e4SLinus Torvalds 	iopte_t iopte;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds         npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	/* A tiny bit of magic ingredience :) */
1061da177e4SLinus Torvalds 	switch (npages) {
1071da177e4SLinus Torvalds 	case 1: i = 0x0231; break;
1081da177e4SLinus Torvalds 	case 2: i = 0x0132; break;
1091da177e4SLinus Torvalds 	default: i = 0x0213; break;
1101da177e4SLinus Torvalds 	}
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds next:	j = (i & 15);
1151da177e4SLinus Torvalds 	rotor = iounit->rotor[j - 1];
1161da177e4SLinus Torvalds 	limit = iounit->limit[j];
1171da177e4SLinus Torvalds 	scan = rotor;
1181da177e4SLinus Torvalds nexti:	scan = find_next_zero_bit(iounit->bmap, limit, scan);
1191da177e4SLinus Torvalds 	if (scan + npages > limit) {
1201da177e4SLinus Torvalds 		if (limit != rotor) {
1211da177e4SLinus Torvalds 			limit = rotor;
1221da177e4SLinus Torvalds 			scan = iounit->limit[j - 1];
1231da177e4SLinus Torvalds 			goto nexti;
1241da177e4SLinus Torvalds 		}
1251da177e4SLinus Torvalds 		i >>= 4;
1261da177e4SLinus Torvalds 		if (!(i & 15))
1271da177e4SLinus Torvalds 			panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
1281da177e4SLinus Torvalds 		goto next;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 	for (k = 1, scan++; k < npages; k++)
1311da177e4SLinus Torvalds 		if (test_bit(scan++, iounit->bmap))
1321da177e4SLinus Torvalds 			goto nexti;
1331da177e4SLinus Torvalds 	iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
1341da177e4SLinus Torvalds 	scan -= npages;
1351da177e4SLinus Torvalds 	iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
1361da177e4SLinus Torvalds 	vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
1371da177e4SLinus Torvalds 	for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
1381da177e4SLinus Torvalds 		set_bit(scan, iounit->bmap);
1396e6e4187SSam Ravnborg 		sbus_writel(iopte_val(iopte), &iounit->page_table[scan]);
1401da177e4SLinus Torvalds 	}
1411da177e4SLinus Torvalds 	IOD(("%08lx\n", vaddr));
1421da177e4SLinus Torvalds 	return vaddr;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
iounit_map_page(struct device * dev,struct page * page,unsigned long offset,size_t len,enum dma_data_direction dir,unsigned long attrs)145ce65d36fSChristoph Hellwig static dma_addr_t iounit_map_page(struct device *dev, struct page *page,
146ce65d36fSChristoph Hellwig 		unsigned long offset, size_t len, enum dma_data_direction dir,
147ce65d36fSChristoph Hellwig 		unsigned long attrs)
1481da177e4SLinus Torvalds {
149ce65d36fSChristoph Hellwig 	void *vaddr = page_address(page) + offset;
150260489faSDavid S. Miller 	struct iounit_struct *iounit = dev->archdata.iommu;
1511da177e4SLinus Torvalds 	unsigned long ret, flags;
1521da177e4SLinus Torvalds 
153ce65d36fSChristoph Hellwig 	/* XXX So what is maxphys for us and how do drivers know it? */
154ce65d36fSChristoph Hellwig 	if (!len || len > 256 * 1024)
155ce65d36fSChristoph Hellwig 		return DMA_MAPPING_ERROR;
156ce65d36fSChristoph Hellwig 
1571da177e4SLinus Torvalds 	spin_lock_irqsave(&iounit->lock, flags);
1581da177e4SLinus Torvalds 	ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
1591da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iounit->lock, flags);
1601da177e4SLinus Torvalds 	return ret;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
iounit_map_sg(struct device * dev,struct scatterlist * sgl,int nents,enum dma_data_direction dir,unsigned long attrs)1631e2934adSChristoph Hellwig static int iounit_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
164ce65d36fSChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
1651da177e4SLinus Torvalds {
166260489faSDavid S. Miller 	struct iounit_struct *iounit = dev->archdata.iommu;
1671e2934adSChristoph Hellwig 	struct scatterlist *sg;
1681da177e4SLinus Torvalds 	unsigned long flags;
1691e2934adSChristoph Hellwig 	int i;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	/* FIXME: Cache some resolved pages - often several sg entries are to the same page */
1721da177e4SLinus Torvalds 	spin_lock_irqsave(&iounit->lock, flags);
1731e2934adSChristoph Hellwig 	for_each_sg(sgl, sg, nents, i) {
174aa83a26aSRobert Reif 		sg->dma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
175aa83a26aSRobert Reif 		sg->dma_length = sg->length;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iounit->lock, flags);
1781e2934adSChristoph Hellwig 	return nents;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
iounit_unmap_page(struct device * dev,dma_addr_t vaddr,size_t len,enum dma_data_direction dir,unsigned long attrs)181ce65d36fSChristoph Hellwig static void iounit_unmap_page(struct device *dev, dma_addr_t vaddr, size_t len,
182ce65d36fSChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
1831da177e4SLinus Torvalds {
184260489faSDavid S. Miller 	struct iounit_struct *iounit = dev->archdata.iommu;
1851da177e4SLinus Torvalds 	unsigned long flags;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	spin_lock_irqsave(&iounit->lock, flags);
1881da177e4SLinus Torvalds 	len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
1891da177e4SLinus Torvalds 	vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
1901da177e4SLinus Torvalds 	IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
1911da177e4SLinus Torvalds 	for (len += vaddr; vaddr < len; vaddr++)
1921da177e4SLinus Torvalds 		clear_bit(vaddr, iounit->bmap);
1931da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iounit->lock, flags);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
iounit_unmap_sg(struct device * dev,struct scatterlist * sgl,int nents,enum dma_data_direction dir,unsigned long attrs)1961e2934adSChristoph Hellwig static void iounit_unmap_sg(struct device *dev, struct scatterlist *sgl,
1971e2934adSChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
1981da177e4SLinus Torvalds {
199260489faSDavid S. Miller 	struct iounit_struct *iounit = dev->archdata.iommu;
2001e2934adSChristoph Hellwig 	unsigned long flags, vaddr, len;
2011e2934adSChristoph Hellwig 	struct scatterlist *sg;
2021e2934adSChristoph Hellwig 	int i;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	spin_lock_irqsave(&iounit->lock, flags);
2051e2934adSChristoph Hellwig 	for_each_sg(sgl, sg, nents, i) {
206aa83a26aSRobert Reif 		len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
207aa83a26aSRobert Reif 		vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
2081da177e4SLinus Torvalds 		IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
2091da177e4SLinus Torvalds 		for (len += vaddr; vaddr < len; vaddr++)
2101da177e4SLinus Torvalds 			clear_bit(vaddr, iounit->bmap);
2111da177e4SLinus Torvalds 	}
2121da177e4SLinus Torvalds 	spin_unlock_irqrestore(&iounit->lock, flags);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds #ifdef CONFIG_SBUS
iounit_alloc(struct device * dev,size_t len,dma_addr_t * dma_handle,gfp_t gfp,unsigned long attrs)216ce65d36fSChristoph Hellwig static void *iounit_alloc(struct device *dev, size_t len,
217ce65d36fSChristoph Hellwig 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
2181da177e4SLinus Torvalds {
2194b1c5df2SDavid S. Miller 	struct iounit_struct *iounit = dev->archdata.iommu;
220ce65d36fSChristoph Hellwig 	unsigned long va, addr, page, end, ret;
2211da177e4SLinus Torvalds 	pgprot_t dvma_prot;
2221918660bSSam Ravnborg 	iopte_t __iomem *iopte;
2231da177e4SLinus Torvalds 
224ce65d36fSChristoph Hellwig 	/* XXX So what is maxphys for us and how do drivers know it? */
225ce65d36fSChristoph Hellwig 	if (!len || len > 256 * 1024)
226ce65d36fSChristoph Hellwig 		return NULL;
227ce65d36fSChristoph Hellwig 
228ce65d36fSChristoph Hellwig 	len = PAGE_ALIGN(len);
229518a2f19SChristoph Hellwig 	va = __get_free_pages(gfp | __GFP_ZERO, get_order(len));
230ce65d36fSChristoph Hellwig 	if (!va)
231ce65d36fSChristoph Hellwig 		return NULL;
232ce65d36fSChristoph Hellwig 
233ce65d36fSChristoph Hellwig 	addr = ret = sparc_dma_alloc_resource(dev, len);
234ce65d36fSChristoph Hellwig 	if (!addr)
235ce65d36fSChristoph Hellwig 		goto out_free_pages;
236ce65d36fSChristoph Hellwig 	*dma_handle = addr;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
2391da177e4SLinus Torvalds 	end = PAGE_ALIGN((addr + len));
2401da177e4SLinus Torvalds 	while(addr < end) {
2411da177e4SLinus Torvalds 		page = va;
2421da177e4SLinus Torvalds 		{
2431da177e4SLinus Torvalds 			pmd_t *pmdp;
2441da177e4SLinus Torvalds 			pte_t *ptep;
2451da177e4SLinus Torvalds 			long i;
2461da177e4SLinus Torvalds 
247e05c7b1fSMike Rapoport 			pmdp = pmd_off_k(addr);
2487a19c361SHugh Dickins 			ptep = pte_offset_kernel(pmdp, addr);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 			set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 			i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
2531da177e4SLinus Torvalds 
2541918660bSSam Ravnborg 			iopte = iounit->page_table + i;
2556e6e4187SSam Ravnborg 			sbus_writel(iopte_val(MKIOPTE(__pa(page))), iopte);
2561da177e4SLinus Torvalds 		}
2571da177e4SLinus Torvalds 		addr += PAGE_SIZE;
2581da177e4SLinus Torvalds 		va += PAGE_SIZE;
2591da177e4SLinus Torvalds 	}
2601da177e4SLinus Torvalds 	flush_cache_all();
2611da177e4SLinus Torvalds 	flush_tlb_all();
2621da177e4SLinus Torvalds 
263ce65d36fSChristoph Hellwig 	return (void *)ret;
264ce65d36fSChristoph Hellwig 
265ce65d36fSChristoph Hellwig out_free_pages:
266ce65d36fSChristoph Hellwig 	free_pages(va, get_order(len));
267ce65d36fSChristoph Hellwig 	return NULL;
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds 
iounit_free(struct device * dev,size_t size,void * cpu_addr,dma_addr_t dma_addr,unsigned long attrs)270ce65d36fSChristoph Hellwig static void iounit_free(struct device *dev, size_t size, void *cpu_addr,
271ce65d36fSChristoph Hellwig 		dma_addr_t dma_addr, unsigned long attrs)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	/* XXX Somebody please fill this in */
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds #endif
2761da177e4SLinus Torvalds 
277ce65d36fSChristoph Hellwig static const struct dma_map_ops iounit_dma_ops = {
278d894d964SDavid S. Miller #ifdef CONFIG_SBUS
279ce65d36fSChristoph Hellwig 	.alloc			= iounit_alloc,
280ce65d36fSChristoph Hellwig 	.free			= iounit_free,
281d894d964SDavid S. Miller #endif
282ce65d36fSChristoph Hellwig 	.map_page		= iounit_map_page,
283ce65d36fSChristoph Hellwig 	.unmap_page		= iounit_unmap_page,
284ce65d36fSChristoph Hellwig 	.map_sg			= iounit_map_sg,
285ce65d36fSChristoph Hellwig 	.unmap_sg		= iounit_unmap_sg,
286d894d964SDavid S. Miller };
287