rman.h revision 83045
140711Swollman/*
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: head/sys/sys/rman.h 83045 2001-09-05 01:22:14Z obrien $
3040711Swollman */
3140711Swollman
3240711Swollman#ifndef _SYS_RMAN_H_
3340711Swollman#define	_SYS_RMAN_H_	1
3440711Swollman
3555205Speter#ifndef	_KERNEL
3640711Swollman#include <sys/queue.h>
3755205Speter#endif
3840711Swollman
3968522Smsmith#define	RF_ALLOCATED	0x0001	/* resource has been reserved */
4068522Smsmith#define	RF_ACTIVE	0x0002	/* resource allocation has been activated */
4168522Smsmith#define	RF_SHAREABLE	0x0004	/* resource permits contemporaneous sharing */
4268522Smsmith#define	RF_TIMESHARE	0x0008	/* resource permits time-division sharing */
4368522Smsmith#define	RF_WANTED	0x0010	/* somebody is waiting for this resource */
4468522Smsmith#define	RF_FIRSTSHARE	0x0020	/* first in sharing list */
4582378Sjon#define	RF_PREFETCHABLE	0x0040	/* resource is prefetchable */
4668522Smsmith
4768522Smsmith#define	RF_ALIGNMENT_SHIFT	10 /* alignment size bit starts bit 10 */
4868522Smsmith#define	RF_ALIGNMENT_MASK	(0x003F << RF_ALIGNMENT_SHIFT)
4968522Smsmith				/* resource address alignemnt size bit mask */
5068522Smsmith#define	RF_ALIGNMENT_LOG2(x)	((x) << RF_ALIGNMENT_SHIFT)
5168522Smsmith#define	RF_ALIGNMENT(x)		(((x) & RF_ALIGNMENT_MASK) >> RF_ALIGNMENT_SHIFT)
5268522Smsmith
5368522Smsmithenum	rman_type { RMAN_UNINIT = 0, RMAN_GAUGE, RMAN_ARRAY };
5468522Smsmith
5540711Swollman/*
5668522Smsmith * String length exported to userspace for resource names, etc.
5768522Smsmith */
5868522Smsmith#define RM_TEXTLEN	32
5968522Smsmith
6068522Smsmith/*
6168522Smsmith * Userspace-exported structures.
6268522Smsmith */
6368522Smsmithstruct u_resource {
6468522Smsmith	uintptr_t	r_handle;		/* resource uniquifier */
6568522Smsmith	uintptr_t	r_parent;		/* parent rman */
6668522Smsmith	uintptr_t	r_device;		/* device owning this resource */
6768522Smsmith	char		r_devname[RM_TEXTLEN];	/* device name XXX obsolete */
6868522Smsmith
6968522Smsmith	u_long		r_start;		/* offset in resource space */
7068522Smsmith	u_long		r_size;			/* size in resource space */
7168522Smsmith	u_int		r_flags;		/* RF_* flags */
7268522Smsmith};
7368522Smsmith
7468522Smsmithstruct u_rman {
7568522Smsmith	uintptr_t	rm_handle;		/* rman uniquifier */
7668522Smsmith	char		rm_descr[RM_TEXTLEN];	/* rman description */
7768522Smsmith
7868522Smsmith	u_long		rm_start;		/* base of managed region */
7968522Smsmith	u_long		rm_size;		/* size of managed region */
8068522Smsmith	enum rman_type	rm_type;		/* region type */
8168522Smsmith};
8268522Smsmith
8368522Smsmith#ifdef _KERNEL
8468522Smsmith/*
8540711Swollman * We use a linked list rather than a bitmap because we need to be able to
8640711Swollman * represent potentially huge objects (like all of a processor's physical
8740711Swollman * address space).  That is also why the indices are defined to have type
8868527Swollman * `unsigned long' -- that being the largest integral type in ISO C (1990).
8968527Swollman * The 1999 version of C allows `long long'; we may need to switch to that
9068527Swollman * at some point in the future, particularly if we want to support 36-bit
9168527Swollman * addresses on IA32 hardware.
9240711Swollman */
9368727SmckusickTAILQ_HEAD(resource_head, resource);
9483045Sobrienstruct resource {
9568727Smckusick	TAILQ_ENTRY(resource)	r_link;
9660938Sjake	LIST_ENTRY(resource)	r_sharelink;
9760938Sjake	LIST_HEAD(, resource) 	*r_sharehead;
9840711Swollman	u_long	r_start;	/* index of the first entry in this resource */
9940711Swollman	u_long	r_end;		/* index of the last entry (inclusive) */
10040711Swollman	u_int	r_flags;
10140711Swollman	void	*r_virtual;	/* virtual address of this resource */
10245720Speter	bus_space_tag_t r_bustag; /* bus_space tag */
10345720Speter	bus_space_handle_t r_bushandle;	/* bus_space handle */
10440711Swollman	struct	device *r_dev;	/* device which has allocated this resource */
10540711Swollman	struct	rman *r_rm;	/* resource manager from whence this came */
10640711Swollman};
10740711Swollman
10883045Sobrienstruct rman {
10940711Swollman	struct	resource_head 	rm_list;
11071576Sjasone	struct	mtx *rm_mtx;	/* mutex used to protect rm_list */
11160938Sjake	TAILQ_ENTRY(rman)	rm_link; /* link in list of all rmans */
11240711Swollman	u_long	rm_start;	/* index of globally first entry */
11340711Swollman	u_long	rm_end;		/* index of globally last entry */
11440711Swollman	enum	rman_type rm_type; /* what type of resource this is */
11540711Swollman	const	char *rm_descr;	/* text descripion of this resource */
11640711Swollman};
11760938SjakeTAILQ_HEAD(rman_head, rman);
11840711Swollman
11940711Swollmanint	rman_activate_resource(struct resource *r);
12040711Swollmanint	rman_await_resource(struct resource *r, int pri, int timo);
12140711Swollmanint	rman_deactivate_resource(struct resource *r);
12240711Swollmanint	rman_fini(struct rman *rm);
12340711Swollmanint	rman_init(struct rman *rm);
12440711Swollmanint	rman_manage_region(struct rman *rm, u_long start, u_long end);
12540711Swollmanint	rman_release_resource(struct resource *r);
12683045Sobrienstruct resource *rman_reserve_resource(struct rman *rm, u_long start,
12740711Swollman					u_long end, u_long count,
12840711Swollman					u_int flags, struct device *dev);
12967261Simpuint32_t rman_make_alignment_flags(uint32_t size);
13040711Swollman
13145720Speter#define rman_get_start(r)	((r)->r_start)
13245720Speter#define rman_get_end(r)		((r)->r_end)
13367267Smdodd#define	rman_get_size(r)	(((r)->r_end - (r)->r_start) + 1)
13445720Speter#define rman_get_flags(r)	((r)->r_flags)
13540711Swollman#define	rman_set_virtual(r,v)	((r)->r_virtual = (v))
13640711Swollman#define	rman_get_virtual(r)	((r)->r_virtual)
13745720Speter#define rman_set_bustag(r,t)	((r)->r_bustag = (t))
13845720Speter#define rman_get_bustag(r)	((r)->r_bustag)
13945720Speter#define rman_set_bushandle(r,h)	((r)->r_bushandle = (h))
14045720Speter#define rman_get_bushandle(r)	((r)->r_bushandle)
14140711Swollman
14240711Swollmanextern	struct rman_head rman_head;
14355205Speter#endif /* _KERNEL */
14440711Swollman
14540711Swollman#endif /* !_SYS_RMAN_H_ */
146