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/config.h>
11 #include <linux/module.h>
12 #include <linux/sched.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/seq_file.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 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 struct file_operations proc_ioports_operations
= {
120 .open
= ioports_open
,
123 .release
= seq_release
,
126 static 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
;
197 int request_resource(struct resource
*root
, struct resource
*new)
199 struct resource
*conflict
;
201 write_lock(&resource_lock
);
202 conflict
= __request_resource(root
, new);
203 write_unlock(&resource_lock
);
204 return conflict
? -EBUSY
: 0;
207 EXPORT_SYMBOL(request_resource
);
209 struct resource
*____request_resource(struct resource
*root
, struct resource
*new)
211 struct resource
*conflict
;
213 write_lock(&resource_lock
);
214 conflict
= __request_resource(root
, new);
215 write_unlock(&resource_lock
);
219 EXPORT_SYMBOL(____request_resource
);
221 int release_resource(struct resource
*old
)
225 write_lock(&resource_lock
);
226 retval
= __release_resource(old
);
227 write_unlock(&resource_lock
);
231 EXPORT_SYMBOL(release_resource
);
233 #ifdef CONFIG_MEMORY_HOTPLUG
235 * Finds the lowest memory reosurce exists within [res->start.res->end)
236 * the caller must specify res->start, res->end, res->flags.
237 * If found, returns 0, res is overwritten, if not found, returns -1.
239 int find_next_system_ram(struct resource
*res
)
241 resource_size_t start
, end
;
249 read_lock(&resource_lock
);
250 for (p
= iomem_resource
.child
; p
; p
= p
->sibling
) {
251 /* system ram is just marked as IORESOURCE_MEM */
252 if (p
->flags
!= res
->flags
)
254 if (p
->start
> end
) {
258 if (p
->start
>= start
)
261 read_unlock(&resource_lock
);
265 res
->start
= p
->start
;
272 * Find empty slot in the resource tree given range and alignment.
274 static int find_resource(struct resource
*root
, struct resource
*new,
275 resource_size_t size
, resource_size_t min
,
276 resource_size_t max
, resource_size_t align
,
277 void (*alignf
)(void *, struct resource
*,
278 resource_size_t
, resource_size_t
),
281 struct resource
*this = root
->child
;
283 new->start
= root
->start
;
285 * Skip past an allocated resource that starts at 0, since the assignment
286 * of this->start - 1 to new->end below would cause an underflow.
288 if (this && this->start
== 0) {
289 new->start
= this->end
+ 1;
290 this = this->sibling
;
294 new->end
= this->start
- 1;
296 new->end
= root
->end
;
297 if (new->start
< min
)
301 new->start
= ALIGN(new->start
, align
);
303 alignf(alignf_data
, new, size
, align
);
304 if (new->start
< new->end
&& new->end
- new->start
>= size
- 1) {
305 new->end
= new->start
+ size
- 1;
310 new->start
= this->end
+ 1;
311 this = this->sibling
;
317 * Allocate empty slot in the resource tree given range and alignment.
319 int allocate_resource(struct resource
*root
, struct resource
*new,
320 resource_size_t size
, resource_size_t min
,
321 resource_size_t max
, resource_size_t align
,
322 void (*alignf
)(void *, struct resource
*,
323 resource_size_t
, resource_size_t
),
328 write_lock(&resource_lock
);
329 err
= find_resource(root
, new, size
, min
, max
, align
, alignf
, alignf_data
);
330 if (err
>= 0 && __request_resource(root
, new))
332 write_unlock(&resource_lock
);
336 EXPORT_SYMBOL(allocate_resource
);
339 * insert_resource - Inserts a resource in the resource tree
340 * @parent: parent of the new resource
341 * @new: new resource to insert
343 * Returns 0 on success, -EBUSY if the resource can't be inserted.
345 * This function is equivalent of request_resource when no conflict
346 * happens. If a conflict happens, and the conflicting resources
347 * entirely fit within the range of the new resource, then the new
348 * resource is inserted and the conflicting resources become childs of
349 * the new resource. Otherwise the new resource becomes the child of
350 * the conflicting resource
352 int insert_resource(struct resource
*parent
, struct resource
*new)
355 struct resource
*first
, *next
;
357 write_lock(&resource_lock
);
360 first
= __request_resource(parent
, new);
368 /* Resource fully contained by the clashing resource? Recurse into it */
369 if (first
->start
<= new->start
&& first
->end
>= new->end
) {
374 for (next
= first
; ; next
= next
->sibling
) {
375 /* Partial overlap? Bad, and unfixable */
376 if (next
->start
< new->start
|| next
->end
> new->end
)
380 if (next
->sibling
->start
> new->end
)
386 new->parent
= parent
;
387 new->sibling
= next
->sibling
;
390 next
->sibling
= NULL
;
391 for (next
= first
; next
; next
= next
->sibling
)
394 if (parent
->child
== first
) {
397 next
= parent
->child
;
398 while (next
->sibling
!= first
)
399 next
= next
->sibling
;
404 write_unlock(&resource_lock
);
408 EXPORT_SYMBOL(insert_resource
);
411 * Given an existing resource, change its start and size to match the
412 * arguments. Returns -EBUSY if it can't fit. Existing children of
413 * the resource are assumed to be immutable.
415 int adjust_resource(struct resource
*res
, resource_size_t start
, resource_size_t size
)
417 struct resource
*tmp
, *parent
= res
->parent
;
418 resource_size_t end
= start
+ size
- 1;
421 write_lock(&resource_lock
);
423 if ((start
< parent
->start
) || (end
> parent
->end
))
426 for (tmp
= res
->child
; tmp
; tmp
= tmp
->sibling
) {
427 if ((tmp
->start
< start
) || (tmp
->end
> end
))
431 if (res
->sibling
&& (res
->sibling
->start
<= end
))
436 while (tmp
->sibling
!= res
)
438 if (start
<= tmp
->end
)
447 write_unlock(&resource_lock
);
451 EXPORT_SYMBOL(adjust_resource
);
454 * This is compatibility stuff for IO resources.
456 * Note how this, unlike the above, knows about
457 * the IO flag meanings (busy etc).
459 * Request-region creates a new busy region.
461 * Check-region returns non-zero if the area is already busy
463 * Release-region releases a matching busy region.
465 struct resource
* __request_region(struct resource
*parent
,
466 resource_size_t start
, resource_size_t n
,
469 struct resource
*res
= kzalloc(sizeof(*res
), GFP_KERNEL
);
474 res
->end
= start
+ n
- 1;
475 res
->flags
= IORESOURCE_BUSY
;
477 write_lock(&resource_lock
);
480 struct resource
*conflict
;
482 conflict
= __request_resource(parent
, res
);
485 if (conflict
!= parent
) {
487 if (!(conflict
->flags
& IORESOURCE_BUSY
))
491 /* Uhhuh, that didn't work out.. */
496 write_unlock(&resource_lock
);
501 EXPORT_SYMBOL(__request_region
);
503 int __check_region(struct resource
*parent
, resource_size_t start
,
506 struct resource
* res
;
508 res
= __request_region(parent
, start
, n
, "check-region");
512 release_resource(res
);
517 EXPORT_SYMBOL(__check_region
);
519 void __release_region(struct resource
*parent
, resource_size_t start
,
528 write_lock(&resource_lock
);
531 struct resource
*res
= *p
;
535 if (res
->start
<= start
&& res
->end
>= end
) {
536 if (!(res
->flags
& IORESOURCE_BUSY
)) {
540 if (res
->start
!= start
|| res
->end
!= end
)
543 write_unlock(&resource_lock
);
550 write_unlock(&resource_lock
);
552 printk(KERN_WARNING
"Trying to free nonexistent resource "
553 "<%016llx-%016llx>\n", (unsigned long long)start
,
554 (unsigned long long)end
);
557 EXPORT_SYMBOL(__release_region
);
560 * Called from init/main.c to reserve IO ports.
563 static int __init
reserve_setup(char *str
)
566 static struct resource reserve
[MAXRESERVE
];
569 int io_start
, io_num
;
572 if (get_option (&str
, &io_start
) != 2)
574 if (get_option (&str
, &io_num
) == 0)
576 if (x
< MAXRESERVE
) {
577 struct resource
*res
= reserve
+ x
;
578 res
->name
= "reserved";
579 res
->start
= io_start
;
580 res
->end
= io_start
+ io_num
- 1;
581 res
->flags
= IORESOURCE_BUSY
;
583 if (request_resource(res
->start
>= 0x10000 ? &iomem_resource
: &ioport_resource
, res
) == 0)
590 __setup("reserve=", reserve_setup
);