resources: factor out resource_clip() to simplify find_resource()
[pandora-kernel.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 #include <linux/module.h>
11 #include <linux/errno.h>
12 #include <linux/ioport.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/fs.h>
17 #include <linux/proc_fs.h>
18 #include <linux/sched.h>
19 #include <linux/seq_file.h>
20 #include <linux/device.h>
21 #include <linux/pfn.h>
22 #include <asm/io.h>
23
24
25 struct resource ioport_resource = {
26         .name   = "PCI IO",
27         .start  = 0,
28         .end    = IO_SPACE_LIMIT,
29         .flags  = IORESOURCE_IO,
30 };
31 EXPORT_SYMBOL(ioport_resource);
32
33 struct resource iomem_resource = {
34         .name   = "PCI mem",
35         .start  = 0,
36         .end    = -1,
37         .flags  = IORESOURCE_MEM,
38 };
39 EXPORT_SYMBOL(iomem_resource);
40
41 static DEFINE_RWLOCK(resource_lock);
42
43 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
44 {
45         struct resource *p = v;
46         (*pos)++;
47         if (p->child)
48                 return p->child;
49         while (!p->sibling && p->parent)
50                 p = p->parent;
51         return p->sibling;
52 }
53
54 #ifdef CONFIG_PROC_FS
55
56 enum { MAX_IORES_LEVEL = 5 };
57
58 static void *r_start(struct seq_file *m, loff_t *pos)
59         __acquires(resource_lock)
60 {
61         struct resource *p = m->private;
62         loff_t l = 0;
63         read_lock(&resource_lock);
64         for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
65                 ;
66         return p;
67 }
68
69 static void r_stop(struct seq_file *m, void *v)
70         __releases(resource_lock)
71 {
72         read_unlock(&resource_lock);
73 }
74
75 static int r_show(struct seq_file *m, void *v)
76 {
77         struct resource *root = m->private;
78         struct resource *r = v, *p;
79         int width = root->end < 0x10000 ? 4 : 8;
80         int depth;
81
82         for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
83                 if (p->parent == root)
84                         break;
85         seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
86                         depth * 2, "",
87                         width, (unsigned long long) r->start,
88                         width, (unsigned long long) r->end,
89                         r->name ? r->name : "<BAD>");
90         return 0;
91 }
92
93 static const struct seq_operations resource_op = {
94         .start  = r_start,
95         .next   = r_next,
96         .stop   = r_stop,
97         .show   = r_show,
98 };
99
100 static int ioports_open(struct inode *inode, struct file *file)
101 {
102         int res = seq_open(file, &resource_op);
103         if (!res) {
104                 struct seq_file *m = file->private_data;
105                 m->private = &ioport_resource;
106         }
107         return res;
108 }
109
110 static int iomem_open(struct inode *inode, struct file *file)
111 {
112         int res = seq_open(file, &resource_op);
113         if (!res) {
114                 struct seq_file *m = file->private_data;
115                 m->private = &iomem_resource;
116         }
117         return res;
118 }
119
120 static const struct file_operations proc_ioports_operations = {
121         .open           = ioports_open,
122         .read           = seq_read,
123         .llseek         = seq_lseek,
124         .release        = seq_release,
125 };
126
127 static const struct file_operations proc_iomem_operations = {
128         .open           = iomem_open,
129         .read           = seq_read,
130         .llseek         = seq_lseek,
131         .release        = seq_release,
132 };
133
134 static int __init ioresources_init(void)
135 {
136         proc_create("ioports", 0, NULL, &proc_ioports_operations);
137         proc_create("iomem", 0, NULL, &proc_iomem_operations);
138         return 0;
139 }
140 __initcall(ioresources_init);
141
142 #endif /* CONFIG_PROC_FS */
143
144 /* Return the conflict entry if you can't request it */
145 static struct resource * __request_resource(struct resource *root, struct resource *new)
146 {
147         resource_size_t start = new->start;
148         resource_size_t end = new->end;
149         struct resource *tmp, **p;
150
151         if (end < start)
152                 return root;
153         if (start < root->start)
154                 return root;
155         if (end > root->end)
156                 return root;
157         p = &root->child;
158         for (;;) {
159                 tmp = *p;
160                 if (!tmp || tmp->start > end) {
161                         new->sibling = tmp;
162                         *p = new;
163                         new->parent = root;
164                         return NULL;
165                 }
166                 p = &tmp->sibling;
167                 if (tmp->end < start)
168                         continue;
169                 return tmp;
170         }
171 }
172
173 static int __release_resource(struct resource *old)
174 {
175         struct resource *tmp, **p;
176
177         p = &old->parent->child;
178         for (;;) {
179                 tmp = *p;
180                 if (!tmp)
181                         break;
182                 if (tmp == old) {
183                         *p = tmp->sibling;
184                         old->parent = NULL;
185                         return 0;
186                 }
187                 p = &tmp->sibling;
188         }
189         return -EINVAL;
190 }
191
192 static void __release_child_resources(struct resource *r)
193 {
194         struct resource *tmp, *p;
195         resource_size_t size;
196
197         p = r->child;
198         r->child = NULL;
199         while (p) {
200                 tmp = p;
201                 p = p->sibling;
202
203                 tmp->parent = NULL;
204                 tmp->sibling = NULL;
205                 __release_child_resources(tmp);
206
207                 printk(KERN_DEBUG "release child resource %pR\n", tmp);
208                 /* need to restore size, and keep flags */
209                 size = resource_size(tmp);
210                 tmp->start = 0;
211                 tmp->end = size - 1;
212         }
213 }
214
215 void release_child_resources(struct resource *r)
216 {
217         write_lock(&resource_lock);
218         __release_child_resources(r);
219         write_unlock(&resource_lock);
220 }
221
222 /**
223  * request_resource_conflict - request and reserve an I/O or memory resource
224  * @root: root resource descriptor
225  * @new: resource descriptor desired by caller
226  *
227  * Returns 0 for success, conflict resource on error.
228  */
229 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
230 {
231         struct resource *conflict;
232
233         write_lock(&resource_lock);
234         conflict = __request_resource(root, new);
235         write_unlock(&resource_lock);
236         return conflict;
237 }
238
239 /**
240  * request_resource - request and reserve an I/O or memory resource
241  * @root: root resource descriptor
242  * @new: resource descriptor desired by caller
243  *
244  * Returns 0 for success, negative error code on error.
245  */
246 int request_resource(struct resource *root, struct resource *new)
247 {
248         struct resource *conflict;
249
250         conflict = request_resource_conflict(root, new);
251         return conflict ? -EBUSY : 0;
252 }
253
254 EXPORT_SYMBOL(request_resource);
255
256 /**
257  * release_resource - release a previously reserved resource
258  * @old: resource pointer
259  */
260 int release_resource(struct resource *old)
261 {
262         int retval;
263
264         write_lock(&resource_lock);
265         retval = __release_resource(old);
266         write_unlock(&resource_lock);
267         return retval;
268 }
269
270 EXPORT_SYMBOL(release_resource);
271
272 #if !defined(CONFIG_ARCH_HAS_WALK_MEMORY)
273 /*
274  * Finds the lowest memory reosurce exists within [res->start.res->end)
275  * the caller must specify res->start, res->end, res->flags and "name".
276  * If found, returns 0, res is overwritten, if not found, returns -1.
277  */
278 static int find_next_system_ram(struct resource *res, char *name)
279 {
280         resource_size_t start, end;
281         struct resource *p;
282
283         BUG_ON(!res);
284
285         start = res->start;
286         end = res->end;
287         BUG_ON(start >= end);
288
289         read_lock(&resource_lock);
290         for (p = iomem_resource.child; p ; p = p->sibling) {
291                 /* system ram is just marked as IORESOURCE_MEM */
292                 if (p->flags != res->flags)
293                         continue;
294                 if (name && strcmp(p->name, name))
295                         continue;
296                 if (p->start > end) {
297                         p = NULL;
298                         break;
299                 }
300                 if ((p->end >= start) && (p->start < end))
301                         break;
302         }
303         read_unlock(&resource_lock);
304         if (!p)
305                 return -1;
306         /* copy data */
307         if (res->start < p->start)
308                 res->start = p->start;
309         if (res->end > p->end)
310                 res->end = p->end;
311         return 0;
312 }
313
314 /*
315  * This function calls callback against all memory range of "System RAM"
316  * which are marked as IORESOURCE_MEM and IORESOUCE_BUSY.
317  * Now, this function is only for "System RAM".
318  */
319 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
320                 void *arg, int (*func)(unsigned long, unsigned long, void *))
321 {
322         struct resource res;
323         unsigned long pfn, end_pfn;
324         u64 orig_end;
325         int ret = -1;
326
327         res.start = (u64) start_pfn << PAGE_SHIFT;
328         res.end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
329         res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
330         orig_end = res.end;
331         while ((res.start < res.end) &&
332                 (find_next_system_ram(&res, "System RAM") >= 0)) {
333                 pfn = (res.start + PAGE_SIZE - 1) >> PAGE_SHIFT;
334                 end_pfn = (res.end + 1) >> PAGE_SHIFT;
335                 if (end_pfn > pfn)
336                         ret = (*func)(pfn, end_pfn - pfn, arg);
337                 if (ret)
338                         break;
339                 res.start = res.end + 1;
340                 res.end = orig_end;
341         }
342         return ret;
343 }
344
345 #endif
346
347 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
348 {
349         return 1;
350 }
351 /*
352  * This generic page_is_ram() returns true if specified address is
353  * registered as "System RAM" in iomem_resource list.
354  */
355 int __weak page_is_ram(unsigned long pfn)
356 {
357         return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
358 }
359
360 static resource_size_t simple_align_resource(void *data,
361                                              const struct resource *avail,
362                                              resource_size_t size,
363                                              resource_size_t align)
364 {
365         return avail->start;
366 }
367
368 static void resource_clip(struct resource *res, resource_size_t min,
369                           resource_size_t max)
370 {
371         if (res->start < min)
372                 res->start = min;
373         if (res->end > max)
374                 res->end = max;
375 }
376
377 /*
378  * Find empty slot in the resource tree given range and alignment.
379  */
380 static int find_resource(struct resource *root, struct resource *new,
381                          resource_size_t size, resource_size_t min,
382                          resource_size_t max, resource_size_t align,
383                          resource_size_t (*alignf)(void *,
384                                                    const struct resource *,
385                                                    resource_size_t,
386                                                    resource_size_t),
387                          void *alignf_data)
388 {
389         struct resource *this = root->child;
390         struct resource tmp = *new;
391
392         tmp.start = root->start;
393         /*
394          * Skip past an allocated resource that starts at 0, since the assignment
395          * of this->start - 1 to tmp->end below would cause an underflow.
396          */
397         if (this && this->start == 0) {
398                 tmp.start = this->end + 1;
399                 this = this->sibling;
400         }
401         for(;;) {
402                 if (this)
403                         tmp.end = this->start - 1;
404                 else
405                         tmp.end = root->end;
406
407                 resource_clip(&tmp, min, max);
408                 tmp.start = ALIGN(tmp.start, align);
409
410                 tmp.start = alignf(alignf_data, &tmp, size, align);
411                 if (tmp.start < tmp.end && tmp.end - tmp.start >= size - 1) {
412                         new->start = tmp.start;
413                         new->end = tmp.start + size - 1;
414                         return 0;
415                 }
416                 if (!this)
417                         break;
418                 tmp.start = this->end + 1;
419                 this = this->sibling;
420         }
421         return -EBUSY;
422 }
423
424 /**
425  * allocate_resource - allocate empty slot in the resource tree given range & alignment
426  * @root: root resource descriptor
427  * @new: resource descriptor desired by caller
428  * @size: requested resource region size
429  * @min: minimum size to allocate
430  * @max: maximum size to allocate
431  * @align: alignment requested, in bytes
432  * @alignf: alignment function, optional, called if not NULL
433  * @alignf_data: arbitrary data to pass to the @alignf function
434  */
435 int allocate_resource(struct resource *root, struct resource *new,
436                       resource_size_t size, resource_size_t min,
437                       resource_size_t max, resource_size_t align,
438                       resource_size_t (*alignf)(void *,
439                                                 const struct resource *,
440                                                 resource_size_t,
441                                                 resource_size_t),
442                       void *alignf_data)
443 {
444         int err;
445
446         if (!alignf)
447                 alignf = simple_align_resource;
448
449         write_lock(&resource_lock);
450         err = find_resource(root, new, size, min, max, align, alignf, alignf_data);
451         if (err >= 0 && __request_resource(root, new))
452                 err = -EBUSY;
453         write_unlock(&resource_lock);
454         return err;
455 }
456
457 EXPORT_SYMBOL(allocate_resource);
458
459 /*
460  * Insert a resource into the resource tree. If successful, return NULL,
461  * otherwise return the conflicting resource (compare to __request_resource())
462  */
463 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
464 {
465         struct resource *first, *next;
466
467         for (;; parent = first) {
468                 first = __request_resource(parent, new);
469                 if (!first)
470                         return first;
471
472                 if (first == parent)
473                         return first;
474
475                 if ((first->start > new->start) || (first->end < new->end))
476                         break;
477                 if ((first->start == new->start) && (first->end == new->end))
478                         break;
479         }
480
481         for (next = first; ; next = next->sibling) {
482                 /* Partial overlap? Bad, and unfixable */
483                 if (next->start < new->start || next->end > new->end)
484                         return next;
485                 if (!next->sibling)
486                         break;
487                 if (next->sibling->start > new->end)
488                         break;
489         }
490
491         new->parent = parent;
492         new->sibling = next->sibling;
493         new->child = first;
494
495         next->sibling = NULL;
496         for (next = first; next; next = next->sibling)
497                 next->parent = new;
498
499         if (parent->child == first) {
500                 parent->child = new;
501         } else {
502                 next = parent->child;
503                 while (next->sibling != first)
504                         next = next->sibling;
505                 next->sibling = new;
506         }
507         return NULL;
508 }
509
510 /**
511  * insert_resource_conflict - Inserts resource in the resource tree
512  * @parent: parent of the new resource
513  * @new: new resource to insert
514  *
515  * Returns 0 on success, conflict resource if the resource can't be inserted.
516  *
517  * This function is equivalent to request_resource_conflict when no conflict
518  * happens. If a conflict happens, and the conflicting resources
519  * entirely fit within the range of the new resource, then the new
520  * resource is inserted and the conflicting resources become children of
521  * the new resource.
522  */
523 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
524 {
525         struct resource *conflict;
526
527         write_lock(&resource_lock);
528         conflict = __insert_resource(parent, new);
529         write_unlock(&resource_lock);
530         return conflict;
531 }
532
533 /**
534  * insert_resource - Inserts a resource in the resource tree
535  * @parent: parent of the new resource
536  * @new: new resource to insert
537  *
538  * Returns 0 on success, -EBUSY if the resource can't be inserted.
539  */
540 int insert_resource(struct resource *parent, struct resource *new)
541 {
542         struct resource *conflict;
543
544         conflict = insert_resource_conflict(parent, new);
545         return conflict ? -EBUSY : 0;
546 }
547
548 /**
549  * insert_resource_expand_to_fit - Insert a resource into the resource tree
550  * @root: root resource descriptor
551  * @new: new resource to insert
552  *
553  * Insert a resource into the resource tree, possibly expanding it in order
554  * to make it encompass any conflicting resources.
555  */
556 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
557 {
558         if (new->parent)
559                 return;
560
561         write_lock(&resource_lock);
562         for (;;) {
563                 struct resource *conflict;
564
565                 conflict = __insert_resource(root, new);
566                 if (!conflict)
567                         break;
568                 if (conflict == root)
569                         break;
570
571                 /* Ok, expand resource to cover the conflict, then try again .. */
572                 if (conflict->start < new->start)
573                         new->start = conflict->start;
574                 if (conflict->end > new->end)
575                         new->end = conflict->end;
576
577                 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
578         }
579         write_unlock(&resource_lock);
580 }
581
582 /**
583  * adjust_resource - modify a resource's start and size
584  * @res: resource to modify
585  * @start: new start value
586  * @size: new size
587  *
588  * Given an existing resource, change its start and size to match the
589  * arguments.  Returns 0 on success, -EBUSY if it can't fit.
590  * Existing children of the resource are assumed to be immutable.
591  */
592 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size)
593 {
594         struct resource *tmp, *parent = res->parent;
595         resource_size_t end = start + size - 1;
596         int result = -EBUSY;
597
598         write_lock(&resource_lock);
599
600         if ((start < parent->start) || (end > parent->end))
601                 goto out;
602
603         for (tmp = res->child; tmp; tmp = tmp->sibling) {
604                 if ((tmp->start < start) || (tmp->end > end))
605                         goto out;
606         }
607
608         if (res->sibling && (res->sibling->start <= end))
609                 goto out;
610
611         tmp = parent->child;
612         if (tmp != res) {
613                 while (tmp->sibling != res)
614                         tmp = tmp->sibling;
615                 if (start <= tmp->end)
616                         goto out;
617         }
618
619         res->start = start;
620         res->end = end;
621         result = 0;
622
623  out:
624         write_unlock(&resource_lock);
625         return result;
626 }
627
628 static void __init __reserve_region_with_split(struct resource *root,
629                 resource_size_t start, resource_size_t end,
630                 const char *name)
631 {
632         struct resource *parent = root;
633         struct resource *conflict;
634         struct resource *res = kzalloc(sizeof(*res), GFP_ATOMIC);
635
636         if (!res)
637                 return;
638
639         res->name = name;
640         res->start = start;
641         res->end = end;
642         res->flags = IORESOURCE_BUSY;
643
644         conflict = __request_resource(parent, res);
645         if (!conflict)
646                 return;
647
648         /* failed, split and try again */
649         kfree(res);
650
651         /* conflict covered whole area */
652         if (conflict->start <= start && conflict->end >= end)
653                 return;
654
655         if (conflict->start > start)
656                 __reserve_region_with_split(root, start, conflict->start-1, name);
657         if (conflict->end < end)
658                 __reserve_region_with_split(root, conflict->end+1, end, name);
659 }
660
661 void __init reserve_region_with_split(struct resource *root,
662                 resource_size_t start, resource_size_t end,
663                 const char *name)
664 {
665         write_lock(&resource_lock);
666         __reserve_region_with_split(root, start, end, name);
667         write_unlock(&resource_lock);
668 }
669
670 EXPORT_SYMBOL(adjust_resource);
671
672 /**
673  * resource_alignment - calculate resource's alignment
674  * @res: resource pointer
675  *
676  * Returns alignment on success, 0 (invalid alignment) on failure.
677  */
678 resource_size_t resource_alignment(struct resource *res)
679 {
680         switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
681         case IORESOURCE_SIZEALIGN:
682                 return resource_size(res);
683         case IORESOURCE_STARTALIGN:
684                 return res->start;
685         default:
686                 return 0;
687         }
688 }
689
690 /*
691  * This is compatibility stuff for IO resources.
692  *
693  * Note how this, unlike the above, knows about
694  * the IO flag meanings (busy etc).
695  *
696  * request_region creates a new busy region.
697  *
698  * check_region returns non-zero if the area is already busy.
699  *
700  * release_region releases a matching busy region.
701  */
702
703 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
704
705 /**
706  * __request_region - create a new busy resource region
707  * @parent: parent resource descriptor
708  * @start: resource start address
709  * @n: resource region size
710  * @name: reserving caller's ID string
711  * @flags: IO resource flags
712  */
713 struct resource * __request_region(struct resource *parent,
714                                    resource_size_t start, resource_size_t n,
715                                    const char *name, int flags)
716 {
717         DECLARE_WAITQUEUE(wait, current);
718         struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL);
719
720         if (!res)
721                 return NULL;
722
723         res->name = name;
724         res->start = start;
725         res->end = start + n - 1;
726         res->flags = IORESOURCE_BUSY;
727         res->flags |= flags;
728
729         write_lock(&resource_lock);
730
731         for (;;) {
732                 struct resource *conflict;
733
734                 conflict = __request_resource(parent, res);
735                 if (!conflict)
736                         break;
737                 if (conflict != parent) {
738                         parent = conflict;
739                         if (!(conflict->flags & IORESOURCE_BUSY))
740                                 continue;
741                 }
742                 if (conflict->flags & flags & IORESOURCE_MUXED) {
743                         add_wait_queue(&muxed_resource_wait, &wait);
744                         write_unlock(&resource_lock);
745                         set_current_state(TASK_UNINTERRUPTIBLE);
746                         schedule();
747                         remove_wait_queue(&muxed_resource_wait, &wait);
748                         write_lock(&resource_lock);
749                         continue;
750                 }
751                 /* Uhhuh, that didn't work out.. */
752                 kfree(res);
753                 res = NULL;
754                 break;
755         }
756         write_unlock(&resource_lock);
757         return res;
758 }
759 EXPORT_SYMBOL(__request_region);
760
761 /**
762  * __check_region - check if a resource region is busy or free
763  * @parent: parent resource descriptor
764  * @start: resource start address
765  * @n: resource region size
766  *
767  * Returns 0 if the region is free at the moment it is checked,
768  * returns %-EBUSY if the region is busy.
769  *
770  * NOTE:
771  * This function is deprecated because its use is racy.
772  * Even if it returns 0, a subsequent call to request_region()
773  * may fail because another driver etc. just allocated the region.
774  * Do NOT use it.  It will be removed from the kernel.
775  */
776 int __check_region(struct resource *parent, resource_size_t start,
777                         resource_size_t n)
778 {
779         struct resource * res;
780
781         res = __request_region(parent, start, n, "check-region", 0);
782         if (!res)
783                 return -EBUSY;
784
785         release_resource(res);
786         kfree(res);
787         return 0;
788 }
789 EXPORT_SYMBOL(__check_region);
790
791 /**
792  * __release_region - release a previously reserved resource region
793  * @parent: parent resource descriptor
794  * @start: resource start address
795  * @n: resource region size
796  *
797  * The described resource region must match a currently busy region.
798  */
799 void __release_region(struct resource *parent, resource_size_t start,
800                         resource_size_t n)
801 {
802         struct resource **p;
803         resource_size_t end;
804
805         p = &parent->child;
806         end = start + n - 1;
807
808         write_lock(&resource_lock);
809
810         for (;;) {
811                 struct resource *res = *p;
812
813                 if (!res)
814                         break;
815                 if (res->start <= start && res->end >= end) {
816                         if (!(res->flags & IORESOURCE_BUSY)) {
817                                 p = &res->child;
818                                 continue;
819                         }
820                         if (res->start != start || res->end != end)
821                                 break;
822                         *p = res->sibling;
823                         write_unlock(&resource_lock);
824                         if (res->flags & IORESOURCE_MUXED)
825                                 wake_up(&muxed_resource_wait);
826                         kfree(res);
827                         return;
828                 }
829                 p = &res->sibling;
830         }
831
832         write_unlock(&resource_lock);
833
834         printk(KERN_WARNING "Trying to free nonexistent resource "
835                 "<%016llx-%016llx>\n", (unsigned long long)start,
836                 (unsigned long long)end);
837 }
838 EXPORT_SYMBOL(__release_region);
839
840 /*
841  * Managed region resource
842  */
843 struct region_devres {
844         struct resource *parent;
845         resource_size_t start;
846         resource_size_t n;
847 };
848
849 static void devm_region_release(struct device *dev, void *res)
850 {
851         struct region_devres *this = res;
852
853         __release_region(this->parent, this->start, this->n);
854 }
855
856 static int devm_region_match(struct device *dev, void *res, void *match_data)
857 {
858         struct region_devres *this = res, *match = match_data;
859
860         return this->parent == match->parent &&
861                 this->start == match->start && this->n == match->n;
862 }
863
864 struct resource * __devm_request_region(struct device *dev,
865                                 struct resource *parent, resource_size_t start,
866                                 resource_size_t n, const char *name)
867 {
868         struct region_devres *dr = NULL;
869         struct resource *res;
870
871         dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
872                           GFP_KERNEL);
873         if (!dr)
874                 return NULL;
875
876         dr->parent = parent;
877         dr->start = start;
878         dr->n = n;
879
880         res = __request_region(parent, start, n, name, 0);
881         if (res)
882                 devres_add(dev, dr);
883         else
884                 devres_free(dr);
885
886         return res;
887 }
888 EXPORT_SYMBOL(__devm_request_region);
889
890 void __devm_release_region(struct device *dev, struct resource *parent,
891                            resource_size_t start, resource_size_t n)
892 {
893         struct region_devres match_data = { parent, start, n };
894
895         __release_region(parent, start, n);
896         WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
897                                &match_data));
898 }
899 EXPORT_SYMBOL(__devm_release_region);
900
901 /*
902  * Called from init/main.c to reserve IO ports.
903  */
904 #define MAXRESERVE 4
905 static int __init reserve_setup(char *str)
906 {
907         static int reserved;
908         static struct resource reserve[MAXRESERVE];
909
910         for (;;) {
911                 unsigned int io_start, io_num;
912                 int x = reserved;
913
914                 if (get_option (&str, &io_start) != 2)
915                         break;
916                 if (get_option (&str, &io_num)   == 0)
917                         break;
918                 if (x < MAXRESERVE) {
919                         struct resource *res = reserve + x;
920                         res->name = "reserved";
921                         res->start = io_start;
922                         res->end = io_start + io_num - 1;
923                         res->flags = IORESOURCE_BUSY;
924                         res->child = NULL;
925                         if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0)
926                                 reserved = x+1;
927                 }
928         }
929         return 1;
930 }
931
932 __setup("reserve=", reserve_setup);
933
934 /*
935  * Check if the requested addr and size spans more than any slot in the
936  * iomem resource tree.
937  */
938 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
939 {
940         struct resource *p = &iomem_resource;
941         int err = 0;
942         loff_t l;
943
944         read_lock(&resource_lock);
945         for (p = p->child; p ; p = r_next(NULL, p, &l)) {
946                 /*
947                  * We can probably skip the resources without
948                  * IORESOURCE_IO attribute?
949                  */
950                 if (p->start >= addr + size)
951                         continue;
952                 if (p->end < addr)
953                         continue;
954                 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
955                     PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
956                         continue;
957                 /*
958                  * if a resource is "BUSY", it's not a hardware resource
959                  * but a driver mapping of such a resource; we don't want
960                  * to warn for those; some drivers legitimately map only
961                  * partial hardware resources. (example: vesafb)
962                  */
963                 if (p->flags & IORESOURCE_BUSY)
964                         continue;
965
966                 printk(KERN_WARNING "resource map sanity check conflict: "
967                        "0x%llx 0x%llx 0x%llx 0x%llx %s\n",
968                        (unsigned long long)addr,
969                        (unsigned long long)(addr + size - 1),
970                        (unsigned long long)p->start,
971                        (unsigned long long)p->end,
972                        p->name);
973                 err = -1;
974                 break;
975         }
976         read_unlock(&resource_lock);
977
978         return err;
979 }
980
981 #ifdef CONFIG_STRICT_DEVMEM
982 static int strict_iomem_checks = 1;
983 #else
984 static int strict_iomem_checks;
985 #endif
986
987 /*
988  * check if an address is reserved in the iomem resource tree
989  * returns 1 if reserved, 0 if not reserved.
990  */
991 int iomem_is_exclusive(u64 addr)
992 {
993         struct resource *p = &iomem_resource;
994         int err = 0;
995         loff_t l;
996         int size = PAGE_SIZE;
997
998         if (!strict_iomem_checks)
999                 return 0;
1000
1001         addr = addr & PAGE_MASK;
1002
1003         read_lock(&resource_lock);
1004         for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1005                 /*
1006                  * We can probably skip the resources without
1007                  * IORESOURCE_IO attribute?
1008                  */
1009                 if (p->start >= addr + size)
1010                         break;
1011                 if (p->end < addr)
1012                         continue;
1013                 if (p->flags & IORESOURCE_BUSY &&
1014                      p->flags & IORESOURCE_EXCLUSIVE) {
1015                         err = 1;
1016                         break;
1017                 }
1018         }
1019         read_unlock(&resource_lock);
1020
1021         return err;
1022 }
1023
1024 static int __init strict_iomem(char *str)
1025 {
1026         if (strstr(str, "relaxed"))
1027                 strict_iomem_checks = 0;
1028         if (strstr(str, "strict"))
1029                 strict_iomem_checks = 1;
1030         return 1;
1031 }
1032
1033 __setup("iomem=", strict_iomem);