xref: /openbmc/linux/kernel/resource.c (revision e4a5b2f6)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	linux/kernel/resource.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1999	Linus Torvalds
61da177e4SLinus Torvalds  * Copyright (C) 1999	Martin Mares <mj@ucw.cz>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Arbitrary resource management.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
1165fed8f6SOctavian Purdila #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1265fed8f6SOctavian Purdila 
139984de1aSPaul Gortmaker #include <linux/export.h>
141da177e4SLinus Torvalds #include <linux/errno.h>
151da177e4SLinus Torvalds #include <linux/ioport.h>
161da177e4SLinus Torvalds #include <linux/init.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/spinlock.h>
191da177e4SLinus Torvalds #include <linux/fs.h>
201da177e4SLinus Torvalds #include <linux/proc_fs.h>
2171a1d8edSDaniel Vetter #include <linux/pseudo_fs.h>
228b6d043bSAlan Cox #include <linux/sched.h>
231da177e4SLinus Torvalds #include <linux/seq_file.h>
249ac7849eSTejun Heo #include <linux/device.h>
25d68612b2SSuresh Siddha #include <linux/pfn.h>
26ebff7d8fSYasuaki Ishimatsu #include <linux/mm.h>
2771a1d8edSDaniel Vetter #include <linux/mount.h>
2890e97820SJiang Liu #include <linux/resource_ext.h>
2971a1d8edSDaniel Vetter #include <uapi/linux/magic.h>
301da177e4SLinus Torvalds #include <asm/io.h>
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds struct resource ioport_resource = {
341da177e4SLinus Torvalds 	.name	= "PCI IO",
356550e07fSGreg Kroah-Hartman 	.start	= 0,
361da177e4SLinus Torvalds 	.end	= IO_SPACE_LIMIT,
371da177e4SLinus Torvalds 	.flags	= IORESOURCE_IO,
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds EXPORT_SYMBOL(ioport_resource);
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds struct resource iomem_resource = {
421da177e4SLinus Torvalds 	.name	= "PCI mem",
436550e07fSGreg Kroah-Hartman 	.start	= 0,
446550e07fSGreg Kroah-Hartman 	.end	= -1,
451da177e4SLinus Torvalds 	.flags	= IORESOURCE_MEM,
461da177e4SLinus Torvalds };
471da177e4SLinus Torvalds EXPORT_SYMBOL(iomem_resource);
481da177e4SLinus Torvalds 
4923c570a6SRam Pai /* constraints to be met while allocating resources */
5023c570a6SRam Pai struct resource_constraint {
5123c570a6SRam Pai 	resource_size_t min, max, align;
5223c570a6SRam Pai 	resource_size_t (*alignf)(void *, const struct resource *,
5323c570a6SRam Pai 			resource_size_t, resource_size_t);
5423c570a6SRam Pai 	void *alignf_data;
5523c570a6SRam Pai };
5623c570a6SRam Pai 
571da177e4SLinus Torvalds static DEFINE_RWLOCK(resource_lock);
581da177e4SLinus Torvalds 
next_resource(struct resource * p)5997523a4eSDavid Hildenbrand static struct resource *next_resource(struct resource *p)
601da177e4SLinus Torvalds {
611da177e4SLinus Torvalds 	if (p->child)
621da177e4SLinus Torvalds 		return p->child;
631da177e4SLinus Torvalds 	while (!p->sibling && p->parent)
641da177e4SLinus Torvalds 		p = p->parent;
651da177e4SLinus Torvalds 	return p->sibling;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds 
next_resource_skip_children(struct resource * p)68b78dfa05SDavid Hildenbrand static struct resource *next_resource_skip_children(struct resource *p)
69b78dfa05SDavid Hildenbrand {
70b78dfa05SDavid Hildenbrand 	while (!p->sibling && p->parent)
71b78dfa05SDavid Hildenbrand 		p = p->parent;
72b78dfa05SDavid Hildenbrand 	return p->sibling;
73b78dfa05SDavid Hildenbrand }
74b78dfa05SDavid Hildenbrand 
75b78dfa05SDavid Hildenbrand #define for_each_resource(_root, _p, _skip_children) \
76b78dfa05SDavid Hildenbrand 	for ((_p) = (_root)->child; (_p); \
77b78dfa05SDavid Hildenbrand 	     (_p) = (_skip_children) ? next_resource_skip_children(_p) : \
78b78dfa05SDavid Hildenbrand 				       next_resource(_p))
79b78dfa05SDavid Hildenbrand 
r_next(struct seq_file * m,void * v,loff_t * pos)808c86e70aSVivek Goyal static void *r_next(struct seq_file *m, void *v, loff_t *pos)
818c86e70aSVivek Goyal {
828c86e70aSVivek Goyal 	struct resource *p = v;
838c86e70aSVivek Goyal 	(*pos)++;
8497523a4eSDavid Hildenbrand 	return (void *)next_resource(p);
858c86e70aSVivek Goyal }
868c86e70aSVivek Goyal 
8713eb8375SIngo Molnar #ifdef CONFIG_PROC_FS
8813eb8375SIngo Molnar 
8913eb8375SIngo Molnar enum { MAX_IORES_LEVEL = 5 };
9013eb8375SIngo Molnar 
r_start(struct seq_file * m,loff_t * pos)911da177e4SLinus Torvalds static void *r_start(struct seq_file *m, loff_t *pos)
921da177e4SLinus Torvalds 	__acquires(resource_lock)
931da177e4SLinus Torvalds {
94359745d7SMuchun Song 	struct resource *p = pde_data(file_inode(m->file));
951da177e4SLinus Torvalds 	loff_t l = 0;
961da177e4SLinus Torvalds 	read_lock(&resource_lock);
971da177e4SLinus Torvalds 	for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
981da177e4SLinus Torvalds 		;
991da177e4SLinus Torvalds 	return p;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
r_stop(struct seq_file * m,void * v)1021da177e4SLinus Torvalds static void r_stop(struct seq_file *m, void *v)
1031da177e4SLinus Torvalds 	__releases(resource_lock)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	read_unlock(&resource_lock);
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
r_show(struct seq_file * m,void * v)1081da177e4SLinus Torvalds static int r_show(struct seq_file *m, void *v)
1091da177e4SLinus Torvalds {
110359745d7SMuchun Song 	struct resource *root = pde_data(file_inode(m->file));
1111da177e4SLinus Torvalds 	struct resource *r = v, *p;
11251d7b120SLinus Torvalds 	unsigned long long start, end;
1131da177e4SLinus Torvalds 	int width = root->end < 0x10000 ? 4 : 8;
1141da177e4SLinus Torvalds 	int depth;
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
1171da177e4SLinus Torvalds 		if (p->parent == root)
1181da177e4SLinus Torvalds 			break;
11951d7b120SLinus Torvalds 
12051d7b120SLinus Torvalds 	if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
12151d7b120SLinus Torvalds 		start = r->start;
12251d7b120SLinus Torvalds 		end = r->end;
12351d7b120SLinus Torvalds 	} else {
12451d7b120SLinus Torvalds 		start = end = 0;
12551d7b120SLinus Torvalds 	}
12651d7b120SLinus Torvalds 
127685143acSGreg Kroah-Hartman 	seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
1281da177e4SLinus Torvalds 			depth * 2, "",
12951d7b120SLinus Torvalds 			width, start,
13051d7b120SLinus Torvalds 			width, end,
1311da177e4SLinus Torvalds 			r->name ? r->name : "<BAD>");
1321da177e4SLinus Torvalds 	return 0;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
13515ad7cdcSHelge Deller static const struct seq_operations resource_op = {
1361da177e4SLinus Torvalds 	.start	= r_start,
1371da177e4SLinus Torvalds 	.next	= r_next,
1381da177e4SLinus Torvalds 	.stop	= r_stop,
1391da177e4SLinus Torvalds 	.show	= r_show,
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds 
ioresources_init(void)1421da177e4SLinus Torvalds static int __init ioresources_init(void)
1431da177e4SLinus Torvalds {
1444e292a96SChristoph Hellwig 	proc_create_seq_data("ioports", 0, NULL, &resource_op,
1454e292a96SChristoph Hellwig 			&ioport_resource);
1464e292a96SChristoph Hellwig 	proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
1471da177e4SLinus Torvalds 	return 0;
1481da177e4SLinus Torvalds }
1491da177e4SLinus Torvalds __initcall(ioresources_init);
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds #endif /* CONFIG_PROC_FS */
1521da177e4SLinus Torvalds 
free_resource(struct resource * res)153ebff7d8fSYasuaki Ishimatsu static void free_resource(struct resource *res)
154ebff7d8fSYasuaki Ishimatsu {
1550cbcc929SMiaohe Lin 	/**
1560cbcc929SMiaohe Lin 	 * If the resource was allocated using memblock early during boot
1570cbcc929SMiaohe Lin 	 * we'll leak it here: we can only return full pages back to the
1580cbcc929SMiaohe Lin 	 * buddy and trying to be smart and reusing them eventually in
1590cbcc929SMiaohe Lin 	 * alloc_resource() overcomplicates resource handling.
1600cbcc929SMiaohe Lin 	 */
1610cbcc929SMiaohe Lin 	if (res && PageSlab(virt_to_head_page(res)))
162ebff7d8fSYasuaki Ishimatsu 		kfree(res);
163ebff7d8fSYasuaki Ishimatsu }
164ebff7d8fSYasuaki Ishimatsu 
alloc_resource(gfp_t flags)165ebff7d8fSYasuaki Ishimatsu static struct resource *alloc_resource(gfp_t flags)
166ebff7d8fSYasuaki Ishimatsu {
1670cbcc929SMiaohe Lin 	return kzalloc(sizeof(struct resource), flags);
168ebff7d8fSYasuaki Ishimatsu }
169ebff7d8fSYasuaki Ishimatsu 
1701da177e4SLinus Torvalds /* Return the conflict entry if you can't request it */
__request_resource(struct resource * root,struct resource * new)1711da177e4SLinus Torvalds static struct resource * __request_resource(struct resource *root, struct resource *new)
1721da177e4SLinus Torvalds {
173d75fc8bbSGreg Kroah-Hartman 	resource_size_t start = new->start;
174d75fc8bbSGreg Kroah-Hartman 	resource_size_t end = new->end;
1751da177e4SLinus Torvalds 	struct resource *tmp, **p;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (end < start)
1781da177e4SLinus Torvalds 		return root;
1791da177e4SLinus Torvalds 	if (start < root->start)
1801da177e4SLinus Torvalds 		return root;
1811da177e4SLinus Torvalds 	if (end > root->end)
1821da177e4SLinus Torvalds 		return root;
1831da177e4SLinus Torvalds 	p = &root->child;
1841da177e4SLinus Torvalds 	for (;;) {
1851da177e4SLinus Torvalds 		tmp = *p;
1861da177e4SLinus Torvalds 		if (!tmp || tmp->start > end) {
1871da177e4SLinus Torvalds 			new->sibling = tmp;
1881da177e4SLinus Torvalds 			*p = new;
1891da177e4SLinus Torvalds 			new->parent = root;
1901da177e4SLinus Torvalds 			return NULL;
1911da177e4SLinus Torvalds 		}
1921da177e4SLinus Torvalds 		p = &tmp->sibling;
1931da177e4SLinus Torvalds 		if (tmp->end < start)
1941da177e4SLinus Torvalds 			continue;
1951da177e4SLinus Torvalds 		return tmp;
1961da177e4SLinus Torvalds 	}
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
__release_resource(struct resource * old,bool release_child)199ff3cc952SToshi Kani static int __release_resource(struct resource *old, bool release_child)
2001da177e4SLinus Torvalds {
201ff3cc952SToshi Kani 	struct resource *tmp, **p, *chd;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	p = &old->parent->child;
2041da177e4SLinus Torvalds 	for (;;) {
2051da177e4SLinus Torvalds 		tmp = *p;
2061da177e4SLinus Torvalds 		if (!tmp)
2071da177e4SLinus Torvalds 			break;
2081da177e4SLinus Torvalds 		if (tmp == old) {
209ff3cc952SToshi Kani 			if (release_child || !(tmp->child)) {
2101da177e4SLinus Torvalds 				*p = tmp->sibling;
211ff3cc952SToshi Kani 			} else {
212ff3cc952SToshi Kani 				for (chd = tmp->child;; chd = chd->sibling) {
213ff3cc952SToshi Kani 					chd->parent = tmp->parent;
214ff3cc952SToshi Kani 					if (!(chd->sibling))
215ff3cc952SToshi Kani 						break;
216ff3cc952SToshi Kani 				}
217ff3cc952SToshi Kani 				*p = tmp->child;
218ff3cc952SToshi Kani 				chd->sibling = tmp->sibling;
219ff3cc952SToshi Kani 			}
2201da177e4SLinus Torvalds 			old->parent = NULL;
2211da177e4SLinus Torvalds 			return 0;
2221da177e4SLinus Torvalds 		}
2231da177e4SLinus Torvalds 		p = &tmp->sibling;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 	return -EINVAL;
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
__release_child_resources(struct resource * r)2285eeec0ecSYinghai Lu static void __release_child_resources(struct resource *r)
2295eeec0ecSYinghai Lu {
2305eeec0ecSYinghai Lu 	struct resource *tmp, *p;
2315eeec0ecSYinghai Lu 	resource_size_t size;
2325eeec0ecSYinghai Lu 
2335eeec0ecSYinghai Lu 	p = r->child;
2345eeec0ecSYinghai Lu 	r->child = NULL;
2355eeec0ecSYinghai Lu 	while (p) {
2365eeec0ecSYinghai Lu 		tmp = p;
2375eeec0ecSYinghai Lu 		p = p->sibling;
2385eeec0ecSYinghai Lu 
2395eeec0ecSYinghai Lu 		tmp->parent = NULL;
2405eeec0ecSYinghai Lu 		tmp->sibling = NULL;
2415eeec0ecSYinghai Lu 		__release_child_resources(tmp);
2425eeec0ecSYinghai Lu 
2435eeec0ecSYinghai Lu 		printk(KERN_DEBUG "release child resource %pR\n", tmp);
2445eeec0ecSYinghai Lu 		/* need to restore size, and keep flags */
2455eeec0ecSYinghai Lu 		size = resource_size(tmp);
2465eeec0ecSYinghai Lu 		tmp->start = 0;
2475eeec0ecSYinghai Lu 		tmp->end = size - 1;
2485eeec0ecSYinghai Lu 	}
2495eeec0ecSYinghai Lu }
2505eeec0ecSYinghai Lu 
release_child_resources(struct resource * r)2515eeec0ecSYinghai Lu void release_child_resources(struct resource *r)
2525eeec0ecSYinghai Lu {
2535eeec0ecSYinghai Lu 	write_lock(&resource_lock);
2545eeec0ecSYinghai Lu 	__release_child_resources(r);
2555eeec0ecSYinghai Lu 	write_unlock(&resource_lock);
2565eeec0ecSYinghai Lu }
2575eeec0ecSYinghai Lu 
258e1ca66d1SRandy Dunlap /**
25966f1207bSBjorn Helgaas  * request_resource_conflict - request and reserve an I/O or memory resource
26066f1207bSBjorn Helgaas  * @root: root resource descriptor
26166f1207bSBjorn Helgaas  * @new: resource descriptor desired by caller
26266f1207bSBjorn Helgaas  *
26366f1207bSBjorn Helgaas  * Returns 0 for success, conflict resource on error.
26466f1207bSBjorn Helgaas  */
request_resource_conflict(struct resource * root,struct resource * new)26566f1207bSBjorn Helgaas struct resource *request_resource_conflict(struct resource *root, struct resource *new)
26666f1207bSBjorn Helgaas {
26766f1207bSBjorn Helgaas 	struct resource *conflict;
26866f1207bSBjorn Helgaas 
26966f1207bSBjorn Helgaas 	write_lock(&resource_lock);
27066f1207bSBjorn Helgaas 	conflict = __request_resource(root, new);
27166f1207bSBjorn Helgaas 	write_unlock(&resource_lock);
27266f1207bSBjorn Helgaas 	return conflict;
27366f1207bSBjorn Helgaas }
27466f1207bSBjorn Helgaas 
27566f1207bSBjorn Helgaas /**
276e1ca66d1SRandy Dunlap  * request_resource - request and reserve an I/O or memory resource
277e1ca66d1SRandy Dunlap  * @root: root resource descriptor
278e1ca66d1SRandy Dunlap  * @new: resource descriptor desired by caller
279e1ca66d1SRandy Dunlap  *
280e1ca66d1SRandy Dunlap  * Returns 0 for success, negative error code on error.
281e1ca66d1SRandy Dunlap  */
request_resource(struct resource * root,struct resource * new)2821da177e4SLinus Torvalds int request_resource(struct resource *root, struct resource *new)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	struct resource *conflict;
2851da177e4SLinus Torvalds 
28666f1207bSBjorn Helgaas 	conflict = request_resource_conflict(root, new);
2871da177e4SLinus Torvalds 	return conflict ? -EBUSY : 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds EXPORT_SYMBOL(request_resource);
2911da177e4SLinus Torvalds 
292e1ca66d1SRandy Dunlap /**
293e1ca66d1SRandy Dunlap  * release_resource - release a previously reserved resource
294e1ca66d1SRandy Dunlap  * @old: resource pointer
295e1ca66d1SRandy Dunlap  */
release_resource(struct resource * old)2961da177e4SLinus Torvalds int release_resource(struct resource *old)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	int retval;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 	write_lock(&resource_lock);
301ff3cc952SToshi Kani 	retval = __release_resource(old, true);
3021da177e4SLinus Torvalds 	write_unlock(&resource_lock);
3031da177e4SLinus Torvalds 	return retval;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds EXPORT_SYMBOL(release_resource);
3071da177e4SLinus Torvalds 
308f26621e6SBorislav Petkov /**
3093be8da57SMauro Carvalho Chehab  * find_next_iomem_res - Finds the lowest iomem resource that covers part of
3103be8da57SMauro Carvalho Chehab  *			 [@start..@end].
311010a93bfSBjorn Helgaas  *
312f26621e6SBorislav Petkov  * If a resource is found, returns 0 and @*res is overwritten with the part
313f26621e6SBorislav Petkov  * of the resource that's within [@start..@end]; if none is found, returns
31449f17c26SNadav Amit  * -ENODEV.  Returns -EINVAL for invalid parameters.
315010a93bfSBjorn Helgaas  *
316f26621e6SBorislav Petkov  * @start:	start address of the resource searched for
317f26621e6SBorislav Petkov  * @end:	end address of same resource
318f26621e6SBorislav Petkov  * @flags:	flags which the resource must have
319f26621e6SBorislav Petkov  * @desc:	descriptor the resource must have
320f26621e6SBorislav Petkov  * @res:	return ptr, if resource found
3213be8da57SMauro Carvalho Chehab  *
3223be8da57SMauro Carvalho Chehab  * The caller must specify @start, @end, @flags, and @desc
3233be8da57SMauro Carvalho Chehab  * (which may be IORES_DESC_NONE).
3242842f114SKAMEZAWA Hiroyuki  */
find_next_iomem_res(resource_size_t start,resource_size_t end,unsigned long flags,unsigned long desc,struct resource * res)325010a93bfSBjorn Helgaas static int find_next_iomem_res(resource_size_t start, resource_size_t end,
326010a93bfSBjorn Helgaas 			       unsigned long flags, unsigned long desc,
32797523a4eSDavid Hildenbrand 			       struct resource *res)
3282842f114SKAMEZAWA Hiroyuki {
3292842f114SKAMEZAWA Hiroyuki 	struct resource *p;
3302842f114SKAMEZAWA Hiroyuki 
331b69c2e20SBorislav Petkov 	if (!res)
332b69c2e20SBorislav Petkov 		return -EINVAL;
3332842f114SKAMEZAWA Hiroyuki 
334b69c2e20SBorislav Petkov 	if (start >= end)
335b69c2e20SBorislav Petkov 		return -EINVAL;
336800df627SVivek Goyal 
3372842f114SKAMEZAWA Hiroyuki 	read_lock(&resource_lock);
3388c86e70aSVivek Goyal 
33997523a4eSDavid Hildenbrand 	for (p = iomem_resource.child; p; p = next_resource(p)) {
34075639875SNadav Amit 		/* If we passed the resource we are looking for, stop */
3412842f114SKAMEZAWA Hiroyuki 		if (p->start > end) {
3422842f114SKAMEZAWA Hiroyuki 			p = NULL;
3432842f114SKAMEZAWA Hiroyuki 			break;
3442842f114SKAMEZAWA Hiroyuki 		}
34575639875SNadav Amit 
34675639875SNadav Amit 		/* Skip until we find a range that matches what we look for */
34775639875SNadav Amit 		if (p->end < start)
34875639875SNadav Amit 			continue;
34975639875SNadav Amit 
35075639875SNadav Amit 		if ((p->flags & flags) != flags)
35175639875SNadav Amit 			continue;
35275639875SNadav Amit 		if ((desc != IORES_DESC_NONE) && (desc != p->desc))
35375639875SNadav Amit 			continue;
35475639875SNadav Amit 
35575639875SNadav Amit 		/* Found a match, break */
3562842f114SKAMEZAWA Hiroyuki 		break;
3572842f114SKAMEZAWA Hiroyuki 	}
3588c86e70aSVivek Goyal 
35949f17c26SNadav Amit 	if (p) {
3602842f114SKAMEZAWA Hiroyuki 		/* copy data */
36173fb952dSDan Williams 		*res = (struct resource) {
36273fb952dSDan Williams 			.start = max(start, p->start),
36373fb952dSDan Williams 			.end = min(end, p->end),
36473fb952dSDan Williams 			.flags = p->flags,
36573fb952dSDan Williams 			.desc = p->desc,
36673fb952dSDan Williams 			.parent = p->parent,
36773fb952dSDan Williams 		};
36849f17c26SNadav Amit 	}
36949f17c26SNadav Amit 
37049f17c26SNadav Amit 	read_unlock(&resource_lock);
37149f17c26SNadav Amit 	return p ? 0 : -ENODEV;
3722842f114SKAMEZAWA Hiroyuki }
373908eedc6SKAMEZAWA Hiroyuki 
__walk_iomem_res_desc(resource_size_t start,resource_size_t end,unsigned long flags,unsigned long desc,void * arg,int (* func)(struct resource *,void *))374010a93bfSBjorn Helgaas static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
375010a93bfSBjorn Helgaas 				 unsigned long flags, unsigned long desc,
37697523a4eSDavid Hildenbrand 				 void *arg,
3771d2e733bSTom Lendacky 				 int (*func)(struct resource *, void *))
3784ac2aed8STom Lendacky {
379010a93bfSBjorn Helgaas 	struct resource res;
3805cd401acSDave Hansen 	int ret = -EINVAL;
3814ac2aed8STom Lendacky 
382010a93bfSBjorn Helgaas 	while (start < end &&
38397523a4eSDavid Hildenbrand 	       !find_next_iomem_res(start, end, flags, desc, &res)) {
384010a93bfSBjorn Helgaas 		ret = (*func)(&res, arg);
3854ac2aed8STom Lendacky 		if (ret)
3864ac2aed8STom Lendacky 			break;
3874ac2aed8STom Lendacky 
388010a93bfSBjorn Helgaas 		start = res.end + 1;
3894ac2aed8STom Lendacky 	}
3904ac2aed8STom Lendacky 
3914ac2aed8STom Lendacky 	return ret;
3924ac2aed8STom Lendacky }
3934ac2aed8STom Lendacky 
394b69c2e20SBorislav Petkov /**
3953be8da57SMauro Carvalho Chehab  * walk_iomem_res_desc - Walks through iomem resources and calls func()
3963be8da57SMauro Carvalho Chehab  *			 with matching resource ranges.
3973be8da57SMauro Carvalho Chehab  * *
3983f33647cSToshi Kani  * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
3993f33647cSToshi Kani  * @flags: I/O resource flags
4003f33647cSToshi Kani  * @start: start addr
4013f33647cSToshi Kani  * @end: end addr
402f75d6515SRandy Dunlap  * @arg: function argument for the callback @func
403f75d6515SRandy Dunlap  * @func: callback function that is called for each qualifying resource area
4043f33647cSToshi Kani  *
4053be8da57SMauro Carvalho Chehab  * All the memory ranges which overlap start,end and also match flags and
4063be8da57SMauro Carvalho Chehab  * desc are valid candidates.
4073be8da57SMauro Carvalho Chehab  *
4083f33647cSToshi Kani  * NOTE: For a new descriptor search, define a new IORES_DESC in
4093f33647cSToshi Kani  * <linux/ioport.h> and set it in 'desc' of a target resource entry.
4103f33647cSToshi Kani  */
walk_iomem_res_desc(unsigned long desc,unsigned long flags,u64 start,u64 end,void * arg,int (* func)(struct resource *,void *))4113f33647cSToshi Kani int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
4121d2e733bSTom Lendacky 		u64 end, void *arg, int (*func)(struct resource *, void *))
4133f33647cSToshi Kani {
41497523a4eSDavid Hildenbrand 	return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
4153f33647cSToshi Kani }
416d76401adSDan Williams EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
4173f33647cSToshi Kani 
4183f33647cSToshi Kani /*
419bd7e6cb3SToshi Kani  * This function calls the @func callback against all memory ranges of type
420bd7e6cb3SToshi Kani  * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
421bd7e6cb3SToshi Kani  * Now, this function is only for System RAM, it deals with full ranges and
422bd7e6cb3SToshi Kani  * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
423bd7e6cb3SToshi Kani  * ranges.
4248c86e70aSVivek Goyal  */
walk_system_ram_res(u64 start,u64 end,void * arg,int (* func)(struct resource *,void *))4258c86e70aSVivek Goyal int walk_system_ram_res(u64 start, u64 end, void *arg,
4261d2e733bSTom Lendacky 			int (*func)(struct resource *, void *))
4278c86e70aSVivek Goyal {
428010a93bfSBjorn Helgaas 	unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
4298c86e70aSVivek Goyal 
43097523a4eSDavid Hildenbrand 	return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
43197523a4eSDavid Hildenbrand 				     func);
4328c86e70aSVivek Goyal }
4338c86e70aSVivek Goyal 
4340e4c12b4STom Lendacky /*
4350e4c12b4STom Lendacky  * This function calls the @func callback against all memory ranges, which
4360e4c12b4STom Lendacky  * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
4370e4c12b4STom Lendacky  */
walk_mem_res(u64 start,u64 end,void * arg,int (* func)(struct resource *,void *))4380e4c12b4STom Lendacky int walk_mem_res(u64 start, u64 end, void *arg,
4390e4c12b4STom Lendacky 		 int (*func)(struct resource *, void *))
4400e4c12b4STom Lendacky {
441010a93bfSBjorn Helgaas 	unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4420e4c12b4STom Lendacky 
44397523a4eSDavid Hildenbrand 	return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
44497523a4eSDavid Hildenbrand 				     func);
4450e4c12b4STom Lendacky }
4460e4c12b4STom Lendacky 
4478c86e70aSVivek Goyal /*
448bd7e6cb3SToshi Kani  * This function calls the @func callback against all memory ranges of type
449bd7e6cb3SToshi Kani  * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
450bd7e6cb3SToshi Kani  * It is to be used only for System RAM.
451908eedc6SKAMEZAWA Hiroyuki  */
walk_system_ram_range(unsigned long start_pfn,unsigned long nr_pages,void * arg,int (* func)(unsigned long,unsigned long,void *))452908eedc6SKAMEZAWA Hiroyuki int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
453908eedc6SKAMEZAWA Hiroyuki 			  void *arg, int (*func)(unsigned long, unsigned long, void *))
45475884fb1SKAMEZAWA Hiroyuki {
455010a93bfSBjorn Helgaas 	resource_size_t start, end;
456010a93bfSBjorn Helgaas 	unsigned long flags;
45775884fb1SKAMEZAWA Hiroyuki 	struct resource res;
45837b99dd5SWu Fengguang 	unsigned long pfn, end_pfn;
4595cd401acSDave Hansen 	int ret = -EINVAL;
460908eedc6SKAMEZAWA Hiroyuki 
461010a93bfSBjorn Helgaas 	start = (u64) start_pfn << PAGE_SHIFT;
462010a93bfSBjorn Helgaas 	end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
463010a93bfSBjorn Helgaas 	flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
464010a93bfSBjorn Helgaas 	while (start < end &&
46597523a4eSDavid Hildenbrand 	       !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
46600ff9a91SDavid Hildenbrand 		pfn = PFN_UP(res.start);
46700ff9a91SDavid Hildenbrand 		end_pfn = PFN_DOWN(res.end + 1);
46837b99dd5SWu Fengguang 		if (end_pfn > pfn)
46937b99dd5SWu Fengguang 			ret = (*func)(pfn, end_pfn - pfn, arg);
47075884fb1SKAMEZAWA Hiroyuki 		if (ret)
47175884fb1SKAMEZAWA Hiroyuki 			break;
472010a93bfSBjorn Helgaas 		start = res.end + 1;
47375884fb1SKAMEZAWA Hiroyuki 	}
47475884fb1SKAMEZAWA Hiroyuki 	return ret;
47575884fb1SKAMEZAWA Hiroyuki }
47675884fb1SKAMEZAWA Hiroyuki 
__is_ram(unsigned long pfn,unsigned long nr_pages,void * arg)47761ef2489SWu Fengguang static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
47861ef2489SWu Fengguang {
47961ef2489SWu Fengguang 	return 1;
48061ef2489SWu Fengguang }
4814ac2aed8STom Lendacky 
48261ef2489SWu Fengguang /*
48361ef2489SWu Fengguang  * This generic page_is_ram() returns true if specified address is
484bd7e6cb3SToshi Kani  * registered as System RAM in iomem_resource list.
48561ef2489SWu Fengguang  */
page_is_ram(unsigned long pfn)486e5273007SAndrew Morton int __weak page_is_ram(unsigned long pfn)
48761ef2489SWu Fengguang {
48861ef2489SWu Fengguang 	return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
48961ef2489SWu Fengguang }
490c5a13032SChen Gong EXPORT_SYMBOL_GPL(page_is_ram);
49161ef2489SWu Fengguang 
__region_intersects(struct resource * parent,resource_size_t start,size_t size,unsigned long flags,unsigned long desc)49214b80582SDan Williams static int __region_intersects(struct resource *parent, resource_size_t start,
49314b80582SDan Williams 			       size_t size, unsigned long flags,
49414b80582SDan Williams 			       unsigned long desc)
495d486ccb2SAlistair Popple {
496d486ccb2SAlistair Popple 	struct resource res;
497d486ccb2SAlistair Popple 	int type = 0; int other = 0;
498d486ccb2SAlistair Popple 	struct resource *p;
499d486ccb2SAlistair Popple 
500d486ccb2SAlistair Popple 	res.start = start;
501d486ccb2SAlistair Popple 	res.end = start + size - 1;
502d486ccb2SAlistair Popple 
50314b80582SDan Williams 	for (p = parent->child; p ; p = p->sibling) {
504d486ccb2SAlistair Popple 		bool is_type = (((p->flags & flags) == flags) &&
505d486ccb2SAlistair Popple 				((desc == IORES_DESC_NONE) ||
506d486ccb2SAlistair Popple 				 (desc == p->desc)));
507d486ccb2SAlistair Popple 
508d486ccb2SAlistair Popple 		if (resource_overlaps(p, &res))
509d486ccb2SAlistair Popple 			is_type ? type++ : other++;
510d486ccb2SAlistair Popple 	}
511d486ccb2SAlistair Popple 
512d486ccb2SAlistair Popple 	if (type == 0)
513d486ccb2SAlistair Popple 		return REGION_DISJOINT;
514d486ccb2SAlistair Popple 
515d486ccb2SAlistair Popple 	if (other == 0)
516d486ccb2SAlistair Popple 		return REGION_INTERSECTS;
517d486ccb2SAlistair Popple 
518d486ccb2SAlistair Popple 	return REGION_MIXED;
519d486ccb2SAlistair Popple }
520d486ccb2SAlistair Popple 
521124fe20dSDan Williams /**
522124fe20dSDan Williams  * region_intersects() - determine intersection of region with known resources
523124fe20dSDan Williams  * @start: region start address
524124fe20dSDan Williams  * @size: size of region
5251c29f25bSToshi Kani  * @flags: flags of resource (in iomem_resource)
5261c29f25bSToshi Kani  * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
52767cf13ceSMike Travis  *
528124fe20dSDan Williams  * Check if the specified region partially overlaps or fully eclipses a
5291c29f25bSToshi Kani  * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
5301c29f25bSToshi Kani  * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
5311c29f25bSToshi Kani  * return REGION_MIXED if the region overlaps @flags/@desc and another
5321c29f25bSToshi Kani  * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
5331c29f25bSToshi Kani  * and no other defined resource. Note that REGION_INTERSECTS is also
5341c29f25bSToshi Kani  * returned in the case when the specified region overlaps RAM and undefined
5351c29f25bSToshi Kani  * memory holes.
536124fe20dSDan Williams  *
537124fe20dSDan Williams  * region_intersect() is used by memory remapping functions to ensure
538124fe20dSDan Williams  * the user is not remapping RAM and is a vast speed up over walking
539124fe20dSDan Williams  * through the resource table page by page.
54067cf13ceSMike Travis  */
region_intersects(resource_size_t start,size_t size,unsigned long flags,unsigned long desc)5411c29f25bSToshi Kani int region_intersects(resource_size_t start, size_t size, unsigned long flags,
5421c29f25bSToshi Kani 		      unsigned long desc)
54367cf13ceSMike Travis {
544d486ccb2SAlistair Popple 	int ret;
545f6c6010aSWei Yang 
54667cf13ceSMike Travis 	read_lock(&resource_lock);
54714b80582SDan Williams 	ret = __region_intersects(&iomem_resource, start, size, flags, desc);
54867cf13ceSMike Travis 	read_unlock(&resource_lock);
549124fe20dSDan Williams 
550d486ccb2SAlistair Popple 	return ret;
55167cf13ceSMike Travis }
5521c29f25bSToshi Kani EXPORT_SYMBOL_GPL(region_intersects);
55367cf13ceSMike Travis 
arch_remove_reservations(struct resource * avail)554fcb11918SBjorn Helgaas void __weak arch_remove_reservations(struct resource *avail)
555fcb11918SBjorn Helgaas {
556fcb11918SBjorn Helgaas }
557fcb11918SBjorn Helgaas 
simple_align_resource(void * data,const struct resource * avail,resource_size_t size,resource_size_t align)558a9cea017SBjorn Helgaas static resource_size_t simple_align_resource(void *data,
559a9cea017SBjorn Helgaas 					     const struct resource *avail,
560a9cea017SBjorn Helgaas 					     resource_size_t size,
561a9cea017SBjorn Helgaas 					     resource_size_t align)
562a9cea017SBjorn Helgaas {
563a9cea017SBjorn Helgaas 	return avail->start;
564a9cea017SBjorn Helgaas }
565a9cea017SBjorn Helgaas 
resource_clip(struct resource * res,resource_size_t min,resource_size_t max)5665d6b1fa3SBjorn Helgaas static void resource_clip(struct resource *res, resource_size_t min,
5675d6b1fa3SBjorn Helgaas 			  resource_size_t max)
5685d6b1fa3SBjorn Helgaas {
5695d6b1fa3SBjorn Helgaas 	if (res->start < min)
5705d6b1fa3SBjorn Helgaas 		res->start = min;
5715d6b1fa3SBjorn Helgaas 	if (res->end > max)
5725d6b1fa3SBjorn Helgaas 		res->end = max;
5735d6b1fa3SBjorn Helgaas }
5745d6b1fa3SBjorn Helgaas 
5751da177e4SLinus Torvalds /*
57623c570a6SRam Pai  * Find empty slot in the resource tree with the given range and
57723c570a6SRam Pai  * alignment constraints
5781da177e4SLinus Torvalds  */
__find_resource(struct resource * root,struct resource * old,struct resource * new,resource_size_t size,struct resource_constraint * constraint)57923c570a6SRam Pai static int __find_resource(struct resource *root, struct resource *old,
58023c570a6SRam Pai 			 struct resource *new,
58123c570a6SRam Pai 			 resource_size_t  size,
58223c570a6SRam Pai 			 struct resource_constraint *constraint)
5831da177e4SLinus Torvalds {
5841da177e4SLinus Torvalds 	struct resource *this = root->child;
585a1862e31SBjorn Helgaas 	struct resource tmp = *new, avail, alloc;
5861da177e4SLinus Torvalds 
5870e2c8b8fSDominik Brodowski 	tmp.start = root->start;
5881da177e4SLinus Torvalds 	/*
589c0f5ac54SBjorn Helgaas 	 * Skip past an allocated resource that starts at 0, since the assignment
590c0f5ac54SBjorn Helgaas 	 * of this->start - 1 to tmp->end below would cause an underflow.
5911da177e4SLinus Torvalds 	 */
59223c570a6SRam Pai 	if (this && this->start == root->start) {
59323c570a6SRam Pai 		tmp.start = (this == old) ? old->start : this->end + 1;
5941da177e4SLinus Torvalds 		this = this->sibling;
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 	for(;;) {
5971da177e4SLinus Torvalds 		if (this)
59823c570a6SRam Pai 			tmp.end = (this == old) ?  this->end : this->start - 1;
5991da177e4SLinus Torvalds 		else
6000e2c8b8fSDominik Brodowski 			tmp.end = root->end;
6015d6b1fa3SBjorn Helgaas 
60247ea91b4SRam Pai 		if (tmp.end < tmp.start)
60347ea91b4SRam Pai 			goto next;
60447ea91b4SRam Pai 
60523c570a6SRam Pai 		resource_clip(&tmp, constraint->min, constraint->max);
606fcb11918SBjorn Helgaas 		arch_remove_reservations(&tmp);
607a9cea017SBjorn Helgaas 
608a1862e31SBjorn Helgaas 		/* Check for overflow after ALIGN() */
60923c570a6SRam Pai 		avail.start = ALIGN(tmp.start, constraint->align);
610a1862e31SBjorn Helgaas 		avail.end = tmp.end;
6115edb93b8SBjorn Helgaas 		avail.flags = new->flags & ~IORESOURCE_UNSET;
612a1862e31SBjorn Helgaas 		if (avail.start >= tmp.start) {
6135edb93b8SBjorn Helgaas 			alloc.flags = avail.flags;
61423c570a6SRam Pai 			alloc.start = constraint->alignf(constraint->alignf_data, &avail,
61523c570a6SRam Pai 					size, constraint->align);
6166909ba14SBjorn Helgaas 			alloc.end = alloc.start + size - 1;
61760bb83b8STakashi Iwai 			if (alloc.start <= alloc.end &&
61860bb83b8STakashi Iwai 			    resource_contains(&avail, &alloc)) {
6196909ba14SBjorn Helgaas 				new->start = alloc.start;
6206909ba14SBjorn Helgaas 				new->end = alloc.end;
6211da177e4SLinus Torvalds 				return 0;
6221da177e4SLinus Torvalds 			}
623a1862e31SBjorn Helgaas 		}
62447ea91b4SRam Pai 
62547ea91b4SRam Pai next:		if (!this || this->end == root->end)
6261da177e4SLinus Torvalds 			break;
62747ea91b4SRam Pai 
62823c570a6SRam Pai 		if (this != old)
6290e2c8b8fSDominik Brodowski 			tmp.start = this->end + 1;
6301da177e4SLinus Torvalds 		this = this->sibling;
6311da177e4SLinus Torvalds 	}
6321da177e4SLinus Torvalds 	return -EBUSY;
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
63523c570a6SRam Pai /*
63623c570a6SRam Pai  * Find empty slot in the resource tree given range and alignment.
63723c570a6SRam Pai  */
find_resource(struct resource * root,struct resource * new,resource_size_t size,struct resource_constraint * constraint)63823c570a6SRam Pai static int find_resource(struct resource *root, struct resource *new,
63923c570a6SRam Pai 			resource_size_t size,
64023c570a6SRam Pai 			struct resource_constraint  *constraint)
64123c570a6SRam Pai {
64223c570a6SRam Pai 	return  __find_resource(root, NULL, new, size, constraint);
64323c570a6SRam Pai }
64423c570a6SRam Pai 
645e1ca66d1SRandy Dunlap /**
64623c570a6SRam Pai  * reallocate_resource - allocate a slot in the resource tree given range & alignment.
64723c570a6SRam Pai  *	The resource will be relocated if the new size cannot be reallocated in the
64823c570a6SRam Pai  *	current location.
64923c570a6SRam Pai  *
65023c570a6SRam Pai  * @root: root resource descriptor
65123c570a6SRam Pai  * @old:  resource descriptor desired by caller
65223c570a6SRam Pai  * @newsize: new size of the resource descriptor
65323c570a6SRam Pai  * @constraint: the size and alignment constraints to be met.
65423c570a6SRam Pai  */
reallocate_resource(struct resource * root,struct resource * old,resource_size_t newsize,struct resource_constraint * constraint)65528ab49ffSDaeseok Youn static int reallocate_resource(struct resource *root, struct resource *old,
65623c570a6SRam Pai 			       resource_size_t newsize,
65723c570a6SRam Pai 			       struct resource_constraint *constraint)
65823c570a6SRam Pai {
65923c570a6SRam Pai 	int err=0;
66023c570a6SRam Pai 	struct resource new = *old;
66123c570a6SRam Pai 	struct resource *conflict;
66223c570a6SRam Pai 
66323c570a6SRam Pai 	write_lock(&resource_lock);
66423c570a6SRam Pai 
66523c570a6SRam Pai 	if ((err = __find_resource(root, old, &new, newsize, constraint)))
66623c570a6SRam Pai 		goto out;
66723c570a6SRam Pai 
66823c570a6SRam Pai 	if (resource_contains(&new, old)) {
66923c570a6SRam Pai 		old->start = new.start;
67023c570a6SRam Pai 		old->end = new.end;
67123c570a6SRam Pai 		goto out;
67223c570a6SRam Pai 	}
67323c570a6SRam Pai 
67423c570a6SRam Pai 	if (old->child) {
67523c570a6SRam Pai 		err = -EBUSY;
67623c570a6SRam Pai 		goto out;
67723c570a6SRam Pai 	}
67823c570a6SRam Pai 
67923c570a6SRam Pai 	if (resource_contains(old, &new)) {
68023c570a6SRam Pai 		old->start = new.start;
68123c570a6SRam Pai 		old->end = new.end;
68223c570a6SRam Pai 	} else {
683ff3cc952SToshi Kani 		__release_resource(old, true);
68423c570a6SRam Pai 		*old = new;
68523c570a6SRam Pai 		conflict = __request_resource(root, old);
68623c570a6SRam Pai 		BUG_ON(conflict);
68723c570a6SRam Pai 	}
68823c570a6SRam Pai out:
68923c570a6SRam Pai 	write_unlock(&resource_lock);
69023c570a6SRam Pai 	return err;
69123c570a6SRam Pai }
69223c570a6SRam Pai 
69323c570a6SRam Pai 
69423c570a6SRam Pai /**
69523c570a6SRam Pai  * allocate_resource - allocate empty slot in the resource tree given range & alignment.
69623c570a6SRam Pai  * 	The resource will be reallocated with a new size if it was already allocated
697e1ca66d1SRandy Dunlap  * @root: root resource descriptor
698e1ca66d1SRandy Dunlap  * @new: resource descriptor desired by caller
699e1ca66d1SRandy Dunlap  * @size: requested resource region size
700ee5e5683SWei Yang  * @min: minimum boundary to allocate
701ee5e5683SWei Yang  * @max: maximum boundary to allocate
702e1ca66d1SRandy Dunlap  * @align: alignment requested, in bytes
703e1ca66d1SRandy Dunlap  * @alignf: alignment function, optional, called if not NULL
704e1ca66d1SRandy Dunlap  * @alignf_data: arbitrary data to pass to the @alignf function
7051da177e4SLinus Torvalds  */
allocate_resource(struct resource * root,struct resource * new,resource_size_t size,resource_size_t min,resource_size_t max,resource_size_t align,resource_size_t (* alignf)(void *,const struct resource *,resource_size_t,resource_size_t),void * alignf_data)7061da177e4SLinus Torvalds int allocate_resource(struct resource *root, struct resource *new,
707d75fc8bbSGreg Kroah-Hartman 		      resource_size_t size, resource_size_t min,
708d75fc8bbSGreg Kroah-Hartman 		      resource_size_t max, resource_size_t align,
709b26b2d49SDominik Brodowski 		      resource_size_t (*alignf)(void *,
7103b7a17fcSDominik Brodowski 						const struct resource *,
711b26b2d49SDominik Brodowski 						resource_size_t,
712b26b2d49SDominik Brodowski 						resource_size_t),
7131da177e4SLinus Torvalds 		      void *alignf_data)
7141da177e4SLinus Torvalds {
7151da177e4SLinus Torvalds 	int err;
71623c570a6SRam Pai 	struct resource_constraint constraint;
7171da177e4SLinus Torvalds 
718a9cea017SBjorn Helgaas 	if (!alignf)
719a9cea017SBjorn Helgaas 		alignf = simple_align_resource;
720a9cea017SBjorn Helgaas 
72123c570a6SRam Pai 	constraint.min = min;
72223c570a6SRam Pai 	constraint.max = max;
72323c570a6SRam Pai 	constraint.align = align;
72423c570a6SRam Pai 	constraint.alignf = alignf;
72523c570a6SRam Pai 	constraint.alignf_data = alignf_data;
72623c570a6SRam Pai 
72723c570a6SRam Pai 	if ( new->parent ) {
72823c570a6SRam Pai 		/* resource is already allocated, try reallocating with
72923c570a6SRam Pai 		   the new constraints */
73023c570a6SRam Pai 		return reallocate_resource(root, new, size, &constraint);
73123c570a6SRam Pai 	}
73223c570a6SRam Pai 
7331da177e4SLinus Torvalds 	write_lock(&resource_lock);
73423c570a6SRam Pai 	err = find_resource(root, new, size, &constraint);
7351da177e4SLinus Torvalds 	if (err >= 0 && __request_resource(root, new))
7361da177e4SLinus Torvalds 		err = -EBUSY;
7371da177e4SLinus Torvalds 	write_unlock(&resource_lock);
7381da177e4SLinus Torvalds 	return err;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds EXPORT_SYMBOL(allocate_resource);
7421da177e4SLinus Torvalds 
7431c388919SGeert Uytterhoeven /**
7441c388919SGeert Uytterhoeven  * lookup_resource - find an existing resource by a resource start address
7451c388919SGeert Uytterhoeven  * @root: root resource descriptor
7461c388919SGeert Uytterhoeven  * @start: resource start address
7471c388919SGeert Uytterhoeven  *
7481c388919SGeert Uytterhoeven  * Returns a pointer to the resource if found, NULL otherwise
7491c388919SGeert Uytterhoeven  */
lookup_resource(struct resource * root,resource_size_t start)7501c388919SGeert Uytterhoeven struct resource *lookup_resource(struct resource *root, resource_size_t start)
7511c388919SGeert Uytterhoeven {
7521c388919SGeert Uytterhoeven 	struct resource *res;
7531c388919SGeert Uytterhoeven 
7541c388919SGeert Uytterhoeven 	read_lock(&resource_lock);
7551c388919SGeert Uytterhoeven 	for (res = root->child; res; res = res->sibling) {
7561c388919SGeert Uytterhoeven 		if (res->start == start)
7571c388919SGeert Uytterhoeven 			break;
7581c388919SGeert Uytterhoeven 	}
7591c388919SGeert Uytterhoeven 	read_unlock(&resource_lock);
7601c388919SGeert Uytterhoeven 
7611c388919SGeert Uytterhoeven 	return res;
7621c388919SGeert Uytterhoeven }
7631c388919SGeert Uytterhoeven 
764bef69ea0SLinus Torvalds /*
765bef69ea0SLinus Torvalds  * Insert a resource into the resource tree. If successful, return NULL,
766bef69ea0SLinus Torvalds  * otherwise return the conflicting resource (compare to __request_resource())
7671da177e4SLinus Torvalds  */
__insert_resource(struct resource * parent,struct resource * new)768bef69ea0SLinus Torvalds static struct resource * __insert_resource(struct resource *parent, struct resource *new)
7691da177e4SLinus Torvalds {
7701da177e4SLinus Torvalds 	struct resource *first, *next;
7711da177e4SLinus Torvalds 
772d33b6fbaSMatthew Wilcox 	for (;; parent = first) {
7731da177e4SLinus Torvalds 		first = __request_resource(parent, new);
7741da177e4SLinus Torvalds 		if (!first)
775bef69ea0SLinus Torvalds 			return first;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 		if (first == parent)
778bef69ea0SLinus Torvalds 			return first;
7795de1cb2dSHuang Shijie 		if (WARN_ON(first == new))	/* duplicated insertion */
7805de1cb2dSHuang Shijie 			return first;
7811da177e4SLinus Torvalds 
782d33b6fbaSMatthew Wilcox 		if ((first->start > new->start) || (first->end < new->end))
783d33b6fbaSMatthew Wilcox 			break;
784d33b6fbaSMatthew Wilcox 		if ((first->start == new->start) && (first->end == new->end))
785d33b6fbaSMatthew Wilcox 			break;
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	for (next = first; ; next = next->sibling) {
7891da177e4SLinus Torvalds 		/* Partial overlap? Bad, and unfixable */
7901da177e4SLinus Torvalds 		if (next->start < new->start || next->end > new->end)
791bef69ea0SLinus Torvalds 			return next;
7921da177e4SLinus Torvalds 		if (!next->sibling)
7931da177e4SLinus Torvalds 			break;
7941da177e4SLinus Torvalds 		if (next->sibling->start > new->end)
7951da177e4SLinus Torvalds 			break;
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	new->parent = parent;
7991da177e4SLinus Torvalds 	new->sibling = next->sibling;
8001da177e4SLinus Torvalds 	new->child = first;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	next->sibling = NULL;
8031da177e4SLinus Torvalds 	for (next = first; next; next = next->sibling)
8041da177e4SLinus Torvalds 		next->parent = new;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	if (parent->child == first) {
8071da177e4SLinus Torvalds 		parent->child = new;
8081da177e4SLinus Torvalds 	} else {
8091da177e4SLinus Torvalds 		next = parent->child;
8101da177e4SLinus Torvalds 		while (next->sibling != first)
8111da177e4SLinus Torvalds 			next = next->sibling;
8121da177e4SLinus Torvalds 		next->sibling = new;
8131da177e4SLinus Torvalds 	}
814bef69ea0SLinus Torvalds 	return NULL;
815bef69ea0SLinus Torvalds }
8161da177e4SLinus Torvalds 
817bef69ea0SLinus Torvalds /**
81866f1207bSBjorn Helgaas  * insert_resource_conflict - Inserts resource in the resource tree
819bef69ea0SLinus Torvalds  * @parent: parent of the new resource
820bef69ea0SLinus Torvalds  * @new: new resource to insert
821bef69ea0SLinus Torvalds  *
82266f1207bSBjorn Helgaas  * Returns 0 on success, conflict resource if the resource can't be inserted.
823bef69ea0SLinus Torvalds  *
82466f1207bSBjorn Helgaas  * This function is equivalent to request_resource_conflict when no conflict
825bef69ea0SLinus Torvalds  * happens. If a conflict happens, and the conflicting resources
826bef69ea0SLinus Torvalds  * entirely fit within the range of the new resource, then the new
827bef69ea0SLinus Torvalds  * resource is inserted and the conflicting resources become children of
828bef69ea0SLinus Torvalds  * the new resource.
829ff3cc952SToshi Kani  *
830ff3cc952SToshi Kani  * This function is intended for producers of resources, such as FW modules
831ff3cc952SToshi Kani  * and bus drivers.
832bef69ea0SLinus Torvalds  */
insert_resource_conflict(struct resource * parent,struct resource * new)83366f1207bSBjorn Helgaas struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
834bef69ea0SLinus Torvalds {
835bef69ea0SLinus Torvalds 	struct resource *conflict;
836bef69ea0SLinus Torvalds 
837bef69ea0SLinus Torvalds 	write_lock(&resource_lock);
838bef69ea0SLinus Torvalds 	conflict = __insert_resource(parent, new);
8391da177e4SLinus Torvalds 	write_unlock(&resource_lock);
84066f1207bSBjorn Helgaas 	return conflict;
84166f1207bSBjorn Helgaas }
84266f1207bSBjorn Helgaas 
84366f1207bSBjorn Helgaas /**
84466f1207bSBjorn Helgaas  * insert_resource - Inserts a resource in the resource tree
84566f1207bSBjorn Helgaas  * @parent: parent of the new resource
84666f1207bSBjorn Helgaas  * @new: new resource to insert
84766f1207bSBjorn Helgaas  *
84866f1207bSBjorn Helgaas  * Returns 0 on success, -EBUSY if the resource can't be inserted.
849ff3cc952SToshi Kani  *
850ff3cc952SToshi Kani  * This function is intended for producers of resources, such as FW modules
851ff3cc952SToshi Kani  * and bus drivers.
85266f1207bSBjorn Helgaas  */
insert_resource(struct resource * parent,struct resource * new)85366f1207bSBjorn Helgaas int insert_resource(struct resource *parent, struct resource *new)
85466f1207bSBjorn Helgaas {
85566f1207bSBjorn Helgaas 	struct resource *conflict;
85666f1207bSBjorn Helgaas 
85766f1207bSBjorn Helgaas 	conflict = insert_resource_conflict(parent, new);
858bef69ea0SLinus Torvalds 	return conflict ? -EBUSY : 0;
859bef69ea0SLinus Torvalds }
8608095d0f2SToshi Kani EXPORT_SYMBOL_GPL(insert_resource);
861bef69ea0SLinus Torvalds 
862bef69ea0SLinus Torvalds /**
863bef69ea0SLinus Torvalds  * insert_resource_expand_to_fit - Insert a resource into the resource tree
8646781f4aeSRandy Dunlap  * @root: root resource descriptor
865bef69ea0SLinus Torvalds  * @new: new resource to insert
866bef69ea0SLinus Torvalds  *
867bef69ea0SLinus Torvalds  * Insert a resource into the resource tree, possibly expanding it in order
868bef69ea0SLinus Torvalds  * to make it encompass any conflicting resources.
869bef69ea0SLinus Torvalds  */
insert_resource_expand_to_fit(struct resource * root,struct resource * new)870bef69ea0SLinus Torvalds void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
871bef69ea0SLinus Torvalds {
872bef69ea0SLinus Torvalds 	if (new->parent)
873bef69ea0SLinus Torvalds 		return;
874bef69ea0SLinus Torvalds 
875bef69ea0SLinus Torvalds 	write_lock(&resource_lock);
876bef69ea0SLinus Torvalds 	for (;;) {
877bef69ea0SLinus Torvalds 		struct resource *conflict;
878bef69ea0SLinus Torvalds 
879bef69ea0SLinus Torvalds 		conflict = __insert_resource(root, new);
880bef69ea0SLinus Torvalds 		if (!conflict)
881bef69ea0SLinus Torvalds 			break;
882bef69ea0SLinus Torvalds 		if (conflict == root)
883bef69ea0SLinus Torvalds 			break;
884bef69ea0SLinus Torvalds 
885bef69ea0SLinus Torvalds 		/* Ok, expand resource to cover the conflict, then try again .. */
886bef69ea0SLinus Torvalds 		if (conflict->start < new->start)
887bef69ea0SLinus Torvalds 			new->start = conflict->start;
888bef69ea0SLinus Torvalds 		if (conflict->end > new->end)
889bef69ea0SLinus Torvalds 			new->end = conflict->end;
890bef69ea0SLinus Torvalds 
8912a4e6285SAndy Shevchenko 		pr_info("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
892bef69ea0SLinus Torvalds 	}
893bef69ea0SLinus Torvalds 	write_unlock(&resource_lock);
8941da177e4SLinus Torvalds }
895974854abSDan Williams /*
896974854abSDan Williams  * Not for general consumption, only early boot memory map parsing, PCI
897974854abSDan Williams  * resource discovery, and late discovery of CXL resources are expected
898974854abSDan Williams  * to use this interface. The former are built-in and only the latter,
899974854abSDan Williams  * CXL, is a module.
900974854abSDan Williams  */
901974854abSDan Williams EXPORT_SYMBOL_NS_GPL(insert_resource_expand_to_fit, CXL);
9021da177e4SLinus Torvalds 
903ff3cc952SToshi Kani /**
904ff3cc952SToshi Kani  * remove_resource - Remove a resource in the resource tree
905ff3cc952SToshi Kani  * @old: resource to remove
906ff3cc952SToshi Kani  *
907ff3cc952SToshi Kani  * Returns 0 on success, -EINVAL if the resource is not valid.
908ff3cc952SToshi Kani  *
909ff3cc952SToshi Kani  * This function removes a resource previously inserted by insert_resource()
910ff3cc952SToshi Kani  * or insert_resource_conflict(), and moves the children (if any) up to
911ff3cc952SToshi Kani  * where they were before.  insert_resource() and insert_resource_conflict()
912ff3cc952SToshi Kani  * insert a new resource, and move any conflicting resources down to the
913ff3cc952SToshi Kani  * children of the new resource.
914ff3cc952SToshi Kani  *
915ff3cc952SToshi Kani  * insert_resource(), insert_resource_conflict() and remove_resource() are
916ff3cc952SToshi Kani  * intended for producers of resources, such as FW modules and bus drivers.
917ff3cc952SToshi Kani  */
remove_resource(struct resource * old)918ff3cc952SToshi Kani int remove_resource(struct resource *old)
919ff3cc952SToshi Kani {
920ff3cc952SToshi Kani 	int retval;
921ff3cc952SToshi Kani 
922ff3cc952SToshi Kani 	write_lock(&resource_lock);
923ff3cc952SToshi Kani 	retval = __release_resource(old, false);
924ff3cc952SToshi Kani 	write_unlock(&resource_lock);
925ff3cc952SToshi Kani 	return retval;
926ff3cc952SToshi Kani }
9278095d0f2SToshi Kani EXPORT_SYMBOL_GPL(remove_resource);
928ff3cc952SToshi Kani 
__adjust_resource(struct resource * res,resource_size_t start,resource_size_t size)929ae8e3a91SToshi Kani static int __adjust_resource(struct resource *res, resource_size_t start,
930ae8e3a91SToshi Kani 				resource_size_t size)
9311da177e4SLinus Torvalds {
9321da177e4SLinus Torvalds 	struct resource *tmp, *parent = res->parent;
933d75fc8bbSGreg Kroah-Hartman 	resource_size_t end = start + size - 1;
9341da177e4SLinus Torvalds 	int result = -EBUSY;
9351da177e4SLinus Torvalds 
93682ec90eaSYinghai Lu 	if (!parent)
93782ec90eaSYinghai Lu 		goto skip;
93882ec90eaSYinghai Lu 
9391da177e4SLinus Torvalds 	if ((start < parent->start) || (end > parent->end))
9401da177e4SLinus Torvalds 		goto out;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	if (res->sibling && (res->sibling->start <= end))
9431da177e4SLinus Torvalds 		goto out;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	tmp = parent->child;
9461da177e4SLinus Torvalds 	if (tmp != res) {
9471da177e4SLinus Torvalds 		while (tmp->sibling != res)
9481da177e4SLinus Torvalds 			tmp = tmp->sibling;
9491da177e4SLinus Torvalds 		if (start <= tmp->end)
9501da177e4SLinus Torvalds 			goto out;
9511da177e4SLinus Torvalds 	}
9521da177e4SLinus Torvalds 
95382ec90eaSYinghai Lu skip:
95482ec90eaSYinghai Lu 	for (tmp = res->child; tmp; tmp = tmp->sibling)
95582ec90eaSYinghai Lu 		if ((tmp->start < start) || (tmp->end > end))
95682ec90eaSYinghai Lu 			goto out;
95782ec90eaSYinghai Lu 
9581da177e4SLinus Torvalds 	res->start = start;
9591da177e4SLinus Torvalds 	res->end = end;
9601da177e4SLinus Torvalds 	result = 0;
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds  out:
963ae8e3a91SToshi Kani 	return result;
964ae8e3a91SToshi Kani }
965ae8e3a91SToshi Kani 
966ae8e3a91SToshi Kani /**
967ae8e3a91SToshi Kani  * adjust_resource - modify a resource's start and size
968ae8e3a91SToshi Kani  * @res: resource to modify
969ae8e3a91SToshi Kani  * @start: new start value
970ae8e3a91SToshi Kani  * @size: new size
971ae8e3a91SToshi Kani  *
972ae8e3a91SToshi Kani  * Given an existing resource, change its start and size to match the
973ae8e3a91SToshi Kani  * arguments.  Returns 0 on success, -EBUSY if it can't fit.
974ae8e3a91SToshi Kani  * Existing children of the resource are assumed to be immutable.
975ae8e3a91SToshi Kani  */
adjust_resource(struct resource * res,resource_size_t start,resource_size_t size)976ae8e3a91SToshi Kani int adjust_resource(struct resource *res, resource_size_t start,
977ae8e3a91SToshi Kani 		    resource_size_t size)
978ae8e3a91SToshi Kani {
979ae8e3a91SToshi Kani 	int result;
980ae8e3a91SToshi Kani 
981ae8e3a91SToshi Kani 	write_lock(&resource_lock);
982ae8e3a91SToshi Kani 	result = __adjust_resource(res, start, size);
9831da177e4SLinus Torvalds 	write_unlock(&resource_lock);
9841da177e4SLinus Torvalds 	return result;
9851da177e4SLinus Torvalds }
98624105748SCong Wang EXPORT_SYMBOL(adjust_resource);
9871da177e4SLinus Torvalds 
988b69c2e20SBorislav Petkov static void __init
__reserve_region_with_split(struct resource * root,resource_size_t start,resource_size_t end,const char * name)989b69c2e20SBorislav Petkov __reserve_region_with_split(struct resource *root, resource_size_t start,
990b69c2e20SBorislav Petkov 			    resource_size_t end, const char *name)
991268364a0SYinghai Lu {
992268364a0SYinghai Lu 	struct resource *parent = root;
993268364a0SYinghai Lu 	struct resource *conflict;
994ebff7d8fSYasuaki Ishimatsu 	struct resource *res = alloc_resource(GFP_ATOMIC);
9954965f566ST Makphaibulchoke 	struct resource *next_res = NULL;
996f37e2334SBjorn Helgaas 	int type = resource_type(root);
997268364a0SYinghai Lu 
998268364a0SYinghai Lu 	if (!res)
999268364a0SYinghai Lu 		return;
1000268364a0SYinghai Lu 
1001268364a0SYinghai Lu 	res->name = name;
1002268364a0SYinghai Lu 	res->start = start;
1003268364a0SYinghai Lu 	res->end = end;
1004f37e2334SBjorn Helgaas 	res->flags = type | IORESOURCE_BUSY;
100543ee493bSToshi Kani 	res->desc = IORES_DESC_NONE;
1006268364a0SYinghai Lu 
10074965f566ST Makphaibulchoke 	while (1) {
1008268364a0SYinghai Lu 
10094965f566ST Makphaibulchoke 		conflict = __request_resource(parent, res);
10104965f566ST Makphaibulchoke 		if (!conflict) {
10114965f566ST Makphaibulchoke 			if (!next_res)
10124965f566ST Makphaibulchoke 				break;
10134965f566ST Makphaibulchoke 			res = next_res;
10144965f566ST Makphaibulchoke 			next_res = NULL;
10154965f566ST Makphaibulchoke 			continue;
10164965f566ST Makphaibulchoke 		}
1017268364a0SYinghai Lu 
10181cf44baaSIngo Molnar 		/* conflict covered whole area */
10194965f566ST Makphaibulchoke 		if (conflict->start <= res->start &&
10204965f566ST Makphaibulchoke 				conflict->end >= res->end) {
1021ebff7d8fSYasuaki Ishimatsu 			free_resource(res);
10224965f566ST Makphaibulchoke 			WARN_ON(next_res);
10234965f566ST Makphaibulchoke 			break;
10244965f566ST Makphaibulchoke 		}
1025268364a0SYinghai Lu 
10264965f566ST Makphaibulchoke 		/* failed, split and try again */
10274965f566ST Makphaibulchoke 		if (conflict->start > res->start) {
10284965f566ST Makphaibulchoke 			end = res->end;
10294965f566ST Makphaibulchoke 			res->end = conflict->start - 1;
10304965f566ST Makphaibulchoke 			if (conflict->end < end) {
1031ebff7d8fSYasuaki Ishimatsu 				next_res = alloc_resource(GFP_ATOMIC);
10324965f566ST Makphaibulchoke 				if (!next_res) {
1033ebff7d8fSYasuaki Ishimatsu 					free_resource(res);
10344965f566ST Makphaibulchoke 					break;
10354965f566ST Makphaibulchoke 				}
10364965f566ST Makphaibulchoke 				next_res->name = name;
10374965f566ST Makphaibulchoke 				next_res->start = conflict->end + 1;
10384965f566ST Makphaibulchoke 				next_res->end = end;
1039f37e2334SBjorn Helgaas 				next_res->flags = type | IORESOURCE_BUSY;
104043ee493bSToshi Kani 				next_res->desc = IORES_DESC_NONE;
10414965f566ST Makphaibulchoke 			}
10424965f566ST Makphaibulchoke 		} else {
10434965f566ST Makphaibulchoke 			res->start = conflict->end + 1;
10444965f566ST Makphaibulchoke 		}
10454965f566ST Makphaibulchoke 	}
10464965f566ST Makphaibulchoke 
1047268364a0SYinghai Lu }
1048268364a0SYinghai Lu 
1049b69c2e20SBorislav Petkov void __init
reserve_region_with_split(struct resource * root,resource_size_t start,resource_size_t end,const char * name)1050b69c2e20SBorislav Petkov reserve_region_with_split(struct resource *root, resource_size_t start,
1051b69c2e20SBorislav Petkov 			  resource_size_t end, const char *name)
1052268364a0SYinghai Lu {
105365fed8f6SOctavian Purdila 	int abort = 0;
105465fed8f6SOctavian Purdila 
1055268364a0SYinghai Lu 	write_lock(&resource_lock);
105665fed8f6SOctavian Purdila 	if (root->start > start || root->end < end) {
105765fed8f6SOctavian Purdila 		pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
105865fed8f6SOctavian Purdila 		       (unsigned long long)start, (unsigned long long)end,
105965fed8f6SOctavian Purdila 		       root);
106065fed8f6SOctavian Purdila 		if (start > root->end || end < root->start)
106165fed8f6SOctavian Purdila 			abort = 1;
106265fed8f6SOctavian Purdila 		else {
106365fed8f6SOctavian Purdila 			if (end > root->end)
106465fed8f6SOctavian Purdila 				end = root->end;
106565fed8f6SOctavian Purdila 			if (start < root->start)
106665fed8f6SOctavian Purdila 				start = root->start;
106765fed8f6SOctavian Purdila 			pr_err("fixing request to [0x%llx-0x%llx]\n",
106865fed8f6SOctavian Purdila 			       (unsigned long long)start,
106965fed8f6SOctavian Purdila 			       (unsigned long long)end);
107065fed8f6SOctavian Purdila 		}
107165fed8f6SOctavian Purdila 		dump_stack();
107265fed8f6SOctavian Purdila 	}
107365fed8f6SOctavian Purdila 	if (!abort)
1074268364a0SYinghai Lu 		__reserve_region_with_split(root, start, end, name);
1075268364a0SYinghai Lu 	write_unlock(&resource_lock);
1076268364a0SYinghai Lu }
1077268364a0SYinghai Lu 
107888452565SIvan Kokshaysky /**
107988452565SIvan Kokshaysky  * resource_alignment - calculate resource's alignment
108088452565SIvan Kokshaysky  * @res: resource pointer
108188452565SIvan Kokshaysky  *
108288452565SIvan Kokshaysky  * Returns alignment on success, 0 (invalid alignment) on failure.
108388452565SIvan Kokshaysky  */
resource_alignment(struct resource * res)108488452565SIvan Kokshaysky resource_size_t resource_alignment(struct resource *res)
108588452565SIvan Kokshaysky {
108688452565SIvan Kokshaysky 	switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
108788452565SIvan Kokshaysky 	case IORESOURCE_SIZEALIGN:
10881a4e564bSMagnus Damm 		return resource_size(res);
108988452565SIvan Kokshaysky 	case IORESOURCE_STARTALIGN:
109088452565SIvan Kokshaysky 		return res->start;
109188452565SIvan Kokshaysky 	default:
109288452565SIvan Kokshaysky 		return 0;
109388452565SIvan Kokshaysky 	}
109488452565SIvan Kokshaysky }
109588452565SIvan Kokshaysky 
10961da177e4SLinus Torvalds /*
10971da177e4SLinus Torvalds  * This is compatibility stuff for IO resources.
10981da177e4SLinus Torvalds  *
10991da177e4SLinus Torvalds  * Note how this, unlike the above, knows about
11001da177e4SLinus Torvalds  * the IO flag meanings (busy etc).
11011da177e4SLinus Torvalds  *
1102e1ca66d1SRandy Dunlap  * request_region creates a new busy region.
11031da177e4SLinus Torvalds  *
1104e1ca66d1SRandy Dunlap  * release_region releases a matching busy region.
1105e1ca66d1SRandy Dunlap  */
1106e1ca66d1SRandy Dunlap 
11078b6d043bSAlan Cox static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
11088b6d043bSAlan Cox 
110971a1d8edSDaniel Vetter static struct inode *iomem_inode;
111071a1d8edSDaniel Vetter 
111171a1d8edSDaniel Vetter #ifdef CONFIG_IO_STRICT_DEVMEM
revoke_iomem(struct resource * res)111271a1d8edSDaniel Vetter static void revoke_iomem(struct resource *res)
111371a1d8edSDaniel Vetter {
111471a1d8edSDaniel Vetter 	/* pairs with smp_store_release() in iomem_init_inode() */
111571a1d8edSDaniel Vetter 	struct inode *inode = smp_load_acquire(&iomem_inode);
111671a1d8edSDaniel Vetter 
111771a1d8edSDaniel Vetter 	/*
111871a1d8edSDaniel Vetter 	 * Check that the initialization has completed. Losing the race
111971a1d8edSDaniel Vetter 	 * is ok because it means drivers are claiming resources before
112071a1d8edSDaniel Vetter 	 * the fs_initcall level of init and prevent iomem_get_mapping users
112171a1d8edSDaniel Vetter 	 * from establishing mappings.
112271a1d8edSDaniel Vetter 	 */
112371a1d8edSDaniel Vetter 	if (!inode)
112471a1d8edSDaniel Vetter 		return;
112571a1d8edSDaniel Vetter 
112671a1d8edSDaniel Vetter 	/*
112771a1d8edSDaniel Vetter 	 * The expectation is that the driver has successfully marked
112871a1d8edSDaniel Vetter 	 * the resource busy by this point, so devmem_is_allowed()
112971a1d8edSDaniel Vetter 	 * should start returning false, however for performance this
113071a1d8edSDaniel Vetter 	 * does not iterate the entire resource range.
113171a1d8edSDaniel Vetter 	 */
113271a1d8edSDaniel Vetter 	if (devmem_is_allowed(PHYS_PFN(res->start)) &&
113371a1d8edSDaniel Vetter 	    devmem_is_allowed(PHYS_PFN(res->end))) {
113471a1d8edSDaniel Vetter 		/*
113571a1d8edSDaniel Vetter 		 * *cringe* iomem=relaxed says "go ahead, what's the
113671a1d8edSDaniel Vetter 		 * worst that can happen?"
113771a1d8edSDaniel Vetter 		 */
113871a1d8edSDaniel Vetter 		return;
113971a1d8edSDaniel Vetter 	}
114071a1d8edSDaniel Vetter 
114171a1d8edSDaniel Vetter 	unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
114271a1d8edSDaniel Vetter }
114371a1d8edSDaniel Vetter #else
revoke_iomem(struct resource * res)114471a1d8edSDaniel Vetter static void revoke_iomem(struct resource *res) {}
114571a1d8edSDaniel Vetter #endif
114671a1d8edSDaniel Vetter 
iomem_get_mapping(void)114771a1d8edSDaniel Vetter struct address_space *iomem_get_mapping(void)
114871a1d8edSDaniel Vetter {
114971a1d8edSDaniel Vetter 	/*
115071a1d8edSDaniel Vetter 	 * This function is only called from file open paths, hence guaranteed
115171a1d8edSDaniel Vetter 	 * that fs_initcalls have completed and no need to check for NULL. But
115271a1d8edSDaniel Vetter 	 * since revoke_iomem can be called before the initcall we still need
115371a1d8edSDaniel Vetter 	 * the barrier to appease checkers.
115471a1d8edSDaniel Vetter 	 */
115571a1d8edSDaniel Vetter 	return smp_load_acquire(&iomem_inode)->i_mapping;
115671a1d8edSDaniel Vetter }
115771a1d8edSDaniel Vetter 
__request_region_locked(struct resource * res,struct resource * parent,resource_size_t start,resource_size_t n,const char * name,int flags)115863cdafe0SAlistair Popple static int __request_region_locked(struct resource *res, struct resource *parent,
1159d75fc8bbSGreg Kroah-Hartman 				   resource_size_t start, resource_size_t n,
1160e8de1481SArjan van de Ven 				   const char *name, int flags)
11611da177e4SLinus Torvalds {
11628b6d043bSAlan Cox 	DECLARE_WAITQUEUE(wait, current);
1163c26ec88eSBjorn Helgaas 
11641da177e4SLinus Torvalds 	res->name = name;
11651da177e4SLinus Torvalds 	res->start = start;
11661da177e4SLinus Torvalds 	res->end = start + n - 1;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	for (;;) {
11691da177e4SLinus Torvalds 		struct resource *conflict;
11701da177e4SLinus Torvalds 
11714e0d8f7eSToshi Kani 		res->flags = resource_type(parent) | resource_ext_type(parent);
11724e0d8f7eSToshi Kani 		res->flags |= IORESOURCE_BUSY | flags;
11734e0d8f7eSToshi Kani 		res->desc = parent->desc;
11744e0d8f7eSToshi Kani 
11751da177e4SLinus Torvalds 		conflict = __request_resource(parent, res);
11761da177e4SLinus Torvalds 		if (!conflict)
11771da177e4SLinus Torvalds 			break;
1178b926b7f3SDave Hansen 		/*
1179b926b7f3SDave Hansen 		 * mm/hmm.c reserves physical addresses which then
1180b926b7f3SDave Hansen 		 * become unavailable to other users.  Conflicts are
1181b926b7f3SDave Hansen 		 * not expected.  Warn to aid debugging if encountered.
1182b926b7f3SDave Hansen 		 */
1183b926b7f3SDave Hansen 		if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1184b926b7f3SDave Hansen 			pr_warn("Unaddressable device %s %pR conflicts with %pR",
1185b926b7f3SDave Hansen 				conflict->name, conflict, res);
1186b926b7f3SDave Hansen 		}
11871da177e4SLinus Torvalds 		if (conflict != parent) {
118859ceeaafSSimon Guinot 			if (!(conflict->flags & IORESOURCE_BUSY)) {
11891da177e4SLinus Torvalds 				parent = conflict;
11901da177e4SLinus Torvalds 				continue;
11911da177e4SLinus Torvalds 			}
119259ceeaafSSimon Guinot 		}
11938b6d043bSAlan Cox 		if (conflict->flags & flags & IORESOURCE_MUXED) {
11948b6d043bSAlan Cox 			add_wait_queue(&muxed_resource_wait, &wait);
11958b6d043bSAlan Cox 			write_unlock(&resource_lock);
11968b6d043bSAlan Cox 			set_current_state(TASK_UNINTERRUPTIBLE);
11978b6d043bSAlan Cox 			schedule();
11988b6d043bSAlan Cox 			remove_wait_queue(&muxed_resource_wait, &wait);
11998b6d043bSAlan Cox 			write_lock(&resource_lock);
12008b6d043bSAlan Cox 			continue;
12018b6d043bSAlan Cox 		}
12021da177e4SLinus Torvalds 		/* Uhhuh, that didn't work out.. */
120363cdafe0SAlistair Popple 		return -EBUSY;
12041da177e4SLinus Torvalds 	}
120563cdafe0SAlistair Popple 
120663cdafe0SAlistair Popple 	return 0;
120763cdafe0SAlistair Popple }
120863cdafe0SAlistair Popple 
120963cdafe0SAlistair Popple /**
121063cdafe0SAlistair Popple  * __request_region - create a new busy resource region
121163cdafe0SAlistair Popple  * @parent: parent resource descriptor
121263cdafe0SAlistair Popple  * @start: resource start address
121363cdafe0SAlistair Popple  * @n: resource region size
121463cdafe0SAlistair Popple  * @name: reserving caller's ID string
121563cdafe0SAlistair Popple  * @flags: IO resource flags
121663cdafe0SAlistair Popple  */
__request_region(struct resource * parent,resource_size_t start,resource_size_t n,const char * name,int flags)121763cdafe0SAlistair Popple struct resource *__request_region(struct resource *parent,
121863cdafe0SAlistair Popple 				  resource_size_t start, resource_size_t n,
121963cdafe0SAlistair Popple 				  const char *name, int flags)
122063cdafe0SAlistair Popple {
122163cdafe0SAlistair Popple 	struct resource *res = alloc_resource(GFP_KERNEL);
122263cdafe0SAlistair Popple 	int ret;
122363cdafe0SAlistair Popple 
122463cdafe0SAlistair Popple 	if (!res)
122563cdafe0SAlistair Popple 		return NULL;
122663cdafe0SAlistair Popple 
122763cdafe0SAlistair Popple 	write_lock(&resource_lock);
122863cdafe0SAlistair Popple 	ret = __request_region_locked(res, parent, start, n, name, flags);
12291da177e4SLinus Torvalds 	write_unlock(&resource_lock);
12303234ac66SDan Williams 
123163cdafe0SAlistair Popple 	if (ret) {
123263cdafe0SAlistair Popple 		free_resource(res);
123363cdafe0SAlistair Popple 		return NULL;
123463cdafe0SAlistair Popple 	}
123563cdafe0SAlistair Popple 
123663cdafe0SAlistair Popple 	if (parent == &iomem_resource)
123771a1d8edSDaniel Vetter 		revoke_iomem(res);
12383234ac66SDan Williams 
12391da177e4SLinus Torvalds 	return res;
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds EXPORT_SYMBOL(__request_region);
12421da177e4SLinus Torvalds 
1243e1ca66d1SRandy Dunlap /**
1244e1ca66d1SRandy Dunlap  * __release_region - release a previously reserved resource region
1245e1ca66d1SRandy Dunlap  * @parent: parent resource descriptor
1246e1ca66d1SRandy Dunlap  * @start: resource start address
1247e1ca66d1SRandy Dunlap  * @n: resource region size
1248e1ca66d1SRandy Dunlap  *
1249e1ca66d1SRandy Dunlap  * The described resource region must match a currently busy region.
1250e1ca66d1SRandy Dunlap  */
__release_region(struct resource * parent,resource_size_t start,resource_size_t n)1251d75fc8bbSGreg Kroah-Hartman void __release_region(struct resource *parent, resource_size_t start,
1252d75fc8bbSGreg Kroah-Hartman 		      resource_size_t n)
12531da177e4SLinus Torvalds {
12541da177e4SLinus Torvalds 	struct resource **p;
1255d75fc8bbSGreg Kroah-Hartman 	resource_size_t end;
12561da177e4SLinus Torvalds 
12571da177e4SLinus Torvalds 	p = &parent->child;
12581da177e4SLinus Torvalds 	end = start + n - 1;
12591da177e4SLinus Torvalds 
12601da177e4SLinus Torvalds 	write_lock(&resource_lock);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	for (;;) {
12631da177e4SLinus Torvalds 		struct resource *res = *p;
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 		if (!res)
12661da177e4SLinus Torvalds 			break;
12671da177e4SLinus Torvalds 		if (res->start <= start && res->end >= end) {
12681da177e4SLinus Torvalds 			if (!(res->flags & IORESOURCE_BUSY)) {
12691da177e4SLinus Torvalds 				p = &res->child;
12701da177e4SLinus Torvalds 				continue;
12711da177e4SLinus Torvalds 			}
12721da177e4SLinus Torvalds 			if (res->start != start || res->end != end)
12731da177e4SLinus Torvalds 				break;
12741da177e4SLinus Torvalds 			*p = res->sibling;
12751da177e4SLinus Torvalds 			write_unlock(&resource_lock);
12768b6d043bSAlan Cox 			if (res->flags & IORESOURCE_MUXED)
12778b6d043bSAlan Cox 				wake_up(&muxed_resource_wait);
1278ebff7d8fSYasuaki Ishimatsu 			free_resource(res);
12791da177e4SLinus Torvalds 			return;
12801da177e4SLinus Torvalds 		}
12811da177e4SLinus Torvalds 		p = &res->sibling;
12821da177e4SLinus Torvalds 	}
12831da177e4SLinus Torvalds 
12841da177e4SLinus Torvalds 	write_unlock(&resource_lock);
12851da177e4SLinus Torvalds 
12862a4e6285SAndy Shevchenko 	pr_warn("Trying to free nonexistent resource <%pa-%pa>\n", &start, &end);
12871da177e4SLinus Torvalds }
12881da177e4SLinus Torvalds EXPORT_SYMBOL(__release_region);
12891da177e4SLinus Torvalds 
1290825f787bSToshi Kani #ifdef CONFIG_MEMORY_HOTREMOVE
1291825f787bSToshi Kani /**
1292825f787bSToshi Kani  * release_mem_region_adjustable - release a previously reserved memory region
1293825f787bSToshi Kani  * @start: resource start address
1294825f787bSToshi Kani  * @size: resource region size
1295825f787bSToshi Kani  *
1296825f787bSToshi Kani  * This interface is intended for memory hot-delete.  The requested region
1297825f787bSToshi Kani  * is released from a currently busy memory resource.  The requested region
1298825f787bSToshi Kani  * must either match exactly or fit into a single busy resource entry.  In
1299825f787bSToshi Kani  * the latter case, the remaining resource is adjusted accordingly.
1300825f787bSToshi Kani  * Existing children of the busy memory resource must be immutable in the
1301825f787bSToshi Kani  * request.
1302825f787bSToshi Kani  *
1303825f787bSToshi Kani  * Note:
1304825f787bSToshi Kani  * - Additional release conditions, such as overlapping region, can be
1305825f787bSToshi Kani  *   supported after they are confirmed as valid cases.
1306825f787bSToshi Kani  * - When a busy memory resource gets split into two entries, the code
1307825f787bSToshi Kani  *   assumes that all children remain in the lower address entry for
1308825f787bSToshi Kani  *   simplicity.  Enhance this logic when necessary.
1309825f787bSToshi Kani  */
release_mem_region_adjustable(resource_size_t start,resource_size_t size)1310cb8e3c8bSDavid Hildenbrand void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1311825f787bSToshi Kani {
1312cb8e3c8bSDavid Hildenbrand 	struct resource *parent = &iomem_resource;
1313ec62d04eSDavid Hildenbrand 	struct resource *new_res = NULL;
1314ec62d04eSDavid Hildenbrand 	bool alloc_nofail = false;
1315825f787bSToshi Kani 	struct resource **p;
1316825f787bSToshi Kani 	struct resource *res;
1317825f787bSToshi Kani 	resource_size_t end;
1318825f787bSToshi Kani 
1319825f787bSToshi Kani 	end = start + size - 1;
1320ec62d04eSDavid Hildenbrand 	if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1321ec62d04eSDavid Hildenbrand 		return;
1322825f787bSToshi Kani 
1323ec62d04eSDavid Hildenbrand 	/*
1324ec62d04eSDavid Hildenbrand 	 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1325ec62d04eSDavid Hildenbrand 	 * just before releasing the region. This is highly unlikely to
1326ec62d04eSDavid Hildenbrand 	 * fail - let's play save and make it never fail as the caller cannot
1327ec62d04eSDavid Hildenbrand 	 * perform any error handling (e.g., trying to re-add memory will fail
1328ec62d04eSDavid Hildenbrand 	 * similarly).
1329ec62d04eSDavid Hildenbrand 	 */
1330ec62d04eSDavid Hildenbrand retry:
1331ec62d04eSDavid Hildenbrand 	new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1332825f787bSToshi Kani 
1333825f787bSToshi Kani 	p = &parent->child;
1334825f787bSToshi Kani 	write_lock(&resource_lock);
1335825f787bSToshi Kani 
1336825f787bSToshi Kani 	while ((res = *p)) {
1337825f787bSToshi Kani 		if (res->start >= end)
1338825f787bSToshi Kani 			break;
1339825f787bSToshi Kani 
1340825f787bSToshi Kani 		/* look for the next resource if it does not fit into */
1341825f787bSToshi Kani 		if (res->start > start || res->end < end) {
1342825f787bSToshi Kani 			p = &res->sibling;
1343825f787bSToshi Kani 			continue;
1344825f787bSToshi Kani 		}
1345825f787bSToshi Kani 
1346825f787bSToshi Kani 		if (!(res->flags & IORESOURCE_MEM))
1347825f787bSToshi Kani 			break;
1348825f787bSToshi Kani 
1349825f787bSToshi Kani 		if (!(res->flags & IORESOURCE_BUSY)) {
1350825f787bSToshi Kani 			p = &res->child;
1351825f787bSToshi Kani 			continue;
1352825f787bSToshi Kani 		}
1353825f787bSToshi Kani 
1354825f787bSToshi Kani 		/* found the target resource; let's adjust accordingly */
1355825f787bSToshi Kani 		if (res->start == start && res->end == end) {
1356825f787bSToshi Kani 			/* free the whole entry */
1357825f787bSToshi Kani 			*p = res->sibling;
1358ebff7d8fSYasuaki Ishimatsu 			free_resource(res);
1359825f787bSToshi Kani 		} else if (res->start == start && res->end != end) {
1360825f787bSToshi Kani 			/* adjust the start */
1361ec62d04eSDavid Hildenbrand 			WARN_ON_ONCE(__adjust_resource(res, end + 1,
1362ec62d04eSDavid Hildenbrand 						       res->end - end));
1363825f787bSToshi Kani 		} else if (res->start != start && res->end == end) {
1364825f787bSToshi Kani 			/* adjust the end */
1365ec62d04eSDavid Hildenbrand 			WARN_ON_ONCE(__adjust_resource(res, res->start,
1366ec62d04eSDavid Hildenbrand 						       start - res->start));
1367825f787bSToshi Kani 		} else {
1368ec62d04eSDavid Hildenbrand 			/* split into two entries - we need a new resource */
1369825f787bSToshi Kani 			if (!new_res) {
1370ec62d04eSDavid Hildenbrand 				new_res = alloc_resource(GFP_ATOMIC);
1371ec62d04eSDavid Hildenbrand 				if (!new_res) {
1372ec62d04eSDavid Hildenbrand 					alloc_nofail = true;
1373ec62d04eSDavid Hildenbrand 					write_unlock(&resource_lock);
1374ec62d04eSDavid Hildenbrand 					goto retry;
1375ec62d04eSDavid Hildenbrand 				}
1376825f787bSToshi Kani 			}
1377825f787bSToshi Kani 			new_res->name = res->name;
1378825f787bSToshi Kani 			new_res->start = end + 1;
1379825f787bSToshi Kani 			new_res->end = res->end;
1380825f787bSToshi Kani 			new_res->flags = res->flags;
138143ee493bSToshi Kani 			new_res->desc = res->desc;
1382825f787bSToshi Kani 			new_res->parent = res->parent;
1383825f787bSToshi Kani 			new_res->sibling = res->sibling;
1384825f787bSToshi Kani 			new_res->child = NULL;
1385825f787bSToshi Kani 
1386ec62d04eSDavid Hildenbrand 			if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1387ec62d04eSDavid Hildenbrand 							   start - res->start)))
1388825f787bSToshi Kani 				break;
1389825f787bSToshi Kani 			res->sibling = new_res;
1390825f787bSToshi Kani 			new_res = NULL;
1391825f787bSToshi Kani 		}
1392825f787bSToshi Kani 
1393825f787bSToshi Kani 		break;
1394825f787bSToshi Kani 	}
1395825f787bSToshi Kani 
1396825f787bSToshi Kani 	write_unlock(&resource_lock);
1397ebff7d8fSYasuaki Ishimatsu 	free_resource(new_res);
1398825f787bSToshi Kani }
1399825f787bSToshi Kani #endif	/* CONFIG_MEMORY_HOTREMOVE */
1400825f787bSToshi Kani 
14019ca6551eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
system_ram_resources_mergeable(struct resource * r1,struct resource * r2)14029ca6551eSDavid Hildenbrand static bool system_ram_resources_mergeable(struct resource *r1,
14039ca6551eSDavid Hildenbrand 					   struct resource *r2)
14049ca6551eSDavid Hildenbrand {
14059ca6551eSDavid Hildenbrand 	/* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
14069ca6551eSDavid Hildenbrand 	return r1->flags == r2->flags && r1->end + 1 == r2->start &&
14079ca6551eSDavid Hildenbrand 	       r1->name == r2->name && r1->desc == r2->desc &&
14089ca6551eSDavid Hildenbrand 	       !r1->child && !r2->child;
14099ca6551eSDavid Hildenbrand }
14109ca6551eSDavid Hildenbrand 
14113be8da57SMauro Carvalho Chehab /**
14129ca6551eSDavid Hildenbrand  * merge_system_ram_resource - mark the System RAM resource mergeable and try to
14139ca6551eSDavid Hildenbrand  *	merge it with adjacent, mergeable resources
14149ca6551eSDavid Hildenbrand  * @res: resource descriptor
14159ca6551eSDavid Hildenbrand  *
14169ca6551eSDavid Hildenbrand  * This interface is intended for memory hotplug, whereby lots of contiguous
14179ca6551eSDavid Hildenbrand  * system ram resources are added (e.g., via add_memory*()) by a driver, and
14189ca6551eSDavid Hildenbrand  * the actual resource boundaries are not of interest (e.g., it might be
14199ca6551eSDavid Hildenbrand  * relevant for DIMMs). Only resources that are marked mergeable, that have the
14209ca6551eSDavid Hildenbrand  * same parent, and that don't have any children are considered. All mergeable
14219ca6551eSDavid Hildenbrand  * resources must be immutable during the request.
14229ca6551eSDavid Hildenbrand  *
14239ca6551eSDavid Hildenbrand  * Note:
14249ca6551eSDavid Hildenbrand  * - The caller has to make sure that no pointers to resources that are
14259ca6551eSDavid Hildenbrand  *   marked mergeable are used anymore after this call - the resource might
14269ca6551eSDavid Hildenbrand  *   be freed and the pointer might be stale!
14279ca6551eSDavid Hildenbrand  * - release_mem_region_adjustable() will split on demand on memory hotunplug
14289ca6551eSDavid Hildenbrand  */
merge_system_ram_resource(struct resource * res)14299ca6551eSDavid Hildenbrand void merge_system_ram_resource(struct resource *res)
14309ca6551eSDavid Hildenbrand {
14319ca6551eSDavid Hildenbrand 	const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
14329ca6551eSDavid Hildenbrand 	struct resource *cur;
14339ca6551eSDavid Hildenbrand 
14349ca6551eSDavid Hildenbrand 	if (WARN_ON_ONCE((res->flags & flags) != flags))
14359ca6551eSDavid Hildenbrand 		return;
14369ca6551eSDavid Hildenbrand 
14379ca6551eSDavid Hildenbrand 	write_lock(&resource_lock);
14389ca6551eSDavid Hildenbrand 	res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
14399ca6551eSDavid Hildenbrand 
14409ca6551eSDavid Hildenbrand 	/* Try to merge with next item in the list. */
14419ca6551eSDavid Hildenbrand 	cur = res->sibling;
14429ca6551eSDavid Hildenbrand 	if (cur && system_ram_resources_mergeable(res, cur)) {
14439ca6551eSDavid Hildenbrand 		res->end = cur->end;
14449ca6551eSDavid Hildenbrand 		res->sibling = cur->sibling;
14459ca6551eSDavid Hildenbrand 		free_resource(cur);
14469ca6551eSDavid Hildenbrand 	}
14479ca6551eSDavid Hildenbrand 
14489ca6551eSDavid Hildenbrand 	/* Try to merge with previous item in the list. */
14499ca6551eSDavid Hildenbrand 	cur = res->parent->child;
14509ca6551eSDavid Hildenbrand 	while (cur && cur->sibling != res)
14519ca6551eSDavid Hildenbrand 		cur = cur->sibling;
14529ca6551eSDavid Hildenbrand 	if (cur && system_ram_resources_mergeable(cur, res)) {
14539ca6551eSDavid Hildenbrand 		cur->end = res->end;
14549ca6551eSDavid Hildenbrand 		cur->sibling = res->sibling;
14559ca6551eSDavid Hildenbrand 		free_resource(res);
14569ca6551eSDavid Hildenbrand 	}
14579ca6551eSDavid Hildenbrand 	write_unlock(&resource_lock);
14589ca6551eSDavid Hildenbrand }
14599ca6551eSDavid Hildenbrand #endif	/* CONFIG_MEMORY_HOTPLUG */
14609ca6551eSDavid Hildenbrand 
14611da177e4SLinus Torvalds /*
14629ac7849eSTejun Heo  * Managed region resource
14639ac7849eSTejun Heo  */
devm_resource_release(struct device * dev,void * ptr)14648d38821cSThierry Reding static void devm_resource_release(struct device *dev, void *ptr)
14658d38821cSThierry Reding {
14668d38821cSThierry Reding 	struct resource **r = ptr;
14678d38821cSThierry Reding 
14688d38821cSThierry Reding 	release_resource(*r);
14698d38821cSThierry Reding }
14708d38821cSThierry Reding 
14718d38821cSThierry Reding /**
14728d38821cSThierry Reding  * devm_request_resource() - request and reserve an I/O or memory resource
14738d38821cSThierry Reding  * @dev: device for which to request the resource
14748d38821cSThierry Reding  * @root: root of the resource tree from which to request the resource
14758d38821cSThierry Reding  * @new: descriptor of the resource to request
14768d38821cSThierry Reding  *
14778d38821cSThierry Reding  * This is a device-managed version of request_resource(). There is usually
14788d38821cSThierry Reding  * no need to release resources requested by this function explicitly since
14798d38821cSThierry Reding  * that will be taken care of when the device is unbound from its driver.
14808d38821cSThierry Reding  * If for some reason the resource needs to be released explicitly, because
14818d38821cSThierry Reding  * of ordering issues for example, drivers must call devm_release_resource()
14828d38821cSThierry Reding  * rather than the regular release_resource().
14838d38821cSThierry Reding  *
14848d38821cSThierry Reding  * When a conflict is detected between any existing resources and the newly
14858d38821cSThierry Reding  * requested resource, an error message will be printed.
14868d38821cSThierry Reding  *
14878d38821cSThierry Reding  * Returns 0 on success or a negative error code on failure.
14888d38821cSThierry Reding  */
devm_request_resource(struct device * dev,struct resource * root,struct resource * new)14898d38821cSThierry Reding int devm_request_resource(struct device *dev, struct resource *root,
14908d38821cSThierry Reding 			  struct resource *new)
14918d38821cSThierry Reding {
14928d38821cSThierry Reding 	struct resource *conflict, **ptr;
14938d38821cSThierry Reding 
14948d38821cSThierry Reding 	ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
14958d38821cSThierry Reding 	if (!ptr)
14968d38821cSThierry Reding 		return -ENOMEM;
14978d38821cSThierry Reding 
14988d38821cSThierry Reding 	*ptr = new;
14998d38821cSThierry Reding 
15008d38821cSThierry Reding 	conflict = request_resource_conflict(root, new);
15018d38821cSThierry Reding 	if (conflict) {
15028d38821cSThierry Reding 		dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
15038d38821cSThierry Reding 			new, conflict->name, conflict);
15048d38821cSThierry Reding 		devres_free(ptr);
15058d38821cSThierry Reding 		return -EBUSY;
15068d38821cSThierry Reding 	}
15078d38821cSThierry Reding 
15088d38821cSThierry Reding 	devres_add(dev, ptr);
15098d38821cSThierry Reding 	return 0;
15108d38821cSThierry Reding }
15118d38821cSThierry Reding EXPORT_SYMBOL(devm_request_resource);
15128d38821cSThierry Reding 
devm_resource_match(struct device * dev,void * res,void * data)15138d38821cSThierry Reding static int devm_resource_match(struct device *dev, void *res, void *data)
15148d38821cSThierry Reding {
15158d38821cSThierry Reding 	struct resource **ptr = res;
15168d38821cSThierry Reding 
15178d38821cSThierry Reding 	return *ptr == data;
15188d38821cSThierry Reding }
15198d38821cSThierry Reding 
15208d38821cSThierry Reding /**
15218d38821cSThierry Reding  * devm_release_resource() - release a previously requested resource
15228d38821cSThierry Reding  * @dev: device for which to release the resource
15238d38821cSThierry Reding  * @new: descriptor of the resource to release
15248d38821cSThierry Reding  *
15258d38821cSThierry Reding  * Releases a resource previously requested using devm_request_resource().
15268d38821cSThierry Reding  */
devm_release_resource(struct device * dev,struct resource * new)15278d38821cSThierry Reding void devm_release_resource(struct device *dev, struct resource *new)
15288d38821cSThierry Reding {
15298d38821cSThierry Reding 	WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
15308d38821cSThierry Reding 			       new));
15318d38821cSThierry Reding }
15328d38821cSThierry Reding EXPORT_SYMBOL(devm_release_resource);
15338d38821cSThierry Reding 
15349ac7849eSTejun Heo struct region_devres {
15359ac7849eSTejun Heo 	struct resource *parent;
15369ac7849eSTejun Heo 	resource_size_t start;
15379ac7849eSTejun Heo 	resource_size_t n;
15389ac7849eSTejun Heo };
15399ac7849eSTejun Heo 
devm_region_release(struct device * dev,void * res)15409ac7849eSTejun Heo static void devm_region_release(struct device *dev, void *res)
15419ac7849eSTejun Heo {
15429ac7849eSTejun Heo 	struct region_devres *this = res;
15439ac7849eSTejun Heo 
15449ac7849eSTejun Heo 	__release_region(this->parent, this->start, this->n);
15459ac7849eSTejun Heo }
15469ac7849eSTejun Heo 
devm_region_match(struct device * dev,void * res,void * match_data)15479ac7849eSTejun Heo static int devm_region_match(struct device *dev, void *res, void *match_data)
15489ac7849eSTejun Heo {
15499ac7849eSTejun Heo 	struct region_devres *this = res, *match = match_data;
15509ac7849eSTejun Heo 
15519ac7849eSTejun Heo 	return this->parent == match->parent &&
15529ac7849eSTejun Heo 		this->start == match->start && this->n == match->n;
15539ac7849eSTejun Heo }
15549ac7849eSTejun Heo 
1555b69c2e20SBorislav Petkov struct resource *
__devm_request_region(struct device * dev,struct resource * parent,resource_size_t start,resource_size_t n,const char * name)1556b69c2e20SBorislav Petkov __devm_request_region(struct device *dev, struct resource *parent,
1557b69c2e20SBorislav Petkov 		      resource_size_t start, resource_size_t n, const char *name)
15589ac7849eSTejun Heo {
15599ac7849eSTejun Heo 	struct region_devres *dr = NULL;
15609ac7849eSTejun Heo 	struct resource *res;
15619ac7849eSTejun Heo 
15629ac7849eSTejun Heo 	dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
15639ac7849eSTejun Heo 			  GFP_KERNEL);
15649ac7849eSTejun Heo 	if (!dr)
15659ac7849eSTejun Heo 		return NULL;
15669ac7849eSTejun Heo 
15679ac7849eSTejun Heo 	dr->parent = parent;
15689ac7849eSTejun Heo 	dr->start = start;
15699ac7849eSTejun Heo 	dr->n = n;
15709ac7849eSTejun Heo 
1571e8de1481SArjan van de Ven 	res = __request_region(parent, start, n, name, 0);
15729ac7849eSTejun Heo 	if (res)
15739ac7849eSTejun Heo 		devres_add(dev, dr);
15749ac7849eSTejun Heo 	else
15759ac7849eSTejun Heo 		devres_free(dr);
15769ac7849eSTejun Heo 
15779ac7849eSTejun Heo 	return res;
15789ac7849eSTejun Heo }
15799ac7849eSTejun Heo EXPORT_SYMBOL(__devm_request_region);
15809ac7849eSTejun Heo 
__devm_release_region(struct device * dev,struct resource * parent,resource_size_t start,resource_size_t n)15819ac7849eSTejun Heo void __devm_release_region(struct device *dev, struct resource *parent,
15829ac7849eSTejun Heo 			   resource_size_t start, resource_size_t n)
15839ac7849eSTejun Heo {
15849ac7849eSTejun Heo 	struct region_devres match_data = { parent, start, n };
15859ac7849eSTejun Heo 
15869ac7849eSTejun Heo 	__release_region(parent, start, n);
15879ac7849eSTejun Heo 	WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
15889ac7849eSTejun Heo 			       &match_data));
15899ac7849eSTejun Heo }
15909ac7849eSTejun Heo EXPORT_SYMBOL(__devm_release_region);
15919ac7849eSTejun Heo 
15929ac7849eSTejun Heo /*
1593ffd2e8dfSBjorn Helgaas  * Reserve I/O ports or memory based on "reserve=" kernel parameter.
15941da177e4SLinus Torvalds  */
15951da177e4SLinus Torvalds #define MAXRESERVE 4
reserve_setup(char * str)15961da177e4SLinus Torvalds static int __init reserve_setup(char *str)
15971da177e4SLinus Torvalds {
15981da177e4SLinus Torvalds 	static int reserved;
15991da177e4SLinus Torvalds 	static struct resource reserve[MAXRESERVE];
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	for (;;) {
16028bc1ad7dSZhang Rui 		unsigned int io_start, io_num;
16031da177e4SLinus Torvalds 		int x = reserved;
1604ffd2e8dfSBjorn Helgaas 		struct resource *parent;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 		if (get_option(&str, &io_start) != 2)
16071da177e4SLinus Torvalds 			break;
16081da177e4SLinus Torvalds 		if (get_option(&str, &io_num) == 0)
16091da177e4SLinus Torvalds 			break;
16101da177e4SLinus Torvalds 		if (x < MAXRESERVE) {
16111da177e4SLinus Torvalds 			struct resource *res = reserve + x;
1612ffd2e8dfSBjorn Helgaas 
1613ffd2e8dfSBjorn Helgaas 			/*
1614ffd2e8dfSBjorn Helgaas 			 * If the region starts below 0x10000, we assume it's
1615ffd2e8dfSBjorn Helgaas 			 * I/O port space; otherwise assume it's memory.
1616ffd2e8dfSBjorn Helgaas 			 */
1617ffd2e8dfSBjorn Helgaas 			if (io_start < 0x10000) {
1618ffd2e8dfSBjorn Helgaas 				res->flags = IORESOURCE_IO;
1619ffd2e8dfSBjorn Helgaas 				parent = &ioport_resource;
1620ffd2e8dfSBjorn Helgaas 			} else {
1621ffd2e8dfSBjorn Helgaas 				res->flags = IORESOURCE_MEM;
1622ffd2e8dfSBjorn Helgaas 				parent = &iomem_resource;
1623ffd2e8dfSBjorn Helgaas 			}
16241da177e4SLinus Torvalds 			res->name = "reserved";
16251da177e4SLinus Torvalds 			res->start = io_start;
16261da177e4SLinus Torvalds 			res->end = io_start + io_num - 1;
1627ffd2e8dfSBjorn Helgaas 			res->flags |= IORESOURCE_BUSY;
162843ee493bSToshi Kani 			res->desc = IORES_DESC_NONE;
16291da177e4SLinus Torvalds 			res->child = NULL;
1630ffd2e8dfSBjorn Helgaas 			if (request_resource(parent, res) == 0)
16311da177e4SLinus Torvalds 				reserved = x+1;
16321da177e4SLinus Torvalds 		}
16331da177e4SLinus Torvalds 	}
16341da177e4SLinus Torvalds 	return 1;
16351da177e4SLinus Torvalds }
16361da177e4SLinus Torvalds __setup("reserve=", reserve_setup);
1637379daf62SSuresh Siddha 
1638379daf62SSuresh Siddha /*
1639379daf62SSuresh Siddha  * Check if the requested addr and size spans more than any slot in the
1640379daf62SSuresh Siddha  * iomem resource tree.
1641379daf62SSuresh Siddha  */
iomem_map_sanity_check(resource_size_t addr,unsigned long size)1642379daf62SSuresh Siddha int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1643379daf62SSuresh Siddha {
1644379daf62SSuresh Siddha 	struct resource *p = &iomem_resource;
16452a4e6285SAndy Shevchenko 	resource_size_t end = addr + size - 1;
1646379daf62SSuresh Siddha 	int err = 0;
1647379daf62SSuresh Siddha 	loff_t l;
1648379daf62SSuresh Siddha 
1649379daf62SSuresh Siddha 	read_lock(&resource_lock);
1650379daf62SSuresh Siddha 	for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1651379daf62SSuresh Siddha 		/*
1652379daf62SSuresh Siddha 		 * We can probably skip the resources without
1653379daf62SSuresh Siddha 		 * IORESOURCE_IO attribute?
1654379daf62SSuresh Siddha 		 */
16552a4e6285SAndy Shevchenko 		if (p->start > end)
1656379daf62SSuresh Siddha 			continue;
1657379daf62SSuresh Siddha 		if (p->end < addr)
1658379daf62SSuresh Siddha 			continue;
1659d68612b2SSuresh Siddha 		if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
16602a4e6285SAndy Shevchenko 		    PFN_DOWN(p->end) >= PFN_DOWN(end))
1661379daf62SSuresh Siddha 			continue;
16623ac52669SArjan van de Ven 		/*
16633ac52669SArjan van de Ven 		 * if a resource is "BUSY", it's not a hardware resource
16643ac52669SArjan van de Ven 		 * but a driver mapping of such a resource; we don't want
16653ac52669SArjan van de Ven 		 * to warn for those; some drivers legitimately map only
16663ac52669SArjan van de Ven 		 * partial hardware resources. (example: vesafb)
16673ac52669SArjan van de Ven 		 */
16683ac52669SArjan van de Ven 		if (p->flags & IORESOURCE_BUSY)
16693ac52669SArjan van de Ven 			continue;
16703ac52669SArjan van de Ven 
16712a4e6285SAndy Shevchenko 		pr_warn("resource sanity check: requesting [mem %pa-%pa], which spans more than %s %pR\n",
16722a4e6285SAndy Shevchenko 			&addr, &end, p->name, p);
1673379daf62SSuresh Siddha 		err = -1;
1674379daf62SSuresh Siddha 		break;
1675379daf62SSuresh Siddha 	}
1676379daf62SSuresh Siddha 	read_unlock(&resource_lock);
1677379daf62SSuresh Siddha 
1678379daf62SSuresh Siddha 	return err;
1679379daf62SSuresh Siddha }
1680e8de1481SArjan van de Ven 
1681e8de1481SArjan van de Ven #ifdef CONFIG_STRICT_DEVMEM
1682e8de1481SArjan van de Ven static int strict_iomem_checks = 1;
1683e8de1481SArjan van de Ven #else
1684e8de1481SArjan van de Ven static int strict_iomem_checks;
1685e8de1481SArjan van de Ven #endif
1686e8de1481SArjan van de Ven 
1687e8de1481SArjan van de Ven /*
1688a9e7b8d4SDavid Hildenbrand  * Check if an address is exclusive to the kernel and must not be mapped to
1689a9e7b8d4SDavid Hildenbrand  * user space, for example, via /dev/mem.
1690a9e7b8d4SDavid Hildenbrand  *
1691a9e7b8d4SDavid Hildenbrand  * Returns true if exclusive to the kernel, otherwise returns false.
1692e8de1481SArjan van de Ven  */
resource_is_exclusive(struct resource * root,u64 addr,resource_size_t size)169327829479SIra Weiny bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size)
1694e8de1481SArjan van de Ven {
1695a9e7b8d4SDavid Hildenbrand 	const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
1696a9e7b8d4SDavid Hildenbrand 						  IORESOURCE_EXCLUSIVE;
1697b78dfa05SDavid Hildenbrand 	bool skip_children = false, err = false;
1698b78dfa05SDavid Hildenbrand 	struct resource *p;
1699e8de1481SArjan van de Ven 
1700e8de1481SArjan van de Ven 	read_lock(&resource_lock);
170127829479SIra Weiny 	for_each_resource(root, p, skip_children) {
1702e8de1481SArjan van de Ven 		if (p->start >= addr + size)
1703e8de1481SArjan van de Ven 			break;
1704b78dfa05SDavid Hildenbrand 		if (p->end < addr) {
1705b78dfa05SDavid Hildenbrand 			skip_children = true;
1706e8de1481SArjan van de Ven 			continue;
1707b78dfa05SDavid Hildenbrand 		}
1708b78dfa05SDavid Hildenbrand 		skip_children = false;
1709b78dfa05SDavid Hildenbrand 
171090a545e9SDan Williams 		/*
1711a9e7b8d4SDavid Hildenbrand 		 * IORESOURCE_SYSTEM_RAM resources are exclusive if
1712a9e7b8d4SDavid Hildenbrand 		 * IORESOURCE_EXCLUSIVE is set, even if they
1713a9e7b8d4SDavid Hildenbrand 		 * are not busy and even if "iomem=relaxed" is set. The
1714a9e7b8d4SDavid Hildenbrand 		 * responsible driver dynamically adds/removes system RAM within
1715a9e7b8d4SDavid Hildenbrand 		 * such an area and uncontrolled access is dangerous.
1716a9e7b8d4SDavid Hildenbrand 		 */
1717a9e7b8d4SDavid Hildenbrand 		if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
1718a9e7b8d4SDavid Hildenbrand 			err = true;
1719a9e7b8d4SDavid Hildenbrand 			break;
1720a9e7b8d4SDavid Hildenbrand 		}
1721a9e7b8d4SDavid Hildenbrand 
1722a9e7b8d4SDavid Hildenbrand 		/*
172390a545e9SDan Williams 		 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
172490a545e9SDan Williams 		 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
172590a545e9SDan Williams 		 * resource is busy.
172690a545e9SDan Williams 		 */
1727a9e7b8d4SDavid Hildenbrand 		if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
172890a545e9SDan Williams 			continue;
172990a545e9SDan Williams 		if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
173090a545e9SDan Williams 				|| p->flags & IORESOURCE_EXCLUSIVE) {
17319825b451SYaowei Bai 			err = true;
1732e8de1481SArjan van de Ven 			break;
1733e8de1481SArjan van de Ven 		}
1734e8de1481SArjan van de Ven 	}
1735e8de1481SArjan van de Ven 	read_unlock(&resource_lock);
1736e8de1481SArjan van de Ven 
1737e8de1481SArjan van de Ven 	return err;
1738e8de1481SArjan van de Ven }
1739e8de1481SArjan van de Ven 
iomem_is_exclusive(u64 addr)174027829479SIra Weiny bool iomem_is_exclusive(u64 addr)
174127829479SIra Weiny {
174227829479SIra Weiny 	return resource_is_exclusive(&iomem_resource, addr & PAGE_MASK,
174327829479SIra Weiny 				     PAGE_SIZE);
174427829479SIra Weiny }
174527829479SIra Weiny 
resource_list_create_entry(struct resource * res,size_t extra_size)174690e97820SJiang Liu struct resource_entry *resource_list_create_entry(struct resource *res,
174790e97820SJiang Liu 						  size_t extra_size)
174890e97820SJiang Liu {
174990e97820SJiang Liu 	struct resource_entry *entry;
175090e97820SJiang Liu 
175190e97820SJiang Liu 	entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
175290e97820SJiang Liu 	if (entry) {
175390e97820SJiang Liu 		INIT_LIST_HEAD(&entry->node);
175490e97820SJiang Liu 		entry->res = res ? res : &entry->__res;
175590e97820SJiang Liu 	}
175690e97820SJiang Liu 
175790e97820SJiang Liu 	return entry;
175890e97820SJiang Liu }
175990e97820SJiang Liu EXPORT_SYMBOL(resource_list_create_entry);
176090e97820SJiang Liu 
resource_list_free(struct list_head * head)176190e97820SJiang Liu void resource_list_free(struct list_head *head)
176290e97820SJiang Liu {
176390e97820SJiang Liu 	struct resource_entry *entry, *tmp;
176490e97820SJiang Liu 
176590e97820SJiang Liu 	list_for_each_entry_safe(entry, tmp, head, node)
176690e97820SJiang Liu 		resource_list_destroy_entry(entry);
176790e97820SJiang Liu }
176890e97820SJiang Liu EXPORT_SYMBOL(resource_list_free);
176990e97820SJiang Liu 
177014b80582SDan Williams #ifdef CONFIG_GET_FREE_REGION
177114b80582SDan Williams #define GFR_DESCENDING		(1UL << 0)
177214b80582SDan Williams #define GFR_REQUEST_REGION	(1UL << 1)
177314b80582SDan Williams #define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT)
177414b80582SDan Williams 
gfr_start(struct resource * base,resource_size_t size,resource_size_t align,unsigned long flags)177514b80582SDan Williams static resource_size_t gfr_start(struct resource *base, resource_size_t size,
177614b80582SDan Williams 				 resource_size_t align, unsigned long flags)
17770c385190SChristoph Hellwig {
177814b80582SDan Williams 	if (flags & GFR_DESCENDING) {
177914b80582SDan Williams 		resource_size_t end;
178014b80582SDan Williams 
178114b80582SDan Williams 		end = min_t(resource_size_t, base->end,
178214b80582SDan Williams 			    (1ULL << MAX_PHYSMEM_BITS) - 1);
178314b80582SDan Williams 		return end - size + 1;
178414b80582SDan Williams 	}
178514b80582SDan Williams 
178614b80582SDan Williams 	return ALIGN(base->start, align);
178714b80582SDan Williams }
178814b80582SDan Williams 
gfr_continue(struct resource * base,resource_size_t addr,resource_size_t size,unsigned long flags)178914b80582SDan Williams static bool gfr_continue(struct resource *base, resource_size_t addr,
179014b80582SDan Williams 			 resource_size_t size, unsigned long flags)
179114b80582SDan Williams {
179214b80582SDan Williams 	if (flags & GFR_DESCENDING)
179314b80582SDan Williams 		return addr > size && addr >= base->start;
179414b80582SDan Williams 	/*
179514b80582SDan Williams 	 * In the ascend case be careful that the last increment by
179614b80582SDan Williams 	 * @size did not wrap 0.
179714b80582SDan Williams 	 */
179814b80582SDan Williams 	return addr > addr - size &&
179914b80582SDan Williams 	       addr <= min_t(resource_size_t, base->end,
180014b80582SDan Williams 			     (1ULL << MAX_PHYSMEM_BITS) - 1);
180114b80582SDan Williams }
180214b80582SDan Williams 
gfr_next(resource_size_t addr,resource_size_t size,unsigned long flags)180314b80582SDan Williams static resource_size_t gfr_next(resource_size_t addr, resource_size_t size,
180414b80582SDan Williams 				unsigned long flags)
180514b80582SDan Williams {
180614b80582SDan Williams 	if (flags & GFR_DESCENDING)
180714b80582SDan Williams 		return addr - size;
180814b80582SDan Williams 	return addr + size;
180914b80582SDan Williams }
181014b80582SDan Williams 
remove_free_mem_region(void * _res)181114b80582SDan Williams static void remove_free_mem_region(void *_res)
181214b80582SDan Williams {
181314b80582SDan Williams 	struct resource *res = _res;
181414b80582SDan Williams 
181514b80582SDan Williams 	if (res->parent)
181614b80582SDan Williams 		remove_resource(res);
181714b80582SDan Williams 	free_resource(res);
181814b80582SDan Williams }
181914b80582SDan Williams 
182014b80582SDan Williams static struct resource *
get_free_mem_region(struct device * dev,struct resource * base,resource_size_t size,const unsigned long align,const char * name,const unsigned long desc,const unsigned long flags)182114b80582SDan Williams get_free_mem_region(struct device *dev, struct resource *base,
182214b80582SDan Williams 		    resource_size_t size, const unsigned long align,
182314b80582SDan Williams 		    const char *name, const unsigned long desc,
182414b80582SDan Williams 		    const unsigned long flags)
182514b80582SDan Williams {
182614b80582SDan Williams 	resource_size_t addr;
18270c385190SChristoph Hellwig 	struct resource *res;
182856fd9491SAlistair Popple 	struct region_devres *dr = NULL;
18290c385190SChristoph Hellwig 
183014b80582SDan Williams 	size = ALIGN(size, align);
18310c385190SChristoph Hellwig 
183256fd9491SAlistair Popple 	res = alloc_resource(GFP_KERNEL);
183356fd9491SAlistair Popple 	if (!res)
183456fd9491SAlistair Popple 		return ERR_PTR(-ENOMEM);
183556fd9491SAlistair Popple 
183614b80582SDan Williams 	if (dev && (flags & GFR_REQUEST_REGION)) {
183756fd9491SAlistair Popple 		dr = devres_alloc(devm_region_release,
183856fd9491SAlistair Popple 				sizeof(struct region_devres), GFP_KERNEL);
183956fd9491SAlistair Popple 		if (!dr) {
184056fd9491SAlistair Popple 			free_resource(res);
184156fd9491SAlistair Popple 			return ERR_PTR(-ENOMEM);
184256fd9491SAlistair Popple 		}
184314b80582SDan Williams 	} else if (dev) {
184414b80582SDan Williams 		if (devm_add_action_or_reset(dev, remove_free_mem_region, res))
184514b80582SDan Williams 			return ERR_PTR(-ENOMEM);
184656fd9491SAlistair Popple 	}
184756fd9491SAlistair Popple 
184856fd9491SAlistair Popple 	write_lock(&resource_lock);
184914b80582SDan Williams 	for (addr = gfr_start(base, size, align, flags);
1850*e4a5b2f6SAlison Schofield 	     gfr_continue(base, addr, align, flags);
1851*e4a5b2f6SAlison Schofield 	     addr = gfr_next(addr, align, flags)) {
185214b80582SDan Williams 		if (__region_intersects(base, addr, size, 0, IORES_DESC_NONE) !=
18530c385190SChristoph Hellwig 		    REGION_DISJOINT)
18540c385190SChristoph Hellwig 			continue;
18550c385190SChristoph Hellwig 
185614b80582SDan Williams 		if (flags & GFR_REQUEST_REGION) {
185714b80582SDan Williams 			if (__request_region_locked(res, &iomem_resource, addr,
185814b80582SDan Williams 						    size, name, 0))
185956fd9491SAlistair Popple 				break;
186056fd9491SAlistair Popple 
186156fd9491SAlistair Popple 			if (dev) {
186256fd9491SAlistair Popple 				dr->parent = &iomem_resource;
186356fd9491SAlistair Popple 				dr->start = addr;
186456fd9491SAlistair Popple 				dr->n = size;
186556fd9491SAlistair Popple 				devres_add(dev, dr);
186656fd9491SAlistair Popple 			}
186756fd9491SAlistair Popple 
186814b80582SDan Williams 			res->desc = desc;
186956fd9491SAlistair Popple 			write_unlock(&resource_lock);
187056fd9491SAlistair Popple 
187114b80582SDan Williams 
187256fd9491SAlistair Popple 			/*
187314b80582SDan Williams 			 * A driver is claiming this region so revoke any
187414b80582SDan Williams 			 * mappings.
187556fd9491SAlistair Popple 			 */
187656fd9491SAlistair Popple 			revoke_iomem(res);
187714b80582SDan Williams 		} else {
187814b80582SDan Williams 			res->start = addr;
187914b80582SDan Williams 			res->end = addr + size - 1;
188014b80582SDan Williams 			res->name = name;
188114b80582SDan Williams 			res->desc = desc;
188214b80582SDan Williams 			res->flags = IORESOURCE_MEM;
188314b80582SDan Williams 
188414b80582SDan Williams 			/*
188514b80582SDan Williams 			 * Only succeed if the resource hosts an exclusive
188614b80582SDan Williams 			 * range after the insert
188714b80582SDan Williams 			 */
188814b80582SDan Williams 			if (__insert_resource(base, res) || res->child)
188914b80582SDan Williams 				break;
189014b80582SDan Williams 
189114b80582SDan Williams 			write_unlock(&resource_lock);
189214b80582SDan Williams 		}
189314b80582SDan Williams 
18940c385190SChristoph Hellwig 		return res;
18950c385190SChristoph Hellwig 	}
189656fd9491SAlistair Popple 	write_unlock(&resource_lock);
189756fd9491SAlistair Popple 
189814b80582SDan Williams 	if (flags & GFR_REQUEST_REGION) {
189956fd9491SAlistair Popple 		free_resource(res);
190056fd9491SAlistair Popple 		devres_free(dr);
190114b80582SDan Williams 	} else if (dev)
190214b80582SDan Williams 		devm_release_action(dev, remove_free_mem_region, res);
19030c385190SChristoph Hellwig 
19040c385190SChristoph Hellwig 	return ERR_PTR(-ERANGE);
19050c385190SChristoph Hellwig }
19060c385190SChristoph Hellwig 
19070092908dSChristoph Hellwig /**
19080092908dSChristoph Hellwig  * devm_request_free_mem_region - find free region for device private memory
19090092908dSChristoph Hellwig  *
19100092908dSChristoph Hellwig  * @dev: device struct to bind the resource to
19110092908dSChristoph Hellwig  * @size: size in bytes of the device memory to add
19120092908dSChristoph Hellwig  * @base: resource tree to look in
19130092908dSChristoph Hellwig  *
19140092908dSChristoph Hellwig  * This function tries to find an empty range of physical address big enough to
19150092908dSChristoph Hellwig  * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
19160092908dSChristoph Hellwig  * memory, which in turn allocates struct pages.
19170092908dSChristoph Hellwig  */
devm_request_free_mem_region(struct device * dev,struct resource * base,unsigned long size)19180092908dSChristoph Hellwig struct resource *devm_request_free_mem_region(struct device *dev,
19190092908dSChristoph Hellwig 		struct resource *base, unsigned long size)
19200092908dSChristoph Hellwig {
192114b80582SDan Williams 	unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
192214b80582SDan Williams 
192314b80582SDan Williams 	return get_free_mem_region(dev, base, size, GFR_DEFAULT_ALIGN,
192414b80582SDan Williams 				   dev_name(dev),
192514b80582SDan Williams 				   IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
19260092908dSChristoph Hellwig }
19270092908dSChristoph Hellwig EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
19280c385190SChristoph Hellwig 
request_free_mem_region(struct resource * base,unsigned long size,const char * name)19290c385190SChristoph Hellwig struct resource *request_free_mem_region(struct resource *base,
19300c385190SChristoph Hellwig 		unsigned long size, const char *name)
19310c385190SChristoph Hellwig {
193214b80582SDan Williams 	unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
193314b80582SDan Williams 
193414b80582SDan Williams 	return get_free_mem_region(NULL, base, size, GFR_DEFAULT_ALIGN, name,
193514b80582SDan Williams 				   IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
19360c385190SChristoph Hellwig }
19370c385190SChristoph Hellwig EXPORT_SYMBOL_GPL(request_free_mem_region);
19380c385190SChristoph Hellwig 
193914b80582SDan Williams /**
194014b80582SDan Williams  * alloc_free_mem_region - find a free region relative to @base
194114b80582SDan Williams  * @base: resource that will parent the new resource
194214b80582SDan Williams  * @size: size in bytes of memory to allocate from @base
194314b80582SDan Williams  * @align: alignment requirements for the allocation
194414b80582SDan Williams  * @name: resource name
194514b80582SDan Williams  *
194614b80582SDan Williams  * Buses like CXL, that can dynamically instantiate new memory regions,
194714b80582SDan Williams  * need a method to allocate physical address space for those regions.
194814b80582SDan Williams  * Allocate and insert a new resource to cover a free, unclaimed by a
194914b80582SDan Williams  * descendant of @base, range in the span of @base.
195014b80582SDan Williams  */
alloc_free_mem_region(struct resource * base,unsigned long size,unsigned long align,const char * name)195114b80582SDan Williams struct resource *alloc_free_mem_region(struct resource *base,
195214b80582SDan Williams 				       unsigned long size, unsigned long align,
195314b80582SDan Williams 				       const char *name)
195414b80582SDan Williams {
195514b80582SDan Williams 	/* Default of ascending direction and insert resource */
195614b80582SDan Williams 	unsigned long flags = 0;
195714b80582SDan Williams 
195814b80582SDan Williams 	return get_free_mem_region(NULL, base, size, align, name,
195914b80582SDan Williams 				   IORES_DESC_NONE, flags);
196014b80582SDan Williams }
196114b80582SDan Williams EXPORT_SYMBOL_NS_GPL(alloc_free_mem_region, CXL);
196214b80582SDan Williams #endif /* CONFIG_GET_FREE_REGION */
19630092908dSChristoph Hellwig 
strict_iomem(char * str)1964e8de1481SArjan van de Ven static int __init strict_iomem(char *str)
1965e8de1481SArjan van de Ven {
1966e8de1481SArjan van de Ven 	if (strstr(str, "relaxed"))
1967e8de1481SArjan van de Ven 		strict_iomem_checks = 0;
1968e8de1481SArjan van de Ven 	if (strstr(str, "strict"))
1969e8de1481SArjan van de Ven 		strict_iomem_checks = 1;
1970e8de1481SArjan van de Ven 	return 1;
1971e8de1481SArjan van de Ven }
1972e8de1481SArjan van de Ven 
iomem_fs_init_fs_context(struct fs_context * fc)197371a1d8edSDaniel Vetter static int iomem_fs_init_fs_context(struct fs_context *fc)
197471a1d8edSDaniel Vetter {
197571a1d8edSDaniel Vetter 	return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
197671a1d8edSDaniel Vetter }
197771a1d8edSDaniel Vetter 
197871a1d8edSDaniel Vetter static struct file_system_type iomem_fs_type = {
197971a1d8edSDaniel Vetter 	.name		= "iomem",
198071a1d8edSDaniel Vetter 	.owner		= THIS_MODULE,
198171a1d8edSDaniel Vetter 	.init_fs_context = iomem_fs_init_fs_context,
198271a1d8edSDaniel Vetter 	.kill_sb	= kill_anon_super,
198371a1d8edSDaniel Vetter };
198471a1d8edSDaniel Vetter 
iomem_init_inode(void)198571a1d8edSDaniel Vetter static int __init iomem_init_inode(void)
198671a1d8edSDaniel Vetter {
198771a1d8edSDaniel Vetter 	static struct vfsmount *iomem_vfs_mount;
198871a1d8edSDaniel Vetter 	static int iomem_fs_cnt;
198971a1d8edSDaniel Vetter 	struct inode *inode;
199071a1d8edSDaniel Vetter 	int rc;
199171a1d8edSDaniel Vetter 
199271a1d8edSDaniel Vetter 	rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
199371a1d8edSDaniel Vetter 	if (rc < 0) {
199471a1d8edSDaniel Vetter 		pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
199571a1d8edSDaniel Vetter 		return rc;
199671a1d8edSDaniel Vetter 	}
199771a1d8edSDaniel Vetter 
199871a1d8edSDaniel Vetter 	inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
199971a1d8edSDaniel Vetter 	if (IS_ERR(inode)) {
200071a1d8edSDaniel Vetter 		rc = PTR_ERR(inode);
200171a1d8edSDaniel Vetter 		pr_err("Cannot allocate inode for iomem: %d\n", rc);
200271a1d8edSDaniel Vetter 		simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
200371a1d8edSDaniel Vetter 		return rc;
200471a1d8edSDaniel Vetter 	}
200571a1d8edSDaniel Vetter 
200671a1d8edSDaniel Vetter 	/*
200771a1d8edSDaniel Vetter 	 * Publish iomem revocation inode initialized.
200871a1d8edSDaniel Vetter 	 * Pairs with smp_load_acquire() in revoke_iomem().
200971a1d8edSDaniel Vetter 	 */
201071a1d8edSDaniel Vetter 	smp_store_release(&iomem_inode, inode);
201171a1d8edSDaniel Vetter 
201271a1d8edSDaniel Vetter 	return 0;
201371a1d8edSDaniel Vetter }
201471a1d8edSDaniel Vetter 
201571a1d8edSDaniel Vetter fs_initcall(iomem_init_inode);
201671a1d8edSDaniel Vetter 
2017e8de1481SArjan van de Ven __setup("iomem=", strict_iomem);
2018