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>
23 struct resource ioport_resource
= {
26 .end
= IO_SPACE_LIMIT
,
27 .flags
= IORESOURCE_IO
,
29 EXPORT_SYMBOL(ioport_resource
);
31 struct resource iomem_resource
= {
35 .flags
= IORESOURCE_MEM
,
37 EXPORT_SYMBOL(iomem_resource
);
39 static DEFINE_RWLOCK(resource_lock
);
43 enum { MAX_IORES_LEVEL
= 5 };
45 static void *r_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
47 struct resource
*p
= v
;
51 while (!p
->sibling
&& p
->parent
)
56 static void *r_start(struct seq_file
*m
, loff_t
*pos
)
57 __acquires(resource_lock
)
59 struct resource
*p
= m
->private;
61 read_lock(&resource_lock
);
62 for (p
= p
->child
; p
&& l
< *pos
; p
= r_next(m
, p
, &l
))
67 static void r_stop(struct seq_file
*m
, void *v
)
68 __releases(resource_lock
)
70 read_unlock(&resource_lock
);
73 static int r_show(struct seq_file
*m
, void *v
)
75 struct resource
*root
= m
->private;
76 struct resource
*r
= v
, *p
;
77 int width
= root
->end
< 0x10000 ? 4 : 8;
80 for (depth
= 0, p
= r
; depth
< MAX_IORES_LEVEL
; depth
++, p
= p
->parent
)
81 if (p
->parent
== root
)
83 seq_printf(m
, "%*s%0*llx-%0*llx : %s\n",
85 width
, (unsigned long long) r
->start
,
86 width
, (unsigned long long) r
->end
,
87 r
->name
? r
->name
: "<BAD>");
91 static struct seq_operations resource_op
= {
98 static int ioports_open(struct inode
*inode
, struct file
*file
)
100 int res
= seq_open(file
, &resource_op
);
102 struct seq_file
*m
= file
->private_data
;
103 m
->private = &ioport_resource
;
108 static int iomem_open(struct inode
*inode
, struct file
*file
)
110 int res
= seq_open(file
, &resource_op
);
112 struct seq_file
*m
= file
->private_data
;
113 m
->private = &iomem_resource
;
118 static struct file_operations proc_ioports_operations
= {
119 .open
= ioports_open
,
122 .release
= seq_release
,
125 static struct file_operations proc_iomem_operations
= {
129 .release
= seq_release
,
132 static int __init
ioresources_init(void)
134 struct proc_dir_entry
*entry
;
136 entry
= create_proc_entry("ioports", 0, NULL
);
138 entry
->proc_fops
= &proc_ioports_operations
;
139 entry
= create_proc_entry("iomem", 0, NULL
);
141 entry
->proc_fops
= &proc_iomem_operations
;
144 __initcall(ioresources_init
);
146 #endif /* CONFIG_PROC_FS */
148 /* Return the conflict entry if you can't request it */
149 static struct resource
* __request_resource(struct resource
*root
, struct resource
*new)
151 resource_size_t start
= new->start
;
152 resource_size_t end
= new->end
;
153 struct resource
*tmp
, **p
;
157 if (start
< root
->start
)
164 if (!tmp
|| tmp
->start
> end
) {
171 if (tmp
->end
< start
)
177 static int __release_resource(struct resource
*old
)
179 struct resource
*tmp
, **p
;
181 p
= &old
->parent
->child
;
196 int request_resource(struct resource
*root
, struct resource
*new)
198 struct resource
*conflict
;
200 write_lock(&resource_lock
);
201 conflict
= __request_resource(root
, new);
202 write_unlock(&resource_lock
);
203 return conflict
? -EBUSY
: 0;
206 EXPORT_SYMBOL(request_resource
);
208 struct resource
*____request_resource(struct resource
*root
, struct resource
*new)
210 struct resource
*conflict
;
212 write_lock(&resource_lock
);
213 conflict
= __request_resource(root
, new);
214 write_unlock(&resource_lock
);
218 EXPORT_SYMBOL(____request_resource
);
220 int release_resource(struct resource
*old
)
224 write_lock(&resource_lock
);
225 retval
= __release_resource(old
);
226 write_unlock(&resource_lock
);
230 EXPORT_SYMBOL(release_resource
);
232 #ifdef CONFIG_MEMORY_HOTPLUG
234 * Finds the lowest memory reosurce exists within [res->start.res->end)
235 * the caller must specify res->start, res->end, res->flags.
236 * If found, returns 0, res is overwritten, if not found, returns -1.
238 int find_next_system_ram(struct resource
*res
)
240 resource_size_t start
, end
;
247 BUG_ON(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
->end
>= start
) && (p
->start
< end
))
261 read_unlock(&resource_lock
);
265 if (res
->start
< p
->start
)
266 res
->start
= p
->start
;
267 if (res
->end
> p
->end
)
274 * Find empty slot in the resource tree given range and alignment.
276 static int find_resource(struct resource
*root
, struct resource
*new,
277 resource_size_t size
, resource_size_t min
,
278 resource_size_t max
, resource_size_t align
,
279 void (*alignf
)(void *, struct resource
*,
280 resource_size_t
, resource_size_t
),
283 struct resource
*this = root
->child
;
285 new->start
= root
->start
;
287 * Skip past an allocated resource that starts at 0, since the assignment
288 * of this->start - 1 to new->end below would cause an underflow.
290 if (this && this->start
== 0) {
291 new->start
= this->end
+ 1;
292 this = this->sibling
;
296 new->end
= this->start
- 1;
298 new->end
= root
->end
;
299 if (new->start
< min
)
303 new->start
= ALIGN(new->start
, align
);
305 alignf(alignf_data
, new, size
, align
);
306 if (new->start
< new->end
&& new->end
- new->start
>= size
- 1) {
307 new->end
= new->start
+ size
- 1;
312 new->start
= this->end
+ 1;
313 this = this->sibling
;
319 * Allocate empty slot in the resource tree given range and alignment.
321 int allocate_resource(struct resource
*root
, struct resource
*new,
322 resource_size_t size
, resource_size_t min
,
323 resource_size_t max
, resource_size_t align
,
324 void (*alignf
)(void *, struct resource
*,
325 resource_size_t
, resource_size_t
),
330 write_lock(&resource_lock
);
331 err
= find_resource(root
, new, size
, min
, max
, align
, alignf
, alignf_data
);
332 if (err
>= 0 && __request_resource(root
, new))
334 write_unlock(&resource_lock
);
338 EXPORT_SYMBOL(allocate_resource
);
341 * insert_resource - Inserts a resource in the resource tree
342 * @parent: parent of the new resource
343 * @new: new resource to insert
345 * Returns 0 on success, -EBUSY if the resource can't be inserted.
347 * This function is equivalent to request_resource when no conflict
348 * happens. If a conflict happens, and the conflicting resources
349 * entirely fit within the range of the new resource, then the new
350 * resource is inserted and the conflicting resources become children of
353 int insert_resource(struct resource
*parent
, struct resource
*new)
356 struct resource
*first
, *next
;
358 write_lock(&resource_lock
);
360 for (;; parent
= first
) {
362 first
= __request_resource(parent
, new);
370 if ((first
->start
> new->start
) || (first
->end
< new->end
))
372 if ((first
->start
== new->start
) && (first
->end
== new->end
))
376 for (next
= first
; ; next
= next
->sibling
) {
377 /* Partial overlap? Bad, and unfixable */
378 if (next
->start
< new->start
|| next
->end
> new->end
)
382 if (next
->sibling
->start
> new->end
)
388 new->parent
= parent
;
389 new->sibling
= next
->sibling
;
392 next
->sibling
= NULL
;
393 for (next
= first
; next
; next
= next
->sibling
)
396 if (parent
->child
== first
) {
399 next
= parent
->child
;
400 while (next
->sibling
!= first
)
401 next
= next
->sibling
;
406 write_unlock(&resource_lock
);
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
);