dvma.c revision 1.27 1 /* $NetBSD: dvma.c,v 1.27 2003/07/15 03:36:20 lukem 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * DVMA (Direct Virtual Memory Access - like DMA)
41 *
42 * In the Sun3 architecture, memory cycles initiated by secondary bus
43 * masters (DVMA devices) passed through the same MMU that governed CPU
44 * accesses. All DVMA devices were wired in such a way so that an offset
45 * was added to the addresses they issued, causing them to access virtual
46 * memory starting at address 0x0FF00000 - the offset. The task of
47 * enabling a DVMA device to access main memory only involved creating
48 * valid mapping in the MMU that translated these high addresses into the
49 * appropriate physical addresses.
50 *
51 * The Sun3x presents a challenge to programming DVMA because the MMU is no
52 * longer shared by both secondary bus masters and the CPU. The MC68030's
53 * built-in MMU serves only to manage virtual memory accesses initiated by
54 * the CPU. Secondary bus master bus accesses pass through a different MMU,
55 * aptly named the 'I/O Mapper'. To enable every device driver that uses
56 * DVMA to understand that these two address spaces are disconnected would
57 * require a tremendous amount of code re-writing. To avoid this, we will
58 * ensure that the I/O Mapper and the MC68030 MMU are programmed together,
59 * so that DVMA mappings are consistent in both the CPU virtual address
60 * space and secondary bus master address space - creating an environment
61 * just like the Sun3 system.
62 *
63 * The maximum address space that any DVMA device in the Sun3x architecture
64 * is capable of addressing is 24 bits wide (16 Megabytes.) We can alias
65 * all of the mappings that exist in the I/O mapper by duplicating them in
66 * a specially reserved section of the CPU's virtual address space, 16
67 * Megabytes in size. Whenever a DVMA buffer is allocated, the allocation
68 * code will enter in a mapping both in the MC68030 MMU page tables and the
69 * I/O mapper.
70 *
71 * The address returned by the allocation routine is a virtual address that
72 * the requesting driver must use to access the buffer. It is up to the
73 * device driver to convert this virtual address into the appropriate slave
74 * address that its device should issue to access the buffer. (There will be
75 * routines that assist the driver in doing so.)
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: dvma.c,v 1.27 2003/07/15 03:36:20 lukem Exp $");
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/device.h>
84 #include <sys/proc.h>
85 #include <sys/malloc.h>
86 #include <sys/extent.h>
87 #include <sys/buf.h>
88 #include <sys/vnode.h>
89 #include <sys/user.h>
90 #include <sys/core.h>
91 #include <sys/exec.h>
92
93 #include <uvm/uvm_extern.h>
94
95 #include <machine/autoconf.h>
96 #include <machine/cpu.h>
97 #include <machine/dvma.h>
98 #include <machine/pmap.h>
99
100 #include <sun3/sun3/machdep.h>
101
102 #include <sun3/sun3x/enable.h>
103 #include <sun3/sun3x/iommu.h>
104
105 /*
106 * Use an extent map to manage DVMA scratch-memory pages.
107 * Note: SunOS says last three pages are reserved (PROM?)
108 * Note: need a separate map (sub-map?) for last 1MB for
109 * use by VME slave interface.
110 */
111
112 /* Number of slots in dvmamap. */
113 struct extent *dvma_extent;
114
115 void
116 dvma_init()
117 {
118
119 /*
120 * Create the extent map for DVMA pages.
121 */
122 dvma_extent = extent_create("dvma", DVMA_MAP_BASE,
123 DVMA_MAP_BASE + (DVMA_MAP_AVAIL - 1), M_DEVBUF,
124 NULL, 0, EX_NOCOALESCE|EX_NOWAIT);
125
126 /*
127 * Enable DVMA in the System Enable register.
128 * Note: This is only necessary for VME slave accesses.
129 * On-board devices are always capable of DVMA.
130 */
131 *enable_reg |= ENA_SDVMA;
132 }
133
134
135 /*
136 * Given a DVMA address, return the physical address that
137 * would be used by some OTHER bus-master besides the CPU.
138 * (Examples: on-board ie/le, VME xy board).
139 */
140 u_long
141 dvma_kvtopa(kva, bustype)
142 void * kva;
143 int bustype;
144 {
145 u_long addr, mask;
146
147 addr = (u_long)kva;
148 if ((addr & DVMA_MAP_BASE) != DVMA_MAP_BASE)
149 panic("dvma_kvtopa: bad dmva addr=0x%lx", addr);
150
151 switch (bustype) {
152 case BUS_OBIO:
153 case BUS_OBMEM:
154 mask = DVMA_OBIO_SLAVE_MASK;
155 break;
156 default: /* VME bus device. */
157 mask = DVMA_VME_SLAVE_MASK;
158 break;
159 }
160
161 return(addr & mask);
162 }
163
164
165 /*
166 * Map a range [va, va+len] of wired virtual addresses in the given map
167 * to a kernel address in DVMA space.
168 */
169 void *
170 dvma_mapin(kmem_va, len, canwait)
171 void * kmem_va;
172 int len, canwait;
173 {
174 void * dvma_addr;
175 vaddr_t kva, tva;
176 int npf, s, error;
177 paddr_t pa;
178 long off;
179 boolean_t rv;
180
181 kva = (vaddr_t)kmem_va;
182 #ifdef DIAGNOSTIC
183 /*
184 * Addresses below VM_MIN_KERNEL_ADDRESS are not part of the kernel
185 * map and should not participate in DVMA.
186 */
187 if (kva < VM_MIN_KERNEL_ADDRESS)
188 panic("dvma_mapin: bad kva");
189 #endif
190
191 /*
192 * Calculate the offset of the data buffer from a page boundary.
193 */
194 off = kva & PGOFSET;
195 kva -= off; /* Truncate starting address to nearest page. */
196 len = round_page(len + off); /* Round the buffer length to pages. */
197 npf = btoc(len); /* Determine the number of pages to be mapped. */
198
199 /*
200 * Try to allocate DVMA space of the appropriate size
201 * in which to do a transfer.
202 */
203 s = splvm();
204 error = extent_alloc(dvma_extent, len, PAGE_SIZE, 0,
205 EX_FAST | EX_NOWAIT | (canwait ? EX_WAITSPACE : 0), &tva);
206 splx(s);
207 if (error)
208 return (NULL);
209
210 /*
211 * Tva is the starting page to which the data buffer will be double
212 * mapped. Dvma_addr is the starting address of the buffer within
213 * that page and is the return value of the function.
214 */
215 dvma_addr = (void *) (tva + off);
216
217 for (;npf--; kva += PAGE_SIZE, tva += PAGE_SIZE) {
218 /*
219 * Retrieve the physical address of each page in the buffer
220 * and enter mappings into the I/O MMU so they may be seen
221 * by external bus masters and into the special DVMA space
222 * in the MC68030 MMU so they may be seen by the CPU.
223 */
224 rv = pmap_extract(pmap_kernel(), kva, &pa);
225 #ifdef DEBUG
226 if (rv == FALSE)
227 panic("dvma_mapin: null page frame");
228 #endif /* DEBUG */
229
230 iommu_enter((tva & IOMMU_VA_MASK), pa);
231 pmap_kenter_pa(tva, pa | PMAP_NC, VM_PROT_READ | VM_PROT_WRITE);
232 }
233 pmap_update(pmap_kernel());
234
235 return (dvma_addr);
236 }
237
238 /*
239 * Remove double map of `va' in DVMA space at `kva'.
240 *
241 * TODO - This function might be the perfect place to handle the
242 * synchronization between the DVMA cache and central RAM
243 * on the 3/470.
244 */
245 void
246 dvma_mapout(dvma_addr, len)
247 void *dvma_addr;
248 int len;
249 {
250 u_long kva;
251 int s, off;
252
253 kva = (u_long)dvma_addr;
254 off = (int)kva & PGOFSET;
255 kva -= off;
256 len = round_page(len + off);
257
258 iommu_remove((kva & IOMMU_VA_MASK), len);
259 pmap_kremove(kva, len);
260 pmap_update(pmap_kernel());
261
262 s = splvm();
263 if (extent_free(dvma_extent, kva, len, EX_NOWAIT | EX_MALLOCOK))
264 panic("dvma_mapout: unable to free region: 0x%lx,0x%x",
265 kva, len);
266 splx(s);
267 }
268
269 /*
270 * Allocate actual memory pages in DVMA space.
271 * (For sun3 compatibility - the ie driver.)
272 */
273 void *
274 dvma_malloc(bytes)
275 size_t bytes;
276 {
277 void *new_mem, *dvma_mem;
278 vsize_t new_size;
279
280 if (!bytes)
281 return NULL;
282 new_size = m68k_round_page(bytes);
283 new_mem = (void*)uvm_km_alloc(kernel_map, new_size);
284 if (!new_mem)
285 return NULL;
286 dvma_mem = dvma_mapin(new_mem, new_size, 1);
287 return (dvma_mem);
288 }
289
290 /*
291 * Free pages from dvma_malloc()
292 */
293 void
294 dvma_free(addr, size)
295 void *addr;
296 size_t size;
297 {
298 vsize_t sz = m68k_round_page(size);
299
300 dvma_mapout(addr, sz);
301 /* XXX: need kmem address to free it...
302 Oh well, we never call this anyway. */
303 }
304