1/*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.  M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose.  It is provided "as is" without express or implied
14 * warranty.
15 *
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * The kernel resource manager.  This code is responsible for keeping track
32 * of hardware resources which are apportioned out to various drivers.
33 * It does not actually assign those resources, and it is not expected
34 * that end-device drivers will call into this code directly.  Rather,
35 * the code which implements the buses that those devices are attached to,
36 * and the code which manages CPU resources, will call this code, and the
37 * end-device drivers will make upcalls to that code to actually perform
38 * the allocation.
39 *
40 * There are two sorts of resources managed by this code.  The first is
41 * the more familiar array (RMAN_ARRAY) type; resources in this class
42 * consist of a sequence of individually-allocatable objects which have
43 * been numbered in some well-defined order.  Most of the resources
44 * are of this type, as it is the most familiar.  The second type is
45 * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
46 * resources in which each instance is indistinguishable from every
47 * other instance).  The principal anticipated application of gauges
48 * is in the context of power consumption, where a bus may have a specific
49 * power budget which all attached devices share.  RMAN_GAUGE is not
50 * implemented yet.
51 *
52 * For array resources, we make one simplifying assumption: two clients
53 * sharing the same resource must use the same range of indices.  That
54 * is to say, sharing of overlapping-but-not-identical regions is not
55 * permitted.
56 */
57
58#include "opt_ddb.h"
59
60#include <sys/cdefs.h>
61__FBSDID("$FreeBSD$");
62
63#include <sys/param.h>
64#include <sys/systm.h>
65#include <sys/kernel.h>
66#include <sys/limits.h>
67#include <sys/lock.h>
68#include <sys/malloc.h>
69#include <sys/mutex.h>
70#include <sys/bus.h>		/* XXX debugging */
71#include <machine/bus.h>
72#include <sys/rman.h>
73#include <sys/sysctl.h>
74
75#ifdef DDB
76#include <ddb/ddb.h>
77#endif
78
79/*
80 * We use a linked list rather than a bitmap because we need to be able to
81 * represent potentially huge objects (like all of a processor's physical
82 * address space).  That is also why the indices are defined to have type
83 * `unsigned long' -- that being the largest integral type in ISO C (1990).
84 * The 1999 version of C allows `long long'; we may need to switch to that
85 * at some point in the future, particularly if we want to support 36-bit
86 * addresses on IA32 hardware.
87 */
88struct resource_i {
89	struct resource		r_r;
90	TAILQ_ENTRY(resource_i)	r_link;
91	LIST_ENTRY(resource_i)	r_sharelink;
92	LIST_HEAD(, resource_i)	*r_sharehead;
93	u_long	r_start;	/* index of the first entry in this resource */
94	u_long	r_end;		/* index of the last entry (inclusive) */
95	u_int	r_flags;
96	void	*r_virtual;	/* virtual address of this resource */
97	struct device *r_dev;	/* device which has allocated this resource */
98	struct rman *r_rm;	/* resource manager from whence this came */
99	int	r_rid;		/* optional rid for this resource. */
100};
101
102static int rman_debug = 0;
103TUNABLE_INT("debug.rman_debug", &rman_debug);
104SYSCTL_INT(_debug, OID_AUTO, rman_debug, CTLFLAG_RW,
105    &rman_debug, 0, "rman debug");
106
107#define DPRINTF(params) if (rman_debug) printf params
108
109static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
110
111struct rman_head rman_head;
112static struct mtx rman_mtx; /* mutex to protect rman_head */
113static int int_rman_release_resource(struct rman *rm, struct resource_i *r);
114
115static __inline struct resource_i *
116int_alloc_resource(int malloc_flag)
117{
118	struct resource_i *r;
119
120	r = malloc(sizeof *r, M_RMAN, malloc_flag | M_ZERO);
121	if (r != NULL) {
122		r->r_r.__r_i = r;
123	}
124	return (r);
125}
126
127int
128rman_init(struct rman *rm)
129{
130	static int once = 0;
131
132	if (once == 0) {
133		once = 1;
134		TAILQ_INIT(&rman_head);
135		mtx_init(&rman_mtx, "rman head", NULL, MTX_DEF);
136	}
137
138	if (rm->rm_start == 0 && rm->rm_end == 0)
139		rm->rm_end = ~0ul;
140	if (rm->rm_type == RMAN_UNINIT)
141		panic("rman_init");
142	if (rm->rm_type == RMAN_GAUGE)
143		panic("implement RMAN_GAUGE");
144
145	TAILQ_INIT(&rm->rm_list);
146	rm->rm_mtx = malloc(sizeof *rm->rm_mtx, M_RMAN, M_NOWAIT | M_ZERO);
147	if (rm->rm_mtx == NULL)
148		return ENOMEM;
149	mtx_init(rm->rm_mtx, "rman", NULL, MTX_DEF);
150
151	mtx_lock(&rman_mtx);
152	TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
153	mtx_unlock(&rman_mtx);
154	return 0;
155}
156
157int
158rman_manage_region(struct rman *rm, u_long start, u_long end)
159{
160	struct resource_i *r, *s, *t;
161	int rv = 0;
162
163	DPRINTF(("rman_manage_region: <%s> request: start %#lx, end %#lx\n",
164	    rm->rm_descr, start, end));
165	if (start < rm->rm_start || end > rm->rm_end)
166		return EINVAL;
167	r = int_alloc_resource(M_NOWAIT);
168	if (r == NULL)
169		return ENOMEM;
170	r->r_start = start;
171	r->r_end = end;
172	r->r_rm = rm;
173
174	mtx_lock(rm->rm_mtx);
175
176	/* Skip entries before us. */
177	TAILQ_FOREACH(s, &rm->rm_list, r_link) {
178		if (s->r_end == ULONG_MAX)
179			break;
180		if (s->r_end + 1 >= r->r_start)
181			break;
182	}
183
184	/* If we ran off the end of the list, insert at the tail. */
185	if (s == NULL) {
186		TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
187	} else {
188		/* Check for any overlap with the current region. */
189		if (r->r_start <= s->r_end && r->r_end >= s->r_start) {
190			rv = EBUSY;
191			goto out;
192		}
193
194		/* Check for any overlap with the next region. */
195		t = TAILQ_NEXT(s, r_link);
196		if (t && r->r_start <= t->r_end && r->r_end >= t->r_start) {
197			rv = EBUSY;
198			goto out;
199		}
200
201		/*
202		 * See if this region can be merged with the next region.  If
203		 * not, clear the pointer.
204		 */
205		if (t && (r->r_end + 1 != t->r_start || t->r_flags != 0))
206			t = NULL;
207
208		/* See if we can merge with the current region. */
209		if (s->r_end + 1 == r->r_start && s->r_flags == 0) {
210			/* Can we merge all 3 regions? */
211			if (t != NULL) {
212				s->r_end = t->r_end;
213				TAILQ_REMOVE(&rm->rm_list, t, r_link);
214				free(r, M_RMAN);
215				free(t, M_RMAN);
216			} else {
217				s->r_end = r->r_end;
218				free(r, M_RMAN);
219			}
220		} else if (t != NULL) {
221			/* Can we merge with just the next region? */
222			t->r_start = r->r_start;
223			free(r, M_RMAN);
224		} else if (s->r_end < r->r_start) {
225			TAILQ_INSERT_AFTER(&rm->rm_list, s, r, r_link);
226		} else {
227			TAILQ_INSERT_BEFORE(s, r, r_link);
228		}
229	}
230out:
231	mtx_unlock(rm->rm_mtx);
232	return rv;
233}
234
235int
236rman_init_from_resource(struct rman *rm, struct resource *r)
237{
238	int rv;
239
240	if ((rv = rman_init(rm)) != 0)
241		return (rv);
242	return (rman_manage_region(rm, r->__r_i->r_start, r->__r_i->r_end));
243}
244
245int
246rman_fini(struct rman *rm)
247{
248	struct resource_i *r;
249
250	mtx_lock(rm->rm_mtx);
251	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
252		if (r->r_flags & RF_ALLOCATED) {
253			mtx_unlock(rm->rm_mtx);
254			return EBUSY;
255		}
256	}
257
258	/*
259	 * There really should only be one of these if we are in this
260	 * state and the code is working properly, but it can't hurt.
261	 */
262	while (!TAILQ_EMPTY(&rm->rm_list)) {
263		r = TAILQ_FIRST(&rm->rm_list);
264		TAILQ_REMOVE(&rm->rm_list, r, r_link);
265		free(r, M_RMAN);
266	}
267	mtx_unlock(rm->rm_mtx);
268	mtx_lock(&rman_mtx);
269	TAILQ_REMOVE(&rman_head, rm, rm_link);
270	mtx_unlock(&rman_mtx);
271	mtx_destroy(rm->rm_mtx);
272	free(rm->rm_mtx, M_RMAN);
273
274	return 0;
275}
276
277int
278rman_first_free_region(struct rman *rm, u_long *start, u_long *end)
279{
280	struct resource_i *r;
281
282	mtx_lock(rm->rm_mtx);
283	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
284		if (!(r->r_flags & RF_ALLOCATED)) {
285			*start = r->r_start;
286			*end = r->r_end;
287			mtx_unlock(rm->rm_mtx);
288			return (0);
289		}
290	}
291	mtx_unlock(rm->rm_mtx);
292	return (ENOENT);
293}
294
295int
296rman_last_free_region(struct rman *rm, u_long *start, u_long *end)
297{
298	struct resource_i *r;
299
300	mtx_lock(rm->rm_mtx);
301	TAILQ_FOREACH_REVERSE(r, &rm->rm_list, resource_head, r_link) {
302		if (!(r->r_flags & RF_ALLOCATED)) {
303			*start = r->r_start;
304			*end = r->r_end;
305			mtx_unlock(rm->rm_mtx);
306			return (0);
307		}
308	}
309	mtx_unlock(rm->rm_mtx);
310	return (ENOENT);
311}
312
313/* Shrink or extend one or both ends of an allocated resource. */
314int
315rman_adjust_resource(struct resource *rr, u_long start, u_long end)
316{
317	struct resource_i *r, *s, *t, *new;
318	struct rman *rm;
319
320	/* Not supported for shared resources. */
321	r = rr->__r_i;
322	if (r->r_flags & RF_SHAREABLE)
323		return (EINVAL);
324
325	/*
326	 * This does not support wholesale moving of a resource.  At
327	 * least part of the desired new range must overlap with the
328	 * existing resource.
329	 */
330	if (end < r->r_start || r->r_end < start)
331		return (EINVAL);
332
333	/*
334	 * Find the two resource regions immediately adjacent to the
335	 * allocated resource.
336	 */
337	rm = r->r_rm;
338	mtx_lock(rm->rm_mtx);
339#ifdef INVARIANTS
340	TAILQ_FOREACH(s, &rm->rm_list, r_link) {
341		if (s == r)
342			break;
343	}
344	if (s == NULL)
345		panic("resource not in list");
346#endif
347	s = TAILQ_PREV(r, resource_head, r_link);
348	t = TAILQ_NEXT(r, r_link);
349	KASSERT(s == NULL || s->r_end + 1 == r->r_start,
350	    ("prev resource mismatch"));
351	KASSERT(t == NULL || r->r_end + 1 == t->r_start,
352	    ("next resource mismatch"));
353
354	/*
355	 * See if the changes are permitted.  Shrinking is always allowed,
356	 * but growing requires sufficient room in the adjacent region.
357	 */
358	if (start < r->r_start && (s == NULL || (s->r_flags & RF_ALLOCATED) ||
359	    s->r_start > start)) {
360		mtx_unlock(rm->rm_mtx);
361		return (EBUSY);
362	}
363	if (end > r->r_end && (t == NULL || (t->r_flags & RF_ALLOCATED) ||
364	    t->r_end < end)) {
365		mtx_unlock(rm->rm_mtx);
366		return (EBUSY);
367	}
368
369	/*
370	 * While holding the lock, grow either end of the resource as
371	 * needed and shrink either end if the shrinking does not require
372	 * allocating a new resource.  We can safely drop the lock and then
373	 * insert a new range to handle the shrinking case afterwards.
374	 */
375	if (start < r->r_start ||
376	    (start > r->r_start && s != NULL && !(s->r_flags & RF_ALLOCATED))) {
377		KASSERT(s->r_flags == 0, ("prev is busy"));
378		r->r_start = start;
379		if (s->r_start == start) {
380			TAILQ_REMOVE(&rm->rm_list, s, r_link);
381			free(s, M_RMAN);
382		} else
383			s->r_end = start - 1;
384	}
385	if (end > r->r_end ||
386	    (end < r->r_end && t != NULL && !(t->r_flags & RF_ALLOCATED))) {
387		KASSERT(t->r_flags == 0, ("next is busy"));
388		r->r_end = end;
389		if (t->r_end == end) {
390			TAILQ_REMOVE(&rm->rm_list, t, r_link);
391			free(t, M_RMAN);
392		} else
393			t->r_start = end + 1;
394	}
395	mtx_unlock(rm->rm_mtx);
396
397	/*
398	 * Handle the shrinking cases that require allocating a new
399	 * resource to hold the newly-free region.  We have to recheck
400	 * if we still need this new region after acquiring the lock.
401	 */
402	if (start > r->r_start) {
403		new = int_alloc_resource(M_WAITOK);
404		new->r_start = r->r_start;
405		new->r_end = start - 1;
406		new->r_rm = rm;
407		mtx_lock(rm->rm_mtx);
408		r->r_start = start;
409		s = TAILQ_PREV(r, resource_head, r_link);
410		if (s != NULL && !(s->r_flags & RF_ALLOCATED)) {
411			s->r_end = start - 1;
412			free(new, M_RMAN);
413		} else
414			TAILQ_INSERT_BEFORE(r, new, r_link);
415		mtx_unlock(rm->rm_mtx);
416	}
417	if (end < r->r_end) {
418		new = int_alloc_resource(M_WAITOK);
419		new->r_start = end + 1;
420		new->r_end = r->r_end;
421		new->r_rm = rm;
422		mtx_lock(rm->rm_mtx);
423		r->r_end = end;
424		t = TAILQ_NEXT(r, r_link);
425		if (t != NULL && !(t->r_flags & RF_ALLOCATED)) {
426			t->r_start = end + 1;
427			free(new, M_RMAN);
428		} else
429			TAILQ_INSERT_AFTER(&rm->rm_list, r, new, r_link);
430		mtx_unlock(rm->rm_mtx);
431	}
432	return (0);
433}
434
435#define	SHARE_TYPE(f)	(f & (RF_SHAREABLE | RF_PREFETCHABLE))
436
437struct resource *
438rman_reserve_resource_bound(struct rman *rm, u_long start, u_long end,
439			    u_long count, u_long bound, u_int flags,
440			    struct device *dev)
441{
442	u_int new_rflags;
443	struct resource_i *r, *s, *rv;
444	u_long rstart, rend, amask, bmask;
445
446	rv = NULL;
447
448	DPRINTF(("rman_reserve_resource_bound: <%s> request: [%#lx, %#lx], "
449	       "length %#lx, flags %u, device %s\n", rm->rm_descr, start, end,
450	       count, flags,
451	       dev == NULL ? "<null>" : device_get_nameunit(dev)));
452	KASSERT((flags & RF_FIRSTSHARE) == 0,
453	    ("invalid flags %#x", flags));
454	new_rflags = (flags & ~RF_FIRSTSHARE) | RF_ALLOCATED;
455
456	mtx_lock(rm->rm_mtx);
457
458	for (r = TAILQ_FIRST(&rm->rm_list);
459	     r && r->r_end < start + count - 1;
460	     r = TAILQ_NEXT(r, r_link))
461		;
462
463	if (r == NULL) {
464		DPRINTF(("could not find a region\n"));
465		goto out;
466	}
467
468	amask = (1ul << RF_ALIGNMENT(flags)) - 1;
469	KASSERT(start <= ULONG_MAX - amask,
470	    ("start (%#lx) + amask (%#lx) would wrap around", start, amask));
471
472	/* If bound is 0, bmask will also be 0 */
473	bmask = ~(bound - 1);
474	/*
475	 * First try to find an acceptable totally-unshared region.
476	 */
477	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
478		DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
479		/*
480		 * The resource list is sorted, so there is no point in
481		 * searching further once r_start is too large.
482		 */
483		if (s->r_start > end - (count - 1)) {
484			DPRINTF(("s->r_start (%#lx) + count - 1> end (%#lx)\n",
485			    s->r_start, end));
486			break;
487		}
488		if (s->r_start > ULONG_MAX - amask) {
489			DPRINTF(("s->r_start (%#lx) + amask (%#lx) too large\n",
490			    s->r_start, amask));
491			break;
492		}
493		if (s->r_flags & RF_ALLOCATED) {
494			DPRINTF(("region is allocated\n"));
495			continue;
496		}
497		rstart = ulmax(s->r_start, start);
498		/*
499		 * Try to find a region by adjusting to boundary and alignment
500		 * until both conditions are satisfied. This is not an optimal
501		 * algorithm, but in most cases it isn't really bad, either.
502		 */
503		do {
504			rstart = (rstart + amask) & ~amask;
505			if (((rstart ^ (rstart + count - 1)) & bmask) != 0)
506				rstart += bound - (rstart & ~bmask);
507		} while ((rstart & amask) != 0 && rstart < end &&
508		    rstart < s->r_end);
509		rend = ulmin(s->r_end, ulmax(rstart + count - 1, end));
510		if (rstart > rend) {
511			DPRINTF(("adjusted start exceeds end\n"));
512			continue;
513		}
514		DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
515		       rstart, rend, (rend - rstart + 1), count));
516
517		if ((rend - rstart + 1) >= count) {
518			DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
519			       rstart, rend, (rend - rstart + 1)));
520			if ((s->r_end - s->r_start + 1) == count) {
521				DPRINTF(("candidate region is entire chunk\n"));
522				rv = s;
523				rv->r_flags = new_rflags;
524				rv->r_dev = dev;
525				goto out;
526			}
527
528			/*
529			 * If s->r_start < rstart and
530			 *    s->r_end > rstart + count - 1, then
531			 * we need to split the region into three pieces
532			 * (the middle one will get returned to the user).
533			 * Otherwise, we are allocating at either the
534			 * beginning or the end of s, so we only need to
535			 * split it in two.  The first case requires
536			 * two new allocations; the second requires but one.
537			 */
538			rv = int_alloc_resource(M_NOWAIT);
539			if (rv == NULL)
540				goto out;
541			rv->r_start = rstart;
542			rv->r_end = rstart + count - 1;
543			rv->r_flags = new_rflags;
544			rv->r_dev = dev;
545			rv->r_rm = rm;
546
547			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
548				DPRINTF(("splitting region in three parts: "
549				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
550				       s->r_start, rv->r_start - 1,
551				       rv->r_start, rv->r_end,
552				       rv->r_end + 1, s->r_end));
553				/*
554				 * We are allocating in the middle.
555				 */
556				r = int_alloc_resource(M_NOWAIT);
557				if (r == NULL) {
558					free(rv, M_RMAN);
559					rv = NULL;
560					goto out;
561				}
562				r->r_start = rv->r_end + 1;
563				r->r_end = s->r_end;
564				r->r_flags = s->r_flags;
565				r->r_rm = rm;
566				s->r_end = rv->r_start - 1;
567				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
568						     r_link);
569				TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
570						     r_link);
571			} else if (s->r_start == rv->r_start) {
572				DPRINTF(("allocating from the beginning\n"));
573				/*
574				 * We are allocating at the beginning.
575				 */
576				s->r_start = rv->r_end + 1;
577				TAILQ_INSERT_BEFORE(s, rv, r_link);
578			} else {
579				DPRINTF(("allocating at the end\n"));
580				/*
581				 * We are allocating at the end.
582				 */
583				s->r_end = rv->r_start - 1;
584				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
585						     r_link);
586			}
587			goto out;
588		}
589	}
590
591	/*
592	 * Now find an acceptable shared region, if the client's requirements
593	 * allow sharing.  By our implementation restriction, a candidate
594	 * region must match exactly by both size and sharing type in order
595	 * to be considered compatible with the client's request.  (The
596	 * former restriction could probably be lifted without too much
597	 * additional work, but this does not seem warranted.)
598	 */
599	DPRINTF(("no unshared regions found\n"));
600	if ((flags & RF_SHAREABLE) == 0)
601		goto out;
602
603	for (s = r; s && s->r_end <= end; s = TAILQ_NEXT(s, r_link)) {
604		if (SHARE_TYPE(s->r_flags) == SHARE_TYPE(flags) &&
605		    s->r_start >= start &&
606		    (s->r_end - s->r_start + 1) == count &&
607		    (s->r_start & amask) == 0 &&
608		    ((s->r_start ^ s->r_end) & bmask) == 0) {
609			rv = int_alloc_resource(M_NOWAIT);
610			if (rv == NULL)
611				goto out;
612			rv->r_start = s->r_start;
613			rv->r_end = s->r_end;
614			rv->r_flags = new_rflags;
615			rv->r_dev = dev;
616			rv->r_rm = rm;
617			if (s->r_sharehead == NULL) {
618				s->r_sharehead = malloc(sizeof *s->r_sharehead,
619						M_RMAN, M_NOWAIT | M_ZERO);
620				if (s->r_sharehead == NULL) {
621					free(rv, M_RMAN);
622					rv = NULL;
623					goto out;
624				}
625				LIST_INIT(s->r_sharehead);
626				LIST_INSERT_HEAD(s->r_sharehead, s,
627						 r_sharelink);
628				s->r_flags |= RF_FIRSTSHARE;
629			}
630			rv->r_sharehead = s->r_sharehead;
631			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
632			goto out;
633		}
634	}
635	/*
636	 * We couldn't find anything.
637	 */
638
639out:
640	mtx_unlock(rm->rm_mtx);
641	return (rv == NULL ? NULL : &rv->r_r);
642}
643
644struct resource *
645rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
646		      u_int flags, struct device *dev)
647{
648
649	return (rman_reserve_resource_bound(rm, start, end, count, 0, flags,
650	    dev));
651}
652
653int
654rman_activate_resource(struct resource *re)
655{
656	struct resource_i *r;
657	struct rman *rm;
658
659	r = re->__r_i;
660	rm = r->r_rm;
661	mtx_lock(rm->rm_mtx);
662	r->r_flags |= RF_ACTIVE;
663	mtx_unlock(rm->rm_mtx);
664	return 0;
665}
666
667int
668rman_deactivate_resource(struct resource *r)
669{
670	struct rman *rm;
671
672	rm = r->__r_i->r_rm;
673	mtx_lock(rm->rm_mtx);
674	r->__r_i->r_flags &= ~RF_ACTIVE;
675	mtx_unlock(rm->rm_mtx);
676	return 0;
677}
678
679static int
680int_rman_release_resource(struct rman *rm, struct resource_i *r)
681{
682	struct resource_i *s, *t;
683
684	if (r->r_flags & RF_ACTIVE)
685		r->r_flags &= ~RF_ACTIVE;
686
687	/*
688	 * Check for a sharing list first.  If there is one, then we don't
689	 * have to think as hard.
690	 */
691	if (r->r_sharehead) {
692		/*
693		 * If a sharing list exists, then we know there are at
694		 * least two sharers.
695		 *
696		 * If we are in the main circleq, appoint someone else.
697		 */
698		LIST_REMOVE(r, r_sharelink);
699		s = LIST_FIRST(r->r_sharehead);
700		if (r->r_flags & RF_FIRSTSHARE) {
701			s->r_flags |= RF_FIRSTSHARE;
702			TAILQ_INSERT_BEFORE(r, s, r_link);
703			TAILQ_REMOVE(&rm->rm_list, r, r_link);
704		}
705
706		/*
707		 * Make sure that the sharing list goes away completely
708		 * if the resource is no longer being shared at all.
709		 */
710		if (LIST_NEXT(s, r_sharelink) == NULL) {
711			free(s->r_sharehead, M_RMAN);
712			s->r_sharehead = NULL;
713			s->r_flags &= ~RF_FIRSTSHARE;
714		}
715		goto out;
716	}
717
718	/*
719	 * Look at the adjacent resources in the list and see if our
720	 * segment can be merged with any of them.  If either of the
721	 * resources is allocated or is not exactly adjacent then they
722	 * cannot be merged with our segment.
723	 */
724	s = TAILQ_PREV(r, resource_head, r_link);
725	if (s != NULL && ((s->r_flags & RF_ALLOCATED) != 0 ||
726	    s->r_end + 1 != r->r_start))
727		s = NULL;
728	t = TAILQ_NEXT(r, r_link);
729	if (t != NULL && ((t->r_flags & RF_ALLOCATED) != 0 ||
730	    r->r_end + 1 != t->r_start))
731		t = NULL;
732
733	if (s != NULL && t != NULL) {
734		/*
735		 * Merge all three segments.
736		 */
737		s->r_end = t->r_end;
738		TAILQ_REMOVE(&rm->rm_list, r, r_link);
739		TAILQ_REMOVE(&rm->rm_list, t, r_link);
740		free(t, M_RMAN);
741	} else if (s != NULL) {
742		/*
743		 * Merge previous segment with ours.
744		 */
745		s->r_end = r->r_end;
746		TAILQ_REMOVE(&rm->rm_list, r, r_link);
747	} else if (t != NULL) {
748		/*
749		 * Merge next segment with ours.
750		 */
751		t->r_start = r->r_start;
752		TAILQ_REMOVE(&rm->rm_list, r, r_link);
753	} else {
754		/*
755		 * At this point, we know there is nothing we
756		 * can potentially merge with, because on each
757		 * side, there is either nothing there or what is
758		 * there is still allocated.  In that case, we don't
759		 * want to remove r from the list; we simply want to
760		 * change it to an unallocated region and return
761		 * without freeing anything.
762		 */
763		r->r_flags &= ~RF_ALLOCATED;
764		r->r_dev = NULL;
765		return 0;
766	}
767
768out:
769	free(r, M_RMAN);
770	return 0;
771}
772
773int
774rman_release_resource(struct resource *re)
775{
776	int rv;
777	struct resource_i *r;
778	struct rman *rm;
779
780	r = re->__r_i;
781	rm = r->r_rm;
782	mtx_lock(rm->rm_mtx);
783	rv = int_rman_release_resource(rm, r);
784	mtx_unlock(rm->rm_mtx);
785	return (rv);
786}
787
788uint32_t
789rman_make_alignment_flags(uint32_t size)
790{
791	int i;
792
793	/*
794	 * Find the hightest bit set, and add one if more than one bit
795	 * set.  We're effectively computing the ceil(log2(size)) here.
796	 */
797	for (i = 31; i > 0; i--)
798		if ((1 << i) & size)
799			break;
800	if (~(1 << i) & size)
801		i++;
802
803	return(RF_ALIGNMENT_LOG2(i));
804}
805
806void
807rman_set_start(struct resource *r, u_long start)
808{
809
810	r->__r_i->r_start = start;
811}
812
813u_long
814rman_get_start(struct resource *r)
815{
816
817	return (r->__r_i->r_start);
818}
819
820void
821rman_set_end(struct resource *r, u_long end)
822{
823
824	r->__r_i->r_end = end;
825}
826
827u_long
828rman_get_end(struct resource *r)
829{
830
831	return (r->__r_i->r_end);
832}
833
834u_long
835rman_get_size(struct resource *r)
836{
837
838	return (r->__r_i->r_end - r->__r_i->r_start + 1);
839}
840
841u_int
842rman_get_flags(struct resource *r)
843{
844
845	return (r->__r_i->r_flags);
846}
847
848void
849rman_set_virtual(struct resource *r, void *v)
850{
851
852	r->__r_i->r_virtual = v;
853}
854
855void *
856rman_get_virtual(struct resource *r)
857{
858
859	return (r->__r_i->r_virtual);
860}
861
862void
863rman_set_bustag(struct resource *r, bus_space_tag_t t)
864{
865
866	r->r_bustag = t;
867}
868
869bus_space_tag_t
870rman_get_bustag(struct resource *r)
871{
872
873	return (r->r_bustag);
874}
875
876void
877rman_set_bushandle(struct resource *r, bus_space_handle_t h)
878{
879
880	r->r_bushandle = h;
881}
882
883bus_space_handle_t
884rman_get_bushandle(struct resource *r)
885{
886
887	return (r->r_bushandle);
888}
889
890void
891rman_set_rid(struct resource *r, int rid)
892{
893
894	r->__r_i->r_rid = rid;
895}
896
897int
898rman_get_rid(struct resource *r)
899{
900
901	return (r->__r_i->r_rid);
902}
903
904void
905rman_set_device(struct resource *r, struct device *dev)
906{
907
908	r->__r_i->r_dev = dev;
909}
910
911struct device *
912rman_get_device(struct resource *r)
913{
914
915	return (r->__r_i->r_dev);
916}
917
918int
919rman_is_region_manager(struct resource *r, struct rman *rm)
920{
921
922	return (r->__r_i->r_rm == rm);
923}
924
925/*
926 * Sysctl interface for scanning the resource lists.
927 *
928 * We take two input parameters; the index into the list of resource
929 * managers, and the resource offset into the list.
930 */
931static int
932sysctl_rman(SYSCTL_HANDLER_ARGS)
933{
934	int			*name = (int *)arg1;
935	u_int			namelen = arg2;
936	int			rman_idx, res_idx;
937	struct rman		*rm;
938	struct resource_i	*res;
939	struct resource_i	*sres;
940	struct u_rman		urm;
941	struct u_resource	ures;
942	int			error;
943
944	if (namelen != 3)
945		return (EINVAL);
946
947	if (bus_data_generation_check(name[0]))
948		return (EINVAL);
949	rman_idx = name[1];
950	res_idx = name[2];
951
952	/*
953	 * Find the indexed resource manager
954	 */
955	mtx_lock(&rman_mtx);
956	TAILQ_FOREACH(rm, &rman_head, rm_link) {
957		if (rman_idx-- == 0)
958			break;
959	}
960	mtx_unlock(&rman_mtx);
961	if (rm == NULL)
962		return (ENOENT);
963
964	/*
965	 * If the resource index is -1, we want details on the
966	 * resource manager.
967	 */
968	if (res_idx == -1) {
969		bzero(&urm, sizeof(urm));
970		urm.rm_handle = (uintptr_t)rm;
971		if (rm->rm_descr != NULL)
972			strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
973		urm.rm_start = rm->rm_start;
974		urm.rm_size = rm->rm_end - rm->rm_start + 1;
975		urm.rm_type = rm->rm_type;
976
977		error = SYSCTL_OUT(req, &urm, sizeof(urm));
978		return (error);
979	}
980
981	/*
982	 * Find the indexed resource and return it.
983	 */
984	mtx_lock(rm->rm_mtx);
985	TAILQ_FOREACH(res, &rm->rm_list, r_link) {
986		if (res->r_sharehead != NULL) {
987			LIST_FOREACH(sres, res->r_sharehead, r_sharelink)
988				if (res_idx-- == 0) {
989					res = sres;
990					goto found;
991				}
992		}
993		else if (res_idx-- == 0)
994				goto found;
995	}
996	mtx_unlock(rm->rm_mtx);
997	return (ENOENT);
998
999found:
1000	bzero(&ures, sizeof(ures));
1001	ures.r_handle = (uintptr_t)res;
1002	ures.r_parent = (uintptr_t)res->r_rm;
1003	ures.r_device = (uintptr_t)res->r_dev;
1004	if (res->r_dev != NULL) {
1005		if (device_get_name(res->r_dev) != NULL) {
1006			snprintf(ures.r_devname, RM_TEXTLEN,
1007			    "%s%d",
1008			    device_get_name(res->r_dev),
1009			    device_get_unit(res->r_dev));
1010		} else {
1011			strlcpy(ures.r_devname, "nomatch",
1012			    RM_TEXTLEN);
1013		}
1014	} else {
1015		ures.r_devname[0] = '\0';
1016	}
1017	ures.r_start = res->r_start;
1018	ures.r_size = res->r_end - res->r_start + 1;
1019	ures.r_flags = res->r_flags;
1020
1021	mtx_unlock(rm->rm_mtx);
1022	error = SYSCTL_OUT(req, &ures, sizeof(ures));
1023	return (error);
1024}
1025
1026static SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
1027    "kernel resource manager");
1028
1029#ifdef DDB
1030static void
1031dump_rman_header(struct rman *rm)
1032{
1033
1034	if (db_pager_quit)
1035		return;
1036	db_printf("rman %p: %s (0x%lx-0x%lx full range)\n",
1037	    rm, rm->rm_descr, rm->rm_start, rm->rm_end);
1038}
1039
1040static void
1041dump_rman(struct rman *rm)
1042{
1043	struct resource_i *r;
1044	const char *devname;
1045
1046	if (db_pager_quit)
1047		return;
1048	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
1049		if (r->r_dev != NULL) {
1050			devname = device_get_nameunit(r->r_dev);
1051			if (devname == NULL)
1052				devname = "nomatch";
1053		} else
1054			devname = NULL;
1055		db_printf("    0x%lx-0x%lx ", r->r_start, r->r_end);
1056		if (devname != NULL)
1057			db_printf("(%s)\n", devname);
1058		else
1059			db_printf("----\n");
1060		if (db_pager_quit)
1061			return;
1062	}
1063}
1064
1065DB_SHOW_COMMAND(rman, db_show_rman)
1066{
1067
1068	if (have_addr) {
1069		dump_rman_header((struct rman *)addr);
1070		dump_rman((struct rman *)addr);
1071	}
1072}
1073
1074DB_SHOW_COMMAND(rmans, db_show_rmans)
1075{
1076	struct rman *rm;
1077
1078	TAILQ_FOREACH(rm, &rman_head, rm_link) {
1079		dump_rman_header(rm);
1080	}
1081}
1082
1083DB_SHOW_ALL_COMMAND(rman, db_show_all_rman)
1084{
1085	struct rman *rm;
1086
1087	TAILQ_FOREACH(rm, &rman_head, rm_link) {
1088		dump_rman_header(rm);
1089		dump_rman(rm);
1090	}
1091}
1092DB_SHOW_ALIAS(allrman, db_show_all_rman);
1093#endif
1094