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