hwmon: (lm90) Fix checkpatch errors
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / resource.c
blob9c9841cb69021fdd259510ce7f117cedf14f0ad7
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;
361 * Find empty slot in the resource tree given range and alignment.
363 static int find_resource(struct resource *root, struct resource *new,
364 resource_size_t size, resource_size_t min,
365 resource_size_t max, resource_size_t align,
366 resource_size_t (*alignf)(void *,
367 const struct resource *,
368 resource_size_t,
369 resource_size_t),
370 void *alignf_data)
372 struct resource *this = root->child;
373 struct resource tmp = *new;
375 tmp.start = root->start;
377 * Skip past an allocated resource that starts at 0, since the assignment
378 * of this->start - 1 to tmp->end below would cause an underflow.
380 if (this && this->start == 0) {
381 tmp.start = this->end + 1;
382 this = this->sibling;
384 for(;;) {
385 if (this)
386 tmp.end = this->start - 1;
387 else
388 tmp.end = root->end;
389 if (tmp.start < min)
390 tmp.start = min;
391 if (tmp.end > max)
392 tmp.end = max;
393 tmp.start = ALIGN(tmp.start, align);
394 if (alignf)
395 tmp.start = alignf(alignf_data, &tmp, size, align);
396 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
397 new->start = tmp.start;
398 new->end = tmp.start + size - 1;
399 return 0;
401 if (!this)
402 break;
403 tmp.start = this->end + 1;
404 this = this->sibling;
406 return -EBUSY;
410 * allocate_resource - allocate empty slot in the resource tree given range & alignment
411 * @root: root resource descriptor
412 * @new: resource descriptor desired by caller
413 * @size: requested resource region size
414 * @min: minimum size to allocate
415 * @max: maximum size to allocate
416 * @align: alignment requested, in bytes
417 * @alignf: alignment function, optional, called if not NULL
418 * @alignf_data: arbitrary data to pass to the @alignf function
420 int allocate_resource(struct resource *root, struct resource *new,
421 resource_size_t size, resource_size_t min,
422 resource_size_t max, resource_size_t align,
423 resource_size_t (*alignf)(void *,
424 const struct resource *,
425 resource_size_t,
426 resource_size_t),
427 void *alignf_data)
429 int err;
431 write_lock(&resource_lock);
432 err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
433 if (err >= 0 && __request_resource(root, new))
434 err = -EBUSY;
435 write_unlock(&resource_lock);
436 return err;
439 EXPORT_SYMBOL(allocate_resource);
442 * Insert a resource into the resource tree. If successful, return NULL,
443 * otherwise return the conflicting resource (compare to __request_resource())
445 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
447 struct resource *first, *next;
449 for (;; parent = first) {
450 first = __request_resource(parent, new);
451 if (!first)
452 return first;
454 if (first == parent)
455 return first;
456 if (WARN_ON(first == new)) /* duplicated insertion */
457 return first;
459 if ((first->start > new->start) || (first->end < new->end))
460 break;
461 if ((first->start == new->start) && (first->end == new->end))
462 break;
465 for (next = first; ; next = next->sibling) {
466 /* Partial overlap? Bad, and unfixable */
467 if (next->start < new->start || next->end > new->end)
468 return next;
469 if (!next->sibling)
470 break;
471 if (next->sibling->start > new->end)
472 break;
475 new->parent = parent;
476 new->sibling = next->sibling;
477 new->child = first;
479 next->sibling = NULL;
480 for (next = first; next; next = next->sibling)
481 next->parent = new;
483 if (parent->child == first) {
484 parent->child = new;
485 } else {
486 next = parent->child;
487 while (next->sibling != first)
488 next = next->sibling;
489 next->sibling = new;
491 return NULL;
495 * insert_resource_conflict - Inserts resource in the resource tree
496 * @parent: parent of the new resource
497 * @new: new resource to insert
499 * Returns 0 on success, conflict resource if the resource can't be inserted.
501 * This function is equivalent to request_resource_conflict when no conflict
502 * happens. If a conflict happens, and the conflicting resources
503 * entirely fit within the range of the new resource, then the new
504 * resource is inserted and the conflicting resources become children of
505 * the new resource.
507 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
509 struct resource *conflict;
511 write_lock(&resource_lock);
512 conflict = __insert_resource(parent, new);
513 write_unlock(&resource_lock);
514 return conflict;
518 * insert_resource - Inserts a resource in the resource tree
519 * @parent: parent of the new resource
520 * @new: new resource to insert
522 * Returns 0 on success, -EBUSY if the resource can't be inserted.
524 int insert_resource(struct resource *parent, struct resource *new)
526 struct resource *conflict;
528 conflict = insert_resource_conflict(parent, new);
529 return conflict ? -EBUSY : 0;
533 * insert_resource_expand_to_fit - Insert a resource into the resource tree
534 * @root: root resource descriptor
535 * @new: new resource to insert
537 * Insert a resource into the resource tree, possibly expanding it in order
538 * to make it encompass any conflicting resources.
540 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
542 if (new->parent)
543 return;
545 write_lock(&resource_lock);
546 for (;;) {
547 struct resource *conflict;
549 conflict = __insert_resource(root, new);
550 if (!conflict)
551 break;
552 if (conflict == root)
553 break;
555 /* Ok, expand resource to cover the conflict, then try again .. */
556 if (conflict->start < new->start)
557 new->start = conflict->start;
558 if (conflict->end > new->end)
559 new->end = conflict->end;
561 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
563 write_unlock(&resource_lock);
567 * adjust_resource - modify a resource's start and size
568 * @res: resource to modify
569 * @start: new start value
570 * @size: new size
572 * Given an existing resource, change its start and size to match the
573 * arguments. Returns 0 on success, -EBUSY if it can't fit.
574 * Existing children of the resource are assumed to be immutable.
576 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
578 struct resource *tmp, *parent = res->parent;
579 resource_size_t end = start + size - 1;
580 int result = -EBUSY;
582 write_lock(&resource_lock);
584 if ((start < parent->start) || (end > parent->end))
585 goto out;
587 for (tmp = res->child; tmp; tmp = tmp->sibling) {
588 if ((tmp->start < start) || (tmp->end > end))
589 goto out;
592 if (res->sibling && (res->sibling->start <= end))
593 goto out;
595 tmp = parent->child;
596 if (tmp != res) {
597 while (tmp->sibling != res)
598 tmp = tmp->sibling;
599 if (start <= tmp->end)
600 goto out;
603 res->start = start;
604 res->end = end;
605 result = 0;
607 out:
608 write_unlock(&resource_lock);
609 return result;
612 static void __init __reserve_region_with_split(struct resource *root,
613 resource_size_t start, resource_size_t end,
614 const char *name)
616 struct resource *parent = root;
617 struct resource *conflict;
618 struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
620 if (!res)
621 return;
623 res->name = name;
624 res->start = start;
625 res->end = end;
626 res->flags = IORESOURCE_BUSY;
628 conflict = __request_resource(parent, res);
629 if (!conflict)
630 return;
632 /* failed, split and try again */
633 kfree(res);
635 /* conflict covered whole area */
636 if (conflict->start <= start && conflict->end >= end)
637 return;
639 if (conflict->start > start)
640 __reserve_region_with_split(root, start, conflict->start-1, name);
641 if (conflict->end < end)
642 __reserve_region_with_split(root, conflict->end+1, end, name);
645 void __init reserve_region_with_split(struct resource *root,
646 resource_size_t start, resource_size_t end,
647 const char *name)
649 write_lock(&resource_lock);
650 __reserve_region_with_split(root, start, end, name);
651 write_unlock(&resource_lock);
654 EXPORT_SYMBOL(adjust_resource);
657 * resource_alignment - calculate resource's alignment
658 * @res: resource pointer
660 * Returns alignment on success, 0 (invalid alignment) on failure.
662 resource_size_t resource_alignment(struct resource *res)
664 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
665 case IORESOURCE_SIZEALIGN:
666 return resource_size(res);
667 case IORESOURCE_STARTALIGN:
668 return res->start;
669 default:
670 return 0;
675 * This is compatibility stuff for IO resources.
677 * Note how this, unlike the above, knows about
678 * the IO flag meanings (busy etc).
680 * request_region creates a new busy region.
682 * check_region returns non-zero if the area is already busy.
684 * release_region releases a matching busy region.
687 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
690 * __request_region - create a new busy resource region
691 * @parent: parent resource descriptor
692 * @start: resource start address
693 * @n: resource region size
694 * @name: reserving caller's ID string
695 * @flags: IO resource flags
697 struct resource * __request_region(struct resource *parent,
698 resource_size_t start, resource_size_t n,
699 const char *name, int flags)
701 DECLARE_WAITQUEUE(wait, current);
702 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
704 if (!res)
705 return NULL;
707 res->name = name;
708 res->start = start;
709 res->end = start + n - 1;
710 res->flags = IORESOURCE_BUSY;
711 res->flags |= flags;
713 write_lock(&resource_lock);
715 for (;;) {
716 struct resource *conflict;
718 conflict = __request_resource(parent, res);
719 if (!conflict)
720 break;
721 if (conflict != parent) {
722 parent = conflict;
723 if (!(conflict->flags & IORESOURCE_BUSY))
724 continue;
726 if (conflict->flags & flags & IORESOURCE_MUXED) {
727 add_wait_queue(&muxed_resource_wait, &wait);
728 write_unlock(&resource_lock);
729 set_current_state(TASK_UNINTERRUPTIBLE);
730 schedule();
731 remove_wait_queue(&muxed_resource_wait, &wait);
732 write_lock(&resource_lock);
733 continue;
735 /* Uhhuh, that didn't work out.. */
736 kfree(res);
737 res = NULL;
738 break;
740 write_unlock(&resource_lock);
741 return res;
743 EXPORT_SYMBOL(__request_region);
746 * __check_region - check if a resource region is busy or free
747 * @parent: parent resource descriptor
748 * @start: resource start address
749 * @n: resource region size
751 * Returns 0 if the region is free at the moment it is checked,
752 * returns %-EBUSY if the region is busy.
754 * NOTE:
755 * This function is deprecated because its use is racy.
756 * Even if it returns 0, a subsequent call to request_region()
757 * may fail because another driver etc. just allocated the region.
758 * Do NOT use it. It will be removed from the kernel.
760 int __check_region(struct resource *parent, resource_size_t start,
761 resource_size_t n)
763 struct resource * res;
765 res = __request_region(parent, start, n, "check-region", 0);
766 if (!res)
767 return -EBUSY;
769 release_resource(res);
770 kfree(res);
771 return 0;
773 EXPORT_SYMBOL(__check_region);
776 * __release_region - release a previously reserved resource region
777 * @parent: parent resource descriptor
778 * @start: resource start address
779 * @n: resource region size
781 * The described resource region must match a currently busy region.
783 void __release_region(struct resource *parent, resource_size_t start,
784 resource_size_t n)
786 struct resource **p;
787 resource_size_t end;
789 p = &parent->child;
790 end = start + n - 1;
792 write_lock(&resource_lock);
794 for (;;) {
795 struct resource *res = *p;
797 if (!res)
798 break;
799 if (res->start <= start && res->end >= end) {
800 if (!(res->flags & IORESOURCE_BUSY)) {
801 p = &res->child;
802 continue;
804 if (res->start != start || res->end != end)
805 break;
806 *p = res->sibling;
807 write_unlock(&resource_lock);
808 if (res->flags & IORESOURCE_MUXED)
809 wake_up(&muxed_resource_wait);
810 kfree(res);
811 return;
813 p = &res->sibling;
816 write_unlock(&resource_lock);
818 printk(KERN_WARNING "Trying to free nonexistent resource "
819 "<%016llx-%016llx>\n", (unsigned long long)start,
820 (unsigned long long)end);
822 EXPORT_SYMBOL(__release_region);
825 * Managed region resource
827 struct region_devres {
828 struct resource *parent;
829 resource_size_t start;
830 resource_size_t n;
833 static void devm_region_release(struct device *dev, void *res)
835 struct region_devres *this = res;
837 __release_region(this->parent, this->start, this->n);
840 static int devm_region_match(struct device *dev, void *res, void *match_data)
842 struct region_devres *this = res, *match = match_data;
844 return this->parent == match->parent &&
845 this->start == match->start && this->n == match->n;
848 struct resource * __devm_request_region(struct device *dev,
849 struct resource *parent, resource_size_t start,
850 resource_size_t n, const char *name)
852 struct region_devres *dr = NULL;
853 struct resource *res;
855 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
856 GFP_KERNEL);
857 if (!dr)
858 return NULL;
860 dr->parent = parent;
861 dr->start = start;
862 dr->n = n;
864 res = __request_region(parent, start, n, name, 0);
865 if (res)
866 devres_add(dev, dr);
867 else
868 devres_free(dr);
870 return res;
872 EXPORT_SYMBOL(__devm_request_region);
874 void __devm_release_region(struct device *dev, struct resource *parent,
875 resource_size_t start, resource_size_t n)
877 struct region_devres match_data = { parent, start, n };
879 __release_region(parent, start, n);
880 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
881 &match_data));
883 EXPORT_SYMBOL(__devm_release_region);
886 * Called from init/main.c to reserve IO ports.
888 #define MAXRESERVE 4
889 static int __init reserve_setup(char *str)
891 static int reserved;
892 static struct resource reserve[MAXRESERVE];
894 for (;;) {
895 unsigned int io_start, io_num;
896 int x = reserved;
898 if (get_option (&str, &io_start) != 2)
899 break;
900 if (get_option (&str, &io_num) == 0)
901 break;
902 if (x < MAXRESERVE) {
903 struct resource *res = reserve + x;
904 res->name = "reserved";
905 res->start = io_start;
906 res->end = io_start + io_num - 1;
907 res->flags = IORESOURCE_BUSY;
908 res->child = NULL;
909 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
910 reserved = x+1;
913 return 1;
916 __setup("reserve=", reserve_setup);
919 * Check if the requested addr and size spans more than any slot in the
920 * iomem resource tree.
922 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
924 struct resource *p = &iomem_resource;
925 int err = 0;
926 loff_t l;
928 read_lock(&resource_lock);
929 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
931 * We can probably skip the resources without
932 * IORESOURCE_IO attribute?
934 if (p->start >= addr + size)
935 continue;
936 if (p->end < addr)
937 continue;
938 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
939 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
940 continue;
942 * if a resource is "BUSY", it's not a hardware resource
943 * but a driver mapping of such a resource; we don't want
944 * to warn for those; some drivers legitimately map only
945 * partial hardware resources. (example: vesafb)
947 if (p->flags & IORESOURCE_BUSY)
948 continue;
950 printk(KERN_WARNING "resource map sanity check conflict: "
951 "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
952 (unsigned long long)addr,
953 (unsigned long long)(addr + size - 1),
954 (unsigned long long)p->start,
955 (unsigned long long)p->end,
956 p->name);
957 err = -1;
958 break;
960 read_unlock(&resource_lock);
962 return err;
965 #ifdef CONFIG_STRICT_DEVMEM
966 static int strict_iomem_checks = 1;
967 #else
968 static int strict_iomem_checks;
969 #endif
972 * check if an address is reserved in the iomem resource tree
973 * returns 1 if reserved, 0 if not reserved.
975 int iomem_is_exclusive(u64 addr)
977 struct resource *p = &iomem_resource;
978 int err = 0;
979 loff_t l;
980 int size = PAGE_SIZE;
982 if (!strict_iomem_checks)
983 return 0;
985 addr = addr & PAGE_MASK;
987 read_lock(&resource_lock);
988 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
990 * We can probably skip the resources without
991 * IORESOURCE_IO attribute?
993 if (p->start >= addr + size)
994 break;
995 if (p->end < addr)
996 continue;
997 if (p->flags & IORESOURCE_BUSY &&
998 p->flags & IORESOURCE_EXCLUSIVE) {
999 err = 1;
1000 break;
1003 read_unlock(&resource_lock);
1005 return err;
1008 static int __init strict_iomem(char *str)
1010 if (strstr(str, "relaxed"))
1011 strict_iomem_checks = 0;
1012 if (strstr(str, "strict"))
1013 strict_iomem_checks = 1;
1014 return 1;
1017 __setup("iomem=", strict_iomem);