xref: /dragonfly/sys/sys/rman.h (revision 869748ea0626d3966a0b0e1a8223de70ef050c3d)
1 /*
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose and without fee is hereby
6  * granted, provided that both the above copyright notice and this
7  * permission notice appear in all copies, that both the above
8  * copyright notice and this permission notice appear in all
9  * supporting documentation, and that the name of M.I.T. not be used
10  * in advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.  M.I.T. makes
12  * no representations about the suitability of this software for any
13  * purpose.  It is provided "as is" without express or implied
14  * warranty.
15  *
16  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/sys/sys/rman.h,v 1.5.2.1 2001/06/05 08:06:07 imp Exp $
30  */
31 
32 #ifndef _SYS_RMAN_H_
33 #define   _SYS_RMAN_H_
34 
35 #ifndef _SYS_TYPES_H_
36 #include <sys/types.h>
37 #endif
38 #ifndef _SYS_QUEUE_H_
39 #include <sys/queue.h>
40 #endif
41 #ifndef _SYS_BUS_H_
42 #include <sys/bus.h>          /* bus_space_tag_t */
43 #endif
44 
45 #define   RF_ALLOCATED        0x0001    /* resource has been reserved */
46 #define   RF_ACTIVE 0x0002    /* resource allocation has been activated */
47 #define   RF_SHAREABLE        0x0004    /* resource permits contemporaneous sharing */
48 #define   RF_TIMESHARE        0x0008    /* resource permits time-division sharing */
49 #define   RF_WANTED 0x0010    /* somebody is waiting for this resource */
50 #define   RF_FIRSTSHARE       0x0020    /* first in sharing list */
51 #define   RF_PREFETCHABLE     0x0040    /* resource is prefetchable */
52 #define   RF_OPTIONAL         0x0080    /* for bus_alloc_resources() */
53 
54 #define   RF_ALIGNMENT_SHIFT  10 /* alignment size bit starts bit 10 */
55 #define   RF_ALIGNMENT_MASK   (0x003F << RF_ALIGNMENT_SHIFT)
56                                         /* resource address alignemnt size bit mask */
57 #define   RF_ALIGNMENT_LOG2(x)          ((x) << RF_ALIGNMENT_SHIFT)
58 #define   RF_ALIGNMENT(x)               (((x) & RF_ALIGNMENT_MASK) >> RF_ALIGNMENT_SHIFT)
59 
60 enum      rman_type { RMAN_UNINIT = 0, RMAN_GAUGE, RMAN_ARRAY };
61 
62 /*
63  * String length exported to userspace for resource names, etc.
64  */
65 #define RM_TEXTLEN  32
66 
67 /*
68  * Userspace-exported structures.
69  */
70 struct u_resource {
71           uintptr_t r_handle;           /* resource uniquifier */
72           uintptr_t r_parent;           /* parent rman */
73           uintptr_t r_device;           /* device owning this resource */
74           char                r_devname[RM_TEXTLEN];        /* device name XXX obsolete */
75 
76           u_long              r_start;            /* offset in resource space */
77           u_long              r_size;                       /* size in resource space */
78           u_int               r_flags;            /* RF_* flags */
79 };
80 
81 struct u_rman {
82           uintptr_t rm_handle;                    /* rman uniquifier */
83           char                rm_descr[RM_TEXTLEN];         /* rman description */
84 
85           u_long              rm_start;           /* base of managed region */
86           u_long              rm_size;            /* size of managed region */
87           enum rman_type      rm_type;            /* region type */
88 };
89 
90 #ifdef _KERNEL
91 /*
92  * We use a linked list rather than a bitmap because we need to be able to
93  * represent potentially huge objects (like all of a processor's physical
94  * address space).  That is also why the indices are defined to have type
95  * `unsigned long' -- that being the largest integral type in Standard C.
96  */
97 TAILQ_HEAD(resource_head, resource);
98 struct    resource {
99           TAILQ_ENTRY(resource)         r_link;
100           LIST_ENTRY(resource)          r_sharelink;
101           LIST_HEAD(, resource)         *r_sharehead;
102           u_long    r_start;  /* index of the first entry in this resource */
103           u_long    r_end;              /* index of the last entry (inclusive) */
104           u_int     r_flags;
105           void      *r_virtual;         /* virtual address of this resource */
106           bus_space_tag_t r_bustag; /* bus_space tag */
107           bus_space_handle_t r_bushandle;         /* bus_space handle */
108           device_t r_dev;               /* device which has allocated this resource */
109           struct    rman *r_rm;         /* resource manager from whence this came */
110           int     r_rid;          /* optional rid for this resource. */
111 };
112 
113 struct lwkt_token;
114 
115 struct    rman {
116           struct    resource_head       rm_list;
117           struct    lwkt_token          *rm_slock; /* mutex used to protect rm_list */
118           TAILQ_ENTRY(rman)   rm_link; /* link in list of all rmans */
119           u_long    rm_start; /* index of globally first entry */
120           u_long    rm_end;             /* index of globally last entry */
121           enum      rman_type rm_type; /* what type of resource this is */
122           const     char *rm_descr;     /* text descripion of this resource */
123           int       rm_cpuid; /* owner cpuid */
124           int       rm_hold;  /* destruction interlock */
125 };
126 
127 int       rman_activate_resource(struct resource *r);
128 #if 0
129 int       rman_await_resource(struct resource *r, int slpflags, int timo);
130 #endif
131 int       rman_deactivate_resource(struct resource *r);
132 int       rman_fini(struct rman *rm);
133 int       rman_init(struct rman *rm, int cpuid);
134 int       rman_manage_region(struct rman *rm, u_long start, u_long end);
135 int       rman_release_resource(struct resource *r);
136 struct    resource *rman_reserve_resource(struct rman *rm, u_long start,
137                                                   u_long end, u_long count,
138                                                   u_int flags, device_t dev);
139 uint32_t rman_make_alignment_flags(size_t size);
140 
141 #define rman_get_start(r)     ((r)->r_start)
142 #define rman_get_end(r)                 ((r)->r_end)
143 #define rman_get_device(r)    ((r)->r_dev)
144 #define rman_set_device(r,dev)          ((r)->r_dev = (dev))
145 #define rman_get_size(r)      ((r)->r_end - (r)->r_start + 1)
146 #define rman_get_flags(r)     ((r)->r_flags)
147 #define   rman_set_virtual(r,v)         ((r)->r_virtual = (v))
148 #define   rman_get_virtual(r) ((r)->r_virtual)
149 #define rman_set_bustag(r,t)  ((r)->r_bustag = (t))
150 #define rman_get_bustag(r)    ((r)->r_bustag)
151 #define rman_set_bushandle(r,h)         ((r)->r_bushandle = (h))
152 #define rman_get_bushandle(r) ((r)->r_bushandle)
153 #define rman_set_rid(r,i)       ((r)->r_rid = (i))
154 #define rman_get_rid(r)         ((r)->r_rid)
155 #define rman_is_region_manager(r,rm) ((r)->r_rm == rm)
156 #define rman_get_cpuid(r)     ((r)->r_rm->rm_cpuid)
157 
158 #endif /* _KERNEL */
159 
160 #endif /* !_SYS_RMAN_H_ */
161