rbus.c revision 1.25.20.1 1 /* $NetBSD: rbus.c,v 1.25.20.1 2009/05/04 08:12:36 yamt Exp $ */
2 /*
3 * Copyright (c) 1999 and 2000
4 * HAYAKAWA Koichi. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by HAYAKAWA Koichi.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: rbus.c,v 1.25.20.1 2009/05/04 08:12:36 yamt Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/malloc.h>
39 #include <sys/extent.h>
40
41 #include <sys/bus.h>
42
43 #include <dev/cardbus/rbus.h>
44
45 /* #define RBUS_DEBUG */
46
47 #if defined RBUS_DEBUG
48 #define STATIC
49 #define DPRINTF(a) printf a
50 #else
51 #define STATIC static
52 #define DPRINTF(a)
53 #endif
54
55
56
57 static rbus_tag_t rbus_new_body(bus_space_tag_t bt, rbus_tag_t parent,
58 struct extent *ex, bus_addr_t start,
59 bus_addr_t end, bus_addr_t offset,
60 int flags);
61
62
63 int
64 rbus_space_alloc(rbus_tag_t rbt, bus_addr_t addr, bus_size_t size, bus_addr_t mask, bus_addr_t align, int flags, bus_addr_t *addrp, bus_space_handle_t *bshp)
65 {
66 return rbus_space_alloc_subregion(rbt, rbt->rb_start, rbt->rb_end,
67 addr, size, mask, align, flags, addrp, bshp);
68 }
69
70
71
72
73 int
74 rbus_space_alloc_subregion(rbus_tag_t rbt, bus_addr_t substart, bus_addr_t subend, bus_addr_t addr, bus_size_t size, bus_addr_t mask, bus_addr_t align, int flags, bus_addr_t *addrp, bus_space_handle_t *bshp)
75 {
76 bus_addr_t decodesize = mask + 1;
77 bus_addr_t boundary, search_addr;
78 int val;
79 u_long result;
80 int exflags = EX_FAST | EX_NOWAIT | EX_MALLOCOK;
81
82 DPRINTF(("rbus_space_alloc: addr %lx, size %lx, mask %lx, align %lx\n",
83 (u_long)addr, (u_long)size, (u_long)mask, (u_long)align));
84
85 addr += rbt->rb_offset;
86
87 if (mask == 0) {
88 /* FULL Decode */
89 decodesize = 0;
90 }
91
92 if (rbt->rb_flags == RBUS_SPACE_ASK_PARENT) {
93 return rbus_space_alloc(rbt->rb_parent, addr, size, mask,
94 align, flags, addrp, bshp);
95 } else if (rbt->rb_flags == RBUS_SPACE_SHARE ||
96 rbt->rb_flags == RBUS_SPACE_DEDICATE) {
97 /* rbt has its own sh_extent */
98
99 /*
100 * sanity check: the subregion [substart, subend] should be
101 * smaller than the region included in sh_extent.
102 */
103 if (substart < rbt->rb_ext->ex_start
104 || subend > rbt->rb_ext->ex_end) {
105 DPRINTF(("rbus: out of range\n"));
106 return 1;
107 }
108
109 if (decodesize == align) {
110 if(extent_alloc_subregion(rbt->rb_ext, substart,
111 subend, size, align, 0, exflags, &result)) {
112 return 1;
113 }
114 } else if (decodesize == 0) {
115 /* maybe, the register is overflowed. */
116
117 if (extent_alloc_subregion(rbt->rb_ext, addr,
118 addr + size, size, 1, 0, exflags, &result)) {
119 return 1;
120 }
121 } else {
122
123 boundary = decodesize > align ? decodesize : align;
124
125 search_addr = (substart & ~(boundary - 1)) + addr;
126
127 if (search_addr < substart) {
128 search_addr += boundary;
129 }
130
131 val = 1;
132 for (; search_addr + size <= subend;
133 search_addr += boundary) {
134 val = extent_alloc_subregion(rbt->rb_ext,
135 search_addr, search_addr + size, size,
136 align, 0, exflags, &result);
137 DPRINTF(("rbus: trying [%lx:%lx] %lx\n",
138 (u_long)search_addr,
139 (u_long)search_addr + size, (u_long)align));
140 if (val == 0) {
141 break;
142 }
143 }
144 if (val != 0) {
145 /* no space found */
146 DPRINTF(("rbus: no space found\n"));
147 return 1;
148 }
149 }
150
151 if(md_space_map(rbt->rb_bt, result, size, flags, bshp)) {
152 /* map failed */
153 extent_free(rbt->rb_ext, result, size, exflags);
154 return 1;
155 }
156
157 if (addrp != NULL) {
158 *addrp = result + rbt->rb_offset;
159 }
160 return 0;
161 } else {
162 /* error!! */
163 DPRINTF(("rbus: no rbus type\n"));
164 return 1;
165 }
166 }
167
168
169
170
171
172 int
173 rbus_space_free(rbus_tag_t rbt, bus_space_handle_t bsh, bus_size_t size, bus_addr_t *addrp)
174 {
175 int exflags = EX_FAST | EX_NOWAIT;
176 bus_addr_t addr;
177 int status = 1;
178
179 if (rbt->rb_flags == RBUS_SPACE_ASK_PARENT) {
180 status = rbus_space_free(rbt->rb_parent, bsh, size, &addr);
181 } else if (rbt->rb_flags == RBUS_SPACE_SHARE ||
182 rbt->rb_flags == RBUS_SPACE_DEDICATE) {
183 md_space_unmap(rbt->rb_bt, bsh, size, &addr);
184
185 extent_free(rbt->rb_ext, addr, size, exflags);
186
187 status = 0;
188 } else {
189 /* error. INVALID rbustag */
190 status = 1;
191 }
192 if (status == 0 && addrp != NULL) {
193 *addrp = addr;
194 }
195 return status;
196 }
197
198
199
200 /*
201 * static rbus_tag_t
202 * rbus_new_body(bus_space_tag_t bt, rbus_tag_t parent,
203 * struct extent *ex, bus_addr_t start, bus_size_t end,
204 * bus_addr_t offset, int flags)
205 *
206 */
207 static rbus_tag_t
208 rbus_new_body(bus_space_tag_t bt, rbus_tag_t parent, struct extent *ex, bus_addr_t start, bus_addr_t end, bus_addr_t offset, int flags)
209 {
210 rbus_tag_t rb;
211
212 /* sanity check */
213 if (parent != NULL) {
214 if (start < parent->rb_start || end > parent->rb_end) {
215 /*
216 * out of range: [start, size] should be
217 * contained in parent space
218 */
219 return 0;
220 /* Should I invoke panic? */
221 }
222 }
223
224 if (NULL == (rb = (rbus_tag_t)malloc(sizeof(struct rbustag), M_DEVBUF,
225 M_NOWAIT))) {
226 panic("no memory for rbus instance");
227 }
228
229 rb->rb_bt = bt;
230 rb->rb_parent = parent;
231 rb->rb_start = start;
232 rb->rb_end = end;
233 rb->rb_offset = offset;
234 rb->rb_flags = flags;
235 rb->rb_ext = ex;
236
237 DPRINTF(("rbus_new_body: [%lx, %lx] type %s name [%s]\n",
238 (u_long)start, (u_long)end,
239 flags == RBUS_SPACE_SHARE ? "share" :
240 flags == RBUS_SPACE_DEDICATE ? "dedicated" :
241 flags == RBUS_SPACE_ASK_PARENT ? "parent" : "invalid",
242 ex != NULL ? ex->ex_name : "noname"));
243
244 return rb;
245 }
246
247
248
249 /*
250 * rbus_tag_t rbus_new(rbus_tag_t parent, bus_addr_t start, bus_size_t
251 * size, bus_addr_t offset, int flags)
252 *
253 * This function makes a new child rbus instance.
254 */
255 rbus_tag_t
256 rbus_new(rbus_tag_t parent, bus_addr_t start, bus_size_t size, bus_addr_t offset, int flags)
257 {
258 rbus_tag_t rb;
259 struct extent *ex = NULL;
260 bus_addr_t end = start + size;
261
262 if (flags == RBUS_SPACE_SHARE) {
263 ex = parent->rb_ext;
264 } else if (flags == RBUS_SPACE_DEDICATE) {
265 if (NULL == (ex = extent_create("rbus", start, end, M_DEVBUF,
266 NULL, 0, EX_NOCOALESCE|EX_NOWAIT))) {
267 return NULL;
268 }
269 } else if (flags == RBUS_SPACE_ASK_PARENT) {
270 ex = NULL;
271 } else {
272 /* Invalid flag */
273 return 0;
274 }
275
276 rb = rbus_new_body(parent->rb_bt, parent, ex, start, start + size,
277 offset, flags);
278
279 if ((rb == NULL) && (flags == RBUS_SPACE_DEDICATE)) {
280 extent_destroy(ex);
281 }
282
283 return rb;
284 }
285
286
287
288
289 /*
290 * rbus_tag_t rbus_new_root_delegate(bus_space_tag, bus_addr_t,
291 * bus_size_t, bus_addr_t offset)
292 *
293 * This function makes a root rbus instance.
294 */
295 rbus_tag_t
296 rbus_new_root_delegate(bus_space_tag_t bt, bus_addr_t start, bus_size_t size, bus_addr_t offset)
297 {
298 rbus_tag_t rb;
299 struct extent *ex;
300
301 if (NULL == (ex = extent_create("rbus root", start, start + size,
302 M_DEVBUF, NULL, 0, EX_NOCOALESCE|EX_NOWAIT))) {
303 return NULL;
304 }
305
306 rb = rbus_new_body(bt, NULL, ex, start, start + size, offset,
307 RBUS_SPACE_DEDICATE);
308
309 if (rb == NULL) {
310 extent_destroy(ex);
311 }
312
313 return rb;
314 }
315
316
317
318 /*
319 * rbus_tag_t rbus_new_root_share(bus_space_tag, struct extent *,
320 * bus_addr_t, bus_size_t, bus_addr_t offset)
321 *
322 * This function makes a root rbus instance.
323 */
324 rbus_tag_t
325 rbus_new_root_share(bus_space_tag_t bt, struct extent *ex, bus_addr_t start, bus_size_t size, bus_addr_t offset)
326 {
327 /* sanity check */
328 if (start < ex->ex_start || start + size > ex->ex_end) {
329 /*
330 * out of range: [start, size] should be contained in
331 * parent space
332 */
333 return 0;
334 /* Should I invoke panic? */
335 }
336
337 return rbus_new_body(bt, NULL, ex, start, start + size, offset,
338 RBUS_SPACE_SHARE);
339 }
340
341
342
343
344
345 /*
346 * int rbus_delete (rbus_tag_t rb)
347 *
348 * This function deletes the rbus structure pointed in the argument.
349 */
350 int
351 rbus_delete(rbus_tag_t rb)
352 {
353 DPRINTF(("rbus_delete called [%s]\n",
354 rb->rb_ext != NULL ? rb->rb_ext->ex_name : "noname"));
355 if (rb->rb_flags == RBUS_SPACE_DEDICATE) {
356 extent_destroy(rb->rb_ext);
357 }
358
359 free(rb, M_DEVBUF);
360
361 return 0;
362 }
363