kernel/resource: disallow access to exclusive system RAM regions
[linux-2.6-block.git] / kernel / resource.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/kernel/resource.c
4 *
5 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
7 *
8 * Arbitrary resource management.
9 */
10
65fed8f6
OP
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
9984de1a 13#include <linux/export.h>
1da177e4
LT
14#include <linux/errno.h>
15#include <linux/ioport.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/spinlock.h>
19#include <linux/fs.h>
20#include <linux/proc_fs.h>
71a1d8ed 21#include <linux/pseudo_fs.h>
8b6d043b 22#include <linux/sched.h>
1da177e4 23#include <linux/seq_file.h>
9ac7849e 24#include <linux/device.h>
d68612b2 25#include <linux/pfn.h>
ebff7d8f 26#include <linux/mm.h>
71a1d8ed 27#include <linux/mount.h>
90e97820 28#include <linux/resource_ext.h>
71a1d8ed 29#include <uapi/linux/magic.h>
1da177e4
LT
30#include <asm/io.h>
31
32
33struct resource ioport_resource = {
34 .name = "PCI IO",
6550e07f 35 .start = 0,
1da177e4
LT
36 .end = IO_SPACE_LIMIT,
37 .flags = IORESOURCE_IO,
38};
1da177e4
LT
39EXPORT_SYMBOL(ioport_resource);
40
41struct resource iomem_resource = {
42 .name = "PCI mem",
6550e07f
GKH
43 .start = 0,
44 .end = -1,
1da177e4
LT
45 .flags = IORESOURCE_MEM,
46};
1da177e4
LT
47EXPORT_SYMBOL(iomem_resource);
48
23c570a6
RP
49/* 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
1da177e4
LT
57static DEFINE_RWLOCK(resource_lock);
58
ebff7d8f
YI
59/*
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
97523a4e 67static struct resource *next_resource(struct resource *p)
1da177e4 68{
1da177e4
LT
69 if (p->child)
70 return p->child;
71 while (!p->sibling && p->parent)
72 p = p->parent;
73 return p->sibling;
74}
75
b78dfa05
DH
76static 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
8c86e70a
VG
88static void *r_next(struct seq_file *m, void *v, loff_t *pos)
89{
90 struct resource *p = v;
91 (*pos)++;
97523a4e 92 return (void *)next_resource(p);
8c86e70a
VG
93}
94
13eb8375
IM
95#ifdef CONFIG_PROC_FS
96
97enum { MAX_IORES_LEVEL = 5 };
98
1da177e4
LT
99static void *r_start(struct seq_file *m, loff_t *pos)
100 __acquires(resource_lock)
101{
4e292a96 102 struct resource *p = PDE_DATA(file_inode(m->file));
1da177e4
LT
103 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{
4e292a96 118 struct resource *root = PDE_DATA(file_inode(m->file));
1da177e4 119 struct resource *r = v, *p;
51d7b120 120 unsigned long long start, end;
1da177e4
LT
121 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;
51d7b120
LT
127
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
685143ac 135 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
1da177e4 136 depth * 2, "",
51d7b120
LT
137 width, start,
138 width, end,
1da177e4
LT
139 r->name ? r->name : "<BAD>");
140 return 0;
141}
142
15ad7cdc 143static const struct seq_operations resource_op = {
1da177e4
LT
144 .start = r_start,
145 .next = r_next,
146 .stop = r_stop,
147 .show = r_show,
148};
149
1da177e4
LT
150static int __init ioresources_init(void)
151{
4e292a96
CH
152 proc_create_seq_data("ioports", 0, NULL, &resource_op,
153 &ioport_resource);
154 proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
1da177e4
LT
155 return 0;
156}
157__initcall(ioresources_init);
158
159#endif /* CONFIG_PROC_FS */
160
ebff7d8f
YI
161static 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
1da177e4
LT
195/* Return the conflict entry if you can't request it */
196static struct resource * __request_resource(struct resource *root, struct resource *new)
197{
d75fc8bb
GKH
198 resource_size_t start = new->start;
199 resource_size_t end = new->end;
1da177e4
LT
200 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
ff3cc952 224static int __release_resource(struct resource *old, bool release_child)
1da177e4 225{
ff3cc952 226 struct resource *tmp, **p, *chd;
1da177e4
LT
227
228 p = &old->parent->child;
229 for (;;) {
230 tmp = *p;
231 if (!tmp)
232 break;
233 if (tmp == old) {
ff3cc952
TK
234 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 }
1da177e4
LT
245 old->parent = NULL;
246 return 0;
247 }
248 p = &tmp->sibling;
249 }
250 return -EINVAL;
251}
252
5eeec0ec
YL
253static 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
e1ca66d1 283/**
66f1207b 284 * request_resource_conflict - request and reserve an I/O or memory resource
e1ca66d1
RD
285 * @root: root resource descriptor
286 * @new: resource descriptor desired by caller
287 *
66f1207b 288 * Returns 0 for success, conflict resource on error.
e1ca66d1 289 */
66f1207b 290struct resource *request_resource_conflict(struct resource *root, struct resource *new)
1da177e4
LT
291{
292 struct resource *conflict;
293
294 write_lock(&resource_lock);
295 conflict = __request_resource(root, new);
296 write_unlock(&resource_lock);
66f1207b
BH
297 return conflict;
298}
299
300/**
301 * 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 */
307int request_resource(struct resource *root, struct resource *new)
308{
309 struct resource *conflict;
310
311 conflict = request_resource_conflict(root, new);
1da177e4
LT
312 return conflict ? -EBUSY : 0;
313}
314
315EXPORT_SYMBOL(request_resource);
316
e1ca66d1
RD
317/**
318 * release_resource - release a previously reserved resource
319 * @old: resource pointer
320 */
1da177e4
LT
321int release_resource(struct resource *old)
322{
323 int retval;
324
325 write_lock(&resource_lock);
ff3cc952 326 retval = __release_resource(old, true);
1da177e4
LT
327 write_unlock(&resource_lock);
328 return retval;
329}
330
331EXPORT_SYMBOL(release_resource);
332
f26621e6 333/**
3be8da57
MCC
334 * find_next_iomem_res - Finds the lowest iomem resource that covers part of
335 * [@start..@end].
010a93bf 336 *
f26621e6
BP
337 * 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
49f17c26 339 * -ENODEV. Returns -EINVAL for invalid parameters.
010a93bf 340 *
f26621e6
BP
341 * @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
f26621e6 345 * @res: return ptr, if resource found
3be8da57
MCC
346 *
347 * The caller must specify @start, @end, @flags, and @desc
348 * (which may be IORES_DESC_NONE).
2842f114 349 */
010a93bf
BH
350static int find_next_iomem_res(resource_size_t start, resource_size_t end,
351 unsigned long flags, unsigned long desc,
97523a4e 352 struct resource *res)
2842f114 353{
2842f114
KH
354 struct resource *p;
355
b69c2e20
BP
356 if (!res)
357 return -EINVAL;
2842f114 358
b69c2e20
BP
359 if (start >= end)
360 return -EINVAL;
8c86e70a 361
800df627
VG
362 read_lock(&resource_lock);
363
97523a4e 364 for (p = iomem_resource.child; p; p = next_resource(p)) {
75639875 365 /* If we passed the resource we are looking for, stop */
2842f114
KH
366 if (p->start > end) {
367 p = NULL;
368 break;
369 }
75639875
NA
370
371 /* Skip until we find a range that matches what we look for */
372 if (p->end < start)
373 continue;
374
75639875
NA
375 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;
2842f114 382 }
8c86e70a 383
49f17c26
NA
384 if (p) {
385 /* copy data */
73fb952d
DW
386 *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 };
49f17c26
NA
393 }
394
2842f114 395 read_unlock(&resource_lock);
49f17c26 396 return p ? 0 : -ENODEV;
2842f114 397}
908eedc6 398
010a93bf
BH
399static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
400 unsigned long flags, unsigned long desc,
97523a4e 401 void *arg,
1d2e733b 402 int (*func)(struct resource *, void *))
4ac2aed8 403{
010a93bf 404 struct resource res;
5cd401ac 405 int ret = -EINVAL;
4ac2aed8 406
010a93bf 407 while (start < end &&
97523a4e 408 !find_next_iomem_res(start, end, flags, desc, &res)) {
010a93bf 409 ret = (*func)(&res, arg);
4ac2aed8
TL
410 if (ret)
411 break;
412
010a93bf 413 start = res.end + 1;
4ac2aed8
TL
414 }
415
416 return ret;
417}
418
b69c2e20 419/**
3be8da57
MCC
420 * walk_iomem_res_desc - Walks through iomem resources and calls func()
421 * with matching resource ranges.
422 * *
3f33647c
TK
423 * @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
f75d6515
RD
427 * @arg: function argument for the callback @func
428 * @func: callback function that is called for each qualifying resource area
3f33647c 429 *
3be8da57
MCC
430 * All the memory ranges which overlap start,end and also match flags and
431 * desc are valid candidates.
432 *
3f33647c
TK
433 * 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,
1d2e733b 437 u64 end, void *arg, int (*func)(struct resource *, void *))
3f33647c 438{
97523a4e 439 return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
3f33647c 440}
d76401ad 441EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
3f33647c 442
8c86e70a 443/*
bd7e6cb3
TK
444 * 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.
8c86e70a
VG
449 */
450int walk_system_ram_res(u64 start, u64 end, void *arg,
b69c2e20 451 int (*func)(struct resource *, void *))
8c86e70a 452{
010a93bf 453 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
8c86e70a 454
97523a4e
DH
455 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
456 func);
0e4c12b4
TL
457}
458
459/*
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{
010a93bf 466 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
0e4c12b4 467
97523a4e
DH
468 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
469 func);
8c86e70a
VG
470}
471
908eedc6 472/*
bd7e6cb3
TK
473 * 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.
908eedc6
KH
476 */
477int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
b69c2e20 478 void *arg, int (*func)(unsigned long, unsigned long, void *))
75884fb1 479{
010a93bf
BH
480 resource_size_t start, end;
481 unsigned long flags;
75884fb1 482 struct resource res;
37b99dd5 483 unsigned long pfn, end_pfn;
5cd401ac 484 int ret = -EINVAL;
908eedc6 485
010a93bf
BH
486 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 &&
97523a4e 490 !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
00ff9a91
DH
491 pfn = PFN_UP(res.start);
492 end_pfn = PFN_DOWN(res.end + 1);
37b99dd5 493 if (end_pfn > pfn)
f4149660 494 ret = (*func)(pfn, end_pfn - pfn, arg);
75884fb1
KH
495 if (ret)
496 break;
010a93bf 497 start = res.end + 1;
75884fb1
KH
498 }
499 return ret;
500}
501
61ef2489
WF
502static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
503{
504 return 1;
505}
4ac2aed8 506
61ef2489
WF
507/*
508 * This generic page_is_ram() returns true if specified address is
bd7e6cb3 509 * registered as System RAM in iomem_resource list.
61ef2489 510 */
e5273007 511int __weak page_is_ram(unsigned long pfn)
61ef2489
WF
512{
513 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
514}
c5a13032 515EXPORT_SYMBOL_GPL(page_is_ram);
61ef2489 516
d486ccb2
AP
517static 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
124fe20d
DW
545/**
546 * region_intersects() - determine intersection of region with known resources
547 * @start: region start address
548 * @size: size of region
1c29f25b
TK
549 * @flags: flags of resource (in iomem_resource)
550 * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
67cf13ce 551 *
124fe20d 552 * Check if the specified region partially overlaps or fully eclipses a
1c29f25b
TK
553 * 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.
124fe20d
DW
560 *
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.
67cf13ce 564 */
1c29f25b
TK
565int region_intersects(resource_size_t start, size_t size, unsigned long flags,
566 unsigned long desc)
67cf13ce 567{
d486ccb2 568 int ret;
f6c6010a 569
67cf13ce 570 read_lock(&resource_lock);
d486ccb2 571 ret = __region_intersects(start, size, flags, desc);
67cf13ce 572 read_unlock(&resource_lock);
124fe20d 573
d486ccb2 574 return ret;
67cf13ce 575}
1c29f25b 576EXPORT_SYMBOL_GPL(region_intersects);
67cf13ce 577
fcb11918
BH
578void __weak arch_remove_reservations(struct resource *avail)
579{
580}
581
a9cea017
BH
582static 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
5d6b1fa3
BH
590static 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
1da177e4 599/*
23c570a6
RP
600 * Find empty slot in the resource tree with the given range and
601 * alignment constraints
1da177e4 602 */
23c570a6
RP
603static int __find_resource(struct resource *root, struct resource *old,
604 struct resource *new,
605 resource_size_t size,
606 struct resource_constraint *constraint)
1da177e4
LT
607{
608 struct resource *this = root->child;
a1862e31 609 struct resource tmp = *new, avail, alloc;
1da177e4 610
0e2c8b8f 611 tmp.start = root->start;
1da177e4 612 /*
c0f5ac54
BH
613 * 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.
1da177e4 615 */
23c570a6
RP
616 if (this && this->start == root->start) {
617 tmp.start = (this == old) ? old->start : this->end + 1;
1da177e4
LT
618 this = this->sibling;
619 }
c0f5ac54 620 for(;;) {
1da177e4 621 if (this)
23c570a6 622 tmp.end = (this == old) ? this->end : this->start - 1;
1da177e4 623 else
0e2c8b8f 624 tmp.end = root->end;
5d6b1fa3 625
47ea91b4
RP
626 if (tmp.end < tmp.start)
627 goto next;
628
23c570a6 629 resource_clip(&tmp, constraint->min, constraint->max);
fcb11918 630 arch_remove_reservations(&tmp);
a9cea017 631
a1862e31 632 /* Check for overflow after ALIGN() */
23c570a6 633 avail.start = ALIGN(tmp.start, constraint->align);
a1862e31 634 avail.end = tmp.end;
5edb93b8 635 avail.flags = new->flags & ~IORESOURCE_UNSET;
a1862e31 636 if (avail.start >= tmp.start) {
5edb93b8 637 alloc.flags = avail.flags;
23c570a6
RP
638 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
639 size, constraint->align);
a1862e31 640 alloc.end = alloc.start + size - 1;
60bb83b8
TI
641 if (alloc.start <= alloc.end &&
642 resource_contains(&avail, &alloc)) {
a1862e31
BH
643 new->start = alloc.start;
644 new->end = alloc.end;
645 return 0;
646 }
1da177e4 647 }
47ea91b4
RP
648
649next: if (!this || this->end == root->end)
1da177e4 650 break;
47ea91b4 651
23c570a6
RP
652 if (this != old)
653 tmp.start = this->end + 1;
1da177e4
LT
654 this = this->sibling;
655 }
656 return -EBUSY;
657}
658
23c570a6
RP
659/*
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
e1ca66d1 669/**
23c570a6
RP
670 * 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 */
28ab49ff 679static int reallocate_resource(struct resource *root, struct resource *old,
b69c2e20
BP
680 resource_size_t newsize,
681 struct resource_constraint *constraint)
23c570a6
RP
682{
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 {
ff3cc952 707 __release_resource(old, true);
23c570a6
RP
708 *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
e1ca66d1
RD
721 * @root: root resource descriptor
722 * @new: resource descriptor desired by caller
723 * @size: requested resource region size
ee5e5683
WY
724 * @min: minimum boundary to allocate
725 * @max: maximum boundary to allocate
e1ca66d1
RD
726 * @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
1da177e4
LT
729 */
730int allocate_resource(struct resource *root, struct resource *new,
d75fc8bb
GKH
731 resource_size_t size, resource_size_t min,
732 resource_size_t max, resource_size_t align,
b26b2d49 733 resource_size_t (*alignf)(void *,
3b7a17fc 734 const struct resource *,
b26b2d49
DB
735 resource_size_t,
736 resource_size_t),
1da177e4
LT
737 void *alignf_data)
738{
739 int err;
23c570a6 740 struct resource_constraint constraint;
1da177e4 741
a9cea017
BH
742 if (!alignf)
743 alignf = simple_align_resource;
744
23c570a6
RP
745 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
1da177e4 757 write_lock(&resource_lock);
23c570a6 758 err = find_resource(root, new, size, &constraint);
1da177e4
LT
759 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
1c388919
GU
767/**
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
bef69ea0
LT
788/*
789 * Insert a resource into the resource tree. If successful, return NULL,
790 * otherwise return the conflicting resource (compare to __request_resource())
1da177e4 791 */
bef69ea0 792static struct resource * __insert_resource(struct resource *parent, struct resource *new)
1da177e4 793{
1da177e4
LT
794 struct resource *first, *next;
795
d33b6fba 796 for (;; parent = first) {
d33b6fba
MW
797 first = __request_resource(parent, new);
798 if (!first)
bef69ea0 799 return first;
d33b6fba 800
d33b6fba 801 if (first == parent)
bef69ea0 802 return first;
5de1cb2d
HS
803 if (WARN_ON(first == new)) /* duplicated insertion */
804 return first;
d33b6fba
MW
805
806 if ((first->start > new->start) || (first->end < new->end))
807 break;
808 if ((first->start == new->start) && (first->end == new->end))
809 break;
1da177e4
LT
810 }
811
812 for (next = first; ; next = next->sibling) {
813 /* Partial overlap? Bad, and unfixable */
814 if (next->start < new->start || next->end > new->end)
bef69ea0 815 return next;
1da177e4
LT
816 if (!next->sibling)
817 break;
818 if (next->sibling->start > new->end)
819 break;
820 }
821
1da177e4
LT
822 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 }
bef69ea0
LT
838 return NULL;
839}
1da177e4 840
bef69ea0 841/**
66f1207b 842 * insert_resource_conflict - Inserts resource in the resource tree
bef69ea0
LT
843 * @parent: parent of the new resource
844 * @new: new resource to insert
845 *
66f1207b 846 * Returns 0 on success, conflict resource if the resource can't be inserted.
bef69ea0 847 *
66f1207b 848 * This function is equivalent to request_resource_conflict when no conflict
bef69ea0
LT
849 * 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.
ff3cc952
TK
853 *
854 * This function is intended for producers of resources, such as FW modules
855 * and bus drivers.
bef69ea0 856 */
66f1207b 857struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
bef69ea0
LT
858{
859 struct resource *conflict;
860
861 write_lock(&resource_lock);
862 conflict = __insert_resource(parent, new);
863 write_unlock(&resource_lock);
66f1207b
BH
864 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.
ff3cc952
TK
873 *
874 * This function is intended for producers of resources, such as FW modules
875 * and bus drivers.
66f1207b
BH
876 */
877int insert_resource(struct resource *parent, struct resource *new)
878{
879 struct resource *conflict;
880
881 conflict = insert_resource_conflict(parent, new);
bef69ea0
LT
882 return conflict ? -EBUSY : 0;
883}
8095d0f2 884EXPORT_SYMBOL_GPL(insert_resource);
bef69ea0
LT
885
886/**
887 * insert_resource_expand_to_fit - Insert a resource into the resource tree
6781f4ae 888 * @root: root resource descriptor
bef69ea0
LT
889 * @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 }
1da177e4 917 write_unlock(&resource_lock);
1da177e4
LT
918}
919
ff3cc952
TK
920/**
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}
8095d0f2 944EXPORT_SYMBOL_GPL(remove_resource);
ff3cc952 945
ae8e3a91
TK
946static int __adjust_resource(struct resource *res, resource_size_t start,
947 resource_size_t size)
1da177e4
LT
948{
949 struct resource *tmp, *parent = res->parent;
d75fc8bb 950 resource_size_t end = start + size - 1;
1da177e4
LT
951 int result = -EBUSY;
952
82ec90ea
YL
953 if (!parent)
954 goto skip;
955
1da177e4
LT
956 if ((start < parent->start) || (end > parent->end))
957 goto out;
958
1da177e4
LT
959 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
82ec90ea
YL
970skip:
971 for (tmp = res->child; tmp; tmp = tmp->sibling)
972 if ((tmp->start < start) || (tmp->end > end))
973 goto out;
974
1da177e4
LT
975 res->start = start;
976 res->end = end;
977 result = 0;
978
979 out:
ae8e3a91
TK
980 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,
b69c2e20 994 resource_size_t size)
ae8e3a91
TK
995{
996 int result;
997
998 write_lock(&resource_lock);
999 result = __adjust_resource(res, start, size);
1da177e4
LT
1000 write_unlock(&resource_lock);
1001 return result;
1002}
24105748 1003EXPORT_SYMBOL(adjust_resource);
1da177e4 1004
b69c2e20
BP
1005static void __init
1006__reserve_region_with_split(struct resource *root, resource_size_t start,
1007 resource_size_t end, const char *name)
268364a0
YL
1008{
1009 struct resource *parent = root;
1010 struct resource *conflict;
ebff7d8f 1011 struct resource *res = alloc_resource(GFP_ATOMIC);
4965f566 1012 struct resource *next_res = NULL;
f37e2334 1013 int type = resource_type(root);
268364a0
YL
1014
1015 if (!res)
1016 return;
1017
1018 res->name = name;
1019 res->start = start;
1020 res->end = end;
f37e2334 1021 res->flags = type | IORESOURCE_BUSY;
43ee493b 1022 res->desc = IORES_DESC_NONE;
268364a0 1023
4965f566 1024 while (1) {
268364a0 1025
4965f566
M
1026 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 }
268364a0 1034
4965f566
M
1035 /* conflict covered whole area */
1036 if (conflict->start <= res->start &&
1037 conflict->end >= res->end) {
ebff7d8f 1038 free_resource(res);
4965f566
M
1039 WARN_ON(next_res);
1040 break;
1041 }
1042
1043 /* 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) {
ebff7d8f 1048 next_res = alloc_resource(GFP_ATOMIC);
4965f566 1049 if (!next_res) {
ebff7d8f 1050 free_resource(res);
4965f566
M
1051 break;
1052 }
1053 next_res->name = name;
1054 next_res->start = conflict->end + 1;
1055 next_res->end = end;
f37e2334 1056 next_res->flags = type | IORESOURCE_BUSY;
43ee493b 1057 next_res->desc = IORES_DESC_NONE;
4965f566
M
1058 }
1059 } else {
1060 res->start = conflict->end + 1;
1061 }
1062 }
268364a0
YL
1063
1064}
1065
b69c2e20
BP
1066void __init
1067reserve_region_with_split(struct resource *root, resource_size_t start,
1068 resource_size_t end, const char *name)
268364a0 1069{
65fed8f6
OP
1070 int abort = 0;
1071
268364a0 1072 write_lock(&resource_lock);
65fed8f6
OP
1073 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);
268364a0
YL
1092 write_unlock(&resource_lock);
1093}
1094
88452565
IK
1095/**
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:
1a4e564b 1105 return resource_size(res);
88452565
IK
1106 case IORESOURCE_STARTALIGN:
1107 return res->start;
1108 default:
1109 return 0;
1110 }
1111}
1112
1da177e4
LT
1113/*
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 *
e1ca66d1 1119 * request_region creates a new busy region.
1da177e4 1120 *
e1ca66d1
RD
1121 * release_region releases a matching busy region.
1122 */
1123
8b6d043b
AC
1124static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1125
71a1d8ed
DV
1126static 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
63cdafe0 1175static int __request_region_locked(struct resource *res, struct resource *parent,
d75fc8bb 1176 resource_size_t start, resource_size_t n,
e8de1481 1177 const char *name, int flags)
1da177e4 1178{
8b6d043b 1179 DECLARE_WAITQUEUE(wait, current);
c26ec88e
BH
1180
1181 res->name = name;
1182 res->start = start;
1183 res->end = start + n - 1;
c26ec88e 1184
c26ec88e
BH
1185 for (;;) {
1186 struct resource *conflict;
1187
4e0d8f7e
TK
1188 res->flags = resource_type(parent) | resource_ext_type(parent);
1189 res->flags |= IORESOURCE_BUSY | flags;
1190 res->desc = parent->desc;
1191
c26ec88e
BH
1192 conflict = __request_resource(parent, res);
1193 if (!conflict)
1da177e4 1194 break;
b926b7f3
DH
1195 /*
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 }
c26ec88e 1204 if (conflict != parent) {
59ceeaaf
SG
1205 if (!(conflict->flags & IORESOURCE_BUSY)) {
1206 parent = conflict;
c26ec88e 1207 continue;
59ceeaaf 1208 }
1da177e4 1209 }
8b6d043b
AC
1210 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 }
c26ec88e 1219 /* Uhhuh, that didn't work out.. */
63cdafe0 1220 return -EBUSY;
1da177e4 1221 }
63cdafe0
AP
1222
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);
c26ec88e 1246 write_unlock(&resource_lock);
3234ac66 1247
63cdafe0
AP
1248 if (ret) {
1249 free_resource(res);
1250 return NULL;
1251 }
1252
1253 if (parent == &iomem_resource)
71a1d8ed 1254 revoke_iomem(res);
3234ac66 1255
1da177e4
LT
1256 return res;
1257}
1da177e4
LT
1258EXPORT_SYMBOL(__request_region);
1259
e1ca66d1
RD
1260/**
1261 * __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 */
d75fc8bb 1268void __release_region(struct resource *parent, resource_size_t start,
b69c2e20 1269 resource_size_t n)
1da177e4
LT
1270{
1271 struct resource **p;
d75fc8bb 1272 resource_size_t end;
1da177e4
LT
1273
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);
8b6d043b
AC
1293 if (res->flags & IORESOURCE_MUXED)
1294 wake_up(&muxed_resource_wait);
ebff7d8f 1295 free_resource(res);
1da177e4
LT
1296 return;
1297 }
1298 p = &res->sibling;
1299 }
1300
1301 write_unlock(&resource_lock);
1302
685143ac
GKH
1303 printk(KERN_WARNING "Trying to free nonexistent resource "
1304 "<%016llx-%016llx>\n", (unsigned long long)start,
1305 (unsigned long long)end);
1da177e4 1306}
1da177e4
LT
1307EXPORT_SYMBOL(__release_region);
1308
825f787b
TK
1309#ifdef CONFIG_MEMORY_HOTREMOVE
1310/**
1311 * release_mem_region_adjustable - release a previously reserved memory region
825f787b
TK
1312 * @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 */
cb8e3c8b 1329void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
825f787b 1330{
cb8e3c8b 1331 struct resource *parent = &iomem_resource;
ec62d04e
DH
1332 struct resource *new_res = NULL;
1333 bool alloc_nofail = false;
825f787b
TK
1334 struct resource **p;
1335 struct resource *res;
825f787b 1336 resource_size_t end;
825f787b
TK
1337
1338 end = start + size - 1;
ec62d04e
DH
1339 if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1340 return;
825f787b 1341
ec62d04e
DH
1342 /*
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));
825f787b
TK
1351
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
65c78784
OS
1365 /*
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)) {
65c78784
OS
1376 break;
1377 }
1378
825f787b
TK
1379 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;
ebff7d8f 1391 free_resource(res);
825f787b
TK
1392 } else if (res->start == start && res->end != end) {
1393 /* adjust the start */
ec62d04e
DH
1394 WARN_ON_ONCE(__adjust_resource(res, end + 1,
1395 res->end - end));
825f787b
TK
1396 } else if (res->start != start && res->end == end) {
1397 /* adjust the end */
ec62d04e
DH
1398 WARN_ON_ONCE(__adjust_resource(res, res->start,
1399 start - res->start));
825f787b 1400 } else {
ec62d04e 1401 /* split into two entries - we need a new resource */
825f787b 1402 if (!new_res) {
ec62d04e
DH
1403 new_res = alloc_resource(GFP_ATOMIC);
1404 if (!new_res) {
1405 alloc_nofail = true;
1406 write_unlock(&resource_lock);
1407 goto retry;
1408 }
825f787b
TK
1409 }
1410 new_res->name = res->name;
1411 new_res->start = end + 1;
1412 new_res->end = res->end;
1413 new_res->flags = res->flags;
43ee493b 1414 new_res->desc = res->desc;
825f787b
TK
1415 new_res->parent = res->parent;
1416 new_res->sibling = res->sibling;
1417 new_res->child = NULL;
1418
ec62d04e
DH
1419 if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1420 start - res->start)))
825f787b
TK
1421 break;
1422 res->sibling = new_res;
1423 new_res = NULL;
1424 }
1425
1426 break;
1427 }
1428
1429 write_unlock(&resource_lock);
ebff7d8f 1430 free_resource(new_res);
825f787b
TK
1431}
1432#endif /* CONFIG_MEMORY_HOTREMOVE */
1433
9ca6551e
DH
1434#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
3be8da57 1444/**
9ca6551e 1445 * merge_system_ram_resource - mark the System RAM resource mergeable and try to
3be8da57 1446 * merge it with adjacent, mergeable resources
9ca6551e
DH
1447 * @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
9ac7849e
TH
1494/*
1495 * Managed region resource
1496 */
8d38821c
TR
1497static 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
9ac7849e
TH
1567struct 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
b69c2e20
BP
1588struct resource *
1589__devm_request_region(struct device *dev, struct resource *parent,
1590 resource_size_t start, resource_size_t n, const char *name)
9ac7849e
TH
1591{
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
e8de1481 1604 res = __request_region(parent, start, n, name, 0);
9ac7849e
TH
1605 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
1da177e4 1625/*
ffd2e8df 1626 * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1da177e4
LT
1627 */
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 (;;) {
8bc1ad7d 1635 unsigned int io_start, io_num;
1da177e4 1636 int x = reserved;
ffd2e8df 1637 struct resource *parent;
1da177e4 1638
ffd2e8df 1639 if (get_option(&str, &io_start) != 2)
1da177e4 1640 break;
ffd2e8df 1641 if (get_option(&str, &io_num) == 0)
1da177e4
LT
1642 break;
1643 if (x < MAXRESERVE) {
1644 struct resource *res = reserve + x;
ffd2e8df
BH
1645
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 }
1da177e4
LT
1657 res->name = "reserved";
1658 res->start = io_start;
1659 res->end = io_start + io_num - 1;
ffd2e8df 1660 res->flags |= IORESOURCE_BUSY;
43ee493b 1661 res->desc = IORES_DESC_NONE;
1da177e4 1662 res->child = NULL;
ffd2e8df 1663 if (request_resource(parent, res) == 0)
1da177e4
LT
1664 reserved = x+1;
1665 }
1666 }
1667 return 1;
1668}
1da177e4 1669__setup("reserve=", reserve_setup);
379daf62
SS
1670
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;
d68612b2
SS
1691 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1692 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
379daf62 1693 continue;
3ac52669
AV
1694 /*
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
e4c72966 1703 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
13eb8375
IM
1704 (unsigned long long)addr,
1705 (unsigned long long)(addr + size - 1),
e4c72966 1706 p->name, p);
379daf62
SS
1707 err = -1;
1708 break;
1709 }
1710 read_unlock(&resource_lock);
1711
1712 return err;
1713}
e8de1481
AV
1714
1715#ifdef CONFIG_STRICT_DEVMEM
1716static int strict_iomem_checks = 1;
1717#else
1718static int strict_iomem_checks;
1719#endif
1720
1721/*
a9e7b8d4
DH
1722 * 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.
e8de1481 1726 */
9825b451 1727bool iomem_is_exclusive(u64 addr)
e8de1481 1728{
a9e7b8d4
DH
1729 const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM |
1730 IORESOURCE_EXCLUSIVE;
b78dfa05 1731 bool skip_children = false, err = false;
e8de1481 1732 int size = PAGE_SIZE;
b78dfa05 1733 struct resource *p;
e8de1481 1734
e8de1481
AV
1735 addr = addr & PAGE_MASK;
1736
1737 read_lock(&resource_lock);
b78dfa05 1738 for_each_resource(&iomem_resource, p, skip_children) {
e8de1481
AV
1739 if (p->start >= addr + size)
1740 break;
b78dfa05
DH
1741 if (p->end < addr) {
1742 skip_children = true;
e8de1481 1743 continue;
b78dfa05
DH
1744 }
1745 skip_children = false;
1746
a9e7b8d4
DH
1747 /*
1748 * 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
90a545e9
DW
1759 /*
1760 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1761 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1762 * resource is busy.
1763 */
a9e7b8d4 1764 if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY))
90a545e9
DW
1765 continue;
1766 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1767 || p->flags & IORESOURCE_EXCLUSIVE) {
9825b451 1768 err = true;
e8de1481
AV
1769 break;
1770 }
1771 }
1772 read_unlock(&resource_lock);
1773
1774 return err;
1775}
1776
90e97820
JL
1777struct 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
0092908d 1801#ifdef CONFIG_DEVICE_PRIVATE
0c385190
CH
1802static struct resource *__request_free_mem_region(struct device *dev,
1803 struct resource *base, unsigned long size, const char *name)
0092908d
CH
1804{
1805 resource_size_t end, addr;
1806 struct resource *res;
56fd9491 1807 struct region_devres *dr = NULL;
0092908d
CH
1808
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
56fd9491
AP
1813 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);
0092908d 1827 for (; addr > size && addr >= base->start; addr -= size) {
56fd9491 1828 if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
0092908d
CH
1829 REGION_DISJOINT)
1830 continue;
1831
eb1f065f 1832 if (__request_region_locked(res, &iomem_resource, addr, size,
56fd9491
AP
1833 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
0092908d 1843 res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
56fd9491
AP
1844 write_unlock(&resource_lock);
1845
1846 /*
1847 * A driver is claiming this region so revoke any mappings.
1848 */
1849 revoke_iomem(res);
0092908d
CH
1850 return res;
1851 }
56fd9491
AP
1852 write_unlock(&resource_lock);
1853
1854 free_resource(res);
1855 if (dr)
1856 devres_free(dr);
0092908d
CH
1857
1858 return ERR_PTR(-ERANGE);
1859}
0c385190
CH
1860
1861/**
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{
1875 return __request_free_mem_region(dev, base, size, dev_name(dev));
1876}
0092908d 1877EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
0c385190
CH
1878
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
0092908d
CH
1886#endif /* CONFIG_DEVICE_PRIVATE */
1887
e8de1481
AV
1888static 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
71a1d8ed
DV
1897static 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
e8de1481 1941__setup("iomem=", strict_iomem);