]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - kernel/resource.c
kernel/resource: fix kfree() of bootmem memory again
[mirror_ubuntu-jammy-kernel.git] / kernel / resource.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/kernel/resource.c
4 *
5 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
7 *
8 * Arbitrary resource management.
9 */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/export.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/fs.h>
20 #include <linux/proc_fs.h>
21 #include <linux/pseudo_fs.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/device.h>
25 #include <linux/pfn.h>
26 #include <linux/mm.h>
27 #include <linux/mount.h>
28 #include <linux/resource_ext.h>
29 #include <uapi/linux/magic.h>
30 #include <asm/io.h>
31
32
33 struct resource ioport_resource = {
34 .name = "PCI IO",
35 .start = 0,
36 .end = IO_SPACE_LIMIT,
37 .flags = IORESOURCE_IO,
38 };
39 EXPORT_SYMBOL(ioport_resource);
40
41 struct resource iomem_resource = {
42 .name = "PCI mem",
43 .start = 0,
44 .end = -1,
45 .flags = IORESOURCE_MEM,
46 };
47 EXPORT_SYMBOL(iomem_resource);
48
49 /* constraints to be met while allocating resources */
50 struct resource_constraint {
51 resource_size_t min, max, align;
52 resource_size_t (*alignf)(void *, const struct resource *,
53 resource_size_t, resource_size_t);
54 void *alignf_data;
55 };
56
57 static DEFINE_RWLOCK(resource_lock);
58
59 static struct resource *next_resource(struct resource *p)
60 {
61 if (p->child)
62 return p->child;
63 while (!p->sibling && p->parent)
64 p = p->parent;
65 return p->sibling;
66 }
67
68 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
69 {
70 struct resource *p = v;
71 (*pos)++;
72 return (void *)next_resource(p);
73 }
74
75 #ifdef CONFIG_PROC_FS
76
77 enum { MAX_IORES_LEVEL = 5 };
78
79 static void *r_start(struct seq_file *m, loff_t *pos)
80 __acquires(resource_lock)
81 {
82 struct resource *p = PDE_DATA(file_inode(m->file));
83 loff_t l = 0;
84 read_lock(&resource_lock);
85 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
86 ;
87 return p;
88 }
89
90 static void r_stop(struct seq_file *m, void *v)
91 __releases(resource_lock)
92 {
93 read_unlock(&resource_lock);
94 }
95
96 static int r_show(struct seq_file *m, void *v)
97 {
98 struct resource *root = PDE_DATA(file_inode(m->file));
99 struct resource *r = v, *p;
100 unsigned long long start, end;
101 int width = root->end < 0x10000 ? 4 : 8;
102 int depth;
103
104 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
105 if (p->parent == root)
106 break;
107
108 if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
109 start = r->start;
110 end = r->end;
111 } else {
112 start = end = 0;
113 }
114
115 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
116 depth * 2, "",
117 width, start,
118 width, end,
119 r->name ? r->name : "<BAD>");
120 return 0;
121 }
122
123 static const struct seq_operations resource_op = {
124 .start = r_start,
125 .next = r_next,
126 .stop = r_stop,
127 .show = r_show,
128 };
129
130 static int __init ioresources_init(void)
131 {
132 proc_create_seq_data("ioports", 0, NULL, &resource_op,
133 &ioport_resource);
134 proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
135 return 0;
136 }
137 __initcall(ioresources_init);
138
139 #endif /* CONFIG_PROC_FS */
140
141 static void free_resource(struct resource *res)
142 {
143 /**
144 * If the resource was allocated using memblock early during boot
145 * we'll leak it here: we can only return full pages back to the
146 * buddy and trying to be smart and reusing them eventually in
147 * alloc_resource() overcomplicates resource handling.
148 */
149 if (res && PageSlab(virt_to_head_page(res)))
150 kfree(res);
151 }
152
153 static struct resource *alloc_resource(gfp_t flags)
154 {
155 return kzalloc(sizeof(struct resource), flags);
156 }
157
158 /* Return the conflict entry if you can't request it */
159 static struct resource * __request_resource(struct resource *root, struct resource *new)
160 {
161 resource_size_t start = new->start;
162 resource_size_t end = new->end;
163 struct resource *tmp, **p;
164
165 if (end < start)
166 return root;
167 if (start < root->start)
168 return root;
169 if (end > root->end)
170 return root;
171 p = &root->child;
172 for (;;) {
173 tmp = *p;
174 if (!tmp || tmp->start > end) {
175 new->sibling = tmp;
176 *p = new;
177 new->parent = root;
178 return NULL;
179 }
180 p = &tmp->sibling;
181 if (tmp->end < start)
182 continue;
183 return tmp;
184 }
185 }
186
187 static int __release_resource(struct resource *old, bool release_child)
188 {
189 struct resource *tmp, **p, *chd;
190
191 p = &old->parent->child;
192 for (;;) {
193 tmp = *p;
194 if (!tmp)
195 break;
196 if (tmp == old) {
197 if (release_child || !(tmp->child)) {
198 *p = tmp->sibling;
199 } else {
200 for (chd = tmp->child;; chd = chd->sibling) {
201 chd->parent = tmp->parent;
202 if (!(chd->sibling))
203 break;
204 }
205 *p = tmp->child;
206 chd->sibling = tmp->sibling;
207 }
208 old->parent = NULL;
209 return 0;
210 }
211 p = &tmp->sibling;
212 }
213 return -EINVAL;
214 }
215
216 static void __release_child_resources(struct resource *r)
217 {
218 struct resource *tmp, *p;
219 resource_size_t size;
220
221 p = r->child;
222 r->child = NULL;
223 while (p) {
224 tmp = p;
225 p = p->sibling;
226
227 tmp->parent = NULL;
228 tmp->sibling = NULL;
229 __release_child_resources(tmp);
230
231 printk(KERN_DEBUG "release child resource %pR\n", tmp);
232 /* need to restore size, and keep flags */
233 size = resource_size(tmp);
234 tmp->start = 0;
235 tmp->end = size - 1;
236 }
237 }
238
239 void release_child_resources(struct resource *r)
240 {
241 write_lock(&resource_lock);
242 __release_child_resources(r);
243 write_unlock(&resource_lock);
244 }
245
246 /**
247 * request_resource_conflict - request and reserve an I/O or memory resource
248 * @root: root resource descriptor
249 * @new: resource descriptor desired by caller
250 *
251 * Returns 0 for success, conflict resource on error.
252 */
253 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
254 {
255 struct resource *conflict;
256
257 write_lock(&resource_lock);
258 conflict = __request_resource(root, new);
259 write_unlock(&resource_lock);
260 return conflict;
261 }
262
263 /**
264 * request_resource - request and reserve an I/O or memory resource
265 * @root: root resource descriptor
266 * @new: resource descriptor desired by caller
267 *
268 * Returns 0 for success, negative error code on error.
269 */
270 int request_resource(struct resource *root, struct resource *new)
271 {
272 struct resource *conflict;
273
274 conflict = request_resource_conflict(root, new);
275 return conflict ? -EBUSY : 0;
276 }
277
278 EXPORT_SYMBOL(request_resource);
279
280 /**
281 * release_resource - release a previously reserved resource
282 * @old: resource pointer
283 */
284 int release_resource(struct resource *old)
285 {
286 int retval;
287
288 write_lock(&resource_lock);
289 retval = __release_resource(old, true);
290 write_unlock(&resource_lock);
291 return retval;
292 }
293
294 EXPORT_SYMBOL(release_resource);
295
296 /**
297 * find_next_iomem_res - Finds the lowest iomem resource that covers part of
298 * [@start..@end].
299 *
300 * If a resource is found, returns 0 and @*res is overwritten with the part
301 * of the resource that's within [@start..@end]; if none is found, returns
302 * -ENODEV. Returns -EINVAL for invalid parameters.
303 *
304 * @start: start address of the resource searched for
305 * @end: end address of same resource
306 * @flags: flags which the resource must have
307 * @desc: descriptor the resource must have
308 * @res: return ptr, if resource found
309 *
310 * The caller must specify @start, @end, @flags, and @desc
311 * (which may be IORES_DESC_NONE).
312 */
313 static int find_next_iomem_res(resource_size_t start, resource_size_t end,
314 unsigned long flags, unsigned long desc,
315 struct resource *res)
316 {
317 struct resource *p;
318
319 if (!res)
320 return -EINVAL;
321
322 if (start >= end)
323 return -EINVAL;
324
325 read_lock(&resource_lock);
326
327 for (p = iomem_resource.child; p; p = next_resource(p)) {
328 /* If we passed the resource we are looking for, stop */
329 if (p->start > end) {
330 p = NULL;
331 break;
332 }
333
334 /* Skip until we find a range that matches what we look for */
335 if (p->end < start)
336 continue;
337
338 if ((p->flags & flags) != flags)
339 continue;
340 if ((desc != IORES_DESC_NONE) && (desc != p->desc))
341 continue;
342
343 /* Found a match, break */
344 break;
345 }
346
347 if (p) {
348 /* copy data */
349 *res = (struct resource) {
350 .start = max(start, p->start),
351 .end = min(end, p->end),
352 .flags = p->flags,
353 .desc = p->desc,
354 .parent = p->parent,
355 };
356 }
357
358 read_unlock(&resource_lock);
359 return p ? 0 : -ENODEV;
360 }
361
362 static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
363 unsigned long flags, unsigned long desc,
364 void *arg,
365 int (*func)(struct resource *, void *))
366 {
367 struct resource res;
368 int ret = -EINVAL;
369
370 while (start < end &&
371 !find_next_iomem_res(start, end, flags, desc, &res)) {
372 ret = (*func)(&res, arg);
373 if (ret)
374 break;
375
376 start = res.end + 1;
377 }
378
379 return ret;
380 }
381
382 /**
383 * walk_iomem_res_desc - Walks through iomem resources and calls func()
384 * with matching resource ranges.
385 * *
386 * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
387 * @flags: I/O resource flags
388 * @start: start addr
389 * @end: end addr
390 * @arg: function argument for the callback @func
391 * @func: callback function that is called for each qualifying resource area
392 *
393 * All the memory ranges which overlap start,end and also match flags and
394 * desc are valid candidates.
395 *
396 * NOTE: For a new descriptor search, define a new IORES_DESC in
397 * <linux/ioport.h> and set it in 'desc' of a target resource entry.
398 */
399 int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
400 u64 end, void *arg, int (*func)(struct resource *, void *))
401 {
402 return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
403 }
404 EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
405
406 /*
407 * This function calls the @func callback against all memory ranges of type
408 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
409 * Now, this function is only for System RAM, it deals with full ranges and
410 * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
411 * ranges.
412 */
413 int walk_system_ram_res(u64 start, u64 end, void *arg,
414 int (*func)(struct resource *, void *))
415 {
416 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
417
418 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
419 func);
420 }
421
422 /*
423 * This function calls the @func callback against all memory ranges, which
424 * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
425 */
426 int walk_mem_res(u64 start, u64 end, void *arg,
427 int (*func)(struct resource *, void *))
428 {
429 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
430
431 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
432 func);
433 }
434
435 /*
436 * This function calls the @func callback against all memory ranges of type
437 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
438 * It is to be used only for System RAM.
439 */
440 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
441 void *arg, int (*func)(unsigned long, unsigned long, void *))
442 {
443 resource_size_t start, end;
444 unsigned long flags;
445 struct resource res;
446 unsigned long pfn, end_pfn;
447 int ret = -EINVAL;
448
449 start = (u64) start_pfn << PAGE_SHIFT;
450 end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
451 flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
452 while (start < end &&
453 !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
454 pfn = PFN_UP(res.start);
455 end_pfn = PFN_DOWN(res.end + 1);
456 if (end_pfn > pfn)
457 ret = (*func)(pfn, end_pfn - pfn, arg);
458 if (ret)
459 break;
460 start = res.end + 1;
461 }
462 return ret;
463 }
464
465 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
466 {
467 return 1;
468 }
469
470 /*
471 * This generic page_is_ram() returns true if specified address is
472 * registered as System RAM in iomem_resource list.
473 */
474 int __weak page_is_ram(unsigned long pfn)
475 {
476 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
477 }
478 EXPORT_SYMBOL_GPL(page_is_ram);
479
480 static int __region_intersects(resource_size_t start, size_t size,
481 unsigned long flags, unsigned long desc)
482 {
483 struct resource res;
484 int type = 0; int other = 0;
485 struct resource *p;
486
487 res.start = start;
488 res.end = start + size - 1;
489
490 for (p = iomem_resource.child; p ; p = p->sibling) {
491 bool is_type = (((p->flags & flags) == flags) &&
492 ((desc == IORES_DESC_NONE) ||
493 (desc == p->desc)));
494
495 if (resource_overlaps(p, &res))
496 is_type ? type++ : other++;
497 }
498
499 if (type == 0)
500 return REGION_DISJOINT;
501
502 if (other == 0)
503 return REGION_INTERSECTS;
504
505 return REGION_MIXED;
506 }
507
508 /**
509 * region_intersects() - determine intersection of region with known resources
510 * @start: region start address
511 * @size: size of region
512 * @flags: flags of resource (in iomem_resource)
513 * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
514 *
515 * Check if the specified region partially overlaps or fully eclipses a
516 * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
517 * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
518 * return REGION_MIXED if the region overlaps @flags/@desc and another
519 * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
520 * and no other defined resource. Note that REGION_INTERSECTS is also
521 * returned in the case when the specified region overlaps RAM and undefined
522 * memory holes.
523 *
524 * region_intersect() is used by memory remapping functions to ensure
525 * the user is not remapping RAM and is a vast speed up over walking
526 * through the resource table page by page.
527 */
528 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
529 unsigned long desc)
530 {
531 int ret;
532
533 read_lock(&resource_lock);
534 ret = __region_intersects(start, size, flags, desc);
535 read_unlock(&resource_lock);
536
537 return ret;
538 }
539 EXPORT_SYMBOL_GPL(region_intersects);
540
541 void __weak arch_remove_reservations(struct resource *avail)
542 {
543 }
544
545 static resource_size_t simple_align_resource(void *data,
546 const struct resource *avail,
547 resource_size_t size,
548 resource_size_t align)
549 {
550 return avail->start;
551 }
552
553 static void resource_clip(struct resource *res, resource_size_t min,
554 resource_size_t max)
555 {
556 if (res->start < min)
557 res->start = min;
558 if (res->end > max)
559 res->end = max;
560 }
561
562 /*
563 * Find empty slot in the resource tree with the given range and
564 * alignment constraints
565 */
566 static int __find_resource(struct resource *root, struct resource *old,
567 struct resource *new,
568 resource_size_t size,
569 struct resource_constraint *constraint)
570 {
571 struct resource *this = root->child;
572 struct resource tmp = *new, avail, alloc;
573
574 tmp.start = root->start;
575 /*
576 * Skip past an allocated resource that starts at 0, since the assignment
577 * of this->start - 1 to tmp->end below would cause an underflow.
578 */
579 if (this && this->start == root->start) {
580 tmp.start = (this == old) ? old->start : this->end + 1;
581 this = this->sibling;
582 }
583 for(;;) {
584 if (this)
585 tmp.end = (this == old) ? this->end : this->start - 1;
586 else
587 tmp.end = root->end;
588
589 if (tmp.end < tmp.start)
590 goto next;
591
592 resource_clip(&tmp, constraint->min, constraint->max);
593 arch_remove_reservations(&tmp);
594
595 /* Check for overflow after ALIGN() */
596 avail.start = ALIGN(tmp.start, constraint->align);
597 avail.end = tmp.end;
598 avail.flags = new->flags & ~IORESOURCE_UNSET;
599 if (avail.start >= tmp.start) {
600 alloc.flags = avail.flags;
601 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
602 size, constraint->align);
603 alloc.end = alloc.start + size - 1;
604 if (alloc.start <= alloc.end &&
605 resource_contains(&avail, &alloc)) {
606 new->start = alloc.start;
607 new->end = alloc.end;
608 return 0;
609 }
610 }
611
612 next: if (!this || this->end == root->end)
613 break;
614
615 if (this != old)
616 tmp.start = this->end + 1;
617 this = this->sibling;
618 }
619 return -EBUSY;
620 }
621
622 /*
623 * Find empty slot in the resource tree given range and alignment.
624 */
625 static int find_resource(struct resource *root, struct resource *new,
626 resource_size_t size,
627 struct resource_constraint *constraint)
628 {
629 return __find_resource(root, NULL, new, size, constraint);
630 }
631
632 /**
633 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
634 * The resource will be relocated if the new size cannot be reallocated in the
635 * current location.
636 *
637 * @root: root resource descriptor
638 * @old: resource descriptor desired by caller
639 * @newsize: new size of the resource descriptor
640 * @constraint: the size and alignment constraints to be met.
641 */
642 static int reallocate_resource(struct resource *root, struct resource *old,
643 resource_size_t newsize,
644 struct resource_constraint *constraint)
645 {
646 int err=0;
647 struct resource new = *old;
648 struct resource *conflict;
649
650 write_lock(&resource_lock);
651
652 if ((err = __find_resource(root, old, &new, newsize, constraint)))
653 goto out;
654
655 if (resource_contains(&new, old)) {
656 old->start = new.start;
657 old->end = new.end;
658 goto out;
659 }
660
661 if (old->child) {
662 err = -EBUSY;
663 goto out;
664 }
665
666 if (resource_contains(old, &new)) {
667 old->start = new.start;
668 old->end = new.end;
669 } else {
670 __release_resource(old, true);
671 *old = new;
672 conflict = __request_resource(root, old);
673 BUG_ON(conflict);
674 }
675 out:
676 write_unlock(&resource_lock);
677 return err;
678 }
679
680
681 /**
682 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
683 * The resource will be reallocated with a new size if it was already allocated
684 * @root: root resource descriptor
685 * @new: resource descriptor desired by caller
686 * @size: requested resource region size
687 * @min: minimum boundary to allocate
688 * @max: maximum boundary to allocate
689 * @align: alignment requested, in bytes
690 * @alignf: alignment function, optional, called if not NULL
691 * @alignf_data: arbitrary data to pass to the @alignf function
692 */
693 int allocate_resource(struct resource *root, struct resource *new,
694 resource_size_t size, resource_size_t min,
695 resource_size_t max, resource_size_t align,
696 resource_size_t (*alignf)(void *,
697 const struct resource *,
698 resource_size_t,
699 resource_size_t),
700 void *alignf_data)
701 {
702 int err;
703 struct resource_constraint constraint;
704
705 if (!alignf)
706 alignf = simple_align_resource;
707
708 constraint.min = min;
709 constraint.max = max;
710 constraint.align = align;
711 constraint.alignf = alignf;
712 constraint.alignf_data = alignf_data;
713
714 if ( new->parent ) {
715 /* resource is already allocated, try reallocating with
716 the new constraints */
717 return reallocate_resource(root, new, size, &constraint);
718 }
719
720 write_lock(&resource_lock);
721 err = find_resource(root, new, size, &constraint);
722 if (err >= 0 && __request_resource(root, new))
723 err = -EBUSY;
724 write_unlock(&resource_lock);
725 return err;
726 }
727
728 EXPORT_SYMBOL(allocate_resource);
729
730 /**
731 * lookup_resource - find an existing resource by a resource start address
732 * @root: root resource descriptor
733 * @start: resource start address
734 *
735 * Returns a pointer to the resource if found, NULL otherwise
736 */
737 struct resource *lookup_resource(struct resource *root, resource_size_t start)
738 {
739 struct resource *res;
740
741 read_lock(&resource_lock);
742 for (res = root->child; res; res = res->sibling) {
743 if (res->start == start)
744 break;
745 }
746 read_unlock(&resource_lock);
747
748 return res;
749 }
750
751 /*
752 * Insert a resource into the resource tree. If successful, return NULL,
753 * otherwise return the conflicting resource (compare to __request_resource())
754 */
755 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
756 {
757 struct resource *first, *next;
758
759 for (;; parent = first) {
760 first = __request_resource(parent, new);
761 if (!first)
762 return first;
763
764 if (first == parent)
765 return first;
766 if (WARN_ON(first == new)) /* duplicated insertion */
767 return first;
768
769 if ((first->start > new->start) || (first->end < new->end))
770 break;
771 if ((first->start == new->start) && (first->end == new->end))
772 break;
773 }
774
775 for (next = first; ; next = next->sibling) {
776 /* Partial overlap? Bad, and unfixable */
777 if (next->start < new->start || next->end > new->end)
778 return next;
779 if (!next->sibling)
780 break;
781 if (next->sibling->start > new->end)
782 break;
783 }
784
785 new->parent = parent;
786 new->sibling = next->sibling;
787 new->child = first;
788
789 next->sibling = NULL;
790 for (next = first; next; next = next->sibling)
791 next->parent = new;
792
793 if (parent->child == first) {
794 parent->child = new;
795 } else {
796 next = parent->child;
797 while (next->sibling != first)
798 next = next->sibling;
799 next->sibling = new;
800 }
801 return NULL;
802 }
803
804 /**
805 * insert_resource_conflict - Inserts resource in the resource tree
806 * @parent: parent of the new resource
807 * @new: new resource to insert
808 *
809 * Returns 0 on success, conflict resource if the resource can't be inserted.
810 *
811 * This function is equivalent to request_resource_conflict when no conflict
812 * happens. If a conflict happens, and the conflicting resources
813 * entirely fit within the range of the new resource, then the new
814 * resource is inserted and the conflicting resources become children of
815 * the new resource.
816 *
817 * This function is intended for producers of resources, such as FW modules
818 * and bus drivers.
819 */
820 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
821 {
822 struct resource *conflict;
823
824 write_lock(&resource_lock);
825 conflict = __insert_resource(parent, new);
826 write_unlock(&resource_lock);
827 return conflict;
828 }
829
830 /**
831 * insert_resource - Inserts a resource in the resource tree
832 * @parent: parent of the new resource
833 * @new: new resource to insert
834 *
835 * Returns 0 on success, -EBUSY if the resource can't be inserted.
836 *
837 * This function is intended for producers of resources, such as FW modules
838 * and bus drivers.
839 */
840 int insert_resource(struct resource *parent, struct resource *new)
841 {
842 struct resource *conflict;
843
844 conflict = insert_resource_conflict(parent, new);
845 return conflict ? -EBUSY : 0;
846 }
847 EXPORT_SYMBOL_GPL(insert_resource);
848
849 /**
850 * insert_resource_expand_to_fit - Insert a resource into the resource tree
851 * @root: root resource descriptor
852 * @new: new resource to insert
853 *
854 * Insert a resource into the resource tree, possibly expanding it in order
855 * to make it encompass any conflicting resources.
856 */
857 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
858 {
859 if (new->parent)
860 return;
861
862 write_lock(&resource_lock);
863 for (;;) {
864 struct resource *conflict;
865
866 conflict = __insert_resource(root, new);
867 if (!conflict)
868 break;
869 if (conflict == root)
870 break;
871
872 /* Ok, expand resource to cover the conflict, then try again .. */
873 if (conflict->start < new->start)
874 new->start = conflict->start;
875 if (conflict->end > new->end)
876 new->end = conflict->end;
877
878 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
879 }
880 write_unlock(&resource_lock);
881 }
882
883 /**
884 * remove_resource - Remove a resource in the resource tree
885 * @old: resource to remove
886 *
887 * Returns 0 on success, -EINVAL if the resource is not valid.
888 *
889 * This function removes a resource previously inserted by insert_resource()
890 * or insert_resource_conflict(), and moves the children (if any) up to
891 * where they were before. insert_resource() and insert_resource_conflict()
892 * insert a new resource, and move any conflicting resources down to the
893 * children of the new resource.
894 *
895 * insert_resource(), insert_resource_conflict() and remove_resource() are
896 * intended for producers of resources, such as FW modules and bus drivers.
897 */
898 int remove_resource(struct resource *old)
899 {
900 int retval;
901
902 write_lock(&resource_lock);
903 retval = __release_resource(old, false);
904 write_unlock(&resource_lock);
905 return retval;
906 }
907 EXPORT_SYMBOL_GPL(remove_resource);
908
909 static int __adjust_resource(struct resource *res, resource_size_t start,
910 resource_size_t size)
911 {
912 struct resource *tmp, *parent = res->parent;
913 resource_size_t end = start + size - 1;
914 int result = -EBUSY;
915
916 if (!parent)
917 goto skip;
918
919 if ((start < parent->start) || (end > parent->end))
920 goto out;
921
922 if (res->sibling && (res->sibling->start <= end))
923 goto out;
924
925 tmp = parent->child;
926 if (tmp != res) {
927 while (tmp->sibling != res)
928 tmp = tmp->sibling;
929 if (start <= tmp->end)
930 goto out;
931 }
932
933 skip:
934 for (tmp = res->child; tmp; tmp = tmp->sibling)
935 if ((tmp->start < start) || (tmp->end > end))
936 goto out;
937
938 res->start = start;
939 res->end = end;
940 result = 0;
941
942 out:
943 return result;
944 }
945
946 /**
947 * adjust_resource - modify a resource's start and size
948 * @res: resource to modify
949 * @start: new start value
950 * @size: new size
951 *
952 * Given an existing resource, change its start and size to match the
953 * arguments. Returns 0 on success, -EBUSY if it can't fit.
954 * Existing children of the resource are assumed to be immutable.
955 */
956 int adjust_resource(struct resource *res, resource_size_t start,
957 resource_size_t size)
958 {
959 int result;
960
961 write_lock(&resource_lock);
962 result = __adjust_resource(res, start, size);
963 write_unlock(&resource_lock);
964 return result;
965 }
966 EXPORT_SYMBOL(adjust_resource);
967
968 static void __init
969 __reserve_region_with_split(struct resource *root, resource_size_t start,
970 resource_size_t end, const char *name)
971 {
972 struct resource *parent = root;
973 struct resource *conflict;
974 struct resource *res = alloc_resource(GFP_ATOMIC);
975 struct resource *next_res = NULL;
976 int type = resource_type(root);
977
978 if (!res)
979 return;
980
981 res->name = name;
982 res->start = start;
983 res->end = end;
984 res->flags = type | IORESOURCE_BUSY;
985 res->desc = IORES_DESC_NONE;
986
987 while (1) {
988
989 conflict = __request_resource(parent, res);
990 if (!conflict) {
991 if (!next_res)
992 break;
993 res = next_res;
994 next_res = NULL;
995 continue;
996 }
997
998 /* conflict covered whole area */
999 if (conflict->start <= res->start &&
1000 conflict->end >= res->end) {
1001 free_resource(res);
1002 WARN_ON(next_res);
1003 break;
1004 }
1005
1006 /* failed, split and try again */
1007 if (conflict->start > res->start) {
1008 end = res->end;
1009 res->end = conflict->start - 1;
1010 if (conflict->end < end) {
1011 next_res = alloc_resource(GFP_ATOMIC);
1012 if (!next_res) {
1013 free_resource(res);
1014 break;
1015 }
1016 next_res->name = name;
1017 next_res->start = conflict->end + 1;
1018 next_res->end = end;
1019 next_res->flags = type | IORESOURCE_BUSY;
1020 next_res->desc = IORES_DESC_NONE;
1021 }
1022 } else {
1023 res->start = conflict->end + 1;
1024 }
1025 }
1026
1027 }
1028
1029 void __init
1030 reserve_region_with_split(struct resource *root, resource_size_t start,
1031 resource_size_t end, const char *name)
1032 {
1033 int abort = 0;
1034
1035 write_lock(&resource_lock);
1036 if (root->start > start || root->end < end) {
1037 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1038 (unsigned long long)start, (unsigned long long)end,
1039 root);
1040 if (start > root->end || end < root->start)
1041 abort = 1;
1042 else {
1043 if (end > root->end)
1044 end = root->end;
1045 if (start < root->start)
1046 start = root->start;
1047 pr_err("fixing request to [0x%llx-0x%llx]\n",
1048 (unsigned long long)start,
1049 (unsigned long long)end);
1050 }
1051 dump_stack();
1052 }
1053 if (!abort)
1054 __reserve_region_with_split(root, start, end, name);
1055 write_unlock(&resource_lock);
1056 }
1057
1058 /**
1059 * resource_alignment - calculate resource's alignment
1060 * @res: resource pointer
1061 *
1062 * Returns alignment on success, 0 (invalid alignment) on failure.
1063 */
1064 resource_size_t resource_alignment(struct resource *res)
1065 {
1066 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1067 case IORESOURCE_SIZEALIGN:
1068 return resource_size(res);
1069 case IORESOURCE_STARTALIGN:
1070 return res->start;
1071 default:
1072 return 0;
1073 }
1074 }
1075
1076 /*
1077 * This is compatibility stuff for IO resources.
1078 *
1079 * Note how this, unlike the above, knows about
1080 * the IO flag meanings (busy etc).
1081 *
1082 * request_region creates a new busy region.
1083 *
1084 * release_region releases a matching busy region.
1085 */
1086
1087 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1088
1089 static struct inode *iomem_inode;
1090
1091 #ifdef CONFIG_IO_STRICT_DEVMEM
1092 static void revoke_iomem(struct resource *res)
1093 {
1094 /* pairs with smp_store_release() in iomem_init_inode() */
1095 struct inode *inode = smp_load_acquire(&iomem_inode);
1096
1097 /*
1098 * Check that the initialization has completed. Losing the race
1099 * is ok because it means drivers are claiming resources before
1100 * the fs_initcall level of init and prevent iomem_get_mapping users
1101 * from establishing mappings.
1102 */
1103 if (!inode)
1104 return;
1105
1106 /*
1107 * The expectation is that the driver has successfully marked
1108 * the resource busy by this point, so devmem_is_allowed()
1109 * should start returning false, however for performance this
1110 * does not iterate the entire resource range.
1111 */
1112 if (devmem_is_allowed(PHYS_PFN(res->start)) &&
1113 devmem_is_allowed(PHYS_PFN(res->end))) {
1114 /*
1115 * *cringe* iomem=relaxed says "go ahead, what's the
1116 * worst that can happen?"
1117 */
1118 return;
1119 }
1120
1121 unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
1122 }
1123 #else
1124 static void revoke_iomem(struct resource *res) {}
1125 #endif
1126
1127 struct address_space *iomem_get_mapping(void)
1128 {
1129 /*
1130 * This function is only called from file open paths, hence guaranteed
1131 * that fs_initcalls have completed and no need to check for NULL. But
1132 * since revoke_iomem can be called before the initcall we still need
1133 * the barrier to appease checkers.
1134 */
1135 return smp_load_acquire(&iomem_inode)->i_mapping;
1136 }
1137
1138 static int __request_region_locked(struct resource *res, struct resource *parent,
1139 resource_size_t start, resource_size_t n,
1140 const char *name, int flags)
1141 {
1142 DECLARE_WAITQUEUE(wait, current);
1143
1144 res->name = name;
1145 res->start = start;
1146 res->end = start + n - 1;
1147
1148 for (;;) {
1149 struct resource *conflict;
1150
1151 res->flags = resource_type(parent) | resource_ext_type(parent);
1152 res->flags |= IORESOURCE_BUSY | flags;
1153 res->desc = parent->desc;
1154
1155 conflict = __request_resource(parent, res);
1156 if (!conflict)
1157 break;
1158 /*
1159 * mm/hmm.c reserves physical addresses which then
1160 * become unavailable to other users. Conflicts are
1161 * not expected. Warn to aid debugging if encountered.
1162 */
1163 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1164 pr_warn("Unaddressable device %s %pR conflicts with %pR",
1165 conflict->name, conflict, res);
1166 }
1167 if (conflict != parent) {
1168 if (!(conflict->flags & IORESOURCE_BUSY)) {
1169 parent = conflict;
1170 continue;
1171 }
1172 }
1173 if (conflict->flags & flags & IORESOURCE_MUXED) {
1174 add_wait_queue(&muxed_resource_wait, &wait);
1175 write_unlock(&resource_lock);
1176 set_current_state(TASK_UNINTERRUPTIBLE);
1177 schedule();
1178 remove_wait_queue(&muxed_resource_wait, &wait);
1179 write_lock(&resource_lock);
1180 continue;
1181 }
1182 /* Uhhuh, that didn't work out.. */
1183 return -EBUSY;
1184 }
1185
1186 return 0;
1187 }
1188
1189 /**
1190 * __request_region - create a new busy resource region
1191 * @parent: parent resource descriptor
1192 * @start: resource start address
1193 * @n: resource region size
1194 * @name: reserving caller's ID string
1195 * @flags: IO resource flags
1196 */
1197 struct resource *__request_region(struct resource *parent,
1198 resource_size_t start, resource_size_t n,
1199 const char *name, int flags)
1200 {
1201 struct resource *res = alloc_resource(GFP_KERNEL);
1202 int ret;
1203
1204 if (!res)
1205 return NULL;
1206
1207 write_lock(&resource_lock);
1208 ret = __request_region_locked(res, parent, start, n, name, flags);
1209 write_unlock(&resource_lock);
1210
1211 if (ret) {
1212 free_resource(res);
1213 return NULL;
1214 }
1215
1216 if (parent == &iomem_resource)
1217 revoke_iomem(res);
1218
1219 return res;
1220 }
1221 EXPORT_SYMBOL(__request_region);
1222
1223 /**
1224 * __release_region - release a previously reserved resource region
1225 * @parent: parent resource descriptor
1226 * @start: resource start address
1227 * @n: resource region size
1228 *
1229 * The described resource region must match a currently busy region.
1230 */
1231 void __release_region(struct resource *parent, resource_size_t start,
1232 resource_size_t n)
1233 {
1234 struct resource **p;
1235 resource_size_t end;
1236
1237 p = &parent->child;
1238 end = start + n - 1;
1239
1240 write_lock(&resource_lock);
1241
1242 for (;;) {
1243 struct resource *res = *p;
1244
1245 if (!res)
1246 break;
1247 if (res->start <= start && res->end >= end) {
1248 if (!(res->flags & IORESOURCE_BUSY)) {
1249 p = &res->child;
1250 continue;
1251 }
1252 if (res->start != start || res->end != end)
1253 break;
1254 *p = res->sibling;
1255 write_unlock(&resource_lock);
1256 if (res->flags & IORESOURCE_MUXED)
1257 wake_up(&muxed_resource_wait);
1258 free_resource(res);
1259 return;
1260 }
1261 p = &res->sibling;
1262 }
1263
1264 write_unlock(&resource_lock);
1265
1266 printk(KERN_WARNING "Trying to free nonexistent resource "
1267 "<%016llx-%016llx>\n", (unsigned long long)start,
1268 (unsigned long long)end);
1269 }
1270 EXPORT_SYMBOL(__release_region);
1271
1272 #ifdef CONFIG_MEMORY_HOTREMOVE
1273 /**
1274 * release_mem_region_adjustable - release a previously reserved memory region
1275 * @start: resource start address
1276 * @size: resource region size
1277 *
1278 * This interface is intended for memory hot-delete. The requested region
1279 * is released from a currently busy memory resource. The requested region
1280 * must either match exactly or fit into a single busy resource entry. In
1281 * the latter case, the remaining resource is adjusted accordingly.
1282 * Existing children of the busy memory resource must be immutable in the
1283 * request.
1284 *
1285 * Note:
1286 * - Additional release conditions, such as overlapping region, can be
1287 * supported after they are confirmed as valid cases.
1288 * - When a busy memory resource gets split into two entries, the code
1289 * assumes that all children remain in the lower address entry for
1290 * simplicity. Enhance this logic when necessary.
1291 */
1292 void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1293 {
1294 struct resource *parent = &iomem_resource;
1295 struct resource *new_res = NULL;
1296 bool alloc_nofail = false;
1297 struct resource **p;
1298 struct resource *res;
1299 resource_size_t end;
1300
1301 end = start + size - 1;
1302 if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1303 return;
1304
1305 /*
1306 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1307 * just before releasing the region. This is highly unlikely to
1308 * fail - let's play save and make it never fail as the caller cannot
1309 * perform any error handling (e.g., trying to re-add memory will fail
1310 * similarly).
1311 */
1312 retry:
1313 new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1314
1315 p = &parent->child;
1316 write_lock(&resource_lock);
1317
1318 while ((res = *p)) {
1319 if (res->start >= end)
1320 break;
1321
1322 /* look for the next resource if it does not fit into */
1323 if (res->start > start || res->end < end) {
1324 p = &res->sibling;
1325 continue;
1326 }
1327
1328 /*
1329 * All memory regions added from memory-hotplug path have the
1330 * flag IORESOURCE_SYSTEM_RAM. If the resource does not have
1331 * this flag, we know that we are dealing with a resource coming
1332 * from HMM/devm. HMM/devm use another mechanism to add/release
1333 * a resource. This goes via devm_request_mem_region and
1334 * devm_release_mem_region.
1335 * HMM/devm take care to release their resources when they want,
1336 * so if we are dealing with them, let us just back off here.
1337 */
1338 if (!(res->flags & IORESOURCE_SYSRAM)) {
1339 break;
1340 }
1341
1342 if (!(res->flags & IORESOURCE_MEM))
1343 break;
1344
1345 if (!(res->flags & IORESOURCE_BUSY)) {
1346 p = &res->child;
1347 continue;
1348 }
1349
1350 /* found the target resource; let's adjust accordingly */
1351 if (res->start == start && res->end == end) {
1352 /* free the whole entry */
1353 *p = res->sibling;
1354 free_resource(res);
1355 } else if (res->start == start && res->end != end) {
1356 /* adjust the start */
1357 WARN_ON_ONCE(__adjust_resource(res, end + 1,
1358 res->end - end));
1359 } else if (res->start != start && res->end == end) {
1360 /* adjust the end */
1361 WARN_ON_ONCE(__adjust_resource(res, res->start,
1362 start - res->start));
1363 } else {
1364 /* split into two entries - we need a new resource */
1365 if (!new_res) {
1366 new_res = alloc_resource(GFP_ATOMIC);
1367 if (!new_res) {
1368 alloc_nofail = true;
1369 write_unlock(&resource_lock);
1370 goto retry;
1371 }
1372 }
1373 new_res->name = res->name;
1374 new_res->start = end + 1;
1375 new_res->end = res->end;
1376 new_res->flags = res->flags;
1377 new_res->desc = res->desc;
1378 new_res->parent = res->parent;
1379 new_res->sibling = res->sibling;
1380 new_res->child = NULL;
1381
1382 if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1383 start - res->start)))
1384 break;
1385 res->sibling = new_res;
1386 new_res = NULL;
1387 }
1388
1389 break;
1390 }
1391
1392 write_unlock(&resource_lock);
1393 free_resource(new_res);
1394 }
1395 #endif /* CONFIG_MEMORY_HOTREMOVE */
1396
1397 #ifdef CONFIG_MEMORY_HOTPLUG
1398 static bool system_ram_resources_mergeable(struct resource *r1,
1399 struct resource *r2)
1400 {
1401 /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1402 return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1403 r1->name == r2->name && r1->desc == r2->desc &&
1404 !r1->child && !r2->child;
1405 }
1406
1407 /**
1408 * merge_system_ram_resource - mark the System RAM resource mergeable and try to
1409 * merge it with adjacent, mergeable resources
1410 * @res: resource descriptor
1411 *
1412 * This interface is intended for memory hotplug, whereby lots of contiguous
1413 * system ram resources are added (e.g., via add_memory*()) by a driver, and
1414 * the actual resource boundaries are not of interest (e.g., it might be
1415 * relevant for DIMMs). Only resources that are marked mergeable, that have the
1416 * same parent, and that don't have any children are considered. All mergeable
1417 * resources must be immutable during the request.
1418 *
1419 * Note:
1420 * - The caller has to make sure that no pointers to resources that are
1421 * marked mergeable are used anymore after this call - the resource might
1422 * be freed and the pointer might be stale!
1423 * - release_mem_region_adjustable() will split on demand on memory hotunplug
1424 */
1425 void merge_system_ram_resource(struct resource *res)
1426 {
1427 const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1428 struct resource *cur;
1429
1430 if (WARN_ON_ONCE((res->flags & flags) != flags))
1431 return;
1432
1433 write_lock(&resource_lock);
1434 res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1435
1436 /* Try to merge with next item in the list. */
1437 cur = res->sibling;
1438 if (cur && system_ram_resources_mergeable(res, cur)) {
1439 res->end = cur->end;
1440 res->sibling = cur->sibling;
1441 free_resource(cur);
1442 }
1443
1444 /* Try to merge with previous item in the list. */
1445 cur = res->parent->child;
1446 while (cur && cur->sibling != res)
1447 cur = cur->sibling;
1448 if (cur && system_ram_resources_mergeable(cur, res)) {
1449 cur->end = res->end;
1450 cur->sibling = res->sibling;
1451 free_resource(res);
1452 }
1453 write_unlock(&resource_lock);
1454 }
1455 #endif /* CONFIG_MEMORY_HOTPLUG */
1456
1457 /*
1458 * Managed region resource
1459 */
1460 static void devm_resource_release(struct device *dev, void *ptr)
1461 {
1462 struct resource **r = ptr;
1463
1464 release_resource(*r);
1465 }
1466
1467 /**
1468 * devm_request_resource() - request and reserve an I/O or memory resource
1469 * @dev: device for which to request the resource
1470 * @root: root of the resource tree from which to request the resource
1471 * @new: descriptor of the resource to request
1472 *
1473 * This is a device-managed version of request_resource(). There is usually
1474 * no need to release resources requested by this function explicitly since
1475 * that will be taken care of when the device is unbound from its driver.
1476 * If for some reason the resource needs to be released explicitly, because
1477 * of ordering issues for example, drivers must call devm_release_resource()
1478 * rather than the regular release_resource().
1479 *
1480 * When a conflict is detected between any existing resources and the newly
1481 * requested resource, an error message will be printed.
1482 *
1483 * Returns 0 on success or a negative error code on failure.
1484 */
1485 int devm_request_resource(struct device *dev, struct resource *root,
1486 struct resource *new)
1487 {
1488 struct resource *conflict, **ptr;
1489
1490 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1491 if (!ptr)
1492 return -ENOMEM;
1493
1494 *ptr = new;
1495
1496 conflict = request_resource_conflict(root, new);
1497 if (conflict) {
1498 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1499 new, conflict->name, conflict);
1500 devres_free(ptr);
1501 return -EBUSY;
1502 }
1503
1504 devres_add(dev, ptr);
1505 return 0;
1506 }
1507 EXPORT_SYMBOL(devm_request_resource);
1508
1509 static int devm_resource_match(struct device *dev, void *res, void *data)
1510 {
1511 struct resource **ptr = res;
1512
1513 return *ptr == data;
1514 }
1515
1516 /**
1517 * devm_release_resource() - release a previously requested resource
1518 * @dev: device for which to release the resource
1519 * @new: descriptor of the resource to release
1520 *
1521 * Releases a resource previously requested using devm_request_resource().
1522 */
1523 void devm_release_resource(struct device *dev, struct resource *new)
1524 {
1525 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1526 new));
1527 }
1528 EXPORT_SYMBOL(devm_release_resource);
1529
1530 struct region_devres {
1531 struct resource *parent;
1532 resource_size_t start;
1533 resource_size_t n;
1534 };
1535
1536 static void devm_region_release(struct device *dev, void *res)
1537 {
1538 struct region_devres *this = res;
1539
1540 __release_region(this->parent, this->start, this->n);
1541 }
1542
1543 static int devm_region_match(struct device *dev, void *res, void *match_data)
1544 {
1545 struct region_devres *this = res, *match = match_data;
1546
1547 return this->parent == match->parent &&
1548 this->start == match->start && this->n == match->n;
1549 }
1550
1551 struct resource *
1552 __devm_request_region(struct device *dev, struct resource *parent,
1553 resource_size_t start, resource_size_t n, const char *name)
1554 {
1555 struct region_devres *dr = NULL;
1556 struct resource *res;
1557
1558 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1559 GFP_KERNEL);
1560 if (!dr)
1561 return NULL;
1562
1563 dr->parent = parent;
1564 dr->start = start;
1565 dr->n = n;
1566
1567 res = __request_region(parent, start, n, name, 0);
1568 if (res)
1569 devres_add(dev, dr);
1570 else
1571 devres_free(dr);
1572
1573 return res;
1574 }
1575 EXPORT_SYMBOL(__devm_request_region);
1576
1577 void __devm_release_region(struct device *dev, struct resource *parent,
1578 resource_size_t start, resource_size_t n)
1579 {
1580 struct region_devres match_data = { parent, start, n };
1581
1582 __release_region(parent, start, n);
1583 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1584 &match_data));
1585 }
1586 EXPORT_SYMBOL(__devm_release_region);
1587
1588 /*
1589 * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1590 */
1591 #define MAXRESERVE 4
1592 static int __init reserve_setup(char *str)
1593 {
1594 static int reserved;
1595 static struct resource reserve[MAXRESERVE];
1596
1597 for (;;) {
1598 unsigned int io_start, io_num;
1599 int x = reserved;
1600 struct resource *parent;
1601
1602 if (get_option(&str, &io_start) != 2)
1603 break;
1604 if (get_option(&str, &io_num) == 0)
1605 break;
1606 if (x < MAXRESERVE) {
1607 struct resource *res = reserve + x;
1608
1609 /*
1610 * If the region starts below 0x10000, we assume it's
1611 * I/O port space; otherwise assume it's memory.
1612 */
1613 if (io_start < 0x10000) {
1614 res->flags = IORESOURCE_IO;
1615 parent = &ioport_resource;
1616 } else {
1617 res->flags = IORESOURCE_MEM;
1618 parent = &iomem_resource;
1619 }
1620 res->name = "reserved";
1621 res->start = io_start;
1622 res->end = io_start + io_num - 1;
1623 res->flags |= IORESOURCE_BUSY;
1624 res->desc = IORES_DESC_NONE;
1625 res->child = NULL;
1626 if (request_resource(parent, res) == 0)
1627 reserved = x+1;
1628 }
1629 }
1630 return 1;
1631 }
1632 __setup("reserve=", reserve_setup);
1633
1634 /*
1635 * Check if the requested addr and size spans more than any slot in the
1636 * iomem resource tree.
1637 */
1638 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1639 {
1640 struct resource *p = &iomem_resource;
1641 int err = 0;
1642 loff_t l;
1643
1644 read_lock(&resource_lock);
1645 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1646 /*
1647 * We can probably skip the resources without
1648 * IORESOURCE_IO attribute?
1649 */
1650 if (p->start >= addr + size)
1651 continue;
1652 if (p->end < addr)
1653 continue;
1654 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1655 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1656 continue;
1657 /*
1658 * if a resource is "BUSY", it's not a hardware resource
1659 * but a driver mapping of such a resource; we don't want
1660 * to warn for those; some drivers legitimately map only
1661 * partial hardware resources. (example: vesafb)
1662 */
1663 if (p->flags & IORESOURCE_BUSY)
1664 continue;
1665
1666 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1667 (unsigned long long)addr,
1668 (unsigned long long)(addr + size - 1),
1669 p->name, p);
1670 err = -1;
1671 break;
1672 }
1673 read_unlock(&resource_lock);
1674
1675 return err;
1676 }
1677
1678 #ifdef CONFIG_STRICT_DEVMEM
1679 static int strict_iomem_checks = 1;
1680 #else
1681 static int strict_iomem_checks;
1682 #endif
1683
1684 /*
1685 * check if an address is reserved in the iomem resource tree
1686 * returns true if reserved, false if not reserved.
1687 */
1688 bool iomem_is_exclusive(u64 addr)
1689 {
1690 struct resource *p = &iomem_resource;
1691 bool err = false;
1692 loff_t l;
1693 int size = PAGE_SIZE;
1694
1695 if (!strict_iomem_checks)
1696 return false;
1697
1698 addr = addr & PAGE_MASK;
1699
1700 read_lock(&resource_lock);
1701 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1702 /*
1703 * We can probably skip the resources without
1704 * IORESOURCE_IO attribute?
1705 */
1706 if (p->start >= addr + size)
1707 break;
1708 if (p->end < addr)
1709 continue;
1710 /*
1711 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1712 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1713 * resource is busy.
1714 */
1715 if ((p->flags & IORESOURCE_BUSY) == 0)
1716 continue;
1717 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1718 || p->flags & IORESOURCE_EXCLUSIVE) {
1719 err = true;
1720 break;
1721 }
1722 }
1723 read_unlock(&resource_lock);
1724
1725 return err;
1726 }
1727
1728 struct resource_entry *resource_list_create_entry(struct resource *res,
1729 size_t extra_size)
1730 {
1731 struct resource_entry *entry;
1732
1733 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1734 if (entry) {
1735 INIT_LIST_HEAD(&entry->node);
1736 entry->res = res ? res : &entry->__res;
1737 }
1738
1739 return entry;
1740 }
1741 EXPORT_SYMBOL(resource_list_create_entry);
1742
1743 void resource_list_free(struct list_head *head)
1744 {
1745 struct resource_entry *entry, *tmp;
1746
1747 list_for_each_entry_safe(entry, tmp, head, node)
1748 resource_list_destroy_entry(entry);
1749 }
1750 EXPORT_SYMBOL(resource_list_free);
1751
1752 #ifdef CONFIG_DEVICE_PRIVATE
1753 static struct resource *__request_free_mem_region(struct device *dev,
1754 struct resource *base, unsigned long size, const char *name)
1755 {
1756 resource_size_t end, addr;
1757 struct resource *res;
1758 struct region_devres *dr = NULL;
1759
1760 size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
1761 end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
1762 addr = end - size + 1UL;
1763
1764 res = alloc_resource(GFP_KERNEL);
1765 if (!res)
1766 return ERR_PTR(-ENOMEM);
1767
1768 if (dev) {
1769 dr = devres_alloc(devm_region_release,
1770 sizeof(struct region_devres), GFP_KERNEL);
1771 if (!dr) {
1772 free_resource(res);
1773 return ERR_PTR(-ENOMEM);
1774 }
1775 }
1776
1777 write_lock(&resource_lock);
1778 for (; addr > size && addr >= base->start; addr -= size) {
1779 if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
1780 REGION_DISJOINT)
1781 continue;
1782
1783 if (__request_region_locked(res, &iomem_resource, addr, size,
1784 name, 0))
1785 break;
1786
1787 if (dev) {
1788 dr->parent = &iomem_resource;
1789 dr->start = addr;
1790 dr->n = size;
1791 devres_add(dev, dr);
1792 }
1793
1794 res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1795 write_unlock(&resource_lock);
1796
1797 /*
1798 * A driver is claiming this region so revoke any mappings.
1799 */
1800 revoke_iomem(res);
1801 return res;
1802 }
1803 write_unlock(&resource_lock);
1804
1805 free_resource(res);
1806 if (dr)
1807 devres_free(dr);
1808
1809 return ERR_PTR(-ERANGE);
1810 }
1811
1812 /**
1813 * devm_request_free_mem_region - find free region for device private memory
1814 *
1815 * @dev: device struct to bind the resource to
1816 * @size: size in bytes of the device memory to add
1817 * @base: resource tree to look in
1818 *
1819 * This function tries to find an empty range of physical address big enough to
1820 * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
1821 * memory, which in turn allocates struct pages.
1822 */
1823 struct resource *devm_request_free_mem_region(struct device *dev,
1824 struct resource *base, unsigned long size)
1825 {
1826 return __request_free_mem_region(dev, base, size, dev_name(dev));
1827 }
1828 EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
1829
1830 struct resource *request_free_mem_region(struct resource *base,
1831 unsigned long size, const char *name)
1832 {
1833 return __request_free_mem_region(NULL, base, size, name);
1834 }
1835 EXPORT_SYMBOL_GPL(request_free_mem_region);
1836
1837 #endif /* CONFIG_DEVICE_PRIVATE */
1838
1839 static int __init strict_iomem(char *str)
1840 {
1841 if (strstr(str, "relaxed"))
1842 strict_iomem_checks = 0;
1843 if (strstr(str, "strict"))
1844 strict_iomem_checks = 1;
1845 return 1;
1846 }
1847
1848 static int iomem_fs_init_fs_context(struct fs_context *fc)
1849 {
1850 return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
1851 }
1852
1853 static struct file_system_type iomem_fs_type = {
1854 .name = "iomem",
1855 .owner = THIS_MODULE,
1856 .init_fs_context = iomem_fs_init_fs_context,
1857 .kill_sb = kill_anon_super,
1858 };
1859
1860 static int __init iomem_init_inode(void)
1861 {
1862 static struct vfsmount *iomem_vfs_mount;
1863 static int iomem_fs_cnt;
1864 struct inode *inode;
1865 int rc;
1866
1867 rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
1868 if (rc < 0) {
1869 pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
1870 return rc;
1871 }
1872
1873 inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
1874 if (IS_ERR(inode)) {
1875 rc = PTR_ERR(inode);
1876 pr_err("Cannot allocate inode for iomem: %d\n", rc);
1877 simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
1878 return rc;
1879 }
1880
1881 /*
1882 * Publish iomem revocation inode initialized.
1883 * Pairs with smp_load_acquire() in revoke_iomem().
1884 */
1885 smp_store_release(&iomem_inode, inode);
1886
1887 return 0;
1888 }
1889
1890 fs_initcall(iomem_init_inode);
1891
1892 __setup("iomem=", strict_iomem);