sgmap.c revision 1.3 1 /* $NetBSD: sgmap.c,v 1.3 1999/07/08 18:11:02 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/proc.h>
44 #include <sys/malloc.h>
45
46 #include <vm/vm.h>
47
48 #include <machine/bus.h>
49 #include <machine/sgmap.h>
50
51 void
52 vax_sgmap_init(t, sgmap, name, sgvabase, sgvasize, ptva, minptalign)
53 bus_dma_tag_t t;
54 struct vax_sgmap *sgmap;
55 const char *name;
56 bus_addr_t sgvabase;
57 bus_size_t sgvasize;
58 struct pte *ptva;
59 bus_size_t minptalign;
60 {
61 bus_dma_segment_t seg;
62 size_t ptsize;
63 int rseg;
64
65 if (sgvasize & PGOFSET) {
66 printf("size botch for sgmap `%s'\n", name);
67 goto die;
68 }
69
70 sgmap->aps_sgvabase = sgvabase;
71 sgmap->aps_sgvasize = sgvasize;
72
73 if (ptva != NULL) {
74 /*
75 * We already have a page table; this may be a system
76 * where the page table resides in bridge-resident SRAM.
77 */
78 sgmap->aps_pt = ptva;
79 } else {
80 /*
81 * Compute the page table size and allocate it. At minimum,
82 * this must be aligned to the page table size. However,
83 * some platforms have more strict alignment reqirements.
84 */
85 ptsize = (sgvasize / VAX_NBPG) * sizeof(struct pte);
86 if (minptalign != 0) {
87 if (minptalign < ptsize)
88 minptalign = ptsize;
89 } else
90 minptalign = ptsize;
91 if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
92 BUS_DMA_NOWAIT)) {
93 panic("unable to allocate page table for sgmap `%s'\n",
94 name);
95 goto die;
96 }
97 sgmap->aps_pt = (struct pte *)(seg.ds_addr | KERNBASE);
98 }
99
100 /*
101 * Create the extent map used to manage the virtual address
102 * space.
103 */
104 sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
105 M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
106 if (sgmap->aps_ex == NULL) {
107 printf("unable to create extent map for sgmap `%s'\n", name);
108 goto die;
109 }
110
111 return;
112 die:
113 panic("vax_sgmap_init");
114 }
115
116 int
117 vax_sgmap_alloc(map, origlen, sgmap, flags)
118 bus_dmamap_t map;
119 bus_size_t origlen;
120 struct vax_sgmap *sgmap;
121 int flags;
122 {
123 int error;
124 bus_size_t len = origlen;
125
126 #ifdef DIAGNOSTIC
127 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
128 panic("vax_sgmap_alloc: already have sgva space");
129 #endif
130
131 map->_dm_sgvalen = vax_round_page(len);
132
133 #if 0
134 printf("len %x -> %x, _dm_sgvalen %x _dm_boundary %x boundary %x -> ",
135 origlen, len, map->_dm_sgvalen, map->_dm_boundary, boundary);
136 #endif
137
138 error = extent_alloc(sgmap->aps_ex, map->_dm_sgvalen, VAX_NBPG,
139 0, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK,
140 &map->_dm_sgva);
141 #if 0
142 printf("error %d _dm_sgva %x\n", error, map->_dm_sgva);
143 #endif
144
145 if (error == 0)
146 map->_dm_flags |= DMAMAP_HAS_SGMAP;
147 else
148 map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
149
150 return (error);
151 }
152
153 void
154 vax_sgmap_free(map, sgmap)
155 bus_dmamap_t map;
156 struct vax_sgmap *sgmap;
157 {
158
159 #ifdef DIAGNOSTIC
160 if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0)
161 panic("vax_sgmap_free: no sgva space to free");
162 #endif
163
164 if (extent_free(sgmap->aps_ex, map->_dm_sgva, map->_dm_sgvalen,
165 EX_NOWAIT))
166 panic("vax_sgmap_free");
167
168 map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
169 }
170
171 int
172 vax_sgmap_load(t, map, buf, buflen, p, flags, sgmap)
173 bus_dma_tag_t t;
174 bus_dmamap_t map;
175 void *buf;
176 bus_size_t buflen;
177 struct proc *p;
178 int flags;
179 struct vax_sgmap *sgmap;
180 {
181 vaddr_t endva, va = (vaddr_t)buf;
182 paddr_t pa;
183 bus_addr_t dmaoffset;
184 bus_size_t dmalen;
185 long *pte, *page_table = (long *)sgmap->aps_pt;
186 int pteidx, error;
187
188 /*
189 * Make sure that on error condition we return "no valid mappings".
190 */
191 map->dm_mapsize = 0;
192 map->dm_nsegs = 0;
193
194 if (buflen > map->_dm_size)
195 return (EINVAL);
196
197 /*
198 * Remember the offset into the first page and the total
199 * transfer length.
200 */
201 dmaoffset = ((u_long)buf) & VAX_PGOFSET;
202 dmalen = buflen;
203
204
205 /*
206 * Allocate the necessary virtual address space for the
207 * mapping. Round the size, since we deal with whole pages.
208 *
209 * alpha_sgmap_alloc will deal with the appropriate spill page
210 * allocations.
211 *
212 */
213 endva = vax_round_page(va + buflen);
214 va = vax_trunc_page(va);
215 if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0) {
216 error = vax_sgmap_alloc(map, (endva - va), sgmap, flags);
217 if (error)
218 return (error);
219 }
220
221 pteidx = map->_dm_sgva >> VAX_PGSHIFT;
222 pte = &page_table[pteidx];
223
224 /*
225 * Generate the DMA address.
226 */
227 map->dm_segs[0].ds_addr = map->_dm_sgva + dmaoffset;
228 map->dm_segs[0].ds_len = dmalen;
229
230
231 map->_dm_pteidx = pteidx;
232 map->_dm_ptecnt = 0;
233
234 /*
235 * Create the bus-specific page tables.
236 * Can be done much more efficient than this.
237 */
238 for (; va < endva; va += VAX_NBPG, pteidx++,
239 pte = &page_table[pteidx], map->_dm_ptecnt++) {
240 /*
241 * Get the physical address for this segment.
242 */
243 if (p != NULL)
244 (void) pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa);
245 else
246 pa = kvtophys(va);
247
248 /*
249 * Load the current PTE with this page.
250 */
251 *pte = (pa >> VAX_PGSHIFT) | PG_V;
252 }
253
254 map->dm_mapsize = buflen;
255 map->dm_nsegs = 1;
256 return (0);
257 }
258
259 int
260 vax_sgmap_load_mbuf(t, map, m, flags, sgmap)
261 bus_dma_tag_t t;
262 bus_dmamap_t map;
263 struct mbuf *m;
264 int flags;
265 struct vax_sgmap *sgmap;
266 {
267
268 panic("vax_sgmap_load_mbuf : not implemented");
269 }
270
271 int
272 vax_sgmap_load_uio(t, map, uio, flags, sgmap)
273 bus_dma_tag_t t;
274 bus_dmamap_t map;
275 struct uio *uio;
276 int flags;
277 struct vax_sgmap *sgmap;
278 {
279
280 panic("vax_sgmap_load_uio : not implemented");
281 }
282
283 int
284 vax_sgmap_load_raw(t, map, segs, nsegs, size, flags, sgmap)
285 bus_dma_tag_t t;
286 bus_dmamap_t map;
287 bus_dma_segment_t *segs;
288 int nsegs;
289 bus_size_t size;
290 int flags;
291 struct vax_sgmap *sgmap;
292 {
293
294 panic("vax_sgmap_load_raw : not implemented");
295 }
296
297 void
298 vax_sgmap_unload(t, map, sgmap)
299 bus_dma_tag_t t;
300 bus_dmamap_t map;
301 struct vax_sgmap *sgmap;
302 {
303 long *pte, *page_table = (long *)sgmap->aps_pt;
304 int ptecnt, pteidx;
305
306 /*
307 * Invalidate the PTEs for the mapping.
308 */
309 for (ptecnt = map->_dm_ptecnt, pteidx = map->_dm_pteidx,
310 pte = &page_table[pteidx];
311 ptecnt != 0;
312 ptecnt--, pteidx++,
313 pte = &page_table[pteidx]) {
314 *pte = 0;
315 }
316
317 /*
318 * Free the virtual address space used by the mapping
319 * if necessary.
320 */
321 if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
322 vax_sgmap_free(map, sgmap);
323 /*
324 * Mark the mapping invalid.
325 */
326 map->dm_mapsize = 0;
327 map->dm_nsegs = 0;
328 }
329