iommu.c revision 1.2 1 1.2 eeh /* $NetBSD: iommu.c,v 1.2 1999/06/20 00:51:29 eeh Exp $ */
2 1.1 mrg
3 1.1 mrg /*-
4 1.1 mrg * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * This code is derived from software contributed to The NetBSD Foundation
8 1.1 mrg * by Paul Kranenburg.
9 1.1 mrg *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer in the
17 1.1 mrg * documentation and/or other materials provided with the distribution.
18 1.1 mrg * 3. All advertising materials mentioning features or use of this software
19 1.1 mrg * must display the following acknowledgement:
20 1.1 mrg * This product includes software developed by the NetBSD
21 1.1 mrg * Foundation, Inc. and its contributors.
22 1.1 mrg * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 mrg * contributors may be used to endorse or promote products derived
24 1.1 mrg * from this software without specific prior written permission.
25 1.1 mrg *
26 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 mrg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 mrg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 mrg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 mrg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 mrg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 mrg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 mrg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 mrg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 mrg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 mrg * POSSIBILITY OF SUCH DAMAGE.
37 1.1 mrg */
38 1.1 mrg
39 1.1 mrg /*
40 1.1 mrg * Copyright (c) 1992, 1993
41 1.1 mrg * The Regents of the University of California. All rights reserved.
42 1.1 mrg *
43 1.1 mrg * This software was developed by the Computer Systems Engineering group
44 1.1 mrg * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
45 1.1 mrg * contributed to Berkeley.
46 1.1 mrg *
47 1.1 mrg * All advertising materials mentioning features or use of this software
48 1.1 mrg * must display the following acknowledgement:
49 1.1 mrg * This product includes software developed by the University of
50 1.1 mrg * California, Lawrence Berkeley Laboratory.
51 1.1 mrg *
52 1.1 mrg * Redistribution and use in source and binary forms, with or without
53 1.1 mrg * modification, are permitted provided that the following conditions
54 1.1 mrg * are met:
55 1.1 mrg * 1. Redistributions of source code must retain the above copyright
56 1.1 mrg * notice, this list of conditions and the following disclaimer.
57 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
58 1.1 mrg * notice, this list of conditions and the following disclaimer in the
59 1.1 mrg * documentation and/or other materials provided with the distribution.
60 1.1 mrg * 3. All advertising materials mentioning features or use of this software
61 1.1 mrg * must display the following acknowledgement:
62 1.1 mrg * This product includes software developed by the University of
63 1.1 mrg * California, Berkeley and its contributors.
64 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
65 1.1 mrg * may be used to endorse or promote products derived from this software
66 1.1 mrg * without specific prior written permission.
67 1.1 mrg *
68 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
69 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
70 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
71 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
72 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
73 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
74 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
76 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
77 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
78 1.1 mrg * SUCH DAMAGE.
79 1.1 mrg *
80 1.1 mrg * from: NetBSD: sbus.c,v 1.13 1999/05/23 07:24:02 mrg Exp
81 1.1 mrg * from: @(#)sbus.c 8.1 (Berkeley) 6/11/93
82 1.1 mrg */
83 1.1 mrg
84 1.1 mrg #include <sys/param.h>
85 1.1 mrg #include <sys/extent.h>
86 1.1 mrg #include <sys/malloc.h>
87 1.1 mrg #include <sys/systm.h>
88 1.1 mrg #include <sys/device.h>
89 1.1 mrg #include <vm/vm.h>
90 1.1 mrg
91 1.1 mrg #include <machine/bus.h>
92 1.1 mrg #include <sparc64/sparc64/vaddrs.h>
93 1.1 mrg #include <sparc64/dev/iommureg.h>
94 1.1 mrg #include <sparc64/dev/iommuvar.h>
95 1.1 mrg
96 1.1 mrg #include <machine/autoconf.h>
97 1.1 mrg #include <machine/ctlreg.h>
98 1.1 mrg #include <machine/cpu.h>
99 1.1 mrg
100 1.1 mrg #ifdef DEBUG
101 1.1 mrg #define IDB_DVMA 0x1
102 1.1 mrg #define IDB_INTR 0x2
103 1.1 mrg int iommudebug = 0;
104 1.1 mrg #endif
105 1.1 mrg
106 1.1 mrg /*
107 1.1 mrg * initialise the UltraSPARC IOMMU (SBUS or PCI):
108 1.1 mrg * - allocate and setup the iotsb.
109 1.1 mrg * - enable the IOMMU
110 1.1 mrg * - initialise the streaming buffers
111 1.1 mrg * - create a private DVMA map.
112 1.1 mrg *
113 1.1 mrg */
114 1.1 mrg void
115 1.1 mrg iommu_init(name, is, tsbsize)
116 1.1 mrg char *name;
117 1.1 mrg struct iommu_state *is;
118 1.1 mrg int tsbsize;
119 1.1 mrg {
120 1.1 mrg
121 1.1 mrg /*
122 1.1 mrg * Setup the iommu.
123 1.1 mrg *
124 1.1 mrg * The sun4u iommu is part of the SBUS controller so we will
125 1.1 mrg * deal with it here. We could try to fake a device node so
126 1.1 mrg * we can eventually share it with the PCI bus run by psycho,
127 1.1 mrg * but I don't want to get into that sort of cruft.
128 1.1 mrg *
129 1.1 mrg * First we need to allocate a IOTSB. Problem is that the IOMMU
130 1.1 mrg * can only access the IOTSB by physical address, so all the
131 1.1 mrg * pages must be contiguous. Luckily, the smallest IOTSB size
132 1.1 mrg * is one 8K page.
133 1.1 mrg */
134 1.1 mrg if (tsbsize != 0)
135 1.1 mrg panic("tsbsize != 0; FIX ME"); /* XXX */
136 1.1 mrg
137 1.1 mrg /* we want 8K pages */
138 1.1 mrg is->is_cr = IOMMUCR_8KPG | IOMMUCR_EN;
139 1.2 eeh /*
140 1.2 eeh *
141 1.2 eeh * The IOMMU address space always ends at 0xffffe000, but the starting
142 1.2 eeh * address depends on the size of the map. The map size is 1024 * 2 ^
143 1.2 eeh * is->is_tsbsize entries, where each entry is 8 bytes. The start of
144 1.2 eeh * the map can be calculated by (0xffffe000 << (8 + is->is_tsbsize)).
145 1.2 eeh *
146 1.2 eeh * Note: the stupid IOMMU ignores the high bits of an address, so a
147 1.2 eeh * NULL DMA pointer will be translated by the first page of the IOTSB.
148 1.2 eeh * To trap bugs we'll skip the first entry in the IOTSB.
149 1.2 eeh */
150 1.2 eeh is->is_dvmabase = IOTSB_VSTART(is->is_tsbsize) + NBPG;
151 1.1 mrg is->is_tsbsize = tsbsize;
152 1.1 mrg is->is_tsb = malloc(NBPG, M_DMAMAP, M_WAITOK); /* XXX */
153 1.1 mrg is->is_ptsb = pmap_extract(pmap_kernel(), (vaddr_t)is->is_tsb);
154 1.1 mrg
155 1.1 mrg #ifdef DEBUG
156 1.1 mrg if (iommudebug & IDB_DVMA)
157 1.1 mrg {
158 1.1 mrg /* Probe the iommu */
159 1.1 mrg struct iommureg *regs = is->is_iommu;
160 1.1 mrg int64_t cr, tsb;
161 1.1 mrg
162 1.1 mrg printf("iommu regs at: cr=%lx tsb=%lx flush=%lx\n", ®s->iommu_cr,
163 1.1 mrg ®s->iommu_tsb, ®s->iommu_flush);
164 1.1 mrg cr = regs->iommu_cr;
165 1.1 mrg tsb = regs->iommu_tsb;
166 1.1 mrg printf("iommu cr=%lx tsb=%lx\n", (long)cr, (long)tsb);
167 1.1 mrg printf("TSB base %p phys %p\n", (long)is->is_tsb, (long)is->is_ptsb);
168 1.1 mrg delay(1000000); /* 1 s */
169 1.1 mrg }
170 1.1 mrg #endif
171 1.1 mrg
172 1.1 mrg /*
173 1.1 mrg * Initialize streaming buffer.
174 1.1 mrg */
175 1.1 mrg is->is_flushpa = pmap_extract(pmap_kernel(), (vaddr_t)&is->is_flush);
176 1.1 mrg
177 1.1 mrg /*
178 1.1 mrg * now actually start up the IOMMU
179 1.1 mrg */
180 1.1 mrg iommu_reset(is);
181 1.1 mrg
182 1.1 mrg /*
183 1.1 mrg * Now all the hardware's working we need to allocate a dvma map.
184 1.1 mrg */
185 1.1 mrg is->is_dvmamap = extent_create(name,
186 1.2 eeh is->is_dvmabase, IOTSB_VEND,
187 1.1 mrg M_DEVBUF, 0, 0, EX_NOWAIT);
188 1.1 mrg }
189 1.1 mrg
190 1.1 mrg void
191 1.1 mrg iommu_reset(is)
192 1.1 mrg struct iommu_state *is;
193 1.1 mrg {
194 1.1 mrg
195 1.1 mrg /* Need to do 64-bit stores */
196 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_cr, 0, is->is_cr);
197 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_tsb, 0, is->is_ptsb);
198 1.1 mrg /* Enable diagnostics mode? */
199 1.1 mrg bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_ctl, 0, STRBUF_EN);
200 1.2 eeh }
201 1.2 eeh
202 1.2 eeh /*
203 1.2 eeh * Here are the iommu control routines.
204 1.2 eeh */
205 1.2 eeh void
206 1.2 eeh iommu_enter(is, va, pa, flags)
207 1.2 eeh struct iommu_state *is;
208 1.2 eeh vaddr_t va;
209 1.2 eeh int64_t pa;
210 1.2 eeh int flags;
211 1.2 eeh {
212 1.2 eeh int64_t tte;
213 1.2 eeh
214 1.2 eeh #ifdef DIAGNOSTIC
215 1.2 eeh if (va < is->is_dvmabase)
216 1.2 eeh panic("sbus_enter: va 0x%lx not in DVMA space",va);
217 1.2 eeh #endif
218 1.2 eeh
219 1.2 eeh tte = MAKEIOTTE(pa, !(flags&BUS_DMA_NOWRITE), !(flags&BUS_DMA_NOCACHE),
220 1.2 eeh !(flags&BUS_DMA_COHERENT));
221 1.2 eeh
222 1.2 eeh /* Is the streamcache flush really needed? */
223 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_pgflush,
224 1.2 eeh 0, va);
225 1.2 eeh iommu_flush(is);
226 1.2 eeh #ifdef DEBUG
227 1.2 eeh if (iommudebug & IDB_DVMA)
228 1.2 eeh printf("Clearing TSB slot %d for va %p\n",
229 1.2 eeh (int)IOTSBSLOT(va,is->is_tsbsize), va);
230 1.2 eeh #endif
231 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = tte;
232 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush,
233 1.2 eeh 0, va);
234 1.2 eeh #ifdef DEBUG
235 1.2 eeh if (iommudebug & IDB_DVMA)
236 1.2 eeh printf("sbus_enter: va %lx pa %lx TSB[%lx]@%p=%lx\n",
237 1.2 eeh va, (long)pa, IOTSBSLOT(va,is->is_tsbsize),
238 1.2 eeh &is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
239 1.2 eeh (long)tte);
240 1.2 eeh #endif
241 1.2 eeh }
242 1.2 eeh
243 1.2 eeh /*
244 1.2 eeh * iommu_remove: removes mappings created by iommu_enter
245 1.2 eeh *
246 1.2 eeh * Only demap from IOMMU if flag is set.
247 1.2 eeh */
248 1.2 eeh void
249 1.2 eeh iommu_remove(is, va, len)
250 1.2 eeh struct iommu_state *is;
251 1.2 eeh vaddr_t va;
252 1.2 eeh size_t len;
253 1.2 eeh {
254 1.2 eeh
255 1.2 eeh #ifdef DIAGNOSTIC
256 1.2 eeh if (va < is->is_dvmabase)
257 1.2 eeh panic("sbus_remove: va 0x%lx not in DVMA space", (long)va);
258 1.2 eeh if ((long)(va + len) < (long)va)
259 1.2 eeh panic("sbus_remove: va 0x%lx + len 0x%lx wraps",
260 1.2 eeh (long) va, (long) len);
261 1.2 eeh if (len & ~0xfffffff)
262 1.2 eeh panic("sbus_remove: rediculous len 0x%lx", (long)len);
263 1.2 eeh #endif
264 1.2 eeh
265 1.2 eeh va = trunc_page(va);
266 1.2 eeh while (len > 0) {
267 1.2 eeh
268 1.2 eeh /*
269 1.2 eeh * Streaming buffer flushes:
270 1.2 eeh *
271 1.2 eeh * 1 Tell strbuf to flush by storing va to strbuf_pgflush
272 1.2 eeh * If we're not on a cache line boundary (64-bits):
273 1.2 eeh * 2 Store 0 in flag
274 1.2 eeh * 3 Store pointer to flag in flushsync
275 1.2 eeh * 4 wait till flushsync becomes 0x1
276 1.2 eeh *
277 1.2 eeh * If it takes more than .5 sec, something went wrong.
278 1.2 eeh */
279 1.2 eeh #ifdef DEBUG
280 1.2 eeh if (iommudebug & IDB_DVMA)
281 1.2 eeh printf("sbus_remove: flushing va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
282 1.2 eeh (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
283 1.2 eeh (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
284 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
285 1.2 eeh (u_long)len);
286 1.2 eeh #endif
287 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_pgflush, 0, va);
288 1.2 eeh if (len <= NBPG) {
289 1.2 eeh iommu_flush(is);
290 1.2 eeh len = 0;
291 1.2 eeh } else len -= NBPG;
292 1.2 eeh #ifdef DEBUG
293 1.2 eeh if (iommudebug & IDB_DVMA)
294 1.2 eeh printf("sbus_remove: flushed va %p TSB[%lx]@%p=%lx, %lu bytes left\n",
295 1.2 eeh (long)va, (long)IOTSBSLOT(va,is->is_tsbsize),
296 1.2 eeh (long)&is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)],
297 1.2 eeh (long)(is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)]),
298 1.2 eeh (u_long)len);
299 1.2 eeh #endif
300 1.2 eeh is->is_tsb[IOTSBSLOT(va,is->is_tsbsize)] = 0;
301 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_iommu->iommu_flush, 0, va);
302 1.2 eeh va += NBPG;
303 1.2 eeh }
304 1.2 eeh }
305 1.2 eeh
306 1.2 eeh int
307 1.2 eeh iommu_flush(is)
308 1.2 eeh struct iommu_state *is;
309 1.2 eeh {
310 1.2 eeh struct timeval cur, flushtimeout;
311 1.2 eeh
312 1.2 eeh #define BUMPTIME(t, usec) { \
313 1.2 eeh register volatile struct timeval *tp = (t); \
314 1.2 eeh register long us; \
315 1.2 eeh \
316 1.2 eeh tp->tv_usec = us = tp->tv_usec + (usec); \
317 1.2 eeh if (us >= 1000000) { \
318 1.2 eeh tp->tv_usec = us - 1000000; \
319 1.2 eeh tp->tv_sec++; \
320 1.2 eeh } \
321 1.2 eeh }
322 1.2 eeh
323 1.2 eeh is->is_flush = 0;
324 1.2 eeh membar_sync();
325 1.2 eeh bus_space_write_8(is->is_bustag, &is->is_sb->strbuf_flushsync, 0, is->is_flushpa);
326 1.2 eeh membar_sync();
327 1.2 eeh
328 1.2 eeh microtime(&flushtimeout);
329 1.2 eeh cur = flushtimeout;
330 1.2 eeh BUMPTIME(&flushtimeout, 500000); /* 1/2 sec */
331 1.2 eeh
332 1.2 eeh #ifdef DEBUG
333 1.2 eeh if (iommudebug & IDB_DVMA)
334 1.2 eeh printf("sbus_flush: flush = %lx at va = %lx pa = %lx now=%lx:%lx until = %lx:%lx\n",
335 1.2 eeh (long)is->is_flush, (long)&is->is_flush,
336 1.2 eeh (long)is->is_flushpa, cur.tv_sec, cur.tv_usec,
337 1.2 eeh flushtimeout.tv_sec, flushtimeout.tv_usec);
338 1.2 eeh #endif
339 1.2 eeh /* Bypass non-coherent D$ */
340 1.2 eeh while (!ldxa(is->is_flushpa, ASI_PHYS_CACHED) &&
341 1.2 eeh ((cur.tv_sec <= flushtimeout.tv_sec) &&
342 1.2 eeh (cur.tv_usec <= flushtimeout.tv_usec)))
343 1.2 eeh microtime(&cur);
344 1.2 eeh
345 1.2 eeh #ifdef DIAGNOSTIC
346 1.2 eeh if (!is->is_flush) {
347 1.2 eeh printf("sbus_flush: flush timeout %p at %p\n", (long)is->is_flush,
348 1.2 eeh (long)is->is_flushpa); /* panic? */
349 1.2 eeh #ifdef DDB
350 1.2 eeh Debugger();
351 1.2 eeh #endif
352 1.2 eeh }
353 1.2 eeh #endif
354 1.2 eeh #ifdef DEBUG
355 1.2 eeh if (iommudebug & IDB_DVMA)
356 1.2 eeh printf("sbus_flush: flushed\n");
357 1.2 eeh #endif
358 1.2 eeh return (is->is_flush);
359 1.1 mrg }
360