1 /* $NetBSD: sgmap_common.c,v 1.16 2001/01/03 20:29:58 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 41 42 __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.16 2001/01/03 20:29:58 thorpej Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/proc.h> 49 50 #include <uvm/uvm_extern.h> 51 52 #define _ALPHA_BUS_DMA_PRIVATE 53 #include <machine/bus.h> 54 55 #include <alpha/common/sgmapvar.h> 56 57 /* 58 * Some systems will prefetch the next page during a memory -> device DMA. 59 * This can cause machine checks if there is not a spill page after the 60 * last page of the DMA (thus avoiding hitting an invalid SGMAP PTE). 61 */ 62 vaddr_t alpha_sgmap_prefetch_spill_page_va; 63 bus_addr_t alpha_sgmap_prefetch_spill_page_pa; 64 65 void 66 alpha_sgmap_init(bus_dma_tag_t t, struct alpha_sgmap *sgmap, const char *name, 67 bus_addr_t wbase, bus_addr_t sgvabase, bus_size_t sgvasize, size_t ptesize, 68 void *ptva, bus_size_t minptalign) 69 { 70 bus_dma_segment_t seg; 71 size_t ptsize; 72 int rseg; 73 74 if (sgvasize & PGOFSET) { 75 printf("size botch for sgmap `%s'\n", name); 76 goto die; 77 } 78 79 sgmap->aps_wbase = wbase; 80 sgmap->aps_sgvabase = sgvabase; 81 sgmap->aps_sgvasize = sgvasize; 82 83 if (ptva != NULL) { 84 /* 85 * We already have a page table; this may be a system 86 * where the page table resides in bridge-resident SRAM. 87 */ 88 sgmap->aps_pt = ptva; 89 sgmap->aps_ptpa = 0; 90 } else { 91 /* 92 * Compute the page table size and allocate it. At minimum, 93 * this must be aligned to the page table size. However, 94 * some platforms have more strict alignment reqirements. 95 */ 96 ptsize = (sgvasize / NBPG) * ptesize; 97 if (minptalign != 0) { 98 if (minptalign < ptsize) 99 minptalign = ptsize; 100 } else 101 minptalign = ptsize; 102 if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg, 103 BUS_DMA_NOWAIT)) { 104 panic("unable to allocate page table for sgmap `%s'\n", 105 name); 106 goto die; 107 } 108 sgmap->aps_ptpa = seg.ds_addr; 109 sgmap->aps_pt = (caddr_t)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa); 110 } 111 112 /* 113 * Create the extent map used to manage the virtual address 114 * space. 115 */ 116 sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1, 117 M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE); 118 if (sgmap->aps_ex == NULL) { 119 printf("unable to create extent map for sgmap `%s'\n", 120 name); 121 goto die; 122 } 123 124 /* 125 * Allocate a spill page if that hasn't already been done. 126 */ 127 if (alpha_sgmap_prefetch_spill_page_va == 0) { 128 if (bus_dmamem_alloc(t, NBPG, 0, 0, &seg, 1, &rseg, 129 BUS_DMA_NOWAIT)) { 130 printf("unable to allocate spill page for sgmap `%s'\n", 131 name); 132 goto die; 133 } 134 alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr; 135 alpha_sgmap_prefetch_spill_page_va = 136 ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa); 137 bzero((caddr_t)alpha_sgmap_prefetch_spill_page_va, NBPG); 138 } 139 140 return; 141 die: 142 panic("alpha_sgmap_init"); 143 } 144 145 int 146 alpha_sgmap_alloc(bus_dmamap_t map, bus_size_t origlen, 147 struct alpha_sgmap *sgmap, int flags) 148 { 149 int error; 150 bus_size_t len = origlen, boundary, alignment; 151 152 #ifdef DIAGNOSTIC 153 if (map->_dm_flags & DMAMAP_HAS_SGMAP) 154 panic("alpha_sgmap_alloc: already have sgva space"); 155 #endif 156 /* 157 * Add a range for spill page. 158 */ 159 len += NBPG; 160 161 /* 162 * And add an additional amount in case of ALLOCNOW. 163 */ 164 if (flags & BUS_DMA_ALLOCNOW) 165 len += NBPG; 166 167 map->_dm_sgvalen = round_page(len); 168 169 /* 170 * ARGH! If the addition of spill pages bumped us over our 171 * boundary, we have to 2x the boundary limit. 172 */ 173 boundary = map->_dm_boundary; 174 if (boundary && boundary < map->_dm_sgvalen) { 175 alignment = boundary; 176 do { 177 boundary <<= 1; 178 } while (boundary < map->_dm_sgvalen); 179 } else 180 alignment = NBPG; 181 #if 0 182 printf("len %x -> %x, _dm_sgvalen %x _dm_boundary %x boundary %x -> ", 183 origlen, len, map->_dm_sgvalen, map->_dm_boundary, boundary); 184 #endif 185 186 error = extent_alloc(sgmap->aps_ex, map->_dm_sgvalen, alignment, 187 boundary, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT : EX_WAITOK, 188 &map->_dm_sgva); 189 #if 0 190 printf("error %d _dm_sgva %x\n", error, map->_dm_sgva); 191 #endif 192 193 if (error == 0) 194 map->_dm_flags |= DMAMAP_HAS_SGMAP; 195 else 196 map->_dm_flags &= ~DMAMAP_HAS_SGMAP; 197 198 return (error); 199 } 200 201 void 202 alpha_sgmap_free(bus_dmamap_t map, struct alpha_sgmap *sgmap) 203 { 204 205 #ifdef DIAGNOSTIC 206 if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0) 207 panic("alpha_sgmap_free: no sgva space to free"); 208 #endif 209 210 if (extent_free(sgmap->aps_ex, map->_dm_sgva, map->_dm_sgvalen, 211 EX_NOWAIT)) 212 panic("alpha_sgmap_free"); 213 214 map->_dm_flags &= ~DMAMAP_HAS_SGMAP; 215 } 216 217 int 218 alpha_sgmap_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 219 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp) 220 { 221 bus_dmamap_t map; 222 int error; 223 224 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, 225 boundary, flags, &map); 226 if (error) 227 return (error); 228 229 if (flags & BUS_DMA_ALLOCNOW) 230 error = alpha_sgmap_alloc(map, round_page(size), 231 t->_sgmap, flags); 232 233 if (error == 0) 234 *dmamp = map; 235 else 236 alpha_sgmap_dmamap_destroy(t, map); 237 238 return (error); 239 } 240 241 void 242 alpha_sgmap_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 243 { 244 245 if (map->_dm_flags & DMAMAP_HAS_SGMAP) 246 alpha_sgmap_free(map, t->_sgmap); 247 248 _bus_dmamap_destroy(t, map); 249 } 250