sgmap_common.c revision 1.15 1 /* $NetBSD: sgmap_common.c,v 1.15 2001/01/03 20:12:34 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/cdefs.h> /* RCS ID & Copyright macro defns */
41
42 __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.15 2001/01/03 20:12:34 thorpej Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/proc.h>
49
50 #include <uvm/uvm_extern.h>
51
52 #define _ALPHA_BUS_DMA_PRIVATE
53 #include <machine/bus.h>
54
55 #include <alpha/common/sgmapvar.h>
56
57 /*
58 * Some systems will prefetch the next page during a memory -> device DMA.
59 * This can cause machine checks if there is not a spill page after the
60 * last page of the DMA (thus avoiding hitting an invalid SGMAP PTE).
61 */
62 vaddr_t alpha_sgmap_prefetch_spill_page_va;
63 bus_addr_t alpha_sgmap_prefetch_spill_page_pa;
64
65 void
66 alpha_sgmap_init(t, sgmap, name, wbase, sgvabase, sgvasize, ptesize, ptva,
67 minptalign)
68 bus_dma_tag_t t;
69 struct alpha_sgmap *sgmap;
70 const char *name;
71 bus_addr_t wbase;
72 bus_addr_t sgvabase;
73 bus_size_t sgvasize;
74 size_t ptesize;
75 void *ptva;
76 bus_size_t minptalign;
77 {
78 bus_dma_segment_t seg;
79 size_t ptsize;
80 int rseg;
81
82 if (sgvasize & PGOFSET) {
83 printf("size botch for sgmap `%s'\n", name);
84 goto die;
85 }
86
87 sgmap->aps_wbase = wbase;
88 sgmap->aps_sgvabase = sgvabase;
89 sgmap->aps_sgvasize = sgvasize;
90
91 if (ptva != NULL) {
92 /*
93 * We already have a page table; this may be a system
94 * where the page table resides in bridge-resident SRAM.
95 */
96 sgmap->aps_pt = ptva;
97 sgmap->aps_ptpa = 0;
98 } else {
99 /*
100 * Compute the page table size and allocate it. At minimum,
101 * this must be aligned to the page table size. However,
102 * some platforms have more strict alignment reqirements.
103 */
104 ptsize = (sgvasize / NBPG) * ptesize;
105 if (minptalign != 0) {
106 if (minptalign < ptsize)
107 minptalign = ptsize;
108 } else
109 minptalign = ptsize;
110 if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
111 BUS_DMA_NOWAIT)) {
112 panic("unable to allocate page table for sgmap `%s'\n",
113 name);
114 goto die;
115 }
116 sgmap->aps_ptpa = seg.ds_addr;
117 sgmap->aps_pt = (caddr_t)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa);
118 }
119
120 /*
121 * Create the extent map used to manage the virtual address
122 * space.
123 */
124 sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
125 M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
126 if (sgmap->aps_ex == NULL) {
127 printf("unable to create extent map for sgmap `%s'\n",
128 name);
129 goto die;
130 }
131
132 /*
133 * Allocate a spill page if that hasn't already been done.
134 */
135 if (alpha_sgmap_prefetch_spill_page_va == 0) {
136 if (bus_dmamem_alloc(t, NBPG, 0, 0, &seg, 1, &rseg,
137 BUS_DMA_NOWAIT)) {
138 printf("unable to allocate spill page for sgmap `%s'\n",
139 name);
140 goto die;
141 }
142 alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr;
143 alpha_sgmap_prefetch_spill_page_va =
144 ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa);
145 bzero((caddr_t)alpha_sgmap_prefetch_spill_page_va, NBPG);
146 }
147
148 return;
149 die:
150 panic("alpha_sgmap_init");
151 }
152
153 int
154 alpha_sgmap_alloc(map, origlen, sgmap, flags)
155 bus_dmamap_t map;
156 bus_size_t origlen;
157 struct alpha_sgmap *sgmap;
158 int flags;
159 {
160 int error;
161 bus_size_t len = origlen, boundary, alignment;
162
163 #ifdef DIAGNOSTIC
164 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
165 panic("alpha_sgmap_alloc: already have sgva space");
166 #endif
167 /*
168 * Add a range for spill page.
169 */
170 len += NBPG;
171
172 /*
173 * And add an additional amount in case of ALLOCNOW.
174 */
175 if (flags & BUS_DMA_ALLOCNOW)
176 len += NBPG;
177
178 map->_dm_sgvalen = round_page(len);
179
180 /*
181 * ARGH! If the addition of spill pages bumped us over our
182 * boundary, we have to 2x the boundary limit.
183 */
184 boundary = map->_dm_boundary;
185 if (boundary && boundary < map->_dm_sgvalen) {
186 alignment = boundary;
187 do {
188 boundary <<= 1;
189 } while (boundary < map->_dm_sgvalen);
190 } else
191 alignment = NBPG;
192 #if 0
193 printf("len %x -> %x, _dm_sgvalen %x _dm_boundary %x boundary %x -> ",
194 origlen, len, map->_dm_sgvalen, map->_dm_boundary, boundary);
195 #endif
196
197 error = extent_alloc(sgmap->aps_ex, map->_dm_sgvalen, alignment,
198 boundary, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK,
199 &map->_dm_sgva);
200 #if 0
201 printf("error %d _dm_sgva %x\n", error, map->_dm_sgva);
202 #endif
203
204 if (error == 0)
205 map->_dm_flags |= DMAMAP_HAS_SGMAP;
206 else
207 map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
208
209 return (error);
210 }
211
212 void
213 alpha_sgmap_free(map, sgmap)
214 bus_dmamap_t map;
215 struct alpha_sgmap *sgmap;
216 {
217
218 #ifdef DIAGNOSTIC
219 if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0)
220 panic("alpha_sgmap_free: no sgva space to free");
221 #endif
222
223 if (extent_free(sgmap->aps_ex, map->_dm_sgva, map->_dm_sgvalen,
224 EX_NOWAIT))
225 panic("alpha_sgmap_free");
226
227 map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
228 }
229
230 int
231 alpha_sgmap_dmamap_create(t, size, nsegments, maxsegsz, boundary,
232 flags, dmamp)
233 bus_dma_tag_t t;
234 bus_size_t size;
235 int nsegments;
236 bus_size_t maxsegsz;
237 bus_size_t boundary;
238 int flags;
239 bus_dmamap_t *dmamp;
240 {
241 bus_dmamap_t map;
242 int error;
243
244 error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
245 boundary, flags, &map);
246 if (error)
247 return (error);
248
249 if (flags & BUS_DMA_ALLOCNOW)
250 error = alpha_sgmap_alloc(map, round_page(size),
251 t->_sgmap, flags);
252
253 if (error == 0)
254 *dmamp = map;
255 else
256 alpha_sgmap_dmamap_destroy(t, map);
257
258 return (error);
259 }
260
261 void
262 alpha_sgmap_dmamap_destroy(t, map)
263 bus_dma_tag_t t;
264 bus_dmamap_t map;
265 {
266
267 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
268 alpha_sgmap_free(map, t->_sgmap);
269
270 _bus_dmamap_destroy(t, map);
271 }
272