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", S_IRUSR
, 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 (resource_contains(&avail
, &alloc
)) {
615 new->start
= alloc
.start
;
616 new->end
= alloc
.end
;
621 next
: if (!this || this->end
== root
->end
)
625 tmp
.start
= this->end
+ 1;
626 this = this->sibling
;
632 * Find empty slot in the resource tree given range and alignment.
634 static int find_resource(struct resource
*root
, struct resource
*new,
635 resource_size_t size
,
636 struct resource_constraint
*constraint
)
638 return __find_resource(root
, NULL
, new, size
, constraint
);
642 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
643 * The resource will be relocated if the new size cannot be reallocated in the
646 * @root: root resource descriptor
647 * @old: resource descriptor desired by caller
648 * @newsize: new size of the resource descriptor
649 * @constraint: the size and alignment constraints to be met.
651 static int reallocate_resource(struct resource
*root
, struct resource
*old
,
652 resource_size_t newsize
,
653 struct resource_constraint
*constraint
)
656 struct resource
new = *old
;
657 struct resource
*conflict
;
659 write_lock(&resource_lock
);
661 if ((err
= __find_resource(root
, old
, &new, newsize
, constraint
)))
664 if (resource_contains(&new, old
)) {
665 old
->start
= new.start
;
675 if (resource_contains(old
, &new)) {
676 old
->start
= new.start
;
679 __release_resource(old
);
681 conflict
= __request_resource(root
, old
);
685 write_unlock(&resource_lock
);
691 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
692 * The resource will be reallocated with a new size if it was already allocated
693 * @root: root resource descriptor
694 * @new: resource descriptor desired by caller
695 * @size: requested resource region size
696 * @min: minimum boundary to allocate
697 * @max: maximum boundary to allocate
698 * @align: alignment requested, in bytes
699 * @alignf: alignment function, optional, called if not NULL
700 * @alignf_data: arbitrary data to pass to the @alignf function
702 int allocate_resource(struct resource
*root
, struct resource
*new,
703 resource_size_t size
, resource_size_t min
,
704 resource_size_t max
, resource_size_t align
,
705 resource_size_t (*alignf
)(void *,
706 const struct resource
*,
712 struct resource_constraint constraint
;
715 alignf
= simple_align_resource
;
717 constraint
.min
= min
;
718 constraint
.max
= max
;
719 constraint
.align
= align
;
720 constraint
.alignf
= alignf
;
721 constraint
.alignf_data
= alignf_data
;
724 /* resource is already allocated, try reallocating with
725 the new constraints */
726 return reallocate_resource(root
, new, size
, &constraint
);
729 write_lock(&resource_lock
);
730 err
= find_resource(root
, new, size
, &constraint
);
731 if (err
>= 0 && __request_resource(root
, new))
733 write_unlock(&resource_lock
);
737 EXPORT_SYMBOL(allocate_resource
);
740 * lookup_resource - find an existing resource by a resource start address
741 * @root: root resource descriptor
742 * @start: resource start address
744 * Returns a pointer to the resource if found, NULL otherwise
746 struct resource
*lookup_resource(struct resource
*root
, resource_size_t start
)
748 struct resource
*res
;
750 read_lock(&resource_lock
);
751 for (res
= root
->child
; res
; res
= res
->sibling
) {
752 if (res
->start
== start
)
755 read_unlock(&resource_lock
);
761 * Insert a resource into the resource tree. If successful, return NULL,
762 * otherwise return the conflicting resource (compare to __request_resource())
764 static struct resource
* __insert_resource(struct resource
*parent
, struct resource
*new)
766 struct resource
*first
, *next
;
768 for (;; parent
= first
) {
769 first
= __request_resource(parent
, new);
775 if (WARN_ON(first
== new)) /* duplicated insertion */
778 if ((first
->start
> new->start
) || (first
->end
< new->end
))
780 if ((first
->start
== new->start
) && (first
->end
== new->end
))
784 for (next
= first
; ; next
= next
->sibling
) {
785 /* Partial overlap? Bad, and unfixable */
786 if (next
->start
< new->start
|| next
->end
> new->end
)
790 if (next
->sibling
->start
> new->end
)
794 new->parent
= parent
;
795 new->sibling
= next
->sibling
;
798 next
->sibling
= NULL
;
799 for (next
= first
; next
; next
= next
->sibling
)
802 if (parent
->child
== first
) {
805 next
= parent
->child
;
806 while (next
->sibling
!= first
)
807 next
= next
->sibling
;
814 * insert_resource_conflict - Inserts resource in the resource tree
815 * @parent: parent of the new resource
816 * @new: new resource to insert
818 * Returns 0 on success, conflict resource if the resource can't be inserted.
820 * This function is equivalent to request_resource_conflict when no conflict
821 * happens. If a conflict happens, and the conflicting resources
822 * entirely fit within the range of the new resource, then the new
823 * resource is inserted and the conflicting resources become children of
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 int insert_resource(struct resource
*parent
, struct resource
*new)
845 struct resource
*conflict
;
847 conflict
= insert_resource_conflict(parent
, new);
848 return conflict
? -EBUSY
: 0;
852 * insert_resource_expand_to_fit - Insert a resource into the resource tree
853 * @root: root resource descriptor
854 * @new: new resource to insert
856 * Insert a resource into the resource tree, possibly expanding it in order
857 * to make it encompass any conflicting resources.
859 void insert_resource_expand_to_fit(struct resource
*root
, struct resource
*new)
864 write_lock(&resource_lock
);
866 struct resource
*conflict
;
868 conflict
= __insert_resource(root
, new);
871 if (conflict
== root
)
874 /* Ok, expand resource to cover the conflict, then try again .. */
875 if (conflict
->start
< new->start
)
876 new->start
= conflict
->start
;
877 if (conflict
->end
> new->end
)
878 new->end
= conflict
->end
;
880 printk("Expanded resource %s due to conflict with %s\n", new->name
, conflict
->name
);
882 write_unlock(&resource_lock
);
885 static int __adjust_resource(struct resource
*res
, resource_size_t start
,
886 resource_size_t size
)
888 struct resource
*tmp
, *parent
= res
->parent
;
889 resource_size_t end
= start
+ size
- 1;
895 if ((start
< parent
->start
) || (end
> parent
->end
))
898 if (res
->sibling
&& (res
->sibling
->start
<= end
))
903 while (tmp
->sibling
!= res
)
905 if (start
<= tmp
->end
)
910 for (tmp
= res
->child
; tmp
; tmp
= tmp
->sibling
)
911 if ((tmp
->start
< start
) || (tmp
->end
> end
))
923 * adjust_resource - modify a resource's start and size
924 * @res: resource to modify
925 * @start: new start value
928 * Given an existing resource, change its start and size to match the
929 * arguments. Returns 0 on success, -EBUSY if it can't fit.
930 * Existing children of the resource are assumed to be immutable.
932 int adjust_resource(struct resource
*res
, resource_size_t start
,
933 resource_size_t size
)
937 write_lock(&resource_lock
);
938 result
= __adjust_resource(res
, start
, size
);
939 write_unlock(&resource_lock
);
942 EXPORT_SYMBOL(adjust_resource
);
944 static void __init
__reserve_region_with_split(struct resource
*root
,
945 resource_size_t start
, resource_size_t end
,
948 struct resource
*parent
= root
;
949 struct resource
*conflict
;
950 struct resource
*res
= alloc_resource(GFP_ATOMIC
);
951 struct resource
*next_res
= NULL
;
959 res
->flags
= IORESOURCE_BUSY
;
963 conflict
= __request_resource(parent
, res
);
972 /* conflict covered whole area */
973 if (conflict
->start
<= res
->start
&&
974 conflict
->end
>= res
->end
) {
980 /* failed, split and try again */
981 if (conflict
->start
> res
->start
) {
983 res
->end
= conflict
->start
- 1;
984 if (conflict
->end
< end
) {
985 next_res
= alloc_resource(GFP_ATOMIC
);
990 next_res
->name
= name
;
991 next_res
->start
= conflict
->end
+ 1;
993 next_res
->flags
= IORESOURCE_BUSY
;
996 res
->start
= conflict
->end
+ 1;
1002 void __init
reserve_region_with_split(struct resource
*root
,
1003 resource_size_t start
, resource_size_t end
,
1008 write_lock(&resource_lock
);
1009 if (root
->start
> start
|| root
->end
< end
) {
1010 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1011 (unsigned long long)start
, (unsigned long long)end
,
1013 if (start
> root
->end
|| end
< root
->start
)
1016 if (end
> root
->end
)
1018 if (start
< root
->start
)
1019 start
= root
->start
;
1020 pr_err("fixing request to [0x%llx-0x%llx]\n",
1021 (unsigned long long)start
,
1022 (unsigned long long)end
);
1027 __reserve_region_with_split(root
, start
, end
, name
);
1028 write_unlock(&resource_lock
);
1032 * resource_alignment - calculate resource's alignment
1033 * @res: resource pointer
1035 * Returns alignment on success, 0 (invalid alignment) on failure.
1037 resource_size_t
resource_alignment(struct resource
*res
)
1039 switch (res
->flags
& (IORESOURCE_SIZEALIGN
| IORESOURCE_STARTALIGN
)) {
1040 case IORESOURCE_SIZEALIGN
:
1041 return resource_size(res
);
1042 case IORESOURCE_STARTALIGN
:
1050 * This is compatibility stuff for IO resources.
1052 * Note how this, unlike the above, knows about
1053 * the IO flag meanings (busy etc).
1055 * request_region creates a new busy region.
1057 * release_region releases a matching busy region.
1060 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait
);
1063 * __request_region - create a new busy resource region
1064 * @parent: parent resource descriptor
1065 * @start: resource start address
1066 * @n: resource region size
1067 * @name: reserving caller's ID string
1068 * @flags: IO resource flags
1070 struct resource
* __request_region(struct resource
*parent
,
1071 resource_size_t start
, resource_size_t n
,
1072 const char *name
, int flags
)
1074 DECLARE_WAITQUEUE(wait
, current
);
1075 struct resource
*res
= alloc_resource(GFP_KERNEL
);
1082 res
->end
= start
+ n
- 1;
1083 res
->flags
= resource_type(parent
);
1084 res
->flags
|= IORESOURCE_BUSY
| flags
;
1086 write_lock(&resource_lock
);
1089 struct resource
*conflict
;
1091 conflict
= __request_resource(parent
, res
);
1094 if (conflict
!= parent
) {
1095 if (!(conflict
->flags
& IORESOURCE_BUSY
)) {
1100 if (conflict
->flags
& flags
& IORESOURCE_MUXED
) {
1101 add_wait_queue(&muxed_resource_wait
, &wait
);
1102 write_unlock(&resource_lock
);
1103 set_current_state(TASK_UNINTERRUPTIBLE
);
1105 remove_wait_queue(&muxed_resource_wait
, &wait
);
1106 write_lock(&resource_lock
);
1109 /* Uhhuh, that didn't work out.. */
1114 write_unlock(&resource_lock
);
1117 EXPORT_SYMBOL(__request_region
);
1120 * __release_region - release a previously reserved resource region
1121 * @parent: parent resource descriptor
1122 * @start: resource start address
1123 * @n: resource region size
1125 * The described resource region must match a currently busy region.
1127 void __release_region(struct resource
*parent
, resource_size_t start
,
1130 struct resource
**p
;
1131 resource_size_t end
;
1134 end
= start
+ n
- 1;
1136 write_lock(&resource_lock
);
1139 struct resource
*res
= *p
;
1143 if (res
->start
<= start
&& res
->end
>= end
) {
1144 if (!(res
->flags
& IORESOURCE_BUSY
)) {
1148 if (res
->start
!= start
|| res
->end
!= end
)
1151 write_unlock(&resource_lock
);
1152 if (res
->flags
& IORESOURCE_MUXED
)
1153 wake_up(&muxed_resource_wait
);
1160 write_unlock(&resource_lock
);
1162 printk(KERN_WARNING
"Trying to free nonexistent resource "
1163 "<%016llx-%016llx>\n", (unsigned long long)start
,
1164 (unsigned long long)end
);
1166 EXPORT_SYMBOL(__release_region
);
1168 #ifdef CONFIG_MEMORY_HOTREMOVE
1170 * release_mem_region_adjustable - release a previously reserved memory region
1171 * @parent: parent resource descriptor
1172 * @start: resource start address
1173 * @size: resource region size
1175 * This interface is intended for memory hot-delete. The requested region
1176 * is released from a currently busy memory resource. The requested region
1177 * must either match exactly or fit into a single busy resource entry. In
1178 * the latter case, the remaining resource is adjusted accordingly.
1179 * Existing children of the busy memory resource must be immutable in the
1183 * - Additional release conditions, such as overlapping region, can be
1184 * supported after they are confirmed as valid cases.
1185 * - When a busy memory resource gets split into two entries, the code
1186 * assumes that all children remain in the lower address entry for
1187 * simplicity. Enhance this logic when necessary.
1189 int release_mem_region_adjustable(struct resource
*parent
,
1190 resource_size_t start
, resource_size_t size
)
1192 struct resource
**p
;
1193 struct resource
*res
;
1194 struct resource
*new_res
;
1195 resource_size_t end
;
1198 end
= start
+ size
- 1;
1199 if ((start
< parent
->start
) || (end
> parent
->end
))
1202 /* The alloc_resource() result gets checked later */
1203 new_res
= alloc_resource(GFP_KERNEL
);
1206 write_lock(&resource_lock
);
1208 while ((res
= *p
)) {
1209 if (res
->start
>= end
)
1212 /* look for the next resource if it does not fit into */
1213 if (res
->start
> start
|| res
->end
< end
) {
1218 if (!(res
->flags
& IORESOURCE_MEM
))
1221 if (!(res
->flags
& IORESOURCE_BUSY
)) {
1226 /* found the target resource; let's adjust accordingly */
1227 if (res
->start
== start
&& res
->end
== end
) {
1228 /* free the whole entry */
1232 } else if (res
->start
== start
&& res
->end
!= end
) {
1233 /* adjust the start */
1234 ret
= __adjust_resource(res
, end
+ 1,
1236 } else if (res
->start
!= start
&& res
->end
== end
) {
1237 /* adjust the end */
1238 ret
= __adjust_resource(res
, res
->start
,
1239 start
- res
->start
);
1241 /* split into two entries */
1246 new_res
->name
= res
->name
;
1247 new_res
->start
= end
+ 1;
1248 new_res
->end
= res
->end
;
1249 new_res
->flags
= res
->flags
;
1250 new_res
->parent
= res
->parent
;
1251 new_res
->sibling
= res
->sibling
;
1252 new_res
->child
= NULL
;
1254 ret
= __adjust_resource(res
, res
->start
,
1255 start
- res
->start
);
1258 res
->sibling
= new_res
;
1265 write_unlock(&resource_lock
);
1266 free_resource(new_res
);
1269 #endif /* CONFIG_MEMORY_HOTREMOVE */
1272 * Managed region resource
1274 static void devm_resource_release(struct device
*dev
, void *ptr
)
1276 struct resource
**r
= ptr
;
1278 release_resource(*r
);
1282 * devm_request_resource() - request and reserve an I/O or memory resource
1283 * @dev: device for which to request the resource
1284 * @root: root of the resource tree from which to request the resource
1285 * @new: descriptor of the resource to request
1287 * This is a device-managed version of request_resource(). There is usually
1288 * no need to release resources requested by this function explicitly since
1289 * that will be taken care of when the device is unbound from its driver.
1290 * If for some reason the resource needs to be released explicitly, because
1291 * of ordering issues for example, drivers must call devm_release_resource()
1292 * rather than the regular release_resource().
1294 * When a conflict is detected between any existing resources and the newly
1295 * requested resource, an error message will be printed.
1297 * Returns 0 on success or a negative error code on failure.
1299 int devm_request_resource(struct device
*dev
, struct resource
*root
,
1300 struct resource
*new)
1302 struct resource
*conflict
, **ptr
;
1304 ptr
= devres_alloc(devm_resource_release
, sizeof(*ptr
), GFP_KERNEL
);
1310 conflict
= request_resource_conflict(root
, new);
1312 dev_err(dev
, "resource collision: %pR conflicts with %s %pR\n",
1313 new, conflict
->name
, conflict
);
1318 devres_add(dev
, ptr
);
1321 EXPORT_SYMBOL(devm_request_resource
);
1323 static int devm_resource_match(struct device
*dev
, void *res
, void *data
)
1325 struct resource
**ptr
= res
;
1327 return *ptr
== data
;
1331 * devm_release_resource() - release a previously requested resource
1332 * @dev: device for which to release the resource
1333 * @new: descriptor of the resource to release
1335 * Releases a resource previously requested using devm_request_resource().
1337 void devm_release_resource(struct device
*dev
, struct resource
*new)
1339 WARN_ON(devres_release(dev
, devm_resource_release
, devm_resource_match
,
1342 EXPORT_SYMBOL(devm_release_resource
);
1344 struct region_devres
{
1345 struct resource
*parent
;
1346 resource_size_t start
;
1350 static void devm_region_release(struct device
*dev
, void *res
)
1352 struct region_devres
*this = res
;
1354 __release_region(this->parent
, this->start
, this->n
);
1357 static int devm_region_match(struct device
*dev
, void *res
, void *match_data
)
1359 struct region_devres
*this = res
, *match
= match_data
;
1361 return this->parent
== match
->parent
&&
1362 this->start
== match
->start
&& this->n
== match
->n
;
1365 struct resource
* __devm_request_region(struct device
*dev
,
1366 struct resource
*parent
, resource_size_t start
,
1367 resource_size_t n
, const char *name
)
1369 struct region_devres
*dr
= NULL
;
1370 struct resource
*res
;
1372 dr
= devres_alloc(devm_region_release
, sizeof(struct region_devres
),
1377 dr
->parent
= parent
;
1381 res
= __request_region(parent
, start
, n
, name
, 0);
1383 devres_add(dev
, dr
);
1389 EXPORT_SYMBOL(__devm_request_region
);
1391 void __devm_release_region(struct device
*dev
, struct resource
*parent
,
1392 resource_size_t start
, resource_size_t n
)
1394 struct region_devres match_data
= { parent
, start
, n
};
1396 __release_region(parent
, start
, n
);
1397 WARN_ON(devres_destroy(dev
, devm_region_release
, devm_region_match
,
1400 EXPORT_SYMBOL(__devm_release_region
);
1403 * Called from init/main.c to reserve IO ports.
1405 #define MAXRESERVE 4
1406 static int __init
reserve_setup(char *str
)
1408 static int reserved
;
1409 static struct resource reserve
[MAXRESERVE
];
1412 unsigned int io_start
, io_num
;
1415 if (get_option (&str
, &io_start
) != 2)
1417 if (get_option (&str
, &io_num
) == 0)
1419 if (x
< MAXRESERVE
) {
1420 struct resource
*res
= reserve
+ x
;
1421 res
->name
= "reserved";
1422 res
->start
= io_start
;
1423 res
->end
= io_start
+ io_num
- 1;
1424 res
->flags
= IORESOURCE_BUSY
;
1426 if (request_resource(res
->start
>= 0x10000 ? &iomem_resource
: &ioport_resource
, res
) == 0)
1433 __setup("reserve=", reserve_setup
);
1436 * Check if the requested addr and size spans more than any slot in the
1437 * iomem resource tree.
1439 int iomem_map_sanity_check(resource_size_t addr
, unsigned long size
)
1441 struct resource
*p
= &iomem_resource
;
1445 read_lock(&resource_lock
);
1446 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
1448 * We can probably skip the resources without
1449 * IORESOURCE_IO attribute?
1451 if (p
->start
>= addr
+ size
)
1455 if (PFN_DOWN(p
->start
) <= PFN_DOWN(addr
) &&
1456 PFN_DOWN(p
->end
) >= PFN_DOWN(addr
+ size
- 1))
1459 * if a resource is "BUSY", it's not a hardware resource
1460 * but a driver mapping of such a resource; we don't want
1461 * to warn for those; some drivers legitimately map only
1462 * partial hardware resources. (example: vesafb)
1464 if (p
->flags
& IORESOURCE_BUSY
)
1467 printk(KERN_WARNING
"resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1468 (unsigned long long)addr
,
1469 (unsigned long long)(addr
+ size
- 1),
1474 read_unlock(&resource_lock
);
1479 #ifdef CONFIG_STRICT_DEVMEM
1480 static int strict_iomem_checks
= 1;
1482 static int strict_iomem_checks
;
1486 * check if an address is reserved in the iomem resource tree
1487 * returns 1 if reserved, 0 if not reserved.
1489 int iomem_is_exclusive(u64 addr
)
1491 struct resource
*p
= &iomem_resource
;
1494 int size
= PAGE_SIZE
;
1496 if (!strict_iomem_checks
)
1499 addr
= addr
& PAGE_MASK
;
1501 read_lock(&resource_lock
);
1502 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
1504 * We can probably skip the resources without
1505 * IORESOURCE_IO attribute?
1507 if (p
->start
>= addr
+ size
)
1511 if (p
->flags
& IORESOURCE_BUSY
&&
1512 p
->flags
& IORESOURCE_EXCLUSIVE
) {
1517 read_unlock(&resource_lock
);
1522 struct resource_entry
*resource_list_create_entry(struct resource
*res
,
1525 struct resource_entry
*entry
;
1527 entry
= kzalloc(sizeof(*entry
) + extra_size
, GFP_KERNEL
);
1529 INIT_LIST_HEAD(&entry
->node
);
1530 entry
->res
= res
? res
: &entry
->__res
;
1535 EXPORT_SYMBOL(resource_list_create_entry
);
1537 void resource_list_free(struct list_head
*head
)
1539 struct resource_entry
*entry
, *tmp
;
1541 list_for_each_entry_safe(entry
, tmp
, head
, node
)
1542 resource_list_destroy_entry(entry
);
1544 EXPORT_SYMBOL(resource_list_free
);
1546 static int __init
strict_iomem(char *str
)
1548 if (strstr(str
, "relaxed"))
1549 strict_iomem_checks
= 0;
1550 if (strstr(str
, "strict"))
1551 strict_iomem_checks
= 1;
1555 __setup("iomem=", strict_iomem
);