xref: /netbsd-src/sys/arch/sun3/sun3x/dvma.c (revision 5b28f239895d55856221c590945769250e289f5f)
1 /*	$NetBSD: dvma.c,v 1.46 2024/09/08 09:36:49 rillig Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross and Jeremy Cooper.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * DVMA (Direct Virtual Memory Access - like DMA)
34  *
35  * In the Sun3 architecture, memory cycles initiated by secondary bus
36  * masters (DVMA devices) passed through the same MMU that governed CPU
37  * accesses.  All DVMA devices were wired in such a way so that an offset
38  * was added to the addresses they issued, causing them to access virtual
39  * memory starting at address 0x0FF00000 - the offset.  The task of
40  * enabling a DVMA device to access main memory only involved creating
41  * valid mapping in the MMU that translated these high addresses into the
42  * appropriate physical addresses.
43  *
44  * The Sun3x presents a challenge to programming DVMA because the MMU is no
45  * longer shared by both secondary bus masters and the CPU.  The MC68030's
46  * built-in MMU serves only to manage virtual memory accesses initiated by
47  * the CPU.  Secondary bus master bus accesses pass through a different MMU,
48  * aptly named the 'I/O Mapper'.  To enable every device driver that uses
49  * DVMA to understand that these two address spaces are disconnected would
50  * require a tremendous amount of code re-writing. To avoid this, we will
51  * ensure that the I/O Mapper and the MC68030 MMU are programmed together,
52  * so that DVMA mappings are consistent in both the CPU virtual address
53  * space and secondary bus master address space - creating an environment
54  * just like the Sun3 system.
55  *
56  * The maximum address space that any DVMA device in the Sun3x architecture
57  * is capable of addressing is 24 bits wide (16 Megabytes.)  We can alias
58  * all of the mappings that exist in the I/O mapper by duplicating them in
59  * a specially reserved section of the CPU's virtual address space, 16
60  * Megabytes in size.  Whenever a DVMA buffer is allocated, the allocation
61  * code will enter in a mapping both in the MC68030 MMU page tables and the
62  * I/O mapper.
63  *
64  * The address returned by the allocation routine is a virtual address that
65  * the requesting driver must use to access the buffer.  It is up to the
66  * device driver to convert this virtual address into the appropriate slave
67  * address that its device should issue to access the buffer.  (There will be
68  * routines that assist the driver in doing so.)
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: dvma.c,v 1.46 2024/09/08 09:36:49 rillig Exp $");
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/device.h>
77 #include <sys/proc.h>
78 #include <sys/vmem.h>
79 #include <sys/buf.h>
80 #include <sys/vnode.h>
81 #include <sys/core.h>
82 #include <sys/exec.h>
83 
84 #include <uvm/uvm_extern.h>
85 
86 #define _SUN68K_BUS_DMA_PRIVATE
87 #include <machine/autoconf.h>
88 #include <machine/bus.h>
89 #include <machine/cpu.h>
90 #include <machine/dvma.h>
91 #include <machine/pmap.h>
92 
93 #include <sun3/sun3/machdep.h>
94 
95 #include <sun3/sun3x/enable.h>
96 #include <sun3/sun3x/iommu.h>
97 
98 /*
99  * Use a vmem arena to manage DVMA scratch-memory pages.
100  * Note: SunOS says last three pages are reserved (PROM?)
101  * Note: need a separate map (sub-map?) for last 1MB for
102  *       use by VME slave interface.
103  */
104 vmem_t *dvma_arena;
105 
106 void
107 dvma_init(void)
108 {
109 
110 	/*
111 	 * Create the vmem arena for DVMA pages.
112 	 */
113 	dvma_arena = vmem_create("dvma", DVMA_MAP_BASE, DVMA_MAP_AVAIL,
114 				 PAGE_SIZE,		/* quantum */
115 				 NULL,			/* importfn */
116 				 NULL,			/* releasefn */
117 				 NULL,			/* source */
118 				 0,			/* qcache_max */
119 				 VM_SLEEP,
120 				 IPL_VM);
121 
122 	/*
123 	 * Enable DVMA in the System Enable register.
124 	 * Note:  This is only necessary for VME slave accesses.
125 	 *        On-board devices are always capable of DVMA.
126 	 */
127 	*enable_reg |= ENA_SDVMA;
128 }
129 
130 
131 /*
132  * Given a DVMA address, return the physical address that
133  * would be used by some OTHER bus-master besides the CPU.
134  * (Examples: on-board ie/le, VME xy board).
135  */
136 u_long
137 dvma_kvtopa(void *kva, int bustype)
138 {
139 	u_long addr, mask;
140 
141 	addr = (u_long)kva;
142 	if ((addr & DVMA_MAP_BASE) != DVMA_MAP_BASE)
143 		panic("dvma_kvtopa: bad dmva addr=0x%lx", addr);
144 
145 	switch (bustype) {
146 	case BUS_OBIO:
147 	case BUS_OBMEM:
148 		mask = DVMA_OBIO_SLAVE_MASK;
149 		break;
150 	default:	/* VME bus device. */
151 		mask = DVMA_VME_SLAVE_MASK;
152 		break;
153 	}
154 
155 	return addr & mask;
156 }
157 
158 
159 /*
160  * Map a range [va, va+len] of wired virtual addresses in the given map
161  * to a kernel address in DVMA space.
162  */
163 void *
164 dvma_mapin(void *kmem_va, int len, int canwait)
165 {
166 	void *dvma_addr;
167 	vaddr_t kva;
168 	vmem_addr_t tva;
169 	int npf, error;
170 	paddr_t pa;
171 	long off;
172 	bool rv __debugused;
173 
174 	kva = (vaddr_t)kmem_va;
175 	KASSERT(kva >= VM_MIN_KERNEL_ADDRESS);
176 
177 	/*
178 	 * Calculate the offset of the data buffer from a page boundary.
179 	 */
180 	off = kva & PGOFSET;
181 	kva -= off;	/* Truncate starting address to nearest page. */
182 	len = round_page(len + off); /* Round the buffer length to pages. */
183 	npf = btoc(len); /* Determine the number of pages to be mapped. */
184 
185 	/*
186 	 * Try to allocate DVMA space of the appropriate size
187 	 * in which to do a transfer.
188 	 */
189 	const vm_flag_t vmflags = VM_INSTANTFIT |
190 	    (canwait ? VM_SLEEP : VM_NOSLEEP);
191 
192 	error = vmem_xalloc(dvma_arena, len,
193 			    0,			/* alignment */
194 			    0,			/* phase */
195 			    0,			/* nocross */
196 			    VMEM_ADDR_MIN,	/* minaddr */
197 			    VMEM_ADDR_MAX,	/* maxaddr */
198 			    vmflags,
199 			    &tva);
200 	if (error)
201 		return NULL;
202 
203 	/*
204 	 * Tva is the starting page to which the data buffer will be double
205 	 * mapped.  Dvma_addr is the starting address of the buffer within
206 	 * that page and is the return value of the function.
207 	 */
208 	dvma_addr = (void *)(tva + off);
209 
210 	for (; npf--; kva += PAGE_SIZE, tva += PAGE_SIZE) {
211 		/*
212 		 * Retrieve the physical address of each page in the buffer
213 		 * and enter mappings into the I/O MMU so they may be seen
214 		 * by external bus masters and into the special DVMA space
215 		 * in the MC68030 MMU so they may be seen by the CPU.
216 		 */
217 		rv = pmap_extract(pmap_kernel(), kva, &pa);
218 #ifdef	DEBUG
219 		if (rv == false)
220 			panic("dvma_mapin: null page frame");
221 #endif	/* DEBUG */
222 
223 		iommu_enter((tva & IOMMU_VA_MASK), pa);
224 		pmap_kenter_pa(tva,
225 		    pa | PMAP_NC, VM_PROT_READ | VM_PROT_WRITE, 0);
226 	}
227 	pmap_update(pmap_kernel());
228 
229 	return dvma_addr;
230 }
231 
232 /*
233  * Remove double map of `va' in DVMA space at `kva'.
234  *
235  * TODO - This function might be the perfect place to handle the
236  *       synchronization between the DVMA cache and central RAM
237  *       on the 3/470.
238  */
239 void
240 dvma_mapout(void *dvma_addr, int len)
241 {
242 	u_long kva;
243 	int off;
244 
245 	kva = (u_long)dvma_addr;
246 	off = (int)kva & PGOFSET;
247 	kva -= off;
248 	len = round_page(len + off);
249 
250 	iommu_remove((kva & IOMMU_VA_MASK), len);
251 	pmap_kremove(kva, len);
252 	pmap_update(pmap_kernel());
253 
254 	vmem_xfree(dvma_arena, kva, len);
255 }
256 
257 /*
258  * Allocate actual memory pages in DVMA space.
259  * (For sun3 compatibility - the ie driver.)
260  */
261 void *
262 dvma_malloc(size_t bytes)
263 {
264 	void *new_mem, *dvma_mem;
265 	vsize_t new_size;
266 
267 	if (bytes == 0)
268 		return NULL;
269 	new_size = m68k_round_page(bytes);
270 	new_mem = (void *)uvm_km_alloc(kernel_map, new_size, 0, UVM_KMF_WIRED);
271 	if (new_mem == 0)
272 		return NULL;
273 	dvma_mem = dvma_mapin(new_mem, new_size, 1);
274 	return dvma_mem;
275 }
276 
277 /*
278  * Free pages from dvma_malloc()
279  */
280 void
281 dvma_free(void *addr, size_t size)
282 {
283 	vsize_t sz = m68k_round_page(size);
284 
285 	dvma_mapout(addr, sz);
286 	/* XXX: need kmem address to free it...
287 	   Oh well, we never call this anyway. */
288 }
289 
290 int
291 _bus_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map, bus_dma_segment_t *segs,
292     int nsegs, bus_size_t size, int flags)
293 {
294 
295 	panic("_bus_dmamap_load_raw(): not implemented yet.");
296 }
297 
298 int
299 _bus_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
300     bus_size_t buflen, struct proc *p, int flags)
301 {
302 	vaddr_t kva;
303 	vmem_addr_t dva;
304 	vsize_t off, sgsize;
305 	paddr_t pa;
306 	pmap_t pmap;
307 	int error, rv __diagused;
308 
309 	/*
310 	 * Make sure that on error condition we return "no valid mappings".
311 	 */
312 	map->dm_nsegs = 0;
313 	map->dm_mapsize = 0;
314 
315 	if (buflen > map->_dm_size)
316 		return EINVAL;
317 
318 	kva = (vaddr_t)buf;
319 	off = kva & PGOFSET;
320 	sgsize = round_page(off + buflen);
321 
322 	/* Try to allocate DVMA space. */
323 	const vm_flag_t vmflags = VM_INSTANTFIT |
324 	    ((flags & BUS_DMA_NOWAIT) ? VM_NOSLEEP : VM_SLEEP);
325 
326 	error = vmem_xalloc(dvma_arena, sgsize,
327 			    0,			/* alignment */
328 			    0,			/* phase */
329 			    0,			/* nocross */
330 			    VMEM_ADDR_MIN,	/* minaddr */
331 			    VMEM_ADDR_MAX,	/* maxaddr */
332 			    vmflags,
333 			    &dva);
334 	if (error)
335 		return ENOMEM;
336 
337 	/* Fill in the segment. */
338 	map->dm_segs[0].ds_addr = dva + off;
339 	map->dm_segs[0].ds_len = buflen;
340 	map->dm_segs[0]._ds_va = dva;
341 	map->dm_segs[0]._ds_sgsize = sgsize;
342 
343 	/*
344 	 * Now map the DVMA addresses we allocated to point to the
345 	 * pages of the caller's buffer.
346 	 */
347 	if (p != NULL)
348 		pmap = p->p_vmspace->vm_map.pmap;
349 	else
350 		pmap = pmap_kernel();
351 
352 	while (sgsize > 0) {
353 		rv = pmap_extract(pmap, kva, &pa);
354 #ifdef DIAGNOSTIC
355 		if (rv == false)
356 			panic("%s: unmapped VA", __func__);
357 #endif
358 		iommu_enter((dva & IOMMU_VA_MASK), pa);
359 		pmap_kenter_pa(dva,
360 		    pa | PMAP_NC, VM_PROT_READ | VM_PROT_WRITE, 0);
361 		kva += PAGE_SIZE;
362 		dva += PAGE_SIZE;
363 		sgsize -= PAGE_SIZE;
364 	}
365 
366 	map->dm_nsegs = 1;
367 	map->dm_mapsize = map->dm_segs[0].ds_len;
368 
369 	return 0;
370 }
371 
372 void
373 _bus_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
374 {
375 	bus_dma_segment_t *segs;
376 	vaddr_t dva;
377 	vsize_t sgsize;
378 
379 #ifdef DIAGNOSTIC
380 	if (map->dm_nsegs != 1)
381 		panic("%s: invalid nsegs = %d", __func__, map->dm_nsegs);
382 #endif
383 
384 	segs = map->dm_segs;
385 	dva = segs[0]._ds_va & ~PGOFSET;
386 	sgsize = segs[0]._ds_sgsize;
387 
388 	/* Unmap the DVMA addresses. */
389 	iommu_remove((dva & IOMMU_VA_MASK), sgsize);
390 	pmap_kremove(dva, sgsize);
391 	pmap_update(pmap_kernel());
392 
393 	/* Free the DVMA addresses. */
394 	vmem_xfree(dvma_arena, dva, sgsize);
395 
396 	/* Mark the mappings as invalid. */
397 	map->dm_mapsize = 0;
398 	map->dm_nsegs = 0;
399 }
400