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 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
17 #include <linux/proc_fs.h>
18 #include <linux/seq_file.h>
19 #include <linux/device.h>
20 #include <linux/pfn.h>
24 struct resource ioport_resource
= {
27 .end
= IO_SPACE_LIMIT
,
28 .flags
= IORESOURCE_IO
,
30 EXPORT_SYMBOL(ioport_resource
);
32 struct resource iomem_resource
= {
36 .flags
= IORESOURCE_MEM
,
38 EXPORT_SYMBOL(iomem_resource
);
40 static DEFINE_RWLOCK(resource_lock
);
42 static void *r_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
44 struct resource
*p
= v
;
48 while (!p
->sibling
&& p
->parent
)
55 enum { MAX_IORES_LEVEL
= 5 };
57 static void *r_start(struct seq_file
*m
, loff_t
*pos
)
58 __acquires(resource_lock
)
60 struct resource
*p
= m
->private;
62 read_lock(&resource_lock
);
63 for (p
= p
->child
; p
&& l
< *pos
; p
= r_next(m
, p
, &l
))
68 static void r_stop(struct seq_file
*m
, void *v
)
69 __releases(resource_lock
)
71 read_unlock(&resource_lock
);
74 static int r_show(struct seq_file
*m
, void *v
)
76 struct resource
*root
= m
->private;
77 struct resource
*r
= v
, *p
;
78 int width
= root
->end
< 0x10000 ? 4 : 8;
81 for (depth
= 0, p
= r
; depth
< MAX_IORES_LEVEL
; depth
++, p
= p
->parent
)
82 if (p
->parent
== root
)
84 seq_printf(m
, "%*s%0*llx-%0*llx : %s\n",
86 width
, (unsigned long long) r
->start
,
87 width
, (unsigned long long) r
->end
,
88 r
->name
? r
->name
: "<BAD>");
92 static const struct seq_operations resource_op
= {
99 static int ioports_open(struct inode
*inode
, struct file
*file
)
101 int res
= seq_open(file
, &resource_op
);
103 struct seq_file
*m
= file
->private_data
;
104 m
->private = &ioport_resource
;
109 static int iomem_open(struct inode
*inode
, struct file
*file
)
111 int res
= seq_open(file
, &resource_op
);
113 struct seq_file
*m
= file
->private_data
;
114 m
->private = &iomem_resource
;
119 static const struct file_operations proc_ioports_operations
= {
120 .open
= ioports_open
,
123 .release
= seq_release
,
126 static const struct file_operations proc_iomem_operations
= {
130 .release
= seq_release
,
133 static int __init
ioresources_init(void)
135 proc_create("ioports", 0, NULL
, &proc_ioports_operations
);
136 proc_create("iomem", 0, NULL
, &proc_iomem_operations
);
139 __initcall(ioresources_init
);
141 #endif /* CONFIG_PROC_FS */
143 /* Return the conflict entry if you can't request it */
144 static struct resource
* __request_resource(struct resource
*root
, struct resource
*new)
146 resource_size_t start
= new->start
;
147 resource_size_t end
= new->end
;
148 struct resource
*tmp
, **p
;
152 if (start
< root
->start
)
159 if (!tmp
|| tmp
->start
> end
) {
166 if (tmp
->end
< start
)
172 static int __release_resource(struct resource
*old
)
174 struct resource
*tmp
, **p
;
176 p
= &old
->parent
->child
;
191 static void __release_child_resources(struct resource
*r
)
193 struct resource
*tmp
, *p
;
194 resource_size_t size
;
204 __release_child_resources(tmp
);
206 printk(KERN_DEBUG
"release child resource %pR\n", tmp
);
207 /* need to restore size, and keep flags */
208 size
= resource_size(tmp
);
214 void release_child_resources(struct resource
*r
)
216 write_lock(&resource_lock
);
217 __release_child_resources(r
);
218 write_unlock(&resource_lock
);
222 * request_resource - request and reserve an I/O or memory resource
223 * @root: root resource descriptor
224 * @new: resource descriptor desired by caller
226 * Returns 0 for success, negative error code on error.
228 int request_resource(struct resource
*root
, struct resource
*new)
230 struct resource
*conflict
;
232 write_lock(&resource_lock
);
233 conflict
= __request_resource(root
, new);
234 write_unlock(&resource_lock
);
235 return conflict
? -EBUSY
: 0;
238 EXPORT_SYMBOL(request_resource
);
241 * release_resource - release a previously reserved resource
242 * @old: resource pointer
244 int release_resource(struct resource
*old
)
248 write_lock(&resource_lock
);
249 retval
= __release_resource(old
);
250 write_unlock(&resource_lock
);
254 EXPORT_SYMBOL(release_resource
);
256 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
258 * Finds the lowest memory reosurce exists within [res->start.res->end)
259 * the caller must specify res->start, res->end, res->flags and "name".
260 * If found, returns 0, res is overwritten, if not found, returns -1.
262 static int find_next_system_ram(struct resource
*res
, char *name
)
264 resource_size_t start
, end
;
271 BUG_ON(start
>= end
);
273 read_lock(&resource_lock
);
274 for (p
= iomem_resource
.child
; p
; p
= p
->sibling
) {
275 /* system ram is just marked as IORESOURCE_MEM */
276 if (p
->flags
!= res
->flags
)
278 if (name
&& strcmp(p
->name
, name
))
280 if (p
->start
> end
) {
284 if ((p
->end
>= start
) && (p
->start
< end
))
287 read_unlock(&resource_lock
);
291 if (res
->start
< p
->start
)
292 res
->start
= p
->start
;
293 if (res
->end
> p
->end
)
299 * This function calls callback against all memory range of "System RAM"
300 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
301 * Now, this function is only for "System RAM".
303 int walk_system_ram_range(unsigned long start_pfn
, unsigned long nr_pages
,
304 void *arg
, int (*func
)(unsigned long, unsigned long, void *))
307 unsigned long pfn
, end_pfn
;
311 res
.start
= (u64
) start_pfn
<< PAGE_SHIFT
;
312 res
.end
= ((u64
)(start_pfn
+ nr_pages
) << PAGE_SHIFT
) - 1;
313 res
.flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
315 while ((res
.start
< res
.end
) &&
316 (find_next_system_ram(&res
, "System RAM") >= 0)) {
317 pfn
= (res
.start
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
318 end_pfn
= (res
.end
+ 1) >> PAGE_SHIFT
;
320 ret
= (*func
)(pfn
, end_pfn
- pfn
, arg
);
323 res
.start
= res
.end
+ 1;
331 static int __is_ram(unsigned long pfn
, unsigned long nr_pages
, void *arg
)
336 * This generic page_is_ram() returns true if specified address is
337 * registered as "System RAM" in iomem_resource list.
339 int __weak
page_is_ram(unsigned long pfn
)
341 return walk_system_ram_range(pfn
, 1, NULL
, __is_ram
) == 1;
345 * Find empty slot in the resource tree given range and alignment.
347 static int find_resource(struct resource
*root
, struct resource
*new,
348 resource_size_t size
, resource_size_t min
,
349 resource_size_t max
, resource_size_t align
,
350 resource_size_t (*alignf
)(void *,
351 const struct resource
*,
356 struct resource
*this = root
->child
;
357 struct resource tmp
= *new;
359 tmp
.start
= root
->start
;
361 * Skip past an allocated resource that starts at 0, since the assignment
362 * of this->start - 1 to tmp->end below would cause an underflow.
364 if (this && this->start
== 0) {
365 tmp
.start
= this->end
+ 1;
366 this = this->sibling
;
370 tmp
.end
= this->start
- 1;
377 tmp
.start
= ALIGN(tmp
.start
, align
);
379 tmp
.start
= alignf(alignf_data
, &tmp
, size
, align
);
380 if (tmp
.start
< tmp
.end
&& tmp
.end
- tmp
.start
>= size
- 1) {
381 new->start
= tmp
.start
;
382 new->end
= tmp
.start
+ size
- 1;
387 tmp
.start
= this->end
+ 1;
388 this = this->sibling
;
394 * allocate_resource - allocate empty slot in the resource tree given range & alignment
395 * @root: root resource descriptor
396 * @new: resource descriptor desired by caller
397 * @size: requested resource region size
398 * @min: minimum size to allocate
399 * @max: maximum size to allocate
400 * @align: alignment requested, in bytes
401 * @alignf: alignment function, optional, called if not NULL
402 * @alignf_data: arbitrary data to pass to the @alignf function
404 int allocate_resource(struct resource
*root
, struct resource
*new,
405 resource_size_t size
, resource_size_t min
,
406 resource_size_t max
, resource_size_t align
,
407 resource_size_t (*alignf
)(void *,
408 const struct resource
*,
415 write_lock(&resource_lock
);
416 err
= find_resource(root
, new, size
, min
, max
, align
, alignf
, alignf_data
);
417 if (err
>= 0 && __request_resource(root
, new))
419 write_unlock(&resource_lock
);
423 EXPORT_SYMBOL(allocate_resource
);
426 * Insert a resource into the resource tree. If successful, return NULL,
427 * otherwise return the conflicting resource (compare to __request_resource())
429 static struct resource
* __insert_resource(struct resource
*parent
, struct resource
*new)
431 struct resource
*first
, *next
;
433 for (;; parent
= first
) {
434 first
= __request_resource(parent
, new);
441 if ((first
->start
> new->start
) || (first
->end
< new->end
))
443 if ((first
->start
== new->start
) && (first
->end
== new->end
))
447 for (next
= first
; ; next
= next
->sibling
) {
448 /* Partial overlap? Bad, and unfixable */
449 if (next
->start
< new->start
|| next
->end
> new->end
)
453 if (next
->sibling
->start
> new->end
)
457 new->parent
= parent
;
458 new->sibling
= next
->sibling
;
461 next
->sibling
= NULL
;
462 for (next
= first
; next
; next
= next
->sibling
)
465 if (parent
->child
== first
) {
468 next
= parent
->child
;
469 while (next
->sibling
!= first
)
470 next
= next
->sibling
;
477 * insert_resource - Inserts a resource in the resource tree
478 * @parent: parent of the new resource
479 * @new: new resource to insert
481 * Returns 0 on success, -EBUSY if the resource can't be inserted.
483 * This function is equivalent to request_resource when no conflict
484 * happens. If a conflict happens, and the conflicting resources
485 * entirely fit within the range of the new resource, then the new
486 * resource is inserted and the conflicting resources become children of
489 int insert_resource(struct resource
*parent
, struct resource
*new)
491 struct resource
*conflict
;
493 write_lock(&resource_lock
);
494 conflict
= __insert_resource(parent
, new);
495 write_unlock(&resource_lock
);
496 return conflict
? -EBUSY
: 0;
500 * insert_resource_expand_to_fit - Insert a resource into the resource tree
501 * @root: root resource descriptor
502 * @new: new resource to insert
504 * Insert a resource into the resource tree, possibly expanding it in order
505 * to make it encompass any conflicting resources.
507 void insert_resource_expand_to_fit(struct resource
*root
, struct resource
*new)
512 write_lock(&resource_lock
);
514 struct resource
*conflict
;
516 conflict
= __insert_resource(root
, new);
519 if (conflict
== root
)
522 /* Ok, expand resource to cover the conflict, then try again .. */
523 if (conflict
->start
< new->start
)
524 new->start
= conflict
->start
;
525 if (conflict
->end
> new->end
)
526 new->end
= conflict
->end
;
528 printk("Expanded resource %s due to conflict with %s\n", new->name
, conflict
->name
);
530 write_unlock(&resource_lock
);
534 * adjust_resource - modify a resource's start and size
535 * @res: resource to modify
536 * @start: new start value
539 * Given an existing resource, change its start and size to match the
540 * arguments. Returns 0 on success, -EBUSY if it can't fit.
541 * Existing children of the resource are assumed to be immutable.
543 int adjust_resource(struct resource
*res
, resource_size_t start
, resource_size_t size
)
545 struct resource
*tmp
, *parent
= res
->parent
;
546 resource_size_t end
= start
+ size
- 1;
549 write_lock(&resource_lock
);
551 if ((start
< parent
->start
) || (end
> parent
->end
))
554 for (tmp
= res
->child
; tmp
; tmp
= tmp
->sibling
) {
555 if ((tmp
->start
< start
) || (tmp
->end
> end
))
559 if (res
->sibling
&& (res
->sibling
->start
<= end
))
564 while (tmp
->sibling
!= res
)
566 if (start
<= tmp
->end
)
575 write_unlock(&resource_lock
);
579 static void __init
__reserve_region_with_split(struct resource
*root
,
580 resource_size_t start
, resource_size_t end
,
583 struct resource
*parent
= root
;
584 struct resource
*conflict
;
585 struct resource
*res
= kzalloc(sizeof(*res
), GFP_ATOMIC
);
593 res
->flags
= IORESOURCE_BUSY
;
595 conflict
= __request_resource(parent
, res
);
599 /* failed, split and try again */
602 /* conflict covered whole area */
603 if (conflict
->start
<= start
&& conflict
->end
>= end
)
606 if (conflict
->start
> start
)
607 __reserve_region_with_split(root
, start
, conflict
->start
-1, name
);
608 if (conflict
->end
< end
)
609 __reserve_region_with_split(root
, conflict
->end
+1, end
, name
);
612 void __init
reserve_region_with_split(struct resource
*root
,
613 resource_size_t start
, resource_size_t end
,
616 write_lock(&resource_lock
);
617 __reserve_region_with_split(root
, start
, end
, name
);
618 write_unlock(&resource_lock
);
621 EXPORT_SYMBOL(adjust_resource
);
624 * resource_alignment - calculate resource's alignment
625 * @res: resource pointer
627 * Returns alignment on success, 0 (invalid alignment) on failure.
629 resource_size_t
resource_alignment(struct resource
*res
)
631 switch (res
->flags
& (IORESOURCE_SIZEALIGN
| IORESOURCE_STARTALIGN
)) {
632 case IORESOURCE_SIZEALIGN
:
633 return resource_size(res
);
634 case IORESOURCE_STARTALIGN
:
642 * This is compatibility stuff for IO resources.
644 * Note how this, unlike the above, knows about
645 * the IO flag meanings (busy etc).
647 * request_region creates a new busy region.
649 * check_region returns non-zero if the area is already busy.
651 * release_region releases a matching busy region.
655 * __request_region - create a new busy resource region
656 * @parent: parent resource descriptor
657 * @start: resource start address
658 * @n: resource region size
659 * @name: reserving caller's ID string
660 * @flags: IO resource flags
662 struct resource
* __request_region(struct resource
*parent
,
663 resource_size_t start
, resource_size_t n
,
664 const char *name
, int flags
)
666 struct resource
*res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
673 res
->end
= start
+ n
- 1;
674 res
->flags
= IORESOURCE_BUSY
;
677 write_lock(&resource_lock
);
680 struct resource
*conflict
;
682 conflict
= __request_resource(parent
, res
);
685 if (conflict
!= parent
) {
687 if (!(conflict
->flags
& IORESOURCE_BUSY
))
691 /* Uhhuh, that didn't work out.. */
696 write_unlock(&resource_lock
);
699 EXPORT_SYMBOL(__request_region
);
702 * __check_region - check if a resource region is busy or free
703 * @parent: parent resource descriptor
704 * @start: resource start address
705 * @n: resource region size
707 * Returns 0 if the region is free at the moment it is checked,
708 * returns %-EBUSY if the region is busy.
711 * This function is deprecated because its use is racy.
712 * Even if it returns 0, a subsequent call to request_region()
713 * may fail because another driver etc. just allocated the region.
714 * Do NOT use it. It will be removed from the kernel.
716 int __check_region(struct resource
*parent
, resource_size_t start
,
719 struct resource
* res
;
721 res
= __request_region(parent
, start
, n
, "check-region", 0);
725 release_resource(res
);
729 EXPORT_SYMBOL(__check_region
);
732 * __release_region - release a previously reserved resource region
733 * @parent: parent resource descriptor
734 * @start: resource start address
735 * @n: resource region size
737 * The described resource region must match a currently busy region.
739 void __release_region(struct resource
*parent
, resource_size_t start
,
748 write_lock(&resource_lock
);
751 struct resource
*res
= *p
;
755 if (res
->start
<= start
&& res
->end
>= end
) {
756 if (!(res
->flags
& IORESOURCE_BUSY
)) {
760 if (res
->start
!= start
|| res
->end
!= end
)
763 write_unlock(&resource_lock
);
770 write_unlock(&resource_lock
);
772 printk(KERN_WARNING
"Trying to free nonexistent resource "
773 "<%016llx-%016llx>\n", (unsigned long long)start
,
774 (unsigned long long)end
);
776 EXPORT_SYMBOL(__release_region
);
779 * Managed region resource
781 struct region_devres
{
782 struct resource
*parent
;
783 resource_size_t start
;
787 static void devm_region_release(struct device
*dev
, void *res
)
789 struct region_devres
*this = res
;
791 __release_region(this->parent
, this->start
, this->n
);
794 static int devm_region_match(struct device
*dev
, void *res
, void *match_data
)
796 struct region_devres
*this = res
, *match
= match_data
;
798 return this->parent
== match
->parent
&&
799 this->start
== match
->start
&& this->n
== match
->n
;
802 struct resource
* __devm_request_region(struct device
*dev
,
803 struct resource
*parent
, resource_size_t start
,
804 resource_size_t n
, const char *name
)
806 struct region_devres
*dr
= NULL
;
807 struct resource
*res
;
809 dr
= devres_alloc(devm_region_release
, sizeof(struct region_devres
),
818 res
= __request_region(parent
, start
, n
, name
, 0);
826 EXPORT_SYMBOL(__devm_request_region
);
828 void __devm_release_region(struct device
*dev
, struct resource
*parent
,
829 resource_size_t start
, resource_size_t n
)
831 struct region_devres match_data
= { parent
, start
, n
};
833 __release_region(parent
, start
, n
);
834 WARN_ON(devres_destroy(dev
, devm_region_release
, devm_region_match
,
837 EXPORT_SYMBOL(__devm_release_region
);
840 * Called from init/main.c to reserve IO ports.
843 static int __init
reserve_setup(char *str
)
846 static struct resource reserve
[MAXRESERVE
];
849 unsigned int io_start
, io_num
;
852 if (get_option (&str
, &io_start
) != 2)
854 if (get_option (&str
, &io_num
) == 0)
856 if (x
< MAXRESERVE
) {
857 struct resource
*res
= reserve
+ x
;
858 res
->name
= "reserved";
859 res
->start
= io_start
;
860 res
->end
= io_start
+ io_num
- 1;
861 res
->flags
= IORESOURCE_BUSY
;
863 if (request_resource(res
->start
>= 0x10000 ? &iomem_resource
: &ioport_resource
, res
) == 0)
870 __setup("reserve=", reserve_setup
);
873 * Check if the requested addr and size spans more than any slot in the
874 * iomem resource tree.
876 int iomem_map_sanity_check(resource_size_t addr
, unsigned long size
)
878 struct resource
*p
= &iomem_resource
;
882 read_lock(&resource_lock
);
883 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
885 * We can probably skip the resources without
886 * IORESOURCE_IO attribute?
888 if (p
->start
>= addr
+ size
)
892 if (PFN_DOWN(p
->start
) <= PFN_DOWN(addr
) &&
893 PFN_DOWN(p
->end
) >= PFN_DOWN(addr
+ size
- 1))
896 * if a resource is "BUSY", it's not a hardware resource
897 * but a driver mapping of such a resource; we don't want
898 * to warn for those; some drivers legitimately map only
899 * partial hardware resources. (example: vesafb)
901 if (p
->flags
& IORESOURCE_BUSY
)
904 printk(KERN_WARNING
"resource map sanity check conflict: "
905 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
906 (unsigned long long)addr
,
907 (unsigned long long)(addr
+ size
- 1),
908 (unsigned long long)p
->start
,
909 (unsigned long long)p
->end
,
914 read_unlock(&resource_lock
);
919 #ifdef CONFIG_STRICT_DEVMEM
920 static int strict_iomem_checks
= 1;
922 static int strict_iomem_checks
;
926 * check if an address is reserved in the iomem resource tree
927 * returns 1 if reserved, 0 if not reserved.
929 int iomem_is_exclusive(u64 addr
)
931 struct resource
*p
= &iomem_resource
;
934 int size
= PAGE_SIZE
;
936 if (!strict_iomem_checks
)
939 addr
= addr
& PAGE_MASK
;
941 read_lock(&resource_lock
);
942 for (p
= p
->child
; p
; p
= r_next(NULL
, p
, &l
)) {
944 * We can probably skip the resources without
945 * IORESOURCE_IO attribute?
947 if (p
->start
>= addr
+ size
)
951 if (p
->flags
& IORESOURCE_BUSY
&&
952 p
->flags
& IORESOURCE_EXCLUSIVE
) {
957 read_unlock(&resource_lock
);
962 static int __init
strict_iomem(char *str
)
964 if (strstr(str
, "relaxed"))
965 strict_iomem_checks
= 0;
966 if (strstr(str
, "strict"))
967 strict_iomem_checks
= 1;
971 __setup("iomem=", strict_iomem
);