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);
59 static struct resource *next_resource(struct resource *p)
63 while (!p->sibling && p->parent)
68 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
70 struct resource *p = v;
72 return (void *)next_resource(p);
77 enum { MAX_IORES_LEVEL = 5 };
79 static void *r_start(struct seq_file *m, loff_t *pos)
80 __acquires(resource_lock)
82 struct resource *p = PDE_DATA(file_inode(m->file));
84 read_lock(&resource_lock);
85 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
90 static void r_stop(struct seq_file *m, void *v)
91 __releases(resource_lock)
93 read_unlock(&resource_lock);
96 static int r_show(struct seq_file *m, void *v)
98 struct resource *root = PDE_DATA(file_inode(m->file));
99 struct resource *r = v, *p;
100 unsigned long long start, end;
101 int width = root->end < 0x10000 ? 4 : 8;
104 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
105 if (p->parent == root)
108 if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
115 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
119 r->name ? r->name : "<BAD>");
123 static const struct seq_operations resource_op = {
130 static int __init ioresources_init(void)
132 proc_create_seq_data("ioports", 0, NULL, &resource_op,
134 proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
137 __initcall(ioresources_init);
139 #endif /* CONFIG_PROC_FS */
141 static void free_resource(struct resource *res)
144 * If the resource was allocated using memblock early during boot
145 * we'll leak it here: we can only return full pages back to the
146 * buddy and trying to be smart and reusing them eventually in
147 * alloc_resource() overcomplicates resource handling.
149 if (res && PageSlab(virt_to_head_page(res)))
153 static struct resource *alloc_resource(gfp_t flags)
155 return kzalloc(sizeof(struct resource), flags);
158 /* Return the conflict entry if you can't request it */
159 static struct resource * __request_resource(struct resource *root, struct resource *new)
161 resource_size_t start = new->start;
162 resource_size_t end = new->end;
163 struct resource *tmp, **p;
167 if (start < root->start)
174 if (!tmp || tmp->start > end) {
181 if (tmp->end < start)
187 static int __release_resource(struct resource *old, bool release_child)
189 struct resource *tmp, **p, *chd;
192 WARN(old->sibling, "sibling but no parent");
197 p = &old->parent->child;
203 if (release_child || !(tmp->child)) {
206 for (chd = tmp->child;; chd = chd->sibling) {
207 chd->parent = tmp->parent;
212 chd->sibling = tmp->sibling;
222 static void __release_child_resources(struct resource *r)
224 struct resource *tmp, *p;
225 resource_size_t size;
235 __release_child_resources(tmp);
237 printk(KERN_DEBUG "release child resource %pR\n", tmp);
238 /* need to restore size, and keep flags */
239 size = resource_size(tmp);
245 void release_child_resources(struct resource *r)
247 write_lock(&resource_lock);
248 __release_child_resources(r);
249 write_unlock(&resource_lock);
253 * request_resource_conflict - request and reserve an I/O or memory resource
254 * @root: root resource descriptor
255 * @new: resource descriptor desired by caller
257 * Returns 0 for success, conflict resource on error.
259 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
261 struct resource *conflict;
263 write_lock(&resource_lock);
264 conflict = __request_resource(root, new);
265 write_unlock(&resource_lock);
270 * request_resource - request and reserve an I/O or memory resource
271 * @root: root resource descriptor
272 * @new: resource descriptor desired by caller
274 * Returns 0 for success, negative error code on error.
276 int request_resource(struct resource *root, struct resource *new)
278 struct resource *conflict;
280 conflict = request_resource_conflict(root, new);
281 return conflict ? -EBUSY : 0;
284 EXPORT_SYMBOL(request_resource);
287 * release_resource - release a previously reserved resource
288 * @old: resource pointer
290 int release_resource(struct resource *old)
294 write_lock(&resource_lock);
295 retval = __release_resource(old, true);
296 write_unlock(&resource_lock);
300 EXPORT_SYMBOL(release_resource);
303 * find_next_iomem_res - Finds the lowest iomem resource that covers part of
306 * If a resource is found, returns 0 and @*res is overwritten with the part
307 * of the resource that's within [@start..@end]; if none is found, returns
308 * -ENODEV. Returns -EINVAL for invalid parameters.
310 * @start: start address of the resource searched for
311 * @end: end address of same resource
312 * @flags: flags which the resource must have
313 * @desc: descriptor the resource must have
314 * @res: return ptr, if resource found
316 * The caller must specify @start, @end, @flags, and @desc
317 * (which may be IORES_DESC_NONE).
319 static int find_next_iomem_res(resource_size_t start, resource_size_t end,
320 unsigned long flags, unsigned long desc,
321 struct resource *res)
331 read_lock(&resource_lock);
333 for (p = iomem_resource.child; p; p = next_resource(p)) {
334 /* If we passed the resource we are looking for, stop */
335 if (p->start > end) {
340 /* Skip until we find a range that matches what we look for */
344 if ((p->flags & flags) != flags)
346 if ((desc != IORES_DESC_NONE) && (desc != p->desc))
349 /* Found a match, break */
355 *res = (struct resource) {
356 .start = max(start, p->start),
357 .end = min(end, p->end),
364 read_unlock(&resource_lock);
365 return p ? 0 : -ENODEV;
368 static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
369 unsigned long flags, unsigned long desc,
371 int (*func)(struct resource *, void *))
376 while (start < end &&
377 !find_next_iomem_res(start, end, flags, desc, &res)) {
378 ret = (*func)(&res, arg);
389 * walk_iomem_res_desc - Walks through iomem resources and calls func()
390 * with matching resource ranges.
392 * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
393 * @flags: I/O resource flags
396 * @arg: function argument for the callback @func
397 * @func: callback function that is called for each qualifying resource area
399 * All the memory ranges which overlap start,end and also match flags and
400 * desc are valid candidates.
402 * NOTE: For a new descriptor search, define a new IORES_DESC in
403 * <linux/ioport.h> and set it in 'desc' of a target resource entry.
405 int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
406 u64 end, void *arg, int (*func)(struct resource *, void *))
408 return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
410 EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
413 * This function calls the @func callback against all memory ranges of type
414 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
415 * Now, this function is only for System RAM, it deals with full ranges and
416 * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
419 int walk_system_ram_res(u64 start, u64 end, void *arg,
420 int (*func)(struct resource *, void *))
422 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
424 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
429 * This function calls the @func callback against all memory ranges, which
430 * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
432 int walk_mem_res(u64 start, u64 end, void *arg,
433 int (*func)(struct resource *, void *))
435 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
437 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
442 * This function calls the @func callback against all memory ranges of type
443 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
444 * It is to be used only for System RAM.
446 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
447 void *arg, int (*func)(unsigned long, unsigned long, void *))
449 resource_size_t start, end;
452 unsigned long pfn, end_pfn;
455 start = (u64) start_pfn << PAGE_SHIFT;
456 end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
457 flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
458 while (start < end &&
459 !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
460 pfn = PFN_UP(res.start);
461 end_pfn = PFN_DOWN(res.end + 1);
463 ret = (*func)(pfn, end_pfn - pfn, arg);
471 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
477 * This generic page_is_ram() returns true if specified address is
478 * registered as System RAM in iomem_resource list.
480 int __weak page_is_ram(unsigned long pfn)
482 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
484 EXPORT_SYMBOL_GPL(page_is_ram);
486 static int __region_intersects(resource_size_t start, size_t size,
487 unsigned long flags, unsigned long desc)
490 int type = 0; int other = 0;
494 res.end = start + size - 1;
496 for (p = iomem_resource.child; p ; p = p->sibling) {
497 bool is_type = (((p->flags & flags) == flags) &&
498 ((desc == IORES_DESC_NONE) ||
501 if (resource_overlaps(p, &res))
502 is_type ? type++ : other++;
506 return REGION_DISJOINT;
509 return REGION_INTERSECTS;
515 * region_intersects() - determine intersection of region with known resources
516 * @start: region start address
517 * @size: size of region
518 * @flags: flags of resource (in iomem_resource)
519 * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
521 * Check if the specified region partially overlaps or fully eclipses a
522 * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
523 * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
524 * return REGION_MIXED if the region overlaps @flags/@desc and another
525 * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
526 * and no other defined resource. Note that REGION_INTERSECTS is also
527 * returned in the case when the specified region overlaps RAM and undefined
530 * region_intersect() is used by memory remapping functions to ensure
531 * the user is not remapping RAM and is a vast speed up over walking
532 * through the resource table page by page.
534 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
539 read_lock(&resource_lock);
540 ret = __region_intersects(start, size, flags, desc);
541 read_unlock(&resource_lock);
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);
975 __reserve_region_with_split(struct resource *root, resource_size_t start,
976 resource_size_t end, const char *name)
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;
1036 reserve_region_with_split(struct resource *root, resource_size_t start,
1037 resource_size_t end, const char *name)
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);
1095 static struct inode *iomem_inode;
1097 #ifdef CONFIG_IO_STRICT_DEVMEM
1098 static void revoke_iomem(struct resource *res)
1100 /* pairs with smp_store_release() in iomem_init_inode() */
1101 struct inode *inode = smp_load_acquire(&iomem_inode);
1104 * Check that the initialization has completed. Losing the race
1105 * is ok because it means drivers are claiming resources before
1106 * the fs_initcall level of init and prevent iomem_get_mapping users
1107 * from establishing mappings.
1113 * The expectation is that the driver has successfully marked
1114 * the resource busy by this point, so devmem_is_allowed()
1115 * should start returning false, however for performance this
1116 * does not iterate the entire resource range.
1118 if (devmem_is_allowed(PHYS_PFN(res->start)) &&
1119 devmem_is_allowed(PHYS_PFN(res->end))) {
1121 * *cringe* iomem=relaxed says "go ahead, what's the
1122 * worst that can happen?"
1127 unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
1130 static void revoke_iomem(struct resource *res) {}
1133 struct address_space *iomem_get_mapping(void)
1136 * This function is only called from file open paths, hence guaranteed
1137 * that fs_initcalls have completed and no need to check for NULL. But
1138 * since revoke_iomem can be called before the initcall we still need
1139 * the barrier to appease checkers.
1141 return smp_load_acquire(&iomem_inode)->i_mapping;
1144 static int __request_region_locked(struct resource *res, struct resource *parent,
1145 resource_size_t start, resource_size_t n,
1146 const char *name, int flags)
1148 DECLARE_WAITQUEUE(wait, current);
1152 res->end = start + n - 1;
1155 struct resource *conflict;
1157 res->flags = resource_type(parent) | resource_ext_type(parent);
1158 res->flags |= IORESOURCE_BUSY | flags;
1159 res->desc = parent->desc;
1161 conflict = __request_resource(parent, res);
1165 * mm/hmm.c reserves physical addresses which then
1166 * become unavailable to other users. Conflicts are
1167 * not expected. Warn to aid debugging if encountered.
1169 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1170 pr_warn("Unaddressable device %s %pR conflicts with %pR",
1171 conflict->name, conflict, res);
1173 if (conflict != parent) {
1174 if (!(conflict->flags & IORESOURCE_BUSY)) {
1179 if (conflict->flags & flags & IORESOURCE_MUXED) {
1180 add_wait_queue(&muxed_resource_wait, &wait);
1181 write_unlock(&resource_lock);
1182 set_current_state(TASK_UNINTERRUPTIBLE);
1184 remove_wait_queue(&muxed_resource_wait, &wait);
1185 write_lock(&resource_lock);
1188 /* Uhhuh, that didn't work out.. */
1196 * __request_region - create a new busy resource region
1197 * @parent: parent resource descriptor
1198 * @start: resource start address
1199 * @n: resource region size
1200 * @name: reserving caller's ID string
1201 * @flags: IO resource flags
1203 struct resource *__request_region(struct resource *parent,
1204 resource_size_t start, resource_size_t n,
1205 const char *name, int flags)
1207 struct resource *res = alloc_resource(GFP_KERNEL);
1213 write_lock(&resource_lock);
1214 ret = __request_region_locked(res, parent, start, n, name, flags);
1215 write_unlock(&resource_lock);
1222 if (parent == &iomem_resource)
1227 EXPORT_SYMBOL(__request_region);
1230 * __release_region - release a previously reserved resource region
1231 * @parent: parent resource descriptor
1232 * @start: resource start address
1233 * @n: resource region size
1235 * The described resource region must match a currently busy region.
1237 void __release_region(struct resource *parent, resource_size_t start,
1240 struct resource **p;
1241 resource_size_t end;
1244 end = start + n - 1;
1246 write_lock(&resource_lock);
1249 struct resource *res = *p;
1253 if (res->start <= start && res->end >= end) {
1254 if (!(res->flags & IORESOURCE_BUSY)) {
1258 if (res->start != start || res->end != end)
1261 write_unlock(&resource_lock);
1262 if (res->flags & IORESOURCE_MUXED)
1263 wake_up(&muxed_resource_wait);
1270 write_unlock(&resource_lock);
1272 printk(KERN_WARNING "Trying to free nonexistent resource "
1273 "<%016llx-%016llx>\n", (unsigned long long)start,
1274 (unsigned long long)end);
1276 EXPORT_SYMBOL(__release_region);
1278 #ifdef CONFIG_MEMORY_HOTREMOVE
1280 * release_mem_region_adjustable - release a previously reserved memory region
1281 * @start: resource start address
1282 * @size: resource region size
1284 * This interface is intended for memory hot-delete. The requested region
1285 * is released from a currently busy memory resource. The requested region
1286 * must either match exactly or fit into a single busy resource entry. In
1287 * the latter case, the remaining resource is adjusted accordingly.
1288 * Existing children of the busy memory resource must be immutable in the
1292 * - Additional release conditions, such as overlapping region, can be
1293 * supported after they are confirmed as valid cases.
1294 * - When a busy memory resource gets split into two entries, the code
1295 * assumes that all children remain in the lower address entry for
1296 * simplicity. Enhance this logic when necessary.
1298 void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1300 struct resource *parent = &iomem_resource;
1301 struct resource *new_res = NULL;
1302 bool alloc_nofail = false;
1303 struct resource **p;
1304 struct resource *res;
1305 resource_size_t end;
1307 end = start + size - 1;
1308 if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1312 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1313 * just before releasing the region. This is highly unlikely to
1314 * fail - let's play save and make it never fail as the caller cannot
1315 * perform any error handling (e.g., trying to re-add memory will fail
1319 new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1322 write_lock(&resource_lock);
1324 while ((res = *p)) {
1325 if (res->start >= end)
1328 /* look for the next resource if it does not fit into */
1329 if (res->start > start || res->end < end) {
1335 * All memory regions added from memory-hotplug path have the
1336 * flag IORESOURCE_SYSTEM_RAM. If the resource does not have
1337 * this flag, we know that we are dealing with a resource coming
1338 * from HMM/devm. HMM/devm use another mechanism to add/release
1339 * a resource. This goes via devm_request_mem_region and
1340 * devm_release_mem_region.
1341 * HMM/devm take care to release their resources when they want,
1342 * so if we are dealing with them, let us just back off here.
1344 if (!(res->flags & IORESOURCE_SYSRAM)) {
1348 if (!(res->flags & IORESOURCE_MEM))
1351 if (!(res->flags & IORESOURCE_BUSY)) {
1356 /* found the target resource; let's adjust accordingly */
1357 if (res->start == start && res->end == end) {
1358 /* free the whole entry */
1361 } else if (res->start == start && res->end != end) {
1362 /* adjust the start */
1363 WARN_ON_ONCE(__adjust_resource(res, end + 1,
1365 } else if (res->start != start && res->end == end) {
1366 /* adjust the end */
1367 WARN_ON_ONCE(__adjust_resource(res, res->start,
1368 start - res->start));
1370 /* split into two entries - we need a new resource */
1372 new_res = alloc_resource(GFP_ATOMIC);
1374 alloc_nofail = true;
1375 write_unlock(&resource_lock);
1379 new_res->name = res->name;
1380 new_res->start = end + 1;
1381 new_res->end = res->end;
1382 new_res->flags = res->flags;
1383 new_res->desc = res->desc;
1384 new_res->parent = res->parent;
1385 new_res->sibling = res->sibling;
1386 new_res->child = NULL;
1388 if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1389 start - res->start)))
1391 res->sibling = new_res;
1398 write_unlock(&resource_lock);
1399 free_resource(new_res);
1401 #endif /* CONFIG_MEMORY_HOTREMOVE */
1403 #ifdef CONFIG_MEMORY_HOTPLUG
1404 static bool system_ram_resources_mergeable(struct resource *r1,
1405 struct resource *r2)
1407 /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1408 return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1409 r1->name == r2->name && r1->desc == r2->desc &&
1410 !r1->child && !r2->child;
1414 * merge_system_ram_resource - mark the System RAM resource mergeable and try to
1415 * merge it with adjacent, mergeable resources
1416 * @res: resource descriptor
1418 * This interface is intended for memory hotplug, whereby lots of contiguous
1419 * system ram resources are added (e.g., via add_memory*()) by a driver, and
1420 * the actual resource boundaries are not of interest (e.g., it might be
1421 * relevant for DIMMs). Only resources that are marked mergeable, that have the
1422 * same parent, and that don't have any children are considered. All mergeable
1423 * resources must be immutable during the request.
1426 * - The caller has to make sure that no pointers to resources that are
1427 * marked mergeable are used anymore after this call - the resource might
1428 * be freed and the pointer might be stale!
1429 * - release_mem_region_adjustable() will split on demand on memory hotunplug
1431 void merge_system_ram_resource(struct resource *res)
1433 const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1434 struct resource *cur;
1436 if (WARN_ON_ONCE((res->flags & flags) != flags))
1439 write_lock(&resource_lock);
1440 res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1442 /* Try to merge with next item in the list. */
1444 if (cur && system_ram_resources_mergeable(res, cur)) {
1445 res->end = cur->end;
1446 res->sibling = cur->sibling;
1450 /* Try to merge with previous item in the list. */
1451 cur = res->parent->child;
1452 while (cur && cur->sibling != res)
1454 if (cur && system_ram_resources_mergeable(cur, res)) {
1455 cur->end = res->end;
1456 cur->sibling = res->sibling;
1459 write_unlock(&resource_lock);
1461 #endif /* CONFIG_MEMORY_HOTPLUG */
1464 * Managed region resource
1466 static void devm_resource_release(struct device *dev, void *ptr)
1468 struct resource **r = ptr;
1470 release_resource(*r);
1474 * devm_request_resource() - request and reserve an I/O or memory resource
1475 * @dev: device for which to request the resource
1476 * @root: root of the resource tree from which to request the resource
1477 * @new: descriptor of the resource to request
1479 * This is a device-managed version of request_resource(). There is usually
1480 * no need to release resources requested by this function explicitly since
1481 * that will be taken care of when the device is unbound from its driver.
1482 * If for some reason the resource needs to be released explicitly, because
1483 * of ordering issues for example, drivers must call devm_release_resource()
1484 * rather than the regular release_resource().
1486 * When a conflict is detected between any existing resources and the newly
1487 * requested resource, an error message will be printed.
1489 * Returns 0 on success or a negative error code on failure.
1491 int devm_request_resource(struct device *dev, struct resource *root,
1492 struct resource *new)
1494 struct resource *conflict, **ptr;
1496 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1502 conflict = request_resource_conflict(root, new);
1504 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1505 new, conflict->name, conflict);
1510 devres_add(dev, ptr);
1513 EXPORT_SYMBOL(devm_request_resource);
1515 static int devm_resource_match(struct device *dev, void *res, void *data)
1517 struct resource **ptr = res;
1519 return *ptr == data;
1523 * devm_release_resource() - release a previously requested resource
1524 * @dev: device for which to release the resource
1525 * @new: descriptor of the resource to release
1527 * Releases a resource previously requested using devm_request_resource().
1529 void devm_release_resource(struct device *dev, struct resource *new)
1531 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1534 EXPORT_SYMBOL(devm_release_resource);
1536 struct region_devres {
1537 struct resource *parent;
1538 resource_size_t start;
1542 static void devm_region_release(struct device *dev, void *res)
1544 struct region_devres *this = res;
1546 __release_region(this->parent, this->start, this->n);
1549 static int devm_region_match(struct device *dev, void *res, void *match_data)
1551 struct region_devres *this = res, *match = match_data;
1553 return this->parent == match->parent &&
1554 this->start == match->start && this->n == match->n;
1558 __devm_request_region(struct device *dev, struct resource *parent,
1559 resource_size_t start, resource_size_t n, const char *name)
1561 struct region_devres *dr = NULL;
1562 struct resource *res;
1564 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1569 dr->parent = parent;
1573 res = __request_region(parent, start, n, name, 0);
1575 devres_add(dev, dr);
1581 EXPORT_SYMBOL(__devm_request_region);
1583 void __devm_release_region(struct device *dev, struct resource *parent,
1584 resource_size_t start, resource_size_t n)
1586 struct region_devres match_data = { parent, start, n };
1588 __release_region(parent, start, n);
1589 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1592 EXPORT_SYMBOL(__devm_release_region);
1595 * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1597 #define MAXRESERVE 4
1598 static int __init reserve_setup(char *str)
1600 static int reserved;
1601 static struct resource reserve[MAXRESERVE];
1604 unsigned int io_start, io_num;
1606 struct resource *parent;
1608 if (get_option(&str, &io_start) != 2)
1610 if (get_option(&str, &io_num) == 0)
1612 if (x < MAXRESERVE) {
1613 struct resource *res = reserve + x;
1616 * If the region starts below 0x10000, we assume it's
1617 * I/O port space; otherwise assume it's memory.
1619 if (io_start < 0x10000) {
1620 res->flags = IORESOURCE_IO;
1621 parent = &ioport_resource;
1623 res->flags = IORESOURCE_MEM;
1624 parent = &iomem_resource;
1626 res->name = "reserved";
1627 res->start = io_start;
1628 res->end = io_start + io_num - 1;
1629 res->flags |= IORESOURCE_BUSY;
1630 res->desc = IORES_DESC_NONE;
1632 if (request_resource(parent, res) == 0)
1638 __setup("reserve=", reserve_setup);
1641 * Check if the requested addr and size spans more than any slot in the
1642 * iomem resource tree.
1644 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1646 struct resource *p = &iomem_resource;
1650 read_lock(&resource_lock);
1651 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1653 * We can probably skip the resources without
1654 * IORESOURCE_IO attribute?
1656 if (p->start >= addr + size)
1660 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1661 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1664 * if a resource is "BUSY", it's not a hardware resource
1665 * but a driver mapping of such a resource; we don't want
1666 * to warn for those; some drivers legitimately map only
1667 * partial hardware resources. (example: vesafb)
1669 if (p->flags & IORESOURCE_BUSY)
1672 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1673 (unsigned long long)addr,
1674 (unsigned long long)(addr + size - 1),
1679 read_unlock(&resource_lock);
1684 #ifdef CONFIG_STRICT_DEVMEM
1685 static int strict_iomem_checks = 1;
1687 static int strict_iomem_checks;
1691 * check if an address is reserved in the iomem resource tree
1692 * returns true if reserved, false if not reserved.
1694 bool iomem_is_exclusive(u64 addr)
1696 struct resource *p = &iomem_resource;
1699 int size = PAGE_SIZE;
1701 if (!strict_iomem_checks)
1704 addr = addr & PAGE_MASK;
1706 read_lock(&resource_lock);
1707 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1709 * We can probably skip the resources without
1710 * IORESOURCE_IO attribute?
1712 if (p->start >= addr + size)
1717 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1718 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1721 if ((p->flags & IORESOURCE_BUSY) == 0)
1723 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1724 || p->flags & IORESOURCE_EXCLUSIVE) {
1729 read_unlock(&resource_lock);
1734 struct resource_entry *resource_list_create_entry(struct resource *res,
1737 struct resource_entry *entry;
1739 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1741 INIT_LIST_HEAD(&entry->node);
1742 entry->res = res ? res : &entry->__res;
1747 EXPORT_SYMBOL(resource_list_create_entry);
1749 void resource_list_free(struct list_head *head)
1751 struct resource_entry *entry, *tmp;
1753 list_for_each_entry_safe(entry, tmp, head, node)
1754 resource_list_destroy_entry(entry);
1756 EXPORT_SYMBOL(resource_list_free);
1758 #ifdef CONFIG_DEVICE_PRIVATE
1759 static struct resource *__request_free_mem_region(struct device *dev,
1760 struct resource *base, unsigned long size, const char *name)
1762 resource_size_t end, addr;
1763 struct resource *res;
1764 struct region_devres *dr = NULL;
1766 size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
1767 end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
1768 addr = end - size + 1UL;
1770 res = alloc_resource(GFP_KERNEL);
1772 return ERR_PTR(-ENOMEM);
1775 dr = devres_alloc(devm_region_release,
1776 sizeof(struct region_devres), GFP_KERNEL);
1779 return ERR_PTR(-ENOMEM);
1783 write_lock(&resource_lock);
1784 for (; addr > size && addr >= base->start; addr -= size) {
1785 if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
1789 if (__request_region_locked(res, &iomem_resource, addr, size,
1794 dr->parent = &iomem_resource;
1797 devres_add(dev, dr);
1800 res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1801 write_unlock(&resource_lock);
1804 * A driver is claiming this region so revoke any mappings.
1809 write_unlock(&resource_lock);
1815 return ERR_PTR(-ERANGE);
1819 * devm_request_free_mem_region - find free region for device private memory
1821 * @dev: device struct to bind the resource to
1822 * @size: size in bytes of the device memory to add
1823 * @base: resource tree to look in
1825 * This function tries to find an empty range of physical address big enough to
1826 * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
1827 * memory, which in turn allocates struct pages.
1829 struct resource *devm_request_free_mem_region(struct device *dev,
1830 struct resource *base, unsigned long size)
1832 return __request_free_mem_region(dev, base, size, dev_name(dev));
1834 EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
1836 struct resource *request_free_mem_region(struct resource *base,
1837 unsigned long size, const char *name)
1839 return __request_free_mem_region(NULL, base, size, name);
1841 EXPORT_SYMBOL_GPL(request_free_mem_region);
1843 #endif /* CONFIG_DEVICE_PRIVATE */
1845 static int __init strict_iomem(char *str)
1847 if (strstr(str, "relaxed"))
1848 strict_iomem_checks = 0;
1849 if (strstr(str, "strict"))
1850 strict_iomem_checks = 1;
1854 static int iomem_fs_init_fs_context(struct fs_context *fc)
1856 return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
1859 static struct file_system_type iomem_fs_type = {
1861 .owner = THIS_MODULE,
1862 .init_fs_context = iomem_fs_init_fs_context,
1863 .kill_sb = kill_anon_super,
1866 static int __init iomem_init_inode(void)
1868 static struct vfsmount *iomem_vfs_mount;
1869 static int iomem_fs_cnt;
1870 struct inode *inode;
1873 rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
1875 pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
1879 inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
1880 if (IS_ERR(inode)) {
1881 rc = PTR_ERR(inode);
1882 pr_err("Cannot allocate inode for iomem: %d\n", rc);
1883 simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
1888 * Publish iomem revocation inode initialized.
1889 * Pairs with smp_load_acquire() in revoke_iomem().
1891 smp_store_release(&iomem_inode, inode);
1896 fs_initcall(iomem_init_inode);
1898 __setup("iomem=", strict_iomem);