1139825Simp/*-
240711Swollman * Copyright 1998 Massachusetts Institute of Technology
340711Swollman *
440711Swollman * Permission to use, copy, modify, and distribute this software and
540711Swollman * its documentation for any purpose and without fee is hereby
640711Swollman * granted, provided that both the above copyright notice and this
740711Swollman * permission notice appear in all copies, that both the above
840711Swollman * copyright notice and this permission notice appear in all
940711Swollman * supporting documentation, and that the name of M.I.T. not be used
1040711Swollman * in advertising or publicity pertaining to distribution of the
1140711Swollman * software without specific, written prior permission.  M.I.T. makes
1240711Swollman * no representations about the suitability of this software for any
1340711Swollman * purpose.  It is provided "as is" without express or implied
1440711Swollman * warranty.
1540711Swollman *
1640711Swollman * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
1740711Swollman * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
1840711Swollman * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
1940711Swollman * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
2040711Swollman * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2140711Swollman * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2240711Swollman * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
2340711Swollman * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
2440711Swollman * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2540711Swollman * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2640711Swollman * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2740711Swollman * SUCH DAMAGE.
2840711Swollman *
2950477Speter * $FreeBSD$
3040711Swollman */
3140711Swollman
3240711Swollman#ifndef _SYS_RMAN_H_
3340711Swollman#define	_SYS_RMAN_H_	1
3440711Swollman
3555205Speter#ifndef	_KERNEL
3640711Swollman#include <sys/queue.h>
37129887Simp#else
38159536Simp#include <machine/_bus.h>
39129887Simp#include <machine/resource.h>
4055205Speter#endif
4140711Swollman
4268522Smsmith#define	RF_ALLOCATED	0x0001	/* resource has been reserved */
4368522Smsmith#define	RF_ACTIVE	0x0002	/* resource allocation has been activated */
4468522Smsmith#define	RF_SHAREABLE	0x0004	/* resource permits contemporaneous sharing */
45268780Struckman#define	RF_SPARE1	0x0008
46268780Struckman#define	RF_SPARE2	0x0010
4768522Smsmith#define	RF_FIRSTSHARE	0x0020	/* first in sharing list */
4882378Sjon#define	RF_PREFETCHABLE	0x0040	/* resource is prefetchable */
49150521Sphk#define	RF_OPTIONAL	0x0080	/* for bus_alloc_resources() */
50300317Sjhb#define	RF_UNMAPPED	0x0100	/* don't map resource when activating */
5168522Smsmith
5268522Smsmith#define	RF_ALIGNMENT_SHIFT	10 /* alignment size bit starts bit 10 */
5368522Smsmith#define	RF_ALIGNMENT_MASK	(0x003F << RF_ALIGNMENT_SHIFT)
54232237Sjhb				/* resource address alignment size bit mask */
5568522Smsmith#define	RF_ALIGNMENT_LOG2(x)	((x) << RF_ALIGNMENT_SHIFT)
5668522Smsmith#define	RF_ALIGNMENT(x)		(((x) & RF_ALIGNMENT_MASK) >> RF_ALIGNMENT_SHIFT)
5768522Smsmith
5868522Smsmithenum	rman_type { RMAN_UNINIT = 0, RMAN_GAUGE, RMAN_ARRAY };
5968522Smsmith
6040711Swollman/*
6168522Smsmith * String length exported to userspace for resource names, etc.
6268522Smsmith */
6368522Smsmith#define RM_TEXTLEN	32
6468522Smsmith
65295833Sjhibbits#define	RM_MAX_END	(~(rman_res_t)0)
66294883Sjhibbits
67295832Sjhibbits#define	RMAN_IS_DEFAULT_RANGE(s,e)	((s) == 0 && (e) == RM_MAX_END)
68295832Sjhibbits
6968522Smsmith/*
7068522Smsmith * Userspace-exported structures.
7168522Smsmith */
7268522Smsmithstruct u_resource {
7368522Smsmith	uintptr_t	r_handle;		/* resource uniquifier */
7468522Smsmith	uintptr_t	r_parent;		/* parent rman */
7568522Smsmith	uintptr_t	r_device;		/* device owning this resource */
7668522Smsmith	char		r_devname[RM_TEXTLEN];	/* device name XXX obsolete */
7768522Smsmith
78294883Sjhibbits	rman_res_t	r_start;		/* offset in resource space */
79294883Sjhibbits	rman_res_t	r_size;			/* size in resource space */
8068522Smsmith	u_int		r_flags;		/* RF_* flags */
8168522Smsmith};
8268522Smsmith
8368522Smsmithstruct u_rman {
8468522Smsmith	uintptr_t	rm_handle;		/* rman uniquifier */
8568522Smsmith	char		rm_descr[RM_TEXTLEN];	/* rman description */
8668522Smsmith
87294883Sjhibbits	rman_res_t	rm_start;		/* base of managed region */
88294885Sjhibbits	rman_res_t	rm_size;		/* size of managed region */
8968522Smsmith	enum rman_type	rm_type;		/* region type */
9068522Smsmith};
9168522Smsmith
9268522Smsmith#ifdef _KERNEL
93150523Sphk
9468522Smsmith/*
95150523Sphk * The public (kernel) view of struct resource
96150523Sphk *
97150523Sphk * NB: Changing the offset/size/type of existing fields in struct resource
98150523Sphk * NB: breaks the device driver ABI and is strongly FORBIDDEN.
99150523Sphk * NB: Appending new fields is probably just misguided.
100150523Sphk */
101150523Sphk
102150523Sphkstruct resource {
103150523Sphk	struct resource_i	*__r_i;
104150523Sphk	bus_space_tag_t		r_bustag; /* bus_space tag */
105150523Sphk	bus_space_handle_t	r_bushandle;	/* bus_space handle */
106150523Sphk};
107150523Sphk
108151037Sphkstruct resource_i;
109300317Sjhbstruct resource_map;
110151037Sphk
111150523SphkTAILQ_HEAD(resource_head, resource_i);
11240711Swollman
11383045Sobrienstruct rman {
11440711Swollman	struct	resource_head 	rm_list;
11571576Sjasone	struct	mtx *rm_mtx;	/* mutex used to protect rm_list */
11660938Sjake	TAILQ_ENTRY(rman)	rm_link; /* link in list of all rmans */
117294883Sjhibbits	rman_res_t	rm_start;	/* index of globally first entry */
118294883Sjhibbits	rman_res_t	rm_end;	/* index of globally last entry */
11940711Swollman	enum	rman_type rm_type; /* what type of resource this is */
12040711Swollman	const	char *rm_descr;	/* text descripion of this resource */
12140711Swollman};
12260938SjakeTAILQ_HEAD(rman_head, rman);
12340711Swollman
12440711Swollmanint	rman_activate_resource(struct resource *r);
125294883Sjhibbitsint	rman_adjust_resource(struct resource *r, rman_res_t start, rman_res_t end);
126294883Sjhibbitsint	rman_first_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end);
127137492Simpbus_space_handle_t rman_get_bushandle(struct resource *);
128137492Simpbus_space_tag_t rman_get_bustag(struct resource *);
129294883Sjhibbitsrman_res_t	rman_get_end(struct resource *);
130299108Sngiestruct device *rman_get_device(struct resource *);
131137492Simpu_int	rman_get_flags(struct resource *);
132300317Sjhbvoid	rman_get_mapping(struct resource *, struct resource_map *);
133137492Simpint	rman_get_rid(struct resource *);
134294883Sjhibbitsrman_res_t	rman_get_size(struct resource *);
135294883Sjhibbitsrman_res_t	rman_get_start(struct resource *);
136137492Simpvoid   *rman_get_virtual(struct resource *);
13740711Swollmanint	rman_deactivate_resource(struct resource *r);
13840711Swollmanint	rman_fini(struct rman *rm);
13940711Swollmanint	rman_init(struct rman *rm);
140159536Simpint	rman_init_from_resource(struct rman *rm, struct resource *r);
141294883Sjhibbitsint	rman_last_free_region(struct rman *rm, rman_res_t *start, rman_res_t *end);
142137492Simpuint32_t rman_make_alignment_flags(uint32_t size);
143294883Sjhibbitsint	rman_manage_region(struct rman *rm, rman_res_t start, rman_res_t end);
144150547Sphkint	rman_is_region_manager(struct resource *r, struct rman *rm);
14540711Swollmanint	rman_release_resource(struct resource *r);
146294883Sjhibbitsstruct resource *rman_reserve_resource(struct rman *rm, rman_res_t start,
147294883Sjhibbits					rman_res_t end, rman_res_t count,
148299108Sngie					u_int flags, struct device *dev);
149294883Sjhibbitsstruct resource *rman_reserve_resource_bound(struct rman *rm, rman_res_t start,
150294883Sjhibbits					rman_res_t end, rman_res_t count, rman_res_t bound,
151299108Sngie					u_int flags, struct device *dev);
152137496Simpvoid	rman_set_bushandle(struct resource *_r, bus_space_handle_t _h);
153107296Simpvoid	rman_set_bustag(struct resource *_r, bus_space_tag_t _t);
154299108Sngievoid	rman_set_device(struct resource *_r, struct device *_dev);
155294883Sjhibbitsvoid	rman_set_end(struct resource *_r, rman_res_t _end);
156300317Sjhbvoid	rman_set_mapping(struct resource *, struct resource_map *);
157107296Simpvoid	rman_set_rid(struct resource *_r, int _rid);
158294883Sjhibbitsvoid	rman_set_start(struct resource *_r, rman_res_t _start);
159137492Simpvoid	rman_set_virtual(struct resource *_r, void *_v);
16040711Swollman
16140711Swollmanextern	struct rman_head rman_head;
162150698Sphk
16355205Speter#endif /* _KERNEL */
16440711Swollman
16540711Swollman#endif /* !_SYS_RMAN_H_ */
166