xref: /freebsd/sys/sys/rman.h (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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$
30  */
31 
32 #ifndef _SYS_RMAN_H_
33 #define	_SYS_RMAN_H_	1
34 
35 #ifndef	_KERNEL
36 #include <sys/queue.h>
37 #else
38 #include <machine/_bus.h>
39 #include <machine/resource.h>
40 #endif
41 
42 #define	RF_ALLOCATED	0x0001	/* resource has been reserved */
43 #define	RF_ACTIVE	0x0002	/* resource allocation has been activated */
44 #define	RF_SHAREABLE	0x0004	/* resource permits contemporaneous sharing */
45 #define	RF_SPARE1	0x0008
46 #define	RF_SPARE2	0x0010
47 #define	RF_FIRSTSHARE	0x0020	/* first in sharing list */
48 #define	RF_PREFETCHABLE	0x0040	/* resource is prefetchable */
49 #define	RF_OPTIONAL	0x0080	/* for bus_alloc_resources() */
50 
51 #define	RF_ALIGNMENT_SHIFT	10 /* alignment size bit starts bit 10 */
52 #define	RF_ALIGNMENT_MASK	(0x003F << RF_ALIGNMENT_SHIFT)
53 				/* resource address alignment size bit mask */
54 #define	RF_ALIGNMENT_LOG2(x)	((x) << RF_ALIGNMENT_SHIFT)
55 #define	RF_ALIGNMENT(x)		(((x) & RF_ALIGNMENT_MASK) >> RF_ALIGNMENT_SHIFT)
56 
57 enum	rman_type { RMAN_UNINIT = 0, RMAN_GAUGE, RMAN_ARRAY };
58 
59 /*
60  * String length exported to userspace for resource names, etc.
61  */
62 #define RM_TEXTLEN	32
63 
64 #define	RM_MAX_END	((rman_res_t)~0)
65 
66 /*
67  * Userspace-exported structures.
68  */
69 struct u_resource {
70 	uintptr_t	r_handle;		/* resource uniquifier */
71 	uintptr_t	r_parent;		/* parent rman */
72 	uintptr_t	r_device;		/* device owning this resource */
73 	char		r_devname[RM_TEXTLEN];	/* device name XXX obsolete */
74 
75 	rman_res_t	r_start;		/* offset in resource space */
76 	rman_res_t	r_size;			/* size in resource space */
77 	u_int		r_flags;		/* RF_* flags */
78 };
79 
80 struct u_rman {
81 	uintptr_t	rm_handle;		/* rman uniquifier */
82 	char		rm_descr[RM_TEXTLEN];	/* rman description */
83 
84 	rman_res_t	rm_start;		/* base of managed region */
85 	rman_res_t	rm_size;		/* size of managed region */
86 	enum rman_type	rm_type;		/* region type */
87 };
88 
89 #ifdef _KERNEL
90 
91 /*
92  * The public (kernel) view of struct resource
93  *
94  * NB: Changing the offset/size/type of existing fields in struct resource
95  * NB: breaks the device driver ABI and is strongly FORBIDDEN.
96  * NB: Appending new fields is probably just misguided.
97  */
98 
99 struct resource {
100 	struct resource_i	*__r_i;
101 	bus_space_tag_t		r_bustag; /* bus_space tag */
102 	bus_space_handle_t	r_bushandle;	/* bus_space handle */
103 };
104 
105 struct resource_i;
106 
107 TAILQ_HEAD(resource_head, resource_i);
108 
109 struct rman {
110 	struct	resource_head 	rm_list;
111 	struct	mtx *rm_mtx;	/* mutex used to protect rm_list */
112 	TAILQ_ENTRY(rman)	rm_link; /* link in list of all rmans */
113 	rman_res_t	rm_start;	/* index of globally first entry */
114 	rman_res_t	rm_end;	/* index of globally last entry */
115 	enum	rman_type rm_type; /* what type of resource this is */
116 	const	char *rm_descr;	/* text descripion of this resource */
117 };
118 TAILQ_HEAD(rman_head, rman);
119 
120 int	rman_activate_resource(struct resource *r);
121 int	rman_adjust_resource(struct resource *r, rman_res_t start, rman_res_t end);
122 int	rman_await_resource(struct resource *r, int pri, int timo);
123 int	rman_first_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end);
124 bus_space_handle_t rman_get_bushandle(struct resource *);
125 bus_space_tag_t rman_get_bustag(struct resource *);
126 rman_res_t	rman_get_end(struct resource *);
127 struct device *rman_get_device(struct resource *);
128 u_int	rman_get_flags(struct resource *);
129 int	rman_get_rid(struct resource *);
130 rman_res_t	rman_get_size(struct resource *);
131 rman_res_t	rman_get_start(struct resource *);
132 void   *rman_get_virtual(struct resource *);
133 int	rman_deactivate_resource(struct resource *r);
134 int	rman_fini(struct rman *rm);
135 int	rman_init(struct rman *rm);
136 int	rman_init_from_resource(struct rman *rm, struct resource *r);
137 int	rman_last_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end);
138 uint32_t rman_make_alignment_flags(uint32_t size);
139 int	rman_manage_region(struct rman *rm, rman_res_t start, rman_res_t end);
140 int	rman_is_region_manager(struct resource *r, struct rman *rm);
141 int	rman_release_resource(struct resource *r);
142 struct resource *rman_reserve_resource(struct rman *rm, rman_res_t start,
143 					rman_res_t end, rman_res_t count,
144 					u_int flags, struct device *dev);
145 struct resource *rman_reserve_resource_bound(struct rman *rm, rman_res_t start,
146 					rman_res_t end, rman_res_t count, rman_res_t bound,
147 					u_int flags, struct device *dev);
148 void	rman_set_bushandle(struct resource *_r, bus_space_handle_t _h);
149 void	rman_set_bustag(struct resource *_r, bus_space_tag_t _t);
150 void	rman_set_device(struct resource *_r, struct device *_dev);
151 void	rman_set_end(struct resource *_r, rman_res_t _end);
152 void	rman_set_rid(struct resource *_r, int _rid);
153 void	rman_set_start(struct resource *_r, rman_res_t _start);
154 void	rman_set_virtual(struct resource *_r, void *_v);
155 
156 extern	struct rman_head rman_head;
157 
158 #endif /* _KERNEL */
159 
160 #endif /* !_SYS_RMAN_H_ */
161