sgmap_common.c revision 1.5 1 /* $NetBSD: sgmap_common.c,v 1.5 1998/01/17 21:53:52 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.5 1998/01/17 21:53:52 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 <vm/vm.h>
51
52 #include <machine/bus.h>
53
54 #include <alpha/common/sgmapvar.h>
55
56 void
57 alpha_sgmap_init(t, sgmap, name, wbase, sgvabase, sgvasize, ptesize, ptva,
58 minptalign)
59 bus_dma_tag_t t;
60 struct alpha_sgmap *sgmap;
61 const char *name;
62 bus_addr_t wbase;
63 bus_addr_t sgvabase;
64 bus_size_t sgvasize;
65 size_t ptesize;
66 void *ptva;
67 bus_size_t minptalign;
68 {
69 bus_dma_segment_t seg;
70 size_t ptsize;
71 int rseg;
72
73 if (sgvasize & PGOFSET)
74 panic("alpha_sgmap_init: size botch");
75
76 sgmap->aps_wbase = wbase;
77 sgmap->aps_sgvabase = sgvabase;
78 sgmap->aps_sgvasize = sgvasize;
79
80 if (ptva != NULL) {
81 /*
82 * We already have a page table; this may be a system
83 * where the page table resides in bridge-resident SRAM.
84 */
85 sgmap->aps_pt = ptva;
86 sgmap->aps_ptpa = 0;
87 } else {
88 /*
89 * Compute the page table size and allocate it. At minimum,
90 * this must be aligned to the page table size. However,
91 * some platforms have more strict alignment reqirements.
92 */
93 ptsize = (sgvasize / NBPG) * ptesize;
94 if (minptalign != 0) {
95 if (minptalign < ptsize)
96 minptalign = ptsize;
97 } else
98 minptalign = ptsize;
99 if (bus_dmamem_alloc(t, ptsize, minptalign, 0, &seg, 1, &rseg,
100 BUS_DMA_NOWAIT))
101 panic("alpha_sgmap_init: can't allocate page table");
102 sgmap->aps_ptpa = seg.ds_addr;
103 sgmap->aps_pt = (caddr_t)ALPHA_PHYS_TO_K0SEG(sgmap->aps_ptpa);
104 }
105
106 /*
107 * Create the extent map used to manage the virtual address
108 * space.
109 */
110 sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
111 M_DEVBUF, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
112 if (sgmap->aps_ex == NULL)
113 panic("alpha_sgmap_init: can't create extent map");
114 }
115
116 int
117 alpha_sgmap_alloc(map, len, sgmap, flags)
118 bus_dmamap_t map;
119 bus_size_t len;
120 struct alpha_sgmap *sgmap;
121 int flags;
122 {
123 int error;
124
125 #ifdef DIAGNOSTIC
126 if (map->_dm_flags & DMAMAP_HAS_SGMAP)
127 panic("alpha_sgmap_alloc: already have sgva space");
128 #endif
129
130 map->_dm_sgvalen = round_page(len);
131 error = extent_alloc(sgmap->aps_ex, map->_dm_sgvalen, NBPG,
132 map->_dm_boundary, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT :
133 EX_WAITOK, &map->_dm_sgva);
134
135 if (error == 0)
136 map->_dm_flags |= DMAMAP_HAS_SGMAP;
137 else
138 map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
139
140 return (error);
141 }
142
143 void
144 alpha_sgmap_free(map, sgmap)
145 bus_dmamap_t map;
146 struct alpha_sgmap *sgmap;
147 {
148
149 #ifdef DIAGNOSTIC
150 if ((map->_dm_flags & DMAMAP_HAS_SGMAP) == 0)
151 panic("alpha_sgmap_free: no sgva space to free");
152 #endif
153
154 if (extent_free(sgmap->aps_ex, map->_dm_sgva, map->_dm_sgvalen,
155 EX_NOWAIT))
156 panic("alpha_sgmap_free");
157
158 map->_dm_flags &= ~DMAMAP_HAS_SGMAP;
159 }
160