1 /* $NetBSD: rbus.c,v 1.27 2009/03/14 21:04:19 dsl Exp $ */
4 * Copyright (c) 1999 and 2000
5 * HAYAKAWA Koichi. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: rbus.c,v 1.27 2009/03/14 21:04:19 dsl Exp $");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/malloc.h>
35 #include <sys/extent.h>
39 #include <dev/cardbus/rbus.h>
41 /* #define RBUS_DEBUG */
43 #if defined RBUS_DEBUG
45 #define DPRINTF(a) printf a
53 static rbus_tag_t
rbus_new_body(bus_space_tag_t bt
, rbus_tag_t parent
,
54 struct extent
*ex
, bus_addr_t start
,
55 bus_addr_t end
, bus_addr_t offset
,
60 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
)
62 return rbus_space_alloc_subregion(rbt
, rbt
->rb_start
, rbt
->rb_end
,
63 addr
, size
, mask
, align
, flags
, addrp
, bshp
);
70 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
)
72 bus_addr_t decodesize
= mask
+ 1;
73 bus_addr_t boundary
, search_addr
;
76 int exflags
= EX_FAST
| EX_NOWAIT
| EX_MALLOCOK
;
78 DPRINTF(("rbus_space_alloc: addr %lx, size %lx, mask %lx, align %lx\n",
79 (u_long
)addr
, (u_long
)size
, (u_long
)mask
, (u_long
)align
));
81 addr
+= rbt
->rb_offset
;
88 if (rbt
->rb_flags
== RBUS_SPACE_ASK_PARENT
) {
89 return rbus_space_alloc(rbt
->rb_parent
, addr
, size
, mask
,
90 align
, flags
, addrp
, bshp
);
91 } else if (rbt
->rb_flags
== RBUS_SPACE_SHARE
||
92 rbt
->rb_flags
== RBUS_SPACE_DEDICATE
) {
93 /* rbt has its own sh_extent */
96 * sanity check: the subregion [substart, subend] should be
97 * smaller than the region included in sh_extent.
99 if (substart
< rbt
->rb_ext
->ex_start
100 || subend
> rbt
->rb_ext
->ex_end
) {
101 DPRINTF(("rbus: out of range\n"));
105 if (decodesize
== align
) {
106 if(extent_alloc_subregion(rbt
->rb_ext
, substart
,
107 subend
, size
, align
, 0, exflags
, &result
)) {
110 } else if (decodesize
== 0) {
111 /* maybe, the register is overflowed. */
113 if (extent_alloc_subregion(rbt
->rb_ext
, addr
,
114 addr
+ size
, size
, 1, 0, exflags
, &result
)) {
119 boundary
= decodesize
> align
? decodesize
: align
;
121 search_addr
= (substart
& ~(boundary
- 1)) + addr
;
123 if (search_addr
< substart
) {
124 search_addr
+= boundary
;
128 for (; search_addr
+ size
<= subend
;
129 search_addr
+= boundary
) {
130 val
= extent_alloc_subregion(rbt
->rb_ext
,
131 search_addr
, search_addr
+ size
, size
,
132 align
, 0, exflags
, &result
);
133 DPRINTF(("rbus: trying [%lx:%lx] %lx\n",
135 (u_long
)search_addr
+ size
, (u_long
)align
));
142 DPRINTF(("rbus: no space found\n"));
147 if(md_space_map(rbt
->rb_bt
, result
, size
, flags
, bshp
)) {
149 extent_free(rbt
->rb_ext
, result
, size
, exflags
);
154 *addrp
= result
+ rbt
->rb_offset
;
159 DPRINTF(("rbus: no rbus type\n"));
169 rbus_space_free(rbus_tag_t rbt
, bus_space_handle_t bsh
, bus_size_t size
, bus_addr_t
*addrp
)
171 int exflags
= EX_FAST
| EX_NOWAIT
;
175 if (rbt
->rb_flags
== RBUS_SPACE_ASK_PARENT
) {
176 status
= rbus_space_free(rbt
->rb_parent
, bsh
, size
, &addr
);
177 } else if (rbt
->rb_flags
== RBUS_SPACE_SHARE
||
178 rbt
->rb_flags
== RBUS_SPACE_DEDICATE
) {
179 md_space_unmap(rbt
->rb_bt
, bsh
, size
, &addr
);
181 extent_free(rbt
->rb_ext
, addr
, size
, exflags
);
185 /* error. INVALID rbustag */
188 if (status
== 0 && addrp
!= NULL
) {
198 * rbus_new_body(bus_space_tag_t bt, rbus_tag_t parent,
199 * struct extent *ex, bus_addr_t start, bus_size_t end,
200 * bus_addr_t offset, int flags)
204 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 if (parent
!= NULL
) {
210 if (start
< parent
->rb_start
|| end
> parent
->rb_end
) {
212 * out of range: [start, size] should be
213 * contained in parent space
216 /* Should I invoke panic? */
220 if (NULL
== (rb
= (rbus_tag_t
)malloc(sizeof(struct rbustag
), M_DEVBUF
,
222 panic("no memory for rbus instance");
226 rb
->rb_parent
= parent
;
227 rb
->rb_start
= start
;
229 rb
->rb_offset
= offset
;
230 rb
->rb_flags
= flags
;
233 DPRINTF(("rbus_new_body: [%lx, %lx] type %s name [%s]\n",
234 (u_long
)start
, (u_long
)end
,
235 flags
== RBUS_SPACE_SHARE
? "share" :
236 flags
== RBUS_SPACE_DEDICATE
? "dedicated" :
237 flags
== RBUS_SPACE_ASK_PARENT
? "parent" : "invalid",
238 ex
!= NULL
? ex
->ex_name
: "noname"));
246 * rbus_tag_t rbus_new(rbus_tag_t parent, bus_addr_t start, bus_size_t
247 * size, bus_addr_t offset, int flags)
249 * This function makes a new child rbus instance.
252 rbus_new(rbus_tag_t parent
, bus_addr_t start
, bus_size_t size
, bus_addr_t offset
, int flags
)
255 struct extent
*ex
= NULL
;
256 bus_addr_t end
= start
+ size
;
258 if (flags
== RBUS_SPACE_SHARE
) {
260 } else if (flags
== RBUS_SPACE_DEDICATE
) {
261 if (NULL
== (ex
= extent_create("rbus", start
, end
, M_DEVBUF
,
262 NULL
, 0, EX_NOCOALESCE
|EX_NOWAIT
))) {
265 } else if (flags
== RBUS_SPACE_ASK_PARENT
) {
272 rb
= rbus_new_body(parent
->rb_bt
, parent
, ex
, start
, start
+ size
,
275 if ((rb
== NULL
) && (flags
== RBUS_SPACE_DEDICATE
)) {
286 * rbus_tag_t rbus_new_root_delegate(bus_space_tag, bus_addr_t,
287 * bus_size_t, bus_addr_t offset)
289 * This function makes a root rbus instance.
292 rbus_new_root_delegate(bus_space_tag_t bt
, bus_addr_t start
, bus_size_t size
, bus_addr_t offset
)
297 if (NULL
== (ex
= extent_create("rbus root", start
, start
+ size
,
298 M_DEVBUF
, NULL
, 0, EX_NOCOALESCE
|EX_NOWAIT
))) {
302 rb
= rbus_new_body(bt
, NULL
, ex
, start
, start
+ size
, offset
,
303 RBUS_SPACE_DEDICATE
);
315 * rbus_tag_t rbus_new_root_share(bus_space_tag, struct extent *,
316 * bus_addr_t, bus_size_t, bus_addr_t offset)
318 * This function makes a root rbus instance.
321 rbus_new_root_share(bus_space_tag_t bt
, struct extent
*ex
, bus_addr_t start
, bus_size_t size
, bus_addr_t offset
)
324 if (start
< ex
->ex_start
|| start
+ size
> ex
->ex_end
) {
326 * out of range: [start, size] should be contained in
330 /* Should I invoke panic? */
333 return rbus_new_body(bt
, NULL
, ex
, start
, start
+ size
, offset
,
342 * int rbus_delete (rbus_tag_t rb)
344 * This function deletes the rbus structure pointed in the argument.
347 rbus_delete(rbus_tag_t rb
)
349 DPRINTF(("rbus_delete called [%s]\n",
350 rb
->rb_ext
!= NULL
? rb
->rb_ext
->ex_name
: "noname"));
351 if (rb
->rb_flags
== RBUS_SPACE_DEDICATE
) {
352 extent_destroy(rb
->rb_ext
);