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