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/sched.h>
12 #include <linux/errno.h>
13 #include <linux/ioport.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
18 #include <linux/proc_fs.h>
19 #include <linux/seq_file.h>
20 #include <linux/device.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
);
44 enum { MAX_IORES_LEVEL
= 5 };
46 static void *r_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
48 struct resource
*p
= v
;
52 while (!p
->sibling
&& p
->parent
)
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 struct proc_dir_entry
*entry
;
137 entry
= create_proc_entry("ioports", 0, NULL
);
139 entry
->proc_fops
= &proc_ioports_operations
;
140 entry
= create_proc_entry("iomem", 0, NULL
);
142 entry
->proc_fops
= &proc_iomem_operations
;
145 __initcall(ioresources_init
);
147 #endif /* CONFIG_PROC_FS */
149 /* Return the conflict entry if you can't request it */
150 static struct resource
* __request_resource(struct resource
*root
, struct resource
*new)
152 resource_size_t start
= new->start
;
153 resource_size_t end
= new->end
;
154 struct resource
*tmp
, **p
;
158 if (start
< root
->start
)
165 if (!tmp
|| tmp
->start
> end
) {
172 if (tmp
->end
< start
)
178 static int __release_resource(struct resource
*old
)
180 struct resource
*tmp
, **p
;
182 p
= &old
->parent
->child
;
198 * request_resource - request and reserve an I/O or memory resource
199 * @root: root resource descriptor
200 * @new: resource descriptor desired by caller
202 * Returns 0 for success, negative error code on error.
204 int request_resource(struct resource
*root
, struct resource
*new)
206 struct resource
*conflict
;
208 write_lock(&resource_lock
);
209 conflict
= __request_resource(root
, new);
210 write_unlock(&resource_lock
);
211 return conflict
? -EBUSY
: 0;
214 EXPORT_SYMBOL(request_resource
);
217 * ____request_resource - reserve a resource, with resource conflict returned
218 * @root: root resource descriptor
219 * @new: resource descriptor desired by caller
222 * On success, NULL is returned.
223 * On error, a pointer to the conflicting resource is returned.
225 struct resource
*____request_resource(struct resource
*root
, struct resource
*new)
227 struct resource
*conflict
;
229 write_lock(&resource_lock
);
230 conflict
= __request_resource(root
, new);
231 write_unlock(&resource_lock
);
235 EXPORT_SYMBOL(____request_resource
);
238 * release_resource - release a previously reserved resource
239 * @old: resource pointer
241 int release_resource(struct resource
*old
)
245 write_lock(&resource_lock
);
246 retval
= __release_resource(old
);
247 write_unlock(&resource_lock
);
251 EXPORT_SYMBOL(release_resource
);
253 #ifdef CONFIG_MEMORY_HOTPLUG
255 * Finds the lowest memory reosurce exists within [res->start.res->end)
256 * the caller must specify res->start, res->end, res->flags.
257 * If found, returns 0, res is overwritten, if not found, returns -1.
259 int find_next_system_ram(struct resource
*res
)
261 resource_size_t start
, end
;
268 BUG_ON(start
>= end
);
270 read_lock(&resource_lock
);
271 for (p
= iomem_resource
.child
; p
; p
= p
->sibling
) {
272 /* system ram is just marked as IORESOURCE_MEM */
273 if (p
->flags
!= res
->flags
)
275 if (p
->start
> end
) {
279 if ((p
->end
>= start
) && (p
->start
< end
))
282 read_unlock(&resource_lock
);
286 if (res
->start
< p
->start
)
287 res
->start
= p
->start
;
288 if (res
->end
> p
->end
)
295 * Find empty slot in the resource tree given range and alignment.
297 static int find_resource(struct resource
*root
, struct resource
*new,
298 resource_size_t size
, resource_size_t min
,
299 resource_size_t max
, resource_size_t align
,
300 void (*alignf
)(void *, struct resource
*,
301 resource_size_t
, resource_size_t
),
304 struct resource
*this = root
->child
;
306 new->start
= root
->start
;
308 * Skip past an allocated resource that starts at 0, since the assignment
309 * of this->start - 1 to new->end below would cause an underflow.
311 if (this && this->start
== 0) {
312 new->start
= this->end
+ 1;
313 this = this->sibling
;
317 new->end
= this->start
- 1;
319 new->end
= root
->end
;
320 if (new->start
< min
)
324 new->start
= ALIGN(new->start
, align
);
326 alignf(alignf_data
, new, size
, align
);
327 if (new->start
< new->end
&& new->end
- new->start
>= size
- 1) {
328 new->end
= new->start
+ size
- 1;
333 new->start
= this->end
+ 1;
334 this = this->sibling
;
340 * allocate_resource - allocate empty slot in the resource tree given range & alignment
341 * @root: root resource descriptor
342 * @new: resource descriptor desired by caller
343 * @size: requested resource region size
344 * @min: minimum size to allocate
345 * @max: maximum size to allocate
346 * @align: alignment requested, in bytes
347 * @alignf: alignment function, optional, called if not NULL
348 * @alignf_data: arbitrary data to pass to the @alignf function
350 int allocate_resource(struct resource
*root
, struct resource
*new,
351 resource_size_t size
, resource_size_t min
,
352 resource_size_t max
, resource_size_t align
,
353 void (*alignf
)(void *, struct resource
*,
354 resource_size_t
, resource_size_t
),
359 write_lock(&resource_lock
);
360 err
= find_resource(root
, new, size
, min
, max
, align
, alignf
, alignf_data
);
361 if (err
>= 0 && __request_resource(root
, new))
363 write_unlock(&resource_lock
);
367 EXPORT_SYMBOL(allocate_resource
);
370 * insert_resource - Inserts a resource in the resource tree
371 * @parent: parent of the new resource
372 * @new: new resource to insert
374 * Returns 0 on success, -EBUSY if the resource can't be inserted.
376 * This function is equivalent to request_resource when no conflict
377 * happens. If a conflict happens, and the conflicting resources
378 * entirely fit within the range of the new resource, then the new
379 * resource is inserted and the conflicting resources become children of
382 int insert_resource(struct resource
*parent
, struct resource
*new)
385 struct resource
*first
, *next
;
387 write_lock(&resource_lock
);
389 for (;; parent
= first
) {
391 first
= __request_resource(parent
, new);
399 if ((first
->start
> new->start
) || (first
->end
< new->end
))
401 if ((first
->start
== new->start
) && (first
->end
== new->end
))
405 for (next
= first
; ; next
= next
->sibling
) {
406 /* Partial overlap? Bad, and unfixable */
407 if (next
->start
< new->start
|| next
->end
> new->end
)
411 if (next
->sibling
->start
> new->end
)
417 new->parent
= parent
;
418 new->sibling
= next
->sibling
;
421 next
->sibling
= NULL
;
422 for (next
= first
; next
; next
= next
->sibling
)
425 if (parent
->child
== first
) {
428 next
= parent
->child
;
429 while (next
->sibling
!= first
)
430 next
= next
->sibling
;
435 write_unlock(&resource_lock
);
440 * adjust_resource - modify a resource's start and size
441 * @res: resource to modify
442 * @start: new start value
445 * Given an existing resource, change its start and size to match the
446 * arguments. Returns 0 on success, -EBUSY if it can't fit.
447 * Existing children of the resource are assumed to be immutable.
449 int adjust_resource(struct resource
*res
, resource_size_t start
, resource_size_t size
)
451 struct resource
*tmp
, *parent
= res
->parent
;
452 resource_size_t end
= start
+ size
- 1;
455 write_lock(&resource_lock
);
457 if ((start
< parent
->start
) || (end
> parent
->end
))
460 for (tmp
= res
->child
; tmp
; tmp
= tmp
->sibling
) {
461 if ((tmp
->start
< start
) || (tmp
->end
> end
))
465 if (res
->sibling
&& (res
->sibling
->start
<= end
))
470 while (tmp
->sibling
!= res
)
472 if (start
<= tmp
->end
)
481 write_unlock(&resource_lock
);
485 EXPORT_SYMBOL(adjust_resource
);
488 * This is compatibility stuff for IO resources.
490 * Note how this, unlike the above, knows about
491 * the IO flag meanings (busy etc).
493 * request_region creates a new busy region.
495 * check_region returns non-zero if the area is already busy.
497 * release_region releases a matching busy region.
501 * __request_region - create a new busy resource region
502 * @parent: parent resource descriptor
503 * @start: resource start address
504 * @n: resource region size
505 * @name: reserving caller's ID string
507 struct resource
* __request_region(struct resource
*parent
,
508 resource_size_t start
, resource_size_t n
,
511 struct resource
*res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
516 res
->end
= start
+ n
- 1;
517 res
->flags
= IORESOURCE_BUSY
;
519 write_lock(&resource_lock
);
522 struct resource
*conflict
;
524 conflict
= __request_resource(parent
, res
);
527 if (conflict
!= parent
) {
529 if (!(conflict
->flags
& IORESOURCE_BUSY
))
533 /* Uhhuh, that didn't work out.. */
538 write_unlock(&resource_lock
);
542 EXPORT_SYMBOL(__request_region
);
545 * __check_region - check if a resource region is busy or free
546 * @parent: parent resource descriptor
547 * @start: resource start address
548 * @n: resource region size
550 * Returns 0 if the region is free at the moment it is checked,
551 * returns %-EBUSY if the region is busy.
554 * This function is deprecated because its use is racy.
555 * Even if it returns 0, a subsequent call to request_region()
556 * may fail because another driver etc. just allocated the region.
557 * Do NOT use it. It will be removed from the kernel.
559 int __check_region(struct resource
*parent
, resource_size_t start
,
562 struct resource
* res
;
564 res
= __request_region(parent
, start
, n
, "check-region");
568 release_resource(res
);
572 EXPORT_SYMBOL(__check_region
);
575 * __release_region - release a previously reserved resource region
576 * @parent: parent resource descriptor
577 * @start: resource start address
578 * @n: resource region size
580 * The described resource region must match a currently busy region.
582 void __release_region(struct resource
*parent
, resource_size_t start
,
591 write_lock(&resource_lock
);
594 struct resource
*res
= *p
;
598 if (res
->start
<= start
&& res
->end
>= end
) {
599 if (!(res
->flags
& IORESOURCE_BUSY
)) {
603 if (res
->start
!= start
|| res
->end
!= end
)
606 write_unlock(&resource_lock
);
613 write_unlock(&resource_lock
);
615 printk(KERN_WARNING
"Trying to free nonexistent resource "
616 "<%016llx-%016llx>\n", (unsigned long long)start
,
617 (unsigned long long)end
);
619 EXPORT_SYMBOL(__release_region
);
622 * Managed region resource
624 struct region_devres
{
625 struct resource
*parent
;
626 resource_size_t start
;
630 static void devm_region_release(struct device
*dev
, void *res
)
632 struct region_devres
*this = res
;
634 __release_region(this->parent
, this->start
, this->n
);
637 static int devm_region_match(struct device
*dev
, void *res
, void *match_data
)
639 struct region_devres
*this = res
, *match
= match_data
;
641 return this->parent
== match
->parent
&&
642 this->start
== match
->start
&& this->n
== match
->n
;
645 struct resource
* __devm_request_region(struct device
*dev
,
646 struct resource
*parent
, resource_size_t start
,
647 resource_size_t n
, const char *name
)
649 struct region_devres
*dr
= NULL
;
650 struct resource
*res
;
652 dr
= devres_alloc(devm_region_release
, sizeof(struct region_devres
),
661 res
= __request_region(parent
, start
, n
, name
);
669 EXPORT_SYMBOL(__devm_request_region
);
671 void __devm_release_region(struct device
*dev
, struct resource
*parent
,
672 resource_size_t start
, resource_size_t n
)
674 struct region_devres match_data
= { parent
, start
, n
};
676 __release_region(parent
, start
, n
);
677 WARN_ON(devres_destroy(dev
, devm_region_release
, devm_region_match
,
680 EXPORT_SYMBOL(__devm_release_region
);
683 * Called from init/main.c to reserve IO ports.
686 static int __init
reserve_setup(char *str
)
689 static struct resource reserve
[MAXRESERVE
];
692 int io_start
, io_num
;
695 if (get_option (&str
, &io_start
) != 2)
697 if (get_option (&str
, &io_num
) == 0)
699 if (x
< MAXRESERVE
) {
700 struct resource
*res
= reserve
+ x
;
701 res
->name
= "reserved";
702 res
->start
= io_start
;
703 res
->end
= io_start
+ io_num
- 1;
704 res
->flags
= IORESOURCE_BUSY
;
706 if (request_resource(res
->start
>= 0x10000 ? &iomem_resource
: &ioport_resource
, res
) == 0)
713 __setup("reserve=", reserve_setup
);