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