[CPUFREQ] Misc cleanups in ondemand.
[linux-2.6/mini2440.git] / kernel / resource.c
blobbf1130d81b7f09d35c81e436592b407e56c526d3
1 /*
2 * linux/kernel/resource.c
4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
7 * Arbitrary resource management.
8 */
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>
18 #include <linux/fs.h>
19 #include <linux/proc_fs.h>
20 #include <linux/seq_file.h>
21 #include <asm/io.h>
24 struct resource ioport_resource = {
25 .name = "PCI IO",
26 .start = 0,
27 .end = IO_SPACE_LIMIT,
28 .flags = IORESOURCE_IO,
30 EXPORT_SYMBOL(ioport_resource);
32 struct resource iomem_resource = {
33 .name = "PCI mem",
34 .start = 0,
35 .end = -1,
36 .flags = IORESOURCE_MEM,
38 EXPORT_SYMBOL(iomem_resource);
40 static DEFINE_RWLOCK(resource_lock);
42 #ifdef CONFIG_PROC_FS
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;
49 (*pos)++;
50 if (p->child)
51 return p->child;
52 while (!p->sibling && p->parent)
53 p = p->parent;
54 return p->sibling;
57 static void *r_start(struct seq_file *m, loff_t *pos)
58 __acquires(resource_lock)
60 struct resource *p = m->private;
61 loff_t l = 0;
62 read_lock(&resource_lock);
63 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
65 return p;
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;
79 int depth;
81 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
82 if (p->parent == root)
83 break;
84 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
85 depth * 2, "",
86 width, (unsigned long long) r->start,
87 width, (unsigned long long) r->end,
88 r->name ? r->name : "<BAD>");
89 return 0;
92 static struct seq_operations resource_op = {
93 .start = r_start,
94 .next = r_next,
95 .stop = r_stop,
96 .show = r_show,
99 static int ioports_open(struct inode *inode, struct file *file)
101 int res = seq_open(file, &resource_op);
102 if (!res) {
103 struct seq_file *m = file->private_data;
104 m->private = &ioport_resource;
106 return res;
109 static int iomem_open(struct inode *inode, struct file *file)
111 int res = seq_open(file, &resource_op);
112 if (!res) {
113 struct seq_file *m = file->private_data;
114 m->private = &iomem_resource;
116 return res;
119 static struct file_operations proc_ioports_operations = {
120 .open = ioports_open,
121 .read = seq_read,
122 .llseek = seq_lseek,
123 .release = seq_release,
126 static struct file_operations proc_iomem_operations = {
127 .open = iomem_open,
128 .read = seq_read,
129 .llseek = seq_lseek,
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);
138 if (entry)
139 entry->proc_fops = &proc_ioports_operations;
140 entry = create_proc_entry("iomem", 0, NULL);
141 if (entry)
142 entry->proc_fops = &proc_iomem_operations;
143 return 0;
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;
156 if (end < start)
157 return root;
158 if (start < root->start)
159 return root;
160 if (end > root->end)
161 return root;
162 p = &root->child;
163 for (;;) {
164 tmp = *p;
165 if (!tmp || tmp->start > end) {
166 new->sibling = tmp;
167 *p = new;
168 new->parent = root;
169 return NULL;
171 p = &tmp->sibling;
172 if (tmp->end < start)
173 continue;
174 return tmp;
178 static int __release_resource(struct resource *old)
180 struct resource *tmp, **p;
182 p = &old->parent->child;
183 for (;;) {
184 tmp = *p;
185 if (!tmp)
186 break;
187 if (tmp == old) {
188 *p = tmp->sibling;
189 old->parent = NULL;
190 return 0;
192 p = &tmp->sibling;
194 return -EINVAL;
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);
216 return conflict;
219 EXPORT_SYMBOL(____request_resource);
221 int release_resource(struct resource *old)
223 int retval;
225 write_lock(&resource_lock);
226 retval = __release_resource(old);
227 write_unlock(&resource_lock);
228 return retval;
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;
242 struct resource *p;
244 BUG_ON(!res);
246 start = res->start;
247 end = res->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)
253 continue;
254 if (p->start > end) {
255 p = NULL;
256 break;
258 if (p->start >= start)
259 break;
261 read_unlock(&resource_lock);
262 if (!p)
263 return -1;
264 /* copy data */
265 res->start = p->start;
266 res->end = p->end;
267 return 0;
269 #endif
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),
279 void *alignf_data)
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;
292 for(;;) {
293 if (this)
294 new->end = this->start - 1;
295 else
296 new->end = root->end;
297 if (new->start < min)
298 new->start = min;
299 if (new->end > max)
300 new->end = max;
301 new->start = ALIGN(new->start, align);
302 if (alignf)
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;
306 return 0;
308 if (!this)
309 break;
310 new->start = this->end + 1;
311 this = this->sibling;
313 return -EBUSY;
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),
324 void *alignf_data)
326 int err;
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))
331 err = -EBUSY;
332 write_unlock(&resource_lock);
333 return err;
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)
354 int result;
355 struct resource *first, *next;
357 write_lock(&resource_lock);
358 begin:
359 result = 0;
360 first = __request_resource(parent, new);
361 if (!first)
362 goto out;
364 result = -EBUSY;
365 if (first == parent)
366 goto out;
368 /* Resource fully contained by the clashing resource? Recurse into it */
369 if (first->start <= new->start && first->end >= new->end) {
370 parent = first;
371 goto begin;
374 for (next = first; ; next = next->sibling) {
375 /* Partial overlap? Bad, and unfixable */
376 if (next->start < new->start || next->end > new->end)
377 goto out;
378 if (!next->sibling)
379 break;
380 if (next->sibling->start > new->end)
381 break;
384 result = 0;
386 new->parent = parent;
387 new->sibling = next->sibling;
388 new->child = first;
390 next->sibling = NULL;
391 for (next = first; next; next = next->sibling)
392 next->parent = new;
394 if (parent->child == first) {
395 parent->child = new;
396 } else {
397 next = parent->child;
398 while (next->sibling != first)
399 next = next->sibling;
400 next->sibling = new;
403 out:
404 write_unlock(&resource_lock);
405 return result;
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;
419 int result = -EBUSY;
421 write_lock(&resource_lock);
423 if ((start < parent->start) || (end > parent->end))
424 goto out;
426 for (tmp = res->child; tmp; tmp = tmp->sibling) {
427 if ((tmp->start < start) || (tmp->end > end))
428 goto out;
431 if (res->sibling && (res->sibling->start <= end))
432 goto out;
434 tmp = parent->child;
435 if (tmp != res) {
436 while (tmp->sibling != res)
437 tmp = tmp->sibling;
438 if (start <= tmp->end)
439 goto out;
442 res->start = start;
443 res->end = end;
444 result = 0;
446 out:
447 write_unlock(&resource_lock);
448 return result;
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,
467 const char *name)
469 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
471 if (res) {
472 res->name = name;
473 res->start = start;
474 res->end = start + n - 1;
475 res->flags = IORESOURCE_BUSY;
477 write_lock(&resource_lock);
479 for (;;) {
480 struct resource *conflict;
482 conflict = __request_resource(parent, res);
483 if (!conflict)
484 break;
485 if (conflict != parent) {
486 parent = conflict;
487 if (!(conflict->flags & IORESOURCE_BUSY))
488 continue;
491 /* Uhhuh, that didn't work out.. */
492 kfree(res);
493 res = NULL;
494 break;
496 write_unlock(&resource_lock);
498 return res;
501 EXPORT_SYMBOL(__request_region);
503 int __check_region(struct resource *parent, resource_size_t start,
504 resource_size_t n)
506 struct resource * res;
508 res = __request_region(parent, start, n, "check-region");
509 if (!res)
510 return -EBUSY;
512 release_resource(res);
513 kfree(res);
514 return 0;
517 EXPORT_SYMBOL(__check_region);
519 void __release_region(struct resource *parent, resource_size_t start,
520 resource_size_t n)
522 struct resource **p;
523 resource_size_t end;
525 p = &parent->child;
526 end = start + n - 1;
528 write_lock(&resource_lock);
530 for (;;) {
531 struct resource *res = *p;
533 if (!res)
534 break;
535 if (res->start <= start && res->end >= end) {
536 if (!(res->flags & IORESOURCE_BUSY)) {
537 p = &res->child;
538 continue;
540 if (res->start != start || res->end != end)
541 break;
542 *p = res->sibling;
543 write_unlock(&resource_lock);
544 kfree(res);
545 return;
547 p = &res->sibling;
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.
562 #define MAXRESERVE 4
563 static int __init reserve_setup(char *str)
565 static int reserved;
566 static struct resource reserve[MAXRESERVE];
568 for (;;) {
569 int io_start, io_num;
570 int x = reserved;
572 if (get_option (&str, &io_start) != 2)
573 break;
574 if (get_option (&str, &io_num) == 0)
575 break;
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;
582 res->child = NULL;
583 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
584 reserved = x+1;
587 return 1;
590 __setup("reserve=", reserve_setup);