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