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