1 /* $NetBSD: sgmap_common.c,v 1.24 2008/04/28 20:23:11 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998, 2001 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 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 34 35 __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.24 2008/04/28 20:23:11 martin Exp $"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/proc.h> 42 43 #include <uvm/uvm_extern.h> 44 45 #define _ALPHA_BUS_DMA_PRIVATE 46 #include <machine/bus.h> 47 48 #include <alpha/common/sgmapvar.h> 49 50 /* 51 * Some systems will prefetch the next page during a memory -> device DMA. 52 * This can cause machine checks if there is not a spill page after the 53 * last page of the DMA (thus avoiding hitting an invalid SGMAP PTE). 54 */ 55 vaddr_t alpha_sgmap_prefetch_spill_page_va; 56 bus_addr_t alpha_sgmap_prefetch_spill_page_pa; 57 58 void 59 alpha_sgmap_init(bus_dma_tag_t t, struct alpha_sgmap *sgmap, const char *name, 60 bus_addr_t wbase, bus_addr_t sgvabase, bus_size_t sgvasize, size_t ptesize, 61 void *ptva, bus_size_t minptalign) 62 { 63 bus_dma_segment_t seg; 64 size_t ptsize; 65 int rseg; 66 67 if (sgvasize & PGOFSET) { 68 printf("size botch for sgmap `%s'\n", name); 69 goto die; 70 } 71 72 sgmap->aps_wbase = wbase; 73 sgmap->aps_sgvabase = sgvabase; 74 sgmap->aps_sgvasize = sgvasize; 75 76 if (ptva != NULL) { 77 /* 78 * We already have a page table; this may be a system 79 * where the page table resides in bridge-resident SRAM. 80 */ 81 sgmap->aps_pt = ptva; 82 sgmap->aps_ptpa = 0; 83 } else { 84 /* 85 * Compute the page table size and allocate it. At minimum, 86 * this must be aligned to the page table size. However, 87 * some platforms have more strict alignment reqirements. 88 */ 89 ptsize = (sgvasize / PAGE_SIZE) * ptesize; 90 if (minptalign != 0) { 91 if (minptalign < ptsize) 92 minptalign = ptsize; 93 } else 94 minptalign = ptsize; 95 if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg, 96 BUS_DMA_NOWAIT)) { 97 panic("unable to allocate page table for sgmap `%s'", 98 name); 99 goto die; 100 } 101 sgmap->aps_ptpa = seg.ds_addr; 102 sgmap->aps_pt = (void *)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa); 103 } 104 105 /* 106 * Create the extent map used to manage the virtual address 107 * space. 108 */ 109 sgmap->aps_ex = extent_create(name, sgvabase, sgvasize - 1, 110 M_DMAMAP, NULL, 0, EX_NOWAIT|EX_NOCOALESCE); 111 if (sgmap->aps_ex == NULL) { 112 printf("unable to create extent map for sgmap `%s'\n", 113 name); 114 goto die; 115 } 116 117 /* 118 * Allocate a spill page if that hasn't already been done. 119 */ 120 if (alpha_sgmap_prefetch_spill_page_va == 0) { 121 if (bus_dmamem_alloc(t, PAGE_SIZE, 0, 0, &seg, 1, &rseg, 122 BUS_DMA_NOWAIT)) { 123 printf("unable to allocate spill page for sgmap `%s'\n", 124 name); 125 goto die; 126 } 127 alpha_sgmap_prefetch_spill_page_pa = seg.ds_addr; 128 alpha_sgmap_prefetch_spill_page_va = 129 ALPHA_PHYS_TO_K0SEG(alpha_sgmap_prefetch_spill_page_pa); 130 memset((void *)alpha_sgmap_prefetch_spill_page_va, 0, 131 PAGE_SIZE); 132 } 133 134 return; 135 die: 136 panic("alpha_sgmap_init"); 137 } 138 139 int 140 alpha_sgmap_dmamap_create(bus_dma_tag_t t, bus_size_t size, int nsegments, 141 bus_size_t maxsegsz, bus_size_t boundary, int flags, bus_dmamap_t *dmamp) 142 { 143 bus_dmamap_t map; 144 int error; 145 146 error = _bus_dmamap_create(t, size, nsegments, maxsegsz, 147 boundary, flags, &map); 148 if (error) 149 return (error); 150 151 /* XXX BUS_DMA_ALLOCNOW */ 152 153 if (error == 0) 154 *dmamp = map; 155 else 156 alpha_sgmap_dmamap_destroy(t, map); 157 158 return (error); 159 } 160 161 void 162 alpha_sgmap_dmamap_destroy(bus_dma_tag_t t, bus_dmamap_t map) 163 { 164 165 KASSERT(map->dm_mapsize == 0); 166 167 _bus_dmamap_destroy(t, map); 168 } 169