xref: /openbmc/linux/arch/sparc/mm/io-unit.c (revision 9d4fa1a1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * io-unit.c:  IO-UNIT specific routines for memory management.
4  *
5  * Copyright (C) 1997,1998 Jakub Jelinek    (jj@sunsite.mff.cuni.cz)
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/mm.h>
13 #include <linux/highmem.h>	/* pte_offset_map => kmap_atomic */
14 #include <linux/bitops.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 
19 #include <asm/pgalloc.h>
20 #include <asm/pgtable.h>
21 #include <asm/io.h>
22 #include <asm/io-unit.h>
23 #include <asm/mxcc.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
26 #include <asm/dma.h>
27 #include <asm/oplib.h>
28 
29 #include "mm_32.h"
30 
31 /* #define IOUNIT_DEBUG */
32 #ifdef IOUNIT_DEBUG
33 #define IOD(x) printk(x)
34 #else
35 #define IOD(x) do { } while (0)
36 #endif
37 
38 #define IOPERM        (IOUPTE_CACHE | IOUPTE_WRITE | IOUPTE_VALID)
39 #define MKIOPTE(phys) __iopte((((phys)>>4) & IOUPTE_PAGE) | IOPERM)
40 
41 static const struct dma_map_ops iounit_dma_ops;
42 
43 static void __init iounit_iommu_init(struct platform_device *op)
44 {
45 	struct iounit_struct *iounit;
46 	iopte_t __iomem *xpt;
47 	iopte_t __iomem *xptend;
48 
49 	iounit = kzalloc(sizeof(struct iounit_struct), GFP_ATOMIC);
50 	if (!iounit) {
51 		prom_printf("SUN4D: Cannot alloc iounit, halting.\n");
52 		prom_halt();
53 	}
54 
55 	iounit->limit[0] = IOUNIT_BMAP1_START;
56 	iounit->limit[1] = IOUNIT_BMAP2_START;
57 	iounit->limit[2] = IOUNIT_BMAPM_START;
58 	iounit->limit[3] = IOUNIT_BMAPM_END;
59 	iounit->rotor[1] = IOUNIT_BMAP2_START;
60 	iounit->rotor[2] = IOUNIT_BMAPM_START;
61 
62 	xpt = of_ioremap(&op->resource[2], 0, PAGE_SIZE * 16, "XPT");
63 	if (!xpt) {
64 		prom_printf("SUN4D: Cannot map External Page Table.");
65 		prom_halt();
66 	}
67 
68 	op->dev.archdata.iommu = iounit;
69 	iounit->page_table = xpt;
70 	spin_lock_init(&iounit->lock);
71 
72 	xptend = iounit->page_table + (16 * PAGE_SIZE) / sizeof(iopte_t);
73 	for (; xpt < xptend; xpt++)
74 		sbus_writel(0, xpt);
75 
76 	op->dev.dma_ops = &iounit_dma_ops;
77 }
78 
79 static int __init iounit_init(void)
80 {
81 	extern void sun4d_init_sbi_irq(void);
82 	struct device_node *dp;
83 
84 	for_each_node_by_name(dp, "sbi") {
85 		struct platform_device *op = of_find_device_by_node(dp);
86 
87 		iounit_iommu_init(op);
88 		of_propagate_archdata(op);
89 	}
90 
91 	sun4d_init_sbi_irq();
92 
93 	return 0;
94 }
95 
96 subsys_initcall(iounit_init);
97 
98 /* One has to hold iounit->lock to call this */
99 static unsigned long iounit_get_area(struct iounit_struct *iounit, unsigned long vaddr, int size)
100 {
101 	int i, j, k, npages;
102 	unsigned long rotor, scan, limit;
103 	iopte_t iopte;
104 
105         npages = ((vaddr & ~PAGE_MASK) + size + (PAGE_SIZE-1)) >> PAGE_SHIFT;
106 
107 	/* A tiny bit of magic ingredience :) */
108 	switch (npages) {
109 	case 1: i = 0x0231; break;
110 	case 2: i = 0x0132; break;
111 	default: i = 0x0213; break;
112 	}
113 
114 	IOD(("iounit_get_area(%08lx,%d[%d])=", vaddr, size, npages));
115 
116 next:	j = (i & 15);
117 	rotor = iounit->rotor[j - 1];
118 	limit = iounit->limit[j];
119 	scan = rotor;
120 nexti:	scan = find_next_zero_bit(iounit->bmap, limit, scan);
121 	if (scan + npages > limit) {
122 		if (limit != rotor) {
123 			limit = rotor;
124 			scan = iounit->limit[j - 1];
125 			goto nexti;
126 		}
127 		i >>= 4;
128 		if (!(i & 15))
129 			panic("iounit_get_area: Couldn't find free iopte slots for (%08lx,%d)\n", vaddr, size);
130 		goto next;
131 	}
132 	for (k = 1, scan++; k < npages; k++)
133 		if (test_bit(scan++, iounit->bmap))
134 			goto nexti;
135 	iounit->rotor[j - 1] = (scan < limit) ? scan : iounit->limit[j - 1];
136 	scan -= npages;
137 	iopte = MKIOPTE(__pa(vaddr & PAGE_MASK));
138 	vaddr = IOUNIT_DMA_BASE + (scan << PAGE_SHIFT) + (vaddr & ~PAGE_MASK);
139 	for (k = 0; k < npages; k++, iopte = __iopte(iopte_val(iopte) + 0x100), scan++) {
140 		set_bit(scan, iounit->bmap);
141 		sbus_writel(iopte_val(iopte), &iounit->page_table[scan]);
142 	}
143 	IOD(("%08lx\n", vaddr));
144 	return vaddr;
145 }
146 
147 static dma_addr_t iounit_map_page(struct device *dev, struct page *page,
148 		unsigned long offset, size_t len, enum dma_data_direction dir,
149 		unsigned long attrs)
150 {
151 	void *vaddr = page_address(page) + offset;
152 	struct iounit_struct *iounit = dev->archdata.iommu;
153 	unsigned long ret, flags;
154 
155 	/* XXX So what is maxphys for us and how do drivers know it? */
156 	if (!len || len > 256 * 1024)
157 		return DMA_MAPPING_ERROR;
158 
159 	spin_lock_irqsave(&iounit->lock, flags);
160 	ret = iounit_get_area(iounit, (unsigned long)vaddr, len);
161 	spin_unlock_irqrestore(&iounit->lock, flags);
162 	return ret;
163 }
164 
165 static int iounit_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
166 		enum dma_data_direction dir, unsigned long attrs)
167 {
168 	struct iounit_struct *iounit = dev->archdata.iommu;
169 	struct scatterlist *sg;
170 	unsigned long flags;
171 	int i;
172 
173 	/* FIXME: Cache some resolved pages - often several sg entries are to the same page */
174 	spin_lock_irqsave(&iounit->lock, flags);
175 	for_each_sg(sgl, sg, nents, i) {
176 		sg->dma_address = iounit_get_area(iounit, (unsigned long) sg_virt(sg), sg->length);
177 		sg->dma_length = sg->length;
178 	}
179 	spin_unlock_irqrestore(&iounit->lock, flags);
180 	return nents;
181 }
182 
183 static void iounit_unmap_page(struct device *dev, dma_addr_t vaddr, size_t len,
184 		enum dma_data_direction dir, unsigned long attrs)
185 {
186 	struct iounit_struct *iounit = dev->archdata.iommu;
187 	unsigned long flags;
188 
189 	spin_lock_irqsave(&iounit->lock, flags);
190 	len = ((vaddr & ~PAGE_MASK) + len + (PAGE_SIZE-1)) >> PAGE_SHIFT;
191 	vaddr = (vaddr - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
192 	IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
193 	for (len += vaddr; vaddr < len; vaddr++)
194 		clear_bit(vaddr, iounit->bmap);
195 	spin_unlock_irqrestore(&iounit->lock, flags);
196 }
197 
198 static void iounit_unmap_sg(struct device *dev, struct scatterlist *sgl,
199 		int nents, enum dma_data_direction dir, unsigned long attrs)
200 {
201 	struct iounit_struct *iounit = dev->archdata.iommu;
202 	unsigned long flags, vaddr, len;
203 	struct scatterlist *sg;
204 	int i;
205 
206 	spin_lock_irqsave(&iounit->lock, flags);
207 	for_each_sg(sgl, sg, nents, i) {
208 		len = ((sg->dma_address & ~PAGE_MASK) + sg->length + (PAGE_SIZE-1)) >> PAGE_SHIFT;
209 		vaddr = (sg->dma_address - IOUNIT_DMA_BASE) >> PAGE_SHIFT;
210 		IOD(("iounit_release %08lx-%08lx\n", (long)vaddr, (long)len+vaddr));
211 		for (len += vaddr; vaddr < len; vaddr++)
212 			clear_bit(vaddr, iounit->bmap);
213 	}
214 	spin_unlock_irqrestore(&iounit->lock, flags);
215 }
216 
217 #ifdef CONFIG_SBUS
218 static void *iounit_alloc(struct device *dev, size_t len,
219 		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
220 {
221 	struct iounit_struct *iounit = dev->archdata.iommu;
222 	unsigned long va, addr, page, end, ret;
223 	pgprot_t dvma_prot;
224 	iopte_t __iomem *iopte;
225 
226 	/* XXX So what is maxphys for us and how do drivers know it? */
227 	if (!len || len > 256 * 1024)
228 		return NULL;
229 
230 	len = PAGE_ALIGN(len);
231 	va = __get_free_pages(gfp | __GFP_ZERO, get_order(len));
232 	if (!va)
233 		return NULL;
234 
235 	addr = ret = sparc_dma_alloc_resource(dev, len);
236 	if (!addr)
237 		goto out_free_pages;
238 	*dma_handle = addr;
239 
240 	dvma_prot = __pgprot(SRMMU_CACHE | SRMMU_ET_PTE | SRMMU_PRIV);
241 	end = PAGE_ALIGN((addr + len));
242 	while(addr < end) {
243 		page = va;
244 		{
245 			pgd_t *pgdp;
246 			p4d_t *p4dp;
247 			pud_t *pudp;
248 			pmd_t *pmdp;
249 			pte_t *ptep;
250 			long i;
251 
252 			pgdp = pgd_offset(&init_mm, addr);
253 			p4dp = p4d_offset(pgdp, addr);
254 			pudp = pud_offset(p4dp, addr);
255 			pmdp = pmd_offset(pudp, addr);
256 			ptep = pte_offset_map(pmdp, addr);
257 
258 			set_pte(ptep, mk_pte(virt_to_page(page), dvma_prot));
259 
260 			i = ((addr - IOUNIT_DMA_BASE) >> PAGE_SHIFT);
261 
262 			iopte = iounit->page_table + i;
263 			sbus_writel(iopte_val(MKIOPTE(__pa(page))), iopte);
264 		}
265 		addr += PAGE_SIZE;
266 		va += PAGE_SIZE;
267 	}
268 	flush_cache_all();
269 	flush_tlb_all();
270 
271 	return (void *)ret;
272 
273 out_free_pages:
274 	free_pages(va, get_order(len));
275 	return NULL;
276 }
277 
278 static void iounit_free(struct device *dev, size_t size, void *cpu_addr,
279 		dma_addr_t dma_addr, unsigned long attrs)
280 {
281 	/* XXX Somebody please fill this in */
282 }
283 #endif
284 
285 static const struct dma_map_ops iounit_dma_ops = {
286 #ifdef CONFIG_SBUS
287 	.alloc			= iounit_alloc,
288 	.free			= iounit_free,
289 #endif
290 	.map_page		= iounit_map_page,
291 	.unmap_page		= iounit_unmap_page,
292 	.map_sg			= iounit_map_sg,
293 	.unmap_sg		= iounit_unmap_sg,
294 };
295