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>
27 struct resource ioport_resource
= {
30 .end
= IO_SPACE_LIMIT
,
31 .flags
= IORESOURCE_IO
,
33 EXPORT_SYMBOL(ioport_resource
);
35 struct resource iomem_resource
= {
39 .flags
= IORESOURCE_MEM
,
41 EXPORT_SYMBOL(iomem_resource
);
43 /* constraints to be met while allocating resources */
44 struct resource_constraint
{
45 resource_size_t min
, max
, align
;
46 resource_size_t (*alignf
)(void *, const struct resource
*,
47 resource_size_t
, resource_size_t
);
51 static DEFINE_RWLOCK(resource_lock
);
53 static void *r_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
55 struct resource
*p
= v
;
59 while (!p
->sibling
&& p
->parent
)
66 enum { MAX_IORES_LEVEL
= 5 };
68 static void *r_start(struct seq_file
*m
, loff_t
*pos
)
69 __acquires(resource_lock
)
71 struct resource
*p
= m
->private;
73 read_lock(&resource_lock
);
74 for (p
= p
->child
; p
&& l
< *pos
; p
= r_next(m
, p
, &l
))
79 static void r_stop(struct seq_file
*m
, void *v
)
80 __releases(resource_lock
)
82 read_unlock(&resource_lock
);
85 static int r_show(struct seq_file
*m
, void *v
)
87 struct resource
*root
= m
->private;
88 struct resource
*r
= v
, *p
;
89 int width
= root
->end
< 0x10000 ? 4 : 8;
92 for (depth
= 0, p
= r
; depth
< MAX_IORES_LEVEL
; depth
++, p
= p
->parent
)
93 if (p
->parent
== root
)
95 seq_printf(m
, "%*s%0*llx-%0*llx : %s\n",
97 width
, (unsigned long long) r
->start
,
98 width
, (unsigned long long) r
->end
,
99 r
->name
? r
->name
: "<BAD>");
103 static const struct seq_operations resource_op
= {
110 static int ioports_open(struct inode
*inode
, struct file
*file
)
112 int res
= seq_open(file
, &resource_op
);
114 struct seq_file
*m
= file
->private_data
;
115 m
->private = &ioport_resource
;
120 static int iomem_open(struct inode
*inode
, struct file
*file
)
122 int res
= seq_open(file
, &resource_op
);
124 struct seq_file
*m
= file
->private_data
;
125 m
->private = &iomem_resource
;
130 static const struct file_operations proc_ioports_operations
= {
131 .open
= ioports_open
,
134 .release
= seq_release
,
137 static const struct file_operations proc_iomem_operations
= {
141 .release
= seq_release
,
144 static int __init
ioresources_init(void)
146 proc_create("ioports", 0, NULL
, &proc_ioports_operations
);
147 proc_create("iomem", 0, NULL
, &proc_iomem_operations
);
150 __initcall(ioresources_init
);
152 #endif /* CONFIG_PROC_FS */
154 /* Return the conflict entry if you can't request it */
155 static struct resource
* __request_resource(struct resource
*root
, struct resource
*new)
157 resource_size_t start
= new->start
;
158 resource_size_t end
= new->end
;
159 struct resource
*tmp
, **p
;
163 if (start
< root
->start
)
170 if (!tmp
|| tmp
->start
> end
) {
177 if (tmp
->end
< start
)
183 static int __release_resource(struct resource
*old
)
185 struct resource
*tmp
, **p
;
187 p
= &old
->parent
->child
;
202 static void __release_child_resources(struct resource
*r
)
204 struct resource
*tmp
, *p
;
205 resource_size_t size
;
215 __release_child_resources(tmp
);
217 printk(KERN_DEBUG
"release child resource %pR\n", tmp
);
218 /* need to restore size, and keep flags */
219 size
= resource_size(tmp
);
225 void release_child_resources(struct resource
*r
)
227 write_lock(&resource_lock
);
228 __release_child_resources(r
);
229 write_unlock(&resource_lock
);
233 * request_resource_conflict - request and reserve an I/O or memory resource
234 * @root: root resource descriptor
235 * @new: resource descriptor desired by caller
237 * Returns 0 for success, conflict resource on error.
239 struct resource
*request_resource_conflict(struct resource
*root
, struct resource
*new)
241 struct resource
*conflict
;
243 write_lock(&resource_lock
);
244 conflict
= __request_resource(root
, new);
245 write_unlock(&resource_lock
);
250 * request_resource - request and reserve an I/O or memory resource
251 * @root: root resource descriptor
252 * @new: resource descriptor desired by caller
254 * Returns 0 for success, negative error code on error.
256 int request_resource(struct resource
*root
, struct resource
*new)
258 struct resource
*conflict
;
260 conflict
= request_resource_conflict(root
, new);
261 return conflict
? -EBUSY
: 0;
264 EXPORT_SYMBOL(request_resource
);
267 * release_resource - release a previously reserved resource
268 * @old: resource pointer
270 int release_resource(struct resource
*old
)
274 write_lock(&resource_lock
);
275 retval
= __release_resource(old
);
276 write_unlock(&resource_lock
);
280 EXPORT_SYMBOL(release_resource
);
282 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
284 * Finds the lowest memory reosurce exists within [res->start.res->end)
285 * the caller must specify res->start, res->end, res->flags and "name".
286 * If found, returns 0, res is overwritten, if not found, returns -1.
288 static int find_next_system_ram(struct resource
*res
, char *name
)
290 resource_size_t start
, end
;
297 BUG_ON(start
>= end
);
299 read_lock(&resource_lock
);
300 for (p
= iomem_resource
.child
; p
; p
= p
->sibling
) {
301 /* system ram is just marked as IORESOURCE_MEM */
302 if (p
->flags
!= res
->flags
)
304 if (name
&& strcmp(p
->name
, name
))
306 if (p
->start
> end
) {
310 if ((p
->end
>= start
) && (p
->start
< end
))
313 read_unlock(&resource_lock
);
317 if (res
->start
< p
->start
)
318 res
->start
= p
->start
;
319 if (res
->end
> p
->end
)
325 * This function calls callback against all memory range of "System RAM"
326 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
327 * Now, this function is only for "System RAM".
329 int walk_system_ram_range(unsigned long start_pfn
, unsigned long nr_pages
,
330 void *arg
, int (*func
)(unsigned long, unsigned long, void *))
333 unsigned long pfn
, end_pfn
;
337 res
.start
= (u64
) start_pfn
<< PAGE_SHIFT
;
338 res
.end
= ((u64
)(start_pfn
+ nr_pages
) << PAGE_SHIFT
) - 1;
339 res
.flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
341 while ((res
.start
< res
.end
) &&
342 (find_next_system_ram(&res
, "System RAM") >= 0)) {
343 pfn
= (res
.start
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
344 end_pfn
= (res
.end
+ 1) >> PAGE_SHIFT
;
346 ret
= (*func
)(pfn
, end_pfn
- pfn
, arg
);
349 res
.start
= res
.end
+ 1;
357 static int __is_ram(unsigned long pfn
, unsigned long nr_pages
, void *arg
)
362 * This generic page_is_ram() returns true if specified address is
363 * registered as "System RAM" in iomem_resource list.
365 int __weak
page_is_ram(unsigned long pfn
)
367 return walk_system_ram_range(pfn
, 1, NULL
, __is_ram
) == 1;
370 void __weak
arch_remove_reservations(struct resource
*avail
)
374 static resource_size_t
simple_align_resource(void *data
,
375 const struct resource
*avail
,
376 resource_size_t size
,
377 resource_size_t align
)
382 static void resource_clip(struct resource
*res
, resource_size_t min
,
385 if (res
->start
< min
)
391 static bool resource_contains(struct resource
*res1
, struct resource
*res2
)
393 return res1
->start
<= res2
->start
&& res1
->end
>= res2
->end
;
397 * Find empty slot in the resource tree with the given range and
398 * alignment constraints
400 static int __find_resource(struct resource
*root
, struct resource
*old
,
401 struct resource
*new,
402 resource_size_t size
,
403 struct resource_constraint
*constraint
)
405 struct resource
*this = root
->child
;
406 struct resource tmp
= *new, avail
, alloc
;
408 tmp
.flags
= new->flags
;
409 tmp
.start
= root
->start
;
411 * Skip past an allocated resource that starts at 0, since the assignment
412 * of this->start - 1 to tmp->end below would cause an underflow.
414 if (this && this->start
== root
->start
) {
415 tmp
.start
= (this == old
) ? old
->start
: this->end
+ 1;
416 this = this->sibling
;
420 tmp
.end
= (this == old
) ? this->end
: this->start
- 1;
424 if (tmp
.end
< tmp
.start
)
427 resource_clip(&tmp
, constraint
->min
, constraint
->max
);
428 arch_remove_reservations(&tmp
);
430 /* Check for overflow after ALIGN() */
432 avail
.start
= ALIGN(tmp
.start
, constraint
->align
);
434 if (avail
.start
>= tmp
.start
) {
435 alloc
.start
= constraint
->alignf(constraint
->alignf_data
, &avail
,
436 size
, constraint
->align
);
437 alloc
.end
= alloc
.start
+ size
- 1;
438 if (resource_contains(&avail
, &alloc
)) {
439 new->start
= alloc
.start
;
440 new->end
= alloc
.end
;
445 next
: if (!this || this->end
== root
->end
)
449 tmp
.start
= this->end
+ 1;
450 this = this->sibling
;
456 * Find empty slot in the resource tree given range and alignment.
458 static int find_resource(struct resource
*root
, struct resource
*new,
459 resource_size_t size
,
460 struct resource_constraint
*constraint
)
462 return __find_resource(root
, NULL
, new, size
, constraint
);
466 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
467 * The resource will be relocated if the new size cannot be reallocated in the
470 * @root: root resource descriptor
471 * @old: resource descriptor desired by caller
472 * @newsize: new size of the resource descriptor
473 * @constraint: the size and alignment constraints to be met.
475 int reallocate_resource(struct resource
*root
, struct resource
*old
,
476 resource_size_t newsize
,
477 struct resource_constraint
*constraint
)
480 struct resource
new = *old
;
481 struct resource
*conflict
;
483 write_lock(&resource_lock
);
485 if ((err
= __find_resource(root
, old
, &new, newsize
, constraint
)))
488 if (resource_contains(&new, old
)) {
489 old
->start
= new.start
;
499 if (resource_contains(old
, &new)) {
500 old
->start
= new.start
;
503 __release_resource(old
);
505 conflict
= __request_resource(root
, old
);
509 write_unlock(&resource_lock
);
515 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
516 * The resource will be reallocated with a new size if it was already allocated
517 * @root: root resource descriptor
518 * @new: resource descriptor desired by caller
519 * @size: requested resource region size
520 * @min: minimum boundary to allocate
521 * @max: maximum boundary to allocate
522 * @align: alignment requested, in bytes
523 * @alignf: alignment function, optional, called if not NULL
524 * @alignf_data: arbitrary data to pass to the @alignf function
526 int allocate_resource(struct resource
*root
, struct resource
*new,
527 resource_size_t size
, resource_size_t min
,
528 resource_size_t max
, resource_size_t align
,
529 resource_size_t (*alignf
)(void *,
530 const struct resource
*,
536 struct resource_constraint constraint
;
539 alignf
= simple_align_resource
;
541 constraint
.min
= min
;
542 constraint
.max
= max
;
543 constraint
.align
= align
;
544 constraint
.alignf
= alignf
;
545 constraint
.alignf_data
= alignf_data
;
548 /* resource is already allocated, try reallocating with
549 the new constraints */
550 return reallocate_resource(root
, new, size
, &constraint
);
553 write_lock(&resource_lock
);
554 err
= find_resource(root
, new, size
, &constraint
);
555 if (err
>= 0 && __request_resource(root
, new))
557 write_unlock(&resource_lock
);
561 EXPORT_SYMBOL(allocate_resource
);
564 * lookup_resource - find an existing resource by a resource start address
565 * @root: root resource descriptor
566 * @start: resource start address
568 * Returns a pointer to the resource if found, NULL otherwise
570 struct resource
*lookup_resource(struct resource
*root
, resource_size_t start
)
572 struct resource
*res
;
574 read_lock(&resource_lock
);
575 for (res
= root
->child
; res
; res
= res
->sibling
) {
576 if (res
->start
== start
)
579 read_unlock(&resource_lock
);
585 * Insert a resource into the resource tree. If successful, return NULL,
586 * otherwise return the conflicting resource (compare to __request_resource())
588 static struct resource
* __insert_resource(struct resource
*parent
, struct resource
*new)
590 struct resource
*first
, *next
;
592 for (;; parent
= first
) {
593 first
= __request_resource(parent
, new);
599 if (WARN_ON(first
== new)) /* duplicated insertion */
602 if ((first
->start
> new->start
) || (first
->end
< new->end
))
604 if ((first
->start
== new->start
) && (first
->end
== new->end
))
608 for (next
= first
; ; next
= next
->sibling
) {
609 /* Partial overlap? Bad, and unfixable */
610 if (next
->start
< new->start
|| next
->end
> new->end
)
614 if (next
->sibling
->start
> new->end
)
618 new->parent
= parent
;
619 new->sibling
= next
->sibling
;
622 next
->sibling
= NULL
;
623 for (next
= first
; next
; next
= next
->sibling
)
626 if (parent
->child
== first
) {
629 next
= parent
->child
;
630 while (next
->sibling
!= first
)
631 next
= next
->sibling
;
638 * insert_resource_conflict - Inserts resource in the resource tree
639 * @parent: parent of the new resource
640 * @new: new resource to insert
642 * Returns 0 on success, conflict resource if the resource can't be inserted.
644 * This function is equivalent to request_resource_conflict when no conflict
645 * happens. If a conflict happens, and the conflicting resources
646 * entirely fit within the range of the new resource, then the new
647 * resource is inserted and the conflicting resources become children of
650 struct resource
*insert_resource_conflict(struct resource
*parent
, struct resource
*new)
652 struct resource
*conflict
;
654 write_lock(&resource_lock
);
655 conflict
= __insert_resource(parent
, new);
656 write_unlock(&resource_lock
);
661 * insert_resource - Inserts a resource in the resource tree
662 * @parent: parent of the new resource
663 * @new: new resource to insert
665 * Returns 0 on success, -EBUSY if the resource can't be inserted.
667 int insert_resource(struct resource
*parent
, struct resource
*new)
669 struct resource
*conflict
;
671 conflict
= insert_resource_conflict(parent
, new);
672 return conflict
? -EBUSY
: 0;
676 * insert_resource_expand_to_fit - Insert a resource into the resource tree
677 * @root: root resource descriptor
678 * @new: new resource to insert
680 * Insert a resource into the resource tree, possibly expanding it in order
681 * to make it encompass any conflicting resources.
683 void insert_resource_expand_to_fit(struct resource
*root
, struct resource
*new)
688 write_lock(&resource_lock
);
690 struct resource
*conflict
;
692 conflict
= __insert_resource(root
, new);
695 if (conflict
== root
)
698 /* Ok, expand resource to cover the conflict, then try again .. */
699 if (conflict
->start
< new->start
)
700 new->start
= conflict
->start
;
701 if (conflict
->end
> new->end
)
702 new->end
= conflict
->end
;
704 printk("Expanded resource %s due to conflict with %s\n", new->name
, conflict
->name
);
706 write_unlock(&resource_lock
);
709 static int __adjust_resource(struct resource
*res
, resource_size_t start
,
710 resource_size_t size
)
712 struct resource
*tmp
, *parent
= res
->parent
;
713 resource_size_t end
= start
+ size
- 1;
719 if ((start
< parent
->start
) || (end
> parent
->end
))
722 if (res
->sibling
&& (res
->sibling
->start
<= end
))
727 while (tmp
->sibling
!= res
)
729 if (start
<= tmp
->end
)
734 for (tmp
= res
->child
; tmp
; tmp
= tmp
->sibling
)
735 if ((tmp
->start
< start
) || (tmp
->end
> end
))
747 * adjust_resource - modify a resource's start and size
748 * @res: resource to modify
749 * @start: new start value
752 * Given an existing resource, change its start and size to match the
753 * arguments. Returns 0 on success, -EBUSY if it can't fit.
754 * Existing children of the resource are assumed to be immutable.
756 int adjust_resource(struct resource
*res
, resource_size_t start
,
757 resource_size_t size
)
761 write_lock(&resource_lock
);
762 result
= __adjust_resource(res
, start
, size
);
763 write_unlock(&resource_lock
);
766 EXPORT_SYMBOL(adjust_resource
);
768 static void __init
__reserve_region_with_split(struct resource
*root
,
769 resource_size_t start
, resource_size_t end
,
772 struct resource
*parent
= root
;
773 struct resource
*conflict
;
774 struct resource
*res
= kzalloc(sizeof(*res
), GFP_ATOMIC
);
775 struct resource
*next_res
= NULL
;
783 res
->flags
= IORESOURCE_BUSY
;
787 conflict
= __request_resource(parent
, res
);
796 /* conflict covered whole area */
797 if (conflict
->start
<= res
->start
&&
798 conflict
->end
>= res
->end
) {
804 /* failed, split and try again */
805 if (conflict
->start
> res
->start
) {
807 res
->end
= conflict
->start
- 1;
808 if (conflict
->end
< end
) {
809 next_res
= kzalloc(sizeof(*next_res
),
815 next_res
->name
= name
;
816 next_res
->start
= conflict
->end
+ 1;
818 next_res
->flags
= IORESOURCE_BUSY
;
821 res
->start
= conflict
->end
+ 1;
827 void __init
reserve_region_with_split(struct resource
*root
,
828 resource_size_t start
, resource_size_t end
,
833 write_lock(&resource_lock
);
834 if (root
->start
> start
|| root
->end
< end
) {
835 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
836 (unsigned long long)start
, (unsigned long long)end
,
838 if (start
> root
->end
|| end
< root
->start
)
843 if (start
< root
->start
)
845 pr_err("fixing request to [0x%llx-0x%llx]\n",
846 (unsigned long long)start
,
847 (unsigned long long)end
);
852 __reserve_region_with_split(root
, start
, end
, name
);
853 write_unlock(&resource_lock
);
857 * resource_alignment - calculate resource's alignment
858 * @res: resource pointer
860 * Returns alignment on success, 0 (invalid alignment) on failure.
862 resource_size_t
resource_alignment(struct resource
*res
)
864 switch (res
->flags
& (IORESOURCE_SIZEALIGN
| IORESOURCE_STARTALIGN
)) {
865 case IORESOURCE_SIZEALIGN
:
866 return resource_size(res
);
867 case IORESOURCE_STARTALIGN
:
875 * This is compatibility stuff for IO resources.
877 * Note how this, unlike the above, knows about
878 * the IO flag meanings (busy etc).
880 * request_region creates a new busy region.
882 * check_region returns non-zero if the area is already busy.
884 * release_region releases a matching busy region.
887 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait
);
890 * __request_region - create a new busy resource region
891 * @parent: parent resource descriptor
892 * @start: resource start address
893 * @n: resource region size
894 * @name: reserving caller's ID string
895 * @flags: IO resource flags
897 struct resource
* __request_region(struct resource
*parent
,
898 resource_size_t start
, resource_size_t n
,
899 const char *name
, int flags
)
901 DECLARE_WAITQUEUE(wait
, current
);
902 struct resource
*res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
909 res
->end
= start
+ n
- 1;
910 res
->flags
= IORESOURCE_BUSY
;
913 write_lock(&resource_lock
);
916 struct resource
*conflict
;
918 conflict
= __request_resource(parent
, res
);
921 if (conflict
!= parent
) {
923 if (!(conflict
->flags
& IORESOURCE_BUSY
))
926 if (conflict
->flags
& flags
& IORESOURCE_MUXED
) {
927 add_wait_queue(&muxed_resource_wait
, &wait
);
928 write_unlock(&resource_lock
);
929 set_current_state(TASK_UNINTERRUPTIBLE
);
931 remove_wait_queue(&muxed_resource_wait
, &wait
);
932 write_lock(&resource_lock
);
935 /* Uhhuh, that didn't work out.. */
940 write_unlock(&resource_lock
);
943 EXPORT_SYMBOL(__request_region
);
946 * __check_region - check if a resource region is busy or free
947 * @parent: parent resource descriptor
948 * @start: resource start address
949 * @n: resource region size
951 * Returns 0 if the region is free at the moment it is checked,
952 * returns %-EBUSY if the region is busy.
955 * This function is deprecated because its use is racy.
956 * Even if it returns 0, a subsequent call to request_region()
957 * may fail because another driver etc. just allocated the region.
958 * Do NOT use it. It will be removed from the kernel.
960 int __check_region(struct resource
*parent
, resource_size_t start
,
963 struct resource
* res
;
965 res
= __request_region(parent
, start
, n
, "check-region", 0);
969 release_resource(res
);
973 EXPORT_SYMBOL(__check_region
);
976 * __release_region - release a previously reserved resource region
977 * @parent: parent resource descriptor
978 * @start: resource start address
979 * @n: resource region size
981 * The described resource region must match a currently busy region.
983 void __release_region(struct resource
*parent
, resource_size_t start
,
992 write_lock(&resource_lock
);
995 struct resource
*res
= *p
;
999 if (res
->start
<= start
&& res
->end
>= end
) {
1000 if (!(res
->flags
& IORESOURCE_BUSY
)) {
1004 if (res
->start
!= start
|| res
->end
!= end
)
1007 write_unlock(&resource_lock
);
1008 if (res
->flags
& IORESOURCE_MUXED
)
1009 wake_up(&muxed_resource_wait
);
1016 write_unlock(&resource_lock
);
1018 printk(KERN_WARNING
"Trying to free nonexistent resource "
1019 "<%016llx-%016llx>\n", (unsigned long long)start
,
1020 (unsigned long long)end
);
1022 EXPORT_SYMBOL(__release_region
);
1024 #ifdef CONFIG_MEMORY_HOTREMOVE
1026 * release_mem_region_adjustable - release a previously reserved memory region
1027 * @parent: parent resource descriptor
1028 * @start: resource start address
1029 * @size: resource region size
1031 * This interface is intended for memory hot-delete. The requested region
1032 * is released from a currently busy memory resource. The requested region
1033 * must either match exactly or fit into a single busy resource entry. In
1034 * the latter case, the remaining resource is adjusted accordingly.
1035 * Existing children of the busy memory resource must be immutable in the
1039 * - Additional release conditions, such as overlapping region, can be
1040 * supported after they are confirmed as valid cases.
1041 * - When a busy memory resource gets split into two entries, the code
1042 * assumes that all children remain in the lower address entry for
1043 * simplicity. Enhance this logic when necessary.
1045 int release_mem_region_adjustable(struct resource
*parent
,
1046 resource_size_t start
, resource_size_t size
)
1048 struct resource
**p
;
1049 struct resource
*res
;
1050 struct resource
*new_res
;
1051 resource_size_t end
;
1054 end
= start
+ size
- 1;
1055 if ((start
< parent
->start
) || (end
> parent
->end
))
1058 /* The kzalloc() result gets checked later */
1059 new_res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
1062 write_lock(&resource_lock
);
1064 while ((res
= *p
)) {
1065 if (res
->start
>= end
)
1068 /* look for the next resource if it does not fit into */
1069 if (res
->start
> start
|| res
->end
< end
) {
1074 if (!(res
->flags
& IORESOURCE_MEM
))
1077 if (!(res
->flags
& IORESOURCE_BUSY
)) {
1082 /* found the target resource; let's adjust accordingly */
1083 if (res
->start
== start
&& res
->end
== end
) {
1084 /* free the whole entry */
1088 } else if (res
->start
== start
&& res
->end
!= end
) {
1089 /* adjust the start */
1090 ret
= __adjust_resource(res
, end
+ 1,
1092 } else if (res
->start
!= start
&& res
->end
== end
) {
1093 /* adjust the end */
1094 ret
= __adjust_resource(res
, res
->start
,
1095 start
- res
->start
);
1097 /* split into two entries */
1102 new_res
->name
= res
->name
;
1103 new_res
->start
= end
+ 1;
1104 new_res
->end
= res
->end
;
1105 new_res
->flags
= res
->flags
;
1106 new_res
->parent
= res
->parent
;
1107 new_res
->sibling
= res
->sibling
;
1108 new_res
->child
= NULL
;
1110 ret
= __adjust_resource(res
, res
->start
,
1111 start
- res
->start
);
1114 res
->sibling
= new_res
;
1121 write_unlock(&resource_lock
);
1125 #endif /* CONFIG_MEMORY_HOTREMOVE */
1128 * Managed region resource
1130 struct region_devres
{
1131 struct resource
*parent
;
1132 resource_size_t start
;
1136 static void devm_region_release(struct device
*dev
, void *res
)
1138 struct region_devres
*this = res
;
1140 __release_region(this->parent
, this->start
, this->n
);
1143 static int devm_region_match(struct device
*dev
, void *res
, void *match_data
)
1145 struct region_devres
*this = res
, *match
= match_data
;
1147 return this->parent
== match
->parent
&&
1148 this->start
== match
->start
&& this->n
== match
->n
;
1151 struct resource
* __devm_request_region(struct device
*dev
,
1152 struct resource
*parent
, resource_size_t start
,
1153 resource_size_t n
, const char *name
)
1155 struct region_devres
*dr
= NULL
;
1156 struct resource
*res
;
1158 dr
= devres_alloc(devm_region_release
, sizeof(struct region_devres
),
1163 dr
->parent
= parent
;
1167 res
= __request_region(parent
, start
, n
, name
, 0);
1169 devres_add(dev
, dr
);
1175 EXPORT_SYMBOL(__devm_request_region
);
1177 void __devm_release_region(struct device
*dev
, struct resource
*parent
,
1178 resource_size_t start
, resource_size_t n
)
1180 struct region_devres match_data
= { parent
, start
, n
};
1182 __release_region(parent
, start
, n
);
1183 WARN_ON(devres_destroy(dev
, devm_region_release
, devm_region_match
,
1186 EXPORT_SYMBOL(__devm_release_region
);
1189 * Called from init/main.c to reserve IO ports.
1191 #define MAXRESERVE 4
1192 static int __init
reserve_setup(char *str
)
1194 static int reserved
;
1195 static struct resource reserve
[MAXRESERVE
];
1198 unsigned int io_start
, io_num
;
1201 if (get_option (&str
, &io_start
) != 2)
1203 if (get_option (&str
, &io_num
) == 0)
1205 if (x
< MAXRESERVE
) {
1206 struct resource
*res
= reserve
+ x
;
1207 res
->name
= "reserved";
1208 res
->start
= io_start
;
1209 res
->end
= io_start
+ io_num
- 1;
1210 res
->flags
= IORESOURCE_BUSY
;
1212 if (request_resource(res
->start
>= 0x10000 ? &iomem_resource
: &ioport_resource
, res
) == 0)
1219 __setup("reserve=", reserve_setup
);
1222 * Check if the requested addr and size spans more than any slot in the
1223 * iomem resource tree.
1225 int iomem_map_sanity_check(resource_size_t addr
, unsigned long size
)
1227 struct resource
*p
= &iomem_resource
;
1231 read_lock(&resource_lock
);
1232 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
1234 * We can probably skip the resources without
1235 * IORESOURCE_IO attribute?
1237 if (p
->start
>= addr
+ size
)
1241 if (PFN_DOWN(p
->start
) <= PFN_DOWN(addr
) &&
1242 PFN_DOWN(p
->end
) >= PFN_DOWN(addr
+ size
- 1))
1245 * if a resource is "BUSY", it's not a hardware resource
1246 * but a driver mapping of such a resource; we don't want
1247 * to warn for those; some drivers legitimately map only
1248 * partial hardware resources. (example: vesafb)
1250 if (p
->flags
& IORESOURCE_BUSY
)
1253 printk(KERN_WARNING
"resource map sanity check conflict: "
1254 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
1255 (unsigned long long)addr
,
1256 (unsigned long long)(addr
+ size
- 1),
1257 (unsigned long long)p
->start
,
1258 (unsigned long long)p
->end
,
1263 read_unlock(&resource_lock
);
1268 #ifdef CONFIG_STRICT_DEVMEM
1269 static int strict_iomem_checks
= 1;
1271 static int strict_iomem_checks
;
1275 * check if an address is reserved in the iomem resource tree
1276 * returns 1 if reserved, 0 if not reserved.
1278 int iomem_is_exclusive(u64 addr
)
1280 struct resource
*p
= &iomem_resource
;
1283 int size
= PAGE_SIZE
;
1285 if (!strict_iomem_checks
)
1288 addr
= addr
& PAGE_MASK
;
1290 read_lock(&resource_lock
);
1291 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
1293 * We can probably skip the resources without
1294 * IORESOURCE_IO attribute?
1296 if (p
->start
>= addr
+ size
)
1300 if (p
->flags
& IORESOURCE_BUSY
&&
1301 p
->flags
& IORESOURCE_EXCLUSIVE
) {
1306 read_unlock(&resource_lock
);
1311 static int __init
strict_iomem(char *str
)
1313 if (strstr(str
, "relaxed"))
1314 strict_iomem_checks
= 0;
1315 if (strstr(str
, "strict"))
1316 strict_iomem_checks
= 1;
1320 __setup("iomem=", strict_iomem
);