[UDP]: UDP can use sk_hash to speedup lookups
[pv_ops_mirror.git] / kernel / resource.c
blob2a3f886365808241e329222772d28dfadf87da62
1 /*
2 * linux/kernel/resource.c
4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
7 * Arbitrary resource management.
8 */
10 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/errno.h>
13 #include <linux/ioport.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
17 #include <linux/fs.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/device.h>
21 #include <asm/io.h>
24 struct resource ioport_resource = {
25 .name = "PCI IO",
26 .start = 0,
27 .end = IO_SPACE_LIMIT,
28 .flags = IORESOURCE_IO,
30 EXPORT_SYMBOL(ioport_resource);
32 struct resource iomem_resource = {
33 .name = "PCI mem",
34 .start = 0,
35 .end = -1,
36 .flags = IORESOURCE_MEM,
38 EXPORT_SYMBOL(iomem_resource);
40 static DEFINE_RWLOCK(resource_lock);
42 #ifdef CONFIG_PROC_FS
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;
49 (*pos)++;
50 if (p->child)
51 return p->child;
52 while (!p->sibling && p->parent)
53 p = p->parent;
54 return p->sibling;
57 static void *r_start(struct seq_file *m, loff_t *pos)
58 __acquires(resource_lock)
60 struct resource *p = m->private;
61 loff_t l = 0;
62 read_lock(&resource_lock);
63 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
65 return p;
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;
79 int depth;
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*llx-%0*llx : %s\n",
85 depth * 2, "",
86 width, (unsigned long long) r->start,
87 width, (unsigned long long) r->end,
88 r->name ? r->name : "<BAD>");
89 return 0;
92 static const struct seq_operations resource_op = {
93 .start = r_start,
94 .next = r_next,
95 .stop = r_stop,
96 .show = r_show,
99 static int ioports_open(struct inode *inode, struct file *file)
101 int res = seq_open(file, &resource_op);
102 if (!res) {
103 struct seq_file *m = file->private_data;
104 m->private = &ioport_resource;
106 return res;
109 static int iomem_open(struct inode *inode, struct file *file)
111 int res = seq_open(file, &resource_op);
112 if (!res) {
113 struct seq_file *m = file->private_data;
114 m->private = &iomem_resource;
116 return res;
119 static const struct file_operations proc_ioports_operations = {
120 .open = ioports_open,
121 .read = seq_read,
122 .llseek = seq_lseek,
123 .release = seq_release,
126 static const struct file_operations proc_iomem_operations = {
127 .open = iomem_open,
128 .read = seq_read,
129 .llseek = seq_lseek,
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);
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;
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;
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;
171 p = &tmp->sibling;
172 if (tmp->end < start)
173 continue;
174 return tmp;
178 static int __release_resource(struct resource *old)
180 struct resource *tmp, **p;
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;
192 p = &tmp->sibling;
194 return -EINVAL;
198 * request_resource - request and reserve an I/O or memory resource
199 * @root: root resource descriptor
200 * @new: resource descriptor desired by caller
202 * Returns 0 for success, negative error code on error.
204 int request_resource(struct resource *root, struct resource *new)
206 struct resource *conflict;
208 write_lock(&resource_lock);
209 conflict = __request_resource(root, new);
210 write_unlock(&resource_lock);
211 return conflict ? -EBUSY : 0;
214 EXPORT_SYMBOL(request_resource);
217 * ____request_resource - reserve a resource, with resource conflict returned
218 * @root: root resource descriptor
219 * @new: resource descriptor desired by caller
221 * Returns:
222 * On success, NULL is returned.
223 * On error, a pointer to the conflicting resource is returned.
225 struct resource *____request_resource(struct resource *root, struct resource *new)
227 struct resource *conflict;
229 write_lock(&resource_lock);
230 conflict = __request_resource(root, new);
231 write_unlock(&resource_lock);
232 return conflict;
235 EXPORT_SYMBOL(____request_resource);
238 * release_resource - release a previously reserved resource
239 * @old: resource pointer
241 int release_resource(struct resource *old)
243 int retval;
245 write_lock(&resource_lock);
246 retval = __release_resource(old);
247 write_unlock(&resource_lock);
248 return retval;
251 EXPORT_SYMBOL(release_resource);
253 #ifdef CONFIG_MEMORY_HOTPLUG
255 * Finds the lowest memory reosurce exists within [res->start.res->end)
256 * the caller must specify res->start, res->end, res->flags.
257 * If found, returns 0, res is overwritten, if not found, returns -1.
259 int find_next_system_ram(struct resource *res)
261 resource_size_t start, end;
262 struct resource *p;
264 BUG_ON(!res);
266 start = res->start;
267 end = res->end;
268 BUG_ON(start >= end);
270 read_lock(&resource_lock);
271 for (p = iomem_resource.child; p ; p = p->sibling) {
272 /* system ram is just marked as IORESOURCE_MEM */
273 if (p->flags != res->flags)
274 continue;
275 if (p->start > end) {
276 p = NULL;
277 break;
279 if ((p->end >= start) && (p->start < end))
280 break;
282 read_unlock(&resource_lock);
283 if (!p)
284 return -1;
285 /* copy data */
286 if (res->start < p->start)
287 res->start = p->start;
288 if (res->end > p->end)
289 res->end = p->end;
290 return 0;
292 #endif
295 * Find empty slot in the resource tree given range and alignment.
297 static int find_resource(struct resource *root, struct resource *new,
298 resource_size_t size, resource_size_t min,
299 resource_size_t max, resource_size_t align,
300 void (*alignf)(void *, struct resource *,
301 resource_size_t, resource_size_t),
302 void *alignf_data)
304 struct resource *this = root->child;
306 new->start = root->start;
308 * Skip past an allocated resource that starts at 0, since the assignment
309 * of this->start - 1 to new->end below would cause an underflow.
311 if (this && this->start == 0) {
312 new->start = this->end + 1;
313 this = this->sibling;
315 for(;;) {
316 if (this)
317 new->end = this->start - 1;
318 else
319 new->end = root->end;
320 if (new->start < min)
321 new->start = min;
322 if (new->end > max)
323 new->end = max;
324 new->start = ALIGN(new->start, align);
325 if (alignf)
326 alignf(alignf_data, new, size, align);
327 if (new->start < new->end && new->end - new->start >= size - 1) {
328 new->end = new->start + size - 1;
329 return 0;
331 if (!this)
332 break;
333 new->start = this->end + 1;
334 this = this->sibling;
336 return -EBUSY;
340 * allocate_resource - allocate empty slot in the resource tree given range & alignment
341 * @root: root resource descriptor
342 * @new: resource descriptor desired by caller
343 * @size: requested resource region size
344 * @min: minimum size to allocate
345 * @max: maximum size to allocate
346 * @align: alignment requested, in bytes
347 * @alignf: alignment function, optional, called if not NULL
348 * @alignf_data: arbitrary data to pass to the @alignf function
350 int allocate_resource(struct resource *root, struct resource *new,
351 resource_size_t size, resource_size_t min,
352 resource_size_t max, resource_size_t align,
353 void (*alignf)(void *, struct resource *,
354 resource_size_t, resource_size_t),
355 void *alignf_data)
357 int err;
359 write_lock(&resource_lock);
360 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
361 if (err >= 0 && __request_resource(root, new))
362 err = -EBUSY;
363 write_unlock(&resource_lock);
364 return err;
367 EXPORT_SYMBOL(allocate_resource);
370 * insert_resource - Inserts a resource in the resource tree
371 * @parent: parent of the new resource
372 * @new: new resource to insert
374 * Returns 0 on success, -EBUSY if the resource can't be inserted.
376 * This function is equivalent to request_resource when no conflict
377 * happens. If a conflict happens, and the conflicting resources
378 * entirely fit within the range of the new resource, then the new
379 * resource is inserted and the conflicting resources become children of
380 * the new resource.
382 int insert_resource(struct resource *parent, struct resource *new)
384 int result;
385 struct resource *first, *next;
387 write_lock(&resource_lock);
389 for (;; parent = first) {
390 result = 0;
391 first = __request_resource(parent, new);
392 if (!first)
393 goto out;
395 result = -EBUSY;
396 if (first == parent)
397 goto out;
399 if ((first->start > new->start) || (first->end < new->end))
400 break;
401 if ((first->start == new->start) && (first->end == new->end))
402 break;
405 for (next = first; ; next = next->sibling) {
406 /* Partial overlap? Bad, and unfixable */
407 if (next->start < new->start || next->end > new->end)
408 goto out;
409 if (!next->sibling)
410 break;
411 if (next->sibling->start > new->end)
412 break;
415 result = 0;
417 new->parent = parent;
418 new->sibling = next->sibling;
419 new->child = first;
421 next->sibling = NULL;
422 for (next = first; next; next = next->sibling)
423 next->parent = new;
425 if (parent->child == first) {
426 parent->child = new;
427 } else {
428 next = parent->child;
429 while (next->sibling != first)
430 next = next->sibling;
431 next->sibling = new;
434 out:
435 write_unlock(&resource_lock);
436 return result;
440 * adjust_resource - modify a resource's start and size
441 * @res: resource to modify
442 * @start: new start value
443 * @size: new size
445 * Given an existing resource, change its start and size to match the
446 * arguments. Returns 0 on success, -EBUSY if it can't fit.
447 * Existing children of the resource are assumed to be immutable.
449 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
451 struct resource *tmp, *parent = res->parent;
452 resource_size_t end = start + size - 1;
453 int result = -EBUSY;
455 write_lock(&resource_lock);
457 if ((start < parent->start) || (end > parent->end))
458 goto out;
460 for (tmp = res->child; tmp; tmp = tmp->sibling) {
461 if ((tmp->start < start) || (tmp->end > end))
462 goto out;
465 if (res->sibling && (res->sibling->start <= end))
466 goto out;
468 tmp = parent->child;
469 if (tmp != res) {
470 while (tmp->sibling != res)
471 tmp = tmp->sibling;
472 if (start <= tmp->end)
473 goto out;
476 res->start = start;
477 res->end = end;
478 result = 0;
480 out:
481 write_unlock(&resource_lock);
482 return result;
485 EXPORT_SYMBOL(adjust_resource);
488 * This is compatibility stuff for IO resources.
490 * Note how this, unlike the above, knows about
491 * the IO flag meanings (busy etc).
493 * request_region creates a new busy region.
495 * check_region returns non-zero if the area is already busy.
497 * release_region releases a matching busy region.
501 * __request_region - create a new busy resource region
502 * @parent: parent resource descriptor
503 * @start: resource start address
504 * @n: resource region size
505 * @name: reserving caller's ID string
507 struct resource * __request_region(struct resource *parent,
508 resource_size_t start, resource_size_t n,
509 const char *name)
511 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
513 if (res) {
514 res->name = name;
515 res->start = start;
516 res->end = start + n - 1;
517 res->flags = IORESOURCE_BUSY;
519 write_lock(&resource_lock);
521 for (;;) {
522 struct resource *conflict;
524 conflict = __request_resource(parent, res);
525 if (!conflict)
526 break;
527 if (conflict != parent) {
528 parent = conflict;
529 if (!(conflict->flags & IORESOURCE_BUSY))
530 continue;
533 /* Uhhuh, that didn't work out.. */
534 kfree(res);
535 res = NULL;
536 break;
538 write_unlock(&resource_lock);
540 return res;
542 EXPORT_SYMBOL(__request_region);
545 * __check_region - check if a resource region is busy or free
546 * @parent: parent resource descriptor
547 * @start: resource start address
548 * @n: resource region size
550 * Returns 0 if the region is free at the moment it is checked,
551 * returns %-EBUSY if the region is busy.
553 * NOTE:
554 * This function is deprecated because its use is racy.
555 * Even if it returns 0, a subsequent call to request_region()
556 * may fail because another driver etc. just allocated the region.
557 * Do NOT use it. It will be removed from the kernel.
559 int __check_region(struct resource *parent, resource_size_t start,
560 resource_size_t n)
562 struct resource * res;
564 res = __request_region(parent, start, n, "check-region");
565 if (!res)
566 return -EBUSY;
568 release_resource(res);
569 kfree(res);
570 return 0;
572 EXPORT_SYMBOL(__check_region);
575 * __release_region - release a previously reserved resource region
576 * @parent: parent resource descriptor
577 * @start: resource start address
578 * @n: resource region size
580 * The described resource region must match a currently busy region.
582 void __release_region(struct resource *parent, resource_size_t start,
583 resource_size_t n)
585 struct resource **p;
586 resource_size_t end;
588 p = &parent->child;
589 end = start + n - 1;
591 write_lock(&resource_lock);
593 for (;;) {
594 struct resource *res = *p;
596 if (!res)
597 break;
598 if (res->start <= start && res->end >= end) {
599 if (!(res->flags & IORESOURCE_BUSY)) {
600 p = &res->child;
601 continue;
603 if (res->start != start || res->end != end)
604 break;
605 *p = res->sibling;
606 write_unlock(&resource_lock);
607 kfree(res);
608 return;
610 p = &res->sibling;
613 write_unlock(&resource_lock);
615 printk(KERN_WARNING "Trying to free nonexistent resource "
616 "<%016llx-%016llx>\n", (unsigned long long)start,
617 (unsigned long long)end);
619 EXPORT_SYMBOL(__release_region);
622 * Managed region resource
624 struct region_devres {
625 struct resource *parent;
626 resource_size_t start;
627 resource_size_t n;
630 static void devm_region_release(struct device *dev, void *res)
632 struct region_devres *this = res;
634 __release_region(this->parent, this->start, this->n);
637 static int devm_region_match(struct device *dev, void *res, void *match_data)
639 struct region_devres *this = res, *match = match_data;
641 return this->parent == match->parent &&
642 this->start == match->start && this->n == match->n;
645 struct resource * __devm_request_region(struct device *dev,
646 struct resource *parent, resource_size_t start,
647 resource_size_t n, const char *name)
649 struct region_devres *dr = NULL;
650 struct resource *res;
652 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
653 GFP_KERNEL);
654 if (!dr)
655 return NULL;
657 dr->parent = parent;
658 dr->start = start;
659 dr->n = n;
661 res = __request_region(parent, start, n, name);
662 if (res)
663 devres_add(dev, dr);
664 else
665 devres_free(dr);
667 return res;
669 EXPORT_SYMBOL(__devm_request_region);
671 void __devm_release_region(struct device *dev, struct resource *parent,
672 resource_size_t start, resource_size_t n)
674 struct region_devres match_data = { parent, start, n };
676 __release_region(parent, start, n);
677 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
678 &match_data));
680 EXPORT_SYMBOL(__devm_release_region);
683 * Called from init/main.c to reserve IO ports.
685 #define MAXRESERVE 4
686 static int __init reserve_setup(char *str)
688 static int reserved;
689 static struct resource reserve[MAXRESERVE];
691 for (;;) {
692 int io_start, io_num;
693 int x = reserved;
695 if (get_option (&str, &io_start) != 2)
696 break;
697 if (get_option (&str, &io_num) == 0)
698 break;
699 if (x < MAXRESERVE) {
700 struct resource *res = reserve + x;
701 res->name = "reserved";
702 res->start = io_start;
703 res->end = io_start + io_num - 1;
704 res->flags = IORESOURCE_BUSY;
705 res->child = NULL;
706 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
707 reserved = x+1;
710 return 1;
713 __setup("reserve=", reserve_setup);