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