OSDN Git Service

Merge android-4.4.155 (b3f777e) into msm-4.4
[sagit-ice-cold/kernel_xiaomi_msm8998.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 (alloc.start <= alloc.end &&
615                             resource_contains(&avail, &alloc)) {
616                                 new->start = alloc.start;
617                                 new->end = alloc.end;
618                                 return 0;
619                         }
620                 }
621
622 next:           if (!this || this->end == root->end)
623                         break;
624
625                 if (this != old)
626                         tmp.start = this->end + 1;
627                 this = this->sibling;
628         }
629         return -EBUSY;
630 }
631
632 /*
633  * Find empty slot in the resource tree given range and alignment.
634  */
635 static int find_resource(struct resource *root, struct resource *new,
636                         resource_size_t size,
637                         struct resource_constraint  *constraint)
638 {
639         return  __find_resource(root, NULL, new, size, constraint);
640 }
641
642 /**
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.
646  *
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.
651  */
652 static int reallocate_resource(struct resource *root, struct resource *old,
653                         resource_size_t newsize,
654                         struct resource_constraint  *constraint)
655 {
656         int err=0;
657         struct resource new = *old;
658         struct resource *conflict;
659
660         write_lock(&resource_lock);
661
662         if ((err = __find_resource(root, old, &new, newsize, constraint)))
663                 goto out;
664
665         if (resource_contains(&new, old)) {
666                 old->start = new.start;
667                 old->end = new.end;
668                 goto out;
669         }
670
671         if (old->child) {
672                 err = -EBUSY;
673                 goto out;
674         }
675
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);
684         }
685 out:
686         write_unlock(&resource_lock);
687         return err;
688 }
689
690
691 /**
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
702  */
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)
711 {
712         int err;
713         struct resource_constraint constraint;
714
715         if (!alignf)
716                 alignf = simple_align_resource;
717
718         constraint.min = min;
719         constraint.max = max;
720         constraint.align = align;
721         constraint.alignf = alignf;
722         constraint.alignf_data = alignf_data;
723
724         if ( new->parent ) {
725                 /* resource is already allocated, try reallocating with
726                    the new constraints */
727                 return reallocate_resource(root, new, size, &constraint);
728         }
729
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;
736 }
737
738 EXPORT_SYMBOL(allocate_resource);
739
740 /**
741  * lookup_resource - find an existing resource by a resource start address
742  * @root: root resource descriptor
743  * @start: resource start address
744  *
745  * Returns a pointer to the resource if found, NULL otherwise
746  */
747 struct resource *lookup_resource(struct resource *root, resource_size_t start)
748 {
749         struct resource *res;
750
751         read_lock(&resource_lock);
752         for (res = root->child; res; res = res->sibling) {
753                 if (res->start == start)
754                         break;
755         }
756         read_unlock(&resource_lock);
757
758         return res;
759 }
760
761 /*
762  * Insert a resource into the resource tree. If successful, return NULL,
763  * otherwise return the conflicting resource (compare to __request_resource())
764  */
765 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
766 {
767         struct resource *first, *next;
768
769         for (;; parent = first) {
770                 first = __request_resource(parent, new);
771                 if (!first)
772                         return first;
773
774                 if (first == parent)
775                         return first;
776                 if (WARN_ON(first == new))      /* duplicated insertion */
777                         return first;
778
779                 if ((first->start > new->start) || (first->end < new->end))
780                         break;
781                 if ((first->start == new->start) && (first->end == new->end))
782                         break;
783         }
784
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;
793         }
794
795         new->parent = parent;
796         new->sibling = next->sibling;
797         new->child = first;
798
799         next->sibling = NULL;
800         for (next = first; next; next = next->sibling)
801                 next->parent = new;
802
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;
810         }
811         return NULL;
812 }
813
814 /**
815  * insert_resource_conflict - Inserts resource in the resource tree
816  * @parent: parent of the new resource
817  * @new: new resource to insert
818  *
819  * Returns 0 on success, conflict resource if the resource can't be inserted.
820  *
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.
826  */
827 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
828 {
829         struct resource *conflict;
830
831         write_lock(&resource_lock);
832         conflict = __insert_resource(parent, new);
833         write_unlock(&resource_lock);
834         return conflict;
835 }
836
837 /**
838  * insert_resource - Inserts a resource in the resource tree
839  * @parent: parent of the new resource
840  * @new: new resource to insert
841  *
842  * Returns 0 on success, -EBUSY if the resource can't be inserted.
843  */
844 int insert_resource(struct resource *parent, struct resource *new)
845 {
846         struct resource *conflict;
847
848         conflict = insert_resource_conflict(parent, new);
849         return conflict ? -EBUSY : 0;
850 }
851
852 /**
853  * insert_resource_expand_to_fit - Insert a resource into the resource tree
854  * @root: root resource descriptor
855  * @new: new resource to insert
856  *
857  * Insert a resource into the resource tree, possibly expanding it in order
858  * to make it encompass any conflicting resources.
859  */
860 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
861 {
862         if (new->parent)
863                 return;
864
865         write_lock(&resource_lock);
866         for (;;) {
867                 struct resource *conflict;
868
869                 conflict = __insert_resource(root, new);
870                 if (!conflict)
871                         break;
872                 if (conflict == root)
873                         break;
874
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;
880
881                 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
882         }
883         write_unlock(&resource_lock);
884 }
885
886 static int __adjust_resource(struct resource *res, resource_size_t start,
887                                 resource_size_t size)
888 {
889         struct resource *tmp, *parent = res->parent;
890         resource_size_t end = start + size - 1;
891         int result = -EBUSY;
892
893         if (!parent)
894                 goto skip;
895
896         if ((start < parent->start) || (end > parent->end))
897                 goto out;
898
899         if (res->sibling && (res->sibling->start <= end))
900                 goto out;
901
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;
908         }
909
910 skip:
911         for (tmp = res->child; tmp; tmp = tmp->sibling)
912                 if ((tmp->start < start) || (tmp->end > end))
913                         goto out;
914
915         res->start = start;
916         res->end = end;
917         result = 0;
918
919  out:
920         return result;
921 }
922
923 /**
924  * adjust_resource - modify a resource's start and size
925  * @res: resource to modify
926  * @start: new start value
927  * @size: new size
928  *
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.
932  */
933 int adjust_resource(struct resource *res, resource_size_t start,
934                         resource_size_t size)
935 {
936         int result;
937
938         write_lock(&resource_lock);
939         result = __adjust_resource(res, start, size);
940         write_unlock(&resource_lock);
941         return result;
942 }
943 EXPORT_SYMBOL(adjust_resource);
944
945 static void __init __reserve_region_with_split(struct resource *root,
946                 resource_size_t start, resource_size_t end,
947                 const char *name)
948 {
949         struct resource *parent = root;
950         struct resource *conflict;
951         struct resource *res = alloc_resource(GFP_ATOMIC);
952         struct resource *next_res = NULL;
953
954         if (!res)
955                 return;
956
957         res->name = name;
958         res->start = start;
959         res->end = end;
960         res->flags = IORESOURCE_BUSY;
961
962         while (1) {
963
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;
971                 }
972
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;
979                 }
980
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;
990                                 }
991                                 next_res->name = name;
992                                 next_res->start = conflict->end + 1;
993                                 next_res->end = end;
994                                 next_res->flags = IORESOURCE_BUSY;
995                         }
996                 } else {
997                         res->start = conflict->end + 1;
998                 }
999         }
1000
1001 }
1002
1003 void __init reserve_region_with_split(struct resource *root,
1004                 resource_size_t start, resource_size_t end,
1005                 const char *name)
1006 {
1007         int abort = 0;
1008
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);
1024                 }
1025                 dump_stack();
1026         }
1027         if (!abort)
1028                 __reserve_region_with_split(root, start, end, name);
1029         write_unlock(&resource_lock);
1030 }
1031
1032 /**
1033  * resource_alignment - calculate resource's alignment
1034  * @res: resource pointer
1035  *
1036  * Returns alignment on success, 0 (invalid alignment) on failure.
1037  */
1038 resource_size_t resource_alignment(struct resource *res)
1039 {
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;
1047         }
1048 }
1049
1050 /*
1051  * This is compatibility stuff for IO resources.
1052  *
1053  * Note how this, unlike the above, knows about
1054  * the IO flag meanings (busy etc).
1055  *
1056  * request_region creates a new busy region.
1057  *
1058  * release_region releases a matching busy region.
1059  */
1060
1061 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1062
1063 /**
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
1070  */
1071 struct resource * __request_region(struct resource *parent,
1072                                    resource_size_t start, resource_size_t n,
1073                                    const char *name, int flags)
1074 {
1075         DECLARE_WAITQUEUE(wait, current);
1076         struct resource *res = alloc_resource(GFP_KERNEL);
1077
1078         if (!res)
1079                 return NULL;
1080
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;
1086
1087         write_lock(&resource_lock);
1088
1089         for (;;) {
1090                 struct resource *conflict;
1091
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;
1099                         }
1100                 }
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;
1109                 }
1110                 /* Uhhuh, that didn't work out.. */
1111                 free_resource(res);
1112                 res = NULL;
1113                 break;
1114         }
1115         write_unlock(&resource_lock);
1116         return res;
1117 }
1118 EXPORT_SYMBOL(__request_region);
1119
1120 /**
1121  * __release_region - release a previously reserved resource region
1122  * @parent: parent resource descriptor
1123  * @start: resource start address
1124  * @n: resource region size
1125  *
1126  * The described resource region must match a currently busy region.
1127  */
1128 void __release_region(struct resource *parent, resource_size_t start,
1129                         resource_size_t n)
1130 {
1131         struct resource **p;
1132         resource_size_t end;
1133
1134         p = &parent->child;
1135         end = start + n - 1;
1136
1137         write_lock(&resource_lock);
1138
1139         for (;;) {
1140                 struct resource *res = *p;
1141
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;
1148                         }
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;
1157                 }
1158                 p = &res->sibling;
1159         }
1160
1161         write_unlock(&resource_lock);
1162
1163         printk(KERN_WARNING "Trying to free nonexistent resource "
1164                 "<%016llx-%016llx>\n", (unsigned long long)start,
1165                 (unsigned long long)end);
1166 }
1167 EXPORT_SYMBOL(__release_region);
1168
1169 #ifdef CONFIG_MEMORY_HOTREMOVE
1170 /**
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
1175  *
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.
1182  *
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.
1189  */
1190 int release_mem_region_adjustable(struct resource *parent,
1191                         resource_size_t start, resource_size_t size)
1192 {
1193         struct resource **p;
1194         struct resource *res;
1195         struct resource *new_res;
1196         resource_size_t end;
1197         int ret = -EINVAL;
1198
1199         end = start + size - 1;
1200         if ((start < parent->start) || (end > parent->end))
1201                 return ret;
1202
1203         /* The alloc_resource() result gets checked later */
1204         new_res = alloc_resource(GFP_KERNEL);
1205
1206         p = &parent->child;
1207         write_lock(&resource_lock);
1208
1209         while ((res = *p)) {
1210                 if (res->start >= end)
1211                         break;
1212
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;
1217                 }
1218
1219                 if (!(res->flags & IORESOURCE_MEM))
1220                         break;
1221
1222                 if (!(res->flags & IORESOURCE_BUSY)) {
1223                         p = &res->child;
1224                         continue;
1225                 }
1226
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;
1246                         }
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;
1254
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;
1261                 }
1262
1263                 break;
1264         }
1265
1266         write_unlock(&resource_lock);
1267         free_resource(new_res);
1268         return ret;
1269 }
1270 #endif  /* CONFIG_MEMORY_HOTREMOVE */
1271
1272 /*
1273  * Managed region resource
1274  */
1275 static void devm_resource_release(struct device *dev, void *ptr)
1276 {
1277         struct resource **r = ptr;
1278
1279         release_resource(*r);
1280 }
1281
1282 /**
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
1287  *
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().
1294  *
1295  * When a conflict is detected between any existing resources and the newly
1296  * requested resource, an error message will be printed.
1297  *
1298  * Returns 0 on success or a negative error code on failure.
1299  */
1300 int devm_request_resource(struct device *dev, struct resource *root,
1301                           struct resource *new)
1302 {
1303         struct resource *conflict, **ptr;
1304
1305         ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1306         if (!ptr)
1307                 return -ENOMEM;
1308
1309         *ptr = new;
1310
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;
1317         }
1318
1319         devres_add(dev, ptr);
1320         return 0;
1321 }
1322 EXPORT_SYMBOL(devm_request_resource);
1323
1324 static int devm_resource_match(struct device *dev, void *res, void *data)
1325 {
1326         struct resource **ptr = res;
1327
1328         return *ptr == data;
1329 }
1330
1331 /**
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
1335  *
1336  * Releases a resource previously requested using devm_request_resource().
1337  */
1338 void devm_release_resource(struct device *dev, struct resource *new)
1339 {
1340         WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1341                                new));
1342 }
1343 EXPORT_SYMBOL(devm_release_resource);
1344
1345 struct region_devres {
1346         struct resource *parent;
1347         resource_size_t start;
1348         resource_size_t n;
1349 };
1350
1351 static void devm_region_release(struct device *dev, void *res)
1352 {
1353         struct region_devres *this = res;
1354
1355         __release_region(this->parent, this->start, this->n);
1356 }
1357
1358 static int devm_region_match(struct device *dev, void *res, void *match_data)
1359 {
1360         struct region_devres *this = res, *match = match_data;
1361
1362         return this->parent == match->parent &&
1363                 this->start == match->start && this->n == match->n;
1364 }
1365
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)
1369 {
1370         struct region_devres *dr = NULL;
1371         struct resource *res;
1372
1373         dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1374                           GFP_KERNEL);
1375         if (!dr)
1376                 return NULL;
1377
1378         dr->parent = parent;
1379         dr->start = start;
1380         dr->n = n;
1381
1382         res = __request_region(parent, start, n, name, 0);
1383         if (res)
1384                 devres_add(dev, dr);
1385         else
1386                 devres_free(dr);
1387
1388         return res;
1389 }
1390 EXPORT_SYMBOL(__devm_request_region);
1391
1392 void __devm_release_region(struct device *dev, struct resource *parent,
1393                            resource_size_t start, resource_size_t n)
1394 {
1395         struct region_devres match_data = { parent, start, n };
1396
1397         __release_region(parent, start, n);
1398         WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1399                                &match_data));
1400 }
1401 EXPORT_SYMBOL(__devm_release_region);
1402
1403 /*
1404  * Called from init/main.c to reserve IO ports.
1405  */
1406 #define MAXRESERVE 4
1407 static int __init reserve_setup(char *str)
1408 {
1409         static int reserved;
1410         static struct resource reserve[MAXRESERVE];
1411
1412         for (;;) {
1413                 unsigned int io_start, io_num;
1414                 int x = reserved;
1415
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;
1429                 }
1430         }
1431         return 1;
1432 }
1433
1434 __setup("reserve=", reserve_setup);
1435
1436 /*
1437  * Check if the requested addr and size spans more than any slot in the
1438  * iomem resource tree.
1439  */
1440 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1441 {
1442         struct resource *p = &iomem_resource;
1443         int err = 0;
1444         loff_t l;
1445
1446         read_lock(&resource_lock);
1447         for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1448                 /*
1449                  * We can probably skip the resources without
1450                  * IORESOURCE_IO attribute?
1451                  */
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;
1459                 /*
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)
1464                  */
1465                 if (p->flags & IORESOURCE_BUSY)
1466                         continue;
1467
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;
1474         }
1475         read_unlock(&resource_lock);
1476
1477         return err;
1478 }
1479
1480 #ifdef CONFIG_STRICT_DEVMEM
1481 static int strict_iomem_checks = 1;
1482 #else
1483 static int strict_iomem_checks;
1484 #endif
1485
1486 /*
1487  * check if an address is reserved in the iomem resource tree
1488  * returns 1 if reserved, 0 if not reserved.
1489  */
1490 int iomem_is_exclusive(u64 addr)
1491 {
1492         struct resource *p = &iomem_resource;
1493         int err = 0;
1494         loff_t l;
1495         int size = PAGE_SIZE;
1496
1497         if (!strict_iomem_checks)
1498                 return 0;
1499
1500         addr = addr & PAGE_MASK;
1501
1502         read_lock(&resource_lock);
1503         for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1504                 /*
1505                  * We can probably skip the resources without
1506                  * IORESOURCE_IO attribute?
1507                  */
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;
1516                 }
1517         }
1518         read_unlock(&resource_lock);
1519
1520         return err;
1521 }
1522
1523 struct resource_entry *resource_list_create_entry(struct resource *res,
1524                                                   size_t extra_size)
1525 {
1526         struct resource_entry *entry;
1527
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;
1532         }
1533
1534         return entry;
1535 }
1536 EXPORT_SYMBOL(resource_list_create_entry);
1537
1538 void resource_list_free(struct list_head *head)
1539 {
1540         struct resource_entry *entry, *tmp;
1541
1542         list_for_each_entry_safe(entry, tmp, head, node)
1543                 resource_list_destroy_entry(entry);
1544 }
1545 EXPORT_SYMBOL(resource_list_free);
1546
1547 static int __init strict_iomem(char *str)
1548 {
1549         if (strstr(str, "relaxed"))
1550                 strict_iomem_checks = 0;
1551         if (strstr(str, "strict"))
1552                 strict_iomem_checks = 1;
1553         return 1;
1554 }
1555
1556 __setup("iomem=", strict_iomem);