xref: /netbsd-src/sys/dev/cardbus/rbus.c (revision 89c9828deb9dc122d96d1dd9289e805edb57b492)
1*89c9828dSpara /*	$NetBSD: rbus.c,v 1.29 2012/01/27 18:53:07 para Exp $	*/
2be360188Ssnj 
34eee1111Shaya /*
4de5dacd8Shaya  * Copyright (c) 1999 and 2000
54eee1111Shaya  *     HAYAKAWA Koichi.  All rights reserved.
64eee1111Shaya  *
74eee1111Shaya  * Redistribution and use in source and binary forms, with or without
84eee1111Shaya  * modification, are permitted provided that the following conditions
94eee1111Shaya  * are met:
104eee1111Shaya  * 1. Redistributions of source code must retain the above copyright
114eee1111Shaya  *    notice, this list of conditions and the following disclaimer.
124eee1111Shaya  * 2. Redistributions in binary form must reproduce the above copyright
134eee1111Shaya  *    notice, this list of conditions and the following disclaimer in the
144eee1111Shaya  *    documentation and/or other materials provided with the distribution.
154eee1111Shaya  *
164eee1111Shaya  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
174eee1111Shaya  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
184eee1111Shaya  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
194eee1111Shaya  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
204eee1111Shaya  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
214eee1111Shaya  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
224eee1111Shaya  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
234eee1111Shaya  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
244eee1111Shaya  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
254eee1111Shaya  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
264eee1111Shaya  */
274eee1111Shaya 
28f61cbe74Slukem #include <sys/cdefs.h>
29*89c9828dSpara __KERNEL_RCSID(0, "$NetBSD: rbus.c,v 1.29 2012/01/27 18:53:07 para Exp $");
304eee1111Shaya 
314eee1111Shaya #include <sys/param.h>
324eee1111Shaya #include <sys/systm.h>
334eee1111Shaya #include <sys/device.h>
344eee1111Shaya #include <sys/malloc.h>
354eee1111Shaya #include <sys/extent.h>
364eee1111Shaya 
37a2a38285Sad #include <sys/bus.h>
384eee1111Shaya 
394eee1111Shaya #include <dev/cardbus/rbus.h>
404eee1111Shaya 
414eee1111Shaya /* #define RBUS_DEBUG */
424eee1111Shaya 
434eee1111Shaya #if defined RBUS_DEBUG
444eee1111Shaya #define STATIC
454eee1111Shaya #define DPRINTF(a) printf a
464eee1111Shaya #else
474eee1111Shaya #define STATIC static
484eee1111Shaya #define DPRINTF(a)
494eee1111Shaya #endif
504eee1111Shaya 
514eee1111Shaya 
524eee1111Shaya 
5318db93c7Sperry static rbus_tag_t rbus_new_body(bus_space_tag_t bt, rbus_tag_t parent,
544eee1111Shaya 				    struct extent *ex, bus_addr_t start,
554eee1111Shaya 				    bus_addr_t end, bus_addr_t offset,
5618db93c7Sperry 				    int flags);
574eee1111Shaya 
584eee1111Shaya 
594eee1111Shaya int
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)6082357f6dSdsl 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)
614eee1111Shaya {
62de5dacd8Shaya 	return rbus_space_alloc_subregion(rbt, rbt->rb_start, rbt->rb_end,
63de5dacd8Shaya 	    addr, size, mask, align, flags, addrp, bshp);
644eee1111Shaya }
654eee1111Shaya 
664eee1111Shaya 
674eee1111Shaya 
684eee1111Shaya 
694eee1111Shaya int
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)7082357f6dSdsl 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)
714eee1111Shaya {
724eee1111Shaya 	bus_addr_t decodesize = mask + 1;
7307fb02cbSchristos 	bus_addr_t boundary, search_addr;
741a159507Saugustss 	int val;
7564f78183Snakayama 	u_long result;
76f89b0f51Shaya 	int exflags = EX_FAST | EX_NOWAIT | EX_MALLOCOK;
774eee1111Shaya 
785da7a65cSaugustss 	DPRINTF(("rbus_space_alloc: addr %lx, size %lx, mask %lx, align %lx\n",
79f8a82ee6Sscw 	    (u_long)addr, (u_long)size, (u_long)mask, (u_long)align));
804eee1111Shaya 
814eee1111Shaya 	addr += rbt->rb_offset;
824eee1111Shaya 
835da7a65cSaugustss 	if (mask == 0) {
844eee1111Shaya 		/* FULL Decode */
854eee1111Shaya 		decodesize = 0;
865da7a65cSaugustss 	}
874eee1111Shaya 
885da7a65cSaugustss 	if (rbt->rb_flags == RBUS_SPACE_ASK_PARENT) {
89de5dacd8Shaya 		return rbus_space_alloc(rbt->rb_parent, addr, size, mask,
90de5dacd8Shaya 		    align, flags, addrp, bshp);
915da7a65cSaugustss 	} else if (rbt->rb_flags == RBUS_SPACE_SHARE ||
925da7a65cSaugustss 	    rbt->rb_flags == RBUS_SPACE_DEDICATE) {
934eee1111Shaya 		/* rbt has its own sh_extent */
944eee1111Shaya 
95de5dacd8Shaya 		/*
96de5dacd8Shaya 		 * sanity check: the subregion [substart, subend] should be
97de5dacd8Shaya 		 * smaller than the region included in sh_extent.
98de5dacd8Shaya 		 */
99de5dacd8Shaya 		if (substart < rbt->rb_ext->ex_start
100de5dacd8Shaya 		    || subend > rbt->rb_ext->ex_end) {
101f89b0f51Shaya 			DPRINTF(("rbus: out of range\n"));
1024eee1111Shaya 			return 1;
1034eee1111Shaya 		}
1045da7a65cSaugustss 
1055da7a65cSaugustss 		if (decodesize == align) {
106de5dacd8Shaya 			if(extent_alloc_subregion(rbt->rb_ext, substart,
10764f78183Snakayama 			    subend, size, align, 0, exflags, &result)) {
1085da7a65cSaugustss 				return 1;
1095da7a65cSaugustss 			}
1105da7a65cSaugustss 		} else if (decodesize == 0) {
111a3407cf6Sskd 			/* maybe, the register is overflowed. */
1125da7a65cSaugustss 
113de5dacd8Shaya 			if (extent_alloc_subregion(rbt->rb_ext, addr,
11464f78183Snakayama 			    addr + size, size, 1, 0, exflags, &result)) {
1154eee1111Shaya 				return 1;
1164eee1111Shaya 			}
1174eee1111Shaya 		} else {
1185da7a65cSaugustss 
1194eee1111Shaya 			boundary = decodesize > align ? decodesize : align;
1205da7a65cSaugustss 
1214eee1111Shaya 			search_addr = (substart & ~(boundary - 1)) + addr;
1224eee1111Shaya 
1235da7a65cSaugustss 			if (search_addr < substart) {
1244eee1111Shaya 				search_addr += boundary;
1255da7a65cSaugustss 			}
12607fb02cbSchristos 
1271a159507Saugustss 			val = 1;
12807fb02cbSchristos 			for (; search_addr + size <= subend;
129de5dacd8Shaya 			     search_addr += boundary) {
130de5dacd8Shaya 				val = extent_alloc_subregion(rbt->rb_ext,
131de5dacd8Shaya 				    search_addr, search_addr + size, size,
13264f78183Snakayama 				    align, 0, exflags, &result);
133f89b0f51Shaya 				DPRINTF(("rbus: trying [%lx:%lx] %lx\n",
134f8a82ee6Sscw 				    (u_long)search_addr,
135f8a82ee6Sscw 				    (u_long)search_addr + size, (u_long)align));
1365da7a65cSaugustss 				if (val == 0) {
1374eee1111Shaya 					break;
1384eee1111Shaya 				}
1395da7a65cSaugustss 			}
140de5dacd8Shaya 			if (val != 0) {
141de5dacd8Shaya 				/* no space found */
142f89b0f51Shaya 				DPRINTF(("rbus: no space found\n"));
1434eee1111Shaya 				return 1;
1444eee1111Shaya 			}
1455da7a65cSaugustss 		}
1464eee1111Shaya 
1474eee1111Shaya 		if(md_space_map(rbt->rb_bt, result, size, flags, bshp)) {
1484eee1111Shaya 			/* map failed */
1494eee1111Shaya 			extent_free(rbt->rb_ext, result, size, exflags);
1504eee1111Shaya 			return 1;
1514eee1111Shaya 		}
1524eee1111Shaya 
1535da7a65cSaugustss 		if (addrp != NULL) {
1544eee1111Shaya 			*addrp = result + rbt->rb_offset;
1554eee1111Shaya 		}
1565da7a65cSaugustss 		return 0;
1575da7a65cSaugustss 	} else {
1585da7a65cSaugustss 		/* error!! */
159f89b0f51Shaya 		DPRINTF(("rbus: no rbus type\n"));
1605da7a65cSaugustss 		return 1;
1615da7a65cSaugustss 	}
1625da7a65cSaugustss }
1635da7a65cSaugustss 
1645da7a65cSaugustss 
1655da7a65cSaugustss 
1664eee1111Shaya 
1674eee1111Shaya 
1684eee1111Shaya int
rbus_space_free(rbus_tag_t rbt,bus_space_handle_t bsh,bus_size_t size,bus_addr_t * addrp)169454af1c0Sdsl rbus_space_free(rbus_tag_t rbt, bus_space_handle_t bsh, bus_size_t size, bus_addr_t *addrp)
1704eee1111Shaya {
1714eee1111Shaya 	int exflags = EX_FAST | EX_NOWAIT;
1724eee1111Shaya 	bus_addr_t addr;
1734eee1111Shaya 	int status = 1;
1744eee1111Shaya 
1754eee1111Shaya 	if (rbt->rb_flags == RBUS_SPACE_ASK_PARENT) {
1764eee1111Shaya 		status = rbus_space_free(rbt->rb_parent, bsh, size, &addr);
1774eee1111Shaya 	} else if (rbt->rb_flags == RBUS_SPACE_SHARE ||
1784eee1111Shaya 	    rbt->rb_flags == RBUS_SPACE_DEDICATE) {
1794eee1111Shaya 		md_space_unmap(rbt->rb_bt, bsh, size, &addr);
1804eee1111Shaya 
1814eee1111Shaya 		extent_free(rbt->rb_ext, addr, size, exflags);
1824eee1111Shaya 
1834eee1111Shaya 		status = 0;
1844eee1111Shaya 	} else {
1854eee1111Shaya 		/* error. INVALID rbustag */
1864eee1111Shaya 		status = 1;
1874eee1111Shaya 	}
1883456153aSchristos 	if (status == 0 && addrp != NULL) {
1894eee1111Shaya 		*addrp = addr;
1905da7a65cSaugustss 	}
1914eee1111Shaya 	return status;
1924eee1111Shaya }
1934eee1111Shaya 
1944eee1111Shaya 
1954eee1111Shaya 
1964eee1111Shaya /*
1974eee1111Shaya  * static rbus_tag_t
1984eee1111Shaya  * rbus_new_body(bus_space_tag_t bt, rbus_tag_t parent,
1994eee1111Shaya  *               struct extent *ex, bus_addr_t start, bus_size_t end,
2004eee1111Shaya  *               bus_addr_t offset, int flags)
2014eee1111Shaya  *
2024eee1111Shaya  */
2034eee1111Shaya static rbus_tag_t
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)20482357f6dSdsl 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)
2054eee1111Shaya {
2064eee1111Shaya 	rbus_tag_t rb;
2074eee1111Shaya 
2084eee1111Shaya 	/* sanity check */
2094eee1111Shaya 	if (parent != NULL) {
2104eee1111Shaya 		if (start < parent->rb_start || end > parent->rb_end) {
211de5dacd8Shaya 			/*
212de5dacd8Shaya 			 * out of range: [start, size] should be
213c0177219Swiz 			 * contained in parent space
214de5dacd8Shaya 			 */
2154eee1111Shaya 			return 0;
2164eee1111Shaya 			/* Should I invoke panic? */
2174eee1111Shaya 		}
2184eee1111Shaya 	}
2194eee1111Shaya 
2205da7a65cSaugustss 	if (NULL == (rb = (rbus_tag_t)malloc(sizeof(struct rbustag), M_DEVBUF,
2215da7a65cSaugustss 	    M_NOWAIT))) {
2224eee1111Shaya 		panic("no memory for rbus instance");
2235da7a65cSaugustss 	}
2244eee1111Shaya 
2254eee1111Shaya 	rb->rb_bt = bt;
2264eee1111Shaya 	rb->rb_parent = parent;
2274eee1111Shaya 	rb->rb_start = start;
2284eee1111Shaya 	rb->rb_end = end;
2294eee1111Shaya 	rb->rb_offset = offset;
2304eee1111Shaya 	rb->rb_flags = flags;
2314eee1111Shaya 	rb->rb_ext = ex;
2324eee1111Shaya 
233f8a82ee6Sscw 	DPRINTF(("rbus_new_body: [%lx, %lx] type %s name [%s]\n",
234f8a82ee6Sscw 	    (u_long)start, (u_long)end,
2354eee1111Shaya 	    flags == RBUS_SPACE_SHARE ? "share" :
2364eee1111Shaya 	    flags == RBUS_SPACE_DEDICATE ? "dedicated" :
2374eee1111Shaya 	    flags == RBUS_SPACE_ASK_PARENT ? "parent" : "invalid",
2384eee1111Shaya 	    ex != NULL ? ex->ex_name : "noname"));
2394eee1111Shaya 
2404eee1111Shaya 	return rb;
2414eee1111Shaya }
2424eee1111Shaya 
2434eee1111Shaya 
2444eee1111Shaya 
2454eee1111Shaya /*
2464eee1111Shaya  * rbus_tag_t rbus_new(rbus_tag_t parent, bus_addr_t start, bus_size_t
2474eee1111Shaya  *                     size, bus_addr_t offset, int flags)
2484eee1111Shaya  *
2494eee1111Shaya  *  This function makes a new child rbus instance.
2504eee1111Shaya  */
2514eee1111Shaya rbus_tag_t
rbus_new(rbus_tag_t parent,bus_addr_t start,bus_size_t size,bus_addr_t offset,int flags)252454af1c0Sdsl rbus_new(rbus_tag_t parent, bus_addr_t start, bus_size_t size, bus_addr_t offset, int flags)
2534eee1111Shaya {
2544eee1111Shaya 	rbus_tag_t rb;
2554eee1111Shaya 	struct extent *ex = NULL;
2564eee1111Shaya 	bus_addr_t end = start + size;
2574eee1111Shaya 
2584eee1111Shaya 	if (flags == RBUS_SPACE_SHARE) {
2594eee1111Shaya 		ex = parent->rb_ext;
2604eee1111Shaya 	} else if (flags == RBUS_SPACE_DEDICATE) {
261*89c9828dSpara 		if (NULL == (ex = extent_create("rbus", start, end,
262de5dacd8Shaya 		    NULL, 0, EX_NOCOALESCE|EX_NOWAIT))) {
2634eee1111Shaya 			return NULL;
2644eee1111Shaya 		}
2654eee1111Shaya 	} else if (flags == RBUS_SPACE_ASK_PARENT) {
2664eee1111Shaya 		ex = NULL;
2674eee1111Shaya 	} else {
2684eee1111Shaya 		/* Invalid flag */
2695da7a65cSaugustss 		return 0;
2704eee1111Shaya 	}
2714eee1111Shaya 
2724eee1111Shaya 	rb = rbus_new_body(parent->rb_bt, parent, ex, start, start + size,
2734eee1111Shaya 	    offset, flags);
2744eee1111Shaya 
2755da7a65cSaugustss 	if ((rb == NULL) && (flags == RBUS_SPACE_DEDICATE)) {
2764eee1111Shaya 		extent_destroy(ex);
2775da7a65cSaugustss 	}
2784eee1111Shaya 
2794eee1111Shaya 	return rb;
2804eee1111Shaya }
2814eee1111Shaya 
2824eee1111Shaya 
2834eee1111Shaya 
2844eee1111Shaya 
2854eee1111Shaya /*
2864eee1111Shaya  * rbus_tag_t rbus_new_root_delegate(bus_space_tag, bus_addr_t,
2874eee1111Shaya  *                                   bus_size_t, bus_addr_t offset)
2884eee1111Shaya  *
2894eee1111Shaya  *  This function makes a root rbus instance.
2904eee1111Shaya  */
2914eee1111Shaya rbus_tag_t
rbus_new_root_delegate(bus_space_tag_t bt,bus_addr_t start,bus_size_t size,bus_addr_t offset)292454af1c0Sdsl rbus_new_root_delegate(bus_space_tag_t bt, bus_addr_t start, bus_size_t size, bus_addr_t offset)
2934eee1111Shaya {
2944eee1111Shaya 	rbus_tag_t rb;
2954eee1111Shaya 	struct extent *ex;
2964eee1111Shaya 
297de5dacd8Shaya 	if (NULL == (ex = extent_create("rbus root", start, start + size,
298*89c9828dSpara 	    NULL, 0, EX_NOCOALESCE|EX_NOWAIT))) {
2994eee1111Shaya 		return NULL;
3005da7a65cSaugustss 	}
3014eee1111Shaya 
3024eee1111Shaya 	rb = rbus_new_body(bt, NULL, ex, start, start + size, offset,
3034eee1111Shaya 	    RBUS_SPACE_DEDICATE);
3044eee1111Shaya 
3055da7a65cSaugustss 	if (rb == NULL) {
3064eee1111Shaya 		extent_destroy(ex);
3075da7a65cSaugustss 	}
3084eee1111Shaya 
3094eee1111Shaya 	return rb;
3104eee1111Shaya }
3114eee1111Shaya 
3124eee1111Shaya 
3134eee1111Shaya 
3144eee1111Shaya /*
3154eee1111Shaya  * rbus_tag_t rbus_new_root_share(bus_space_tag, struct extent *,
3164eee1111Shaya  *                                 bus_addr_t, bus_size_t, bus_addr_t offset)
3174eee1111Shaya  *
3184eee1111Shaya  *  This function makes a root rbus instance.
3194eee1111Shaya  */
3204eee1111Shaya rbus_tag_t
rbus_new_root_share(bus_space_tag_t bt,struct extent * ex,bus_addr_t start,bus_size_t size,bus_addr_t offset)321454af1c0Sdsl rbus_new_root_share(bus_space_tag_t bt, struct extent *ex, bus_addr_t start, bus_size_t size, bus_addr_t offset)
3224eee1111Shaya {
3234eee1111Shaya 	/* sanity check */
3244eee1111Shaya 	if (start < ex->ex_start || start + size > ex->ex_end) {
325de5dacd8Shaya 		/*
326c0177219Swiz 		 * out of range: [start, size] should be contained in
327de5dacd8Shaya 		 * parent space
328de5dacd8Shaya 		 */
3294eee1111Shaya 		return 0;
3304eee1111Shaya 		/* Should I invoke panic? */
3314eee1111Shaya 	}
3324eee1111Shaya 
3334eee1111Shaya 	return rbus_new_body(bt, NULL, ex, start, start + size, offset,
3344eee1111Shaya 	    RBUS_SPACE_SHARE);
3354eee1111Shaya }
3364eee1111Shaya 
3374eee1111Shaya 
3384eee1111Shaya 
3394eee1111Shaya 
3404eee1111Shaya 
3414eee1111Shaya /*
3424eee1111Shaya  * int rbus_delete (rbus_tag_t rb)
3434eee1111Shaya  *
3444eee1111Shaya  *   This function deletes the rbus structure pointed in the argument.
3454eee1111Shaya  */
3464eee1111Shaya int
rbus_delete(rbus_tag_t rb)347454af1c0Sdsl rbus_delete(rbus_tag_t rb)
3484eee1111Shaya {
3494eee1111Shaya 	DPRINTF(("rbus_delete called [%s]\n",
3504eee1111Shaya 	    rb->rb_ext != NULL ? rb->rb_ext->ex_name : "noname"));
3515da7a65cSaugustss 	if (rb->rb_flags == RBUS_SPACE_DEDICATE) {
3524eee1111Shaya 		extent_destroy(rb->rb_ext);
3535da7a65cSaugustss 	}
3544eee1111Shaya 
3554eee1111Shaya 	free(rb, M_DEVBUF);
3564eee1111Shaya 
3574eee1111Shaya 	return 0;
3584eee1111Shaya }
359