resources: handle overflow when aligning start of available area
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / resource.c
blobe15b922d4ba4da2727298af2441562e277f9f69d
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/module.h>
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/sched.h>
19 #include <linux/seq_file.h>
20 #include <linux/device.h>
21 #include <linux/pfn.h>
22 #include <asm/io.h>
25 struct resource ioport_resource = {
26 .name = "PCI IO",
27 .start = 0,
28 .end = IO_SPACE_LIMIT,
29 .flags = IORESOURCE_IO,
31 EXPORT_SYMBOL(ioport_resource);
33 struct resource iomem_resource = {
34 .name = "PCI mem",
35 .start = 0,
36 .end = -1,
37 .flags = IORESOURCE_MEM,
39 EXPORT_SYMBOL(iomem_resource);
41 static DEFINE_RWLOCK(resource_lock);
43 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
45 struct resource *p = v;
46 (*pos)++;
47 if (p->child)
48 return p->child;
49 while (!p->sibling && p->parent)
50 p = p->parent;
51 return p->sibling;
54 #ifdef CONFIG_PROC_FS
56 enum { MAX_IORES_LEVEL = 5 };
58 static void *r_start(struct seq_file *m, loff_t *pos)
59 __acquires(resource_lock)
61 struct resource *p = m->private;
62 loff_t l = 0;
63 read_lock(&resource_lock);
64 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
66 return p;
69 static void r_stop(struct seq_file *m, void *v)
70 __releases(resource_lock)
72 read_unlock(&resource_lock);
75 static int r_show(struct seq_file *m, void *v)
77 struct resource *root = m->private;
78 struct resource *r = v, *p;
79 int width = root->end < 0x10000 ? 4 : 8;
80 int depth;
82 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
83 if (p->parent == root)
84 break;
85 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
86 depth * 2, "",
87 width, (unsigned long long) r->start,
88 width, (unsigned long long) r->end,
89 r->name ? r->name : "<BAD>");
90 return 0;
93 static const struct seq_operations resource_op = {
94 .start = r_start,
95 .next = r_next,
96 .stop = r_stop,
97 .show = r_show,
100 static int ioports_open(struct inode *inode, struct file *file)
102 int res = seq_open(file, &resource_op);
103 if (!res) {
104 struct seq_file *m = file->private_data;
105 m->private = &ioport_resource;
107 return res;
110 static int iomem_open(struct inode *inode, struct file *file)
112 int res = seq_open(file, &resource_op);
113 if (!res) {
114 struct seq_file *m = file->private_data;
115 m->private = &iomem_resource;
117 return res;
120 static const struct file_operations proc_ioports_operations = {
121 .open = ioports_open,
122 .read = seq_read,
123 .llseek = seq_lseek,
124 .release = seq_release,
127 static const struct file_operations proc_iomem_operations = {
128 .open = iomem_open,
129 .read = seq_read,
130 .llseek = seq_lseek,
131 .release = seq_release,
134 static int __init ioresources_init(void)
136 proc_create("ioports", 0, NULL, &proc_ioports_operations);
137 proc_create("iomem", 0, NULL, &proc_iomem_operations);
138 return 0;
140 __initcall(ioresources_init);
142 #endif /* CONFIG_PROC_FS */
144 /* Return the conflict entry if you can't request it */
145 static struct resource * __request_resource(struct resource *root, struct resource *new)
147 resource_size_t start = new->start;
148 resource_size_t end = new->end;
149 struct resource *tmp, **p;
151 if (end < start)
152 return root;
153 if (start < root->start)
154 return root;
155 if (end > root->end)
156 return root;
157 p = &root->child;
158 for (;;) {
159 tmp = *p;
160 if (!tmp || tmp->start > end) {
161 new->sibling = tmp;
162 *p = new;
163 new->parent = root;
164 return NULL;
166 p = &tmp->sibling;
167 if (tmp->end < start)
168 continue;
169 return tmp;
173 static int __release_resource(struct resource *old)
175 struct resource *tmp, **p;
177 p = &old->parent->child;
178 for (;;) {
179 tmp = *p;
180 if (!tmp)
181 break;
182 if (tmp == old) {
183 *p = tmp->sibling;
184 old->parent = NULL;
185 return 0;
187 p = &tmp->sibling;
189 return -EINVAL;
192 static void __release_child_resources(struct resource *r)
194 struct resource *tmp, *p;
195 resource_size_t size;
197 p = r->child;
198 r->child = NULL;
199 while (p) {
200 tmp = p;
201 p = p->sibling;
203 tmp->parent = NULL;
204 tmp->sibling = NULL;
205 __release_child_resources(tmp);
207 printk(KERN_DEBUG "release child resource %pR\n", tmp);
208 /* need to restore size, and keep flags */
209 size = resource_size(tmp);
210 tmp->start = 0;
211 tmp->end = size - 1;
215 void release_child_resources(struct resource *r)
217 write_lock(&resource_lock);
218 __release_child_resources(r);
219 write_unlock(&resource_lock);
223 * request_resource_conflict - request and reserve an I/O or memory resource
224 * @root: root resource descriptor
225 * @new: resource descriptor desired by caller
227 * Returns 0 for success, conflict resource on error.
229 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
231 struct resource *conflict;
233 write_lock(&resource_lock);
234 conflict = __request_resource(root, new);
235 write_unlock(&resource_lock);
236 return conflict;
240 * request_resource - request and reserve an I/O or memory resource
241 * @root: root resource descriptor
242 * @new: resource descriptor desired by caller
244 * Returns 0 for success, negative error code on error.
246 int request_resource(struct resource *root, struct resource *new)
248 struct resource *conflict;
250 conflict = request_resource_conflict(root, new);
251 return conflict ? -EBUSY : 0;
254 EXPORT_SYMBOL(request_resource);
257 * release_resource - release a previously reserved resource
258 * @old: resource pointer
260 int release_resource(struct resource *old)
262 int retval;
264 write_lock(&resource_lock);
265 retval = __release_resource(old);
266 write_unlock(&resource_lock);
267 return retval;
270 EXPORT_SYMBOL(release_resource);
272 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
274 * Finds the lowest memory reosurce exists within [res->start.res->end)
275 * the caller must specify res->start, res->end, res->flags and "name".
276 * If found, returns 0, res is overwritten, if not found, returns -1.
278 static int find_next_system_ram(struct resource *res, char *name)
280 resource_size_t start, end;
281 struct resource *p;
283 BUG_ON(!res);
285 start = res->start;
286 end = res->end;
287 BUG_ON(start >= end);
289 read_lock(&resource_lock);
290 for (p = iomem_resource.child; p ; p = p->sibling) {
291 /* system ram is just marked as IORESOURCE_MEM */
292 if (p->flags != res->flags)
293 continue;
294 if (name && strcmp(p->name, name))
295 continue;
296 if (p->start > end) {
297 p = NULL;
298 break;
300 if ((p->end >= start) && (p->start < end))
301 break;
303 read_unlock(&resource_lock);
304 if (!p)
305 return -1;
306 /* copy data */
307 if (res->start < p->start)
308 res->start = p->start;
309 if (res->end > p->end)
310 res->end = p->end;
311 return 0;
315 * This function calls callback against all memory range of "System RAM"
316 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
317 * Now, this function is only for "System RAM".
319 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
320 void *arg, int (*func)(unsigned long, unsigned long, void *))
322 struct resource res;
323 unsigned long pfn, end_pfn;
324 u64 orig_end;
325 int ret = -1;
327 res.start = (u64) start_pfn << PAGE_SHIFT;
328 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
329 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
330 orig_end = res.end;
331 while ((res.start < res.end) &&
332 (find_next_system_ram(&res, "System RAM") >= 0)) {
333 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
334 end_pfn = (res.end + 1) >> PAGE_SHIFT;
335 if (end_pfn > pfn)
336 ret = (*func)(pfn, end_pfn - pfn, arg);
337 if (ret)
338 break;
339 res.start = res.end + 1;
340 res.end = orig_end;
342 return ret;
345 #endif
347 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
349 return 1;
352 * This generic page_is_ram() returns true if specified address is
353 * registered as "System RAM" in iomem_resource list.
355 int __weak page_is_ram(unsigned long pfn)
357 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
360 static resource_size_t simple_align_resource(void *data,
361 const struct resource *avail,
362 resource_size_t size,
363 resource_size_t align)
365 return avail->start;
368 static void resource_clip(struct resource *res, resource_size_t min,
369 resource_size_t max)
371 if (res->start < min)
372 res->start = min;
373 if (res->end > max)
374 res->end = max;
377 static bool resource_contains(struct resource *res1, struct resource *res2)
379 return res1->start <= res2->start && res1->end >= res2->end;
383 * Find empty slot in the resource tree given range and alignment.
385 static int find_resource(struct resource *root, struct resource *new,
386 resource_size_t size, resource_size_t min,
387 resource_size_t max, resource_size_t align,
388 resource_size_t (*alignf)(void *,
389 const struct resource *,
390 resource_size_t,
391 resource_size_t),
392 void *alignf_data)
394 struct resource *this = root->child;
395 struct resource tmp = *new, avail, alloc;
397 tmp.start = root->start;
399 * Skip past an allocated resource that starts at 0, since the assignment
400 * of this->start - 1 to tmp->end below would cause an underflow.
402 if (this && this->start == 0) {
403 tmp.start = this->end + 1;
404 this = this->sibling;
406 for(;;) {
407 if (this)
408 tmp.end = this->start - 1;
409 else
410 tmp.end = root->end;
412 resource_clip(&tmp, min, max);
414 /* Check for overflow after ALIGN() */
415 avail = *new;
416 avail.start = ALIGN(tmp.start, align);
417 avail.end = tmp.end;
418 if (avail.start >= tmp.start) {
419 alloc.start = alignf(alignf_data, &avail, size, align);
420 alloc.end = alloc.start + size - 1;
421 if (resource_contains(&avail, &alloc)) {
422 new->start = alloc.start;
423 new->end = alloc.end;
424 return 0;
427 if (!this)
428 break;
429 tmp.start = this->end + 1;
430 this = this->sibling;
432 return -EBUSY;
436 * allocate_resource - allocate empty slot in the resource tree given range & alignment
437 * @root: root resource descriptor
438 * @new: resource descriptor desired by caller
439 * @size: requested resource region size
440 * @min: minimum size to allocate
441 * @max: maximum size to allocate
442 * @align: alignment requested, in bytes
443 * @alignf: alignment function, optional, called if not NULL
444 * @alignf_data: arbitrary data to pass to the @alignf function
446 int allocate_resource(struct resource *root, struct resource *new,
447 resource_size_t size, resource_size_t min,
448 resource_size_t max, resource_size_t align,
449 resource_size_t (*alignf)(void *,
450 const struct resource *,
451 resource_size_t,
452 resource_size_t),
453 void *alignf_data)
455 int err;
457 if (!alignf)
458 alignf = simple_align_resource;
460 write_lock(&resource_lock);
461 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
462 if (err >= 0 && __request_resource(root, new))
463 err = -EBUSY;
464 write_unlock(&resource_lock);
465 return err;
468 EXPORT_SYMBOL(allocate_resource);
471 * Insert a resource into the resource tree. If successful, return NULL,
472 * otherwise return the conflicting resource (compare to __request_resource())
474 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
476 struct resource *first, *next;
478 for (;; parent = first) {
479 first = __request_resource(parent, new);
480 if (!first)
481 return first;
483 if (first == parent)
484 return first;
486 if ((first->start > new->start) || (first->end < new->end))
487 break;
488 if ((first->start == new->start) && (first->end == new->end))
489 break;
492 for (next = first; ; next = next->sibling) {
493 /* Partial overlap? Bad, and unfixable */
494 if (next->start < new->start || next->end > new->end)
495 return next;
496 if (!next->sibling)
497 break;
498 if (next->sibling->start > new->end)
499 break;
502 new->parent = parent;
503 new->sibling = next->sibling;
504 new->child = first;
506 next->sibling = NULL;
507 for (next = first; next; next = next->sibling)
508 next->parent = new;
510 if (parent->child == first) {
511 parent->child = new;
512 } else {
513 next = parent->child;
514 while (next->sibling != first)
515 next = next->sibling;
516 next->sibling = new;
518 return NULL;
522 * insert_resource_conflict - Inserts resource in the resource tree
523 * @parent: parent of the new resource
524 * @new: new resource to insert
526 * Returns 0 on success, conflict resource if the resource can't be inserted.
528 * This function is equivalent to request_resource_conflict when no conflict
529 * happens. If a conflict happens, and the conflicting resources
530 * entirely fit within the range of the new resource, then the new
531 * resource is inserted and the conflicting resources become children of
532 * the new resource.
534 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
536 struct resource *conflict;
538 write_lock(&resource_lock);
539 conflict = __insert_resource(parent, new);
540 write_unlock(&resource_lock);
541 return conflict;
545 * insert_resource - Inserts a resource in the resource tree
546 * @parent: parent of the new resource
547 * @new: new resource to insert
549 * Returns 0 on success, -EBUSY if the resource can't be inserted.
551 int insert_resource(struct resource *parent, struct resource *new)
553 struct resource *conflict;
555 conflict = insert_resource_conflict(parent, new);
556 return conflict ? -EBUSY : 0;
560 * insert_resource_expand_to_fit - Insert a resource into the resource tree
561 * @root: root resource descriptor
562 * @new: new resource to insert
564 * Insert a resource into the resource tree, possibly expanding it in order
565 * to make it encompass any conflicting resources.
567 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
569 if (new->parent)
570 return;
572 write_lock(&resource_lock);
573 for (;;) {
574 struct resource *conflict;
576 conflict = __insert_resource(root, new);
577 if (!conflict)
578 break;
579 if (conflict == root)
580 break;
582 /* Ok, expand resource to cover the conflict, then try again .. */
583 if (conflict->start < new->start)
584 new->start = conflict->start;
585 if (conflict->end > new->end)
586 new->end = conflict->end;
588 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
590 write_unlock(&resource_lock);
594 * adjust_resource - modify a resource's start and size
595 * @res: resource to modify
596 * @start: new start value
597 * @size: new size
599 * Given an existing resource, change its start and size to match the
600 * arguments. Returns 0 on success, -EBUSY if it can't fit.
601 * Existing children of the resource are assumed to be immutable.
603 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
605 struct resource *tmp, *parent = res->parent;
606 resource_size_t end = start + size - 1;
607 int result = -EBUSY;
609 write_lock(&resource_lock);
611 if ((start < parent->start) || (end > parent->end))
612 goto out;
614 for (tmp = res->child; tmp; tmp = tmp->sibling) {
615 if ((tmp->start < start) || (tmp->end > end))
616 goto out;
619 if (res->sibling && (res->sibling->start <= end))
620 goto out;
622 tmp = parent->child;
623 if (tmp != res) {
624 while (tmp->sibling != res)
625 tmp = tmp->sibling;
626 if (start <= tmp->end)
627 goto out;
630 res->start = start;
631 res->end = end;
632 result = 0;
634 out:
635 write_unlock(&resource_lock);
636 return result;
639 static void __init __reserve_region_with_split(struct resource *root,
640 resource_size_t start, resource_size_t end,
641 const char *name)
643 struct resource *parent = root;
644 struct resource *conflict;
645 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
647 if (!res)
648 return;
650 res->name = name;
651 res->start = start;
652 res->end = end;
653 res->flags = IORESOURCE_BUSY;
655 conflict = __request_resource(parent, res);
656 if (!conflict)
657 return;
659 /* failed, split and try again */
660 kfree(res);
662 /* conflict covered whole area */
663 if (conflict->start <= start && conflict->end >= end)
664 return;
666 if (conflict->start > start)
667 __reserve_region_with_split(root, start, conflict->start-1, name);
668 if (conflict->end < end)
669 __reserve_region_with_split(root, conflict->end+1, end, name);
672 void __init reserve_region_with_split(struct resource *root,
673 resource_size_t start, resource_size_t end,
674 const char *name)
676 write_lock(&resource_lock);
677 __reserve_region_with_split(root, start, end, name);
678 write_unlock(&resource_lock);
681 EXPORT_SYMBOL(adjust_resource);
684 * resource_alignment - calculate resource's alignment
685 * @res: resource pointer
687 * Returns alignment on success, 0 (invalid alignment) on failure.
689 resource_size_t resource_alignment(struct resource *res)
691 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
692 case IORESOURCE_SIZEALIGN:
693 return resource_size(res);
694 case IORESOURCE_STARTALIGN:
695 return res->start;
696 default:
697 return 0;
702 * This is compatibility stuff for IO resources.
704 * Note how this, unlike the above, knows about
705 * the IO flag meanings (busy etc).
707 * request_region creates a new busy region.
709 * check_region returns non-zero if the area is already busy.
711 * release_region releases a matching busy region.
714 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
717 * __request_region - create a new busy resource region
718 * @parent: parent resource descriptor
719 * @start: resource start address
720 * @n: resource region size
721 * @name: reserving caller's ID string
722 * @flags: IO resource flags
724 struct resource * __request_region(struct resource *parent,
725 resource_size_t start, resource_size_t n,
726 const char *name, int flags)
728 DECLARE_WAITQUEUE(wait, current);
729 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
731 if (!res)
732 return NULL;
734 res->name = name;
735 res->start = start;
736 res->end = start + n - 1;
737 res->flags = IORESOURCE_BUSY;
738 res->flags |= flags;
740 write_lock(&resource_lock);
742 for (;;) {
743 struct resource *conflict;
745 conflict = __request_resource(parent, res);
746 if (!conflict)
747 break;
748 if (conflict != parent) {
749 parent = conflict;
750 if (!(conflict->flags & IORESOURCE_BUSY))
751 continue;
753 if (conflict->flags & flags & IORESOURCE_MUXED) {
754 add_wait_queue(&muxed_resource_wait, &wait);
755 write_unlock(&resource_lock);
756 set_current_state(TASK_UNINTERRUPTIBLE);
757 schedule();
758 remove_wait_queue(&muxed_resource_wait, &wait);
759 write_lock(&resource_lock);
760 continue;
762 /* Uhhuh, that didn't work out.. */
763 kfree(res);
764 res = NULL;
765 break;
767 write_unlock(&resource_lock);
768 return res;
770 EXPORT_SYMBOL(__request_region);
773 * __check_region - check if a resource region is busy or free
774 * @parent: parent resource descriptor
775 * @start: resource start address
776 * @n: resource region size
778 * Returns 0 if the region is free at the moment it is checked,
779 * returns %-EBUSY if the region is busy.
781 * NOTE:
782 * This function is deprecated because its use is racy.
783 * Even if it returns 0, a subsequent call to request_region()
784 * may fail because another driver etc. just allocated the region.
785 * Do NOT use it. It will be removed from the kernel.
787 int __check_region(struct resource *parent, resource_size_t start,
788 resource_size_t n)
790 struct resource * res;
792 res = __request_region(parent, start, n, "check-region", 0);
793 if (!res)
794 return -EBUSY;
796 release_resource(res);
797 kfree(res);
798 return 0;
800 EXPORT_SYMBOL(__check_region);
803 * __release_region - release a previously reserved resource region
804 * @parent: parent resource descriptor
805 * @start: resource start address
806 * @n: resource region size
808 * The described resource region must match a currently busy region.
810 void __release_region(struct resource *parent, resource_size_t start,
811 resource_size_t n)
813 struct resource **p;
814 resource_size_t end;
816 p = &parent->child;
817 end = start + n - 1;
819 write_lock(&resource_lock);
821 for (;;) {
822 struct resource *res = *p;
824 if (!res)
825 break;
826 if (res->start <= start && res->end >= end) {
827 if (!(res->flags & IORESOURCE_BUSY)) {
828 p = &res->child;
829 continue;
831 if (res->start != start || res->end != end)
832 break;
833 *p = res->sibling;
834 write_unlock(&resource_lock);
835 if (res->flags & IORESOURCE_MUXED)
836 wake_up(&muxed_resource_wait);
837 kfree(res);
838 return;
840 p = &res->sibling;
843 write_unlock(&resource_lock);
845 printk(KERN_WARNING "Trying to free nonexistent resource "
846 "<%016llx-%016llx>\n", (unsigned long long)start,
847 (unsigned long long)end);
849 EXPORT_SYMBOL(__release_region);
852 * Managed region resource
854 struct region_devres {
855 struct resource *parent;
856 resource_size_t start;
857 resource_size_t n;
860 static void devm_region_release(struct device *dev, void *res)
862 struct region_devres *this = res;
864 __release_region(this->parent, this->start, this->n);
867 static int devm_region_match(struct device *dev, void *res, void *match_data)
869 struct region_devres *this = res, *match = match_data;
871 return this->parent == match->parent &&
872 this->start == match->start && this->n == match->n;
875 struct resource * __devm_request_region(struct device *dev,
876 struct resource *parent, resource_size_t start,
877 resource_size_t n, const char *name)
879 struct region_devres *dr = NULL;
880 struct resource *res;
882 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
883 GFP_KERNEL);
884 if (!dr)
885 return NULL;
887 dr->parent = parent;
888 dr->start = start;
889 dr->n = n;
891 res = __request_region(parent, start, n, name, 0);
892 if (res)
893 devres_add(dev, dr);
894 else
895 devres_free(dr);
897 return res;
899 EXPORT_SYMBOL(__devm_request_region);
901 void __devm_release_region(struct device *dev, struct resource *parent,
902 resource_size_t start, resource_size_t n)
904 struct region_devres match_data = { parent, start, n };
906 __release_region(parent, start, n);
907 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
908 &match_data));
910 EXPORT_SYMBOL(__devm_release_region);
913 * Called from init/main.c to reserve IO ports.
915 #define MAXRESERVE 4
916 static int __init reserve_setup(char *str)
918 static int reserved;
919 static struct resource reserve[MAXRESERVE];
921 for (;;) {
922 unsigned int io_start, io_num;
923 int x = reserved;
925 if (get_option (&str, &io_start) != 2)
926 break;
927 if (get_option (&str, &io_num) == 0)
928 break;
929 if (x < MAXRESERVE) {
930 struct resource *res = reserve + x;
931 res->name = "reserved";
932 res->start = io_start;
933 res->end = io_start + io_num - 1;
934 res->flags = IORESOURCE_BUSY;
935 res->child = NULL;
936 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
937 reserved = x+1;
940 return 1;
943 __setup("reserve=", reserve_setup);
946 * Check if the requested addr and size spans more than any slot in the
947 * iomem resource tree.
949 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
951 struct resource *p = &iomem_resource;
952 int err = 0;
953 loff_t l;
955 read_lock(&resource_lock);
956 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
958 * We can probably skip the resources without
959 * IORESOURCE_IO attribute?
961 if (p->start >= addr + size)
962 continue;
963 if (p->end < addr)
964 continue;
965 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
966 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
967 continue;
969 * if a resource is "BUSY", it's not a hardware resource
970 * but a driver mapping of such a resource; we don't want
971 * to warn for those; some drivers legitimately map only
972 * partial hardware resources. (example: vesafb)
974 if (p->flags & IORESOURCE_BUSY)
975 continue;
977 printk(KERN_WARNING "resource map sanity check conflict: "
978 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
979 (unsigned long long)addr,
980 (unsigned long long)(addr + size - 1),
981 (unsigned long long)p->start,
982 (unsigned long long)p->end,
983 p->name);
984 err = -1;
985 break;
987 read_unlock(&resource_lock);
989 return err;
992 #ifdef CONFIG_STRICT_DEVMEM
993 static int strict_iomem_checks = 1;
994 #else
995 static int strict_iomem_checks;
996 #endif
999 * check if an address is reserved in the iomem resource tree
1000 * returns 1 if reserved, 0 if not reserved.
1002 int iomem_is_exclusive(u64 addr)
1004 struct resource *p = &iomem_resource;
1005 int err = 0;
1006 loff_t l;
1007 int size = PAGE_SIZE;
1009 if (!strict_iomem_checks)
1010 return 0;
1012 addr = addr & PAGE_MASK;
1014 read_lock(&resource_lock);
1015 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1017 * We can probably skip the resources without
1018 * IORESOURCE_IO attribute?
1020 if (p->start >= addr + size)
1021 break;
1022 if (p->end < addr)
1023 continue;
1024 if (p->flags & IORESOURCE_BUSY &&
1025 p->flags & IORESOURCE_EXCLUSIVE) {
1026 err = 1;
1027 break;
1030 read_unlock(&resource_lock);
1032 return err;
1035 static int __init strict_iomem(char *str)
1037 if (strstr(str, "relaxed"))
1038 strict_iomem_checks = 0;
1039 if (strstr(str, "strict"))
1040 strict_iomem_checks = 1;
1041 return 1;
1044 __setup("iomem=", strict_iomem);