rman.h revision 150698
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: head/sys/sys/rman.h 150698 2005-09-28 18:06:25Z phk $
3040711Swollman */
3140711Swollman
3240711Swollman#ifndef _SYS_RMAN_H_
3340711Swollman#define	_SYS_RMAN_H_	1
3440711Swollman
3555205Speter#ifndef	_KERNEL
3640711Swollman#include <sys/queue.h>
37129887Simp#else
38129887Simp#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 */
4568522Smsmith#define	RF_TIMESHARE	0x0008	/* resource permits time-division sharing */
4668522Smsmith#define	RF_WANTED	0x0010	/* somebody is waiting for this resource */
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)
5368522Smsmith				/* resource address alignemnt 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
103150523Sphk/*
10440711Swollman * We use a linked list rather than a bitmap because we need to be able to
10540711Swollman * represent potentially huge objects (like all of a processor's physical
10640711Swollman * address space).  That is also why the indices are defined to have type
10768527Swollman * `unsigned long' -- that being the largest integral type in ISO C (1990).
10868527Swollman * The 1999 version of C allows `long long'; we may need to switch to that
10968527Swollman * at some point in the future, particularly if we want to support 36-bit
11068527Swollman * addresses on IA32 hardware.
11140711Swollman */
112150523SphkTAILQ_HEAD(resource_head, resource_i);
113131344Simp#ifdef __RMAN_RESOURCE_VISIBLE
114150523Sphkstruct resource_i {
115150523Sphk	struct resource		r_r;
116150523Sphk	TAILQ_ENTRY(resource_i)	r_link;
117150523Sphk	LIST_ENTRY(resource_i)	r_sharelink;
118150523Sphk	LIST_HEAD(, resource_i)	*r_sharehead;
11940711Swollman	u_long	r_start;	/* index of the first entry in this resource */
12040711Swollman	u_long	r_end;		/* index of the last entry (inclusive) */
12140711Swollman	u_int	r_flags;
12240711Swollman	void	*r_virtual;	/* virtual address of this resource */
12340711Swollman	struct	device *r_dev;	/* device which has allocated this resource */
12440711Swollman	struct	rman *r_rm;	/* resource manager from whence this came */
125135068Simp	void    *r_spare1;	/* Spare pointer 1 */
126135068Simp	void    *r_spare2;	/* Spare pointer 2 */
127107296Simp	int	r_rid;		/* optional rid for this resource. */
12840711Swollman};
129131413Simp#else
130131413Simpstruct device;
131131413Simp#endif
13240711Swollman
13383045Sobrienstruct rman {
13440711Swollman	struct	resource_head 	rm_list;
13571576Sjasone	struct	mtx *rm_mtx;	/* mutex used to protect rm_list */
13660938Sjake	TAILQ_ENTRY(rman)	rm_link; /* link in list of all rmans */
13740711Swollman	u_long	rm_start;	/* index of globally first entry */
13840711Swollman	u_long	rm_end;		/* index of globally last entry */
13940711Swollman	enum	rman_type rm_type; /* what type of resource this is */
14040711Swollman	const	char *rm_descr;	/* text descripion of this resource */
14140711Swollman};
14260938SjakeTAILQ_HEAD(rman_head, rman);
14340711Swollman
14440711Swollmanint	rman_activate_resource(struct resource *r);
14540711Swollmanint	rman_await_resource(struct resource *r, int pri, int timo);
146137492Simpbus_space_handle_t rman_get_bushandle(struct resource *);
147137492Simpbus_space_tag_t rman_get_bustag(struct resource *);
148137492Simpu_long	rman_get_end(struct resource *);
149137492Simpstruct device *rman_get_device(struct resource *);
150137492Simpu_int	rman_get_flags(struct resource *);
151137492Simpint	rman_get_rid(struct resource *);
152137492Simpu_long	rman_get_size(struct resource *);
153137492Simpu_long	rman_get_start(struct resource *);
154137492Simpvoid   *rman_get_virtual(struct resource *);
15540711Swollmanint	rman_deactivate_resource(struct resource *r);
15640711Swollmanint	rman_fini(struct rman *rm);
15740711Swollmanint	rman_init(struct rman *rm);
158137492Simpuint32_t rman_make_alignment_flags(uint32_t size);
15940711Swollmanint	rman_manage_region(struct rman *rm, u_long start, u_long end);
160150547Sphkint	rman_is_region_manager(struct resource *r, struct rman *rm);
16140711Swollmanint	rman_release_resource(struct resource *r);
16283045Sobrienstruct resource *rman_reserve_resource(struct rman *rm, u_long start,
16340711Swollman					u_long end, u_long count,
16440711Swollman					u_int flags, struct device *dev);
16588372Stmmstruct resource *rman_reserve_resource_bound(struct rman *rm, u_long start,
16688372Stmm					u_long end, u_long count, u_long bound,
16788372Stmm					u_int flags, struct device *dev);
168137496Simpvoid	rman_set_bushandle(struct resource *_r, bus_space_handle_t _h);
169107296Simpvoid	rman_set_bustag(struct resource *_r, bus_space_tag_t _t);
170144932Simpvoid	rman_set_device(struct resource *_r, struct device *_dev);
171137492Simpvoid	rman_set_end(struct resource *_r, u_long _end);
172107296Simpvoid	rman_set_rid(struct resource *_r, int _rid);
173131413Simpvoid	rman_set_start(struct resource *_r, u_long _start);
174137492Simpvoid	rman_set_virtual(struct resource *_r, void *_v);
17540711Swollman
17640711Swollmanextern	struct rman_head rman_head;
177150698Sphk
178150698Sphk/*
179150698Sphk * XXX: puc.c is a big hack.
180150698Sphk * XXX: it should be rewritten to act like a bridge and offer
181150698Sphk * XXX: its own resource manager.
182150698Sphk * XXX: until somebody has time, help it out with these two functions
183150698Sphk */
184150698Sphkstruct resource *rman_secret_puc_alloc_resource(int malloc_flag);
185150698Sphkvoid rman_secret_puc_free_resource(struct resource *r);
18655205Speter#endif /* _KERNEL */
18740711Swollman
18840711Swollman#endif /* !_SYS_RMAN_H_ */
189