sgmap_common.c revision 1.17 1 1.17 thorpej /* $NetBSD: sgmap_common.c,v 1.17 2001/07/12 23:25:40 thorpej Exp $ */
2 1.2 thorpej
3 1.2 thorpej /*-
4 1.4 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.2 thorpej * All rights reserved.
6 1.2 thorpej *
7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.2 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.2 thorpej * NASA Ames Research Center.
10 1.2 thorpej *
11 1.2 thorpej * Redistribution and use in source and binary forms, with or without
12 1.2 thorpej * modification, are permitted provided that the following conditions
13 1.2 thorpej * are met:
14 1.2 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.2 thorpej * notice, this list of conditions and the following disclaimer.
16 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.2 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.2 thorpej * documentation and/or other materials provided with the distribution.
19 1.2 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.2 thorpej * must display the following acknowledgement:
21 1.2 thorpej * This product includes software developed by the NetBSD
22 1.2 thorpej * Foundation, Inc. and its contributors.
23 1.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.2 thorpej * contributors may be used to endorse or promote products derived
25 1.2 thorpej * from this software without specific prior written permission.
26 1.2 thorpej *
27 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.2 thorpej */
39 1.2 thorpej
40 1.2 thorpej #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
41 1.2 thorpej
42 1.17 thorpej __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.17 2001/07/12 23:25:40 thorpej Exp $");
43 1.2 thorpej
44 1.2 thorpej #include <sys/param.h>
45 1.2 thorpej #include <sys/systm.h>
46 1.2 thorpej #include <sys/kernel.h>
47 1.2 thorpej #include <sys/malloc.h>
48 1.2 thorpej #include <sys/proc.h>
49 1.2 thorpej
50 1.13 mrg #include <uvm/uvm_extern.h>
51 1.2 thorpej
52 1.14 thorpej #define _ALPHA_BUS_DMA_PRIVATE
53 1.2 thorpej #include <machine/bus.h>
54 1.2 thorpej
55 1.2 thorpej #include <alpha/common/sgmapvar.h>
56 1.2 thorpej
57 1.6 thorpej /*
58 1.6 thorpej * Some systems will prefetch the next page during a memory -> device DMA.
59 1.6 thorpej * This can cause machine checks if there is not a spill page after the
60 1.6 thorpej * last page of the DMA (thus avoiding hitting an invalid SGMAP PTE).
61 1.6 thorpej */
62 1.11 thorpej vaddr_t alpha_sgmap_prefetch_spill_page_va;
63 1.6 thorpej bus_addr_t alpha_sgmap_prefetch_spill_page_pa;
64 1.6 thorpej
65 1.2 thorpej void
66 1.16 thorpej alpha_sgmap_init(bus_dma_tag_t t, struct alpha_sgmap *sgmap, const char *name,
67 1.16 thorpej bus_addr_t wbase, bus_addr_t sgvabase, bus_size_t sgvasize, size_t ptesize,
68 1.16 thorpej void *ptva, bus_size_t minptalign)
69 1.2 thorpej {
70 1.2 thorpej bus_dma_segment_t seg;
71 1.2 thorpej size_t ptsize;
72 1.2 thorpej int rseg;
73 1.2 thorpej
74 1.10 thorpej if (sgvasize & PGOFSET) {
75 1.10 thorpej printf("size botch for sgmap `%s'\n", name);
76 1.10 thorpej goto die;
77 1.10 thorpej }
78 1.2 thorpej
79 1.2 thorpej sgmap->aps_wbase = wbase;
80 1.2 thorpej sgmap->aps_sgvabase = sgvabase;
81 1.2 thorpej sgmap->aps_sgvasize = sgvasize;
82 1.2 thorpej
83 1.2 thorpej if (ptva != NULL) {
84 1.2 thorpej /*
85 1.2 thorpej * We already have a page table; this may be a system
86 1.2 thorpej * where the page table resides in bridge-resident SRAM.
87 1.2 thorpej */
88 1.2 thorpej sgmap->aps_pt = ptva;
89 1.2 thorpej sgmap->aps_ptpa = 0;
90 1.2 thorpej } else {
91 1.2 thorpej /*
92 1.4 thorpej * Compute the page table size and allocate it. At minimum,
93 1.4 thorpej * this must be aligned to the page table size. However,
94 1.4 thorpej * some platforms have more strict alignment reqirements.
95 1.2 thorpej */
96 1.2 thorpej ptsize = (sgvasize / NBPG) * ptesize;
97 1.4 thorpej if (minptalign != 0) {
98 1.4 thorpej if (minptalign < ptsize)
99 1.4 thorpej minptalign = ptsize;
100 1.4 thorpej } else
101 1.4 thorpej minptalign = ptsize;
102 1.4 thorpej if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
103 1.10 thorpej BUS_DMA_NOWAIT)) {
104 1.10 thorpej panic("unable to allocate page table for sgmap `%s'\n",
105 1.10 thorpej name);
106 1.10 thorpej goto die;
107 1.10 thorpej }
108 1.2 thorpej sgmap->aps_ptpa = seg.ds_addr;
109 1.4 thorpej sgmap->aps_pt = (caddr_t)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa);
110 1.2 thorpej }
111 1.2 thorpej
112 1.2 thorpej /*
113 1.2 thorpej * Create the extent map used to manage the virtual address
114 1.2 thorpej * space.
115 1.2 thorpej */
116 1.9 mjacob sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
117 1.8 thorpej M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
118 1.10 thorpej if (sgmap->aps_ex == NULL) {
119 1.10 thorpej printf("unable to create extent map for sgmap `%s'\n",
120 1.10 thorpej name);
121 1.10 thorpej goto die;
122 1.10 thorpej }
123 1.6 thorpej
124 1.6 thorpej /*
125 1.6 thorpej * Allocate a spill page if that hasn't already been done.
126 1.6 thorpej */
127 1.6 thorpej if (alpha_sgmap_prefetch_spill_page_va == 0) {
128 1.6 thorpej if (bus_dmamem_alloc(t, NBPG, 0, 0, &seg, 1, &rseg,
129 1.10 thorpej BUS_DMA_NOWAIT)) {
130 1.10 thorpej printf("unable to allocate spill page for sgmap `%s'\n",
131 1.10 thorpej name);
132 1.10 thorpej goto die;
133 1.10 thorpej }
134 1.6 thorpej alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr;
135 1.6 thorpej alpha_sgmap_prefetch_spill_page_va =
136 1.6 thorpej ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa);
137 1.17 thorpej memset((caddr_t)alpha_sgmap_prefetch_spill_page_va, 0, NBPG);
138 1.6 thorpej }
139 1.10 thorpej
140 1.10 thorpej return;
141 1.10 thorpej die:
142 1.10 thorpej panic("alpha_sgmap_init");
143 1.2 thorpej }
144 1.2 thorpej
145 1.2 thorpej int
146 1.16 thorpej alpha_sgmap_alloc(bus_dmamap_t map, bus_size_t origlen,
147 1.16 thorpej struct alpha_sgmap *sgmap, int flags)
148 1.2 thorpej {
149 1.2 thorpej int error;
150 1.12 mycroft bus_size_t len = origlen, boundary, alignment;
151 1.2 thorpej
152 1.2 thorpej #ifdef DIAGNOSTIC
153 1.5 thorpej if (map->_dm_flags & DMAMAP_HAS_SGMAP)
154 1.2 thorpej panic("alpha_sgmap_alloc: already have sgva space");
155 1.2 thorpej #endif
156 1.9 mjacob /*
157 1.9 mjacob * Add a range for spill page.
158 1.9 mjacob */
159 1.9 mjacob len += NBPG;
160 1.9 mjacob
161 1.9 mjacob /*
162 1.9 mjacob * And add an additional amount in case of ALLOCNOW.
163 1.9 mjacob */
164 1.9 mjacob if (flags & BUS_DMA_ALLOCNOW)
165 1.9 mjacob len += NBPG;
166 1.2 thorpej
167 1.5 thorpej map->_dm_sgvalen = round_page(len);
168 1.9 mjacob
169 1.9 mjacob /*
170 1.9 mjacob * ARGH! If the addition of spill pages bumped us over our
171 1.9 mjacob * boundary, we have to 2x the boundary limit.
172 1.9 mjacob */
173 1.9 mjacob boundary = map->_dm_boundary;
174 1.12 mycroft if (boundary && boundary < map->_dm_sgvalen) {
175 1.12 mycroft alignment = boundary;
176 1.12 mycroft do {
177 1.12 mycroft boundary <<= 1;
178 1.12 mycroft } while (boundary < map->_dm_sgvalen);
179 1.12 mycroft } else
180 1.12 mycroft alignment = NBPG;
181 1.12 mycroft #if 0
182 1.12 mycroft printf("len %x -> %x, _dm_sgvalen %x _dm_boundary %x boundary %x -> ",
183 1.12 mycroft origlen, len, map->_dm_sgvalen, map->_dm_boundary, boundary);
184 1.9 mjacob #endif
185 1.9 mjacob
186 1.12 mycroft error = extent_alloc(sgmap->aps_ex, map->_dm_sgvalen, alignment,
187 1.12 mycroft boundary, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK,
188 1.12 mycroft &map->_dm_sgva);
189 1.12 mycroft #if 0
190 1.12 mycroft printf("error %d _dm_sgva %x\n", error, map->_dm_sgva);
191 1.12 mycroft #endif
192 1.2 thorpej
193 1.2 thorpej if (error == 0)
194 1.5 thorpej map->_dm_flags |= DMAMAP_HAS_SGMAP;
195 1.2 thorpej else
196 1.5 thorpej map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
197 1.2 thorpej
198 1.2 thorpej return (error);
199 1.2 thorpej }
200 1.2 thorpej
201 1.2 thorpej void
202 1.16 thorpej alpha_sgmap_free(bus_dmamap_t map, struct alpha_sgmap *sgmap)
203 1.2 thorpej {
204 1.2 thorpej
205 1.2 thorpej #ifdef DIAGNOSTIC
206 1.5 thorpej if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0)
207 1.2 thorpej panic("alpha_sgmap_free: no sgva space to free");
208 1.2 thorpej #endif
209 1.2 thorpej
210 1.5 thorpej if (extent_free(sgmap->aps_ex, map->_dm_sgva, map->_dm_sgvalen,
211 1.5 thorpej EX_NOWAIT))
212 1.2 thorpej panic("alpha_sgmap_free");
213 1.2 thorpej
214 1.5 thorpej map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
215 1.14 thorpej }
216 1.14 thorpej
217 1.14 thorpej int
218 1.16 thorpej alpha_sgmap_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
219 1.16 thorpej bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp)
220 1.14 thorpej {
221 1.14 thorpej bus_dmamap_t map;
222 1.14 thorpej int error;
223 1.14 thorpej
224 1.14 thorpej error = _bus_dmamap_create(t, size, nsegments, maxsegsz,
225 1.15 thorpej boundary, flags, &map);
226 1.14 thorpej if (error)
227 1.14 thorpej return (error);
228 1.14 thorpej
229 1.15 thorpej if (flags & BUS_DMA_ALLOCNOW)
230 1.14 thorpej error = alpha_sgmap_alloc(map, round_page(size),
231 1.14 thorpej t->_sgmap, flags);
232 1.15 thorpej
233 1.15 thorpej if (error == 0)
234 1.15 thorpej *dmamp = map;
235 1.15 thorpej else
236 1.15 thorpej alpha_sgmap_dmamap_destroy(t, map);
237 1.14 thorpej
238 1.14 thorpej return (error);
239 1.14 thorpej }
240 1.14 thorpej
241 1.14 thorpej void
242 1.16 thorpej alpha_sgmap_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
243 1.14 thorpej {
244 1.14 thorpej
245 1.14 thorpej if (map->_dm_flags & DMAMAP_HAS_SGMAP)
246 1.14 thorpej alpha_sgmap_free(map, t->_sgmap);
247 1.14 thorpej
248 1.14 thorpej _bus_dmamap_destroy(t, map);
249 1.2 thorpej }
250