subr_rman.c revision 166932
1139804Simp/*-
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.
15152543Syongari *
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 */
2940711Swollman
3040711Swollman/*
3140711Swollman * The kernel resource manager.  This code is responsible for keeping track
3240711Swollman * of hardware resources which are apportioned out to various drivers.
3340711Swollman * It does not actually assign those resources, and it is not expected
3440711Swollman * that end-device drivers will call into this code directly.  Rather,
3540711Swollman * the code which implements the buses that those devices are attached to,
3640711Swollman * and the code which manages CPU resources, will call this code, and the
3740711Swollman * end-device drivers will make upcalls to that code to actually perform
3840711Swollman * the allocation.
3940711Swollman *
4040711Swollman * There are two sorts of resources managed by this code.  The first is
4140711Swollman * the more familiar array (RMAN_ARRAY) type; resources in this class
4240711Swollman * consist of a sequence of individually-allocatable objects which have
4340711Swollman * been numbered in some well-defined order.  Most of the resources
4440711Swollman * are of this type, as it is the most familiar.  The second type is
4540711Swollman * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
4640711Swollman * resources in which each instance is indistinguishable from every
4740711Swollman * other instance).  The principal anticipated application of gauges
4840711Swollman * is in the context of power consumption, where a bus may have a specific
4940711Swollman * power budget which all attached devices share.  RMAN_GAUGE is not
5040711Swollman * implemented yet.
5140711Swollman *
5240711Swollman * For array resources, we make one simplifying assumption: two clients
5340711Swollman * sharing the same resource must use the same range of indices.  That
5440711Swollman * is to say, sharing of overlapping-but-not-identical regions is not
5540711Swollman * permitted.
5640711Swollman */
5740711Swollman
58116182Sobrien#include <sys/cdefs.h>
59116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_rman.c 166932 2007-02-23 22:53:56Z scottl $");
60116182Sobrien
6140711Swollman#include <sys/param.h>
6240711Swollman#include <sys/systm.h>
6341304Sbde#include <sys/kernel.h>
64164881Sjhb#include <sys/limits.h>
6540711Swollman#include <sys/lock.h>
6640711Swollman#include <sys/malloc.h>
6771576Sjasone#include <sys/mutex.h>
6845720Speter#include <sys/bus.h>		/* XXX debugging */
6945720Speter#include <machine/bus.h>
7040711Swollman#include <sys/rman.h>
71102962Siwasaki#include <sys/sysctl.h>
7240711Swollman
73151037Sphk/*
74151037Sphk * We use a linked list rather than a bitmap because we need to be able to
75151037Sphk * represent potentially huge objects (like all of a processor's physical
76151037Sphk * address space).  That is also why the indices are defined to have type
77151037Sphk * `unsigned long' -- that being the largest integral type in ISO C (1990).
78151037Sphk * The 1999 version of C allows `long long'; we may need to switch to that
79151037Sphk * at some point in the future, particularly if we want to support 36-bit
80151037Sphk * addresses on IA32 hardware.
81151037Sphk */
82151037Sphkstruct resource_i {
83151037Sphk	struct resource		r_r;
84151037Sphk	TAILQ_ENTRY(resource_i)	r_link;
85151037Sphk	LIST_ENTRY(resource_i)	r_sharelink;
86151037Sphk	LIST_HEAD(, resource_i)	*r_sharehead;
87151037Sphk	u_long	r_start;	/* index of the first entry in this resource */
88151037Sphk	u_long	r_end;		/* index of the last entry (inclusive) */
89151037Sphk	u_int	r_flags;
90151037Sphk	void	*r_virtual;	/* virtual address of this resource */
91151037Sphk	struct	device *r_dev;	/* device which has allocated this resource */
92151037Sphk	struct	rman *r_rm;	/* resource manager from whence this came */
93151037Sphk	int	r_rid;		/* optional rid for this resource. */
94151037Sphk};
95151037Sphk
96102962Siwasakiint     rman_debug = 0;
97102962SiwasakiTUNABLE_INT("debug.rman_debug", &rman_debug);
98102962SiwasakiSYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
99102962Siwasaki    &rman_debug, 0, "rman debug");
10059910Spaul
101102962Siwasaki#define DPRINTF(params) if (rman_debug) printf params
102102962Siwasaki
10345569Seivindstatic MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
10440711Swollman
10540711Swollmanstruct	rman_head rman_head;
10671576Sjasonestatic	struct mtx rman_mtx; /* mutex to protect rman_head */
107150523Sphkstatic	int int_rman_activate_resource(struct rman *rm, struct resource_i *r,
108150523Sphk				       struct resource_i **whohas);
109150523Sphkstatic	int int_rman_deactivate_resource(struct resource_i *r);
110150523Sphkstatic	int int_rman_release_resource(struct rman *rm, struct resource_i *r);
11140711Swollman
112150523Sphkstatic __inline struct resource_i *
113150523Sphkint_alloc_resource(int malloc_flag)
114150523Sphk{
115150523Sphk	struct resource_i *r;
116150523Sphk
117150523Sphk	r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
118150523Sphk	if (r != NULL) {
119150523Sphk		r->r_r.__r_i = r;
120150523Sphk	}
121150523Sphk	return (r);
122150523Sphk}
123150523Sphk
12440711Swollmanint
12540711Swollmanrman_init(struct rman *rm)
12640711Swollman{
127152543Syongari	static int once = 0;
12840711Swollman
12940711Swollman	if (once == 0) {
13040711Swollman		once = 1;
13140711Swollman		TAILQ_INIT(&rman_head);
13293818Sjhb		mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
13340711Swollman	}
13440711Swollman
13540711Swollman	if (rm->rm_type == RMAN_UNINIT)
13640711Swollman		panic("rman_init");
13740711Swollman	if (rm->rm_type == RMAN_GAUGE)
13840711Swollman		panic("implement RMAN_GAUGE");
13940711Swollman
14068727Smckusick	TAILQ_INIT(&rm->rm_list);
14184781Sjhb	rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
142152543Syongari	if (rm->rm_mtx == NULL)
14340711Swollman		return ENOMEM;
14493818Sjhb	mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
14540711Swollman
14672200Sbmilekic	mtx_lock(&rman_mtx);
14740711Swollman	TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
14872200Sbmilekic	mtx_unlock(&rman_mtx);
14940711Swollman	return 0;
15040711Swollman}
15140711Swollman
15240711Swollman/*
15340711Swollman * NB: this interface is not robust against programming errors which
15440711Swollman * add multiple copies of the same region.
15540711Swollman */
15640711Swollmanint
15740711Swollmanrman_manage_region(struct rman *rm, u_long start, u_long end)
15840711Swollman{
159162224Sjhb	struct resource_i *r, *s, *t;
16040711Swollman
161134040Snjl	DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
162134021Snjl	    rm->rm_descr, start, end));
163150523Sphk	r = int_alloc_resource(M_NOWAIT);
164152543Syongari	if (r == NULL)
16540711Swollman		return ENOMEM;
16640711Swollman	r->r_start = start;
16740711Swollman	r->r_end = end;
16840711Swollman	r->r_rm = rm;
16940711Swollman
17072200Sbmilekic	mtx_lock(rm->rm_mtx);
171162224Sjhb
172162224Sjhb	/* Skip entries before us. */
173164881Sjhb	TAILQ_FOREACH(s, &rm->rm_list, r_link) {
174164881Sjhb		if (s->r_end == ULONG_MAX)
175164881Sjhb			break;
176164881Sjhb		if (s->r_end + 1 >= r->r_start)
177164881Sjhb			break;
178164881Sjhb	}
17940711Swollman
180162224Sjhb	/* If we ran off the end of the list, insert at the tail. */
18168727Smckusick	if (s == NULL) {
18268727Smckusick		TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
18340711Swollman	} else {
184162224Sjhb		/* Check for any overlap with the current region. */
185162224Sjhb		if (r->r_start <= s->r_end && r->r_end >= s->r_start)
186162224Sjhb			return EBUSY;
187162224Sjhb
188162224Sjhb		/* Check for any overlap with the next region. */
189162224Sjhb		t = TAILQ_NEXT(s, r_link);
190162224Sjhb		if (t && r->r_start <= t->r_end && r->r_end >= t->r_start)
191162224Sjhb			return EBUSY;
192162224Sjhb
193162224Sjhb		/*
194162224Sjhb		 * See if this region can be merged with the next region.  If
195162224Sjhb		 * not, clear the pointer.
196162224Sjhb		 */
197162224Sjhb		if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0))
198162224Sjhb			t = NULL;
199162224Sjhb
200162224Sjhb		/* See if we can merge with the current region. */
201162224Sjhb		if (s->r_end + 1 == r->r_start && s->r_flags == 0) {
202162224Sjhb			/* Can we merge all 3 regions? */
203162224Sjhb			if (t != NULL) {
204162224Sjhb				s->r_end = t->r_end;
205162224Sjhb				TAILQ_REMOVE(&rm->rm_list, t, r_link);
206162224Sjhb				free(r, M_RMAN);
207162224Sjhb				free(t, M_RMAN);
208162224Sjhb			} else {
209162224Sjhb				s->r_end = r->r_end;
210162224Sjhb				free(r, M_RMAN);
211162224Sjhb			}
212166932Sscottl		} else if (t != NULL) {
213166932Sscottl			/* Can we merge with just the next region? */
214166932Sscottl			t->r_start = r->r_start;
215166932Sscottl			free(r, M_RMAN);
216166932Sscottl		} else if (s->r_end < r->r_start) {
217166932Sscottl			TAILQ_INSERT_AFTER(&rm->rm_list, s, r, r_link);
218162224Sjhb		} else {
219166932Sscottl			TAILQ_INSERT_BEFORE(s, r, r_link);
220162224Sjhb		}
22140711Swollman	}
22240711Swollman
22372200Sbmilekic	mtx_unlock(rm->rm_mtx);
22440711Swollman	return 0;
22540711Swollman}
22640711Swollman
22740711Swollmanint
228159536Simprman_init_from_resource(struct rman *rm, struct resource *r)
229159536Simp{
230159536Simp	int rv;
231159536Simp
232159536Simp	if ((rv = rman_init(rm)) != 0)
233159536Simp		return (rv);
234159536Simp	return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
235159536Simp}
236159536Simp
237159536Simpint
23840711Swollmanrman_fini(struct rman *rm)
23940711Swollman{
240150523Sphk	struct resource_i *r;
24140711Swollman
24272200Sbmilekic	mtx_lock(rm->rm_mtx);
24368727Smckusick	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
24445720Speter		if (r->r_flags & RF_ALLOCATED) {
24572200Sbmilekic			mtx_unlock(rm->rm_mtx);
24640711Swollman			return EBUSY;
24745720Speter		}
24840711Swollman	}
24940711Swollman
25040711Swollman	/*
25140711Swollman	 * There really should only be one of these if we are in this
25240711Swollman	 * state and the code is working properly, but it can't hurt.
25340711Swollman	 */
25468727Smckusick	while (!TAILQ_EMPTY(&rm->rm_list)) {
25568727Smckusick		r = TAILQ_FIRST(&rm->rm_list);
25668727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
25740711Swollman		free(r, M_RMAN);
25840711Swollman	}
25972200Sbmilekic	mtx_unlock(rm->rm_mtx);
26072200Sbmilekic	mtx_lock(&rman_mtx);
26140711Swollman	TAILQ_REMOVE(&rman_head, rm, rm_link);
26272200Sbmilekic	mtx_unlock(&rman_mtx);
26371576Sjasone	mtx_destroy(rm->rm_mtx);
26471576Sjasone	free(rm->rm_mtx, M_RMAN);
26540711Swollman
26640711Swollman	return 0;
26740711Swollman}
26840711Swollman
26940711Swollmanstruct resource *
27088372Stmmrman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
27188372Stmm		      u_long count, u_long bound,  u_int flags,
27288372Stmm		      struct device *dev)
27340711Swollman{
27440711Swollman	u_int	want_activate;
275150523Sphk	struct	resource_i *r, *s, *rv;
27688372Stmm	u_long	rstart, rend, amask, bmask;
27740711Swollman
278152543Syongari	rv = NULL;
27940711Swollman
280160958Sjb	DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], "
281160958Sjb	       "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end,
282160958Sjb	       count, flags,
283160958Sjb	       dev == NULL ? "<null>" : device_get_nameunit(dev)));
28440711Swollman	want_activate = (flags & RF_ACTIVE);
28540711Swollman	flags &= ~RF_ACTIVE;
28640711Swollman
28772200Sbmilekic	mtx_lock(rm->rm_mtx);
28840711Swollman
289152543Syongari	for (r = TAILQ_FIRST(&rm->rm_list);
29068727Smckusick	     r && r->r_end < start;
29168727Smckusick	     r = TAILQ_NEXT(r, r_link))
29240711Swollman		;
29340711Swollman
29468727Smckusick	if (r == NULL) {
29559910Spaul		DPRINTF(("could not find a region\n"));
29640711Swollman		goto out;
29740711Swollman	}
29840711Swollman
29988372Stmm	amask = (1ul << RF_ALIGNMENT(flags)) - 1;
30088372Stmm	/* If bound is 0, bmask will also be 0 */
30188372Stmm	bmask = ~(bound - 1);
30240711Swollman	/*
30340711Swollman	 * First try to find an acceptable totally-unshared region.
30440711Swollman	 */
30568727Smckusick	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
30659910Spaul		DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
307143665Simp		if (s->r_start + count - 1 > end) {
308143665Simp			DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
309143665Simp			    s->r_start, end));
31040711Swollman			break;
31140711Swollman		}
31240711Swollman		if (s->r_flags & RF_ALLOCATED) {
31359910Spaul			DPRINTF(("region is allocated\n"));
31440711Swollman			continue;
31540711Swollman		}
31688372Stmm		rstart = ulmax(s->r_start, start);
31788372Stmm		/*
31888372Stmm		 * Try to find a region by adjusting to boundary and alignment
31988372Stmm		 * until both conditions are satisfied. This is not an optimal
32088372Stmm		 * algorithm, but in most cases it isn't really bad, either.
32188372Stmm		 */
32288372Stmm		do {
32388372Stmm			rstart = (rstart + amask) & ~amask;
324109646Stmm			if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
32588372Stmm				rstart += bound - (rstart & ~bmask);
32688372Stmm		} while ((rstart & amask) != 0 && rstart < end &&
32788372Stmm		    rstart < s->r_end);
328128172Simp		rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
329102572Siwasaki		if (rstart > rend) {
330102572Siwasaki			DPRINTF(("adjusted start exceeds end\n"));
331102572Siwasaki			continue;
332102572Siwasaki		}
33359910Spaul		DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
33459910Spaul		       rstart, rend, (rend - rstart + 1), count));
33540711Swollman
33640711Swollman		if ((rend - rstart + 1) >= count) {
33759910Spaul			DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
338143664Simp			       rstart, rend, (rend - rstart + 1)));
33940711Swollman			if ((s->r_end - s->r_start + 1) == count) {
34059910Spaul				DPRINTF(("candidate region is entire chunk\n"));
34140711Swollman				rv = s;
34248235Sdfr				rv->r_flags |= RF_ALLOCATED | flags;
34340711Swollman				rv->r_dev = dev;
34440711Swollman				goto out;
34540711Swollman			}
34640711Swollman
34740711Swollman			/*
34840711Swollman			 * If s->r_start < rstart and
34940711Swollman			 *    s->r_end > rstart + count - 1, then
35040711Swollman			 * we need to split the region into three pieces
35140711Swollman			 * (the middle one will get returned to the user).
35240711Swollman			 * Otherwise, we are allocating at either the
35340711Swollman			 * beginning or the end of s, so we only need to
35440711Swollman			 * split it in two.  The first case requires
35540711Swollman			 * two new allocations; the second requires but one.
35640711Swollman			 */
357150523Sphk			rv = int_alloc_resource(M_NOWAIT);
358152543Syongari			if (rv == NULL)
35940711Swollman				goto out;
36040711Swollman			rv->r_start = rstart;
36140711Swollman			rv->r_end = rstart + count - 1;
36240711Swollman			rv->r_flags = flags | RF_ALLOCATED;
36340711Swollman			rv->r_dev = dev;
36445720Speter			rv->r_rm = rm;
365152543Syongari
36640711Swollman			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
36759910Spaul				DPRINTF(("splitting region in three parts: "
36840711Swollman				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
36940711Swollman				       s->r_start, rv->r_start - 1,
37040711Swollman				       rv->r_start, rv->r_end,
37159910Spaul				       rv->r_end + 1, s->r_end));
37240711Swollman				/*
37340711Swollman				 * We are allocating in the middle.
37440711Swollman				 */
375150523Sphk				r = int_alloc_resource(M_NOWAIT);
376152543Syongari				if (r == NULL) {
37740711Swollman					free(rv, M_RMAN);
378152543Syongari					rv = NULL;
37940711Swollman					goto out;
38040711Swollman				}
38140711Swollman				r->r_start = rv->r_end + 1;
38240711Swollman				r->r_end = s->r_end;
38340711Swollman				r->r_flags = s->r_flags;
38445720Speter				r->r_rm = rm;
38540711Swollman				s->r_end = rv->r_start - 1;
38668727Smckusick				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
38740711Swollman						     r_link);
38868727Smckusick				TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
38940711Swollman						     r_link);
39040711Swollman			} else if (s->r_start == rv->r_start) {
39159910Spaul				DPRINTF(("allocating from the beginning\n"));
39240711Swollman				/*
39340711Swollman				 * We are allocating at the beginning.
39440711Swollman				 */
39540711Swollman				s->r_start = rv->r_end + 1;
39668727Smckusick				TAILQ_INSERT_BEFORE(s, rv, r_link);
39740711Swollman			} else {
39859910Spaul				DPRINTF(("allocating at the end\n"));
39940711Swollman				/*
40040711Swollman				 * We are allocating at the end.
40140711Swollman				 */
40240711Swollman				s->r_end = rv->r_start - 1;
40368727Smckusick				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
40440711Swollman						     r_link);
40540711Swollman			}
40640711Swollman			goto out;
40740711Swollman		}
40840711Swollman	}
40940711Swollman
41040711Swollman	/*
41140711Swollman	 * Now find an acceptable shared region, if the client's requirements
41240711Swollman	 * allow sharing.  By our implementation restriction, a candidate
41340711Swollman	 * region must match exactly by both size and sharing type in order
41440711Swollman	 * to be considered compatible with the client's request.  (The
41540711Swollman	 * former restriction could probably be lifted without too much
41640711Swollman	 * additional work, but this does not seem warranted.)
41740711Swollman	 */
41859910Spaul	DPRINTF(("no unshared regions found\n"));
41940711Swollman	if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
42040711Swollman		goto out;
42140711Swollman
42268727Smckusick	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
42340711Swollman		if (s->r_start > end)
42440711Swollman			break;
42540711Swollman		if ((s->r_flags & flags) != flags)
42640711Swollman			continue;
42788372Stmm		rstart = ulmax(s->r_start, start);
428128172Simp		rend = ulmin(s->r_end, ulmax(start + count - 1, end));
42940711Swollman		if (s->r_start >= start && s->r_end <= end
43088372Stmm		    && (s->r_end - s->r_start + 1) == count &&
43188372Stmm		    (s->r_start & amask) == 0 &&
43288372Stmm		    ((s->r_start ^ s->r_end) & bmask) == 0) {
433150523Sphk			rv = int_alloc_resource(M_NOWAIT);
434152543Syongari			if (rv == NULL)
43540711Swollman				goto out;
43640711Swollman			rv->r_start = s->r_start;
43740711Swollman			rv->r_end = s->r_end;
438152543Syongari			rv->r_flags = s->r_flags &
43940711Swollman				(RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
44040711Swollman			rv->r_dev = dev;
44140711Swollman			rv->r_rm = rm;
442152543Syongari			if (s->r_sharehead == NULL) {
44340711Swollman				s->r_sharehead = malloc(sizeof *s->r_sharehead,
44469781Sdwmalone						M_RMAN, M_NOWAIT | M_ZERO);
445152543Syongari				if (s->r_sharehead == NULL) {
44640711Swollman					free(rv, M_RMAN);
447152543Syongari					rv = NULL;
44840711Swollman					goto out;
44940711Swollman				}
45040711Swollman				LIST_INIT(s->r_sharehead);
451152543Syongari				LIST_INSERT_HEAD(s->r_sharehead, s,
45240711Swollman						 r_sharelink);
45345106Sdfr				s->r_flags |= RF_FIRSTSHARE;
45440711Swollman			}
45540711Swollman			rv->r_sharehead = s->r_sharehead;
45640711Swollman			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
45740711Swollman			goto out;
45840711Swollman		}
45940711Swollman	}
46040711Swollman
46140711Swollman	/*
46240711Swollman	 * We couldn't find anything.
46340711Swollman	 */
46440711Swollmanout:
46540711Swollman	/*
46640711Swollman	 * If the user specified RF_ACTIVE in the initial flags,
46740711Swollman	 * which is reflected in `want_activate', we attempt to atomically
46840711Swollman	 * activate the resource.  If this fails, we release the resource
46940711Swollman	 * and indicate overall failure.  (This behavior probably doesn't
47040711Swollman	 * make sense for RF_TIMESHARE-type resources.)
47140711Swollman	 */
47240711Swollman	if (rv && want_activate) {
473150523Sphk		struct resource_i *whohas;
47440711Swollman		if (int_rman_activate_resource(rm, rv, &whohas)) {
47540711Swollman			int_rman_release_resource(rm, rv);
476152543Syongari			rv = NULL;
47740711Swollman		}
47840711Swollman	}
479152543Syongari
48072200Sbmilekic	mtx_unlock(rm->rm_mtx);
481152543Syongari	return (rv == NULL ? NULL : &rv->r_r);
48240711Swollman}
48340711Swollman
48488372Stmmstruct resource *
48588372Stmmrman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
48688372Stmm		      u_int flags, struct device *dev)
48788372Stmm{
48888372Stmm
48988372Stmm	return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
49088372Stmm	    dev));
49188372Stmm}
49288372Stmm
49340711Swollmanstatic int
494150523Sphkint_rman_activate_resource(struct rman *rm, struct resource_i *r,
495150523Sphk			   struct resource_i **whohas)
49640711Swollman{
497150523Sphk	struct resource_i *s;
49840711Swollman	int ok;
49940711Swollman
50040711Swollman	/*
50140711Swollman	 * If we are not timesharing, then there is nothing much to do.
50240711Swollman	 * If we already have the resource, then there is nothing at all to do.
50340711Swollman	 * If we are not on a sharing list with anybody else, then there is
50440711Swollman	 * little to do.
50540711Swollman	 */
50640711Swollman	if ((r->r_flags & RF_TIMESHARE) == 0
50740711Swollman	    || (r->r_flags & RF_ACTIVE) != 0
508152543Syongari	    || r->r_sharehead == NULL) {
50940711Swollman		r->r_flags |= RF_ACTIVE;
51040711Swollman		return 0;
51140711Swollman	}
51240711Swollman
51340711Swollman	ok = 1;
51453225Sphk	for (s = LIST_FIRST(r->r_sharehead); s && ok;
51553225Sphk	     s = LIST_NEXT(s, r_sharelink)) {
51640711Swollman		if ((s->r_flags & RF_ACTIVE) != 0) {
51740711Swollman			ok = 0;
51840711Swollman			*whohas = s;
51940711Swollman		}
52040711Swollman	}
52140711Swollman	if (ok) {
52240711Swollman		r->r_flags |= RF_ACTIVE;
52340711Swollman		return 0;
52440711Swollman	}
52540711Swollman	return EBUSY;
52640711Swollman}
52740711Swollman
52840711Swollmanint
529150523Sphkrman_activate_resource(struct resource *re)
53040711Swollman{
53140711Swollman	int rv;
532150523Sphk	struct resource_i *r, *whohas;
53340711Swollman	struct rman *rm;
53440711Swollman
535150523Sphk	r = re->__r_i;
53640711Swollman	rm = r->r_rm;
53772200Sbmilekic	mtx_lock(rm->rm_mtx);
53840711Swollman	rv = int_rman_activate_resource(rm, r, &whohas);
53972200Sbmilekic	mtx_unlock(rm->rm_mtx);
54040711Swollman	return rv;
54140711Swollman}
54240711Swollman
54340711Swollmanint
544150523Sphkrman_await_resource(struct resource *re, int pri, int timo)
54540711Swollman{
54685519Sjhb	int	rv;
547150523Sphk	struct	resource_i *r, *whohas;
54840711Swollman	struct	rman *rm;
54940711Swollman
550150523Sphk	r = re->__r_i;
55140711Swollman	rm = r->r_rm;
55285519Sjhb	mtx_lock(rm->rm_mtx);
55340711Swollman	for (;;) {
55440711Swollman		rv = int_rman_activate_resource(rm, r, &whohas);
55540711Swollman		if (rv != EBUSY)
55671576Sjasone			return (rv);	/* returns with mutex held */
55740711Swollman
558152543Syongari		if (r->r_sharehead == NULL)
55940711Swollman			panic("rman_await_resource");
56040711Swollman		whohas->r_flags |= RF_WANTED;
56185519Sjhb		rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
56240711Swollman		if (rv) {
56385519Sjhb			mtx_unlock(rm->rm_mtx);
56485519Sjhb			return (rv);
56540711Swollman		}
56640711Swollman	}
56740711Swollman}
56840711Swollman
56945720Speterstatic int
570150523Sphkint_rman_deactivate_resource(struct resource_i *r)
57140711Swollman{
57240711Swollman
57340711Swollman	r->r_flags &= ~RF_ACTIVE;
57440711Swollman	if (r->r_flags & RF_WANTED) {
57540711Swollman		r->r_flags &= ~RF_WANTED;
57640711Swollman		wakeup(r->r_sharehead);
57740711Swollman	}
57845720Speter	return 0;
57945720Speter}
58045720Speter
58145720Speterint
58245720Speterrman_deactivate_resource(struct resource *r)
58345720Speter{
58445720Speter	struct	rman *rm;
58545720Speter
586150523Sphk	rm = r->__r_i->r_rm;
58772200Sbmilekic	mtx_lock(rm->rm_mtx);
588150523Sphk	int_rman_deactivate_resource(r->__r_i);
58972200Sbmilekic	mtx_unlock(rm->rm_mtx);
59040711Swollman	return 0;
59140711Swollman}
59240711Swollman
59340711Swollmanstatic int
594150523Sphkint_rman_release_resource(struct rman *rm, struct resource_i *r)
59540711Swollman{
596150523Sphk	struct	resource_i *s, *t;
59740711Swollman
59840711Swollman	if (r->r_flags & RF_ACTIVE)
59945720Speter		int_rman_deactivate_resource(r);
60040711Swollman
60140711Swollman	/*
60240711Swollman	 * Check for a sharing list first.  If there is one, then we don't
60340711Swollman	 * have to think as hard.
60440711Swollman	 */
60540711Swollman	if (r->r_sharehead) {
60640711Swollman		/*
60740711Swollman		 * If a sharing list exists, then we know there are at
60840711Swollman		 * least two sharers.
60940711Swollman		 *
61040711Swollman		 * If we are in the main circleq, appoint someone else.
61140711Swollman		 */
61240711Swollman		LIST_REMOVE(r, r_sharelink);
61353225Sphk		s = LIST_FIRST(r->r_sharehead);
61440711Swollman		if (r->r_flags & RF_FIRSTSHARE) {
61540711Swollman			s->r_flags |= RF_FIRSTSHARE;
61668727Smckusick			TAILQ_INSERT_BEFORE(r, s, r_link);
61768727Smckusick			TAILQ_REMOVE(&rm->rm_list, r, r_link);
61840711Swollman		}
61940711Swollman
62040711Swollman		/*
62140711Swollman		 * Make sure that the sharing list goes away completely
62240711Swollman		 * if the resource is no longer being shared at all.
62340711Swollman		 */
624152543Syongari		if (LIST_NEXT(s, r_sharelink) == NULL) {
62540711Swollman			free(s->r_sharehead, M_RMAN);
626152543Syongari			s->r_sharehead = NULL;
62740711Swollman			s->r_flags &= ~RF_FIRSTSHARE;
62840711Swollman		}
62940711Swollman		goto out;
63040711Swollman	}
63140711Swollman
63240711Swollman	/*
63340711Swollman	 * Look at the adjacent resources in the list and see if our
634133177Sjhb	 * segment can be merged with any of them.  If either of the
635133177Sjhb	 * resources is allocated or is not exactly adjacent then they
636133177Sjhb	 * cannot be merged with our segment.
63740711Swollman	 */
63868727Smckusick	s = TAILQ_PREV(r, resource_head, r_link);
639133177Sjhb	if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
640133177Sjhb	    s->r_end + 1 != r->r_start))
641133177Sjhb		s = NULL;
64268727Smckusick	t = TAILQ_NEXT(r, r_link);
643133177Sjhb	if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
644133177Sjhb	    r->r_end + 1 != t->r_start))
645133177Sjhb		t = NULL;
64640711Swollman
647133177Sjhb	if (s != NULL && t != NULL) {
64840711Swollman		/*
64940711Swollman		 * Merge all three segments.
65040711Swollman		 */
65140711Swollman		s->r_end = t->r_end;
65268727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
65368727Smckusick		TAILQ_REMOVE(&rm->rm_list, t, r_link);
65440711Swollman		free(t, M_RMAN);
655133177Sjhb	} else if (s != NULL) {
65640711Swollman		/*
65740711Swollman		 * Merge previous segment with ours.
65840711Swollman		 */
65940711Swollman		s->r_end = r->r_end;
66068727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
661133177Sjhb	} else if (t != NULL) {
66240711Swollman		/*
66340711Swollman		 * Merge next segment with ours.
66440711Swollman		 */
66540711Swollman		t->r_start = r->r_start;
66668727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
66740711Swollman	} else {
66840711Swollman		/*
66940711Swollman		 * At this point, we know there is nothing we
67040711Swollman		 * can potentially merge with, because on each
67140711Swollman		 * side, there is either nothing there or what is
67240711Swollman		 * there is still allocated.  In that case, we don't
67340711Swollman		 * want to remove r from the list; we simply want to
67440711Swollman		 * change it to an unallocated region and return
67540711Swollman		 * without freeing anything.
67640711Swollman		 */
67740711Swollman		r->r_flags &= ~RF_ALLOCATED;
67840711Swollman		return 0;
67940711Swollman	}
68040711Swollman
68140711Swollmanout:
68240711Swollman	free(r, M_RMAN);
68340711Swollman	return 0;
68440711Swollman}
68540711Swollman
68640711Swollmanint
687150523Sphkrman_release_resource(struct resource *re)
68840711Swollman{
68940711Swollman	int	rv;
690150523Sphk	struct	resource_i *r;
691150523Sphk	struct	rman *rm;
69240711Swollman
693150523Sphk	r = re->__r_i;
694150523Sphk	rm = r->r_rm;
69572200Sbmilekic	mtx_lock(rm->rm_mtx);
69640711Swollman	rv = int_rman_release_resource(rm, r);
69772200Sbmilekic	mtx_unlock(rm->rm_mtx);
69840711Swollman	return (rv);
69940711Swollman}
70067261Simp
70167261Simpuint32_t
70267261Simprman_make_alignment_flags(uint32_t size)
70367261Simp{
70467261Simp	int	i;
70567261Simp
70667425Simp	/*
70767425Simp	 * Find the hightest bit set, and add one if more than one bit
70867425Simp	 * set.  We're effectively computing the ceil(log2(size)) here.
70967425Simp	 */
71088372Stmm	for (i = 31; i > 0; i--)
71167425Simp		if ((1 << i) & size)
71267425Simp			break;
71367425Simp	if (~(1 << i) & size)
71467425Simp		i++;
71567261Simp
71667261Simp	return(RF_ALIGNMENT_LOG2(i));
71767425Simp}
718107296Simp
719107296Simpu_long
720107296Simprman_get_start(struct resource *r)
721107296Simp{
722150523Sphk	return (r->__r_i->r_start);
723107296Simp}
724107296Simp
725107296Simpu_long
726107296Simprman_get_end(struct resource *r)
727107296Simp{
728150523Sphk	return (r->__r_i->r_end);
729107296Simp}
730107296Simp
731107296Simpu_long
732107296Simprman_get_size(struct resource *r)
733107296Simp{
734150523Sphk	return (r->__r_i->r_end - r->__r_i->r_start + 1);
735107296Simp}
736107296Simp
737107296Simpu_int
738107296Simprman_get_flags(struct resource *r)
739107296Simp{
740150523Sphk	return (r->__r_i->r_flags);
741107296Simp}
742107296Simp
743107296Simpvoid
744107296Simprman_set_virtual(struct resource *r, void *v)
745107296Simp{
746150523Sphk	r->__r_i->r_virtual = v;
747107296Simp}
748107296Simp
749107296Simpvoid *
750107296Simprman_get_virtual(struct resource *r)
751107296Simp{
752150523Sphk	return (r->__r_i->r_virtual);
753107296Simp}
754107296Simp
755107296Simpvoid
756107296Simprman_set_bustag(struct resource *r, bus_space_tag_t t)
757107296Simp{
758107296Simp	r->r_bustag = t;
759107296Simp}
760107296Simp
761107296Simpbus_space_tag_t
762107296Simprman_get_bustag(struct resource *r)
763107296Simp{
764107296Simp	return (r->r_bustag);
765107296Simp}
766107296Simp
767107296Simpvoid
768107296Simprman_set_bushandle(struct resource *r, bus_space_handle_t h)
769107296Simp{
770107296Simp	r->r_bushandle = h;
771107296Simp}
772107296Simp
773107296Simpbus_space_handle_t
774107296Simprman_get_bushandle(struct resource *r)
775107296Simp{
776107296Simp	return (r->r_bushandle);
777107296Simp}
778107296Simp
779107296Simpvoid
780107296Simprman_set_rid(struct resource *r, int rid)
781107296Simp{
782150523Sphk	r->__r_i->r_rid = rid;
783107296Simp}
784107296Simp
785131414Simpvoid
786131414Simprman_set_start(struct resource *r, u_long start)
787131414Simp{
788150523Sphk	r->__r_i->r_start = start;
789131414Simp}
790131414Simp
791131414Simpvoid
792131414Simprman_set_end(struct resource *r, u_long end)
793131414Simp{
794150523Sphk	r->__r_i->r_end = end;
795131414Simp}
796131414Simp
797107296Simpint
798107296Simprman_get_rid(struct resource *r)
799107296Simp{
800150523Sphk	return (r->__r_i->r_rid);
801107296Simp}
802110753Simp
803110753Simpstruct device *
804110753Simprman_get_device(struct resource *r)
805110753Simp{
806150523Sphk	return (r->__r_i->r_dev);
807110753Simp}
808144071Sphk
809144932Simpvoid
810144932Simprman_set_device(struct resource *r, struct device *dev)
811144932Simp{
812150523Sphk	r->__r_i->r_dev = dev;
813144932Simp}
814144932Simp
815150547Sphkint
816150547Sphkrman_is_region_manager(struct resource *r, struct rman *rm)
817150547Sphk{
818150547Sphk
819150547Sphk	return (r->__r_i->r_rm == rm);
820150547Sphk}
821150547Sphk
822144071Sphk/*
823144071Sphk * Sysctl interface for scanning the resource lists.
824144071Sphk *
825144071Sphk * We take two input parameters; the index into the list of resource
826144071Sphk * managers, and the resource offset into the list.
827144071Sphk */
828144071Sphkstatic int
829144071Sphksysctl_rman(SYSCTL_HANDLER_ARGS)
830144071Sphk{
831144071Sphk	int			*name = (int *)arg1;
832144071Sphk	u_int			namelen = arg2;
833144071Sphk	int			rman_idx, res_idx;
834144071Sphk	struct rman		*rm;
835150523Sphk	struct resource_i	*res;
836144071Sphk	struct u_rman		urm;
837144071Sphk	struct u_resource	ures;
838144071Sphk	int			error;
839144071Sphk
840144071Sphk	if (namelen != 3)
841144071Sphk		return (EINVAL);
842144071Sphk
843144071Sphk	if (bus_data_generation_check(name[0]))
844144071Sphk		return (EINVAL);
845144071Sphk	rman_idx = name[1];
846144071Sphk	res_idx = name[2];
847144071Sphk
848144071Sphk	/*
849144071Sphk	 * Find the indexed resource manager
850144071Sphk	 */
851152543Syongari	mtx_lock(&rman_mtx);
852144071Sphk	TAILQ_FOREACH(rm, &rman_head, rm_link) {
853144071Sphk		if (rman_idx-- == 0)
854144071Sphk			break;
855144071Sphk	}
856152543Syongari	mtx_unlock(&rman_mtx);
857144071Sphk	if (rm == NULL)
858144071Sphk		return (ENOENT);
859144071Sphk
860144071Sphk	/*
861144071Sphk	 * If the resource index is -1, we want details on the
862144071Sphk	 * resource manager.
863144071Sphk	 */
864144071Sphk	if (res_idx == -1) {
865145953Scperciva		bzero(&urm, sizeof(urm));
866144071Sphk		urm.rm_handle = (uintptr_t)rm;
867144071Sphk		strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
868144071Sphk		urm.rm_start = rm->rm_start;
869144071Sphk		urm.rm_size = rm->rm_end - rm->rm_start + 1;
870144071Sphk		urm.rm_type = rm->rm_type;
871144071Sphk
872144071Sphk		error = SYSCTL_OUT(req, &urm, sizeof(urm));
873144071Sphk		return (error);
874144071Sphk	}
875144071Sphk
876144071Sphk	/*
877144071Sphk	 * Find the indexed resource and return it.
878144071Sphk	 */
879152543Syongari	mtx_lock(rm->rm_mtx);
880144071Sphk	TAILQ_FOREACH(res, &rm->rm_list, r_link) {
881144071Sphk		if (res_idx-- == 0) {
882145953Scperciva			bzero(&ures, sizeof(ures));
883144071Sphk			ures.r_handle = (uintptr_t)res;
884144071Sphk			ures.r_parent = (uintptr_t)res->r_rm;
885144071Sphk			ures.r_device = (uintptr_t)res->r_dev;
886144071Sphk			if (res->r_dev != NULL) {
887144071Sphk				if (device_get_name(res->r_dev) != NULL) {
888144071Sphk					snprintf(ures.r_devname, RM_TEXTLEN,
889144071Sphk					    "%s%d",
890144071Sphk					    device_get_name(res->r_dev),
891144071Sphk					    device_get_unit(res->r_dev));
892144071Sphk				} else {
893144071Sphk					strlcpy(ures.r_devname, "nomatch",
894144071Sphk					    RM_TEXTLEN);
895144071Sphk				}
896144071Sphk			} else {
897144071Sphk				ures.r_devname[0] = '\0';
898144071Sphk			}
899144071Sphk			ures.r_start = res->r_start;
900144071Sphk			ures.r_size = res->r_end - res->r_start + 1;
901144071Sphk			ures.r_flags = res->r_flags;
902144071Sphk
903152543Syongari			mtx_unlock(rm->rm_mtx);
904144071Sphk			error = SYSCTL_OUT(req, &ures, sizeof(ures));
905144071Sphk			return (error);
906144071Sphk		}
907144071Sphk	}
908152543Syongari	mtx_unlock(rm->rm_mtx);
909144071Sphk	return (ENOENT);
910144071Sphk}
911144071Sphk
912144071SphkSYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
913144071Sphk    "kernel resource manager");
914