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