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,
30 EXPORT_SYMBOL(ioport_resource);
32 struct resource iomem_resource = {
36 .flags = IORESOURCE_MEM,
38 EXPORT_SYMBOL(iomem_resource);
40 static DEFINE_RWLOCK(resource_lock);
44 enum { MAX_IORES_LEVEL = 5 };
46 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
48 struct resource *p = v;
52 while (!p->sibling && p->parent)
57 static void *r_start(struct seq_file *m, loff_t *pos)
58 __acquires(resource_lock)
60 struct resource *p = m->private;
62 read_lock(&resource_lock);
63 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
68 static void r_stop(struct seq_file *m, void *v)
69 __releases(resource_lock)
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*llx-%0*llx : %s\n",
86 width, (unsigned long long) r->start,
87 width, (unsigned long long) r->end,
88 r->name ? r->name : "<BAD>");
92 static 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 resource_size_t start = new->start;
153 resource_size_t 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,
237 resource_size_t size, resource_size_t min,
238 resource_size_t max, resource_size_t align,
239 void (*alignf)(void *, struct resource *,
240 resource_size_t, resource_size_t),
243 struct resource *this = root->child;
245 new->start = root->start;
247 * Skip past an allocated resource that starts at 0, since the assignment
248 * of this->start - 1 to new->end below would cause an underflow.
250 if (this && this->start == 0) {
251 new->start = this->end + 1;
252 this = this->sibling;
256 new->end = this->start - 1;
258 new->end = root->end;
259 if (new->start < min)
263 new->start = ALIGN(new->start, align);
265 alignf(alignf_data, new, size, align);
266 if (new->start < new->end && new->end - new->start >= size - 1) {
267 new->end = new->start + size - 1;
272 new->start = this->end + 1;
273 this = this->sibling;
279 * Allocate empty slot in the resource tree given range and alignment.
281 int allocate_resource(struct resource *root, struct resource *new,
282 resource_size_t size, resource_size_t min,
283 resource_size_t max, resource_size_t align,
284 void (*alignf)(void *, struct resource *,
285 resource_size_t, resource_size_t),
290 write_lock(&resource_lock);
291 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
292 if (err >= 0 && __request_resource(root, new))
294 write_unlock(&resource_lock);
298 EXPORT_SYMBOL(allocate_resource);
301 * insert_resource - Inserts a resource in the resource tree
302 * @parent: parent of the new resource
303 * @new: new resource to insert
305 * Returns 0 on success, -EBUSY if the resource can't be inserted.
307 * This function is equivalent of request_resource when no conflict
308 * happens. If a conflict happens, and the conflicting resources
309 * entirely fit within the range of the new resource, then the new
310 * resource is inserted and the conflicting resources become childs of
311 * the new resource. Otherwise the new resource becomes the child of
312 * the conflicting resource
314 int insert_resource(struct resource *parent, struct resource *new)
317 struct resource *first, *next;
319 write_lock(&resource_lock);
322 first = __request_resource(parent, new);
330 /* Resource fully contained by the clashing resource? Recurse into it */
331 if (first->start <= new->start && first->end >= new->end) {
336 for (next = first; ; next = next->sibling) {
337 /* Partial overlap? Bad, and unfixable */
338 if (next->start < new->start || next->end > new->end)
342 if (next->sibling->start > new->end)
348 new->parent = parent;
349 new->sibling = next->sibling;
352 next->sibling = NULL;
353 for (next = first; next; next = next->sibling)
356 if (parent->child == first) {
359 next = parent->child;
360 while (next->sibling != first)
361 next = next->sibling;
366 write_unlock(&resource_lock);
370 EXPORT_SYMBOL(insert_resource);
373 * Given an existing resource, change its start and size to match the
374 * arguments. Returns -EBUSY if it can't fit. Existing children of
375 * the resource are assumed to be immutable.
377 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
379 struct resource *tmp, *parent = res->parent;
380 resource_size_t end = start + size - 1;
383 write_lock(&resource_lock);
385 if ((start < parent->start) || (end > parent->end))
388 for (tmp = res->child; tmp; tmp = tmp->sibling) {
389 if ((tmp->start < start) || (tmp->end > end))
393 if (res->sibling && (res->sibling->start <= end))
398 while (tmp->sibling != res)
400 if (start <= tmp->end)
409 write_unlock(&resource_lock);
413 EXPORT_SYMBOL(adjust_resource);
416 * This is compatibility stuff for IO resources.
418 * Note how this, unlike the above, knows about
419 * the IO flag meanings (busy etc).
421 * Request-region creates a new busy region.
423 * Check-region returns non-zero if the area is already busy
425 * Release-region releases a matching busy region.
427 struct resource * __request_region(struct resource *parent,
428 resource_size_t start, resource_size_t n,
431 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
436 res->end = start + n - 1;
437 res->flags = IORESOURCE_BUSY;
439 write_lock(&resource_lock);
442 struct resource *conflict;
444 conflict = __request_resource(parent, res);
447 if (conflict != parent) {
449 if (!(conflict->flags & IORESOURCE_BUSY))
453 /* Uhhuh, that didn't work out.. */
458 write_unlock(&resource_lock);
463 EXPORT_SYMBOL(__request_region);
465 int __check_region(struct resource *parent, resource_size_t start,
468 struct resource * res;
470 res = __request_region(parent, start, n, "check-region");
474 release_resource(res);
479 EXPORT_SYMBOL(__check_region);
481 void __release_region(struct resource *parent, resource_size_t start,
490 write_lock(&resource_lock);
493 struct resource *res = *p;
497 if (res->start <= start && res->end >= end) {
498 if (!(res->flags & IORESOURCE_BUSY)) {
502 if (res->start != start || res->end != end)
505 write_unlock(&resource_lock);
512 write_unlock(&resource_lock);
514 printk(KERN_WARNING "Trying to free nonexistent resource "
515 "<%016llx-%016llx>\n", (unsigned long long)start,
516 (unsigned long long)end);
519 EXPORT_SYMBOL(__release_region);
522 * Called from init/main.c to reserve IO ports.
525 static int __init reserve_setup(char *str)
528 static struct resource reserve[MAXRESERVE];
531 int io_start, io_num;
534 if (get_option (&str, &io_start) != 2)
536 if (get_option (&str, &io_num) == 0)
538 if (x < MAXRESERVE) {
539 struct resource *res = reserve + x;
540 res->name = "reserved";
541 res->start = io_start;
542 res->end = io_start + io_num - 1;
543 res->flags = IORESOURCE_BUSY;
545 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
552 __setup("reserve=", reserve_setup);