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
,
31 EXPORT_SYMBOL(ioport_resource
);
33 struct resource iomem_resource
= {
37 .flags
= IORESOURCE_MEM
,
40 EXPORT_SYMBOL(iomem_resource
);
42 static rwlock_t resource_lock
= RW_LOCK_UNLOCKED
;
46 enum { MAX_IORES_LEVEL
= 5 };
48 static void *r_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
50 struct resource
*p
= v
;
54 while (!p
->sibling
&& p
->parent
)
59 static void *r_start(struct seq_file
*m
, loff_t
*pos
)
61 struct resource
*p
= m
->private;
63 read_lock(&resource_lock
);
64 for (p
= p
->child
; p
&& l
< *pos
; p
= r_next(m
, p
, &l
))
69 static void r_stop(struct seq_file
*m
, void *v
)
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*lx-%0*lx : %s\n",
88 r
->name
? r
->name
: "<BAD>");
92 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 unsigned long start
= new->start
;
153 unsigned long 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
);
234 * Find empty slot in the resource tree given range and alignment.
236 static int find_resource(struct resource
*root
, struct resource
*new,
238 unsigned long min
, unsigned long max
,
240 void (*alignf
)(void *, struct resource
*,
241 unsigned long, unsigned long),
244 struct resource
*this = root
->child
;
246 new->start
= root
->start
;
248 * Skip past an allocated resource that starts at 0, since the assignment
249 * of this->start - 1 to new->end below would cause an underflow.
251 if (this && this->start
== 0) {
252 new->start
= this->end
+ 1;
253 this = this->sibling
;
257 new->end
= this->start
- 1;
259 new->end
= root
->end
;
260 if (new->start
< min
)
264 new->start
= (new->start
+ align
- 1) & ~(align
- 1);
266 alignf(alignf_data
, new, size
, align
);
267 if (new->start
< new->end
&& new->end
- new->start
+ 1 >= size
) {
268 new->end
= new->start
+ size
- 1;
273 new->start
= this->end
+ 1;
274 this = this->sibling
;
280 * Allocate empty slot in the resource tree given range and alignment.
282 int allocate_resource(struct resource
*root
, struct resource
*new,
284 unsigned long min
, unsigned long max
,
286 void (*alignf
)(void *, struct resource
*,
287 unsigned long, unsigned long),
292 write_lock(&resource_lock
);
293 err
= find_resource(root
, new, size
, min
, max
, align
, alignf
, alignf_data
);
294 if (err
>= 0 && __request_resource(root
, new))
296 write_unlock(&resource_lock
);
300 EXPORT_SYMBOL(allocate_resource
);
303 * insert_resource - Inserts a resource in the resource tree
304 * @parent: parent of the new resource
305 * @new: new resource to insert
307 * Returns 0 on success, -EBUSY if the resource can't be inserted.
309 * This function is equivalent of request_resource when no conflict
310 * happens. If a conflict happens, and the conflicting resources
311 * entirely fit within the range of the new resource, then the new
312 * resource is inserted and the conflicting resources become childs of
313 * the new resource. Otherwise the new resource becomes the child of
314 * the conflicting resource
316 int insert_resource(struct resource
*parent
, struct resource
*new)
319 struct resource
*first
, *next
;
321 write_lock(&resource_lock
);
324 first
= __request_resource(parent
, new);
332 /* Resource fully contained by the clashing resource? Recurse into it */
333 if (first
->start
<= new->start
&& first
->end
>= new->end
) {
338 for (next
= first
; ; next
= next
->sibling
) {
339 /* Partial overlap? Bad, and unfixable */
340 if (next
->start
< new->start
|| next
->end
> new->end
)
344 if (next
->sibling
->start
> new->end
)
350 new->parent
= parent
;
351 new->sibling
= next
->sibling
;
354 next
->sibling
= NULL
;
355 for (next
= first
; next
; next
= next
->sibling
)
358 if (parent
->child
== first
) {
361 next
= parent
->child
;
362 while (next
->sibling
!= first
)
363 next
= next
->sibling
;
368 write_unlock(&resource_lock
);
372 EXPORT_SYMBOL(insert_resource
);
375 * Given an existing resource, change its start and size to match the
376 * arguments. Returns -EBUSY if it can't fit. Existing children of
377 * the resource are assumed to be immutable.
379 int adjust_resource(struct resource
*res
, unsigned long start
, unsigned long size
)
381 struct resource
*tmp
, *parent
= res
->parent
;
382 unsigned long end
= start
+ size
- 1;
385 write_lock(&resource_lock
);
387 if ((start
< parent
->start
) || (end
> parent
->end
))
390 for (tmp
= res
->child
; tmp
; tmp
= tmp
->sibling
) {
391 if ((tmp
->start
< start
) || (tmp
->end
> end
))
395 if (res
->sibling
&& (res
->sibling
->start
<= end
))
400 while (tmp
->sibling
!= res
)
402 if (start
<= tmp
->end
)
411 write_unlock(&resource_lock
);
415 EXPORT_SYMBOL(adjust_resource
);
418 * This is compatibility stuff for IO resources.
420 * Note how this, unlike the above, knows about
421 * the IO flag meanings (busy etc).
423 * Request-region creates a new busy region.
425 * Check-region returns non-zero if the area is already busy
427 * Release-region releases a matching busy region.
429 struct resource
* __request_region(struct resource
*parent
, unsigned long start
, unsigned long n
, const char *name
)
431 struct resource
*res
= kmalloc(sizeof(*res
), GFP_KERNEL
);
434 memset(res
, 0, sizeof(*res
));
437 res
->end
= start
+ n
- 1;
438 res
->flags
= IORESOURCE_BUSY
;
440 write_lock(&resource_lock
);
443 struct resource
*conflict
;
445 conflict
= __request_resource(parent
, res
);
448 if (conflict
!= parent
) {
450 if (!(conflict
->flags
& IORESOURCE_BUSY
))
454 /* Uhhuh, that didn't work out.. */
459 write_unlock(&resource_lock
);
464 EXPORT_SYMBOL(__request_region
);
466 int __deprecated
__check_region(struct resource
*parent
, unsigned long start
, unsigned long n
)
468 struct resource
* res
;
470 res
= __request_region(parent
, start
, n
, "check-region");
474 release_resource(res
);
479 EXPORT_SYMBOL(__check_region
);
481 void __release_region(struct resource
*parent
, unsigned long start
, unsigned long n
)
489 write_lock(&resource_lock
);
492 struct resource
*res
= *p
;
496 if (res
->start
<= start
&& res
->end
>= end
) {
497 if (!(res
->flags
& IORESOURCE_BUSY
)) {
501 if (res
->start
!= start
|| res
->end
!= end
)
504 write_unlock(&resource_lock
);
511 write_unlock(&resource_lock
);
513 printk(KERN_WARNING
"Trying to free nonexistent resource <%08lx-%08lx>\n", start
, end
);
516 EXPORT_SYMBOL(__release_region
);
519 * Called from init/main.c to reserve IO ports.
522 static int __init
reserve_setup(char *str
)
525 static struct resource reserve
[MAXRESERVE
];
528 int io_start
, io_num
;
531 if (get_option (&str
, &io_start
) != 2)
533 if (get_option (&str
, &io_num
) == 0)
535 if (x
< MAXRESERVE
) {
536 struct resource
*res
= reserve
+ x
;
537 res
->name
= "reserved";
538 res
->start
= io_start
;
539 res
->end
= io_start
+ io_num
- 1;
540 res
->flags
= IORESOURCE_BUSY
;
542 if (request_resource(res
->start
>= 0x10000 ? &iomem_resource
: &ioport_resource
, res
) == 0)
549 __setup("reserve=", reserve_setup
);