iommu.c revision 1.88 1 1.88 cegger /* $NetBSD: iommu.c,v 1.88 2009/11/07 07:27:46 cegger Exp $ */
2 1.82 mrg
3 1.82 mrg /*
4 1.82 mrg * Copyright (c) 1999, 2000 Matthew R. Green
5 1.82 mrg * All rights reserved.
6 1.82 mrg *
7 1.82 mrg * Redistribution and use in source and binary forms, with or without
8 1.82 mrg * modification, are permitted provided that the following conditions
9 1.82 mrg * are met:
10 1.82 mrg * 1. Redistributions of source code must retain the above copyright
11 1.82 mrg * notice, this list of conditions and the following disclaimer.
12 1.82 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.82 mrg * notice, this list of conditions and the following disclaimer in the
14 1.82 mrg * documentation and/or other materials provided with the distribution.
15 1.82 mrg *
16 1.82 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.82 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.82 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.82 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.82 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.82 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.82 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.82 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.82 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.82 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.82 mrg * SUCH DAMAGE.
27 1.82 mrg */
28 1.7 mrg
29 1.7 mrg /*
30 1.48 eeh * Copyright (c) 2001, 2002 Eduardo Horvath
31 1.7 mrg * All rights reserved.
32 1.7 mrg *
33 1.7 mrg * Redistribution and use in source and binary forms, with or without
34 1.7 mrg * modification, are permitted provided that the following conditions
35 1.7 mrg * are met:
36 1.7 mrg * 1. Redistributions of source code must retain the above copyright
37 1.7 mrg * notice, this list of conditions and the following disclaimer.
38 1.7 mrg * 2. Redistributions in binary form must reproduce the above copyright
39 1.7 mrg * notice, this list of conditions and the following disclaimer in the
40 1.7 mrg * documentation and/or other materials provided with the distribution.
41 1.7 mrg * 3. The name of the author may not be used to endorse or promote products
42 1.7 mrg * derived from this software without specific prior written permission.
43 1.7 mrg *
44 1.7 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 1.7 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 1.7 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 1.7 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 1.7 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
49 1.7 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
50 1.7 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
51 1.7 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
52 1.7 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 1.7 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 1.7 mrg * SUCH DAMAGE.
55 1.7 mrg */
56 1.1 mrg
57 1.7 mrg /*
58 1.7 mrg * UltraSPARC IOMMU support; used by both the sbus and pci code.
59 1.7 mrg */
60 1.66 lukem
61 1.66 lukem #include <sys/cdefs.h>
62 1.88 cegger __KERNEL_RCSID(0, "$NetBSD: iommu.c,v 1.88 2009/11/07 07:27:46 cegger Exp $");
63 1.66 lukem
64 1.4 mrg #include "opt_ddb.h"
65 1.4 mrg
66 1.1 mrg #include <sys/param.h>
67 1.1 mrg #include <sys/extent.h>
68 1.1 mrg #include <sys/malloc.h>
69 1.1 mrg #include <sys/systm.h>
70 1.1 mrg #include <sys/device.h>
71 1.41 chs #include <sys/proc.h>
72 1.18 mrg
73 1.18 mrg #include <uvm/uvm_extern.h>
74 1.1 mrg
75 1.1 mrg #include <machine/bus.h>
76 1.1 mrg #include <sparc64/dev/iommureg.h>
77 1.1 mrg #include <sparc64/dev/iommuvar.h>
78 1.1 mrg
79 1.1 mrg #include <machine/autoconf.h>
80 1.1 mrg #include <machine/cpu.h>
81 1.1 mrg
82 1.1 mrg #ifdef DEBUG
83 1.22 mrg #define IDB_BUSDMA 0x1
84 1.22 mrg #define IDB_IOMMU 0x2
85 1.22 mrg #define IDB_INFO 0x4
86 1.36 eeh #define IDB_SYNC 0x8
87 1.10 mrg int iommudebug = 0x0;
88 1.4 mrg #define DPRINTF(l, s) do { if (iommudebug & l) printf s; } while (0)
89 1.4 mrg #else
90 1.4 mrg #define DPRINTF(l, s)
91 1.1 mrg #endif
92 1.1 mrg
93 1.55 eeh #define iommu_strbuf_flush(i, v) do { \
94 1.55 eeh if ((i)->sb_flush) \
95 1.55 eeh bus_space_write_8((i)->sb_is->is_bustag, (i)->sb_sb, \
96 1.50 eeh STRBUFREG(strbuf_pgflush), (v)); \
97 1.42 eeh } while (0)
98 1.42 eeh
99 1.78 cdi static int iommu_strbuf_flush_done(struct strbuf_ctl *);
100 1.85 nakayama static void _iommu_dvmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
101 1.85 nakayama bus_size_t, int);
102 1.11 eeh
103 1.1 mrg /*
104 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
105 1.1 mrg * - allocate and setup the iotsb.
106 1.1 mrg * - enable the IOMMU
107 1.7 mrg * - initialise the streaming buffers (if they exist)
108 1.1 mrg * - create a private DVMA map.
109 1.1 mrg */
110 1.1 mrg void
111 1.79 cdi iommu_init(char *name, struct iommu_state *is, int tsbsize, uint32_t iovabase)
112 1.1 mrg {
113 1.11 eeh psize_t size;
114 1.11 eeh vaddr_t va;
115 1.11 eeh paddr_t pa;
116 1.58 chs struct vm_page *pg;
117 1.58 chs struct pglist pglist;
118 1.1 mrg
119 1.1 mrg /*
120 1.1 mrg * Setup the iommu.
121 1.1 mrg *
122 1.45 eeh * The sun4u iommu is part of the SBUS or PCI controller so we will
123 1.45 eeh * deal with it here..
124 1.1 mrg *
125 1.45 eeh * For sysio and psycho/psycho+ the IOMMU address space always ends at
126 1.45 eeh * 0xffffe000, but the starting address depends on the size of the
127 1.45 eeh * map. The map size is 1024 * 2 ^ is->is_tsbsize entries, where each
128 1.45 eeh * entry is 8 bytes. The start of the map can be calculated by
129 1.45 eeh * (0xffffe000 << (8 + is->is_tsbsize)).
130 1.45 eeh *
131 1.45 eeh * But sabre and hummingbird use a different scheme that seems to
132 1.45 eeh * be hard-wired, so we read the start and size from the PROM and
133 1.45 eeh * just use those values.
134 1.2 eeh */
135 1.11 eeh is->is_cr = (tsbsize << 16) | IOMMUCR_EN;
136 1.11 eeh is->is_tsbsize = tsbsize;
137 1.45 eeh if (iovabase == -1) {
138 1.45 eeh is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize);
139 1.45 eeh is->is_dvmaend = IOTSB_VEND;
140 1.45 eeh } else {
141 1.45 eeh is->is_dvmabase = iovabase;
142 1.45 eeh is->is_dvmaend = iovabase + IOTSB_VSIZE(tsbsize);
143 1.45 eeh }
144 1.11 eeh
145 1.11 eeh /*
146 1.15 eeh * Allocate memory for I/O pagetables. They need to be physically
147 1.15 eeh * contiguous.
148 1.11 eeh */
149 1.11 eeh
150 1.64 thorpej size = PAGE_SIZE << is->is_tsbsize;
151 1.11 eeh if (uvm_pglistalloc((psize_t)size, (paddr_t)0, (paddr_t)-1,
152 1.64 thorpej (paddr_t)PAGE_SIZE, (paddr_t)0, &pglist, 1, 0) != 0)
153 1.11 eeh panic("iommu_init: no memory");
154 1.11 eeh
155 1.76 yamt va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
156 1.11 eeh if (va == 0)
157 1.11 eeh panic("iommu_init: no memory");
158 1.11 eeh is->is_tsb = (int64_t *)va;
159 1.11 eeh
160 1.58 chs is->is_ptsb = VM_PAGE_TO_PHYS(TAILQ_FIRST(&pglist));
161 1.11 eeh
162 1.11 eeh /* Map the pages */
163 1.83 ad TAILQ_FOREACH(pg, &pglist, pageq.queue) {
164 1.58 chs pa = VM_PAGE_TO_PHYS(pg);
165 1.88 cegger pmap_kenter_pa(va, pa | PMAP_NVC,
166 1.88 cegger VM_PROT_READ | VM_PROT_WRITE, 0);
167 1.64 thorpej va += PAGE_SIZE;
168 1.11 eeh }
169 1.38 chris pmap_update(pmap_kernel());
170 1.58 chs memset(is->is_tsb, 0, size);
171 1.1 mrg
172 1.1 mrg #ifdef DEBUG
173 1.22 mrg if (iommudebug & IDB_INFO)
174 1.1 mrg {
175 1.1 mrg /* Probe the iommu */
176 1.1 mrg
177 1.25 mrg printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n",
178 1.50 eeh (u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
179 1.50 eeh offsetof (struct iommureg, iommu_cr)),
180 1.50 eeh (u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
181 1.50 eeh offsetof (struct iommureg, iommu_tsb)),
182 1.50 eeh (u_long)bus_space_read_8(is->is_bustag, is->is_iommu,
183 1.50 eeh offsetof (struct iommureg, iommu_flush)));
184 1.50 eeh printf("iommu cr=%llx tsb=%llx\n",
185 1.50 eeh (unsigned long long)bus_space_read_8(is->is_bustag,
186 1.50 eeh is->is_iommu,
187 1.50 eeh offsetof (struct iommureg, iommu_cr)),
188 1.50 eeh (unsigned long long)bus_space_read_8(is->is_bustag,
189 1.50 eeh is->is_iommu,
190 1.50 eeh offsetof (struct iommureg, iommu_tsb)));
191 1.58 chs printf("TSB base %p phys %llx\n", (void *)is->is_tsb,
192 1.50 eeh (unsigned long long)is->is_ptsb);
193 1.1 mrg delay(1000000); /* 1 s */
194 1.1 mrg }
195 1.1 mrg #endif
196 1.1 mrg
197 1.1 mrg /*
198 1.1 mrg * now actually start up the IOMMU
199 1.1 mrg */
200 1.1 mrg iommu_reset(is);
201 1.1 mrg
202 1.1 mrg /*
203 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
204 1.1 mrg */
205 1.58 chs printf("DVMA map: %x to %x\n",
206 1.11 eeh (unsigned int)is->is_dvmabase,
207 1.45 eeh (unsigned int)is->is_dvmaend);
208 1.58 chs printf("IOTSB: %llx to %llx\n",
209 1.47 eeh (unsigned long long)is->is_ptsb,
210 1.47 eeh (unsigned long long)(is->is_ptsb + size));
211 1.1 mrg is->is_dvmamap = extent_create(name,
212 1.64 thorpej is->is_dvmabase, is->is_dvmaend - PAGE_SIZE,
213 1.64 thorpej M_DEVBUF, 0, 0, EX_NOWAIT);
214 1.1 mrg }
215 1.1 mrg
216 1.8 mrg /*
217 1.8 mrg * Streaming buffers don't exist on the UltraSPARC IIi; we should have
218 1.8 mrg * detected that already and disabled them. If not, we will notice that
219 1.8 mrg * they aren't there when the STRBUF_EN bit does not remain.
220 1.8 mrg */
221 1.1 mrg void
222 1.78 cdi iommu_reset(struct iommu_state *is)
223 1.1 mrg {
224 1.45 eeh int i;
225 1.55 eeh struct strbuf_ctl *sb;
226 1.1 mrg
227 1.1 mrg /* Need to do 64-bit stores */
228 1.58 chs bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_tsb),
229 1.50 eeh is->is_ptsb);
230 1.50 eeh
231 1.11 eeh /* Enable IOMMU in diagnostic mode */
232 1.50 eeh bus_space_write_8(is->is_bustag, is->is_iommu, IOMMUREG(iommu_cr),
233 1.50 eeh is->is_cr|IOMMUCR_DE);
234 1.11 eeh
235 1.58 chs for (i = 0; i < 2; i++) {
236 1.55 eeh if ((sb = is->is_sb[i])) {
237 1.5 mrg
238 1.45 eeh /* Enable diagnostics mode? */
239 1.58 chs bus_space_write_8(is->is_bustag, is->is_sb[i]->sb_sb,
240 1.50 eeh STRBUFREG(strbuf_ctl), STRBUF_EN);
241 1.45 eeh
242 1.45 eeh /* No streaming buffers? Disable them */
243 1.58 chs if (bus_space_read_8(is->is_bustag,
244 1.58 chs is->is_sb[i]->sb_sb,
245 1.55 eeh STRBUFREG(strbuf_ctl)) == 0) {
246 1.55 eeh is->is_sb[i]->sb_flush = NULL;
247 1.55 eeh } else {
248 1.58 chs
249 1.55 eeh /*
250 1.55 eeh * locate the pa of the flush buffer.
251 1.55 eeh */
252 1.55 eeh (void)pmap_extract(pmap_kernel(),
253 1.55 eeh (vaddr_t)is->is_sb[i]->sb_flush,
254 1.55 eeh &is->is_sb[i]->sb_flushpa);
255 1.55 eeh }
256 1.45 eeh }
257 1.42 eeh }
258 1.2 eeh }
259 1.2 eeh
260 1.2 eeh /*
261 1.58 chs * Here are the iommu control routines.
262 1.2 eeh */
263 1.2 eeh void
264 1.78 cdi iommu_enter(struct strbuf_ctl *sb, vaddr_t va, int64_t pa, int flags)
265 1.2 eeh {
266 1.55 eeh struct iommu_state *is = sb->sb_is;
267 1.55 eeh int strbuf = (flags & BUS_DMA_STREAMING);
268 1.2 eeh int64_t tte;
269 1.2 eeh
270 1.2 eeh #ifdef DIAGNOSTIC
271 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
272 1.13 mrg panic("iommu_enter: va %#lx not in DVMA space", va);
273 1.2 eeh #endif
274 1.2 eeh
275 1.55 eeh /* Is the streamcache flush really needed? */
276 1.55 eeh if (sb->sb_flush) {
277 1.55 eeh iommu_strbuf_flush(sb, va);
278 1.55 eeh iommu_strbuf_flush_done(sb);
279 1.55 eeh } else
280 1.55 eeh /* If we can't flush the strbuf don't enable it. */
281 1.55 eeh strbuf = 0;
282 1.55 eeh
283 1.58 chs tte = MAKEIOTTE(pa, !(flags & BUS_DMA_NOWRITE),
284 1.55 eeh !(flags & BUS_DMA_NOCACHE), (strbuf));
285 1.50 eeh #ifdef DEBUG
286 1.50 eeh tte |= (flags & 0xff000LL)<<(4*8);
287 1.50 eeh #endif
288 1.58 chs
289 1.58 chs DPRINTF(IDB_IOMMU, ("Clearing TSB slot %d for va %p\n",
290 1.25 mrg (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va));
291 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
292 1.58 chs bus_space_write_8(is->is_bustag, is->is_iommu,
293 1.50 eeh IOMMUREG(iommu_flush), va);
294 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
295 1.50 eeh va, (long)pa, (u_long)IOTSBSLOT(va,is->is_tsbsize),
296 1.50 eeh (void *)(u_long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
297 1.50 eeh (u_long)tte));
298 1.39 eeh }
299 1.39 eeh
300 1.39 eeh /*
301 1.39 eeh * Find the value of a DVMA address (debug routine).
302 1.39 eeh */
303 1.39 eeh paddr_t
304 1.78 cdi iommu_extract(struct iommu_state *is, vaddr_t dva)
305 1.39 eeh {
306 1.39 eeh int64_t tte = 0;
307 1.58 chs
308 1.45 eeh if (dva >= is->is_dvmabase && dva < is->is_dvmaend)
309 1.55 eeh tte = is->is_tsb[IOTSBSLOT(dva, is->is_tsbsize)];
310 1.39 eeh
311 1.54 eeh if ((tte & IOTTE_V) == 0)
312 1.39 eeh return ((paddr_t)-1L);
313 1.54 eeh return (tte & IOTTE_PAMASK);
314 1.2 eeh }
315 1.2 eeh
316 1.2 eeh /*
317 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
318 1.2 eeh *
319 1.2 eeh * Only demap from IOMMU if flag is set.
320 1.8 mrg *
321 1.8 mrg * XXX: this function needs better internal error checking.
322 1.2 eeh */
323 1.2 eeh void
324 1.78 cdi iommu_remove(struct iommu_state *is, vaddr_t va, size_t len)
325 1.2 eeh {
326 1.2 eeh
327 1.2 eeh #ifdef DIAGNOSTIC
328 1.45 eeh if (va < is->is_dvmabase || va > is->is_dvmaend)
329 1.25 mrg panic("iommu_remove: va 0x%lx not in DVMA space", (u_long)va);
330 1.2 eeh if ((long)(va + len) < (long)va)
331 1.58 chs panic("iommu_remove: va 0x%lx + len 0x%lx wraps",
332 1.2 eeh (long) va, (long) len);
333 1.58 chs if (len & ~0xfffffff)
334 1.72 snj panic("iommu_remove: ridiculous len 0x%lx", (u_long)len);
335 1.2 eeh #endif
336 1.2 eeh
337 1.2 eeh va = trunc_page(va);
338 1.22 mrg DPRINTF(IDB_IOMMU, ("iommu_remove: va %lx TSB[%lx]@%p\n",
339 1.50 eeh va, (u_long)IOTSBSLOT(va, is->is_tsbsize),
340 1.50 eeh &is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)]));
341 1.2 eeh while (len > 0) {
342 1.50 eeh DPRINTF(IDB_IOMMU, ("iommu_remove: clearing TSB slot %d "
343 1.50 eeh "for va %p size %lx\n",
344 1.50 eeh (int)IOTSBSLOT(va,is->is_tsbsize), (void *)(u_long)va,
345 1.50 eeh (u_long)len));
346 1.64 thorpej if (len <= PAGE_SIZE)
347 1.10 mrg len = 0;
348 1.10 mrg else
349 1.64 thorpej len -= PAGE_SIZE;
350 1.8 mrg
351 1.47 eeh /* XXX Zero-ing the entry would not require RMW */
352 1.47 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] &= ~IOTTE_V;
353 1.58 chs bus_space_write_8(is->is_bustag, is->is_iommu,
354 1.50 eeh IOMMUREG(iommu_flush), va);
355 1.64 thorpej va += PAGE_SIZE;
356 1.2 eeh }
357 1.2 eeh }
358 1.2 eeh
359 1.58 chs static int
360 1.78 cdi iommu_strbuf_flush_done(struct strbuf_ctl *sb)
361 1.2 eeh {
362 1.55 eeh struct iommu_state *is = sb->sb_is;
363 1.2 eeh struct timeval cur, flushtimeout;
364 1.2 eeh
365 1.2 eeh #define BUMPTIME(t, usec) { \
366 1.2 eeh register volatile struct timeval *tp = (t); \
367 1.2 eeh register long us; \
368 1.2 eeh \
369 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
370 1.2 eeh if (us >= 1000000) { \
371 1.2 eeh tp->tv_usec = us - 1000000; \
372 1.2 eeh tp->tv_sec++; \
373 1.2 eeh } \
374 1.2 eeh }
375 1.5 mrg
376 1.55 eeh if (!sb->sb_flush)
377 1.5 mrg return (0);
378 1.58 chs
379 1.7 mrg /*
380 1.7 mrg * Streaming buffer flushes:
381 1.58 chs *
382 1.7 mrg * 1 Tell strbuf to flush by storing va to strbuf_pgflush. If
383 1.7 mrg * we're not on a cache line boundary (64-bits):
384 1.7 mrg * 2 Store 0 in flag
385 1.7 mrg * 3 Store pointer to flag in flushsync
386 1.7 mrg * 4 wait till flushsync becomes 0x1
387 1.7 mrg *
388 1.7 mrg * If it takes more than .5 sec, something
389 1.7 mrg * went wrong.
390 1.7 mrg */
391 1.2 eeh
392 1.55 eeh *sb->sb_flush = 0;
393 1.58 chs bus_space_write_8(is->is_bustag, sb->sb_sb,
394 1.55 eeh STRBUFREG(strbuf_flushsync), sb->sb_flushpa);
395 1.2 eeh
396 1.58 chs microtime(&flushtimeout);
397 1.2 eeh cur = flushtimeout;
398 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
399 1.58 chs
400 1.55 eeh DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flush = %lx "
401 1.86 martin "at va = %lx pa = %lx now=%"PRIx64":%"PRIx32" until = %"PRIx64":%"PRIx32"\n",
402 1.58 chs (long)*sb->sb_flush, (long)sb->sb_flush, (long)sb->sb_flushpa,
403 1.42 eeh cur.tv_sec, cur.tv_usec,
404 1.42 eeh flushtimeout.tv_sec, flushtimeout.tv_usec));
405 1.42 eeh
406 1.2 eeh /* Bypass non-coherent D$ */
407 1.55 eeh while ((!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) &&
408 1.59 martin timercmp(&cur, &flushtimeout, <=))
409 1.2 eeh microtime(&cur);
410 1.2 eeh
411 1.2 eeh #ifdef DIAGNOSTIC
412 1.55 eeh if (!ldxa(sb->sb_flushpa, ASI_PHYS_CACHED)) {
413 1.55 eeh printf("iommu_strbuf_flush_done: flush timeout %p, at %p\n",
414 1.55 eeh (void *)(u_long)*sb->sb_flush,
415 1.55 eeh (void *)(u_long)sb->sb_flushpa); /* panic? */
416 1.2 eeh #ifdef DDB
417 1.2 eeh Debugger();
418 1.2 eeh #endif
419 1.2 eeh }
420 1.2 eeh #endif
421 1.31 eeh DPRINTF(IDB_IOMMU, ("iommu_strbuf_flush_done: flushed\n"));
422 1.55 eeh return (*sb->sb_flush);
423 1.7 mrg }
424 1.7 mrg
425 1.7 mrg /*
426 1.7 mrg * IOMMU DVMA operations, common to SBUS and PCI.
427 1.7 mrg */
428 1.7 mrg int
429 1.85 nakayama iommu_dvmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
430 1.85 nakayama bus_size_t buflen, struct proc *p, int flags)
431 1.7 mrg {
432 1.85 nakayama struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
433 1.55 eeh struct iommu_state *is = sb->sb_is;
434 1.7 mrg int s;
435 1.7 mrg int err;
436 1.7 mrg bus_size_t sgsize;
437 1.7 mrg paddr_t curaddr;
438 1.40 eeh u_long dvmaddr, sgstart, sgend;
439 1.71 tsutsui bus_size_t align, boundary, len;
440 1.7 mrg vaddr_t vaddr = (vaddr_t)buf;
441 1.40 eeh int seg;
442 1.58 chs struct pmap *pmap;
443 1.7 mrg
444 1.7 mrg if (map->dm_nsegs) {
445 1.7 mrg /* Already in use?? */
446 1.7 mrg #ifdef DIAGNOSTIC
447 1.7 mrg printf("iommu_dvmamap_load: map still in use\n");
448 1.7 mrg #endif
449 1.7 mrg bus_dmamap_unload(t, map);
450 1.7 mrg }
451 1.58 chs
452 1.7 mrg /*
453 1.7 mrg * Make sure that on error condition we return "no valid mappings".
454 1.7 mrg */
455 1.7 mrg map->dm_nsegs = 0;
456 1.7 mrg if (buflen > map->_dm_size) {
457 1.22 mrg DPRINTF(IDB_BUSDMA,
458 1.7 mrg ("iommu_dvmamap_load(): error %d > %d -- "
459 1.25 mrg "map size exceeded!\n", (int)buflen, (int)map->_dm_size));
460 1.7 mrg return (EINVAL);
461 1.7 mrg }
462 1.7 mrg
463 1.7 mrg sgsize = round_page(buflen + ((int)vaddr & PGOFSET));
464 1.20 mrg
465 1.7 mrg /*
466 1.21 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
467 1.21 eeh * over boundary in the map.
468 1.7 mrg */
469 1.21 eeh if ((boundary = (map->dm_segs[0]._ds_boundary)) == 0)
470 1.21 eeh boundary = map->_dm_boundary;
471 1.64 thorpej align = max(map->dm_segs[0]._ds_align, PAGE_SIZE);
472 1.58 chs
473 1.58 chs /*
474 1.58 chs * If our segment size is larger than the boundary we need to
475 1.40 eeh * split the transfer up int little pieces ourselves.
476 1.40 eeh */
477 1.58 chs s = splhigh();
478 1.58 chs err = extent_alloc(is->is_dvmamap, sgsize, align,
479 1.71 tsutsui (sgsize > boundary) ? 0 : boundary,
480 1.71 tsutsui EX_NOWAIT|EX_BOUNDZERO, &dvmaddr);
481 1.7 mrg splx(s);
482 1.7 mrg
483 1.7 mrg #ifdef DEBUG
484 1.71 tsutsui if (err || (dvmaddr == (u_long)-1)) {
485 1.7 mrg printf("iommu_dvmamap_load(): extent_alloc(%d, %x) failed!\n",
486 1.25 mrg (int)sgsize, flags);
487 1.40 eeh #ifdef DDB
488 1.7 mrg Debugger();
489 1.40 eeh #endif
490 1.58 chs }
491 1.58 chs #endif
492 1.11 eeh if (err != 0)
493 1.11 eeh return (err);
494 1.11 eeh
495 1.65 nakayama if (dvmaddr == (u_long)-1)
496 1.7 mrg return (ENOMEM);
497 1.7 mrg
498 1.40 eeh /* Set the active DVMA map */
499 1.40 eeh map->_dm_dvmastart = dvmaddr;
500 1.40 eeh map->_dm_dvmasize = sgsize;
501 1.40 eeh
502 1.40 eeh /*
503 1.40 eeh * Now split the DVMA range into segments, not crossing
504 1.40 eeh * the boundary.
505 1.40 eeh */
506 1.40 eeh seg = 0;
507 1.40 eeh sgstart = dvmaddr + (vaddr & PGOFSET);
508 1.40 eeh sgend = sgstart + buflen - 1;
509 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
510 1.71 tsutsui DPRINTF(IDB_INFO, ("iommu_dvmamap_load: boundary %lx boundary - 1 %lx "
511 1.71 tsutsui "~(boundary - 1) %lx\n", (long)boundary, (long)(boundary - 1),
512 1.71 tsutsui (long)~(boundary - 1)));
513 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
514 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
515 1.71 tsutsui len = boundary - (sgstart & (boundary - 1));
516 1.71 tsutsui map->dm_segs[seg].ds_len = len;
517 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
518 1.71 tsutsui "seg %d start %lx size %lx\n", seg,
519 1.71 tsutsui (long)map->dm_segs[seg].ds_addr,
520 1.71 tsutsui (long)map->dm_segs[seg].ds_len));
521 1.53 eeh if (++seg >= map->_dm_segcnt) {
522 1.40 eeh /* Too many segments. Fail the operation. */
523 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
524 1.71 tsutsui "too many segments %d\n", seg));
525 1.40 eeh s = splhigh();
526 1.40 eeh /* How can this fail? And if it does what can we do? */
527 1.40 eeh err = extent_free(is->is_dvmamap,
528 1.71 tsutsui dvmaddr, sgsize, EX_NOWAIT);
529 1.40 eeh map->_dm_dvmastart = 0;
530 1.40 eeh map->_dm_dvmasize = 0;
531 1.43 eeh splx(s);
532 1.80 mrg return (EFBIG);
533 1.40 eeh }
534 1.71 tsutsui sgstart += len;
535 1.40 eeh map->dm_segs[seg].ds_addr = sgstart;
536 1.40 eeh }
537 1.40 eeh map->dm_segs[seg].ds_len = sgend - sgstart + 1;
538 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load: "
539 1.71 tsutsui "seg %d start %lx size %lx\n", seg,
540 1.71 tsutsui (long)map->dm_segs[seg].ds_addr, (long)map->dm_segs[seg].ds_len));
541 1.71 tsutsui map->dm_nsegs = seg + 1;
542 1.7 mrg map->dm_mapsize = buflen;
543 1.7 mrg
544 1.7 mrg if (p != NULL)
545 1.7 mrg pmap = p->p_vmspace->vm_map.pmap;
546 1.7 mrg else
547 1.7 mrg pmap = pmap_kernel();
548 1.7 mrg
549 1.7 mrg for (; buflen > 0; ) {
550 1.58 chs
551 1.7 mrg /*
552 1.7 mrg * Get the physical address for this page.
553 1.7 mrg */
554 1.7 mrg if (pmap_extract(pmap, (vaddr_t)vaddr, &curaddr) == FALSE) {
555 1.74 petrov #ifdef DIAGNOSTIC
556 1.74 petrov printf("iommu_dvmamap_load: pmap_extract failed %lx\n", vaddr);
557 1.74 petrov #endif
558 1.7 mrg bus_dmamap_unload(t, map);
559 1.7 mrg return (-1);
560 1.7 mrg }
561 1.7 mrg
562 1.7 mrg /*
563 1.7 mrg * Compute the segment size, and adjust counts.
564 1.7 mrg */
565 1.64 thorpej sgsize = PAGE_SIZE - ((u_long)vaddr & PGOFSET);
566 1.7 mrg if (buflen < sgsize)
567 1.7 mrg sgsize = buflen;
568 1.7 mrg
569 1.22 mrg DPRINTF(IDB_BUSDMA,
570 1.36 eeh ("iommu_dvmamap_load: map %p loading va %p "
571 1.71 tsutsui "dva %lx at pa %lx\n",
572 1.71 tsutsui map, (void *)vaddr, (long)dvmaddr,
573 1.87 nakayama (long)trunc_page(curaddr)));
574 1.55 eeh iommu_enter(sb, trunc_page(dvmaddr), trunc_page(curaddr),
575 1.45 eeh flags|0x4000);
576 1.58 chs
577 1.7 mrg dvmaddr += PAGE_SIZE;
578 1.7 mrg vaddr += sgsize;
579 1.7 mrg buflen -= sgsize;
580 1.7 mrg }
581 1.45 eeh #ifdef DIAGNOSTIC
582 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
583 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
584 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
585 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
586 1.71 tsutsui seg, (long)map->dm_segs[seg].ds_addr,
587 1.71 tsutsui is->is_dvmabase, is->is_dvmaend);
588 1.57 chs #ifdef DDB
589 1.45 eeh Debugger();
590 1.57 chs #endif
591 1.45 eeh }
592 1.45 eeh }
593 1.45 eeh #endif
594 1.7 mrg return (0);
595 1.7 mrg }
596 1.7 mrg
597 1.7 mrg
598 1.7 mrg void
599 1.85 nakayama iommu_dvmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
600 1.7 mrg {
601 1.85 nakayama struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
602 1.55 eeh struct iommu_state *is = sb->sb_is;
603 1.40 eeh int error, s;
604 1.70 christos bus_size_t sgsize = map->_dm_dvmasize;
605 1.7 mrg
606 1.40 eeh /* Flush the iommu */
607 1.40 eeh #ifdef DEBUG
608 1.40 eeh if (!map->_dm_dvmastart) {
609 1.40 eeh printf("iommu_dvmamap_unload: No dvmastart is zero\n");
610 1.40 eeh #ifdef DDB
611 1.40 eeh Debugger();
612 1.40 eeh #endif
613 1.40 eeh }
614 1.40 eeh #endif
615 1.40 eeh iommu_remove(is, map->_dm_dvmastart, map->_dm_dvmasize);
616 1.7 mrg
617 1.23 eeh /* Flush the caches */
618 1.23 eeh bus_dmamap_unload(t->_parent, map);
619 1.23 eeh
620 1.7 mrg /* Mark the mappings as invalid. */
621 1.7 mrg map->dm_mapsize = 0;
622 1.7 mrg map->dm_nsegs = 0;
623 1.58 chs
624 1.7 mrg s = splhigh();
625 1.58 chs error = extent_free(is->is_dvmamap, map->_dm_dvmastart,
626 1.40 eeh map->_dm_dvmasize, EX_NOWAIT);
627 1.43 eeh map->_dm_dvmastart = 0;
628 1.43 eeh map->_dm_dvmasize = 0;
629 1.7 mrg splx(s);
630 1.7 mrg if (error != 0)
631 1.7 mrg printf("warning: %qd of DVMA space lost\n", (long long)sgsize);
632 1.40 eeh
633 1.40 eeh /* Clear the map */
634 1.9 eeh }
635 1.9 eeh
636 1.9 eeh
637 1.9 eeh int
638 1.85 nakayama iommu_dvmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
639 1.85 nakayama bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
640 1.9 eeh {
641 1.85 nakayama struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
642 1.55 eeh struct iommu_state *is = sb->sb_is;
643 1.58 chs struct vm_page *pg;
644 1.40 eeh int i, j, s;
645 1.26 martin int left;
646 1.9 eeh int err;
647 1.9 eeh bus_size_t sgsize;
648 1.9 eeh paddr_t pa;
649 1.21 eeh bus_size_t boundary, align;
650 1.40 eeh u_long dvmaddr, sgstart, sgend;
651 1.58 chs struct pglist *pglist;
652 1.9 eeh int pagesz = PAGE_SIZE;
653 1.45 eeh int npg = 0; /* DEBUG */
654 1.9 eeh
655 1.9 eeh if (map->dm_nsegs) {
656 1.9 eeh /* Already in use?? */
657 1.9 eeh #ifdef DIAGNOSTIC
658 1.9 eeh printf("iommu_dvmamap_load_raw: map still in use\n");
659 1.9 eeh #endif
660 1.9 eeh bus_dmamap_unload(t, map);
661 1.9 eeh }
662 1.40 eeh
663 1.40 eeh /*
664 1.40 eeh * A boundary presented to bus_dmamem_alloc() takes precedence
665 1.40 eeh * over boundary in the map.
666 1.40 eeh */
667 1.40 eeh if ((boundary = segs[0]._ds_boundary) == 0)
668 1.40 eeh boundary = map->_dm_boundary;
669 1.40 eeh
670 1.45 eeh align = max(segs[0]._ds_align, pagesz);
671 1.40 eeh
672 1.9 eeh /*
673 1.9 eeh * Make sure that on error condition we return "no valid mappings".
674 1.9 eeh */
675 1.9 eeh map->dm_nsegs = 0;
676 1.26 martin /* Count up the total number of pages we need */
677 1.26 martin pa = segs[0].ds_addr;
678 1.26 martin sgsize = 0;
679 1.40 eeh left = size;
680 1.58 chs for (i = 0; left && i < nsegs; i++) {
681 1.26 martin if (round_page(pa) != round_page(segs[i].ds_addr))
682 1.26 martin sgsize = round_page(sgsize);
683 1.40 eeh sgsize += min(left, segs[i].ds_len);
684 1.40 eeh left -= segs[i].ds_len;
685 1.26 martin pa = segs[i].ds_addr + segs[i].ds_len;
686 1.26 martin }
687 1.75 petrov sgsize = round_page(sgsize) + PAGE_SIZE; /* XXX reserve extra dvma page */
688 1.9 eeh
689 1.40 eeh s = splhigh();
690 1.58 chs /*
691 1.58 chs * If our segment size is larger than the boundary we need to
692 1.45 eeh * split the transfer up into little pieces ourselves.
693 1.9 eeh */
694 1.40 eeh err = extent_alloc(is->is_dvmamap, sgsize, align,
695 1.40 eeh (sgsize > boundary) ? 0 : boundary,
696 1.40 eeh ((flags & BUS_DMA_NOWAIT) == 0 ? EX_WAITOK : EX_NOWAIT) |
697 1.54 eeh EX_BOUNDZERO, &dvmaddr);
698 1.9 eeh splx(s);
699 1.9 eeh
700 1.9 eeh if (err != 0)
701 1.9 eeh return (err);
702 1.9 eeh
703 1.9 eeh #ifdef DEBUG
704 1.65 nakayama if (dvmaddr == (u_long)-1)
705 1.58 chs {
706 1.9 eeh printf("iommu_dvmamap_load_raw(): extent_alloc(%d, %x) failed!\n",
707 1.25 mrg (int)sgsize, flags);
708 1.57 chs #ifdef DDB
709 1.9 eeh Debugger();
710 1.57 chs #endif
711 1.58 chs }
712 1.58 chs #endif
713 1.65 nakayama if (dvmaddr == (u_long)-1)
714 1.9 eeh return (ENOMEM);
715 1.9 eeh
716 1.40 eeh /* Set the active DVMA map */
717 1.40 eeh map->_dm_dvmastart = dvmaddr;
718 1.40 eeh map->_dm_dvmasize = sgsize;
719 1.40 eeh
720 1.58 chs if ((pglist = segs[0]._ds_mlist) == NULL) {
721 1.69 petrov u_long prev_va = 0UL;
722 1.45 eeh paddr_t prev_pa = 0;
723 1.45 eeh int end = 0, offset;
724 1.45 eeh
725 1.26 martin /*
726 1.45 eeh * This segs is made up of individual physical
727 1.58 chs * segments, probably by _bus_dmamap_load_uio() or
728 1.26 martin * _bus_dmamap_load_mbuf(). Ignore the mlist and
729 1.45 eeh * load each one individually.
730 1.26 martin */
731 1.40 eeh map->dm_mapsize = size;
732 1.40 eeh
733 1.45 eeh j = 0;
734 1.45 eeh for (i = 0; i < nsegs ; i++) {
735 1.40 eeh
736 1.45 eeh pa = segs[i].ds_addr;
737 1.45 eeh offset = (pa & PGOFSET);
738 1.45 eeh pa = trunc_page(pa);
739 1.45 eeh dvmaddr = trunc_page(dvmaddr);
740 1.45 eeh left = min(size, segs[i].ds_len);
741 1.45 eeh
742 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: converting "
743 1.58 chs "physseg %d start %lx size %lx\n", i,
744 1.61 martin (long)segs[i].ds_addr, (long)segs[i].ds_len));
745 1.26 martin
746 1.58 chs if ((pa == prev_pa) &&
747 1.47 eeh ((offset != 0) || (end != offset))) {
748 1.45 eeh /* We can re-use this mapping */
749 1.45 eeh dvmaddr = prev_va;
750 1.45 eeh }
751 1.29 martin
752 1.45 eeh sgstart = dvmaddr + offset;
753 1.45 eeh sgend = sgstart + left - 1;
754 1.26 martin
755 1.45 eeh /* Are the segments virtually adjacent? */
756 1.58 chs if ((j > 0) && (end == offset) &&
757 1.45 eeh ((offset == 0) || (pa == prev_pa))) {
758 1.45 eeh /* Just append to the previous segment. */
759 1.45 eeh map->dm_segs[--j].ds_len += left;
760 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
761 1.45 eeh "appending seg %d start %lx size %lx\n", j,
762 1.58 chs (long)map->dm_segs[j].ds_addr,
763 1.61 martin (long)map->dm_segs[j].ds_len));
764 1.45 eeh } else {
765 1.53 eeh if (j >= map->_dm_segcnt) {
766 1.85 nakayama iommu_dvmamap_unload(t, map);
767 1.80 mrg return (EFBIG);
768 1.53 eeh }
769 1.45 eeh map->dm_segs[j].ds_addr = sgstart;
770 1.45 eeh map->dm_segs[j].ds_len = left;
771 1.45 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
772 1.45 eeh "seg %d start %lx size %lx\n", j,
773 1.48 eeh (long)map->dm_segs[j].ds_addr,
774 1.61 martin (long)map->dm_segs[j].ds_len));
775 1.40 eeh }
776 1.45 eeh end = (offset + left) & PGOFSET;
777 1.40 eeh
778 1.40 eeh /* Check for boundary issues */
779 1.40 eeh while ((sgstart & ~(boundary - 1)) !=
780 1.40 eeh (sgend & ~(boundary - 1))) {
781 1.40 eeh /* Need a new segment. */
782 1.40 eeh map->dm_segs[j].ds_len =
783 1.53 eeh boundary - (sgstart & (boundary - 1));
784 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
785 1.40 eeh "seg %d start %lx size %lx\n", j,
786 1.58 chs (long)map->dm_segs[j].ds_addr,
787 1.61 martin (long)map->dm_segs[j].ds_len));
788 1.53 eeh if (++j >= map->_dm_segcnt) {
789 1.85 nakayama iommu_dvmamap_unload(t, map);
790 1.80 mrg return (EFBIG);
791 1.40 eeh }
792 1.40 eeh sgstart = roundup(sgstart, boundary);
793 1.40 eeh map->dm_segs[j].ds_addr = sgstart;
794 1.40 eeh map->dm_segs[j].ds_len = sgend - sgstart + 1;
795 1.40 eeh }
796 1.40 eeh
797 1.26 martin if (sgsize == 0)
798 1.26 martin panic("iommu_dmamap_load_raw: size botch");
799 1.40 eeh
800 1.45 eeh /* Now map a series of pages. */
801 1.51 eeh while (dvmaddr <= sgend) {
802 1.45 eeh DPRINTF(IDB_BUSDMA,
803 1.45 eeh ("iommu_dvmamap_load_raw: map %p "
804 1.45 eeh "loading va %lx at pa %lx\n",
805 1.45 eeh map, (long)dvmaddr,
806 1.45 eeh (long)(pa)));
807 1.45 eeh /* Enter it if we haven't before. */
808 1.46 eeh if (prev_va != dvmaddr)
809 1.55 eeh iommu_enter(sb, prev_va = dvmaddr,
810 1.45 eeh prev_pa = pa,
811 1.58 chs flags | (++npg << 12));
812 1.45 eeh dvmaddr += pagesz;
813 1.45 eeh pa += pagesz;
814 1.45 eeh }
815 1.45 eeh
816 1.45 eeh size -= left;
817 1.45 eeh ++j;
818 1.26 martin }
819 1.45 eeh
820 1.45 eeh map->dm_nsegs = j;
821 1.45 eeh #ifdef DIAGNOSTIC
822 1.45 eeh { int seg;
823 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
824 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
825 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
826 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
827 1.58 chs seg, (long)map->dm_segs[seg].ds_addr,
828 1.45 eeh is->is_dvmabase, is->is_dvmaend);
829 1.57 chs #ifdef DDB
830 1.45 eeh Debugger();
831 1.57 chs #endif
832 1.45 eeh }
833 1.45 eeh }
834 1.45 eeh }
835 1.45 eeh #endif
836 1.26 martin return (0);
837 1.26 martin }
838 1.58 chs
839 1.9 eeh /*
840 1.40 eeh * This was allocated with bus_dmamem_alloc.
841 1.58 chs * The pages are on a `pglist'.
842 1.9 eeh */
843 1.9 eeh map->dm_mapsize = size;
844 1.26 martin i = 0;
845 1.40 eeh sgstart = dvmaddr;
846 1.40 eeh sgend = sgstart + size - 1;
847 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
848 1.40 eeh while ((sgstart & ~(boundary - 1)) != (sgend & ~(boundary - 1))) {
849 1.40 eeh /* Oops. We crossed a boundary. Split the xfer. */
850 1.53 eeh map->dm_segs[i].ds_len = boundary - (sgstart & (boundary - 1));
851 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
852 1.40 eeh "seg %d start %lx size %lx\n", i,
853 1.48 eeh (long)map->dm_segs[i].ds_addr,
854 1.61 martin (long)map->dm_segs[i].ds_len));
855 1.53 eeh if (++i >= map->_dm_segcnt) {
856 1.40 eeh /* Too many segments. Fail the operation. */
857 1.40 eeh s = splhigh();
858 1.40 eeh /* How can this fail? And if it does what can we do? */
859 1.40 eeh err = extent_free(is->is_dvmamap,
860 1.40 eeh dvmaddr, sgsize, EX_NOWAIT);
861 1.40 eeh map->_dm_dvmastart = 0;
862 1.40 eeh map->_dm_dvmasize = 0;
863 1.43 eeh splx(s);
864 1.80 mrg return (EFBIG);
865 1.40 eeh }
866 1.40 eeh sgstart = roundup(sgstart, boundary);
867 1.40 eeh map->dm_segs[i].ds_addr = sgstart;
868 1.40 eeh }
869 1.40 eeh DPRINTF(IDB_INFO, ("iommu_dvmamap_load_raw: "
870 1.40 eeh "seg %d start %lx size %lx\n", i,
871 1.61 martin (long)map->dm_segs[i].ds_addr, (long)map->dm_segs[i].ds_len));
872 1.40 eeh map->dm_segs[i].ds_len = sgend - sgstart + 1;
873 1.9 eeh
874 1.83 ad TAILQ_FOREACH(pg, pglist, pageq.queue) {
875 1.9 eeh if (sgsize == 0)
876 1.9 eeh panic("iommu_dmamap_load_raw: size botch");
877 1.58 chs pa = VM_PAGE_TO_PHYS(pg);
878 1.9 eeh
879 1.22 mrg DPRINTF(IDB_BUSDMA,
880 1.9 eeh ("iommu_dvmamap_load_raw: map %p loading va %lx at pa %lx\n",
881 1.9 eeh map, (long)dvmaddr, (long)(pa)));
882 1.55 eeh iommu_enter(sb, dvmaddr, pa, flags|0x8000);
883 1.58 chs
884 1.9 eeh dvmaddr += pagesz;
885 1.9 eeh sgsize -= pagesz;
886 1.9 eeh }
887 1.40 eeh map->dm_mapsize = size;
888 1.40 eeh map->dm_nsegs = i+1;
889 1.45 eeh #ifdef DIAGNOSTIC
890 1.45 eeh { int seg;
891 1.45 eeh for (seg = 0; seg < map->dm_nsegs; seg++) {
892 1.45 eeh if (map->dm_segs[seg].ds_addr < is->is_dvmabase ||
893 1.45 eeh map->dm_segs[seg].ds_addr > is->is_dvmaend) {
894 1.45 eeh printf("seg %d dvmaddr %lx out of range %x - %x\n",
895 1.58 chs seg, (long)map->dm_segs[seg].ds_addr,
896 1.45 eeh is->is_dvmabase, is->is_dvmaend);
897 1.57 chs #ifdef DDB
898 1.45 eeh Debugger();
899 1.57 chs #endif
900 1.45 eeh }
901 1.45 eeh }
902 1.45 eeh }
903 1.45 eeh #endif
904 1.9 eeh return (0);
905 1.7 mrg }
906 1.7 mrg
907 1.67 petrov
908 1.67 petrov /*
909 1.67 petrov * Flush an individual dma segment, returns non-zero if the streaming buffers
910 1.67 petrov * need flushing afterwards.
911 1.67 petrov */
912 1.67 petrov static int
913 1.67 petrov iommu_dvmamap_sync_range(struct strbuf_ctl *sb, vaddr_t va, bus_size_t len)
914 1.67 petrov {
915 1.67 petrov vaddr_t vaend;
916 1.67 petrov struct iommu_state *is = sb->sb_is;
917 1.67 petrov
918 1.67 petrov #ifdef DIAGNOSTIC
919 1.67 petrov if (va < is->is_dvmabase || va > is->is_dvmaend)
920 1.67 petrov panic("invalid va: %llx", (long long)va);
921 1.67 petrov #endif
922 1.67 petrov
923 1.67 petrov if ((is->is_tsb[IOTSBSLOT(va, is->is_tsbsize)] & IOTTE_STREAM) == 0) {
924 1.67 petrov DPRINTF(IDB_BUSDMA,
925 1.67 petrov ("iommu_dvmamap_sync_range: attempting to flush "
926 1.67 petrov "non-streaming entry\n"));
927 1.67 petrov return (0);
928 1.67 petrov }
929 1.67 petrov
930 1.87 nakayama vaend = round_page(va + len);
931 1.87 nakayama va = trunc_page(va);
932 1.67 petrov
933 1.67 petrov #ifdef DIAGNOSTIC
934 1.67 petrov if (va < is->is_dvmabase || vaend > is->is_dvmaend)
935 1.67 petrov panic("invalid va range: %llx to %llx (%x to %x)",
936 1.67 petrov (long long)va, (long long)vaend,
937 1.67 petrov is->is_dvmabase,
938 1.67 petrov is->is_dvmaend);
939 1.67 petrov #endif
940 1.67 petrov
941 1.67 petrov for ( ; va <= vaend; va += PAGE_SIZE) {
942 1.67 petrov DPRINTF(IDB_BUSDMA,
943 1.67 petrov ("iommu_dvmamap_sync_range: flushing va %p\n",
944 1.67 petrov (void *)(u_long)va));
945 1.67 petrov iommu_strbuf_flush(sb, va);
946 1.67 petrov }
947 1.67 petrov
948 1.67 petrov return (1);
949 1.67 petrov }
950 1.67 petrov
951 1.85 nakayama static void
952 1.85 nakayama _iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
953 1.85 nakayama bus_size_t len, int ops)
954 1.7 mrg {
955 1.85 nakayama struct strbuf_ctl *sb = (struct strbuf_ctl *)map->_dm_cookie;
956 1.67 petrov bus_size_t count;
957 1.67 petrov int i, needsflush = 0;
958 1.63 petrov
959 1.63 petrov if (!sb->sb_flush)
960 1.63 petrov return;
961 1.7 mrg
962 1.67 petrov for (i = 0; i < map->dm_nsegs; i++) {
963 1.67 petrov if (offset < map->dm_segs[i].ds_len)
964 1.67 petrov break;
965 1.67 petrov offset -= map->dm_segs[i].ds_len;
966 1.67 petrov }
967 1.60 petrov
968 1.67 petrov if (i == map->dm_nsegs)
969 1.68 martin panic("iommu_dvmamap_sync: segment too short %llu",
970 1.68 martin (unsigned long long)offset);
971 1.60 petrov
972 1.62 petrov if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_POSTWRITE)) {
973 1.60 petrov /* Nothing to do */;
974 1.60 petrov }
975 1.60 petrov
976 1.62 petrov if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_PREWRITE)) {
977 1.67 petrov
978 1.67 petrov for (; len > 0 && i < map->dm_nsegs; i++) {
979 1.67 petrov count = MIN(map->dm_segs[i].ds_len - offset, len);
980 1.67 petrov if (count > 0 &&
981 1.67 petrov iommu_dvmamap_sync_range(sb,
982 1.67 petrov map->dm_segs[i].ds_addr + offset, count))
983 1.67 petrov needsflush = 1;
984 1.67 petrov offset = 0;
985 1.67 petrov len -= count;
986 1.67 petrov }
987 1.60 petrov #ifdef DIAGNOSTIC
988 1.67 petrov if (i == map->dm_nsegs && len > 0)
989 1.73 nakayama panic("iommu_dvmamap_sync: leftover %llu",
990 1.73 nakayama (unsigned long long)len);
991 1.60 petrov #endif
992 1.55 eeh
993 1.67 petrov if (needsflush)
994 1.58 chs iommu_strbuf_flush_done(sb);
995 1.7 mrg }
996 1.7 mrg }
997 1.7 mrg
998 1.85 nakayama void
999 1.85 nakayama iommu_dvmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
1000 1.85 nakayama bus_size_t len, int ops)
1001 1.85 nakayama {
1002 1.85 nakayama
1003 1.85 nakayama if (ops & (BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE)) {
1004 1.85 nakayama /* Flush the CPU then the IOMMU */
1005 1.85 nakayama bus_dmamap_sync(t->_parent, map, offset, len, ops);
1006 1.85 nakayama _iommu_dvmamap_sync(t, map, offset, len, ops);
1007 1.85 nakayama }
1008 1.85 nakayama if (ops & (BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE)) {
1009 1.85 nakayama /* Flush the IOMMU then the CPU */
1010 1.85 nakayama _iommu_dvmamap_sync(t, map, offset, len, ops);
1011 1.85 nakayama bus_dmamap_sync(t->_parent, map, offset, len, ops);
1012 1.85 nakayama }
1013 1.85 nakayama }
1014 1.85 nakayama
1015 1.7 mrg int
1016 1.85 nakayama iommu_dvmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
1017 1.85 nakayama bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
1018 1.85 nakayama int flags)
1019 1.7 mrg {
1020 1.7 mrg
1021 1.25 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_alloc: sz %llx align %llx bound %llx "
1022 1.25 mrg "segp %p flags %d\n", (unsigned long long)size,
1023 1.25 mrg (unsigned long long)alignment, (unsigned long long)boundary,
1024 1.25 mrg segs, flags));
1025 1.7 mrg return (bus_dmamem_alloc(t->_parent, size, alignment, boundary,
1026 1.21 eeh segs, nsegs, rsegs, flags|BUS_DMA_DVMA));
1027 1.7 mrg }
1028 1.7 mrg
1029 1.7 mrg void
1030 1.85 nakayama iommu_dvmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
1031 1.7 mrg {
1032 1.7 mrg
1033 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_free: segp %p nsegs %d\n",
1034 1.7 mrg segs, nsegs));
1035 1.7 mrg bus_dmamem_free(t->_parent, segs, nsegs);
1036 1.7 mrg }
1037 1.7 mrg
1038 1.7 mrg /*
1039 1.7 mrg * Map the DVMA mappings into the kernel pmap.
1040 1.7 mrg * Check the flags to see whether we're streaming or coherent.
1041 1.7 mrg */
1042 1.7 mrg int
1043 1.85 nakayama iommu_dvmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
1044 1.85 nakayama size_t size, void **kvap, int flags)
1045 1.7 mrg {
1046 1.58 chs struct vm_page *pg;
1047 1.7 mrg vaddr_t va;
1048 1.7 mrg bus_addr_t addr;
1049 1.58 chs struct pglist *pglist;
1050 1.8 mrg int cbit;
1051 1.77 yamt const uvm_flag_t kmflags =
1052 1.77 yamt (flags & BUS_DMA_NOWAIT) != 0 ? UVM_KMF_NOWAIT : 0;
1053 1.7 mrg
1054 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: segp %p nsegs %d size %lx\n",
1055 1.7 mrg segs, nsegs, size));
1056 1.7 mrg
1057 1.7 mrg /*
1058 1.8 mrg * Allocate some space in the kernel map, and then map these pages
1059 1.8 mrg * into this space.
1060 1.7 mrg */
1061 1.8 mrg size = round_page(size);
1062 1.77 yamt va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY | kmflags);
1063 1.8 mrg if (va == 0)
1064 1.8 mrg return (ENOMEM);
1065 1.7 mrg
1066 1.81 christos *kvap = (void *)va;
1067 1.7 mrg
1068 1.58 chs /*
1069 1.7 mrg * digest flags:
1070 1.7 mrg */
1071 1.7 mrg cbit = 0;
1072 1.7 mrg if (flags & BUS_DMA_COHERENT) /* Disable vcache */
1073 1.7 mrg cbit |= PMAP_NVC;
1074 1.7 mrg if (flags & BUS_DMA_NOCACHE) /* sideffects */
1075 1.7 mrg cbit |= PMAP_NC;
1076 1.7 mrg
1077 1.7 mrg /*
1078 1.8 mrg * Now take this and map it into the CPU.
1079 1.7 mrg */
1080 1.58 chs pglist = segs[0]._ds_mlist;
1081 1.83 ad TAILQ_FOREACH(pg, pglist, pageq.queue) {
1082 1.8 mrg #ifdef DIAGNOSTIC
1083 1.7 mrg if (size == 0)
1084 1.7 mrg panic("iommu_dvmamem_map: size botch");
1085 1.8 mrg #endif
1086 1.58 chs addr = VM_PAGE_TO_PHYS(pg);
1087 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_map: "
1088 1.25 mrg "mapping va %lx at %llx\n", va, (unsigned long long)addr | cbit));
1089 1.88 cegger pmap_kenter_pa(va, addr | cbit,
1090 1.88 cegger VM_PROT_READ | VM_PROT_WRITE, 0);
1091 1.7 mrg va += PAGE_SIZE;
1092 1.7 mrg size -= PAGE_SIZE;
1093 1.7 mrg }
1094 1.38 chris pmap_update(pmap_kernel());
1095 1.7 mrg return (0);
1096 1.7 mrg }
1097 1.7 mrg
1098 1.7 mrg /*
1099 1.7 mrg * Unmap DVMA mappings from kernel
1100 1.7 mrg */
1101 1.7 mrg void
1102 1.85 nakayama iommu_dvmamem_unmap(bus_dma_tag_t t, void *kva, size_t size)
1103 1.7 mrg {
1104 1.58 chs
1105 1.22 mrg DPRINTF(IDB_BUSDMA, ("iommu_dvmamem_unmap: kvm %p size %lx\n",
1106 1.7 mrg kva, size));
1107 1.58 chs
1108 1.7 mrg #ifdef DIAGNOSTIC
1109 1.7 mrg if ((u_long)kva & PGOFSET)
1110 1.7 mrg panic("iommu_dvmamem_unmap");
1111 1.7 mrg #endif
1112 1.58 chs
1113 1.7 mrg size = round_page(size);
1114 1.58 chs pmap_kremove((vaddr_t)kva, size);
1115 1.38 chris pmap_update(pmap_kernel());
1116 1.76 yamt uvm_km_free(kernel_map, (vaddr_t)kva, size, UVM_KMF_VAONLY);
1117 1.1 mrg }
1118