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 */
45269383Struckman#define	RF_SPARE1	0x0008
46269383Struckman#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() */
5068522Smsmith
5168522Smsmith#define	RF_ALIGNMENT_SHIFT	10 /* alignment size bit starts bit 10 */
5268522Smsmith#define	RF_ALIGNMENT_MASK	(0x003F << RF_ALIGNMENT_SHIFT)
53232237Sjhb				/* resource address alignment size bit mask */
5468522Smsmith#define	RF_ALIGNMENT_LOG2(x)	((x) << RF_ALIGNMENT_SHIFT)
5568522Smsmith#define	RF_ALIGNMENT(x)		(((x) & RF_ALIGNMENT_MASK) >> RF_ALIGNMENT_SHIFT)
5668522Smsmith
5768522Smsmithenum	rman_type { RMAN_UNINIT = 0, RMAN_GAUGE, RMAN_ARRAY };
5868522Smsmith
5940711Swollman/*
6068522Smsmith * String length exported to userspace for resource names, etc.
6168522Smsmith */
6268522Smsmith#define RM_TEXTLEN	32
6368522Smsmith
6468522Smsmith/*
6568522Smsmith * Userspace-exported structures.
6668522Smsmith */
6768522Smsmithstruct u_resource {
6868522Smsmith	uintptr_t	r_handle;		/* resource uniquifier */
6968522Smsmith	uintptr_t	r_parent;		/* parent rman */
7068522Smsmith	uintptr_t	r_device;		/* device owning this resource */
7168522Smsmith	char		r_devname[RM_TEXTLEN];	/* device name XXX obsolete */
7268522Smsmith
7368522Smsmith	u_long		r_start;		/* offset in resource space */
7468522Smsmith	u_long		r_size;			/* size in resource space */
7568522Smsmith	u_int		r_flags;		/* RF_* flags */
7668522Smsmith};
7768522Smsmith
7868522Smsmithstruct u_rman {
7968522Smsmith	uintptr_t	rm_handle;		/* rman uniquifier */
8068522Smsmith	char		rm_descr[RM_TEXTLEN];	/* rman description */
8168522Smsmith
8268522Smsmith	u_long		rm_start;		/* base of managed region */
8368522Smsmith	u_long		rm_size;		/* size of managed region */
8468522Smsmith	enum rman_type	rm_type;		/* region type */
8568522Smsmith};
8668522Smsmith
8768522Smsmith#ifdef _KERNEL
88150523Sphk
8968522Smsmith/*
90150523Sphk * The public (kernel) view of struct resource
91150523Sphk *
92150523Sphk * NB: Changing the offset/size/type of existing fields in struct resource
93150523Sphk * NB: breaks the device driver ABI and is strongly FORBIDDEN.
94150523Sphk * NB: Appending new fields is probably just misguided.
95150523Sphk */
96150523Sphk
97150523Sphkstruct resource {
98150523Sphk	struct resource_i	*__r_i;
99150523Sphk	bus_space_tag_t		r_bustag; /* bus_space tag */
100150523Sphk	bus_space_handle_t	r_bushandle;	/* bus_space handle */
101150523Sphk};
102150523Sphk
103151037Sphkstruct resource_i;
104151037Sphk
105150523SphkTAILQ_HEAD(resource_head, resource_i);
10640711Swollman
10783045Sobrienstruct rman {
10840711Swollman	struct	resource_head 	rm_list;
10971576Sjasone	struct	mtx *rm_mtx;	/* mutex used to protect rm_list */
11060938Sjake	TAILQ_ENTRY(rman)	rm_link; /* link in list of all rmans */
11140711Swollman	u_long	rm_start;	/* index of globally first entry */
11240711Swollman	u_long	rm_end;		/* index of globally last entry */
11340711Swollman	enum	rman_type rm_type; /* what type of resource this is */
11440711Swollman	const	char *rm_descr;	/* text descripion of this resource */
11540711Swollman};
11660938SjakeTAILQ_HEAD(rman_head, rman);
11740711Swollman
11840711Swollmanint	rman_activate_resource(struct resource *r);
119221220Sjhbint	rman_adjust_resource(struct resource *r, u_long start, u_long end);
12040711Swollmanint	rman_await_resource(struct resource *r, int pri, int timo);
121221220Sjhbint	rman_first_free_region(struct rman *rm, u_long *start, u_long *end);
122137492Simpbus_space_handle_t rman_get_bushandle(struct resource *);
123137492Simpbus_space_tag_t rman_get_bustag(struct resource *);
124137492Simpu_long	rman_get_end(struct resource *);
125137492Simpstruct device *rman_get_device(struct resource *);
126137492Simpu_int	rman_get_flags(struct resource *);
127137492Simpint	rman_get_rid(struct resource *);
128137492Simpu_long	rman_get_size(struct resource *);
129137492Simpu_long	rman_get_start(struct resource *);
130137492Simpvoid   *rman_get_virtual(struct resource *);
13140711Swollmanint	rman_deactivate_resource(struct resource *r);
13240711Swollmanint	rman_fini(struct rman *rm);
13340711Swollmanint	rman_init(struct rman *rm);
134159536Simpint	rman_init_from_resource(struct rman *rm, struct resource *r);
135221220Sjhbint	rman_last_free_region(struct rman *rm, u_long *start, u_long *end);
136137492Simpuint32_t rman_make_alignment_flags(uint32_t size);
13740711Swollmanint	rman_manage_region(struct rman *rm, u_long start, u_long end);
138150547Sphkint	rman_is_region_manager(struct resource *r, struct rman *rm);
13940711Swollmanint	rman_release_resource(struct resource *r);
14083045Sobrienstruct resource *rman_reserve_resource(struct rman *rm, u_long start,
14140711Swollman					u_long end, u_long count,
14240711Swollman					u_int flags, struct device *dev);
14388372Stmmstruct resource *rman_reserve_resource_bound(struct rman *rm, u_long start,
14488372Stmm					u_long end, u_long count, u_long bound,
14588372Stmm					u_int flags, struct device *dev);
146137496Simpvoid	rman_set_bushandle(struct resource *_r, bus_space_handle_t _h);
147107296Simpvoid	rman_set_bustag(struct resource *_r, bus_space_tag_t _t);
148144932Simpvoid	rman_set_device(struct resource *_r, struct device *_dev);
149137492Simpvoid	rman_set_end(struct resource *_r, u_long _end);
150107296Simpvoid	rman_set_rid(struct resource *_r, int _rid);
151131413Simpvoid	rman_set_start(struct resource *_r, u_long _start);
152137492Simpvoid	rman_set_virtual(struct resource *_r, void *_v);
15340711Swollman
15440711Swollmanextern	struct rman_head rman_head;
155150698Sphk
15655205Speter#endif /* _KERNEL */
15740711Swollman
15840711Swollman#endif /* !_SYS_RMAN_H_ */
159