USB: option: add support for DW5821e
[linux/fpc-iii.git] / kernel / resource.c
blob41718cd8cab5301070b52b3c0073d487a75ee760
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/export.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/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/device.h>
23 #include <linux/pfn.h>
24 #include <linux/mm.h>
25 #include <linux/resource_ext.h>
26 #include <asm/io.h>
29 struct resource ioport_resource = {
30 .name = "PCI IO",
31 .start = 0,
32 .end = IO_SPACE_LIMIT,
33 .flags = IORESOURCE_IO,
35 EXPORT_SYMBOL(ioport_resource);
37 struct resource iomem_resource = {
38 .name = "PCI mem",
39 .start = 0,
40 .end = -1,
41 .flags = IORESOURCE_MEM,
43 EXPORT_SYMBOL(iomem_resource);
45 /* constraints to be met while allocating resources */
46 struct resource_constraint {
47 resource_size_t min, max, align;
48 resource_size_t (*alignf)(void *, const struct resource *,
49 resource_size_t, resource_size_t);
50 void *alignf_data;
53 static DEFINE_RWLOCK(resource_lock);
56 * For memory hotplug, there is no way to free resource entries allocated
57 * by boot mem after the system is up. So for reusing the resource entry
58 * we need to remember the resource.
60 static struct resource *bootmem_resource_free;
61 static DEFINE_SPINLOCK(bootmem_resource_lock);
63 static struct resource *next_resource(struct resource *p, bool sibling_only)
65 /* Caller wants to traverse through siblings only */
66 if (sibling_only)
67 return p->sibling;
69 if (p->child)
70 return p->child;
71 while (!p->sibling && p->parent)
72 p = p->parent;
73 return p->sibling;
76 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
78 struct resource *p = v;
79 (*pos)++;
80 return (void *)next_resource(p, false);
83 #ifdef CONFIG_PROC_FS
85 enum { MAX_IORES_LEVEL = 5 };
87 static void *r_start(struct seq_file *m, loff_t *pos)
88 __acquires(resource_lock)
90 struct resource *p = m->private;
91 loff_t l = 0;
92 read_lock(&resource_lock);
93 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
95 return p;
98 static void r_stop(struct seq_file *m, void *v)
99 __releases(resource_lock)
101 read_unlock(&resource_lock);
104 static int r_show(struct seq_file *m, void *v)
106 struct resource *root = m->private;
107 struct resource *r = v, *p;
108 unsigned long long start, end;
109 int width = root->end < 0x10000 ? 4 : 8;
110 int depth;
112 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
113 if (p->parent == root)
114 break;
116 if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
117 start = r->start;
118 end = r->end;
119 } else {
120 start = end = 0;
123 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
124 depth * 2, "",
125 width, start,
126 width, end,
127 r->name ? r->name : "<BAD>");
128 return 0;
131 static const struct seq_operations resource_op = {
132 .start = r_start,
133 .next = r_next,
134 .stop = r_stop,
135 .show = r_show,
138 static int ioports_open(struct inode *inode, struct file *file)
140 int res = seq_open(file, &resource_op);
141 if (!res) {
142 struct seq_file *m = file->private_data;
143 m->private = &ioport_resource;
145 return res;
148 static int iomem_open(struct inode *inode, struct file *file)
150 int res = seq_open(file, &resource_op);
151 if (!res) {
152 struct seq_file *m = file->private_data;
153 m->private = &iomem_resource;
155 return res;
158 static const struct file_operations proc_ioports_operations = {
159 .open = ioports_open,
160 .read = seq_read,
161 .llseek = seq_lseek,
162 .release = seq_release,
165 static const struct file_operations proc_iomem_operations = {
166 .open = iomem_open,
167 .read = seq_read,
168 .llseek = seq_lseek,
169 .release = seq_release,
172 static int __init ioresources_init(void)
174 proc_create("ioports", 0, NULL, &proc_ioports_operations);
175 proc_create("iomem", 0, NULL, &proc_iomem_operations);
176 return 0;
178 __initcall(ioresources_init);
180 #endif /* CONFIG_PROC_FS */
182 static void free_resource(struct resource *res)
184 if (!res)
185 return;
187 if (!PageSlab(virt_to_head_page(res))) {
188 spin_lock(&bootmem_resource_lock);
189 res->sibling = bootmem_resource_free;
190 bootmem_resource_free = res;
191 spin_unlock(&bootmem_resource_lock);
192 } else {
193 kfree(res);
197 static struct resource *alloc_resource(gfp_t flags)
199 struct resource *res = NULL;
201 spin_lock(&bootmem_resource_lock);
202 if (bootmem_resource_free) {
203 res = bootmem_resource_free;
204 bootmem_resource_free = res->sibling;
206 spin_unlock(&bootmem_resource_lock);
208 if (res)
209 memset(res, 0, sizeof(struct resource));
210 else
211 res = kzalloc(sizeof(struct resource), flags);
213 return res;
216 /* Return the conflict entry if you can't request it */
217 static struct resource * __request_resource(struct resource *root, struct resource *new)
219 resource_size_t start = new->start;
220 resource_size_t end = new->end;
221 struct resource *tmp, **p;
223 if (end < start)
224 return root;
225 if (start < root->start)
226 return root;
227 if (end > root->end)
228 return root;
229 p = &root->child;
230 for (;;) {
231 tmp = *p;
232 if (!tmp || tmp->start > end) {
233 new->sibling = tmp;
234 *p = new;
235 new->parent = root;
236 return NULL;
238 p = &tmp->sibling;
239 if (tmp->end < start)
240 continue;
241 return tmp;
245 static int __release_resource(struct resource *old)
247 struct resource *tmp, **p;
249 p = &old->parent->child;
250 for (;;) {
251 tmp = *p;
252 if (!tmp)
253 break;
254 if (tmp == old) {
255 *p = tmp->sibling;
256 old->parent = NULL;
257 return 0;
259 p = &tmp->sibling;
261 return -EINVAL;
264 static void __release_child_resources(struct resource *r)
266 struct resource *tmp, *p;
267 resource_size_t size;
269 p = r->child;
270 r->child = NULL;
271 while (p) {
272 tmp = p;
273 p = p->sibling;
275 tmp->parent = NULL;
276 tmp->sibling = NULL;
277 __release_child_resources(tmp);
279 printk(KERN_DEBUG "release child resource %pR\n", tmp);
280 /* need to restore size, and keep flags */
281 size = resource_size(tmp);
282 tmp->start = 0;
283 tmp->end = size - 1;
287 void release_child_resources(struct resource *r)
289 write_lock(&resource_lock);
290 __release_child_resources(r);
291 write_unlock(&resource_lock);
295 * request_resource_conflict - request and reserve an I/O or memory resource
296 * @root: root resource descriptor
297 * @new: resource descriptor desired by caller
299 * Returns 0 for success, conflict resource on error.
301 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
303 struct resource *conflict;
305 write_lock(&resource_lock);
306 conflict = __request_resource(root, new);
307 write_unlock(&resource_lock);
308 return conflict;
312 * request_resource - request and reserve an I/O or memory resource
313 * @root: root resource descriptor
314 * @new: resource descriptor desired by caller
316 * Returns 0 for success, negative error code on error.
318 int request_resource(struct resource *root, struct resource *new)
320 struct resource *conflict;
322 conflict = request_resource_conflict(root, new);
323 return conflict ? -EBUSY : 0;
326 EXPORT_SYMBOL(request_resource);
329 * release_resource - release a previously reserved resource
330 * @old: resource pointer
332 int release_resource(struct resource *old)
334 int retval;
336 write_lock(&resource_lock);
337 retval = __release_resource(old);
338 write_unlock(&resource_lock);
339 return retval;
342 EXPORT_SYMBOL(release_resource);
345 * Finds the lowest iomem reosurce exists with-in [res->start.res->end)
346 * the caller must specify res->start, res->end, res->flags and "name".
347 * If found, returns 0, res is overwritten, if not found, returns -1.
348 * This walks through whole tree and not just first level children
349 * until and unless first_level_children_only is true.
351 static int find_next_iomem_res(struct resource *res, char *name,
352 bool first_level_children_only)
354 resource_size_t start, end;
355 struct resource *p;
356 bool sibling_only = false;
358 BUG_ON(!res);
360 start = res->start;
361 end = res->end;
362 BUG_ON(start >= end);
364 if (first_level_children_only)
365 sibling_only = true;
367 read_lock(&resource_lock);
369 for (p = iomem_resource.child; p; p = next_resource(p, sibling_only)) {
370 if (p->flags != res->flags)
371 continue;
372 if (name && strcmp(p->name, name))
373 continue;
374 if (p->start > end) {
375 p = NULL;
376 break;
378 if ((p->end >= start) && (p->start < end))
379 break;
382 read_unlock(&resource_lock);
383 if (!p)
384 return -1;
385 /* copy data */
386 if (res->start < p->start)
387 res->start = p->start;
388 if (res->end > p->end)
389 res->end = p->end;
390 return 0;
394 * Walks through iomem resources and calls func() with matching resource
395 * ranges. This walks through whole tree and not just first level children.
396 * All the memory ranges which overlap start,end and also match flags and
397 * name are valid candidates.
399 * @name: name of resource
400 * @flags: resource flags
401 * @start: start addr
402 * @end: end addr
404 int walk_iomem_res(char *name, unsigned long flags, u64 start, u64 end,
405 void *arg, int (*func)(u64, u64, void *))
407 struct resource res;
408 u64 orig_end;
409 int ret = -1;
411 res.start = start;
412 res.end = end;
413 res.flags = flags;
414 orig_end = res.end;
415 while ((res.start < res.end) &&
416 (!find_next_iomem_res(&res, name, false))) {
417 ret = (*func)(res.start, res.end, arg);
418 if (ret)
419 break;
420 res.start = res.end + 1;
421 res.end = orig_end;
423 return ret;
427 * This function calls callback against all memory range of "System RAM"
428 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
429 * Now, this function is only for "System RAM". This function deals with
430 * full ranges and not pfn. If resources are not pfn aligned, dealing
431 * with pfn can truncate ranges.
433 int walk_system_ram_res(u64 start, u64 end, void *arg,
434 int (*func)(u64, u64, void *))
436 struct resource res;
437 u64 orig_end;
438 int ret = -1;
440 res.start = start;
441 res.end = end;
442 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
443 orig_end = res.end;
444 while ((res.start < res.end) &&
445 (!find_next_iomem_res(&res, "System RAM", true))) {
446 ret = (*func)(res.start, res.end, arg);
447 if (ret)
448 break;
449 res.start = res.end + 1;
450 res.end = orig_end;
452 return ret;
455 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
458 * This function calls callback against all memory range of "System RAM"
459 * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
460 * Now, this function is only for "System RAM".
462 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
463 void *arg, int (*func)(unsigned long, unsigned long, void *))
465 struct resource res;
466 unsigned long pfn, end_pfn;
467 u64 orig_end;
468 int ret = -1;
470 res.start = (u64) start_pfn << PAGE_SHIFT;
471 res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
472 res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
473 orig_end = res.end;
474 while ((res.start < res.end) &&
475 (find_next_iomem_res(&res, "System RAM", true) >= 0)) {
476 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
477 end_pfn = (res.end + 1) >> PAGE_SHIFT;
478 if (end_pfn > pfn)
479 ret = (*func)(pfn, end_pfn - pfn, arg);
480 if (ret)
481 break;
482 res.start = res.end + 1;
483 res.end = orig_end;
485 return ret;
488 #endif
490 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
492 return 1;
495 * This generic page_is_ram() returns true if specified address is
496 * registered as "System RAM" in iomem_resource list.
498 int __weak page_is_ram(unsigned long pfn)
500 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
502 EXPORT_SYMBOL_GPL(page_is_ram);
505 * region_intersects() - determine intersection of region with known resources
506 * @start: region start address
507 * @size: size of region
508 * @name: name of resource (in iomem_resource)
510 * Check if the specified region partially overlaps or fully eclipses a
511 * resource identified by @name. Return REGION_DISJOINT if the region
512 * does not overlap @name, return REGION_MIXED if the region overlaps
513 * @type and another resource, and return REGION_INTERSECTS if the
514 * region overlaps @type and no other defined resource. Note, that
515 * REGION_INTERSECTS is also returned in the case when the specified
516 * region overlaps RAM and undefined memory holes.
518 * region_intersect() is used by memory remapping functions to ensure
519 * the user is not remapping RAM and is a vast speed up over walking
520 * through the resource table page by page.
522 int region_intersects(resource_size_t start, size_t size, const char *name)
524 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
525 resource_size_t end = start + size - 1;
526 int type = 0; int other = 0;
527 struct resource *p;
529 read_lock(&resource_lock);
530 for (p = iomem_resource.child; p ; p = p->sibling) {
531 bool is_type = strcmp(p->name, name) == 0 && p->flags == flags;
533 if (start >= p->start && start <= p->end)
534 is_type ? type++ : other++;
535 if (end >= p->start && end <= p->end)
536 is_type ? type++ : other++;
537 if (p->start >= start && p->end <= end)
538 is_type ? type++ : other++;
540 read_unlock(&resource_lock);
542 if (other == 0)
543 return type ? REGION_INTERSECTS : REGION_DISJOINT;
545 if (type)
546 return REGION_MIXED;
548 return REGION_DISJOINT;
551 void __weak arch_remove_reservations(struct resource *avail)
555 static resource_size_t simple_align_resource(void *data,
556 const struct resource *avail,
557 resource_size_t size,
558 resource_size_t align)
560 return avail->start;
563 static void resource_clip(struct resource *res, resource_size_t min,
564 resource_size_t max)
566 if (res->start < min)
567 res->start = min;
568 if (res->end > max)
569 res->end = max;
573 * Find empty slot in the resource tree with the given range and
574 * alignment constraints
576 static int __find_resource(struct resource *root, struct resource *old,
577 struct resource *new,
578 resource_size_t size,
579 struct resource_constraint *constraint)
581 struct resource *this = root->child;
582 struct resource tmp = *new, avail, alloc;
584 tmp.start = root->start;
586 * Skip past an allocated resource that starts at 0, since the assignment
587 * of this->start - 1 to tmp->end below would cause an underflow.
589 if (this && this->start == root->start) {
590 tmp.start = (this == old) ? old->start : this->end + 1;
591 this = this->sibling;
593 for(;;) {
594 if (this)
595 tmp.end = (this == old) ? this->end : this->start - 1;
596 else
597 tmp.end = root->end;
599 if (tmp.end < tmp.start)
600 goto next;
602 resource_clip(&tmp, constraint->min, constraint->max);
603 arch_remove_reservations(&tmp);
605 /* Check for overflow after ALIGN() */
606 avail.start = ALIGN(tmp.start, constraint->align);
607 avail.end = tmp.end;
608 avail.flags = new->flags & ~IORESOURCE_UNSET;
609 if (avail.start >= tmp.start) {
610 alloc.flags = avail.flags;
611 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
612 size, constraint->align);
613 alloc.end = alloc.start + size - 1;
614 if (alloc.start <= alloc.end &&
615 resource_contains(&avail, &alloc)) {
616 new->start = alloc.start;
617 new->end = alloc.end;
618 return 0;
622 next: if (!this || this->end == root->end)
623 break;
625 if (this != old)
626 tmp.start = this->end + 1;
627 this = this->sibling;
629 return -EBUSY;
633 * Find empty slot in the resource tree given range and alignment.
635 static int find_resource(struct resource *root, struct resource *new,
636 resource_size_t size,
637 struct resource_constraint *constraint)
639 return __find_resource(root, NULL, new, size, constraint);
643 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
644 * The resource will be relocated if the new size cannot be reallocated in the
645 * current location.
647 * @root: root resource descriptor
648 * @old: resource descriptor desired by caller
649 * @newsize: new size of the resource descriptor
650 * @constraint: the size and alignment constraints to be met.
652 static int reallocate_resource(struct resource *root, struct resource *old,
653 resource_size_t newsize,
654 struct resource_constraint *constraint)
656 int err=0;
657 struct resource new = *old;
658 struct resource *conflict;
660 write_lock(&resource_lock);
662 if ((err = __find_resource(root, old, &new, newsize, constraint)))
663 goto out;
665 if (resource_contains(&new, old)) {
666 old->start = new.start;
667 old->end = new.end;
668 goto out;
671 if (old->child) {
672 err = -EBUSY;
673 goto out;
676 if (resource_contains(old, &new)) {
677 old->start = new.start;
678 old->end = new.end;
679 } else {
680 __release_resource(old);
681 *old = new;
682 conflict = __request_resource(root, old);
683 BUG_ON(conflict);
685 out:
686 write_unlock(&resource_lock);
687 return err;
692 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
693 * The resource will be reallocated with a new size if it was already allocated
694 * @root: root resource descriptor
695 * @new: resource descriptor desired by caller
696 * @size: requested resource region size
697 * @min: minimum boundary to allocate
698 * @max: maximum boundary to allocate
699 * @align: alignment requested, in bytes
700 * @alignf: alignment function, optional, called if not NULL
701 * @alignf_data: arbitrary data to pass to the @alignf function
703 int allocate_resource(struct resource *root, struct resource *new,
704 resource_size_t size, resource_size_t min,
705 resource_size_t max, resource_size_t align,
706 resource_size_t (*alignf)(void *,
707 const struct resource *,
708 resource_size_t,
709 resource_size_t),
710 void *alignf_data)
712 int err;
713 struct resource_constraint constraint;
715 if (!alignf)
716 alignf = simple_align_resource;
718 constraint.min = min;
719 constraint.max = max;
720 constraint.align = align;
721 constraint.alignf = alignf;
722 constraint.alignf_data = alignf_data;
724 if ( new->parent ) {
725 /* resource is already allocated, try reallocating with
726 the new constraints */
727 return reallocate_resource(root, new, size, &constraint);
730 write_lock(&resource_lock);
731 err = find_resource(root, new, size, &constraint);
732 if (err >= 0 && __request_resource(root, new))
733 err = -EBUSY;
734 write_unlock(&resource_lock);
735 return err;
738 EXPORT_SYMBOL(allocate_resource);
741 * lookup_resource - find an existing resource by a resource start address
742 * @root: root resource descriptor
743 * @start: resource start address
745 * Returns a pointer to the resource if found, NULL otherwise
747 struct resource *lookup_resource(struct resource *root, resource_size_t start)
749 struct resource *res;
751 read_lock(&resource_lock);
752 for (res = root->child; res; res = res->sibling) {
753 if (res->start == start)
754 break;
756 read_unlock(&resource_lock);
758 return res;
762 * Insert a resource into the resource tree. If successful, return NULL,
763 * otherwise return the conflicting resource (compare to __request_resource())
765 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
767 struct resource *first, *next;
769 for (;; parent = first) {
770 first = __request_resource(parent, new);
771 if (!first)
772 return first;
774 if (first == parent)
775 return first;
776 if (WARN_ON(first == new)) /* duplicated insertion */
777 return first;
779 if ((first->start > new->start) || (first->end < new->end))
780 break;
781 if ((first->start == new->start) && (first->end == new->end))
782 break;
785 for (next = first; ; next = next->sibling) {
786 /* Partial overlap? Bad, and unfixable */
787 if (next->start < new->start || next->end > new->end)
788 return next;
789 if (!next->sibling)
790 break;
791 if (next->sibling->start > new->end)
792 break;
795 new->parent = parent;
796 new->sibling = next->sibling;
797 new->child = first;
799 next->sibling = NULL;
800 for (next = first; next; next = next->sibling)
801 next->parent = new;
803 if (parent->child == first) {
804 parent->child = new;
805 } else {
806 next = parent->child;
807 while (next->sibling != first)
808 next = next->sibling;
809 next->sibling = new;
811 return NULL;
815 * insert_resource_conflict - Inserts resource in the resource tree
816 * @parent: parent of the new resource
817 * @new: new resource to insert
819 * Returns 0 on success, conflict resource if the resource can't be inserted.
821 * This function is equivalent to request_resource_conflict when no conflict
822 * happens. If a conflict happens, and the conflicting resources
823 * entirely fit within the range of the new resource, then the new
824 * resource is inserted and the conflicting resources become children of
825 * the new resource.
827 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
829 struct resource *conflict;
831 write_lock(&resource_lock);
832 conflict = __insert_resource(parent, new);
833 write_unlock(&resource_lock);
834 return conflict;
838 * insert_resource - Inserts a resource in the resource tree
839 * @parent: parent of the new resource
840 * @new: new resource to insert
842 * Returns 0 on success, -EBUSY if the resource can't be inserted.
844 int insert_resource(struct resource *parent, struct resource *new)
846 struct resource *conflict;
848 conflict = insert_resource_conflict(parent, new);
849 return conflict ? -EBUSY : 0;
853 * insert_resource_expand_to_fit - Insert a resource into the resource tree
854 * @root: root resource descriptor
855 * @new: new resource to insert
857 * Insert a resource into the resource tree, possibly expanding it in order
858 * to make it encompass any conflicting resources.
860 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
862 if (new->parent)
863 return;
865 write_lock(&resource_lock);
866 for (;;) {
867 struct resource *conflict;
869 conflict = __insert_resource(root, new);
870 if (!conflict)
871 break;
872 if (conflict == root)
873 break;
875 /* Ok, expand resource to cover the conflict, then try again .. */
876 if (conflict->start < new->start)
877 new->start = conflict->start;
878 if (conflict->end > new->end)
879 new->end = conflict->end;
881 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
883 write_unlock(&resource_lock);
886 static int __adjust_resource(struct resource *res, resource_size_t start,
887 resource_size_t size)
889 struct resource *tmp, *parent = res->parent;
890 resource_size_t end = start + size - 1;
891 int result = -EBUSY;
893 if (!parent)
894 goto skip;
896 if ((start < parent->start) || (end > parent->end))
897 goto out;
899 if (res->sibling && (res->sibling->start <= end))
900 goto out;
902 tmp = parent->child;
903 if (tmp != res) {
904 while (tmp->sibling != res)
905 tmp = tmp->sibling;
906 if (start <= tmp->end)
907 goto out;
910 skip:
911 for (tmp = res->child; tmp; tmp = tmp->sibling)
912 if ((tmp->start < start) || (tmp->end > end))
913 goto out;
915 res->start = start;
916 res->end = end;
917 result = 0;
919 out:
920 return result;
924 * adjust_resource - modify a resource's start and size
925 * @res: resource to modify
926 * @start: new start value
927 * @size: new size
929 * Given an existing resource, change its start and size to match the
930 * arguments. Returns 0 on success, -EBUSY if it can't fit.
931 * Existing children of the resource are assumed to be immutable.
933 int adjust_resource(struct resource *res, resource_size_t start,
934 resource_size_t size)
936 int result;
938 write_lock(&resource_lock);
939 result = __adjust_resource(res, start, size);
940 write_unlock(&resource_lock);
941 return result;
943 EXPORT_SYMBOL(adjust_resource);
945 static void __init __reserve_region_with_split(struct resource *root,
946 resource_size_t start, resource_size_t end,
947 const char *name)
949 struct resource *parent = root;
950 struct resource *conflict;
951 struct resource *res = alloc_resource(GFP_ATOMIC);
952 struct resource *next_res = NULL;
954 if (!res)
955 return;
957 res->name = name;
958 res->start = start;
959 res->end = end;
960 res->flags = IORESOURCE_BUSY;
962 while (1) {
964 conflict = __request_resource(parent, res);
965 if (!conflict) {
966 if (!next_res)
967 break;
968 res = next_res;
969 next_res = NULL;
970 continue;
973 /* conflict covered whole area */
974 if (conflict->start <= res->start &&
975 conflict->end >= res->end) {
976 free_resource(res);
977 WARN_ON(next_res);
978 break;
981 /* failed, split and try again */
982 if (conflict->start > res->start) {
983 end = res->end;
984 res->end = conflict->start - 1;
985 if (conflict->end < end) {
986 next_res = alloc_resource(GFP_ATOMIC);
987 if (!next_res) {
988 free_resource(res);
989 break;
991 next_res->name = name;
992 next_res->start = conflict->end + 1;
993 next_res->end = end;
994 next_res->flags = IORESOURCE_BUSY;
996 } else {
997 res->start = conflict->end + 1;
1003 void __init reserve_region_with_split(struct resource *root,
1004 resource_size_t start, resource_size_t end,
1005 const char *name)
1007 int abort = 0;
1009 write_lock(&resource_lock);
1010 if (root->start > start || root->end < end) {
1011 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1012 (unsigned long long)start, (unsigned long long)end,
1013 root);
1014 if (start > root->end || end < root->start)
1015 abort = 1;
1016 else {
1017 if (end > root->end)
1018 end = root->end;
1019 if (start < root->start)
1020 start = root->start;
1021 pr_err("fixing request to [0x%llx-0x%llx]\n",
1022 (unsigned long long)start,
1023 (unsigned long long)end);
1025 dump_stack();
1027 if (!abort)
1028 __reserve_region_with_split(root, start, end, name);
1029 write_unlock(&resource_lock);
1033 * resource_alignment - calculate resource's alignment
1034 * @res: resource pointer
1036 * Returns alignment on success, 0 (invalid alignment) on failure.
1038 resource_size_t resource_alignment(struct resource *res)
1040 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1041 case IORESOURCE_SIZEALIGN:
1042 return resource_size(res);
1043 case IORESOURCE_STARTALIGN:
1044 return res->start;
1045 default:
1046 return 0;
1051 * This is compatibility stuff for IO resources.
1053 * Note how this, unlike the above, knows about
1054 * the IO flag meanings (busy etc).
1056 * request_region creates a new busy region.
1058 * release_region releases a matching busy region.
1061 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1064 * __request_region - create a new busy resource region
1065 * @parent: parent resource descriptor
1066 * @start: resource start address
1067 * @n: resource region size
1068 * @name: reserving caller's ID string
1069 * @flags: IO resource flags
1071 struct resource * __request_region(struct resource *parent,
1072 resource_size_t start, resource_size_t n,
1073 const char *name, int flags)
1075 DECLARE_WAITQUEUE(wait, current);
1076 struct resource *res = alloc_resource(GFP_KERNEL);
1078 if (!res)
1079 return NULL;
1081 res->name = name;
1082 res->start = start;
1083 res->end = start + n - 1;
1084 res->flags = resource_type(parent);
1085 res->flags |= IORESOURCE_BUSY | flags;
1087 write_lock(&resource_lock);
1089 for (;;) {
1090 struct resource *conflict;
1092 conflict = __request_resource(parent, res);
1093 if (!conflict)
1094 break;
1095 if (conflict != parent) {
1096 if (!(conflict->flags & IORESOURCE_BUSY)) {
1097 parent = conflict;
1098 continue;
1101 if (conflict->flags & flags & IORESOURCE_MUXED) {
1102 add_wait_queue(&muxed_resource_wait, &wait);
1103 write_unlock(&resource_lock);
1104 set_current_state(TASK_UNINTERRUPTIBLE);
1105 schedule();
1106 remove_wait_queue(&muxed_resource_wait, &wait);
1107 write_lock(&resource_lock);
1108 continue;
1110 /* Uhhuh, that didn't work out.. */
1111 free_resource(res);
1112 res = NULL;
1113 break;
1115 write_unlock(&resource_lock);
1116 return res;
1118 EXPORT_SYMBOL(__request_region);
1121 * __release_region - release a previously reserved resource region
1122 * @parent: parent resource descriptor
1123 * @start: resource start address
1124 * @n: resource region size
1126 * The described resource region must match a currently busy region.
1128 void __release_region(struct resource *parent, resource_size_t start,
1129 resource_size_t n)
1131 struct resource **p;
1132 resource_size_t end;
1134 p = &parent->child;
1135 end = start + n - 1;
1137 write_lock(&resource_lock);
1139 for (;;) {
1140 struct resource *res = *p;
1142 if (!res)
1143 break;
1144 if (res->start <= start && res->end >= end) {
1145 if (!(res->flags & IORESOURCE_BUSY)) {
1146 p = &res->child;
1147 continue;
1149 if (res->start != start || res->end != end)
1150 break;
1151 *p = res->sibling;
1152 write_unlock(&resource_lock);
1153 if (res->flags & IORESOURCE_MUXED)
1154 wake_up(&muxed_resource_wait);
1155 free_resource(res);
1156 return;
1158 p = &res->sibling;
1161 write_unlock(&resource_lock);
1163 printk(KERN_WARNING "Trying to free nonexistent resource "
1164 "<%016llx-%016llx>\n", (unsigned long long)start,
1165 (unsigned long long)end);
1167 EXPORT_SYMBOL(__release_region);
1169 #ifdef CONFIG_MEMORY_HOTREMOVE
1171 * release_mem_region_adjustable - release a previously reserved memory region
1172 * @parent: parent resource descriptor
1173 * @start: resource start address
1174 * @size: resource region size
1176 * This interface is intended for memory hot-delete. The requested region
1177 * is released from a currently busy memory resource. The requested region
1178 * must either match exactly or fit into a single busy resource entry. In
1179 * the latter case, the remaining resource is adjusted accordingly.
1180 * Existing children of the busy memory resource must be immutable in the
1181 * request.
1183 * Note:
1184 * - Additional release conditions, such as overlapping region, can be
1185 * supported after they are confirmed as valid cases.
1186 * - When a busy memory resource gets split into two entries, the code
1187 * assumes that all children remain in the lower address entry for
1188 * simplicity. Enhance this logic when necessary.
1190 int release_mem_region_adjustable(struct resource *parent,
1191 resource_size_t start, resource_size_t size)
1193 struct resource **p;
1194 struct resource *res;
1195 struct resource *new_res;
1196 resource_size_t end;
1197 int ret = -EINVAL;
1199 end = start + size - 1;
1200 if ((start < parent->start) || (end > parent->end))
1201 return ret;
1203 /* The alloc_resource() result gets checked later */
1204 new_res = alloc_resource(GFP_KERNEL);
1206 p = &parent->child;
1207 write_lock(&resource_lock);
1209 while ((res = *p)) {
1210 if (res->start >= end)
1211 break;
1213 /* look for the next resource if it does not fit into */
1214 if (res->start > start || res->end < end) {
1215 p = &res->sibling;
1216 continue;
1219 if (!(res->flags & IORESOURCE_MEM))
1220 break;
1222 if (!(res->flags & IORESOURCE_BUSY)) {
1223 p = &res->child;
1224 continue;
1227 /* found the target resource; let's adjust accordingly */
1228 if (res->start == start && res->end == end) {
1229 /* free the whole entry */
1230 *p = res->sibling;
1231 free_resource(res);
1232 ret = 0;
1233 } else if (res->start == start && res->end != end) {
1234 /* adjust the start */
1235 ret = __adjust_resource(res, end + 1,
1236 res->end - end);
1237 } else if (res->start != start && res->end == end) {
1238 /* adjust the end */
1239 ret = __adjust_resource(res, res->start,
1240 start - res->start);
1241 } else {
1242 /* split into two entries */
1243 if (!new_res) {
1244 ret = -ENOMEM;
1245 break;
1247 new_res->name = res->name;
1248 new_res->start = end + 1;
1249 new_res->end = res->end;
1250 new_res->flags = res->flags;
1251 new_res->parent = res->parent;
1252 new_res->sibling = res->sibling;
1253 new_res->child = NULL;
1255 ret = __adjust_resource(res, res->start,
1256 start - res->start);
1257 if (ret)
1258 break;
1259 res->sibling = new_res;
1260 new_res = NULL;
1263 break;
1266 write_unlock(&resource_lock);
1267 free_resource(new_res);
1268 return ret;
1270 #endif /* CONFIG_MEMORY_HOTREMOVE */
1273 * Managed region resource
1275 static void devm_resource_release(struct device *dev, void *ptr)
1277 struct resource **r = ptr;
1279 release_resource(*r);
1283 * devm_request_resource() - request and reserve an I/O or memory resource
1284 * @dev: device for which to request the resource
1285 * @root: root of the resource tree from which to request the resource
1286 * @new: descriptor of the resource to request
1288 * This is a device-managed version of request_resource(). There is usually
1289 * no need to release resources requested by this function explicitly since
1290 * that will be taken care of when the device is unbound from its driver.
1291 * If for some reason the resource needs to be released explicitly, because
1292 * of ordering issues for example, drivers must call devm_release_resource()
1293 * rather than the regular release_resource().
1295 * When a conflict is detected between any existing resources and the newly
1296 * requested resource, an error message will be printed.
1298 * Returns 0 on success or a negative error code on failure.
1300 int devm_request_resource(struct device *dev, struct resource *root,
1301 struct resource *new)
1303 struct resource *conflict, **ptr;
1305 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1306 if (!ptr)
1307 return -ENOMEM;
1309 *ptr = new;
1311 conflict = request_resource_conflict(root, new);
1312 if (conflict) {
1313 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1314 new, conflict->name, conflict);
1315 devres_free(ptr);
1316 return -EBUSY;
1319 devres_add(dev, ptr);
1320 return 0;
1322 EXPORT_SYMBOL(devm_request_resource);
1324 static int devm_resource_match(struct device *dev, void *res, void *data)
1326 struct resource **ptr = res;
1328 return *ptr == data;
1332 * devm_release_resource() - release a previously requested resource
1333 * @dev: device for which to release the resource
1334 * @new: descriptor of the resource to release
1336 * Releases a resource previously requested using devm_request_resource().
1338 void devm_release_resource(struct device *dev, struct resource *new)
1340 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1341 new));
1343 EXPORT_SYMBOL(devm_release_resource);
1345 struct region_devres {
1346 struct resource *parent;
1347 resource_size_t start;
1348 resource_size_t n;
1351 static void devm_region_release(struct device *dev, void *res)
1353 struct region_devres *this = res;
1355 __release_region(this->parent, this->start, this->n);
1358 static int devm_region_match(struct device *dev, void *res, void *match_data)
1360 struct region_devres *this = res, *match = match_data;
1362 return this->parent == match->parent &&
1363 this->start == match->start && this->n == match->n;
1366 struct resource * __devm_request_region(struct device *dev,
1367 struct resource *parent, resource_size_t start,
1368 resource_size_t n, const char *name)
1370 struct region_devres *dr = NULL;
1371 struct resource *res;
1373 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1374 GFP_KERNEL);
1375 if (!dr)
1376 return NULL;
1378 dr->parent = parent;
1379 dr->start = start;
1380 dr->n = n;
1382 res = __request_region(parent, start, n, name, 0);
1383 if (res)
1384 devres_add(dev, dr);
1385 else
1386 devres_free(dr);
1388 return res;
1390 EXPORT_SYMBOL(__devm_request_region);
1392 void __devm_release_region(struct device *dev, struct resource *parent,
1393 resource_size_t start, resource_size_t n)
1395 struct region_devres match_data = { parent, start, n };
1397 __release_region(parent, start, n);
1398 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1399 &match_data));
1401 EXPORT_SYMBOL(__devm_release_region);
1404 * Called from init/main.c to reserve IO ports.
1406 #define MAXRESERVE 4
1407 static int __init reserve_setup(char *str)
1409 static int reserved;
1410 static struct resource reserve[MAXRESERVE];
1412 for (;;) {
1413 unsigned int io_start, io_num;
1414 int x = reserved;
1416 if (get_option (&str, &io_start) != 2)
1417 break;
1418 if (get_option (&str, &io_num) == 0)
1419 break;
1420 if (x < MAXRESERVE) {
1421 struct resource *res = reserve + x;
1422 res->name = "reserved";
1423 res->start = io_start;
1424 res->end = io_start + io_num - 1;
1425 res->flags = IORESOURCE_BUSY;
1426 res->child = NULL;
1427 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
1428 reserved = x+1;
1431 return 1;
1434 __setup("reserve=", reserve_setup);
1437 * Check if the requested addr and size spans more than any slot in the
1438 * iomem resource tree.
1440 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1442 struct resource *p = &iomem_resource;
1443 int err = 0;
1444 loff_t l;
1446 read_lock(&resource_lock);
1447 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1449 * We can probably skip the resources without
1450 * IORESOURCE_IO attribute?
1452 if (p->start >= addr + size)
1453 continue;
1454 if (p->end < addr)
1455 continue;
1456 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1457 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1458 continue;
1460 * if a resource is "BUSY", it's not a hardware resource
1461 * but a driver mapping of such a resource; we don't want
1462 * to warn for those; some drivers legitimately map only
1463 * partial hardware resources. (example: vesafb)
1465 if (p->flags & IORESOURCE_BUSY)
1466 continue;
1468 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1469 (unsigned long long)addr,
1470 (unsigned long long)(addr + size - 1),
1471 p->name, p);
1472 err = -1;
1473 break;
1475 read_unlock(&resource_lock);
1477 return err;
1480 #ifdef CONFIG_STRICT_DEVMEM
1481 static int strict_iomem_checks = 1;
1482 #else
1483 static int strict_iomem_checks;
1484 #endif
1487 * check if an address is reserved in the iomem resource tree
1488 * returns 1 if reserved, 0 if not reserved.
1490 int iomem_is_exclusive(u64 addr)
1492 struct resource *p = &iomem_resource;
1493 int err = 0;
1494 loff_t l;
1495 int size = PAGE_SIZE;
1497 if (!strict_iomem_checks)
1498 return 0;
1500 addr = addr & PAGE_MASK;
1502 read_lock(&resource_lock);
1503 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1505 * We can probably skip the resources without
1506 * IORESOURCE_IO attribute?
1508 if (p->start >= addr + size)
1509 break;
1510 if (p->end < addr)
1511 continue;
1512 if (p->flags & IORESOURCE_BUSY &&
1513 p->flags & IORESOURCE_EXCLUSIVE) {
1514 err = 1;
1515 break;
1518 read_unlock(&resource_lock);
1520 return err;
1523 struct resource_entry *resource_list_create_entry(struct resource *res,
1524 size_t extra_size)
1526 struct resource_entry *entry;
1528 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1529 if (entry) {
1530 INIT_LIST_HEAD(&entry->node);
1531 entry->res = res ? res : &entry->__res;
1534 return entry;
1536 EXPORT_SYMBOL(resource_list_create_entry);
1538 void resource_list_free(struct list_head *head)
1540 struct resource_entry *entry, *tmp;
1542 list_for_each_entry_safe(entry, tmp, head, node)
1543 resource_list_destroy_entry(entry);
1545 EXPORT_SYMBOL(resource_list_free);
1547 static int __init strict_iomem(char *str)
1549 if (strstr(str, "relaxed"))
1550 strict_iomem_checks = 0;
1551 if (strstr(str, "strict"))
1552 strict_iomem_checks = 1;
1553 return 1;
1556 __setup("iomem=", strict_iomem);