xref: /linux/arch/sparc/mm/iommu.c (revision 7e996890b88078011bfb55ce072712d464207dad)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * iommu.c:  IOMMU specific routines for memory management.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1995 David S. Miller  (davem@caip.rutgers.edu)
61da177e4SLinus Torvalds  * Copyright (C) 1995,2002 Pete Zaitcev     (zaitcev@yahoo.com)
71da177e4SLinus Torvalds  * Copyright (C) 1996 Eddie C. Dost    (ecd@skynet.be)
81da177e4SLinus Torvalds  * Copyright (C) 1997,1998 Jakub Jelinek    (jj@sunsite.mff.cuni.cz)
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/mm.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/highmem.h>	/* pte_offset_map => kmap_atomic */
16ce65d36fSChristoph Hellwig #include <linux/dma-mapping.h>
179dc69230SDavid S. Miller #include <linux/of.h>
189dc69230SDavid S. Miller #include <linux/of_device.h>
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <asm/pgalloc.h>
211da177e4SLinus Torvalds #include <asm/pgtable.h>
221da177e4SLinus Torvalds #include <asm/io.h>
231da177e4SLinus Torvalds #include <asm/mxcc.h>
241da177e4SLinus Torvalds #include <asm/mbus.h>
251da177e4SLinus Torvalds #include <asm/cacheflush.h>
261da177e4SLinus Torvalds #include <asm/tlbflush.h>
271da177e4SLinus Torvalds #include <asm/bitext.h>
281da177e4SLinus Torvalds #include <asm/iommu.h>
291da177e4SLinus Torvalds #include <asm/dma.h>
301da177e4SLinus Torvalds 
31e8c29c83SSam Ravnborg #include "mm_32.h"
32e8c29c83SSam Ravnborg 
331da177e4SLinus Torvalds /*
341da177e4SLinus Torvalds  * This can be sized dynamically, but we will do this
351da177e4SLinus Torvalds  * only when we have a guidance about actual I/O pressures.
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds #define IOMMU_RNGE	IOMMU_RNGE_256MB
381da177e4SLinus Torvalds #define IOMMU_START	0xF0000000
391da177e4SLinus Torvalds #define IOMMU_WINSIZE	(256*1024*1024U)
409a0ac1b6SAkinobu Mita #define IOMMU_NPTES	(IOMMU_WINSIZE/PAGE_SIZE)	/* 64K PTEs, 256KB */
411da177e4SLinus Torvalds #define IOMMU_ORDER	6				/* 4096 * (1<<6) */
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds static int viking_flush;
441da177e4SLinus Torvalds /* viking.S */
451da177e4SLinus Torvalds extern void viking_flush_page(unsigned long page);
461da177e4SLinus Torvalds extern void viking_mxcc_flush_page(unsigned long page);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Values precomputed according to CPU type.
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds static unsigned int ioperm_noc;		/* Consistent mapping iopte flags */
521da177e4SLinus Torvalds static pgprot_t dvma_prot;		/* Consistent mapping pte flags */
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #define IOPERM        (IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID)
551da177e4SLinus Torvalds #define MKIOPTE(pfn, perm) (((((pfn)<<8) & IOPTE_PAGE) | (perm)) & ~IOPTE_WAZ)
561da177e4SLinus Torvalds 
57cd4cd730SGrant Likely static void __init sbus_iommu_init(struct platform_device *op)
581da177e4SLinus Torvalds {
591da177e4SLinus Torvalds 	struct iommu_struct *iommu;
60e0039348SDavid S. Miller 	unsigned int impl, vers;
611da177e4SLinus Torvalds 	unsigned long *bitmap;
62f977ea49SSam Ravnborg 	unsigned long control;
63f977ea49SSam Ravnborg 	unsigned long base;
64e0039348SDavid S. Miller 	unsigned long tmp;
65e0039348SDavid S. Miller 
6671cd03b0SJulia Lawall 	iommu = kmalloc(sizeof(struct iommu_struct), GFP_KERNEL);
671da177e4SLinus Torvalds 	if (!iommu) {
681da177e4SLinus Torvalds 		prom_printf("Unable to allocate iommu structure\n");
691da177e4SLinus Torvalds 		prom_halt();
701da177e4SLinus Torvalds 	}
71e0039348SDavid S. Miller 
72046e26a8SDavid S. Miller 	iommu->regs = of_ioremap(&op->resource[0], 0, PAGE_SIZE * 3,
73e0039348SDavid S. Miller 				 "iommu_regs");
741da177e4SLinus Torvalds 	if (!iommu->regs) {
751da177e4SLinus Torvalds 		prom_printf("Cannot map IOMMU registers\n");
761da177e4SLinus Torvalds 		prom_halt();
771da177e4SLinus Torvalds 	}
78f977ea49SSam Ravnborg 
79f977ea49SSam Ravnborg 	control = sbus_readl(&iommu->regs->control);
80f977ea49SSam Ravnborg 	impl = (control & IOMMU_CTRL_IMPL) >> 28;
81f977ea49SSam Ravnborg 	vers = (control & IOMMU_CTRL_VERS) >> 24;
82f977ea49SSam Ravnborg 	control &= ~(IOMMU_CTRL_RNGE);
83f977ea49SSam Ravnborg 	control |= (IOMMU_RNGE_256MB | IOMMU_CTRL_ENAB);
84f977ea49SSam Ravnborg 	sbus_writel(control, &iommu->regs->control);
85f977ea49SSam Ravnborg 
861da177e4SLinus Torvalds 	iommu_invalidate(iommu->regs);
871da177e4SLinus Torvalds 	iommu->start = IOMMU_START;
881da177e4SLinus Torvalds 	iommu->end = 0xffffffff;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* Allocate IOMMU page table */
911da177e4SLinus Torvalds 	/* Stupid alignment constraints give me a headache.
921da177e4SLinus Torvalds 	   We need 256K or 512K or 1M or 2M area aligned to
931da177e4SLinus Torvalds            its size and current gfp will fortunately give
941da177e4SLinus Torvalds            it to us. */
951da177e4SLinus Torvalds         tmp = __get_free_pages(GFP_KERNEL, IOMMU_ORDER);
961da177e4SLinus Torvalds 	if (!tmp) {
975da444aaSAkinobu Mita 		prom_printf("Unable to allocate iommu table [0x%lx]\n",
981da177e4SLinus Torvalds 			    IOMMU_NPTES * sizeof(iopte_t));
991da177e4SLinus Torvalds 		prom_halt();
1001da177e4SLinus Torvalds 	}
1011da177e4SLinus Torvalds 	iommu->page_table = (iopte_t *)tmp;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	/* Initialize new table. */
1041da177e4SLinus Torvalds 	memset(iommu->page_table, 0, IOMMU_NPTES*sizeof(iopte_t));
1051da177e4SLinus Torvalds 	flush_cache_all();
1061da177e4SLinus Torvalds 	flush_tlb_all();
107f977ea49SSam Ravnborg 
108f977ea49SSam Ravnborg 	base = __pa((unsigned long)iommu->page_table) >> 4;
109f977ea49SSam Ravnborg 	sbus_writel(base, &iommu->regs->base);
1101da177e4SLinus Torvalds 	iommu_invalidate(iommu->regs);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	bitmap = kmalloc(IOMMU_NPTES>>3, GFP_KERNEL);
1131da177e4SLinus Torvalds 	if (!bitmap) {
1141da177e4SLinus Torvalds 		prom_printf("Unable to allocate iommu bitmap [%d]\n",
1151da177e4SLinus Torvalds 			    (int)(IOMMU_NPTES>>3));
1161da177e4SLinus Torvalds 		prom_halt();
1171da177e4SLinus Torvalds 	}
1181da177e4SLinus Torvalds 	bit_map_init(&iommu->usemap, bitmap, IOMMU_NPTES);
1191da177e4SLinus Torvalds 	/* To be coherent on HyperSparc, the page color of DVMA
1201da177e4SLinus Torvalds 	 * and physical addresses must match.
1211da177e4SLinus Torvalds 	 */
1221da177e4SLinus Torvalds 	if (srmmu_modtype == HyperSparc)
1231da177e4SLinus Torvalds 		iommu->usemap.num_colors = vac_cache_size >> PAGE_SHIFT;
1241da177e4SLinus Torvalds 	else
1251da177e4SLinus Torvalds 		iommu->usemap.num_colors = 1;
1261da177e4SLinus Torvalds 
127046e26a8SDavid S. Miller 	printk(KERN_INFO "IOMMU: impl %d vers %d table 0x%p[%d B] map [%d b]\n",
1281da177e4SLinus Torvalds 	       impl, vers, iommu->page_table,
1291da177e4SLinus Torvalds 	       (int)(IOMMU_NPTES*sizeof(iopte_t)), (int)IOMMU_NPTES);
1301da177e4SLinus Torvalds 
131e0039348SDavid S. Miller 	op->dev.archdata.iommu = iommu;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
134046e26a8SDavid S. Miller static int __init iommu_init(void)
135046e26a8SDavid S. Miller {
136046e26a8SDavid S. Miller 	struct device_node *dp;
137046e26a8SDavid S. Miller 
138046e26a8SDavid S. Miller 	for_each_node_by_name(dp, "iommu") {
139cd4cd730SGrant Likely 		struct platform_device *op = of_find_device_by_node(dp);
140046e26a8SDavid S. Miller 
141046e26a8SDavid S. Miller 		sbus_iommu_init(op);
142046e26a8SDavid S. Miller 		of_propagate_archdata(op);
143046e26a8SDavid S. Miller 	}
144046e26a8SDavid S. Miller 
145046e26a8SDavid S. Miller 	return 0;
146046e26a8SDavid S. Miller }
147046e26a8SDavid S. Miller 
148046e26a8SDavid S. Miller subsys_initcall(iommu_init);
149046e26a8SDavid S. Miller 
1501da177e4SLinus Torvalds /* Flush the iotlb entries to ram. */
1511da177e4SLinus Torvalds /* This could be better if we didn't have to flush whole pages. */
1521da177e4SLinus Torvalds static void iommu_flush_iotlb(iopte_t *iopte, unsigned int niopte)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	unsigned long start;
1551da177e4SLinus Torvalds 	unsigned long end;
1561da177e4SLinus Torvalds 
1573185d4d2SBob Breuer 	start = (unsigned long)iopte;
1581da177e4SLinus Torvalds 	end = PAGE_ALIGN(start + niopte*sizeof(iopte_t));
1593185d4d2SBob Breuer 	start &= PAGE_MASK;
1601da177e4SLinus Torvalds 	if (viking_mxcc_present) {
1611da177e4SLinus Torvalds 		while(start < end) {
1621da177e4SLinus Torvalds 			viking_mxcc_flush_page(start);
1631da177e4SLinus Torvalds 			start += PAGE_SIZE;
1641da177e4SLinus Torvalds 		}
1651da177e4SLinus Torvalds 	} else if (viking_flush) {
1661da177e4SLinus Torvalds 		while(start < end) {
1671da177e4SLinus Torvalds 			viking_flush_page(start);
1681da177e4SLinus Torvalds 			start += PAGE_SIZE;
1691da177e4SLinus Torvalds 		}
1701da177e4SLinus Torvalds 	} else {
1711da177e4SLinus Torvalds 		while(start < end) {
1721da177e4SLinus Torvalds 			__flush_page_to_ram(start);
1731da177e4SLinus Torvalds 			start += PAGE_SIZE;
1741da177e4SLinus Torvalds 		}
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
178b8205942SChristoph Hellwig static u32 iommu_get_one(struct device *dev, phys_addr_t paddr, int npages)
1791da177e4SLinus Torvalds {
180260489faSDavid S. Miller 	struct iommu_struct *iommu = dev->archdata.iommu;
1811da177e4SLinus Torvalds 	int ioptex;
1821da177e4SLinus Torvalds 	iopte_t *iopte, *iopte0;
1831da177e4SLinus Torvalds 	unsigned int busa, busa0;
184b8205942SChristoph Hellwig 	unsigned long pfn = __phys_to_pfn(paddr);
1851da177e4SLinus Torvalds 	int i;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	/* page color = pfn of page */
188b8205942SChristoph Hellwig 	ioptex = bit_map_string_get(&iommu->usemap, npages, pfn);
1891da177e4SLinus Torvalds 	if (ioptex < 0)
1901da177e4SLinus Torvalds 		panic("iommu out");
1911da177e4SLinus Torvalds 	busa0 = iommu->start + (ioptex << PAGE_SHIFT);
1921da177e4SLinus Torvalds 	iopte0 = &iommu->page_table[ioptex];
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	busa = busa0;
1951da177e4SLinus Torvalds 	iopte = iopte0;
1961da177e4SLinus Torvalds 	for (i = 0; i < npages; i++) {
197b8205942SChristoph Hellwig 		iopte_val(*iopte) = MKIOPTE(pfn, IOPERM);
1981da177e4SLinus Torvalds 		iommu_invalidate_page(iommu->regs, busa);
1991da177e4SLinus Torvalds 		busa += PAGE_SIZE;
2001da177e4SLinus Torvalds 		iopte++;
201b8205942SChristoph Hellwig 		pfn++;
2021da177e4SLinus Torvalds 	}
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	iommu_flush_iotlb(iopte0, npages);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	return busa0;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
209ce65d36fSChristoph Hellwig static dma_addr_t __sbus_iommu_map_page(struct device *dev, struct page *page,
2108668b38cSChristoph Hellwig 		unsigned long offset, size_t len, bool per_page_flush)
2111da177e4SLinus Torvalds {
212*7e996890SChristoph Hellwig 	phys_addr_t paddr = page_to_phys(page) + offset;
213*7e996890SChristoph Hellwig 	unsigned long off = paddr & ~PAGE_MASK;
214ce65d36fSChristoph Hellwig 	unsigned long npages = (off + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
2151da177e4SLinus Torvalds 
216ce65d36fSChristoph Hellwig 	/* XXX So what is maxphys for us and how do drivers know it? */
217ce65d36fSChristoph Hellwig 	if (!len || len > 256 * 1024)
218ce65d36fSChristoph Hellwig 		return DMA_MAPPING_ERROR;
2198668b38cSChristoph Hellwig 
220*7e996890SChristoph Hellwig 	if (per_page_flush && !PageHighMem(page)) {
221*7e996890SChristoph Hellwig 		unsigned long vaddr, p;
2228668b38cSChristoph Hellwig 
223*7e996890SChristoph Hellwig 		vaddr = (unsigned long)page_address(page) + offset;
224*7e996890SChristoph Hellwig 		for (p = vaddr & PAGE_MASK; p < vaddr + len; p += PAGE_SIZE)
2258668b38cSChristoph Hellwig 			flush_page_for_dma(p);
2268668b38cSChristoph Hellwig 	}
2278668b38cSChristoph Hellwig 
228*7e996890SChristoph Hellwig 	return iommu_get_one(dev, paddr, npages) + off;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
231ce65d36fSChristoph Hellwig static dma_addr_t sbus_iommu_map_page_gflush(struct device *dev,
232ce65d36fSChristoph Hellwig 		struct page *page, unsigned long offset, size_t len,
233ce65d36fSChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	flush_page_for_dma(0);
2368668b38cSChristoph Hellwig 	return __sbus_iommu_map_page(dev, page, offset, len, false);
2371da177e4SLinus Torvalds }
2381da177e4SLinus Torvalds 
239ce65d36fSChristoph Hellwig static dma_addr_t sbus_iommu_map_page_pflush(struct device *dev,
240ce65d36fSChristoph Hellwig 		struct page *page, unsigned long offset, size_t len,
241ce65d36fSChristoph Hellwig 		enum dma_data_direction dir, unsigned long attrs)
2421da177e4SLinus Torvalds {
2438668b38cSChristoph Hellwig 	return __sbus_iommu_map_page(dev, page, offset, len, true);
244ce65d36fSChristoph Hellwig }
245ce65d36fSChristoph Hellwig 
246ff5cbec0SChristoph Hellwig static int __sbus_iommu_map_sg(struct device *dev, struct scatterlist *sgl,
247ff5cbec0SChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs,
248ff5cbec0SChristoph Hellwig 		bool per_page_flush)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	unsigned long page, oldpage = 0;
2516c503d0dSChristoph Hellwig 	struct scatterlist *sg;
2526c503d0dSChristoph Hellwig 	int i, j, n;
2531da177e4SLinus Torvalds 
2546c503d0dSChristoph Hellwig 	for_each_sg(sgl, sg, nents, j) {
2551da177e4SLinus Torvalds 		n = (sg->length + sg->offset + PAGE_SIZE-1) >> PAGE_SHIFT;
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds 		/*
2581da177e4SLinus Torvalds 		 * We expect unmapped highmem pages to be not in the cache.
2591da177e4SLinus Torvalds 		 * XXX Is this a good assumption?
2601da177e4SLinus Torvalds 		 * XXX What if someone else unmaps it here and races us?
2611da177e4SLinus Torvalds 		 */
262ff5cbec0SChristoph Hellwig 		if (per_page_flush && !PageHighMem(sg_page(sg))) {
263031abf0bSChristoph Hellwig 			page = (unsigned long)page_address(sg_page(sg));
2641da177e4SLinus Torvalds 			for (i = 0; i < n; i++) {
2651da177e4SLinus Torvalds 				if (page != oldpage) {	/* Already flushed? */
2661da177e4SLinus Torvalds 					flush_page_for_dma(page);
2671da177e4SLinus Torvalds 					oldpage = page;
2681da177e4SLinus Torvalds 				}
2691da177e4SLinus Torvalds 				page += PAGE_SIZE;
2701da177e4SLinus Torvalds 			}
2711da177e4SLinus Torvalds 		}
2721da177e4SLinus Torvalds 
273b8205942SChristoph Hellwig 		sg->dma_address = iommu_get_one(dev, sg_phys(sg), n) + sg->offset;
274aa83a26aSRobert Reif 		sg->dma_length = sg->length;
2751da177e4SLinus Torvalds 	}
276ce65d36fSChristoph Hellwig 
2776c503d0dSChristoph Hellwig 	return nents;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
280ff5cbec0SChristoph Hellwig static int sbus_iommu_map_sg_gflush(struct device *dev, struct scatterlist *sgl,
281ff5cbec0SChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
282ff5cbec0SChristoph Hellwig {
283ff5cbec0SChristoph Hellwig 	flush_page_for_dma(0);
284ff5cbec0SChristoph Hellwig 	return __sbus_iommu_map_sg(dev, sgl, nents, dir, attrs, false);
285ff5cbec0SChristoph Hellwig }
286ff5cbec0SChristoph Hellwig 
287ff5cbec0SChristoph Hellwig static int sbus_iommu_map_sg_pflush(struct device *dev, struct scatterlist *sgl,
288ff5cbec0SChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
289ff5cbec0SChristoph Hellwig {
290ff5cbec0SChristoph Hellwig 	return __sbus_iommu_map_sg(dev, sgl, nents, dir, attrs, true);
291ff5cbec0SChristoph Hellwig }
292ff5cbec0SChristoph Hellwig 
293f25b23bcSChristoph Hellwig static void sbus_iommu_unmap_page(struct device *dev, dma_addr_t dma_addr,
294f25b23bcSChristoph Hellwig 		size_t len, enum dma_data_direction dir, unsigned long attrs)
2951da177e4SLinus Torvalds {
296260489faSDavid S. Miller 	struct iommu_struct *iommu = dev->archdata.iommu;
297f25b23bcSChristoph Hellwig 	unsigned int busa = dma_addr & PAGE_MASK;
298f25b23bcSChristoph Hellwig 	unsigned long off = dma_addr & ~PAGE_MASK;
299f25b23bcSChristoph Hellwig 	unsigned int npages = (off + len + PAGE_SIZE-1) >> PAGE_SHIFT;
300f25b23bcSChristoph Hellwig 	unsigned int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
301f25b23bcSChristoph Hellwig 	unsigned int i;
3021da177e4SLinus Torvalds 
3031ae61388SEric Sesterhenn 	BUG_ON(busa < iommu->start);
3041da177e4SLinus Torvalds 	for (i = 0; i < npages; i++) {
3051da177e4SLinus Torvalds 		iopte_val(iommu->page_table[ioptex + i]) = 0;
3061da177e4SLinus Torvalds 		iommu_invalidate_page(iommu->regs, busa);
3071da177e4SLinus Torvalds 		busa += PAGE_SIZE;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 	bit_map_clear(&iommu->usemap, ioptex, npages);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3126c503d0dSChristoph Hellwig static void sbus_iommu_unmap_sg(struct device *dev, struct scatterlist *sgl,
3136c503d0dSChristoph Hellwig 		int nents, enum dma_data_direction dir, unsigned long attrs)
3141da177e4SLinus Torvalds {
3156c503d0dSChristoph Hellwig 	struct scatterlist *sg;
316a7fce1f7SChristoph Hellwig 	int i;
3171da177e4SLinus Torvalds 
3186c503d0dSChristoph Hellwig 	for_each_sg(sgl, sg, nents, i) {
319a7fce1f7SChristoph Hellwig 		sbus_iommu_unmap_page(dev, sg->dma_address, sg->length, dir,
320a7fce1f7SChristoph Hellwig 				attrs);
321aa83a26aSRobert Reif 		sg->dma_address = 0x21212121;
3221da177e4SLinus Torvalds 	}
3231da177e4SLinus Torvalds }
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds #ifdef CONFIG_SBUS
326ce65d36fSChristoph Hellwig static void *sbus_iommu_alloc(struct device *dev, size_t len,
327ce65d36fSChristoph Hellwig 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
3281da177e4SLinus Torvalds {
3294b1c5df2SDavid S. Miller 	struct iommu_struct *iommu = dev->archdata.iommu;
330ce65d36fSChristoph Hellwig 	unsigned long va, addr, page, end, ret;
3311da177e4SLinus Torvalds 	iopte_t *iopte = iommu->page_table;
3321da177e4SLinus Torvalds 	iopte_t *first;
3331da177e4SLinus Torvalds 	int ioptex;
3341da177e4SLinus Torvalds 
335ce65d36fSChristoph Hellwig 	/* XXX So what is maxphys for us and how do drivers know it? */
336ce65d36fSChristoph Hellwig 	if (!len || len > 256 * 1024)
337ce65d36fSChristoph Hellwig 		return NULL;
338ce65d36fSChristoph Hellwig 
339ce65d36fSChristoph Hellwig 	len = PAGE_ALIGN(len);
340518a2f19SChristoph Hellwig 	va = __get_free_pages(gfp | __GFP_ZERO, get_order(len));
341ce65d36fSChristoph Hellwig 	if (va == 0)
342ce65d36fSChristoph Hellwig 		return NULL;
343ce65d36fSChristoph Hellwig 
344ce65d36fSChristoph Hellwig 	addr = ret = sparc_dma_alloc_resource(dev, len);
345ce65d36fSChristoph Hellwig 	if (!addr)
346ce65d36fSChristoph Hellwig 		goto out_free_pages;
347ce65d36fSChristoph Hellwig 
3481ae61388SEric Sesterhenn 	BUG_ON((va & ~PAGE_MASK) != 0);
3491ae61388SEric Sesterhenn 	BUG_ON((addr & ~PAGE_MASK) != 0);
3501ae61388SEric Sesterhenn 	BUG_ON((len & ~PAGE_MASK) != 0);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	/* page color = physical address */
3531da177e4SLinus Torvalds 	ioptex = bit_map_string_get(&iommu->usemap, len >> PAGE_SHIFT,
3541da177e4SLinus Torvalds 		addr >> PAGE_SHIFT);
3551da177e4SLinus Torvalds 	if (ioptex < 0)
3561da177e4SLinus Torvalds 		panic("iommu out");
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	iopte += ioptex;
3591da177e4SLinus Torvalds 	first = iopte;
3601da177e4SLinus Torvalds 	end = addr + len;
3611da177e4SLinus Torvalds 	while(addr < end) {
3621da177e4SLinus Torvalds 		page = va;
3631da177e4SLinus Torvalds 		{
3641da177e4SLinus Torvalds 			pgd_t *pgdp;
3651da177e4SLinus Torvalds 			pmd_t *pmdp;
3661da177e4SLinus Torvalds 			pte_t *ptep;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 			if (viking_mxcc_present)
3691da177e4SLinus Torvalds 				viking_mxcc_flush_page(page);
3701da177e4SLinus Torvalds 			else if (viking_flush)
3711da177e4SLinus Torvalds 				viking_flush_page(page);
3721da177e4SLinus Torvalds 			else
3731da177e4SLinus Torvalds 				__flush_page_to_ram(page);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 			pgdp = pgd_offset(&init_mm, addr);
3761da177e4SLinus Torvalds 			pmdp = pmd_offset(pgdp, addr);
3771da177e4SLinus Torvalds 			ptep = pte_offset_map(pmdp, addr);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 			set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
3801da177e4SLinus Torvalds 		}
3811da177e4SLinus Torvalds 		iopte_val(*iopte++) =
3821da177e4SLinus Torvalds 		    MKIOPTE(page_to_pfn(virt_to_page(page)), ioperm_noc);
3831da177e4SLinus Torvalds 		addr += PAGE_SIZE;
3841da177e4SLinus Torvalds 		va += PAGE_SIZE;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 	/* P3: why do we need this?
3871da177e4SLinus Torvalds 	 *
3881da177e4SLinus Torvalds 	 * DAVEM: Because there are several aspects, none of which
3891da177e4SLinus Torvalds 	 *        are handled by a single interface.  Some cpus are
3901da177e4SLinus Torvalds 	 *        completely not I/O DMA coherent, and some have
3911da177e4SLinus Torvalds 	 *        virtually indexed caches.  The driver DMA flushing
3921da177e4SLinus Torvalds 	 *        methods handle the former case, but here during
3931da177e4SLinus Torvalds 	 *        IOMMU page table modifications, and usage of non-cacheable
3941da177e4SLinus Torvalds 	 *        cpu mappings of pages potentially in the cpu caches, we have
3951da177e4SLinus Torvalds 	 *        to handle the latter case as well.
3961da177e4SLinus Torvalds 	 */
3971da177e4SLinus Torvalds 	flush_cache_all();
3981da177e4SLinus Torvalds 	iommu_flush_iotlb(first, len >> PAGE_SHIFT);
3991da177e4SLinus Torvalds 	flush_tlb_all();
4001da177e4SLinus Torvalds 	iommu_invalidate(iommu->regs);
4011da177e4SLinus Torvalds 
402ce65d36fSChristoph Hellwig 	*dma_handle = iommu->start + (ioptex << PAGE_SHIFT);
403ce65d36fSChristoph Hellwig 	return (void *)ret;
404ce65d36fSChristoph Hellwig 
405ce65d36fSChristoph Hellwig out_free_pages:
406ce65d36fSChristoph Hellwig 	free_pages(va, get_order(len));
407ce65d36fSChristoph Hellwig 	return NULL;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
410ce65d36fSChristoph Hellwig static void sbus_iommu_free(struct device *dev, size_t len, void *cpu_addr,
411ce65d36fSChristoph Hellwig 			       dma_addr_t busa, unsigned long attrs)
4121da177e4SLinus Torvalds {
4134b1c5df2SDavid S. Miller 	struct iommu_struct *iommu = dev->archdata.iommu;
4141da177e4SLinus Torvalds 	iopte_t *iopte = iommu->page_table;
415ce65d36fSChristoph Hellwig 	struct page *page = virt_to_page(cpu_addr);
4161da177e4SLinus Torvalds 	int ioptex = (busa - iommu->start) >> PAGE_SHIFT;
417ce65d36fSChristoph Hellwig 	unsigned long end;
418ce65d36fSChristoph Hellwig 
419ce65d36fSChristoph Hellwig 	if (!sparc_dma_free_resource(cpu_addr, len))
420ce65d36fSChristoph Hellwig 		return;
4211da177e4SLinus Torvalds 
4221ae61388SEric Sesterhenn 	BUG_ON((busa & ~PAGE_MASK) != 0);
4231ae61388SEric Sesterhenn 	BUG_ON((len & ~PAGE_MASK) != 0);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	iopte += ioptex;
4261da177e4SLinus Torvalds 	end = busa + len;
4271da177e4SLinus Torvalds 	while (busa < end) {
4281da177e4SLinus Torvalds 		iopte_val(*iopte++) = 0;
4291da177e4SLinus Torvalds 		busa += PAGE_SIZE;
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds 	flush_tlb_all();
4321da177e4SLinus Torvalds 	iommu_invalidate(iommu->regs);
4331da177e4SLinus Torvalds 	bit_map_clear(&iommu->usemap, ioptex, len >> PAGE_SHIFT);
434ce65d36fSChristoph Hellwig 
435ce65d36fSChristoph Hellwig 	__free_pages(page, get_order(len));
4361da177e4SLinus Torvalds }
4371da177e4SLinus Torvalds #endif
4381da177e4SLinus Torvalds 
439ce65d36fSChristoph Hellwig static const struct dma_map_ops sbus_iommu_dma_gflush_ops = {
440d894d964SDavid S. Miller #ifdef CONFIG_SBUS
441ce65d36fSChristoph Hellwig 	.alloc			= sbus_iommu_alloc,
442ce65d36fSChristoph Hellwig 	.free			= sbus_iommu_free,
443d894d964SDavid S. Miller #endif
444ce65d36fSChristoph Hellwig 	.map_page		= sbus_iommu_map_page_gflush,
445ce65d36fSChristoph Hellwig 	.unmap_page		= sbus_iommu_unmap_page,
446ce65d36fSChristoph Hellwig 	.map_sg			= sbus_iommu_map_sg_gflush,
447ce65d36fSChristoph Hellwig 	.unmap_sg		= sbus_iommu_unmap_sg,
448d894d964SDavid S. Miller };
449d894d964SDavid S. Miller 
450ce65d36fSChristoph Hellwig static const struct dma_map_ops sbus_iommu_dma_pflush_ops = {
451d894d964SDavid S. Miller #ifdef CONFIG_SBUS
452ce65d36fSChristoph Hellwig 	.alloc			= sbus_iommu_alloc,
453ce65d36fSChristoph Hellwig 	.free			= sbus_iommu_free,
454d894d964SDavid S. Miller #endif
455ce65d36fSChristoph Hellwig 	.map_page		= sbus_iommu_map_page_pflush,
456ce65d36fSChristoph Hellwig 	.unmap_page		= sbus_iommu_unmap_page,
457ce65d36fSChristoph Hellwig 	.map_sg			= sbus_iommu_map_sg_pflush,
458ce65d36fSChristoph Hellwig 	.unmap_sg		= sbus_iommu_unmap_sg,
459d894d964SDavid S. Miller };
460d894d964SDavid S. Miller 
4611da177e4SLinus Torvalds void __init ld_mmu_iommu(void)
4621da177e4SLinus Torvalds {
4635d83d666SDavid S. Miller 	if (flush_page_for_dma_global) {
4641da177e4SLinus Torvalds 		/* flush_page_for_dma flushes everything, no matter of what page is it */
465ce65d36fSChristoph Hellwig 		dma_ops = &sbus_iommu_dma_gflush_ops;
4661da177e4SLinus Torvalds 	} else {
467ce65d36fSChristoph Hellwig 		dma_ops = &sbus_iommu_dma_pflush_ops;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	if (viking_mxcc_present || srmmu_modtype == HyperSparc) {
4711da177e4SLinus Torvalds 		dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
4721da177e4SLinus Torvalds 		ioperm_noc = IOPTE_CACHE | IOPTE_WRITE | IOPTE_VALID;
4731da177e4SLinus Torvalds 	} else {
4741da177e4SLinus Torvalds 		dvma_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV);
4751da177e4SLinus Torvalds 		ioperm_noc = IOPTE_WRITE | IOPTE_VALID;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds }
478