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 {
496*393331e1SHuang Ying resource_size_t ostart, oend;
497d486ccb2SAlistair Popple int type = 0; int other = 0;
498*393331e1SHuang Ying struct resource *p, *dp;
499*393331e1SHuang Ying bool is_type, covered;
500*393331e1SHuang Ying struct resource res;
501d486ccb2SAlistair Popple
502d486ccb2SAlistair Popple res.start = start;
503d486ccb2SAlistair Popple res.end = start + size - 1;
504d486ccb2SAlistair Popple
50514b80582SDan Williams for (p = parent->child; p ; p = p->sibling) {
506*393331e1SHuang Ying if (!resource_overlaps(p, &res))
507*393331e1SHuang Ying continue;
508*393331e1SHuang Ying is_type = (p->flags & flags) == flags &&
509*393331e1SHuang Ying (desc == IORES_DESC_NONE || desc == p->desc);
510*393331e1SHuang Ying if (is_type) {
511*393331e1SHuang Ying type++;
512*393331e1SHuang Ying continue;
513*393331e1SHuang Ying }
514*393331e1SHuang Ying /*
515*393331e1SHuang Ying * Continue to search in descendant resources as if the
516*393331e1SHuang Ying * matched descendant resources cover some ranges of 'p'.
517*393331e1SHuang Ying *
518*393331e1SHuang Ying * |------------- "CXL Window 0" ------------|
519*393331e1SHuang Ying * |-- "System RAM" --|
520*393331e1SHuang Ying *
521*393331e1SHuang Ying * will behave similar as the following fake resource
522*393331e1SHuang Ying * tree when searching "System RAM".
523*393331e1SHuang Ying *
524*393331e1SHuang Ying * |-- "System RAM" --||-- "CXL Window 0a" --|
525*393331e1SHuang Ying */
526*393331e1SHuang Ying covered = false;
527*393331e1SHuang Ying ostart = max(res.start, p->start);
528*393331e1SHuang Ying oend = min(res.end, p->end);
529*393331e1SHuang Ying for_each_resource(p, dp, false) {
530*393331e1SHuang Ying if (!resource_overlaps(dp, &res))
531*393331e1SHuang Ying continue;
532*393331e1SHuang Ying is_type = (dp->flags & flags) == flags &&
533*393331e1SHuang Ying (desc == IORES_DESC_NONE || desc == dp->desc);
534*393331e1SHuang Ying if (is_type) {
535*393331e1SHuang Ying type++;
536*393331e1SHuang Ying /*
537*393331e1SHuang Ying * Range from 'ostart' to 'dp->start'
538*393331e1SHuang Ying * isn't covered by matched resource.
539*393331e1SHuang Ying */
540*393331e1SHuang Ying if (dp->start > ostart)
541*393331e1SHuang Ying break;
542*393331e1SHuang Ying if (dp->end >= oend) {
543*393331e1SHuang Ying covered = true;
544*393331e1SHuang Ying break;
545*393331e1SHuang Ying }
546*393331e1SHuang Ying /* Remove covered range */
547*393331e1SHuang Ying ostart = max(ostart, dp->end + 1);
548*393331e1SHuang Ying }
549*393331e1SHuang Ying }
550*393331e1SHuang Ying if (!covered)
551*393331e1SHuang Ying other++;
552d486ccb2SAlistair Popple }
553d486ccb2SAlistair Popple
554d486ccb2SAlistair Popple if (type == 0)
555d486ccb2SAlistair Popple return REGION_DISJOINT;
556d486ccb2SAlistair Popple
557d486ccb2SAlistair Popple if (other == 0)
558d486ccb2SAlistair Popple return REGION_INTERSECTS;
559d486ccb2SAlistair Popple
560d486ccb2SAlistair Popple return REGION_MIXED;
561d486ccb2SAlistair Popple }
562d486ccb2SAlistair Popple
563124fe20dSDan Williams /**
564124fe20dSDan Williams * region_intersects() - determine intersection of region with known resources
565124fe20dSDan Williams * @start: region start address
566124fe20dSDan Williams * @size: size of region
5671c29f25bSToshi Kani * @flags: flags of resource (in iomem_resource)
5681c29f25bSToshi Kani * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
56967cf13ceSMike Travis *
570124fe20dSDan Williams * Check if the specified region partially overlaps or fully eclipses a
5711c29f25bSToshi Kani * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
5721c29f25bSToshi Kani * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
5731c29f25bSToshi Kani * return REGION_MIXED if the region overlaps @flags/@desc and another
5741c29f25bSToshi Kani * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
5751c29f25bSToshi Kani * and no other defined resource. Note that REGION_INTERSECTS is also
5761c29f25bSToshi Kani * returned in the case when the specified region overlaps RAM and undefined
5771c29f25bSToshi Kani * memory holes.
578124fe20dSDan Williams *
579124fe20dSDan Williams * region_intersect() is used by memory remapping functions to ensure
580124fe20dSDan Williams * the user is not remapping RAM and is a vast speed up over walking
581124fe20dSDan Williams * through the resource table page by page.
58267cf13ceSMike Travis */
region_intersects(resource_size_t start,size_t size,unsigned long flags,unsigned long desc)5831c29f25bSToshi Kani int region_intersects(resource_size_t start, size_t size, unsigned long flags,
5841c29f25bSToshi Kani unsigned long desc)
58567cf13ceSMike Travis {
586d486ccb2SAlistair Popple int ret;
587f6c6010aSWei Yang
58867cf13ceSMike Travis read_lock(&resource_lock);
58914b80582SDan Williams ret = __region_intersects(&iomem_resource, start, size, flags, desc);
59067cf13ceSMike Travis read_unlock(&resource_lock);
591124fe20dSDan Williams
592d486ccb2SAlistair Popple return ret;
59367cf13ceSMike Travis }
5941c29f25bSToshi Kani EXPORT_SYMBOL_GPL(region_intersects);
59567cf13ceSMike Travis
arch_remove_reservations(struct resource * avail)596fcb11918SBjorn Helgaas void __weak arch_remove_reservations(struct resource *avail)
597fcb11918SBjorn Helgaas {
598fcb11918SBjorn Helgaas }
599fcb11918SBjorn Helgaas
simple_align_resource(void * data,const struct resource * avail,resource_size_t size,resource_size_t align)600a9cea017SBjorn Helgaas static resource_size_t simple_align_resource(void *data,
601a9cea017SBjorn Helgaas const struct resource *avail,
602a9cea017SBjorn Helgaas resource_size_t size,
603a9cea017SBjorn Helgaas resource_size_t align)
604a9cea017SBjorn Helgaas {
605a9cea017SBjorn Helgaas return avail->start;
606a9cea017SBjorn Helgaas }
607a9cea017SBjorn Helgaas
resource_clip(struct resource * res,resource_size_t min,resource_size_t max)6085d6b1fa3SBjorn Helgaas static void resource_clip(struct resource *res, resource_size_t min,
6095d6b1fa3SBjorn Helgaas resource_size_t max)
6105d6b1fa3SBjorn Helgaas {
6115d6b1fa3SBjorn Helgaas if (res->start < min)
6125d6b1fa3SBjorn Helgaas res->start = min;
6135d6b1fa3SBjorn Helgaas if (res->end > max)
6145d6b1fa3SBjorn Helgaas res->end = max;
6155d6b1fa3SBjorn Helgaas }
6165d6b1fa3SBjorn Helgaas
6171da177e4SLinus Torvalds /*
61823c570a6SRam Pai * Find empty slot in the resource tree with the given range and
61923c570a6SRam Pai * alignment constraints
6201da177e4SLinus Torvalds */
__find_resource(struct resource * root,struct resource * old,struct resource * new,resource_size_t size,struct resource_constraint * constraint)62123c570a6SRam Pai static int __find_resource(struct resource *root, struct resource *old,
62223c570a6SRam Pai struct resource *new,
62323c570a6SRam Pai resource_size_t size,
62423c570a6SRam Pai struct resource_constraint *constraint)
6251da177e4SLinus Torvalds {
6261da177e4SLinus Torvalds struct resource *this = root->child;
627a1862e31SBjorn Helgaas struct resource tmp = *new, avail, alloc;
6281da177e4SLinus Torvalds
6290e2c8b8fSDominik Brodowski tmp.start = root->start;
6301da177e4SLinus Torvalds /*
631c0f5ac54SBjorn Helgaas * Skip past an allocated resource that starts at 0, since the assignment
632c0f5ac54SBjorn Helgaas * of this->start - 1 to tmp->end below would cause an underflow.
6331da177e4SLinus Torvalds */
63423c570a6SRam Pai if (this && this->start == root->start) {
63523c570a6SRam Pai tmp.start = (this == old) ? old->start : this->end + 1;
6361da177e4SLinus Torvalds this = this->sibling;
6371da177e4SLinus Torvalds }
6381da177e4SLinus Torvalds for(;;) {
6391da177e4SLinus Torvalds if (this)
64023c570a6SRam Pai tmp.end = (this == old) ? this->end : this->start - 1;
6411da177e4SLinus Torvalds else
6420e2c8b8fSDominik Brodowski tmp.end = root->end;
6435d6b1fa3SBjorn Helgaas
64447ea91b4SRam Pai if (tmp.end < tmp.start)
64547ea91b4SRam Pai goto next;
64647ea91b4SRam Pai
64723c570a6SRam Pai resource_clip(&tmp, constraint->min, constraint->max);
648fcb11918SBjorn Helgaas arch_remove_reservations(&tmp);
649a9cea017SBjorn Helgaas
650a1862e31SBjorn Helgaas /* Check for overflow after ALIGN() */
65123c570a6SRam Pai avail.start = ALIGN(tmp.start, constraint->align);
652a1862e31SBjorn Helgaas avail.end = tmp.end;
6535edb93b8SBjorn Helgaas avail.flags = new->flags & ~IORESOURCE_UNSET;
654a1862e31SBjorn Helgaas if (avail.start >= tmp.start) {
6555edb93b8SBjorn Helgaas alloc.flags = avail.flags;
65623c570a6SRam Pai alloc.start = constraint->alignf(constraint->alignf_data, &avail,
65723c570a6SRam Pai size, constraint->align);
6586909ba14SBjorn Helgaas alloc.end = alloc.start + size - 1;
65960bb83b8STakashi Iwai if (alloc.start <= alloc.end &&
66060bb83b8STakashi Iwai resource_contains(&avail, &alloc)) {
6616909ba14SBjorn Helgaas new->start = alloc.start;
6626909ba14SBjorn Helgaas new->end = alloc.end;
6631da177e4SLinus Torvalds return 0;
6641da177e4SLinus Torvalds }
665a1862e31SBjorn Helgaas }
66647ea91b4SRam Pai
66747ea91b4SRam Pai next: if (!this || this->end == root->end)
6681da177e4SLinus Torvalds break;
66947ea91b4SRam Pai
67023c570a6SRam Pai if (this != old)
6710e2c8b8fSDominik Brodowski tmp.start = this->end + 1;
6721da177e4SLinus Torvalds this = this->sibling;
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds return -EBUSY;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds
67723c570a6SRam Pai /*
67823c570a6SRam Pai * Find empty slot in the resource tree given range and alignment.
67923c570a6SRam Pai */
find_resource(struct resource * root,struct resource * new,resource_size_t size,struct resource_constraint * constraint)68023c570a6SRam Pai static int find_resource(struct resource *root, struct resource *new,
68123c570a6SRam Pai resource_size_t size,
68223c570a6SRam Pai struct resource_constraint *constraint)
68323c570a6SRam Pai {
68423c570a6SRam Pai return __find_resource(root, NULL, new, size, constraint);
68523c570a6SRam Pai }
68623c570a6SRam Pai
687e1ca66d1SRandy Dunlap /**
68823c570a6SRam Pai * reallocate_resource - allocate a slot in the resource tree given range & alignment.
68923c570a6SRam Pai * The resource will be relocated if the new size cannot be reallocated in the
69023c570a6SRam Pai * current location.
69123c570a6SRam Pai *
69223c570a6SRam Pai * @root: root resource descriptor
69323c570a6SRam Pai * @old: resource descriptor desired by caller
69423c570a6SRam Pai * @newsize: new size of the resource descriptor
69523c570a6SRam Pai * @constraint: the size and alignment constraints to be met.
69623c570a6SRam Pai */
reallocate_resource(struct resource * root,struct resource * old,resource_size_t newsize,struct resource_constraint * constraint)69728ab49ffSDaeseok Youn static int reallocate_resource(struct resource *root, struct resource *old,
69823c570a6SRam Pai resource_size_t newsize,
69923c570a6SRam Pai struct resource_constraint *constraint)
70023c570a6SRam Pai {
70123c570a6SRam Pai int err=0;
70223c570a6SRam Pai struct resource new = *old;
70323c570a6SRam Pai struct resource *conflict;
70423c570a6SRam Pai
70523c570a6SRam Pai write_lock(&resource_lock);
70623c570a6SRam Pai
70723c570a6SRam Pai if ((err = __find_resource(root, old, &new, newsize, constraint)))
70823c570a6SRam Pai goto out;
70923c570a6SRam Pai
71023c570a6SRam Pai if (resource_contains(&new, old)) {
71123c570a6SRam Pai old->start = new.start;
71223c570a6SRam Pai old->end = new.end;
71323c570a6SRam Pai goto out;
71423c570a6SRam Pai }
71523c570a6SRam Pai
71623c570a6SRam Pai if (old->child) {
71723c570a6SRam Pai err = -EBUSY;
71823c570a6SRam Pai goto out;
71923c570a6SRam Pai }
72023c570a6SRam Pai
72123c570a6SRam Pai if (resource_contains(old, &new)) {
72223c570a6SRam Pai old->start = new.start;
72323c570a6SRam Pai old->end = new.end;
72423c570a6SRam Pai } else {
725ff3cc952SToshi Kani __release_resource(old, true);
72623c570a6SRam Pai *old = new;
72723c570a6SRam Pai conflict = __request_resource(root, old);
72823c570a6SRam Pai BUG_ON(conflict);
72923c570a6SRam Pai }
73023c570a6SRam Pai out:
73123c570a6SRam Pai write_unlock(&resource_lock);
73223c570a6SRam Pai return err;
73323c570a6SRam Pai }
73423c570a6SRam Pai
73523c570a6SRam Pai
73623c570a6SRam Pai /**
73723c570a6SRam Pai * allocate_resource - allocate empty slot in the resource tree given range & alignment.
73823c570a6SRam Pai * The resource will be reallocated with a new size if it was already allocated
739e1ca66d1SRandy Dunlap * @root: root resource descriptor
740e1ca66d1SRandy Dunlap * @new: resource descriptor desired by caller
741e1ca66d1SRandy Dunlap * @size: requested resource region size
742ee5e5683SWei Yang * @min: minimum boundary to allocate
743ee5e5683SWei Yang * @max: maximum boundary to allocate
744e1ca66d1SRandy Dunlap * @align: alignment requested, in bytes
745e1ca66d1SRandy Dunlap * @alignf: alignment function, optional, called if not NULL
746e1ca66d1SRandy Dunlap * @alignf_data: arbitrary data to pass to the @alignf function
7471da177e4SLinus 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)7481da177e4SLinus Torvalds int allocate_resource(struct resource *root, struct resource *new,
749d75fc8bbSGreg Kroah-Hartman resource_size_t size, resource_size_t min,
750d75fc8bbSGreg Kroah-Hartman resource_size_t max, resource_size_t align,
751b26b2d49SDominik Brodowski resource_size_t (*alignf)(void *,
7523b7a17fcSDominik Brodowski const struct resource *,
753b26b2d49SDominik Brodowski resource_size_t,
754b26b2d49SDominik Brodowski resource_size_t),
7551da177e4SLinus Torvalds void *alignf_data)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds int err;
75823c570a6SRam Pai struct resource_constraint constraint;
7591da177e4SLinus Torvalds
760a9cea017SBjorn Helgaas if (!alignf)
761a9cea017SBjorn Helgaas alignf = simple_align_resource;
762a9cea017SBjorn Helgaas
76323c570a6SRam Pai constraint.min = min;
76423c570a6SRam Pai constraint.max = max;
76523c570a6SRam Pai constraint.align = align;
76623c570a6SRam Pai constraint.alignf = alignf;
76723c570a6SRam Pai constraint.alignf_data = alignf_data;
76823c570a6SRam Pai
76923c570a6SRam Pai if ( new->parent ) {
77023c570a6SRam Pai /* resource is already allocated, try reallocating with
77123c570a6SRam Pai the new constraints */
77223c570a6SRam Pai return reallocate_resource(root, new, size, &constraint);
77323c570a6SRam Pai }
77423c570a6SRam Pai
7751da177e4SLinus Torvalds write_lock(&resource_lock);
77623c570a6SRam Pai err = find_resource(root, new, size, &constraint);
7771da177e4SLinus Torvalds if (err >= 0 && __request_resource(root, new))
7781da177e4SLinus Torvalds err = -EBUSY;
7791da177e4SLinus Torvalds write_unlock(&resource_lock);
7801da177e4SLinus Torvalds return err;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds
7831da177e4SLinus Torvalds EXPORT_SYMBOL(allocate_resource);
7841da177e4SLinus Torvalds
7851c388919SGeert Uytterhoeven /**
7861c388919SGeert Uytterhoeven * lookup_resource - find an existing resource by a resource start address
7871c388919SGeert Uytterhoeven * @root: root resource descriptor
7881c388919SGeert Uytterhoeven * @start: resource start address
7891c388919SGeert Uytterhoeven *
7901c388919SGeert Uytterhoeven * Returns a pointer to the resource if found, NULL otherwise
7911c388919SGeert Uytterhoeven */
lookup_resource(struct resource * root,resource_size_t start)7921c388919SGeert Uytterhoeven struct resource *lookup_resource(struct resource *root, resource_size_t start)
7931c388919SGeert Uytterhoeven {
7941c388919SGeert Uytterhoeven struct resource *res;
7951c388919SGeert Uytterhoeven
7961c388919SGeert Uytterhoeven read_lock(&resource_lock);
7971c388919SGeert Uytterhoeven for (res = root->child; res; res = res->sibling) {
7981c388919SGeert Uytterhoeven if (res->start == start)
7991c388919SGeert Uytterhoeven break;
8001c388919SGeert Uytterhoeven }
8011c388919SGeert Uytterhoeven read_unlock(&resource_lock);
8021c388919SGeert Uytterhoeven
8031c388919SGeert Uytterhoeven return res;
8041c388919SGeert Uytterhoeven }
8051c388919SGeert Uytterhoeven
806bef69ea0SLinus Torvalds /*
807bef69ea0SLinus Torvalds * Insert a resource into the resource tree. If successful, return NULL,
808bef69ea0SLinus Torvalds * otherwise return the conflicting resource (compare to __request_resource())
8091da177e4SLinus Torvalds */
__insert_resource(struct resource * parent,struct resource * new)810bef69ea0SLinus Torvalds static struct resource * __insert_resource(struct resource *parent, struct resource *new)
8111da177e4SLinus Torvalds {
8121da177e4SLinus Torvalds struct resource *first, *next;
8131da177e4SLinus Torvalds
814d33b6fbaSMatthew Wilcox for (;; parent = first) {
8151da177e4SLinus Torvalds first = __request_resource(parent, new);
8161da177e4SLinus Torvalds if (!first)
817bef69ea0SLinus Torvalds return first;
8181da177e4SLinus Torvalds
8191da177e4SLinus Torvalds if (first == parent)
820bef69ea0SLinus Torvalds return first;
8215de1cb2dSHuang Shijie if (WARN_ON(first == new)) /* duplicated insertion */
8225de1cb2dSHuang Shijie return first;
8231da177e4SLinus Torvalds
824d33b6fbaSMatthew Wilcox if ((first->start > new->start) || (first->end < new->end))
825d33b6fbaSMatthew Wilcox break;
826d33b6fbaSMatthew Wilcox if ((first->start == new->start) && (first->end == new->end))
827d33b6fbaSMatthew Wilcox break;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds
8301da177e4SLinus Torvalds for (next = first; ; next = next->sibling) {
8311da177e4SLinus Torvalds /* Partial overlap? Bad, and unfixable */
8321da177e4SLinus Torvalds if (next->start < new->start || next->end > new->end)
833bef69ea0SLinus Torvalds return next;
8341da177e4SLinus Torvalds if (!next->sibling)
8351da177e4SLinus Torvalds break;
8361da177e4SLinus Torvalds if (next->sibling->start > new->end)
8371da177e4SLinus Torvalds break;
8381da177e4SLinus Torvalds }
8391da177e4SLinus Torvalds
8401da177e4SLinus Torvalds new->parent = parent;
8411da177e4SLinus Torvalds new->sibling = next->sibling;
8421da177e4SLinus Torvalds new->child = first;
8431da177e4SLinus Torvalds
8441da177e4SLinus Torvalds next->sibling = NULL;
8451da177e4SLinus Torvalds for (next = first; next; next = next->sibling)
8461da177e4SLinus Torvalds next->parent = new;
8471da177e4SLinus Torvalds
8481da177e4SLinus Torvalds if (parent->child == first) {
8491da177e4SLinus Torvalds parent->child = new;
8501da177e4SLinus Torvalds } else {
8511da177e4SLinus Torvalds next = parent->child;
8521da177e4SLinus Torvalds while (next->sibling != first)
8531da177e4SLinus Torvalds next = next->sibling;
8541da177e4SLinus Torvalds next->sibling = new;
8551da177e4SLinus Torvalds }
856bef69ea0SLinus Torvalds return NULL;
857bef69ea0SLinus Torvalds }
8581da177e4SLinus Torvalds
859bef69ea0SLinus Torvalds /**
86066f1207bSBjorn Helgaas * insert_resource_conflict - Inserts resource in the resource tree
861bef69ea0SLinus Torvalds * @parent: parent of the new resource
862bef69ea0SLinus Torvalds * @new: new resource to insert
863bef69ea0SLinus Torvalds *
86466f1207bSBjorn Helgaas * Returns 0 on success, conflict resource if the resource can't be inserted.
865bef69ea0SLinus Torvalds *
86666f1207bSBjorn Helgaas * This function is equivalent to request_resource_conflict when no conflict
867bef69ea0SLinus Torvalds * happens. If a conflict happens, and the conflicting resources
868bef69ea0SLinus Torvalds * entirely fit within the range of the new resource, then the new
869bef69ea0SLinus Torvalds * resource is inserted and the conflicting resources become children of
870bef69ea0SLinus Torvalds * the new resource.
871ff3cc952SToshi Kani *
872ff3cc952SToshi Kani * This function is intended for producers of resources, such as FW modules
873ff3cc952SToshi Kani * and bus drivers.
874bef69ea0SLinus Torvalds */
insert_resource_conflict(struct resource * parent,struct resource * new)87566f1207bSBjorn Helgaas struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
876bef69ea0SLinus Torvalds {
877bef69ea0SLinus Torvalds struct resource *conflict;
878bef69ea0SLinus Torvalds
879bef69ea0SLinus Torvalds write_lock(&resource_lock);
880bef69ea0SLinus Torvalds conflict = __insert_resource(parent, new);
8811da177e4SLinus Torvalds write_unlock(&resource_lock);
88266f1207bSBjorn Helgaas return conflict;
88366f1207bSBjorn Helgaas }
88466f1207bSBjorn Helgaas
88566f1207bSBjorn Helgaas /**
88666f1207bSBjorn Helgaas * insert_resource - Inserts a resource in the resource tree
88766f1207bSBjorn Helgaas * @parent: parent of the new resource
88866f1207bSBjorn Helgaas * @new: new resource to insert
88966f1207bSBjorn Helgaas *
89066f1207bSBjorn Helgaas * Returns 0 on success, -EBUSY if the resource can't be inserted.
891ff3cc952SToshi Kani *
892ff3cc952SToshi Kani * This function is intended for producers of resources, such as FW modules
893ff3cc952SToshi Kani * and bus drivers.
89466f1207bSBjorn Helgaas */
insert_resource(struct resource * parent,struct resource * new)89566f1207bSBjorn Helgaas int insert_resource(struct resource *parent, struct resource *new)
89666f1207bSBjorn Helgaas {
89766f1207bSBjorn Helgaas struct resource *conflict;
89866f1207bSBjorn Helgaas
89966f1207bSBjorn Helgaas conflict = insert_resource_conflict(parent, new);
900bef69ea0SLinus Torvalds return conflict ? -EBUSY : 0;
901bef69ea0SLinus Torvalds }
9028095d0f2SToshi Kani EXPORT_SYMBOL_GPL(insert_resource);
903bef69ea0SLinus Torvalds
904bef69ea0SLinus Torvalds /**
905bef69ea0SLinus Torvalds * insert_resource_expand_to_fit - Insert a resource into the resource tree
9066781f4aeSRandy Dunlap * @root: root resource descriptor
907bef69ea0SLinus Torvalds * @new: new resource to insert
908bef69ea0SLinus Torvalds *
909bef69ea0SLinus Torvalds * Insert a resource into the resource tree, possibly expanding it in order
910bef69ea0SLinus Torvalds * to make it encompass any conflicting resources.
911bef69ea0SLinus Torvalds */
insert_resource_expand_to_fit(struct resource * root,struct resource * new)912bef69ea0SLinus Torvalds void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
913bef69ea0SLinus Torvalds {
914bef69ea0SLinus Torvalds if (new->parent)
915bef69ea0SLinus Torvalds return;
916bef69ea0SLinus Torvalds
917bef69ea0SLinus Torvalds write_lock(&resource_lock);
918bef69ea0SLinus Torvalds for (;;) {
919bef69ea0SLinus Torvalds struct resource *conflict;
920bef69ea0SLinus Torvalds
921bef69ea0SLinus Torvalds conflict = __insert_resource(root, new);
922bef69ea0SLinus Torvalds if (!conflict)
923bef69ea0SLinus Torvalds break;
924bef69ea0SLinus Torvalds if (conflict == root)
925bef69ea0SLinus Torvalds break;
926bef69ea0SLinus Torvalds
927bef69ea0SLinus Torvalds /* Ok, expand resource to cover the conflict, then try again .. */
928bef69ea0SLinus Torvalds if (conflict->start < new->start)
929bef69ea0SLinus Torvalds new->start = conflict->start;
930bef69ea0SLinus Torvalds if (conflict->end > new->end)
931bef69ea0SLinus Torvalds new->end = conflict->end;
932bef69ea0SLinus Torvalds
9332a4e6285SAndy Shevchenko pr_info("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
934bef69ea0SLinus Torvalds }
935bef69ea0SLinus Torvalds write_unlock(&resource_lock);
9361da177e4SLinus Torvalds }
937974854abSDan Williams /*
938974854abSDan Williams * Not for general consumption, only early boot memory map parsing, PCI
939974854abSDan Williams * resource discovery, and late discovery of CXL resources are expected
940974854abSDan Williams * to use this interface. The former are built-in and only the latter,
941974854abSDan Williams * CXL, is a module.
942974854abSDan Williams */
943974854abSDan Williams EXPORT_SYMBOL_NS_GPL(insert_resource_expand_to_fit, CXL);
9441da177e4SLinus Torvalds
945ff3cc952SToshi Kani /**
946ff3cc952SToshi Kani * remove_resource - Remove a resource in the resource tree
947ff3cc952SToshi Kani * @old: resource to remove
948ff3cc952SToshi Kani *
949ff3cc952SToshi Kani * Returns 0 on success, -EINVAL if the resource is not valid.
950ff3cc952SToshi Kani *
951ff3cc952SToshi Kani * This function removes a resource previously inserted by insert_resource()
952ff3cc952SToshi Kani * or insert_resource_conflict(), and moves the children (if any) up to
953ff3cc952SToshi Kani * where they were before. insert_resource() and insert_resource_conflict()
954ff3cc952SToshi Kani * insert a new resource, and move any conflicting resources down to the
955ff3cc952SToshi Kani * children of the new resource.
956ff3cc952SToshi Kani *
957ff3cc952SToshi Kani * insert_resource(), insert_resource_conflict() and remove_resource() are
958ff3cc952SToshi Kani * intended for producers of resources, such as FW modules and bus drivers.
959ff3cc952SToshi Kani */
remove_resource(struct resource * old)960ff3cc952SToshi Kani int remove_resource(struct resource *old)
961ff3cc952SToshi Kani {
962ff3cc952SToshi Kani int retval;
963ff3cc952SToshi Kani
964ff3cc952SToshi Kani write_lock(&resource_lock);
965ff3cc952SToshi Kani retval = __release_resource(old, false);
966ff3cc952SToshi Kani write_unlock(&resource_lock);
967ff3cc952SToshi Kani return retval;
968ff3cc952SToshi Kani }
9698095d0f2SToshi Kani EXPORT_SYMBOL_GPL(remove_resource);
970ff3cc952SToshi Kani
__adjust_resource(struct resource * res,resource_size_t start,resource_size_t size)971ae8e3a91SToshi Kani static int __adjust_resource(struct resource *res, resource_size_t start,
972ae8e3a91SToshi Kani resource_size_t size)
9731da177e4SLinus Torvalds {
9741da177e4SLinus Torvalds struct resource *tmp, *parent = res->parent;
975d75fc8bbSGreg Kroah-Hartman resource_size_t end = start + size - 1;
9761da177e4SLinus Torvalds int result = -EBUSY;
9771da177e4SLinus Torvalds
97882ec90eaSYinghai Lu if (!parent)
97982ec90eaSYinghai Lu goto skip;
98082ec90eaSYinghai Lu
9811da177e4SLinus Torvalds if ((start < parent->start) || (end > parent->end))
9821da177e4SLinus Torvalds goto out;
9831da177e4SLinus Torvalds
9841da177e4SLinus Torvalds if (res->sibling && (res->sibling->start <= end))
9851da177e4SLinus Torvalds goto out;
9861da177e4SLinus Torvalds
9871da177e4SLinus Torvalds tmp = parent->child;
9881da177e4SLinus Torvalds if (tmp != res) {
9891da177e4SLinus Torvalds while (tmp->sibling != res)
9901da177e4SLinus Torvalds tmp = tmp->sibling;
9911da177e4SLinus Torvalds if (start <= tmp->end)
9921da177e4SLinus Torvalds goto out;
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds
99582ec90eaSYinghai Lu skip:
99682ec90eaSYinghai Lu for (tmp = res->child; tmp; tmp = tmp->sibling)
99782ec90eaSYinghai Lu if ((tmp->start < start) || (tmp->end > end))
99882ec90eaSYinghai Lu goto out;
99982ec90eaSYinghai Lu
10001da177e4SLinus Torvalds res->start = start;
10011da177e4SLinus Torvalds res->end = end;
10021da177e4SLinus Torvalds result = 0;
10031da177e4SLinus Torvalds
10041da177e4SLinus Torvalds out:
1005ae8e3a91SToshi Kani return result;
1006ae8e3a91SToshi Kani }
1007ae8e3a91SToshi Kani
1008ae8e3a91SToshi Kani /**
1009ae8e3a91SToshi Kani * adjust_resource - modify a resource's start and size
1010ae8e3a91SToshi Kani * @res: resource to modify
1011ae8e3a91SToshi Kani * @start: new start value
1012ae8e3a91SToshi Kani * @size: new size
1013ae8e3a91SToshi Kani *
1014ae8e3a91SToshi Kani * Given an existing resource, change its start and size to match the
1015ae8e3a91SToshi Kani * arguments. Returns 0 on success, -EBUSY if it can't fit.
1016ae8e3a91SToshi Kani * Existing children of the resource are assumed to be immutable.
1017ae8e3a91SToshi Kani */
adjust_resource(struct resource * res,resource_size_t start,resource_size_t size)1018ae8e3a91SToshi Kani int adjust_resource(struct resource *res, resource_size_t start,
1019ae8e3a91SToshi Kani resource_size_t size)
1020ae8e3a91SToshi Kani {
1021ae8e3a91SToshi Kani int result;
1022ae8e3a91SToshi Kani
1023ae8e3a91SToshi Kani write_lock(&resource_lock);
1024ae8e3a91SToshi Kani result = __adjust_resource(res, start, size);
10251da177e4SLinus Torvalds write_unlock(&resource_lock);
10261da177e4SLinus Torvalds return result;
10271da177e4SLinus Torvalds }
102824105748SCong Wang EXPORT_SYMBOL(adjust_resource);
10291da177e4SLinus Torvalds
1030b69c2e20SBorislav Petkov static void __init
__reserve_region_with_split(struct resource * root,resource_size_t start,resource_size_t end,const char * name)1031b69c2e20SBorislav Petkov __reserve_region_with_split(struct resource *root, resource_size_t start,
1032b69c2e20SBorislav Petkov resource_size_t end, const char *name)
1033268364a0SYinghai Lu {
1034268364a0SYinghai Lu struct resource *parent = root;
1035268364a0SYinghai Lu struct resource *conflict;
1036ebff7d8fSYasuaki Ishimatsu struct resource *res = alloc_resource(GFP_ATOMIC);
10374965f566ST Makphaibulchoke struct resource *next_res = NULL;
1038f37e2334SBjorn Helgaas int type = resource_type(root);
1039268364a0SYinghai Lu
1040268364a0SYinghai Lu if (!res)
1041268364a0SYinghai Lu return;
1042268364a0SYinghai Lu
1043268364a0SYinghai Lu res->name = name;
1044268364a0SYinghai Lu res->start = start;
1045268364a0SYinghai Lu res->end = end;
1046f37e2334SBjorn Helgaas res->flags = type | IORESOURCE_BUSY;
104743ee493bSToshi Kani res->desc = IORES_DESC_NONE;
1048268364a0SYinghai Lu
10494965f566ST Makphaibulchoke while (1) {
1050268364a0SYinghai Lu
10514965f566ST Makphaibulchoke conflict = __request_resource(parent, res);
10524965f566ST Makphaibulchoke if (!conflict) {
10534965f566ST Makphaibulchoke if (!next_res)
10544965f566ST Makphaibulchoke break;
10554965f566ST Makphaibulchoke res = next_res;
10564965f566ST Makphaibulchoke next_res = NULL;
10574965f566ST Makphaibulchoke continue;
10584965f566ST Makphaibulchoke }
1059268364a0SYinghai Lu
10601cf44baaSIngo Molnar /* conflict covered whole area */
10614965f566ST Makphaibulchoke if (conflict->start <= res->start &&
10624965f566ST Makphaibulchoke conflict->end >= res->end) {
1063ebff7d8fSYasuaki Ishimatsu free_resource(res);
10644965f566ST Makphaibulchoke WARN_ON(next_res);
10654965f566ST Makphaibulchoke break;
10664965f566ST Makphaibulchoke }
1067268364a0SYinghai Lu
10684965f566ST Makphaibulchoke /* failed, split and try again */
10694965f566ST Makphaibulchoke if (conflict->start > res->start) {
10704965f566ST Makphaibulchoke end = res->end;
10714965f566ST Makphaibulchoke res->end = conflict->start - 1;
10724965f566ST Makphaibulchoke if (conflict->end < end) {
1073ebff7d8fSYasuaki Ishimatsu next_res = alloc_resource(GFP_ATOMIC);
10744965f566ST Makphaibulchoke if (!next_res) {
1075ebff7d8fSYasuaki Ishimatsu free_resource(res);
10764965f566ST Makphaibulchoke break;
10774965f566ST Makphaibulchoke }
10784965f566ST Makphaibulchoke next_res->name = name;
10794965f566ST Makphaibulchoke next_res->start = conflict->end + 1;
10804965f566ST Makphaibulchoke next_res->end = end;
1081f37e2334SBjorn Helgaas next_res->flags = type | IORESOURCE_BUSY;
108243ee493bSToshi Kani next_res->desc = IORES_DESC_NONE;
10834965f566ST Makphaibulchoke }
10844965f566ST Makphaibulchoke } else {
10854965f566ST Makphaibulchoke res->start = conflict->end + 1;
10864965f566ST Makphaibulchoke }
10874965f566ST Makphaibulchoke }
10884965f566ST Makphaibulchoke
1089268364a0SYinghai Lu }
1090268364a0SYinghai Lu
1091b69c2e20SBorislav Petkov void __init
reserve_region_with_split(struct resource * root,resource_size_t start,resource_size_t end,const char * name)1092b69c2e20SBorislav Petkov reserve_region_with_split(struct resource *root, resource_size_t start,
1093b69c2e20SBorislav Petkov resource_size_t end, const char *name)
1094268364a0SYinghai Lu {
109565fed8f6SOctavian Purdila int abort = 0;
109665fed8f6SOctavian Purdila
1097268364a0SYinghai Lu write_lock(&resource_lock);
109865fed8f6SOctavian Purdila if (root->start > start || root->end < end) {
109965fed8f6SOctavian Purdila pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
110065fed8f6SOctavian Purdila (unsigned long long)start, (unsigned long long)end,
110165fed8f6SOctavian Purdila root);
110265fed8f6SOctavian Purdila if (start > root->end || end < root->start)
110365fed8f6SOctavian Purdila abort = 1;
110465fed8f6SOctavian Purdila else {
110565fed8f6SOctavian Purdila if (end > root->end)
110665fed8f6SOctavian Purdila end = root->end;
110765fed8f6SOctavian Purdila if (start < root->start)
110865fed8f6SOctavian Purdila start = root->start;
110965fed8f6SOctavian Purdila pr_err("fixing request to [0x%llx-0x%llx]\n",
111065fed8f6SOctavian Purdila (unsigned long long)start,
111165fed8f6SOctavian Purdila (unsigned long long)end);
111265fed8f6SOctavian Purdila }
111365fed8f6SOctavian Purdila dump_stack();
111465fed8f6SOctavian Purdila }
111565fed8f6SOctavian Purdila if (!abort)
1116268364a0SYinghai Lu __reserve_region_with_split(root, start, end, name);
1117268364a0SYinghai Lu write_unlock(&resource_lock);
1118268364a0SYinghai Lu }
1119268364a0SYinghai Lu
112088452565SIvan Kokshaysky /**
112188452565SIvan Kokshaysky * resource_alignment - calculate resource's alignment
112288452565SIvan Kokshaysky * @res: resource pointer
112388452565SIvan Kokshaysky *
112488452565SIvan Kokshaysky * Returns alignment on success, 0 (invalid alignment) on failure.
112588452565SIvan Kokshaysky */
resource_alignment(struct resource * res)112688452565SIvan Kokshaysky resource_size_t resource_alignment(struct resource *res)
112788452565SIvan Kokshaysky {
112888452565SIvan Kokshaysky switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
112988452565SIvan Kokshaysky case IORESOURCE_SIZEALIGN:
11301a4e564bSMagnus Damm return resource_size(res);
113188452565SIvan Kokshaysky case IORESOURCE_STARTALIGN:
113288452565SIvan Kokshaysky return res->start;
113388452565SIvan Kokshaysky default:
113488452565SIvan Kokshaysky return 0;
113588452565SIvan Kokshaysky }
113688452565SIvan Kokshaysky }
113788452565SIvan Kokshaysky
11381da177e4SLinus Torvalds /*
11391da177e4SLinus Torvalds * This is compatibility stuff for IO resources.
11401da177e4SLinus Torvalds *
11411da177e4SLinus Torvalds * Note how this, unlike the above, knows about
11421da177e4SLinus Torvalds * the IO flag meanings (busy etc).
11431da177e4SLinus Torvalds *
1144e1ca66d1SRandy Dunlap * request_region creates a new busy region.
11451da177e4SLinus Torvalds *
1146e1ca66d1SRandy Dunlap * release_region releases a matching busy region.
1147e1ca66d1SRandy Dunlap */
1148e1ca66d1SRandy Dunlap
11498b6d043bSAlan Cox static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
11508b6d043bSAlan Cox
115171a1d8edSDaniel Vetter static struct inode *iomem_inode;
115271a1d8edSDaniel Vetter
115371a1d8edSDaniel Vetter #ifdef CONFIG_IO_STRICT_DEVMEM
revoke_iomem(struct resource * res)115471a1d8edSDaniel Vetter static void revoke_iomem(struct resource *res)
115571a1d8edSDaniel Vetter {
115671a1d8edSDaniel Vetter /* pairs with smp_store_release() in iomem_init_inode() */
115771a1d8edSDaniel Vetter struct inode *inode = smp_load_acquire(&iomem_inode);
115871a1d8edSDaniel Vetter
115971a1d8edSDaniel Vetter /*
116071a1d8edSDaniel Vetter * Check that the initialization has completed. Losing the race
116171a1d8edSDaniel Vetter * is ok because it means drivers are claiming resources before
116271a1d8edSDaniel Vetter * the fs_initcall level of init and prevent iomem_get_mapping users
116371a1d8edSDaniel Vetter * from establishing mappings.
116471a1d8edSDaniel Vetter */
116571a1d8edSDaniel Vetter if (!inode)
116671a1d8edSDaniel Vetter return;
116771a1d8edSDaniel Vetter
116871a1d8edSDaniel Vetter /*
116971a1d8edSDaniel Vetter * The expectation is that the driver has successfully marked
117071a1d8edSDaniel Vetter * the resource busy by this point, so devmem_is_allowed()
117171a1d8edSDaniel Vetter * should start returning false, however for performance this
117271a1d8edSDaniel Vetter * does not iterate the entire resource range.
117371a1d8edSDaniel Vetter */
117471a1d8edSDaniel Vetter if (devmem_is_allowed(PHYS_PFN(res->start)) &&
117571a1d8edSDaniel Vetter devmem_is_allowed(PHYS_PFN(res->end))) {
117671a1d8edSDaniel Vetter /*
117771a1d8edSDaniel Vetter * *cringe* iomem=relaxed says "go ahead, what's the
117871a1d8edSDaniel Vetter * worst that can happen?"
117971a1d8edSDaniel Vetter */
118071a1d8edSDaniel Vetter return;
118171a1d8edSDaniel Vetter }
118271a1d8edSDaniel Vetter
118371a1d8edSDaniel Vetter unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
118471a1d8edSDaniel Vetter }
118571a1d8edSDaniel Vetter #else
revoke_iomem(struct resource * res)118671a1d8edSDaniel Vetter static void revoke_iomem(struct resource *res) {}
118771a1d8edSDaniel Vetter #endif
118871a1d8edSDaniel Vetter
iomem_get_mapping(void)118971a1d8edSDaniel Vetter struct address_space *iomem_get_mapping(void)
119071a1d8edSDaniel Vetter {
119171a1d8edSDaniel Vetter /*
119271a1d8edSDaniel Vetter * This function is only called from file open paths, hence guaranteed
119371a1d8edSDaniel Vetter * that fs_initcalls have completed and no need to check for NULL. But
119471a1d8edSDaniel Vetter * since revoke_iomem can be called before the initcall we still need
119571a1d8edSDaniel Vetter * the barrier to appease checkers.
119671a1d8edSDaniel Vetter */
119771a1d8edSDaniel Vetter return smp_load_acquire(&iomem_inode)->i_mapping;
119871a1d8edSDaniel Vetter }
119971a1d8edSDaniel Vetter
__request_region_locked(struct resource * res,struct resource * parent,resource_size_t start,resource_size_t n,const char * name,int flags)120063cdafe0SAlistair Popple static int __request_region_locked(struct resource *res, struct resource *parent,
1201d75fc8bbSGreg Kroah-Hartman resource_size_t start, resource_size_t n,
1202e8de1481SArjan van de Ven const char *name, int flags)
12031da177e4SLinus Torvalds {
12048b6d043bSAlan Cox DECLARE_WAITQUEUE(wait, current);
1205c26ec88eSBjorn Helgaas
12061da177e4SLinus Torvalds res->name = name;
12071da177e4SLinus Torvalds res->start = start;
12081da177e4SLinus Torvalds res->end = start + n - 1;
12091da177e4SLinus Torvalds
12101da177e4SLinus Torvalds for (;;) {
12111da177e4SLinus Torvalds struct resource *conflict;
12121da177e4SLinus Torvalds
12134e0d8f7eSToshi Kani res->flags = resource_type(parent) | resource_ext_type(parent);
12144e0d8f7eSToshi Kani res->flags |= IORESOURCE_BUSY | flags;
12154e0d8f7eSToshi Kani res->desc = parent->desc;
12164e0d8f7eSToshi Kani
12171da177e4SLinus Torvalds conflict = __request_resource(parent, res);
12181da177e4SLinus Torvalds if (!conflict)
12191da177e4SLinus Torvalds break;
1220b926b7f3SDave Hansen /*
1221b926b7f3SDave Hansen * mm/hmm.c reserves physical addresses which then
1222b926b7f3SDave Hansen * become unavailable to other users. Conflicts are
1223b926b7f3SDave Hansen * not expected. Warn to aid debugging if encountered.
1224b926b7f3SDave Hansen */
1225b926b7f3SDave Hansen if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1226b926b7f3SDave Hansen pr_warn("Unaddressable device %s %pR conflicts with %pR",
1227b926b7f3SDave Hansen conflict->name, conflict, res);
1228b926b7f3SDave Hansen }
12291da177e4SLinus Torvalds if (conflict != parent) {
123059ceeaafSSimon Guinot if (!(conflict->flags & IORESOURCE_BUSY)) {
12311da177e4SLinus Torvalds parent = conflict;
12321da177e4SLinus Torvalds continue;
12331da177e4SLinus Torvalds }
123459ceeaafSSimon Guinot }
12358b6d043bSAlan Cox if (conflict->flags & flags & IORESOURCE_MUXED) {
12368b6d043bSAlan Cox add_wait_queue(&muxed_resource_wait, &wait);
12378b6d043bSAlan Cox write_unlock(&resource_lock);
12388b6d043bSAlan Cox set_current_state(TASK_UNINTERRUPTIBLE);
12398b6d043bSAlan Cox schedule();
12408b6d043bSAlan Cox remove_wait_queue(&muxed_resource_wait, &wait);
12418b6d043bSAlan Cox write_lock(&resource_lock);
12428b6d043bSAlan Cox continue;
12438b6d043bSAlan Cox }
12441da177e4SLinus Torvalds /* Uhhuh, that didn't work out.. */
124563cdafe0SAlistair Popple return -EBUSY;
12461da177e4SLinus Torvalds }
124763cdafe0SAlistair Popple
124863cdafe0SAlistair Popple return 0;
124963cdafe0SAlistair Popple }
125063cdafe0SAlistair Popple
125163cdafe0SAlistair Popple /**
125263cdafe0SAlistair Popple * __request_region - create a new busy resource region
125363cdafe0SAlistair Popple * @parent: parent resource descriptor
125463cdafe0SAlistair Popple * @start: resource start address
125563cdafe0SAlistair Popple * @n: resource region size
125663cdafe0SAlistair Popple * @name: reserving caller's ID string
125763cdafe0SAlistair Popple * @flags: IO resource flags
125863cdafe0SAlistair Popple */
__request_region(struct resource * parent,resource_size_t start,resource_size_t n,const char * name,int flags)125963cdafe0SAlistair Popple struct resource *__request_region(struct resource *parent,
126063cdafe0SAlistair Popple resource_size_t start, resource_size_t n,
126163cdafe0SAlistair Popple const char *name, int flags)
126263cdafe0SAlistair Popple {
126363cdafe0SAlistair Popple struct resource *res = alloc_resource(GFP_KERNEL);
126463cdafe0SAlistair Popple int ret;
126563cdafe0SAlistair Popple
126663cdafe0SAlistair Popple if (!res)
126763cdafe0SAlistair Popple return NULL;
126863cdafe0SAlistair Popple
126963cdafe0SAlistair Popple write_lock(&resource_lock);
127063cdafe0SAlistair Popple ret = __request_region_locked(res, parent, start, n, name, flags);
12711da177e4SLinus Torvalds write_unlock(&resource_lock);
12723234ac66SDan Williams
127363cdafe0SAlistair Popple if (ret) {
127463cdafe0SAlistair Popple free_resource(res);
127563cdafe0SAlistair Popple return NULL;
127663cdafe0SAlistair Popple }
127763cdafe0SAlistair Popple
127863cdafe0SAlistair Popple if (parent == &iomem_resource)
127971a1d8edSDaniel Vetter revoke_iomem(res);
12803234ac66SDan Williams
12811da177e4SLinus Torvalds return res;
12821da177e4SLinus Torvalds }
12831da177e4SLinus Torvalds EXPORT_SYMBOL(__request_region);
12841da177e4SLinus Torvalds
1285e1ca66d1SRandy Dunlap /**
1286e1ca66d1SRandy Dunlap * __release_region - release a previously reserved resource region
1287e1ca66d1SRandy Dunlap * @parent: parent resource descriptor
1288e1ca66d1SRandy Dunlap * @start: resource start address
1289e1ca66d1SRandy Dunlap * @n: resource region size
1290e1ca66d1SRandy Dunlap *
1291e1ca66d1SRandy Dunlap * The described resource region must match a currently busy region.
1292e1ca66d1SRandy Dunlap */
__release_region(struct resource * parent,resource_size_t start,resource_size_t n)1293d75fc8bbSGreg Kroah-Hartman void __release_region(struct resource *parent, resource_size_t start,
1294d75fc8bbSGreg Kroah-Hartman resource_size_t n)
12951da177e4SLinus Torvalds {
12961da177e4SLinus Torvalds struct resource **p;
1297d75fc8bbSGreg Kroah-Hartman resource_size_t end;
12981da177e4SLinus Torvalds
12991da177e4SLinus Torvalds p = &parent->child;
13001da177e4SLinus Torvalds end = start + n - 1;
13011da177e4SLinus Torvalds
13021da177e4SLinus Torvalds write_lock(&resource_lock);
13031da177e4SLinus Torvalds
13041da177e4SLinus Torvalds for (;;) {
13051da177e4SLinus Torvalds struct resource *res = *p;
13061da177e4SLinus Torvalds
13071da177e4SLinus Torvalds if (!res)
13081da177e4SLinus Torvalds break;
13091da177e4SLinus Torvalds if (res->start <= start && res->end >= end) {
13101da177e4SLinus Torvalds if (!(res->flags & IORESOURCE_BUSY)) {
13111da177e4SLinus Torvalds p = &res->child;
13121da177e4SLinus Torvalds continue;
13131da177e4SLinus Torvalds }
13141da177e4SLinus Torvalds if (res->start != start || res->end != end)
13151da177e4SLinus Torvalds break;
13161da177e4SLinus Torvalds *p = res->sibling;
13171da177e4SLinus Torvalds write_unlock(&resource_lock);
13188b6d043bSAlan Cox if (res->flags & IORESOURCE_MUXED)
13198b6d043bSAlan Cox wake_up(&muxed_resource_wait);
1320ebff7d8fSYasuaki Ishimatsu free_resource(res);
13211da177e4SLinus Torvalds return;
13221da177e4SLinus Torvalds }
13231da177e4SLinus Torvalds p = &res->sibling;
13241da177e4SLinus Torvalds }
13251da177e4SLinus Torvalds
13261da177e4SLinus Torvalds write_unlock(&resource_lock);
13271da177e4SLinus Torvalds
13282a4e6285SAndy Shevchenko pr_warn("Trying to free nonexistent resource <%pa-%pa>\n", &start, &end);
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds EXPORT_SYMBOL(__release_region);
13311da177e4SLinus Torvalds
1332825f787bSToshi Kani #ifdef CONFIG_MEMORY_HOTREMOVE
1333825f787bSToshi Kani /**
1334825f787bSToshi Kani * release_mem_region_adjustable - release a previously reserved memory region
1335825f787bSToshi Kani * @start: resource start address
1336825f787bSToshi Kani * @size: resource region size
1337825f787bSToshi Kani *
1338825f787bSToshi Kani * This interface is intended for memory hot-delete. The requested region
1339825f787bSToshi Kani * is released from a currently busy memory resource. The requested region
1340825f787bSToshi Kani * must either match exactly or fit into a single busy resource entry. In
1341825f787bSToshi Kani * the latter case, the remaining resource is adjusted accordingly.
1342825f787bSToshi Kani * Existing children of the busy memory resource must be immutable in the
1343825f787bSToshi Kani * request.
1344825f787bSToshi Kani *
1345825f787bSToshi Kani * Note:
1346825f787bSToshi Kani * - Additional release conditions, such as overlapping region, can be
1347825f787bSToshi Kani * supported after they are confirmed as valid cases.
1348825f787bSToshi Kani * - When a busy memory resource gets split into two entries, the code
1349825f787bSToshi Kani * assumes that all children remain in the lower address entry for
1350825f787bSToshi Kani * simplicity. Enhance this logic when necessary.
1351825f787bSToshi Kani */
release_mem_region_adjustable(resource_size_t start,resource_size_t size)1352cb8e3c8bSDavid Hildenbrand void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1353825f787bSToshi Kani {
1354cb8e3c8bSDavid Hildenbrand struct resource *parent = &iomem_resource;
1355ec62d04eSDavid Hildenbrand struct resource *new_res = NULL;
1356ec62d04eSDavid Hildenbrand bool alloc_nofail = false;
1357825f787bSToshi Kani struct resource **p;
1358825f787bSToshi Kani struct resource *res;
1359825f787bSToshi Kani resource_size_t end;
1360825f787bSToshi Kani
1361825f787bSToshi Kani end = start + size - 1;
1362ec62d04eSDavid Hildenbrand if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1363ec62d04eSDavid Hildenbrand return;
1364825f787bSToshi Kani
1365ec62d04eSDavid Hildenbrand /*
1366ec62d04eSDavid Hildenbrand * We free up quite a lot of memory on memory hotunplug (esp., memap),
1367ec62d04eSDavid Hildenbrand * just before releasing the region. This is highly unlikely to
1368ec62d04eSDavid Hildenbrand * fail - let's play save and make it never fail as the caller cannot
1369ec62d04eSDavid Hildenbrand * perform any error handling (e.g., trying to re-add memory will fail
1370ec62d04eSDavid Hildenbrand * similarly).
1371ec62d04eSDavid Hildenbrand */
1372ec62d04eSDavid Hildenbrand retry:
1373ec62d04eSDavid Hildenbrand new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1374825f787bSToshi Kani
1375825f787bSToshi Kani p = &parent->child;
1376825f787bSToshi Kani write_lock(&resource_lock);
1377825f787bSToshi Kani
1378825f787bSToshi Kani while ((res = *p)) {
1379825f787bSToshi Kani if (res->start >= end)
1380825f787bSToshi Kani break;
1381825f787bSToshi Kani
1382825f787bSToshi Kani /* look for the next resource if it does not fit into */
1383825f787bSToshi Kani if (res->start > start || res->end < end) {
1384825f787bSToshi Kani p = &res->sibling;
1385825f787bSToshi Kani continue;
1386825f787bSToshi Kani }
1387825f787bSToshi Kani
1388825f787bSToshi Kani if (!(res->flags & IORESOURCE_MEM))
1389825f787bSToshi Kani break;
1390825f787bSToshi Kani
1391825f787bSToshi Kani if (!(res->flags & IORESOURCE_BUSY)) {
1392825f787bSToshi Kani p = &res->child;
1393825f787bSToshi Kani continue;
1394825f787bSToshi Kani }
1395825f787bSToshi Kani
1396825f787bSToshi Kani /* found the target resource; let's adjust accordingly */
1397825f787bSToshi Kani if (res->start == start && res->end == end) {
1398825f787bSToshi Kani /* free the whole entry */
1399825f787bSToshi Kani *p = res->sibling;
1400ebff7d8fSYasuaki Ishimatsu free_resource(res);
1401825f787bSToshi Kani } else if (res->start == start && res->end != end) {
1402825f787bSToshi Kani /* adjust the start */
1403ec62d04eSDavid Hildenbrand WARN_ON_ONCE(__adjust_resource(res, end + 1,
1404ec62d04eSDavid Hildenbrand res->end - end));
1405825f787bSToshi Kani } else if (res->start != start && res->end == end) {
1406825f787bSToshi Kani /* adjust the end */
1407ec62d04eSDavid Hildenbrand WARN_ON_ONCE(__adjust_resource(res, res->start,
1408ec62d04eSDavid Hildenbrand start - res->start));
1409825f787bSToshi Kani } else {
1410ec62d04eSDavid Hildenbrand /* split into two entries - we need a new resource */
1411825f787bSToshi Kani if (!new_res) {
1412ec62d04eSDavid Hildenbrand new_res = alloc_resource(GFP_ATOMIC);
1413ec62d04eSDavid Hildenbrand if (!new_res) {
1414ec62d04eSDavid Hildenbrand alloc_nofail = true;
1415ec62d04eSDavid Hildenbrand write_unlock(&resource_lock);
1416ec62d04eSDavid Hildenbrand goto retry;
1417ec62d04eSDavid Hildenbrand }
1418825f787bSToshi Kani }
1419825f787bSToshi Kani new_res->name = res->name;
1420825f787bSToshi Kani new_res->start = end + 1;
1421825f787bSToshi Kani new_res->end = res->end;
1422825f787bSToshi Kani new_res->flags = res->flags;
142343ee493bSToshi Kani new_res->desc = res->desc;
1424825f787bSToshi Kani new_res->parent = res->parent;
1425825f787bSToshi Kani new_res->sibling = res->sibling;
1426825f787bSToshi Kani new_res->child = NULL;
1427825f787bSToshi Kani
1428ec62d04eSDavid Hildenbrand if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1429ec62d04eSDavid Hildenbrand start - res->start)))
1430825f787bSToshi Kani break;
1431825f787bSToshi Kani res->sibling = new_res;
1432825f787bSToshi Kani new_res = NULL;
1433825f787bSToshi Kani }
1434825f787bSToshi Kani
1435825f787bSToshi Kani break;
1436825f787bSToshi Kani }
1437825f787bSToshi Kani
1438825f787bSToshi Kani write_unlock(&resource_lock);
1439ebff7d8fSYasuaki Ishimatsu free_resource(new_res);
1440825f787bSToshi Kani }
1441825f787bSToshi Kani #endif /* CONFIG_MEMORY_HOTREMOVE */
1442825f787bSToshi Kani
14439ca6551eSDavid Hildenbrand #ifdef CONFIG_MEMORY_HOTPLUG
system_ram_resources_mergeable(struct resource * r1,struct resource * r2)14449ca6551eSDavid Hildenbrand static bool system_ram_resources_mergeable(struct resource *r1,
14459ca6551eSDavid Hildenbrand struct resource *r2)
14469ca6551eSDavid Hildenbrand {
14479ca6551eSDavid Hildenbrand /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
14489ca6551eSDavid Hildenbrand return r1->flags == r2->flags && r1->end + 1 == r2->start &&
14499ca6551eSDavid Hildenbrand r1->name == r2->name && r1->desc == r2->desc &&
14509ca6551eSDavid Hildenbrand !r1->child && !r2->child;
14519ca6551eSDavid Hildenbrand }
14529ca6551eSDavid Hildenbrand
14533be8da57SMauro Carvalho Chehab /**
14549ca6551eSDavid Hildenbrand * merge_system_ram_resource - mark the System RAM resource mergeable and try to
14559ca6551eSDavid Hildenbrand * merge it with adjacent, mergeable resources
14569ca6551eSDavid Hildenbrand * @res: resource descriptor
14579ca6551eSDavid Hildenbrand *
14589ca6551eSDavid Hildenbrand * This interface is intended for memory hotplug, whereby lots of contiguous
14599ca6551eSDavid Hildenbrand * system ram resources are added (e.g., via add_memory*()) by a driver, and
14609ca6551eSDavid Hildenbrand * the actual resource boundaries are not of interest (e.g., it might be
14619ca6551eSDavid Hildenbrand * relevant for DIMMs). Only resources that are marked mergeable, that have the
14629ca6551eSDavid Hildenbrand * same parent, and that don't have any children are considered. All mergeable
14639ca6551eSDavid Hildenbrand * resources must be immutable during the request.
14649ca6551eSDavid Hildenbrand *
14659ca6551eSDavid Hildenbrand * Note:
14669ca6551eSDavid Hildenbrand * - The caller has to make sure that no pointers to resources that are
14679ca6551eSDavid Hildenbrand * marked mergeable are used anymore after this call - the resource might
14689ca6551eSDavid Hildenbrand * be freed and the pointer might be stale!
14699ca6551eSDavid Hildenbrand * - release_mem_region_adjustable() will split on demand on memory hotunplug
14709ca6551eSDavid Hildenbrand */
merge_system_ram_resource(struct resource * res)14719ca6551eSDavid Hildenbrand void merge_system_ram_resource(struct resource *res)
14729ca6551eSDavid Hildenbrand {
14739ca6551eSDavid Hildenbrand const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
14749ca6551eSDavid Hildenbrand struct resource *cur;
14759ca6551eSDavid Hildenbrand
14769ca6551eSDavid Hildenbrand if (WARN_ON_ONCE((res->flags & flags) != flags))
14779ca6551eSDavid Hildenbrand return;
14789ca6551eSDavid Hildenbrand
14799ca6551eSDavid Hildenbrand write_lock(&resource_lock);
14809ca6551eSDavid Hildenbrand res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
14819ca6551eSDavid Hildenbrand
14829ca6551eSDavid Hildenbrand /* Try to merge with next item in the list. */
14839ca6551eSDavid Hildenbrand cur = res->sibling;
14849ca6551eSDavid Hildenbrand if (cur && system_ram_resources_mergeable(res, cur)) {
14859ca6551eSDavid Hildenbrand res->end = cur->end;
14869ca6551eSDavid Hildenbrand res->sibling = cur->sibling;
14879ca6551eSDavid Hildenbrand free_resource(cur);
14889ca6551eSDavid Hildenbrand }
14899ca6551eSDavid Hildenbrand
14909ca6551eSDavid Hildenbrand /* Try to merge with previous item in the list. */
14919ca6551eSDavid Hildenbrand cur = res->parent->child;
14929ca6551eSDavid Hildenbrand while (cur && cur->sibling != res)
14939ca6551eSDavid Hildenbrand cur = cur->sibling;
14949ca6551eSDavid Hildenbrand if (cur && system_ram_resources_mergeable(cur, res)) {
14959ca6551eSDavid Hildenbrand cur->end = res->end;
14969ca6551eSDavid Hildenbrand cur->sibling = res->sibling;
14979ca6551eSDavid Hildenbrand free_resource(res);
14989ca6551eSDavid Hildenbrand }
14999ca6551eSDavid Hildenbrand write_unlock(&resource_lock);
15009ca6551eSDavid Hildenbrand }
15019ca6551eSDavid Hildenbrand #endif /* CONFIG_MEMORY_HOTPLUG */
15029ca6551eSDavid Hildenbrand
15031da177e4SLinus Torvalds /*
15049ac7849eSTejun Heo * Managed region resource
15059ac7849eSTejun Heo */
devm_resource_release(struct device * dev,void * ptr)15068d38821cSThierry Reding static void devm_resource_release(struct device *dev, void *ptr)
15078d38821cSThierry Reding {
15088d38821cSThierry Reding struct resource **r = ptr;
15098d38821cSThierry Reding
15108d38821cSThierry Reding release_resource(*r);
15118d38821cSThierry Reding }
15128d38821cSThierry Reding
15138d38821cSThierry Reding /**
15148d38821cSThierry Reding * devm_request_resource() - request and reserve an I/O or memory resource
15158d38821cSThierry Reding * @dev: device for which to request the resource
15168d38821cSThierry Reding * @root: root of the resource tree from which to request the resource
15178d38821cSThierry Reding * @new: descriptor of the resource to request
15188d38821cSThierry Reding *
15198d38821cSThierry Reding * This is a device-managed version of request_resource(). There is usually
15208d38821cSThierry Reding * no need to release resources requested by this function explicitly since
15218d38821cSThierry Reding * that will be taken care of when the device is unbound from its driver.
15228d38821cSThierry Reding * If for some reason the resource needs to be released explicitly, because
15238d38821cSThierry Reding * of ordering issues for example, drivers must call devm_release_resource()
15248d38821cSThierry Reding * rather than the regular release_resource().
15258d38821cSThierry Reding *
15268d38821cSThierry Reding * When a conflict is detected between any existing resources and the newly
15278d38821cSThierry Reding * requested resource, an error message will be printed.
15288d38821cSThierry Reding *
15298d38821cSThierry Reding * Returns 0 on success or a negative error code on failure.
15308d38821cSThierry Reding */
devm_request_resource(struct device * dev,struct resource * root,struct resource * new)15318d38821cSThierry Reding int devm_request_resource(struct device *dev, struct resource *root,
15328d38821cSThierry Reding struct resource *new)
15338d38821cSThierry Reding {
15348d38821cSThierry Reding struct resource *conflict, **ptr;
15358d38821cSThierry Reding
15368d38821cSThierry Reding ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
15378d38821cSThierry Reding if (!ptr)
15388d38821cSThierry Reding return -ENOMEM;
15398d38821cSThierry Reding
15408d38821cSThierry Reding *ptr = new;
15418d38821cSThierry Reding
15428d38821cSThierry Reding conflict = request_resource_conflict(root, new);
15438d38821cSThierry Reding if (conflict) {
15448d38821cSThierry Reding dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
15458d38821cSThierry Reding new, conflict->name, conflict);
15468d38821cSThierry Reding devres_free(ptr);
15478d38821cSThierry Reding return -EBUSY;
15488d38821cSThierry Reding }
15498d38821cSThierry Reding
15508d38821cSThierry Reding devres_add(dev, ptr);
15518d38821cSThierry Reding return 0;
15528d38821cSThierry Reding }
15538d38821cSThierry Reding EXPORT_SYMBOL(devm_request_resource);
15548d38821cSThierry Reding
devm_resource_match(struct device * dev,void * res,void * data)15558d38821cSThierry Reding static int devm_resource_match(struct device *dev, void *res, void *data)
15568d38821cSThierry Reding {
15578d38821cSThierry Reding struct resource **ptr = res;
15588d38821cSThierry Reding
15598d38821cSThierry Reding return *ptr == data;
15608d38821cSThierry Reding }
15618d38821cSThierry Reding
15628d38821cSThierry Reding /**
15638d38821cSThierry Reding * devm_release_resource() - release a previously requested resource
15648d38821cSThierry Reding * @dev: device for which to release the resource
15658d38821cSThierry Reding * @new: descriptor of the resource to release
15668d38821cSThierry Reding *
15678d38821cSThierry Reding * Releases a resource previously requested using devm_request_resource().
15688d38821cSThierry Reding */
devm_release_resource(struct device * dev,struct resource * new)15698d38821cSThierry Reding void devm_release_resource(struct device *dev, struct resource *new)
15708d38821cSThierry Reding {
15718d38821cSThierry Reding WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
15728d38821cSThierry Reding new));
15738d38821cSThierry Reding }
15748d38821cSThierry Reding EXPORT_SYMBOL(devm_release_resource);
15758d38821cSThierry Reding
15769ac7849eSTejun Heo struct region_devres {
15779ac7849eSTejun Heo struct resource *parent;
15789ac7849eSTejun Heo resource_size_t start;
15799ac7849eSTejun Heo resource_size_t n;
15809ac7849eSTejun Heo };
15819ac7849eSTejun Heo
devm_region_release(struct device * dev,void * res)15829ac7849eSTejun Heo static void devm_region_release(struct device *dev, void *res)
15839ac7849eSTejun Heo {
15849ac7849eSTejun Heo struct region_devres *this = res;
15859ac7849eSTejun Heo
15869ac7849eSTejun Heo __release_region(this->parent, this->start, this->n);
15879ac7849eSTejun Heo }
15889ac7849eSTejun Heo
devm_region_match(struct device * dev,void * res,void * match_data)15899ac7849eSTejun Heo static int devm_region_match(struct device *dev, void *res, void *match_data)
15909ac7849eSTejun Heo {
15919ac7849eSTejun Heo struct region_devres *this = res, *match = match_data;
15929ac7849eSTejun Heo
15939ac7849eSTejun Heo return this->parent == match->parent &&
15949ac7849eSTejun Heo this->start == match->start && this->n == match->n;
15959ac7849eSTejun Heo }
15969ac7849eSTejun Heo
1597b69c2e20SBorislav Petkov struct resource *
__devm_request_region(struct device * dev,struct resource * parent,resource_size_t start,resource_size_t n,const char * name)1598b69c2e20SBorislav Petkov __devm_request_region(struct device *dev, struct resource *parent,
1599b69c2e20SBorislav Petkov resource_size_t start, resource_size_t n, const char *name)
16009ac7849eSTejun Heo {
16019ac7849eSTejun Heo struct region_devres *dr = NULL;
16029ac7849eSTejun Heo struct resource *res;
16039ac7849eSTejun Heo
16049ac7849eSTejun Heo dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
16059ac7849eSTejun Heo GFP_KERNEL);
16069ac7849eSTejun Heo if (!dr)
16079ac7849eSTejun Heo return NULL;
16089ac7849eSTejun Heo
16099ac7849eSTejun Heo dr->parent = parent;
16109ac7849eSTejun Heo dr->start = start;
16119ac7849eSTejun Heo dr->n = n;
16129ac7849eSTejun Heo
1613e8de1481SArjan van de Ven res = __request_region(parent, start, n, name, 0);
16149ac7849eSTejun Heo if (res)
16159ac7849eSTejun Heo devres_add(dev, dr);
16169ac7849eSTejun Heo else
16179ac7849eSTejun Heo devres_free(dr);
16189ac7849eSTejun Heo
16199ac7849eSTejun Heo return res;
16209ac7849eSTejun Heo }
16219ac7849eSTejun Heo EXPORT_SYMBOL(__devm_request_region);
16229ac7849eSTejun Heo
__devm_release_region(struct device * dev,struct resource * parent,resource_size_t start,resource_size_t n)16239ac7849eSTejun Heo void __devm_release_region(struct device *dev, struct resource *parent,
16249ac7849eSTejun Heo resource_size_t start, resource_size_t n)
16259ac7849eSTejun Heo {
16269ac7849eSTejun Heo struct region_devres match_data = { parent, start, n };
16279ac7849eSTejun Heo
16289ac7849eSTejun Heo __release_region(parent, start, n);
16299ac7849eSTejun Heo WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
16309ac7849eSTejun Heo &match_data));
16319ac7849eSTejun Heo }
16329ac7849eSTejun Heo EXPORT_SYMBOL(__devm_release_region);
16339ac7849eSTejun Heo
16349ac7849eSTejun Heo /*
1635ffd2e8dfSBjorn Helgaas * Reserve I/O ports or memory based on "reserve=" kernel parameter.
16361da177e4SLinus Torvalds */
16371da177e4SLinus Torvalds #define MAXRESERVE 4
reserve_setup(char * str)16381da177e4SLinus Torvalds static int __init reserve_setup(char *str)
16391da177e4SLinus Torvalds {
16401da177e4SLinus Torvalds static int reserved;
16411da177e4SLinus Torvalds static struct resource reserve[MAXRESERVE];
16421da177e4SLinus Torvalds
16431da177e4SLinus Torvalds for (;;) {
16448bc1ad7dSZhang Rui unsigned int io_start, io_num;
16451da177e4SLinus Torvalds int x = reserved;
1646ffd2e8dfSBjorn Helgaas struct resource *parent;
16471da177e4SLinus Torvalds
16481da177e4SLinus Torvalds if (get_option(&str, &io_start) != 2)
16491da177e4SLinus Torvalds break;
16501da177e4SLinus Torvalds if (get_option(&str, &io_num) == 0)
16511da177e4SLinus Torvalds break;
16521da177e4SLinus Torvalds if (x < MAXRESERVE) {
16531da177e4SLinus Torvalds struct resource *res = reserve + x;
1654ffd2e8dfSBjorn Helgaas
1655ffd2e8dfSBjorn Helgaas /*
1656ffd2e8dfSBjorn Helgaas * If the region starts below 0x10000, we assume it's
1657ffd2e8dfSBjorn Helgaas * I/O port space; otherwise assume it's memory.
1658ffd2e8dfSBjorn Helgaas */
1659ffd2e8dfSBjorn Helgaas if (io_start < 0x10000) {
1660ffd2e8dfSBjorn Helgaas res->flags = IORESOURCE_IO;
1661ffd2e8dfSBjorn Helgaas parent = &ioport_resource;
1662ffd2e8dfSBjorn Helgaas } else {
1663ffd2e8dfSBjorn Helgaas res->flags = IORESOURCE_MEM;
1664ffd2e8dfSBjorn Helgaas parent = &iomem_resource;
1665ffd2e8dfSBjorn Helgaas }
16661da177e4SLinus Torvalds res->name = "reserved";
16671da177e4SLinus Torvalds res->start = io_start;
16681da177e4SLinus Torvalds res->end = io_start + io_num - 1;
1669ffd2e8dfSBjorn Helgaas res->flags |= IORESOURCE_BUSY;
167043ee493bSToshi Kani res->desc = IORES_DESC_NONE;
16711da177e4SLinus Torvalds res->child = NULL;
1672ffd2e8dfSBjorn Helgaas if (request_resource(parent, res) == 0)
16731da177e4SLinus Torvalds reserved = x+1;
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds return 1;
16771da177e4SLinus Torvalds }
16781da177e4SLinus Torvalds __setup("reserve=", reserve_setup);
1679379daf62SSuresh Siddha
1680379daf62SSuresh Siddha /*
1681379daf62SSuresh Siddha * Check if the requested addr and size spans more than any slot in the
1682379daf62SSuresh Siddha * iomem resource tree.
1683379daf62SSuresh Siddha */
iomem_map_sanity_check(resource_size_t addr,unsigned long size)1684379daf62SSuresh Siddha int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1685379daf62SSuresh Siddha {
1686379daf62SSuresh Siddha struct resource *p = &iomem_resource;
16872a4e6285SAndy Shevchenko resource_size_t end = addr + size - 1;
1688379daf62SSuresh Siddha int err = 0;
1689379daf62SSuresh Siddha loff_t l;
1690379daf62SSuresh Siddha
1691379daf62SSuresh Siddha read_lock(&resource_lock);
1692379daf62SSuresh Siddha for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1693379daf62SSuresh Siddha /*
1694379daf62SSuresh Siddha * We can probably skip the resources without
1695379daf62SSuresh Siddha * IORESOURCE_IO attribute?
1696379daf62SSuresh Siddha */
16972a4e6285SAndy Shevchenko if (p->start > end)
1698379daf62SSuresh Siddha continue;
1699379daf62SSuresh Siddha if (p->end < addr)
1700379daf62SSuresh Siddha continue;
1701d68612b2SSuresh Siddha if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
17022a4e6285SAndy Shevchenko PFN_DOWN(p->end) >= PFN_DOWN(end))
1703379daf62SSuresh Siddha continue;
17043ac52669SArjan van de Ven /*
17053ac52669SArjan van de Ven * if a resource is "BUSY", it's not a hardware resource
17063ac52669SArjan van de Ven * but a driver mapping of such a resource; we don't want
17073ac52669SArjan van de Ven * to warn for those; some drivers legitimately map only
17083ac52669SArjan van de Ven * partial hardware resources. (example: vesafb)
17093ac52669SArjan van de Ven */
17103ac52669SArjan van de Ven if (p->flags & IORESOURCE_BUSY)
17113ac52669SArjan van de Ven continue;
17123ac52669SArjan van de Ven
17132a4e6285SAndy Shevchenko pr_warn("resource sanity check: requesting [mem %pa-%pa], which spans more than %s %pR\n",
17142a4e6285SAndy Shevchenko &addr, &end, p->name, p);
1715379daf62SSuresh Siddha err = -1;
1716379daf62SSuresh Siddha break;
1717379daf62SSuresh Siddha }
1718379daf62SSuresh Siddha read_unlock(&resource_lock);
1719379daf62SSuresh Siddha
1720379daf62SSuresh Siddha return err;
1721379daf62SSuresh Siddha }
1722e8de1481SArjan van de Ven
1723e8de1481SArjan van de Ven #ifdef CONFIG_STRICT_DEVMEM
1724e8de1481SArjan van de Ven static int strict_iomem_checks = 1;
1725e8de1481SArjan van de Ven #else
1726e8de1481SArjan van de Ven static int strict_iomem_checks;
1727e8de1481SArjan van de Ven #endif
1728e8de1481SArjan van de Ven
1729e8de1481SArjan van de Ven /*
1730a9e7b8d4SDavid Hildenbrand * Check if an address is exclusive to the kernel and must not be mapped to
1731a9e7b8d4SDavid Hildenbrand * user space, for example, via /dev/mem.
1732a9e7b8d4SDavid Hildenbrand *
1733a9e7b8d4SDavid Hildenbrand * Returns true if exclusive to the kernel, otherwise returns false.
1734e8de1481SArjan van de Ven */
resource_is_exclusive(struct resource * root,u64 addr,resource_size_t size)173527829479SIra Weiny bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size)
1736e8de1481SArjan van de Ven {
1737a9e7b8d4SDavid Hildenbrand const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
1738a9e7b8d4SDavid Hildenbrand IORESOURCE_EXCLUSIVE;
1739b78dfa05SDavid Hildenbrand bool skip_children = false, err = false;
1740b78dfa05SDavid Hildenbrand struct resource *p;
1741e8de1481SArjan van de Ven
1742e8de1481SArjan van de Ven read_lock(&resource_lock);
174327829479SIra Weiny for_each_resource(root, p, skip_children) {
1744e8de1481SArjan van de Ven if (p->start >= addr + size)
1745e8de1481SArjan van de Ven break;
1746b78dfa05SDavid Hildenbrand if (p->end < addr) {
1747b78dfa05SDavid Hildenbrand skip_children = true;
1748e8de1481SArjan van de Ven continue;
1749b78dfa05SDavid Hildenbrand }
1750b78dfa05SDavid Hildenbrand skip_children = false;
1751b78dfa05SDavid Hildenbrand
175290a545e9SDan Williams /*
1753a9e7b8d4SDavid Hildenbrand * IORESOURCE_SYSTEM_RAM resources are exclusive if
1754a9e7b8d4SDavid Hildenbrand * IORESOURCE_EXCLUSIVE is set, even if they
1755a9e7b8d4SDavid Hildenbrand * are not busy and even if "iomem=relaxed" is set. The
1756a9e7b8d4SDavid Hildenbrand * responsible driver dynamically adds/removes system RAM within
1757a9e7b8d4SDavid Hildenbrand * such an area and uncontrolled access is dangerous.
1758a9e7b8d4SDavid Hildenbrand */
1759a9e7b8d4SDavid Hildenbrand if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
1760a9e7b8d4SDavid Hildenbrand err = true;
1761a9e7b8d4SDavid Hildenbrand break;
1762a9e7b8d4SDavid Hildenbrand }
1763a9e7b8d4SDavid Hildenbrand
1764a9e7b8d4SDavid Hildenbrand /*
176590a545e9SDan Williams * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
176690a545e9SDan Williams * or CONFIG_IO_STRICT_DEVMEM is enabled and the
176790a545e9SDan Williams * resource is busy.
176890a545e9SDan Williams */
1769a9e7b8d4SDavid Hildenbrand if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
177090a545e9SDan Williams continue;
177190a545e9SDan Williams if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
177290a545e9SDan Williams || p->flags & IORESOURCE_EXCLUSIVE) {
17739825b451SYaowei Bai err = true;
1774e8de1481SArjan van de Ven break;
1775e8de1481SArjan van de Ven }
1776e8de1481SArjan van de Ven }
1777e8de1481SArjan van de Ven read_unlock(&resource_lock);
1778e8de1481SArjan van de Ven
1779e8de1481SArjan van de Ven return err;
1780e8de1481SArjan van de Ven }
1781e8de1481SArjan van de Ven
iomem_is_exclusive(u64 addr)178227829479SIra Weiny bool iomem_is_exclusive(u64 addr)
178327829479SIra Weiny {
178427829479SIra Weiny return resource_is_exclusive(&iomem_resource, addr & PAGE_MASK,
178527829479SIra Weiny PAGE_SIZE);
178627829479SIra Weiny }
178727829479SIra Weiny
resource_list_create_entry(struct resource * res,size_t extra_size)178890e97820SJiang Liu struct resource_entry *resource_list_create_entry(struct resource *res,
178990e97820SJiang Liu size_t extra_size)
179090e97820SJiang Liu {
179190e97820SJiang Liu struct resource_entry *entry;
179290e97820SJiang Liu
179390e97820SJiang Liu entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
179490e97820SJiang Liu if (entry) {
179590e97820SJiang Liu INIT_LIST_HEAD(&entry->node);
179690e97820SJiang Liu entry->res = res ? res : &entry->__res;
179790e97820SJiang Liu }
179890e97820SJiang Liu
179990e97820SJiang Liu return entry;
180090e97820SJiang Liu }
180190e97820SJiang Liu EXPORT_SYMBOL(resource_list_create_entry);
180290e97820SJiang Liu
resource_list_free(struct list_head * head)180390e97820SJiang Liu void resource_list_free(struct list_head *head)
180490e97820SJiang Liu {
180590e97820SJiang Liu struct resource_entry *entry, *tmp;
180690e97820SJiang Liu
180790e97820SJiang Liu list_for_each_entry_safe(entry, tmp, head, node)
180890e97820SJiang Liu resource_list_destroy_entry(entry);
180990e97820SJiang Liu }
181090e97820SJiang Liu EXPORT_SYMBOL(resource_list_free);
181190e97820SJiang Liu
181214b80582SDan Williams #ifdef CONFIG_GET_FREE_REGION
181314b80582SDan Williams #define GFR_DESCENDING (1UL << 0)
181414b80582SDan Williams #define GFR_REQUEST_REGION (1UL << 1)
181514b80582SDan Williams #define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT)
181614b80582SDan Williams
gfr_start(struct resource * base,resource_size_t size,resource_size_t align,unsigned long flags)181714b80582SDan Williams static resource_size_t gfr_start(struct resource *base, resource_size_t size,
181814b80582SDan Williams resource_size_t align, unsigned long flags)
18190c385190SChristoph Hellwig {
182014b80582SDan Williams if (flags & GFR_DESCENDING) {
182114b80582SDan Williams resource_size_t end;
182214b80582SDan Williams
18230b46b4acSThomas Gleixner end = min_t(resource_size_t, base->end, PHYSMEM_END);
182414b80582SDan Williams return end - size + 1;
182514b80582SDan Williams }
182614b80582SDan Williams
182714b80582SDan Williams return ALIGN(base->start, align);
182814b80582SDan Williams }
182914b80582SDan Williams
gfr_continue(struct resource * base,resource_size_t addr,resource_size_t size,unsigned long flags)183014b80582SDan Williams static bool gfr_continue(struct resource *base, resource_size_t addr,
183114b80582SDan Williams resource_size_t size, unsigned long flags)
183214b80582SDan Williams {
183314b80582SDan Williams if (flags & GFR_DESCENDING)
183414b80582SDan Williams return addr > size && addr >= base->start;
183514b80582SDan Williams /*
183614b80582SDan Williams * In the ascend case be careful that the last increment by
183714b80582SDan Williams * @size did not wrap 0.
183814b80582SDan Williams */
183914b80582SDan Williams return addr > addr - size &&
18400b46b4acSThomas Gleixner addr <= min_t(resource_size_t, base->end, PHYSMEM_END);
184114b80582SDan Williams }
184214b80582SDan Williams
gfr_next(resource_size_t addr,resource_size_t size,unsigned long flags)184314b80582SDan Williams static resource_size_t gfr_next(resource_size_t addr, resource_size_t size,
184414b80582SDan Williams unsigned long flags)
184514b80582SDan Williams {
184614b80582SDan Williams if (flags & GFR_DESCENDING)
184714b80582SDan Williams return addr - size;
184814b80582SDan Williams return addr + size;
184914b80582SDan Williams }
185014b80582SDan Williams
remove_free_mem_region(void * _res)185114b80582SDan Williams static void remove_free_mem_region(void *_res)
185214b80582SDan Williams {
185314b80582SDan Williams struct resource *res = _res;
185414b80582SDan Williams
185514b80582SDan Williams if (res->parent)
185614b80582SDan Williams remove_resource(res);
185714b80582SDan Williams free_resource(res);
185814b80582SDan Williams }
185914b80582SDan Williams
186014b80582SDan 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)186114b80582SDan Williams get_free_mem_region(struct device *dev, struct resource *base,
186214b80582SDan Williams resource_size_t size, const unsigned long align,
186314b80582SDan Williams const char *name, const unsigned long desc,
186414b80582SDan Williams const unsigned long flags)
186514b80582SDan Williams {
186614b80582SDan Williams resource_size_t addr;
18670c385190SChristoph Hellwig struct resource *res;
186856fd9491SAlistair Popple struct region_devres *dr = NULL;
18690c385190SChristoph Hellwig
187014b80582SDan Williams size = ALIGN(size, align);
18710c385190SChristoph Hellwig
187256fd9491SAlistair Popple res = alloc_resource(GFP_KERNEL);
187356fd9491SAlistair Popple if (!res)
187456fd9491SAlistair Popple return ERR_PTR(-ENOMEM);
187556fd9491SAlistair Popple
187614b80582SDan Williams if (dev && (flags & GFR_REQUEST_REGION)) {
187756fd9491SAlistair Popple dr = devres_alloc(devm_region_release,
187856fd9491SAlistair Popple sizeof(struct region_devres), GFP_KERNEL);
187956fd9491SAlistair Popple if (!dr) {
188056fd9491SAlistair Popple free_resource(res);
188156fd9491SAlistair Popple return ERR_PTR(-ENOMEM);
188256fd9491SAlistair Popple }
188314b80582SDan Williams } else if (dev) {
188414b80582SDan Williams if (devm_add_action_or_reset(dev, remove_free_mem_region, res))
188514b80582SDan Williams return ERR_PTR(-ENOMEM);
188656fd9491SAlistair Popple }
188756fd9491SAlistair Popple
188856fd9491SAlistair Popple write_lock(&resource_lock);
188914b80582SDan Williams for (addr = gfr_start(base, size, align, flags);
1890e4a5b2f6SAlison Schofield gfr_continue(base, addr, align, flags);
1891e4a5b2f6SAlison Schofield addr = gfr_next(addr, align, flags)) {
189214b80582SDan Williams if (__region_intersects(base, addr, size, 0, IORES_DESC_NONE) !=
18930c385190SChristoph Hellwig REGION_DISJOINT)
18940c385190SChristoph Hellwig continue;
18950c385190SChristoph Hellwig
189614b80582SDan Williams if (flags & GFR_REQUEST_REGION) {
189714b80582SDan Williams if (__request_region_locked(res, &iomem_resource, addr,
189814b80582SDan Williams size, name, 0))
189956fd9491SAlistair Popple break;
190056fd9491SAlistair Popple
190156fd9491SAlistair Popple if (dev) {
190256fd9491SAlistair Popple dr->parent = &iomem_resource;
190356fd9491SAlistair Popple dr->start = addr;
190456fd9491SAlistair Popple dr->n = size;
190556fd9491SAlistair Popple devres_add(dev, dr);
190656fd9491SAlistair Popple }
190756fd9491SAlistair Popple
190814b80582SDan Williams res->desc = desc;
190956fd9491SAlistair Popple write_unlock(&resource_lock);
191056fd9491SAlistair Popple
191114b80582SDan Williams
191256fd9491SAlistair Popple /*
191314b80582SDan Williams * A driver is claiming this region so revoke any
191414b80582SDan Williams * mappings.
191556fd9491SAlistair Popple */
191656fd9491SAlistair Popple revoke_iomem(res);
191714b80582SDan Williams } else {
191814b80582SDan Williams res->start = addr;
191914b80582SDan Williams res->end = addr + size - 1;
192014b80582SDan Williams res->name = name;
192114b80582SDan Williams res->desc = desc;
192214b80582SDan Williams res->flags = IORESOURCE_MEM;
192314b80582SDan Williams
192414b80582SDan Williams /*
192514b80582SDan Williams * Only succeed if the resource hosts an exclusive
192614b80582SDan Williams * range after the insert
192714b80582SDan Williams */
192814b80582SDan Williams if (__insert_resource(base, res) || res->child)
192914b80582SDan Williams break;
193014b80582SDan Williams
193114b80582SDan Williams write_unlock(&resource_lock);
193214b80582SDan Williams }
193314b80582SDan Williams
19340c385190SChristoph Hellwig return res;
19350c385190SChristoph Hellwig }
193656fd9491SAlistair Popple write_unlock(&resource_lock);
193756fd9491SAlistair Popple
193814b80582SDan Williams if (flags & GFR_REQUEST_REGION) {
193956fd9491SAlistair Popple free_resource(res);
194056fd9491SAlistair Popple devres_free(dr);
194114b80582SDan Williams } else if (dev)
194214b80582SDan Williams devm_release_action(dev, remove_free_mem_region, res);
19430c385190SChristoph Hellwig
19440c385190SChristoph Hellwig return ERR_PTR(-ERANGE);
19450c385190SChristoph Hellwig }
19460c385190SChristoph Hellwig
19470092908dSChristoph Hellwig /**
19480092908dSChristoph Hellwig * devm_request_free_mem_region - find free region for device private memory
19490092908dSChristoph Hellwig *
19500092908dSChristoph Hellwig * @dev: device struct to bind the resource to
19510092908dSChristoph Hellwig * @size: size in bytes of the device memory to add
19520092908dSChristoph Hellwig * @base: resource tree to look in
19530092908dSChristoph Hellwig *
19540092908dSChristoph Hellwig * This function tries to find an empty range of physical address big enough to
19550092908dSChristoph Hellwig * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
19560092908dSChristoph Hellwig * memory, which in turn allocates struct pages.
19570092908dSChristoph Hellwig */
devm_request_free_mem_region(struct device * dev,struct resource * base,unsigned long size)19580092908dSChristoph Hellwig struct resource *devm_request_free_mem_region(struct device *dev,
19590092908dSChristoph Hellwig struct resource *base, unsigned long size)
19600092908dSChristoph Hellwig {
196114b80582SDan Williams unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
196214b80582SDan Williams
196314b80582SDan Williams return get_free_mem_region(dev, base, size, GFR_DEFAULT_ALIGN,
196414b80582SDan Williams dev_name(dev),
196514b80582SDan Williams IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
19660092908dSChristoph Hellwig }
19670092908dSChristoph Hellwig EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
19680c385190SChristoph Hellwig
request_free_mem_region(struct resource * base,unsigned long size,const char * name)19690c385190SChristoph Hellwig struct resource *request_free_mem_region(struct resource *base,
19700c385190SChristoph Hellwig unsigned long size, const char *name)
19710c385190SChristoph Hellwig {
197214b80582SDan Williams unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION;
197314b80582SDan Williams
197414b80582SDan Williams return get_free_mem_region(NULL, base, size, GFR_DEFAULT_ALIGN, name,
197514b80582SDan Williams IORES_DESC_DEVICE_PRIVATE_MEMORY, flags);
19760c385190SChristoph Hellwig }
19770c385190SChristoph Hellwig EXPORT_SYMBOL_GPL(request_free_mem_region);
19780c385190SChristoph Hellwig
197914b80582SDan Williams /**
198014b80582SDan Williams * alloc_free_mem_region - find a free region relative to @base
198114b80582SDan Williams * @base: resource that will parent the new resource
198214b80582SDan Williams * @size: size in bytes of memory to allocate from @base
198314b80582SDan Williams * @align: alignment requirements for the allocation
198414b80582SDan Williams * @name: resource name
198514b80582SDan Williams *
198614b80582SDan Williams * Buses like CXL, that can dynamically instantiate new memory regions,
198714b80582SDan Williams * need a method to allocate physical address space for those regions.
198814b80582SDan Williams * Allocate and insert a new resource to cover a free, unclaimed by a
198914b80582SDan Williams * descendant of @base, range in the span of @base.
199014b80582SDan Williams */
alloc_free_mem_region(struct resource * base,unsigned long size,unsigned long align,const char * name)199114b80582SDan Williams struct resource *alloc_free_mem_region(struct resource *base,
199214b80582SDan Williams unsigned long size, unsigned long align,
199314b80582SDan Williams const char *name)
199414b80582SDan Williams {
199514b80582SDan Williams /* Default of ascending direction and insert resource */
199614b80582SDan Williams unsigned long flags = 0;
199714b80582SDan Williams
199814b80582SDan Williams return get_free_mem_region(NULL, base, size, align, name,
199914b80582SDan Williams IORES_DESC_NONE, flags);
200014b80582SDan Williams }
200114b80582SDan Williams EXPORT_SYMBOL_NS_GPL(alloc_free_mem_region, CXL);
200214b80582SDan Williams #endif /* CONFIG_GET_FREE_REGION */
20030092908dSChristoph Hellwig
strict_iomem(char * str)2004e8de1481SArjan van de Ven static int __init strict_iomem(char *str)
2005e8de1481SArjan van de Ven {
2006e8de1481SArjan van de Ven if (strstr(str, "relaxed"))
2007e8de1481SArjan van de Ven strict_iomem_checks = 0;
2008e8de1481SArjan van de Ven if (strstr(str, "strict"))
2009e8de1481SArjan van de Ven strict_iomem_checks = 1;
2010e8de1481SArjan van de Ven return 1;
2011e8de1481SArjan van de Ven }
2012e8de1481SArjan van de Ven
iomem_fs_init_fs_context(struct fs_context * fc)201371a1d8edSDaniel Vetter static int iomem_fs_init_fs_context(struct fs_context *fc)
201471a1d8edSDaniel Vetter {
201571a1d8edSDaniel Vetter return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
201671a1d8edSDaniel Vetter }
201771a1d8edSDaniel Vetter
201871a1d8edSDaniel Vetter static struct file_system_type iomem_fs_type = {
201971a1d8edSDaniel Vetter .name = "iomem",
202071a1d8edSDaniel Vetter .owner = THIS_MODULE,
202171a1d8edSDaniel Vetter .init_fs_context = iomem_fs_init_fs_context,
202271a1d8edSDaniel Vetter .kill_sb = kill_anon_super,
202371a1d8edSDaniel Vetter };
202471a1d8edSDaniel Vetter
iomem_init_inode(void)202571a1d8edSDaniel Vetter static int __init iomem_init_inode(void)
202671a1d8edSDaniel Vetter {
202771a1d8edSDaniel Vetter static struct vfsmount *iomem_vfs_mount;
202871a1d8edSDaniel Vetter static int iomem_fs_cnt;
202971a1d8edSDaniel Vetter struct inode *inode;
203071a1d8edSDaniel Vetter int rc;
203171a1d8edSDaniel Vetter
203271a1d8edSDaniel Vetter rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
203371a1d8edSDaniel Vetter if (rc < 0) {
203471a1d8edSDaniel Vetter pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
203571a1d8edSDaniel Vetter return rc;
203671a1d8edSDaniel Vetter }
203771a1d8edSDaniel Vetter
203871a1d8edSDaniel Vetter inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
203971a1d8edSDaniel Vetter if (IS_ERR(inode)) {
204071a1d8edSDaniel Vetter rc = PTR_ERR(inode);
204171a1d8edSDaniel Vetter pr_err("Cannot allocate inode for iomem: %d\n", rc);
204271a1d8edSDaniel Vetter simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
204371a1d8edSDaniel Vetter return rc;
204471a1d8edSDaniel Vetter }
204571a1d8edSDaniel Vetter
204671a1d8edSDaniel Vetter /*
204771a1d8edSDaniel Vetter * Publish iomem revocation inode initialized.
204871a1d8edSDaniel Vetter * Pairs with smp_load_acquire() in revoke_iomem().
204971a1d8edSDaniel Vetter */
205071a1d8edSDaniel Vetter smp_store_release(&iomem_inode, inode);
205171a1d8edSDaniel Vetter
205271a1d8edSDaniel Vetter return 0;
205371a1d8edSDaniel Vetter }
205471a1d8edSDaniel Vetter
205571a1d8edSDaniel Vetter fs_initcall(iomem_init_inode);
205671a1d8edSDaniel Vetter
2057e8de1481SArjan van de Ven __setup("iomem=", strict_iomem);
2058