xref: /freebsd/sys/kern/subr_rman.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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  * $FreeBSD$
30  */
31 
32 /*
33  * The kernel resource manager.  This code is responsible for keeping track
34  * of hardware resources which are apportioned out to various drivers.
35  * It does not actually assign those resources, and it is not expected
36  * that end-device drivers will call into this code directly.  Rather,
37  * the code which implements the buses that those devices are attached to,
38  * and the code which manages CPU resources, will call this code, and the
39  * end-device drivers will make upcalls to that code to actually perform
40  * the allocation.
41  *
42  * There are two sorts of resources managed by this code.  The first is
43  * the more familiar array (RMAN_ARRAY) type; resources in this class
44  * consist of a sequence of individually-allocatable objects which have
45  * been numbered in some well-defined order.  Most of the resources
46  * are of this type, as it is the most familiar.  The second type is
47  * called a gauge (RMAN_GAUGE), and models fungible resources (i.e.,
48  * resources in which each instance is indistinguishable from every
49  * other instance).  The principal anticipated application of gauges
50  * is in the context of power consumption, where a bus may have a specific
51  * power budget which all attached devices share.  RMAN_GAUGE is not
52  * implemented yet.
53  *
54  * For array resources, we make one simplifying assumption: two clients
55  * sharing the same resource must use the same range of indices.  That
56  * is to say, sharing of overlapping-but-not-identical regions is not
57  * permitted.
58  */
59 
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/kernel.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/bus.h>		/* XXX debugging */
66 #include <machine/bus.h>
67 #include <sys/rman.h>
68 
69 #ifdef RMAN_DEBUG
70 #define DPRINTF(params) printf##params
71 #else
72 #define DPRINTF(params)
73 #endif
74 
75 static MALLOC_DEFINE(M_RMAN, "rman", "Resource manager");
76 
77 struct	rman_head rman_head;
78 #ifndef NULL_SIMPLELOCKS
79 static	struct simplelock rman_lock; /* mutex to protect rman_head */
80 #endif
81 static	int int_rman_activate_resource(struct rman *rm, struct resource *r,
82 				       struct resource **whohas);
83 static	int int_rman_deactivate_resource(struct resource *r);
84 static	int int_rman_release_resource(struct rman *rm, struct resource *r);
85 
86 int
87 rman_init(struct rman *rm)
88 {
89 	static int once;
90 
91 	if (once == 0) {
92 		once = 1;
93 		TAILQ_INIT(&rman_head);
94 		simple_lock_init(&rman_lock);
95 	}
96 
97 	if (rm->rm_type == RMAN_UNINIT)
98 		panic("rman_init");
99 	if (rm->rm_type == RMAN_GAUGE)
100 		panic("implement RMAN_GAUGE");
101 
102 	TAILQ_INIT(&rm->rm_list);
103 	rm->rm_slock = malloc(sizeof *rm->rm_slock, M_RMAN, M_NOWAIT);
104 	if (rm->rm_slock == 0)
105 		return ENOMEM;
106 	simple_lock_init(rm->rm_slock);
107 
108 	simple_lock(&rman_lock);
109 	TAILQ_INSERT_TAIL(&rman_head, rm, rm_link);
110 	simple_unlock(&rman_lock);
111 	return 0;
112 }
113 
114 /*
115  * NB: this interface is not robust against programming errors which
116  * add multiple copies of the same region.
117  */
118 int
119 rman_manage_region(struct rman *rm, u_long start, u_long end)
120 {
121 	struct resource *r, *s;
122 
123 	r = malloc(sizeof *r, M_RMAN, M_NOWAIT);
124 	if (r == 0)
125 		return ENOMEM;
126 	bzero(r, sizeof *r);
127 	r->r_sharehead = 0;
128 	r->r_start = start;
129 	r->r_end = end;
130 	r->r_flags = 0;
131 	r->r_dev = 0;
132 	r->r_rm = rm;
133 
134 	simple_lock(rm->rm_slock);
135 	for (s = TAILQ_FIRST(&rm->rm_list);
136 	     s && s->r_end < r->r_start;
137 	     s = TAILQ_NEXT(s, r_link))
138 		;
139 
140 	if (s == NULL) {
141 		TAILQ_INSERT_TAIL(&rm->rm_list, r, r_link);
142 	} else {
143 		TAILQ_INSERT_BEFORE(s, r, r_link);
144 	}
145 
146 	simple_unlock(rm->rm_slock);
147 	return 0;
148 }
149 
150 int
151 rman_fini(struct rman *rm)
152 {
153 	struct resource *r;
154 
155 	simple_lock(rm->rm_slock);
156 	TAILQ_FOREACH(r, &rm->rm_list, r_link) {
157 		if (r->r_flags & RF_ALLOCATED) {
158 			simple_unlock(rm->rm_slock);
159 			return EBUSY;
160 		}
161 	}
162 
163 	/*
164 	 * There really should only be one of these if we are in this
165 	 * state and the code is working properly, but it can't hurt.
166 	 */
167 	while (!TAILQ_EMPTY(&rm->rm_list)) {
168 		r = TAILQ_FIRST(&rm->rm_list);
169 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
170 		free(r, M_RMAN);
171 	}
172 	simple_unlock(rm->rm_slock);
173 	simple_lock(&rman_lock);
174 	TAILQ_REMOVE(&rman_head, rm, rm_link);
175 	simple_unlock(&rman_lock);
176 	free(rm->rm_slock, M_RMAN);
177 
178 	return 0;
179 }
180 
181 struct resource *
182 rman_reserve_resource(struct rman *rm, u_long start, u_long end, u_long count,
183 		      u_int flags, struct device *dev)
184 {
185 	u_int	want_activate;
186 	struct	resource *r, *s, *rv;
187 	u_long	rstart, rend;
188 
189 	rv = 0;
190 
191 	DPRINTF(("rman_reserve_resource: <%s> request: [%#lx, %#lx], length "
192 	       "%#lx, flags %u, device %s%d\n", rm->rm_descr, start, end,
193 	       count, flags, device_get_name(dev), device_get_unit(dev)));
194 	want_activate = (flags & RF_ACTIVE);
195 	flags &= ~RF_ACTIVE;
196 
197 	simple_lock(rm->rm_slock);
198 
199 	for (r = TAILQ_FIRST(&rm->rm_list);
200 	     r && r->r_end < start;
201 	     r = TAILQ_NEXT(r, r_link))
202 		;
203 
204 	if (r == NULL) {
205 		DPRINTF(("could not find a region\n"));
206 		goto out;
207 	}
208 
209 	/*
210 	 * First try to find an acceptable totally-unshared region.
211 	 */
212 	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
213 		DPRINTF(("considering [%#lx, %#lx]\n", s->r_start, s->r_end));
214 		if (s->r_start > end) {
215 			DPRINTF(("s->r_start (%#lx) > end (%#lx)\n", s->r_start, end));
216 			break;
217 		}
218 		if (s->r_flags & RF_ALLOCATED) {
219 			DPRINTF(("region is allocated\n"));
220 			continue;
221 		}
222 		rstart = max(s->r_start, start);
223 		rstart = (rstart + ((1ul << RF_ALIGNMENT(flags))) - 1) &
224 		    ~((1ul << RF_ALIGNMENT(flags)) - 1);
225 		rend = min(s->r_end, max(rstart + count, end));
226 		DPRINTF(("truncated region: [%#lx, %#lx]; size %#lx (requested %#lx)\n",
227 		       rstart, rend, (rend - rstart + 1), count));
228 
229 		if ((rend - rstart + 1) >= count) {
230 			DPRINTF(("candidate region: [%#lx, %#lx], size %#lx\n",
231 			       rend, rstart, (rend - rstart + 1)));
232 			if ((s->r_end - s->r_start + 1) == count) {
233 				DPRINTF(("candidate region is entire chunk\n"));
234 				rv = s;
235 				rv->r_flags |= RF_ALLOCATED | flags;
236 				rv->r_dev = dev;
237 				goto out;
238 			}
239 
240 			/*
241 			 * If s->r_start < rstart and
242 			 *    s->r_end > rstart + count - 1, then
243 			 * we need to split the region into three pieces
244 			 * (the middle one will get returned to the user).
245 			 * Otherwise, we are allocating at either the
246 			 * beginning or the end of s, so we only need to
247 			 * split it in two.  The first case requires
248 			 * two new allocations; the second requires but one.
249 			 */
250 			rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT);
251 			if (rv == 0)
252 				goto out;
253 			bzero(rv, sizeof *rv);
254 			rv->r_start = rstart;
255 			rv->r_end = rstart + count - 1;
256 			rv->r_flags = flags | RF_ALLOCATED;
257 			rv->r_dev = dev;
258 			rv->r_sharehead = 0;
259 			rv->r_rm = rm;
260 
261 			if (s->r_start < rv->r_start && s->r_end > rv->r_end) {
262 				DPRINTF(("splitting region in three parts: "
263 				       "[%#lx, %#lx]; [%#lx, %#lx]; [%#lx, %#lx]\n",
264 				       s->r_start, rv->r_start - 1,
265 				       rv->r_start, rv->r_end,
266 				       rv->r_end + 1, s->r_end));
267 				/*
268 				 * We are allocating in the middle.
269 				 */
270 				r = malloc(sizeof *r, M_RMAN, M_NOWAIT);
271 				if (r == 0) {
272 					free(rv, M_RMAN);
273 					rv = 0;
274 					goto out;
275 				}
276 				bzero(r, sizeof *r);
277 				r->r_start = rv->r_end + 1;
278 				r->r_end = s->r_end;
279 				r->r_flags = s->r_flags;
280 				r->r_dev = 0;
281 				r->r_sharehead = 0;
282 				r->r_rm = rm;
283 				s->r_end = rv->r_start - 1;
284 				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
285 						     r_link);
286 				TAILQ_INSERT_AFTER(&rm->rm_list, rv, r,
287 						     r_link);
288 			} else if (s->r_start == rv->r_start) {
289 				DPRINTF(("allocating from the beginning\n"));
290 				/*
291 				 * We are allocating at the beginning.
292 				 */
293 				s->r_start = rv->r_end + 1;
294 				TAILQ_INSERT_BEFORE(s, rv, r_link);
295 			} else {
296 				DPRINTF(("allocating at the end\n"));
297 				/*
298 				 * We are allocating at the end.
299 				 */
300 				s->r_end = rv->r_start - 1;
301 				TAILQ_INSERT_AFTER(&rm->rm_list, s, rv,
302 						     r_link);
303 			}
304 			goto out;
305 		}
306 	}
307 
308 	/*
309 	 * Now find an acceptable shared region, if the client's requirements
310 	 * allow sharing.  By our implementation restriction, a candidate
311 	 * region must match exactly by both size and sharing type in order
312 	 * to be considered compatible with the client's request.  (The
313 	 * former restriction could probably be lifted without too much
314 	 * additional work, but this does not seem warranted.)
315 	 */
316 	DPRINTF(("no unshared regions found\n"));
317 	if ((flags & (RF_SHAREABLE | RF_TIMESHARE)) == 0)
318 		goto out;
319 
320 	for (s = r; s; s = TAILQ_NEXT(s, r_link)) {
321 		if (s->r_start > end)
322 			break;
323 		if ((s->r_flags & flags) != flags)
324 			continue;
325 		rstart = max(s->r_start, start);
326 		rend = min(s->r_end, max(start + count, end));
327 		if (s->r_start >= start && s->r_end <= end
328 		    && (s->r_end - s->r_start + 1) == count) {
329 			rv = malloc(sizeof *rv, M_RMAN, M_NOWAIT);
330 			if (rv == 0)
331 				goto out;
332 			bzero(rv, sizeof *rv);
333 			rv->r_start = s->r_start;
334 			rv->r_end = s->r_end;
335 			rv->r_flags = s->r_flags &
336 				(RF_ALLOCATED | RF_SHAREABLE | RF_TIMESHARE);
337 			rv->r_dev = dev;
338 			rv->r_rm = rm;
339 			if (s->r_sharehead == 0) {
340 				s->r_sharehead = malloc(sizeof *s->r_sharehead,
341 							M_RMAN, M_NOWAIT);
342 				if (s->r_sharehead == 0) {
343 					free(rv, M_RMAN);
344 					rv = 0;
345 					goto out;
346 				}
347 				bzero(s->r_sharehead, sizeof *s->r_sharehead);
348 				LIST_INIT(s->r_sharehead);
349 				LIST_INSERT_HEAD(s->r_sharehead, s,
350 						 r_sharelink);
351 				s->r_flags |= RF_FIRSTSHARE;
352 			}
353 			rv->r_sharehead = s->r_sharehead;
354 			LIST_INSERT_HEAD(s->r_sharehead, rv, r_sharelink);
355 			goto out;
356 		}
357 	}
358 
359 	/*
360 	 * We couldn't find anything.
361 	 */
362 out:
363 	/*
364 	 * If the user specified RF_ACTIVE in the initial flags,
365 	 * which is reflected in `want_activate', we attempt to atomically
366 	 * activate the resource.  If this fails, we release the resource
367 	 * and indicate overall failure.  (This behavior probably doesn't
368 	 * make sense for RF_TIMESHARE-type resources.)
369 	 */
370 	if (rv && want_activate) {
371 		struct resource *whohas;
372 		if (int_rman_activate_resource(rm, rv, &whohas)) {
373 			int_rman_release_resource(rm, rv);
374 			rv = 0;
375 		}
376 	}
377 
378 	simple_unlock(rm->rm_slock);
379 	return (rv);
380 }
381 
382 static int
383 int_rman_activate_resource(struct rman *rm, struct resource *r,
384 			   struct resource **whohas)
385 {
386 	struct resource *s;
387 	int ok;
388 
389 	/*
390 	 * If we are not timesharing, then there is nothing much to do.
391 	 * If we already have the resource, then there is nothing at all to do.
392 	 * If we are not on a sharing list with anybody else, then there is
393 	 * little to do.
394 	 */
395 	if ((r->r_flags & RF_TIMESHARE) == 0
396 	    || (r->r_flags & RF_ACTIVE) != 0
397 	    || r->r_sharehead == 0) {
398 		r->r_flags |= RF_ACTIVE;
399 		return 0;
400 	}
401 
402 	ok = 1;
403 	for (s = LIST_FIRST(r->r_sharehead); s && ok;
404 	     s = LIST_NEXT(s, r_sharelink)) {
405 		if ((s->r_flags & RF_ACTIVE) != 0) {
406 			ok = 0;
407 			*whohas = s;
408 		}
409 	}
410 	if (ok) {
411 		r->r_flags |= RF_ACTIVE;
412 		return 0;
413 	}
414 	return EBUSY;
415 }
416 
417 int
418 rman_activate_resource(struct resource *r)
419 {
420 	int rv;
421 	struct resource *whohas;
422 	struct rman *rm;
423 
424 	rm = r->r_rm;
425 	simple_lock(rm->rm_slock);
426 	rv = int_rman_activate_resource(rm, r, &whohas);
427 	simple_unlock(rm->rm_slock);
428 	return rv;
429 }
430 
431 int
432 rman_await_resource(struct resource *r, int pri, int timo)
433 {
434 	int	rv, s;
435 	struct	resource *whohas;
436 	struct	rman *rm;
437 
438 	rm = r->r_rm;
439 	for (;;) {
440 		simple_lock(rm->rm_slock);
441 		rv = int_rman_activate_resource(rm, r, &whohas);
442 		if (rv != EBUSY)
443 			return (rv);	/* returns with simplelock */
444 
445 		if (r->r_sharehead == 0)
446 			panic("rman_await_resource");
447 		/*
448 		 * splhigh hopefully will prevent a race between
449 		 * simple_unlock and tsleep where a process
450 		 * could conceivably get in and release the resource
451 		 * before we have a chance to sleep on it.
452 		 */
453 		s = splhigh();
454 		whohas->r_flags |= RF_WANTED;
455 		simple_unlock(rm->rm_slock);
456 		rv = tsleep(r->r_sharehead, pri, "rmwait", timo);
457 		if (rv) {
458 			splx(s);
459 			return rv;
460 		}
461 		simple_lock(rm->rm_slock);
462 		splx(s);
463 	}
464 }
465 
466 static int
467 int_rman_deactivate_resource(struct resource *r)
468 {
469 	struct	rman *rm;
470 
471 	rm = r->r_rm;
472 	r->r_flags &= ~RF_ACTIVE;
473 	if (r->r_flags & RF_WANTED) {
474 		r->r_flags &= ~RF_WANTED;
475 		wakeup(r->r_sharehead);
476 	}
477 	return 0;
478 }
479 
480 int
481 rman_deactivate_resource(struct resource *r)
482 {
483 	struct	rman *rm;
484 
485 	rm = r->r_rm;
486 	simple_lock(rm->rm_slock);
487 	int_rman_deactivate_resource(r);
488 	simple_unlock(rm->rm_slock);
489 	return 0;
490 }
491 
492 static int
493 int_rman_release_resource(struct rman *rm, struct resource *r)
494 {
495 	struct	resource *s, *t;
496 
497 	if (r->r_flags & RF_ACTIVE)
498 		int_rman_deactivate_resource(r);
499 
500 	/*
501 	 * Check for a sharing list first.  If there is one, then we don't
502 	 * have to think as hard.
503 	 */
504 	if (r->r_sharehead) {
505 		/*
506 		 * If a sharing list exists, then we know there are at
507 		 * least two sharers.
508 		 *
509 		 * If we are in the main circleq, appoint someone else.
510 		 */
511 		LIST_REMOVE(r, r_sharelink);
512 		s = LIST_FIRST(r->r_sharehead);
513 		if (r->r_flags & RF_FIRSTSHARE) {
514 			s->r_flags |= RF_FIRSTSHARE;
515 			TAILQ_INSERT_BEFORE(r, s, r_link);
516 			TAILQ_REMOVE(&rm->rm_list, r, r_link);
517 		}
518 
519 		/*
520 		 * Make sure that the sharing list goes away completely
521 		 * if the resource is no longer being shared at all.
522 		 */
523 		if (LIST_NEXT(s, r_sharelink) == 0) {
524 			free(s->r_sharehead, M_RMAN);
525 			s->r_sharehead = 0;
526 			s->r_flags &= ~RF_FIRSTSHARE;
527 		}
528 		goto out;
529 	}
530 
531 	/*
532 	 * Look at the adjacent resources in the list and see if our
533 	 * segment can be merged with any of them.
534 	 */
535 	s = TAILQ_PREV(r, resource_head, r_link);
536 	t = TAILQ_NEXT(r, r_link);
537 
538 	if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0
539 	    && t != NULL && (t->r_flags & RF_ALLOCATED) == 0) {
540 		/*
541 		 * Merge all three segments.
542 		 */
543 		s->r_end = t->r_end;
544 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
545 		TAILQ_REMOVE(&rm->rm_list, t, r_link);
546 		free(t, M_RMAN);
547 	} else if (s != NULL && (s->r_flags & RF_ALLOCATED) == 0) {
548 		/*
549 		 * Merge previous segment with ours.
550 		 */
551 		s->r_end = r->r_end;
552 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
553 	} else if (t != NULL && (t->r_flags & RF_ALLOCATED) == 0) {
554 		/*
555 		 * Merge next segment with ours.
556 		 */
557 		t->r_start = r->r_start;
558 		TAILQ_REMOVE(&rm->rm_list, r, r_link);
559 	} else {
560 		/*
561 		 * At this point, we know there is nothing we
562 		 * can potentially merge with, because on each
563 		 * side, there is either nothing there or what is
564 		 * there is still allocated.  In that case, we don't
565 		 * want to remove r from the list; we simply want to
566 		 * change it to an unallocated region and return
567 		 * without freeing anything.
568 		 */
569 		r->r_flags &= ~RF_ALLOCATED;
570 		return 0;
571 	}
572 
573 out:
574 	free(r, M_RMAN);
575 	return 0;
576 }
577 
578 int
579 rman_release_resource(struct resource *r)
580 {
581 	int	rv;
582 	struct	rman *rm = r->r_rm;
583 
584 	simple_lock(rm->rm_slock);
585 	rv = int_rman_release_resource(rm, r);
586 	simple_unlock(rm->rm_slock);
587 	return (rv);
588 }
589 
590 uint32_t
591 rman_make_alignment_flags(uint32_t size)
592 {
593 	int	i;
594 
595 	/*
596 	 * Find the hightest bit set, and add one if more than one bit
597 	 * set.  We're effectively computing the ceil(log2(size)) here.
598 	 */
599 	for (i = 32; i > 0; i--)
600 		if ((1 << i) & size)
601 			break;
602 	if (~(1 << i) & size)
603 		i++;
604 
605 	return(RF_ALIGNMENT_LOG2(i));
606 }
607