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