2 * linux/kernel/resource.c
4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
7 * Arbitrary resource management.
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>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
24 struct resource ioport_resource = {
27 .end = IO_SPACE_LIMIT,
28 .flags = IORESOURCE_IO,
31 EXPORT_SYMBOL(ioport_resource);
33 struct resource iomem_resource = {
37 .flags = IORESOURCE_MEM,
40 EXPORT_SYMBOL(iomem_resource);
42 static rwlock_t resource_lock = RW_LOCK_UNLOCKED;
46 enum { MAX_IORES_LEVEL = 5 };
48 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
50 struct resource *p = v;
54 while (!p->sibling && p->parent)
59 static void *r_start(struct seq_file *m, loff_t *pos)
61 struct resource *p = m->private;
63 read_lock(&resource_lock);
64 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
69 static void r_stop(struct seq_file *m, void *v)
71 read_unlock(&resource_lock);
74 static int r_show(struct seq_file *m, void *v)
76 struct resource *root = m->private;
77 struct resource *r = v, *p;
78 int width = root->end < 0x10000 ? 4 : 8;
81 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
82 if (p->parent == root)
84 seq_printf(m, "%*s%0*lx-%0*lx : %s\n",
88 r->name ? r->name : "<BAD>");
92 struct seq_operations resource_op = {
99 static int ioports_open(struct inode *inode, struct file *file)
101 int res = seq_open(file, &resource_op);
103 struct seq_file *m = file->private_data;
104 m->private = &ioport_resource;
109 static int iomem_open(struct inode *inode, struct file *file)
111 int res = seq_open(file, &resource_op);
113 struct seq_file *m = file->private_data;
114 m->private = &iomem_resource;
119 static struct file_operations proc_ioports_operations = {
120 .open = ioports_open,
123 .release = seq_release,
126 static struct file_operations proc_iomem_operations = {
130 .release = seq_release,
133 static int __init ioresources_init(void)
135 struct proc_dir_entry *entry;
137 entry = create_proc_entry("ioports", 0, NULL);
139 entry->proc_fops = &proc_ioports_operations;
140 entry = create_proc_entry("iomem", 0, NULL);
142 entry->proc_fops = &proc_iomem_operations;
145 __initcall(ioresources_init);
147 #endif /* CONFIG_PROC_FS */
149 /* Return the conflict entry if you can't request it */
150 static struct resource * __request_resource(struct resource *root, struct resource *new)
152 unsigned long start = new->start;
153 unsigned long end = new->end;
154 struct resource *tmp, **p;
158 if (start < root->start)
165 if (!tmp || tmp->start > end) {
172 if (tmp->end < start)
178 static int __release_resource(struct resource *old)
180 struct resource *tmp, **p;
182 p = &old->parent->child;
197 int request_resource(struct resource *root, struct resource *new)
199 struct resource *conflict;
201 write_lock(&resource_lock);
202 conflict = __request_resource(root, new);
203 write_unlock(&resource_lock);
204 return conflict ? -EBUSY : 0;
207 EXPORT_SYMBOL(request_resource);
209 struct resource *____request_resource(struct resource *root, struct resource *new)
211 struct resource *conflict;
213 write_lock(&resource_lock);
214 conflict = __request_resource(root, new);
215 write_unlock(&resource_lock);
219 EXPORT_SYMBOL(____request_resource);
221 int release_resource(struct resource *old)
225 write_lock(&resource_lock);
226 retval = __release_resource(old);
227 write_unlock(&resource_lock);
231 EXPORT_SYMBOL(release_resource);
234 * Find empty slot in the resource tree given range and alignment.
236 static int find_resource(struct resource *root, struct resource *new,
238 unsigned long min, unsigned long max,
240 void (*alignf)(void *, struct resource *,
241 unsigned long, unsigned long),
244 struct resource *this = root->child;
246 new->start = root->start;
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.
251 if (this && this->start == 0) {
252 new->start = this->end + 1;
253 this = this->sibling;
257 new->end = this->start - 1;
259 new->end = root->end;
260 if (new->start < min)
264 new->start = (new->start + align - 1) & ~(align - 1);
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;
273 new->start = this->end + 1;
274 this = this->sibling;
280 * Allocate empty slot in the resource tree given range and alignment.
282 int allocate_resource(struct resource *root, struct resource *new,
284 unsigned long min, unsigned long max,
286 void (*alignf)(void *, struct resource *,
287 unsigned long, unsigned long),
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))
296 write_unlock(&resource_lock);
300 EXPORT_SYMBOL(allocate_resource);
303 * insert_resource - Inserts a resource in the resource tree
304 * @parent: parent of the new resource
305 * @new: new resource to insert
307 * Returns 0 on success, -EBUSY if the resource can't be inserted.
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
316 int insert_resource(struct resource *parent, struct resource *new)
319 struct resource *first, *next;
321 write_lock(&resource_lock);
323 first = __request_resource(parent, new);
331 for (next = first; next->sibling; next = next->sibling)
332 if (next->sibling->start > new->end)
335 /* existing resource overlaps end of new resource */
336 if (next->end > new->end) {
344 new->parent = parent;
345 new->sibling = next->sibling;
348 next->sibling = NULL;
349 for (next = first; next; next = next->sibling)
352 if (parent->child == first) {
355 next = parent->child;
356 while (next->sibling != first)
357 next = next->sibling;
362 write_unlock(&resource_lock);
366 EXPORT_SYMBOL(insert_resource);
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.
373 int adjust_resource(struct resource *res, unsigned long start, unsigned long size)
375 struct resource *tmp, *parent = res->parent;
376 unsigned long end = start + size - 1;
379 write_lock(&resource_lock);
381 if ((start < parent->start) || (end > parent->end))
384 for (tmp = res->child; tmp; tmp = tmp->sibling) {
385 if ((tmp->start < start) || (tmp->end > end))
389 if (res->sibling && (res->sibling->start <= end))
394 while (tmp->sibling != res)
396 if (start <= tmp->end)
405 write_unlock(&resource_lock);
409 EXPORT_SYMBOL(adjust_resource);
412 * This is compatibility stuff for IO resources.
414 * Note how this, unlike the above, knows about
415 * the IO flag meanings (busy etc).
417 * Request-region creates a new busy region.
419 * Check-region returns non-zero if the area is already busy
421 * Release-region releases a matching busy region.
423 struct resource * __request_region(struct resource *parent, unsigned long start, unsigned long n, const char *name)
425 struct resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
428 memset(res, 0, sizeof(*res));
431 res->end = start + n - 1;
432 res->flags = IORESOURCE_BUSY;
434 write_lock(&resource_lock);
437 struct resource *conflict;
439 conflict = __request_resource(parent, res);
442 if (conflict != parent) {
444 if (!(conflict->flags & IORESOURCE_BUSY))
448 /* Uhhuh, that didn't work out.. */
453 write_unlock(&resource_lock);
458 EXPORT_SYMBOL(__request_region);
460 int __deprecated __check_region(struct resource *parent, unsigned long start, unsigned long n)
462 struct resource * res;
464 res = __request_region(parent, start, n, "check-region");
468 release_resource(res);
473 EXPORT_SYMBOL(__check_region);
475 void __release_region(struct resource *parent, unsigned long start, unsigned long n)
483 write_lock(&resource_lock);
486 struct resource *res = *p;
490 if (res->start <= start && res->end >= end) {
491 if (!(res->flags & IORESOURCE_BUSY)) {
495 if (res->start != start || res->end != end)
498 write_unlock(&resource_lock);
505 write_unlock(&resource_lock);
507 printk(KERN_WARNING "Trying to free nonexistent resource <%08lx-%08lx>\n", start, end);
510 EXPORT_SYMBOL(__release_region);
513 * Called from init/main.c to reserve IO ports.
516 static int __init reserve_setup(char *str)
519 static struct resource reserve[MAXRESERVE];
522 int io_start, io_num;
525 if (get_option (&str, &io_start) != 2)
527 if (get_option (&str, &io_num) == 0)
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;
536 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
543 __setup("reserve=", reserve_setup);