xref: /openbmc/linux/kernel/resource.c (revision e92606fa)
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 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 
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 
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 
102 static void r_stop(struct seq_file *m, void *v)
103 	__releases(resource_lock)
104 {
105 	read_unlock(&resource_lock);
106 }
107 
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 
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 
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 
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 */
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 
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 
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 
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  */
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  */
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  */
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  */
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 
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  */
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  */
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  */
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  */
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 
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  */
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 
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 	struct resource res;
497 	int type = 0; int other = 0;
498 	struct resource *p;
499 
500 	res.start = start;
501 	res.end = start + size - 1;
502 
503 	for (p = parent->child; p ; p = p->sibling) {
504 		bool is_type = (((p->flags & flags) == flags) &&
505 				((desc == IORES_DESC_NONE) ||
506 				 (desc == p->desc)));
507 
508 		if (resource_overlaps(p, &res))
509 			is_type ? type++ : other++;
510 	}
511 
512 	if (type == 0)
513 		return REGION_DISJOINT;
514 
515 	if (other == 0)
516 		return REGION_INTERSECTS;
517 
518 	return REGION_MIXED;
519 }
520 
521 /**
522  * region_intersects() - determine intersection of region with known resources
523  * @start: region start address
524  * @size: size of region
525  * @flags: flags of resource (in iomem_resource)
526  * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
527  *
528  * Check if the specified region partially overlaps or fully eclipses a
529  * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
530  * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
531  * return REGION_MIXED if the region overlaps @flags/@desc and another
532  * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
533  * and no other defined resource. Note that REGION_INTERSECTS is also
534  * returned in the case when the specified region overlaps RAM and undefined
535  * memory holes.
536  *
537  * region_intersect() is used by memory remapping functions to ensure
538  * the user is not remapping RAM and is a vast speed up over walking
539  * through the resource table page by page.
540  */
541 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
542 		      unsigned long desc)
543 {
544 	int ret;
545 
546 	read_lock(&resource_lock);
547 	ret = __region_intersects(&iomem_resource, start, size, flags, desc);
548 	read_unlock(&resource_lock);
549 
550 	return ret;
551 }
552 EXPORT_SYMBOL_GPL(region_intersects);
553 
554 void __weak arch_remove_reservations(struct resource *avail)
555 {
556 }
557 
558 static resource_size_t simple_align_resource(void *data,
559 					     const struct resource *avail,
560 					     resource_size_t size,
561 					     resource_size_t align)
562 {
563 	return avail->start;
564 }
565 
566 static void resource_clip(struct resource *res, resource_size_t min,
567 			  resource_size_t max)
568 {
569 	if (res->start < min)
570 		res->start = min;
571 	if (res->end > max)
572 		res->end = max;
573 }
574 
575 /*
576  * Find empty slot in the resource tree with the given range and
577  * alignment constraints
578  */
579 static int __find_resource(struct resource *root, struct resource *old,
580 			 struct resource *new,
581 			 resource_size_t  size,
582 			 struct resource_constraint *constraint)
583 {
584 	struct resource *this = root->child;
585 	struct resource tmp = *new, avail, alloc;
586 
587 	tmp.start = root->start;
588 	/*
589 	 * Skip past an allocated resource that starts at 0, since the assignment
590 	 * of this->start - 1 to tmp->end below would cause an underflow.
591 	 */
592 	if (this && this->start == root->start) {
593 		tmp.start = (this == old) ? old->start : this->end + 1;
594 		this = this->sibling;
595 	}
596 	for(;;) {
597 		if (this)
598 			tmp.end = (this == old) ?  this->end : this->start - 1;
599 		else
600 			tmp.end = root->end;
601 
602 		if (tmp.end < tmp.start)
603 			goto next;
604 
605 		resource_clip(&tmp, constraint->min, constraint->max);
606 		arch_remove_reservations(&tmp);
607 
608 		/* Check for overflow after ALIGN() */
609 		avail.start = ALIGN(tmp.start, constraint->align);
610 		avail.end = tmp.end;
611 		avail.flags = new->flags & ~IORESOURCE_UNSET;
612 		if (avail.start >= tmp.start) {
613 			alloc.flags = avail.flags;
614 			alloc.start = constraint->alignf(constraint->alignf_data, &avail,
615 					size, constraint->align);
616 			alloc.end = alloc.start + size - 1;
617 			if (alloc.start <= alloc.end &&
618 			    resource_contains(&avail, &alloc)) {
619 				new->start = alloc.start;
620 				new->end = alloc.end;
621 				return 0;
622 			}
623 		}
624 
625 next:		if (!this || this->end == root->end)
626 			break;
627 
628 		if (this != old)
629 			tmp.start = this->end + 1;
630 		this = this->sibling;
631 	}
632 	return -EBUSY;
633 }
634 
635 /*
636  * Find empty slot in the resource tree given range and alignment.
637  */
638 static int find_resource(struct resource *root, struct resource *new,
639 			resource_size_t size,
640 			struct resource_constraint  *constraint)
641 {
642 	return  __find_resource(root, NULL, new, size, constraint);
643 }
644 
645 /**
646  * reallocate_resource - allocate a slot in the resource tree given range & alignment.
647  *	The resource will be relocated if the new size cannot be reallocated in the
648  *	current location.
649  *
650  * @root: root resource descriptor
651  * @old:  resource descriptor desired by caller
652  * @newsize: new size of the resource descriptor
653  * @constraint: the size and alignment constraints to be met.
654  */
655 static int reallocate_resource(struct resource *root, struct resource *old,
656 			       resource_size_t newsize,
657 			       struct resource_constraint *constraint)
658 {
659 	int err=0;
660 	struct resource new = *old;
661 	struct resource *conflict;
662 
663 	write_lock(&resource_lock);
664 
665 	if ((err = __find_resource(root, old, &new, newsize, constraint)))
666 		goto out;
667 
668 	if (resource_contains(&new, old)) {
669 		old->start = new.start;
670 		old->end = new.end;
671 		goto out;
672 	}
673 
674 	if (old->child) {
675 		err = -EBUSY;
676 		goto out;
677 	}
678 
679 	if (resource_contains(old, &new)) {
680 		old->start = new.start;
681 		old->end = new.end;
682 	} else {
683 		__release_resource(old, true);
684 		*old = new;
685 		conflict = __request_resource(root, old);
686 		BUG_ON(conflict);
687 	}
688 out:
689 	write_unlock(&resource_lock);
690 	return err;
691 }
692 
693 
694 /**
695  * allocate_resource - allocate empty slot in the resource tree given range & alignment.
696  * 	The resource will be reallocated with a new size if it was already allocated
697  * @root: root resource descriptor
698  * @new: resource descriptor desired by caller
699  * @size: requested resource region size
700  * @min: minimum boundary to allocate
701  * @max: maximum boundary to allocate
702  * @align: alignment requested, in bytes
703  * @alignf: alignment function, optional, called if not NULL
704  * @alignf_data: arbitrary data to pass to the @alignf function
705  */
706 int allocate_resource(struct resource *root, struct resource *new,
707 		      resource_size_t size, resource_size_t min,
708 		      resource_size_t max, resource_size_t align,
709 		      resource_size_t (*alignf)(void *,
710 						const struct resource *,
711 						resource_size_t,
712 						resource_size_t),
713 		      void *alignf_data)
714 {
715 	int err;
716 	struct resource_constraint constraint;
717 
718 	if (!alignf)
719 		alignf = simple_align_resource;
720 
721 	constraint.min = min;
722 	constraint.max = max;
723 	constraint.align = align;
724 	constraint.alignf = alignf;
725 	constraint.alignf_data = alignf_data;
726 
727 	if ( new->parent ) {
728 		/* resource is already allocated, try reallocating with
729 		   the new constraints */
730 		return reallocate_resource(root, new, size, &constraint);
731 	}
732 
733 	write_lock(&resource_lock);
734 	err = find_resource(root, new, size, &constraint);
735 	if (err >= 0 && __request_resource(root, new))
736 		err = -EBUSY;
737 	write_unlock(&resource_lock);
738 	return err;
739 }
740 
741 EXPORT_SYMBOL(allocate_resource);
742 
743 /**
744  * lookup_resource - find an existing resource by a resource start address
745  * @root: root resource descriptor
746  * @start: resource start address
747  *
748  * Returns a pointer to the resource if found, NULL otherwise
749  */
750 struct resource *lookup_resource(struct resource *root, resource_size_t start)
751 {
752 	struct resource *res;
753 
754 	read_lock(&resource_lock);
755 	for (res = root->child; res; res = res->sibling) {
756 		if (res->start == start)
757 			break;
758 	}
759 	read_unlock(&resource_lock);
760 
761 	return res;
762 }
763 
764 /*
765  * Insert a resource into the resource tree. If successful, return NULL,
766  * otherwise return the conflicting resource (compare to __request_resource())
767  */
768 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
769 {
770 	struct resource *first, *next;
771 
772 	for (;; parent = first) {
773 		first = __request_resource(parent, new);
774 		if (!first)
775 			return first;
776 
777 		if (first == parent)
778 			return first;
779 		if (WARN_ON(first == new))	/* duplicated insertion */
780 			return first;
781 
782 		if ((first->start > new->start) || (first->end < new->end))
783 			break;
784 		if ((first->start == new->start) && (first->end == new->end))
785 			break;
786 	}
787 
788 	for (next = first; ; next = next->sibling) {
789 		/* Partial overlap? Bad, and unfixable */
790 		if (next->start < new->start || next->end > new->end)
791 			return next;
792 		if (!next->sibling)
793 			break;
794 		if (next->sibling->start > new->end)
795 			break;
796 	}
797 
798 	new->parent = parent;
799 	new->sibling = next->sibling;
800 	new->child = first;
801 
802 	next->sibling = NULL;
803 	for (next = first; next; next = next->sibling)
804 		next->parent = new;
805 
806 	if (parent->child == first) {
807 		parent->child = new;
808 	} else {
809 		next = parent->child;
810 		while (next->sibling != first)
811 			next = next->sibling;
812 		next->sibling = new;
813 	}
814 	return NULL;
815 }
816 
817 /**
818  * insert_resource_conflict - Inserts resource in the resource tree
819  * @parent: parent of the new resource
820  * @new: new resource to insert
821  *
822  * Returns 0 on success, conflict resource if the resource can't be inserted.
823  *
824  * This function is equivalent to request_resource_conflict when no conflict
825  * happens. If a conflict happens, and the conflicting resources
826  * entirely fit within the range of the new resource, then the new
827  * resource is inserted and the conflicting resources become children of
828  * the new resource.
829  *
830  * This function is intended for producers of resources, such as FW modules
831  * and bus drivers.
832  */
833 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
834 {
835 	struct resource *conflict;
836 
837 	write_lock(&resource_lock);
838 	conflict = __insert_resource(parent, new);
839 	write_unlock(&resource_lock);
840 	return conflict;
841 }
842 
843 /**
844  * insert_resource - Inserts a resource in the resource tree
845  * @parent: parent of the new resource
846  * @new: new resource to insert
847  *
848  * Returns 0 on success, -EBUSY if the resource can't be inserted.
849  *
850  * This function is intended for producers of resources, such as FW modules
851  * and bus drivers.
852  */
853 int insert_resource(struct resource *parent, struct resource *new)
854 {
855 	struct resource *conflict;
856 
857 	conflict = insert_resource_conflict(parent, new);
858 	return conflict ? -EBUSY : 0;
859 }
860 EXPORT_SYMBOL_GPL(insert_resource);
861 
862 /**
863  * insert_resource_expand_to_fit - Insert a resource into the resource tree
864  * @root: root resource descriptor
865  * @new: new resource to insert
866  *
867  * Insert a resource into the resource tree, possibly expanding it in order
868  * to make it encompass any conflicting resources.
869  */
870 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
871 {
872 	if (new->parent)
873 		return;
874 
875 	write_lock(&resource_lock);
876 	for (;;) {
877 		struct resource *conflict;
878 
879 		conflict = __insert_resource(root, new);
880 		if (!conflict)
881 			break;
882 		if (conflict == root)
883 			break;
884 
885 		/* Ok, expand resource to cover the conflict, then try again .. */
886 		if (conflict->start < new->start)
887 			new->start = conflict->start;
888 		if (conflict->end > new->end)
889 			new->end = conflict->end;
890 
891 		pr_info("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
892 	}
893 	write_unlock(&resource_lock);
894 }
895 /*
896  * Not for general consumption, only early boot memory map parsing, PCI
897  * resource discovery, and late discovery of CXL resources are expected
898  * to use this interface. The former are built-in and only the latter,
899  * CXL, is a module.
900  */
901 EXPORT_SYMBOL_NS_GPL(insert_resource_expand_to_fit, CXL);
902 
903 /**
904  * remove_resource - Remove a resource in the resource tree
905  * @old: resource to remove
906  *
907  * Returns 0 on success, -EINVAL if the resource is not valid.
908  *
909  * This function removes a resource previously inserted by insert_resource()
910  * or insert_resource_conflict(), and moves the children (if any) up to
911  * where they were before.  insert_resource() and insert_resource_conflict()
912  * insert a new resource, and move any conflicting resources down to the
913  * children of the new resource.
914  *
915  * insert_resource(), insert_resource_conflict() and remove_resource() are
916  * intended for producers of resources, such as FW modules and bus drivers.
917  */
918 int remove_resource(struct resource *old)
919 {
920 	int retval;
921 
922 	write_lock(&resource_lock);
923 	retval = __release_resource(old, false);
924 	write_unlock(&resource_lock);
925 	return retval;
926 }
927 EXPORT_SYMBOL_GPL(remove_resource);
928 
929 static int __adjust_resource(struct resource *res, resource_size_t start,
930 				resource_size_t size)
931 {
932 	struct resource *tmp, *parent = res->parent;
933 	resource_size_t end = start + size - 1;
934 	int result = -EBUSY;
935 
936 	if (!parent)
937 		goto skip;
938 
939 	if ((start < parent->start) || (end > parent->end))
940 		goto out;
941 
942 	if (res->sibling && (res->sibling->start <= end))
943 		goto out;
944 
945 	tmp = parent->child;
946 	if (tmp != res) {
947 		while (tmp->sibling != res)
948 			tmp = tmp->sibling;
949 		if (start <= tmp->end)
950 			goto out;
951 	}
952 
953 skip:
954 	for (tmp = res->child; tmp; tmp = tmp->sibling)
955 		if ((tmp->start < start) || (tmp->end > end))
956 			goto out;
957 
958 	res->start = start;
959 	res->end = end;
960 	result = 0;
961 
962  out:
963 	return result;
964 }
965 
966 /**
967  * adjust_resource - modify a resource's start and size
968  * @res: resource to modify
969  * @start: new start value
970  * @size: new size
971  *
972  * Given an existing resource, change its start and size to match the
973  * arguments.  Returns 0 on success, -EBUSY if it can't fit.
974  * Existing children of the resource are assumed to be immutable.
975  */
976 int adjust_resource(struct resource *res, resource_size_t start,
977 		    resource_size_t size)
978 {
979 	int result;
980 
981 	write_lock(&resource_lock);
982 	result = __adjust_resource(res, start, size);
983 	write_unlock(&resource_lock);
984 	return result;
985 }
986 EXPORT_SYMBOL(adjust_resource);
987 
988 static void __init
989 __reserve_region_with_split(struct resource *root, resource_size_t start,
990 			    resource_size_t end, const char *name)
991 {
992 	struct resource *parent = root;
993 	struct resource *conflict;
994 	struct resource *res = alloc_resource(GFP_ATOMIC);
995 	struct resource *next_res = NULL;
996 	int type = resource_type(root);
997 
998 	if (!res)
999 		return;
1000 
1001 	res->name = name;
1002 	res->start = start;
1003 	res->end = end;
1004 	res->flags = type | IORESOURCE_BUSY;
1005 	res->desc = IORES_DESC_NONE;
1006 
1007 	while (1) {
1008 
1009 		conflict = __request_resource(parent, res);
1010 		if (!conflict) {
1011 			if (!next_res)
1012 				break;
1013 			res = next_res;
1014 			next_res = NULL;
1015 			continue;
1016 		}
1017 
1018 		/* conflict covered whole area */
1019 		if (conflict->start <= res->start &&
1020 				conflict->end >= res->end) {
1021 			free_resource(res);
1022 			WARN_ON(next_res);
1023 			break;
1024 		}
1025 
1026 		/* failed, split and try again */
1027 		if (conflict->start > res->start) {
1028 			end = res->end;
1029 			res->end = conflict->start - 1;
1030 			if (conflict->end < end) {
1031 				next_res = alloc_resource(GFP_ATOMIC);
1032 				if (!next_res) {
1033 					free_resource(res);
1034 					break;
1035 				}
1036 				next_res->name = name;
1037 				next_res->start = conflict->end + 1;
1038 				next_res->end = end;
1039 				next_res->flags = type | IORESOURCE_BUSY;
1040 				next_res->desc = IORES_DESC_NONE;
1041 			}
1042 		} else {
1043 			res->start = conflict->end + 1;
1044 		}
1045 	}
1046 
1047 }
1048 
1049 void __init
1050 reserve_region_with_split(struct resource *root, resource_size_t start,
1051 			  resource_size_t end, const char *name)
1052 {
1053 	int abort = 0;
1054 
1055 	write_lock(&resource_lock);
1056 	if (root->start > start || root->end < end) {
1057 		pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1058 		       (unsigned long long)start, (unsigned long long)end,
1059 		       root);
1060 		if (start > root->end || end < root->start)
1061 			abort = 1;
1062 		else {
1063 			if (end > root->end)
1064 				end = root->end;
1065 			if (start < root->start)
1066 				start = root->start;
1067 			pr_err("fixing request to [0x%llx-0x%llx]\n",
1068 			       (unsigned long long)start,
1069 			       (unsigned long long)end);
1070 		}
1071 		dump_stack();
1072 	}
1073 	if (!abort)
1074 		__reserve_region_with_split(root, start, end, name);
1075 	write_unlock(&resource_lock);
1076 }
1077 
1078 /**
1079  * resource_alignment - calculate resource's alignment
1080  * @res: resource pointer
1081  *
1082  * Returns alignment on success, 0 (invalid alignment) on failure.
1083  */
1084 resource_size_t resource_alignment(struct resource *res)
1085 {
1086 	switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1087 	case IORESOURCE_SIZEALIGN:
1088 		return resource_size(res);
1089 	case IORESOURCE_STARTALIGN:
1090 		return res->start;
1091 	default:
1092 		return 0;
1093 	}
1094 }
1095 
1096 /*
1097  * This is compatibility stuff for IO resources.
1098  *
1099  * Note how this, unlike the above, knows about
1100  * the IO flag meanings (busy etc).
1101  *
1102  * request_region creates a new busy region.
1103  *
1104  * release_region releases a matching busy region.
1105  */
1106 
1107 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1108 
1109 static struct inode *iomem_inode;
1110 
1111 #ifdef CONFIG_IO_STRICT_DEVMEM
1112 static void revoke_iomem(struct resource *res)
1113 {
1114 	/* pairs with smp_store_release() in iomem_init_inode() */
1115 	struct inode *inode = smp_load_acquire(&iomem_inode);
1116 
1117 	/*
1118 	 * Check that the initialization has completed. Losing the race
1119 	 * is ok because it means drivers are claiming resources before
1120 	 * the fs_initcall level of init and prevent iomem_get_mapping users
1121 	 * from establishing mappings.
1122 	 */
1123 	if (!inode)
1124 		return;
1125 
1126 	/*
1127 	 * The expectation is that the driver has successfully marked
1128 	 * the resource busy by this point, so devmem_is_allowed()
1129 	 * should start returning false, however for performance this
1130 	 * does not iterate the entire resource range.
1131 	 */
1132 	if (devmem_is_allowed(PHYS_PFN(res->start)) &&
1133 	    devmem_is_allowed(PHYS_PFN(res->end))) {
1134 		/*
1135 		 * *cringe* iomem=relaxed says "go ahead, what's the
1136 		 * worst that can happen?"
1137 		 */
1138 		return;
1139 	}
1140 
1141 	unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
1142 }
1143 #else
1144 static void revoke_iomem(struct resource *res) {}
1145 #endif
1146 
1147 struct address_space *iomem_get_mapping(void)
1148 {
1149 	/*
1150 	 * This function is only called from file open paths, hence guaranteed
1151 	 * that fs_initcalls have completed and no need to check for NULL. But
1152 	 * since revoke_iomem can be called before the initcall we still need
1153 	 * the barrier to appease checkers.
1154 	 */
1155 	return smp_load_acquire(&iomem_inode)->i_mapping;
1156 }
1157 
1158 static int __request_region_locked(struct resource *res, struct resource *parent,
1159 				   resource_size_t start, resource_size_t n,
1160 				   const char *name, int flags)
1161 {
1162 	DECLARE_WAITQUEUE(wait, current);
1163 
1164 	res->name = name;
1165 	res->start = start;
1166 	res->end = start + n - 1;
1167 
1168 	for (;;) {
1169 		struct resource *conflict;
1170 
1171 		res->flags = resource_type(parent) | resource_ext_type(parent);
1172 		res->flags |= IORESOURCE_BUSY | flags;
1173 		res->desc = parent->desc;
1174 
1175 		conflict = __request_resource(parent, res);
1176 		if (!conflict)
1177 			break;
1178 		/*
1179 		 * mm/hmm.c reserves physical addresses which then
1180 		 * become unavailable to other users.  Conflicts are
1181 		 * not expected.  Warn to aid debugging if encountered.
1182 		 */
1183 		if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1184 			pr_warn("Unaddressable device %s %pR conflicts with %pR",
1185 				conflict->name, conflict, res);
1186 		}
1187 		if (conflict != parent) {
1188 			if (!(conflict->flags & IORESOURCE_BUSY)) {
1189 				parent = conflict;
1190 				continue;
1191 			}
1192 		}
1193 		if (conflict->flags & flags & IORESOURCE_MUXED) {
1194 			add_wait_queue(&muxed_resource_wait, &wait);
1195 			write_unlock(&resource_lock);
1196 			set_current_state(TASK_UNINTERRUPTIBLE);
1197 			schedule();
1198 			remove_wait_queue(&muxed_resource_wait, &wait);
1199 			write_lock(&resource_lock);
1200 			continue;
1201 		}
1202 		/* Uhhuh, that didn't work out.. */
1203 		return -EBUSY;
1204 	}
1205 
1206 	return 0;
1207 }
1208 
1209 /**
1210  * __request_region - create a new busy resource region
1211  * @parent: parent resource descriptor
1212  * @start: resource start address
1213  * @n: resource region size
1214  * @name: reserving caller's ID string
1215  * @flags: IO resource flags
1216  */
1217 struct resource *__request_region(struct resource *parent,
1218 				  resource_size_t start, resource_size_t n,
1219 				  const char *name, int flags)
1220 {
1221 	struct resource *res = alloc_resource(GFP_KERNEL);
1222 	int ret;
1223 
1224 	if (!res)
1225 		return NULL;
1226 
1227 	write_lock(&resource_lock);
1228 	ret = __request_region_locked(res, parent, start, n, name, flags);
1229 	write_unlock(&resource_lock);
1230 
1231 	if (ret) {
1232 		free_resource(res);
1233 		return NULL;
1234 	}
1235 
1236 	if (parent == &iomem_resource)
1237 		revoke_iomem(res);
1238 
1239 	return res;
1240 }
1241 EXPORT_SYMBOL(__request_region);
1242 
1243 /**
1244  * __release_region - release a previously reserved resource region
1245  * @parent: parent resource descriptor
1246  * @start: resource start address
1247  * @n: resource region size
1248  *
1249  * The described resource region must match a currently busy region.
1250  */
1251 void __release_region(struct resource *parent, resource_size_t start,
1252 		      resource_size_t n)
1253 {
1254 	struct resource **p;
1255 	resource_size_t end;
1256 
1257 	p = &parent->child;
1258 	end = start + n - 1;
1259 
1260 	write_lock(&resource_lock);
1261 
1262 	for (;;) {
1263 		struct resource *res = *p;
1264 
1265 		if (!res)
1266 			break;
1267 		if (res->start <= start && res->end >= end) {
1268 			if (!(res->flags & IORESOURCE_BUSY)) {
1269 				p = &res->child;
1270 				continue;
1271 			}
1272 			if (res->start != start || res->end != end)
1273 				break;
1274 			*p = res->sibling;
1275 			write_unlock(&resource_lock);
1276 			if (res->flags & IORESOURCE_MUXED)
1277 				wake_up(&muxed_resource_wait);
1278 			free_resource(res);
1279 			return;
1280 		}
1281 		p = &res->sibling;
1282 	}
1283 
1284 	write_unlock(&resource_lock);
1285 
1286 	pr_warn("Trying to free nonexistent resource <%pa-%pa>\n", &start, &end);
1287 }
1288 EXPORT_SYMBOL(__release_region);
1289 
1290 #ifdef CONFIG_MEMORY_HOTREMOVE
1291 /**
1292  * release_mem_region_adjustable - release a previously reserved memory region
1293  * @start: resource start address
1294  * @size: resource region size
1295  *
1296  * This interface is intended for memory hot-delete.  The requested region
1297  * is released from a currently busy memory resource.  The requested region
1298  * must either match exactly or fit into a single busy resource entry.  In
1299  * the latter case, the remaining resource is adjusted accordingly.
1300  * Existing children of the busy memory resource must be immutable in the
1301  * request.
1302  *
1303  * Note:
1304  * - Additional release conditions, such as overlapping region, can be
1305  *   supported after they are confirmed as valid cases.
1306  * - When a busy memory resource gets split into two entries, the code
1307  *   assumes that all children remain in the lower address entry for
1308  *   simplicity.  Enhance this logic when necessary.
1309  */
1310 void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1311 {
1312 	struct resource *parent = &iomem_resource;
1313 	struct resource *new_res = NULL;
1314 	bool alloc_nofail = false;
1315 	struct resource **p;
1316 	struct resource *res;
1317 	resource_size_t end;
1318 
1319 	end = start + size - 1;
1320 	if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1321 		return;
1322 
1323 	/*
1324 	 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1325 	 * just before releasing the region. This is highly unlikely to
1326 	 * fail - let's play save and make it never fail as the caller cannot
1327 	 * perform any error handling (e.g., trying to re-add memory will fail
1328 	 * similarly).
1329 	 */
1330 retry:
1331 	new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1332 
1333 	p = &parent->child;
1334 	write_lock(&resource_lock);
1335 
1336 	while ((res = *p)) {
1337 		if (res->start >= end)
1338 			break;
1339 
1340 		/* look for the next resource if it does not fit into */
1341 		if (res->start > start || res->end < end) {
1342 			p = &res->sibling;
1343 			continue;
1344 		}
1345 
1346 		if (!(res->flags & IORESOURCE_MEM))
1347 			break;
1348 
1349 		if (!(res->flags & IORESOURCE_BUSY)) {
1350 			p = &res->child;
1351 			continue;
1352 		}
1353 
1354 		/* found the target resource; let's adjust accordingly */
1355 		if (res->start == start && res->end == end) {
1356 			/* free the whole entry */
1357 			*p = res->sibling;
1358 			free_resource(res);
1359 		} else if (res->start == start && res->end != end) {
1360 			/* adjust the start */
1361 			WARN_ON_ONCE(__adjust_resource(res, end + 1,
1362 						       res->end - end));
1363 		} else if (res->start != start && res->end == end) {
1364 			/* adjust the end */
1365 			WARN_ON_ONCE(__adjust_resource(res, res->start,
1366 						       start - res->start));
1367 		} else {
1368 			/* split into two entries - we need a new resource */
1369 			if (!new_res) {
1370 				new_res = alloc_resource(GFP_ATOMIC);
1371 				if (!new_res) {
1372 					alloc_nofail = true;
1373 					write_unlock(&resource_lock);
1374 					goto retry;
1375 				}
1376 			}
1377 			new_res->name = res->name;
1378 			new_res->start = end + 1;
1379 			new_res->end = res->end;
1380 			new_res->flags = res->flags;
1381 			new_res->desc = res->desc;
1382 			new_res->parent = res->parent;
1383 			new_res->sibling = res->sibling;
1384 			new_res->child = NULL;
1385 
1386 			if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1387 							   start - res->start)))
1388 				break;
1389 			res->sibling = new_res;
1390 			new_res = NULL;
1391 		}
1392 
1393 		break;
1394 	}
1395 
1396 	write_unlock(&resource_lock);
1397 	free_resource(new_res);
1398 }
1399 #endif	/* CONFIG_MEMORY_HOTREMOVE */
1400 
1401 #ifdef CONFIG_MEMORY_HOTPLUG
1402 static bool system_ram_resources_mergeable(struct resource *r1,
1403 					   struct resource *r2)
1404 {
1405 	/* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1406 	return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1407 	       r1->name == r2->name && r1->desc == r2->desc &&
1408 	       !r1->child && !r2->child;
1409 }
1410 
1411 /**
1412  * merge_system_ram_resource - mark the System RAM resource mergeable and try to
1413  *	merge it with adjacent, mergeable resources
1414  * @res: resource descriptor
1415  *
1416  * This interface is intended for memory hotplug, whereby lots of contiguous
1417  * system ram resources are added (e.g., via add_memory*()) by a driver, and
1418  * the actual resource boundaries are not of interest (e.g., it might be
1419  * relevant for DIMMs). Only resources that are marked mergeable, that have the
1420  * same parent, and that don't have any children are considered. All mergeable
1421  * resources must be immutable during the request.
1422  *
1423  * Note:
1424  * - The caller has to make sure that no pointers to resources that are
1425  *   marked mergeable are used anymore after this call - the resource might
1426  *   be freed and the pointer might be stale!
1427  * - release_mem_region_adjustable() will split on demand on memory hotunplug
1428  */
1429 void merge_system_ram_resource(struct resource *res)
1430 {
1431 	const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1432 	struct resource *cur;
1433 
1434 	if (WARN_ON_ONCE((res->flags & flags) != flags))
1435 		return;
1436 
1437 	write_lock(&resource_lock);
1438 	res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1439 
1440 	/* Try to merge with next item in the list. */
1441 	cur = res->sibling;
1442 	if (cur && system_ram_resources_mergeable(res, cur)) {
1443 		res->end = cur->end;
1444 		res->sibling = cur->sibling;
1445 		free_resource(cur);
1446 	}
1447 
1448 	/* Try to merge with previous item in the list. */
1449 	cur = res->parent->child;
1450 	while (cur && cur->sibling != res)
1451 		cur = cur->sibling;
1452 	if (cur && system_ram_resources_mergeable(cur, res)) {
1453 		cur->end = res->end;
1454 		cur->sibling = res->sibling;
1455 		free_resource(res);
1456 	}
1457 	write_unlock(&resource_lock);
1458 }
1459 #endif	/* CONFIG_MEMORY_HOTPLUG */
1460 
1461 /*
1462  * Managed region resource
1463  */
1464 static void devm_resource_release(struct device *dev, void *ptr)
1465 {
1466 	struct resource **r = ptr;
1467 
1468 	release_resource(*r);
1469 }
1470 
1471 /**
1472  * devm_request_resource() - request and reserve an I/O or memory resource
1473  * @dev: device for which to request the resource
1474  * @root: root of the resource tree from which to request the resource
1475  * @new: descriptor of the resource to request
1476  *
1477  * This is a device-managed version of request_resource(). There is usually
1478  * no need to release resources requested by this function explicitly since
1479  * that will be taken care of when the device is unbound from its driver.
1480  * If for some reason the resource needs to be released explicitly, because
1481  * of ordering issues for example, drivers must call devm_release_resource()
1482  * rather than the regular release_resource().
1483  *
1484  * When a conflict is detected between any existing resources and the newly
1485  * requested resource, an error message will be printed.
1486  *
1487  * Returns 0 on success or a negative error code on failure.
1488  */
1489 int devm_request_resource(struct device *dev, struct resource *root,
1490 			  struct resource *new)
1491 {
1492 	struct resource *conflict, **ptr;
1493 
1494 	ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1495 	if (!ptr)
1496 		return -ENOMEM;
1497 
1498 	*ptr = new;
1499 
1500 	conflict = request_resource_conflict(root, new);
1501 	if (conflict) {
1502 		dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1503 			new, conflict->name, conflict);
1504 		devres_free(ptr);
1505 		return -EBUSY;
1506 	}
1507 
1508 	devres_add(dev, ptr);
1509 	return 0;
1510 }
1511 EXPORT_SYMBOL(devm_request_resource);
1512 
1513 static int devm_resource_match(struct device *dev, void *res, void *data)
1514 {
1515 	struct resource **ptr = res;
1516 
1517 	return *ptr == data;
1518 }
1519 
1520 /**
1521  * devm_release_resource() - release a previously requested resource
1522  * @dev: device for which to release the resource
1523  * @new: descriptor of the resource to release
1524  *
1525  * Releases a resource previously requested using devm_request_resource().
1526  */
1527 void devm_release_resource(struct device *dev, struct resource *new)
1528 {
1529 	WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1530 			       new));
1531 }
1532 EXPORT_SYMBOL(devm_release_resource);
1533 
1534 struct region_devres {
1535 	struct resource *parent;
1536 	resource_size_t start;
1537 	resource_size_t n;
1538 };
1539 
1540 static void devm_region_release(struct device *dev, void *res)
1541 {
1542 	struct region_devres *this = res;
1543 
1544 	__release_region(this->parent, this->start, this->n);
1545 }
1546 
1547 static int devm_region_match(struct device *dev, void *res, void *match_data)
1548 {
1549 	struct region_devres *this = res, *match = match_data;
1550 
1551 	return this->parent == match->parent &&
1552 		this->start == match->start && this->n == match->n;
1553 }
1554 
1555 struct resource *
1556 __devm_request_region(struct device *dev, struct resource *parent,
1557 		      resource_size_t start, resource_size_t n, const char *name)
1558 {
1559 	struct region_devres *dr = NULL;
1560 	struct resource *res;
1561 
1562 	dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1563 			  GFP_KERNEL);
1564 	if (!dr)
1565 		return NULL;
1566 
1567 	dr->parent = parent;
1568 	dr->start = start;
1569 	dr->n = n;
1570 
1571 	res = __request_region(parent, start, n, name, 0);
1572 	if (res)
1573 		devres_add(dev, dr);
1574 	else
1575 		devres_free(dr);
1576 
1577 	return res;
1578 }
1579 EXPORT_SYMBOL(__devm_request_region);
1580 
1581 void __devm_release_region(struct device *dev, struct resource *parent,
1582 			   resource_size_t start, resource_size_t n)
1583 {
1584 	struct region_devres match_data = { parent, start, n };
1585 
1586 	__release_region(parent, start, n);
1587 	WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1588 			       &match_data));
1589 }
1590 EXPORT_SYMBOL(__devm_release_region);
1591 
1592 /*
1593  * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1594  */
1595 #define MAXRESERVE 4
1596 static int __init reserve_setup(char *str)
1597 {
1598 	static int reserved;
1599 	static struct resource reserve[MAXRESERVE];
1600 
1601 	for (;;) {
1602 		unsigned int io_start, io_num;
1603 		int x = reserved;
1604 		struct resource *parent;
1605 
1606 		if (get_option(&str, &io_start) != 2)
1607 			break;
1608 		if (get_option(&str, &io_num) == 0)
1609 			break;
1610 		if (x < MAXRESERVE) {
1611 			struct resource *res = reserve + x;
1612 
1613 			/*
1614 			 * If the region starts below 0x10000, we assume it's
1615 			 * I/O port space; otherwise assume it's memory.
1616 			 */
1617 			if (io_start < 0x10000) {
1618 				res->flags = IORESOURCE_IO;
1619 				parent = &ioport_resource;
1620 			} else {
1621 				res->flags = IORESOURCE_MEM;
1622 				parent = &iomem_resource;
1623 			}
1624 			res->name = "reserved";
1625 			res->start = io_start;
1626 			res->end = io_start + io_num - 1;
1627 			res->flags |= IORESOURCE_BUSY;
1628 			res->desc = IORES_DESC_NONE;
1629 			res->child = NULL;
1630 			if (request_resource(parent, res) == 0)
1631 				reserved = x+1;
1632 		}
1633 	}
1634 	return 1;
1635 }
1636 __setup("reserve=", reserve_setup);
1637 
1638 /*
1639  * Check if the requested addr and size spans more than any slot in the
1640  * iomem resource tree.
1641  */
1642 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1643 {
1644 	struct resource *p = &iomem_resource;
1645 	resource_size_t end = addr + size - 1;
1646 	int err = 0;
1647 	loff_t l;
1648 
1649 	read_lock(&resource_lock);
1650 	for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1651 		/*
1652 		 * We can probably skip the resources without
1653 		 * IORESOURCE_IO attribute?
1654 		 */
1655 		if (p->start > end)
1656 			continue;
1657 		if (p->end < addr)
1658 			continue;
1659 		if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1660 		    PFN_DOWN(p->end) >= PFN_DOWN(end))
1661 			continue;
1662 		/*
1663 		 * if a resource is "BUSY", it's not a hardware resource
1664 		 * but a driver mapping of such a resource; we don't want
1665 		 * to warn for those; some drivers legitimately map only
1666 		 * partial hardware resources. (example: vesafb)
1667 		 */
1668 		if (p->flags & IORESOURCE_BUSY)
1669 			continue;
1670 
1671 		pr_warn("resource sanity check: requesting [mem %pa-%pa], which spans more than %s %pR\n",
1672 			&addr, &end, p->name, p);
1673 		err = -1;
1674 		break;
1675 	}
1676 	read_unlock(&resource_lock);
1677 
1678 	return err;
1679 }
1680 
1681 #ifdef CONFIG_STRICT_DEVMEM
1682 static int strict_iomem_checks = 1;
1683 #else
1684 static int strict_iomem_checks;
1685 #endif
1686 
1687 /*
1688  * Check if an address is exclusive to the kernel and must not be mapped to
1689  * user space, for example, via /dev/mem.
1690  *
1691  * Returns true if exclusive to the kernel, otherwise returns false.
1692  */
1693 bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size)
1694 {
1695 	const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
1696 						  IORESOURCE_EXCLUSIVE;
1697 	bool skip_children = false, err = false;
1698 	struct resource *p;
1699 
1700 	read_lock(&resource_lock);
1701 	for_each_resource(root, p, skip_children) {
1702 		if (p->start >= addr + size)
1703 			break;
1704 		if (p->end < addr) {
1705 			skip_children = true;
1706 			continue;
1707 		}
1708 		skip_children = false;
1709 
1710 		/*
1711 		 * IORESOURCE_SYSTEM_RAM resources are exclusive if
1712 		 * IORESOURCE_EXCLUSIVE is set, even if they
1713 		 * are not busy and even if "iomem=relaxed" is set. The
1714 		 * responsible driver dynamically adds/removes system RAM within
1715 		 * such an area and uncontrolled access is dangerous.
1716 		 */
1717 		if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
1718 			err = true;
1719 			break;
1720 		}
1721 
1722 		/*
1723 		 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1724 		 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1725 		 * resource is busy.
1726 		 */
1727 		if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
1728 			continue;
1729 		if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1730 				|| p->flags & IORESOURCE_EXCLUSIVE) {
1731 			err = true;
1732 			break;
1733 		}
1734 	}
1735 	read_unlock(&resource_lock);
1736 
1737 	return err;
1738 }
1739 
1740 bool iomem_is_exclusive(u64 addr)
1741 {
1742 	return resource_is_exclusive(&iomem_resource, addr & PAGE_MASK,
1743 				     PAGE_SIZE);
1744 }
1745 
1746 struct resource_entry *resource_list_create_entry(struct resource *res,
1747 						  size_t extra_size)
1748 {
1749 	struct resource_entry *entry;
1750 
1751 	entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1752 	if (entry) {
1753 		INIT_LIST_HEAD(&entry->node);
1754 		entry->res = res ? res : &entry->__res;
1755 	}
1756 
1757 	return entry;
1758 }
1759 EXPORT_SYMBOL(resource_list_create_entry);
1760 
1761 void resource_list_free(struct list_head *head)
1762 {
1763 	struct resource_entry *entry, *tmp;
1764 
1765 	list_for_each_entry_safe(entry, tmp, head, node)
1766 		resource_list_destroy_entry(entry);
1767 }
1768 EXPORT_SYMBOL(resource_list_free);
1769 
1770 #ifdef CONFIG_GET_FREE_REGION
1771 #define GFR_DESCENDING		(1UL << 0)
1772 #define GFR_REQUEST_REGION	(1UL << 1)
1773 #define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT)
1774 
1775 static resource_size_t gfr_start(struct resource *base, resource_size_t size,
1776 				 resource_size_t align, unsigned long flags)
1777 {
1778 	if (flags & GFR_DESCENDING) {
1779 		resource_size_t end;
1780 
1781 		end = min_t(resource_size_t, base->end,
1782 			    (1ULL << MAX_PHYSMEM_BITS) - 1);
1783 		return end - size + 1;
1784 	}
1785 
1786 	return ALIGN(base->start, align);
1787 }
1788 
1789 static bool gfr_continue(struct resource *base, resource_size_t addr,
1790 			 resource_size_t size, unsigned long flags)
1791 {
1792 	if (flags & GFR_DESCENDING)
1793 		return addr > size && addr >= base->start;
1794 	/*
1795 	 * In the ascend case be careful that the last increment by
1796 	 * @size did not wrap 0.
1797 	 */
1798 	return addr > addr - size &&
1799 	       addr <= min_t(resource_size_t, base->end,
1800 			     (1ULL << MAX_PHYSMEM_BITS) - 1);
1801 }
1802 
1803 static resource_size_t gfr_next(resource_size_t addr, resource_size_t size,
1804 				unsigned long flags)
1805 {
1806 	if (flags & GFR_DESCENDING)
1807 		return addr - size;
1808 	return addr + size;
1809 }
1810 
1811 static void remove_free_mem_region(void *_res)
1812 {
1813 	struct resource *res = _res;
1814 
1815 	if (res->parent)
1816 		remove_resource(res);
1817 	free_resource(res);
1818 }
1819 
1820 static struct resource *
1821 get_free_mem_region(struct device *dev, struct resource *base,
1822 		    resource_size_t size, const unsigned long align,
1823 		    const char *name, const unsigned long desc,
1824 		    const unsigned long flags)
1825 {
1826 	resource_size_t addr;
1827 	struct resource *res;
1828 	struct region_devres *dr = NULL;
1829 
1830 	size = ALIGN(size, align);
1831 
1832 	res = alloc_resource(GFP_KERNEL);
1833 	if (!res)
1834 		return ERR_PTR(-ENOMEM);
1835 
1836 	if (dev && (flags & GFR_REQUEST_REGION)) {
1837 		dr = devres_alloc(devm_region_release,
1838 				sizeof(struct region_devres), GFP_KERNEL);
1839 		if (!dr) {
1840 			free_resource(res);
1841 			return ERR_PTR(-ENOMEM);
1842 		}
1843 	} else if (dev) {
1844 		if (devm_add_action_or_reset(dev, remove_free_mem_region, res))
1845 			return ERR_PTR(-ENOMEM);
1846 	}
1847 
1848 	write_lock(&resource_lock);
1849 	for (addr = gfr_start(base, size, align, flags);
1850 	     gfr_continue(base, addr, size, flags);
1851 	     addr = gfr_next(addr, size, flags)) {
1852 		if (__region_intersects(base, addr, size, 0, IORES_DESC_NONE) !=
1853 		    REGION_DISJOINT)
1854 			continue;
1855 
1856 		if (flags & GFR_REQUEST_REGION) {
1857 			if (__request_region_locked(res, &iomem_resource, addr,
1858 						    size, name, 0))
1859 				break;
1860 
1861 			if (dev) {
1862 				dr->parent = &iomem_resource;
1863 				dr->start = addr;
1864 				dr->n = size;
1865 				devres_add(dev, dr);
1866 			}
1867 
1868 			res->desc = desc;
1869 			write_unlock(&resource_lock);
1870 
1871 
1872 			/*
1873 			 * A driver is claiming this region so revoke any
1874 			 * mappings.
1875 			 */
1876 			revoke_iomem(res);
1877 		} else {
1878 			res->start = addr;
1879 			res->end = addr + size - 1;
1880 			res->name = name;
1881 			res->desc = desc;
1882 			res->flags = IORESOURCE_MEM;
1883 
1884 			/*
1885 			 * Only succeed if the resource hosts an exclusive
1886 			 * range after the insert
1887 			 */
1888 			if (__insert_resource(base, res) || res->child)
1889 				break;
1890 
1891 			write_unlock(&resource_lock);
1892 		}
1893 
1894 		return res;
1895 	}
1896 	write_unlock(&resource_lock);
1897 
1898 	if (flags & GFR_REQUEST_REGION) {
1899 		free_resource(res);
1900 		devres_free(dr);
1901 	} else if (dev)
1902 		devm_release_action(dev, remove_free_mem_region, res);
1903 
1904 	return ERR_PTR(-ERANGE);
1905 }
1906 
1907 /**
1908  * devm_request_free_mem_region - find free region for device private memory
1909  *
1910  * @dev: device struct to bind the resource to
1911  * @size: size in bytes of the device memory to add
1912  * @base: resource tree to look in
1913  *
1914  * This function tries to find an empty range of physical address big enough to
1915  * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
1916  * memory, which in turn allocates struct pages.
1917  */
1918 struct resource *devm_request_free_mem_region(struct device *dev,
1919 		struct resource *base, unsigned long size)
1920 {
1921 	unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
1922 
1923 	return get_free_mem_region(dev, base, size, GFR_DEFAULT_ALIGN,
1924 				   dev_name(dev),
1925 				   IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
1926 }
1927 EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
1928 
1929 struct resource *request_free_mem_region(struct resource *base,
1930 		unsigned long size, const char *name)
1931 {
1932 	unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
1933 
1934 	return get_free_mem_region(NULL, base, size, GFR_DEFAULT_ALIGN, name,
1935 				   IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
1936 }
1937 EXPORT_SYMBOL_GPL(request_free_mem_region);
1938 
1939 /**
1940  * alloc_free_mem_region - find a free region relative to @base
1941  * @base: resource that will parent the new resource
1942  * @size: size in bytes of memory to allocate from @base
1943  * @align: alignment requirements for the allocation
1944  * @name: resource name
1945  *
1946  * Buses like CXL, that can dynamically instantiate new memory regions,
1947  * need a method to allocate physical address space for those regions.
1948  * Allocate and insert a new resource to cover a free, unclaimed by a
1949  * descendant of @base, range in the span of @base.
1950  */
1951 struct resource *alloc_free_mem_region(struct resource *base,
1952 				       unsigned long size, unsigned long align,
1953 				       const char *name)
1954 {
1955 	/* Default of ascending direction and insert resource */
1956 	unsigned long flags = 0;
1957 
1958 	return get_free_mem_region(NULL, base, size, align, name,
1959 				   IORES_DESC_NONE, flags);
1960 }
1961 EXPORT_SYMBOL_NS_GPL(alloc_free_mem_region, CXL);
1962 #endif /* CONFIG_GET_FREE_REGION */
1963 
1964 static int __init strict_iomem(char *str)
1965 {
1966 	if (strstr(str, "relaxed"))
1967 		strict_iomem_checks = 0;
1968 	if (strstr(str, "strict"))
1969 		strict_iomem_checks = 1;
1970 	return 1;
1971 }
1972 
1973 static int iomem_fs_init_fs_context(struct fs_context *fc)
1974 {
1975 	return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
1976 }
1977 
1978 static struct file_system_type iomem_fs_type = {
1979 	.name		= "iomem",
1980 	.owner		= THIS_MODULE,
1981 	.init_fs_context = iomem_fs_init_fs_context,
1982 	.kill_sb	= kill_anon_super,
1983 };
1984 
1985 static int __init iomem_init_inode(void)
1986 {
1987 	static struct vfsmount *iomem_vfs_mount;
1988 	static int iomem_fs_cnt;
1989 	struct inode *inode;
1990 	int rc;
1991 
1992 	rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
1993 	if (rc < 0) {
1994 		pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
1995 		return rc;
1996 	}
1997 
1998 	inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
1999 	if (IS_ERR(inode)) {
2000 		rc = PTR_ERR(inode);
2001 		pr_err("Cannot allocate inode for iomem: %d\n", rc);
2002 		simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
2003 		return rc;
2004 	}
2005 
2006 	/*
2007 	 * Publish iomem revocation inode initialized.
2008 	 * Pairs with smp_load_acquire() in revoke_iomem().
2009 	 */
2010 	smp_store_release(&iomem_inode, inode);
2011 
2012 	return 0;
2013 }
2014 
2015 fs_initcall(iomem_init_inode);
2016 
2017 __setup("iomem=", strict_iomem);
2018