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
= m
->private;
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
= m
->private;
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 ioports_open(struct inode
*inode
, struct file
*file
)
140 int res
= seq_open(file
, &resource_op
);
142 struct seq_file
*m
= file
->private_data
;
143 m
->private = &ioport_resource
;
148 static int iomem_open(struct inode
*inode
, struct file
*file
)
150 int res
= seq_open(file
, &resource_op
);
152 struct seq_file
*m
= file
->private_data
;
153 m
->private = &iomem_resource
;
158 static const struct file_operations proc_ioports_operations
= {
159 .open
= ioports_open
,
162 .release
= seq_release
,
165 static const struct file_operations proc_iomem_operations
= {
169 .release
= seq_release
,
172 static int __init
ioresources_init(void)
174 proc_create("ioports", 0, NULL
, &proc_ioports_operations
);
175 proc_create("iomem", 0, NULL
, &proc_iomem_operations
);
178 __initcall(ioresources_init
);
180 #endif /* CONFIG_PROC_FS */
182 static void free_resource(struct resource
*res
)
187 if (!PageSlab(virt_to_head_page(res
))) {
188 spin_lock(&bootmem_resource_lock
);
189 res
->sibling
= bootmem_resource_free
;
190 bootmem_resource_free
= res
;
191 spin_unlock(&bootmem_resource_lock
);
197 static struct resource
*alloc_resource(gfp_t flags
)
199 struct resource
*res
= NULL
;
201 spin_lock(&bootmem_resource_lock
);
202 if (bootmem_resource_free
) {
203 res
= bootmem_resource_free
;
204 bootmem_resource_free
= res
->sibling
;
206 spin_unlock(&bootmem_resource_lock
);
209 memset(res
, 0, sizeof(struct resource
));
211 res
= kzalloc(sizeof(struct resource
), flags
);
216 /* Return the conflict entry if you can't request it */
217 static struct resource
* __request_resource(struct resource
*root
, struct resource
*new)
219 resource_size_t start
= new->start
;
220 resource_size_t end
= new->end
;
221 struct resource
*tmp
, **p
;
225 if (start
< root
->start
)
232 if (!tmp
|| tmp
->start
> end
) {
239 if (tmp
->end
< start
)
245 static int __release_resource(struct resource
*old
)
247 struct resource
*tmp
, **p
;
249 p
= &old
->parent
->child
;
264 static void __release_child_resources(struct resource
*r
)
266 struct resource
*tmp
, *p
;
267 resource_size_t size
;
277 __release_child_resources(tmp
);
279 printk(KERN_DEBUG
"release child resource %pR\n", tmp
);
280 /* need to restore size, and keep flags */
281 size
= resource_size(tmp
);
287 void release_child_resources(struct resource
*r
)
289 write_lock(&resource_lock
);
290 __release_child_resources(r
);
291 write_unlock(&resource_lock
);
295 * request_resource_conflict - request and reserve an I/O or memory resource
296 * @root: root resource descriptor
297 * @new: resource descriptor desired by caller
299 * Returns 0 for success, conflict resource on error.
301 struct resource
*request_resource_conflict(struct resource
*root
, struct resource
*new)
303 struct resource
*conflict
;
305 write_lock(&resource_lock
);
306 conflict
= __request_resource(root
, new);
307 write_unlock(&resource_lock
);
312 * request_resource - request and reserve an I/O or memory resource
313 * @root: root resource descriptor
314 * @new: resource descriptor desired by caller
316 * Returns 0 for success, negative error code on error.
318 int request_resource(struct resource
*root
, struct resource
*new)
320 struct resource
*conflict
;
322 conflict
= request_resource_conflict(root
, new);
323 return conflict
? -EBUSY
: 0;
326 EXPORT_SYMBOL(request_resource
);
329 * release_resource - release a previously reserved resource
330 * @old: resource pointer
332 int release_resource(struct resource
*old
)
336 write_lock(&resource_lock
);
337 retval
= __release_resource(old
);
338 write_unlock(&resource_lock
);
342 EXPORT_SYMBOL(release_resource
);
345 * Finds the lowest iomem reosurce exists with-in [res->start.res->end)
346 * the caller must specify res->start, res->end, res->flags and "name".
347 * If found, returns 0, res is overwritten, if not found, returns -1.
348 * This walks through whole tree and not just first level children
349 * until and unless first_level_children_only is true.
351 static int find_next_iomem_res(struct resource
*res
, char *name
,
352 bool first_level_children_only
)
354 resource_size_t start
, end
;
356 bool sibling_only
= false;
362 BUG_ON(start
>= end
);
364 if (first_level_children_only
)
367 read_lock(&resource_lock
);
369 for (p
= iomem_resource
.child
; p
; p
= next_resource(p
, sibling_only
)) {
370 if (p
->flags
!= res
->flags
)
372 if (name
&& strcmp(p
->name
, name
))
374 if (p
->start
> end
) {
378 if ((p
->end
>= start
) && (p
->start
< end
))
382 read_unlock(&resource_lock
);
386 if (res
->start
< p
->start
)
387 res
->start
= p
->start
;
388 if (res
->end
> p
->end
)
394 * Walks through iomem resources and calls func() with matching resource
395 * ranges. This walks through whole tree and not just first level children.
396 * All the memory ranges which overlap start,end and also match flags and
397 * name are valid candidates.
399 * @name: name of resource
400 * @flags: resource flags
404 int walk_iomem_res(char *name
, unsigned long flags
, u64 start
, u64 end
,
405 void *arg
, int (*func
)(u64
, u64
, void *))
415 while ((res
.start
< res
.end
) &&
416 (!find_next_iomem_res(&res
, name
, false))) {
417 ret
= (*func
)(res
.start
, res
.end
, arg
);
420 res
.start
= res
.end
+ 1;
427 * This function calls callback against all memory range of "System RAM"
428 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
429 * Now, this function is only for "System RAM". This function deals with
430 * full ranges and not pfn. If resources are not pfn aligned, dealing
431 * with pfn can truncate ranges.
433 int walk_system_ram_res(u64 start
, u64 end
, void *arg
,
434 int (*func
)(u64
, u64
, void *))
442 res
.flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
444 while ((res
.start
< res
.end
) &&
445 (!find_next_iomem_res(&res
, "System RAM", true))) {
446 ret
= (*func
)(res
.start
, res
.end
, arg
);
449 res
.start
= res
.end
+ 1;
455 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
458 * This function calls callback against all memory range of "System RAM"
459 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
460 * Now, this function is only for "System RAM".
462 int walk_system_ram_range(unsigned long start_pfn
, unsigned long nr_pages
,
463 void *arg
, int (*func
)(unsigned long, unsigned long, void *))
466 unsigned long pfn
, end_pfn
;
470 res
.start
= (u64
) start_pfn
<< PAGE_SHIFT
;
471 res
.end
= ((u64
)(start_pfn
+ nr_pages
) << PAGE_SHIFT
) - 1;
472 res
.flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
474 while ((res
.start
< res
.end
) &&
475 (find_next_iomem_res(&res
, "System RAM", true) >= 0)) {
476 pfn
= (res
.start
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
477 end_pfn
= (res
.end
+ 1) >> PAGE_SHIFT
;
479 ret
= (*func
)(pfn
, end_pfn
- pfn
, arg
);
482 res
.start
= res
.end
+ 1;
490 static int __is_ram(unsigned long pfn
, unsigned long nr_pages
, void *arg
)
495 * This generic page_is_ram() returns true if specified address is
496 * registered as "System RAM" in iomem_resource list.
498 int __weak
page_is_ram(unsigned long pfn
)
500 return walk_system_ram_range(pfn
, 1, NULL
, __is_ram
) == 1;
502 EXPORT_SYMBOL_GPL(page_is_ram
);
505 * region_intersects() - determine intersection of region with known resources
506 * @start: region start address
507 * @size: size of region
508 * @name: name of resource (in iomem_resource)
510 * Check if the specified region partially overlaps or fully eclipses a
511 * resource identified by @name. Return REGION_DISJOINT if the region
512 * does not overlap @name, return REGION_MIXED if the region overlaps
513 * @type and another resource, and return REGION_INTERSECTS if the
514 * region overlaps @type and no other defined resource. Note, that
515 * REGION_INTERSECTS is also returned in the case when the specified
516 * region overlaps RAM and undefined memory holes.
518 * region_intersect() is used by memory remapping functions to ensure
519 * the user is not remapping RAM and is a vast speed up over walking
520 * through the resource table page by page.
522 int region_intersects(resource_size_t start
, size_t size
, const char *name
)
524 unsigned long flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
525 resource_size_t end
= start
+ size
- 1;
526 int type
= 0; int other
= 0;
529 read_lock(&resource_lock
);
530 for (p
= iomem_resource
.child
; p
; p
= p
->sibling
) {
531 bool is_type
= strcmp(p
->name
, name
) == 0 && p
->flags
== flags
;
533 if (start
>= p
->start
&& start
<= p
->end
)
534 is_type
? type
++ : other
++;
535 if (end
>= p
->start
&& end
<= p
->end
)
536 is_type
? type
++ : other
++;
537 if (p
->start
>= start
&& p
->end
<= end
)
538 is_type
? type
++ : other
++;
540 read_unlock(&resource_lock
);
543 return type
? REGION_INTERSECTS
: REGION_DISJOINT
;
548 return REGION_DISJOINT
;
551 void __weak
arch_remove_reservations(struct resource
*avail
)
555 static resource_size_t
simple_align_resource(void *data
,
556 const struct resource
*avail
,
557 resource_size_t size
,
558 resource_size_t align
)
563 static void resource_clip(struct resource
*res
, resource_size_t min
,
566 if (res
->start
< min
)
573 * Find empty slot in the resource tree with the given range and
574 * alignment constraints
576 static int __find_resource(struct resource
*root
, struct resource
*old
,
577 struct resource
*new,
578 resource_size_t size
,
579 struct resource_constraint
*constraint
)
581 struct resource
*this = root
->child
;
582 struct resource tmp
= *new, avail
, alloc
;
584 tmp
.start
= root
->start
;
586 * Skip past an allocated resource that starts at 0, since the assignment
587 * of this->start - 1 to tmp->end below would cause an underflow.
589 if (this && this->start
== root
->start
) {
590 tmp
.start
= (this == old
) ? old
->start
: this->end
+ 1;
591 this = this->sibling
;
595 tmp
.end
= (this == old
) ? this->end
: this->start
- 1;
599 if (tmp
.end
< tmp
.start
)
602 resource_clip(&tmp
, constraint
->min
, constraint
->max
);
603 arch_remove_reservations(&tmp
);
605 /* Check for overflow after ALIGN() */
606 avail
.start
= ALIGN(tmp
.start
, constraint
->align
);
608 avail
.flags
= new->flags
& ~IORESOURCE_UNSET
;
609 if (avail
.start
>= tmp
.start
) {
610 alloc
.flags
= avail
.flags
;
611 alloc
.start
= constraint
->alignf(constraint
->alignf_data
, &avail
,
612 size
, constraint
->align
);
613 alloc
.end
= alloc
.start
+ size
- 1;
614 if (alloc
.start
<= alloc
.end
&&
615 resource_contains(&avail
, &alloc
)) {
616 new->start
= alloc
.start
;
617 new->end
= alloc
.end
;
622 next
: if (!this || this->end
== root
->end
)
626 tmp
.start
= this->end
+ 1;
627 this = this->sibling
;
633 * Find empty slot in the resource tree given range and alignment.
635 static int find_resource(struct resource
*root
, struct resource
*new,
636 resource_size_t size
,
637 struct resource_constraint
*constraint
)
639 return __find_resource(root
, NULL
, new, size
, constraint
);
643 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
644 * The resource will be relocated if the new size cannot be reallocated in the
647 * @root: root resource descriptor
648 * @old: resource descriptor desired by caller
649 * @newsize: new size of the resource descriptor
650 * @constraint: the size and alignment constraints to be met.
652 static int reallocate_resource(struct resource
*root
, struct resource
*old
,
653 resource_size_t newsize
,
654 struct resource_constraint
*constraint
)
657 struct resource
new = *old
;
658 struct resource
*conflict
;
660 write_lock(&resource_lock
);
662 if ((err
= __find_resource(root
, old
, &new, newsize
, constraint
)))
665 if (resource_contains(&new, old
)) {
666 old
->start
= new.start
;
676 if (resource_contains(old
, &new)) {
677 old
->start
= new.start
;
680 __release_resource(old
);
682 conflict
= __request_resource(root
, old
);
686 write_unlock(&resource_lock
);
692 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
693 * The resource will be reallocated with a new size if it was already allocated
694 * @root: root resource descriptor
695 * @new: resource descriptor desired by caller
696 * @size: requested resource region size
697 * @min: minimum boundary to allocate
698 * @max: maximum boundary to allocate
699 * @align: alignment requested, in bytes
700 * @alignf: alignment function, optional, called if not NULL
701 * @alignf_data: arbitrary data to pass to the @alignf function
703 int allocate_resource(struct resource
*root
, struct resource
*new,
704 resource_size_t size
, resource_size_t min
,
705 resource_size_t max
, resource_size_t align
,
706 resource_size_t (*alignf
)(void *,
707 const struct resource
*,
713 struct resource_constraint constraint
;
716 alignf
= simple_align_resource
;
718 constraint
.min
= min
;
719 constraint
.max
= max
;
720 constraint
.align
= align
;
721 constraint
.alignf
= alignf
;
722 constraint
.alignf_data
= alignf_data
;
725 /* resource is already allocated, try reallocating with
726 the new constraints */
727 return reallocate_resource(root
, new, size
, &constraint
);
730 write_lock(&resource_lock
);
731 err
= find_resource(root
, new, size
, &constraint
);
732 if (err
>= 0 && __request_resource(root
, new))
734 write_unlock(&resource_lock
);
738 EXPORT_SYMBOL(allocate_resource
);
741 * lookup_resource - find an existing resource by a resource start address
742 * @root: root resource descriptor
743 * @start: resource start address
745 * Returns a pointer to the resource if found, NULL otherwise
747 struct resource
*lookup_resource(struct resource
*root
, resource_size_t start
)
749 struct resource
*res
;
751 read_lock(&resource_lock
);
752 for (res
= root
->child
; res
; res
= res
->sibling
) {
753 if (res
->start
== start
)
756 read_unlock(&resource_lock
);
762 * Insert a resource into the resource tree. If successful, return NULL,
763 * otherwise return the conflicting resource (compare to __request_resource())
765 static struct resource
* __insert_resource(struct resource
*parent
, struct resource
*new)
767 struct resource
*first
, *next
;
769 for (;; parent
= first
) {
770 first
= __request_resource(parent
, new);
776 if (WARN_ON(first
== new)) /* duplicated insertion */
779 if ((first
->start
> new->start
) || (first
->end
< new->end
))
781 if ((first
->start
== new->start
) && (first
->end
== new->end
))
785 for (next
= first
; ; next
= next
->sibling
) {
786 /* Partial overlap? Bad, and unfixable */
787 if (next
->start
< new->start
|| next
->end
> new->end
)
791 if (next
->sibling
->start
> new->end
)
795 new->parent
= parent
;
796 new->sibling
= next
->sibling
;
799 next
->sibling
= NULL
;
800 for (next
= first
; next
; next
= next
->sibling
)
803 if (parent
->child
== first
) {
806 next
= parent
->child
;
807 while (next
->sibling
!= first
)
808 next
= next
->sibling
;
815 * insert_resource_conflict - Inserts resource in the resource tree
816 * @parent: parent of the new resource
817 * @new: new resource to insert
819 * Returns 0 on success, conflict resource if the resource can't be inserted.
821 * This function is equivalent to request_resource_conflict when no conflict
822 * happens. If a conflict happens, and the conflicting resources
823 * entirely fit within the range of the new resource, then the new
824 * resource is inserted and the conflicting resources become children of
827 struct resource
*insert_resource_conflict(struct resource
*parent
, struct resource
*new)
829 struct resource
*conflict
;
831 write_lock(&resource_lock
);
832 conflict
= __insert_resource(parent
, new);
833 write_unlock(&resource_lock
);
838 * insert_resource - Inserts a resource in the resource tree
839 * @parent: parent of the new resource
840 * @new: new resource to insert
842 * Returns 0 on success, -EBUSY if the resource can't be inserted.
844 int insert_resource(struct resource
*parent
, struct resource
*new)
846 struct resource
*conflict
;
848 conflict
= insert_resource_conflict(parent
, new);
849 return conflict
? -EBUSY
: 0;
853 * insert_resource_expand_to_fit - Insert a resource into the resource tree
854 * @root: root resource descriptor
855 * @new: new resource to insert
857 * Insert a resource into the resource tree, possibly expanding it in order
858 * to make it encompass any conflicting resources.
860 void insert_resource_expand_to_fit(struct resource
*root
, struct resource
*new)
865 write_lock(&resource_lock
);
867 struct resource
*conflict
;
869 conflict
= __insert_resource(root
, new);
872 if (conflict
== root
)
875 /* Ok, expand resource to cover the conflict, then try again .. */
876 if (conflict
->start
< new->start
)
877 new->start
= conflict
->start
;
878 if (conflict
->end
> new->end
)
879 new->end
= conflict
->end
;
881 printk("Expanded resource %s due to conflict with %s\n", new->name
, conflict
->name
);
883 write_unlock(&resource_lock
);
886 static int __adjust_resource(struct resource
*res
, resource_size_t start
,
887 resource_size_t size
)
889 struct resource
*tmp
, *parent
= res
->parent
;
890 resource_size_t end
= start
+ size
- 1;
896 if ((start
< parent
->start
) || (end
> parent
->end
))
899 if (res
->sibling
&& (res
->sibling
->start
<= end
))
904 while (tmp
->sibling
!= res
)
906 if (start
<= tmp
->end
)
911 for (tmp
= res
->child
; tmp
; tmp
= tmp
->sibling
)
912 if ((tmp
->start
< start
) || (tmp
->end
> end
))
924 * adjust_resource - modify a resource's start and size
925 * @res: resource to modify
926 * @start: new start value
929 * Given an existing resource, change its start and size to match the
930 * arguments. Returns 0 on success, -EBUSY if it can't fit.
931 * Existing children of the resource are assumed to be immutable.
933 int adjust_resource(struct resource
*res
, resource_size_t start
,
934 resource_size_t size
)
938 write_lock(&resource_lock
);
939 result
= __adjust_resource(res
, start
, size
);
940 write_unlock(&resource_lock
);
943 EXPORT_SYMBOL(adjust_resource
);
945 static void __init
__reserve_region_with_split(struct resource
*root
,
946 resource_size_t start
, resource_size_t end
,
949 struct resource
*parent
= root
;
950 struct resource
*conflict
;
951 struct resource
*res
= alloc_resource(GFP_ATOMIC
);
952 struct resource
*next_res
= NULL
;
960 res
->flags
= IORESOURCE_BUSY
;
964 conflict
= __request_resource(parent
, res
);
973 /* conflict covered whole area */
974 if (conflict
->start
<= res
->start
&&
975 conflict
->end
>= res
->end
) {
981 /* failed, split and try again */
982 if (conflict
->start
> res
->start
) {
984 res
->end
= conflict
->start
- 1;
985 if (conflict
->end
< end
) {
986 next_res
= alloc_resource(GFP_ATOMIC
);
991 next_res
->name
= name
;
992 next_res
->start
= conflict
->end
+ 1;
994 next_res
->flags
= IORESOURCE_BUSY
;
997 res
->start
= conflict
->end
+ 1;
1003 void __init
reserve_region_with_split(struct resource
*root
,
1004 resource_size_t start
, resource_size_t end
,
1009 write_lock(&resource_lock
);
1010 if (root
->start
> start
|| root
->end
< end
) {
1011 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1012 (unsigned long long)start
, (unsigned long long)end
,
1014 if (start
> root
->end
|| end
< root
->start
)
1017 if (end
> root
->end
)
1019 if (start
< root
->start
)
1020 start
= root
->start
;
1021 pr_err("fixing request to [0x%llx-0x%llx]\n",
1022 (unsigned long long)start
,
1023 (unsigned long long)end
);
1028 __reserve_region_with_split(root
, start
, end
, name
);
1029 write_unlock(&resource_lock
);
1033 * resource_alignment - calculate resource's alignment
1034 * @res: resource pointer
1036 * Returns alignment on success, 0 (invalid alignment) on failure.
1038 resource_size_t
resource_alignment(struct resource
*res
)
1040 switch (res
->flags
& (IORESOURCE_SIZEALIGN
| IORESOURCE_STARTALIGN
)) {
1041 case IORESOURCE_SIZEALIGN
:
1042 return resource_size(res
);
1043 case IORESOURCE_STARTALIGN
:
1051 * This is compatibility stuff for IO resources.
1053 * Note how this, unlike the above, knows about
1054 * the IO flag meanings (busy etc).
1056 * request_region creates a new busy region.
1058 * release_region releases a matching busy region.
1061 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait
);
1064 * __request_region - create a new busy resource region
1065 * @parent: parent resource descriptor
1066 * @start: resource start address
1067 * @n: resource region size
1068 * @name: reserving caller's ID string
1069 * @flags: IO resource flags
1071 struct resource
* __request_region(struct resource
*parent
,
1072 resource_size_t start
, resource_size_t n
,
1073 const char *name
, int flags
)
1075 DECLARE_WAITQUEUE(wait
, current
);
1076 struct resource
*res
= alloc_resource(GFP_KERNEL
);
1083 res
->end
= start
+ n
- 1;
1084 res
->flags
= resource_type(parent
);
1085 res
->flags
|= IORESOURCE_BUSY
| flags
;
1087 write_lock(&resource_lock
);
1090 struct resource
*conflict
;
1092 conflict
= __request_resource(parent
, res
);
1095 if (conflict
!= parent
) {
1096 if (!(conflict
->flags
& IORESOURCE_BUSY
)) {
1101 if (conflict
->flags
& flags
& IORESOURCE_MUXED
) {
1102 add_wait_queue(&muxed_resource_wait
, &wait
);
1103 write_unlock(&resource_lock
);
1104 set_current_state(TASK_UNINTERRUPTIBLE
);
1106 remove_wait_queue(&muxed_resource_wait
, &wait
);
1107 write_lock(&resource_lock
);
1110 /* Uhhuh, that didn't work out.. */
1115 write_unlock(&resource_lock
);
1118 EXPORT_SYMBOL(__request_region
);
1121 * __release_region - release a previously reserved resource region
1122 * @parent: parent resource descriptor
1123 * @start: resource start address
1124 * @n: resource region size
1126 * The described resource region must match a currently busy region.
1128 void __release_region(struct resource
*parent
, resource_size_t start
,
1131 struct resource
**p
;
1132 resource_size_t end
;
1135 end
= start
+ n
- 1;
1137 write_lock(&resource_lock
);
1140 struct resource
*res
= *p
;
1144 if (res
->start
<= start
&& res
->end
>= end
) {
1145 if (!(res
->flags
& IORESOURCE_BUSY
)) {
1149 if (res
->start
!= start
|| res
->end
!= end
)
1152 write_unlock(&resource_lock
);
1153 if (res
->flags
& IORESOURCE_MUXED
)
1154 wake_up(&muxed_resource_wait
);
1161 write_unlock(&resource_lock
);
1163 printk(KERN_WARNING
"Trying to free nonexistent resource "
1164 "<%016llx-%016llx>\n", (unsigned long long)start
,
1165 (unsigned long long)end
);
1167 EXPORT_SYMBOL(__release_region
);
1169 #ifdef CONFIG_MEMORY_HOTREMOVE
1171 * release_mem_region_adjustable - release a previously reserved memory region
1172 * @parent: parent resource descriptor
1173 * @start: resource start address
1174 * @size: resource region size
1176 * This interface is intended for memory hot-delete. The requested region
1177 * is released from a currently busy memory resource. The requested region
1178 * must either match exactly or fit into a single busy resource entry. In
1179 * the latter case, the remaining resource is adjusted accordingly.
1180 * Existing children of the busy memory resource must be immutable in the
1184 * - Additional release conditions, such as overlapping region, can be
1185 * supported after they are confirmed as valid cases.
1186 * - When a busy memory resource gets split into two entries, the code
1187 * assumes that all children remain in the lower address entry for
1188 * simplicity. Enhance this logic when necessary.
1190 int release_mem_region_adjustable(struct resource
*parent
,
1191 resource_size_t start
, resource_size_t size
)
1193 struct resource
**p
;
1194 struct resource
*res
;
1195 struct resource
*new_res
;
1196 resource_size_t end
;
1199 end
= start
+ size
- 1;
1200 if ((start
< parent
->start
) || (end
> parent
->end
))
1203 /* The alloc_resource() result gets checked later */
1204 new_res
= alloc_resource(GFP_KERNEL
);
1207 write_lock(&resource_lock
);
1209 while ((res
= *p
)) {
1210 if (res
->start
>= end
)
1213 /* look for the next resource if it does not fit into */
1214 if (res
->start
> start
|| res
->end
< end
) {
1219 if (!(res
->flags
& IORESOURCE_MEM
))
1222 if (!(res
->flags
& IORESOURCE_BUSY
)) {
1227 /* found the target resource; let's adjust accordingly */
1228 if (res
->start
== start
&& res
->end
== end
) {
1229 /* free the whole entry */
1233 } else if (res
->start
== start
&& res
->end
!= end
) {
1234 /* adjust the start */
1235 ret
= __adjust_resource(res
, end
+ 1,
1237 } else if (res
->start
!= start
&& res
->end
== end
) {
1238 /* adjust the end */
1239 ret
= __adjust_resource(res
, res
->start
,
1240 start
- res
->start
);
1242 /* split into two entries */
1247 new_res
->name
= res
->name
;
1248 new_res
->start
= end
+ 1;
1249 new_res
->end
= res
->end
;
1250 new_res
->flags
= res
->flags
;
1251 new_res
->parent
= res
->parent
;
1252 new_res
->sibling
= res
->sibling
;
1253 new_res
->child
= NULL
;
1255 ret
= __adjust_resource(res
, res
->start
,
1256 start
- res
->start
);
1259 res
->sibling
= new_res
;
1266 write_unlock(&resource_lock
);
1267 free_resource(new_res
);
1270 #endif /* CONFIG_MEMORY_HOTREMOVE */
1273 * Managed region resource
1275 static void devm_resource_release(struct device
*dev
, void *ptr
)
1277 struct resource
**r
= ptr
;
1279 release_resource(*r
);
1283 * devm_request_resource() - request and reserve an I/O or memory resource
1284 * @dev: device for which to request the resource
1285 * @root: root of the resource tree from which to request the resource
1286 * @new: descriptor of the resource to request
1288 * This is a device-managed version of request_resource(). There is usually
1289 * no need to release resources requested by this function explicitly since
1290 * that will be taken care of when the device is unbound from its driver.
1291 * If for some reason the resource needs to be released explicitly, because
1292 * of ordering issues for example, drivers must call devm_release_resource()
1293 * rather than the regular release_resource().
1295 * When a conflict is detected between any existing resources and the newly
1296 * requested resource, an error message will be printed.
1298 * Returns 0 on success or a negative error code on failure.
1300 int devm_request_resource(struct device
*dev
, struct resource
*root
,
1301 struct resource
*new)
1303 struct resource
*conflict
, **ptr
;
1305 ptr
= devres_alloc(devm_resource_release
, sizeof(*ptr
), GFP_KERNEL
);
1311 conflict
= request_resource_conflict(root
, new);
1313 dev_err(dev
, "resource collision: %pR conflicts with %s %pR\n",
1314 new, conflict
->name
, conflict
);
1319 devres_add(dev
, ptr
);
1322 EXPORT_SYMBOL(devm_request_resource
);
1324 static int devm_resource_match(struct device
*dev
, void *res
, void *data
)
1326 struct resource
**ptr
= res
;
1328 return *ptr
== data
;
1332 * devm_release_resource() - release a previously requested resource
1333 * @dev: device for which to release the resource
1334 * @new: descriptor of the resource to release
1336 * Releases a resource previously requested using devm_request_resource().
1338 void devm_release_resource(struct device
*dev
, struct resource
*new)
1340 WARN_ON(devres_release(dev
, devm_resource_release
, devm_resource_match
,
1343 EXPORT_SYMBOL(devm_release_resource
);
1345 struct region_devres
{
1346 struct resource
*parent
;
1347 resource_size_t start
;
1351 static void devm_region_release(struct device
*dev
, void *res
)
1353 struct region_devres
*this = res
;
1355 __release_region(this->parent
, this->start
, this->n
);
1358 static int devm_region_match(struct device
*dev
, void *res
, void *match_data
)
1360 struct region_devres
*this = res
, *match
= match_data
;
1362 return this->parent
== match
->parent
&&
1363 this->start
== match
->start
&& this->n
== match
->n
;
1366 struct resource
* __devm_request_region(struct device
*dev
,
1367 struct resource
*parent
, resource_size_t start
,
1368 resource_size_t n
, const char *name
)
1370 struct region_devres
*dr
= NULL
;
1371 struct resource
*res
;
1373 dr
= devres_alloc(devm_region_release
, sizeof(struct region_devres
),
1378 dr
->parent
= parent
;
1382 res
= __request_region(parent
, start
, n
, name
, 0);
1384 devres_add(dev
, dr
);
1390 EXPORT_SYMBOL(__devm_request_region
);
1392 void __devm_release_region(struct device
*dev
, struct resource
*parent
,
1393 resource_size_t start
, resource_size_t n
)
1395 struct region_devres match_data
= { parent
, start
, n
};
1397 __release_region(parent
, start
, n
);
1398 WARN_ON(devres_destroy(dev
, devm_region_release
, devm_region_match
,
1401 EXPORT_SYMBOL(__devm_release_region
);
1404 * Called from init/main.c to reserve IO ports.
1406 #define MAXRESERVE 4
1407 static int __init
reserve_setup(char *str
)
1409 static int reserved
;
1410 static struct resource reserve
[MAXRESERVE
];
1413 unsigned int io_start
, io_num
;
1416 if (get_option (&str
, &io_start
) != 2)
1418 if (get_option (&str
, &io_num
) == 0)
1420 if (x
< MAXRESERVE
) {
1421 struct resource
*res
= reserve
+ x
;
1422 res
->name
= "reserved";
1423 res
->start
= io_start
;
1424 res
->end
= io_start
+ io_num
- 1;
1425 res
->flags
= IORESOURCE_BUSY
;
1427 if (request_resource(res
->start
>= 0x10000 ? &iomem_resource
: &ioport_resource
, res
) == 0)
1434 __setup("reserve=", reserve_setup
);
1437 * Check if the requested addr and size spans more than any slot in the
1438 * iomem resource tree.
1440 int iomem_map_sanity_check(resource_size_t addr
, unsigned long size
)
1442 struct resource
*p
= &iomem_resource
;
1446 read_lock(&resource_lock
);
1447 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
1449 * We can probably skip the resources without
1450 * IORESOURCE_IO attribute?
1452 if (p
->start
>= addr
+ size
)
1456 if (PFN_DOWN(p
->start
) <= PFN_DOWN(addr
) &&
1457 PFN_DOWN(p
->end
) >= PFN_DOWN(addr
+ size
- 1))
1460 * if a resource is "BUSY", it's not a hardware resource
1461 * but a driver mapping of such a resource; we don't want
1462 * to warn for those; some drivers legitimately map only
1463 * partial hardware resources. (example: vesafb)
1465 if (p
->flags
& IORESOURCE_BUSY
)
1468 printk(KERN_WARNING
"resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1469 (unsigned long long)addr
,
1470 (unsigned long long)(addr
+ size
- 1),
1475 read_unlock(&resource_lock
);
1480 #ifdef CONFIG_STRICT_DEVMEM
1481 static int strict_iomem_checks
= 1;
1483 static int strict_iomem_checks
;
1487 * check if an address is reserved in the iomem resource tree
1488 * returns 1 if reserved, 0 if not reserved.
1490 int iomem_is_exclusive(u64 addr
)
1492 struct resource
*p
= &iomem_resource
;
1495 int size
= PAGE_SIZE
;
1497 if (!strict_iomem_checks
)
1500 addr
= addr
& PAGE_MASK
;
1502 read_lock(&resource_lock
);
1503 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
1505 * We can probably skip the resources without
1506 * IORESOURCE_IO attribute?
1508 if (p
->start
>= addr
+ size
)
1512 if (p
->flags
& IORESOURCE_BUSY
&&
1513 p
->flags
& IORESOURCE_EXCLUSIVE
) {
1518 read_unlock(&resource_lock
);
1523 struct resource_entry
*resource_list_create_entry(struct resource
*res
,
1526 struct resource_entry
*entry
;
1528 entry
= kzalloc(sizeof(*entry
) + extra_size
, GFP_KERNEL
);
1530 INIT_LIST_HEAD(&entry
->node
);
1531 entry
->res
= res
? res
: &entry
->__res
;
1536 EXPORT_SYMBOL(resource_list_create_entry
);
1538 void resource_list_free(struct list_head
*head
)
1540 struct resource_entry
*entry
, *tmp
;
1542 list_for_each_entry_safe(entry
, tmp
, head
, node
)
1543 resource_list_destroy_entry(entry
);
1545 EXPORT_SYMBOL(resource_list_free
);
1547 static int __init
strict_iomem(char *str
)
1549 if (strstr(str
, "relaxed"))
1550 strict_iomem_checks
= 0;
1551 if (strstr(str
, "strict"))
1552 strict_iomem_checks
= 1;
1556 __setup("iomem=", strict_iomem
);