subr_rman.c revision 221218
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
58168791Sjhb#include "opt_ddb.h"
59168791Sjhb
60116182Sobrien#include <sys/cdefs.h>
61116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_rman.c 221218 2011-04-29 18:41:21Z jhb $");
62116182Sobrien
6340711Swollman#include <sys/param.h>
6440711Swollman#include <sys/systm.h>
6541304Sbde#include <sys/kernel.h>
66164881Sjhb#include <sys/limits.h>
6740711Swollman#include <sys/lock.h>
6840711Swollman#include <sys/malloc.h>
6971576Sjasone#include <sys/mutex.h>
7045720Speter#include <sys/bus.h>		/* XXX debugging */
7145720Speter#include <machine/bus.h>
7240711Swollman#include <sys/rman.h>
73102962Siwasaki#include <sys/sysctl.h>
7440711Swollman
75168791Sjhb#ifdef DDB
76168791Sjhb#include <ddb/ddb.h>
77168791Sjhb#endif
78168791Sjhb
79151037Sphk/*
80151037Sphk * We use a linked list rather than a bitmap because we need to be able to
81151037Sphk * represent potentially huge objects (like all of a processor's physical
82151037Sphk * address space).  That is also why the indices are defined to have type
83151037Sphk * `unsigned long' -- that being the largest integral type in ISO C (1990).
84151037Sphk * The 1999 version of C allows `long long'; we may need to switch to that
85151037Sphk * at some point in the future, particularly if we want to support 36-bit
86151037Sphk * addresses on IA32 hardware.
87151037Sphk */
88151037Sphkstruct resource_i {
89151037Sphk	struct resource		r_r;
90151037Sphk	TAILQ_ENTRY(resource_i)	r_link;
91151037Sphk	LIST_ENTRY(resource_i)	r_sharelink;
92151037Sphk	LIST_HEAD(, resource_i)	*r_sharehead;
93151037Sphk	u_long	r_start;	/* index of the first entry in this resource */
94151037Sphk	u_long	r_end;		/* index of the last entry (inclusive) */
95151037Sphk	u_int	r_flags;
96151037Sphk	void	*r_virtual;	/* virtual address of this resource */
97151037Sphk	struct	device *r_dev;	/* device which has allocated this resource */
98151037Sphk	struct	rman *r_rm;	/* resource manager from whence this came */
99151037Sphk	int	r_rid;		/* optional rid for this resource. */
100151037Sphk};
101151037Sphk
102188061Simpstatic int     rman_debug = 0;
103102962SiwasakiTUNABLE_INT("debug.rman_debug", &rman_debug);
104102962SiwasakiSYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
105102962Siwasaki    &rman_debug, 0, "rman debug");
10659910Spaul
107102962Siwasaki#define DPRINTF(params) if (rman_debug) printf params
108102962Siwasaki
10945569Seivindstatic MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
11040711Swollman
11140711Swollmanstruct	rman_head rman_head;
11271576Sjasonestatic	struct mtx rman_mtx; /* mutex to protect rman_head */
113150523Sphkstatic	int int_rman_activate_resource(struct rman *rm, struct resource_i *r,
114150523Sphk				       struct resource_i **whohas);
115150523Sphkstatic	int int_rman_deactivate_resource(struct resource_i *r);
116150523Sphkstatic	int int_rman_release_resource(struct rman *rm, struct resource_i *r);
11740711Swollman
118150523Sphkstatic __inline struct resource_i *
119150523Sphkint_alloc_resource(int malloc_flag)
120150523Sphk{
121150523Sphk	struct resource_i *r;
122150523Sphk
123150523Sphk	r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
124150523Sphk	if (r != NULL) {
125150523Sphk		r->r_r.__r_i = r;
126150523Sphk	}
127150523Sphk	return (r);
128150523Sphk}
129150523Sphk
13040711Swollmanint
13140711Swollmanrman_init(struct rman *rm)
13240711Swollman{
133152543Syongari	static int once = 0;
13440711Swollman
13540711Swollman	if (once == 0) {
13640711Swollman		once = 1;
13740711Swollman		TAILQ_INIT(&rman_head);
13893818Sjhb		mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
13940711Swollman	}
14040711Swollman
141221218Sjhb	if (rm->rm_start == 0 && rm->rm_end == 0)
142221218Sjhb		rm->rm_end = ~0ul;
14340711Swollman	if (rm->rm_type == RMAN_UNINIT)
14440711Swollman		panic("rman_init");
14540711Swollman	if (rm->rm_type == RMAN_GAUGE)
14640711Swollman		panic("implement RMAN_GAUGE");
14740711Swollman
14868727Smckusick	TAILQ_INIT(&rm->rm_list);
14984781Sjhb	rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
150152543Syongari	if (rm->rm_mtx == NULL)
15140711Swollman		return ENOMEM;
15293818Sjhb	mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
15340711Swollman
15472200Sbmilekic	mtx_lock(&rman_mtx);
15540711Swollman	TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
15672200Sbmilekic	mtx_unlock(&rman_mtx);
15740711Swollman	return 0;
15840711Swollman}
15940711Swollman
16040711Swollmanint
16140711Swollmanrman_manage_region(struct rman *rm, u_long start, u_long end)
16240711Swollman{
163162224Sjhb	struct resource_i *r, *s, *t;
16440711Swollman
165134040Snjl	DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
166134021Snjl	    rm->rm_descr, start, end));
167221218Sjhb	if (start < rm->rm_start || end > rm->rm_end)
168221218Sjhb		return EINVAL;
169150523Sphk	r = int_alloc_resource(M_NOWAIT);
170152543Syongari	if (r == NULL)
17140711Swollman		return ENOMEM;
17240711Swollman	r->r_start = start;
17340711Swollman	r->r_end = end;
17440711Swollman	r->r_rm = rm;
17540711Swollman
17672200Sbmilekic	mtx_lock(rm->rm_mtx);
177162224Sjhb
178162224Sjhb	/* Skip entries before us. */
179164881Sjhb	TAILQ_FOREACH(s, &rm->rm_list, r_link) {
180164881Sjhb		if (s->r_end == ULONG_MAX)
181164881Sjhb			break;
182164881Sjhb		if (s->r_end + 1 >= r->r_start)
183164881Sjhb			break;
184164881Sjhb	}
18540711Swollman
186162224Sjhb	/* If we ran off the end of the list, insert at the tail. */
18768727Smckusick	if (s == NULL) {
18868727Smckusick		TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
18940711Swollman	} else {
190162224Sjhb		/* Check for any overlap with the current region. */
191162224Sjhb		if (r->r_start <= s->r_end && r->r_end >= s->r_start)
192162224Sjhb			return EBUSY;
193162224Sjhb
194162224Sjhb		/* Check for any overlap with the next region. */
195162224Sjhb		t = TAILQ_NEXT(s, r_link);
196162224Sjhb		if (t && r->r_start <= t->r_end && r->r_end >= t->r_start)
197162224Sjhb			return EBUSY;
198162224Sjhb
199162224Sjhb		/*
200162224Sjhb		 * See if this region can be merged with the next region.  If
201162224Sjhb		 * not, clear the pointer.
202162224Sjhb		 */
203162224Sjhb		if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0))
204162224Sjhb			t = NULL;
205162224Sjhb
206162224Sjhb		/* See if we can merge with the current region. */
207162224Sjhb		if (s->r_end + 1 == r->r_start && s->r_flags == 0) {
208162224Sjhb			/* Can we merge all 3 regions? */
209162224Sjhb			if (t != NULL) {
210162224Sjhb				s->r_end = t->r_end;
211162224Sjhb				TAILQ_REMOVE(&rm->rm_list, t, r_link);
212162224Sjhb				free(r, M_RMAN);
213162224Sjhb				free(t, M_RMAN);
214162224Sjhb			} else {
215162224Sjhb				s->r_end = r->r_end;
216162224Sjhb				free(r, M_RMAN);
217162224Sjhb			}
218166932Sscottl		} else if (t != NULL) {
219166932Sscottl			/* Can we merge with just the next region? */
220166932Sscottl			t->r_start = r->r_start;
221166932Sscottl			free(r, M_RMAN);
222166932Sscottl		} else if (s->r_end < r->r_start) {
223166932Sscottl			TAILQ_INSERT_AFTER(&rm->rm_list, s, r, r_link);
224162224Sjhb		} else {
225166932Sscottl			TAILQ_INSERT_BEFORE(s, r, r_link);
226162224Sjhb		}
22740711Swollman	}
22840711Swollman
22972200Sbmilekic	mtx_unlock(rm->rm_mtx);
23040711Swollman	return 0;
23140711Swollman}
23240711Swollman
23340711Swollmanint
234159536Simprman_init_from_resource(struct rman *rm, struct resource *r)
235159536Simp{
236159536Simp	int rv;
237159536Simp
238159536Simp	if ((rv = rman_init(rm)) != 0)
239159536Simp		return (rv);
240159536Simp	return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
241159536Simp}
242159536Simp
243159536Simpint
24440711Swollmanrman_fini(struct rman *rm)
24540711Swollman{
246150523Sphk	struct resource_i *r;
24740711Swollman
24872200Sbmilekic	mtx_lock(rm->rm_mtx);
24968727Smckusick	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
25045720Speter		if (r->r_flags & RF_ALLOCATED) {
25172200Sbmilekic			mtx_unlock(rm->rm_mtx);
25240711Swollman			return EBUSY;
25345720Speter		}
25440711Swollman	}
25540711Swollman
25640711Swollman	/*
25740711Swollman	 * There really should only be one of these if we are in this
25840711Swollman	 * state and the code is working properly, but it can't hurt.
25940711Swollman	 */
26068727Smckusick	while (!TAILQ_EMPTY(&rm->rm_list)) {
26168727Smckusick		r = TAILQ_FIRST(&rm->rm_list);
26268727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
26340711Swollman		free(r, M_RMAN);
26440711Swollman	}
26572200Sbmilekic	mtx_unlock(rm->rm_mtx);
26672200Sbmilekic	mtx_lock(&rman_mtx);
26740711Swollman	TAILQ_REMOVE(&rman_head, rm, rm_link);
26872200Sbmilekic	mtx_unlock(&rman_mtx);
26971576Sjasone	mtx_destroy(rm->rm_mtx);
27071576Sjasone	free(rm->rm_mtx, M_RMAN);
27140711Swollman
27240711Swollman	return 0;
27340711Swollman}
27440711Swollman
27540711Swollmanstruct resource *
27688372Stmmrman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
27788372Stmm		      u_long count, u_long bound,  u_int flags,
27888372Stmm		      struct device *dev)
27940711Swollman{
28040711Swollman	u_int	want_activate;
281150523Sphk	struct	resource_i *r, *s, *rv;
28288372Stmm	u_long	rstart, rend, amask, bmask;
28340711Swollman
284152543Syongari	rv = NULL;
28540711Swollman
286160958Sjb	DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], "
287160958Sjb	       "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end,
288160958Sjb	       count, flags,
289160958Sjb	       dev == NULL ? "<null>" : device_get_nameunit(dev)));
29040711Swollman	want_activate = (flags & RF_ACTIVE);
29140711Swollman	flags &= ~RF_ACTIVE;
29240711Swollman
29372200Sbmilekic	mtx_lock(rm->rm_mtx);
29440711Swollman
295152543Syongari	for (r = TAILQ_FIRST(&rm->rm_list);
29668727Smckusick	     r && r->r_end < start;
29768727Smckusick	     r = TAILQ_NEXT(r, r_link))
29840711Swollman		;
29940711Swollman
30068727Smckusick	if (r == NULL) {
30159910Spaul		DPRINTF(("could not find a region\n"));
30240711Swollman		goto out;
30340711Swollman	}
30440711Swollman
30588372Stmm	amask = (1ul << RF_ALIGNMENT(flags)) - 1;
30688372Stmm	/* If bound is 0, bmask will also be 0 */
30788372Stmm	bmask = ~(bound - 1);
30840711Swollman	/*
30940711Swollman	 * First try to find an acceptable totally-unshared region.
31040711Swollman	 */
31168727Smckusick	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
31259910Spaul		DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
313143665Simp		if (s->r_start + count - 1 > end) {
314143665Simp			DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
315143665Simp			    s->r_start, end));
31640711Swollman			break;
31740711Swollman		}
31840711Swollman		if (s->r_flags & RF_ALLOCATED) {
31959910Spaul			DPRINTF(("region is allocated\n"));
32040711Swollman			continue;
32140711Swollman		}
32288372Stmm		rstart = ulmax(s->r_start, start);
32388372Stmm		/*
32488372Stmm		 * Try to find a region by adjusting to boundary and alignment
32588372Stmm		 * until both conditions are satisfied. This is not an optimal
32688372Stmm		 * algorithm, but in most cases it isn't really bad, either.
32788372Stmm		 */
32888372Stmm		do {
32988372Stmm			rstart = (rstart + amask) & ~amask;
330109646Stmm			if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
33188372Stmm				rstart += bound - (rstart & ~bmask);
33288372Stmm		} while ((rstart & amask) != 0 && rstart < end &&
33388372Stmm		    rstart < s->r_end);
334128172Simp		rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
335102572Siwasaki		if (rstart > rend) {
336102572Siwasaki			DPRINTF(("adjusted start exceeds end\n"));
337102572Siwasaki			continue;
338102572Siwasaki		}
33959910Spaul		DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
34059910Spaul		       rstart, rend, (rend - rstart + 1), count));
34140711Swollman
34240711Swollman		if ((rend - rstart + 1) >= count) {
34359910Spaul			DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
344143664Simp			       rstart, rend, (rend - rstart + 1)));
34540711Swollman			if ((s->r_end - s->r_start + 1) == count) {
34659910Spaul				DPRINTF(("candidate region is entire chunk\n"));
34740711Swollman				rv = s;
34848235Sdfr				rv->r_flags |= RF_ALLOCATED | flags;
34940711Swollman				rv->r_dev = dev;
35040711Swollman				goto out;
35140711Swollman			}
35240711Swollman
35340711Swollman			/*
35440711Swollman			 * If s->r_start < rstart and
35540711Swollman			 *    s->r_end > rstart + count - 1, then
35640711Swollman			 * we need to split the region into three pieces
35740711Swollman			 * (the middle one will get returned to the user).
35840711Swollman			 * Otherwise, we are allocating at either the
35940711Swollman			 * beginning or the end of s, so we only need to
36040711Swollman			 * split it in two.  The first case requires
36140711Swollman			 * two new allocations; the second requires but one.
36240711Swollman			 */
363150523Sphk			rv = int_alloc_resource(M_NOWAIT);
364152543Syongari			if (rv == NULL)
36540711Swollman				goto out;
36640711Swollman			rv->r_start = rstart;
36740711Swollman			rv->r_end = rstart + count - 1;
36840711Swollman			rv->r_flags = flags | RF_ALLOCATED;
36940711Swollman			rv->r_dev = dev;
37045720Speter			rv->r_rm = rm;
371152543Syongari
37240711Swollman			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
37359910Spaul				DPRINTF(("splitting region in three parts: "
37440711Swollman				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
37540711Swollman				       s->r_start, rv->r_start - 1,
37640711Swollman				       rv->r_start, rv->r_end,
37759910Spaul				       rv->r_end + 1, s->r_end));
37840711Swollman				/*
37940711Swollman				 * We are allocating in the middle.
38040711Swollman				 */
381150523Sphk				r = int_alloc_resource(M_NOWAIT);
382152543Syongari				if (r == NULL) {
38340711Swollman					free(rv, M_RMAN);
384152543Syongari					rv = NULL;
38540711Swollman					goto out;
38640711Swollman				}
38740711Swollman				r->r_start = rv->r_end + 1;
38840711Swollman				r->r_end = s->r_end;
38940711Swollman				r->r_flags = s->r_flags;
39045720Speter				r->r_rm = rm;
39140711Swollman				s->r_end = rv->r_start - 1;
39268727Smckusick				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
39340711Swollman						     r_link);
39468727Smckusick				TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
39540711Swollman						     r_link);
39640711Swollman			} else if (s->r_start == rv->r_start) {
39759910Spaul				DPRINTF(("allocating from the beginning\n"));
39840711Swollman				/*
39940711Swollman				 * We are allocating at the beginning.
40040711Swollman				 */
40140711Swollman				s->r_start = rv->r_end + 1;
40268727Smckusick				TAILQ_INSERT_BEFORE(s, rv, r_link);
40340711Swollman			} else {
40459910Spaul				DPRINTF(("allocating at the end\n"));
40540711Swollman				/*
40640711Swollman				 * We are allocating at the end.
40740711Swollman				 */
40840711Swollman				s->r_end = rv->r_start - 1;
40968727Smckusick				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
41040711Swollman						     r_link);
41140711Swollman			}
41240711Swollman			goto out;
41340711Swollman		}
41440711Swollman	}
41540711Swollman
41640711Swollman	/*
41740711Swollman	 * Now find an acceptable shared region, if the client's requirements
41840711Swollman	 * allow sharing.  By our implementation restriction, a candidate
41940711Swollman	 * region must match exactly by both size and sharing type in order
42040711Swollman	 * to be considered compatible with the client's request.  (The
42140711Swollman	 * former restriction could probably be lifted without too much
42240711Swollman	 * additional work, but this does not seem warranted.)
42340711Swollman	 */
42459910Spaul	DPRINTF(("no unshared regions found\n"));
42540711Swollman	if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
42640711Swollman		goto out;
42740711Swollman
42868727Smckusick	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
42940711Swollman		if (s->r_start > end)
43040711Swollman			break;
43140711Swollman		if ((s->r_flags & flags) != flags)
43240711Swollman			continue;
43388372Stmm		rstart = ulmax(s->r_start, start);
434128172Simp		rend = ulmin(s->r_end, ulmax(start + count - 1, end));
43540711Swollman		if (s->r_start >= start && s->r_end <= end
43688372Stmm		    && (s->r_end - s->r_start + 1) == count &&
43788372Stmm		    (s->r_start & amask) == 0 &&
43888372Stmm		    ((s->r_start ^ s->r_end) & bmask) == 0) {
439150523Sphk			rv = int_alloc_resource(M_NOWAIT);
440152543Syongari			if (rv == NULL)
44140711Swollman				goto out;
44240711Swollman			rv->r_start = s->r_start;
44340711Swollman			rv->r_end = s->r_end;
444152543Syongari			rv->r_flags = s->r_flags &
44540711Swollman				(RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
44640711Swollman			rv->r_dev = dev;
44740711Swollman			rv->r_rm = rm;
448152543Syongari			if (s->r_sharehead == NULL) {
44940711Swollman				s->r_sharehead = malloc(sizeof *s->r_sharehead,
45069781Sdwmalone						M_RMAN, M_NOWAIT | M_ZERO);
451152543Syongari				if (s->r_sharehead == NULL) {
45240711Swollman					free(rv, M_RMAN);
453152543Syongari					rv = NULL;
45440711Swollman					goto out;
45540711Swollman				}
45640711Swollman				LIST_INIT(s->r_sharehead);
457152543Syongari				LIST_INSERT_HEAD(s->r_sharehead, s,
45840711Swollman						 r_sharelink);
45945106Sdfr				s->r_flags |= RF_FIRSTSHARE;
46040711Swollman			}
46140711Swollman			rv->r_sharehead = s->r_sharehead;
46240711Swollman			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
46340711Swollman			goto out;
46440711Swollman		}
46540711Swollman	}
46640711Swollman
46740711Swollman	/*
46840711Swollman	 * We couldn't find anything.
46940711Swollman	 */
47040711Swollmanout:
47140711Swollman	/*
47240711Swollman	 * If the user specified RF_ACTIVE in the initial flags,
47340711Swollman	 * which is reflected in `want_activate', we attempt to atomically
47440711Swollman	 * activate the resource.  If this fails, we release the resource
47540711Swollman	 * and indicate overall failure.  (This behavior probably doesn't
47640711Swollman	 * make sense for RF_TIMESHARE-type resources.)
47740711Swollman	 */
47840711Swollman	if (rv && want_activate) {
479150523Sphk		struct resource_i *whohas;
48040711Swollman		if (int_rman_activate_resource(rm, rv, &whohas)) {
48140711Swollman			int_rman_release_resource(rm, rv);
482152543Syongari			rv = NULL;
48340711Swollman		}
48440711Swollman	}
485152543Syongari
48672200Sbmilekic	mtx_unlock(rm->rm_mtx);
487152543Syongari	return (rv == NULL ? NULL : &rv->r_r);
48840711Swollman}
48940711Swollman
49088372Stmmstruct resource *
49188372Stmmrman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
49288372Stmm		      u_int flags, struct device *dev)
49388372Stmm{
49488372Stmm
49588372Stmm	return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
49688372Stmm	    dev));
49788372Stmm}
49888372Stmm
49940711Swollmanstatic int
500150523Sphkint_rman_activate_resource(struct rman *rm, struct resource_i *r,
501150523Sphk			   struct resource_i **whohas)
50240711Swollman{
503150523Sphk	struct resource_i *s;
50440711Swollman	int ok;
50540711Swollman
50640711Swollman	/*
50740711Swollman	 * If we are not timesharing, then there is nothing much to do.
50840711Swollman	 * If we already have the resource, then there is nothing at all to do.
50940711Swollman	 * If we are not on a sharing list with anybody else, then there is
51040711Swollman	 * little to do.
51140711Swollman	 */
51240711Swollman	if ((r->r_flags & RF_TIMESHARE) == 0
51340711Swollman	    || (r->r_flags & RF_ACTIVE) != 0
514152543Syongari	    || r->r_sharehead == NULL) {
51540711Swollman		r->r_flags |= RF_ACTIVE;
51640711Swollman		return 0;
51740711Swollman	}
51840711Swollman
51940711Swollman	ok = 1;
52053225Sphk	for (s = LIST_FIRST(r->r_sharehead); s && ok;
52153225Sphk	     s = LIST_NEXT(s, r_sharelink)) {
52240711Swollman		if ((s->r_flags & RF_ACTIVE) != 0) {
52340711Swollman			ok = 0;
52440711Swollman			*whohas = s;
52540711Swollman		}
52640711Swollman	}
52740711Swollman	if (ok) {
52840711Swollman		r->r_flags |= RF_ACTIVE;
52940711Swollman		return 0;
53040711Swollman	}
53140711Swollman	return EBUSY;
53240711Swollman}
53340711Swollman
53440711Swollmanint
535150523Sphkrman_activate_resource(struct resource *re)
53640711Swollman{
53740711Swollman	int rv;
538150523Sphk	struct resource_i *r, *whohas;
53940711Swollman	struct rman *rm;
54040711Swollman
541150523Sphk	r = re->__r_i;
54240711Swollman	rm = r->r_rm;
54372200Sbmilekic	mtx_lock(rm->rm_mtx);
54440711Swollman	rv = int_rman_activate_resource(rm, r, &whohas);
54572200Sbmilekic	mtx_unlock(rm->rm_mtx);
54640711Swollman	return rv;
54740711Swollman}
54840711Swollman
54940711Swollmanint
550150523Sphkrman_await_resource(struct resource *re, int pri, int timo)
55140711Swollman{
55285519Sjhb	int	rv;
553150523Sphk	struct	resource_i *r, *whohas;
55440711Swollman	struct	rman *rm;
55540711Swollman
556150523Sphk	r = re->__r_i;
55740711Swollman	rm = r->r_rm;
55885519Sjhb	mtx_lock(rm->rm_mtx);
55940711Swollman	for (;;) {
56040711Swollman		rv = int_rman_activate_resource(rm, r, &whohas);
56140711Swollman		if (rv != EBUSY)
56271576Sjasone			return (rv);	/* returns with mutex held */
56340711Swollman
564152543Syongari		if (r->r_sharehead == NULL)
56540711Swollman			panic("rman_await_resource");
56640711Swollman		whohas->r_flags |= RF_WANTED;
56785519Sjhb		rv = msleep(r->r_sharehead, rm->rm_mtx, pri, "rmwait", timo);
56840711Swollman		if (rv) {
56985519Sjhb			mtx_unlock(rm->rm_mtx);
57085519Sjhb			return (rv);
57140711Swollman		}
57240711Swollman	}
57340711Swollman}
57440711Swollman
57545720Speterstatic int
576150523Sphkint_rman_deactivate_resource(struct resource_i *r)
57740711Swollman{
57840711Swollman
57940711Swollman	r->r_flags &= ~RF_ACTIVE;
58040711Swollman	if (r->r_flags & RF_WANTED) {
58140711Swollman		r->r_flags &= ~RF_WANTED;
58240711Swollman		wakeup(r->r_sharehead);
58340711Swollman	}
58445720Speter	return 0;
58545720Speter}
58645720Speter
58745720Speterint
58845720Speterrman_deactivate_resource(struct resource *r)
58945720Speter{
59045720Speter	struct	rman *rm;
59145720Speter
592150523Sphk	rm = r->__r_i->r_rm;
59372200Sbmilekic	mtx_lock(rm->rm_mtx);
594150523Sphk	int_rman_deactivate_resource(r->__r_i);
59572200Sbmilekic	mtx_unlock(rm->rm_mtx);
59640711Swollman	return 0;
59740711Swollman}
59840711Swollman
59940711Swollmanstatic int
600150523Sphkint_rman_release_resource(struct rman *rm, struct resource_i *r)
60140711Swollman{
602150523Sphk	struct	resource_i *s, *t;
60340711Swollman
60440711Swollman	if (r->r_flags & RF_ACTIVE)
60545720Speter		int_rman_deactivate_resource(r);
60640711Swollman
60740711Swollman	/*
60840711Swollman	 * Check for a sharing list first.  If there is one, then we don't
60940711Swollman	 * have to think as hard.
61040711Swollman	 */
61140711Swollman	if (r->r_sharehead) {
61240711Swollman		/*
61340711Swollman		 * If a sharing list exists, then we know there are at
61440711Swollman		 * least two sharers.
61540711Swollman		 *
61640711Swollman		 * If we are in the main circleq, appoint someone else.
61740711Swollman		 */
61840711Swollman		LIST_REMOVE(r, r_sharelink);
61953225Sphk		s = LIST_FIRST(r->r_sharehead);
62040711Swollman		if (r->r_flags & RF_FIRSTSHARE) {
62140711Swollman			s->r_flags |= RF_FIRSTSHARE;
62268727Smckusick			TAILQ_INSERT_BEFORE(r, s, r_link);
62368727Smckusick			TAILQ_REMOVE(&rm->rm_list, r, r_link);
62440711Swollman		}
62540711Swollman
62640711Swollman		/*
62740711Swollman		 * Make sure that the sharing list goes away completely
62840711Swollman		 * if the resource is no longer being shared at all.
62940711Swollman		 */
630152543Syongari		if (LIST_NEXT(s, r_sharelink) == NULL) {
63140711Swollman			free(s->r_sharehead, M_RMAN);
632152543Syongari			s->r_sharehead = NULL;
63340711Swollman			s->r_flags &= ~RF_FIRSTSHARE;
63440711Swollman		}
63540711Swollman		goto out;
63640711Swollman	}
63740711Swollman
63840711Swollman	/*
63940711Swollman	 * Look at the adjacent resources in the list and see if our
640133177Sjhb	 * segment can be merged with any of them.  If either of the
641133177Sjhb	 * resources is allocated or is not exactly adjacent then they
642133177Sjhb	 * cannot be merged with our segment.
64340711Swollman	 */
64468727Smckusick	s = TAILQ_PREV(r, resource_head, r_link);
645133177Sjhb	if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
646133177Sjhb	    s->r_end + 1 != r->r_start))
647133177Sjhb		s = NULL;
64868727Smckusick	t = TAILQ_NEXT(r, r_link);
649133177Sjhb	if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
650133177Sjhb	    r->r_end + 1 != t->r_start))
651133177Sjhb		t = NULL;
65240711Swollman
653133177Sjhb	if (s != NULL && t != NULL) {
65440711Swollman		/*
65540711Swollman		 * Merge all three segments.
65640711Swollman		 */
65740711Swollman		s->r_end = t->r_end;
65868727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
65968727Smckusick		TAILQ_REMOVE(&rm->rm_list, t, r_link);
66040711Swollman		free(t, M_RMAN);
661133177Sjhb	} else if (s != NULL) {
66240711Swollman		/*
66340711Swollman		 * Merge previous segment with ours.
66440711Swollman		 */
66540711Swollman		s->r_end = r->r_end;
66668727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
667133177Sjhb	} else if (t != NULL) {
66840711Swollman		/*
66940711Swollman		 * Merge next segment with ours.
67040711Swollman		 */
67140711Swollman		t->r_start = r->r_start;
67268727Smckusick		TAILQ_REMOVE(&rm->rm_list, r, r_link);
67340711Swollman	} else {
67440711Swollman		/*
67540711Swollman		 * At this point, we know there is nothing we
67640711Swollman		 * can potentially merge with, because on each
67740711Swollman		 * side, there is either nothing there or what is
67840711Swollman		 * there is still allocated.  In that case, we don't
67940711Swollman		 * want to remove r from the list; we simply want to
68040711Swollman		 * change it to an unallocated region and return
68140711Swollman		 * without freeing anything.
68240711Swollman		 */
68340711Swollman		r->r_flags &= ~RF_ALLOCATED;
68440711Swollman		return 0;
68540711Swollman	}
68640711Swollman
68740711Swollmanout:
68840711Swollman	free(r, M_RMAN);
68940711Swollman	return 0;
69040711Swollman}
69140711Swollman
69240711Swollmanint
693150523Sphkrman_release_resource(struct resource *re)
69440711Swollman{
69540711Swollman	int	rv;
696150523Sphk	struct	resource_i *r;
697150523Sphk	struct	rman *rm;
69840711Swollman
699150523Sphk	r = re->__r_i;
700150523Sphk	rm = r->r_rm;
70172200Sbmilekic	mtx_lock(rm->rm_mtx);
70240711Swollman	rv = int_rman_release_resource(rm, r);
70372200Sbmilekic	mtx_unlock(rm->rm_mtx);
70440711Swollman	return (rv);
70540711Swollman}
70667261Simp
70767261Simpuint32_t
70867261Simprman_make_alignment_flags(uint32_t size)
70967261Simp{
71067261Simp	int	i;
71167261Simp
71267425Simp	/*
71367425Simp	 * Find the hightest bit set, and add one if more than one bit
71467425Simp	 * set.  We're effectively computing the ceil(log2(size)) here.
71567425Simp	 */
71688372Stmm	for (i = 31; i > 0; i--)
71767425Simp		if ((1 << i) & size)
71867425Simp			break;
71967425Simp	if (~(1 << i) & size)
72067425Simp		i++;
72167261Simp
72267261Simp	return(RF_ALIGNMENT_LOG2(i));
72367425Simp}
724107296Simp
725182162Sjhbvoid
726182162Sjhbrman_set_start(struct resource *r, u_long start)
727182162Sjhb{
728182162Sjhb	r->__r_i->r_start = start;
729182162Sjhb}
730182162Sjhb
731107296Simpu_long
732107296Simprman_get_start(struct resource *r)
733107296Simp{
734150523Sphk	return (r->__r_i->r_start);
735107296Simp}
736107296Simp
737182162Sjhbvoid
738182162Sjhbrman_set_end(struct resource *r, u_long end)
739182162Sjhb{
740182162Sjhb	r->__r_i->r_end = end;
741182162Sjhb}
742182162Sjhb
743107296Simpu_long
744107296Simprman_get_end(struct resource *r)
745107296Simp{
746150523Sphk	return (r->__r_i->r_end);
747107296Simp}
748107296Simp
749107296Simpu_long
750107296Simprman_get_size(struct resource *r)
751107296Simp{
752150523Sphk	return (r->__r_i->r_end - r->__r_i->r_start + 1);
753107296Simp}
754107296Simp
755107296Simpu_int
756107296Simprman_get_flags(struct resource *r)
757107296Simp{
758150523Sphk	return (r->__r_i->r_flags);
759107296Simp}
760107296Simp
761107296Simpvoid
762107296Simprman_set_virtual(struct resource *r, void *v)
763107296Simp{
764150523Sphk	r->__r_i->r_virtual = v;
765107296Simp}
766107296Simp
767107296Simpvoid *
768107296Simprman_get_virtual(struct resource *r)
769107296Simp{
770150523Sphk	return (r->__r_i->r_virtual);
771107296Simp}
772107296Simp
773107296Simpvoid
774107296Simprman_set_bustag(struct resource *r, bus_space_tag_t t)
775107296Simp{
776107296Simp	r->r_bustag = t;
777107296Simp}
778107296Simp
779107296Simpbus_space_tag_t
780107296Simprman_get_bustag(struct resource *r)
781107296Simp{
782107296Simp	return (r->r_bustag);
783107296Simp}
784107296Simp
785107296Simpvoid
786107296Simprman_set_bushandle(struct resource *r, bus_space_handle_t h)
787107296Simp{
788107296Simp	r->r_bushandle = h;
789107296Simp}
790107296Simp
791107296Simpbus_space_handle_t
792107296Simprman_get_bushandle(struct resource *r)
793107296Simp{
794107296Simp	return (r->r_bushandle);
795107296Simp}
796107296Simp
797107296Simpvoid
798107296Simprman_set_rid(struct resource *r, int rid)
799107296Simp{
800150523Sphk	r->__r_i->r_rid = rid;
801107296Simp}
802107296Simp
803182162Sjhbint
804182162Sjhbrman_get_rid(struct resource *r)
805131414Simp{
806182162Sjhb	return (r->__r_i->r_rid);
807131414Simp}
808131414Simp
809131414Simpvoid
810182162Sjhbrman_set_device(struct resource *r, struct device *dev)
811131414Simp{
812182162Sjhb	r->__r_i->r_dev = dev;
813131414Simp}
814131414Simp
815110753Simpstruct device *
816110753Simprman_get_device(struct resource *r)
817110753Simp{
818150523Sphk	return (r->__r_i->r_dev);
819110753Simp}
820144071Sphk
821150547Sphkint
822150547Sphkrman_is_region_manager(struct resource *r, struct rman *rm)
823150547Sphk{
824150547Sphk
825150547Sphk	return (r->__r_i->r_rm == rm);
826150547Sphk}
827150547Sphk
828144071Sphk/*
829144071Sphk * Sysctl interface for scanning the resource lists.
830144071Sphk *
831144071Sphk * We take two input parameters; the index into the list of resource
832144071Sphk * managers, and the resource offset into the list.
833144071Sphk */
834144071Sphkstatic int
835144071Sphksysctl_rman(SYSCTL_HANDLER_ARGS)
836144071Sphk{
837144071Sphk	int			*name = (int *)arg1;
838144071Sphk	u_int			namelen = arg2;
839144071Sphk	int			rman_idx, res_idx;
840144071Sphk	struct rman		*rm;
841150523Sphk	struct resource_i	*res;
842192379Savg	struct resource_i	*sres;
843144071Sphk	struct u_rman		urm;
844144071Sphk	struct u_resource	ures;
845144071Sphk	int			error;
846144071Sphk
847144071Sphk	if (namelen != 3)
848144071Sphk		return (EINVAL);
849144071Sphk
850144071Sphk	if (bus_data_generation_check(name[0]))
851144071Sphk		return (EINVAL);
852144071Sphk	rman_idx = name[1];
853144071Sphk	res_idx = name[2];
854144071Sphk
855144071Sphk	/*
856144071Sphk	 * Find the indexed resource manager
857144071Sphk	 */
858152543Syongari	mtx_lock(&rman_mtx);
859144071Sphk	TAILQ_FOREACH(rm, &rman_head, rm_link) {
860144071Sphk		if (rman_idx-- == 0)
861144071Sphk			break;
862144071Sphk	}
863152543Syongari	mtx_unlock(&rman_mtx);
864144071Sphk	if (rm == NULL)
865144071Sphk		return (ENOENT);
866144071Sphk
867144071Sphk	/*
868144071Sphk	 * If the resource index is -1, we want details on the
869144071Sphk	 * resource manager.
870144071Sphk	 */
871144071Sphk	if (res_idx == -1) {
872145953Scperciva		bzero(&urm, sizeof(urm));
873144071Sphk		urm.rm_handle = (uintptr_t)rm;
874184173Smarcel		if (rm->rm_descr != NULL)
875184173Smarcel			strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
876144071Sphk		urm.rm_start = rm->rm_start;
877144071Sphk		urm.rm_size = rm->rm_end - rm->rm_start + 1;
878144071Sphk		urm.rm_type = rm->rm_type;
879144071Sphk
880144071Sphk		error = SYSCTL_OUT(req, &urm, sizeof(urm));
881144071Sphk		return (error);
882144071Sphk	}
883144071Sphk
884144071Sphk	/*
885144071Sphk	 * Find the indexed resource and return it.
886144071Sphk	 */
887152543Syongari	mtx_lock(rm->rm_mtx);
888144071Sphk	TAILQ_FOREACH(res, &rm->rm_list, r_link) {
889192379Savg		if (res->r_sharehead != NULL) {
890192379Savg			LIST_FOREACH(sres, res->r_sharehead, r_sharelink)
891192379Savg				if (res_idx-- == 0) {
892192379Savg					res = sres;
893192379Savg					goto found;
894144071Sphk				}
895192379Savg		}
896192379Savg		else if (res_idx-- == 0)
897192379Savg				goto found;
898192379Savg	}
899192379Savg	mtx_unlock(rm->rm_mtx);
900192379Savg	return (ENOENT);
901144071Sphk
902192379Savgfound:
903192379Savg	bzero(&ures, sizeof(ures));
904192379Savg	ures.r_handle = (uintptr_t)res;
905192379Savg	ures.r_parent = (uintptr_t)res->r_rm;
906192379Savg	ures.r_device = (uintptr_t)res->r_dev;
907192379Savg	if (res->r_dev != NULL) {
908192379Savg		if (device_get_name(res->r_dev) != NULL) {
909192379Savg			snprintf(ures.r_devname, RM_TEXTLEN,
910192379Savg			    "%s%d",
911192379Savg			    device_get_name(res->r_dev),
912192379Savg			    device_get_unit(res->r_dev));
913192379Savg		} else {
914192379Savg			strlcpy(ures.r_devname, "nomatch",
915192379Savg			    RM_TEXTLEN);
916144071Sphk		}
917192379Savg	} else {
918192379Savg		ures.r_devname[0] = '\0';
919144071Sphk	}
920192379Savg	ures.r_start = res->r_start;
921192379Savg	ures.r_size = res->r_end - res->r_start + 1;
922192379Savg	ures.r_flags = res->r_flags;
923192379Savg
924152543Syongari	mtx_unlock(rm->rm_mtx);
925192379Savg	error = SYSCTL_OUT(req, &ures, sizeof(ures));
926192379Savg	return (error);
927144071Sphk}
928144071Sphk
929144071SphkSYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
930144071Sphk    "kernel resource manager");
931168791Sjhb
932168791Sjhb#ifdef DDB
933168791Sjhbstatic void
934220606Sgavindump_rman_header(struct rman *rm)
935220606Sgavin{
936220606Sgavin
937220606Sgavin	if (db_pager_quit)
938220606Sgavin		return;
939220606Sgavin	db_printf("rman %p: %s (0x%lx-0x%lx full range)\n",
940220606Sgavin	    rm, rm->rm_descr, rm->rm_start, rm->rm_end);
941220606Sgavin}
942220606Sgavin
943220606Sgavinstatic void
944168791Sjhbdump_rman(struct rman *rm)
945168791Sjhb{
946168791Sjhb	struct resource_i *r;
947168791Sjhb	const char *devname;
948168791Sjhb
949168791Sjhb	if (db_pager_quit)
950168791Sjhb		return;
951168791Sjhb	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
952168791Sjhb		if (r->r_dev != NULL) {
953168791Sjhb			devname = device_get_nameunit(r->r_dev);
954168791Sjhb			if (devname == NULL)
955168791Sjhb				devname = "nomatch";
956168791Sjhb		} else
957168791Sjhb			devname = NULL;
958168791Sjhb		db_printf("    0x%lx-0x%lx ", r->r_start, r->r_end);
959168791Sjhb		if (devname != NULL)
960168791Sjhb			db_printf("(%s)\n", devname);
961168791Sjhb		else
962168791Sjhb			db_printf("----\n");
963168791Sjhb		if (db_pager_quit)
964168791Sjhb			return;
965168791Sjhb	}
966168791Sjhb}
967168791Sjhb
968168791SjhbDB_SHOW_COMMAND(rman, db_show_rman)
969168791Sjhb{
970168791Sjhb
971220606Sgavin	if (have_addr) {
972220606Sgavin		dump_rman_header((struct rman *)addr);
973168791Sjhb		dump_rman((struct rman *)addr);
974220606Sgavin	}
975168791Sjhb}
976168791Sjhb
977220606SgavinDB_SHOW_COMMAND(rmans, db_show_rmans)
978220606Sgavin{
979220606Sgavin	struct rman *rm;
980220606Sgavin
981220606Sgavin	TAILQ_FOREACH(rm, &rman_head, rm_link) {
982220606Sgavin		dump_rman_header(rm);
983220606Sgavin	}
984220606Sgavin}
985220606Sgavin
986183054SsamDB_SHOW_ALL_COMMAND(rman, db_show_all_rman)
987168791Sjhb{
988168791Sjhb	struct rman *rm;
989168791Sjhb
990220606Sgavin	TAILQ_FOREACH(rm, &rman_head, rm_link) {
991220606Sgavin		dump_rman_header(rm);
992168791Sjhb		dump_rman(rm);
993220606Sgavin	}
994168791Sjhb}
995183054SsamDB_SHOW_ALIAS(allrman, db_show_all_rman);
996168791Sjhb#endif
997