Home | History | Annotate | Line # | Download | only in dev
dma.c revision 1.25.12.2
      1  1.25.12.1   nathanw /*	$NetBSD: dma.c,v 1.25.12.2 2002/11/11 21:58:11 nathanw 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.25.12.1   nathanw 
     78  1.25.12.1   nathanw #include <sys/cdefs.h>
     79  1.25.12.1   nathanw __KERNEL_RCSID(0, "$NetBSD: dma.c,v 1.25.12.2 2002/11/11 21:58:11 nathanw 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.4   mycroft #include <sys/time.h>
     87        1.4   mycroft #include <sys/kernel.h>
     88        1.4   mycroft #include <sys/proc.h>
     89       1.11   thorpej #include <sys/device.h>
     90        1.4   mycroft 
     91       1.14    scottr #include <machine/frame.h>
     92        1.4   mycroft #include <machine/cpu.h>
     93       1.17   thorpej #include <machine/intr.h>
     94  1.25.12.2   nathanw #include <m68k/cacheops.h>
     95        1.4   mycroft 
     96        1.4   mycroft #include <hp300/dev/dmareg.h>
     97        1.4   mycroft #include <hp300/dev/dmavar.h>
     98        1.1       cgd 
     99        1.1       cgd /*
    100        1.1       cgd  * The largest single request will be MAXPHYS bytes which will require
    101        1.1       cgd  * at most MAXPHYS/NBPG+1 chain elements to describe, i.e. if none of
    102        1.1       cgd  * the buffer pages are physically contiguous (MAXPHYS/NBPG) and the
    103        1.1       cgd  * buffer is not page aligned (+1).
    104        1.1       cgd  */
    105        1.1       cgd #define	DMAMAXIO	(MAXPHYS/NBPG+1)
    106        1.1       cgd 
    107       1.19   thorpej struct dma_chain {
    108        1.1       cgd 	int	dc_count;
    109        1.1       cgd 	char	*dc_addr;
    110        1.1       cgd };
    111        1.1       cgd 
    112       1.19   thorpej struct dma_channel {
    113       1.11   thorpej 	struct	dmaqueue *dm_job;		/* current job */
    114        1.6   thorpej 	struct	dmadevice *dm_hwaddr;		/* registers if DMA_C */
    115        1.6   thorpej 	struct	dmaBdevice *dm_Bhwaddr;		/* registers if not DMA_C */
    116        1.6   thorpej 	char	dm_flags;			/* misc. flags */
    117        1.6   thorpej 	u_short	dm_cmd;				/* DMA controller command */
    118       1.11   thorpej 	int	dm_cur;				/* current segment */
    119       1.11   thorpej 	int	dm_last;			/* last segment */
    120        1.6   thorpej 	struct	dma_chain dm_chain[DMAMAXIO];	/* all segments */
    121        1.6   thorpej };
    122        1.6   thorpej 
    123       1.19   thorpej struct dma_softc {
    124        1.6   thorpej 	struct	dmareg *sc_dmareg;		/* pointer to our hardware */
    125        1.6   thorpej 	struct	dma_channel sc_chan[NDMACHAN];	/* 2 channels */
    126       1.11   thorpej 	TAILQ_HEAD(, dmaqueue) sc_queue;	/* job queue */
    127       1.25   thorpej 	struct	callout sc_debug_ch;
    128        1.6   thorpej 	char	sc_type;			/* A, B, or C */
    129       1.10   thorpej 	int	sc_ipl;				/* our interrupt level */
    130       1.10   thorpej 	void	*sc_ih;				/* interrupt cookie */
    131       1.19   thorpej } dma_softc;
    132        1.1       cgd 
    133        1.1       cgd /* types */
    134        1.1       cgd #define	DMA_B	0
    135        1.1       cgd #define DMA_C	1
    136        1.1       cgd 
    137        1.1       cgd /* flags */
    138        1.1       cgd #define DMAF_PCFLUSH	0x01
    139        1.1       cgd #define DMAF_VCFLUSH	0x02
    140        1.1       cgd #define DMAF_NOINTR	0x04
    141        1.1       cgd 
    142        1.7   thorpej int	dmaintr __P((void *));
    143        1.1       cgd 
    144        1.1       cgd #ifdef DEBUG
    145        1.1       cgd int	dmadebug = 0;
    146        1.1       cgd #define DDB_WORD	0x01	/* same as DMAGO_WORD */
    147        1.1       cgd #define DDB_LWORD	0x02	/* same as DMAGO_LWORD */
    148        1.1       cgd #define	DDB_FOLLOW	0x04
    149        1.1       cgd #define DDB_IO		0x08
    150        1.1       cgd 
    151        1.3   mycroft void	dmatimeout __P((void *));
    152        1.6   thorpej int	dmatimo[NDMACHAN];
    153        1.1       cgd 
    154        1.6   thorpej long	dmahits[NDMACHAN];
    155        1.6   thorpej long	dmamisses[NDMACHAN];
    156        1.6   thorpej long	dmabyte[NDMACHAN];
    157        1.6   thorpej long	dmaword[NDMACHAN];
    158        1.6   thorpej long	dmalword[NDMACHAN];
    159        1.1       cgd #endif
    160        1.1       cgd 
    161       1.19   thorpej /*
    162       1.19   thorpej  * Initialize the DMA engine, called by dioattach()
    163       1.19   thorpej  */
    164        1.1       cgd void
    165        1.1       cgd dmainit()
    166        1.1       cgd {
    167       1.19   thorpej 	struct dma_softc *sc = &dma_softc;
    168        1.6   thorpej 	struct dmareg *dma;
    169        1.6   thorpej 	struct dma_channel *dc;
    170        1.6   thorpej 	int i;
    171        1.1       cgd 	char rev;
    172        1.1       cgd 
    173        1.6   thorpej 	/* There's just one. */
    174        1.6   thorpej 	sc->sc_dmareg = (struct dmareg *)DMA_BASE;
    175        1.6   thorpej 	dma = sc->sc_dmareg;
    176        1.6   thorpej 
    177        1.1       cgd 	/*
    178        1.6   thorpej 	 * Determine the DMA type.  A DMA_A or DMA_B will fail the
    179        1.6   thorpej 	 * following probe.
    180        1.6   thorpej 	 *
    181        1.6   thorpej 	 * XXX Don't know how to easily differentiate the A and B cards,
    182        1.1       cgd 	 * so we just hope nobody has an A card (A cards will work if
    183       1.10   thorpej 	 * splbio works out to ipl 3).
    184        1.1       cgd 	 */
    185        1.6   thorpej 	if (badbaddr((char *)&dma->dma_id[2])) {
    186        1.1       cgd 		rev = 'B';
    187        1.1       cgd #if !defined(HP320)
    188        1.1       cgd 		panic("dmainit: DMA card requires hp320 support");
    189        1.1       cgd #endif
    190        1.6   thorpej 	} else
    191        1.6   thorpej 		rev = dma->dma_id[2];
    192        1.6   thorpej 
    193        1.6   thorpej 	sc->sc_type = (rev == 'B') ? DMA_B : DMA_C;
    194        1.1       cgd 
    195       1.11   thorpej 	TAILQ_INIT(&sc->sc_queue);
    196       1.25   thorpej 	callout_init(&sc->sc_debug_ch);
    197       1.11   thorpej 
    198        1.6   thorpej 	for (i = 0; i < NDMACHAN; i++) {
    199        1.6   thorpej 		dc = &sc->sc_chan[i];
    200       1.11   thorpej 		dc->dm_job = NULL;
    201        1.6   thorpej 		switch (i) {
    202        1.6   thorpej 		case 0:
    203        1.6   thorpej 			dc->dm_hwaddr = &dma->dma_chan0;
    204        1.6   thorpej 			dc->dm_Bhwaddr = &dma->dma_Bchan0;
    205        1.6   thorpej 			break;
    206        1.6   thorpej 
    207        1.6   thorpej 		case 1:
    208        1.6   thorpej 			dc->dm_hwaddr = &dma->dma_chan1;
    209        1.6   thorpej 			dc->dm_Bhwaddr = &dma->dma_Bchan1;
    210        1.6   thorpej 			break;
    211        1.6   thorpej 
    212        1.6   thorpej 		default:
    213        1.6   thorpej 			panic("dmainit: more than 2 channels?");
    214        1.6   thorpej 			/* NOTREACHED */
    215        1.6   thorpej 		}
    216        1.1       cgd 	}
    217       1.11   thorpej 
    218        1.1       cgd #ifdef DEBUG
    219        1.1       cgd 	/* make sure timeout is really not needed */
    220       1.25   thorpej 	callout_reset(&sc->sc_debug_ch, 30 * hz, dmatimeout, sc);
    221        1.1       cgd #endif
    222        1.1       cgd 
    223       1.19   thorpej 	printf("98620%c, 2 channels, %d bit DMA\n",
    224       1.19   thorpej 	    rev, (rev == 'B') ? 16 : 32);
    225        1.7   thorpej 
    226       1.10   thorpej 	/*
    227       1.10   thorpej 	 * Defer hooking up our interrupt until the first
    228       1.10   thorpej 	 * DMA-using controller has hooked up theirs.
    229       1.10   thorpej 	 */
    230       1.10   thorpej 	sc->sc_ih = NULL;
    231       1.10   thorpej }
    232       1.10   thorpej 
    233       1.10   thorpej /*
    234       1.10   thorpej  * Compute the ipl and (re)establish the interrupt handler
    235       1.10   thorpej  * for the DMA controller.
    236       1.10   thorpej  */
    237       1.10   thorpej void
    238       1.10   thorpej dmacomputeipl()
    239       1.10   thorpej {
    240       1.19   thorpej 	struct dma_softc *sc = &dma_softc;
    241       1.10   thorpej 
    242       1.10   thorpej 	if (sc->sc_ih != NULL)
    243       1.17   thorpej 		intr_disestablish(sc->sc_ih);
    244       1.10   thorpej 
    245       1.10   thorpej 	/*
    246       1.10   thorpej 	 * Our interrupt level must be as high as the highest
    247       1.10   thorpej 	 * device using DMA (i.e. splbio).
    248       1.10   thorpej 	 */
    249       1.24   thorpej 	sc->sc_ipl = PSLTOIPL(hp300_ipls[HP300_IPL_BIO]);
    250       1.17   thorpej 	sc->sc_ih = intr_establish(dmaintr, sc, sc->sc_ipl, IPL_BIO);
    251        1.1       cgd }
    252        1.1       cgd 
    253        1.1       cgd int
    254        1.1       cgd dmareq(dq)
    255       1.11   thorpej 	struct dmaqueue *dq;
    256        1.1       cgd {
    257       1.19   thorpej 	struct dma_softc *sc = &dma_softc;
    258       1.11   thorpej 	int i, chan, s;
    259       1.11   thorpej 
    260       1.11   thorpej #if 1
    261       1.11   thorpej 	s = splhigh();	/* XXXthorpej */
    262       1.11   thorpej #else
    263       1.11   thorpej 	s = splbio();
    264       1.11   thorpej #endif
    265       1.11   thorpej 
    266       1.11   thorpej 	chan = dq->dq_chan;
    267       1.11   thorpej 	for (i = NDMACHAN - 1; i >= 0; i--) {
    268       1.11   thorpej 		/*
    269       1.11   thorpej 		 * Can we use this channel?
    270       1.11   thorpej 		 */
    271        1.1       cgd 		if ((chan & (1 << i)) == 0)
    272        1.1       cgd 			continue;
    273       1.11   thorpej 
    274       1.11   thorpej 		/*
    275       1.11   thorpej 		 * We can use it; is it busy?
    276       1.11   thorpej 		 */
    277       1.11   thorpej 		if (sc->sc_chan[i].dm_job != NULL)
    278        1.1       cgd 			continue;
    279       1.11   thorpej 
    280       1.11   thorpej 		/*
    281       1.11   thorpej 		 * Not busy; give the caller this channel.
    282       1.11   thorpej 		 */
    283       1.11   thorpej 		sc->sc_chan[i].dm_job = dq;
    284       1.11   thorpej 		dq->dq_chan = i;
    285        1.1       cgd 		splx(s);
    286       1.11   thorpej 		return (1);
    287        1.1       cgd 	}
    288       1.11   thorpej 
    289       1.11   thorpej 	/*
    290       1.11   thorpej 	 * Couldn't get a channel now; put this in the queue.
    291       1.11   thorpej 	 */
    292       1.11   thorpej 	TAILQ_INSERT_TAIL(&sc->sc_queue, dq, dq_list);
    293        1.1       cgd 	splx(s);
    294       1.11   thorpej 	return (0);
    295        1.1       cgd }
    296        1.1       cgd 
    297        1.1       cgd void
    298        1.1       cgd dmafree(dq)
    299       1.11   thorpej 	struct dmaqueue *dq;
    300        1.1       cgd {
    301       1.11   thorpej 	int unit = dq->dq_chan;
    302       1.19   thorpej 	struct dma_softc *sc = &dma_softc;
    303       1.11   thorpej 	struct dma_channel *dc = &sc->sc_chan[unit];
    304       1.11   thorpej 	struct dmaqueue *dn;
    305       1.11   thorpej 	int chan, s;
    306       1.11   thorpej 
    307       1.11   thorpej #if 1
    308       1.11   thorpej 	s = splhigh();	/* XXXthorpej */
    309       1.11   thorpej #else
    310       1.11   thorpej 	s = splbio();
    311       1.11   thorpej #endif
    312        1.1       cgd 
    313        1.1       cgd #ifdef DEBUG
    314        1.1       cgd 	dmatimo[unit] = 0;
    315        1.1       cgd #endif
    316       1.11   thorpej 
    317        1.1       cgd 	DMA_CLEAR(dc);
    318       1.18   thorpej 
    319       1.18   thorpej #if defined(CACHE_HAVE_PAC) || defined(M68040)
    320        1.1       cgd 	/*
    321        1.1       cgd 	 * XXX we may not always go thru the flush code in dmastop()
    322        1.1       cgd 	 */
    323        1.6   thorpej 	if (dc->dm_flags & DMAF_PCFLUSH) {
    324        1.1       cgd 		PCIA();
    325        1.6   thorpej 		dc->dm_flags &= ~DMAF_PCFLUSH;
    326        1.1       cgd 	}
    327        1.1       cgd #endif
    328       1.18   thorpej 
    329       1.18   thorpej #if defined(CACHE_HAVE_VAC)
    330        1.6   thorpej 	if (dc->dm_flags & DMAF_VCFLUSH) {
    331        1.1       cgd 		/*
    332        1.1       cgd 		 * 320/350s have VACs that may also need flushing.
    333        1.1       cgd 		 * In our case we only flush the supervisor side
    334        1.1       cgd 		 * because we know that if we are DMAing to user
    335        1.1       cgd 		 * space, the physical pages will also be mapped
    336        1.1       cgd 		 * in kernel space (via vmapbuf) and hence cache-
    337        1.1       cgd 		 * inhibited by the pmap module due to the multiple
    338        1.1       cgd 		 * mapping.
    339        1.1       cgd 		 */
    340        1.1       cgd 		DCIS();
    341        1.6   thorpej 		dc->dm_flags &= ~DMAF_VCFLUSH;
    342        1.1       cgd 	}
    343        1.1       cgd #endif
    344       1.18   thorpej 
    345       1.11   thorpej 	/*
    346       1.11   thorpej 	 * Channel is now free.  Look for another job to run on this
    347       1.11   thorpej 	 * channel.
    348       1.11   thorpej 	 */
    349       1.11   thorpej 	dc->dm_job = NULL;
    350        1.1       cgd 	chan = 1 << unit;
    351       1.11   thorpej 	for (dn = sc->sc_queue.tqh_first; dn != NULL;
    352       1.11   thorpej 	    dn = dn->dq_list.tqe_next) {
    353       1.11   thorpej 		if (dn->dq_chan & chan) {
    354       1.11   thorpej 			/* Found one... */
    355       1.11   thorpej 			TAILQ_REMOVE(&sc->sc_queue, dn, dq_list);
    356       1.11   thorpej 			dc->dm_job = dn;
    357       1.11   thorpej 			dn->dq_chan = dq->dq_chan;
    358        1.1       cgd 			splx(s);
    359       1.11   thorpej 
    360       1.11   thorpej 			/* Start the initiator. */
    361       1.11   thorpej 			(*dn->dq_start)(dn->dq_softc);
    362        1.1       cgd 			return;
    363        1.1       cgd 		}
    364        1.1       cgd 	}
    365        1.1       cgd 	splx(s);
    366        1.1       cgd }
    367        1.1       cgd 
    368        1.1       cgd void
    369        1.1       cgd dmago(unit, addr, count, flags)
    370        1.1       cgd 	int unit;
    371       1.13    scottr 	char *addr;
    372       1.13    scottr 	int count;
    373       1.13    scottr 	int flags;
    374        1.1       cgd {
    375       1.19   thorpej 	struct dma_softc *sc = &dma_softc;
    376       1.13    scottr 	struct dma_channel *dc = &sc->sc_chan[unit];
    377       1.13    scottr 	char *dmaend = NULL;
    378       1.13    scottr 	int seg, tcount;
    379        1.1       cgd 
    380        1.1       cgd 	if (count > MAXPHYS)
    381        1.1       cgd 		panic("dmago: count > MAXPHYS");
    382       1.18   thorpej 
    383        1.1       cgd #if defined(HP320)
    384        1.6   thorpej 	if (sc->sc_type == DMA_B && (flags & DMAGO_LWORD))
    385        1.1       cgd 		panic("dmago: no can do 32-bit DMA");
    386        1.1       cgd #endif
    387       1.18   thorpej 
    388        1.1       cgd #ifdef DEBUG
    389        1.1       cgd 	if (dmadebug & DDB_FOLLOW)
    390       1.15    scottr 		printf("dmago(%d, %p, %x, %x)\n",
    391        1.1       cgd 		       unit, addr, count, flags);
    392        1.1       cgd 	if (flags & DMAGO_LWORD)
    393        1.1       cgd 		dmalword[unit]++;
    394        1.1       cgd 	else if (flags & DMAGO_WORD)
    395        1.1       cgd 		dmaword[unit]++;
    396        1.1       cgd 	else
    397        1.1       cgd 		dmabyte[unit]++;
    398        1.1       cgd #endif
    399        1.1       cgd 	/*
    400        1.1       cgd 	 * Build the DMA chain
    401        1.1       cgd 	 */
    402       1.11   thorpej 	for (seg = 0; count > 0; seg++) {
    403       1.11   thorpej 		dc->dm_chain[seg].dc_addr = (char *) kvtop(addr);
    404       1.18   thorpej #if defined(M68040)
    405        1.4   mycroft 		/*
    406        1.4   mycroft 		 * Push back dirty cache lines
    407        1.4   mycroft 		 */
    408        1.4   mycroft 		if (mmutype == MMU_68040)
    409       1.23    kleink 			DCFP((paddr_t)dc->dm_chain[seg].dc_addr);
    410        1.4   mycroft #endif
    411        1.1       cgd 		if (count < (tcount = NBPG - ((int)addr & PGOFSET)))
    412        1.1       cgd 			tcount = count;
    413       1.11   thorpej 		dc->dm_chain[seg].dc_count = tcount;
    414        1.1       cgd 		addr += tcount;
    415        1.1       cgd 		count -= tcount;
    416        1.1       cgd 		if (flags & DMAGO_LWORD)
    417        1.1       cgd 			tcount >>= 2;
    418        1.1       cgd 		else if (flags & DMAGO_WORD)
    419        1.1       cgd 			tcount >>= 1;
    420       1.11   thorpej 
    421       1.11   thorpej 		/*
    422       1.11   thorpej 		 * Try to compact the DMA transfer if the pages are adjacent.
    423       1.11   thorpej 		 * Note: this will never happen on the first iteration.
    424       1.11   thorpej 		 */
    425       1.11   thorpej 		if (dc->dm_chain[seg].dc_addr == dmaend
    426        1.1       cgd #if defined(HP320)
    427        1.1       cgd 		    /* only 16-bit count on 98620B */
    428        1.6   thorpej 		    && (sc->sc_type != DMA_B ||
    429       1.11   thorpej 			dc->dm_chain[seg - 1].dc_count + tcount <= 65536)
    430        1.1       cgd #endif
    431        1.1       cgd 		) {
    432        1.1       cgd #ifdef DEBUG
    433        1.1       cgd 			dmahits[unit]++;
    434        1.1       cgd #endif
    435       1.11   thorpej 			dmaend += dc->dm_chain[seg].dc_count;
    436       1.11   thorpej 			dc->dm_chain[--seg].dc_count += tcount;
    437        1.1       cgd 		} else {
    438        1.1       cgd #ifdef DEBUG
    439        1.1       cgd 			dmamisses[unit]++;
    440        1.1       cgd #endif
    441       1.11   thorpej 			dmaend = dc->dm_chain[seg].dc_addr +
    442       1.11   thorpej 			    dc->dm_chain[seg].dc_count;
    443       1.11   thorpej 			dc->dm_chain[seg].dc_count = tcount;
    444        1.1       cgd 		}
    445        1.1       cgd 	}
    446       1.11   thorpej 	dc->dm_cur = 0;
    447       1.11   thorpej 	dc->dm_last = --seg;
    448        1.6   thorpej 	dc->dm_flags = 0;
    449        1.1       cgd 	/*
    450        1.1       cgd 	 * Set up the command word based on flags
    451        1.1       cgd 	 */
    452       1.10   thorpej 	dc->dm_cmd = DMA_ENAB | DMA_IPL(sc->sc_ipl) | DMA_START;
    453        1.1       cgd 	if ((flags & DMAGO_READ) == 0)
    454        1.6   thorpej 		dc->dm_cmd |= DMA_WRT;
    455        1.1       cgd 	if (flags & DMAGO_LWORD)
    456        1.6   thorpej 		dc->dm_cmd |= DMA_LWORD;
    457        1.1       cgd 	else if (flags & DMAGO_WORD)
    458        1.6   thorpej 		dc->dm_cmd |= DMA_WORD;
    459        1.1       cgd 	if (flags & DMAGO_PRI)
    460        1.6   thorpej 		dc->dm_cmd |= DMA_PRI;
    461       1.18   thorpej 
    462       1.18   thorpej #if defined(M68040)
    463        1.4   mycroft 	/*
    464        1.4   mycroft 	 * On the 68040 we need to flush (push) the data cache before a
    465        1.4   mycroft 	 * DMA (already done above) and flush again after DMA completes.
    466        1.4   mycroft 	 * In theory we should only need to flush prior to a write DMA
    467        1.4   mycroft 	 * and purge after a read DMA but if the entire page is not
    468        1.4   mycroft 	 * involved in the DMA we might purge some valid data.
    469        1.4   mycroft 	 */
    470        1.4   mycroft 	if (mmutype == MMU_68040 && (flags & DMAGO_READ))
    471        1.6   thorpej 		dc->dm_flags |= DMAF_PCFLUSH;
    472        1.4   mycroft #endif
    473       1.18   thorpej 
    474       1.18   thorpej #if defined(CACHE_HAVE_PAC)
    475        1.1       cgd 	/*
    476        1.1       cgd 	 * Remember if we need to flush external physical cache when
    477        1.1       cgd 	 * DMA is done.  We only do this if we are reading (writing memory).
    478        1.1       cgd 	 */
    479        1.1       cgd 	if (ectype == EC_PHYS && (flags & DMAGO_READ))
    480        1.6   thorpej 		dc->dm_flags |= DMAF_PCFLUSH;
    481        1.1       cgd #endif
    482       1.18   thorpej 
    483       1.18   thorpej #if defined(CACHE_HAVE_VAC)
    484        1.1       cgd 	if (ectype == EC_VIRT && (flags & DMAGO_READ))
    485        1.6   thorpej 		dc->dm_flags |= DMAF_VCFLUSH;
    486        1.1       cgd #endif
    487       1.18   thorpej 
    488        1.1       cgd 	/*
    489        1.1       cgd 	 * Remember if we can skip the dma completion interrupt on
    490        1.1       cgd 	 * the last segment in the chain.
    491        1.1       cgd 	 */
    492        1.1       cgd 	if (flags & DMAGO_NOINT) {
    493        1.6   thorpej 		if (dc->dm_cur == dc->dm_last)
    494        1.6   thorpej 			dc->dm_cmd &= ~DMA_ENAB;
    495        1.1       cgd 		else
    496        1.6   thorpej 			dc->dm_flags |= DMAF_NOINTR;
    497        1.1       cgd 	}
    498        1.1       cgd #ifdef DEBUG
    499       1.11   thorpej 	if (dmadebug & DDB_IO) {
    500       1.15    scottr 		if (((dmadebug&DDB_WORD) && (dc->dm_cmd&DMA_WORD)) ||
    501       1.15    scottr 		    ((dmadebug&DDB_LWORD) && (dc->dm_cmd&DMA_LWORD))) {
    502        1.9  christos 			printf("dmago: cmd %x, flags %x\n",
    503        1.6   thorpej 			       dc->dm_cmd, dc->dm_flags);
    504       1.11   thorpej 			for (seg = 0; seg <= dc->dm_last; seg++)
    505       1.15    scottr 				printf("  %d: %d@%p\n", seg,
    506       1.11   thorpej 				    dc->dm_chain[seg].dc_count,
    507       1.11   thorpej 				    dc->dm_chain[seg].dc_addr);
    508        1.1       cgd 		}
    509       1.11   thorpej 	}
    510        1.1       cgd 	dmatimo[unit] = 1;
    511        1.1       cgd #endif
    512       1.19   thorpej 	DMA_ARM(sc, dc);
    513        1.1       cgd }
    514        1.1       cgd 
    515        1.1       cgd void
    516        1.1       cgd dmastop(unit)
    517       1.13    scottr 	int unit;
    518        1.1       cgd {
    519       1.19   thorpej 	struct dma_softc *sc = &dma_softc;
    520       1.13    scottr 	struct dma_channel *dc = &sc->sc_chan[unit];
    521        1.1       cgd 
    522        1.1       cgd #ifdef DEBUG
    523        1.1       cgd 	if (dmadebug & DDB_FOLLOW)
    524        1.9  christos 		printf("dmastop(%d)\n", unit);
    525        1.1       cgd 	dmatimo[unit] = 0;
    526        1.1       cgd #endif
    527        1.1       cgd 	DMA_CLEAR(dc);
    528       1.18   thorpej 
    529       1.18   thorpej #if defined(CACHE_HAVE_PAC) || defined(M68040)
    530        1.6   thorpej 	if (dc->dm_flags & DMAF_PCFLUSH) {
    531        1.1       cgd 		PCIA();
    532        1.6   thorpej 		dc->dm_flags &= ~DMAF_PCFLUSH;
    533        1.1       cgd 	}
    534        1.1       cgd #endif
    535       1.18   thorpej 
    536       1.18   thorpej #if defined(CACHE_HAVE_VAC)
    537        1.6   thorpej 	if (dc->dm_flags & DMAF_VCFLUSH) {
    538        1.1       cgd 		/*
    539        1.1       cgd 		 * 320/350s have VACs that may also need flushing.
    540        1.1       cgd 		 * In our case we only flush the supervisor side
    541        1.1       cgd 		 * because we know that if we are DMAing to user
    542        1.1       cgd 		 * space, the physical pages will also be mapped
    543        1.1       cgd 		 * in kernel space (via vmapbuf) and hence cache-
    544        1.1       cgd 		 * inhibited by the pmap module due to the multiple
    545        1.1       cgd 		 * mapping.
    546        1.1       cgd 		 */
    547        1.1       cgd 		DCIS();
    548        1.6   thorpej 		dc->dm_flags &= ~DMAF_VCFLUSH;
    549        1.1       cgd 	}
    550        1.1       cgd #endif
    551       1.18   thorpej 
    552        1.1       cgd 	/*
    553        1.1       cgd 	 * We may get this interrupt after a device service routine
    554        1.1       cgd 	 * has freed the dma channel.  So, ignore the intr if there's
    555        1.1       cgd 	 * nothing on the queue.
    556        1.1       cgd 	 */
    557       1.11   thorpej 	if (dc->dm_job != NULL)
    558       1.11   thorpej 		(*dc->dm_job->dq_done)(dc->dm_job->dq_softc);
    559        1.1       cgd }
    560        1.1       cgd 
    561        1.1       cgd int
    562        1.7   thorpej dmaintr(arg)
    563        1.7   thorpej 	void *arg;
    564        1.1       cgd {
    565        1.7   thorpej 	struct dma_softc *sc = arg;
    566       1.13    scottr 	struct dma_channel *dc;
    567       1.13    scottr 	int i, stat;
    568        1.1       cgd 	int found = 0;
    569        1.1       cgd 
    570        1.1       cgd #ifdef DEBUG
    571        1.1       cgd 	if (dmadebug & DDB_FOLLOW)
    572        1.9  christos 		printf("dmaintr\n");
    573        1.1       cgd #endif
    574        1.6   thorpej 	for (i = 0; i < NDMACHAN; i++) {
    575        1.6   thorpej 		dc = &sc->sc_chan[i];
    576        1.1       cgd 		stat = DMA_STAT(dc);
    577        1.1       cgd 		if ((stat & DMA_INTR) == 0)
    578        1.1       cgd 			continue;
    579        1.1       cgd 		found++;
    580        1.1       cgd #ifdef DEBUG
    581        1.1       cgd 		if (dmadebug & DDB_IO) {
    582       1.15    scottr 			if (((dmadebug&DDB_WORD) && (dc->dm_cmd&DMA_WORD)) ||
    583       1.15    scottr 			    ((dmadebug&DDB_LWORD) && (dc->dm_cmd&DMA_LWORD)))
    584       1.11   thorpej 			  printf("dmaintr: flags %x unit %d stat %x next %d\n",
    585       1.11   thorpej 			   dc->dm_flags, i, stat, dc->dm_cur + 1);
    586        1.1       cgd 		}
    587        1.1       cgd 		if (stat & DMA_ARMED)
    588       1.19   thorpej 			printf("dma channel %d: intr when armed\n", i);
    589        1.1       cgd #endif
    590       1.11   thorpej 		/*
    591       1.11   thorpej 		 * Load the next segemnt, or finish up if we're done.
    592       1.11   thorpej 		 */
    593       1.11   thorpej 		dc->dm_cur++;
    594       1.11   thorpej 		if (dc->dm_cur <= dc->dm_last) {
    595        1.1       cgd #ifdef DEBUG
    596        1.1       cgd 			dmatimo[i] = 1;
    597        1.1       cgd #endif
    598        1.1       cgd 			/*
    599       1.11   thorpej 			 * If we're the last segment, disable the
    600       1.11   thorpej 			 * completion interrupt, if necessary.
    601        1.1       cgd 			 */
    602        1.6   thorpej 			if (dc->dm_cur == dc->dm_last &&
    603        1.6   thorpej 			    (dc->dm_flags & DMAF_NOINTR))
    604        1.6   thorpej 				dc->dm_cmd &= ~DMA_ENAB;
    605        1.1       cgd 			DMA_CLEAR(dc);
    606       1.19   thorpej 			DMA_ARM(sc, dc);
    607        1.1       cgd 		} else
    608        1.1       cgd 			dmastop(i);
    609        1.1       cgd 	}
    610        1.1       cgd 	return(found);
    611        1.1       cgd }
    612        1.1       cgd 
    613        1.1       cgd #ifdef DEBUG
    614        1.1       cgd void
    615        1.3   mycroft dmatimeout(arg)
    616        1.3   mycroft 	void *arg;
    617        1.1       cgd {
    618       1.13    scottr 	int i, s;
    619        1.6   thorpej 	struct dma_softc *sc = arg;
    620        1.1       cgd 
    621        1.6   thorpej 	for (i = 0; i < NDMACHAN; i++) {
    622        1.1       cgd 		s = splbio();
    623        1.1       cgd 		if (dmatimo[i]) {
    624        1.1       cgd 			if (dmatimo[i] > 1)
    625       1.19   thorpej 				printf("dma channel %d timeout #%d\n",
    626       1.19   thorpej 				    i, dmatimo[i]-1);
    627        1.1       cgd 			dmatimo[i]++;
    628        1.1       cgd 		}
    629        1.1       cgd 		splx(s);
    630        1.1       cgd 	}
    631       1.25   thorpej 	callout_reset(&sc->sc_debug_ch, 30 * hz, dmatimeout, sc);
    632        1.1       cgd }
    633        1.1       cgd #endif
    634