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