Home | History | Annotate | Line # | Download | only in g2
gapspci_dma.c revision 1.11
      1  1.11     yamt /*	$NetBSD: gapspci_dma.c,v 1.11 2005/04/01 11:59:26 yamt Exp $	*/
      2   1.1  thorpej 
      3   1.1  thorpej /*-
      4   1.1  thorpej  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1  thorpej  * All rights reserved.
      6   1.1  thorpej  *
      7   1.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  thorpej  * by Jason R. Thorpe.
      9   1.1  thorpej  *
     10   1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1  thorpej  * modification, are permitted provided that the following conditions
     12   1.1  thorpej  * are met:
     13   1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19   1.1  thorpej  *    must display the following acknowledgement:
     20   1.1  thorpej  *	This product includes software developed by the NetBSD
     21   1.1  thorpej  *	Foundation, Inc. and its contributors.
     22   1.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1  thorpej  *    contributors may be used to endorse or promote products derived
     24   1.1  thorpej  *    from this software without specific prior written permission.
     25   1.1  thorpej  *
     26   1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1  thorpej  */
     38   1.1  thorpej 
     39   1.1  thorpej /*
     40   1.1  thorpej  * Bus DMA implementation for the SEGA GAPS PCI bridge.
     41   1.1  thorpej  *
     42   1.1  thorpej  * NOTE: We only implement a small subset of what the bus_space(9)
     43   1.1  thorpej  * API specifies.  Right now, the GAPS PCI bridge is only used for
     44   1.1  thorpej  * the Dreamcast Broadband Adatper, so we only provide what the
     45   1.1  thorpej  * pci(4) and rtk(4) drivers need.
     46   1.1  thorpej  */
     47   1.1  thorpej 
     48   1.1  thorpej #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     49  1.11     yamt __KERNEL_RCSID(0, "$NetBSD: gapspci_dma.c,v 1.11 2005/04/01 11:59:26 yamt Exp $");
     50   1.1  thorpej 
     51   1.1  thorpej #include <sys/param.h>
     52   1.1  thorpej #include <sys/systm.h>
     53   1.1  thorpej #include <sys/device.h>
     54   1.1  thorpej #include <sys/mbuf.h>
     55   1.1  thorpej #include <sys/extent.h>
     56   1.1  thorpej #include <sys/malloc.h>
     57   1.1  thorpej 
     58   1.1  thorpej #include <machine/cpu.h>
     59   1.1  thorpej #include <machine/bus.h>
     60   1.1  thorpej 
     61   1.1  thorpej #include <dev/pci/pcivar.h>
     62   1.1  thorpej 
     63   1.1  thorpej #include <dreamcast/dev/g2/gapspcivar.h>
     64   1.1  thorpej 
     65   1.1  thorpej #include <uvm/uvm_extern.h>
     66   1.1  thorpej 
     67   1.1  thorpej int	gaps_dmamap_create(bus_dma_tag_t, bus_size_t, int, bus_size_t,
     68   1.1  thorpej 	    bus_size_t, int, bus_dmamap_t *);
     69   1.1  thorpej void	gaps_dmamap_destroy(bus_dma_tag_t, bus_dmamap_t);
     70   1.1  thorpej int	gaps_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
     71   1.1  thorpej 	    struct proc *, int);
     72   1.1  thorpej int	gaps_dmamap_load_mbuf(bus_dma_tag_t, bus_dmamap_t, struct mbuf *, int);
     73   1.1  thorpej int	gaps_dmamap_load_uio(bus_dma_tag_t, bus_dmamap_t, struct uio *, int);
     74   1.1  thorpej int	gaps_dmamap_load_raw(bus_dma_tag_t, bus_dmamap_t, bus_dma_segment_t *,
     75   1.1  thorpej 	    int, bus_size_t, int);
     76   1.1  thorpej void	gaps_dmamap_unload(bus_dma_tag_t, bus_dmamap_t);
     77   1.1  thorpej void	gaps_dmamap_sync(bus_dma_tag_t, bus_dmamap_t, bus_addr_t,
     78   1.1  thorpej 	    bus_size_t, int);
     79   1.1  thorpej 
     80   1.1  thorpej int	gaps_dmamem_alloc(bus_dma_tag_t tag, bus_size_t size,
     81   1.1  thorpej 	    bus_size_t alignment, bus_size_t boundary, bus_dma_segment_t *segs,
     82   1.1  thorpej 	    int nsegs, int *rsegs, int flags);
     83   1.1  thorpej void	gaps_dmamem_free(bus_dma_tag_t tag, bus_dma_segment_t *segs, int nsegs);
     84   1.1  thorpej int	gaps_dmamem_map(bus_dma_tag_t tag, bus_dma_segment_t *segs, int nsegs,
     85   1.1  thorpej 	    size_t size, caddr_t *kvap, int flags);
     86   1.1  thorpej void	gaps_dmamem_unmap(bus_dma_tag_t tag, caddr_t kva, size_t size);
     87   1.1  thorpej paddr_t	gaps_dmamem_mmap(bus_dma_tag_t tag, bus_dma_segment_t *segs, int nsegs,
     88   1.1  thorpej 	    off_t off, int prot, int flags);
     89   1.1  thorpej 
     90   1.1  thorpej void
     91   1.1  thorpej gaps_dma_init(struct gaps_softc *sc)
     92   1.1  thorpej {
     93   1.1  thorpej 	bus_dma_tag_t t = &sc->sc_dmat;
     94   1.1  thorpej 
     95   1.1  thorpej 	memset(t, 0, sizeof(*t));
     96   1.1  thorpej 
     97   1.1  thorpej 	t->_cookie = sc;
     98   1.1  thorpej 	t->_dmamap_create = gaps_dmamap_create;
     99   1.1  thorpej 	t->_dmamap_destroy = gaps_dmamap_destroy;
    100   1.1  thorpej 	t->_dmamap_load = gaps_dmamap_load;
    101   1.1  thorpej 	t->_dmamap_load_mbuf = gaps_dmamap_load_mbuf;
    102   1.1  thorpej 	t->_dmamap_load_uio = gaps_dmamap_load_uio;
    103   1.1  thorpej 	t->_dmamap_load_raw = gaps_dmamap_load_raw;
    104   1.1  thorpej 	t->_dmamap_unload = gaps_dmamap_unload;
    105   1.1  thorpej 	t->_dmamap_sync = gaps_dmamap_sync;
    106   1.1  thorpej 
    107   1.1  thorpej 	t->_dmamem_alloc = gaps_dmamem_alloc;
    108   1.1  thorpej 	t->_dmamem_free = gaps_dmamem_free;
    109   1.1  thorpej 	t->_dmamem_map = gaps_dmamem_map;
    110   1.1  thorpej 	t->_dmamem_unmap = gaps_dmamem_unmap;
    111   1.1  thorpej 	t->_dmamem_mmap = gaps_dmamem_mmap;
    112   1.1  thorpej 
    113   1.1  thorpej 	/*
    114   1.1  thorpej 	 * The GAPS PCI bridge has 32k of DMA memory.  We manage it
    115   1.1  thorpej 	 * with an extent map.
    116   1.1  thorpej 	 */
    117   1.1  thorpej 	sc->sc_dma_ex = extent_create("gaps dma",
    118   1.2   marcus 	    sc->sc_dmabase, sc->sc_dmabase + (sc->sc_dmasize - 1),
    119   1.1  thorpej 	    M_DEVBUF, NULL, 0, EX_WAITOK | EXF_NOCOALESCE);
    120   1.1  thorpej 
    121   1.2   marcus 	if (bus_space_map(sc->sc_memt, sc->sc_dmabase, sc->sc_dmasize,
    122   1.1  thorpej 	    0, &sc->sc_dma_memh) != 0)
    123   1.1  thorpej 		panic("gaps_dma_init: can't map SRAM buffer");
    124   1.1  thorpej }
    125   1.1  thorpej 
    126   1.1  thorpej /*
    127   1.1  thorpej  * A GAPS DMA map -- has the standard DMA map, plus some extra
    128   1.1  thorpej  * housekeeping data.
    129   1.1  thorpej  */
    130   1.1  thorpej struct gaps_dmamap {
    131   1.1  thorpej 	struct dreamcast_bus_dmamap gd_dmamap;
    132   1.1  thorpej 	void *gd_origbuf;
    133   1.1  thorpej 	int gd_buftype;
    134   1.1  thorpej };
    135   1.1  thorpej 
    136   1.1  thorpej #define	GAPS_DMA_BUFTYPE_INVALID	0
    137   1.1  thorpej #define	GAPS_DMA_BUFTYPE_LINEAR		1
    138   1.1  thorpej #define	GAPS_DMA_BUFTYPE_MBUF		2
    139   1.1  thorpej 
    140   1.1  thorpej int
    141   1.1  thorpej gaps_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments,
    142   1.1  thorpej     bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamap)
    143   1.1  thorpej {
    144   1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    145   1.1  thorpej 	struct gaps_dmamap *gmap;
    146   1.1  thorpej 	bus_dmamap_t map;
    147   1.1  thorpej 
    148   1.1  thorpej 	/*
    149   1.1  thorpej 	 * Allocate an initialize the DMA map.  The end of the map is
    150   1.1  thorpej 	 * a variable-sized array of segments, so we allocate enough
    151   1.1  thorpej 	 * room for them in one shot.  Since the DMA map always includes
    152   1.1  thorpej 	 * one segment, and we only support one segment, this is really
    153   1.1  thorpej 	 * easy.
    154   1.1  thorpej 	 *
    155   1.1  thorpej 	 * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
    156   1.1  thorpej 	 * of ALLOCNOW notifies others that we've reserved these resources
    157   1.1  thorpej 	 * and they are not to be freed.
    158   1.1  thorpej 	 */
    159   1.1  thorpej 
    160   1.1  thorpej 	gmap = malloc(sizeof(*gmap), M_DMAMAP,
    161   1.1  thorpej 	    (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK);
    162   1.1  thorpej 	if (gmap == NULL)
    163   1.9  tsutsui 		return ENOMEM;
    164   1.1  thorpej 
    165   1.1  thorpej 	memset(gmap, 0, sizeof(*gmap));
    166   1.1  thorpej 
    167   1.1  thorpej 	gmap->gd_buftype = GAPS_DMA_BUFTYPE_INVALID;
    168   1.1  thorpej 
    169   1.1  thorpej 	map = &gmap->gd_dmamap;
    170   1.1  thorpej 
    171   1.1  thorpej 	map->_dm_size = size;
    172   1.1  thorpej 	map->_dm_segcnt = 1;
    173  1.10     matt 	map->_dm_maxmaxsegsz = maxsegsz;
    174   1.1  thorpej 	map->_dm_boundary = boundary;
    175   1.1  thorpej 	map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
    176  1.10     matt 	map->dm_maxsegsz = maxsegsz;
    177   1.1  thorpej 
    178   1.1  thorpej 	if (flags & BUS_DMA_ALLOCNOW) {
    179   1.1  thorpej 		u_long res;
    180   1.1  thorpej 		int error;
    181   1.1  thorpej 
    182   1.1  thorpej 		error = extent_alloc(sc->sc_dma_ex, size, 1024 /* XXX */,
    183   1.1  thorpej 		    map->_dm_boundary,
    184   1.1  thorpej 		    (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK, &res);
    185   1.1  thorpej 		if (error) {
    186   1.1  thorpej 			free(gmap, M_DEVBUF);
    187   1.9  tsutsui 			return error;
    188   1.1  thorpej 		}
    189   1.1  thorpej 
    190   1.1  thorpej 		map->dm_segs[0].ds_addr = res;
    191   1.1  thorpej 		map->dm_segs[0].ds_len = size;
    192   1.1  thorpej 
    193   1.1  thorpej 		map->dm_mapsize = size;
    194   1.1  thorpej 		map->dm_nsegs = 1;
    195   1.1  thorpej 	} else {
    196   1.1  thorpej 		map->dm_mapsize = 0;		/* no valid mappings */
    197   1.1  thorpej 		map->dm_nsegs = 0;
    198   1.1  thorpej 	}
    199   1.1  thorpej 
    200   1.1  thorpej 	*dmamap = map;
    201   1.1  thorpej 
    202   1.9  tsutsui 	return 0;
    203   1.1  thorpej }
    204   1.1  thorpej 
    205   1.1  thorpej void
    206   1.1  thorpej gaps_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    207   1.1  thorpej {
    208   1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    209   1.1  thorpej 
    210   1.1  thorpej 	if (map->_dm_flags & BUS_DMA_ALLOCNOW) {
    211   1.1  thorpej 		(void) extent_free(sc->sc_dma_ex,
    212   1.1  thorpej 		    map->dm_segs[0].ds_addr,
    213   1.1  thorpej 		    map->dm_mapsize, EX_NOWAIT);
    214   1.1  thorpej 	}
    215   1.1  thorpej 	free(map, M_DMAMAP);
    216   1.1  thorpej }
    217   1.1  thorpej 
    218   1.1  thorpej int
    219   1.1  thorpej gaps_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *addr,
    220   1.1  thorpej     bus_size_t size, struct proc *p, int flags)
    221   1.1  thorpej {
    222   1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    223   1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    224   1.1  thorpej 	u_long res;
    225   1.1  thorpej 	int error;
    226   1.1  thorpej 
    227   1.1  thorpej 	if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) {
    228   1.1  thorpej 		/*
    229   1.1  thorpej 		 * Make sure that on error condition we return
    230   1.1  thorpej 		 * "no valid mappings".
    231   1.1  thorpej 		 */
    232   1.1  thorpej 		map->dm_mapsize = 0;
    233   1.1  thorpej 		map->dm_nsegs = 0;
    234  1.10     matt 		KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    235   1.1  thorpej 	}
    236   1.1  thorpej 
    237   1.1  thorpej 	/* XXX Don't support DMA to process space right now. */
    238   1.1  thorpej 	if (p != NULL)
    239   1.9  tsutsui 		return EINVAL;
    240   1.1  thorpej 
    241   1.1  thorpej 	if (size > map->_dm_size)
    242   1.9  tsutsui 		return EINVAL;
    243   1.1  thorpej 
    244   1.1  thorpej 	error = extent_alloc(sc->sc_dma_ex, size, 1024 /* XXX */,
    245   1.1  thorpej 	    map->_dm_boundary,
    246   1.1  thorpej 	    (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK, &res);
    247   1.1  thorpej 	if (error)
    248   1.9  tsutsui 		return error;
    249   1.1  thorpej 
    250   1.1  thorpej 	map->dm_segs[0].ds_addr = res;
    251   1.1  thorpej 	map->dm_segs[0].ds_len = size;
    252   1.1  thorpej 
    253   1.1  thorpej 	gmap->gd_origbuf = addr;
    254   1.1  thorpej 	gmap->gd_buftype = GAPS_DMA_BUFTYPE_LINEAR;
    255   1.1  thorpej 
    256   1.1  thorpej 	map->dm_mapsize = size;
    257   1.1  thorpej 	map->dm_nsegs = 1;
    258   1.1  thorpej 
    259   1.9  tsutsui 	return 0;
    260   1.1  thorpej }
    261   1.1  thorpej 
    262   1.1  thorpej int
    263   1.1  thorpej gaps_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    264   1.1  thorpej     int flags)
    265   1.1  thorpej {
    266   1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    267   1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    268   1.1  thorpej 	u_long res;
    269   1.1  thorpej 	int error;
    270   1.1  thorpej 
    271   1.1  thorpej 	if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) {
    272   1.1  thorpej 		/*
    273   1.1  thorpej 		 * Make sure that on error condition we return
    274   1.1  thorpej 		 * "no valid mappings".
    275   1.1  thorpej 		 */
    276   1.1  thorpej 		map->dm_mapsize = 0;
    277   1.1  thorpej 		map->dm_nsegs = 0;
    278  1.10     matt 		KASSERT(map->dm_maxsegsz <= map->_dm_maxmaxsegsz);
    279   1.1  thorpej 	}
    280   1.1  thorpej 
    281   1.1  thorpej #ifdef DIAGNOSTIC
    282   1.1  thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    283   1.1  thorpej 		panic("gaps_dmamap_load_mbuf: no packet header");
    284   1.1  thorpej #endif
    285   1.1  thorpej 
    286   1.1  thorpej 	if (m0->m_pkthdr.len > map->_dm_size)
    287   1.9  tsutsui 		return EINVAL;
    288   1.1  thorpej 
    289   1.1  thorpej 	error = extent_alloc(sc->sc_dma_ex, m0->m_pkthdr.len, 1024 /* XXX */,
    290   1.1  thorpej 	    map->_dm_boundary,
    291   1.1  thorpej 	    (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK, &res);
    292   1.1  thorpej 	if (error)
    293   1.9  tsutsui 		return error;
    294   1.1  thorpej 
    295   1.1  thorpej 	map->dm_segs[0].ds_addr = res;
    296   1.1  thorpej 	map->dm_segs[0].ds_len = m0->m_pkthdr.len;
    297   1.1  thorpej 
    298   1.1  thorpej 	gmap->gd_origbuf = m0;
    299   1.1  thorpej 	gmap->gd_buftype = GAPS_DMA_BUFTYPE_MBUF;
    300   1.1  thorpej 
    301   1.1  thorpej 	map->dm_mapsize = m0->m_pkthdr.len;
    302   1.1  thorpej 	map->dm_nsegs = 1;
    303   1.1  thorpej 
    304   1.9  tsutsui 	return 0;
    305   1.1  thorpej }
    306   1.1  thorpej 
    307   1.1  thorpej int
    308   1.1  thorpej gaps_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    309   1.1  thorpej     int flags)
    310   1.1  thorpej {
    311   1.1  thorpej 
    312   1.1  thorpej 	printf("gaps_dmamap_load_uio: not implemented\n");
    313   1.9  tsutsui 	return EINVAL;
    314   1.1  thorpej }
    315   1.1  thorpej 
    316   1.1  thorpej int
    317   1.1  thorpej gaps_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    318   1.1  thorpej     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    319   1.1  thorpej {
    320   1.1  thorpej 
    321   1.1  thorpej 	printf("gaps_dmamap_load_raw: not implemented\n");
    322   1.9  tsutsui 	return EINVAL;
    323   1.1  thorpej }
    324   1.1  thorpej 
    325   1.1  thorpej void
    326   1.1  thorpej gaps_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    327   1.1  thorpej {
    328   1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    329   1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    330   1.1  thorpej 
    331   1.1  thorpej 	if (gmap->gd_buftype == GAPS_DMA_BUFTYPE_INVALID) {
    332   1.1  thorpej 		printf("gaps_dmamap_unload: DMA map not loaded!\n");
    333   1.1  thorpej 		return;
    334   1.1  thorpej 	}
    335   1.1  thorpej 
    336   1.1  thorpej 	if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) {
    337   1.1  thorpej 		(void) extent_free(sc->sc_dma_ex,
    338   1.1  thorpej 		    map->dm_segs[0].ds_addr,
    339   1.1  thorpej 		    map->dm_mapsize, EX_NOWAIT);
    340   1.1  thorpej 
    341  1.10     matt 		map->dm_maxsegsz = map->_dm_maxmaxsegsz;
    342   1.1  thorpej 		map->dm_mapsize = 0;
    343   1.1  thorpej 		map->dm_nsegs = 0;
    344   1.1  thorpej 	}
    345   1.1  thorpej 
    346   1.1  thorpej 	gmap->gd_buftype = GAPS_DMA_BUFTYPE_INVALID;
    347   1.1  thorpej }
    348   1.1  thorpej 
    349   1.1  thorpej void
    350   1.1  thorpej gaps_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    351   1.1  thorpej     bus_size_t len, int ops)
    352   1.1  thorpej {
    353   1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    354   1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    355   1.1  thorpej 	bus_addr_t dmaoff = map->dm_segs[0].ds_addr - sc->sc_dmabase;
    356   1.1  thorpej 
    357   1.1  thorpej 	/*
    358   1.1  thorpej 	 * Mixing PRE and POST operations is not allowed.
    359   1.1  thorpej 	 */
    360   1.1  thorpej 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    361   1.1  thorpej 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    362   1.1  thorpej 		panic("gaps_dmamap_sync: mix PRE and POST");
    363   1.1  thorpej 
    364   1.1  thorpej #ifdef DIAGNOSTIC
    365   1.1  thorpej 	if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
    366   1.3  thorpej 		if (offset >= map->dm_mapsize) {
    367   1.3  thorpej 			printf("offset 0x%lx mapsize 0x%lx\n",
    368   1.3  thorpej 			    offset, map->dm_mapsize);
    369   1.1  thorpej 			panic("gaps_dmamap_sync: bad offset");
    370   1.3  thorpej 		}
    371   1.3  thorpej 		if (len == 0 || (offset + len) > map->dm_mapsize) {
    372   1.3  thorpej 			printf("len 0x%lx offset 0x%lx mapsize 0x%lx\n",
    373   1.3  thorpej 			    len, offset, map->dm_mapsize);
    374   1.1  thorpej 			panic("gaps_dmamap_sync: bad length");
    375   1.3  thorpej 		}
    376   1.1  thorpej 	}
    377   1.1  thorpej #endif
    378   1.1  thorpej 
    379   1.1  thorpej 	switch (gmap->gd_buftype) {
    380   1.1  thorpej 	case GAPS_DMA_BUFTYPE_INVALID:
    381   1.1  thorpej 		printf("gaps_dmamap_sync: DMA map is not loaded!\n");
    382   1.1  thorpej 		return;
    383   1.1  thorpej 
    384   1.1  thorpej 	case GAPS_DMA_BUFTYPE_LINEAR:
    385   1.1  thorpej 		/*
    386   1.1  thorpej 		 * Nothing to do for pre-read.
    387   1.1  thorpej 		 */
    388   1.1  thorpej 
    389   1.1  thorpej 		if (ops & BUS_DMASYNC_PREWRITE) {
    390   1.1  thorpej 			/*
    391   1.1  thorpej 			 * Copy the caller's buffer to the SRAM buffer.
    392   1.1  thorpej 			 */
    393   1.1  thorpej 			bus_space_write_region_1(sc->sc_memt,
    394   1.1  thorpej 			    sc->sc_dma_memh,
    395   1.1  thorpej 			    dmaoff + offset,
    396   1.9  tsutsui 			    (uint8_t *)gmap->gd_origbuf + offset, len);
    397   1.1  thorpej 		}
    398   1.1  thorpej 
    399   1.1  thorpej 		if (ops & BUS_DMASYNC_POSTREAD) {
    400   1.1  thorpej 			/*
    401   1.1  thorpej 			 * Copy the SRAM buffer to the caller's buffer.
    402   1.1  thorpej 			 */
    403   1.1  thorpej 			bus_space_read_region_1(sc->sc_memt,
    404   1.1  thorpej 			    sc->sc_dma_memh,
    405   1.1  thorpej 			    dmaoff + offset,
    406   1.9  tsutsui 			    (uint8_t *)gmap->gd_origbuf + offset, len);
    407   1.1  thorpej 		}
    408   1.1  thorpej 
    409   1.1  thorpej 		/*
    410   1.1  thorpej 		 * Nothing to do for post-write.
    411   1.1  thorpej 		 */
    412   1.1  thorpej 		break;
    413   1.1  thorpej 
    414   1.1  thorpej 	case GAPS_DMA_BUFTYPE_MBUF:
    415   1.1  thorpej 	    {
    416   1.1  thorpej 		struct mbuf *m, *m0 = gmap->gd_origbuf;
    417   1.1  thorpej 		bus_size_t minlen, moff;
    418   1.1  thorpej 
    419   1.1  thorpej 		/*
    420   1.1  thorpej 		 * Nothing to do for pre-read.
    421   1.1  thorpej 		 */
    422   1.1  thorpej 
    423   1.1  thorpej 		if (ops & BUS_DMASYNC_PREWRITE) {
    424   1.1  thorpej 			/*
    425   1.1  thorpej 			 * Copy the caller's buffer into the SRAM buffer.
    426   1.1  thorpej 			 */
    427   1.1  thorpej 			for (moff = offset, m = m0; m != NULL && len != 0;
    428   1.1  thorpej 			     m = m->m_next) {
    429   1.1  thorpej 				/* Find the beginning mbuf. */
    430   1.1  thorpej 				if (moff >= m->m_len) {
    431   1.1  thorpej 					moff -= m->m_len;
    432   1.1  thorpej 					continue;
    433   1.1  thorpej 				}
    434   1.1  thorpej 
    435   1.1  thorpej 				/*
    436   1.1  thorpej 				 * Now at the first mbuf to sync; nail
    437   1.1  thorpej 				 * each one until we have exhausted the
    438   1.1  thorpej 				 * length.
    439   1.1  thorpej 				 */
    440   1.1  thorpej 				minlen = len < m->m_len - moff ?
    441   1.1  thorpej 				    len : m->m_len - moff;
    442   1.1  thorpej 
    443   1.1  thorpej 				bus_space_write_region_1(sc->sc_memt,
    444   1.1  thorpej 				    sc->sc_dma_memh, dmaoff + offset,
    445   1.9  tsutsui 				    mtod(m, uint8_t *) + moff, minlen);
    446   1.1  thorpej 
    447   1.1  thorpej 				moff = 0;
    448   1.1  thorpej 				len -= minlen;
    449   1.1  thorpej 				offset += minlen;
    450   1.1  thorpej 			}
    451   1.1  thorpej 		}
    452   1.1  thorpej 
    453   1.1  thorpej 		if (ops & BUS_DMASYNC_POSTREAD) {
    454   1.1  thorpej 			/*
    455   1.1  thorpej 			 * Copy the SRAM buffer into the caller's buffer.
    456   1.1  thorpej 			 */
    457   1.1  thorpej 			for (moff = offset, m = m0; m != NULL && len != 0;
    458   1.1  thorpej 			     m = m->m_next) {
    459   1.1  thorpej 				/* Find the beginning mbuf. */
    460   1.1  thorpej 				if (moff >= m->m_len) {
    461   1.1  thorpej 					moff -= m->m_len;
    462   1.1  thorpej 					continue;
    463   1.1  thorpej 				}
    464   1.1  thorpej 
    465   1.1  thorpej 				/*
    466   1.1  thorpej 				 * Now at the first mbuf to sync; nail
    467   1.1  thorpej 				 * each one until we have exhausted the
    468   1.1  thorpej 				 * length.
    469   1.1  thorpej 				 */
    470   1.1  thorpej 				minlen = len < m->m_len - moff ?
    471   1.1  thorpej 				    len : m->m_len - moff;
    472   1.1  thorpej 
    473   1.1  thorpej 				bus_space_read_region_1(sc->sc_memt,
    474   1.1  thorpej 				    sc->sc_dma_memh, dmaoff + offset,
    475   1.9  tsutsui 				    mtod(m, uint8_t *) + moff, minlen);
    476   1.1  thorpej 
    477   1.1  thorpej 				moff = 0;
    478   1.1  thorpej 				len -= minlen;
    479   1.1  thorpej 				offset += minlen;
    480   1.1  thorpej 			}
    481   1.1  thorpej 		}
    482   1.1  thorpej 
    483   1.1  thorpej 		/*
    484   1.1  thorpej 		 * Nothing to do for post-write.
    485   1.1  thorpej 		 */
    486   1.1  thorpej 		break;
    487   1.1  thorpej 	    }
    488   1.1  thorpej 
    489   1.1  thorpej 	default:
    490   1.1  thorpej 		printf("unknown buffer type %d\n", gmap->gd_buftype);
    491   1.1  thorpej 		panic("gaps_dmamap_sync");
    492   1.1  thorpej 	}
    493   1.1  thorpej }
    494   1.1  thorpej 
    495   1.1  thorpej int
    496   1.1  thorpej gaps_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    497   1.1  thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    498   1.1  thorpej     int flags)
    499   1.1  thorpej {
    500   1.1  thorpej 	extern paddr_t avail_start, avail_end;	/* from pmap.c */
    501   1.1  thorpej 
    502   1.1  thorpej 	struct pglist mlist;
    503   1.1  thorpej 	paddr_t curaddr, lastaddr;
    504   1.5      chs 	struct vm_page *m;
    505   1.1  thorpej 	int curseg, error;
    506   1.1  thorpej 
    507   1.1  thorpej 	/* Always round the size. */
    508   1.1  thorpej 	size = round_page(size);
    509   1.1  thorpej 
    510   1.1  thorpej 	/*
    511   1.1  thorpej 	 * Allocate the pages from the VM system.
    512   1.1  thorpej 	 */
    513   1.1  thorpej 	error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
    514   1.1  thorpej 	    alignment, boundary, &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    515   1.1  thorpej 	if (error)
    516   1.9  tsutsui 		return error;
    517   1.1  thorpej 
    518   1.1  thorpej 	/*
    519   1.1  thorpej 	 * Compute the location, size, and number of segments actually
    520   1.1  thorpej 	 * returned by the VM code.
    521   1.1  thorpej 	 */
    522   1.1  thorpej 	m = mlist.tqh_first;
    523   1.1  thorpej 	curseg = 0;
    524   1.1  thorpej 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
    525   1.1  thorpej 	segs[curseg].ds_len = PAGE_SIZE;
    526   1.1  thorpej 	m = TAILQ_NEXT(m, pageq);
    527   1.1  thorpej 
    528   1.1  thorpej 	for (; m != NULL; m = TAILQ_NEXT(m, pageq)) {
    529   1.1  thorpej 		curaddr = VM_PAGE_TO_PHYS(m);
    530   1.1  thorpej 		if (curaddr == (lastaddr + PAGE_SIZE))
    531   1.1  thorpej 			segs[curseg].ds_len += PAGE_SIZE;
    532   1.1  thorpej 		else {
    533   1.1  thorpej 			curseg++;
    534   1.1  thorpej 			segs[curseg].ds_addr = curaddr;
    535   1.1  thorpej 			segs[curseg].ds_len = PAGE_SIZE;
    536   1.1  thorpej 		}
    537   1.1  thorpej 		lastaddr = curaddr;
    538   1.1  thorpej 	}
    539   1.1  thorpej 
    540   1.1  thorpej 	*rsegs = curseg + 1;
    541   1.1  thorpej 
    542   1.9  tsutsui 	return 0;
    543   1.1  thorpej }
    544   1.1  thorpej 
    545   1.1  thorpej void
    546   1.1  thorpej gaps_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    547   1.1  thorpej {
    548   1.1  thorpej 	struct pglist mlist;
    549   1.5      chs 	struct vm_page *m;
    550   1.1  thorpej 	bus_addr_t addr;
    551   1.1  thorpej 	int curseg;
    552   1.1  thorpej 
    553   1.1  thorpej 	/*
    554   1.1  thorpej 	 * Build a list of pages to free back to the VM system.
    555   1.1  thorpej 	 */
    556   1.1  thorpej 	TAILQ_INIT(&mlist);
    557   1.1  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    558   1.1  thorpej 		for (addr = segs[curseg].ds_addr;
    559   1.1  thorpej 		     addr < segs[curseg].ds_addr + segs[curseg].ds_len;
    560   1.1  thorpej 		     addr += PAGE_SIZE) {
    561   1.1  thorpej 			m = PHYS_TO_VM_PAGE(addr);
    562   1.1  thorpej 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    563   1.1  thorpej 		}
    564   1.1  thorpej 	}
    565   1.1  thorpej 
    566   1.1  thorpej 	uvm_pglistfree(&mlist);
    567   1.1  thorpej }
    568   1.1  thorpej 
    569   1.1  thorpej int
    570   1.1  thorpej gaps_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    571   1.1  thorpej     size_t size, caddr_t *kvap, int flags)
    572   1.1  thorpej {
    573   1.1  thorpej 	vaddr_t va;
    574   1.1  thorpej 	bus_addr_t addr;
    575   1.1  thorpej 	int curseg;
    576   1.1  thorpej 
    577   1.1  thorpej 	/*
    578   1.1  thorpej 	 * If we're only mapping 1 segment, use P2SEG, to avoid
    579   1.1  thorpej 	 * TLB thrashing.
    580   1.1  thorpej 	 */
    581   1.1  thorpej 	if (nsegs == 1) {
    582   1.9  tsutsui 		*kvap = (caddr_t)SH3_PHYS_TO_P2SEG(segs[0].ds_addr);
    583   1.9  tsutsui 		return 0;
    584   1.1  thorpej 	}
    585   1.1  thorpej 
    586   1.1  thorpej 	size = round_page(size);
    587   1.1  thorpej 
    588  1.11     yamt 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
    589   1.1  thorpej 
    590   1.1  thorpej 	if (va == 0)
    591   1.9  tsutsui 		return ENOMEM;
    592   1.1  thorpej 
    593   1.9  tsutsui 	*kvap = (caddr_t)va;
    594   1.1  thorpej 
    595   1.1  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    596   1.1  thorpej 		for (addr = segs[curseg].ds_addr;
    597   1.1  thorpej 		     addr < segs[curseg].ds_addr + segs[curseg].ds_len;
    598   1.1  thorpej 		     addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
    599   1.1  thorpej 			if (size == 0)
    600   1.1  thorpej 				panic("gaps_dmamem_map: size botch");
    601   1.1  thorpej 			pmap_kenter_pa(va, addr,
    602   1.1  thorpej 			    VM_PROT_READ | VM_PROT_WRITE);
    603   1.1  thorpej 		}
    604   1.1  thorpej 	}
    605   1.6    chris 	pmap_update(pmap_kernel());
    606   1.1  thorpej 
    607   1.9  tsutsui 	return 0;
    608   1.1  thorpej }
    609   1.1  thorpej 
    610   1.1  thorpej void
    611   1.1  thorpej gaps_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
    612   1.1  thorpej {
    613   1.1  thorpej 
    614   1.1  thorpej #ifdef DIAGNOSTIC
    615   1.1  thorpej 	if ((u_long) kva & PAGE_MASK)
    616   1.1  thorpej 		panic("gaps_dmamem_unmap");
    617   1.1  thorpej #endif
    618   1.1  thorpej 
    619   1.1  thorpej 	/*
    620   1.1  thorpej 	 * Nothing to do if we mapped it with P2SEG.
    621   1.1  thorpej 	 */
    622   1.9  tsutsui 	if (kva >= (caddr_t)SH3_P2SEG_BASE &&
    623   1.9  tsutsui 	    kva <= (caddr_t)SH3_P2SEG_END)
    624   1.1  thorpej 		return;
    625   1.1  thorpej 
    626   1.1  thorpej 	size = round_page(size);
    627   1.1  thorpej 	pmap_kremove((vaddr_t) kva, size);
    628   1.6    chris 	pmap_update(pmap_kernel());
    629  1.11     yamt 	uvm_km_free(kernel_map, (vaddr_t) kva, size, UVM_KMF_VAONLY);
    630   1.1  thorpej }
    631   1.1  thorpej 
    632   1.1  thorpej paddr_t
    633   1.1  thorpej gaps_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    634   1.1  thorpej     off_t off, int prot, int flags)
    635   1.1  thorpej {
    636   1.1  thorpej 
    637   1.1  thorpej 	/* Not implemented. */
    638   1.9  tsutsui 	return -1;
    639   1.1  thorpej }
    640