Home | History | Annotate | Line # | Download | only in jazz
jazzdmatlb.c revision 1.2
      1  1.2   mrg /*	$NetBSD: jazzdmatlb.c,v 1.2 2000/06/29 08:34:12 mrg Exp $	*/
      2  1.1  soda /*	$OpenBSD: dma.c,v 1.5 1998/03/01 16:49:57 niklas Exp $	*/
      3  1.1  soda 
      4  1.1  soda /*-
      5  1.1  soda  * Copyright (C) 2000 Shuichiro URATA.  All rights reserved.
      6  1.1  soda  *
      7  1.1  soda  * Redistribution and use in source and binary forms, with or without
      8  1.1  soda  * modification, are permitted provided that the following conditions
      9  1.1  soda  * are met:
     10  1.1  soda  * 1. Redistributions of source code must retain the above copyright
     11  1.1  soda  *    notice, this list of conditions and the following disclaimer.
     12  1.1  soda  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  soda  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  soda  *    documentation and/or other materials provided with the distribution.
     15  1.1  soda  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  soda  *    derived from this software without specific prior written permission.
     17  1.1  soda  *
     18  1.1  soda  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1  soda  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1  soda  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1  soda  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1  soda  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1  soda  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1  soda  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1  soda  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1  soda  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  1.1  soda  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  soda  */
     29  1.1  soda 
     30  1.1  soda /*
     31  1.1  soda  * Jazz derived system dma driver. Handles resource allocation and
     32  1.1  soda  * logical (virtual) address remaping.
     33  1.1  soda  */
     34  1.1  soda 
     35  1.1  soda #include <sys/param.h>
     36  1.1  soda #include <sys/systm.h>
     37  1.1  soda #include <sys/malloc.h>
     38  1.1  soda #include <sys/extent.h>
     39  1.1  soda #include <sys/device.h>
     40  1.1  soda 
     41  1.2   mrg #include <uvm/uvm_extern.h>
     42  1.1  soda 
     43  1.1  soda #include <machine/autoconf.h>
     44  1.1  soda #include <machine/bus.h>
     45  1.1  soda 
     46  1.1  soda #include <arc/pica/pica.h>
     47  1.1  soda #include <arc/jazz/jazzdmatlbreg.h>
     48  1.1  soda #include <arc/jazz/jazzdmatlbvar.h>
     49  1.1  soda 
     50  1.1  soda extern paddr_t	kvtophys __P((vaddr_t));	/* XXX */
     51  1.1  soda 
     52  1.1  soda /*
     53  1.1  soda  * Currently, only NET and BIO devices use DMA, and splnet > splbio.
     54  1.1  soda  */
     55  1.1  soda #define spldma()	splnet()
     56  1.1  soda 
     57  1.1  soda #define NDMATLB		(JAZZ_DMATLB_SIZE / sizeof(jazz_dma_pte_t))
     58  1.1  soda 
     59  1.1  soda static bus_space_tag_t dmatlb_iot;
     60  1.1  soda static bus_space_handle_t dmatlb_ioh;
     61  1.1  soda 
     62  1.1  soda static struct extent *dmatlbmap;
     63  1.1  soda static jazz_dma_pte_t *dma_tlb;
     64  1.1  soda 
     65  1.1  soda /*
     66  1.1  soda  *  Initialize the dma mapping register area and pool.
     67  1.1  soda  */
     68  1.1  soda void
     69  1.1  soda jazz_dmatlb_init(iot, ioaddr)
     70  1.1  soda 	bus_space_tag_t iot;
     71  1.1  soda 	bus_addr_t ioaddr;
     72  1.1  soda {
     73  1.1  soda 	int err;
     74  1.1  soda 
     75  1.1  soda 	dmatlb_iot = iot;
     76  1.1  soda 	err = bus_space_map(iot, ioaddr, JAZZ_DMATLB_REGSIZE, 0, &dmatlb_ioh);
     77  1.1  soda 	if (err != 0)
     78  1.1  soda 		panic("jazz_dmatlb_init: cannot map 0x%x\n", ioaddr);
     79  1.1  soda 
     80  1.1  soda 	dma_tlb = (jazz_dma_pte_t *)PICA_TL_BASE;
     81  1.1  soda 
     82  1.1  soda 	mips3_FlushCache();	/* Make sure no map entries are cached */
     83  1.1  soda 	bzero((char *)dma_tlb, JAZZ_DMATLB_SIZE);
     84  1.1  soda 
     85  1.1  soda 	dmatlbmap = extent_create("dmatlb", 0, NDMATLB, M_DEVBUF, NULL, NULL,
     86  1.1  soda 	    EX_NOWAIT);
     87  1.1  soda 	if (dmatlbmap == NULL)
     88  1.1  soda 		panic("jazz_dmatlb_init: cannot create extent map");
     89  1.1  soda 
     90  1.1  soda 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_MAP,
     91  1.1  soda 	    MIPS_KSEG1_TO_PHYS(dma_tlb));
     92  1.1  soda 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_LIMIT,
     93  1.1  soda 	    JAZZ_DMATLB_SIZE);
     94  1.1  soda 	jazz_dmatlb_flush();
     95  1.1  soda }
     96  1.1  soda 
     97  1.1  soda /*
     98  1.1  soda  *  Allocate an array of 'size' DMA PTEs.
     99  1.1  soda  *  Return address to first pte.
    100  1.1  soda  */
    101  1.1  soda jazz_dma_pte_t *
    102  1.1  soda jazz_dmatlb_alloc(npte, boundary, flags, addr)
    103  1.1  soda 	int npte;
    104  1.1  soda 	bus_size_t boundary;
    105  1.1  soda 	int flags;
    106  1.1  soda 	bus_addr_t *addr;
    107  1.1  soda {
    108  1.1  soda 	u_long start;
    109  1.1  soda 	int err;
    110  1.1  soda 	int s;
    111  1.1  soda 
    112  1.1  soda 	s = spldma();
    113  1.1  soda 	err = extent_alloc(dmatlbmap, npte, 1, boundary / JAZZ_DMA_PAGE_SIZE,
    114  1.1  soda 	    (flags & BUS_DMA_WAITOK) ? (EX_WAITSPACE | EX_WAITOK) : EX_NOWAIT,
    115  1.1  soda 	    &start);
    116  1.1  soda 	splx(s);
    117  1.1  soda 
    118  1.1  soda 	if (err)
    119  1.1  soda 		return (NULL);
    120  1.1  soda 
    121  1.1  soda 	*addr = start * JAZZ_DMA_PAGE_SIZE;
    122  1.1  soda 
    123  1.1  soda 	return (dma_tlb + start);
    124  1.1  soda }
    125  1.1  soda 
    126  1.1  soda /*
    127  1.1  soda  *  Free an array of DMA PTEs.
    128  1.1  soda  */
    129  1.1  soda void
    130  1.1  soda jazz_dmatlb_free(addr, npte)
    131  1.1  soda 	bus_addr_t addr;
    132  1.1  soda 	int npte;
    133  1.1  soda {
    134  1.1  soda 	u_long start;
    135  1.1  soda 	int s;
    136  1.1  soda 
    137  1.1  soda 	start = addr / JAZZ_DMA_PAGE_SIZE;
    138  1.1  soda 	s = spldma();
    139  1.1  soda 	extent_free(dmatlbmap, start, npte, EX_NOWAIT);
    140  1.1  soda 	splx(s);
    141  1.1  soda }
    142  1.1  soda 
    143  1.1  soda /*
    144  1.1  soda  *  Map up a virtual address space in dma space given by
    145  1.1  soda  *  the dma control structure.
    146  1.1  soda  */
    147  1.1  soda void
    148  1.1  soda jazz_dmatlb_map_va(p, va, size, dma_pte)
    149  1.1  soda 	struct proc *p;
    150  1.1  soda 	vaddr_t va;
    151  1.1  soda 	vsize_t size;
    152  1.1  soda 	jazz_dma_pte_t *dma_pte;
    153  1.1  soda {
    154  1.1  soda 	paddr_t pa;
    155  1.1  soda 
    156  1.1  soda 	size = jazz_dma_page_round(size + jazz_dma_page_offs(va));
    157  1.1  soda 	va &= JAZZ_DMA_PAGE_NUM;
    158  1.1  soda 	while (size > 0) {
    159  1.1  soda 		if (p != NULL)
    160  1.1  soda 			(void)pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa);
    161  1.1  soda 		else
    162  1.1  soda 			pa = kvtophys(va);
    163  1.1  soda 
    164  1.1  soda 		pa &= JAZZ_DMA_PAGE_NUM;
    165  1.1  soda 		dma_pte->lo_addr = pa;
    166  1.1  soda 		dma_pte->hi_addr = 0;
    167  1.1  soda 		dma_pte++;
    168  1.1  soda 		va += JAZZ_DMA_PAGE_SIZE;
    169  1.1  soda 		size -= JAZZ_DMA_PAGE_SIZE;
    170  1.1  soda 	}
    171  1.1  soda }
    172  1.1  soda 
    173  1.1  soda /*
    174  1.1  soda  *  Map up a physical address space in dma space given by
    175  1.1  soda  *  the dma control structure.
    176  1.1  soda  */
    177  1.1  soda void
    178  1.1  soda jazz_dmatlb_map_pa(pa, size, dma_pte)
    179  1.1  soda 	paddr_t pa;
    180  1.1  soda 	psize_t size;
    181  1.1  soda 	jazz_dma_pte_t *dma_pte;
    182  1.1  soda {
    183  1.1  soda 	size = jazz_dma_page_round(size + jazz_dma_page_offs(pa));
    184  1.1  soda 	pa &= JAZZ_DMA_PAGE_NUM;
    185  1.1  soda 	while (size > 0) {
    186  1.1  soda 		dma_pte->lo_addr = pa;
    187  1.1  soda 		dma_pte->hi_addr = 0;
    188  1.1  soda 		dma_pte++;
    189  1.1  soda 		pa += JAZZ_DMA_PAGE_SIZE;
    190  1.1  soda 		size -= JAZZ_DMA_PAGE_SIZE;
    191  1.1  soda 	}
    192  1.1  soda }
    193  1.1  soda 
    194  1.1  soda /*
    195  1.1  soda  *  Prepare for new dma by flushing
    196  1.1  soda  */
    197  1.1  soda void
    198  1.1  soda jazz_dmatlb_flush()
    199  1.1  soda {
    200  1.1  soda 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_IVALID, 0);
    201  1.1  soda }
    202