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);
43 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
45 struct resource *p = v;
49 while (!p->sibling && p->parent)
56 enum { MAX_IORES_LEVEL = 5 };
58 static void *r_start(struct seq_file *m, loff_t *pos)
59 __acquires(resource_lock)
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)
70 __releases(resource_lock)
72 read_unlock(&resource_lock);
75 static int r_show(struct seq_file *m, void *v)
77 struct resource *root = m->private;
78 struct resource *r = v, *p;
79 int width = root->end < 0x10000 ? 4 : 8;
82 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
83 if (p->parent == root)
85 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
87 width, (unsigned long long) r->start,
88 width, (unsigned long long) r->end,
89 r->name ? r->name : "<BAD>");
93 static const struct seq_operations resource_op = {
100 static int ioports_open(struct inode *inode, struct file *file)
102 int res = seq_open(file, &resource_op);
104 struct seq_file *m = file->private_data;
105 m->private = &ioport_resource;
110 static int iomem_open(struct inode *inode, struct file *file)
112 int res = seq_open(file, &resource_op);
114 struct seq_file *m = file->private_data;
115 m->private = &iomem_resource;
120 static const struct file_operations proc_ioports_operations = {
121 .open = ioports_open,
124 .release = seq_release,
127 static const struct file_operations proc_iomem_operations = {
131 .release = seq_release,
134 static int __init ioresources_init(void)
136 proc_create("ioports", 0, NULL, &proc_ioports_operations);
137 proc_create("iomem", 0, NULL, &proc_iomem_operations);
140 __initcall(ioresources_init);
142 #endif /* CONFIG_PROC_FS */
144 /* Return the conflict entry if you can't request it */
145 static struct resource * __request_resource(struct resource *root, struct resource *new)
147 resource_size_t start = new->start;
148 resource_size_t end = new->end;
149 struct resource *tmp, **p;
153 if (start < root->start)
160 if (!tmp || tmp->start > end) {
167 if (tmp->end < start)
173 static int __release_resource(struct resource *old)
175 struct resource *tmp, **p;
177 p = &old->parent->child;
192 static void __release_child_resources(struct resource *r)
194 struct resource *tmp, *p;
195 resource_size_t size;
205 __release_child_resources(tmp);
207 printk(KERN_DEBUG "release child resource %pR\n", tmp);
208 /* need to restore size, and keep flags */
209 size = resource_size(tmp);
215 void release_child_resources(struct resource *r)
217 write_lock(&resource_lock);
218 __release_child_resources(r);
219 write_unlock(&resource_lock);
223 * request_resource_conflict - request and reserve an I/O or memory resource
224 * @root: root resource descriptor
225 * @new: resource descriptor desired by caller
227 * Returns 0 for success, conflict resource on error.
229 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
231 struct resource *conflict;
233 write_lock(&resource_lock);
234 conflict = __request_resource(root, new);
235 write_unlock(&resource_lock);
240 * request_resource - 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, negative error code on error.
246 int request_resource(struct resource *root, struct resource *new)
248 struct resource *conflict;
250 conflict = request_resource_conflict(root, new);
251 return conflict ? -EBUSY : 0;
254 EXPORT_SYMBOL(request_resource);
257 * release_resource - release a previously reserved resource
258 * @old: resource pointer
260 int release_resource(struct resource *old)
264 write_lock(&resource_lock);
265 retval = __release_resource(old);
266 write_unlock(&resource_lock);
270 EXPORT_SYMBOL(release_resource);
272 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
274 * Finds the lowest memory reosurce exists within [res->start.res->end)
275 * the caller must specify res->start, res->end, res->flags and "name".
276 * If found, returns 0, res is overwritten, if not found, returns -1.
278 static int find_next_system_ram(struct resource *res, char *name)
280 resource_size_t start, end;
287 BUG_ON(start >= end);
289 read_lock(&resource_lock);
290 for (p = iomem_resource.child; p ; p = p->sibling) {
291 /* system ram is just marked as IORESOURCE_MEM */
292 if (p->flags != res->flags)
294 if (name && strcmp(p->name, name))
296 if (p->start > end) {
300 if ((p->end >= start) && (p->start < end))
303 read_unlock(&resource_lock);
307 if (res->start < p->start)
308 res->start = p->start;
309 if (res->end > p->end)
315 * This function calls callback against all memory range of "System RAM"
316 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
317 * Now, this function is only for "System RAM".
319 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
320 void *arg, int (*func)(unsigned long, unsigned long, void *))
323 unsigned long pfn, end_pfn;
327 res.start = (u64) start_pfn << PAGE_SHIFT;
328 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
329 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
331 while ((res.start < res.end) &&
332 (find_next_system_ram(&res, "System RAM") >= 0)) {
333 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
334 end_pfn = (res.end + 1) >> PAGE_SHIFT;
336 ret = (*func)(pfn, end_pfn - pfn, arg);
339 res.start = res.end + 1;
347 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
352 * This generic page_is_ram() returns true if specified address is
353 * registered as "System RAM" in iomem_resource list.
355 int __weak page_is_ram(unsigned long pfn)
357 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
360 static resource_size_t simple_align_resource(void *data,
361 const struct resource *avail,
362 resource_size_t size,
363 resource_size_t align)
369 * Find empty slot in the resource tree given range and alignment.
371 static int find_resource(struct resource *root, struct resource *new,
372 resource_size_t size, resource_size_t min,
373 resource_size_t max, resource_size_t align,
374 resource_size_t (*alignf)(void *,
375 const struct resource *,
380 struct resource *this = root->child;
381 struct resource tmp = *new;
383 tmp.start = root->start;
385 * Skip past an allocated resource that starts at 0, since the assignment
386 * of this->start - 1 to tmp->end below would cause an underflow.
388 if (this && this->start == 0) {
389 tmp.start = this->end + 1;
390 this = this->sibling;
394 tmp.end = this->start - 1;
401 tmp.start = ALIGN(tmp.start, align);
403 tmp.start = alignf(alignf_data, &tmp, size, align);
404 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
405 new->start = tmp.start;
406 new->end = tmp.start + size - 1;
411 tmp.start = this->end + 1;
412 this = this->sibling;
418 * allocate_resource - allocate empty slot in the resource tree given range & alignment
419 * @root: root resource descriptor
420 * @new: resource descriptor desired by caller
421 * @size: requested resource region size
422 * @min: minimum size to allocate
423 * @max: maximum size to allocate
424 * @align: alignment requested, in bytes
425 * @alignf: alignment function, optional, called if not NULL
426 * @alignf_data: arbitrary data to pass to the @alignf function
428 int allocate_resource(struct resource *root, struct resource *new,
429 resource_size_t size, resource_size_t min,
430 resource_size_t max, resource_size_t align,
431 resource_size_t (*alignf)(void *,
432 const struct resource *,
440 alignf = simple_align_resource;
442 write_lock(&resource_lock);
443 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
444 if (err >= 0 && __request_resource(root, new))
446 write_unlock(&resource_lock);
450 EXPORT_SYMBOL(allocate_resource);
453 * Insert a resource into the resource tree. If successful, return NULL,
454 * otherwise return the conflicting resource (compare to __request_resource())
456 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
458 struct resource *first, *next;
460 for (;; parent = first) {
461 first = __request_resource(parent, new);
468 if ((first->start > new->start) || (first->end < new->end))
470 if ((first->start == new->start) && (first->end == new->end))
474 for (next = first; ; next = next->sibling) {
475 /* Partial overlap? Bad, and unfixable */
476 if (next->start < new->start || next->end > new->end)
480 if (next->sibling->start > new->end)
484 new->parent = parent;
485 new->sibling = next->sibling;
488 next->sibling = NULL;
489 for (next = first; next; next = next->sibling)
492 if (parent->child == first) {
495 next = parent->child;
496 while (next->sibling != first)
497 next = next->sibling;
504 * insert_resource_conflict - Inserts resource in the resource tree
505 * @parent: parent of the new resource
506 * @new: new resource to insert
508 * Returns 0 on success, conflict resource if the resource can't be inserted.
510 * This function is equivalent to request_resource_conflict when no conflict
511 * happens. If a conflict happens, and the conflicting resources
512 * entirely fit within the range of the new resource, then the new
513 * resource is inserted and the conflicting resources become children of
516 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
518 struct resource *conflict;
520 write_lock(&resource_lock);
521 conflict = __insert_resource(parent, new);
522 write_unlock(&resource_lock);
527 * insert_resource - Inserts a resource in the resource tree
528 * @parent: parent of the new resource
529 * @new: new resource to insert
531 * Returns 0 on success, -EBUSY if the resource can't be inserted.
533 int insert_resource(struct resource *parent, struct resource *new)
535 struct resource *conflict;
537 conflict = insert_resource_conflict(parent, new);
538 return conflict ? -EBUSY : 0;
542 * insert_resource_expand_to_fit - Insert a resource into the resource tree
543 * @root: root resource descriptor
544 * @new: new resource to insert
546 * Insert a resource into the resource tree, possibly expanding it in order
547 * to make it encompass any conflicting resources.
549 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
554 write_lock(&resource_lock);
556 struct resource *conflict;
558 conflict = __insert_resource(root, new);
561 if (conflict == root)
564 /* Ok, expand resource to cover the conflict, then try again .. */
565 if (conflict->start < new->start)
566 new->start = conflict->start;
567 if (conflict->end > new->end)
568 new->end = conflict->end;
570 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
572 write_unlock(&resource_lock);
576 * adjust_resource - modify a resource's start and size
577 * @res: resource to modify
578 * @start: new start value
581 * Given an existing resource, change its start and size to match the
582 * arguments. Returns 0 on success, -EBUSY if it can't fit.
583 * Existing children of the resource are assumed to be immutable.
585 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
587 struct resource *tmp, *parent = res->parent;
588 resource_size_t end = start + size - 1;
591 write_lock(&resource_lock);
593 if ((start < parent->start) || (end > parent->end))
596 for (tmp = res->child; tmp; tmp = tmp->sibling) {
597 if ((tmp->start < start) || (tmp->end > end))
601 if (res->sibling && (res->sibling->start <= end))
606 while (tmp->sibling != res)
608 if (start <= tmp->end)
617 write_unlock(&resource_lock);
621 static void __init __reserve_region_with_split(struct resource *root,
622 resource_size_t start, resource_size_t end,
625 struct resource *parent = root;
626 struct resource *conflict;
627 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
635 res->flags = IORESOURCE_BUSY;
637 conflict = __request_resource(parent, res);
641 /* failed, split and try again */
644 /* conflict covered whole area */
645 if (conflict->start <= start && conflict->end >= end)
648 if (conflict->start > start)
649 __reserve_region_with_split(root, start, conflict->start-1, name);
650 if (conflict->end < end)
651 __reserve_region_with_split(root, conflict->end+1, end, name);
654 void __init reserve_region_with_split(struct resource *root,
655 resource_size_t start, resource_size_t end,
658 write_lock(&resource_lock);
659 __reserve_region_with_split(root, start, end, name);
660 write_unlock(&resource_lock);
663 EXPORT_SYMBOL(adjust_resource);
666 * resource_alignment - calculate resource's alignment
667 * @res: resource pointer
669 * Returns alignment on success, 0 (invalid alignment) on failure.
671 resource_size_t resource_alignment(struct resource *res)
673 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
674 case IORESOURCE_SIZEALIGN:
675 return resource_size(res);
676 case IORESOURCE_STARTALIGN:
684 * This is compatibility stuff for IO resources.
686 * Note how this, unlike the above, knows about
687 * the IO flag meanings (busy etc).
689 * request_region creates a new busy region.
691 * check_region returns non-zero if the area is already busy.
693 * release_region releases a matching busy region.
696 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
699 * __request_region - create a new busy resource region
700 * @parent: parent resource descriptor
701 * @start: resource start address
702 * @n: resource region size
703 * @name: reserving caller's ID string
704 * @flags: IO resource flags
706 struct resource * __request_region(struct resource *parent,
707 resource_size_t start, resource_size_t n,
708 const char *name, int flags)
710 DECLARE_WAITQUEUE(wait, current);
711 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
718 res->end = start + n - 1;
719 res->flags = IORESOURCE_BUSY;
722 write_lock(&resource_lock);
725 struct resource *conflict;
727 conflict = __request_resource(parent, res);
730 if (conflict != parent) {
732 if (!(conflict->flags & IORESOURCE_BUSY))
735 if (conflict->flags & flags & IORESOURCE_MUXED) {
736 add_wait_queue(&muxed_resource_wait, &wait);
737 write_unlock(&resource_lock);
738 set_current_state(TASK_UNINTERRUPTIBLE);
740 remove_wait_queue(&muxed_resource_wait, &wait);
741 write_lock(&resource_lock);
744 /* Uhhuh, that didn't work out.. */
749 write_unlock(&resource_lock);
752 EXPORT_SYMBOL(__request_region);
755 * __check_region - check if a resource region is busy or free
756 * @parent: parent resource descriptor
757 * @start: resource start address
758 * @n: resource region size
760 * Returns 0 if the region is free at the moment it is checked,
761 * returns %-EBUSY if the region is busy.
764 * This function is deprecated because its use is racy.
765 * Even if it returns 0, a subsequent call to request_region()
766 * may fail because another driver etc. just allocated the region.
767 * Do NOT use it. It will be removed from the kernel.
769 int __check_region(struct resource *parent, resource_size_t start,
772 struct resource * res;
774 res = __request_region(parent, start, n, "check-region", 0);
778 release_resource(res);
782 EXPORT_SYMBOL(__check_region);
785 * __release_region - release a previously reserved resource region
786 * @parent: parent resource descriptor
787 * @start: resource start address
788 * @n: resource region size
790 * The described resource region must match a currently busy region.
792 void __release_region(struct resource *parent, resource_size_t start,
801 write_lock(&resource_lock);
804 struct resource *res = *p;
808 if (res->start <= start && res->end >= end) {
809 if (!(res->flags & IORESOURCE_BUSY)) {
813 if (res->start != start || res->end != end)
816 write_unlock(&resource_lock);
817 if (res->flags & IORESOURCE_MUXED)
818 wake_up(&muxed_resource_wait);
825 write_unlock(&resource_lock);
827 printk(KERN_WARNING "Trying to free nonexistent resource "
828 "<%016llx-%016llx>\n", (unsigned long long)start,
829 (unsigned long long)end);
831 EXPORT_SYMBOL(__release_region);
834 * Managed region resource
836 struct region_devres {
837 struct resource *parent;
838 resource_size_t start;
842 static void devm_region_release(struct device *dev, void *res)
844 struct region_devres *this = res;
846 __release_region(this->parent, this->start, this->n);
849 static int devm_region_match(struct device *dev, void *res, void *match_data)
851 struct region_devres *this = res, *match = match_data;
853 return this->parent == match->parent &&
854 this->start == match->start && this->n == match->n;
857 struct resource * __devm_request_region(struct device *dev,
858 struct resource *parent, resource_size_t start,
859 resource_size_t n, const char *name)
861 struct region_devres *dr = NULL;
862 struct resource *res;
864 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
873 res = __request_region(parent, start, n, name, 0);
881 EXPORT_SYMBOL(__devm_request_region);
883 void __devm_release_region(struct device *dev, struct resource *parent,
884 resource_size_t start, resource_size_t n)
886 struct region_devres match_data = { parent, start, n };
888 __release_region(parent, start, n);
889 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
892 EXPORT_SYMBOL(__devm_release_region);
895 * Called from init/main.c to reserve IO ports.
898 static int __init reserve_setup(char *str)
901 static struct resource reserve[MAXRESERVE];
904 unsigned int io_start, io_num;
907 if (get_option (&str, &io_start) != 2)
909 if (get_option (&str, &io_num) == 0)
911 if (x < MAXRESERVE) {
912 struct resource *res = reserve + x;
913 res->name = "reserved";
914 res->start = io_start;
915 res->end = io_start + io_num - 1;
916 res->flags = IORESOURCE_BUSY;
918 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
925 __setup("reserve=", reserve_setup);
928 * Check if the requested addr and size spans more than any slot in the
929 * iomem resource tree.
931 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
933 struct resource *p = &iomem_resource;
937 read_lock(&resource_lock);
938 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
940 * We can probably skip the resources without
941 * IORESOURCE_IO attribute?
943 if (p->start >= addr + size)
947 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
948 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
951 * if a resource is "BUSY", it's not a hardware resource
952 * but a driver mapping of such a resource; we don't want
953 * to warn for those; some drivers legitimately map only
954 * partial hardware resources. (example: vesafb)
956 if (p->flags & IORESOURCE_BUSY)
959 printk(KERN_WARNING "resource map sanity check conflict: "
960 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
961 (unsigned long long)addr,
962 (unsigned long long)(addr + size - 1),
963 (unsigned long long)p->start,
964 (unsigned long long)p->end,
969 read_unlock(&resource_lock);
974 #ifdef CONFIG_STRICT_DEVMEM
975 static int strict_iomem_checks = 1;
977 static int strict_iomem_checks;
981 * check if an address is reserved in the iomem resource tree
982 * returns 1 if reserved, 0 if not reserved.
984 int iomem_is_exclusive(u64 addr)
986 struct resource *p = &iomem_resource;
989 int size = PAGE_SIZE;
991 if (!strict_iomem_checks)
994 addr = addr & PAGE_MASK;
996 read_lock(&resource_lock);
997 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
999 * We can probably skip the resources without
1000 * IORESOURCE_IO attribute?
1002 if (p->start >= addr + size)
1006 if (p->flags & IORESOURCE_BUSY &&
1007 p->flags & IORESOURCE_EXCLUSIVE) {
1012 read_unlock(&resource_lock);
1017 static int __init strict_iomem(char *str)
1019 if (strstr(str, "relaxed"))
1020 strict_iomem_checks = 0;
1021 if (strstr(str, "strict"))
1022 strict_iomem_checks = 1;
1026 __setup("iomem=", strict_iomem);