rbus.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* $OpenBSD: rbus.c,v 1.16 2010/09/22 02:28:37 jsg Exp $ */
  2. /* $NetBSD: rbus.c,v 1.3 1999/11/06 06:20:53 soren Exp $ */
  3. /*
  4. * Copyright (c) 1999
  5. * HAYAKAWA Koichi. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  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.
  15. *
  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.
  26. */
  27. #include <sys/param.h>
  28. #include <sys/systm.h>
  29. #include <sys/device.h>
  30. #include <sys/malloc.h>
  31. #include <sys/extent.h>
  32. #include <machine/bus.h>
  33. #include <dev/cardbus/rbus.h>
  34. /* #define RBUS_DEBUG */
  35. #if defined RBUS_DEBUG
  36. #define STATIC
  37. #define DPRINTF(a) printf a
  38. #else
  39. #ifdef DDB
  40. #define STATIC
  41. #else
  42. #define STATIC static
  43. #endif
  44. #define DPRINTF(a)
  45. #endif
  46. int
  47. rbus_space_alloc(rbus_tag_t rbt, bus_addr_t addr, bus_size_t size,
  48. bus_addr_t mask, bus_addr_t align, int flags, bus_addr_t *addrp,
  49. bus_space_handle_t *bshp)
  50. {
  51. return (rbus_space_alloc_subregion(rbt, rbt->rb_start, rbt->rb_end,
  52. addr, size, mask, align, flags, addrp, bshp));
  53. }
  54. int
  55. rbus_space_alloc_subregion(rbus_tag_t rbt, bus_addr_t substart,
  56. bus_addr_t subend, bus_addr_t addr, bus_size_t size,
  57. bus_addr_t mask, bus_addr_t align, int flags, bus_addr_t *addrp,
  58. bus_space_handle_t *bshp)
  59. {
  60. bus_addr_t decodesize = mask + 1;
  61. bus_addr_t boundary, search_addr;
  62. int val;
  63. u_long result;
  64. int exflags = EX_FAST | EX_NOWAIT | EX_MALLOCOK;
  65. DPRINTF(("rbus_space_alloc: addr %lx, size %lx, mask %lx, align %lx\n",
  66. (u_long)addr, (u_long)size, (u_long)mask, (u_long)align));
  67. if (mask == 0) {
  68. /* FULL Decode */
  69. decodesize = 0;
  70. }
  71. if (rbt->rb_flags == RBUS_SPACE_SHARE ||
  72. rbt->rb_flags == RBUS_SPACE_DEDICATE) {
  73. /* rbt has its own sh_extent */
  74. /* sanity check: the subregion [substart, subend] should be
  75. smaller than the region included in sh_extent */
  76. if (substart < rbt->rb_ext->ex_start ||
  77. subend > rbt->rb_ext->ex_end) {
  78. DPRINTF(("rbus: out of range\n"));
  79. return (1);
  80. }
  81. if (decodesize == align) {
  82. if (extent_alloc_subregion(rbt->rb_ext, substart,
  83. subend, size, align, 0, 0, exflags, &result))
  84. return (1);
  85. } else if (decodesize == 0) {
  86. /* maybe, the register is overflowed. */
  87. if (extent_alloc_subregion(rbt->rb_ext, addr,
  88. addr + size, size, 1, 0, 0, exflags, &result))
  89. return (1);
  90. } else {
  91. boundary = decodesize > align ? decodesize : align;
  92. search_addr = (substart & ~(boundary - 1)) + addr;
  93. if (search_addr < substart)
  94. search_addr += boundary;
  95. val = 1;
  96. for (; search_addr + size <= subend;
  97. search_addr += boundary) {
  98. val = extent_alloc_subregion(
  99. rbt->rb_ext,search_addr,
  100. search_addr + size, size, align, 0, 0,
  101. exflags, &result);
  102. DPRINTF(("rbus: trying [%lx:%lx] %lx\n",
  103. (u_long)search_addr,
  104. (u_long)search_addr + size,
  105. (u_long)align));
  106. if (val == 0)
  107. break;
  108. }
  109. if (val != 0) {
  110. /* no space found */
  111. DPRINTF(("rbus: no space found\n"));
  112. return (1);
  113. }
  114. }
  115. if (md_space_map(rbt, result, size, flags, bshp)) {
  116. /* map failed */
  117. extent_free(rbt->rb_ext, result, size, exflags);
  118. return (1);
  119. }
  120. if (addrp != NULL)
  121. *addrp = result;
  122. return (0);
  123. } else {
  124. /* error!! */
  125. DPRINTF(("rbus: no rbus type\n"));
  126. return (1);
  127. }
  128. }
  129. int
  130. rbus_space_free(rbus_tag_t rbt, bus_space_handle_t bsh, bus_size_t size,
  131. bus_addr_t *addrp)
  132. {
  133. int exflags = EX_FAST | EX_NOWAIT;
  134. bus_addr_t addr;
  135. int status = 1;
  136. if (rbt->rb_flags == RBUS_SPACE_SHARE ||
  137. rbt->rb_flags == RBUS_SPACE_DEDICATE) {
  138. md_space_unmap(rbt, bsh, size, &addr);
  139. extent_free(rbt->rb_ext, addr, size, exflags);
  140. status = 0;
  141. } else {
  142. /* error. INVALID rbustag */
  143. status = 1;
  144. }
  145. if (addrp != NULL)
  146. *addrp = addr;
  147. return (status);
  148. }
  149. /*
  150. * rbus_tag_t
  151. * rbus_new_body(bus_space_tag_t bt,
  152. * struct extent *ex, bus_addr_t start, bus_size_t end,
  153. * int flags)
  154. *
  155. */
  156. rbus_tag_t
  157. rbus_new_body(bus_space_tag_t bt, struct extent *ex,
  158. bus_addr_t start, bus_addr_t end, int flags)
  159. {
  160. rbus_tag_t rb;
  161. if ((rb = (rbus_tag_t)malloc(sizeof(struct rbustag), M_DEVBUF,
  162. M_NOWAIT)) == NULL) {
  163. panic("no memory for rbus instance");
  164. }
  165. rb->rb_bt = bt;
  166. rb->rb_start = start;
  167. rb->rb_end = end;
  168. rb->rb_flags = flags;
  169. rb->rb_ext = ex;
  170. DPRINTF(("rbus_new_body: [%lx, %lx] type %s name [%s]\n",
  171. (u_long)start, (u_long)end,
  172. flags == RBUS_SPACE_SHARE ? "share" :
  173. flags == RBUS_SPACE_DEDICATE ? "dedicated" : "invalid",
  174. ex != NULL ? ex->ex_name : "noname"));
  175. return (rb);
  176. }
  177. /*
  178. * rbus_tag_t rbus_new_root_delegate(bus_space_tag, bus_addr_t,
  179. * bus_size_t)
  180. *
  181. * This function makes a root rbus instance.
  182. */
  183. rbus_tag_t
  184. rbus_new_root_delegate(bus_space_tag_t bt, bus_addr_t start, bus_size_t size)
  185. {
  186. rbus_tag_t rb;
  187. struct extent *ex;
  188. if ((ex = extent_create("rbus root", start, start + size, M_DEVBUF,
  189. NULL, 0, EX_NOCOALESCE|EX_NOWAIT)) == NULL)
  190. return (NULL);
  191. rb = rbus_new_body(bt, ex, start, start + size,
  192. RBUS_SPACE_DEDICATE);
  193. if (rb == NULL)
  194. extent_destroy(ex);
  195. return (rb);
  196. }
  197. /*
  198. * rbus_tag_t rbus_new_root_share(bus_space_tag, struct extent *,
  199. * bus_addr_t, bus_size_t)
  200. *
  201. * This function makes a root rbus instance.
  202. */
  203. rbus_tag_t
  204. rbus_new_root_share(bus_space_tag_t bt, struct extent *ex, bus_addr_t start,
  205. bus_size_t size)
  206. {
  207. /* sanity check */
  208. if (start < ex->ex_start || start + size > ex->ex_end) {
  209. /* out of range: [start, size] should be contained in
  210. * parent space
  211. */
  212. return (0);
  213. /* Should I invoke panic? */
  214. }
  215. return (rbus_new_body(bt, ex, start, start + size,
  216. RBUS_SPACE_SHARE));
  217. }