patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / kernel / resource.c
1 /*
2  *      linux/kernel/resource.c
3  *
4  * Copyright (C) 1999   Linus Torvalds
5  * Copyright (C) 1999   Martin Mares <mj@ucw.cz>
6  *
7  * Arbitrary resource management.
8  */
9
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/errno.h>
14 #include <linux/ioport.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/fs.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <asm/io.h>
22
23
24 struct resource ioport_resource = {
25         .name   = "PCI IO",
26         .start  = 0x0000,
27         .end    = IO_SPACE_LIMIT,
28         .flags  = IORESOURCE_IO,
29 };
30
31 EXPORT_SYMBOL(ioport_resource);
32
33 struct resource iomem_resource = {
34         .name   = "PCI mem",
35         .start  = 0UL,
36         .end    = ~0UL,
37         .flags  = IORESOURCE_MEM,
38 };
39
40 EXPORT_SYMBOL(iomem_resource);
41
42 static rwlock_t resource_lock = RW_LOCK_UNLOCKED;
43
44 #ifdef CONFIG_PROC_FS
45
46 enum { MAX_IORES_LEVEL = 5 };
47
48 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
49 {
50         struct resource *p = v;
51         (*pos)++;
52         if (p->child)
53                 return p->child;
54         while (!p->sibling && p->parent)
55                 p = p->parent;
56         return p->sibling;
57 }
58
59 static void *r_start(struct seq_file *m, loff_t *pos)
60 {
61         struct resource *p = m->private;
62         loff_t l = 0;
63         read_lock(&resource_lock);
64         for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
65                 ;
66         return p;
67 }
68
69 static void r_stop(struct seq_file *m, void *v)
70 {
71         read_unlock(&resource_lock);
72 }
73
74 static int r_show(struct seq_file *m, void *v)
75 {
76         struct resource *root = m->private;
77         struct resource *r = v, *p;
78         int width = root->end < 0x10000 ? 4 : 8;
79         int depth;
80
81         for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
82                 if (p->parent == root)
83                         break;
84         seq_printf(m, "%*s%0*lx-%0*lx : %s\n",
85                         depth * 2, "",
86                         width, r->start,
87                         width, r->end,
88                         r->name ? r->name : "<BAD>");
89         return 0;
90 }
91
92 struct seq_operations resource_op = {
93         .start  = r_start,
94         .next   = r_next,
95         .stop   = r_stop,
96         .show   = r_show,
97 };
98
99 static int ioports_open(struct inode *inode, struct file *file)
100 {
101         int res = seq_open(file, &resource_op);
102         if (!res) {
103                 struct seq_file *m = file->private_data;
104                 m->private = &ioport_resource;
105         }
106         return res;
107 }
108
109 static int iomem_open(struct inode *inode, struct file *file)
110 {
111         int res = seq_open(file, &resource_op);
112         if (!res) {
113                 struct seq_file *m = file->private_data;
114                 m->private = &iomem_resource;
115         }
116         return res;
117 }
118
119 static struct file_operations proc_ioports_operations = {
120         .open           = ioports_open,
121         .read           = seq_read,
122         .llseek         = seq_lseek,
123         .release        = seq_release,
124 };
125
126 static struct file_operations proc_iomem_operations = {
127         .open           = iomem_open,
128         .read           = seq_read,
129         .llseek         = seq_lseek,
130         .release        = seq_release,
131 };
132
133 static int __init ioresources_init(void)
134 {
135         struct proc_dir_entry *entry;
136
137         entry = create_proc_entry("ioports", 0, NULL);
138         if (entry)
139                 entry->proc_fops = &proc_ioports_operations;
140         entry = create_proc_entry("iomem", 0, NULL);
141         if (entry)
142                 entry->proc_fops = &proc_iomem_operations;
143         return 0;
144 }
145 __initcall(ioresources_init);
146
147 #endif /* CONFIG_PROC_FS */
148
149 /* Return the conflict entry if you can't request it */
150 static struct resource * __request_resource(struct resource *root, struct resource *new)
151 {
152         unsigned long start = new->start;
153         unsigned long end = new->end;
154         struct resource *tmp, **p;
155
156         if (end < start)
157                 return root;
158         if (start < root->start)
159                 return root;
160         if (end > root->end)
161                 return root;
162         p = &root->child;
163         for (;;) {
164                 tmp = *p;
165                 if (!tmp || tmp->start > end) {
166                         new->sibling = tmp;
167                         *p = new;
168                         new->parent = root;
169                         return NULL;
170                 }
171                 p = &tmp->sibling;
172                 if (tmp->end < start)
173                         continue;
174                 return tmp;
175         }
176 }
177
178 static int __release_resource(struct resource *old)
179 {
180         struct resource *tmp, **p;
181
182         p = &old->parent->child;
183         for (;;) {
184                 tmp = *p;
185                 if (!tmp)
186                         break;
187                 if (tmp == old) {
188                         *p = tmp->sibling;
189                         old->parent = NULL;
190                         return 0;
191                 }
192                 p = &tmp->sibling;
193         }
194         return -EINVAL;
195 }
196
197 int request_resource(struct resource *root, struct resource *new)
198 {
199         struct resource *conflict;
200
201         write_lock(&resource_lock);
202         conflict = __request_resource(root, new);
203         write_unlock(&resource_lock);
204         return conflict ? -EBUSY : 0;
205 }
206
207 EXPORT_SYMBOL(request_resource);
208
209 struct resource *____request_resource(struct resource *root, struct resource *new)
210 {
211         struct resource *conflict;
212
213         write_lock(&resource_lock);
214         conflict = __request_resource(root, new);
215         write_unlock(&resource_lock);
216         return conflict;
217 }
218
219 EXPORT_SYMBOL(____request_resource);
220
221 int release_resource(struct resource *old)
222 {
223         int retval;
224
225         write_lock(&resource_lock);
226         retval = __release_resource(old);
227         write_unlock(&resource_lock);
228         return retval;
229 }
230
231 EXPORT_SYMBOL(release_resource);
232
233 /*
234  * Find empty slot in the resource tree given range and alignment.
235  */
236 static int find_resource(struct resource *root, struct resource *new,
237                          unsigned long size,
238                          unsigned long min, unsigned long max,
239                          unsigned long align,
240                          void (*alignf)(void *, struct resource *,
241                                         unsigned long, unsigned long),
242                          void *alignf_data)
243 {
244         struct resource *this = root->child;
245
246         new->start = root->start;
247         /*
248          * Skip past an allocated resource that starts at 0, since the assignment
249          * of this->start - 1 to new->end below would cause an underflow.
250          */
251         if (this && this->start == 0) {
252                 new->start = this->end + 1;
253                 this = this->sibling;
254         }
255         for(;;) {
256                 if (this)
257                         new->end = this->start - 1;
258                 else
259                         new->end = root->end;
260                 if (new->start < min)
261                         new->start = min;
262                 if (new->end > max)
263                         new->end = max;
264                 new->start = (new->start + align - 1) & ~(align - 1);
265                 if (alignf)
266                         alignf(alignf_data, new, size, align);
267                 if (new->start < new->end && new->end - new->start + 1 >= size) {
268                         new->end = new->start + size - 1;
269                         return 0;
270                 }
271                 if (!this)
272                         break;
273                 new->start = this->end + 1;
274                 this = this->sibling;
275         }
276         return -EBUSY;
277 }
278
279 /*
280  * Allocate empty slot in the resource tree given range and alignment.
281  */
282 int allocate_resource(struct resource *root, struct resource *new,
283                       unsigned long size,
284                       unsigned long min, unsigned long max,
285                       unsigned long align,
286                       void (*alignf)(void *, struct resource *,
287                                      unsigned long, unsigned long),
288                       void *alignf_data)
289 {
290         int err;
291
292         write_lock(&resource_lock);
293         err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
294         if (err >= 0 && __request_resource(root, new))
295                 err = -EBUSY;
296         write_unlock(&resource_lock);
297         return err;
298 }
299
300 EXPORT_SYMBOL(allocate_resource);
301
302 /**
303  * insert_resource - Inserts a resource in the resource tree
304  * @parent: parent of the new resource
305  * @new: new resource to insert
306  *
307  * Returns 0 on success, -EBUSY if the resource can't be inserted.
308  *
309  * This function is equivalent of request_resource when no conflict
310  * happens. If a conflict happens, and the conflicting resources
311  * entirely fit within the range of the new resource, then the new
312  * resource is inserted and the conflicting resources become childs of
313  * the new resource.  Otherwise the new resource becomes the child of
314  * the conflicting resource
315  */
316 int insert_resource(struct resource *parent, struct resource *new)
317 {
318         int result = 0;
319         struct resource *first, *next;
320
321         write_lock(&resource_lock);
322  begin:
323         first = __request_resource(parent, new);
324         if (!first)
325                 goto out;
326
327         result = -EBUSY;
328         if (first == parent)
329                 goto out;
330
331         for (next = first; next->sibling; next = next->sibling)
332                 if (next->sibling->start > new->end)
333                         break;
334
335         /* existing resource includes new resource */
336         if (next->end >= new->end) {
337                 parent = next;
338                 result = 0;
339                 goto begin;
340         }
341
342         result = 0;
343
344         new->parent = parent;
345         new->sibling = next->sibling;
346         new->child = first;
347
348         next->sibling = NULL;
349         for (next = first; next; next = next->sibling)
350                 next->parent = new;
351
352         if (parent->child == first) {
353                 parent->child = new;
354         } else {
355                 next = parent->child;
356                 while (next->sibling != first)
357                         next = next->sibling;
358                 next->sibling = new;
359         }
360
361  out:
362         write_unlock(&resource_lock);
363         return result;
364 }
365
366 EXPORT_SYMBOL(insert_resource);
367
368 /*
369  * Given an existing resource, change its start and size to match the
370  * arguments.  Returns -EBUSY if it can't fit.  Existing children of
371  * the resource are assumed to be immutable.
372  */
373 int adjust_resource(struct resource *res, unsigned long start, unsigned long size)
374 {
375         struct resource *tmp, *parent = res->parent;
376         unsigned long end = start + size - 1;
377         int result = -EBUSY;
378
379         write_lock(&resource_lock);
380
381         if ((start < parent->start) || (end > parent->end))
382                 goto out;
383
384         for (tmp = res->child; tmp; tmp = tmp->sibling) {
385                 if ((tmp->start < start) || (tmp->end > end))
386                         goto out;
387         }
388
389         if (res->sibling && (res->sibling->start <= end))
390                 goto out;
391
392         tmp = parent->child;
393         if (tmp != res) {
394                 while (tmp->sibling != res)
395                         tmp = tmp->sibling;
396                 if (start <= tmp->end)
397                         goto out;
398         }
399
400         res->start = start;
401         res->end = end;
402         result = 0;
403
404  out:
405         write_unlock(&resource_lock);
406         return result;
407 }
408
409 EXPORT_SYMBOL(adjust_resource);
410
411 /*
412  * This is compatibility stuff for IO resources.
413  *
414  * Note how this, unlike the above, knows about
415  * the IO flag meanings (busy etc).
416  *
417  * Request-region creates a new busy region.
418  *
419  * Check-region returns non-zero if the area is already busy
420  *
421  * Release-region releases a matching busy region.
422  */
423 struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
424 {
425         struct resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
426
427         if (res) {
428                 memset(res, 0, sizeof(*res));
429                 res->name = name;
430                 res->start = start;
431                 res->end = start + n - 1;
432                 res->flags = IORESOURCE_BUSY;
433
434                 write_lock(&resource_lock);
435
436                 for (;;) {
437                         struct resource *conflict;
438
439                         conflict = __request_resource(parent, res);
440                         if (!conflict)
441                                 break;
442                         if (conflict != parent) {
443                                 parent = conflict;
444                                 if (!(conflict->flags & IORESOURCE_BUSY))
445                                         continue;
446                         }
447
448                         /* Uhhuh, that didn't work out.. */
449                         kfree(res);
450                         res = NULL;
451                         break;
452                 }
453                 write_unlock(&resource_lock);
454         }
455         return res;
456 }
457
458 EXPORT_SYMBOL(__request_region);
459
460 int __deprecated __check_region(struct resource *parent, unsigned long start, unsigned long n)
461 {
462         struct resource * res;
463
464         res = __request_region(parent, start, n, "check-region");
465         if (!res)
466                 return -EBUSY;
467
468         release_resource(res);
469         kfree(res);
470         return 0;
471 }
472
473 EXPORT_SYMBOL(__check_region);
474
475 void __release_region(struct resource *parent, unsigned long start, unsigned long n)
476 {
477         struct resource **p;
478         unsigned long end;
479
480         p = &parent->child;
481         end = start + n - 1;
482
483         write_lock(&resource_lock);
484
485         for (;;) {
486                 struct resource *res = *p;
487
488                 if (!res)
489                         break;
490                 if (res->start <= start && res->end >= end) {
491                         if (!(res->flags & IORESOURCE_BUSY)) {
492                                 p = &res->child;
493                                 continue;
494                         }
495                         if (res->start != start || res->end != end)
496                                 break;
497                         *p = res->sibling;
498                         write_unlock(&resource_lock);
499                         kfree(res);
500                         return;
501                 }
502                 p = &res->sibling;
503         }
504
505         write_unlock(&resource_lock);
506
507         printk(KERN_WARNING "Trying to free nonexistent resource <%08lx-%08lx>\n", start, end);
508 }
509
510 EXPORT_SYMBOL(__release_region);
511
512 /*
513  * Called from init/main.c to reserve IO ports.
514  */
515 #define MAXRESERVE 4
516 static int __init reserve_setup(char *str)
517 {
518         static int reserved;
519         static struct resource reserve[MAXRESERVE];
520
521         for (;;) {
522                 int io_start, io_num;
523                 int x = reserved;
524
525                 if (get_option (&str, &io_start) != 2)
526                         break;
527                 if (get_option (&str, &io_num)   == 0)
528                         break;
529                 if (x < MAXRESERVE) {
530                         struct resource *res = reserve + x;
531                         res->name = "reserved";
532                         res->start = io_start;
533                         res->end = io_start + io_num - 1;
534                         res->flags = IORESOURCE_BUSY;
535                         res->child = NULL;
536                         if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
537                                 reserved = x+1;
538                 }
539         }
540         return 1;
541 }
542
543 __setup("reserve=", reserve_setup);