1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/kernel/resource.c
5 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
8 * Arbitrary resource management.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/export.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
20 #include <linux/proc_fs.h>
21 #include <linux/pseudo_fs.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/device.h>
25 #include <linux/pfn.h>
27 #include <linux/mount.h>
28 #include <linux/resource_ext.h>
29 #include <uapi/linux/magic.h>
33 struct resource ioport_resource = {
36 .end = IO_SPACE_LIMIT,
37 .flags = IORESOURCE_IO,
39 EXPORT_SYMBOL(ioport_resource);
41 struct resource iomem_resource = {
45 .flags = IORESOURCE_MEM,
47 EXPORT_SYMBOL(iomem_resource);
49 /* constraints to be met while allocating resources */
50 struct resource_constraint {
51 resource_size_t min, max, align;
52 resource_size_t (*alignf)(void *, const struct resource *,
53 resource_size_t, resource_size_t);
57 static DEFINE_RWLOCK(resource_lock);
60 * For memory hotplug, there is no way to free resource entries allocated
61 * by boot mem after the system is up. So for reusing the resource entry
62 * we need to remember the resource.
64 static struct resource *bootmem_resource_free;
65 static DEFINE_SPINLOCK(bootmem_resource_lock);
67 static struct resource *next_resource(struct resource *p)
71 while (!p->sibling && p->parent)
76 static struct resource *next_resource_skip_children(struct resource *p)
78 while (!p->sibling && p->parent)
83 #define for_each_resource(_root, _p, _skip_children) \
84 for ((_p) = (_root)->child; (_p); \
85 (_p) = (_skip_children) ? next_resource_skip_children(_p) : \
88 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
90 struct resource *p = v;
92 return (void *)next_resource(p);
97 enum { MAX_IORES_LEVEL = 5 };
99 static void *r_start(struct seq_file *m, loff_t *pos)
100 __acquires(resource_lock)
102 struct resource *p = PDE_DATA(file_inode(m->file));
104 read_lock(&resource_lock);
105 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
110 static void r_stop(struct seq_file *m, void *v)
111 __releases(resource_lock)
113 read_unlock(&resource_lock);
116 static int r_show(struct seq_file *m, void *v)
118 struct resource *root = PDE_DATA(file_inode(m->file));
119 struct resource *r = v, *p;
120 unsigned long long start, end;
121 int width = root->end < 0x10000 ? 4 : 8;
124 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
125 if (p->parent == root)
128 if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
135 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
139 r->name ? r->name : "<BAD>");
143 static const struct seq_operations resource_op = {
150 static int __init ioresources_init(void)
152 proc_create_seq_data("ioports", 0, NULL, &resource_op,
154 proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
157 __initcall(ioresources_init);
159 #endif /* CONFIG_PROC_FS */
161 static void free_resource(struct resource *res)
166 if (!PageSlab(virt_to_head_page(res))) {
167 spin_lock(&bootmem_resource_lock);
168 res->sibling = bootmem_resource_free;
169 bootmem_resource_free = res;
170 spin_unlock(&bootmem_resource_lock);
176 static struct resource *alloc_resource(gfp_t flags)
178 struct resource *res = NULL;
180 spin_lock(&bootmem_resource_lock);
181 if (bootmem_resource_free) {
182 res = bootmem_resource_free;
183 bootmem_resource_free = res->sibling;
185 spin_unlock(&bootmem_resource_lock);
188 memset(res, 0, sizeof(struct resource));
190 res = kzalloc(sizeof(struct resource), flags);
195 /* Return the conflict entry if you can't request it */
196 static struct resource * __request_resource(struct resource *root, struct resource *new)
198 resource_size_t start = new->start;
199 resource_size_t end = new->end;
200 struct resource *tmp, **p;
204 if (start < root->start)
211 if (!tmp || tmp->start > end) {
218 if (tmp->end < start)
224 static int __release_resource(struct resource *old, bool release_child)
226 struct resource *tmp, **p, *chd;
228 p = &old->parent->child;
234 if (release_child || !(tmp->child)) {
237 for (chd = tmp->child;; chd = chd->sibling) {
238 chd->parent = tmp->parent;
243 chd->sibling = tmp->sibling;
253 static void __release_child_resources(struct resource *r)
255 struct resource *tmp, *p;
256 resource_size_t size;
266 __release_child_resources(tmp);
268 printk(KERN_DEBUG "release child resource %pR\n", tmp);
269 /* need to restore size, and keep flags */
270 size = resource_size(tmp);
276 void release_child_resources(struct resource *r)
278 write_lock(&resource_lock);
279 __release_child_resources(r);
280 write_unlock(&resource_lock);
284 * request_resource_conflict - request and reserve an I/O or memory resource
285 * @root: root resource descriptor
286 * @new: resource descriptor desired by caller
288 * Returns 0 for success, conflict resource on error.
290 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
292 struct resource *conflict;
294 write_lock(&resource_lock);
295 conflict = __request_resource(root, new);
296 write_unlock(&resource_lock);
301 * request_resource - request and reserve an I/O or memory resource
302 * @root: root resource descriptor
303 * @new: resource descriptor desired by caller
305 * Returns 0 for success, negative error code on error.
307 int request_resource(struct resource *root, struct resource *new)
309 struct resource *conflict;
311 conflict = request_resource_conflict(root, new);
312 return conflict ? -EBUSY : 0;
315 EXPORT_SYMBOL(request_resource);
318 * release_resource - release a previously reserved resource
319 * @old: resource pointer
321 int release_resource(struct resource *old)
325 write_lock(&resource_lock);
326 retval = __release_resource(old, true);
327 write_unlock(&resource_lock);
331 EXPORT_SYMBOL(release_resource);
334 * find_next_iomem_res - Finds the lowest iomem resource that covers part of
337 * If a resource is found, returns 0 and @*res is overwritten with the part
338 * of the resource that's within [@start..@end]; if none is found, returns
339 * -ENODEV. Returns -EINVAL for invalid parameters.
341 * @start: start address of the resource searched for
342 * @end: end address of same resource
343 * @flags: flags which the resource must have
344 * @desc: descriptor the resource must have
345 * @res: return ptr, if resource found
347 * The caller must specify @start, @end, @flags, and @desc
348 * (which may be IORES_DESC_NONE).
350 static int find_next_iomem_res(resource_size_t start, resource_size_t end,
351 unsigned long flags, unsigned long desc,
352 struct resource *res)
362 read_lock(&resource_lock);
364 for (p = iomem_resource.child; p; p = next_resource(p)) {
365 /* If we passed the resource we are looking for, stop */
366 if (p->start > end) {
371 /* Skip until we find a range that matches what we look for */
375 if ((p->flags & flags) != flags)
377 if ((desc != IORES_DESC_NONE) && (desc != p->desc))
380 /* Found a match, break */
386 *res = (struct resource) {
387 .start = max(start, p->start),
388 .end = min(end, p->end),
395 read_unlock(&resource_lock);
396 return p ? 0 : -ENODEV;
399 static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
400 unsigned long flags, unsigned long desc,
402 int (*func)(struct resource *, void *))
407 while (start < end &&
408 !find_next_iomem_res(start, end, flags, desc, &res)) {
409 ret = (*func)(&res, arg);
420 * walk_iomem_res_desc - Walks through iomem resources and calls func()
421 * with matching resource ranges.
423 * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
424 * @flags: I/O resource flags
427 * @arg: function argument for the callback @func
428 * @func: callback function that is called for each qualifying resource area
430 * All the memory ranges which overlap start,end and also match flags and
431 * desc are valid candidates.
433 * NOTE: For a new descriptor search, define a new IORES_DESC in
434 * <linux/ioport.h> and set it in 'desc' of a target resource entry.
436 int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
437 u64 end, void *arg, int (*func)(struct resource *, void *))
439 return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
441 EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
444 * This function calls the @func callback against all memory ranges of type
445 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
446 * Now, this function is only for System RAM, it deals with full ranges and
447 * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
450 int walk_system_ram_res(u64 start, u64 end, void *arg,
451 int (*func)(struct resource *, void *))
453 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
455 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
460 * This function calls the @func callback against all memory ranges, which
461 * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
463 int walk_mem_res(u64 start, u64 end, void *arg,
464 int (*func)(struct resource *, void *))
466 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
468 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
473 * This function calls the @func callback against all memory ranges of type
474 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
475 * It is to be used only for System RAM.
477 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
478 void *arg, int (*func)(unsigned long, unsigned long, void *))
480 resource_size_t start, end;
483 unsigned long pfn, end_pfn;
486 start = (u64) start_pfn << PAGE_SHIFT;
487 end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
488 flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
489 while (start < end &&
490 !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
491 pfn = PFN_UP(res.start);
492 end_pfn = PFN_DOWN(res.end + 1);
494 ret = (*func)(pfn, end_pfn - pfn, arg);
502 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
508 * This generic page_is_ram() returns true if specified address is
509 * registered as System RAM in iomem_resource list.
511 int __weak page_is_ram(unsigned long pfn)
513 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
515 EXPORT_SYMBOL_GPL(page_is_ram);
517 static int __region_intersects(resource_size_t start, size_t size,
518 unsigned long flags, unsigned long desc)
521 int type = 0; int other = 0;
525 res.end = start + size - 1;
527 for (p = iomem_resource.child; p ; p = p->sibling) {
528 bool is_type = (((p->flags & flags) == flags) &&
529 ((desc == IORES_DESC_NONE) ||
532 if (resource_overlaps(p, &res))
533 is_type ? type++ : other++;
537 return REGION_DISJOINT;
540 return REGION_INTERSECTS;
546 * region_intersects() - determine intersection of region with known resources
547 * @start: region start address
548 * @size: size of region
549 * @flags: flags of resource (in iomem_resource)
550 * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
552 * Check if the specified region partially overlaps or fully eclipses a
553 * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
554 * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
555 * return REGION_MIXED if the region overlaps @flags/@desc and another
556 * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
557 * and no other defined resource. Note that REGION_INTERSECTS is also
558 * returned in the case when the specified region overlaps RAM and undefined
561 * region_intersect() is used by memory remapping functions to ensure
562 * the user is not remapping RAM and is a vast speed up over walking
563 * through the resource table page by page.
565 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
570 read_lock(&resource_lock);
571 ret = __region_intersects(start, size, flags, desc);
572 read_unlock(&resource_lock);
576 EXPORT_SYMBOL_GPL(region_intersects);
578 void __weak arch_remove_reservations(struct resource *avail)
582 static resource_size_t simple_align_resource(void *data,
583 const struct resource *avail,
584 resource_size_t size,
585 resource_size_t align)
590 static void resource_clip(struct resource *res, resource_size_t min,
593 if (res->start < min)
600 * Find empty slot in the resource tree with the given range and
601 * alignment constraints
603 static int __find_resource(struct resource *root, struct resource *old,
604 struct resource *new,
605 resource_size_t size,
606 struct resource_constraint *constraint)
608 struct resource *this = root->child;
609 struct resource tmp = *new, avail, alloc;
611 tmp.start = root->start;
613 * Skip past an allocated resource that starts at 0, since the assignment
614 * of this->start - 1 to tmp->end below would cause an underflow.
616 if (this && this->start == root->start) {
617 tmp.start = (this == old) ? old->start : this->end + 1;
618 this = this->sibling;
622 tmp.end = (this == old) ? this->end : this->start - 1;
626 if (tmp.end < tmp.start)
629 resource_clip(&tmp, constraint->min, constraint->max);
630 arch_remove_reservations(&tmp);
632 /* Check for overflow after ALIGN() */
633 avail.start = ALIGN(tmp.start, constraint->align);
635 avail.flags = new->flags & ~IORESOURCE_UNSET;
636 if (avail.start >= tmp.start) {
637 alloc.flags = avail.flags;
638 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
639 size, constraint->align);
640 alloc.end = alloc.start + size - 1;
641 if (alloc.start <= alloc.end &&
642 resource_contains(&avail, &alloc)) {
643 new->start = alloc.start;
644 new->end = alloc.end;
649 next: if (!this || this->end == root->end)
653 tmp.start = this->end + 1;
654 this = this->sibling;
660 * Find empty slot in the resource tree given range and alignment.
662 static int find_resource(struct resource *root, struct resource *new,
663 resource_size_t size,
664 struct resource_constraint *constraint)
666 return __find_resource(root, NULL, new, size, constraint);
670 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
671 * The resource will be relocated if the new size cannot be reallocated in the
674 * @root: root resource descriptor
675 * @old: resource descriptor desired by caller
676 * @newsize: new size of the resource descriptor
677 * @constraint: the size and alignment constraints to be met.
679 static int reallocate_resource(struct resource *root, struct resource *old,
680 resource_size_t newsize,
681 struct resource_constraint *constraint)
684 struct resource new = *old;
685 struct resource *conflict;
687 write_lock(&resource_lock);
689 if ((err = __find_resource(root, old, &new, newsize, constraint)))
692 if (resource_contains(&new, old)) {
693 old->start = new.start;
703 if (resource_contains(old, &new)) {
704 old->start = new.start;
707 __release_resource(old, true);
709 conflict = __request_resource(root, old);
713 write_unlock(&resource_lock);
719 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
720 * The resource will be reallocated with a new size if it was already allocated
721 * @root: root resource descriptor
722 * @new: resource descriptor desired by caller
723 * @size: requested resource region size
724 * @min: minimum boundary to allocate
725 * @max: maximum boundary to allocate
726 * @align: alignment requested, in bytes
727 * @alignf: alignment function, optional, called if not NULL
728 * @alignf_data: arbitrary data to pass to the @alignf function
730 int allocate_resource(struct resource *root, struct resource *new,
731 resource_size_t size, resource_size_t min,
732 resource_size_t max, resource_size_t align,
733 resource_size_t (*alignf)(void *,
734 const struct resource *,
740 struct resource_constraint constraint;
743 alignf = simple_align_resource;
745 constraint.min = min;
746 constraint.max = max;
747 constraint.align = align;
748 constraint.alignf = alignf;
749 constraint.alignf_data = alignf_data;
752 /* resource is already allocated, try reallocating with
753 the new constraints */
754 return reallocate_resource(root, new, size, &constraint);
757 write_lock(&resource_lock);
758 err = find_resource(root, new, size, &constraint);
759 if (err >= 0 && __request_resource(root, new))
761 write_unlock(&resource_lock);
765 EXPORT_SYMBOL(allocate_resource);
768 * lookup_resource - find an existing resource by a resource start address
769 * @root: root resource descriptor
770 * @start: resource start address
772 * Returns a pointer to the resource if found, NULL otherwise
774 struct resource *lookup_resource(struct resource *root, resource_size_t start)
776 struct resource *res;
778 read_lock(&resource_lock);
779 for (res = root->child; res; res = res->sibling) {
780 if (res->start == start)
783 read_unlock(&resource_lock);
789 * Insert a resource into the resource tree. If successful, return NULL,
790 * otherwise return the conflicting resource (compare to __request_resource())
792 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
794 struct resource *first, *next;
796 for (;; parent = first) {
797 first = __request_resource(parent, new);
803 if (WARN_ON(first == new)) /* duplicated insertion */
806 if ((first->start > new->start) || (first->end < new->end))
808 if ((first->start == new->start) && (first->end == new->end))
812 for (next = first; ; next = next->sibling) {
813 /* Partial overlap? Bad, and unfixable */
814 if (next->start < new->start || next->end > new->end)
818 if (next->sibling->start > new->end)
822 new->parent = parent;
823 new->sibling = next->sibling;
826 next->sibling = NULL;
827 for (next = first; next; next = next->sibling)
830 if (parent->child == first) {
833 next = parent->child;
834 while (next->sibling != first)
835 next = next->sibling;
842 * insert_resource_conflict - Inserts resource in the resource tree
843 * @parent: parent of the new resource
844 * @new: new resource to insert
846 * Returns 0 on success, conflict resource if the resource can't be inserted.
848 * This function is equivalent to request_resource_conflict when no conflict
849 * happens. If a conflict happens, and the conflicting resources
850 * entirely fit within the range of the new resource, then the new
851 * resource is inserted and the conflicting resources become children of
854 * This function is intended for producers of resources, such as FW modules
857 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
859 struct resource *conflict;
861 write_lock(&resource_lock);
862 conflict = __insert_resource(parent, new);
863 write_unlock(&resource_lock);
868 * insert_resource - Inserts a resource in the resource tree
869 * @parent: parent of the new resource
870 * @new: new resource to insert
872 * Returns 0 on success, -EBUSY if the resource can't be inserted.
874 * This function is intended for producers of resources, such as FW modules
877 int insert_resource(struct resource *parent, struct resource *new)
879 struct resource *conflict;
881 conflict = insert_resource_conflict(parent, new);
882 return conflict ? -EBUSY : 0;
884 EXPORT_SYMBOL_GPL(insert_resource);
887 * insert_resource_expand_to_fit - Insert a resource into the resource tree
888 * @root: root resource descriptor
889 * @new: new resource to insert
891 * Insert a resource into the resource tree, possibly expanding it in order
892 * to make it encompass any conflicting resources.
894 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
899 write_lock(&resource_lock);
901 struct resource *conflict;
903 conflict = __insert_resource(root, new);
906 if (conflict == root)
909 /* Ok, expand resource to cover the conflict, then try again .. */
910 if (conflict->start < new->start)
911 new->start = conflict->start;
912 if (conflict->end > new->end)
913 new->end = conflict->end;
915 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
917 write_unlock(&resource_lock);
921 * remove_resource - Remove a resource in the resource tree
922 * @old: resource to remove
924 * Returns 0 on success, -EINVAL if the resource is not valid.
926 * This function removes a resource previously inserted by insert_resource()
927 * or insert_resource_conflict(), and moves the children (if any) up to
928 * where they were before. insert_resource() and insert_resource_conflict()
929 * insert a new resource, and move any conflicting resources down to the
930 * children of the new resource.
932 * insert_resource(), insert_resource_conflict() and remove_resource() are
933 * intended for producers of resources, such as FW modules and bus drivers.
935 int remove_resource(struct resource *old)
939 write_lock(&resource_lock);
940 retval = __release_resource(old, false);
941 write_unlock(&resource_lock);
944 EXPORT_SYMBOL_GPL(remove_resource);
946 static int __adjust_resource(struct resource *res, resource_size_t start,
947 resource_size_t size)
949 struct resource *tmp, *parent = res->parent;
950 resource_size_t end = start + size - 1;
956 if ((start < parent->start) || (end > parent->end))
959 if (res->sibling && (res->sibling->start <= end))
964 while (tmp->sibling != res)
966 if (start <= tmp->end)
971 for (tmp = res->child; tmp; tmp = tmp->sibling)
972 if ((tmp->start < start) || (tmp->end > end))
984 * adjust_resource - modify a resource's start and size
985 * @res: resource to modify
986 * @start: new start value
989 * Given an existing resource, change its start and size to match the
990 * arguments. Returns 0 on success, -EBUSY if it can't fit.
991 * Existing children of the resource are assumed to be immutable.
993 int adjust_resource(struct resource *res, resource_size_t start,
994 resource_size_t size)
998 write_lock(&resource_lock);
999 result = __adjust_resource(res, start, size);
1000 write_unlock(&resource_lock);
1003 EXPORT_SYMBOL(adjust_resource);
1006 __reserve_region_with_split(struct resource *root, resource_size_t start,
1007 resource_size_t end, const char *name)
1009 struct resource *parent = root;
1010 struct resource *conflict;
1011 struct resource *res = alloc_resource(GFP_ATOMIC);
1012 struct resource *next_res = NULL;
1013 int type = resource_type(root);
1021 res->flags = type | IORESOURCE_BUSY;
1022 res->desc = IORES_DESC_NONE;
1026 conflict = __request_resource(parent, res);
1035 /* conflict covered whole area */
1036 if (conflict->start <= res->start &&
1037 conflict->end >= res->end) {
1043 /* failed, split and try again */
1044 if (conflict->start > res->start) {
1046 res->end = conflict->start - 1;
1047 if (conflict->end < end) {
1048 next_res = alloc_resource(GFP_ATOMIC);
1053 next_res->name = name;
1054 next_res->start = conflict->end + 1;
1055 next_res->end = end;
1056 next_res->flags = type | IORESOURCE_BUSY;
1057 next_res->desc = IORES_DESC_NONE;
1060 res->start = conflict->end + 1;
1067 reserve_region_with_split(struct resource *root, resource_size_t start,
1068 resource_size_t end, const char *name)
1072 write_lock(&resource_lock);
1073 if (root->start > start || root->end < end) {
1074 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1075 (unsigned long long)start, (unsigned long long)end,
1077 if (start > root->end || end < root->start)
1080 if (end > root->end)
1082 if (start < root->start)
1083 start = root->start;
1084 pr_err("fixing request to [0x%llx-0x%llx]\n",
1085 (unsigned long long)start,
1086 (unsigned long long)end);
1091 __reserve_region_with_split(root, start, end, name);
1092 write_unlock(&resource_lock);
1096 * resource_alignment - calculate resource's alignment
1097 * @res: resource pointer
1099 * Returns alignment on success, 0 (invalid alignment) on failure.
1101 resource_size_t resource_alignment(struct resource *res)
1103 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1104 case IORESOURCE_SIZEALIGN:
1105 return resource_size(res);
1106 case IORESOURCE_STARTALIGN:
1114 * This is compatibility stuff for IO resources.
1116 * Note how this, unlike the above, knows about
1117 * the IO flag meanings (busy etc).
1119 * request_region creates a new busy region.
1121 * release_region releases a matching busy region.
1124 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1126 static struct inode *iomem_inode;
1128 #ifdef CONFIG_IO_STRICT_DEVMEM
1129 static void revoke_iomem(struct resource *res)
1131 /* pairs with smp_store_release() in iomem_init_inode() */
1132 struct inode *inode = smp_load_acquire(&iomem_inode);
1135 * Check that the initialization has completed. Losing the race
1136 * is ok because it means drivers are claiming resources before
1137 * the fs_initcall level of init and prevent iomem_get_mapping users
1138 * from establishing mappings.
1144 * The expectation is that the driver has successfully marked
1145 * the resource busy by this point, so devmem_is_allowed()
1146 * should start returning false, however for performance this
1147 * does not iterate the entire resource range.
1149 if (devmem_is_allowed(PHYS_PFN(res->start)) &&
1150 devmem_is_allowed(PHYS_PFN(res->end))) {
1152 * *cringe* iomem=relaxed says "go ahead, what's the
1153 * worst that can happen?"
1158 unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
1161 static void revoke_iomem(struct resource *res) {}
1164 struct address_space *iomem_get_mapping(void)
1167 * This function is only called from file open paths, hence guaranteed
1168 * that fs_initcalls have completed and no need to check for NULL. But
1169 * since revoke_iomem can be called before the initcall we still need
1170 * the barrier to appease checkers.
1172 return smp_load_acquire(&iomem_inode)->i_mapping;
1175 static int __request_region_locked(struct resource *res, struct resource *parent,
1176 resource_size_t start, resource_size_t n,
1177 const char *name, int flags)
1179 DECLARE_WAITQUEUE(wait, current);
1183 res->end = start + n - 1;
1186 struct resource *conflict;
1188 res->flags = resource_type(parent) | resource_ext_type(parent);
1189 res->flags |= IORESOURCE_BUSY | flags;
1190 res->desc = parent->desc;
1192 conflict = __request_resource(parent, res);
1196 * mm/hmm.c reserves physical addresses which then
1197 * become unavailable to other users. Conflicts are
1198 * not expected. Warn to aid debugging if encountered.
1200 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1201 pr_warn("Unaddressable device %s %pR conflicts with %pR",
1202 conflict->name, conflict, res);
1204 if (conflict != parent) {
1205 if (!(conflict->flags & IORESOURCE_BUSY)) {
1210 if (conflict->flags & flags & IORESOURCE_MUXED) {
1211 add_wait_queue(&muxed_resource_wait, &wait);
1212 write_unlock(&resource_lock);
1213 set_current_state(TASK_UNINTERRUPTIBLE);
1215 remove_wait_queue(&muxed_resource_wait, &wait);
1216 write_lock(&resource_lock);
1219 /* Uhhuh, that didn't work out.. */
1227 * __request_region - create a new busy resource region
1228 * @parent: parent resource descriptor
1229 * @start: resource start address
1230 * @n: resource region size
1231 * @name: reserving caller's ID string
1232 * @flags: IO resource flags
1234 struct resource *__request_region(struct resource *parent,
1235 resource_size_t start, resource_size_t n,
1236 const char *name, int flags)
1238 struct resource *res = alloc_resource(GFP_KERNEL);
1244 write_lock(&resource_lock);
1245 ret = __request_region_locked(res, parent, start, n, name, flags);
1246 write_unlock(&resource_lock);
1253 if (parent == &iomem_resource)
1258 EXPORT_SYMBOL(__request_region);
1261 * __release_region - release a previously reserved resource region
1262 * @parent: parent resource descriptor
1263 * @start: resource start address
1264 * @n: resource region size
1266 * The described resource region must match a currently busy region.
1268 void __release_region(struct resource *parent, resource_size_t start,
1271 struct resource **p;
1272 resource_size_t end;
1275 end = start + n - 1;
1277 write_lock(&resource_lock);
1280 struct resource *res = *p;
1284 if (res->start <= start && res->end >= end) {
1285 if (!(res->flags & IORESOURCE_BUSY)) {
1289 if (res->start != start || res->end != end)
1292 write_unlock(&resource_lock);
1293 if (res->flags & IORESOURCE_MUXED)
1294 wake_up(&muxed_resource_wait);
1301 write_unlock(&resource_lock);
1303 printk(KERN_WARNING "Trying to free nonexistent resource "
1304 "<%016llx-%016llx>\n", (unsigned long long)start,
1305 (unsigned long long)end);
1307 EXPORT_SYMBOL(__release_region);
1309 #ifdef CONFIG_MEMORY_HOTREMOVE
1311 * release_mem_region_adjustable - release a previously reserved memory region
1312 * @start: resource start address
1313 * @size: resource region size
1315 * This interface is intended for memory hot-delete. The requested region
1316 * is released from a currently busy memory resource. The requested region
1317 * must either match exactly or fit into a single busy resource entry. In
1318 * the latter case, the remaining resource is adjusted accordingly.
1319 * Existing children of the busy memory resource must be immutable in the
1323 * - Additional release conditions, such as overlapping region, can be
1324 * supported after they are confirmed as valid cases.
1325 * - When a busy memory resource gets split into two entries, the code
1326 * assumes that all children remain in the lower address entry for
1327 * simplicity. Enhance this logic when necessary.
1329 void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1331 struct resource *parent = &iomem_resource;
1332 struct resource *new_res = NULL;
1333 bool alloc_nofail = false;
1334 struct resource **p;
1335 struct resource *res;
1336 resource_size_t end;
1338 end = start + size - 1;
1339 if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1343 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1344 * just before releasing the region. This is highly unlikely to
1345 * fail - let's play save and make it never fail as the caller cannot
1346 * perform any error handling (e.g., trying to re-add memory will fail
1350 new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1353 write_lock(&resource_lock);
1355 while ((res = *p)) {
1356 if (res->start >= end)
1359 /* look for the next resource if it does not fit into */
1360 if (res->start > start || res->end < end) {
1366 * All memory regions added from memory-hotplug path have the
1367 * flag IORESOURCE_SYSTEM_RAM. If the resource does not have
1368 * this flag, we know that we are dealing with a resource coming
1369 * from HMM/devm. HMM/devm use another mechanism to add/release
1370 * a resource. This goes via devm_request_mem_region and
1371 * devm_release_mem_region.
1372 * HMM/devm take care to release their resources when they want,
1373 * so if we are dealing with them, let us just back off here.
1375 if (!(res->flags & IORESOURCE_SYSRAM)) {
1379 if (!(res->flags & IORESOURCE_MEM))
1382 if (!(res->flags & IORESOURCE_BUSY)) {
1387 /* found the target resource; let's adjust accordingly */
1388 if (res->start == start && res->end == end) {
1389 /* free the whole entry */
1392 } else if (res->start == start && res->end != end) {
1393 /* adjust the start */
1394 WARN_ON_ONCE(__adjust_resource(res, end + 1,
1396 } else if (res->start != start && res->end == end) {
1397 /* adjust the end */
1398 WARN_ON_ONCE(__adjust_resource(res, res->start,
1399 start - res->start));
1401 /* split into two entries - we need a new resource */
1403 new_res = alloc_resource(GFP_ATOMIC);
1405 alloc_nofail = true;
1406 write_unlock(&resource_lock);
1410 new_res->name = res->name;
1411 new_res->start = end + 1;
1412 new_res->end = res->end;
1413 new_res->flags = res->flags;
1414 new_res->desc = res->desc;
1415 new_res->parent = res->parent;
1416 new_res->sibling = res->sibling;
1417 new_res->child = NULL;
1419 if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1420 start - res->start)))
1422 res->sibling = new_res;
1429 write_unlock(&resource_lock);
1430 free_resource(new_res);
1432 #endif /* CONFIG_MEMORY_HOTREMOVE */
1434 #ifdef CONFIG_MEMORY_HOTPLUG
1435 static bool system_ram_resources_mergeable(struct resource *r1,
1436 struct resource *r2)
1438 /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1439 return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1440 r1->name == r2->name && r1->desc == r2->desc &&
1441 !r1->child && !r2->child;
1445 * merge_system_ram_resource - mark the System RAM resource mergeable and try to
1446 * merge it with adjacent, mergeable resources
1447 * @res: resource descriptor
1449 * This interface is intended for memory hotplug, whereby lots of contiguous
1450 * system ram resources are added (e.g., via add_memory*()) by a driver, and
1451 * the actual resource boundaries are not of interest (e.g., it might be
1452 * relevant for DIMMs). Only resources that are marked mergeable, that have the
1453 * same parent, and that don't have any children are considered. All mergeable
1454 * resources must be immutable during the request.
1457 * - The caller has to make sure that no pointers to resources that are
1458 * marked mergeable are used anymore after this call - the resource might
1459 * be freed and the pointer might be stale!
1460 * - release_mem_region_adjustable() will split on demand on memory hotunplug
1462 void merge_system_ram_resource(struct resource *res)
1464 const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1465 struct resource *cur;
1467 if (WARN_ON_ONCE((res->flags & flags) != flags))
1470 write_lock(&resource_lock);
1471 res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1473 /* Try to merge with next item in the list. */
1475 if (cur && system_ram_resources_mergeable(res, cur)) {
1476 res->end = cur->end;
1477 res->sibling = cur->sibling;
1481 /* Try to merge with previous item in the list. */
1482 cur = res->parent->child;
1483 while (cur && cur->sibling != res)
1485 if (cur && system_ram_resources_mergeable(cur, res)) {
1486 cur->end = res->end;
1487 cur->sibling = res->sibling;
1490 write_unlock(&resource_lock);
1492 #endif /* CONFIG_MEMORY_HOTPLUG */
1495 * Managed region resource
1497 static void devm_resource_release(struct device *dev, void *ptr)
1499 struct resource **r = ptr;
1501 release_resource(*r);
1505 * devm_request_resource() - request and reserve an I/O or memory resource
1506 * @dev: device for which to request the resource
1507 * @root: root of the resource tree from which to request the resource
1508 * @new: descriptor of the resource to request
1510 * This is a device-managed version of request_resource(). There is usually
1511 * no need to release resources requested by this function explicitly since
1512 * that will be taken care of when the device is unbound from its driver.
1513 * If for some reason the resource needs to be released explicitly, because
1514 * of ordering issues for example, drivers must call devm_release_resource()
1515 * rather than the regular release_resource().
1517 * When a conflict is detected between any existing resources and the newly
1518 * requested resource, an error message will be printed.
1520 * Returns 0 on success or a negative error code on failure.
1522 int devm_request_resource(struct device *dev, struct resource *root,
1523 struct resource *new)
1525 struct resource *conflict, **ptr;
1527 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1533 conflict = request_resource_conflict(root, new);
1535 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1536 new, conflict->name, conflict);
1541 devres_add(dev, ptr);
1544 EXPORT_SYMBOL(devm_request_resource);
1546 static int devm_resource_match(struct device *dev, void *res, void *data)
1548 struct resource **ptr = res;
1550 return *ptr == data;
1554 * devm_release_resource() - release a previously requested resource
1555 * @dev: device for which to release the resource
1556 * @new: descriptor of the resource to release
1558 * Releases a resource previously requested using devm_request_resource().
1560 void devm_release_resource(struct device *dev, struct resource *new)
1562 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1565 EXPORT_SYMBOL(devm_release_resource);
1567 struct region_devres {
1568 struct resource *parent;
1569 resource_size_t start;
1573 static void devm_region_release(struct device *dev, void *res)
1575 struct region_devres *this = res;
1577 __release_region(this->parent, this->start, this->n);
1580 static int devm_region_match(struct device *dev, void *res, void *match_data)
1582 struct region_devres *this = res, *match = match_data;
1584 return this->parent == match->parent &&
1585 this->start == match->start && this->n == match->n;
1589 __devm_request_region(struct device *dev, struct resource *parent,
1590 resource_size_t start, resource_size_t n, const char *name)
1592 struct region_devres *dr = NULL;
1593 struct resource *res;
1595 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1600 dr->parent = parent;
1604 res = __request_region(parent, start, n, name, 0);
1606 devres_add(dev, dr);
1612 EXPORT_SYMBOL(__devm_request_region);
1614 void __devm_release_region(struct device *dev, struct resource *parent,
1615 resource_size_t start, resource_size_t n)
1617 struct region_devres match_data = { parent, start, n };
1619 __release_region(parent, start, n);
1620 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1623 EXPORT_SYMBOL(__devm_release_region);
1626 * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1628 #define MAXRESERVE 4
1629 static int __init reserve_setup(char *str)
1631 static int reserved;
1632 static struct resource reserve[MAXRESERVE];
1635 unsigned int io_start, io_num;
1637 struct resource *parent;
1639 if (get_option(&str, &io_start) != 2)
1641 if (get_option(&str, &io_num) == 0)
1643 if (x < MAXRESERVE) {
1644 struct resource *res = reserve + x;
1647 * If the region starts below 0x10000, we assume it's
1648 * I/O port space; otherwise assume it's memory.
1650 if (io_start < 0x10000) {
1651 res->flags = IORESOURCE_IO;
1652 parent = &ioport_resource;
1654 res->flags = IORESOURCE_MEM;
1655 parent = &iomem_resource;
1657 res->name = "reserved";
1658 res->start = io_start;
1659 res->end = io_start + io_num - 1;
1660 res->flags |= IORESOURCE_BUSY;
1661 res->desc = IORES_DESC_NONE;
1663 if (request_resource(parent, res) == 0)
1669 __setup("reserve=", reserve_setup);
1672 * Check if the requested addr and size spans more than any slot in the
1673 * iomem resource tree.
1675 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1677 struct resource *p = &iomem_resource;
1681 read_lock(&resource_lock);
1682 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1684 * We can probably skip the resources without
1685 * IORESOURCE_IO attribute?
1687 if (p->start >= addr + size)
1691 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1692 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1695 * if a resource is "BUSY", it's not a hardware resource
1696 * but a driver mapping of such a resource; we don't want
1697 * to warn for those; some drivers legitimately map only
1698 * partial hardware resources. (example: vesafb)
1700 if (p->flags & IORESOURCE_BUSY)
1703 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1704 (unsigned long long)addr,
1705 (unsigned long long)(addr + size - 1),
1710 read_unlock(&resource_lock);
1715 #ifdef CONFIG_STRICT_DEVMEM
1716 static int strict_iomem_checks = 1;
1718 static int strict_iomem_checks;
1722 * Check if an address is exclusive to the kernel and must not be mapped to
1723 * user space, for example, via /dev/mem.
1725 * Returns true if exclusive to the kernel, otherwise returns false.
1727 bool iomem_is_exclusive(u64 addr)
1729 const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
1730 IORESOURCE_EXCLUSIVE;
1731 bool skip_children = false, err = false;
1732 int size = PAGE_SIZE;
1735 addr = addr & PAGE_MASK;
1737 read_lock(&resource_lock);
1738 for_each_resource(&iomem_resource, p, skip_children) {
1739 if (p->start >= addr + size)
1741 if (p->end < addr) {
1742 skip_children = true;
1745 skip_children = false;
1748 * IORESOURCE_SYSTEM_RAM resources are exclusive if
1749 * IORESOURCE_EXCLUSIVE is set, even if they
1750 * are not busy and even if "iomem=relaxed" is set. The
1751 * responsible driver dynamically adds/removes system RAM within
1752 * such an area and uncontrolled access is dangerous.
1754 if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
1760 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1761 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1764 if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
1766 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1767 || p->flags & IORESOURCE_EXCLUSIVE) {
1772 read_unlock(&resource_lock);
1777 struct resource_entry *resource_list_create_entry(struct resource *res,
1780 struct resource_entry *entry;
1782 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1784 INIT_LIST_HEAD(&entry->node);
1785 entry->res = res ? res : &entry->__res;
1790 EXPORT_SYMBOL(resource_list_create_entry);
1792 void resource_list_free(struct list_head *head)
1794 struct resource_entry *entry, *tmp;
1796 list_for_each_entry_safe(entry, tmp, head, node)
1797 resource_list_destroy_entry(entry);
1799 EXPORT_SYMBOL(resource_list_free);
1801 #ifdef CONFIG_DEVICE_PRIVATE
1802 static struct resource *__request_free_mem_region(struct device *dev,
1803 struct resource *base, unsigned long size, const char *name)
1805 resource_size_t end, addr;
1806 struct resource *res;
1807 struct region_devres *dr = NULL;
1809 size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
1810 end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
1811 addr = end - size + 1UL;
1813 res = alloc_resource(GFP_KERNEL);
1815 return ERR_PTR(-ENOMEM);
1818 dr = devres_alloc(devm_region_release,
1819 sizeof(struct region_devres), GFP_KERNEL);
1822 return ERR_PTR(-ENOMEM);
1826 write_lock(&resource_lock);
1827 for (; addr > size && addr >= base->start; addr -= size) {
1828 if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
1832 if (__request_region_locked(res, &iomem_resource, addr, size,
1837 dr->parent = &iomem_resource;
1840 devres_add(dev, dr);
1843 res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1844 write_unlock(&resource_lock);
1847 * A driver is claiming this region so revoke any mappings.
1852 write_unlock(&resource_lock);
1858 return ERR_PTR(-ERANGE);
1862 * devm_request_free_mem_region - find free region for device private memory
1864 * @dev: device struct to bind the resource to
1865 * @size: size in bytes of the device memory to add
1866 * @base: resource tree to look in
1868 * This function tries to find an empty range of physical address big enough to
1869 * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
1870 * memory, which in turn allocates struct pages.
1872 struct resource *devm_request_free_mem_region(struct device *dev,
1873 struct resource *base, unsigned long size)
1875 return __request_free_mem_region(dev, base, size, dev_name(dev));
1877 EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
1879 struct resource *request_free_mem_region(struct resource *base,
1880 unsigned long size, const char *name)
1882 return __request_free_mem_region(NULL, base, size, name);
1884 EXPORT_SYMBOL_GPL(request_free_mem_region);
1886 #endif /* CONFIG_DEVICE_PRIVATE */
1888 static int __init strict_iomem(char *str)
1890 if (strstr(str, "relaxed"))
1891 strict_iomem_checks = 0;
1892 if (strstr(str, "strict"))
1893 strict_iomem_checks = 1;
1897 static int iomem_fs_init_fs_context(struct fs_context *fc)
1899 return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
1902 static struct file_system_type iomem_fs_type = {
1904 .owner = THIS_MODULE,
1905 .init_fs_context = iomem_fs_init_fs_context,
1906 .kill_sb = kill_anon_super,
1909 static int __init iomem_init_inode(void)
1911 static struct vfsmount *iomem_vfs_mount;
1912 static int iomem_fs_cnt;
1913 struct inode *inode;
1916 rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
1918 pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
1922 inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
1923 if (IS_ERR(inode)) {
1924 rc = PTR_ERR(inode);
1925 pr_err("Cannot allocate inode for iomem: %d\n", rc);
1926 simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
1931 * Publish iomem revocation inode initialized.
1932 * Pairs with smp_load_acquire() in revoke_iomem().
1934 smp_store_release(&iomem_inode, inode);
1939 fs_initcall(iomem_init_inode);
1941 __setup("iomem=", strict_iomem);