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 DEFINE_RWLOCK(resource_lock);
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)
60 __acquires(resource_lock)
62 struct resource *p = m->private;
64 read_lock(&resource_lock);
65 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
70 static void r_stop(struct seq_file *m, void *v)
71 __releases(resource_lock)
73 read_unlock(&resource_lock);
76 static int r_show(struct seq_file *m, void *v)
78 struct resource *root = m->private;
79 struct resource *r = v, *p;
80 int width = root->end < 0x10000 ? 4 : 8;
83 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
84 if (p->parent == root)
86 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
88 width, (unsigned long long) r->start,
89 width, (unsigned long long) r->end,
90 r->name ? r->name : "<BAD>");
94 static struct seq_operations resource_op = {
101 static int ioports_open(struct inode *inode, struct file *file)
103 int res = seq_open(file, &resource_op);
105 struct seq_file *m = file->private_data;
106 m->private = &ioport_resource;
111 static int iomem_open(struct inode *inode, struct file *file)
113 int res = seq_open(file, &resource_op);
115 struct seq_file *m = file->private_data;
116 m->private = &iomem_resource;
121 static struct file_operations proc_ioports_operations = {
122 .open = ioports_open,
125 .release = seq_release,
128 static struct file_operations proc_iomem_operations = {
132 .release = seq_release,
135 static int __init ioresources_init(void)
137 struct proc_dir_entry *entry;
139 entry = create_proc_entry("ioports", 0, NULL);
141 entry->proc_fops = &proc_ioports_operations;
142 entry = create_proc_entry("iomem", 0, NULL);
144 entry->proc_fops = &proc_iomem_operations;
147 __initcall(ioresources_init);
149 #endif /* CONFIG_PROC_FS */
151 /* Return the conflict entry if you can't request it */
152 static struct resource * __request_resource(struct resource *root, struct resource *new)
154 resource_size_t start = new->start;
155 resource_size_t end = new->end;
156 struct resource *tmp, **p;
160 if (start < root->start)
167 if (!tmp || tmp->start > end) {
174 if (tmp->end < start)
180 static int __release_resource(struct resource *old)
182 struct resource *tmp, **p;
184 p = &old->parent->child;
199 int request_resource(struct resource *root, struct resource *new)
201 struct resource *conflict;
203 write_lock(&resource_lock);
204 conflict = __request_resource(root, new);
205 write_unlock(&resource_lock);
206 return conflict ? -EBUSY : 0;
209 EXPORT_SYMBOL(request_resource);
211 struct resource *____request_resource(struct resource *root, struct resource *new)
213 struct resource *conflict;
215 write_lock(&resource_lock);
216 conflict = __request_resource(root, new);
217 write_unlock(&resource_lock);
221 EXPORT_SYMBOL(____request_resource);
223 int release_resource(struct resource *old)
227 write_lock(&resource_lock);
228 retval = __release_resource(old);
229 write_unlock(&resource_lock);
233 EXPORT_SYMBOL(release_resource);
236 * Find empty slot in the resource tree given range and alignment.
238 static int find_resource(struct resource *root, struct resource *new,
239 resource_size_t size, resource_size_t min,
240 resource_size_t max, resource_size_t align,
241 void (*alignf)(void *, struct resource *,
242 resource_size_t, resource_size_t),
245 struct resource *this = root->child;
247 new->start = root->start;
249 * Skip past an allocated resource that starts at 0, since the assignment
250 * of this->start - 1 to new->end below would cause an underflow.
252 if (this && this->start == 0) {
253 new->start = this->end + 1;
254 this = this->sibling;
258 new->end = this->start - 1;
260 new->end = root->end;
261 if (new->start < min)
265 new->start = ALIGN(new->start, align);
267 alignf(alignf_data, new, size, align);
268 if (new->start < new->end && new->end - new->start >= size - 1) {
269 new->end = new->start + size - 1;
274 new->start = this->end + 1;
275 this = this->sibling;
281 * Allocate empty slot in the resource tree given range and alignment.
283 int allocate_resource(struct resource *root, struct resource *new,
284 resource_size_t size, resource_size_t min,
285 resource_size_t max, resource_size_t align,
286 void (*alignf)(void *, struct resource *,
287 resource_size_t, resource_size_t),
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);
324 first = __request_resource(parent, new);
332 /* Resource fully contained by the clashing resource? Recurse into it */
333 if (first->start <= new->start && first->end >= new->end) {
338 for (next = first; ; next = next->sibling) {
339 /* Partial overlap? Bad, and unfixable */
340 if (next->start < new->start || next->end > new->end)
344 if (next->sibling->start > new->end)
350 new->parent = parent;
351 new->sibling = next->sibling;
354 next->sibling = NULL;
355 for (next = first; next; next = next->sibling)
358 if (parent->child == first) {
361 next = parent->child;
362 while (next->sibling != first)
363 next = next->sibling;
368 write_unlock(&resource_lock);
372 EXPORT_SYMBOL(insert_resource);
375 * Given an existing resource, change its start and size to match the
376 * arguments. Returns -EBUSY if it can't fit. Existing children of
377 * the resource are assumed to be immutable.
379 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
381 struct resource *tmp, *parent = res->parent;
382 resource_size_t end = start + size - 1;
385 write_lock(&resource_lock);
387 if ((start < parent->start) || (end > parent->end))
390 for (tmp = res->child; tmp; tmp = tmp->sibling) {
391 if ((tmp->start < start) || (tmp->end > end))
395 if (res->sibling && (res->sibling->start <= end))
400 while (tmp->sibling != res)
402 if (start <= tmp->end)
411 write_unlock(&resource_lock);
415 EXPORT_SYMBOL(adjust_resource);
418 * This is compatibility stuff for IO resources.
420 * Note how this, unlike the above, knows about
421 * the IO flag meanings (busy etc).
423 * Request-region creates a new busy region.
425 * Check-region returns non-zero if the area is already busy
427 * Release-region releases a matching busy region.
429 struct resource * __request_region(struct resource *parent,
430 resource_size_t start, resource_size_t n,
433 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
438 res->end = start + n - 1;
439 res->flags = IORESOURCE_BUSY;
441 write_lock(&resource_lock);
444 struct resource *conflict;
446 conflict = __request_resource(parent, res);
449 if (conflict != parent) {
451 if (!(conflict->flags & IORESOURCE_BUSY))
455 /* Uhhuh, that didn't work out.. */
460 write_unlock(&resource_lock);
465 EXPORT_SYMBOL(__request_region);
467 int __check_region(struct resource *parent, resource_size_t start,
470 struct resource * res;
472 res = __request_region(parent, start, n, "check-region");
476 release_resource(res);
481 EXPORT_SYMBOL(__check_region);
483 void __release_region(struct resource *parent, resource_size_t start,
492 write_lock(&resource_lock);
495 struct resource *res = *p;
499 if (res->start <= start && res->end >= end) {
500 if (!(res->flags & IORESOURCE_BUSY)) {
504 if (res->start != start || res->end != end)
507 write_unlock(&resource_lock);
514 write_unlock(&resource_lock);
516 printk(KERN_WARNING "Trying to free nonexistent resource "
517 "<%016llx-%016llx>\n", (unsigned long long)start,
518 (unsigned long long)end);
521 EXPORT_SYMBOL(__release_region);
524 * Called from init/main.c to reserve IO ports.
527 static int __init reserve_setup(char *str)
530 static struct resource reserve[MAXRESERVE];
533 int io_start, io_num;
536 if (get_option (&str, &io_start) != 2)
538 if (get_option (&str, &io_num) == 0)
540 if (x < MAXRESERVE) {
541 struct resource *res = reserve + x;
542 res->name = "reserved";
543 res->start = io_start;
544 res->end = io_start + io_num - 1;
545 res->flags = IORESOURCE_BUSY;
547 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
554 __setup("reserve=", reserve_setup);