blob: cc73029088a785d941e3281e0f147829b752cf9f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/resource.c
3 *
4 * Copyright (C) 1999 Linus Torvalds
5 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
6 *
7 * Arbitrary resource management.
8 */
9
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>
22
23
24struct resource ioport_resource = {
25 .name = "PCI IO",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070026 .start = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 .end = IO_SPACE_LIMIT,
28 .flags = IORESOURCE_IO,
29};
Linus Torvalds1da177e2005-04-16 15:20:36 -070030EXPORT_SYMBOL(ioport_resource);
31
32struct resource iomem_resource = {
33 .name = "PCI mem",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070034 .start = 0,
35 .end = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .flags = IORESOURCE_MEM,
37};
Linus Torvalds1da177e2005-04-16 15:20:36 -070038EXPORT_SYMBOL(iomem_resource);
39
40static DEFINE_RWLOCK(resource_lock);
41
42#ifdef CONFIG_PROC_FS
43
44enum { MAX_IORES_LEVEL = 5 };
45
46static void *r_next(struct seq_file *m, void *v, loff_t *pos)
47{
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;
55}
56
57static void *r_start(struct seq_file *m, loff_t *pos)
58 __acquires(resource_lock)
59{
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))
64 ;
65 return p;
66}
67
68static void r_stop(struct seq_file *m, void *v)
69 __releases(resource_lock)
70{
71 read_unlock(&resource_lock);
72}
73
74static int r_show(struct seq_file *m, void *v)
75{
76 struct resource *root = m->private;
77 struct resource *r = v, *p;
78 int width = root->end < 0x10000 ? 4 : 8;
79 int depth;
80
81 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
82 if (p->parent == root)
83 break;
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -070084 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 depth * 2, "",
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -070086 width, (unsigned long long) r->start,
87 width, (unsigned long long) r->end,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 r->name ? r->name : "<BAD>");
89 return 0;
90}
91
92static struct seq_operations resource_op = {
93 .start = r_start,
94 .next = r_next,
95 .stop = r_stop,
96 .show = r_show,
97};
98
99static int ioports_open(struct inode *inode, struct file *file)
100{
101 int res = seq_open(file, &resource_op);
102 if (!res) {
103 struct seq_file *m = file->private_data;
104 m->private = &ioport_resource;
105 }
106 return res;
107}
108
109static int iomem_open(struct inode *inode, struct file *file)
110{
111 int res = seq_open(file, &resource_op);
112 if (!res) {
113 struct seq_file *m = file->private_data;
114 m->private = &iomem_resource;
115 }
116 return res;
117}
118
119static struct file_operations proc_ioports_operations = {
120 .open = ioports_open,
121 .read = seq_read,
122 .llseek = seq_lseek,
123 .release = seq_release,
124};
125
126static struct file_operations proc_iomem_operations = {
127 .open = iomem_open,
128 .read = seq_read,
129 .llseek = seq_lseek,
130 .release = seq_release,
131};
132
133static int __init ioresources_init(void)
134{
135 struct proc_dir_entry *entry;
136
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;
144}
145__initcall(ioresources_init);
146
147#endif /* CONFIG_PROC_FS */
148
149/* Return the conflict entry if you can't request it */
150static struct resource * __request_resource(struct resource *root, struct resource *new)
151{
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700152 resource_size_t start = new->start;
153 resource_size_t end = new->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct resource *tmp, **p;
155
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;
170 }
171 p = &tmp->sibling;
172 if (tmp->end < start)
173 continue;
174 return tmp;
175 }
176}
177
178static int __release_resource(struct resource *old)
179{
180 struct resource *tmp, **p;
181
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;
191 }
192 p = &tmp->sibling;
193 }
194 return -EINVAL;
195}
196
197int request_resource(struct resource *root, struct resource *new)
198{
199 struct resource *conflict;
200
201 write_lock(&resource_lock);
202 conflict = __request_resource(root, new);
203 write_unlock(&resource_lock);
204 return conflict ? -EBUSY : 0;
205}
206
207EXPORT_SYMBOL(request_resource);
208
209struct resource *____request_resource(struct resource *root, struct resource *new)
210{
211 struct resource *conflict;
212
213 write_lock(&resource_lock);
214 conflict = __request_resource(root, new);
215 write_unlock(&resource_lock);
216 return conflict;
217}
218
219EXPORT_SYMBOL(____request_resource);
220
221int release_resource(struct resource *old)
222{
223 int retval;
224
225 write_lock(&resource_lock);
226 retval = __release_resource(old);
227 write_unlock(&resource_lock);
228 return retval;
229}
230
231EXPORT_SYMBOL(release_resource);
232
233/*
234 * Find empty slot in the resource tree given range and alignment.
235 */
236static int find_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700237 resource_size_t size, resource_size_t min,
238 resource_size_t max, resource_size_t align,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 void (*alignf)(void *, struct resource *,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700240 resource_size_t, resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 void *alignf_data)
242{
243 struct resource *this = root->child;
244
245 new->start = root->start;
246 /*
247 * Skip past an allocated resource that starts at 0, since the assignment
248 * of this->start - 1 to new->end below would cause an underflow.
249 */
250 if (this && this->start == 0) {
251 new->start = this->end + 1;
252 this = this->sibling;
253 }
254 for(;;) {
255 if (this)
256 new->end = this->start - 1;
257 else
258 new->end = root->end;
259 if (new->start < min)
260 new->start = min;
261 if (new->end > max)
262 new->end = max;
Nick Wilson8c0e33c2005-06-25 14:59:00 -0700263 new->start = ALIGN(new->start, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (alignf)
265 alignf(alignf_data, new, size, align);
Lennert Buytenhekb52402c2005-04-16 15:25:58 -0700266 if (new->start < new->end && new->end - new->start >= size - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 new->end = new->start + size - 1;
268 return 0;
269 }
270 if (!this)
271 break;
272 new->start = this->end + 1;
273 this = this->sibling;
274 }
275 return -EBUSY;
276}
277
278/*
279 * Allocate empty slot in the resource tree given range and alignment.
280 */
281int allocate_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700282 resource_size_t size, resource_size_t min,
283 resource_size_t max, resource_size_t align,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 void (*alignf)(void *, struct resource *,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700285 resource_size_t, resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 void *alignf_data)
287{
288 int err;
289
290 write_lock(&resource_lock);
291 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
292 if (err >= 0 && __request_resource(root, new))
293 err = -EBUSY;
294 write_unlock(&resource_lock);
295 return err;
296}
297
298EXPORT_SYMBOL(allocate_resource);
299
300/**
301 * insert_resource - Inserts a resource in the resource tree
302 * @parent: parent of the new resource
303 * @new: new resource to insert
304 *
305 * Returns 0 on success, -EBUSY if the resource can't be inserted.
306 *
307 * This function is equivalent of request_resource when no conflict
308 * happens. If a conflict happens, and the conflicting resources
309 * entirely fit within the range of the new resource, then the new
310 * resource is inserted and the conflicting resources become childs of
311 * the new resource. Otherwise the new resource becomes the child of
312 * the conflicting resource
313 */
314int insert_resource(struct resource *parent, struct resource *new)
315{
316 int result;
317 struct resource *first, *next;
318
319 write_lock(&resource_lock);
320 begin:
321 result = 0;
322 first = __request_resource(parent, new);
323 if (!first)
324 goto out;
325
326 result = -EBUSY;
327 if (first == parent)
328 goto out;
329
330 /* Resource fully contained by the clashing resource? Recurse into it */
331 if (first->start <= new->start && first->end >= new->end) {
332 parent = first;
333 goto begin;
334 }
335
336 for (next = first; ; next = next->sibling) {
337 /* Partial overlap? Bad, and unfixable */
338 if (next->start < new->start || next->end > new->end)
339 goto out;
340 if (!next->sibling)
341 break;
342 if (next->sibling->start > new->end)
343 break;
344 }
345
346 result = 0;
347
348 new->parent = parent;
349 new->sibling = next->sibling;
350 new->child = first;
351
352 next->sibling = NULL;
353 for (next = first; next; next = next->sibling)
354 next->parent = new;
355
356 if (parent->child == first) {
357 parent->child = new;
358 } else {
359 next = parent->child;
360 while (next->sibling != first)
361 next = next->sibling;
362 next->sibling = new;
363 }
364
365 out:
366 write_unlock(&resource_lock);
367 return result;
368}
369
370EXPORT_SYMBOL(insert_resource);
371
372/*
373 * Given an existing resource, change its start and size to match the
374 * arguments. Returns -EBUSY if it can't fit. Existing children of
375 * the resource are assumed to be immutable.
376 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700377int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct resource *tmp, *parent = res->parent;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700380 resource_size_t end = start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int result = -EBUSY;
382
383 write_lock(&resource_lock);
384
385 if ((start < parent->start) || (end > parent->end))
386 goto out;
387
388 for (tmp = res->child; tmp; tmp = tmp->sibling) {
389 if ((tmp->start < start) || (tmp->end > end))
390 goto out;
391 }
392
393 if (res->sibling && (res->sibling->start <= end))
394 goto out;
395
396 tmp = parent->child;
397 if (tmp != res) {
398 while (tmp->sibling != res)
399 tmp = tmp->sibling;
400 if (start <= tmp->end)
401 goto out;
402 }
403
404 res->start = start;
405 res->end = end;
406 result = 0;
407
408 out:
409 write_unlock(&resource_lock);
410 return result;
411}
412
413EXPORT_SYMBOL(adjust_resource);
414
415/*
416 * This is compatibility stuff for IO resources.
417 *
418 * Note how this, unlike the above, knows about
419 * the IO flag meanings (busy etc).
420 *
421 * Request-region creates a new busy region.
422 *
423 * Check-region returns non-zero if the area is already busy
424 *
425 * Release-region releases a matching busy region.
426 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700427struct resource * __request_region(struct resource *parent,
428 resource_size_t start, resource_size_t n,
429 const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Pekka J Enbergdd392712005-09-06 15:18:31 -0700431 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 res->name = name;
435 res->start = start;
436 res->end = start + n - 1;
437 res->flags = IORESOURCE_BUSY;
438
439 write_lock(&resource_lock);
440
441 for (;;) {
442 struct resource *conflict;
443
444 conflict = __request_resource(parent, res);
445 if (!conflict)
446 break;
447 if (conflict != parent) {
448 parent = conflict;
449 if (!(conflict->flags & IORESOURCE_BUSY))
450 continue;
451 }
452
453 /* Uhhuh, that didn't work out.. */
454 kfree(res);
455 res = NULL;
456 break;
457 }
458 write_unlock(&resource_lock);
459 }
460 return res;
461}
462
463EXPORT_SYMBOL(__request_region);
464
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700465int __check_region(struct resource *parent, resource_size_t start,
466 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct resource * res;
469
470 res = __request_region(parent, start, n, "check-region");
471 if (!res)
472 return -EBUSY;
473
474 release_resource(res);
475 kfree(res);
476 return 0;
477}
478
479EXPORT_SYMBOL(__check_region);
480
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700481void __release_region(struct resource *parent, resource_size_t start,
482 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct resource **p;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700485 resource_size_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 p = &parent->child;
488 end = start + n - 1;
489
490 write_lock(&resource_lock);
491
492 for (;;) {
493 struct resource *res = *p;
494
495 if (!res)
496 break;
497 if (res->start <= start && res->end >= end) {
498 if (!(res->flags & IORESOURCE_BUSY)) {
499 p = &res->child;
500 continue;
501 }
502 if (res->start != start || res->end != end)
503 break;
504 *p = res->sibling;
505 write_unlock(&resource_lock);
506 kfree(res);
507 return;
508 }
509 p = &res->sibling;
510 }
511
512 write_unlock(&resource_lock);
513
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700514 printk(KERN_WARNING "Trying to free nonexistent resource "
515 "<%016llx-%016llx>\n", (unsigned long long)start,
516 (unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519EXPORT_SYMBOL(__release_region);
520
521/*
522 * Called from init/main.c to reserve IO ports.
523 */
524#define MAXRESERVE 4
525static int __init reserve_setup(char *str)
526{
527 static int reserved;
528 static struct resource reserve[MAXRESERVE];
529
530 for (;;) {
531 int io_start, io_num;
532 int x = reserved;
533
534 if (get_option (&str, &io_start) != 2)
535 break;
536 if (get_option (&str, &io_num) == 0)
537 break;
538 if (x < MAXRESERVE) {
539 struct resource *res = reserve + x;
540 res->name = "reserved";
541 res->start = io_start;
542 res->end = io_start + io_num - 1;
543 res->flags = IORESOURCE_BUSY;
544 res->child = NULL;
545 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
546 reserved = x+1;
547 }
548 }
549 return 1;
550}
551
552__setup("reserve=", reserve_setup);