blob: 9c08d6e9eef27533baa011ed637c0b40338a0f3c [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/kernel/resource.c
4 *
5 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
7 *
8 * Arbitrary resource management.
9 */
10
Octavian Purdila65fed8f2012-07-30 14:42:58 -070011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Paul Gortmaker9984de12011-05-23 14:51:41 -040013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/ioport.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/spinlock.h>
19#include <linux/fs.h>
20#include <linux/proc_fs.h>
Daniel Vetter71a1d8e2020-11-27 17:41:24 +010021#include <linux/pseudo_fs.h>
Alan Cox8b6d0432010-03-29 19:38:00 +020022#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/seq_file.h>
Tejun Heo9ac78492007-01-20 16:00:26 +090024#include <linux/device.h>
Suresh Siddhad68612b2008-10-28 11:45:42 -070025#include <linux/pfn.h>
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -070026#include <linux/mm.h>
Daniel Vetter71a1d8e2020-11-27 17:41:24 +010027#include <linux/mount.h>
Jiang Liu90e97822015-02-05 13:44:43 +080028#include <linux/resource_ext.h>
Daniel Vetter71a1d8e2020-11-27 17:41:24 +010029#include <uapi/linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31
32
33struct resource ioport_resource = {
34 .name = "PCI IO",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070035 .start = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .end = IO_SPACE_LIMIT,
37 .flags = IORESOURCE_IO,
38};
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL(ioport_resource);
40
41struct resource iomem_resource = {
42 .name = "PCI mem",
Greg Kroah-Hartman6550e072006-06-12 17:11:31 -070043 .start = 0,
44 .end = -1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .flags = IORESOURCE_MEM,
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047EXPORT_SYMBOL(iomem_resource);
48
Ram Pai23c570a2011-07-05 23:44:30 -070049/* constraints to be met while allocating resources */
50struct resource_constraint {
51 resource_size_t min, max, align;
52 resource_size_t (*alignf)(void *, const struct resource *,
53 resource_size_t, resource_size_t);
54 void *alignf_data;
55};
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static DEFINE_RWLOCK(resource_lock);
58
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -070059/*
60 * For memory hotplug, there is no way to free resource entries allocated
61 * by boot mem after the system is up. So for reusing the resource entry
62 * we need to remember the resource.
63 */
64static struct resource *bootmem_resource_free;
65static DEFINE_SPINLOCK(bootmem_resource_lock);
66
David Hildenbrand97523a42021-05-06 18:05:20 -070067static struct resource *next_resource(struct resource *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (p->child)
70 return p->child;
71 while (!p->sibling && p->parent)
72 p = p->parent;
73 return p->sibling;
74}
75
David Hildenbrandb78dfa02021-11-08 18:35:46 -080076static struct resource *next_resource_skip_children(struct resource *p)
77{
78 while (!p->sibling && p->parent)
79 p = p->parent;
80 return p->sibling;
81}
82
83#define for_each_resource(_root, _p, _skip_children) \
84 for ((_p) = (_root)->child; (_p); \
85 (_p) = (_skip_children) ? next_resource_skip_children(_p) : \
86 next_resource(_p))
87
Vivek Goyal8c86e702014-08-08 14:25:50 -070088static void *r_next(struct seq_file *m, void *v, loff_t *pos)
89{
90 struct resource *p = v;
91 (*pos)++;
David Hildenbrand97523a42021-05-06 18:05:20 -070092 return (void *)next_resource(p);
Vivek Goyal8c86e702014-08-08 14:25:50 -070093}
94
Ingo Molnar13eb8372008-09-26 10:10:12 +020095#ifdef CONFIG_PROC_FS
96
97enum { MAX_IORES_LEVEL = 5 };
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static void *r_start(struct seq_file *m, loff_t *pos)
100 __acquires(resource_lock)
101{
Muchun Song359745d2022-01-21 22:14:23 -0800102 struct resource *p = pde_data(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 loff_t l = 0;
104 read_lock(&resource_lock);
105 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
106 ;
107 return p;
108}
109
110static void r_stop(struct seq_file *m, void *v)
111 __releases(resource_lock)
112{
113 read_unlock(&resource_lock);
114}
115
116static int r_show(struct seq_file *m, void *v)
117{
Muchun Song359745d2022-01-21 22:14:23 -0800118 struct resource *root = pde_data(file_inode(m->file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct resource *r = v, *p;
Linus Torvalds51d7b122016-04-14 12:05:37 -0700120 unsigned long long start, end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 int width = root->end < 0x10000 ? 4 : 8;
122 int depth;
123
124 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
125 if (p->parent == root)
126 break;
Linus Torvalds51d7b122016-04-14 12:05:37 -0700127
128 if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
129 start = r->start;
130 end = r->end;
131 } else {
132 start = end = 0;
133 }
134
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -0700135 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 depth * 2, "",
Linus Torvalds51d7b122016-04-14 12:05:37 -0700137 width, start,
138 width, end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 r->name ? r->name : "<BAD>");
140 return 0;
141}
142
Helge Deller15ad7cd2006-12-06 20:40:36 -0800143static const struct seq_operations resource_op = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .start = r_start,
145 .next = r_next,
146 .stop = r_stop,
147 .show = r_show,
148};
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int __init ioresources_init(void)
151{
Christoph Hellwig4e292a92018-04-11 11:52:39 +0200152 proc_create_seq_data("ioports", 0, NULL, &resource_op,
153 &ioport_resource);
154 proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
156}
157__initcall(ioresources_init);
158
159#endif /* CONFIG_PROC_FS */
160
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -0700161static void free_resource(struct resource *res)
162{
163 if (!res)
164 return;
165
166 if (!PageSlab(virt_to_head_page(res))) {
167 spin_lock(&bootmem_resource_lock);
168 res->sibling = bootmem_resource_free;
169 bootmem_resource_free = res;
170 spin_unlock(&bootmem_resource_lock);
171 } else {
172 kfree(res);
173 }
174}
175
176static struct resource *alloc_resource(gfp_t flags)
177{
178 struct resource *res = NULL;
179
180 spin_lock(&bootmem_resource_lock);
181 if (bootmem_resource_free) {
182 res = bootmem_resource_free;
183 bootmem_resource_free = res->sibling;
184 }
185 spin_unlock(&bootmem_resource_lock);
186
187 if (res)
188 memset(res, 0, sizeof(struct resource));
189 else
190 res = kzalloc(sizeof(struct resource), flags);
191
192 return res;
193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/* Return the conflict entry if you can't request it */
196static struct resource * __request_resource(struct resource *root, struct resource *new)
197{
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700198 resource_size_t start = new->start;
199 resource_size_t end = new->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct resource *tmp, **p;
201
202 if (end < start)
203 return root;
204 if (start < root->start)
205 return root;
206 if (end > root->end)
207 return root;
208 p = &root->child;
209 for (;;) {
210 tmp = *p;
211 if (!tmp || tmp->start > end) {
212 new->sibling = tmp;
213 *p = new;
214 new->parent = root;
215 return NULL;
216 }
217 p = &tmp->sibling;
218 if (tmp->end < start)
219 continue;
220 return tmp;
221 }
222}
223
Toshi Kaniff3cc952016-03-09 12:47:04 -0700224static int __release_resource(struct resource *old, bool release_child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Toshi Kaniff3cc952016-03-09 12:47:04 -0700226 struct resource *tmp, **p, *chd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 p = &old->parent->child;
229 for (;;) {
230 tmp = *p;
231 if (!tmp)
232 break;
233 if (tmp == old) {
Toshi Kaniff3cc952016-03-09 12:47:04 -0700234 if (release_child || !(tmp->child)) {
235 *p = tmp->sibling;
236 } else {
237 for (chd = tmp->child;; chd = chd->sibling) {
238 chd->parent = tmp->parent;
239 if (!(chd->sibling))
240 break;
241 }
242 *p = tmp->child;
243 chd->sibling = tmp->sibling;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 old->parent = NULL;
246 return 0;
247 }
248 p = &tmp->sibling;
249 }
250 return -EINVAL;
251}
252
Yinghai Lu5eeec0e2009-12-22 15:02:22 -0800253static void __release_child_resources(struct resource *r)
254{
255 struct resource *tmp, *p;
256 resource_size_t size;
257
258 p = r->child;
259 r->child = NULL;
260 while (p) {
261 tmp = p;
262 p = p->sibling;
263
264 tmp->parent = NULL;
265 tmp->sibling = NULL;
266 __release_child_resources(tmp);
267
268 printk(KERN_DEBUG "release child resource %pR\n", tmp);
269 /* need to restore size, and keep flags */
270 size = resource_size(tmp);
271 tmp->start = 0;
272 tmp->end = size - 1;
273 }
274}
275
276void release_child_resources(struct resource *r)
277{
278 write_lock(&resource_lock);
279 __release_child_resources(r);
280 write_unlock(&resource_lock);
281}
282
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -0700283/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700284 * request_resource_conflict - request and reserve an I/O or memory resource
285 * @root: root resource descriptor
286 * @new: resource descriptor desired by caller
287 *
288 * Returns 0 for success, conflict resource on error.
289 */
290struct resource *request_resource_conflict(struct resource *root, struct resource *new)
291{
292 struct resource *conflict;
293
294 write_lock(&resource_lock);
295 conflict = __request_resource(root, new);
296 write_unlock(&resource_lock);
297 return conflict;
298}
299
300/**
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -0700301 * request_resource - request and reserve an I/O or memory resource
302 * @root: root resource descriptor
303 * @new: resource descriptor desired by caller
304 *
305 * Returns 0 for success, negative error code on error.
306 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307int request_resource(struct resource *root, struct resource *new)
308{
309 struct resource *conflict;
310
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700311 conflict = request_resource_conflict(root, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return conflict ? -EBUSY : 0;
313}
314
315EXPORT_SYMBOL(request_resource);
316
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -0700317/**
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -0700318 * release_resource - release a previously reserved resource
319 * @old: resource pointer
320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321int release_resource(struct resource *old)
322{
323 int retval;
324
325 write_lock(&resource_lock);
Toshi Kaniff3cc952016-03-09 12:47:04 -0700326 retval = __release_resource(old, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 write_unlock(&resource_lock);
328 return retval;
329}
330
331EXPORT_SYMBOL(release_resource);
332
Borislav Petkovf26621e2018-11-05 10:33:07 +0100333/**
Mauro Carvalho Chehab3be8da52020-12-15 20:46:16 -0800334 * find_next_iomem_res - Finds the lowest iomem resource that covers part of
335 * [@start..@end].
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500336 *
Borislav Petkovf26621e2018-11-05 10:33:07 +0100337 * If a resource is found, returns 0 and @*res is overwritten with the part
338 * of the resource that's within [@start..@end]; if none is found, returns
Nadav Amit49f17c22019-07-18 15:57:31 -0700339 * -ENODEV. Returns -EINVAL for invalid parameters.
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500340 *
Borislav Petkovf26621e2018-11-05 10:33:07 +0100341 * @start: start address of the resource searched for
342 * @end: end address of same resource
343 * @flags: flags which the resource must have
344 * @desc: descriptor the resource must have
Borislav Petkovf26621e2018-11-05 10:33:07 +0100345 * @res: return ptr, if resource found
Mauro Carvalho Chehab3be8da52020-12-15 20:46:16 -0800346 *
347 * The caller must specify @start, @end, @flags, and @desc
348 * (which may be IORES_DESC_NONE).
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700349 */
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500350static int find_next_iomem_res(resource_size_t start, resource_size_t end,
351 unsigned long flags, unsigned long desc,
David Hildenbrand97523a42021-05-06 18:05:20 -0700352 struct resource *res)
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700353{
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700354 struct resource *p;
355
Borislav Petkovb69c2e22018-10-09 16:11:21 +0200356 if (!res)
357 return -EINVAL;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700358
Borislav Petkovb69c2e22018-10-09 16:11:21 +0200359 if (start >= end)
360 return -EINVAL;
Vivek Goyal800df622014-08-29 15:18:29 -0700361
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700362 read_lock(&resource_lock);
Vivek Goyal8c86e702014-08-08 14:25:50 -0700363
David Hildenbrand97523a42021-05-06 18:05:20 -0700364 for (p = iomem_resource.child; p; p = next_resource(p)) {
Nadav Amit75639872019-07-18 15:57:34 -0700365 /* If we passed the resource we are looking for, stop */
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700366 if (p->start > end) {
367 p = NULL;
368 break;
369 }
Nadav Amit75639872019-07-18 15:57:34 -0700370
371 /* Skip until we find a range that matches what we look for */
372 if (p->end < start)
373 continue;
374
Nadav Amit75639872019-07-18 15:57:34 -0700375 if ((p->flags & flags) != flags)
376 continue;
377 if ((desc != IORES_DESC_NONE) && (desc != p->desc))
378 continue;
379
380 /* Found a match, break */
381 break;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700382 }
Vivek Goyal8c86e702014-08-08 14:25:50 -0700383
Nadav Amit49f17c22019-07-18 15:57:31 -0700384 if (p) {
385 /* copy data */
Dan Williams73fb9522020-10-13 16:49:18 -0700386 *res = (struct resource) {
387 .start = max(start, p->start),
388 .end = min(end, p->end),
389 .flags = p->flags,
390 .desc = p->desc,
391 .parent = p->parent,
392 };
Nadav Amit49f17c22019-07-18 15:57:31 -0700393 }
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500394
Nadav Amit49f17c22019-07-18 15:57:31 -0700395 read_unlock(&resource_lock);
396 return p ? 0 : -ENODEV;
KAMEZAWA Hiroyuki2842f112006-06-27 02:53:36 -0700397}
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700398
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500399static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
400 unsigned long flags, unsigned long desc,
David Hildenbrand97523a42021-05-06 18:05:20 -0700401 void *arg,
Tom Lendacky1d2e7332017-10-20 09:30:51 -0500402 int (*func)(struct resource *, void *))
Tom Lendacky4ac2aed2017-10-20 09:30:50 -0500403{
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500404 struct resource res;
Dave Hansen5cd401a2019-02-25 10:57:30 -0800405 int ret = -EINVAL;
Tom Lendacky4ac2aed2017-10-20 09:30:50 -0500406
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500407 while (start < end &&
David Hildenbrand97523a42021-05-06 18:05:20 -0700408 !find_next_iomem_res(start, end, flags, desc, &res)) {
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500409 ret = (*func)(&res, arg);
Tom Lendacky4ac2aed2017-10-20 09:30:50 -0500410 if (ret)
411 break;
412
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500413 start = res.end + 1;
Tom Lendacky4ac2aed2017-10-20 09:30:50 -0500414 }
415
416 return ret;
417}
418
Borislav Petkovb69c2e22018-10-09 16:11:21 +0200419/**
Mauro Carvalho Chehab3be8da52020-12-15 20:46:16 -0800420 * walk_iomem_res_desc - Walks through iomem resources and calls func()
421 * with matching resource ranges.
422 * *
Toshi Kani3f336472016-01-26 21:57:29 +0100423 * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
424 * @flags: I/O resource flags
425 * @start: start addr
426 * @end: end addr
Randy Dunlapf75d6512018-11-04 18:40:14 -0800427 * @arg: function argument for the callback @func
428 * @func: callback function that is called for each qualifying resource area
Toshi Kani3f336472016-01-26 21:57:29 +0100429 *
Mauro Carvalho Chehab3be8da52020-12-15 20:46:16 -0800430 * All the memory ranges which overlap start,end and also match flags and
431 * desc are valid candidates.
432 *
Toshi Kani3f336472016-01-26 21:57:29 +0100433 * NOTE: For a new descriptor search, define a new IORES_DESC in
434 * <linux/ioport.h> and set it in 'desc' of a target resource entry.
435 */
436int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
Tom Lendacky1d2e7332017-10-20 09:30:51 -0500437 u64 end, void *arg, int (*func)(struct resource *, void *))
Toshi Kani3f336472016-01-26 21:57:29 +0100438{
David Hildenbrand97523a42021-05-06 18:05:20 -0700439 return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
Toshi Kani3f336472016-01-26 21:57:29 +0100440}
Dan Williamsd76401a2018-06-02 11:43:39 -0700441EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
Toshi Kani3f336472016-01-26 21:57:29 +0100442
443/*
Toshi Kanibd7e6cb2016-01-26 21:57:26 +0100444 * This function calls the @func callback against all memory ranges of type
445 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
446 * Now, this function is only for System RAM, it deals with full ranges and
447 * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
448 * ranges.
Vivek Goyal8c86e702014-08-08 14:25:50 -0700449 */
450int walk_system_ram_res(u64 start, u64 end, void *arg,
Borislav Petkovb69c2e22018-10-09 16:11:21 +0200451 int (*func)(struct resource *, void *))
Vivek Goyal8c86e702014-08-08 14:25:50 -0700452{
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500453 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
Vivek Goyal8c86e702014-08-08 14:25:50 -0700454
David Hildenbrand97523a42021-05-06 18:05:20 -0700455 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
456 func);
Vivek Goyal8c86e702014-08-08 14:25:50 -0700457}
458
Tom Lendacky0e4c12b2017-10-20 09:30:52 -0500459/*
460 * This function calls the @func callback against all memory ranges, which
461 * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
462 */
463int walk_mem_res(u64 start, u64 end, void *arg,
464 int (*func)(struct resource *, void *))
465{
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500466 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Tom Lendacky0e4c12b2017-10-20 09:30:52 -0500467
David Hildenbrand97523a42021-05-06 18:05:20 -0700468 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
469 func);
Tom Lendacky0e4c12b2017-10-20 09:30:52 -0500470}
471
Vivek Goyal8c86e702014-08-08 14:25:50 -0700472/*
Toshi Kanibd7e6cb2016-01-26 21:57:26 +0100473 * This function calls the @func callback against all memory ranges of type
474 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
475 * It is to be used only for System RAM.
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700476 */
477int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
Borislav Petkovb69c2e22018-10-09 16:11:21 +0200478 void *arg, int (*func)(unsigned long, unsigned long, void *))
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700479{
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500480 resource_size_t start, end;
481 unsigned long flags;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700482 struct resource res;
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800483 unsigned long pfn, end_pfn;
Dave Hansen5cd401a2019-02-25 10:57:30 -0800484 int ret = -EINVAL;
KAMEZAWA Hiroyuki908eedc2009-09-22 16:45:46 -0700485
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500486 start = (u64) start_pfn << PAGE_SHIFT;
487 end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
488 flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
489 while (start < end &&
David Hildenbrand97523a42021-05-06 18:05:20 -0700490 !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
David Hildenbrand00ff9a92019-09-23 15:35:55 -0700491 pfn = PFN_UP(res.start);
492 end_pfn = PFN_DOWN(res.end + 1);
Wu Fengguang37b99dd2010-03-01 21:55:51 +0800493 if (end_pfn > pfn)
H. Peter Anvinf4149662010-03-02 11:21:09 -0800494 ret = (*func)(pfn, end_pfn - pfn, arg);
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700495 if (ret)
496 break;
Bjorn Helgaas010a93b2018-09-27 09:22:09 -0500497 start = res.end + 1;
KAMEZAWA Hiroyuki75884fb2007-10-16 01:26:10 -0700498 }
499 return ret;
500}
501
Wu Fengguang61ef2482010-01-22 16:16:19 +0800502static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
503{
504 return 1;
505}
Tom Lendacky4ac2aed2017-10-20 09:30:50 -0500506
Wu Fengguang61ef2482010-01-22 16:16:19 +0800507/*
508 * This generic page_is_ram() returns true if specified address is
Toshi Kanibd7e6cb2016-01-26 21:57:26 +0100509 * registered as System RAM in iomem_resource list.
Wu Fengguang61ef2482010-01-22 16:16:19 +0800510 */
Andrew Mortone5273002010-01-26 16:31:19 -0800511int __weak page_is_ram(unsigned long pfn)
Wu Fengguang61ef2482010-01-22 16:16:19 +0800512{
513 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
514}
Chen Gongc5a13032013-06-06 15:20:51 -0700515EXPORT_SYMBOL_GPL(page_is_ram);
Wu Fengguang61ef2482010-01-22 16:16:19 +0800516
Alistair Poppled486ccb2021-05-06 18:05:24 -0700517static int __region_intersects(resource_size_t start, size_t size,
518 unsigned long flags, unsigned long desc)
519{
520 struct resource res;
521 int type = 0; int other = 0;
522 struct resource *p;
523
524 res.start = start;
525 res.end = start + size - 1;
526
527 for (p = iomem_resource.child; p ; p = p->sibling) {
528 bool is_type = (((p->flags & flags) == flags) &&
529 ((desc == IORES_DESC_NONE) ||
530 (desc == p->desc)));
531
532 if (resource_overlaps(p, &res))
533 is_type ? type++ : other++;
534 }
535
536 if (type == 0)
537 return REGION_DISJOINT;
538
539 if (other == 0)
540 return REGION_INTERSECTS;
541
542 return REGION_MIXED;
543}
544
Dan Williams124fe202015-08-10 23:07:05 -0400545/**
546 * region_intersects() - determine intersection of region with known resources
547 * @start: region start address
548 * @size: size of region
Toshi Kani1c29f252016-01-26 21:57:28 +0100549 * @flags: flags of resource (in iomem_resource)
550 * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
Mike Travis67cf13c2014-10-13 15:54:03 -0700551 *
Dan Williams124fe202015-08-10 23:07:05 -0400552 * Check if the specified region partially overlaps or fully eclipses a
Toshi Kani1c29f252016-01-26 21:57:28 +0100553 * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
554 * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
555 * return REGION_MIXED if the region overlaps @flags/@desc and another
556 * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
557 * and no other defined resource. Note that REGION_INTERSECTS is also
558 * returned in the case when the specified region overlaps RAM and undefined
559 * memory holes.
Dan Williams124fe202015-08-10 23:07:05 -0400560 *
561 * region_intersect() is used by memory remapping functions to ensure
562 * the user is not remapping RAM and is a vast speed up over walking
563 * through the resource table page by page.
Mike Travis67cf13c2014-10-13 15:54:03 -0700564 */
Toshi Kani1c29f252016-01-26 21:57:28 +0100565int region_intersects(resource_size_t start, size_t size, unsigned long flags,
566 unsigned long desc)
Mike Travis67cf13c2014-10-13 15:54:03 -0700567{
Alistair Poppled486ccb2021-05-06 18:05:24 -0700568 int ret;
Wei Yangf6c60102019-03-05 16:34:32 +0800569
Mike Travis67cf13c2014-10-13 15:54:03 -0700570 read_lock(&resource_lock);
Alistair Poppled486ccb2021-05-06 18:05:24 -0700571 ret = __region_intersects(start, size, flags, desc);
Mike Travis67cf13c2014-10-13 15:54:03 -0700572 read_unlock(&resource_lock);
Dan Williams124fe202015-08-10 23:07:05 -0400573
Alistair Poppled486ccb2021-05-06 18:05:24 -0700574 return ret;
Mike Travis67cf13c2014-10-13 15:54:03 -0700575}
Toshi Kani1c29f252016-01-26 21:57:28 +0100576EXPORT_SYMBOL_GPL(region_intersects);
Mike Travis67cf13c2014-10-13 15:54:03 -0700577
Bjorn Helgaasfcb11912010-12-16 10:38:46 -0700578void __weak arch_remove_reservations(struct resource *avail)
579{
580}
581
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600582static resource_size_t simple_align_resource(void *data,
583 const struct resource *avail,
584 resource_size_t size,
585 resource_size_t align)
586{
587 return avail->start;
588}
589
Bjorn Helgaas5d6b1fa2010-10-26 15:41:18 -0600590static void resource_clip(struct resource *res, resource_size_t min,
591 resource_size_t max)
592{
593 if (res->start < min)
594 res->start = min;
595 if (res->end > max)
596 res->end = max;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
Ram Pai23c570a2011-07-05 23:44:30 -0700600 * Find empty slot in the resource tree with the given range and
601 * alignment constraints
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Ram Pai23c570a2011-07-05 23:44:30 -0700603static int __find_resource(struct resource *root, struct resource *old,
604 struct resource *new,
605 resource_size_t size,
606 struct resource_constraint *constraint)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct resource *this = root->child;
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600609 struct resource tmp = *new, avail, alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100611 tmp.start = root->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
Bjorn Helgaasc0f5ac52010-12-16 10:38:41 -0700613 * Skip past an allocated resource that starts at 0, since the assignment
614 * of this->start - 1 to tmp->end below would cause an underflow.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 */
Ram Pai23c570a2011-07-05 23:44:30 -0700616 if (this && this->start == root->start) {
617 tmp.start = (this == old) ? old->start : this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 this = this->sibling;
619 }
Bjorn Helgaasc0f5ac52010-12-16 10:38:41 -0700620 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (this)
Ram Pai23c570a2011-07-05 23:44:30 -0700622 tmp.end = (this == old) ? this->end : this->start - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else
Dominik Brodowski0e2c8b82009-12-20 10:50:02 +0100624 tmp.end = root->end;
Bjorn Helgaas5d6b1fa2010-10-26 15:41:18 -0600625
Ram Pai47ea91b2011-09-22 15:48:58 +0800626 if (tmp.end < tmp.start)
627 goto next;
628
Ram Pai23c570a2011-07-05 23:44:30 -0700629 resource_clip(&tmp, constraint->min, constraint->max);
Bjorn Helgaasfcb11912010-12-16 10:38:46 -0700630 arch_remove_reservations(&tmp);
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600631
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600632 /* Check for overflow after ALIGN() */
Ram Pai23c570a2011-07-05 23:44:30 -0700633 avail.start = ALIGN(tmp.start, constraint->align);
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600634 avail.end = tmp.end;
Bjorn Helgaas5edb93b2014-02-04 19:32:28 -0800635 avail.flags = new->flags & ~IORESOURCE_UNSET;
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600636 if (avail.start >= tmp.start) {
Bjorn Helgaas5edb93b2014-02-04 19:32:28 -0800637 alloc.flags = avail.flags;
Ram Pai23c570a2011-07-05 23:44:30 -0700638 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
639 size, constraint->align);
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600640 alloc.end = alloc.start + size - 1;
Takashi Iwai60bb83b2018-04-13 15:35:13 -0700641 if (alloc.start <= alloc.end &&
642 resource_contains(&avail, &alloc)) {
Bjorn Helgaasa1862e32010-10-26 15:41:28 -0600643 new->start = alloc.start;
644 new->end = alloc.end;
645 return 0;
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Ram Pai47ea91b2011-09-22 15:48:58 +0800648
649next: if (!this || this->end == root->end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
Ram Pai47ea91b2011-09-22 15:48:58 +0800651
Ram Pai23c570a2011-07-05 23:44:30 -0700652 if (this != old)
653 tmp.start = this->end + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 this = this->sibling;
655 }
656 return -EBUSY;
657}
658
Ram Pai23c570a2011-07-05 23:44:30 -0700659/*
660 * Find empty slot in the resource tree given range and alignment.
661 */
662static int find_resource(struct resource *root, struct resource *new,
663 resource_size_t size,
664 struct resource_constraint *constraint)
665{
666 return __find_resource(root, NULL, new, size, constraint);
667}
668
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -0700669/**
Ram Pai23c570a2011-07-05 23:44:30 -0700670 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
671 * The resource will be relocated if the new size cannot be reallocated in the
672 * current location.
673 *
674 * @root: root resource descriptor
675 * @old: resource descriptor desired by caller
676 * @newsize: new size of the resource descriptor
677 * @constraint: the size and alignment constraints to be met.
678 */
Daeseok Youn28ab49f2014-04-03 14:48:36 -0700679static int reallocate_resource(struct resource *root, struct resource *old,
Borislav Petkovb69c2e22018-10-09 16:11:21 +0200680 resource_size_t newsize,
681 struct resource_constraint *constraint)
Ram Pai23c570a2011-07-05 23:44:30 -0700682{
683 int err=0;
684 struct resource new = *old;
685 struct resource *conflict;
686
687 write_lock(&resource_lock);
688
689 if ((err = __find_resource(root, old, &new, newsize, constraint)))
690 goto out;
691
692 if (resource_contains(&new, old)) {
693 old->start = new.start;
694 old->end = new.end;
695 goto out;
696 }
697
698 if (old->child) {
699 err = -EBUSY;
700 goto out;
701 }
702
703 if (resource_contains(old, &new)) {
704 old->start = new.start;
705 old->end = new.end;
706 } else {
Toshi Kaniff3cc952016-03-09 12:47:04 -0700707 __release_resource(old, true);
Ram Pai23c570a2011-07-05 23:44:30 -0700708 *old = new;
709 conflict = __request_resource(root, old);
710 BUG_ON(conflict);
711 }
712out:
713 write_unlock(&resource_lock);
714 return err;
715}
716
717
718/**
719 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
720 * The resource will be reallocated with a new size if it was already allocated
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -0700721 * @root: root resource descriptor
722 * @new: resource descriptor desired by caller
723 * @size: requested resource region size
Wei Yangee5e5682012-05-31 16:26:05 -0700724 * @min: minimum boundary to allocate
725 * @max: maximum boundary to allocate
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -0700726 * @align: alignment requested, in bytes
727 * @alignf: alignment function, optional, called if not NULL
728 * @alignf_data: arbitrary data to pass to the @alignf function
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
730int allocate_resource(struct resource *root, struct resource *new,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700731 resource_size_t size, resource_size_t min,
732 resource_size_t max, resource_size_t align,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100733 resource_size_t (*alignf)(void *,
Dominik Brodowski3b7a17f2010-01-01 17:40:50 +0100734 const struct resource *,
Dominik Brodowskib26b2d42010-01-01 17:40:49 +0100735 resource_size_t,
736 resource_size_t),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 void *alignf_data)
738{
739 int err;
Ram Pai23c570a2011-07-05 23:44:30 -0700740 struct resource_constraint constraint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Bjorn Helgaasa9cea012010-10-26 15:41:13 -0600742 if (!alignf)
743 alignf = simple_align_resource;
744
Ram Pai23c570a2011-07-05 23:44:30 -0700745 constraint.min = min;
746 constraint.max = max;
747 constraint.align = align;
748 constraint.alignf = alignf;
749 constraint.alignf_data = alignf_data;
750
751 if ( new->parent ) {
752 /* resource is already allocated, try reallocating with
753 the new constraints */
754 return reallocate_resource(root, new, size, &constraint);
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 write_lock(&resource_lock);
Ram Pai23c570a2011-07-05 23:44:30 -0700758 err = find_resource(root, new, size, &constraint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (err >= 0 && __request_resource(root, new))
760 err = -EBUSY;
761 write_unlock(&resource_lock);
762 return err;
763}
764
765EXPORT_SYMBOL(allocate_resource);
766
Geert Uytterhoeven1c388912011-05-07 20:53:16 +0200767/**
768 * lookup_resource - find an existing resource by a resource start address
769 * @root: root resource descriptor
770 * @start: resource start address
771 *
772 * Returns a pointer to the resource if found, NULL otherwise
773 */
774struct resource *lookup_resource(struct resource *root, resource_size_t start)
775{
776 struct resource *res;
777
778 read_lock(&resource_lock);
779 for (res = root->child; res; res = res->sibling) {
780 if (res->start == start)
781 break;
782 }
783 read_unlock(&resource_lock);
784
785 return res;
786}
787
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700788/*
789 * Insert a resource into the resource tree. If successful, return NULL,
790 * otherwise return the conflicting resource (compare to __request_resource())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700792static struct resource * __insert_resource(struct resource *parent, struct resource *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 struct resource *first, *next;
795
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700796 for (;; parent = first) {
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700797 first = __request_resource(parent, new);
798 if (!first)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700799 return first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700801 if (first == parent)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700802 return first;
Huang Shijie5de1cb22010-10-27 15:34:52 -0700803 if (WARN_ON(first == new)) /* duplicated insertion */
804 return first;
Matthew Wilcoxd33b6fb2006-06-30 02:31:24 -0700805
806 if ((first->start > new->start) || (first->end < new->end))
807 break;
808 if ((first->start == new->start) && (first->end == new->end))
809 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
811
812 for (next = first; ; next = next->sibling) {
813 /* Partial overlap? Bad, and unfixable */
814 if (next->start < new->start || next->end > new->end)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700815 return next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (!next->sibling)
817 break;
818 if (next->sibling->start > new->end)
819 break;
820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 new->parent = parent;
823 new->sibling = next->sibling;
824 new->child = first;
825
826 next->sibling = NULL;
827 for (next = first; next; next = next->sibling)
828 next->parent = new;
829
830 if (parent->child == first) {
831 parent->child = new;
832 } else {
833 next = parent->child;
834 while (next->sibling != first)
835 next = next->sibling;
836 next->sibling = new;
837 }
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700838 return NULL;
839}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700841/**
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700842 * insert_resource_conflict - Inserts resource in the resource tree
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700843 * @parent: parent of the new resource
844 * @new: new resource to insert
845 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700846 * Returns 0 on success, conflict resource if the resource can't be inserted.
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700847 *
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700848 * This function is equivalent to request_resource_conflict when no conflict
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700849 * happens. If a conflict happens, and the conflicting resources
850 * entirely fit within the range of the new resource, then the new
851 * resource is inserted and the conflicting resources become children of
852 * the new resource.
Toshi Kaniff3cc952016-03-09 12:47:04 -0700853 *
854 * This function is intended for producers of resources, such as FW modules
855 * and bus drivers.
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700856 */
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700857struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700858{
859 struct resource *conflict;
860
861 write_lock(&resource_lock);
862 conflict = __insert_resource(parent, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 write_unlock(&resource_lock);
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700864 return conflict;
865}
866
867/**
868 * insert_resource - Inserts a resource in the resource tree
869 * @parent: parent of the new resource
870 * @new: new resource to insert
871 *
872 * Returns 0 on success, -EBUSY if the resource can't be inserted.
Toshi Kaniff3cc952016-03-09 12:47:04 -0700873 *
874 * This function is intended for producers of resources, such as FW modules
875 * and bus drivers.
Bjorn Helgaas66f12072010-03-11 17:01:09 -0700876 */
877int insert_resource(struct resource *parent, struct resource *new)
878{
879 struct resource *conflict;
880
881 conflict = insert_resource_conflict(parent, new);
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700882 return conflict ? -EBUSY : 0;
883}
Toshi Kani8095d0f2016-03-09 12:47:05 -0700884EXPORT_SYMBOL_GPL(insert_resource);
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700885
886/**
887 * insert_resource_expand_to_fit - Insert a resource into the resource tree
Randy Dunlap6781f4a2008-08-31 20:31:55 -0700888 * @root: root resource descriptor
Linus Torvaldsbef69ea2008-08-29 20:18:31 -0700889 * @new: new resource to insert
890 *
891 * Insert a resource into the resource tree, possibly expanding it in order
892 * to make it encompass any conflicting resources.
893 */
894void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
895{
896 if (new->parent)
897 return;
898
899 write_lock(&resource_lock);
900 for (;;) {
901 struct resource *conflict;
902
903 conflict = __insert_resource(root, new);
904 if (!conflict)
905 break;
906 if (conflict == root)
907 break;
908
909 /* Ok, expand resource to cover the conflict, then try again .. */
910 if (conflict->start < new->start)
911 new->start = conflict->start;
912 if (conflict->end > new->end)
913 new->end = conflict->end;
914
915 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
916 }
917 write_unlock(&resource_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Toshi Kaniff3cc952016-03-09 12:47:04 -0700920/**
921 * remove_resource - Remove a resource in the resource tree
922 * @old: resource to remove
923 *
924 * Returns 0 on success, -EINVAL if the resource is not valid.
925 *
926 * This function removes a resource previously inserted by insert_resource()
927 * or insert_resource_conflict(), and moves the children (if any) up to
928 * where they were before. insert_resource() and insert_resource_conflict()
929 * insert a new resource, and move any conflicting resources down to the
930 * children of the new resource.
931 *
932 * insert_resource(), insert_resource_conflict() and remove_resource() are
933 * intended for producers of resources, such as FW modules and bus drivers.
934 */
935int remove_resource(struct resource *old)
936{
937 int retval;
938
939 write_lock(&resource_lock);
940 retval = __release_resource(old, false);
941 write_unlock(&resource_lock);
942 return retval;
943}
Toshi Kani8095d0f2016-03-09 12:47:05 -0700944EXPORT_SYMBOL_GPL(remove_resource);
Toshi Kaniff3cc952016-03-09 12:47:04 -0700945
Toshi Kaniae8e3a92013-04-29 15:08:17 -0700946static int __adjust_resource(struct resource *res, resource_size_t start,
947 resource_size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 struct resource *tmp, *parent = res->parent;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -0700950 resource_size_t end = start + size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int result = -EBUSY;
952
Yinghai Lu82ec90e2012-05-17 18:51:11 -0700953 if (!parent)
954 goto skip;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if ((start < parent->start) || (end > parent->end))
957 goto out;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (res->sibling && (res->sibling->start <= end))
960 goto out;
961
962 tmp = parent->child;
963 if (tmp != res) {
964 while (tmp->sibling != res)
965 tmp = tmp->sibling;
966 if (start <= tmp->end)
967 goto out;
968 }
969
Yinghai Lu82ec90e2012-05-17 18:51:11 -0700970skip:
971 for (tmp = res->child; tmp; tmp = tmp->sibling)
972 if ((tmp->start < start) || (tmp->end > end))
973 goto out;
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 res->start = start;
976 res->end = end;
977 result = 0;
978
979 out:
Toshi Kaniae8e3a92013-04-29 15:08:17 -0700980 return result;
981}
982
983/**
984 * adjust_resource - modify a resource's start and size
985 * @res: resource to modify
986 * @start: new start value
987 * @size: new size
988 *
989 * Given an existing resource, change its start and size to match the
990 * arguments. Returns 0 on success, -EBUSY if it can't fit.
991 * Existing children of the resource are assumed to be immutable.
992 */
993int adjust_resource(struct resource *res, resource_size_t start,
Borislav Petkovb69c2e22018-10-09 16:11:21 +0200994 resource_size_t size)
Toshi Kaniae8e3a92013-04-29 15:08:17 -0700995{
996 int result;
997
998 write_lock(&resource_lock);
999 result = __adjust_resource(res, start, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 write_unlock(&resource_lock);
1001 return result;
1002}
Cong Wang24105742012-02-03 21:42:39 +08001003EXPORT_SYMBOL(adjust_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Borislav Petkovb69c2e22018-10-09 16:11:21 +02001005static void __init
1006__reserve_region_with_split(struct resource *root, resource_size_t start,
1007 resource_size_t end, const char *name)
Yinghai Lu268364a2008-09-04 21:02:44 +02001008{
1009 struct resource *parent = root;
1010 struct resource *conflict;
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -07001011 struct resource *res = alloc_resource(GFP_ATOMIC);
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001012 struct resource *next_res = NULL;
Bjorn Helgaasf37e2332017-12-01 14:07:18 -06001013 int type = resource_type(root);
Yinghai Lu268364a2008-09-04 21:02:44 +02001014
1015 if (!res)
1016 return;
1017
1018 res->name = name;
1019 res->start = start;
1020 res->end = end;
Bjorn Helgaasf37e2332017-12-01 14:07:18 -06001021 res->flags = type | IORESOURCE_BUSY;
Toshi Kani43ee4932016-01-26 21:57:19 +01001022 res->desc = IORES_DESC_NONE;
Yinghai Lu268364a2008-09-04 21:02:44 +02001023
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001024 while (1) {
Yinghai Lu268364a2008-09-04 21:02:44 +02001025
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001026 conflict = __request_resource(parent, res);
1027 if (!conflict) {
1028 if (!next_res)
1029 break;
1030 res = next_res;
1031 next_res = NULL;
1032 continue;
1033 }
Yinghai Lu268364a2008-09-04 21:02:44 +02001034
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001035 /* conflict covered whole area */
1036 if (conflict->start <= res->start &&
1037 conflict->end >= res->end) {
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -07001038 free_resource(res);
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001039 WARN_ON(next_res);
1040 break;
1041 }
Yinghai Lu268364a2008-09-04 21:02:44 +02001042
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001043 /* failed, split and try again */
1044 if (conflict->start > res->start) {
1045 end = res->end;
1046 res->end = conflict->start - 1;
1047 if (conflict->end < end) {
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -07001048 next_res = alloc_resource(GFP_ATOMIC);
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001049 if (!next_res) {
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -07001050 free_resource(res);
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001051 break;
1052 }
1053 next_res->name = name;
1054 next_res->start = conflict->end + 1;
1055 next_res->end = end;
Bjorn Helgaasf37e2332017-12-01 14:07:18 -06001056 next_res->flags = type | IORESOURCE_BUSY;
Toshi Kani43ee4932016-01-26 21:57:19 +01001057 next_res->desc = IORES_DESC_NONE;
T Makphaibulchoke4965f562012-10-04 17:16:55 -07001058 }
1059 } else {
1060 res->start = conflict->end + 1;
1061 }
1062 }
1063
Yinghai Lu268364a2008-09-04 21:02:44 +02001064}
1065
Borislav Petkovb69c2e22018-10-09 16:11:21 +02001066void __init
1067reserve_region_with_split(struct resource *root, resource_size_t start,
1068 resource_size_t end, const char *name)
Yinghai Lu268364a2008-09-04 21:02:44 +02001069{
Octavian Purdila65fed8f2012-07-30 14:42:58 -07001070 int abort = 0;
1071
Yinghai Lu268364a2008-09-04 21:02:44 +02001072 write_lock(&resource_lock);
Octavian Purdila65fed8f2012-07-30 14:42:58 -07001073 if (root->start > start || root->end < end) {
1074 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1075 (unsigned long long)start, (unsigned long long)end,
1076 root);
1077 if (start > root->end || end < root->start)
1078 abort = 1;
1079 else {
1080 if (end > root->end)
1081 end = root->end;
1082 if (start < root->start)
1083 start = root->start;
1084 pr_err("fixing request to [0x%llx-0x%llx]\n",
1085 (unsigned long long)start,
1086 (unsigned long long)end);
1087 }
1088 dump_stack();
1089 }
1090 if (!abort)
1091 __reserve_region_with_split(root, start, end, name);
Yinghai Lu268364a2008-09-04 21:02:44 +02001092 write_unlock(&resource_lock);
1093}
1094
Ivan Kokshaysky88452562008-03-30 19:50:14 +04001095/**
1096 * resource_alignment - calculate resource's alignment
1097 * @res: resource pointer
1098 *
1099 * Returns alignment on success, 0 (invalid alignment) on failure.
1100 */
1101resource_size_t resource_alignment(struct resource *res)
1102{
1103 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1104 case IORESOURCE_SIZEALIGN:
Magnus Damm1a4e5642008-07-29 22:32:57 -07001105 return resource_size(res);
Ivan Kokshaysky88452562008-03-30 19:50:14 +04001106 case IORESOURCE_STARTALIGN:
1107 return res->start;
1108 default:
1109 return 0;
1110 }
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
1114 * This is compatibility stuff for IO resources.
1115 *
1116 * Note how this, unlike the above, knows about
1117 * the IO flag meanings (busy etc).
1118 *
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -07001119 * request_region creates a new busy region.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 *
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -07001121 * release_region releases a matching busy region.
1122 */
1123
Alan Cox8b6d0432010-03-29 19:38:00 +02001124static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1125
Daniel Vetter71a1d8e2020-11-27 17:41:24 +01001126static struct inode *iomem_inode;
1127
1128#ifdef CONFIG_IO_STRICT_DEVMEM
1129static void revoke_iomem(struct resource *res)
1130{
1131 /* pairs with smp_store_release() in iomem_init_inode() */
1132 struct inode *inode = smp_load_acquire(&iomem_inode);
1133
1134 /*
1135 * Check that the initialization has completed. Losing the race
1136 * is ok because it means drivers are claiming resources before
1137 * the fs_initcall level of init and prevent iomem_get_mapping users
1138 * from establishing mappings.
1139 */
1140 if (!inode)
1141 return;
1142
1143 /*
1144 * The expectation is that the driver has successfully marked
1145 * the resource busy by this point, so devmem_is_allowed()
1146 * should start returning false, however for performance this
1147 * does not iterate the entire resource range.
1148 */
1149 if (devmem_is_allowed(PHYS_PFN(res->start)) &&
1150 devmem_is_allowed(PHYS_PFN(res->end))) {
1151 /*
1152 * *cringe* iomem=relaxed says "go ahead, what's the
1153 * worst that can happen?"
1154 */
1155 return;
1156 }
1157
1158 unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
1159}
1160#else
1161static void revoke_iomem(struct resource *res) {}
1162#endif
1163
1164struct address_space *iomem_get_mapping(void)
1165{
1166 /*
1167 * This function is only called from file open paths, hence guaranteed
1168 * that fs_initcalls have completed and no need to check for NULL. But
1169 * since revoke_iomem can be called before the initcall we still need
1170 * the barrier to appease checkers.
1171 */
1172 return smp_load_acquire(&iomem_inode)->i_mapping;
1173}
1174
Alistair Popple63cdafe2021-05-06 18:05:27 -07001175static int __request_region_locked(struct resource *res, struct resource *parent,
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -07001176 resource_size_t start, resource_size_t n,
Arjan van de Vene8de1482008-10-22 19:55:31 -07001177 const char *name, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Alan Cox8b6d0432010-03-29 19:38:00 +02001179 DECLARE_WAITQUEUE(wait, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Bjorn Helgaasc26ec882008-10-15 22:05:14 -07001181 res->name = name;
1182 res->start = start;
1183 res->end = start + n - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Bjorn Helgaasc26ec882008-10-15 22:05:14 -07001185 for (;;) {
1186 struct resource *conflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Toshi Kani4e0d8f7e2016-03-09 12:47:03 -07001188 res->flags = resource_type(parent) | resource_ext_type(parent);
1189 res->flags |= IORESOURCE_BUSY | flags;
1190 res->desc = parent->desc;
1191
Bjorn Helgaasc26ec882008-10-15 22:05:14 -07001192 conflict = __request_resource(parent, res);
1193 if (!conflict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 break;
Dave Hansenb926b7f2019-02-25 10:57:33 -08001195 /*
1196 * mm/hmm.c reserves physical addresses which then
1197 * become unavailable to other users. Conflicts are
1198 * not expected. Warn to aid debugging if encountered.
1199 */
1200 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1201 pr_warn("Unaddressable device %s %pR conflicts with %pR",
1202 conflict->name, conflict, res);
1203 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -07001204 if (conflict != parent) {
Simon Guinot59ceeaa2015-09-10 00:15:18 +02001205 if (!(conflict->flags & IORESOURCE_BUSY)) {
1206 parent = conflict;
Bjorn Helgaasc26ec882008-10-15 22:05:14 -07001207 continue;
Simon Guinot59ceeaa2015-09-10 00:15:18 +02001208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
Alan Cox8b6d0432010-03-29 19:38:00 +02001210 if (conflict->flags & flags & IORESOURCE_MUXED) {
1211 add_wait_queue(&muxed_resource_wait, &wait);
1212 write_unlock(&resource_lock);
1213 set_current_state(TASK_UNINTERRUPTIBLE);
1214 schedule();
1215 remove_wait_queue(&muxed_resource_wait, &wait);
1216 write_lock(&resource_lock);
1217 continue;
1218 }
Bjorn Helgaasc26ec882008-10-15 22:05:14 -07001219 /* Uhhuh, that didn't work out.. */
Alistair Popple63cdafe2021-05-06 18:05:27 -07001220 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Alistair Popple63cdafe2021-05-06 18:05:27 -07001222
1223 return 0;
1224}
1225
1226/**
1227 * __request_region - create a new busy resource region
1228 * @parent: parent resource descriptor
1229 * @start: resource start address
1230 * @n: resource region size
1231 * @name: reserving caller's ID string
1232 * @flags: IO resource flags
1233 */
1234struct resource *__request_region(struct resource *parent,
1235 resource_size_t start, resource_size_t n,
1236 const char *name, int flags)
1237{
1238 struct resource *res = alloc_resource(GFP_KERNEL);
1239 int ret;
1240
1241 if (!res)
1242 return NULL;
1243
1244 write_lock(&resource_lock);
1245 ret = __request_region_locked(res, parent, start, n, name, flags);
Bjorn Helgaasc26ec882008-10-15 22:05:14 -07001246 write_unlock(&resource_lock);
Dan Williams3234ac62020-05-21 14:06:17 -07001247
Alistair Popple63cdafe2021-05-06 18:05:27 -07001248 if (ret) {
1249 free_resource(res);
1250 return NULL;
1251 }
1252
1253 if (parent == &iomem_resource)
Daniel Vetter71a1d8e2020-11-27 17:41:24 +01001254 revoke_iomem(res);
Dan Williams3234ac62020-05-21 14:06:17 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return res;
1257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258EXPORT_SYMBOL(__request_region);
1259
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -07001260/**
Randy Dunlape1ca66d1b2006-10-03 01:13:51 -07001261 * __release_region - release a previously reserved resource region
1262 * @parent: parent resource descriptor
1263 * @start: resource start address
1264 * @n: resource region size
1265 *
1266 * The described resource region must match a currently busy region.
1267 */
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -07001268void __release_region(struct resource *parent, resource_size_t start,
Borislav Petkovb69c2e22018-10-09 16:11:21 +02001269 resource_size_t n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct resource **p;
Greg Kroah-Hartmand75fc8b2006-06-12 16:09:23 -07001272 resource_size_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 p = &parent->child;
1275 end = start + n - 1;
1276
1277 write_lock(&resource_lock);
1278
1279 for (;;) {
1280 struct resource *res = *p;
1281
1282 if (!res)
1283 break;
1284 if (res->start <= start && res->end >= end) {
1285 if (!(res->flags & IORESOURCE_BUSY)) {
1286 p = &res->child;
1287 continue;
1288 }
1289 if (res->start != start || res->end != end)
1290 break;
1291 *p = res->sibling;
1292 write_unlock(&resource_lock);
Alan Cox8b6d0432010-03-29 19:38:00 +02001293 if (res->flags & IORESOURCE_MUXED)
1294 wake_up(&muxed_resource_wait);
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -07001295 free_resource(res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return;
1297 }
1298 p = &res->sibling;
1299 }
1300
1301 write_unlock(&resource_lock);
1302
Greg Kroah-Hartman685143ac2006-06-12 15:18:31 -07001303 printk(KERN_WARNING "Trying to free nonexistent resource "
1304 "<%016llx-%016llx>\n", (unsigned long long)start,
1305 (unsigned long long)end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307EXPORT_SYMBOL(__release_region);
1308
Toshi Kani825f7872013-04-29 15:08:19 -07001309#ifdef CONFIG_MEMORY_HOTREMOVE
1310/**
1311 * release_mem_region_adjustable - release a previously reserved memory region
Toshi Kani825f7872013-04-29 15:08:19 -07001312 * @start: resource start address
1313 * @size: resource region size
1314 *
1315 * This interface is intended for memory hot-delete. The requested region
1316 * is released from a currently busy memory resource. The requested region
1317 * must either match exactly or fit into a single busy resource entry. In
1318 * the latter case, the remaining resource is adjusted accordingly.
1319 * Existing children of the busy memory resource must be immutable in the
1320 * request.
1321 *
1322 * Note:
1323 * - Additional release conditions, such as overlapping region, can be
1324 * supported after they are confirmed as valid cases.
1325 * - When a busy memory resource gets split into two entries, the code
1326 * assumes that all children remain in the lower address entry for
1327 * simplicity. Enhance this logic when necessary.
1328 */
David Hildenbrandcb8e3c82020-10-15 20:09:12 -07001329void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
Toshi Kani825f7872013-04-29 15:08:19 -07001330{
David Hildenbrandcb8e3c82020-10-15 20:09:12 -07001331 struct resource *parent = &iomem_resource;
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001332 struct resource *new_res = NULL;
1333 bool alloc_nofail = false;
Toshi Kani825f7872013-04-29 15:08:19 -07001334 struct resource **p;
1335 struct resource *res;
Toshi Kani825f7872013-04-29 15:08:19 -07001336 resource_size_t end;
Toshi Kani825f7872013-04-29 15:08:19 -07001337
1338 end = start + size - 1;
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001339 if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1340 return;
Toshi Kani825f7872013-04-29 15:08:19 -07001341
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001342 /*
1343 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1344 * just before releasing the region. This is highly unlikely to
1345 * fail - let's play save and make it never fail as the caller cannot
1346 * perform any error handling (e.g., trying to re-add memory will fail
1347 * similarly).
1348 */
1349retry:
1350 new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
Toshi Kani825f7872013-04-29 15:08:19 -07001351
1352 p = &parent->child;
1353 write_lock(&resource_lock);
1354
1355 while ((res = *p)) {
1356 if (res->start >= end)
1357 break;
1358
1359 /* look for the next resource if it does not fit into */
1360 if (res->start > start || res->end < end) {
1361 p = &res->sibling;
1362 continue;
1363 }
1364
Oscar Salvador65c78782018-12-28 00:36:26 -08001365 /*
1366 * All memory regions added from memory-hotplug path have the
1367 * flag IORESOURCE_SYSTEM_RAM. If the resource does not have
1368 * this flag, we know that we are dealing with a resource coming
1369 * from HMM/devm. HMM/devm use another mechanism to add/release
1370 * a resource. This goes via devm_request_mem_region and
1371 * devm_release_mem_region.
1372 * HMM/devm take care to release their resources when they want,
1373 * so if we are dealing with them, let us just back off here.
1374 */
1375 if (!(res->flags & IORESOURCE_SYSRAM)) {
Oscar Salvador65c78782018-12-28 00:36:26 -08001376 break;
1377 }
1378
Toshi Kani825f7872013-04-29 15:08:19 -07001379 if (!(res->flags & IORESOURCE_MEM))
1380 break;
1381
1382 if (!(res->flags & IORESOURCE_BUSY)) {
1383 p = &res->child;
1384 continue;
1385 }
1386
1387 /* found the target resource; let's adjust accordingly */
1388 if (res->start == start && res->end == end) {
1389 /* free the whole entry */
1390 *p = res->sibling;
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -07001391 free_resource(res);
Toshi Kani825f7872013-04-29 15:08:19 -07001392 } else if (res->start == start && res->end != end) {
1393 /* adjust the start */
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001394 WARN_ON_ONCE(__adjust_resource(res, end + 1,
1395 res->end - end));
Toshi Kani825f7872013-04-29 15:08:19 -07001396 } else if (res->start != start && res->end == end) {
1397 /* adjust the end */
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001398 WARN_ON_ONCE(__adjust_resource(res, res->start,
1399 start - res->start));
Toshi Kani825f7872013-04-29 15:08:19 -07001400 } else {
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001401 /* split into two entries - we need a new resource */
Toshi Kani825f7872013-04-29 15:08:19 -07001402 if (!new_res) {
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001403 new_res = alloc_resource(GFP_ATOMIC);
1404 if (!new_res) {
1405 alloc_nofail = true;
1406 write_unlock(&resource_lock);
1407 goto retry;
1408 }
Toshi Kani825f7872013-04-29 15:08:19 -07001409 }
1410 new_res->name = res->name;
1411 new_res->start = end + 1;
1412 new_res->end = res->end;
1413 new_res->flags = res->flags;
Toshi Kani43ee4932016-01-26 21:57:19 +01001414 new_res->desc = res->desc;
Toshi Kani825f7872013-04-29 15:08:19 -07001415 new_res->parent = res->parent;
1416 new_res->sibling = res->sibling;
1417 new_res->child = NULL;
1418
David Hildenbrandec62d04e2020-10-15 20:08:28 -07001419 if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1420 start - res->start)))
Toshi Kani825f7872013-04-29 15:08:19 -07001421 break;
1422 res->sibling = new_res;
1423 new_res = NULL;
1424 }
1425
1426 break;
1427 }
1428
1429 write_unlock(&resource_lock);
Yasuaki Ishimatsuebff7d82013-04-29 15:08:56 -07001430 free_resource(new_res);
Toshi Kani825f7872013-04-29 15:08:19 -07001431}
1432#endif /* CONFIG_MEMORY_HOTREMOVE */
1433
David Hildenbrand9ca65512020-10-15 20:08:49 -07001434#ifdef CONFIG_MEMORY_HOTPLUG
1435static bool system_ram_resources_mergeable(struct resource *r1,
1436 struct resource *r2)
1437{
1438 /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1439 return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1440 r1->name == r2->name && r1->desc == r2->desc &&
1441 !r1->child && !r2->child;
1442}
1443
Mauro Carvalho Chehab3be8da52020-12-15 20:46:16 -08001444/**
David Hildenbrand9ca65512020-10-15 20:08:49 -07001445 * merge_system_ram_resource - mark the System RAM resource mergeable and try to
Mauro Carvalho Chehab3be8da52020-12-15 20:46:16 -08001446 * merge it with adjacent, mergeable resources
David Hildenbrand9ca65512020-10-15 20:08:49 -07001447 * @res: resource descriptor
1448 *
1449 * This interface is intended for memory hotplug, whereby lots of contiguous
1450 * system ram resources are added (e.g., via add_memory*()) by a driver, and
1451 * the actual resource boundaries are not of interest (e.g., it might be
1452 * relevant for DIMMs). Only resources that are marked mergeable, that have the
1453 * same parent, and that don't have any children are considered. All mergeable
1454 * resources must be immutable during the request.
1455 *
1456 * Note:
1457 * - The caller has to make sure that no pointers to resources that are
1458 * marked mergeable are used anymore after this call - the resource might
1459 * be freed and the pointer might be stale!
1460 * - release_mem_region_adjustable() will split on demand on memory hotunplug
1461 */
1462void merge_system_ram_resource(struct resource *res)
1463{
1464 const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1465 struct resource *cur;
1466
1467 if (WARN_ON_ONCE((res->flags & flags) != flags))
1468 return;
1469
1470 write_lock(&resource_lock);
1471 res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1472
1473 /* Try to merge with next item in the list. */
1474 cur = res->sibling;
1475 if (cur && system_ram_resources_mergeable(res, cur)) {
1476 res->end = cur->end;
1477 res->sibling = cur->sibling;
1478 free_resource(cur);
1479 }
1480
1481 /* Try to merge with previous item in the list. */
1482 cur = res->parent->child;
1483 while (cur && cur->sibling != res)
1484 cur = cur->sibling;
1485 if (cur && system_ram_resources_mergeable(cur, res)) {
1486 cur->end = res->end;
1487 cur->sibling = res->sibling;
1488 free_resource(res);
1489 }
1490 write_unlock(&resource_lock);
1491}
1492#endif /* CONFIG_MEMORY_HOTPLUG */
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494/*
Tejun Heo9ac78492007-01-20 16:00:26 +09001495 * Managed region resource
1496 */
Thierry Reding8d388212014-08-01 14:15:10 +02001497static void devm_resource_release(struct device *dev, void *ptr)
1498{
1499 struct resource **r = ptr;
1500
1501 release_resource(*r);
1502}
1503
1504/**
1505 * devm_request_resource() - request and reserve an I/O or memory resource
1506 * @dev: device for which to request the resource
1507 * @root: root of the resource tree from which to request the resource
1508 * @new: descriptor of the resource to request
1509 *
1510 * This is a device-managed version of request_resource(). There is usually
1511 * no need to release resources requested by this function explicitly since
1512 * that will be taken care of when the device is unbound from its driver.
1513 * If for some reason the resource needs to be released explicitly, because
1514 * of ordering issues for example, drivers must call devm_release_resource()
1515 * rather than the regular release_resource().
1516 *
1517 * When a conflict is detected between any existing resources and the newly
1518 * requested resource, an error message will be printed.
1519 *
1520 * Returns 0 on success or a negative error code on failure.
1521 */
1522int devm_request_resource(struct device *dev, struct resource *root,
1523 struct resource *new)
1524{
1525 struct resource *conflict, **ptr;
1526
1527 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1528 if (!ptr)
1529 return -ENOMEM;
1530
1531 *ptr = new;
1532
1533 conflict = request_resource_conflict(root, new);
1534 if (conflict) {
1535 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1536 new, conflict->name, conflict);
1537 devres_free(ptr);
1538 return -EBUSY;
1539 }
1540
1541 devres_add(dev, ptr);
1542 return 0;
1543}
1544EXPORT_SYMBOL(devm_request_resource);
1545
1546static int devm_resource_match(struct device *dev, void *res, void *data)
1547{
1548 struct resource **ptr = res;
1549
1550 return *ptr == data;
1551}
1552
1553/**
1554 * devm_release_resource() - release a previously requested resource
1555 * @dev: device for which to release the resource
1556 * @new: descriptor of the resource to release
1557 *
1558 * Releases a resource previously requested using devm_request_resource().
1559 */
1560void devm_release_resource(struct device *dev, struct resource *new)
1561{
1562 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1563 new));
1564}
1565EXPORT_SYMBOL(devm_release_resource);
1566
Tejun Heo9ac78492007-01-20 16:00:26 +09001567struct region_devres {
1568 struct resource *parent;
1569 resource_size_t start;
1570 resource_size_t n;
1571};
1572
1573static void devm_region_release(struct device *dev, void *res)
1574{
1575 struct region_devres *this = res;
1576
1577 __release_region(this->parent, this->start, this->n);
1578}
1579
1580static int devm_region_match(struct device *dev, void *res, void *match_data)
1581{
1582 struct region_devres *this = res, *match = match_data;
1583
1584 return this->parent == match->parent &&
1585 this->start == match->start && this->n == match->n;
1586}
1587
Borislav Petkovb69c2e22018-10-09 16:11:21 +02001588struct resource *
1589__devm_request_region(struct device *dev, struct resource *parent,
1590 resource_size_t start, resource_size_t n, const char *name)
Tejun Heo9ac78492007-01-20 16:00:26 +09001591{
1592 struct region_devres *dr = NULL;
1593 struct resource *res;
1594
1595 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1596 GFP_KERNEL);
1597 if (!dr)
1598 return NULL;
1599
1600 dr->parent = parent;
1601 dr->start = start;
1602 dr->n = n;
1603
Arjan van de Vene8de1482008-10-22 19:55:31 -07001604 res = __request_region(parent, start, n, name, 0);
Tejun Heo9ac78492007-01-20 16:00:26 +09001605 if (res)
1606 devres_add(dev, dr);
1607 else
1608 devres_free(dr);
1609
1610 return res;
1611}
1612EXPORT_SYMBOL(__devm_request_region);
1613
1614void __devm_release_region(struct device *dev, struct resource *parent,
1615 resource_size_t start, resource_size_t n)
1616{
1617 struct region_devres match_data = { parent, start, n };
1618
1619 __release_region(parent, start, n);
1620 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1621 &match_data));
1622}
1623EXPORT_SYMBOL(__devm_release_region);
1624
1625/*
Bjorn Helgaasffd2e8d2017-12-01 11:50:33 -06001626 * Reserve I/O ports or memory based on "reserve=" kernel parameter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
1628#define MAXRESERVE 4
1629static int __init reserve_setup(char *str)
1630{
1631 static int reserved;
1632 static struct resource reserve[MAXRESERVE];
1633
1634 for (;;) {
Zhang Rui8bc1ad72009-06-30 11:41:31 -07001635 unsigned int io_start, io_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int x = reserved;
Bjorn Helgaasffd2e8d2017-12-01 11:50:33 -06001637 struct resource *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Bjorn Helgaasffd2e8d2017-12-01 11:50:33 -06001639 if (get_option(&str, &io_start) != 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 break;
Bjorn Helgaasffd2e8d2017-12-01 11:50:33 -06001641 if (get_option(&str, &io_num) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 break;
1643 if (x < MAXRESERVE) {
1644 struct resource *res = reserve + x;
Bjorn Helgaasffd2e8d2017-12-01 11:50:33 -06001645
1646 /*
1647 * If the region starts below 0x10000, we assume it's
1648 * I/O port space; otherwise assume it's memory.
1649 */
1650 if (io_start < 0x10000) {
1651 res->flags = IORESOURCE_IO;
1652 parent = &ioport_resource;
1653 } else {
1654 res->flags = IORESOURCE_MEM;
1655 parent = &iomem_resource;
1656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 res->name = "reserved";
1658 res->start = io_start;
1659 res->end = io_start + io_num - 1;
Bjorn Helgaasffd2e8d2017-12-01 11:50:33 -06001660 res->flags |= IORESOURCE_BUSY;
Toshi Kani43ee4932016-01-26 21:57:19 +01001661 res->desc = IORES_DESC_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 res->child = NULL;
Bjorn Helgaasffd2e8d2017-12-01 11:50:33 -06001663 if (request_resource(parent, res) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 reserved = x+1;
1665 }
1666 }
1667 return 1;
1668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669__setup("reserve=", reserve_setup);
Suresh Siddha379daf62008-09-25 18:43:34 -07001670
1671/*
1672 * Check if the requested addr and size spans more than any slot in the
1673 * iomem resource tree.
1674 */
1675int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1676{
1677 struct resource *p = &iomem_resource;
1678 int err = 0;
1679 loff_t l;
1680
1681 read_lock(&resource_lock);
1682 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1683 /*
1684 * We can probably skip the resources without
1685 * IORESOURCE_IO attribute?
1686 */
1687 if (p->start >= addr + size)
1688 continue;
1689 if (p->end < addr)
1690 continue;
Suresh Siddhad68612b2008-10-28 11:45:42 -07001691 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1692 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
Suresh Siddha379daf62008-09-25 18:43:34 -07001693 continue;
Arjan van de Ven3ac52662008-12-13 09:15:27 -08001694 /*
1695 * if a resource is "BUSY", it's not a hardware resource
1696 * but a driver mapping of such a resource; we don't want
1697 * to warn for those; some drivers legitimately map only
1698 * partial hardware resources. (example: vesafb)
1699 */
1700 if (p->flags & IORESOURCE_BUSY)
1701 continue;
1702
Bjorn Helgaase4c72962014-04-14 15:38:11 -06001703 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
Ingo Molnar13eb8372008-09-26 10:10:12 +02001704 (unsigned long long)addr,
1705 (unsigned long long)(addr + size - 1),
Bjorn Helgaase4c72962014-04-14 15:38:11 -06001706 p->name, p);
Suresh Siddha379daf62008-09-25 18:43:34 -07001707 err = -1;
1708 break;
1709 }
1710 read_unlock(&resource_lock);
1711
1712 return err;
1713}
Arjan van de Vene8de1482008-10-22 19:55:31 -07001714
1715#ifdef CONFIG_STRICT_DEVMEM
1716static int strict_iomem_checks = 1;
1717#else
1718static int strict_iomem_checks;
1719#endif
1720
1721/*
David Hildenbranda9e7b8d2021-11-08 18:35:50 -08001722 * Check if an address is exclusive to the kernel and must not be mapped to
1723 * user space, for example, via /dev/mem.
1724 *
1725 * Returns true if exclusive to the kernel, otherwise returns false.
Arjan van de Vene8de1482008-10-22 19:55:31 -07001726 */
Yaowei Bai9825b452018-02-06 15:41:28 -08001727bool iomem_is_exclusive(u64 addr)
Arjan van de Vene8de1482008-10-22 19:55:31 -07001728{
David Hildenbranda9e7b8d2021-11-08 18:35:50 -08001729 const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
1730 IORESOURCE_EXCLUSIVE;
David Hildenbrandb78dfa02021-11-08 18:35:46 -08001731 bool skip_children = false, err = false;
Arjan van de Vene8de1482008-10-22 19:55:31 -07001732 int size = PAGE_SIZE;
David Hildenbrandb78dfa02021-11-08 18:35:46 -08001733 struct resource *p;
Arjan van de Vene8de1482008-10-22 19:55:31 -07001734
Arjan van de Vene8de1482008-10-22 19:55:31 -07001735 addr = addr & PAGE_MASK;
1736
1737 read_lock(&resource_lock);
David Hildenbrandb78dfa02021-11-08 18:35:46 -08001738 for_each_resource(&iomem_resource, p, skip_children) {
Arjan van de Vene8de1482008-10-22 19:55:31 -07001739 if (p->start >= addr + size)
1740 break;
David Hildenbrandb78dfa02021-11-08 18:35:46 -08001741 if (p->end < addr) {
1742 skip_children = true;
Arjan van de Vene8de1482008-10-22 19:55:31 -07001743 continue;
David Hildenbrandb78dfa02021-11-08 18:35:46 -08001744 }
1745 skip_children = false;
1746
Dan Williams90a545e2015-11-23 15:49:03 -08001747 /*
David Hildenbranda9e7b8d2021-11-08 18:35:50 -08001748 * IORESOURCE_SYSTEM_RAM resources are exclusive if
1749 * IORESOURCE_EXCLUSIVE is set, even if they
1750 * are not busy and even if "iomem=relaxed" is set. The
1751 * responsible driver dynamically adds/removes system RAM within
1752 * such an area and uncontrolled access is dangerous.
1753 */
1754 if ((p->flags & exclusive_system_ram) == exclusive_system_ram) {
1755 err = true;
1756 break;
1757 }
1758
1759 /*
Dan Williams90a545e2015-11-23 15:49:03 -08001760 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1761 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1762 * resource is busy.
1763 */
David Hildenbranda9e7b8d2021-11-08 18:35:50 -08001764 if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
Dan Williams90a545e2015-11-23 15:49:03 -08001765 continue;
1766 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1767 || p->flags & IORESOURCE_EXCLUSIVE) {
Yaowei Bai9825b452018-02-06 15:41:28 -08001768 err = true;
Arjan van de Vene8de1482008-10-22 19:55:31 -07001769 break;
1770 }
1771 }
1772 read_unlock(&resource_lock);
1773
1774 return err;
1775}
1776
Jiang Liu90e97822015-02-05 13:44:43 +08001777struct resource_entry *resource_list_create_entry(struct resource *res,
1778 size_t extra_size)
1779{
1780 struct resource_entry *entry;
1781
1782 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1783 if (entry) {
1784 INIT_LIST_HEAD(&entry->node);
1785 entry->res = res ? res : &entry->__res;
1786 }
1787
1788 return entry;
1789}
1790EXPORT_SYMBOL(resource_list_create_entry);
1791
1792void resource_list_free(struct list_head *head)
1793{
1794 struct resource_entry *entry, *tmp;
1795
1796 list_for_each_entry_safe(entry, tmp, head, node)
1797 resource_list_destroy_entry(entry);
1798}
1799EXPORT_SYMBOL(resource_list_free);
1800
Christoph Hellwig00929082019-06-26 14:27:06 +02001801#ifdef CONFIG_DEVICE_PRIVATE
Christoph Hellwig0c385192019-08-18 11:05:54 +02001802static struct resource *__request_free_mem_region(struct device *dev,
1803 struct resource *base, unsigned long size, const char *name)
1804{
1805 resource_size_t end, addr;
1806 struct resource *res;
Alistair Popple56fd9492021-05-06 18:05:30 -07001807 struct region_devres *dr = NULL;
Christoph Hellwig0c385192019-08-18 11:05:54 +02001808
1809 size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
1810 end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
1811 addr = end - size + 1UL;
1812
Alistair Popple56fd9492021-05-06 18:05:30 -07001813 res = alloc_resource(GFP_KERNEL);
1814 if (!res)
1815 return ERR_PTR(-ENOMEM);
1816
1817 if (dev) {
1818 dr = devres_alloc(devm_region_release,
1819 sizeof(struct region_devres), GFP_KERNEL);
1820 if (!dr) {
1821 free_resource(res);
1822 return ERR_PTR(-ENOMEM);
1823 }
1824 }
1825
1826 write_lock(&resource_lock);
Christoph Hellwig0c385192019-08-18 11:05:54 +02001827 for (; addr > size && addr >= base->start; addr -= size) {
Alistair Popple56fd9492021-05-06 18:05:30 -07001828 if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
Christoph Hellwig0c385192019-08-18 11:05:54 +02001829 REGION_DISJOINT)
1830 continue;
1831
Alistair Poppleeb1f0652021-05-14 17:27:13 -07001832 if (__request_region_locked(res, &iomem_resource, addr, size,
Alistair Popple56fd9492021-05-06 18:05:30 -07001833 name, 0))
1834 break;
1835
1836 if (dev) {
1837 dr->parent = &iomem_resource;
1838 dr->start = addr;
1839 dr->n = size;
1840 devres_add(dev, dr);
1841 }
1842
Christoph Hellwig0c385192019-08-18 11:05:54 +02001843 res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
Alistair Popple56fd9492021-05-06 18:05:30 -07001844 write_unlock(&resource_lock);
1845
1846 /*
1847 * A driver is claiming this region so revoke any mappings.
1848 */
1849 revoke_iomem(res);
Christoph Hellwig0c385192019-08-18 11:05:54 +02001850 return res;
1851 }
Alistair Popple56fd9492021-05-06 18:05:30 -07001852 write_unlock(&resource_lock);
1853
1854 free_resource(res);
1855 if (dr)
1856 devres_free(dr);
Christoph Hellwig0c385192019-08-18 11:05:54 +02001857
1858 return ERR_PTR(-ERANGE);
1859}
1860
Christoph Hellwig00929082019-06-26 14:27:06 +02001861/**
1862 * devm_request_free_mem_region - find free region for device private memory
1863 *
1864 * @dev: device struct to bind the resource to
1865 * @size: size in bytes of the device memory to add
1866 * @base: resource tree to look in
1867 *
1868 * This function tries to find an empty range of physical address big enough to
1869 * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
1870 * memory, which in turn allocates struct pages.
1871 */
1872struct resource *devm_request_free_mem_region(struct device *dev,
1873 struct resource *base, unsigned long size)
1874{
Christoph Hellwig0c385192019-08-18 11:05:54 +02001875 return __request_free_mem_region(dev, base, size, dev_name(dev));
Christoph Hellwig00929082019-06-26 14:27:06 +02001876}
1877EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
Christoph Hellwig0c385192019-08-18 11:05:54 +02001878
1879struct resource *request_free_mem_region(struct resource *base,
1880 unsigned long size, const char *name)
1881{
1882 return __request_free_mem_region(NULL, base, size, name);
1883}
1884EXPORT_SYMBOL_GPL(request_free_mem_region);
1885
Christoph Hellwig00929082019-06-26 14:27:06 +02001886#endif /* CONFIG_DEVICE_PRIVATE */
1887
Arjan van de Vene8de1482008-10-22 19:55:31 -07001888static int __init strict_iomem(char *str)
1889{
1890 if (strstr(str, "relaxed"))
1891 strict_iomem_checks = 0;
1892 if (strstr(str, "strict"))
1893 strict_iomem_checks = 1;
1894 return 1;
1895}
1896
Daniel Vetter71a1d8e2020-11-27 17:41:24 +01001897static int iomem_fs_init_fs_context(struct fs_context *fc)
1898{
1899 return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
1900}
1901
1902static struct file_system_type iomem_fs_type = {
1903 .name = "iomem",
1904 .owner = THIS_MODULE,
1905 .init_fs_context = iomem_fs_init_fs_context,
1906 .kill_sb = kill_anon_super,
1907};
1908
1909static int __init iomem_init_inode(void)
1910{
1911 static struct vfsmount *iomem_vfs_mount;
1912 static int iomem_fs_cnt;
1913 struct inode *inode;
1914 int rc;
1915
1916 rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
1917 if (rc < 0) {
1918 pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
1919 return rc;
1920 }
1921
1922 inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
1923 if (IS_ERR(inode)) {
1924 rc = PTR_ERR(inode);
1925 pr_err("Cannot allocate inode for iomem: %d\n", rc);
1926 simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
1927 return rc;
1928 }
1929
1930 /*
1931 * Publish iomem revocation inode initialized.
1932 * Pairs with smp_load_acquire() in revoke_iomem().
1933 */
1934 smp_store_release(&iomem_inode, inode);
1935
1936 return 0;
1937}
1938
1939fs_initcall(iomem_init_inode);
1940
Arjan van de Vene8de1482008-10-22 19:55:31 -07001941__setup("iomem=", strict_iomem);