1 /* $NetBSD: jazzdmatlb.c,v 1.6 2001/11/14 18:15:15 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/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 #include <mips/cache.h> 51 52 extern paddr_t kvtophys __P((vaddr_t)); /* XXX */ 53 54 /* 55 * Currently, only NET and BIO devices use DMA, and splnet > splbio. 56 */ 57 #define spldma() splnet() 58 59 #define NDMATLB (JAZZ_DMATLB_SIZE / sizeof(jazz_dma_pte_t)) 60 61 static bus_space_tag_t dmatlb_iot; 62 static bus_space_handle_t dmatlb_ioh; 63 64 static struct extent *dmatlbmap; 65 static jazz_dma_pte_t *dma_tlb; 66 67 /* 68 * Initialize the dma mapping register area and pool. 69 */ 70 void 71 jazz_dmatlb_init(iot, ioaddr) 72 bus_space_tag_t iot; 73 bus_addr_t ioaddr; 74 { 75 int err; 76 77 dmatlb_iot = iot; 78 err = bus_space_map(iot, ioaddr, JAZZ_DMATLB_REGSIZE, 0, &dmatlb_ioh); 79 if (err != 0) 80 panic("jazz_dmatlb_init: cannot map 0x%lx\n", ioaddr); 81 82 dma_tlb = (jazz_dma_pte_t *)PICA_TL_BASE; 83 84 mips_dcache_wbinv_all();/* Make sure no map entries are cached */ 85 bzero((char *)dma_tlb, JAZZ_DMATLB_SIZE); 86 87 dmatlbmap = extent_create("dmatlb", 0, NDMATLB, M_DEVBUF, NULL, NULL, 88 EX_NOWAIT); 89 if (dmatlbmap == NULL) 90 panic("jazz_dmatlb_init: cannot create extent map"); 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(npte, boundary, flags, addr) 105 int npte; 106 bus_size_t boundary; 107 int flags; 108 bus_addr_t *addr; 109 { 110 u_long start; 111 int err; 112 int s; 113 114 s = spldma(); 115 err = extent_alloc(dmatlbmap, npte, 1, boundary / JAZZ_DMA_PAGE_SIZE, 116 (flags & BUS_DMA_WAITOK) ? (EX_WAITSPACE | EX_WAITOK) : EX_NOWAIT, 117 &start); 118 splx(s); 119 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(addr, npte) 133 bus_addr_t addr; 134 int npte; 135 { 136 u_long start; 137 int s; 138 139 start = addr / JAZZ_DMA_PAGE_SIZE; 140 s = spldma(); 141 extent_free(dmatlbmap, start, npte, EX_NOWAIT); 142 splx(s); 143 } 144 145 /* 146 * Map up a virtual address space in dma space given by 147 * the dma control structure. 148 */ 149 void 150 jazz_dmatlb_map_va(p, va, size, dma_pte) 151 struct proc *p; 152 vaddr_t va; 153 vsize_t size; 154 jazz_dma_pte_t *dma_pte; 155 { 156 paddr_t pa; 157 158 size = jazz_dma_page_round(size + jazz_dma_page_offs(va)); 159 va &= JAZZ_DMA_PAGE_NUM; 160 while (size > 0) { 161 if (p != NULL) 162 (void)pmap_extract(p->p_vmspace->vm_map.pmap, va, &pa); 163 else 164 pa = kvtophys(va); 165 166 pa &= JAZZ_DMA_PAGE_NUM; 167 dma_pte->lo_addr = pa; 168 dma_pte->hi_addr = 0; 169 dma_pte++; 170 va += JAZZ_DMA_PAGE_SIZE; 171 size -= JAZZ_DMA_PAGE_SIZE; 172 } 173 } 174 175 /* 176 * Map up a physical address space in dma space given by 177 * the dma control structure. 178 */ 179 void 180 jazz_dmatlb_map_pa(pa, size, dma_pte) 181 paddr_t pa; 182 psize_t size; 183 jazz_dma_pte_t *dma_pte; 184 { 185 size = jazz_dma_page_round(size + jazz_dma_page_offs(pa)); 186 pa &= JAZZ_DMA_PAGE_NUM; 187 while (size > 0) { 188 dma_pte->lo_addr = pa; 189 dma_pte->hi_addr = 0; 190 dma_pte++; 191 pa += JAZZ_DMA_PAGE_SIZE; 192 size -= JAZZ_DMA_PAGE_SIZE; 193 } 194 } 195 196 /* 197 * Prepare for new dma by flushing 198 */ 199 void 200 jazz_dmatlb_flush() 201 { 202 bus_space_write_4(dmatlb_iot, dmatlb_ioh, JAZZ_DMATLBREG_IVALID, 0); 203 } 204