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