iommu.c revision 1.3.2.4 1 1.3.2.4 bouyer /* $NetBSD: iommu.c,v 1.3.2.4 2001/03/12 13:29:28 bouyer Exp $ */
2 1.3.2.1 bouyer
3 1.3.2.1 bouyer /*
4 1.3.2.1 bouyer * Copyright (c) 1999, 2000 Matthew R. Green
5 1.3.2.1 bouyer * All rights reserved.
6 1.3.2.1 bouyer *
7 1.3.2.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.3.2.1 bouyer * modification, are permitted provided that the following conditions
9 1.3.2.1 bouyer * are met:
10 1.3.2.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.3.2.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.3.2.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.2.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.3.2.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.3.2.1 bouyer * 3. The name of the author may not be used to endorse or promote products
16 1.3.2.1 bouyer * derived from this software without specific prior written permission.
17 1.3.2.1 bouyer *
18 1.3.2.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.3.2.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.3.2.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.3.2.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.3.2.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.3.2.1 bouyer * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.3.2.1 bouyer * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.3.2.1 bouyer * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.3.2.1 bouyer * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.3.2.1 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.3.2.1 bouyer * SUCH DAMAGE.
29 1.3.2.1 bouyer */
30 1.1 mrg
31 1.1 mrg /*-
32 1.1 mrg * Copyright (c) 1998 The NetBSD Foundation, Inc.
33 1.1 mrg * All rights reserved.
34 1.1 mrg *
35 1.1 mrg * This code is derived from software contributed to The NetBSD Foundation
36 1.1 mrg * by Paul Kranenburg.
37 1.1 mrg *
38 1.1 mrg * Redistribution and use in source and binary forms, with or without
39 1.1 mrg * modification, are permitted provided that the following conditions
40 1.1 mrg * are met:
41 1.1 mrg * 1. Redistributions of source code must retain the above copyright
42 1.1 mrg * notice, this list of conditions and the following disclaimer.
43 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
44 1.1 mrg * notice, this list of conditions and the following disclaimer in the
45 1.1 mrg * documentation and/or other materials provided with the distribution.
46 1.1 mrg * 3. All advertising materials mentioning features or use of this software
47 1.1 mrg * must display the following acknowledgement:
48 1.1 mrg * This product includes software developed by the NetBSD
49 1.1 mrg * Foundation, Inc. and its contributors.
50 1.1 mrg * 4. Neither the name of The NetBSD Foundation nor the names of its
51 1.1 mrg * contributors may be used to endorse or promote products derived
52 1.1 mrg * from this software without specific prior written permission.
53 1.1 mrg *
54 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
55 1.1 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 1.1 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
57 1.1 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
58 1.1 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
59 1.1 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
60 1.1 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
61 1.1 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
62 1.1 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63 1.1 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
64 1.1 mrg * POSSIBILITY OF SUCH DAMAGE.
65 1.1 mrg */
66 1.1 mrg
67 1.1 mrg /*
68 1.1 mrg * Copyright (c) 1992, 1993
69 1.1 mrg * The Regents of the University of California. All rights reserved.
70 1.1 mrg *
71 1.1 mrg * This software was developed by the Computer Systems Engineering group
72 1.1 mrg * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
73 1.1 mrg * contributed to Berkeley.
74 1.1 mrg *
75 1.1 mrg * All advertising materials mentioning features or use of this software
76 1.1 mrg * must display the following acknowledgement:
77 1.1 mrg * This product includes software developed by the University of
78 1.1 mrg * California, Lawrence Berkeley Laboratory.
79 1.1 mrg *
80 1.1 mrg * Redistribution and use in source and binary forms, with or without
81 1.1 mrg * modification, are permitted provided that the following conditions
82 1.1 mrg * are met:
83 1.1 mrg * 1. Redistributions of source code must retain the above copyright
84 1.1 mrg * notice, this list of conditions and the following disclaimer.
85 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
86 1.1 mrg * notice, this list of conditions and the following disclaimer in the
87 1.1 mrg * documentation and/or other materials provided with the distribution.
88 1.1 mrg * 3. All advertising materials mentioning features or use of this software
89 1.1 mrg * must display the following acknowledgement:
90 1.1 mrg * This product includes software developed by the University of
91 1.1 mrg * California, Berkeley and its contributors.
92 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
93 1.1 mrg * may be used to endorse or promote products derived from this software
94 1.1 mrg * without specific prior written permission.
95 1.1 mrg *
96 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
97 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
98 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
99 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
100 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
101 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
102 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
103 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
104 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
105 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
106 1.1 mrg * SUCH DAMAGE.
107 1.1 mrg *
108 1.1 mrg * from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
109 1.1 mrg * from: @(#)sbus.c 8.1 (Berkeley) 6/11/93
110 1.1 mrg */
111 1.1 mrg
112 1.3.2.1 bouyer /*
113 1.3.2.1 bouyer * UltraSPARC IOMMU support; used by both the sbus and pci code.
114 1.3.2.1 bouyer */
115 1.3.2.1 bouyer #include "opt_ddb.h"
116 1.3.2.1 bouyer
117 1.1 mrg #include <sys/param.h>
118 1.1 mrg #include <sys/extent.h>
119 1.1 mrg #include <sys/malloc.h>
120 1.1 mrg #include <sys/systm.h>
121 1.1 mrg #include <sys/device.h>
122 1.3.2.1 bouyer
123 1.3.2.1 bouyer #include <uvm/uvm_extern.h>
124 1.1 mrg
125 1.1 mrg #include <machine/bus.h>
126 1.3.2.1 bouyer #include <sparc64/sparc64/cache.h>
127 1.1 mrg #include <sparc64/dev/iommureg.h>
128 1.1 mrg #include <sparc64/dev/iommuvar.h>
129 1.1 mrg
130 1.1 mrg #include <machine/autoconf.h>
131 1.1 mrg #include <machine/cpu.h>
132 1.1 mrg
133 1.1 mrg #ifdef DEBUG
134 1.3.2.1 bouyer #define IDB_BUSDMA 0x1
135 1.3.2.1 bouyer #define IDB_IOMMU 0x2
136 1.3.2.1 bouyer #define IDB_INFO 0x4
137 1.3.2.1 bouyer int iommudebug = 0x0;
138 1.3.2.1 bouyer #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
139 1.3.2.1 bouyer #else
140 1.3.2.1 bouyer #define DPRINTF(l, s)
141 1.1 mrg #endif
142 1.1 mrg
143 1.3.2.4 bouyer #define iommu_strbuf_flush(i,v) bus_space_write_8((i)->is_bustag, \
144 1.3.2.4 bouyer (bus_space_handle_t)(u_long)&(i)->is_sb->strbuf_pgflush, 0, (v))
145 1.3.2.4 bouyer static int iommu_strbuf_flush_done __P((struct iommu_state *));
146 1.3.2.1 bouyer
147 1.1 mrg /*
148 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
149 1.1 mrg * - allocate and setup the iotsb.
150 1.1 mrg * - enable the IOMMU
151 1.3.2.1 bouyer * - initialise the streaming buffers (if they exist)
152 1.1 mrg * - create a private DVMA map.
153 1.1 mrg */
154 1.1 mrg void
155 1.1 mrg iommu_init(name, is, tsbsize)
156 1.1 mrg char *name;
157 1.1 mrg struct iommu_state *is;
158 1.1 mrg int tsbsize;
159 1.1 mrg {
160 1.3.2.1 bouyer psize_t size;
161 1.3.2.1 bouyer vaddr_t va;
162 1.3.2.1 bouyer paddr_t pa;
163 1.3.2.1 bouyer vm_page_t m;
164 1.3.2.1 bouyer struct pglist mlist;
165 1.1 mrg
166 1.1 mrg /*
167 1.1 mrg * Setup the iommu.
168 1.1 mrg *
169 1.3.2.1 bouyer * The sun4u iommu is part of the SBUS or PCI controller so we
170 1.3.2.1 bouyer * will deal with it here..
171 1.2 eeh *
172 1.2 eeh * The IOMMU address space always ends at 0xffffe000, but the starting
173 1.2 eeh * address depends on the size of the map. The map size is 1024 * 2 ^
174 1.2 eeh * is->is_tsbsize entries, where each entry is 8 bytes. The start of
175 1.2 eeh * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
176 1.2 eeh *
177 1.2 eeh * Note: the stupid IOMMU ignores the high bits of an address, so a
178 1.2 eeh * NULL DMA pointer will be translated by the first page of the IOTSB.
179 1.2 eeh * To trap bugs we'll skip the first entry in the IOTSB.
180 1.2 eeh */
181 1.3.2.1 bouyer is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
182 1.1 mrg is->is_tsbsize = tsbsize;
183 1.3.2.1 bouyer is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize) + NBPG;
184 1.3.2.1 bouyer
185 1.3.2.1 bouyer /*
186 1.3.2.1 bouyer * Allocate memory for I/O pagetables. They need to be physically
187 1.3.2.1 bouyer * contiguous.
188 1.3.2.1 bouyer */
189 1.3.2.1 bouyer
190 1.3.2.1 bouyer size = NBPG<<(is->is_tsbsize);
191 1.3.2.1 bouyer TAILQ_INIT(&mlist);
192 1.3.2.1 bouyer if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
193 1.3.2.1 bouyer (paddr_t)NBPG, (paddr_t)0, &mlist, 1, 0) != 0)
194 1.3.2.1 bouyer panic("iommu_init: no memory");
195 1.3.2.1 bouyer
196 1.3.2.1 bouyer va = uvm_km_valloc(kernel_map, size);
197 1.3.2.1 bouyer if (va == 0)
198 1.3.2.1 bouyer panic("iommu_init: no memory");
199 1.3.2.1 bouyer is->is_tsb = (int64_t *)va;
200 1.3.2.1 bouyer
201 1.3.2.1 bouyer m = TAILQ_FIRST(&mlist);
202 1.3.2.1 bouyer is->is_ptsb = VM_PAGE_TO_PHYS(m);
203 1.3.2.1 bouyer
204 1.3.2.1 bouyer /* Map the pages */
205 1.3.2.1 bouyer for (; m != NULL; m = TAILQ_NEXT(m,pageq)) {
206 1.3.2.1 bouyer pa = VM_PAGE_TO_PHYS(m);
207 1.3.2.1 bouyer pmap_enter(pmap_kernel(), va, pa | PMAP_NVC,
208 1.3.2.1 bouyer VM_PROT_READ|VM_PROT_WRITE,
209 1.3.2.1 bouyer VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
210 1.3.2.1 bouyer va += NBPG;
211 1.3.2.1 bouyer }
212 1.3.2.1 bouyer bzero(is->is_tsb, size);
213 1.1 mrg
214 1.1 mrg #ifdef DEBUG
215 1.3.2.1 bouyer if (iommudebug & IDB_INFO)
216 1.1 mrg {
217 1.1 mrg /* Probe the iommu */
218 1.1 mrg struct iommureg *regs = is->is_iommu;
219 1.1 mrg
220 1.3.2.2 bouyer printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
221 1.3.2.2 bouyer (u_long)®s->iommu_cr,
222 1.3.2.2 bouyer (u_long)®s->iommu_tsb,
223 1.3.2.2 bouyer (u_long)®s->iommu_flush);
224 1.3.2.2 bouyer printf("iommu cr=%llx tsb=%llx\n", (unsigned long long)regs->iommu_cr, (unsigned long long)regs->iommu_tsb);
225 1.3.2.2 bouyer printf("TSB base %p phys %llx\n", (void *)is->is_tsb, (unsigned long long)is->is_ptsb);
226 1.1 mrg delay(1000000); /* 1 s */
227 1.1 mrg }
228 1.1 mrg #endif
229 1.1 mrg
230 1.1 mrg /*
231 1.3.2.1 bouyer * Initialize streaming buffer, if it is there.
232 1.1 mrg */
233 1.3.2.1 bouyer if (is->is_sb)
234 1.3.2.1 bouyer (void)pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush,
235 1.3.2.1 bouyer (paddr_t *)&is->is_flushpa);
236 1.1 mrg
237 1.1 mrg /*
238 1.1 mrg * now actually start up the IOMMU
239 1.1 mrg */
240 1.1 mrg iommu_reset(is);
241 1.1 mrg
242 1.1 mrg /*
243 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
244 1.1 mrg */
245 1.3.2.1 bouyer printf("DVMA map: %x to %x\n",
246 1.3.2.1 bouyer (unsigned int)is->is_dvmabase,
247 1.3.2.1 bouyer (unsigned int)IOTSB_VEND);
248 1.1 mrg is->is_dvmamap = extent_create(name,
249 1.3.2.1 bouyer is->is_dvmabase, (u_long)IOTSB_VEND,
250 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
251 1.1 mrg }
252 1.1 mrg
253 1.3.2.1 bouyer /*
254 1.3.2.1 bouyer * Streaming buffers don't exist on the UltraSPARC IIi; we should have
255 1.3.2.1 bouyer * detected that already and disabled them. If not, we will notice that
256 1.3.2.1 bouyer * they aren't there when the STRBUF_EN bit does not remain.
257 1.3.2.1 bouyer */
258 1.1 mrg void
259 1.1 mrg iommu_reset(is)
260 1.1 mrg struct iommu_state *is;
261 1.1 mrg {
262 1.1 mrg
263 1.1 mrg /* Need to do 64-bit stores */
264 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
265 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_iommu->iommu_tsb,
266 1.3.2.1 bouyer 0, is->is_ptsb);
267 1.3.2.1 bouyer /* Enable IOMMU in diagnostic mode */
268 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
269 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_iommu->iommu_cr, 0,
270 1.3.2.1 bouyer is->is_cr|IOMMUCR_DE);
271 1.3.2.1 bouyer
272 1.3.2.1 bouyer
273 1.3.2.1 bouyer if (!is->is_sb)
274 1.3.2.1 bouyer return;
275 1.3.2.1 bouyer
276 1.1 mrg /* Enable diagnostics mode? */
277 1.3.2.1 bouyer bus_space_write_8(is->is_bustag,
278 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl,
279 1.3.2.1 bouyer 0, STRBUF_EN);
280 1.3.2.1 bouyer
281 1.3.2.1 bouyer /* No streaming buffers? Disable them */
282 1.3.2.1 bouyer if (bus_space_read_8(is->is_bustag,
283 1.3.2.1 bouyer (bus_space_handle_t)(u_long)&is->is_sb->strbuf_ctl,
284 1.3.2.1 bouyer 0) == 0)
285 1.3.2.1 bouyer is->is_sb = 0;
286 1.2 eeh }
287 1.2 eeh
288 1.2 eeh /*
289 1.2 eeh * Here are the iommu control routines.
290 1.2 eeh */
291 1.2 eeh void
292 1.2 eeh iommu_enter(is, va, pa, flags)
293 1.2 eeh struct iommu_state *is;
294 1.2 eeh vaddr_t va;
295 1.2 eeh int64_t pa;
296 1.2 eeh int flags;
297 1.2 eeh {
298 1.2 eeh int64_t tte;
299 1.2 eeh
300 1.2 eeh #ifdef DIAGNOSTIC
301 1.2 eeh if (va < is->is_dvmabase)
302 1.3.2.1 bouyer panic("iommu_enter: va %#lx not in DVMA space", va);
303 1.2 eeh #endif
304 1.2 eeh
305 1.2 eeh tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
306 1.3.2.4 bouyer (flags&BUS_DMA_STREAMING));
307 1.2 eeh
308 1.2 eeh /* Is the streamcache flush really needed? */
309 1.3.2.1 bouyer if (is->is_sb) {
310 1.3.2.4 bouyer iommu_strbuf_flush(is, va);
311 1.3.2.4 bouyer iommu_strbuf_flush_done(is);
312 1.3.2.1 bouyer }
313 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
314 1.3.2.2 bouyer (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
315 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
316 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
317 1.3.2.1 bouyer &is->is_iommu->iommu_flush, 0, va);
318 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
319 1.3.2.2 bouyer va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
320 1.3.2.2 bouyer (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
321 1.3.2.2 bouyer (u_long)tte));
322 1.2 eeh }
323 1.2 eeh
324 1.2 eeh /*
325 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
326 1.2 eeh *
327 1.2 eeh * Only demap from IOMMU if flag is set.
328 1.3.2.1 bouyer *
329 1.3.2.1 bouyer * XXX: this function needs better internal error checking.
330 1.2 eeh */
331 1.2 eeh void
332 1.2 eeh iommu_remove(is, va, len)
333 1.2 eeh struct iommu_state *is;
334 1.2 eeh vaddr_t va;
335 1.2 eeh size_t len;
336 1.2 eeh {
337 1.2 eeh
338 1.2 eeh #ifdef DIAGNOSTIC
339 1.2 eeh if (va < is->is_dvmabase)
340 1.3.2.2 bouyer panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
341 1.2 eeh if ((long)(va + len) < (long)va)
342 1.3.2.1 bouyer panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
343 1.2 eeh (long) va, (long) len);
344 1.2 eeh if (len & ~0xfffffff)
345 1.3.2.2 bouyer panic("iommu_remove: rediculous len 0x%lx", (u_long)len);
346 1.2 eeh #endif
347 1.2 eeh
348 1.2 eeh va = trunc_page(va);
349 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
350 1.3.2.2 bouyer va, (u_long)IOTSBSLOT(va,is->is_tsbsize),
351 1.3.2.1 bouyer &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]));
352 1.2 eeh while (len > 0) {
353 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d for va %p size %lx\n",
354 1.3.2.2 bouyer (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va, (u_long)len));
355 1.3.2.1 bouyer if (is->is_sb) {
356 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
357 1.3.2.2 bouyer (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
358 1.3.2.2 bouyer (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
359 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
360 1.3.2.1 bouyer (u_long)len));
361 1.3.2.4 bouyer iommu_strbuf_flush(is, va);
362 1.3.2.1 bouyer if (len <= NBPG)
363 1.3.2.4 bouyer iommu_strbuf_flush_done(is);
364 1.3.2.1 bouyer DPRINTF(IDB_IOMMU, ("iommu_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
365 1.3.2.2 bouyer (void *)(u_long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
366 1.3.2.2 bouyer (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
367 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
368 1.3.2.1 bouyer (u_long)len));
369 1.3.2.1 bouyer } else
370 1.3.2.1 bouyer membar_sync(); /* XXX */
371 1.3.2.1 bouyer
372 1.3.2.1 bouyer if (len <= NBPG)
373 1.3.2.1 bouyer len = 0;
374 1.3.2.1 bouyer else
375 1.3.2.1 bouyer len -= NBPG;
376 1.3.2.1 bouyer
377 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
378 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
379 1.3.2.1 bouyer &is->is_iommu->iommu_flush, 0, va);
380 1.2 eeh va += NBPG;
381 1.2 eeh }
382 1.2 eeh }
383 1.2 eeh
384 1.3.2.1 bouyer static int
385 1.3.2.4 bouyer iommu_strbuf_flush_done(is)
386 1.2 eeh struct iommu_state *is;
387 1.2 eeh {
388 1.2 eeh struct timeval cur, flushtimeout;
389 1.2 eeh
390 1.2 eeh #define BUMPTIME(t, usec) { \
391 1.2 eeh register volatile struct timeval *tp = (t); \
392 1.2 eeh register long us; \
393 1.2 eeh \
394 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
395 1.2 eeh if (us >= 1000000) { \
396 1.2 eeh tp->tv_usec = us - 1000000; \
397 1.2 eeh tp->tv_sec++; \
398 1.2 eeh } \
399 1.2 eeh }
400 1.2 eeh
401 1.3.2.1 bouyer if (!is->is_sb)
402 1.3.2.1 bouyer return (0);
403 1.3.2.1 bouyer
404 1.3.2.1 bouyer /*
405 1.3.2.1 bouyer * Streaming buffer flushes:
406 1.3.2.1 bouyer *
407 1.3.2.1 bouyer * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
408 1.3.2.1 bouyer * we're not on a cache line boundary (64-bits):
409 1.3.2.1 bouyer * 2 Store 0 in flag
410 1.3.2.1 bouyer * 3 Store pointer to flag in flushsync
411 1.3.2.1 bouyer * 4 wait till flushsync becomes 0x1
412 1.3.2.1 bouyer *
413 1.3.2.1 bouyer * If it takes more than .5 sec, something
414 1.3.2.1 bouyer * went wrong.
415 1.3.2.1 bouyer */
416 1.3.2.1 bouyer
417 1.2 eeh is->is_flush = 0;
418 1.2 eeh membar_sync();
419 1.3.2.1 bouyer bus_space_write_8(is->is_bustag, (bus_space_handle_t)(u_long)
420 1.3.2.1 bouyer &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
421 1.2 eeh membar_sync();
422 1.2 eeh
423 1.2 eeh microtime(&flushtimeout);
424 1.2 eeh cur = flushtimeout;
425 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
426 1.2 eeh
427 1.3.2.4 bouyer DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
428 1.2 eeh (long)is->is_flush, (long)&is->is_flush,
429 1.2 eeh (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
430 1.3.2.1 bouyer flushtimeout.tv_sec, flushtimeout.tv_usec));
431 1.2 eeh /* Bypass non-coherent D$ */
432 1.2 eeh while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
433 1.2 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
434 1.2 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
435 1.2 eeh microtime(&cur);
436 1.2 eeh
437 1.2 eeh #ifdef DIAGNOSTIC
438 1.3.2.3 bouyer if (!ldxa(is->is_flushpa, ASI_PHYS_CACHED)) {
439 1.3.2.4 bouyer printf("iommu_strbuf_flush_done: flush timeout %p at %p\n",
440 1.3.2.2 bouyer (void *)(u_long)is->is_flush,
441 1.3.2.2 bouyer (void *)(u_long)is->is_flushpa); /* panic? */
442 1.2 eeh #ifdef DDB
443 1.2 eeh Debugger();
444 1.2 eeh #endif
445 1.2 eeh }
446 1.2 eeh #endif
447 1.3.2.4 bouyer DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
448 1.3.2.1 bouyer return (is->is_flush);
449 1.3.2.1 bouyer }
450 1.3.2.1 bouyer
451 1.3.2.1 bouyer /*
452 1.3.2.1 bouyer * IOMMU DVMA operations, common to SBUS and PCI.
453 1.3.2.1 bouyer */
454 1.3.2.1 bouyer int
455 1.3.2.1 bouyer iommu_dvmamap_load(t, is, map, buf, buflen, p, flags)
456 1.3.2.1 bouyer bus_dma_tag_t t;
457 1.3.2.1 bouyer struct iommu_state *is;
458 1.3.2.1 bouyer bus_dmamap_t map;
459 1.3.2.1 bouyer void *buf;
460 1.3.2.1 bouyer bus_size_t buflen;
461 1.3.2.1 bouyer struct proc *p;
462 1.3.2.1 bouyer int flags;
463 1.3.2.1 bouyer {
464 1.3.2.1 bouyer int s;
465 1.3.2.1 bouyer int err;
466 1.3.2.1 bouyer bus_size_t sgsize;
467 1.3.2.1 bouyer paddr_t curaddr;
468 1.3.2.1 bouyer u_long dvmaddr;
469 1.3.2.1 bouyer bus_size_t align, boundary;
470 1.3.2.1 bouyer vaddr_t vaddr = (vaddr_t)buf;
471 1.3.2.1 bouyer pmap_t pmap;
472 1.3.2.1 bouyer
473 1.3.2.1 bouyer if (map->dm_nsegs) {
474 1.3.2.1 bouyer /* Already in use?? */
475 1.3.2.1 bouyer #ifdef DIAGNOSTIC
476 1.3.2.1 bouyer printf("iommu_dvmamap_load: map still in use\n");
477 1.3.2.1 bouyer #endif
478 1.3.2.1 bouyer bus_dmamap_unload(t, map);
479 1.3.2.1 bouyer }
480 1.3.2.1 bouyer /*
481 1.3.2.1 bouyer * Make sure that on error condition we return "no valid mappings".
482 1.3.2.1 bouyer */
483 1.3.2.1 bouyer map->dm_nsegs = 0;
484 1.3.2.1 bouyer
485 1.3.2.1 bouyer if (buflen > map->_dm_size) {
486 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
487 1.3.2.1 bouyer ("iommu_dvmamap_load(): error %d > %d -- "
488 1.3.2.2 bouyer "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
489 1.3.2.1 bouyer return (EINVAL);
490 1.3.2.1 bouyer }
491 1.3.2.1 bouyer
492 1.3.2.1 bouyer sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
493 1.3.2.1 bouyer
494 1.3.2.1 bouyer /*
495 1.3.2.1 bouyer * A boundary presented to bus_dmamem_alloc() takes precedence
496 1.3.2.1 bouyer * over boundary in the map.
497 1.3.2.1 bouyer */
498 1.3.2.1 bouyer if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
499 1.3.2.1 bouyer boundary = map->_dm_boundary;
500 1.3.2.1 bouyer align = max(map->dm_segs[0]._ds_align, NBPG);
501 1.3.2.1 bouyer s = splhigh();
502 1.3.2.1 bouyer err = extent_alloc(is->is_dvmamap, sgsize, align,
503 1.3.2.1 bouyer boundary, EX_NOWAIT|EX_BOUNDZERO, (u_long *)&dvmaddr);
504 1.3.2.1 bouyer splx(s);
505 1.3.2.1 bouyer
506 1.3.2.1 bouyer #ifdef DEBUG
507 1.3.2.1 bouyer if (err || (dvmaddr == (bus_addr_t)-1))
508 1.3.2.1 bouyer {
509 1.3.2.1 bouyer printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
510 1.3.2.2 bouyer (int)sgsize, flags);
511 1.3.2.1 bouyer Debugger();
512 1.3.2.1 bouyer }
513 1.3.2.1 bouyer #endif
514 1.3.2.1 bouyer if (err != 0)
515 1.3.2.1 bouyer return (err);
516 1.3.2.1 bouyer
517 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
518 1.3.2.1 bouyer return (ENOMEM);
519 1.3.2.1 bouyer
520 1.3.2.1 bouyer /*
521 1.3.2.1 bouyer * We always use just one segment.
522 1.3.2.1 bouyer */
523 1.3.2.1 bouyer map->dm_mapsize = buflen;
524 1.3.2.1 bouyer map->dm_nsegs = 1;
525 1.3.2.1 bouyer map->dm_segs[0].ds_addr = dvmaddr + (vaddr & PGOFSET);
526 1.3.2.1 bouyer map->dm_segs[0].ds_len = buflen;
527 1.3.2.1 bouyer
528 1.3.2.1 bouyer if (p != NULL)
529 1.3.2.1 bouyer pmap = p->p_vmspace->vm_map.pmap;
530 1.3.2.1 bouyer else
531 1.3.2.1 bouyer pmap = pmap_kernel();
532 1.3.2.1 bouyer
533 1.3.2.1 bouyer dvmaddr = trunc_page(map->dm_segs[0].ds_addr);
534 1.3.2.1 bouyer for (; buflen > 0; ) {
535 1.3.2.1 bouyer /*
536 1.3.2.1 bouyer * Get the physical address for this page.
537 1.3.2.1 bouyer */
538 1.3.2.1 bouyer if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
539 1.3.2.1 bouyer bus_dmamap_unload(t, map);
540 1.3.2.1 bouyer return (-1);
541 1.3.2.1 bouyer }
542 1.3.2.1 bouyer
543 1.3.2.1 bouyer /*
544 1.3.2.1 bouyer * Compute the segment size, and adjust counts.
545 1.3.2.1 bouyer */
546 1.3.2.1 bouyer sgsize = NBPG - ((u_long)vaddr & PGOFSET);
547 1.3.2.1 bouyer if (buflen < sgsize)
548 1.3.2.1 bouyer sgsize = buflen;
549 1.3.2.1 bouyer
550 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
551 1.3.2.1 bouyer ("iommu_dvmamap_load: map %p loading va %p dva %lx at pa %lx\n",
552 1.3.2.1 bouyer map, (void *)vaddr, (long)dvmaddr, (long)(curaddr&~(NBPG-1))));
553 1.3.2.1 bouyer iommu_enter(is, trunc_page(dvmaddr), trunc_page(curaddr),
554 1.3.2.1 bouyer flags);
555 1.3.2.1 bouyer
556 1.3.2.1 bouyer dvmaddr += PAGE_SIZE;
557 1.3.2.1 bouyer vaddr += sgsize;
558 1.3.2.1 bouyer buflen -= sgsize;
559 1.3.2.1 bouyer }
560 1.3.2.1 bouyer return (0);
561 1.3.2.1 bouyer }
562 1.3.2.1 bouyer
563 1.3.2.1 bouyer
564 1.3.2.1 bouyer void
565 1.3.2.1 bouyer iommu_dvmamap_unload(t, is, map)
566 1.3.2.1 bouyer bus_dma_tag_t t;
567 1.3.2.1 bouyer struct iommu_state *is;
568 1.3.2.1 bouyer bus_dmamap_t map;
569 1.3.2.1 bouyer {
570 1.3.2.3 bouyer vaddr_t addr, offset;
571 1.3.2.1 bouyer size_t len;
572 1.3.2.3 bouyer int error, s, i;
573 1.3.2.1 bouyer bus_addr_t dvmaddr;
574 1.3.2.1 bouyer bus_size_t sgsize;
575 1.3.2.3 bouyer paddr_t pa;
576 1.3.2.1 bouyer
577 1.3.2.1 bouyer dvmaddr = (map->dm_segs[0].ds_addr & ~PGOFSET);
578 1.3.2.3 bouyer pa = 0;
579 1.3.2.3 bouyer sgsize = 0;
580 1.3.2.3 bouyer for (i = 0; i<map->dm_nsegs; i++) {
581 1.3.2.3 bouyer
582 1.3.2.3 bouyer addr = trunc_page(map->dm_segs[i].ds_addr);
583 1.3.2.3 bouyer offset = map->dm_segs[i].ds_addr & PGOFSET;
584 1.3.2.3 bouyer len = map->dm_segs[i].ds_len;
585 1.3.2.3 bouyer if (len == 0 || addr == 0)
586 1.3.2.3 bouyer printf("iommu_dvmamap_unload: map = %p, i = %d, len = %d, addr = %lx\n",
587 1.3.2.3 bouyer map, (int)i, (int)len, (unsigned long)addr);
588 1.3.2.1 bouyer
589 1.3.2.3 bouyer DPRINTF(IDB_BUSDMA,
590 1.3.2.3 bouyer ("iommu_dvmamap_unload: map %p removing va %lx size %lx\n",
591 1.3.2.3 bouyer map, (long)addr, (long)len));
592 1.3.2.3 bouyer iommu_remove(is, addr, len);
593 1.3.2.3 bouyer
594 1.3.2.3 bouyer if (trunc_page(pa) == addr)
595 1.3.2.3 bouyer sgsize += trunc_page(len + offset);
596 1.3.2.3 bouyer else
597 1.3.2.3 bouyer sgsize += round_page(len + offset);
598 1.3.2.3 bouyer pa = addr + offset + len;
599 1.3.2.3 bouyer
600 1.3.2.3 bouyer }
601 1.3.2.1 bouyer /* Flush the caches */
602 1.3.2.1 bouyer bus_dmamap_unload(t->_parent, map);
603 1.3.2.1 bouyer
604 1.3.2.1 bouyer /* Mark the mappings as invalid. */
605 1.3.2.1 bouyer map->dm_mapsize = 0;
606 1.3.2.1 bouyer map->dm_nsegs = 0;
607 1.3.2.1 bouyer
608 1.3.2.1 bouyer s = splhigh();
609 1.3.2.1 bouyer error = extent_free(is->is_dvmamap, dvmaddr, sgsize, EX_NOWAIT);
610 1.3.2.1 bouyer splx(s);
611 1.3.2.1 bouyer if (error != 0)
612 1.3.2.1 bouyer printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
613 1.3.2.1 bouyer }
614 1.3.2.1 bouyer
615 1.3.2.1 bouyer
616 1.3.2.1 bouyer int
617 1.3.2.1 bouyer iommu_dvmamap_load_raw(t, is, map, segs, nsegs, flags, size)
618 1.3.2.1 bouyer bus_dma_tag_t t;
619 1.3.2.1 bouyer struct iommu_state *is;
620 1.3.2.1 bouyer bus_dmamap_t map;
621 1.3.2.1 bouyer bus_dma_segment_t *segs;
622 1.3.2.1 bouyer int nsegs;
623 1.3.2.1 bouyer int flags;
624 1.3.2.1 bouyer bus_size_t size;
625 1.3.2.1 bouyer {
626 1.3.2.1 bouyer vm_page_t m;
627 1.3.2.3 bouyer int i, s;
628 1.3.2.3 bouyer int left;
629 1.3.2.1 bouyer int err;
630 1.3.2.1 bouyer bus_size_t sgsize;
631 1.3.2.1 bouyer paddr_t pa;
632 1.3.2.1 bouyer bus_size_t boundary, align;
633 1.3.2.1 bouyer u_long dvmaddr;
634 1.3.2.1 bouyer struct pglist *mlist;
635 1.3.2.1 bouyer int pagesz = PAGE_SIZE;
636 1.3.2.1 bouyer
637 1.3.2.1 bouyer if (map->dm_nsegs) {
638 1.3.2.1 bouyer /* Already in use?? */
639 1.3.2.1 bouyer #ifdef DIAGNOSTIC
640 1.3.2.1 bouyer printf("iommu_dvmamap_load_raw: map still in use\n");
641 1.3.2.1 bouyer #endif
642 1.3.2.1 bouyer bus_dmamap_unload(t, map);
643 1.3.2.1 bouyer }
644 1.3.2.1 bouyer /*
645 1.3.2.1 bouyer * Make sure that on error condition we return "no valid mappings".
646 1.3.2.1 bouyer */
647 1.3.2.1 bouyer map->dm_nsegs = 0;
648 1.3.2.3 bouyer /* Count up the total number of pages we need */
649 1.3.2.3 bouyer pa = segs[0].ds_addr;
650 1.3.2.3 bouyer sgsize = 0;
651 1.3.2.3 bouyer for (i=0; i<nsegs; i++) {
652 1.3.2.3 bouyer sgsize += segs[i].ds_len;
653 1.3.2.3 bouyer if (round_page(pa) != round_page(segs[i].ds_addr))
654 1.3.2.3 bouyer sgsize = round_page(sgsize);
655 1.3.2.3 bouyer pa = segs[i].ds_addr + segs[i].ds_len;
656 1.3.2.3 bouyer }
657 1.3.2.3 bouyer sgsize = round_page(sgsize);
658 1.3.2.1 bouyer
659 1.3.2.1 bouyer /*
660 1.3.2.1 bouyer * A boundary presented to bus_dmamem_alloc() takes precedence
661 1.3.2.1 bouyer * over boundary in the map.
662 1.3.2.1 bouyer */
663 1.3.2.1 bouyer if ((boundary = segs[0]._ds_boundary) == 0)
664 1.3.2.1 bouyer boundary = map->_dm_boundary;
665 1.3.2.1 bouyer
666 1.3.2.1 bouyer align = max(segs[0]._ds_align, NBPG);
667 1.3.2.1 bouyer s = splhigh();
668 1.3.2.1 bouyer err = extent_alloc(is->is_dvmamap, sgsize, align, boundary,
669 1.3.2.1 bouyer ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT)|EX_BOUNDZERO,
670 1.3.2.1 bouyer (u_long *)&dvmaddr);
671 1.3.2.1 bouyer splx(s);
672 1.3.2.1 bouyer
673 1.3.2.1 bouyer if (err != 0)
674 1.3.2.1 bouyer return (err);
675 1.3.2.1 bouyer
676 1.2 eeh #ifdef DEBUG
677 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
678 1.3.2.1 bouyer {
679 1.3.2.1 bouyer printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
680 1.3.2.2 bouyer (int)sgsize, flags);
681 1.3.2.1 bouyer Debugger();
682 1.3.2.1 bouyer }
683 1.3.2.1 bouyer #endif
684 1.3.2.1 bouyer if (dvmaddr == (bus_addr_t)-1)
685 1.3.2.1 bouyer return (ENOMEM);
686 1.3.2.1 bouyer
687 1.3.2.3 bouyer if ((mlist = segs[0]._ds_mlist) == NULL) {
688 1.3.2.3 bouyer u_long prev_va = NULL;
689 1.3.2.3 bouyer /*
690 1.3.2.3 bouyer * This segs is made up of individual physical pages,
691 1.3.2.3 bouyer * probably by _bus_dmamap_load_uio() or
692 1.3.2.3 bouyer * _bus_dmamap_load_mbuf(). Ignore the mlist and
693 1.3.2.3 bouyer * load each segment individually.
694 1.3.2.3 bouyer */
695 1.3.2.3 bouyer
696 1.3.2.3 bouyer /* We'll never end up with less segments than we got as input.
697 1.3.2.3 bouyer this gives us a chance to fail quickly */
698 1.3.2.3 bouyer if (nsegs > map->_dm_segcnt)
699 1.3.2.3 bouyer return (E2BIG);
700 1.3.2.3 bouyer
701 1.3.2.3 bouyer i = 0;
702 1.3.2.3 bouyer dvmaddr += (segs[i].ds_addr & PGOFSET);
703 1.3.2.3 bouyer map->dm_segs[i].ds_addr = dvmaddr;
704 1.3.2.3 bouyer map->dm_segs[i].ds_len = left = segs[i].ds_len;
705 1.3.2.3 bouyer pa = segs[i].ds_addr;
706 1.3.2.3 bouyer
707 1.3.2.3 bouyer while (left > 0) {
708 1.3.2.3 bouyer int incr;
709 1.3.2.3 bouyer
710 1.3.2.3 bouyer if (sgsize == 0)
711 1.3.2.3 bouyer panic("iommu_dmamap_load_raw: size botch");
712 1.3.2.3 bouyer DPRINTF(IDB_BUSDMA,
713 1.3.2.3 bouyer ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
714 1.3.2.3 bouyer map, (long)dvmaddr, (long)(pa)));
715 1.3.2.3 bouyer /* Enter it if we haven't before. */
716 1.3.2.3 bouyer if (prev_va != trunc_page(dvmaddr))
717 1.3.2.3 bouyer iommu_enter(is, prev_va = trunc_page(dvmaddr),
718 1.3.2.3 bouyer trunc_page(pa), flags);
719 1.3.2.3 bouyer incr = min(pagesz, left);
720 1.3.2.3 bouyer dvmaddr += incr;
721 1.3.2.3 bouyer pa += incr;
722 1.3.2.3 bouyer left -= incr;
723 1.3.2.3 bouyer
724 1.3.2.3 bouyer /* Next segment */
725 1.3.2.3 bouyer if (left <= 0 && ++i < nsegs) {
726 1.3.2.3 bouyer u_long offset;
727 1.3.2.3 bouyer
728 1.3.2.3 bouyer /*
729 1.3.2.3 bouyer * If the two segs are on different physical pages
730 1.3.2.3 bouyer * move to a new virtual page.
731 1.3.2.3 bouyer */
732 1.3.2.3 bouyer offset = (segs[i].ds_addr & PGOFSET);
733 1.3.2.3 bouyer if (trunc_page(pa) != trunc_page(segs[i].ds_addr))
734 1.3.2.3 bouyer dvmaddr += NBPG;
735 1.3.2.3 bouyer
736 1.3.2.3 bouyer pa = segs[i].ds_addr;
737 1.3.2.3 bouyer dvmaddr = trunc_page(dvmaddr) + offset;
738 1.3.2.3 bouyer
739 1.3.2.3 bouyer map->dm_segs[i].ds_len = left = segs[i].ds_len;
740 1.3.2.3 bouyer map->dm_segs[i].ds_addr = dvmaddr;
741 1.3.2.3 bouyer }
742 1.3.2.3 bouyer }
743 1.3.2.3 bouyer map->dm_nsegs = i;
744 1.3.2.3 bouyer
745 1.3.2.3 bouyer /* bail out if we created more segments than the dmamap is
746 1.3.2.3 bouyer allowed to carry */
747 1.3.2.3 bouyer if (i > map->_dm_segcnt) {
748 1.3.2.3 bouyer iommu_dvmamap_unload(t, is, map);
749 1.3.2.3 bouyer return (E2BIG);
750 1.3.2.3 bouyer }
751 1.3.2.3 bouyer
752 1.3.2.3 bouyer return (0);
753 1.3.2.3 bouyer }
754 1.3.2.1 bouyer /*
755 1.3.2.3 bouyer * This was allocated with bus_dmamem_alloc. We only
756 1.3.2.3 bouyer * have one segment, and the pages are on an `mlist'.
757 1.3.2.1 bouyer */
758 1.3.2.1 bouyer map->dm_mapsize = size;
759 1.3.2.3 bouyer i = 0;
760 1.3.2.1 bouyer map->dm_segs[0].ds_addr = dvmaddr;
761 1.3.2.1 bouyer map->dm_segs[0].ds_len = size;
762 1.3.2.1 bouyer
763 1.3.2.1 bouyer for (m = TAILQ_FIRST(mlist); m != NULL; m = TAILQ_NEXT(m,pageq)) {
764 1.3.2.1 bouyer if (sgsize == 0)
765 1.3.2.1 bouyer panic("iommu_dmamap_load_raw: size botch");
766 1.3.2.1 bouyer pa = VM_PAGE_TO_PHYS(m);
767 1.3.2.1 bouyer
768 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
769 1.3.2.1 bouyer ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
770 1.3.2.1 bouyer map, (long)dvmaddr, (long)(pa)));
771 1.3.2.1 bouyer iommu_enter(is, dvmaddr, pa, flags);
772 1.3.2.1 bouyer
773 1.3.2.1 bouyer dvmaddr += pagesz;
774 1.3.2.1 bouyer sgsize -= pagesz;
775 1.3.2.1 bouyer }
776 1.3.2.3 bouyer map->dm_nsegs = i;
777 1.3.2.1 bouyer return (0);
778 1.3.2.1 bouyer }
779 1.3.2.1 bouyer
780 1.3.2.1 bouyer void
781 1.3.2.1 bouyer iommu_dvmamap_sync(t, is, map, offset, len, ops)
782 1.3.2.1 bouyer bus_dma_tag_t t;
783 1.3.2.1 bouyer struct iommu_state *is;
784 1.3.2.1 bouyer bus_dmamap_t map;
785 1.3.2.1 bouyer bus_addr_t offset;
786 1.3.2.1 bouyer bus_size_t len;
787 1.3.2.1 bouyer int ops;
788 1.3.2.1 bouyer {
789 1.3.2.1 bouyer vaddr_t va = map->dm_segs[0].ds_addr + offset;
790 1.3.2.1 bouyer
791 1.3.2.1 bouyer /*
792 1.3.2.1 bouyer * We only support one DMA segment; supporting more makes this code
793 1.3.2.1 bouyer * too unweildy.
794 1.3.2.1 bouyer */
795 1.3.2.1 bouyer
796 1.3.2.1 bouyer if (ops & BUS_DMASYNC_PREREAD) {
797 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
798 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
799 1.3.2.2 bouyer "BUS_DMASYNC_PREREAD\n", (void *)(u_long)va, (u_long)len));
800 1.3.2.1 bouyer
801 1.3.2.1 bouyer /* Nothing to do */;
802 1.3.2.1 bouyer }
803 1.3.2.1 bouyer if (ops & BUS_DMASYNC_POSTREAD) {
804 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
805 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
806 1.3.2.2 bouyer "BUS_DMASYNC_POSTREAD\n", (void *)(u_long)va, (u_long)len));
807 1.3.2.1 bouyer /* if we have a streaming buffer, flush it here first */
808 1.3.2.1 bouyer if (is->is_sb)
809 1.3.2.1 bouyer while (len > 0) {
810 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
811 1.3.2.1 bouyer ("iommu_dvmamap_sync: flushing va %p, %lu "
812 1.3.2.2 bouyer "bytes left\n", (void *)(u_long)va, (u_long)len));
813 1.3.2.4 bouyer iommu_strbuf_flush(is, va);
814 1.3.2.1 bouyer if (len <= NBPG) {
815 1.3.2.4 bouyer iommu_strbuf_flush_done(is);
816 1.3.2.1 bouyer len = 0;
817 1.3.2.1 bouyer } else
818 1.3.2.1 bouyer len -= NBPG;
819 1.3.2.1 bouyer va += NBPG;
820 1.3.2.1 bouyer }
821 1.3.2.1 bouyer }
822 1.3.2.1 bouyer if (ops & BUS_DMASYNC_PREWRITE) {
823 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
824 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
825 1.3.2.2 bouyer "BUS_DMASYNC_PREWRITE\n", (void *)(u_long)va, (u_long)len));
826 1.3.2.4 bouyer /* if we have a streaming buffer, flush it here first */
827 1.3.2.4 bouyer if (is->is_sb)
828 1.3.2.4 bouyer while (len > 0) {
829 1.3.2.4 bouyer DPRINTF(IDB_BUSDMA,
830 1.3.2.4 bouyer ("iommu_dvmamap_sync: flushing va %p, %lu "
831 1.3.2.4 bouyer "bytes left\n", (void *)(u_long)va, (u_long)len));
832 1.3.2.4 bouyer iommu_strbuf_flush(is, va);
833 1.3.2.4 bouyer if (len <= NBPG) {
834 1.3.2.4 bouyer iommu_strbuf_flush_done(is);
835 1.3.2.4 bouyer len = 0;
836 1.3.2.4 bouyer } else
837 1.3.2.4 bouyer len -= NBPG;
838 1.3.2.4 bouyer va += NBPG;
839 1.3.2.4 bouyer }
840 1.3.2.1 bouyer }
841 1.3.2.1 bouyer if (ops & BUS_DMASYNC_POSTWRITE) {
842 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA,
843 1.3.2.1 bouyer ("iommu_dvmamap_sync: syncing va %p len %lu "
844 1.3.2.2 bouyer "BUS_DMASYNC_POSTWRITE\n", (void *)(u_long)va, (u_long)len));
845 1.3.2.1 bouyer /* Nothing to do */;
846 1.3.2.1 bouyer }
847 1.3.2.1 bouyer }
848 1.3.2.1 bouyer
849 1.3.2.1 bouyer int
850 1.3.2.1 bouyer iommu_dvmamem_alloc(t, is, size, alignment, boundary, segs, nsegs, rsegs, flags)
851 1.3.2.1 bouyer bus_dma_tag_t t;
852 1.3.2.1 bouyer struct iommu_state *is;
853 1.3.2.1 bouyer bus_size_t size, alignment, boundary;
854 1.3.2.1 bouyer bus_dma_segment_t *segs;
855 1.3.2.1 bouyer int nsegs;
856 1.3.2.1 bouyer int *rsegs;
857 1.3.2.1 bouyer int flags;
858 1.3.2.1 bouyer {
859 1.3.2.1 bouyer
860 1.3.2.2 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
861 1.3.2.2 bouyer "segp %p flags %d\n", (unsigned long long)size,
862 1.3.2.2 bouyer (unsigned long long)alignment, (unsigned long long)boundary,
863 1.3.2.2 bouyer segs, flags));
864 1.3.2.1 bouyer return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
865 1.3.2.1 bouyer segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
866 1.3.2.1 bouyer }
867 1.3.2.1 bouyer
868 1.3.2.1 bouyer void
869 1.3.2.1 bouyer iommu_dvmamem_free(t, is, segs, nsegs)
870 1.3.2.1 bouyer bus_dma_tag_t t;
871 1.3.2.1 bouyer struct iommu_state *is;
872 1.3.2.1 bouyer bus_dma_segment_t *segs;
873 1.3.2.1 bouyer int nsegs;
874 1.3.2.1 bouyer {
875 1.3.2.1 bouyer
876 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
877 1.3.2.1 bouyer segs, nsegs));
878 1.3.2.1 bouyer bus_dmamem_free(t->_parent, segs, nsegs);
879 1.3.2.1 bouyer }
880 1.3.2.1 bouyer
881 1.3.2.1 bouyer /*
882 1.3.2.1 bouyer * Map the DVMA mappings into the kernel pmap.
883 1.3.2.1 bouyer * Check the flags to see whether we're streaming or coherent.
884 1.3.2.1 bouyer */
885 1.3.2.1 bouyer int
886 1.3.2.1 bouyer iommu_dvmamem_map(t, is, segs, nsegs, size, kvap, flags)
887 1.3.2.1 bouyer bus_dma_tag_t t;
888 1.3.2.1 bouyer struct iommu_state *is;
889 1.3.2.1 bouyer bus_dma_segment_t *segs;
890 1.3.2.1 bouyer int nsegs;
891 1.3.2.1 bouyer size_t size;
892 1.3.2.1 bouyer caddr_t *kvap;
893 1.3.2.1 bouyer int flags;
894 1.3.2.1 bouyer {
895 1.3.2.1 bouyer vm_page_t m;
896 1.3.2.1 bouyer vaddr_t va;
897 1.3.2.1 bouyer bus_addr_t addr;
898 1.3.2.1 bouyer struct pglist *mlist;
899 1.3.2.1 bouyer int cbit;
900 1.3.2.1 bouyer
901 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
902 1.3.2.1 bouyer segs, nsegs, size));
903 1.3.2.1 bouyer
904 1.3.2.1 bouyer /*
905 1.3.2.1 bouyer * Allocate some space in the kernel map, and then map these pages
906 1.3.2.1 bouyer * into this space.
907 1.3.2.1 bouyer */
908 1.3.2.1 bouyer size = round_page(size);
909 1.3.2.1 bouyer va = uvm_km_valloc(kernel_map, size);
910 1.3.2.1 bouyer if (va == 0)
911 1.3.2.1 bouyer return (ENOMEM);
912 1.3.2.1 bouyer
913 1.3.2.1 bouyer *kvap = (caddr_t)va;
914 1.3.2.1 bouyer
915 1.3.2.1 bouyer /*
916 1.3.2.1 bouyer * digest flags:
917 1.3.2.1 bouyer */
918 1.3.2.1 bouyer cbit = 0;
919 1.3.2.1 bouyer if (flags & BUS_DMA_COHERENT) /* Disable vcache */
920 1.3.2.1 bouyer cbit |= PMAP_NVC;
921 1.3.2.1 bouyer if (flags & BUS_DMA_NOCACHE) /* sideffects */
922 1.3.2.1 bouyer cbit |= PMAP_NC;
923 1.3.2.1 bouyer
924 1.3.2.1 bouyer /*
925 1.3.2.1 bouyer * Now take this and map it into the CPU.
926 1.3.2.1 bouyer */
927 1.3.2.1 bouyer mlist = segs[0]._ds_mlist;
928 1.3.2.1 bouyer for (m = mlist->tqh_first; m != NULL; m = m->pageq.tqe_next) {
929 1.3.2.1 bouyer #ifdef DIAGNOSTIC
930 1.3.2.1 bouyer if (size == 0)
931 1.3.2.1 bouyer panic("iommu_dvmamem_map: size botch");
932 1.3.2.1 bouyer #endif
933 1.3.2.1 bouyer addr = VM_PAGE_TO_PHYS(m);
934 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
935 1.3.2.2 bouyer "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
936 1.3.2.1 bouyer pmap_enter(pmap_kernel(), va, addr | cbit,
937 1.3.2.1 bouyer VM_PROT_READ | VM_PROT_WRITE,
938 1.3.2.1 bouyer VM_PROT_READ | VM_PROT_WRITE | PMAP_WIRED);
939 1.3.2.1 bouyer va += PAGE_SIZE;
940 1.3.2.1 bouyer size -= PAGE_SIZE;
941 1.3.2.1 bouyer }
942 1.3.2.1 bouyer
943 1.3.2.1 bouyer return (0);
944 1.3.2.1 bouyer }
945 1.3.2.1 bouyer
946 1.3.2.1 bouyer /*
947 1.3.2.1 bouyer * Unmap DVMA mappings from kernel
948 1.3.2.1 bouyer */
949 1.3.2.1 bouyer void
950 1.3.2.1 bouyer iommu_dvmamem_unmap(t, is, kva, size)
951 1.3.2.1 bouyer bus_dma_tag_t t;
952 1.3.2.1 bouyer struct iommu_state *is;
953 1.3.2.1 bouyer caddr_t kva;
954 1.3.2.1 bouyer size_t size;
955 1.3.2.1 bouyer {
956 1.3.2.1 bouyer
957 1.3.2.1 bouyer DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
958 1.3.2.1 bouyer kva, size));
959 1.3.2.1 bouyer
960 1.3.2.1 bouyer #ifdef DIAGNOSTIC
961 1.3.2.1 bouyer if ((u_long)kva & PGOFSET)
962 1.3.2.1 bouyer panic("iommu_dvmamem_unmap");
963 1.3.2.1 bouyer #endif
964 1.3.2.1 bouyer
965 1.3.2.1 bouyer size = round_page(size);
966 1.3.2.1 bouyer pmap_remove(pmap_kernel(), (vaddr_t)kva, size);
967 1.3.2.1 bouyer #if 0
968 1.3.2.1 bouyer /*
969 1.3.2.1 bouyer * XXX ? is this necessary? i think so and i think other
970 1.3.2.1 bouyer * implementations are missing it.
971 1.3.2.1 bouyer */
972 1.3.2.1 bouyer uvm_km_free(kernel_map, (vaddr_t)kva, size);
973 1.2 eeh #endif
974 1.1 mrg }
975