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