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