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/module.h>
11 #include <linux/errno.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
17 #include <linux/proc_fs.h>
18 #include <linux/sched.h>
19 #include <linux/seq_file.h>
20 #include <linux/device.h>
21 #include <linux/pfn.h>
25 struct resource ioport_resource = {
28 .end = IO_SPACE_LIMIT,
29 .flags = IORESOURCE_IO,
31 EXPORT_SYMBOL(ioport_resource);
33 struct resource iomem_resource = {
37 .flags = IORESOURCE_MEM,
39 EXPORT_SYMBOL(iomem_resource);
41 static DEFINE_RWLOCK(resource_lock);
44 * By default, we allocate free space bottom-up. The architecture can request
45 * top-down by clearing this flag. The user can override the architecture's
46 * choice with the "resource_alloc_from_bottom" kernel boot option, but that
47 * should only be a debugging tool.
49 int resource_alloc_from_bottom = 1;
51 static __init int setup_alloc_from_bottom(char *s)
54 "resource: allocating from bottom-up; please report a bug\n");
55 resource_alloc_from_bottom = 1;
58 early_param("resource_alloc_from_bottom", setup_alloc_from_bottom);
60 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
62 struct resource *p = v;
66 while (!p->sibling && p->parent)
73 enum { MAX_IORES_LEVEL = 5 };
75 static void *r_start(struct seq_file *m, loff_t *pos)
76 __acquires(resource_lock)
78 struct resource *p = m->private;
80 read_lock(&resource_lock);
81 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
86 static void r_stop(struct seq_file *m, void *v)
87 __releases(resource_lock)
89 read_unlock(&resource_lock);
92 static int r_show(struct seq_file *m, void *v)
94 struct resource *root = m->private;
95 struct resource *r = v, *p;
96 int width = root->end < 0x10000 ? 4 : 8;
99 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
100 if (p->parent == root)
102 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
104 width, (unsigned long long) r->start,
105 width, (unsigned long long) r->end,
106 r->name ? r->name : "<BAD>");
110 static const struct seq_operations resource_op = {
117 static int ioports_open(struct inode *inode, struct file *file)
119 int res = seq_open(file, &resource_op);
121 struct seq_file *m = file->private_data;
122 m->private = &ioport_resource;
127 static int iomem_open(struct inode *inode, struct file *file)
129 int res = seq_open(file, &resource_op);
131 struct seq_file *m = file->private_data;
132 m->private = &iomem_resource;
137 static const struct file_operations proc_ioports_operations = {
138 .open = ioports_open,
141 .release = seq_release,
144 static const struct file_operations proc_iomem_operations = {
148 .release = seq_release,
151 static int __init ioresources_init(void)
153 proc_create("ioports", 0, NULL, &proc_ioports_operations);
154 proc_create("iomem", 0, NULL, &proc_iomem_operations);
157 __initcall(ioresources_init);
159 #endif /* CONFIG_PROC_FS */
161 /* Return the conflict entry if you can't request it */
162 static struct resource * __request_resource(struct resource *root, struct resource *new)
164 resource_size_t start = new->start;
165 resource_size_t end = new->end;
166 struct resource *tmp, **p;
170 if (start < root->start)
177 if (!tmp || tmp->start > end) {
184 if (tmp->end < start)
190 static int __release_resource(struct resource *old)
192 struct resource *tmp, **p;
194 p = &old->parent->child;
209 static void __release_child_resources(struct resource *r)
211 struct resource *tmp, *p;
212 resource_size_t size;
222 __release_child_resources(tmp);
224 printk(KERN_DEBUG "release child resource %pR\n", tmp);
225 /* need to restore size, and keep flags */
226 size = resource_size(tmp);
232 void release_child_resources(struct resource *r)
234 write_lock(&resource_lock);
235 __release_child_resources(r);
236 write_unlock(&resource_lock);
240 * request_resource_conflict - request and reserve an I/O or memory resource
241 * @root: root resource descriptor
242 * @new: resource descriptor desired by caller
244 * Returns 0 for success, conflict resource on error.
246 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
248 struct resource *conflict;
250 write_lock(&resource_lock);
251 conflict = __request_resource(root, new);
252 write_unlock(&resource_lock);
257 * request_resource - request and reserve an I/O or memory resource
258 * @root: root resource descriptor
259 * @new: resource descriptor desired by caller
261 * Returns 0 for success, negative error code on error.
263 int request_resource(struct resource *root, struct resource *new)
265 struct resource *conflict;
267 conflict = request_resource_conflict(root, new);
268 return conflict ? -EBUSY : 0;
271 EXPORT_SYMBOL(request_resource);
274 * release_resource - release a previously reserved resource
275 * @old: resource pointer
277 int release_resource(struct resource *old)
281 write_lock(&resource_lock);
282 retval = __release_resource(old);
283 write_unlock(&resource_lock);
287 EXPORT_SYMBOL(release_resource);
289 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
291 * Finds the lowest memory reosurce exists within [res->start.res->end)
292 * the caller must specify res->start, res->end, res->flags and "name".
293 * If found, returns 0, res is overwritten, if not found, returns -1.
295 static int find_next_system_ram(struct resource *res, char *name)
297 resource_size_t start, end;
304 BUG_ON(start >= end);
306 read_lock(&resource_lock);
307 for (p = iomem_resource.child; p ; p = p->sibling) {
308 /* system ram is just marked as IORESOURCE_MEM */
309 if (p->flags != res->flags)
311 if (name && strcmp(p->name, name))
313 if (p->start > end) {
317 if ((p->end >= start) && (p->start < end))
320 read_unlock(&resource_lock);
324 if (res->start < p->start)
325 res->start = p->start;
326 if (res->end > p->end)
332 * This function calls callback against all memory range of "System RAM"
333 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
334 * Now, this function is only for "System RAM".
336 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
337 void *arg, int (*func)(unsigned long, unsigned long, void *))
340 unsigned long pfn, end_pfn;
344 res.start = (u64) start_pfn << PAGE_SHIFT;
345 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
346 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
348 while ((res.start < res.end) &&
349 (find_next_system_ram(&res, "System RAM") >= 0)) {
350 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
351 end_pfn = (res.end + 1) >> PAGE_SHIFT;
353 ret = (*func)(pfn, end_pfn - pfn, arg);
356 res.start = res.end + 1;
364 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
369 * This generic page_is_ram() returns true if specified address is
370 * registered as "System RAM" in iomem_resource list.
372 int __weak page_is_ram(unsigned long pfn)
374 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
377 static resource_size_t simple_align_resource(void *data,
378 const struct resource *avail,
379 resource_size_t size,
380 resource_size_t align)
385 static void resource_clip(struct resource *res, resource_size_t min,
388 if (res->start < min)
394 static bool resource_contains(struct resource *res1, struct resource *res2)
396 return res1->start <= res2->start && res1->end >= res2->end;
400 * Find the resource before "child" in the sibling list of "root" children.
402 static struct resource *find_sibling_prev(struct resource *root, struct resource *child)
404 struct resource *this;
406 for (this = root->child; this; this = this->sibling)
407 if (this->sibling == child)
414 * Find empty slot in the resource tree given range and alignment.
415 * This version allocates from the end of the root resource first.
417 static int find_resource_from_top(struct resource *root, struct resource *new,
418 resource_size_t size, resource_size_t min,
419 resource_size_t max, resource_size_t align,
420 resource_size_t (*alignf)(void *,
421 const struct resource *,
426 struct resource *this;
427 struct resource tmp, avail, alloc;
429 tmp.start = root->end;
432 this = find_sibling_prev(root, NULL);
435 if (this->end < root->end)
436 tmp.start = this->end + 1;
438 tmp.start = root->start;
440 resource_clip(&tmp, min, max);
442 /* Check for overflow after ALIGN() */
444 avail.start = ALIGN(tmp.start, align);
446 if (avail.start >= tmp.start) {
447 alloc.start = alignf(alignf_data, &avail, size, align);
448 alloc.end = alloc.start + size - 1;
449 if (resource_contains(&avail, &alloc)) {
450 new->start = alloc.start;
451 new->end = alloc.end;
456 if (!this || this->start == root->start)
459 tmp.end = this->start - 1;
460 this = find_sibling_prev(root, this);
466 * Find empty slot in the resource tree given range and alignment.
467 * This version allocates from the beginning of the root resource first.
469 static int find_resource(struct resource *root, struct resource *new,
470 resource_size_t size, resource_size_t min,
471 resource_size_t max, resource_size_t align,
472 resource_size_t (*alignf)(void *,
473 const struct resource *,
478 struct resource *this = root->child;
479 struct resource tmp = *new, avail, alloc;
481 tmp.start = root->start;
483 * Skip past an allocated resource that starts at 0, since the
484 * assignment of this->start - 1 to tmp->end below would cause an
487 if (this && this->start == 0) {
488 tmp.start = this->end + 1;
489 this = this->sibling;
493 tmp.end = this->start - 1;
497 resource_clip(&tmp, min, max);
499 /* Check for overflow after ALIGN() */
501 avail.start = ALIGN(tmp.start, align);
503 if (avail.start >= tmp.start) {
504 alloc.start = alignf(alignf_data, &avail, size, align);
505 alloc.end = alloc.start + size - 1;
506 if (resource_contains(&avail, &alloc)) {
507 new->start = alloc.start;
508 new->end = alloc.end;
516 tmp.start = this->end + 1;
517 this = this->sibling;
523 * allocate_resource - allocate empty slot in the resource tree given range & alignment
524 * @root: root resource descriptor
525 * @new: resource descriptor desired by caller
526 * @size: requested resource region size
527 * @min: minimum size to allocate
528 * @max: maximum size to allocate
529 * @align: alignment requested, in bytes
530 * @alignf: alignment function, optional, called if not NULL
531 * @alignf_data: arbitrary data to pass to the @alignf function
533 int allocate_resource(struct resource *root, struct resource *new,
534 resource_size_t size, resource_size_t min,
535 resource_size_t max, resource_size_t align,
536 resource_size_t (*alignf)(void *,
537 const struct resource *,
545 alignf = simple_align_resource;
547 write_lock(&resource_lock);
548 if (resource_alloc_from_bottom)
549 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
551 err = find_resource_from_top(root, new, size, min, max, align, alignf, alignf_data);
552 if (err >= 0 && __request_resource(root, new))
554 write_unlock(&resource_lock);
558 EXPORT_SYMBOL(allocate_resource);
561 * Insert a resource into the resource tree. If successful, return NULL,
562 * otherwise return the conflicting resource (compare to __request_resource())
564 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
566 struct resource *first, *next;
568 for (;; parent = first) {
569 first = __request_resource(parent, new);
576 if ((first->start > new->start) || (first->end < new->end))
578 if ((first->start == new->start) && (first->end == new->end))
582 for (next = first; ; next = next->sibling) {
583 /* Partial overlap? Bad, and unfixable */
584 if (next->start < new->start || next->end > new->end)
588 if (next->sibling->start > new->end)
592 new->parent = parent;
593 new->sibling = next->sibling;
596 next->sibling = NULL;
597 for (next = first; next; next = next->sibling)
600 if (parent->child == first) {
603 next = parent->child;
604 while (next->sibling != first)
605 next = next->sibling;
612 * insert_resource_conflict - Inserts resource in the resource tree
613 * @parent: parent of the new resource
614 * @new: new resource to insert
616 * Returns 0 on success, conflict resource if the resource can't be inserted.
618 * This function is equivalent to request_resource_conflict when no conflict
619 * happens. If a conflict happens, and the conflicting resources
620 * entirely fit within the range of the new resource, then the new
621 * resource is inserted and the conflicting resources become children of
624 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
626 struct resource *conflict;
628 write_lock(&resource_lock);
629 conflict = __insert_resource(parent, new);
630 write_unlock(&resource_lock);
635 * insert_resource - Inserts a resource in the resource tree
636 * @parent: parent of the new resource
637 * @new: new resource to insert
639 * Returns 0 on success, -EBUSY if the resource can't be inserted.
641 int insert_resource(struct resource *parent, struct resource *new)
643 struct resource *conflict;
645 conflict = insert_resource_conflict(parent, new);
646 return conflict ? -EBUSY : 0;
650 * insert_resource_expand_to_fit - Insert a resource into the resource tree
651 * @root: root resource descriptor
652 * @new: new resource to insert
654 * Insert a resource into the resource tree, possibly expanding it in order
655 * to make it encompass any conflicting resources.
657 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
662 write_lock(&resource_lock);
664 struct resource *conflict;
666 conflict = __insert_resource(root, new);
669 if (conflict == root)
672 /* Ok, expand resource to cover the conflict, then try again .. */
673 if (conflict->start < new->start)
674 new->start = conflict->start;
675 if (conflict->end > new->end)
676 new->end = conflict->end;
678 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
680 write_unlock(&resource_lock);
684 * adjust_resource - modify a resource's start and size
685 * @res: resource to modify
686 * @start: new start value
689 * Given an existing resource, change its start and size to match the
690 * arguments. Returns 0 on success, -EBUSY if it can't fit.
691 * Existing children of the resource are assumed to be immutable.
693 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
695 struct resource *tmp, *parent = res->parent;
696 resource_size_t end = start + size - 1;
699 write_lock(&resource_lock);
701 if ((start < parent->start) || (end > parent->end))
704 for (tmp = res->child; tmp; tmp = tmp->sibling) {
705 if ((tmp->start < start) || (tmp->end > end))
709 if (res->sibling && (res->sibling->start <= end))
714 while (tmp->sibling != res)
716 if (start <= tmp->end)
725 write_unlock(&resource_lock);
729 static void __init __reserve_region_with_split(struct resource *root,
730 resource_size_t start, resource_size_t end,
733 struct resource *parent = root;
734 struct resource *conflict;
735 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
743 res->flags = IORESOURCE_BUSY;
745 conflict = __request_resource(parent, res);
749 /* failed, split and try again */
752 /* conflict covered whole area */
753 if (conflict->start <= start && conflict->end >= end)
756 if (conflict->start > start)
757 __reserve_region_with_split(root, start, conflict->start-1, name);
758 if (conflict->end < end)
759 __reserve_region_with_split(root, conflict->end+1, end, name);
762 void __init reserve_region_with_split(struct resource *root,
763 resource_size_t start, resource_size_t end,
766 write_lock(&resource_lock);
767 __reserve_region_with_split(root, start, end, name);
768 write_unlock(&resource_lock);
771 EXPORT_SYMBOL(adjust_resource);
774 * resource_alignment - calculate resource's alignment
775 * @res: resource pointer
777 * Returns alignment on success, 0 (invalid alignment) on failure.
779 resource_size_t resource_alignment(struct resource *res)
781 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
782 case IORESOURCE_SIZEALIGN:
783 return resource_size(res);
784 case IORESOURCE_STARTALIGN:
792 * This is compatibility stuff for IO resources.
794 * Note how this, unlike the above, knows about
795 * the IO flag meanings (busy etc).
797 * request_region creates a new busy region.
799 * check_region returns non-zero if the area is already busy.
801 * release_region releases a matching busy region.
804 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
807 * __request_region - create a new busy resource region
808 * @parent: parent resource descriptor
809 * @start: resource start address
810 * @n: resource region size
811 * @name: reserving caller's ID string
812 * @flags: IO resource flags
814 struct resource * __request_region(struct resource *parent,
815 resource_size_t start, resource_size_t n,
816 const char *name, int flags)
818 DECLARE_WAITQUEUE(wait, current);
819 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
826 res->end = start + n - 1;
827 res->flags = IORESOURCE_BUSY;
830 write_lock(&resource_lock);
833 struct resource *conflict;
835 conflict = __request_resource(parent, res);
838 if (conflict != parent) {
840 if (!(conflict->flags & IORESOURCE_BUSY))
843 if (conflict->flags & flags & IORESOURCE_MUXED) {
844 add_wait_queue(&muxed_resource_wait, &wait);
845 write_unlock(&resource_lock);
846 set_current_state(TASK_UNINTERRUPTIBLE);
848 remove_wait_queue(&muxed_resource_wait, &wait);
849 write_lock(&resource_lock);
852 /* Uhhuh, that didn't work out.. */
857 write_unlock(&resource_lock);
860 EXPORT_SYMBOL(__request_region);
863 * __check_region - check if a resource region is busy or free
864 * @parent: parent resource descriptor
865 * @start: resource start address
866 * @n: resource region size
868 * Returns 0 if the region is free at the moment it is checked,
869 * returns %-EBUSY if the region is busy.
872 * This function is deprecated because its use is racy.
873 * Even if it returns 0, a subsequent call to request_region()
874 * may fail because another driver etc. just allocated the region.
875 * Do NOT use it. It will be removed from the kernel.
877 int __check_region(struct resource *parent, resource_size_t start,
880 struct resource * res;
882 res = __request_region(parent, start, n, "check-region", 0);
886 release_resource(res);
890 EXPORT_SYMBOL(__check_region);
893 * __release_region - release a previously reserved resource region
894 * @parent: parent resource descriptor
895 * @start: resource start address
896 * @n: resource region size
898 * The described resource region must match a currently busy region.
900 void __release_region(struct resource *parent, resource_size_t start,
909 write_lock(&resource_lock);
912 struct resource *res = *p;
916 if (res->start <= start && res->end >= end) {
917 if (!(res->flags & IORESOURCE_BUSY)) {
921 if (res->start != start || res->end != end)
924 write_unlock(&resource_lock);
925 if (res->flags & IORESOURCE_MUXED)
926 wake_up(&muxed_resource_wait);
933 write_unlock(&resource_lock);
935 printk(KERN_WARNING "Trying to free nonexistent resource "
936 "<%016llx-%016llx>\n", (unsigned long long)start,
937 (unsigned long long)end);
939 EXPORT_SYMBOL(__release_region);
942 * Managed region resource
944 struct region_devres {
945 struct resource *parent;
946 resource_size_t start;
950 static void devm_region_release(struct device *dev, void *res)
952 struct region_devres *this = res;
954 __release_region(this->parent, this->start, this->n);
957 static int devm_region_match(struct device *dev, void *res, void *match_data)
959 struct region_devres *this = res, *match = match_data;
961 return this->parent == match->parent &&
962 this->start == match->start && this->n == match->n;
965 struct resource * __devm_request_region(struct device *dev,
966 struct resource *parent, resource_size_t start,
967 resource_size_t n, const char *name)
969 struct region_devres *dr = NULL;
970 struct resource *res;
972 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
981 res = __request_region(parent, start, n, name, 0);
989 EXPORT_SYMBOL(__devm_request_region);
991 void __devm_release_region(struct device *dev, struct resource *parent,
992 resource_size_t start, resource_size_t n)
994 struct region_devres match_data = { parent, start, n };
996 __release_region(parent, start, n);
997 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1000 EXPORT_SYMBOL(__devm_release_region);
1003 * Called from init/main.c to reserve IO ports.
1005 #define MAXRESERVE 4
1006 static int __init reserve_setup(char *str)
1008 static int reserved;
1009 static struct resource reserve[MAXRESERVE];
1012 unsigned int io_start, io_num;
1015 if (get_option (&str, &io_start) != 2)
1017 if (get_option (&str, &io_num) == 0)
1019 if (x < MAXRESERVE) {
1020 struct resource *res = reserve + x;
1021 res->name = "reserved";
1022 res->start = io_start;
1023 res->end = io_start + io_num - 1;
1024 res->flags = IORESOURCE_BUSY;
1026 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
1033 __setup("reserve=", reserve_setup);
1036 * Check if the requested addr and size spans more than any slot in the
1037 * iomem resource tree.
1039 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1041 struct resource *p = &iomem_resource;
1045 read_lock(&resource_lock);
1046 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1048 * We can probably skip the resources without
1049 * IORESOURCE_IO attribute?
1051 if (p->start >= addr + size)
1055 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1056 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1059 * if a resource is "BUSY", it's not a hardware resource
1060 * but a driver mapping of such a resource; we don't want
1061 * to warn for those; some drivers legitimately map only
1062 * partial hardware resources. (example: vesafb)
1064 if (p->flags & IORESOURCE_BUSY)
1067 printk(KERN_WARNING "resource map sanity check conflict: "
1068 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
1069 (unsigned long long)addr,
1070 (unsigned long long)(addr + size - 1),
1071 (unsigned long long)p->start,
1072 (unsigned long long)p->end,
1077 read_unlock(&resource_lock);
1082 #ifdef CONFIG_STRICT_DEVMEM
1083 static int strict_iomem_checks = 1;
1085 static int strict_iomem_checks;
1089 * check if an address is reserved in the iomem resource tree
1090 * returns 1 if reserved, 0 if not reserved.
1092 int iomem_is_exclusive(u64 addr)
1094 struct resource *p = &iomem_resource;
1097 int size = PAGE_SIZE;
1099 if (!strict_iomem_checks)
1102 addr = addr & PAGE_MASK;
1104 read_lock(&resource_lock);
1105 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1107 * We can probably skip the resources without
1108 * IORESOURCE_IO attribute?
1110 if (p->start >= addr + size)
1114 if (p->flags & IORESOURCE_BUSY &&
1115 p->flags & IORESOURCE_EXCLUSIVE) {
1120 read_unlock(&resource_lock);
1125 static int __init strict_iomem(char *str)
1127 if (strstr(str, "relaxed"))
1128 strict_iomem_checks = 0;
1129 if (strstr(str, "strict"))
1130 strict_iomem_checks = 1;
1134 __setup("iomem=", strict_iomem);