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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/export.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/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/device.h>
23 #include <linux/pfn.h>
25 #include <linux/resource_ext.h>
29 struct resource ioport_resource = {
32 .end = IO_SPACE_LIMIT,
33 .flags = IORESOURCE_IO,
35 EXPORT_SYMBOL(ioport_resource);
37 struct resource iomem_resource = {
41 .flags = IORESOURCE_MEM,
43 EXPORT_SYMBOL(iomem_resource);
45 /* constraints to be met while allocating resources */
46 struct resource_constraint {
47 resource_size_t min, max, align;
48 resource_size_t (*alignf)(void *, const struct resource *,
49 resource_size_t, resource_size_t);
53 static DEFINE_RWLOCK(resource_lock);
56 * For memory hotplug, there is no way to free resource entries allocated
57 * by boot mem after the system is up. So for reusing the resource entry
58 * we need to remember the resource.
60 static struct resource *bootmem_resource_free;
61 static DEFINE_SPINLOCK(bootmem_resource_lock);
63 static struct resource *next_resource(struct resource *p, bool sibling_only)
65 /* Caller wants to traverse through siblings only */
71 while (!p->sibling && p->parent)
76 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
78 struct resource *p = v;
80 return (void *)next_resource(p, false);
85 enum { MAX_IORES_LEVEL = 5 };
87 static void *r_start(struct seq_file *m, loff_t *pos)
88 __acquires(resource_lock)
90 struct resource *p = PDE_DATA(file_inode(m->file));
92 read_lock(&resource_lock);
93 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
98 static void r_stop(struct seq_file *m, void *v)
99 __releases(resource_lock)
101 read_unlock(&resource_lock);
104 static int r_show(struct seq_file *m, void *v)
106 struct resource *root = PDE_DATA(file_inode(m->file));
107 struct resource *r = v, *p;
108 unsigned long long start, end;
109 int width = root->end < 0x10000 ? 4 : 8;
112 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
113 if (p->parent == root)
116 if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
123 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
127 r->name ? r->name : "<BAD>");
131 static const struct seq_operations resource_op = {
138 static int __init ioresources_init(void)
140 proc_create_seq_data("ioports", 0, NULL, &resource_op,
142 proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
145 __initcall(ioresources_init);
147 #endif /* CONFIG_PROC_FS */
149 static void free_resource(struct resource *res)
154 if (!PageSlab(virt_to_head_page(res))) {
155 spin_lock(&bootmem_resource_lock);
156 res->sibling = bootmem_resource_free;
157 bootmem_resource_free = res;
158 spin_unlock(&bootmem_resource_lock);
164 static struct resource *alloc_resource(gfp_t flags)
166 struct resource *res = NULL;
168 spin_lock(&bootmem_resource_lock);
169 if (bootmem_resource_free) {
170 res = bootmem_resource_free;
171 bootmem_resource_free = res->sibling;
173 spin_unlock(&bootmem_resource_lock);
176 memset(res, 0, sizeof(struct resource));
178 res = kzalloc(sizeof(struct resource), flags);
183 /* Return the conflict entry if you can't request it */
184 static struct resource * __request_resource(struct resource *root, struct resource *new)
186 resource_size_t start = new->start;
187 resource_size_t end = new->end;
188 struct resource *tmp, **p;
192 if (start < root->start)
199 if (!tmp || tmp->start > end) {
206 if (tmp->end < start)
212 static int __release_resource(struct resource *old, bool release_child)
214 struct resource *tmp, **p, *chd;
216 p = &old->parent->child;
222 if (release_child || !(tmp->child)) {
225 for (chd = tmp->child;; chd = chd->sibling) {
226 chd->parent = tmp->parent;
231 chd->sibling = tmp->sibling;
241 static void __release_child_resources(struct resource *r)
243 struct resource *tmp, *p;
244 resource_size_t size;
254 __release_child_resources(tmp);
256 printk(KERN_DEBUG "release child resource %pR\n", tmp);
257 /* need to restore size, and keep flags */
258 size = resource_size(tmp);
264 void release_child_resources(struct resource *r)
266 write_lock(&resource_lock);
267 __release_child_resources(r);
268 write_unlock(&resource_lock);
272 * request_resource_conflict - request and reserve an I/O or memory resource
273 * @root: root resource descriptor
274 * @new: resource descriptor desired by caller
276 * Returns 0 for success, conflict resource on error.
278 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
280 struct resource *conflict;
282 write_lock(&resource_lock);
283 conflict = __request_resource(root, new);
284 write_unlock(&resource_lock);
289 * request_resource - request and reserve an I/O or memory resource
290 * @root: root resource descriptor
291 * @new: resource descriptor desired by caller
293 * Returns 0 for success, negative error code on error.
295 int request_resource(struct resource *root, struct resource *new)
297 struct resource *conflict;
299 conflict = request_resource_conflict(root, new);
300 return conflict ? -EBUSY : 0;
303 EXPORT_SYMBOL(request_resource);
306 * release_resource - release a previously reserved resource
307 * @old: resource pointer
309 int release_resource(struct resource *old)
313 write_lock(&resource_lock);
314 retval = __release_resource(old, true);
315 write_unlock(&resource_lock);
319 EXPORT_SYMBOL(release_resource);
322 * Finds the lowest iomem resource that covers part of [start..end]. The
323 * caller must specify start, end, flags, and desc (which may be
326 * If a resource is found, returns 0 and *res is overwritten with the part
327 * of the resource that's within [start..end]; if none is found, returns
328 * -ENODEV. Returns -EINVAL for invalid parameters.
330 * This function walks the whole tree and not just first level children
331 * unless @first_level_children_only is true.
333 static int find_next_iomem_res(resource_size_t start, resource_size_t end,
334 unsigned long flags, unsigned long desc,
335 bool first_level_children_only,
336 struct resource *res)
339 bool sibling_only = false;
342 BUG_ON(start >= end);
344 if (first_level_children_only)
347 read_lock(&resource_lock);
349 for (p = iomem_resource.child; p; p = next_resource(p, sibling_only)) {
350 if ((p->flags & flags) != flags)
352 if ((desc != IORES_DESC_NONE) && (desc != p->desc))
354 if (p->start > end) {
358 if ((p->end >= start) && (p->start <= end))
364 res->start = max(start, p->start);
365 res->end = min(end, p->end);
366 res->flags = p->flags;
370 read_unlock(&resource_lock);
371 return p ? 0 : -ENODEV;
374 static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
375 unsigned long flags, unsigned long desc,
376 bool first_level_children_only, void *arg,
377 int (*func)(struct resource *, void *))
382 while (start < end &&
383 !find_next_iomem_res(start, end, flags, desc,
384 first_level_children_only, &res)) {
385 ret = (*func)(&res, arg);
396 * Walks through iomem resources and calls func() with matching resource
397 * ranges. This walks through whole tree and not just first level children.
398 * All the memory ranges which overlap start,end and also match flags and
399 * desc are valid candidates.
401 * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
402 * @flags: I/O resource flags
406 * NOTE: For a new descriptor search, define a new IORES_DESC in
407 * <linux/ioport.h> and set it in 'desc' of a target resource entry.
409 int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
410 u64 end, void *arg, int (*func)(struct resource *, void *))
412 return __walk_iomem_res_desc(start, end, flags, desc, false, arg, func);
414 EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
417 * This function calls the @func callback against all memory ranges of type
418 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
419 * Now, this function is only for System RAM, it deals with full ranges and
420 * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
423 int walk_system_ram_res(u64 start, u64 end, void *arg,
424 int (*func)(struct resource *, void *))
426 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
428 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true,
433 * This function calls the @func callback against all memory ranges, which
434 * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
436 int walk_mem_res(u64 start, u64 end, void *arg,
437 int (*func)(struct resource *, void *))
439 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
441 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, true,
445 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
448 * This function calls the @func callback against all memory ranges of type
449 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
450 * It is to be used only for System RAM.
452 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
453 void *arg, int (*func)(unsigned long, unsigned long, void *))
455 resource_size_t start, end;
458 unsigned long pfn, end_pfn;
461 start = (u64) start_pfn << PAGE_SHIFT;
462 end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
463 flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
464 while (start < end &&
465 !find_next_iomem_res(start, end, flags, IORES_DESC_NONE,
467 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
468 end_pfn = (res.end + 1) >> PAGE_SHIFT;
470 ret = (*func)(pfn, end_pfn - pfn, arg);
480 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
486 * This generic page_is_ram() returns true if specified address is
487 * registered as System RAM in iomem_resource list.
489 int __weak page_is_ram(unsigned long pfn)
491 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
493 EXPORT_SYMBOL_GPL(page_is_ram);
496 * region_intersects() - determine intersection of region with known resources
497 * @start: region start address
498 * @size: size of region
499 * @flags: flags of resource (in iomem_resource)
500 * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
502 * Check if the specified region partially overlaps or fully eclipses a
503 * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
504 * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
505 * return REGION_MIXED if the region overlaps @flags/@desc and another
506 * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
507 * and no other defined resource. Note that REGION_INTERSECTS is also
508 * returned in the case when the specified region overlaps RAM and undefined
511 * region_intersect() is used by memory remapping functions to ensure
512 * the user is not remapping RAM and is a vast speed up over walking
513 * through the resource table page by page.
515 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
518 resource_size_t end = start + size - 1;
519 int type = 0; int other = 0;
522 read_lock(&resource_lock);
523 for (p = iomem_resource.child; p ; p = p->sibling) {
524 bool is_type = (((p->flags & flags) == flags) &&
525 ((desc == IORES_DESC_NONE) ||
528 if (start >= p->start && start <= p->end)
529 is_type ? type++ : other++;
530 if (end >= p->start && end <= p->end)
531 is_type ? type++ : other++;
532 if (p->start >= start && p->end <= end)
533 is_type ? type++ : other++;
535 read_unlock(&resource_lock);
538 return type ? REGION_INTERSECTS : REGION_DISJOINT;
543 return REGION_DISJOINT;
545 EXPORT_SYMBOL_GPL(region_intersects);
547 void __weak arch_remove_reservations(struct resource *avail)
551 static resource_size_t simple_align_resource(void *data,
552 const struct resource *avail,
553 resource_size_t size,
554 resource_size_t align)
559 static void resource_clip(struct resource *res, resource_size_t min,
562 if (res->start < min)
569 * Find empty slot in the resource tree with the given range and
570 * alignment constraints
572 static int __find_resource(struct resource *root, struct resource *old,
573 struct resource *new,
574 resource_size_t size,
575 struct resource_constraint *constraint)
577 struct resource *this = root->child;
578 struct resource tmp = *new, avail, alloc;
580 tmp.start = root->start;
582 * Skip past an allocated resource that starts at 0, since the assignment
583 * of this->start - 1 to tmp->end below would cause an underflow.
585 if (this && this->start == root->start) {
586 tmp.start = (this == old) ? old->start : this->end + 1;
587 this = this->sibling;
591 tmp.end = (this == old) ? this->end : this->start - 1;
595 if (tmp.end < tmp.start)
598 resource_clip(&tmp, constraint->min, constraint->max);
599 arch_remove_reservations(&tmp);
601 /* Check for overflow after ALIGN() */
602 avail.start = ALIGN(tmp.start, constraint->align);
604 avail.flags = new->flags & ~IORESOURCE_UNSET;
605 if (avail.start >= tmp.start) {
606 alloc.flags = avail.flags;
607 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
608 size, constraint->align);
609 alloc.end = alloc.start + size - 1;
610 if (alloc.start <= alloc.end &&
611 resource_contains(&avail, &alloc)) {
612 new->start = alloc.start;
613 new->end = alloc.end;
618 next: if (!this || this->end == root->end)
622 tmp.start = this->end + 1;
623 this = this->sibling;
629 * Find empty slot in the resource tree given range and alignment.
631 static int find_resource(struct resource *root, struct resource *new,
632 resource_size_t size,
633 struct resource_constraint *constraint)
635 return __find_resource(root, NULL, new, size, constraint);
639 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
640 * The resource will be relocated if the new size cannot be reallocated in the
643 * @root: root resource descriptor
644 * @old: resource descriptor desired by caller
645 * @newsize: new size of the resource descriptor
646 * @constraint: the size and alignment constraints to be met.
648 static int reallocate_resource(struct resource *root, struct resource *old,
649 resource_size_t newsize,
650 struct resource_constraint *constraint)
653 struct resource new = *old;
654 struct resource *conflict;
656 write_lock(&resource_lock);
658 if ((err = __find_resource(root, old, &new, newsize, constraint)))
661 if (resource_contains(&new, old)) {
662 old->start = new.start;
672 if (resource_contains(old, &new)) {
673 old->start = new.start;
676 __release_resource(old, true);
678 conflict = __request_resource(root, old);
682 write_unlock(&resource_lock);
688 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
689 * The resource will be reallocated with a new size if it was already allocated
690 * @root: root resource descriptor
691 * @new: resource descriptor desired by caller
692 * @size: requested resource region size
693 * @min: minimum boundary to allocate
694 * @max: maximum boundary to allocate
695 * @align: alignment requested, in bytes
696 * @alignf: alignment function, optional, called if not NULL
697 * @alignf_data: arbitrary data to pass to the @alignf function
699 int allocate_resource(struct resource *root, struct resource *new,
700 resource_size_t size, resource_size_t min,
701 resource_size_t max, resource_size_t align,
702 resource_size_t (*alignf)(void *,
703 const struct resource *,
709 struct resource_constraint constraint;
712 alignf = simple_align_resource;
714 constraint.min = min;
715 constraint.max = max;
716 constraint.align = align;
717 constraint.alignf = alignf;
718 constraint.alignf_data = alignf_data;
721 /* resource is already allocated, try reallocating with
722 the new constraints */
723 return reallocate_resource(root, new, size, &constraint);
726 write_lock(&resource_lock);
727 err = find_resource(root, new, size, &constraint);
728 if (err >= 0 && __request_resource(root, new))
730 write_unlock(&resource_lock);
734 EXPORT_SYMBOL(allocate_resource);
737 * lookup_resource - find an existing resource by a resource start address
738 * @root: root resource descriptor
739 * @start: resource start address
741 * Returns a pointer to the resource if found, NULL otherwise
743 struct resource *lookup_resource(struct resource *root, resource_size_t start)
745 struct resource *res;
747 read_lock(&resource_lock);
748 for (res = root->child; res; res = res->sibling) {
749 if (res->start == start)
752 read_unlock(&resource_lock);
758 * Insert a resource into the resource tree. If successful, return NULL,
759 * otherwise return the conflicting resource (compare to __request_resource())
761 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
763 struct resource *first, *next;
765 for (;; parent = first) {
766 first = __request_resource(parent, new);
772 if (WARN_ON(first == new)) /* duplicated insertion */
775 if ((first->start > new->start) || (first->end < new->end))
777 if ((first->start == new->start) && (first->end == new->end))
781 for (next = first; ; next = next->sibling) {
782 /* Partial overlap? Bad, and unfixable */
783 if (next->start < new->start || next->end > new->end)
787 if (next->sibling->start > new->end)
791 new->parent = parent;
792 new->sibling = next->sibling;
795 next->sibling = NULL;
796 for (next = first; next; next = next->sibling)
799 if (parent->child == first) {
802 next = parent->child;
803 while (next->sibling != first)
804 next = next->sibling;
811 * insert_resource_conflict - Inserts resource in the resource tree
812 * @parent: parent of the new resource
813 * @new: new resource to insert
815 * Returns 0 on success, conflict resource if the resource can't be inserted.
817 * This function is equivalent to request_resource_conflict when no conflict
818 * happens. If a conflict happens, and the conflicting resources
819 * entirely fit within the range of the new resource, then the new
820 * resource is inserted and the conflicting resources become children of
823 * This function is intended for producers of resources, such as FW modules
826 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
828 struct resource *conflict;
830 write_lock(&resource_lock);
831 conflict = __insert_resource(parent, new);
832 write_unlock(&resource_lock);
837 * insert_resource - Inserts a resource in the resource tree
838 * @parent: parent of the new resource
839 * @new: new resource to insert
841 * Returns 0 on success, -EBUSY if the resource can't be inserted.
843 * This function is intended for producers of resources, such as FW modules
846 int insert_resource(struct resource *parent, struct resource *new)
848 struct resource *conflict;
850 conflict = insert_resource_conflict(parent, new);
851 return conflict ? -EBUSY : 0;
853 EXPORT_SYMBOL_GPL(insert_resource);
856 * insert_resource_expand_to_fit - Insert a resource into the resource tree
857 * @root: root resource descriptor
858 * @new: new resource to insert
860 * Insert a resource into the resource tree, possibly expanding it in order
861 * to make it encompass any conflicting resources.
863 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
868 write_lock(&resource_lock);
870 struct resource *conflict;
872 conflict = __insert_resource(root, new);
875 if (conflict == root)
878 /* Ok, expand resource to cover the conflict, then try again .. */
879 if (conflict->start < new->start)
880 new->start = conflict->start;
881 if (conflict->end > new->end)
882 new->end = conflict->end;
884 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
886 write_unlock(&resource_lock);
890 * remove_resource - Remove a resource in the resource tree
891 * @old: resource to remove
893 * Returns 0 on success, -EINVAL if the resource is not valid.
895 * This function removes a resource previously inserted by insert_resource()
896 * or insert_resource_conflict(), and moves the children (if any) up to
897 * where they were before. insert_resource() and insert_resource_conflict()
898 * insert a new resource, and move any conflicting resources down to the
899 * children of the new resource.
901 * insert_resource(), insert_resource_conflict() and remove_resource() are
902 * intended for producers of resources, such as FW modules and bus drivers.
904 int remove_resource(struct resource *old)
908 write_lock(&resource_lock);
909 retval = __release_resource(old, false);
910 write_unlock(&resource_lock);
913 EXPORT_SYMBOL_GPL(remove_resource);
915 static int __adjust_resource(struct resource *res, resource_size_t start,
916 resource_size_t size)
918 struct resource *tmp, *parent = res->parent;
919 resource_size_t end = start + size - 1;
925 if ((start < parent->start) || (end > parent->end))
928 if (res->sibling && (res->sibling->start <= end))
933 while (tmp->sibling != res)
935 if (start <= tmp->end)
940 for (tmp = res->child; tmp; tmp = tmp->sibling)
941 if ((tmp->start < start) || (tmp->end > end))
953 * adjust_resource - modify a resource's start and size
954 * @res: resource to modify
955 * @start: new start value
958 * Given an existing resource, change its start and size to match the
959 * arguments. Returns 0 on success, -EBUSY if it can't fit.
960 * Existing children of the resource are assumed to be immutable.
962 int adjust_resource(struct resource *res, resource_size_t start,
963 resource_size_t size)
967 write_lock(&resource_lock);
968 result = __adjust_resource(res, start, size);
969 write_unlock(&resource_lock);
972 EXPORT_SYMBOL(adjust_resource);
974 static void __init __reserve_region_with_split(struct resource *root,
975 resource_size_t start, resource_size_t end,
978 struct resource *parent = root;
979 struct resource *conflict;
980 struct resource *res = alloc_resource(GFP_ATOMIC);
981 struct resource *next_res = NULL;
982 int type = resource_type(root);
990 res->flags = type | IORESOURCE_BUSY;
991 res->desc = IORES_DESC_NONE;
995 conflict = __request_resource(parent, res);
1004 /* conflict covered whole area */
1005 if (conflict->start <= res->start &&
1006 conflict->end >= res->end) {
1012 /* failed, split and try again */
1013 if (conflict->start > res->start) {
1015 res->end = conflict->start - 1;
1016 if (conflict->end < end) {
1017 next_res = alloc_resource(GFP_ATOMIC);
1022 next_res->name = name;
1023 next_res->start = conflict->end + 1;
1024 next_res->end = end;
1025 next_res->flags = type | IORESOURCE_BUSY;
1026 next_res->desc = IORES_DESC_NONE;
1029 res->start = conflict->end + 1;
1035 void __init reserve_region_with_split(struct resource *root,
1036 resource_size_t start, resource_size_t end,
1041 write_lock(&resource_lock);
1042 if (root->start > start || root->end < end) {
1043 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1044 (unsigned long long)start, (unsigned long long)end,
1046 if (start > root->end || end < root->start)
1049 if (end > root->end)
1051 if (start < root->start)
1052 start = root->start;
1053 pr_err("fixing request to [0x%llx-0x%llx]\n",
1054 (unsigned long long)start,
1055 (unsigned long long)end);
1060 __reserve_region_with_split(root, start, end, name);
1061 write_unlock(&resource_lock);
1065 * resource_alignment - calculate resource's alignment
1066 * @res: resource pointer
1068 * Returns alignment on success, 0 (invalid alignment) on failure.
1070 resource_size_t resource_alignment(struct resource *res)
1072 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1073 case IORESOURCE_SIZEALIGN:
1074 return resource_size(res);
1075 case IORESOURCE_STARTALIGN:
1083 * This is compatibility stuff for IO resources.
1085 * Note how this, unlike the above, knows about
1086 * the IO flag meanings (busy etc).
1088 * request_region creates a new busy region.
1090 * release_region releases a matching busy region.
1093 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1096 * __request_region - create a new busy resource region
1097 * @parent: parent resource descriptor
1098 * @start: resource start address
1099 * @n: resource region size
1100 * @name: reserving caller's ID string
1101 * @flags: IO resource flags
1103 struct resource * __request_region(struct resource *parent,
1104 resource_size_t start, resource_size_t n,
1105 const char *name, int flags)
1107 DECLARE_WAITQUEUE(wait, current);
1108 struct resource *res = alloc_resource(GFP_KERNEL);
1115 res->end = start + n - 1;
1117 write_lock(&resource_lock);
1120 struct resource *conflict;
1122 res->flags = resource_type(parent) | resource_ext_type(parent);
1123 res->flags |= IORESOURCE_BUSY | flags;
1124 res->desc = parent->desc;
1126 conflict = __request_resource(parent, res);
1129 if (conflict != parent) {
1130 if (!(conflict->flags & IORESOURCE_BUSY)) {
1135 if (conflict->flags & flags & IORESOURCE_MUXED) {
1136 add_wait_queue(&muxed_resource_wait, &wait);
1137 write_unlock(&resource_lock);
1138 set_current_state(TASK_UNINTERRUPTIBLE);
1140 remove_wait_queue(&muxed_resource_wait, &wait);
1141 write_lock(&resource_lock);
1144 /* Uhhuh, that didn't work out.. */
1149 write_unlock(&resource_lock);
1152 EXPORT_SYMBOL(__request_region);
1155 * __release_region - release a previously reserved resource region
1156 * @parent: parent resource descriptor
1157 * @start: resource start address
1158 * @n: resource region size
1160 * The described resource region must match a currently busy region.
1162 void __release_region(struct resource *parent, resource_size_t start,
1165 struct resource **p;
1166 resource_size_t end;
1169 end = start + n - 1;
1171 write_lock(&resource_lock);
1174 struct resource *res = *p;
1178 if (res->start <= start && res->end >= end) {
1179 if (!(res->flags & IORESOURCE_BUSY)) {
1183 if (res->start != start || res->end != end)
1186 write_unlock(&resource_lock);
1187 if (res->flags & IORESOURCE_MUXED)
1188 wake_up(&muxed_resource_wait);
1195 write_unlock(&resource_lock);
1197 printk(KERN_WARNING "Trying to free nonexistent resource "
1198 "<%016llx-%016llx>\n", (unsigned long long)start,
1199 (unsigned long long)end);
1201 EXPORT_SYMBOL(__release_region);
1203 #ifdef CONFIG_MEMORY_HOTREMOVE
1205 * release_mem_region_adjustable - release a previously reserved memory region
1206 * @parent: parent resource descriptor
1207 * @start: resource start address
1208 * @size: resource region size
1210 * This interface is intended for memory hot-delete. The requested region
1211 * is released from a currently busy memory resource. The requested region
1212 * must either match exactly or fit into a single busy resource entry. In
1213 * the latter case, the remaining resource is adjusted accordingly.
1214 * Existing children of the busy memory resource must be immutable in the
1218 * - Additional release conditions, such as overlapping region, can be
1219 * supported after they are confirmed as valid cases.
1220 * - When a busy memory resource gets split into two entries, the code
1221 * assumes that all children remain in the lower address entry for
1222 * simplicity. Enhance this logic when necessary.
1224 int release_mem_region_adjustable(struct resource *parent,
1225 resource_size_t start, resource_size_t size)
1227 struct resource **p;
1228 struct resource *res;
1229 struct resource *new_res;
1230 resource_size_t end;
1233 end = start + size - 1;
1234 if ((start < parent->start) || (end > parent->end))
1237 /* The alloc_resource() result gets checked later */
1238 new_res = alloc_resource(GFP_KERNEL);
1241 write_lock(&resource_lock);
1243 while ((res = *p)) {
1244 if (res->start >= end)
1247 /* look for the next resource if it does not fit into */
1248 if (res->start > start || res->end < end) {
1253 if (!(res->flags & IORESOURCE_MEM))
1256 if (!(res->flags & IORESOURCE_BUSY)) {
1261 /* found the target resource; let's adjust accordingly */
1262 if (res->start == start && res->end == end) {
1263 /* free the whole entry */
1267 } else if (res->start == start && res->end != end) {
1268 /* adjust the start */
1269 ret = __adjust_resource(res, end + 1,
1271 } else if (res->start != start && res->end == end) {
1272 /* adjust the end */
1273 ret = __adjust_resource(res, res->start,
1274 start - res->start);
1276 /* split into two entries */
1281 new_res->name = res->name;
1282 new_res->start = end + 1;
1283 new_res->end = res->end;
1284 new_res->flags = res->flags;
1285 new_res->desc = res->desc;
1286 new_res->parent = res->parent;
1287 new_res->sibling = res->sibling;
1288 new_res->child = NULL;
1290 ret = __adjust_resource(res, res->start,
1291 start - res->start);
1294 res->sibling = new_res;
1301 write_unlock(&resource_lock);
1302 free_resource(new_res);
1305 #endif /* CONFIG_MEMORY_HOTREMOVE */
1308 * Managed region resource
1310 static void devm_resource_release(struct device *dev, void *ptr)
1312 struct resource **r = ptr;
1314 release_resource(*r);
1318 * devm_request_resource() - request and reserve an I/O or memory resource
1319 * @dev: device for which to request the resource
1320 * @root: root of the resource tree from which to request the resource
1321 * @new: descriptor of the resource to request
1323 * This is a device-managed version of request_resource(). There is usually
1324 * no need to release resources requested by this function explicitly since
1325 * that will be taken care of when the device is unbound from its driver.
1326 * If for some reason the resource needs to be released explicitly, because
1327 * of ordering issues for example, drivers must call devm_release_resource()
1328 * rather than the regular release_resource().
1330 * When a conflict is detected between any existing resources and the newly
1331 * requested resource, an error message will be printed.
1333 * Returns 0 on success or a negative error code on failure.
1335 int devm_request_resource(struct device *dev, struct resource *root,
1336 struct resource *new)
1338 struct resource *conflict, **ptr;
1340 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1346 conflict = request_resource_conflict(root, new);
1348 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1349 new, conflict->name, conflict);
1354 devres_add(dev, ptr);
1357 EXPORT_SYMBOL(devm_request_resource);
1359 static int devm_resource_match(struct device *dev, void *res, void *data)
1361 struct resource **ptr = res;
1363 return *ptr == data;
1367 * devm_release_resource() - release a previously requested resource
1368 * @dev: device for which to release the resource
1369 * @new: descriptor of the resource to release
1371 * Releases a resource previously requested using devm_request_resource().
1373 void devm_release_resource(struct device *dev, struct resource *new)
1375 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1378 EXPORT_SYMBOL(devm_release_resource);
1380 struct region_devres {
1381 struct resource *parent;
1382 resource_size_t start;
1386 static void devm_region_release(struct device *dev, void *res)
1388 struct region_devres *this = res;
1390 __release_region(this->parent, this->start, this->n);
1393 static int devm_region_match(struct device *dev, void *res, void *match_data)
1395 struct region_devres *this = res, *match = match_data;
1397 return this->parent == match->parent &&
1398 this->start == match->start && this->n == match->n;
1401 struct resource * __devm_request_region(struct device *dev,
1402 struct resource *parent, resource_size_t start,
1403 resource_size_t n, const char *name)
1405 struct region_devres *dr = NULL;
1406 struct resource *res;
1408 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1413 dr->parent = parent;
1417 res = __request_region(parent, start, n, name, 0);
1419 devres_add(dev, dr);
1425 EXPORT_SYMBOL(__devm_request_region);
1427 void __devm_release_region(struct device *dev, struct resource *parent,
1428 resource_size_t start, resource_size_t n)
1430 struct region_devres match_data = { parent, start, n };
1432 __release_region(parent, start, n);
1433 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1436 EXPORT_SYMBOL(__devm_release_region);
1439 * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1441 #define MAXRESERVE 4
1442 static int __init reserve_setup(char *str)
1444 static int reserved;
1445 static struct resource reserve[MAXRESERVE];
1448 unsigned int io_start, io_num;
1450 struct resource *parent;
1452 if (get_option(&str, &io_start) != 2)
1454 if (get_option(&str, &io_num) == 0)
1456 if (x < MAXRESERVE) {
1457 struct resource *res = reserve + x;
1460 * If the region starts below 0x10000, we assume it's
1461 * I/O port space; otherwise assume it's memory.
1463 if (io_start < 0x10000) {
1464 res->flags = IORESOURCE_IO;
1465 parent = &ioport_resource;
1467 res->flags = IORESOURCE_MEM;
1468 parent = &iomem_resource;
1470 res->name = "reserved";
1471 res->start = io_start;
1472 res->end = io_start + io_num - 1;
1473 res->flags |= IORESOURCE_BUSY;
1474 res->desc = IORES_DESC_NONE;
1476 if (request_resource(parent, res) == 0)
1482 __setup("reserve=", reserve_setup);
1485 * Check if the requested addr and size spans more than any slot in the
1486 * iomem resource tree.
1488 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1490 struct resource *p = &iomem_resource;
1494 read_lock(&resource_lock);
1495 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1497 * We can probably skip the resources without
1498 * IORESOURCE_IO attribute?
1500 if (p->start >= addr + size)
1504 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1505 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1508 * if a resource is "BUSY", it's not a hardware resource
1509 * but a driver mapping of such a resource; we don't want
1510 * to warn for those; some drivers legitimately map only
1511 * partial hardware resources. (example: vesafb)
1513 if (p->flags & IORESOURCE_BUSY)
1516 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1517 (unsigned long long)addr,
1518 (unsigned long long)(addr + size - 1),
1523 read_unlock(&resource_lock);
1528 #ifdef CONFIG_STRICT_DEVMEM
1529 static int strict_iomem_checks = 1;
1531 static int strict_iomem_checks;
1535 * check if an address is reserved in the iomem resource tree
1536 * returns true if reserved, false if not reserved.
1538 bool iomem_is_exclusive(u64 addr)
1540 struct resource *p = &iomem_resource;
1543 int size = PAGE_SIZE;
1545 if (!strict_iomem_checks)
1548 addr = addr & PAGE_MASK;
1550 read_lock(&resource_lock);
1551 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1553 * We can probably skip the resources without
1554 * IORESOURCE_IO attribute?
1556 if (p->start >= addr + size)
1561 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1562 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1565 if ((p->flags & IORESOURCE_BUSY) == 0)
1567 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1568 || p->flags & IORESOURCE_EXCLUSIVE) {
1573 read_unlock(&resource_lock);
1578 struct resource_entry *resource_list_create_entry(struct resource *res,
1581 struct resource_entry *entry;
1583 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1585 INIT_LIST_HEAD(&entry->node);
1586 entry->res = res ? res : &entry->__res;
1591 EXPORT_SYMBOL(resource_list_create_entry);
1593 void resource_list_free(struct list_head *head)
1595 struct resource_entry *entry, *tmp;
1597 list_for_each_entry_safe(entry, tmp, head, node)
1598 resource_list_destroy_entry(entry);
1600 EXPORT_SYMBOL(resource_list_free);
1602 static int __init strict_iomem(char *str)
1604 if (strstr(str, "relaxed"))
1605 strict_iomem_checks = 0;
1606 if (strstr(str, "strict"))
1607 strict_iomem_checks = 1;
1611 __setup("iomem=", strict_iomem);