Home | History | Annotate | Line # | Download | only in jazz
      1 /*	$NetBSD: jazzdmatlb.c,v 1.18 2023/12/20 06:36:02 thorpej 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/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: jazzdmatlb.c,v 1.18 2023/12/20 06:36:02 thorpej Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/device.h>
     41 #include <sys/proc.h>
     42 #include <sys/vmem.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <machine/autoconf.h>
     47 #include <sys/bus.h>
     48 
     49 #include <arc/jazz/jazzdmatlbreg.h>
     50 #include <arc/jazz/jazzdmatlbvar.h>
     51 
     52 #include <mips/cache.h>
     53 
     54 extern paddr_t	kvtophys(vaddr_t);	/* XXX */
     55 
     56 #define NDMATLB		(JAZZ_DMATLB_SIZE / sizeof(jazz_dma_pte_t))
     57 
     58 static bus_space_tag_t dmatlb_iot;
     59 static bus_space_handle_t dmatlb_ioh;
     60 
     61 static vmem_t *dmatlb_arena;
     62 static jazz_dma_pte_t *dma_tlb;
     63 
     64 /*
     65  *  Initialize the dma mapping register area and pool.
     66  */
     67 void
     68 jazz_dmatlb_init(bus_space_tag_t iot, bus_addr_t ioaddr)
     69 {
     70 	int err;
     71 
     72 	dmatlb_iot = iot;
     73 	err = bus_space_map(iot, ioaddr, JAZZ_DMATLB_REGSIZE, 0, &dmatlb_ioh);
     74 	if (err != 0)
     75 		panic("jazz_dmatlb_init: cannot map 0x%lx", ioaddr);
     76 
     77 	dma_tlb = (jazz_dma_pte_t *)PICA_TL_BASE;
     78 
     79 	mips_dcache_wbinv_all();/* Make sure no map entries are cached */
     80 	memset((char *)dma_tlb, 0, JAZZ_DMATLB_SIZE);
     81 
     82 	dmatlb_arena = vmem_create("dmatlb", 0, NDMATLB,
     83 				   1,		/* quantum */
     84 				   NULL,	/* importfn */
     85 				   NULL,	/* releasefn */
     86 				   NULL,	/* source */
     87 				   0,		/* qcache_max */
     88 				   VM_SLEEP,
     89 				   IPL_VM);
     90 	KASSERT(dmatlb_arena != NULL);
     91 
     92 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_MAP,
     93 	    MIPS_KSEG1_TO_PHYS(dma_tlb));
     94 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_LIMIT,
     95 	    JAZZ_DMATLB_SIZE);
     96 	jazz_dmatlb_flush();
     97 }
     98 
     99 /*
    100  *  Allocate an array of 'size' DMA PTEs.
    101  *  Return address to first pte.
    102  */
    103 jazz_dma_pte_t *
    104 jazz_dmatlb_alloc(int npte, bus_size_t boundary, int flags, bus_addr_t *addr)
    105 {
    106 	vmem_addr_t start;
    107 	int err;
    108 
    109 	const vm_flag_t vmflags = VM_INSTANTFIT |
    110 	    ((flags & BUS_DMA_WAITOK) ? VM_SLEEP : VM_NOSLEEP);
    111 
    112 	err = vmem_xalloc(dmatlb_arena, npte,
    113 			  1,			/* align */
    114 			  0,			/* phase */
    115 			  boundary / JAZZ_DMA_PAGE_SIZE,
    116 			  VMEM_ADDR_MIN,
    117 			  VMEM_ADDR_MAX,
    118 			  vmflags,
    119 			  &start);
    120 	if (err)
    121 		return NULL;
    122 
    123 	*addr = start * JAZZ_DMA_PAGE_SIZE;
    124 
    125 	return dma_tlb + start;
    126 }
    127 
    128 /*
    129  *  Free an array of DMA PTEs.
    130  */
    131 void
    132 jazz_dmatlb_free(bus_addr_t addr, int npte)
    133 {
    134 	vmem_addr_t start;
    135 
    136 	start = addr / JAZZ_DMA_PAGE_SIZE;
    137 	vmem_xfree(dmatlb_arena, start, npte);
    138 }
    139 
    140 /*
    141  *  Map up a virtual address space in dma space given by
    142  *  the dma control structure.
    143  */
    144 void
    145 jazz_dmatlb_map_va(struct vmspace *vm, vaddr_t va, vsize_t size,
    146     jazz_dma_pte_t *dma_pte)
    147 {
    148 	paddr_t pa;
    149 
    150 	size = jazz_dma_page_round(size + jazz_dma_page_offs(va));
    151 	va &= JAZZ_DMA_PAGE_NUM;
    152 	while (size > 0) {
    153 		if (!VMSPACE_IS_KERNEL_P(vm))
    154 			(void)pmap_extract(vm_map_pmap(&vm->vm_map), va, &pa);
    155 		else
    156 			pa = kvtophys(va);
    157 
    158 		pa &= JAZZ_DMA_PAGE_NUM;
    159 		dma_pte->lo_addr = pa;
    160 		dma_pte->hi_addr = 0;
    161 		dma_pte++;
    162 		va += JAZZ_DMA_PAGE_SIZE;
    163 		size -= JAZZ_DMA_PAGE_SIZE;
    164 	}
    165 }
    166 
    167 /*
    168  *  Map up a physical address space in dma space given by
    169  *  the dma control structure.
    170  */
    171 void
    172 jazz_dmatlb_map_pa(paddr_t pa, psize_t size, jazz_dma_pte_t *dma_pte)
    173 {
    174 
    175 	size = jazz_dma_page_round(size + jazz_dma_page_offs(pa));
    176 	pa &= JAZZ_DMA_PAGE_NUM;
    177 	while (size > 0) {
    178 		dma_pte->lo_addr = pa;
    179 		dma_pte->hi_addr = 0;
    180 		dma_pte++;
    181 		pa += JAZZ_DMA_PAGE_SIZE;
    182 		size -= JAZZ_DMA_PAGE_SIZE;
    183 	}
    184 }
    185 
    186 /*
    187  *  Prepare for new dma by flushing
    188  */
    189 void
    190 jazz_dmatlb_flush(void)
    191 {
    192 
    193 	bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_IVALID, 0);
    194 }
    195