defconfig: set extra cmdline for apex
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / kernel / resource.c
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
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
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>
27
28
29 struct resource ioport_resource = {
30 .name = "PCI IO",
31 .start = 0,
32 .end = IO_SPACE_LIMIT,
33 .flags = IORESOURCE_IO,
34 };
35 EXPORT_SYMBOL(ioport_resource);
36
37 struct resource iomem_resource = {
38 .name = "PCI mem",
39 .start = 0,
40 .end = -1,
41 .flags = IORESOURCE_MEM,
42 };
43 EXPORT_SYMBOL(iomem_resource);
44
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;
51 };
52
53 static DEFINE_RWLOCK(resource_lock);
54
55 /*
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.
59 */
60 static struct resource *bootmem_resource_free;
61 static DEFINE_SPINLOCK(bootmem_resource_lock);
62
63 static struct resource *next_resource(struct resource *p, bool sibling_only)
64 {
65 /* Caller wants to traverse through siblings only */
66 if (sibling_only)
67 return p->sibling;
68
69 if (p->child)
70 return p->child;
71 while (!p->sibling && p->parent)
72 p = p->parent;
73 return p->sibling;
74 }
75
76 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
77 {
78 struct resource *p = v;
79 (*pos)++;
80 return (void *)next_resource(p, false);
81 }
82
83 #ifdef CONFIG_PROC_FS
84
85 enum { MAX_IORES_LEVEL = 5 };
86
87 static void *r_start(struct seq_file *m, loff_t *pos)
88 __acquires(resource_lock)
89 {
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))
94 ;
95 return p;
96 }
97
98 static void r_stop(struct seq_file *m, void *v)
99 __releases(resource_lock)
100 {
101 read_unlock(&resource_lock);
102 }
103
104 static int r_show(struct seq_file *m, void *v)
105 {
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;
111
112 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
113 if (p->parent == root)
114 break;
115
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;
121 }
122
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;
129 }
130
131 static const struct seq_operations resource_op = {
132 .start = r_start,
133 .next = r_next,
134 .stop = r_stop,
135 .show = r_show,
136 };
137
138 static int ioports_open(struct inode *inode, struct file *file)
139 {
140 int res = seq_open(file, &resource_op);
141 if (!res) {
142 struct seq_file *m = file->private_data;
143 m->private = &ioport_resource;
144 }
145 return res;
146 }
147
148 static int iomem_open(struct inode *inode, struct file *file)
149 {
150 int res = seq_open(file, &resource_op);
151 if (!res) {
152 struct seq_file *m = file->private_data;
153 m->private = &iomem_resource;
154 }
155 return res;
156 }
157
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,
163 };
164
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,
170 };
171
172 static int __init ioresources_init(void)
173 {
174 proc_create("ioports", 0, NULL, &proc_ioports_operations);
175 proc_create("iomem", S_IRUSR, NULL, &proc_iomem_operations);
176 return 0;
177 }
178 __initcall(ioresources_init);
179
180 #endif /* CONFIG_PROC_FS */
181
182 static void free_resource(struct resource *res)
183 {
184 if (!res)
185 return;
186
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);
194 }
195 }
196
197 static struct resource *alloc_resource(gfp_t flags)
198 {
199 struct resource *res = NULL;
200
201 spin_lock(&bootmem_resource_lock);
202 if (bootmem_resource_free) {
203 res = bootmem_resource_free;
204 bootmem_resource_free = res->sibling;
205 }
206 spin_unlock(&bootmem_resource_lock);
207
208 if (res)
209 memset(res, 0, sizeof(struct resource));
210 else
211 res = kzalloc(sizeof(struct resource), flags);
212
213 return res;
214 }
215
216 /* Return the conflict entry if you can't request it */
217 static struct resource * __request_resource(struct resource *root, struct resource *new)
218 {
219 resource_size_t start = new->start;
220 resource_size_t end = new->end;
221 struct resource *tmp, **p;
222
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;
237 }
238 p = &tmp->sibling;
239 if (tmp->end < start)
240 continue;
241 return tmp;
242 }
243 }
244
245 static int __release_resource(struct resource *old)
246 {
247 struct resource *tmp, **p;
248
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;
258 }
259 p = &tmp->sibling;
260 }
261 return -EINVAL;
262 }
263
264 static void __release_child_resources(struct resource *r)
265 {
266 struct resource *tmp, *p;
267 resource_size_t size;
268
269 p = r->child;
270 r->child = NULL;
271 while (p) {
272 tmp = p;
273 p = p->sibling;
274
275 tmp->parent = NULL;
276 tmp->sibling = NULL;
277 __release_child_resources(tmp);
278
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;
284 }
285 }
286
287 void release_child_resources(struct resource *r)
288 {
289 write_lock(&resource_lock);
290 __release_child_resources(r);
291 write_unlock(&resource_lock);
292 }
293
294 /**
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
298 *
299 * Returns 0 for success, conflict resource on error.
300 */
301 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
302 {
303 struct resource *conflict;
304
305 write_lock(&resource_lock);
306 conflict = __request_resource(root, new);
307 write_unlock(&resource_lock);
308 return conflict;
309 }
310
311 /**
312 * request_resource - request and reserve an I/O or memory resource
313 * @root: root resource descriptor
314 * @new: resource descriptor desired by caller
315 *
316 * Returns 0 for success, negative error code on error.
317 */
318 int request_resource(struct resource *root, struct resource *new)
319 {
320 struct resource *conflict;
321
322 conflict = request_resource_conflict(root, new);
323 return conflict ? -EBUSY : 0;
324 }
325
326 EXPORT_SYMBOL(request_resource);
327
328 /**
329 * release_resource - release a previously reserved resource
330 * @old: resource pointer
331 */
332 int release_resource(struct resource *old)
333 {
334 int retval;
335
336 write_lock(&resource_lock);
337 retval = __release_resource(old);
338 write_unlock(&resource_lock);
339 return retval;
340 }
341
342 EXPORT_SYMBOL(release_resource);
343
344 /*
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.
350 */
351 static int find_next_iomem_res(struct resource *res, char *name,
352 bool first_level_children_only)
353 {
354 resource_size_t start, end;
355 struct resource *p;
356 bool sibling_only = false;
357
358 BUG_ON(!res);
359
360 start = res->start;
361 end = res->end;
362 BUG_ON(start >= end);
363
364 if (first_level_children_only)
365 sibling_only = true;
366
367 read_lock(&resource_lock);
368
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;
377 }
378 if ((p->end >= start) && (p->start < end))
379 break;
380 }
381
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;
391 }
392
393 /*
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.
398 *
399 * @name: name of resource
400 * @flags: resource flags
401 * @start: start addr
402 * @end: end addr
403 */
404 int walk_iomem_res(char *name, unsigned long flags, u64 start, u64 end,
405 void *arg, int (*func)(u64, u64, void *))
406 {
407 struct resource res;
408 u64 orig_end;
409 int ret = -1;
410
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;
422 }
423 return ret;
424 }
425
426 /*
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.
432 */
433 int walk_system_ram_res(u64 start, u64 end, void *arg,
434 int (*func)(u64, u64, void *))
435 {
436 struct resource res;
437 u64 orig_end;
438 int ret = -1;
439
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;
451 }
452 return ret;
453 }
454
455 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
456
457 /*
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".
461 */
462 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
463 void *arg, int (*func)(unsigned long, unsigned long, void *))
464 {
465 struct resource res;
466 unsigned long pfn, end_pfn;
467 u64 orig_end;
468 int ret = -1;
469
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;
484 }
485 return ret;
486 }
487
488 #endif
489
490 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
491 {
492 return 1;
493 }
494 /*
495 * This generic page_is_ram() returns true if specified address is
496 * registered as "System RAM" in iomem_resource list.
497 */
498 int __weak page_is_ram(unsigned long pfn)
499 {
500 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
501 }
502 EXPORT_SYMBOL_GPL(page_is_ram);
503
504 /**
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)
509 *
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.
517 *
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.
521 */
522 int region_intersects(resource_size_t start, size_t size, const char *name)
523 {
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;
528
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;
532
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++;
539 }
540 read_unlock(&resource_lock);
541
542 if (other == 0)
543 return type ? REGION_INTERSECTS : REGION_DISJOINT;
544
545 if (type)
546 return REGION_MIXED;
547
548 return REGION_DISJOINT;
549 }
550
551 void __weak arch_remove_reservations(struct resource *avail)
552 {
553 }
554
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)
559 {
560 return avail->start;
561 }
562
563 static void resource_clip(struct resource *res, resource_size_t min,
564 resource_size_t max)
565 {
566 if (res->start < min)
567 res->start = min;
568 if (res->end > max)
569 res->end = max;
570 }
571
572 /*
573 * Find empty slot in the resource tree with the given range and
574 * alignment constraints
575 */
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)
580 {
581 struct resource *this = root->child;
582 struct resource tmp = *new, avail, alloc;
583
584 tmp.start = root->start;
585 /*
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.
588 */
589 if (this && this->start == root->start) {
590 tmp.start = (this == old) ? old->start : this->end + 1;
591 this = this->sibling;
592 }
593 for(;;) {
594 if (this)
595 tmp.end = (this == old) ? this->end : this->start - 1;
596 else
597 tmp.end = root->end;
598
599 if (tmp.end < tmp.start)
600 goto next;
601
602 resource_clip(&tmp, constraint->min, constraint->max);
603 arch_remove_reservations(&tmp);
604
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 (resource_contains(&avail, &alloc)) {
615 new->start = alloc.start;
616 new->end = alloc.end;
617 return 0;
618 }
619 }
620
621 next: if (!this || this->end == root->end)
622 break;
623
624 if (this != old)
625 tmp.start = this->end + 1;
626 this = this->sibling;
627 }
628 return -EBUSY;
629 }
630
631 /*
632 * Find empty slot in the resource tree given range and alignment.
633 */
634 static int find_resource(struct resource *root, struct resource *new,
635 resource_size_t size,
636 struct resource_constraint *constraint)
637 {
638 return __find_resource(root, NULL, new, size, constraint);
639 }
640
641 /**
642 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
643 * The resource will be relocated if the new size cannot be reallocated in the
644 * current location.
645 *
646 * @root: root resource descriptor
647 * @old: resource descriptor desired by caller
648 * @newsize: new size of the resource descriptor
649 * @constraint: the size and alignment constraints to be met.
650 */
651 static int reallocate_resource(struct resource *root, struct resource *old,
652 resource_size_t newsize,
653 struct resource_constraint *constraint)
654 {
655 int err=0;
656 struct resource new = *old;
657 struct resource *conflict;
658
659 write_lock(&resource_lock);
660
661 if ((err = __find_resource(root, old, &new, newsize, constraint)))
662 goto out;
663
664 if (resource_contains(&new, old)) {
665 old->start = new.start;
666 old->end = new.end;
667 goto out;
668 }
669
670 if (old->child) {
671 err = -EBUSY;
672 goto out;
673 }
674
675 if (resource_contains(old, &new)) {
676 old->start = new.start;
677 old->end = new.end;
678 } else {
679 __release_resource(old);
680 *old = new;
681 conflict = __request_resource(root, old);
682 BUG_ON(conflict);
683 }
684 out:
685 write_unlock(&resource_lock);
686 return err;
687 }
688
689
690 /**
691 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
692 * The resource will be reallocated with a new size if it was already allocated
693 * @root: root resource descriptor
694 * @new: resource descriptor desired by caller
695 * @size: requested resource region size
696 * @min: minimum boundary to allocate
697 * @max: maximum boundary to allocate
698 * @align: alignment requested, in bytes
699 * @alignf: alignment function, optional, called if not NULL
700 * @alignf_data: arbitrary data to pass to the @alignf function
701 */
702 int allocate_resource(struct resource *root, struct resource *new,
703 resource_size_t size, resource_size_t min,
704 resource_size_t max, resource_size_t align,
705 resource_size_t (*alignf)(void *,
706 const struct resource *,
707 resource_size_t,
708 resource_size_t),
709 void *alignf_data)
710 {
711 int err;
712 struct resource_constraint constraint;
713
714 if (!alignf)
715 alignf = simple_align_resource;
716
717 constraint.min = min;
718 constraint.max = max;
719 constraint.align = align;
720 constraint.alignf = alignf;
721 constraint.alignf_data = alignf_data;
722
723 if ( new->parent ) {
724 /* resource is already allocated, try reallocating with
725 the new constraints */
726 return reallocate_resource(root, new, size, &constraint);
727 }
728
729 write_lock(&resource_lock);
730 err = find_resource(root, new, size, &constraint);
731 if (err >= 0 && __request_resource(root, new))
732 err = -EBUSY;
733 write_unlock(&resource_lock);
734 return err;
735 }
736
737 EXPORT_SYMBOL(allocate_resource);
738
739 /**
740 * lookup_resource - find an existing resource by a resource start address
741 * @root: root resource descriptor
742 * @start: resource start address
743 *
744 * Returns a pointer to the resource if found, NULL otherwise
745 */
746 struct resource *lookup_resource(struct resource *root, resource_size_t start)
747 {
748 struct resource *res;
749
750 read_lock(&resource_lock);
751 for (res = root->child; res; res = res->sibling) {
752 if (res->start == start)
753 break;
754 }
755 read_unlock(&resource_lock);
756
757 return res;
758 }
759
760 /*
761 * Insert a resource into the resource tree. If successful, return NULL,
762 * otherwise return the conflicting resource (compare to __request_resource())
763 */
764 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
765 {
766 struct resource *first, *next;
767
768 for (;; parent = first) {
769 first = __request_resource(parent, new);
770 if (!first)
771 return first;
772
773 if (first == parent)
774 return first;
775 if (WARN_ON(first == new)) /* duplicated insertion */
776 return first;
777
778 if ((first->start > new->start) || (first->end < new->end))
779 break;
780 if ((first->start == new->start) && (first->end == new->end))
781 break;
782 }
783
784 for (next = first; ; next = next->sibling) {
785 /* Partial overlap? Bad, and unfixable */
786 if (next->start < new->start || next->end > new->end)
787 return next;
788 if (!next->sibling)
789 break;
790 if (next->sibling->start > new->end)
791 break;
792 }
793
794 new->parent = parent;
795 new->sibling = next->sibling;
796 new->child = first;
797
798 next->sibling = NULL;
799 for (next = first; next; next = next->sibling)
800 next->parent = new;
801
802 if (parent->child == first) {
803 parent->child = new;
804 } else {
805 next = parent->child;
806 while (next->sibling != first)
807 next = next->sibling;
808 next->sibling = new;
809 }
810 return NULL;
811 }
812
813 /**
814 * insert_resource_conflict - Inserts resource in the resource tree
815 * @parent: parent of the new resource
816 * @new: new resource to insert
817 *
818 * Returns 0 on success, conflict resource if the resource can't be inserted.
819 *
820 * This function is equivalent to request_resource_conflict when no conflict
821 * happens. If a conflict happens, and the conflicting resources
822 * entirely fit within the range of the new resource, then the new
823 * resource is inserted and the conflicting resources become children of
824 * the new resource.
825 */
826 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
827 {
828 struct resource *conflict;
829
830 write_lock(&resource_lock);
831 conflict = __insert_resource(parent, new);
832 write_unlock(&resource_lock);
833 return conflict;
834 }
835
836 /**
837 * insert_resource - Inserts a resource in the resource tree
838 * @parent: parent of the new resource
839 * @new: new resource to insert
840 *
841 * Returns 0 on success, -EBUSY if the resource can't be inserted.
842 */
843 int insert_resource(struct resource *parent, struct resource *new)
844 {
845 struct resource *conflict;
846
847 conflict = insert_resource_conflict(parent, new);
848 return conflict ? -EBUSY : 0;
849 }
850
851 /**
852 * insert_resource_expand_to_fit - Insert a resource into the resource tree
853 * @root: root resource descriptor
854 * @new: new resource to insert
855 *
856 * Insert a resource into the resource tree, possibly expanding it in order
857 * to make it encompass any conflicting resources.
858 */
859 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
860 {
861 if (new->parent)
862 return;
863
864 write_lock(&resource_lock);
865 for (;;) {
866 struct resource *conflict;
867
868 conflict = __insert_resource(root, new);
869 if (!conflict)
870 break;
871 if (conflict == root)
872 break;
873
874 /* Ok, expand resource to cover the conflict, then try again .. */
875 if (conflict->start < new->start)
876 new->start = conflict->start;
877 if (conflict->end > new->end)
878 new->end = conflict->end;
879
880 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
881 }
882 write_unlock(&resource_lock);
883 }
884
885 static int __adjust_resource(struct resource *res, resource_size_t start,
886 resource_size_t size)
887 {
888 struct resource *tmp, *parent = res->parent;
889 resource_size_t end = start + size - 1;
890 int result = -EBUSY;
891
892 if (!parent)
893 goto skip;
894
895 if ((start < parent->start) || (end > parent->end))
896 goto out;
897
898 if (res->sibling && (res->sibling->start <= end))
899 goto out;
900
901 tmp = parent->child;
902 if (tmp != res) {
903 while (tmp->sibling != res)
904 tmp = tmp->sibling;
905 if (start <= tmp->end)
906 goto out;
907 }
908
909 skip:
910 for (tmp = res->child; tmp; tmp = tmp->sibling)
911 if ((tmp->start < start) || (tmp->end > end))
912 goto out;
913
914 res->start = start;
915 res->end = end;
916 result = 0;
917
918 out:
919 return result;
920 }
921
922 /**
923 * adjust_resource - modify a resource's start and size
924 * @res: resource to modify
925 * @start: new start value
926 * @size: new size
927 *
928 * Given an existing resource, change its start and size to match the
929 * arguments. Returns 0 on success, -EBUSY if it can't fit.
930 * Existing children of the resource are assumed to be immutable.
931 */
932 int adjust_resource(struct resource *res, resource_size_t start,
933 resource_size_t size)
934 {
935 int result;
936
937 write_lock(&resource_lock);
938 result = __adjust_resource(res, start, size);
939 write_unlock(&resource_lock);
940 return result;
941 }
942 EXPORT_SYMBOL(adjust_resource);
943
944 static void __init __reserve_region_with_split(struct resource *root,
945 resource_size_t start, resource_size_t end,
946 const char *name)
947 {
948 struct resource *parent = root;
949 struct resource *conflict;
950 struct resource *res = alloc_resource(GFP_ATOMIC);
951 struct resource *next_res = NULL;
952
953 if (!res)
954 return;
955
956 res->name = name;
957 res->start = start;
958 res->end = end;
959 res->flags = IORESOURCE_BUSY;
960
961 while (1) {
962
963 conflict = __request_resource(parent, res);
964 if (!conflict) {
965 if (!next_res)
966 break;
967 res = next_res;
968 next_res = NULL;
969 continue;
970 }
971
972 /* conflict covered whole area */
973 if (conflict->start <= res->start &&
974 conflict->end >= res->end) {
975 free_resource(res);
976 WARN_ON(next_res);
977 break;
978 }
979
980 /* failed, split and try again */
981 if (conflict->start > res->start) {
982 end = res->end;
983 res->end = conflict->start - 1;
984 if (conflict->end < end) {
985 next_res = alloc_resource(GFP_ATOMIC);
986 if (!next_res) {
987 free_resource(res);
988 break;
989 }
990 next_res->name = name;
991 next_res->start = conflict->end + 1;
992 next_res->end = end;
993 next_res->flags = IORESOURCE_BUSY;
994 }
995 } else {
996 res->start = conflict->end + 1;
997 }
998 }
999
1000 }
1001
1002 void __init reserve_region_with_split(struct resource *root,
1003 resource_size_t start, resource_size_t end,
1004 const char *name)
1005 {
1006 int abort = 0;
1007
1008 write_lock(&resource_lock);
1009 if (root->start > start || root->end < end) {
1010 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1011 (unsigned long long)start, (unsigned long long)end,
1012 root);
1013 if (start > root->end || end < root->start)
1014 abort = 1;
1015 else {
1016 if (end > root->end)
1017 end = root->end;
1018 if (start < root->start)
1019 start = root->start;
1020 pr_err("fixing request to [0x%llx-0x%llx]\n",
1021 (unsigned long long)start,
1022 (unsigned long long)end);
1023 }
1024 dump_stack();
1025 }
1026 if (!abort)
1027 __reserve_region_with_split(root, start, end, name);
1028 write_unlock(&resource_lock);
1029 }
1030
1031 /**
1032 * resource_alignment - calculate resource's alignment
1033 * @res: resource pointer
1034 *
1035 * Returns alignment on success, 0 (invalid alignment) on failure.
1036 */
1037 resource_size_t resource_alignment(struct resource *res)
1038 {
1039 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1040 case IORESOURCE_SIZEALIGN:
1041 return resource_size(res);
1042 case IORESOURCE_STARTALIGN:
1043 return res->start;
1044 default:
1045 return 0;
1046 }
1047 }
1048
1049 /*
1050 * This is compatibility stuff for IO resources.
1051 *
1052 * Note how this, unlike the above, knows about
1053 * the IO flag meanings (busy etc).
1054 *
1055 * request_region creates a new busy region.
1056 *
1057 * release_region releases a matching busy region.
1058 */
1059
1060 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1061
1062 /**
1063 * __request_region - create a new busy resource region
1064 * @parent: parent resource descriptor
1065 * @start: resource start address
1066 * @n: resource region size
1067 * @name: reserving caller's ID string
1068 * @flags: IO resource flags
1069 */
1070 struct resource * __request_region(struct resource *parent,
1071 resource_size_t start, resource_size_t n,
1072 const char *name, int flags)
1073 {
1074 DECLARE_WAITQUEUE(wait, current);
1075 struct resource *res = alloc_resource(GFP_KERNEL);
1076
1077 if (!res)
1078 return NULL;
1079
1080 res->name = name;
1081 res->start = start;
1082 res->end = start + n - 1;
1083 res->flags = resource_type(parent);
1084 res->flags |= IORESOURCE_BUSY | flags;
1085
1086 write_lock(&resource_lock);
1087
1088 for (;;) {
1089 struct resource *conflict;
1090
1091 conflict = __request_resource(parent, res);
1092 if (!conflict)
1093 break;
1094 if (conflict != parent) {
1095 if (!(conflict->flags & IORESOURCE_BUSY)) {
1096 parent = conflict;
1097 continue;
1098 }
1099 }
1100 if (conflict->flags & flags & IORESOURCE_MUXED) {
1101 add_wait_queue(&muxed_resource_wait, &wait);
1102 write_unlock(&resource_lock);
1103 set_current_state(TASK_UNINTERRUPTIBLE);
1104 schedule();
1105 remove_wait_queue(&muxed_resource_wait, &wait);
1106 write_lock(&resource_lock);
1107 continue;
1108 }
1109 /* Uhhuh, that didn't work out.. */
1110 free_resource(res);
1111 res = NULL;
1112 break;
1113 }
1114 write_unlock(&resource_lock);
1115 return res;
1116 }
1117 EXPORT_SYMBOL(__request_region);
1118
1119 /**
1120 * __release_region - release a previously reserved resource region
1121 * @parent: parent resource descriptor
1122 * @start: resource start address
1123 * @n: resource region size
1124 *
1125 * The described resource region must match a currently busy region.
1126 */
1127 void __release_region(struct resource *parent, resource_size_t start,
1128 resource_size_t n)
1129 {
1130 struct resource **p;
1131 resource_size_t end;
1132
1133 p = &parent->child;
1134 end = start + n - 1;
1135
1136 write_lock(&resource_lock);
1137
1138 for (;;) {
1139 struct resource *res = *p;
1140
1141 if (!res)
1142 break;
1143 if (res->start <= start && res->end >= end) {
1144 if (!(res->flags & IORESOURCE_BUSY)) {
1145 p = &res->child;
1146 continue;
1147 }
1148 if (res->start != start || res->end != end)
1149 break;
1150 *p = res->sibling;
1151 write_unlock(&resource_lock);
1152 if (res->flags & IORESOURCE_MUXED)
1153 wake_up(&muxed_resource_wait);
1154 free_resource(res);
1155 return;
1156 }
1157 p = &res->sibling;
1158 }
1159
1160 write_unlock(&resource_lock);
1161
1162 printk(KERN_WARNING "Trying to free nonexistent resource "
1163 "<%016llx-%016llx>\n", (unsigned long long)start,
1164 (unsigned long long)end);
1165 }
1166 EXPORT_SYMBOL(__release_region);
1167
1168 #ifdef CONFIG_MEMORY_HOTREMOVE
1169 /**
1170 * release_mem_region_adjustable - release a previously reserved memory region
1171 * @parent: parent resource descriptor
1172 * @start: resource start address
1173 * @size: resource region size
1174 *
1175 * This interface is intended for memory hot-delete. The requested region
1176 * is released from a currently busy memory resource. The requested region
1177 * must either match exactly or fit into a single busy resource entry. In
1178 * the latter case, the remaining resource is adjusted accordingly.
1179 * Existing children of the busy memory resource must be immutable in the
1180 * request.
1181 *
1182 * Note:
1183 * - Additional release conditions, such as overlapping region, can be
1184 * supported after they are confirmed as valid cases.
1185 * - When a busy memory resource gets split into two entries, the code
1186 * assumes that all children remain in the lower address entry for
1187 * simplicity. Enhance this logic when necessary.
1188 */
1189 int release_mem_region_adjustable(struct resource *parent,
1190 resource_size_t start, resource_size_t size)
1191 {
1192 struct resource **p;
1193 struct resource *res;
1194 struct resource *new_res;
1195 resource_size_t end;
1196 int ret = -EINVAL;
1197
1198 end = start + size - 1;
1199 if ((start < parent->start) || (end > parent->end))
1200 return ret;
1201
1202 /* The alloc_resource() result gets checked later */
1203 new_res = alloc_resource(GFP_KERNEL);
1204
1205 p = &parent->child;
1206 write_lock(&resource_lock);
1207
1208 while ((res = *p)) {
1209 if (res->start >= end)
1210 break;
1211
1212 /* look for the next resource if it does not fit into */
1213 if (res->start > start || res->end < end) {
1214 p = &res->sibling;
1215 continue;
1216 }
1217
1218 if (!(res->flags & IORESOURCE_MEM))
1219 break;
1220
1221 if (!(res->flags & IORESOURCE_BUSY)) {
1222 p = &res->child;
1223 continue;
1224 }
1225
1226 /* found the target resource; let's adjust accordingly */
1227 if (res->start == start && res->end == end) {
1228 /* free the whole entry */
1229 *p = res->sibling;
1230 free_resource(res);
1231 ret = 0;
1232 } else if (res->start == start && res->end != end) {
1233 /* adjust the start */
1234 ret = __adjust_resource(res, end + 1,
1235 res->end - end);
1236 } else if (res->start != start && res->end == end) {
1237 /* adjust the end */
1238 ret = __adjust_resource(res, res->start,
1239 start - res->start);
1240 } else {
1241 /* split into two entries */
1242 if (!new_res) {
1243 ret = -ENOMEM;
1244 break;
1245 }
1246 new_res->name = res->name;
1247 new_res->start = end + 1;
1248 new_res->end = res->end;
1249 new_res->flags = res->flags;
1250 new_res->parent = res->parent;
1251 new_res->sibling = res->sibling;
1252 new_res->child = NULL;
1253
1254 ret = __adjust_resource(res, res->start,
1255 start - res->start);
1256 if (ret)
1257 break;
1258 res->sibling = new_res;
1259 new_res = NULL;
1260 }
1261
1262 break;
1263 }
1264
1265 write_unlock(&resource_lock);
1266 free_resource(new_res);
1267 return ret;
1268 }
1269 #endif /* CONFIG_MEMORY_HOTREMOVE */
1270
1271 /*
1272 * Managed region resource
1273 */
1274 static void devm_resource_release(struct device *dev, void *ptr)
1275 {
1276 struct resource **r = ptr;
1277
1278 release_resource(*r);
1279 }
1280
1281 /**
1282 * devm_request_resource() - request and reserve an I/O or memory resource
1283 * @dev: device for which to request the resource
1284 * @root: root of the resource tree from which to request the resource
1285 * @new: descriptor of the resource to request
1286 *
1287 * This is a device-managed version of request_resource(). There is usually
1288 * no need to release resources requested by this function explicitly since
1289 * that will be taken care of when the device is unbound from its driver.
1290 * If for some reason the resource needs to be released explicitly, because
1291 * of ordering issues for example, drivers must call devm_release_resource()
1292 * rather than the regular release_resource().
1293 *
1294 * When a conflict is detected between any existing resources and the newly
1295 * requested resource, an error message will be printed.
1296 *
1297 * Returns 0 on success or a negative error code on failure.
1298 */
1299 int devm_request_resource(struct device *dev, struct resource *root,
1300 struct resource *new)
1301 {
1302 struct resource *conflict, **ptr;
1303
1304 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1305 if (!ptr)
1306 return -ENOMEM;
1307
1308 *ptr = new;
1309
1310 conflict = request_resource_conflict(root, new);
1311 if (conflict) {
1312 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1313 new, conflict->name, conflict);
1314 devres_free(ptr);
1315 return -EBUSY;
1316 }
1317
1318 devres_add(dev, ptr);
1319 return 0;
1320 }
1321 EXPORT_SYMBOL(devm_request_resource);
1322
1323 static int devm_resource_match(struct device *dev, void *res, void *data)
1324 {
1325 struct resource **ptr = res;
1326
1327 return *ptr == data;
1328 }
1329
1330 /**
1331 * devm_release_resource() - release a previously requested resource
1332 * @dev: device for which to release the resource
1333 * @new: descriptor of the resource to release
1334 *
1335 * Releases a resource previously requested using devm_request_resource().
1336 */
1337 void devm_release_resource(struct device *dev, struct resource *new)
1338 {
1339 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1340 new));
1341 }
1342 EXPORT_SYMBOL(devm_release_resource);
1343
1344 struct region_devres {
1345 struct resource *parent;
1346 resource_size_t start;
1347 resource_size_t n;
1348 };
1349
1350 static void devm_region_release(struct device *dev, void *res)
1351 {
1352 struct region_devres *this = res;
1353
1354 __release_region(this->parent, this->start, this->n);
1355 }
1356
1357 static int devm_region_match(struct device *dev, void *res, void *match_data)
1358 {
1359 struct region_devres *this = res, *match = match_data;
1360
1361 return this->parent == match->parent &&
1362 this->start == match->start && this->n == match->n;
1363 }
1364
1365 struct resource * __devm_request_region(struct device *dev,
1366 struct resource *parent, resource_size_t start,
1367 resource_size_t n, const char *name)
1368 {
1369 struct region_devres *dr = NULL;
1370 struct resource *res;
1371
1372 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1373 GFP_KERNEL);
1374 if (!dr)
1375 return NULL;
1376
1377 dr->parent = parent;
1378 dr->start = start;
1379 dr->n = n;
1380
1381 res = __request_region(parent, start, n, name, 0);
1382 if (res)
1383 devres_add(dev, dr);
1384 else
1385 devres_free(dr);
1386
1387 return res;
1388 }
1389 EXPORT_SYMBOL(__devm_request_region);
1390
1391 void __devm_release_region(struct device *dev, struct resource *parent,
1392 resource_size_t start, resource_size_t n)
1393 {
1394 struct region_devres match_data = { parent, start, n };
1395
1396 __release_region(parent, start, n);
1397 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1398 &match_data));
1399 }
1400 EXPORT_SYMBOL(__devm_release_region);
1401
1402 /*
1403 * Called from init/main.c to reserve IO ports.
1404 */
1405 #define MAXRESERVE 4
1406 static int __init reserve_setup(char *str)
1407 {
1408 static int reserved;
1409 static struct resource reserve[MAXRESERVE];
1410
1411 for (;;) {
1412 unsigned int io_start, io_num;
1413 int x = reserved;
1414
1415 if (get_option (&str, &io_start) != 2)
1416 break;
1417 if (get_option (&str, &io_num) == 0)
1418 break;
1419 if (x < MAXRESERVE) {
1420 struct resource *res = reserve + x;
1421 res->name = "reserved";
1422 res->start = io_start;
1423 res->end = io_start + io_num - 1;
1424 res->flags = IORESOURCE_BUSY;
1425 res->child = NULL;
1426 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
1427 reserved = x+1;
1428 }
1429 }
1430 return 1;
1431 }
1432
1433 __setup("reserve=", reserve_setup);
1434
1435 /*
1436 * Check if the requested addr and size spans more than any slot in the
1437 * iomem resource tree.
1438 */
1439 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1440 {
1441 struct resource *p = &iomem_resource;
1442 int err = 0;
1443 loff_t l;
1444
1445 read_lock(&resource_lock);
1446 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1447 /*
1448 * We can probably skip the resources without
1449 * IORESOURCE_IO attribute?
1450 */
1451 if (p->start >= addr + size)
1452 continue;
1453 if (p->end < addr)
1454 continue;
1455 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1456 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1457 continue;
1458 /*
1459 * if a resource is "BUSY", it's not a hardware resource
1460 * but a driver mapping of such a resource; we don't want
1461 * to warn for those; some drivers legitimately map only
1462 * partial hardware resources. (example: vesafb)
1463 */
1464 if (p->flags & IORESOURCE_BUSY)
1465 continue;
1466
1467 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1468 (unsigned long long)addr,
1469 (unsigned long long)(addr + size - 1),
1470 p->name, p);
1471 err = -1;
1472 break;
1473 }
1474 read_unlock(&resource_lock);
1475
1476 return err;
1477 }
1478
1479 #ifdef CONFIG_STRICT_DEVMEM
1480 static int strict_iomem_checks = 1;
1481 #else
1482 static int strict_iomem_checks;
1483 #endif
1484
1485 /*
1486 * check if an address is reserved in the iomem resource tree
1487 * returns 1 if reserved, 0 if not reserved.
1488 */
1489 int iomem_is_exclusive(u64 addr)
1490 {
1491 struct resource *p = &iomem_resource;
1492 int err = 0;
1493 loff_t l;
1494 int size = PAGE_SIZE;
1495
1496 if (!strict_iomem_checks)
1497 return 0;
1498
1499 addr = addr & PAGE_MASK;
1500
1501 read_lock(&resource_lock);
1502 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1503 /*
1504 * We can probably skip the resources without
1505 * IORESOURCE_IO attribute?
1506 */
1507 if (p->start >= addr + size)
1508 break;
1509 if (p->end < addr)
1510 continue;
1511 if (p->flags & IORESOURCE_BUSY &&
1512 p->flags & IORESOURCE_EXCLUSIVE) {
1513 err = 1;
1514 break;
1515 }
1516 }
1517 read_unlock(&resource_lock);
1518
1519 return err;
1520 }
1521
1522 struct resource_entry *resource_list_create_entry(struct resource *res,
1523 size_t extra_size)
1524 {
1525 struct resource_entry *entry;
1526
1527 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1528 if (entry) {
1529 INIT_LIST_HEAD(&entry->node);
1530 entry->res = res ? res : &entry->__res;
1531 }
1532
1533 return entry;
1534 }
1535 EXPORT_SYMBOL(resource_list_create_entry);
1536
1537 void resource_list_free(struct list_head *head)
1538 {
1539 struct resource_entry *entry, *tmp;
1540
1541 list_for_each_entry_safe(entry, tmp, head, node)
1542 resource_list_destroy_entry(entry);
1543 }
1544 EXPORT_SYMBOL(resource_list_free);
1545
1546 static int __init strict_iomem(char *str)
1547 {
1548 if (strstr(str, "relaxed"))
1549 strict_iomem_checks = 0;
1550 if (strstr(str, "strict"))
1551 strict_iomem_checks = 1;
1552 return 1;
1553 }
1554
1555 __setup("iomem=", strict_iomem);