iopaau.c revision 1.3 1 /* $NetBSD: iopaau.c,v 1.3 2002/08/02 02:08:11 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2002 Wasabi Systems, Inc.
5 * All rights reserved.
6 *
7 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
23 * written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Common code for XScale-based I/O Processor Application Accelerator
40 * Unit support.
41 *
42 * The AAU provides a back-end for the dmover(9) facility.
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: iopaau.c,v 1.3 2002/08/02 02:08:11 thorpej Exp $");
47
48 #include <sys/param.h>
49 #include <sys/pool.h>
50 #include <sys/lock.h>
51 #include <sys/systm.h>
52 #include <sys/device.h>
53 #include <sys/uio.h>
54
55 #include <uvm/uvm.h>
56
57 #include <machine/bus.h>
58
59 #include <arm/xscale/iopaaureg.h>
60 #include <arm/xscale/iopaauvar.h>
61
62 #ifdef AAU_DEBUG
63 #define DPRINTF(x) printf x
64 #else
65 #define DPRINTF(x) /* nothing */
66 #endif
67
68 static struct pool aau_desc_4_pool;
69 static struct pool_cache aau_desc_4_cache;
70
71 /*
72 * iopaau_desc_ctor:
73 *
74 * Constructor for all types of descriptors.
75 */
76 static int
77 iopaau_desc_ctor(void *arg, void *object, int flags)
78 {
79 struct aau_desc_4 *d = object;
80
81 /*
82 * Cache the physical address of the hardware portion of
83 * the descriptor in the software portion of the descriptor
84 * for quick reference later.
85 */
86 d->d_pa = vtophys(d) + SYNC_DESC_4_OFFSET;
87 KASSERT((d->d_pa & 31) == 0);
88 return (0);
89 }
90
91 /*
92 * iopaau_desc_4_free:
93 *
94 * Free a chain of aau_desc_4 structures.
95 */
96 void
97 iopaau_desc_4_free(struct iopaau_softc *sc, void *firstdesc)
98 {
99 struct aau_desc_4 *d, *next;
100
101 for (d = firstdesc; d != NULL; d = next) {
102 next = d->d_next;
103 pool_cache_put(&aau_desc_4_cache, d);
104 }
105 }
106
107 /*
108 * iopaau_start:
109 *
110 * Start an AAU request. Must be called at splbio().
111 */
112 static void
113 iopaau_start(struct iopaau_softc *sc)
114 {
115 struct dmover_backend *dmb = &sc->sc_dmb;
116 struct dmover_request *dreq;
117 struct iopaau_function *af;
118 int error;
119
120 for (;;) {
121
122 KASSERT(sc->sc_running == NULL);
123
124 dreq = TAILQ_FIRST(&dmb->dmb_pendreqs);
125 if (dreq == NULL)
126 return;
127
128 dmover_backend_remque(dmb, dreq);
129 dreq->dreq_flags |= DMOVER_REQ_RUNNING;
130
131 sc->sc_running = dreq;
132
133 /* XXXUNLOCK */
134
135 af = dreq->dreq_assignment->das_algdesc->dad_data;
136 error = (*af->af_setup)(sc, dreq);
137
138 /* XXXLOCK */
139
140 if (error) {
141 dreq->dreq_flags |= DMOVER_REQ_ERROR;
142 dreq->dreq_error = error;
143 sc->sc_running = NULL;
144 /* XXXUNLOCK */
145 dmover_done(dreq);
146 /* XXXLOCK */
147 continue;
148 }
149
150 #ifdef DIAGNOSTIC
151 if (bus_space_read_4(sc->sc_st, sc->sc_sh, AAU_ASR) &
152 AAU_ASR_AAF)
153 panic("iopaau_start: AAU already active");
154 #endif
155
156 DPRINTF(("%s: starting dreq %p\n", sc->sc_dev.dv_xname,
157 dreq));
158
159 bus_space_write_4(sc->sc_st, sc->sc_sh, AAU_ANDAR,
160 sc->sc_firstdesc_pa);
161 bus_space_write_4(sc->sc_st, sc->sc_sh, AAU_ACR,
162 AAU_ACR_AAE);
163
164 break;
165 }
166 }
167
168 /*
169 * iopaau_finish:
170 *
171 * Finish the current operation. AAU must be stopped.
172 */
173 static void
174 iopaau_finish(struct iopaau_softc *sc)
175 {
176 struct dmover_request *dreq = sc->sc_running;
177 struct iopaau_function *af =
178 dreq->dreq_assignment->das_algdesc->dad_data;
179 void *firstdesc = sc->sc_firstdesc;
180 int i, ninputs = dreq->dreq_assignment->das_algdesc->dad_ninputs;
181
182 sc->sc_running = NULL;
183
184 /* If the function has inputs, unmap them. */
185 for (i = 0; i < ninputs; i++) {
186 bus_dmamap_sync(sc->sc_dmat, sc->sc_map_in[i], 0,
187 sc->sc_map_in[i]->dm_mapsize, BUS_DMASYNC_POSTREAD);
188 bus_dmamap_unload(sc->sc_dmat, sc->sc_map_in[i]);
189 }
190
191 /* Unload the output buffer DMA map. */
192 bus_dmamap_sync(sc->sc_dmat, sc->sc_map_out, 0,
193 sc->sc_map_out->dm_mapsize, BUS_DMASYNC_POSTWRITE);
194 bus_dmamap_unload(sc->sc_dmat, sc->sc_map_out);
195
196 /* Get the next transfer started. */
197 iopaau_start(sc);
198
199 /* Now free descriptors for last transfer. */
200 (*af->af_free)(sc, firstdesc);
201
202 dmover_done(dreq);
203 }
204
205 /*
206 * iopaau_process:
207 *
208 * Dmover back-end entry point.
209 */
210 void
211 iopaau_process(struct dmover_backend *dmb)
212 {
213 struct iopaau_softc *sc = dmb->dmb_cookie;
214 int s;
215
216 s = splbio();
217 /* XXXLOCK */
218
219 if (sc->sc_running == NULL)
220 iopaau_start(sc);
221
222 /* XXXUNLOCK */
223 splx(s);
224 }
225
226 /*
227 * iopaau_func_fill_immed_setup:
228 *
229 * Common code shared by the zero and fillN setup routines.
230 */
231 static int
232 iopaau_func_fill_immed_setup(struct iopaau_softc *sc,
233 struct dmover_request *dreq, uint32_t immed)
234 {
235 bus_dmamap_t dmamap = sc->sc_map_out;
236 uint32_t *prevpa;
237 struct aau_desc_4 **prevp, *cur;
238 int error, seg;
239
240 switch (dreq->dreq_outbuf_type) {
241 case DMOVER_BUF_LINEAR:
242 error = bus_dmamap_load(sc->sc_dmat, dmamap,
243 dreq->dreq_outbuf.dmbuf_linear.l_addr,
244 dreq->dreq_outbuf.dmbuf_linear.l_len, NULL,
245 BUS_DMA_NOWAIT|BUS_DMA_WRITE|BUS_DMA_STREAMING);
246 break;
247
248 case DMOVER_BUF_UIO:
249 {
250 struct uio *uio = dreq->dreq_outbuf.dmbuf_uio;
251
252 if (uio->uio_rw != UIO_READ)
253 return (EINVAL);
254
255 error = bus_dmamap_load_uio(sc->sc_dmat, dmamap,
256 uio, BUS_DMA_NOWAIT|BUS_DMA_WRITE|BUS_DMA_STREAMING);
257 break;
258 }
259 }
260
261 if (__predict_false(error != 0))
262 return (error);
263
264 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
265 BUS_DMASYNC_PREWRITE);
266
267 prevp = (struct aau_desc_4 **) &sc->sc_firstdesc;
268 prevpa = &sc->sc_firstdesc_pa;
269
270 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
271 cur = pool_cache_get(&aau_desc_4_cache, PR_NOWAIT);
272 if (cur == NULL) {
273 *prevp = NULL;
274 error = ENOMEM;
275 goto bad;
276 }
277
278 *prevp = cur;
279 *prevpa = cur->d_pa;
280
281 prevp = &cur->d_next;
282 prevpa = &cur->d_nda;
283
284 /*
285 * We don't actually enforce the page alignment
286 * constraint, here, because there is only one
287 * data stream to worry about.
288 */
289
290 cur->d_sar1 = immed;
291 cur->d_dar = dmamap->dm_segs[seg].ds_addr;
292 cur->d_bc = dmamap->dm_segs[seg].ds_len;
293 cur->d_dc = AAU_DC_B1_CC(AAU_DC_CC_FILL) | AAU_DC_DWE;
294 SYNC_DESC_4(cur);
295 }
296
297 *prevp = NULL;
298 *prevpa = 0;
299
300 cur->d_dc |= AAU_DC_IE;
301 SYNC_DESC_4(cur);
302
303 sc->sc_lastdesc = cur;
304
305 return (0);
306
307 bad:
308 iopaau_desc_4_free(sc, sc->sc_firstdesc);
309 bus_dmamap_unload(sc->sc_dmat, sc->sc_map_out);
310 sc->sc_firstdesc = NULL;
311
312 return (error);
313 }
314
315 /*
316 * iopaau_func_zero_setup:
317 *
318 * Setup routine for the "zero" function.
319 */
320 int
321 iopaau_func_zero_setup(struct iopaau_softc *sc, struct dmover_request *dreq)
322 {
323
324 return (iopaau_func_fill_immed_setup(sc, dreq, 0));
325 }
326
327 /*
328 * iopaau_func_fill8_setup:
329 *
330 * Setup routine for the "fill8" function.
331 */
332 int
333 iopaau_func_fill8_setup(struct iopaau_softc *sc, struct dmover_request *dreq)
334 {
335
336 return (iopaau_func_fill_immed_setup(sc, dreq,
337 dreq->dreq_immediate[0] |
338 (dreq->dreq_immediate[0] << 8) |
339 (dreq->dreq_immediate[0] << 16) |
340 (dreq->dreq_immediate[0] << 24)));
341 }
342
343 /*
344 * iopaau_func_copy_setup:
345 *
346 * Setup routine for the "copy" function.
347 */
348 int
349 iopaau_func_copy_setup(struct iopaau_softc *sc, struct dmover_request *dreq)
350 {
351 bus_dmamap_t dmamap = sc->sc_map_out;
352 bus_dmamap_t inmap = sc->sc_map_in[0];
353 uint32_t *prevpa;
354 struct aau_desc_4 **prevp, *cur;
355 int error, seg;
356
357 switch (dreq->dreq_outbuf_type) {
358 case DMOVER_BUF_LINEAR:
359 error = bus_dmamap_load(sc->sc_dmat, dmamap,
360 dreq->dreq_outbuf.dmbuf_linear.l_addr,
361 dreq->dreq_outbuf.dmbuf_linear.l_len, NULL,
362 BUS_DMA_NOWAIT|BUS_DMA_WRITE|BUS_DMA_STREAMING);
363 break;
364
365 case DMOVER_BUF_UIO:
366 {
367 struct uio *uio = dreq->dreq_outbuf.dmbuf_uio;
368
369 if (uio->uio_rw != UIO_READ)
370 return (EINVAL);
371
372 error = bus_dmamap_load_uio(sc->sc_dmat, dmamap,
373 uio, BUS_DMA_NOWAIT|BUS_DMA_WRITE|BUS_DMA_STREAMING);
374 break;
375 }
376 }
377
378 if (__predict_false(error != 0))
379 return (error);
380
381 switch (dreq->dreq_inbuf_type) {
382 case DMOVER_BUF_LINEAR:
383 error = bus_dmamap_load(sc->sc_dmat, inmap,
384 dreq->dreq_inbuf[0].dmbuf_linear.l_addr,
385 dreq->dreq_inbuf[0].dmbuf_linear.l_len, NULL,
386 BUS_DMA_NOWAIT|BUS_DMA_READ|BUS_DMA_STREAMING);
387 break;
388
389 case DMOVER_BUF_UIO:
390 {
391 struct uio *uio = dreq->dreq_inbuf[0].dmbuf_uio;
392
393 if (uio->uio_rw != UIO_WRITE) {
394 error = EINVAL;
395 break;
396 }
397
398 error = bus_dmamap_load_uio(sc->sc_dmat, inmap,
399 uio, BUS_DMA_NOWAIT|BUS_DMA_READ|BUS_DMA_STREAMING);
400 break;
401 }
402 }
403
404 if (__predict_false(error != 0)) {
405 bus_dmamap_unload(sc->sc_dmat, dmamap);
406 return (error);
407 }
408
409 if (dmamap->dm_nsegs != inmap->dm_nsegs) {
410 bus_dmamap_unload(sc->sc_dmat, dmamap);
411 bus_dmamap_unload(sc->sc_dmat, inmap);
412 return (EFAULT); /* "address" error, sort of. */
413 }
414
415 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
416 BUS_DMASYNC_PREWRITE);
417 bus_dmamap_sync(sc->sc_dmat, inmap, 0, inmap->dm_mapsize,
418 BUS_DMASYNC_PREREAD);
419
420 prevp = (struct aau_desc_4 **) &sc->sc_firstdesc;
421 prevpa = &sc->sc_firstdesc_pa;
422
423 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
424 cur = pool_cache_get(&aau_desc_4_cache, PR_NOWAIT);
425 if (cur == NULL) {
426 *prevp = NULL;
427 error = ENOMEM;
428 goto bad;
429 }
430
431 *prevp = cur;
432 *prevpa = cur->d_pa;
433
434 prevp = &cur->d_next;
435 prevpa = &cur->d_nda;
436
437 if (dmamap->dm_segs[seg].ds_len !=
438 inmap->dm_segs[seg].ds_len) {
439 *prevp = NULL;
440 error = EFAULT; /* "address" error, sort of. */
441 goto bad;
442 }
443
444 cur->d_sar1 = inmap->dm_segs[seg].ds_addr;
445 cur->d_dar = dmamap->dm_segs[seg].ds_addr;
446 cur->d_bc = dmamap->dm_segs[seg].ds_len;
447 cur->d_dc = AAU_DC_B1_CC(AAU_DC_CC_DIRECT_FILL) | AAU_DC_DWE;
448 SYNC_DESC_4(cur);
449 }
450
451 *prevp = NULL;
452 *prevpa = 0;
453
454 cur->d_dc |= AAU_DC_IE;
455 SYNC_DESC_4(cur);
456
457 sc->sc_lastdesc = cur;
458
459 return (0);
460
461 bad:
462 iopaau_desc_4_free(sc, sc->sc_firstdesc);
463 bus_dmamap_unload(sc->sc_dmat, sc->sc_map_out);
464 bus_dmamap_unload(sc->sc_dmat, sc->sc_map_in[0]);
465 sc->sc_firstdesc = NULL;
466
467 return (error);
468 }
469
470 int
471 iopaau_intr(void *arg)
472 {
473 struct iopaau_softc *sc = arg;
474 struct dmover_request *dreq;
475 uint32_t asr;
476
477 /* Clear the interrupt. */
478 asr = bus_space_read_4(sc->sc_st, sc->sc_sh, AAU_ASR);
479 if (asr == 0)
480 return (0);
481 bus_space_write_4(sc->sc_st, sc->sc_sh, AAU_ASR, asr);
482
483 /* XXX -- why does this happen? */
484 if (sc->sc_running == NULL) {
485 printf("%s: unexpected interrupt, ASR = 0x%08x\n",
486 sc->sc_dev.dv_xname, asr);
487 return (1);
488 }
489 dreq = sc->sc_running;
490
491 /* Stop the AAU. */
492 bus_space_write_4(sc->sc_st, sc->sc_sh, AAU_ACR, 0);
493
494 DPRINTF(("%s: got interrupt for dreq %p\n", sc->sc_dev.dv_xname,
495 dreq));
496
497 if (__predict_false((asr & AAU_ASR_ETIF) != 0)) {
498 /*
499 * We expect to get end-of-chain interrupts, not
500 * end-of-transfer interrupts, so panic if we get
501 * one of these.
502 */
503 panic("aau_intr: got EOT interrupt");
504 }
505
506 if (__predict_false((asr & AAU_ASR_MA) != 0)) {
507 printf("%s: WARNING: got master abort\n", sc->sc_dev.dv_xname);
508 dreq->dreq_flags |= DMOVER_REQ_ERROR;
509 dreq->dreq_error = EFAULT;
510 }
511
512 /* Finish this transfer, start next one. */
513 iopaau_finish(sc);
514
515 return (1);
516 }
517
518 void
519 iopaau_attach(struct iopaau_softc *sc)
520 {
521 int error, i;
522
523 error = bus_dmamap_create(sc->sc_dmat, AAU_MAX_XFER, AAU_MAX_SEGS,
524 AAU_MAX_XFER, AAU_IO_BOUNDARY, 0, &sc->sc_map_out);
525 if (error) {
526 printf("%s: unable to create output DMA map, error = %d\n",
527 sc->sc_dev.dv_xname, error);
528 return;
529 }
530
531 for (i = 0; i < AAU_MAX_INPUTS; i++) {
532 error = bus_dmamap_create(sc->sc_dmat, AAU_MAX_XFER,
533 AAU_MAX_SEGS, AAU_MAX_XFER, AAU_IO_BOUNDARY, 0,
534 &sc->sc_map_in[i]);
535 if (error) {
536 printf("%s: unable to create input %d DMA map, "
537 "error = %d\n", sc->sc_dev.dv_xname, i, error);
538 return;
539 }
540 }
541
542 /*
543 * Initialize global resources. Ok to do here, since there's
544 * only one AAU.
545 */
546 pool_init(&aau_desc_4_pool, sizeof(struct aau_desc_4),
547 8 * 4, offsetof(struct aau_desc_4, d_nda), 0, "aaud4pl",
548 NULL);
549 pool_cache_init(&aau_desc_4_cache, &aau_desc_4_pool, iopaau_desc_ctor,
550 NULL, NULL);
551
552 /* Register us with dmover. */
553 dmover_backend_register(&sc->sc_dmb);
554 }
555