]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/resource.c
tg3: Fix message 80 char violations
[net-next-2.6.git] / kernel / resource.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4 10#include <linux/module.h>
1da177e4
LT
11#include <linux/errno.h>
12#include <linux/ioport.h>
13#include <linux/init.h>
14#include <linux/slab.h>
15#include <linux/spinlock.h>
16#include <linux/fs.h>
17#include <linux/proc_fs.h>
18#include <linux/seq_file.h>
9ac7849e 19#include <linux/device.h>
d68612b2 20#include <linux/pfn.h>
1da177e4
LT
21#include <asm/io.h>
22
23
24struct resource ioport_resource = {
25 .name = "PCI IO",
6550e07f 26 .start = 0,
1da177e4
LT
27 .end = IO_SPACE_LIMIT,
28 .flags = IORESOURCE_IO,
29};
1da177e4
LT
30EXPORT_SYMBOL(ioport_resource);
31
32struct resource iomem_resource = {
33 .name = "PCI mem",
6550e07f
GKH
34 .start = 0,
35 .end = -1,
1da177e4
LT
36 .flags = IORESOURCE_MEM,
37};
1da177e4
LT
38EXPORT_SYMBOL(iomem_resource);
39
40static DEFINE_RWLOCK(resource_lock);
41
1da177e4
LT
42static void *r_next(struct seq_file *m, void *v, loff_t *pos)
43{
44 struct resource *p = v;
45 (*pos)++;
46 if (p->child)
47 return p->child;
48 while (!p->sibling && p->parent)
49 p = p->parent;
50 return p->sibling;
51}
52
13eb8375
IM
53#ifdef CONFIG_PROC_FS
54
55enum { MAX_IORES_LEVEL = 5 };
56
1da177e4
LT
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;
685143ac 84 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
1da177e4 85 depth * 2, "",
685143ac
GKH
86 width, (unsigned long long) r->start,
87 width, (unsigned long long) r->end,
1da177e4
LT
88 r->name ? r->name : "<BAD>");
89 return 0;
90}
91
15ad7cdc 92static const struct seq_operations resource_op = {
1da177e4
LT
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
15ad7cdc 119static const struct file_operations proc_ioports_operations = {
1da177e4
LT
120 .open = ioports_open,
121 .read = seq_read,
122 .llseek = seq_lseek,
123 .release = seq_release,
124};
125
15ad7cdc 126static const struct file_operations proc_iomem_operations = {
1da177e4
LT
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{
c33fff0a
DL
135 proc_create("ioports", 0, NULL, &proc_ioports_operations);
136 proc_create("iomem", 0, NULL, &proc_iomem_operations);
1da177e4
LT
137 return 0;
138}
139__initcall(ioresources_init);
140
141#endif /* CONFIG_PROC_FS */
142
143/* Return the conflict entry if you can't request it */
144static struct resource * __request_resource(struct resource *root, struct resource *new)
145{
d75fc8bb
GKH
146 resource_size_t start = new->start;
147 resource_size_t end = new->end;
1da177e4
LT
148 struct resource *tmp, **p;
149
150 if (end < start)
151 return root;
152 if (start < root->start)
153 return root;
154 if (end > root->end)
155 return root;
156 p = &root->child;
157 for (;;) {
158 tmp = *p;
159 if (!tmp || tmp->start > end) {
160 new->sibling = tmp;
161 *p = new;
162 new->parent = root;
163 return NULL;
164 }
165 p = &tmp->sibling;
166 if (tmp->end < start)
167 continue;
168 return tmp;
169 }
170}
171
172static int __release_resource(struct resource *old)
173{
174 struct resource *tmp, **p;
175
176 p = &old->parent->child;
177 for (;;) {
178 tmp = *p;
179 if (!tmp)
180 break;
181 if (tmp == old) {
182 *p = tmp->sibling;
183 old->parent = NULL;
184 return 0;
185 }
186 p = &tmp->sibling;
187 }
188 return -EINVAL;
189}
190
5eeec0ec
YL
191static void __release_child_resources(struct resource *r)
192{
193 struct resource *tmp, *p;
194 resource_size_t size;
195
196 p = r->child;
197 r->child = NULL;
198 while (p) {
199 tmp = p;
200 p = p->sibling;
201
202 tmp->parent = NULL;
203 tmp->sibling = NULL;
204 __release_child_resources(tmp);
205
206 printk(KERN_DEBUG "release child resource %pR\n", tmp);
207 /* need to restore size, and keep flags */
208 size = resource_size(tmp);
209 tmp->start = 0;
210 tmp->end = size - 1;
211 }
212}
213
214void release_child_resources(struct resource *r)
215{
216 write_lock(&resource_lock);
217 __release_child_resources(r);
218 write_unlock(&resource_lock);
219}
220
e1ca66d1
RD
221/**
222 * request_resource - request and reserve an I/O or memory resource
223 * @root: root resource descriptor
224 * @new: resource descriptor desired by caller
225 *
226 * Returns 0 for success, negative error code on error.
227 */
1da177e4
LT
228int request_resource(struct resource *root, struct resource *new)
229{
230 struct resource *conflict;
231
232 write_lock(&resource_lock);
233 conflict = __request_resource(root, new);
234 write_unlock(&resource_lock);
235 return conflict ? -EBUSY : 0;
236}
237
238EXPORT_SYMBOL(request_resource);
239
e1ca66d1
RD
240/**
241 * release_resource - release a previously reserved resource
242 * @old: resource pointer
243 */
1da177e4
LT
244int release_resource(struct resource *old)
245{
246 int retval;
247
248 write_lock(&resource_lock);
249 retval = __release_resource(old);
250 write_unlock(&resource_lock);
251 return retval;
252}
253
254EXPORT_SYMBOL(release_resource);
255
908eedc6 256#if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
2842f114
KH
257/*
258 * Finds the lowest memory reosurce exists within [res->start.res->end)
908eedc6 259 * the caller must specify res->start, res->end, res->flags and "name".
2842f114
KH
260 * If found, returns 0, res is overwritten, if not found, returns -1.
261 */
908eedc6 262static int find_next_system_ram(struct resource *res, char *name)
2842f114
KH
263{
264 resource_size_t start, end;
265 struct resource *p;
266
267 BUG_ON(!res);
268
269 start = res->start;
270 end = res->end;
58c1b5b0 271 BUG_ON(start >= end);
2842f114
KH
272
273 read_lock(&resource_lock);
274 for (p = iomem_resource.child; p ; p = p->sibling) {
275 /* system ram is just marked as IORESOURCE_MEM */
276 if (p->flags != res->flags)
277 continue;
908eedc6
KH
278 if (name && strcmp(p->name, name))
279 continue;
2842f114
KH
280 if (p->start > end) {
281 p = NULL;
282 break;
283 }
58c1b5b0 284 if ((p->end >= start) && (p->start < end))
2842f114
KH
285 break;
286 }
287 read_unlock(&resource_lock);
288 if (!p)
289 return -1;
290 /* copy data */
0f04ab5e
KH
291 if (res->start < p->start)
292 res->start = p->start;
293 if (res->end > p->end)
294 res->end = p->end;
2842f114
KH
295 return 0;
296}
908eedc6
KH
297
298/*
299 * This function calls callback against all memory range of "System RAM"
300 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
301 * Now, this function is only for "System RAM".
302 */
303int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
304 void *arg, int (*func)(unsigned long, unsigned long, void *))
75884fb1
KH
305{
306 struct resource res;
37b99dd5 307 unsigned long pfn, end_pfn;
75884fb1
KH
308 u64 orig_end;
309 int ret = -1;
908eedc6 310
75884fb1
KH
311 res.start = (u64) start_pfn << PAGE_SHIFT;
312 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
887c3cb1 313 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
75884fb1 314 orig_end = res.end;
908eedc6
KH
315 while ((res.start < res.end) &&
316 (find_next_system_ram(&res, "System RAM") >= 0)) {
37b99dd5
WF
317 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
318 end_pfn = (res.end + 1) >> PAGE_SHIFT;
319 if (end_pfn > pfn)
f4149660 320 ret = (*func)(pfn, end_pfn - pfn, arg);
75884fb1
KH
321 if (ret)
322 break;
323 res.start = res.end + 1;
324 res.end = orig_end;
325 }
326 return ret;
327}
328
2842f114
KH
329#endif
330
61ef2489
WF
331static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
332{
333 return 1;
334}
335/*
336 * This generic page_is_ram() returns true if specified address is
337 * registered as "System RAM" in iomem_resource list.
338 */
e5273007 339int __weak page_is_ram(unsigned long pfn)
61ef2489
WF
340{
341 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
342}
343
1da177e4
LT
344/*
345 * Find empty slot in the resource tree given range and alignment.
346 */
347static int find_resource(struct resource *root, struct resource *new,
d75fc8bb
GKH
348 resource_size_t size, resource_size_t min,
349 resource_size_t max, resource_size_t align,
b26b2d49 350 resource_size_t (*alignf)(void *,
3b7a17fc 351 const struct resource *,
b26b2d49
DB
352 resource_size_t,
353 resource_size_t),
1da177e4
LT
354 void *alignf_data)
355{
356 struct resource *this = root->child;
0e2c8b8f 357 struct resource tmp = *new;
1da177e4 358
0e2c8b8f 359 tmp.start = root->start;
1da177e4
LT
360 /*
361 * Skip past an allocated resource that starts at 0, since the assignment
0e2c8b8f 362 * of this->start - 1 to tmp->end below would cause an underflow.
1da177e4
LT
363 */
364 if (this && this->start == 0) {
0e2c8b8f 365 tmp.start = this->end + 1;
1da177e4
LT
366 this = this->sibling;
367 }
368 for(;;) {
369 if (this)
0e2c8b8f 370 tmp.end = this->start - 1;
1da177e4 371 else
0e2c8b8f
DB
372 tmp.end = root->end;
373 if (tmp.start < min)
374 tmp.start = min;
375 if (tmp.end > max)
376 tmp.end = max;
377 tmp.start = ALIGN(tmp.start, align);
1da177e4 378 if (alignf)
b26b2d49 379 tmp.start = alignf(alignf_data, &tmp, size, align);
0e2c8b8f
DB
380 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
381 new->start = tmp.start;
382 new->end = tmp.start + size - 1;
1da177e4
LT
383 return 0;
384 }
385 if (!this)
386 break;
0e2c8b8f 387 tmp.start = this->end + 1;
1da177e4
LT
388 this = this->sibling;
389 }
390 return -EBUSY;
391}
392
e1ca66d1
RD
393/**
394 * allocate_resource - allocate empty slot in the resource tree given range & alignment
395 * @root: root resource descriptor
396 * @new: resource descriptor desired by caller
397 * @size: requested resource region size
398 * @min: minimum size to allocate
399 * @max: maximum size to allocate
400 * @align: alignment requested, in bytes
401 * @alignf: alignment function, optional, called if not NULL
402 * @alignf_data: arbitrary data to pass to the @alignf function
1da177e4
LT
403 */
404int allocate_resource(struct resource *root, struct resource *new,
d75fc8bb
GKH
405 resource_size_t size, resource_size_t min,
406 resource_size_t max, resource_size_t align,
b26b2d49 407 resource_size_t (*alignf)(void *,
3b7a17fc 408 const struct resource *,
b26b2d49
DB
409 resource_size_t,
410 resource_size_t),
1da177e4
LT
411 void *alignf_data)
412{
413 int err;
414
415 write_lock(&resource_lock);
416 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
417 if (err >= 0 && __request_resource(root, new))
418 err = -EBUSY;
419 write_unlock(&resource_lock);
420 return err;
421}
422
423EXPORT_SYMBOL(allocate_resource);
424
bef69ea0
LT
425/*
426 * Insert a resource into the resource tree. If successful, return NULL,
427 * otherwise return the conflicting resource (compare to __request_resource())
1da177e4 428 */
bef69ea0 429static struct resource * __insert_resource(struct resource *parent, struct resource *new)
1da177e4 430{
1da177e4
LT
431 struct resource *first, *next;
432
d33b6fba 433 for (;; parent = first) {
d33b6fba
MW
434 first = __request_resource(parent, new);
435 if (!first)
bef69ea0 436 return first;
d33b6fba 437
d33b6fba 438 if (first == parent)
bef69ea0 439 return first;
d33b6fba
MW
440
441 if ((first->start > new->start) || (first->end < new->end))
442 break;
443 if ((first->start == new->start) && (first->end == new->end))
444 break;
1da177e4
LT
445 }
446
447 for (next = first; ; next = next->sibling) {
448 /* Partial overlap? Bad, and unfixable */
449 if (next->start < new->start || next->end > new->end)
bef69ea0 450 return next;
1da177e4
LT
451 if (!next->sibling)
452 break;
453 if (next->sibling->start > new->end)
454 break;
455 }
456
1da177e4
LT
457 new->parent = parent;
458 new->sibling = next->sibling;
459 new->child = first;
460
461 next->sibling = NULL;
462 for (next = first; next; next = next->sibling)
463 next->parent = new;
464
465 if (parent->child == first) {
466 parent->child = new;
467 } else {
468 next = parent->child;
469 while (next->sibling != first)
470 next = next->sibling;
471 next->sibling = new;
472 }
bef69ea0
LT
473 return NULL;
474}
1da177e4 475
bef69ea0
LT
476/**
477 * insert_resource - Inserts a resource in the resource tree
478 * @parent: parent of the new resource
479 * @new: new resource to insert
480 *
481 * Returns 0 on success, -EBUSY if the resource can't be inserted.
482 *
483 * This function is equivalent to request_resource when no conflict
484 * happens. If a conflict happens, and the conflicting resources
485 * entirely fit within the range of the new resource, then the new
486 * resource is inserted and the conflicting resources become children of
487 * the new resource.
488 */
489int insert_resource(struct resource *parent, struct resource *new)
490{
491 struct resource *conflict;
492
493 write_lock(&resource_lock);
494 conflict = __insert_resource(parent, new);
495 write_unlock(&resource_lock);
496 return conflict ? -EBUSY : 0;
497}
498
499/**
500 * insert_resource_expand_to_fit - Insert a resource into the resource tree
6781f4ae 501 * @root: root resource descriptor
bef69ea0
LT
502 * @new: new resource to insert
503 *
504 * Insert a resource into the resource tree, possibly expanding it in order
505 * to make it encompass any conflicting resources.
506 */
507void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
508{
509 if (new->parent)
510 return;
511
512 write_lock(&resource_lock);
513 for (;;) {
514 struct resource *conflict;
515
516 conflict = __insert_resource(root, new);
517 if (!conflict)
518 break;
519 if (conflict == root)
520 break;
521
522 /* Ok, expand resource to cover the conflict, then try again .. */
523 if (conflict->start < new->start)
524 new->start = conflict->start;
525 if (conflict->end > new->end)
526 new->end = conflict->end;
527
528 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
529 }
1da177e4 530 write_unlock(&resource_lock);
1da177e4
LT
531}
532
e1ca66d1
RD
533/**
534 * adjust_resource - modify a resource's start and size
535 * @res: resource to modify
536 * @start: new start value
537 * @size: new size
538 *
1da177e4 539 * Given an existing resource, change its start and size to match the
e1ca66d1
RD
540 * arguments. Returns 0 on success, -EBUSY if it can't fit.
541 * Existing children of the resource are assumed to be immutable.
1da177e4 542 */
d75fc8bb 543int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
1da177e4
LT
544{
545 struct resource *tmp, *parent = res->parent;
d75fc8bb 546 resource_size_t end = start + size - 1;
1da177e4
LT
547 int result = -EBUSY;
548
549 write_lock(&resource_lock);
550
551 if ((start < parent->start) || (end > parent->end))
552 goto out;
553
554 for (tmp = res->child; tmp; tmp = tmp->sibling) {
555 if ((tmp->start < start) || (tmp->end > end))
556 goto out;
557 }
558
559 if (res->sibling && (res->sibling->start <= end))
560 goto out;
561
562 tmp = parent->child;
563 if (tmp != res) {
564 while (tmp->sibling != res)
565 tmp = tmp->sibling;
566 if (start <= tmp->end)
567 goto out;
568 }
569
570 res->start = start;
571 res->end = end;
572 result = 0;
573
574 out:
575 write_unlock(&resource_lock);
576 return result;
577}
578
268364a0
YL
579static void __init __reserve_region_with_split(struct resource *root,
580 resource_size_t start, resource_size_t end,
581 const char *name)
582{
583 struct resource *parent = root;
584 struct resource *conflict;
42c02023 585 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
268364a0
YL
586
587 if (!res)
588 return;
589
590 res->name = name;
591 res->start = start;
592 res->end = end;
593 res->flags = IORESOURCE_BUSY;
594
ff54250a
LT
595 conflict = __request_resource(parent, res);
596 if (!conflict)
597 return;
268364a0 598
ff54250a
LT
599 /* failed, split and try again */
600 kfree(res);
268364a0 601
ff54250a
LT
602 /* conflict covered whole area */
603 if (conflict->start <= start && conflict->end >= end)
604 return;
268364a0 605
ff54250a
LT
606 if (conflict->start > start)
607 __reserve_region_with_split(root, start, conflict->start-1, name);
608 if (conflict->end < end)
609 __reserve_region_with_split(root, conflict->end+1, end, name);
268364a0
YL
610}
611
bea92112 612void __init reserve_region_with_split(struct resource *root,
268364a0
YL
613 resource_size_t start, resource_size_t end,
614 const char *name)
615{
616 write_lock(&resource_lock);
617 __reserve_region_with_split(root, start, end, name);
618 write_unlock(&resource_lock);
619}
620
1da177e4
LT
621EXPORT_SYMBOL(adjust_resource);
622
88452565
IK
623/**
624 * resource_alignment - calculate resource's alignment
625 * @res: resource pointer
626 *
627 * Returns alignment on success, 0 (invalid alignment) on failure.
628 */
629resource_size_t resource_alignment(struct resource *res)
630{
631 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
632 case IORESOURCE_SIZEALIGN:
1a4e564b 633 return resource_size(res);
88452565
IK
634 case IORESOURCE_STARTALIGN:
635 return res->start;
636 default:
637 return 0;
638 }
639}
640
1da177e4
LT
641/*
642 * This is compatibility stuff for IO resources.
643 *
644 * Note how this, unlike the above, knows about
645 * the IO flag meanings (busy etc).
646 *
e1ca66d1 647 * request_region creates a new busy region.
1da177e4 648 *
e1ca66d1 649 * check_region returns non-zero if the area is already busy.
1da177e4 650 *
e1ca66d1
RD
651 * release_region releases a matching busy region.
652 */
653
654/**
655 * __request_region - create a new busy resource region
656 * @parent: parent resource descriptor
657 * @start: resource start address
658 * @n: resource region size
659 * @name: reserving caller's ID string
6ae301e8 660 * @flags: IO resource flags
1da177e4 661 */
d75fc8bb
GKH
662struct resource * __request_region(struct resource *parent,
663 resource_size_t start, resource_size_t n,
e8de1481 664 const char *name, int flags)
1da177e4 665{
dd392710 666 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
1da177e4 667
c26ec88e
BH
668 if (!res)
669 return NULL;
670
671 res->name = name;
672 res->start = start;
673 res->end = start + n - 1;
674 res->flags = IORESOURCE_BUSY;
e8de1481 675 res->flags |= flags;
c26ec88e
BH
676
677 write_lock(&resource_lock);
678
679 for (;;) {
680 struct resource *conflict;
681
682 conflict = __request_resource(parent, res);
683 if (!conflict)
1da177e4 684 break;
c26ec88e
BH
685 if (conflict != parent) {
686 parent = conflict;
687 if (!(conflict->flags & IORESOURCE_BUSY))
688 continue;
1da177e4 689 }
c26ec88e
BH
690
691 /* Uhhuh, that didn't work out.. */
692 kfree(res);
693 res = NULL;
694 break;
1da177e4 695 }
c26ec88e 696 write_unlock(&resource_lock);
1da177e4
LT
697 return res;
698}
1da177e4
LT
699EXPORT_SYMBOL(__request_region);
700
e1ca66d1
RD
701/**
702 * __check_region - check if a resource region is busy or free
703 * @parent: parent resource descriptor
704 * @start: resource start address
705 * @n: resource region size
706 *
707 * Returns 0 if the region is free at the moment it is checked,
708 * returns %-EBUSY if the region is busy.
709 *
710 * NOTE:
711 * This function is deprecated because its use is racy.
712 * Even if it returns 0, a subsequent call to request_region()
713 * may fail because another driver etc. just allocated the region.
714 * Do NOT use it. It will be removed from the kernel.
715 */
d75fc8bb
GKH
716int __check_region(struct resource *parent, resource_size_t start,
717 resource_size_t n)
1da177e4
LT
718{
719 struct resource * res;
720
e8de1481 721 res = __request_region(parent, start, n, "check-region", 0);
1da177e4
LT
722 if (!res)
723 return -EBUSY;
724
725 release_resource(res);
726 kfree(res);
727 return 0;
728}
1da177e4
LT
729EXPORT_SYMBOL(__check_region);
730
e1ca66d1
RD
731/**
732 * __release_region - release a previously reserved resource region
733 * @parent: parent resource descriptor
734 * @start: resource start address
735 * @n: resource region size
736 *
737 * The described resource region must match a currently busy region.
738 */
d75fc8bb
GKH
739void __release_region(struct resource *parent, resource_size_t start,
740 resource_size_t n)
1da177e4
LT
741{
742 struct resource **p;
d75fc8bb 743 resource_size_t end;
1da177e4
LT
744
745 p = &parent->child;
746 end = start + n - 1;
747
748 write_lock(&resource_lock);
749
750 for (;;) {
751 struct resource *res = *p;
752
753 if (!res)
754 break;
755 if (res->start <= start && res->end >= end) {
756 if (!(res->flags & IORESOURCE_BUSY)) {
757 p = &res->child;
758 continue;
759 }
760 if (res->start != start || res->end != end)
761 break;
762 *p = res->sibling;
763 write_unlock(&resource_lock);
764 kfree(res);
765 return;
766 }
767 p = &res->sibling;
768 }
769
770 write_unlock(&resource_lock);
771
685143ac
GKH
772 printk(KERN_WARNING "Trying to free nonexistent resource "
773 "<%016llx-%016llx>\n", (unsigned long long)start,
774 (unsigned long long)end);
1da177e4 775}
1da177e4
LT
776EXPORT_SYMBOL(__release_region);
777
9ac7849e
TH
778/*
779 * Managed region resource
780 */
781struct region_devres {
782 struct resource *parent;
783 resource_size_t start;
784 resource_size_t n;
785};
786
787static void devm_region_release(struct device *dev, void *res)
788{
789 struct region_devres *this = res;
790
791 __release_region(this->parent, this->start, this->n);
792}
793
794static int devm_region_match(struct device *dev, void *res, void *match_data)
795{
796 struct region_devres *this = res, *match = match_data;
797
798 return this->parent == match->parent &&
799 this->start == match->start && this->n == match->n;
800}
801
802struct resource * __devm_request_region(struct device *dev,
803 struct resource *parent, resource_size_t start,
804 resource_size_t n, const char *name)
805{
806 struct region_devres *dr = NULL;
807 struct resource *res;
808
809 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
810 GFP_KERNEL);
811 if (!dr)
812 return NULL;
813
814 dr->parent = parent;
815 dr->start = start;
816 dr->n = n;
817
e8de1481 818 res = __request_region(parent, start, n, name, 0);
9ac7849e
TH
819 if (res)
820 devres_add(dev, dr);
821 else
822 devres_free(dr);
823
824 return res;
825}
826EXPORT_SYMBOL(__devm_request_region);
827
828void __devm_release_region(struct device *dev, struct resource *parent,
829 resource_size_t start, resource_size_t n)
830{
831 struct region_devres match_data = { parent, start, n };
832
833 __release_region(parent, start, n);
834 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
835 &match_data));
836}
837EXPORT_SYMBOL(__devm_release_region);
838
1da177e4
LT
839/*
840 * Called from init/main.c to reserve IO ports.
841 */
842#define MAXRESERVE 4
843static int __init reserve_setup(char *str)
844{
845 static int reserved;
846 static struct resource reserve[MAXRESERVE];
847
848 for (;;) {
8bc1ad7d 849 unsigned int io_start, io_num;
1da177e4
LT
850 int x = reserved;
851
852 if (get_option (&str, &io_start) != 2)
853 break;
854 if (get_option (&str, &io_num) == 0)
855 break;
856 if (x < MAXRESERVE) {
857 struct resource *res = reserve + x;
858 res->name = "reserved";
859 res->start = io_start;
860 res->end = io_start + io_num - 1;
861 res->flags = IORESOURCE_BUSY;
862 res->child = NULL;
863 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
864 reserved = x+1;
865 }
866 }
867 return 1;
868}
869
870__setup("reserve=", reserve_setup);
379daf62
SS
871
872/*
873 * Check if the requested addr and size spans more than any slot in the
874 * iomem resource tree.
875 */
876int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
877{
878 struct resource *p = &iomem_resource;
879 int err = 0;
880 loff_t l;
881
882 read_lock(&resource_lock);
883 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
884 /*
885 * We can probably skip the resources without
886 * IORESOURCE_IO attribute?
887 */
888 if (p->start >= addr + size)
889 continue;
890 if (p->end < addr)
891 continue;
d68612b2
SS
892 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
893 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
379daf62 894 continue;
3ac52669
AV
895 /*
896 * if a resource is "BUSY", it's not a hardware resource
897 * but a driver mapping of such a resource; we don't want
898 * to warn for those; some drivers legitimately map only
899 * partial hardware resources. (example: vesafb)
900 */
901 if (p->flags & IORESOURCE_BUSY)
902 continue;
903
379daf62
SS
904 printk(KERN_WARNING "resource map sanity check conflict: "
905 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
13eb8375
IM
906 (unsigned long long)addr,
907 (unsigned long long)(addr + size - 1),
908 (unsigned long long)p->start,
909 (unsigned long long)p->end,
910 p->name);
379daf62
SS
911 err = -1;
912 break;
913 }
914 read_unlock(&resource_lock);
915
916 return err;
917}
e8de1481
AV
918
919#ifdef CONFIG_STRICT_DEVMEM
920static int strict_iomem_checks = 1;
921#else
922static int strict_iomem_checks;
923#endif
924
925/*
926 * check if an address is reserved in the iomem resource tree
927 * returns 1 if reserved, 0 if not reserved.
928 */
929int iomem_is_exclusive(u64 addr)
930{
931 struct resource *p = &iomem_resource;
932 int err = 0;
933 loff_t l;
934 int size = PAGE_SIZE;
935
936 if (!strict_iomem_checks)
937 return 0;
938
939 addr = addr & PAGE_MASK;
940
941 read_lock(&resource_lock);
942 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
943 /*
944 * We can probably skip the resources without
945 * IORESOURCE_IO attribute?
946 */
947 if (p->start >= addr + size)
948 break;
949 if (p->end < addr)
950 continue;
951 if (p->flags & IORESOURCE_BUSY &&
952 p->flags & IORESOURCE_EXCLUSIVE) {
953 err = 1;
954 break;
955 }
956 }
957 read_unlock(&resource_lock);
958
959 return err;
960}
961
962static int __init strict_iomem(char *str)
963{
964 if (strstr(str, "relaxed"))
965 strict_iomem_checks = 0;
966 if (strstr(str, "strict"))
967 strict_iomem_checks = 1;
968 return 1;
969}
970
971__setup("iomem=", strict_iomem);