xref: /freebsd/sys/sys/rman.h (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
1 /*-
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright 1998 Massachusetts Institute of Technology
5  *
6  * Permission to use, copy, modify, and distribute this software and
7  * its documentation for any purpose and without fee is hereby
8  * granted, provided that both the above copyright notice and this
9  * permission notice appear in all copies, that both the above
10  * copyright notice and this permission notice appear in all
11  * supporting documentation, and that the name of M.I.T. not be used
12  * in advertising or publicity pertaining to distribution of the
13  * software without specific, written prior permission.  M.I.T. makes
14  * no representations about the suitability of this software for any
15  * purpose.  It is provided "as is" without express or implied
16  * warranty.
17  *
18  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
19  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
22  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #ifndef _SYS_RMAN_H_
35 #define	_SYS_RMAN_H_	1
36 
37 #ifndef	_KERNEL
38 #include <sys/queue.h>
39 #else
40 #include <machine/_bus.h>
41 #include <machine/resource.h>
42 #endif
43 
44 #define	RF_ALLOCATED	0x0001	/* resource has been reserved */
45 #define	RF_ACTIVE	0x0002	/* resource allocation has been activated */
46 #define	RF_SHAREABLE	0x0004	/* resource permits contemporaneous sharing */
47 #define	RF_SPARE1	0x0008
48 #define	RF_SPARE2	0x0010
49 #define	RF_FIRSTSHARE	0x0020	/* first in sharing list */
50 #define	RF_PREFETCHABLE	0x0040	/* resource is prefetchable */
51 #define	RF_OPTIONAL	0x0080	/* for bus_alloc_resources() */
52 #define	RF_UNMAPPED	0x0100	/* don't map resource when activating */
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 alignment 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 #define	RM_MAX_END	(~(rman_res_t)0)
68 
69 #define	RMAN_IS_DEFAULT_RANGE(s,e)	((s) == 0 && (e) == RM_MAX_END)
70 
71 /*
72  * Userspace-exported structures.
73  */
74 struct u_resource {
75 	uintptr_t	r_handle;		/* resource uniquifier */
76 	uintptr_t	r_parent;		/* parent rman */
77 	uintptr_t	r_device;		/* device owning this resource */
78 	char		r_devname[RM_TEXTLEN];	/* device name XXX obsolete */
79 
80 	rman_res_t	r_start;		/* offset in resource space */
81 	rman_res_t	r_size;			/* size in resource space */
82 	u_int		r_flags;		/* RF_* flags */
83 };
84 
85 struct u_rman {
86 	uintptr_t	rm_handle;		/* rman uniquifier */
87 	char		rm_descr[RM_TEXTLEN];	/* rman description */
88 
89 	rman_res_t	rm_start;		/* base of managed region */
90 	rman_res_t	rm_size;		/* size of managed region */
91 	enum rman_type	rm_type;		/* region type */
92 };
93 
94 #ifdef _KERNEL
95 
96 /*
97  * The public (kernel) view of struct resource
98  *
99  * NB: Changing the offset/size/type of existing fields in struct resource
100  * NB: breaks the device driver ABI and is strongly FORBIDDEN.
101  * NB: Appending new fields is probably just misguided.
102  */
103 
104 struct resource {
105 	struct resource_i	*__r_i;
106 	bus_space_tag_t		r_bustag; /* bus_space tag */
107 	bus_space_handle_t	r_bushandle;	/* bus_space handle */
108 };
109 
110 struct resource_i;
111 struct resource_map;
112 
113 TAILQ_HEAD(resource_head, resource_i);
114 
115 struct rman {
116 	struct	resource_head 	rm_list;
117 	struct	mtx *rm_mtx;	/* mutex used to protect rm_list */
118 	TAILQ_ENTRY(rman)	rm_link; /* link in list of all rmans */
119 	rman_res_t	rm_start;	/* index of globally first entry */
120 	rman_res_t	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 };
124 TAILQ_HEAD(rman_head, rman);
125 
126 int	rman_activate_resource(struct resource *r);
127 int	rman_adjust_resource(struct resource *r, rman_res_t start, rman_res_t end);
128 int	rman_first_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end);
129 bus_space_handle_t rman_get_bushandle(struct resource *);
130 bus_space_tag_t rman_get_bustag(struct resource *);
131 rman_res_t	rman_get_end(struct resource *);
132 device_t rman_get_device(struct resource *);
133 u_int	rman_get_flags(struct resource *);
134 void	rman_get_mapping(struct resource *, struct resource_map *);
135 int	rman_get_rid(struct resource *);
136 rman_res_t	rman_get_size(struct resource *);
137 rman_res_t	rman_get_start(struct resource *);
138 void   *rman_get_virtual(struct resource *);
139 int	rman_deactivate_resource(struct resource *r);
140 int	rman_fini(struct rman *rm);
141 int	rman_init(struct rman *rm);
142 int	rman_init_from_resource(struct rman *rm, struct resource *r);
143 int	rman_last_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end);
144 uint32_t rman_make_alignment_flags(uint32_t size);
145 int	rman_manage_region(struct rman *rm, rman_res_t start, rman_res_t end);
146 int	rman_is_region_manager(struct resource *r, struct rman *rm);
147 int	rman_release_resource(struct resource *r);
148 struct resource *rman_reserve_resource(struct rman *rm, rman_res_t start,
149 					rman_res_t end, rman_res_t count,
150 					u_int flags, device_t dev);
151 struct resource *rman_reserve_resource_bound(struct rman *rm, rman_res_t start,
152 					rman_res_t end, rman_res_t count, rman_res_t bound,
153 					u_int flags, device_t dev);
154 void	rman_set_bushandle(struct resource *_r, bus_space_handle_t _h);
155 void	rman_set_bustag(struct resource *_r, bus_space_tag_t _t);
156 void	rman_set_device(struct resource *_r, device_t _dev);
157 void	rman_set_end(struct resource *_r, rman_res_t _end);
158 void	rman_set_mapping(struct resource *, struct resource_map *);
159 void	rman_set_rid(struct resource *_r, int _rid);
160 void	rman_set_start(struct resource *_r, rman_res_t _start);
161 void	rman_set_virtual(struct resource *_r, void *_v);
162 
163 extern	struct rman_head rman_head;
164 
165 #endif /* _KERNEL */
166 
167 #endif /* !_SYS_RMAN_H_ */
168