Home | History | Annotate | Line # | Download | only in g2
gapspci_dma.c revision 1.8.10.1
      1  1.8.10.1     yamt /*	$NetBSD: gapspci_dma.c,v 1.8.10.1 2005/01/26 11:57:30 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.8.10.1     yamt __KERNEL_RCSID(0, "$NetBSD: gapspci_dma.c,v 1.8.10.1 2005/01/26 11:57:30 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.1  thorpej 		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.1  thorpej 	map->_dm_maxsegsz = 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.1  thorpej 
    177       1.1  thorpej 	if (flags & BUS_DMA_ALLOCNOW) {
    178       1.1  thorpej 		u_long res;
    179       1.1  thorpej 		int error;
    180       1.1  thorpej 
    181       1.1  thorpej 		error = extent_alloc(sc->sc_dma_ex, size, 1024 /* XXX */,
    182       1.1  thorpej 		    map->_dm_boundary,
    183       1.1  thorpej 		    (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK, &res);
    184       1.1  thorpej 		if (error) {
    185       1.1  thorpej 			free(gmap, M_DEVBUF);
    186       1.1  thorpej 			return (error);
    187       1.1  thorpej 		}
    188       1.1  thorpej 
    189       1.1  thorpej 		map->dm_segs[0].ds_addr = res;
    190       1.1  thorpej 		map->dm_segs[0].ds_len = size;
    191       1.1  thorpej 
    192       1.1  thorpej 		map->dm_mapsize = size;
    193       1.1  thorpej 		map->dm_nsegs = 1;
    194       1.1  thorpej 	} else {
    195       1.1  thorpej 		map->dm_mapsize = 0;		/* no valid mappings */
    196       1.1  thorpej 		map->dm_nsegs = 0;
    197       1.1  thorpej 	}
    198       1.1  thorpej 
    199       1.1  thorpej 	*dmamap = map;
    200       1.1  thorpej 
    201       1.1  thorpej 	return (0);
    202       1.1  thorpej }
    203       1.1  thorpej 
    204       1.1  thorpej void
    205       1.1  thorpej gaps_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map)
    206       1.1  thorpej {
    207       1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    208       1.1  thorpej 
    209       1.1  thorpej 	if (map->_dm_flags & BUS_DMA_ALLOCNOW) {
    210       1.1  thorpej 		(void) extent_free(sc->sc_dma_ex,
    211       1.1  thorpej 		    map->dm_segs[0].ds_addr,
    212       1.1  thorpej 		    map->dm_mapsize, EX_NOWAIT);
    213       1.1  thorpej 	}
    214       1.1  thorpej 	free(map, M_DMAMAP);
    215       1.1  thorpej }
    216       1.1  thorpej 
    217       1.1  thorpej int
    218       1.1  thorpej gaps_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *addr,
    219       1.1  thorpej     bus_size_t size, struct proc *p, int flags)
    220       1.1  thorpej {
    221       1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    222       1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    223       1.1  thorpej 	u_long res;
    224       1.1  thorpej 	int error;
    225       1.1  thorpej 
    226       1.1  thorpej 	if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) {
    227       1.1  thorpej 		/*
    228       1.1  thorpej 		 * Make sure that on error condition we return
    229       1.1  thorpej 		 * "no valid mappings".
    230       1.1  thorpej 		 */
    231       1.1  thorpej 		map->dm_mapsize = 0;
    232       1.1  thorpej 		map->dm_nsegs = 0;
    233       1.1  thorpej 	}
    234       1.1  thorpej 
    235       1.1  thorpej 	/* XXX Don't support DMA to process space right now. */
    236       1.1  thorpej 	if (p != NULL)
    237       1.1  thorpej 		return (EINVAL);
    238       1.1  thorpej 
    239       1.1  thorpej 	if (size > map->_dm_size)
    240       1.1  thorpej 		return (EINVAL);
    241       1.1  thorpej 
    242       1.1  thorpej 	error = extent_alloc(sc->sc_dma_ex, size, 1024 /* XXX */,
    243       1.1  thorpej 	    map->_dm_boundary,
    244       1.1  thorpej 	    (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK, &res);
    245       1.1  thorpej 	if (error)
    246       1.1  thorpej 		return (error);
    247       1.1  thorpej 
    248       1.1  thorpej 	map->dm_segs[0].ds_addr = res;
    249       1.1  thorpej 	map->dm_segs[0].ds_len = size;
    250       1.1  thorpej 
    251       1.1  thorpej 	gmap->gd_origbuf = addr;
    252       1.1  thorpej 	gmap->gd_buftype = GAPS_DMA_BUFTYPE_LINEAR;
    253       1.1  thorpej 
    254       1.1  thorpej 	map->dm_mapsize = size;
    255       1.1  thorpej 	map->dm_nsegs = 1;
    256       1.1  thorpej 
    257       1.1  thorpej 	return (0);
    258       1.1  thorpej }
    259       1.1  thorpej 
    260       1.1  thorpej int
    261       1.1  thorpej gaps_dmamap_load_mbuf(bus_dma_tag_t t, bus_dmamap_t map, struct mbuf *m0,
    262       1.1  thorpej     int flags)
    263       1.1  thorpej {
    264       1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    265       1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    266       1.1  thorpej 	u_long res;
    267       1.1  thorpej 	int error;
    268       1.1  thorpej 
    269       1.1  thorpej 	if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) {
    270       1.1  thorpej 		/*
    271       1.1  thorpej 		 * Make sure that on error condition we return
    272       1.1  thorpej 		 * "no valid mappings".
    273       1.1  thorpej 		 */
    274       1.1  thorpej 		map->dm_mapsize = 0;
    275       1.1  thorpej 		map->dm_nsegs = 0;
    276       1.1  thorpej 	}
    277       1.1  thorpej 
    278       1.1  thorpej #ifdef DIAGNOSTIC
    279       1.1  thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    280       1.1  thorpej 		panic("gaps_dmamap_load_mbuf: no packet header");
    281       1.1  thorpej #endif
    282       1.1  thorpej 
    283       1.1  thorpej 	if (m0->m_pkthdr.len > map->_dm_size)
    284       1.1  thorpej 		return (EINVAL);
    285       1.1  thorpej 
    286       1.1  thorpej 	error = extent_alloc(sc->sc_dma_ex, m0->m_pkthdr.len, 1024 /* XXX */,
    287       1.1  thorpej 	    map->_dm_boundary,
    288       1.1  thorpej 	    (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK, &res);
    289       1.1  thorpej 	if (error)
    290       1.1  thorpej 		return (error);
    291       1.1  thorpej 
    292       1.1  thorpej 	map->dm_segs[0].ds_addr = res;
    293       1.1  thorpej 	map->dm_segs[0].ds_len = m0->m_pkthdr.len;
    294       1.1  thorpej 
    295       1.1  thorpej 	gmap->gd_origbuf = m0;
    296       1.1  thorpej 	gmap->gd_buftype = GAPS_DMA_BUFTYPE_MBUF;
    297       1.1  thorpej 
    298       1.1  thorpej 	map->dm_mapsize = m0->m_pkthdr.len;
    299       1.1  thorpej 	map->dm_nsegs = 1;
    300       1.1  thorpej 
    301       1.1  thorpej 	return (0);
    302       1.1  thorpej }
    303       1.1  thorpej 
    304       1.1  thorpej int
    305       1.1  thorpej gaps_dmamap_load_uio(bus_dma_tag_t t, bus_dmamap_t map, struct uio *uio,
    306       1.1  thorpej     int flags)
    307       1.1  thorpej {
    308       1.1  thorpej 
    309       1.1  thorpej 	printf("gaps_dmamap_load_uio: not implemented\n");
    310       1.1  thorpej 	return (EINVAL);
    311       1.1  thorpej }
    312       1.1  thorpej 
    313       1.1  thorpej int
    314       1.1  thorpej gaps_dmamap_load_raw(bus_dma_tag_t t, bus_dmamap_t map,
    315       1.1  thorpej     bus_dma_segment_t *segs, int nsegs, bus_size_t size, int flags)
    316       1.1  thorpej {
    317       1.1  thorpej 
    318       1.1  thorpej 	printf("gaps_dmamap_load_raw: not implemented\n");
    319       1.1  thorpej 	return (EINVAL);
    320       1.1  thorpej }
    321       1.1  thorpej 
    322       1.1  thorpej void
    323       1.1  thorpej gaps_dmamap_unload(bus_dma_tag_t t, bus_dmamap_t map)
    324       1.1  thorpej {
    325       1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    326       1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    327       1.1  thorpej 
    328       1.1  thorpej 	if (gmap->gd_buftype == GAPS_DMA_BUFTYPE_INVALID) {
    329       1.1  thorpej 		printf("gaps_dmamap_unload: DMA map not loaded!\n");
    330       1.1  thorpej 		return;
    331       1.1  thorpej 	}
    332       1.1  thorpej 
    333       1.1  thorpej 	if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0) {
    334       1.1  thorpej 		(void) extent_free(sc->sc_dma_ex,
    335       1.1  thorpej 		    map->dm_segs[0].ds_addr,
    336       1.1  thorpej 		    map->dm_mapsize, EX_NOWAIT);
    337       1.1  thorpej 
    338       1.1  thorpej 		map->dm_mapsize = 0;
    339       1.1  thorpej 		map->dm_nsegs = 0;
    340       1.1  thorpej 	}
    341       1.1  thorpej 
    342       1.1  thorpej 	gmap->gd_buftype = GAPS_DMA_BUFTYPE_INVALID;
    343       1.1  thorpej }
    344       1.1  thorpej 
    345       1.1  thorpej void
    346       1.1  thorpej gaps_dmamap_sync(bus_dma_tag_t t, bus_dmamap_t map, bus_addr_t offset,
    347       1.1  thorpej     bus_size_t len, int ops)
    348       1.1  thorpej {
    349       1.1  thorpej 	struct gaps_softc *sc = t->_cookie;
    350       1.1  thorpej 	struct gaps_dmamap *gmap = (void *) map;
    351       1.1  thorpej 	bus_addr_t dmaoff = map->dm_segs[0].ds_addr - sc->sc_dmabase;
    352       1.1  thorpej 
    353       1.1  thorpej 	/*
    354       1.1  thorpej 	 * Mixing PRE and POST operations is not allowed.
    355       1.1  thorpej 	 */
    356       1.1  thorpej 	if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
    357       1.1  thorpej 	    (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
    358       1.1  thorpej 		panic("gaps_dmamap_sync: mix PRE and POST");
    359       1.1  thorpej 
    360       1.1  thorpej #ifdef DIAGNOSTIC
    361       1.1  thorpej 	if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
    362       1.3  thorpej 		if (offset >= map->dm_mapsize) {
    363       1.3  thorpej 			printf("offset 0x%lx mapsize 0x%lx\n",
    364       1.3  thorpej 			    offset, map->dm_mapsize);
    365       1.1  thorpej 			panic("gaps_dmamap_sync: bad offset");
    366       1.3  thorpej 		}
    367       1.3  thorpej 		if (len == 0 || (offset + len) > map->dm_mapsize) {
    368       1.3  thorpej 			printf("len 0x%lx offset 0x%lx mapsize 0x%lx\n",
    369       1.3  thorpej 			    len, offset, map->dm_mapsize);
    370       1.1  thorpej 			panic("gaps_dmamap_sync: bad length");
    371       1.3  thorpej 		}
    372       1.1  thorpej 	}
    373       1.1  thorpej #endif
    374       1.1  thorpej 
    375       1.1  thorpej 	switch (gmap->gd_buftype) {
    376       1.1  thorpej 	case GAPS_DMA_BUFTYPE_INVALID:
    377       1.1  thorpej 		printf("gaps_dmamap_sync: DMA map is not loaded!\n");
    378       1.1  thorpej 		return;
    379       1.1  thorpej 
    380       1.1  thorpej 	case GAPS_DMA_BUFTYPE_LINEAR:
    381       1.1  thorpej 		/*
    382       1.1  thorpej 		 * Nothing to do for pre-read.
    383       1.1  thorpej 		 */
    384       1.1  thorpej 
    385       1.1  thorpej 		if (ops & BUS_DMASYNC_PREWRITE) {
    386       1.1  thorpej 			/*
    387       1.1  thorpej 			 * Copy the caller's buffer to the SRAM buffer.
    388       1.1  thorpej 			 */
    389       1.1  thorpej 			bus_space_write_region_1(sc->sc_memt,
    390       1.1  thorpej 			    sc->sc_dma_memh,
    391       1.1  thorpej 			    dmaoff + offset,
    392       1.1  thorpej 			    (u_int8_t *)gmap->gd_origbuf + offset, len);
    393       1.1  thorpej 		}
    394       1.1  thorpej 
    395       1.1  thorpej 		if (ops & BUS_DMASYNC_POSTREAD) {
    396       1.1  thorpej 			/*
    397       1.1  thorpej 			 * Copy the SRAM buffer to the caller's buffer.
    398       1.1  thorpej 			 */
    399       1.1  thorpej 			bus_space_read_region_1(sc->sc_memt,
    400       1.1  thorpej 			    sc->sc_dma_memh,
    401       1.1  thorpej 			    dmaoff + offset,
    402       1.1  thorpej 			    (u_int8_t *)gmap->gd_origbuf + offset, len);
    403       1.1  thorpej 		}
    404       1.1  thorpej 
    405       1.1  thorpej 		/*
    406       1.1  thorpej 		 * Nothing to do for post-write.
    407       1.1  thorpej 		 */
    408       1.1  thorpej 		break;
    409       1.1  thorpej 
    410       1.1  thorpej 	case GAPS_DMA_BUFTYPE_MBUF:
    411       1.1  thorpej 	    {
    412       1.1  thorpej 		struct mbuf *m, *m0 = gmap->gd_origbuf;
    413       1.1  thorpej 		bus_size_t minlen, moff;
    414       1.1  thorpej 
    415       1.1  thorpej 		/*
    416       1.1  thorpej 		 * Nothing to do for pre-read.
    417       1.1  thorpej 		 */
    418       1.1  thorpej 
    419       1.1  thorpej 		if (ops & BUS_DMASYNC_PREWRITE) {
    420       1.1  thorpej 			/*
    421       1.1  thorpej 			 * Copy the caller's buffer into the SRAM buffer.
    422       1.1  thorpej 			 */
    423       1.1  thorpej 			for (moff = offset, m = m0; m != NULL && len != 0;
    424       1.1  thorpej 			     m = m->m_next) {
    425       1.1  thorpej 				/* Find the beginning mbuf. */
    426       1.1  thorpej 				if (moff >= m->m_len) {
    427       1.1  thorpej 					moff -= m->m_len;
    428       1.1  thorpej 					continue;
    429       1.1  thorpej 				}
    430       1.1  thorpej 
    431       1.1  thorpej 				/*
    432       1.1  thorpej 				 * Now at the first mbuf to sync; nail
    433       1.1  thorpej 				 * each one until we have exhausted the
    434       1.1  thorpej 				 * length.
    435       1.1  thorpej 				 */
    436       1.1  thorpej 				minlen = len < m->m_len - moff ?
    437       1.1  thorpej 				    len : m->m_len - moff;
    438       1.1  thorpej 
    439       1.1  thorpej 				bus_space_write_region_1(sc->sc_memt,
    440       1.1  thorpej 				    sc->sc_dma_memh, dmaoff + offset,
    441       1.1  thorpej 				    mtod(m, u_int8_t *) + moff, minlen);
    442       1.1  thorpej 
    443       1.1  thorpej 				moff = 0;
    444       1.1  thorpej 				len -= minlen;
    445       1.1  thorpej 				offset += minlen;
    446       1.1  thorpej 			}
    447       1.1  thorpej 		}
    448       1.1  thorpej 
    449       1.1  thorpej 		if (ops & BUS_DMASYNC_POSTREAD) {
    450       1.1  thorpej 			/*
    451       1.1  thorpej 			 * Copy the SRAM buffer into the caller's buffer.
    452       1.1  thorpej 			 */
    453       1.1  thorpej 			for (moff = offset, m = m0; m != NULL && len != 0;
    454       1.1  thorpej 			     m = m->m_next) {
    455       1.1  thorpej 				/* Find the beginning mbuf. */
    456       1.1  thorpej 				if (moff >= m->m_len) {
    457       1.1  thorpej 					moff -= m->m_len;
    458       1.1  thorpej 					continue;
    459       1.1  thorpej 				}
    460       1.1  thorpej 
    461       1.1  thorpej 				/*
    462       1.1  thorpej 				 * Now at the first mbuf to sync; nail
    463       1.1  thorpej 				 * each one until we have exhausted the
    464       1.1  thorpej 				 * length.
    465       1.1  thorpej 				 */
    466       1.1  thorpej 				minlen = len < m->m_len - moff ?
    467       1.1  thorpej 				    len : m->m_len - moff;
    468       1.1  thorpej 
    469       1.1  thorpej 				bus_space_read_region_1(sc->sc_memt,
    470       1.1  thorpej 				    sc->sc_dma_memh, dmaoff + offset,
    471       1.1  thorpej 				    mtod(m, u_int8_t *) + moff, minlen);
    472       1.1  thorpej 
    473       1.1  thorpej 				moff = 0;
    474       1.1  thorpej 				len -= minlen;
    475       1.1  thorpej 				offset += minlen;
    476       1.1  thorpej 			}
    477       1.1  thorpej 		}
    478       1.1  thorpej 
    479       1.1  thorpej 		/*
    480       1.1  thorpej 		 * Nothing to do for post-write.
    481       1.1  thorpej 		 */
    482       1.1  thorpej 		break;
    483       1.1  thorpej 	    }
    484       1.1  thorpej 
    485       1.1  thorpej 	default:
    486       1.1  thorpej 		printf("unknown buffer type %d\n", gmap->gd_buftype);
    487       1.1  thorpej 		panic("gaps_dmamap_sync");
    488       1.1  thorpej 	}
    489       1.1  thorpej }
    490       1.1  thorpej 
    491       1.1  thorpej int
    492       1.1  thorpej gaps_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    493       1.1  thorpej     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    494       1.1  thorpej     int flags)
    495       1.1  thorpej {
    496       1.1  thorpej 	extern paddr_t avail_start, avail_end;	/* from pmap.c */
    497       1.1  thorpej 
    498       1.1  thorpej 	struct pglist mlist;
    499       1.1  thorpej 	paddr_t curaddr, lastaddr;
    500       1.5      chs 	struct vm_page *m;
    501       1.1  thorpej 	int curseg, error;
    502       1.1  thorpej 
    503       1.1  thorpej 	/* Always round the size. */
    504       1.1  thorpej 	size = round_page(size);
    505       1.1  thorpej 
    506       1.1  thorpej 	/*
    507       1.1  thorpej 	 * Allocate the pages from the VM system.
    508       1.1  thorpej 	 */
    509       1.1  thorpej 	error = uvm_pglistalloc(size, avail_start, avail_end - PAGE_SIZE,
    510       1.1  thorpej 	    alignment, boundary, &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
    511       1.1  thorpej 	if (error)
    512       1.1  thorpej 		return (error);
    513       1.1  thorpej 
    514       1.1  thorpej 	/*
    515       1.1  thorpej 	 * Compute the location, size, and number of segments actually
    516       1.1  thorpej 	 * returned by the VM code.
    517       1.1  thorpej 	 */
    518       1.1  thorpej 	m = mlist.tqh_first;
    519       1.1  thorpej 	curseg = 0;
    520       1.1  thorpej 	lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
    521       1.1  thorpej 	segs[curseg].ds_len = PAGE_SIZE;
    522       1.1  thorpej 	m = TAILQ_NEXT(m, pageq);
    523       1.1  thorpej 
    524       1.1  thorpej 	for (; m != NULL; m = TAILQ_NEXT(m, pageq)) {
    525       1.1  thorpej 		curaddr = VM_PAGE_TO_PHYS(m);
    526       1.1  thorpej 		if (curaddr == (lastaddr + PAGE_SIZE))
    527       1.1  thorpej 			segs[curseg].ds_len += PAGE_SIZE;
    528       1.1  thorpej 		else {
    529       1.1  thorpej 			curseg++;
    530       1.1  thorpej 			segs[curseg].ds_addr = curaddr;
    531       1.1  thorpej 			segs[curseg].ds_len = PAGE_SIZE;
    532       1.1  thorpej 		}
    533       1.1  thorpej 		lastaddr = curaddr;
    534       1.1  thorpej 	}
    535       1.1  thorpej 
    536       1.1  thorpej 	*rsegs = curseg + 1;
    537       1.1  thorpej 
    538       1.1  thorpej 	return (0);
    539       1.1  thorpej }
    540       1.1  thorpej 
    541       1.1  thorpej void
    542       1.1  thorpej gaps_dmamem_free(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs)
    543       1.1  thorpej {
    544       1.1  thorpej 	struct pglist mlist;
    545       1.5      chs 	struct vm_page *m;
    546       1.1  thorpej 	bus_addr_t addr;
    547       1.1  thorpej 	int curseg;
    548       1.1  thorpej 
    549       1.1  thorpej 	/*
    550       1.1  thorpej 	 * Build a list of pages to free back to the VM system.
    551       1.1  thorpej 	 */
    552       1.1  thorpej 	TAILQ_INIT(&mlist);
    553       1.1  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    554       1.1  thorpej 		for (addr = segs[curseg].ds_addr;
    555       1.1  thorpej 		     addr < segs[curseg].ds_addr + segs[curseg].ds_len;
    556       1.1  thorpej 		     addr += PAGE_SIZE) {
    557       1.1  thorpej 			m = PHYS_TO_VM_PAGE(addr);
    558       1.1  thorpej 			TAILQ_INSERT_TAIL(&mlist, m, pageq);
    559       1.1  thorpej 		}
    560       1.1  thorpej 	}
    561       1.1  thorpej 
    562       1.1  thorpej 	uvm_pglistfree(&mlist);
    563       1.1  thorpej }
    564       1.1  thorpej 
    565       1.1  thorpej int
    566       1.1  thorpej gaps_dmamem_map(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    567       1.1  thorpej     size_t size, caddr_t *kvap, int flags)
    568       1.1  thorpej {
    569       1.1  thorpej 	vaddr_t va;
    570       1.1  thorpej 	bus_addr_t addr;
    571       1.1  thorpej 	int curseg;
    572       1.1  thorpej 
    573       1.1  thorpej 	/*
    574       1.1  thorpej 	 * If we're only mapping 1 segment, use P2SEG, to avoid
    575       1.1  thorpej 	 * TLB thrashing.
    576       1.1  thorpej 	 */
    577       1.1  thorpej 	if (nsegs == 1) {
    578       1.1  thorpej 		*kvap = (caddr_t) SH3_PHYS_TO_P2SEG(segs[0].ds_addr);
    579       1.1  thorpej 		return (0);
    580       1.1  thorpej 	}
    581       1.1  thorpej 
    582       1.1  thorpej 	size = round_page(size);
    583       1.1  thorpej 
    584  1.8.10.1     yamt 	va = uvm_km_alloc(kernel_map, size, 0, UVM_KMF_VAONLY);
    585       1.1  thorpej 
    586       1.1  thorpej 	if (va == 0)
    587       1.1  thorpej 		return (ENOMEM);
    588       1.1  thorpej 
    589       1.1  thorpej 	*kvap = (caddr_t) va;
    590       1.1  thorpej 
    591       1.1  thorpej 	for (curseg = 0; curseg < nsegs; curseg++) {
    592       1.1  thorpej 		for (addr = segs[curseg].ds_addr;
    593       1.1  thorpej 		     addr < segs[curseg].ds_addr + segs[curseg].ds_len;
    594       1.1  thorpej 		     addr += PAGE_SIZE, va += PAGE_SIZE, size -= PAGE_SIZE) {
    595       1.1  thorpej 			if (size == 0)
    596       1.1  thorpej 				panic("gaps_dmamem_map: size botch");
    597       1.1  thorpej 			pmap_kenter_pa(va, addr,
    598       1.1  thorpej 			    VM_PROT_READ | VM_PROT_WRITE);
    599       1.1  thorpej 		}
    600       1.1  thorpej 	}
    601       1.6    chris 	pmap_update(pmap_kernel());
    602       1.1  thorpej 
    603       1.1  thorpej 	return (0);
    604       1.1  thorpej }
    605       1.1  thorpej 
    606       1.1  thorpej void
    607       1.1  thorpej gaps_dmamem_unmap(bus_dma_tag_t t, caddr_t kva, size_t size)
    608       1.1  thorpej {
    609       1.1  thorpej 
    610       1.1  thorpej #ifdef DIAGNOSTIC
    611       1.1  thorpej 	if ((u_long) kva & PAGE_MASK)
    612       1.1  thorpej 		panic("gaps_dmamem_unmap");
    613       1.1  thorpej #endif
    614       1.1  thorpej 
    615       1.1  thorpej 	/*
    616       1.1  thorpej 	 * Nothing to do if we mapped it with P2SEG.
    617       1.1  thorpej 	 */
    618       1.1  thorpej 	if (kva >= (caddr_t) SH3_P2SEG_BASE &&
    619       1.1  thorpej 	    kva <= (caddr_t) SH3_P2SEG_END)
    620       1.1  thorpej 		return;
    621       1.1  thorpej 
    622       1.1  thorpej 	size = round_page(size);
    623       1.1  thorpej 	pmap_kremove((vaddr_t) kva, size);
    624       1.6    chris 	pmap_update(pmap_kernel());
    625  1.8.10.1     yamt 	uvm_km_free(kernel_map, (vaddr_t) kva, size, UVM_KMF_VAONLY);
    626       1.1  thorpej }
    627       1.1  thorpej 
    628       1.1  thorpej paddr_t
    629       1.1  thorpej gaps_dmamem_mmap(bus_dma_tag_t t, bus_dma_segment_t *segs, int nsegs,
    630       1.1  thorpej     off_t off, int prot, int flags)
    631       1.1  thorpej {
    632       1.1  thorpej 
    633       1.1  thorpej 	/* Not implemented. */
    634       1.1  thorpej 	return (-1);
    635       1.1  thorpej }
    636