sgmap_common.c revision 1.2.2.2 1 /* $NetBSD: sgmap_common.c,v 1.2.2.2 1997/06/07 04:42:54 cgd Exp $ */
2
3 /*-
4 * Copyright (c) 1997 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 <machine/options.h> /* Config options headers */
41 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
42
43 __KERNEL_RCSID(0, "$NetBSD: sgmap_common.c,v 1.2.2.2 1997/06/07 04:42:54 cgd Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/proc.h>
50
51 #include <vm/vm.h>
52
53 #include <machine/bus.h>
54
55 #include <alpha/common/sgmapvar.h>
56
57 void
58 alpha_sgmap_init(t, sgmap, name, wbase, sgvabase, sgvasize, ptesize, ptva)
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 {
68 bus_dma_segment_t seg;
69 size_t ptsize;
70 int rseg;
71
72 if (sgvasize & PGOFSET)
73 panic("alpha_sgmap_init: size botch");
74
75 sgmap->aps_wbase = wbase;
76 sgmap->aps_sgvabase = sgvabase;
77 sgmap->aps_sgvasize = sgvasize;
78
79 if (ptva != NULL) {
80 /*
81 * We already have a page table; this may be a system
82 * where the page table resides in bridge-resident SRAM.
83 */
84 sgmap->aps_pt = ptva;
85 sgmap->aps_ptpa = 0;
86 } else {
87 /*
88 * Compute the page table size and allocate it. It must
89 * be aligned to its size.
90 */
91 ptsize = (sgvasize / NBPG) * ptesize;
92 if (bus_dmamem_alloc(t, ptsize, ptsize, 0, &seg, 1, &rseg,
93 BUS_DMA_NOWAIT))
94 panic("alpha_sgmap_init: can't allocate page table");
95 if (bus_dmamem_map(t, &seg, rseg, ptsize,
96 (caddr_t *)&sgmap->aps_pt, BUS_DMA_NOWAIT))
97 panic("alpha_sgmap_init: can't map page table");
98 sgmap->aps_ptpa = seg.ds_addr;
99 }
100
101 /*
102 * Create the extent map used to manage the virtual address
103 * space.
104 */
105 sgmap->aps_ex = extent_create((char *)name, sgvabase, sgvasize - 1,
106 M_DEVBUF, NULL, 0, EX_NOWAIT|EX_NOCOALESCE);
107 if (sgmap->aps_ex == NULL)
108 panic("alpha_sgmap_init: can't create extent map");
109 }
110
111 int
112 alpha_sgmap_alloc(map, len, sgmap, flags)
113 bus_dmamap_t map;
114 bus_size_t len;
115 struct alpha_sgmap *sgmap;
116 int flags;
117 {
118 struct alpha_sgmap_cookie *a = map->_dm_sgcookie;
119 int error;
120
121 #ifdef DIAGNOSTIC
122 if (a->apdc_flags & APDC_HAS_SGMAP)
123 panic("alpha_sgmap_alloc: already have sgva space");
124 #endif
125
126 a->apdc_len = round_page(len);
127 error = extent_alloc(sgmap->aps_ex, a->apdc_len, NBPG,
128 map->_dm_boundary, (flags & BUS_DMA_NOWAIT) ? EX_NOWAIT :
129 EX_WAITOK, &a->apdc_sgva);
130
131 if (error == 0)
132 a->apdc_flags |= APDC_HAS_SGMAP;
133 else
134 a->apdc_flags &= ~APDC_HAS_SGMAP;
135
136 return (error);
137 }
138
139 void
140 alpha_sgmap_free(sgmap, a)
141 struct alpha_sgmap *sgmap;
142 struct alpha_sgmap_cookie *a;
143 {
144
145 #ifdef DIAGNOSTIC
146 if ((a->apdc_flags & APDC_HAS_SGMAP) == 0)
147 panic("alpha_sgmap_free: no sgva space to free");
148 #endif
149
150 if (extent_free(sgmap->aps_ex, a->apdc_sgva,
151 a->apdc_len, EX_NOWAIT))
152 panic("alpha_sgmap_free");
153
154 a->apdc_flags &= ~APDC_HAS_SGMAP;
155 }
156