Home | History | Annotate | Line # | Download | only in dev
dma.c revision 1.28
      1  1.28  gmcgarry /*	$NetBSD: dma.c,v 1.28 2002/12/22 00:17:15 gmcgarry Exp $	*/
      2  1.20   thorpej 
      3  1.20   thorpej /*-
      4  1.20   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  1.20   thorpej  * All rights reserved.
      6  1.20   thorpej  *
      7  1.20   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.20   thorpej  * by Jason R. Thorpe.
      9  1.20   thorpej  *
     10  1.20   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.20   thorpej  * modification, are permitted provided that the following conditions
     12  1.20   thorpej  * are met:
     13  1.20   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.20   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.20   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.20   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.20   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.20   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.20   thorpej  *    must display the following acknowledgement:
     20  1.20   thorpej  *	This product includes software developed by the NetBSD
     21  1.20   thorpej  *	Foundation, Inc. and its contributors.
     22  1.20   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.20   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.20   thorpej  *    from this software without specific prior written permission.
     25  1.20   thorpej  *
     26  1.20   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.20   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.20   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.20   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.20   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.20   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.20   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.20   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.20   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.20   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.20   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.20   thorpej  */
     38   1.5       cgd 
     39   1.1       cgd /*
     40   1.4   mycroft  * Copyright (c) 1982, 1990, 1993
     41   1.4   mycroft  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.1       cgd  *    must display the following acknowledgement:
     53   1.1       cgd  *	This product includes software developed by the University of
     54   1.1       cgd  *	California, Berkeley and its contributors.
     55   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.1       cgd  *    may be used to endorse or promote products derived from this software
     57   1.1       cgd  *    without specific prior written permission.
     58   1.1       cgd  *
     59   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1       cgd  * SUCH DAMAGE.
     70   1.1       cgd  *
     71   1.5       cgd  *	@(#)dma.c	8.1 (Berkeley) 6/10/93
     72   1.1       cgd  */
     73   1.1       cgd 
     74   1.1       cgd /*
     75   1.1       cgd  * DMA driver
     76   1.1       cgd  */
     77  1.26  gmcgarry 
     78  1.26  gmcgarry #include <sys/cdefs.h>
     79  1.28  gmcgarry __KERNEL_RCSID(0, "$NetBSD: dma.c,v 1.28 2002/12/22 00:17:15 gmcgarry Exp $");
     80   1.1       cgd 
     81  1.18   thorpej #include <machine/hp300spu.h>	/* XXX param.h includes cpu.h */
     82  1.18   thorpej 
     83   1.4   mycroft #include <sys/param.h>
     84   1.4   mycroft #include <sys/systm.h>
     85  1.25   thorpej #include <sys/callout.h>
     86  1.28  gmcgarry #include <sys/device.h>
     87   1.4   mycroft #include <sys/kernel.h>
     88   1.4   mycroft #include <sys/proc.h>
     89   1.4   mycroft 
     90  1.28  gmcgarry #include <machine/bus.h>
     91  1.28  gmcgarry 
     92  1.27       chs #include <m68k/cacheops.h>
     93   1.4   mycroft 
     94  1.28  gmcgarry #include <hp300/dev/intiovar.h>
     95   1.4   mycroft #include <hp300/dev/dmareg.h>
     96   1.4   mycroft #include <hp300/dev/dmavar.h>
     97   1.1       cgd 
     98   1.1       cgd /*
     99   1.1       cgd  * The largest single request will be MAXPHYS bytes which will require
    100   1.1       cgd  * at most MAXPHYS/NBPG+1 chain elements to describe, i.e. if none of
    101   1.1       cgd  * the buffer pages are physically contiguous (MAXPHYS/NBPG) and the
    102   1.1       cgd  * buffer is not page aligned (+1).
    103   1.1       cgd  */
    104   1.1       cgd #define	DMAMAXIO	(MAXPHYS/NBPG+1)
    105   1.1       cgd 
    106  1.19   thorpej struct dma_chain {
    107   1.1       cgd 	int	dc_count;
    108   1.1       cgd 	char	*dc_addr;
    109   1.1       cgd };
    110   1.1       cgd 
    111  1.19   thorpej struct dma_channel {
    112  1.11   thorpej 	struct	dmaqueue *dm_job;		/* current job */
    113   1.6   thorpej 	struct	dmadevice *dm_hwaddr;		/* registers if DMA_C */
    114   1.6   thorpej 	struct	dmaBdevice *dm_Bhwaddr;		/* registers if not DMA_C */
    115   1.6   thorpej 	char	dm_flags;			/* misc. flags */
    116   1.6   thorpej 	u_short	dm_cmd;				/* DMA controller command */
    117  1.11   thorpej 	int	dm_cur;				/* current segment */
    118  1.11   thorpej 	int	dm_last;			/* last segment */
    119   1.6   thorpej 	struct	dma_chain dm_chain[DMAMAXIO];	/* all segments */
    120   1.6   thorpej };
    121   1.6   thorpej 
    122  1.19   thorpej struct dma_softc {
    123  1.28  gmcgarry 	struct  device sc_dev;
    124  1.28  gmcgarry 	bus_space_tag_t sc_bst;
    125  1.28  gmcgarry 	bus_space_handle_t sc_bsh;
    126  1.28  gmcgarry 
    127   1.6   thorpej 	struct	dmareg *sc_dmareg;		/* pointer to our hardware */
    128   1.6   thorpej 	struct	dma_channel sc_chan[NDMACHAN];	/* 2 channels */
    129  1.11   thorpej 	TAILQ_HEAD(, dmaqueue) sc_queue;	/* job queue */
    130  1.25   thorpej 	struct	callout sc_debug_ch;
    131   1.6   thorpej 	char	sc_type;			/* A, B, or C */
    132  1.10   thorpej 	int	sc_ipl;				/* our interrupt level */
    133  1.10   thorpej 	void	*sc_ih;				/* interrupt cookie */
    134  1.28  gmcgarry };
    135   1.1       cgd 
    136   1.1       cgd /* types */
    137   1.1       cgd #define	DMA_B	0
    138   1.1       cgd #define DMA_C	1
    139   1.1       cgd 
    140   1.1       cgd /* flags */
    141   1.1       cgd #define DMAF_PCFLUSH	0x01
    142   1.1       cgd #define DMAF_VCFLUSH	0x02
    143   1.1       cgd #define DMAF_NOINTR	0x04
    144   1.1       cgd 
    145  1.28  gmcgarry int	dmamatch(struct device *, struct cfdata *, void *);
    146  1.28  gmcgarry void	dmaattach(struct device *, struct device *, void *);
    147  1.28  gmcgarry 
    148  1.28  gmcgarry CFATTACH_DECL(dma, sizeof(struct dma_softc),
    149  1.28  gmcgarry     dmamatch, dmaattach, NULL, NULL);
    150  1.28  gmcgarry 
    151   1.7   thorpej int	dmaintr __P((void *));
    152   1.1       cgd 
    153   1.1       cgd #ifdef DEBUG
    154   1.1       cgd int	dmadebug = 0;
    155   1.1       cgd #define DDB_WORD	0x01	/* same as DMAGO_WORD */
    156   1.1       cgd #define DDB_LWORD	0x02	/* same as DMAGO_LWORD */
    157   1.1       cgd #define	DDB_FOLLOW	0x04
    158   1.1       cgd #define DDB_IO		0x08
    159   1.1       cgd 
    160   1.3   mycroft void	dmatimeout __P((void *));
    161   1.6   thorpej int	dmatimo[NDMACHAN];
    162   1.1       cgd 
    163   1.6   thorpej long	dmahits[NDMACHAN];
    164   1.6   thorpej long	dmamisses[NDMACHAN];
    165   1.6   thorpej long	dmabyte[NDMACHAN];
    166   1.6   thorpej long	dmaword[NDMACHAN];
    167   1.6   thorpej long	dmalword[NDMACHAN];
    168   1.1       cgd #endif
    169   1.1       cgd 
    170  1.28  gmcgarry static struct dma_softc *dma_softc;
    171  1.28  gmcgarry 
    172  1.28  gmcgarry int
    173  1.28  gmcgarry dmamatch(parent, match, aux)
    174  1.28  gmcgarry 	struct device *parent;
    175  1.28  gmcgarry 	struct cfdata *match;
    176  1.28  gmcgarry 	void *aux;
    177  1.28  gmcgarry {
    178  1.28  gmcgarry 	struct intio_attach_args *ia = aux;
    179  1.28  gmcgarry 	static int dmafound = 0;                /* can only have one */
    180  1.28  gmcgarry 
    181  1.28  gmcgarry 	if (strcmp("dma", ia->ia_modname) != 0 || dmafound)
    182  1.28  gmcgarry 		return (0);
    183  1.28  gmcgarry 
    184  1.28  gmcgarry 	dmafound = 1;
    185  1.28  gmcgarry 	return (1);
    186  1.28  gmcgarry }
    187  1.28  gmcgarry 
    188  1.28  gmcgarry 
    189  1.28  gmcgarry 
    190   1.1       cgd void
    191  1.28  gmcgarry dmaattach(parent, self, aux)
    192  1.28  gmcgarry 	struct device *parent, *self;
    193  1.28  gmcgarry 	void *aux;
    194   1.1       cgd {
    195  1.28  gmcgarry 	struct dma_softc *sc = (struct dma_softc *)self;
    196  1.28  gmcgarry 	struct intio_attach_args *ia = aux;
    197  1.28  gmcgarry 	struct dma_channel *dc;
    198   1.6   thorpej 	struct dmareg *dma;
    199   1.6   thorpej 	int i;
    200   1.1       cgd 	char rev;
    201   1.1       cgd 
    202   1.6   thorpej 	/* There's just one. */
    203  1.28  gmcgarry 	dma_softc = sc;
    204  1.28  gmcgarry 
    205  1.28  gmcgarry 	sc->sc_bst = ia->ia_bst;
    206  1.28  gmcgarry 	if (bus_space_map(sc->sc_bst, ia->ia_iobase, INTIO_DEVSIZE, 0,
    207  1.28  gmcgarry 	     &sc->sc_bsh)) {
    208  1.28  gmcgarry 		printf("%s: can't map registers\n", sc->sc_dev.dv_xname);
    209  1.28  gmcgarry 		return;
    210  1.28  gmcgarry 	}
    211  1.28  gmcgarry 
    212  1.28  gmcgarry 	dma = (struct dmareg *)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
    213  1.28  gmcgarry 	sc->sc_dmareg = dma;
    214   1.6   thorpej 
    215   1.1       cgd 	/*
    216   1.6   thorpej 	 * Determine the DMA type.  A DMA_A or DMA_B will fail the
    217   1.6   thorpej 	 * following probe.
    218   1.6   thorpej 	 *
    219   1.6   thorpej 	 * XXX Don't know how to easily differentiate the A and B cards,
    220   1.1       cgd 	 * so we just hope nobody has an A card (A cards will work if
    221  1.10   thorpej 	 * splbio works out to ipl 3).
    222   1.1       cgd 	 */
    223   1.6   thorpej 	if (badbaddr((char *)&dma->dma_id[2])) {
    224   1.1       cgd 		rev = 'B';
    225   1.1       cgd #if !defined(HP320)
    226   1.1       cgd 		panic("dmainit: DMA card requires hp320 support");
    227   1.1       cgd #endif
    228   1.6   thorpej 	} else
    229   1.6   thorpej 		rev = dma->dma_id[2];
    230   1.6   thorpej 
    231   1.6   thorpej 	sc->sc_type = (rev == 'B') ? DMA_B : DMA_C;
    232   1.1       cgd 
    233  1.11   thorpej 	TAILQ_INIT(&sc->sc_queue);
    234  1.25   thorpej 	callout_init(&sc->sc_debug_ch);
    235  1.11   thorpej 
    236   1.6   thorpej 	for (i = 0; i < NDMACHAN; i++) {
    237   1.6   thorpej 		dc = &sc->sc_chan[i];
    238  1.11   thorpej 		dc->dm_job = NULL;
    239   1.6   thorpej 		switch (i) {
    240   1.6   thorpej 		case 0:
    241   1.6   thorpej 			dc->dm_hwaddr = &dma->dma_chan0;
    242   1.6   thorpej 			dc->dm_Bhwaddr = &dma->dma_Bchan0;
    243   1.6   thorpej 			break;
    244   1.6   thorpej 
    245   1.6   thorpej 		case 1:
    246   1.6   thorpej 			dc->dm_hwaddr = &dma->dma_chan1;
    247   1.6   thorpej 			dc->dm_Bhwaddr = &dma->dma_Bchan1;
    248   1.6   thorpej 			break;
    249   1.6   thorpej 
    250   1.6   thorpej 		default:
    251   1.6   thorpej 			panic("dmainit: more than 2 channels?");
    252   1.6   thorpej 			/* NOTREACHED */
    253   1.6   thorpej 		}
    254   1.1       cgd 	}
    255  1.11   thorpej 
    256   1.1       cgd #ifdef DEBUG
    257   1.1       cgd 	/* make sure timeout is really not needed */
    258  1.25   thorpej 	callout_reset(&sc->sc_debug_ch, 30 * hz, dmatimeout, sc);
    259   1.1       cgd #endif
    260   1.1       cgd 
    261  1.28  gmcgarry 	printf(": 98620%c, 2 channels, %d-bit DMA\n",
    262  1.19   thorpej 	    rev, (rev == 'B') ? 16 : 32);
    263   1.7   thorpej 
    264  1.10   thorpej 	/*
    265  1.10   thorpej 	 * Defer hooking up our interrupt until the first
    266  1.10   thorpej 	 * DMA-using controller has hooked up theirs.
    267  1.10   thorpej 	 */
    268  1.10   thorpej 	sc->sc_ih = NULL;
    269  1.10   thorpej }
    270  1.10   thorpej 
    271  1.10   thorpej /*
    272  1.10   thorpej  * Compute the ipl and (re)establish the interrupt handler
    273  1.10   thorpej  * for the DMA controller.
    274  1.10   thorpej  */
    275  1.10   thorpej void
    276  1.10   thorpej dmacomputeipl()
    277  1.10   thorpej {
    278  1.28  gmcgarry 	struct dma_softc *sc = dma_softc;
    279  1.10   thorpej 
    280  1.10   thorpej 	if (sc->sc_ih != NULL)
    281  1.17   thorpej 		intr_disestablish(sc->sc_ih);
    282  1.10   thorpej 
    283  1.10   thorpej 	/*
    284  1.10   thorpej 	 * Our interrupt level must be as high as the highest
    285  1.10   thorpej 	 * device using DMA (i.e. splbio).
    286  1.10   thorpej 	 */
    287  1.24   thorpej 	sc->sc_ipl = PSLTOIPL(hp300_ipls[HP300_IPL_BIO]);
    288  1.17   thorpej 	sc->sc_ih = intr_establish(dmaintr, sc, sc->sc_ipl, IPL_BIO);
    289   1.1       cgd }
    290   1.1       cgd 
    291   1.1       cgd int
    292   1.1       cgd dmareq(dq)
    293  1.11   thorpej 	struct dmaqueue *dq;
    294   1.1       cgd {
    295  1.28  gmcgarry 	struct dma_softc *sc = dma_softc;
    296  1.11   thorpej 	int i, chan, s;
    297  1.11   thorpej 
    298  1.11   thorpej #if 1
    299  1.11   thorpej 	s = splhigh();	/* XXXthorpej */
    300  1.11   thorpej #else
    301  1.11   thorpej 	s = splbio();
    302  1.11   thorpej #endif
    303  1.11   thorpej 
    304  1.11   thorpej 	chan = dq->dq_chan;
    305  1.11   thorpej 	for (i = NDMACHAN - 1; i >= 0; i--) {
    306  1.11   thorpej 		/*
    307  1.11   thorpej 		 * Can we use this channel?
    308  1.11   thorpej 		 */
    309   1.1       cgd 		if ((chan & (1 << i)) == 0)
    310   1.1       cgd 			continue;
    311  1.11   thorpej 
    312  1.11   thorpej 		/*
    313  1.11   thorpej 		 * We can use it; is it busy?
    314  1.11   thorpej 		 */
    315  1.11   thorpej 		if (sc->sc_chan[i].dm_job != NULL)
    316   1.1       cgd 			continue;
    317  1.11   thorpej 
    318  1.11   thorpej 		/*
    319  1.11   thorpej 		 * Not busy; give the caller this channel.
    320  1.11   thorpej 		 */
    321  1.11   thorpej 		sc->sc_chan[i].dm_job = dq;
    322  1.11   thorpej 		dq->dq_chan = i;
    323   1.1       cgd 		splx(s);
    324  1.11   thorpej 		return (1);
    325   1.1       cgd 	}
    326  1.11   thorpej 
    327  1.11   thorpej 	/*
    328  1.11   thorpej 	 * Couldn't get a channel now; put this in the queue.
    329  1.11   thorpej 	 */
    330  1.11   thorpej 	TAILQ_INSERT_TAIL(&sc->sc_queue, dq, dq_list);
    331   1.1       cgd 	splx(s);
    332  1.11   thorpej 	return (0);
    333   1.1       cgd }
    334   1.1       cgd 
    335   1.1       cgd void
    336   1.1       cgd dmafree(dq)
    337  1.11   thorpej 	struct dmaqueue *dq;
    338   1.1       cgd {
    339  1.11   thorpej 	int unit = dq->dq_chan;
    340  1.28  gmcgarry 	struct dma_softc *sc = dma_softc;
    341  1.11   thorpej 	struct dma_channel *dc = &sc->sc_chan[unit];
    342  1.11   thorpej 	struct dmaqueue *dn;
    343  1.11   thorpej 	int chan, s;
    344  1.11   thorpej 
    345  1.11   thorpej #if 1
    346  1.11   thorpej 	s = splhigh();	/* XXXthorpej */
    347  1.11   thorpej #else
    348  1.11   thorpej 	s = splbio();
    349  1.11   thorpej #endif
    350   1.1       cgd 
    351   1.1       cgd #ifdef DEBUG
    352   1.1       cgd 	dmatimo[unit] = 0;
    353   1.1       cgd #endif
    354  1.11   thorpej 
    355   1.1       cgd 	DMA_CLEAR(dc);
    356  1.18   thorpej 
    357  1.18   thorpej #if defined(CACHE_HAVE_PAC) || defined(M68040)
    358   1.1       cgd 	/*
    359   1.1       cgd 	 * XXX we may not always go thru the flush code in dmastop()
    360   1.1       cgd 	 */
    361   1.6   thorpej 	if (dc->dm_flags & DMAF_PCFLUSH) {
    362   1.1       cgd 		PCIA();
    363   1.6   thorpej 		dc->dm_flags &= ~DMAF_PCFLUSH;
    364   1.1       cgd 	}
    365   1.1       cgd #endif
    366  1.18   thorpej 
    367  1.18   thorpej #if defined(CACHE_HAVE_VAC)
    368   1.6   thorpej 	if (dc->dm_flags & DMAF_VCFLUSH) {
    369   1.1       cgd 		/*
    370   1.1       cgd 		 * 320/350s have VACs that may also need flushing.
    371   1.1       cgd 		 * In our case we only flush the supervisor side
    372   1.1       cgd 		 * because we know that if we are DMAing to user
    373   1.1       cgd 		 * space, the physical pages will also be mapped
    374   1.1       cgd 		 * in kernel space (via vmapbuf) and hence cache-
    375   1.1       cgd 		 * inhibited by the pmap module due to the multiple
    376   1.1       cgd 		 * mapping.
    377   1.1       cgd 		 */
    378   1.1       cgd 		DCIS();
    379   1.6   thorpej 		dc->dm_flags &= ~DMAF_VCFLUSH;
    380   1.1       cgd 	}
    381   1.1       cgd #endif
    382  1.18   thorpej 
    383  1.11   thorpej 	/*
    384  1.11   thorpej 	 * Channel is now free.  Look for another job to run on this
    385  1.11   thorpej 	 * channel.
    386  1.11   thorpej 	 */
    387  1.11   thorpej 	dc->dm_job = NULL;
    388   1.1       cgd 	chan = 1 << unit;
    389  1.11   thorpej 	for (dn = sc->sc_queue.tqh_first; dn != NULL;
    390  1.11   thorpej 	    dn = dn->dq_list.tqe_next) {
    391  1.11   thorpej 		if (dn->dq_chan & chan) {
    392  1.11   thorpej 			/* Found one... */
    393  1.11   thorpej 			TAILQ_REMOVE(&sc->sc_queue, dn, dq_list);
    394  1.11   thorpej 			dc->dm_job = dn;
    395  1.11   thorpej 			dn->dq_chan = dq->dq_chan;
    396   1.1       cgd 			splx(s);
    397  1.11   thorpej 
    398  1.11   thorpej 			/* Start the initiator. */
    399  1.11   thorpej 			(*dn->dq_start)(dn->dq_softc);
    400   1.1       cgd 			return;
    401   1.1       cgd 		}
    402   1.1       cgd 	}
    403   1.1       cgd 	splx(s);
    404   1.1       cgd }
    405   1.1       cgd 
    406   1.1       cgd void
    407   1.1       cgd dmago(unit, addr, count, flags)
    408   1.1       cgd 	int unit;
    409  1.13    scottr 	char *addr;
    410  1.13    scottr 	int count;
    411  1.13    scottr 	int flags;
    412   1.1       cgd {
    413  1.28  gmcgarry 	struct dma_softc *sc = dma_softc;
    414  1.13    scottr 	struct dma_channel *dc = &sc->sc_chan[unit];
    415  1.13    scottr 	char *dmaend = NULL;
    416  1.13    scottr 	int seg, tcount;
    417   1.1       cgd 
    418   1.1       cgd 	if (count > MAXPHYS)
    419   1.1       cgd 		panic("dmago: count > MAXPHYS");
    420  1.18   thorpej 
    421   1.1       cgd #if defined(HP320)
    422   1.6   thorpej 	if (sc->sc_type == DMA_B && (flags & DMAGO_LWORD))
    423   1.1       cgd 		panic("dmago: no can do 32-bit DMA");
    424   1.1       cgd #endif
    425  1.18   thorpej 
    426   1.1       cgd #ifdef DEBUG
    427   1.1       cgd 	if (dmadebug & DDB_FOLLOW)
    428  1.15    scottr 		printf("dmago(%d, %p, %x, %x)\n",
    429   1.1       cgd 		       unit, addr, count, flags);
    430   1.1       cgd 	if (flags & DMAGO_LWORD)
    431   1.1       cgd 		dmalword[unit]++;
    432   1.1       cgd 	else if (flags & DMAGO_WORD)
    433   1.1       cgd 		dmaword[unit]++;
    434   1.1       cgd 	else
    435   1.1       cgd 		dmabyte[unit]++;
    436   1.1       cgd #endif
    437   1.1       cgd 	/*
    438   1.1       cgd 	 * Build the DMA chain
    439   1.1       cgd 	 */
    440  1.11   thorpej 	for (seg = 0; count > 0; seg++) {
    441  1.11   thorpej 		dc->dm_chain[seg].dc_addr = (char *) kvtop(addr);
    442  1.18   thorpej #if defined(M68040)
    443   1.4   mycroft 		/*
    444   1.4   mycroft 		 * Push back dirty cache lines
    445   1.4   mycroft 		 */
    446   1.4   mycroft 		if (mmutype == MMU_68040)
    447  1.23    kleink 			DCFP((paddr_t)dc->dm_chain[seg].dc_addr);
    448   1.4   mycroft #endif
    449   1.1       cgd 		if (count < (tcount = NBPG - ((int)addr & PGOFSET)))
    450   1.1       cgd 			tcount = count;
    451  1.11   thorpej 		dc->dm_chain[seg].dc_count = tcount;
    452   1.1       cgd 		addr += tcount;
    453   1.1       cgd 		count -= tcount;
    454   1.1       cgd 		if (flags & DMAGO_LWORD)
    455   1.1       cgd 			tcount >>= 2;
    456   1.1       cgd 		else if (flags & DMAGO_WORD)
    457   1.1       cgd 			tcount >>= 1;
    458  1.11   thorpej 
    459  1.11   thorpej 		/*
    460  1.11   thorpej 		 * Try to compact the DMA transfer if the pages are adjacent.
    461  1.11   thorpej 		 * Note: this will never happen on the first iteration.
    462  1.11   thorpej 		 */
    463  1.11   thorpej 		if (dc->dm_chain[seg].dc_addr == dmaend
    464   1.1       cgd #if defined(HP320)
    465   1.1       cgd 		    /* only 16-bit count on 98620B */
    466   1.6   thorpej 		    && (sc->sc_type != DMA_B ||
    467  1.11   thorpej 			dc->dm_chain[seg - 1].dc_count + tcount <= 65536)
    468   1.1       cgd #endif
    469   1.1       cgd 		) {
    470   1.1       cgd #ifdef DEBUG
    471   1.1       cgd 			dmahits[unit]++;
    472   1.1       cgd #endif
    473  1.11   thorpej 			dmaend += dc->dm_chain[seg].dc_count;
    474  1.11   thorpej 			dc->dm_chain[--seg].dc_count += tcount;
    475   1.1       cgd 		} else {
    476   1.1       cgd #ifdef DEBUG
    477   1.1       cgd 			dmamisses[unit]++;
    478   1.1       cgd #endif
    479  1.11   thorpej 			dmaend = dc->dm_chain[seg].dc_addr +
    480  1.11   thorpej 			    dc->dm_chain[seg].dc_count;
    481  1.11   thorpej 			dc->dm_chain[seg].dc_count = tcount;
    482   1.1       cgd 		}
    483   1.1       cgd 	}
    484  1.11   thorpej 	dc->dm_cur = 0;
    485  1.11   thorpej 	dc->dm_last = --seg;
    486   1.6   thorpej 	dc->dm_flags = 0;
    487   1.1       cgd 	/*
    488   1.1       cgd 	 * Set up the command word based on flags
    489   1.1       cgd 	 */
    490  1.10   thorpej 	dc->dm_cmd = DMA_ENAB | DMA_IPL(sc->sc_ipl) | DMA_START;
    491   1.1       cgd 	if ((flags & DMAGO_READ) == 0)
    492   1.6   thorpej 		dc->dm_cmd |= DMA_WRT;
    493   1.1       cgd 	if (flags & DMAGO_LWORD)
    494   1.6   thorpej 		dc->dm_cmd |= DMA_LWORD;
    495   1.1       cgd 	else if (flags & DMAGO_WORD)
    496   1.6   thorpej 		dc->dm_cmd |= DMA_WORD;
    497   1.1       cgd 	if (flags & DMAGO_PRI)
    498   1.6   thorpej 		dc->dm_cmd |= DMA_PRI;
    499  1.18   thorpej 
    500  1.18   thorpej #if defined(M68040)
    501   1.4   mycroft 	/*
    502   1.4   mycroft 	 * On the 68040 we need to flush (push) the data cache before a
    503   1.4   mycroft 	 * DMA (already done above) and flush again after DMA completes.
    504   1.4   mycroft 	 * In theory we should only need to flush prior to a write DMA
    505   1.4   mycroft 	 * and purge after a read DMA but if the entire page is not
    506   1.4   mycroft 	 * involved in the DMA we might purge some valid data.
    507   1.4   mycroft 	 */
    508   1.4   mycroft 	if (mmutype == MMU_68040 && (flags & DMAGO_READ))
    509   1.6   thorpej 		dc->dm_flags |= DMAF_PCFLUSH;
    510   1.4   mycroft #endif
    511  1.18   thorpej 
    512  1.18   thorpej #if defined(CACHE_HAVE_PAC)
    513   1.1       cgd 	/*
    514   1.1       cgd 	 * Remember if we need to flush external physical cache when
    515   1.1       cgd 	 * DMA is done.  We only do this if we are reading (writing memory).
    516   1.1       cgd 	 */
    517   1.1       cgd 	if (ectype == EC_PHYS && (flags & DMAGO_READ))
    518   1.6   thorpej 		dc->dm_flags |= DMAF_PCFLUSH;
    519   1.1       cgd #endif
    520  1.18   thorpej 
    521  1.18   thorpej #if defined(CACHE_HAVE_VAC)
    522   1.1       cgd 	if (ectype == EC_VIRT && (flags & DMAGO_READ))
    523   1.6   thorpej 		dc->dm_flags |= DMAF_VCFLUSH;
    524   1.1       cgd #endif
    525  1.18   thorpej 
    526   1.1       cgd 	/*
    527   1.1       cgd 	 * Remember if we can skip the dma completion interrupt on
    528   1.1       cgd 	 * the last segment in the chain.
    529   1.1       cgd 	 */
    530   1.1       cgd 	if (flags & DMAGO_NOINT) {
    531   1.6   thorpej 		if (dc->dm_cur == dc->dm_last)
    532   1.6   thorpej 			dc->dm_cmd &= ~DMA_ENAB;
    533   1.1       cgd 		else
    534   1.6   thorpej 			dc->dm_flags |= DMAF_NOINTR;
    535   1.1       cgd 	}
    536   1.1       cgd #ifdef DEBUG
    537  1.11   thorpej 	if (dmadebug & DDB_IO) {
    538  1.15    scottr 		if (((dmadebug&DDB_WORD) && (dc->dm_cmd&DMA_WORD)) ||
    539  1.15    scottr 		    ((dmadebug&DDB_LWORD) && (dc->dm_cmd&DMA_LWORD))) {
    540   1.9  christos 			printf("dmago: cmd %x, flags %x\n",
    541   1.6   thorpej 			       dc->dm_cmd, dc->dm_flags);
    542  1.11   thorpej 			for (seg = 0; seg <= dc->dm_last; seg++)
    543  1.15    scottr 				printf("  %d: %d@%p\n", seg,
    544  1.11   thorpej 				    dc->dm_chain[seg].dc_count,
    545  1.11   thorpej 				    dc->dm_chain[seg].dc_addr);
    546   1.1       cgd 		}
    547  1.11   thorpej 	}
    548   1.1       cgd 	dmatimo[unit] = 1;
    549   1.1       cgd #endif
    550  1.19   thorpej 	DMA_ARM(sc, dc);
    551   1.1       cgd }
    552   1.1       cgd 
    553   1.1       cgd void
    554   1.1       cgd dmastop(unit)
    555  1.13    scottr 	int unit;
    556   1.1       cgd {
    557  1.28  gmcgarry 	struct dma_softc *sc = dma_softc;
    558  1.13    scottr 	struct dma_channel *dc = &sc->sc_chan[unit];
    559   1.1       cgd 
    560   1.1       cgd #ifdef DEBUG
    561   1.1       cgd 	if (dmadebug & DDB_FOLLOW)
    562   1.9  christos 		printf("dmastop(%d)\n", unit);
    563   1.1       cgd 	dmatimo[unit] = 0;
    564   1.1       cgd #endif
    565   1.1       cgd 	DMA_CLEAR(dc);
    566  1.18   thorpej 
    567  1.18   thorpej #if defined(CACHE_HAVE_PAC) || defined(M68040)
    568   1.6   thorpej 	if (dc->dm_flags & DMAF_PCFLUSH) {
    569   1.1       cgd 		PCIA();
    570   1.6   thorpej 		dc->dm_flags &= ~DMAF_PCFLUSH;
    571   1.1       cgd 	}
    572   1.1       cgd #endif
    573  1.18   thorpej 
    574  1.18   thorpej #if defined(CACHE_HAVE_VAC)
    575   1.6   thorpej 	if (dc->dm_flags & DMAF_VCFLUSH) {
    576   1.1       cgd 		/*
    577   1.1       cgd 		 * 320/350s have VACs that may also need flushing.
    578   1.1       cgd 		 * In our case we only flush the supervisor side
    579   1.1       cgd 		 * because we know that if we are DMAing to user
    580   1.1       cgd 		 * space, the physical pages will also be mapped
    581   1.1       cgd 		 * in kernel space (via vmapbuf) and hence cache-
    582   1.1       cgd 		 * inhibited by the pmap module due to the multiple
    583   1.1       cgd 		 * mapping.
    584   1.1       cgd 		 */
    585   1.1       cgd 		DCIS();
    586   1.6   thorpej 		dc->dm_flags &= ~DMAF_VCFLUSH;
    587   1.1       cgd 	}
    588   1.1       cgd #endif
    589  1.18   thorpej 
    590   1.1       cgd 	/*
    591   1.1       cgd 	 * We may get this interrupt after a device service routine
    592   1.1       cgd 	 * has freed the dma channel.  So, ignore the intr if there's
    593   1.1       cgd 	 * nothing on the queue.
    594   1.1       cgd 	 */
    595  1.11   thorpej 	if (dc->dm_job != NULL)
    596  1.11   thorpej 		(*dc->dm_job->dq_done)(dc->dm_job->dq_softc);
    597   1.1       cgd }
    598   1.1       cgd 
    599   1.1       cgd int
    600   1.7   thorpej dmaintr(arg)
    601   1.7   thorpej 	void *arg;
    602   1.1       cgd {
    603   1.7   thorpej 	struct dma_softc *sc = arg;
    604  1.13    scottr 	struct dma_channel *dc;
    605  1.13    scottr 	int i, stat;
    606   1.1       cgd 	int found = 0;
    607   1.1       cgd 
    608   1.1       cgd #ifdef DEBUG
    609   1.1       cgd 	if (dmadebug & DDB_FOLLOW)
    610   1.9  christos 		printf("dmaintr\n");
    611   1.1       cgd #endif
    612   1.6   thorpej 	for (i = 0; i < NDMACHAN; i++) {
    613   1.6   thorpej 		dc = &sc->sc_chan[i];
    614   1.1       cgd 		stat = DMA_STAT(dc);
    615   1.1       cgd 		if ((stat & DMA_INTR) == 0)
    616   1.1       cgd 			continue;
    617   1.1       cgd 		found++;
    618   1.1       cgd #ifdef DEBUG
    619   1.1       cgd 		if (dmadebug & DDB_IO) {
    620  1.15    scottr 			if (((dmadebug&DDB_WORD) && (dc->dm_cmd&DMA_WORD)) ||
    621  1.15    scottr 			    ((dmadebug&DDB_LWORD) && (dc->dm_cmd&DMA_LWORD)))
    622  1.11   thorpej 			  printf("dmaintr: flags %x unit %d stat %x next %d\n",
    623  1.11   thorpej 			   dc->dm_flags, i, stat, dc->dm_cur + 1);
    624   1.1       cgd 		}
    625   1.1       cgd 		if (stat & DMA_ARMED)
    626  1.19   thorpej 			printf("dma channel %d: intr when armed\n", i);
    627   1.1       cgd #endif
    628  1.11   thorpej 		/*
    629  1.11   thorpej 		 * Load the next segemnt, or finish up if we're done.
    630  1.11   thorpej 		 */
    631  1.11   thorpej 		dc->dm_cur++;
    632  1.11   thorpej 		if (dc->dm_cur <= dc->dm_last) {
    633   1.1       cgd #ifdef DEBUG
    634   1.1       cgd 			dmatimo[i] = 1;
    635   1.1       cgd #endif
    636   1.1       cgd 			/*
    637  1.11   thorpej 			 * If we're the last segment, disable the
    638  1.11   thorpej 			 * completion interrupt, if necessary.
    639   1.1       cgd 			 */
    640   1.6   thorpej 			if (dc->dm_cur == dc->dm_last &&
    641   1.6   thorpej 			    (dc->dm_flags & DMAF_NOINTR))
    642   1.6   thorpej 				dc->dm_cmd &= ~DMA_ENAB;
    643   1.1       cgd 			DMA_CLEAR(dc);
    644  1.19   thorpej 			DMA_ARM(sc, dc);
    645   1.1       cgd 		} else
    646   1.1       cgd 			dmastop(i);
    647   1.1       cgd 	}
    648   1.1       cgd 	return(found);
    649   1.1       cgd }
    650   1.1       cgd 
    651   1.1       cgd #ifdef DEBUG
    652   1.1       cgd void
    653   1.3   mycroft dmatimeout(arg)
    654   1.3   mycroft 	void *arg;
    655   1.1       cgd {
    656  1.13    scottr 	int i, s;
    657   1.6   thorpej 	struct dma_softc *sc = arg;
    658   1.1       cgd 
    659   1.6   thorpej 	for (i = 0; i < NDMACHAN; i++) {
    660   1.1       cgd 		s = splbio();
    661   1.1       cgd 		if (dmatimo[i]) {
    662   1.1       cgd 			if (dmatimo[i] > 1)
    663  1.19   thorpej 				printf("dma channel %d timeout #%d\n",
    664  1.19   thorpej 				    i, dmatimo[i]-1);
    665   1.1       cgd 			dmatimo[i]++;
    666   1.1       cgd 		}
    667   1.1       cgd 		splx(s);
    668   1.1       cgd 	}
    669  1.25   thorpej 	callout_reset(&sc->sc_debug_ch, 30 * hz, dmatimeout, sc);
    670   1.1       cgd }
    671   1.1       cgd #endif
    672