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