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