1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/kernel/resource.c
4 *
5 * Copyright (C) 1999 Linus Torvalds
6 * Copyright (C) 1999 Martin Mares <mj@ucw.cz>
7 *
8 * Arbitrary resource management.
9 */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/export.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/fs.h>
20 #include <linux/proc_fs.h>
21 #include <linux/pseudo_fs.h>
22 #include <linux/sched.h>
23 #include <linux/seq_file.h>
24 #include <linux/device.h>
25 #include <linux/pfn.h>
26 #include <linux/mm.h>
27 #include <linux/mount.h>
28 #include <linux/resource_ext.h>
29 #include <uapi/linux/magic.h>
30 #include <asm/io.h>
31
32
33 struct resource ioport_resource = {
34 .name = "PCI IO",
35 .start = 0,
36 .end = IO_SPACE_LIMIT,
37 .flags = IORESOURCE_IO,
38 };
39 EXPORT_SYMBOL(ioport_resource);
40
41 struct resource iomem_resource = {
42 .name = "PCI mem",
43 .start = 0,
44 .end = -1,
45 .flags = IORESOURCE_MEM,
46 };
47 EXPORT_SYMBOL(iomem_resource);
48
49 /* constraints to be met while allocating resources */
50 struct resource_constraint {
51 resource_size_t min, max, align;
52 resource_size_t (*alignf)(void *, const struct resource *,
53 resource_size_t, resource_size_t);
54 void *alignf_data;
55 };
56
57 static DEFINE_RWLOCK(resource_lock);
58
next_resource(struct resource * p)59 static struct resource *next_resource(struct resource *p)
60 {
61 if (p->child)
62 return p->child;
63 while (!p->sibling && p->parent)
64 p = p->parent;
65 return p->sibling;
66 }
67
r_next(struct seq_file * m,void * v,loff_t * pos)68 static void *r_next(struct seq_file *m, void *v, loff_t *pos)
69 {
70 struct resource *p = v;
71 (*pos)++;
72 return (void *)next_resource(p);
73 }
74
75 #ifdef CONFIG_PROC_FS
76
77 enum { MAX_IORES_LEVEL = 5 };
78
r_start(struct seq_file * m,loff_t * pos)79 static void *r_start(struct seq_file *m, loff_t *pos)
80 __acquires(resource_lock)
81 {
82 struct resource *p = PDE_DATA(file_inode(m->file));
83 loff_t l = 0;
84 read_lock(&resource_lock);
85 for (p = p->child; p && l < *pos; p = r_next(m, p, &l))
86 ;
87 return p;
88 }
89
r_stop(struct seq_file * m,void * v)90 static void r_stop(struct seq_file *m, void *v)
91 __releases(resource_lock)
92 {
93 read_unlock(&resource_lock);
94 }
95
r_show(struct seq_file * m,void * v)96 static int r_show(struct seq_file *m, void *v)
97 {
98 struct resource *root = PDE_DATA(file_inode(m->file));
99 struct resource *r = v, *p;
100 unsigned long long start, end;
101 int width = root->end < 0x10000 ? 4 : 8;
102 int depth;
103
104 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent)
105 if (p->parent == root)
106 break;
107
108 if (file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) {
109 start = r->start;
110 end = r->end;
111 } else {
112 start = end = 0;
113 }
114
115 seq_printf(m, "%*s%0*llx-%0*llx : %s\n",
116 depth * 2, "",
117 width, start,
118 width, end,
119 r->name ? r->name : "<BAD>");
120 return 0;
121 }
122
123 static const struct seq_operations resource_op = {
124 .start = r_start,
125 .next = r_next,
126 .stop = r_stop,
127 .show = r_show,
128 };
129
ioresources_init(void)130 static int __init ioresources_init(void)
131 {
132 proc_create_seq_data("ioports", 0, NULL, &resource_op,
133 &ioport_resource);
134 proc_create_seq_data("iomem", 0, NULL, &resource_op, &iomem_resource);
135 return 0;
136 }
137 __initcall(ioresources_init);
138
139 #endif /* CONFIG_PROC_FS */
140
free_resource(struct resource * res)141 static void free_resource(struct resource *res)
142 {
143 /**
144 * If the resource was allocated using memblock early during boot
145 * we'll leak it here: we can only return full pages back to the
146 * buddy and trying to be smart and reusing them eventually in
147 * alloc_resource() overcomplicates resource handling.
148 */
149 if (res && PageSlab(virt_to_head_page(res)))
150 kfree(res);
151 }
152
alloc_resource(gfp_t flags)153 static struct resource *alloc_resource(gfp_t flags)
154 {
155 return kzalloc(sizeof(struct resource), flags);
156 }
157
158 /* Return the conflict entry if you can't request it */
__request_resource(struct resource * root,struct resource * new)159 static struct resource * __request_resource(struct resource *root, struct resource *new)
160 {
161 resource_size_t start = new->start;
162 resource_size_t end = new->end;
163 struct resource *tmp, **p;
164
165 if (end < start)
166 return root;
167 if (start < root->start)
168 return root;
169 if (end > root->end)
170 return root;
171 p = &root->child;
172 for (;;) {
173 tmp = *p;
174 if (!tmp || tmp->start > end) {
175 new->sibling = tmp;
176 *p = new;
177 new->parent = root;
178 return NULL;
179 }
180 p = &tmp->sibling;
181 if (tmp->end < start)
182 continue;
183 return tmp;
184 }
185 }
186
__release_resource(struct resource * old,bool release_child)187 static int __release_resource(struct resource *old, bool release_child)
188 {
189 struct resource *tmp, **p, *chd;
190
191 p = &old->parent->child;
192 for (;;) {
193 tmp = *p;
194 if (!tmp)
195 break;
196 if (tmp == old) {
197 if (release_child || !(tmp->child)) {
198 *p = tmp->sibling;
199 } else {
200 for (chd = tmp->child;; chd = chd->sibling) {
201 chd->parent = tmp->parent;
202 if (!(chd->sibling))
203 break;
204 }
205 *p = tmp->child;
206 chd->sibling = tmp->sibling;
207 }
208 old->parent = NULL;
209 return 0;
210 }
211 p = &tmp->sibling;
212 }
213 return -EINVAL;
214 }
215
__release_child_resources(struct resource * r)216 static void __release_child_resources(struct resource *r)
217 {
218 struct resource *tmp, *p;
219 resource_size_t size;
220
221 p = r->child;
222 r->child = NULL;
223 while (p) {
224 tmp = p;
225 p = p->sibling;
226
227 tmp->parent = NULL;
228 tmp->sibling = NULL;
229 __release_child_resources(tmp);
230
231 printk(KERN_DEBUG "release child resource %pR\n", tmp);
232 /* need to restore size, and keep flags */
233 size = resource_size(tmp);
234 tmp->start = 0;
235 tmp->end = size - 1;
236 }
237 }
238
release_child_resources(struct resource * r)239 void release_child_resources(struct resource *r)
240 {
241 write_lock(&resource_lock);
242 __release_child_resources(r);
243 write_unlock(&resource_lock);
244 }
245
246 /**
247 * request_resource_conflict - request and reserve an I/O or memory resource
248 * @root: root resource descriptor
249 * @new: resource descriptor desired by caller
250 *
251 * Returns 0 for success, conflict resource on error.
252 */
request_resource_conflict(struct resource * root,struct resource * new)253 struct resource *request_resource_conflict(struct resource *root, struct resource *new)
254 {
255 struct resource *conflict;
256
257 write_lock(&resource_lock);
258 conflict = __request_resource(root, new);
259 write_unlock(&resource_lock);
260 return conflict;
261 }
262
263 /**
264 * request_resource - request and reserve an I/O or memory resource
265 * @root: root resource descriptor
266 * @new: resource descriptor desired by caller
267 *
268 * Returns 0 for success, negative error code on error.
269 */
request_resource(struct resource * root,struct resource * new)270 int request_resource(struct resource *root, struct resource *new)
271 {
272 struct resource *conflict;
273
274 conflict = request_resource_conflict(root, new);
275 return conflict ? -EBUSY : 0;
276 }
277
278 EXPORT_SYMBOL(request_resource);
279
280 /**
281 * release_resource - release a previously reserved resource
282 * @old: resource pointer
283 */
release_resource(struct resource * old)284 int release_resource(struct resource *old)
285 {
286 int retval;
287
288 write_lock(&resource_lock);
289 retval = __release_resource(old, true);
290 write_unlock(&resource_lock);
291 return retval;
292 }
293
294 EXPORT_SYMBOL(release_resource);
295
296 /**
297 * find_next_iomem_res - Finds the lowest iomem resource that covers part of
298 * [@start..@end].
299 *
300 * If a resource is found, returns 0 and @*res is overwritten with the part
301 * of the resource that's within [@start..@end]; if none is found, returns
302 * -ENODEV. Returns -EINVAL for invalid parameters.
303 *
304 * @start: start address of the resource searched for
305 * @end: end address of same resource
306 * @flags: flags which the resource must have
307 * @desc: descriptor the resource must have
308 * @res: return ptr, if resource found
309 *
310 * The caller must specify @start, @end, @flags, and @desc
311 * (which may be IORES_DESC_NONE).
312 */
find_next_iomem_res(resource_size_t start,resource_size_t end,unsigned long flags,unsigned long desc,struct resource * res)313 static int find_next_iomem_res(resource_size_t start, resource_size_t end,
314 unsigned long flags, unsigned long desc,
315 struct resource *res)
316 {
317 struct resource *p;
318
319 if (!res)
320 return -EINVAL;
321
322 if (start >= end)
323 return -EINVAL;
324
325 read_lock(&resource_lock);
326
327 for (p = iomem_resource.child; p; p = next_resource(p)) {
328 /* If we passed the resource we are looking for, stop */
329 if (p->start > end) {
330 p = NULL;
331 break;
332 }
333
334 /* Skip until we find a range that matches what we look for */
335 if (p->end < start)
336 continue;
337
338 if ((p->flags & flags) != flags)
339 continue;
340 if ((desc != IORES_DESC_NONE) && (desc != p->desc))
341 continue;
342
343 /* Found a match, break */
344 break;
345 }
346
347 if (p) {
348 /* copy data */
349 *res = (struct resource) {
350 .start = max(start, p->start),
351 .end = min(end, p->end),
352 .flags = p->flags,
353 .desc = p->desc,
354 .parent = p->parent,
355 };
356 }
357
358 read_unlock(&resource_lock);
359 return p ? 0 : -ENODEV;
360 }
361
__walk_iomem_res_desc(resource_size_t start,resource_size_t end,unsigned long flags,unsigned long desc,void * arg,int (* func)(struct resource *,void *))362 static int __walk_iomem_res_desc(resource_size_t start, resource_size_t end,
363 unsigned long flags, unsigned long desc,
364 void *arg,
365 int (*func)(struct resource *, void *))
366 {
367 struct resource res;
368 int ret = -EINVAL;
369
370 while (start < end &&
371 !find_next_iomem_res(start, end, flags, desc, &res)) {
372 ret = (*func)(&res, arg);
373 if (ret)
374 break;
375
376 start = res.end + 1;
377 }
378
379 return ret;
380 }
381
382 /**
383 * walk_iomem_res_desc - Walks through iomem resources and calls func()
384 * with matching resource ranges.
385 * *
386 * @desc: I/O resource descriptor. Use IORES_DESC_NONE to skip @desc check.
387 * @flags: I/O resource flags
388 * @start: start addr
389 * @end: end addr
390 * @arg: function argument for the callback @func
391 * @func: callback function that is called for each qualifying resource area
392 *
393 * All the memory ranges which overlap start,end and also match flags and
394 * desc are valid candidates.
395 *
396 * NOTE: For a new descriptor search, define a new IORES_DESC in
397 * <linux/ioport.h> and set it in 'desc' of a target resource entry.
398 */
walk_iomem_res_desc(unsigned long desc,unsigned long flags,u64 start,u64 end,void * arg,int (* func)(struct resource *,void *))399 int walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start,
400 u64 end, void *arg, int (*func)(struct resource *, void *))
401 {
402 return __walk_iomem_res_desc(start, end, flags, desc, arg, func);
403 }
404 EXPORT_SYMBOL_GPL(walk_iomem_res_desc);
405
406 /*
407 * This function calls the @func callback against all memory ranges of type
408 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
409 * Now, this function is only for System RAM, it deals with full ranges and
410 * not PFNs. If resources are not PFN-aligned, dealing with PFNs can truncate
411 * ranges.
412 */
walk_system_ram_res(u64 start,u64 end,void * arg,int (* func)(struct resource *,void *))413 int walk_system_ram_res(u64 start, u64 end, void *arg,
414 int (*func)(struct resource *, void *))
415 {
416 unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
417
418 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
419 func);
420 }
421
422 /*
423 * This function calls the @func callback against all memory ranges, which
424 * are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
425 */
walk_mem_res(u64 start,u64 end,void * arg,int (* func)(struct resource *,void *))426 int walk_mem_res(u64 start, u64 end, void *arg,
427 int (*func)(struct resource *, void *))
428 {
429 unsigned long flags = IORESOURCE_MEM | IORESOURCE_BUSY;
430
431 return __walk_iomem_res_desc(start, end, flags, IORES_DESC_NONE, arg,
432 func);
433 }
434
435 /*
436 * This function calls the @func callback against all memory ranges of type
437 * System RAM which are marked as IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY.
438 * It is to be used only for System RAM.
439 */
walk_system_ram_range(unsigned long start_pfn,unsigned long nr_pages,void * arg,int (* func)(unsigned long,unsigned long,void *))440 int walk_system_ram_range(unsigned long start_pfn, unsigned long nr_pages,
441 void *arg, int (*func)(unsigned long, unsigned long, void *))
442 {
443 resource_size_t start, end;
444 unsigned long flags;
445 struct resource res;
446 unsigned long pfn, end_pfn;
447 int ret = -EINVAL;
448
449 start = (u64) start_pfn << PAGE_SHIFT;
450 end = ((u64)(start_pfn + nr_pages) << PAGE_SHIFT) - 1;
451 flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
452 while (start < end &&
453 !find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res)) {
454 pfn = PFN_UP(res.start);
455 end_pfn = PFN_DOWN(res.end + 1);
456 if (end_pfn > pfn)
457 ret = (*func)(pfn, end_pfn - pfn, arg);
458 if (ret)
459 break;
460 start = res.end + 1;
461 }
462 return ret;
463 }
464
__is_ram(unsigned long pfn,unsigned long nr_pages,void * arg)465 static int __is_ram(unsigned long pfn, unsigned long nr_pages, void *arg)
466 {
467 return 1;
468 }
469
470 /*
471 * This generic page_is_ram() returns true if specified address is
472 * registered as System RAM in iomem_resource list.
473 */
page_is_ram(unsigned long pfn)474 int __weak page_is_ram(unsigned long pfn)
475 {
476 return walk_system_ram_range(pfn, 1, NULL, __is_ram) == 1;
477 }
478 EXPORT_SYMBOL_GPL(page_is_ram);
479
__region_intersects(resource_size_t start,size_t size,unsigned long flags,unsigned long desc)480 static int __region_intersects(resource_size_t start, size_t size,
481 unsigned long flags, unsigned long desc)
482 {
483 struct resource res;
484 int type = 0; int other = 0;
485 struct resource *p;
486
487 res.start = start;
488 res.end = start + size - 1;
489
490 for (p = iomem_resource.child; p ; p = p->sibling) {
491 bool is_type = (((p->flags & flags) == flags) &&
492 ((desc == IORES_DESC_NONE) ||
493 (desc == p->desc)));
494
495 if (resource_overlaps(p, &res))
496 is_type ? type++ : other++;
497 }
498
499 if (type == 0)
500 return REGION_DISJOINT;
501
502 if (other == 0)
503 return REGION_INTERSECTS;
504
505 return REGION_MIXED;
506 }
507
508 /**
509 * region_intersects() - determine intersection of region with known resources
510 * @start: region start address
511 * @size: size of region
512 * @flags: flags of resource (in iomem_resource)
513 * @desc: descriptor of resource (in iomem_resource) or IORES_DESC_NONE
514 *
515 * Check if the specified region partially overlaps or fully eclipses a
516 * resource identified by @flags and @desc (optional with IORES_DESC_NONE).
517 * Return REGION_DISJOINT if the region does not overlap @flags/@desc,
518 * return REGION_MIXED if the region overlaps @flags/@desc and another
519 * resource, and return REGION_INTERSECTS if the region overlaps @flags/@desc
520 * and no other defined resource. Note that REGION_INTERSECTS is also
521 * returned in the case when the specified region overlaps RAM and undefined
522 * memory holes.
523 *
524 * region_intersect() is used by memory remapping functions to ensure
525 * the user is not remapping RAM and is a vast speed up over walking
526 * through the resource table page by page.
527 */
region_intersects(resource_size_t start,size_t size,unsigned long flags,unsigned long desc)528 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
529 unsigned long desc)
530 {
531 int ret;
532
533 read_lock(&resource_lock);
534 ret = __region_intersects(start, size, flags, desc);
535 read_unlock(&resource_lock);
536
537 return ret;
538 }
539 EXPORT_SYMBOL_GPL(region_intersects);
540
arch_remove_reservations(struct resource * avail)541 void __weak arch_remove_reservations(struct resource *avail)
542 {
543 }
544
simple_align_resource(void * data,const struct resource * avail,resource_size_t size,resource_size_t align)545 static resource_size_t simple_align_resource(void *data,
546 const struct resource *avail,
547 resource_size_t size,
548 resource_size_t align)
549 {
550 return avail->start;
551 }
552
resource_clip(struct resource * res,resource_size_t min,resource_size_t max)553 static void resource_clip(struct resource *res, resource_size_t min,
554 resource_size_t max)
555 {
556 if (res->start < min)
557 res->start = min;
558 if (res->end > max)
559 res->end = max;
560 }
561
562 /*
563 * Find empty slot in the resource tree with the given range and
564 * alignment constraints
565 */
__find_resource(struct resource * root,struct resource * old,struct resource * new,resource_size_t size,struct resource_constraint * constraint)566 static int __find_resource(struct resource *root, struct resource *old,
567 struct resource *new,
568 resource_size_t size,
569 struct resource_constraint *constraint)
570 {
571 struct resource *this = root->child;
572 struct resource tmp = *new, avail, alloc;
573
574 tmp.start = root->start;
575 /*
576 * Skip past an allocated resource that starts at 0, since the assignment
577 * of this->start - 1 to tmp->end below would cause an underflow.
578 */
579 if (this && this->start == root->start) {
580 tmp.start = (this == old) ? old->start : this->end + 1;
581 this = this->sibling;
582 }
583 for(;;) {
584 if (this)
585 tmp.end = (this == old) ? this->end : this->start - 1;
586 else
587 tmp.end = root->end;
588
589 if (tmp.end < tmp.start)
590 goto next;
591
592 resource_clip(&tmp, constraint->min, constraint->max);
593 arch_remove_reservations(&tmp);
594
595 /* Check for overflow after ALIGN() */
596 avail.start = ALIGN(tmp.start, constraint->align);
597 avail.end = tmp.end;
598 avail.flags = new->flags & ~IORESOURCE_UNSET;
599 if (avail.start >= tmp.start) {
600 alloc.flags = avail.flags;
601 alloc.start = constraint->alignf(constraint->alignf_data, &avail,
602 size, constraint->align);
603 alloc.end = alloc.start + size - 1;
604 if (alloc.start <= alloc.end &&
605 resource_contains(&avail, &alloc)) {
606 new->start = alloc.start;
607 new->end = alloc.end;
608 return 0;
609 }
610 }
611
612 next: if (!this || this->end == root->end)
613 break;
614
615 if (this != old)
616 tmp.start = this->end + 1;
617 this = this->sibling;
618 }
619 return -EBUSY;
620 }
621
622 /*
623 * Find empty slot in the resource tree given range and alignment.
624 */
find_resource(struct resource * root,struct resource * new,resource_size_t size,struct resource_constraint * constraint)625 static int find_resource(struct resource *root, struct resource *new,
626 resource_size_t size,
627 struct resource_constraint *constraint)
628 {
629 return __find_resource(root, NULL, new, size, constraint);
630 }
631
632 /**
633 * reallocate_resource - allocate a slot in the resource tree given range & alignment.
634 * The resource will be relocated if the new size cannot be reallocated in the
635 * current location.
636 *
637 * @root: root resource descriptor
638 * @old: resource descriptor desired by caller
639 * @newsize: new size of the resource descriptor
640 * @constraint: the size and alignment constraints to be met.
641 */
reallocate_resource(struct resource * root,struct resource * old,resource_size_t newsize,struct resource_constraint * constraint)642 static int reallocate_resource(struct resource *root, struct resource *old,
643 resource_size_t newsize,
644 struct resource_constraint *constraint)
645 {
646 int err=0;
647 struct resource new = *old;
648 struct resource *conflict;
649
650 write_lock(&resource_lock);
651
652 if ((err = __find_resource(root, old, &new, newsize, constraint)))
653 goto out;
654
655 if (resource_contains(&new, old)) {
656 old->start = new.start;
657 old->end = new.end;
658 goto out;
659 }
660
661 if (old->child) {
662 err = -EBUSY;
663 goto out;
664 }
665
666 if (resource_contains(old, &new)) {
667 old->start = new.start;
668 old->end = new.end;
669 } else {
670 __release_resource(old, true);
671 *old = new;
672 conflict = __request_resource(root, old);
673 BUG_ON(conflict);
674 }
675 out:
676 write_unlock(&resource_lock);
677 return err;
678 }
679
680
681 /**
682 * allocate_resource - allocate empty slot in the resource tree given range & alignment.
683 * The resource will be reallocated with a new size if it was already allocated
684 * @root: root resource descriptor
685 * @new: resource descriptor desired by caller
686 * @size: requested resource region size
687 * @min: minimum boundary to allocate
688 * @max: maximum boundary to allocate
689 * @align: alignment requested, in bytes
690 * @alignf: alignment function, optional, called if not NULL
691 * @alignf_data: arbitrary data to pass to the @alignf function
692 */
allocate_resource(struct resource * root,struct resource * new,resource_size_t size,resource_size_t min,resource_size_t max,resource_size_t align,resource_size_t (* alignf)(void *,const struct resource *,resource_size_t,resource_size_t),void * alignf_data)693 int allocate_resource(struct resource *root, struct resource *new,
694 resource_size_t size, resource_size_t min,
695 resource_size_t max, resource_size_t align,
696 resource_size_t (*alignf)(void *,
697 const struct resource *,
698 resource_size_t,
699 resource_size_t),
700 void *alignf_data)
701 {
702 int err;
703 struct resource_constraint constraint;
704
705 if (!alignf)
706 alignf = simple_align_resource;
707
708 constraint.min = min;
709 constraint.max = max;
710 constraint.align = align;
711 constraint.alignf = alignf;
712 constraint.alignf_data = alignf_data;
713
714 if ( new->parent ) {
715 /* resource is already allocated, try reallocating with
716 the new constraints */
717 return reallocate_resource(root, new, size, &constraint);
718 }
719
720 write_lock(&resource_lock);
721 err = find_resource(root, new, size, &constraint);
722 if (err >= 0 && __request_resource(root, new))
723 err = -EBUSY;
724 write_unlock(&resource_lock);
725 return err;
726 }
727
728 EXPORT_SYMBOL(allocate_resource);
729
730 /**
731 * lookup_resource - find an existing resource by a resource start address
732 * @root: root resource descriptor
733 * @start: resource start address
734 *
735 * Returns a pointer to the resource if found, NULL otherwise
736 */
lookup_resource(struct resource * root,resource_size_t start)737 struct resource *lookup_resource(struct resource *root, resource_size_t start)
738 {
739 struct resource *res;
740
741 read_lock(&resource_lock);
742 for (res = root->child; res; res = res->sibling) {
743 if (res->start == start)
744 break;
745 }
746 read_unlock(&resource_lock);
747
748 return res;
749 }
750
751 /*
752 * Insert a resource into the resource tree. If successful, return NULL,
753 * otherwise return the conflicting resource (compare to __request_resource())
754 */
__insert_resource(struct resource * parent,struct resource * new)755 static struct resource * __insert_resource(struct resource *parent, struct resource *new)
756 {
757 struct resource *first, *next;
758
759 for (;; parent = first) {
760 first = __request_resource(parent, new);
761 if (!first)
762 return first;
763
764 if (first == parent)
765 return first;
766 if (WARN_ON(first == new)) /* duplicated insertion */
767 return first;
768
769 if ((first->start > new->start) || (first->end < new->end))
770 break;
771 if ((first->start == new->start) && (first->end == new->end))
772 break;
773 }
774
775 for (next = first; ; next = next->sibling) {
776 /* Partial overlap? Bad, and unfixable */
777 if (next->start < new->start || next->end > new->end)
778 return next;
779 if (!next->sibling)
780 break;
781 if (next->sibling->start > new->end)
782 break;
783 }
784
785 new->parent = parent;
786 new->sibling = next->sibling;
787 new->child = first;
788
789 next->sibling = NULL;
790 for (next = first; next; next = next->sibling)
791 next->parent = new;
792
793 if (parent->child == first) {
794 parent->child = new;
795 } else {
796 next = parent->child;
797 while (next->sibling != first)
798 next = next->sibling;
799 next->sibling = new;
800 }
801 return NULL;
802 }
803
804 /**
805 * insert_resource_conflict - Inserts resource in the resource tree
806 * @parent: parent of the new resource
807 * @new: new resource to insert
808 *
809 * Returns 0 on success, conflict resource if the resource can't be inserted.
810 *
811 * This function is equivalent to request_resource_conflict when no conflict
812 * happens. If a conflict happens, and the conflicting resources
813 * entirely fit within the range of the new resource, then the new
814 * resource is inserted and the conflicting resources become children of
815 * the new resource.
816 *
817 * This function is intended for producers of resources, such as FW modules
818 * and bus drivers.
819 */
insert_resource_conflict(struct resource * parent,struct resource * new)820 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new)
821 {
822 struct resource *conflict;
823
824 write_lock(&resource_lock);
825 conflict = __insert_resource(parent, new);
826 write_unlock(&resource_lock);
827 return conflict;
828 }
829
830 /**
831 * insert_resource - Inserts a resource in the resource tree
832 * @parent: parent of the new resource
833 * @new: new resource to insert
834 *
835 * Returns 0 on success, -EBUSY if the resource can't be inserted.
836 *
837 * This function is intended for producers of resources, such as FW modules
838 * and bus drivers.
839 */
insert_resource(struct resource * parent,struct resource * new)840 int insert_resource(struct resource *parent, struct resource *new)
841 {
842 struct resource *conflict;
843
844 conflict = insert_resource_conflict(parent, new);
845 return conflict ? -EBUSY : 0;
846 }
847 EXPORT_SYMBOL_GPL(insert_resource);
848
849 /**
850 * insert_resource_expand_to_fit - Insert a resource into the resource tree
851 * @root: root resource descriptor
852 * @new: new resource to insert
853 *
854 * Insert a resource into the resource tree, possibly expanding it in order
855 * to make it encompass any conflicting resources.
856 */
insert_resource_expand_to_fit(struct resource * root,struct resource * new)857 void insert_resource_expand_to_fit(struct resource *root, struct resource *new)
858 {
859 if (new->parent)
860 return;
861
862 write_lock(&resource_lock);
863 for (;;) {
864 struct resource *conflict;
865
866 conflict = __insert_resource(root, new);
867 if (!conflict)
868 break;
869 if (conflict == root)
870 break;
871
872 /* Ok, expand resource to cover the conflict, then try again .. */
873 if (conflict->start < new->start)
874 new->start = conflict->start;
875 if (conflict->end > new->end)
876 new->end = conflict->end;
877
878 printk("Expanded resource %s due to conflict with %s\n", new->name, conflict->name);
879 }
880 write_unlock(&resource_lock);
881 }
882
883 /**
884 * remove_resource - Remove a resource in the resource tree
885 * @old: resource to remove
886 *
887 * Returns 0 on success, -EINVAL if the resource is not valid.
888 *
889 * This function removes a resource previously inserted by insert_resource()
890 * or insert_resource_conflict(), and moves the children (if any) up to
891 * where they were before. insert_resource() and insert_resource_conflict()
892 * insert a new resource, and move any conflicting resources down to the
893 * children of the new resource.
894 *
895 * insert_resource(), insert_resource_conflict() and remove_resource() are
896 * intended for producers of resources, such as FW modules and bus drivers.
897 */
remove_resource(struct resource * old)898 int remove_resource(struct resource *old)
899 {
900 int retval;
901
902 write_lock(&resource_lock);
903 retval = __release_resource(old, false);
904 write_unlock(&resource_lock);
905 return retval;
906 }
907 EXPORT_SYMBOL_GPL(remove_resource);
908
__adjust_resource(struct resource * res,resource_size_t start,resource_size_t size)909 static int __adjust_resource(struct resource *res, resource_size_t start,
910 resource_size_t size)
911 {
912 struct resource *tmp, *parent = res->parent;
913 resource_size_t end = start + size - 1;
914 int result = -EBUSY;
915
916 if (!parent)
917 goto skip;
918
919 if ((start < parent->start) || (end > parent->end))
920 goto out;
921
922 if (res->sibling && (res->sibling->start <= end))
923 goto out;
924
925 tmp = parent->child;
926 if (tmp != res) {
927 while (tmp->sibling != res)
928 tmp = tmp->sibling;
929 if (start <= tmp->end)
930 goto out;
931 }
932
933 skip:
934 for (tmp = res->child; tmp; tmp = tmp->sibling)
935 if ((tmp->start < start) || (tmp->end > end))
936 goto out;
937
938 res->start = start;
939 res->end = end;
940 result = 0;
941
942 out:
943 return result;
944 }
945
946 /**
947 * adjust_resource - modify a resource's start and size
948 * @res: resource to modify
949 * @start: new start value
950 * @size: new size
951 *
952 * Given an existing resource, change its start and size to match the
953 * arguments. Returns 0 on success, -EBUSY if it can't fit.
954 * Existing children of the resource are assumed to be immutable.
955 */
adjust_resource(struct resource * res,resource_size_t start,resource_size_t size)956 int adjust_resource(struct resource *res, resource_size_t start,
957 resource_size_t size)
958 {
959 int result;
960
961 write_lock(&resource_lock);
962 result = __adjust_resource(res, start, size);
963 write_unlock(&resource_lock);
964 return result;
965 }
966 EXPORT_SYMBOL(adjust_resource);
967
968 static void __init
__reserve_region_with_split(struct resource * root,resource_size_t start,resource_size_t end,const char * name)969 __reserve_region_with_split(struct resource *root, resource_size_t start,
970 resource_size_t end, const char *name)
971 {
972 struct resource *parent = root;
973 struct resource *conflict;
974 struct resource *res = alloc_resource(GFP_ATOMIC);
975 struct resource *next_res = NULL;
976 int type = resource_type(root);
977
978 if (!res)
979 return;
980
981 res->name = name;
982 res->start = start;
983 res->end = end;
984 res->flags = type | IORESOURCE_BUSY;
985 res->desc = IORES_DESC_NONE;
986
987 while (1) {
988
989 conflict = __request_resource(parent, res);
990 if (!conflict) {
991 if (!next_res)
992 break;
993 res = next_res;
994 next_res = NULL;
995 continue;
996 }
997
998 /* conflict covered whole area */
999 if (conflict->start <= res->start &&
1000 conflict->end >= res->end) {
1001 free_resource(res);
1002 WARN_ON(next_res);
1003 break;
1004 }
1005
1006 /* failed, split and try again */
1007 if (conflict->start > res->start) {
1008 end = res->end;
1009 res->end = conflict->start - 1;
1010 if (conflict->end < end) {
1011 next_res = alloc_resource(GFP_ATOMIC);
1012 if (!next_res) {
1013 free_resource(res);
1014 break;
1015 }
1016 next_res->name = name;
1017 next_res->start = conflict->end + 1;
1018 next_res->end = end;
1019 next_res->flags = type | IORESOURCE_BUSY;
1020 next_res->desc = IORES_DESC_NONE;
1021 }
1022 } else {
1023 res->start = conflict->end + 1;
1024 }
1025 }
1026
1027 }
1028
1029 void __init
reserve_region_with_split(struct resource * root,resource_size_t start,resource_size_t end,const char * name)1030 reserve_region_with_split(struct resource *root, resource_size_t start,
1031 resource_size_t end, const char *name)
1032 {
1033 int abort = 0;
1034
1035 write_lock(&resource_lock);
1036 if (root->start > start || root->end < end) {
1037 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n",
1038 (unsigned long long)start, (unsigned long long)end,
1039 root);
1040 if (start > root->end || end < root->start)
1041 abort = 1;
1042 else {
1043 if (end > root->end)
1044 end = root->end;
1045 if (start < root->start)
1046 start = root->start;
1047 pr_err("fixing request to [0x%llx-0x%llx]\n",
1048 (unsigned long long)start,
1049 (unsigned long long)end);
1050 }
1051 dump_stack();
1052 }
1053 if (!abort)
1054 __reserve_region_with_split(root, start, end, name);
1055 write_unlock(&resource_lock);
1056 }
1057
1058 /**
1059 * resource_alignment - calculate resource's alignment
1060 * @res: resource pointer
1061 *
1062 * Returns alignment on success, 0 (invalid alignment) on failure.
1063 */
resource_alignment(struct resource * res)1064 resource_size_t resource_alignment(struct resource *res)
1065 {
1066 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) {
1067 case IORESOURCE_SIZEALIGN:
1068 return resource_size(res);
1069 case IORESOURCE_STARTALIGN:
1070 return res->start;
1071 default:
1072 return 0;
1073 }
1074 }
1075
1076 /*
1077 * This is compatibility stuff for IO resources.
1078 *
1079 * Note how this, unlike the above, knows about
1080 * the IO flag meanings (busy etc).
1081 *
1082 * request_region creates a new busy region.
1083 *
1084 * release_region releases a matching busy region.
1085 */
1086
1087 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait);
1088
1089 static struct inode *iomem_inode;
1090
1091 #ifdef CONFIG_IO_STRICT_DEVMEM
revoke_iomem(struct resource * res)1092 static void revoke_iomem(struct resource *res)
1093 {
1094 /* pairs with smp_store_release() in iomem_init_inode() */
1095 struct inode *inode = smp_load_acquire(&iomem_inode);
1096
1097 /*
1098 * Check that the initialization has completed. Losing the race
1099 * is ok because it means drivers are claiming resources before
1100 * the fs_initcall level of init and prevent iomem_get_mapping users
1101 * from establishing mappings.
1102 */
1103 if (!inode)
1104 return;
1105
1106 /*
1107 * The expectation is that the driver has successfully marked
1108 * the resource busy by this point, so devmem_is_allowed()
1109 * should start returning false, however for performance this
1110 * does not iterate the entire resource range.
1111 */
1112 if (devmem_is_allowed(PHYS_PFN(res->start)) &&
1113 devmem_is_allowed(PHYS_PFN(res->end))) {
1114 /*
1115 * *cringe* iomem=relaxed says "go ahead, what's the
1116 * worst that can happen?"
1117 */
1118 return;
1119 }
1120
1121 unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1);
1122 }
1123 #else
revoke_iomem(struct resource * res)1124 static void revoke_iomem(struct resource *res) {}
1125 #endif
1126
iomem_get_mapping(void)1127 struct address_space *iomem_get_mapping(void)
1128 {
1129 /*
1130 * This function is only called from file open paths, hence guaranteed
1131 * that fs_initcalls have completed and no need to check for NULL. But
1132 * since revoke_iomem can be called before the initcall we still need
1133 * the barrier to appease checkers.
1134 */
1135 return smp_load_acquire(&iomem_inode)->i_mapping;
1136 }
1137
__request_region_locked(struct resource * res,struct resource * parent,resource_size_t start,resource_size_t n,const char * name,int flags)1138 static int __request_region_locked(struct resource *res, struct resource *parent,
1139 resource_size_t start, resource_size_t n,
1140 const char *name, int flags)
1141 {
1142 DECLARE_WAITQUEUE(wait, current);
1143
1144 res->name = name;
1145 res->start = start;
1146 res->end = start + n - 1;
1147
1148 for (;;) {
1149 struct resource *conflict;
1150
1151 res->flags = resource_type(parent) | resource_ext_type(parent);
1152 res->flags |= IORESOURCE_BUSY | flags;
1153 res->desc = parent->desc;
1154
1155 conflict = __request_resource(parent, res);
1156 if (!conflict)
1157 break;
1158 /*
1159 * mm/hmm.c reserves physical addresses which then
1160 * become unavailable to other users. Conflicts are
1161 * not expected. Warn to aid debugging if encountered.
1162 */
1163 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
1164 pr_warn("Unaddressable device %s %pR conflicts with %pR",
1165 conflict->name, conflict, res);
1166 }
1167 if (conflict != parent) {
1168 if (!(conflict->flags & IORESOURCE_BUSY)) {
1169 parent = conflict;
1170 continue;
1171 }
1172 }
1173 if (conflict->flags & flags & IORESOURCE_MUXED) {
1174 add_wait_queue(&muxed_resource_wait, &wait);
1175 write_unlock(&resource_lock);
1176 set_current_state(TASK_UNINTERRUPTIBLE);
1177 schedule();
1178 remove_wait_queue(&muxed_resource_wait, &wait);
1179 write_lock(&resource_lock);
1180 continue;
1181 }
1182 /* Uhhuh, that didn't work out.. */
1183 return -EBUSY;
1184 }
1185
1186 return 0;
1187 }
1188
1189 /**
1190 * __request_region - create a new busy resource region
1191 * @parent: parent resource descriptor
1192 * @start: resource start address
1193 * @n: resource region size
1194 * @name: reserving caller's ID string
1195 * @flags: IO resource flags
1196 */
__request_region(struct resource * parent,resource_size_t start,resource_size_t n,const char * name,int flags)1197 struct resource *__request_region(struct resource *parent,
1198 resource_size_t start, resource_size_t n,
1199 const char *name, int flags)
1200 {
1201 struct resource *res = alloc_resource(GFP_KERNEL);
1202 int ret;
1203
1204 if (!res)
1205 return NULL;
1206
1207 write_lock(&resource_lock);
1208 ret = __request_region_locked(res, parent, start, n, name, flags);
1209 write_unlock(&resource_lock);
1210
1211 if (ret) {
1212 free_resource(res);
1213 return NULL;
1214 }
1215
1216 if (parent == &iomem_resource)
1217 revoke_iomem(res);
1218
1219 return res;
1220 }
1221 EXPORT_SYMBOL(__request_region);
1222
1223 /**
1224 * __release_region - release a previously reserved resource region
1225 * @parent: parent resource descriptor
1226 * @start: resource start address
1227 * @n: resource region size
1228 *
1229 * The described resource region must match a currently busy region.
1230 */
__release_region(struct resource * parent,resource_size_t start,resource_size_t n)1231 void __release_region(struct resource *parent, resource_size_t start,
1232 resource_size_t n)
1233 {
1234 struct resource **p;
1235 resource_size_t end;
1236
1237 p = &parent->child;
1238 end = start + n - 1;
1239
1240 write_lock(&resource_lock);
1241
1242 for (;;) {
1243 struct resource *res = *p;
1244
1245 if (!res)
1246 break;
1247 if (res->start <= start && res->end >= end) {
1248 if (!(res->flags & IORESOURCE_BUSY)) {
1249 p = &res->child;
1250 continue;
1251 }
1252 if (res->start != start || res->end != end)
1253 break;
1254 *p = res->sibling;
1255 write_unlock(&resource_lock);
1256 if (res->flags & IORESOURCE_MUXED)
1257 wake_up(&muxed_resource_wait);
1258 free_resource(res);
1259 return;
1260 }
1261 p = &res->sibling;
1262 }
1263
1264 write_unlock(&resource_lock);
1265
1266 printk(KERN_WARNING "Trying to free nonexistent resource "
1267 "<%016llx-%016llx>\n", (unsigned long long)start,
1268 (unsigned long long)end);
1269 }
1270 EXPORT_SYMBOL(__release_region);
1271
1272 #ifdef CONFIG_MEMORY_HOTREMOVE
1273 /**
1274 * release_mem_region_adjustable - release a previously reserved memory region
1275 * @start: resource start address
1276 * @size: resource region size
1277 *
1278 * This interface is intended for memory hot-delete. The requested region
1279 * is released from a currently busy memory resource. The requested region
1280 * must either match exactly or fit into a single busy resource entry. In
1281 * the latter case, the remaining resource is adjusted accordingly.
1282 * Existing children of the busy memory resource must be immutable in the
1283 * request.
1284 *
1285 * Note:
1286 * - Additional release conditions, such as overlapping region, can be
1287 * supported after they are confirmed as valid cases.
1288 * - When a busy memory resource gets split into two entries, the code
1289 * assumes that all children remain in the lower address entry for
1290 * simplicity. Enhance this logic when necessary.
1291 */
release_mem_region_adjustable(resource_size_t start,resource_size_t size)1292 void release_mem_region_adjustable(resource_size_t start, resource_size_t size)
1293 {
1294 struct resource *parent = &iomem_resource;
1295 struct resource *new_res = NULL;
1296 bool alloc_nofail = false;
1297 struct resource **p;
1298 struct resource *res;
1299 resource_size_t end;
1300
1301 end = start + size - 1;
1302 if (WARN_ON_ONCE((start < parent->start) || (end > parent->end)))
1303 return;
1304
1305 /*
1306 * We free up quite a lot of memory on memory hotunplug (esp., memap),
1307 * just before releasing the region. This is highly unlikely to
1308 * fail - let's play save and make it never fail as the caller cannot
1309 * perform any error handling (e.g., trying to re-add memory will fail
1310 * similarly).
1311 */
1312 retry:
1313 new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0));
1314
1315 p = &parent->child;
1316 write_lock(&resource_lock);
1317
1318 while ((res = *p)) {
1319 if (res->start >= end)
1320 break;
1321
1322 /* look for the next resource if it does not fit into */
1323 if (res->start > start || res->end < end) {
1324 p = &res->sibling;
1325 continue;
1326 }
1327
1328 if (!(res->flags & IORESOURCE_MEM))
1329 break;
1330
1331 if (!(res->flags & IORESOURCE_BUSY)) {
1332 p = &res->child;
1333 continue;
1334 }
1335
1336 /* found the target resource; let's adjust accordingly */
1337 if (res->start == start && res->end == end) {
1338 /* free the whole entry */
1339 *p = res->sibling;
1340 free_resource(res);
1341 } else if (res->start == start && res->end != end) {
1342 /* adjust the start */
1343 WARN_ON_ONCE(__adjust_resource(res, end + 1,
1344 res->end - end));
1345 } else if (res->start != start && res->end == end) {
1346 /* adjust the end */
1347 WARN_ON_ONCE(__adjust_resource(res, res->start,
1348 start - res->start));
1349 } else {
1350 /* split into two entries - we need a new resource */
1351 if (!new_res) {
1352 new_res = alloc_resource(GFP_ATOMIC);
1353 if (!new_res) {
1354 alloc_nofail = true;
1355 write_unlock(&resource_lock);
1356 goto retry;
1357 }
1358 }
1359 new_res->name = res->name;
1360 new_res->start = end + 1;
1361 new_res->end = res->end;
1362 new_res->flags = res->flags;
1363 new_res->desc = res->desc;
1364 new_res->parent = res->parent;
1365 new_res->sibling = res->sibling;
1366 new_res->child = NULL;
1367
1368 if (WARN_ON_ONCE(__adjust_resource(res, res->start,
1369 start - res->start)))
1370 break;
1371 res->sibling = new_res;
1372 new_res = NULL;
1373 }
1374
1375 break;
1376 }
1377
1378 write_unlock(&resource_lock);
1379 free_resource(new_res);
1380 }
1381 #endif /* CONFIG_MEMORY_HOTREMOVE */
1382
1383 #ifdef CONFIG_MEMORY_HOTPLUG
system_ram_resources_mergeable(struct resource * r1,struct resource * r2)1384 static bool system_ram_resources_mergeable(struct resource *r1,
1385 struct resource *r2)
1386 {
1387 /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */
1388 return r1->flags == r2->flags && r1->end + 1 == r2->start &&
1389 r1->name == r2->name && r1->desc == r2->desc &&
1390 !r1->child && !r2->child;
1391 }
1392
1393 /**
1394 * merge_system_ram_resource - mark the System RAM resource mergeable and try to
1395 * merge it with adjacent, mergeable resources
1396 * @res: resource descriptor
1397 *
1398 * This interface is intended for memory hotplug, whereby lots of contiguous
1399 * system ram resources are added (e.g., via add_memory*()) by a driver, and
1400 * the actual resource boundaries are not of interest (e.g., it might be
1401 * relevant for DIMMs). Only resources that are marked mergeable, that have the
1402 * same parent, and that don't have any children are considered. All mergeable
1403 * resources must be immutable during the request.
1404 *
1405 * Note:
1406 * - The caller has to make sure that no pointers to resources that are
1407 * marked mergeable are used anymore after this call - the resource might
1408 * be freed and the pointer might be stale!
1409 * - release_mem_region_adjustable() will split on demand on memory hotunplug
1410 */
merge_system_ram_resource(struct resource * res)1411 void merge_system_ram_resource(struct resource *res)
1412 {
1413 const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
1414 struct resource *cur;
1415
1416 if (WARN_ON_ONCE((res->flags & flags) != flags))
1417 return;
1418
1419 write_lock(&resource_lock);
1420 res->flags |= IORESOURCE_SYSRAM_MERGEABLE;
1421
1422 /* Try to merge with next item in the list. */
1423 cur = res->sibling;
1424 if (cur && system_ram_resources_mergeable(res, cur)) {
1425 res->end = cur->end;
1426 res->sibling = cur->sibling;
1427 free_resource(cur);
1428 }
1429
1430 /* Try to merge with previous item in the list. */
1431 cur = res->parent->child;
1432 while (cur && cur->sibling != res)
1433 cur = cur->sibling;
1434 if (cur && system_ram_resources_mergeable(cur, res)) {
1435 cur->end = res->end;
1436 cur->sibling = res->sibling;
1437 free_resource(res);
1438 }
1439 write_unlock(&resource_lock);
1440 }
1441 #endif /* CONFIG_MEMORY_HOTPLUG */
1442
1443 /*
1444 * Managed region resource
1445 */
devm_resource_release(struct device * dev,void * ptr)1446 static void devm_resource_release(struct device *dev, void *ptr)
1447 {
1448 struct resource **r = ptr;
1449
1450 release_resource(*r);
1451 }
1452
1453 /**
1454 * devm_request_resource() - request and reserve an I/O or memory resource
1455 * @dev: device for which to request the resource
1456 * @root: root of the resource tree from which to request the resource
1457 * @new: descriptor of the resource to request
1458 *
1459 * This is a device-managed version of request_resource(). There is usually
1460 * no need to release resources requested by this function explicitly since
1461 * that will be taken care of when the device is unbound from its driver.
1462 * If for some reason the resource needs to be released explicitly, because
1463 * of ordering issues for example, drivers must call devm_release_resource()
1464 * rather than the regular release_resource().
1465 *
1466 * When a conflict is detected between any existing resources and the newly
1467 * requested resource, an error message will be printed.
1468 *
1469 * Returns 0 on success or a negative error code on failure.
1470 */
devm_request_resource(struct device * dev,struct resource * root,struct resource * new)1471 int devm_request_resource(struct device *dev, struct resource *root,
1472 struct resource *new)
1473 {
1474 struct resource *conflict, **ptr;
1475
1476 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL);
1477 if (!ptr)
1478 return -ENOMEM;
1479
1480 *ptr = new;
1481
1482 conflict = request_resource_conflict(root, new);
1483 if (conflict) {
1484 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n",
1485 new, conflict->name, conflict);
1486 devres_free(ptr);
1487 return -EBUSY;
1488 }
1489
1490 devres_add(dev, ptr);
1491 return 0;
1492 }
1493 EXPORT_SYMBOL(devm_request_resource);
1494
devm_resource_match(struct device * dev,void * res,void * data)1495 static int devm_resource_match(struct device *dev, void *res, void *data)
1496 {
1497 struct resource **ptr = res;
1498
1499 return *ptr == data;
1500 }
1501
1502 /**
1503 * devm_release_resource() - release a previously requested resource
1504 * @dev: device for which to release the resource
1505 * @new: descriptor of the resource to release
1506 *
1507 * Releases a resource previously requested using devm_request_resource().
1508 */
devm_release_resource(struct device * dev,struct resource * new)1509 void devm_release_resource(struct device *dev, struct resource *new)
1510 {
1511 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match,
1512 new));
1513 }
1514 EXPORT_SYMBOL(devm_release_resource);
1515
1516 struct region_devres {
1517 struct resource *parent;
1518 resource_size_t start;
1519 resource_size_t n;
1520 };
1521
devm_region_release(struct device * dev,void * res)1522 static void devm_region_release(struct device *dev, void *res)
1523 {
1524 struct region_devres *this = res;
1525
1526 __release_region(this->parent, this->start, this->n);
1527 }
1528
devm_region_match(struct device * dev,void * res,void * match_data)1529 static int devm_region_match(struct device *dev, void *res, void *match_data)
1530 {
1531 struct region_devres *this = res, *match = match_data;
1532
1533 return this->parent == match->parent &&
1534 this->start == match->start && this->n == match->n;
1535 }
1536
1537 struct resource *
__devm_request_region(struct device * dev,struct resource * parent,resource_size_t start,resource_size_t n,const char * name)1538 __devm_request_region(struct device *dev, struct resource *parent,
1539 resource_size_t start, resource_size_t n, const char *name)
1540 {
1541 struct region_devres *dr = NULL;
1542 struct resource *res;
1543
1544 dr = devres_alloc(devm_region_release, sizeof(struct region_devres),
1545 GFP_KERNEL);
1546 if (!dr)
1547 return NULL;
1548
1549 dr->parent = parent;
1550 dr->start = start;
1551 dr->n = n;
1552
1553 res = __request_region(parent, start, n, name, 0);
1554 if (res)
1555 devres_add(dev, dr);
1556 else
1557 devres_free(dr);
1558
1559 return res;
1560 }
1561 EXPORT_SYMBOL(__devm_request_region);
1562
__devm_release_region(struct device * dev,struct resource * parent,resource_size_t start,resource_size_t n)1563 void __devm_release_region(struct device *dev, struct resource *parent,
1564 resource_size_t start, resource_size_t n)
1565 {
1566 struct region_devres match_data = { parent, start, n };
1567
1568 __release_region(parent, start, n);
1569 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match,
1570 &match_data));
1571 }
1572 EXPORT_SYMBOL(__devm_release_region);
1573
1574 /*
1575 * Reserve I/O ports or memory based on "reserve=" kernel parameter.
1576 */
1577 #define MAXRESERVE 4
reserve_setup(char * str)1578 static int __init reserve_setup(char *str)
1579 {
1580 static int reserved;
1581 static struct resource reserve[MAXRESERVE];
1582
1583 for (;;) {
1584 unsigned int io_start, io_num;
1585 int x = reserved;
1586 struct resource *parent;
1587
1588 if (get_option(&str, &io_start) != 2)
1589 break;
1590 if (get_option(&str, &io_num) == 0)
1591 break;
1592 if (x < MAXRESERVE) {
1593 struct resource *res = reserve + x;
1594
1595 /*
1596 * If the region starts below 0x10000, we assume it's
1597 * I/O port space; otherwise assume it's memory.
1598 */
1599 if (io_start < 0x10000) {
1600 res->flags = IORESOURCE_IO;
1601 parent = &ioport_resource;
1602 } else {
1603 res->flags = IORESOURCE_MEM;
1604 parent = &iomem_resource;
1605 }
1606 res->name = "reserved";
1607 res->start = io_start;
1608 res->end = io_start + io_num - 1;
1609 res->flags |= IORESOURCE_BUSY;
1610 res->desc = IORES_DESC_NONE;
1611 res->child = NULL;
1612 if (request_resource(parent, res) == 0)
1613 reserved = x+1;
1614 }
1615 }
1616 return 1;
1617 }
1618 __setup("reserve=", reserve_setup);
1619
1620 /*
1621 * Check if the requested addr and size spans more than any slot in the
1622 * iomem resource tree.
1623 */
iomem_map_sanity_check(resource_size_t addr,unsigned long size)1624 int iomem_map_sanity_check(resource_size_t addr, unsigned long size)
1625 {
1626 struct resource *p = &iomem_resource;
1627 int err = 0;
1628 loff_t l;
1629
1630 read_lock(&resource_lock);
1631 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1632 /*
1633 * We can probably skip the resources without
1634 * IORESOURCE_IO attribute?
1635 */
1636 if (p->start >= addr + size)
1637 continue;
1638 if (p->end < addr)
1639 continue;
1640 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) &&
1641 PFN_DOWN(p->end) >= PFN_DOWN(addr + size - 1))
1642 continue;
1643 /*
1644 * if a resource is "BUSY", it's not a hardware resource
1645 * but a driver mapping of such a resource; we don't want
1646 * to warn for those; some drivers legitimately map only
1647 * partial hardware resources. (example: vesafb)
1648 */
1649 if (p->flags & IORESOURCE_BUSY)
1650 continue;
1651
1652 printk(KERN_WARNING "resource sanity check: requesting [mem %#010llx-%#010llx], which spans more than %s %pR\n",
1653 (unsigned long long)addr,
1654 (unsigned long long)(addr + size - 1),
1655 p->name, p);
1656 err = -1;
1657 break;
1658 }
1659 read_unlock(&resource_lock);
1660
1661 return err;
1662 }
1663
1664 #ifdef CONFIG_STRICT_DEVMEM
1665 static int strict_iomem_checks = 1;
1666 #else
1667 static int strict_iomem_checks;
1668 #endif
1669
1670 /*
1671 * check if an address is reserved in the iomem resource tree
1672 * returns true if reserved, false if not reserved.
1673 */
iomem_is_exclusive(u64 addr)1674 bool iomem_is_exclusive(u64 addr)
1675 {
1676 struct resource *p = &iomem_resource;
1677 bool err = false;
1678 loff_t l;
1679 int size = PAGE_SIZE;
1680
1681 if (!strict_iomem_checks)
1682 return false;
1683
1684 addr = addr & PAGE_MASK;
1685
1686 read_lock(&resource_lock);
1687 for (p = p->child; p ; p = r_next(NULL, p, &l)) {
1688 /*
1689 * We can probably skip the resources without
1690 * IORESOURCE_IO attribute?
1691 */
1692 if (p->start >= addr + size)
1693 break;
1694 if (p->end < addr)
1695 continue;
1696 /*
1697 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set
1698 * or CONFIG_IO_STRICT_DEVMEM is enabled and the
1699 * resource is busy.
1700 */
1701 if ((p->flags & IORESOURCE_BUSY) == 0)
1702 continue;
1703 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM)
1704 || p->flags & IORESOURCE_EXCLUSIVE) {
1705 err = true;
1706 break;
1707 }
1708 }
1709 read_unlock(&resource_lock);
1710
1711 return err;
1712 }
1713
resource_list_create_entry(struct resource * res,size_t extra_size)1714 struct resource_entry *resource_list_create_entry(struct resource *res,
1715 size_t extra_size)
1716 {
1717 struct resource_entry *entry;
1718
1719 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL);
1720 if (entry) {
1721 INIT_LIST_HEAD(&entry->node);
1722 entry->res = res ? res : &entry->__res;
1723 }
1724
1725 return entry;
1726 }
1727 EXPORT_SYMBOL(resource_list_create_entry);
1728
resource_list_free(struct list_head * head)1729 void resource_list_free(struct list_head *head)
1730 {
1731 struct resource_entry *entry, *tmp;
1732
1733 list_for_each_entry_safe(entry, tmp, head, node)
1734 resource_list_destroy_entry(entry);
1735 }
1736 EXPORT_SYMBOL(resource_list_free);
1737
1738 #ifdef CONFIG_DEVICE_PRIVATE
__request_free_mem_region(struct device * dev,struct resource * base,unsigned long size,const char * name)1739 static struct resource *__request_free_mem_region(struct device *dev,
1740 struct resource *base, unsigned long size, const char *name)
1741 {
1742 resource_size_t end, addr;
1743 struct resource *res;
1744 struct region_devres *dr = NULL;
1745
1746 size = ALIGN(size, 1UL << PA_SECTION_SHIFT);
1747 end = min_t(unsigned long, base->end, (1UL << MAX_PHYSMEM_BITS) - 1);
1748 addr = end - size + 1UL;
1749
1750 res = alloc_resource(GFP_KERNEL);
1751 if (!res)
1752 return ERR_PTR(-ENOMEM);
1753
1754 if (dev) {
1755 dr = devres_alloc(devm_region_release,
1756 sizeof(struct region_devres), GFP_KERNEL);
1757 if (!dr) {
1758 free_resource(res);
1759 return ERR_PTR(-ENOMEM);
1760 }
1761 }
1762
1763 write_lock(&resource_lock);
1764 for (; addr > size && addr >= base->start; addr -= size) {
1765 if (__region_intersects(addr, size, 0, IORES_DESC_NONE) !=
1766 REGION_DISJOINT)
1767 continue;
1768
1769 if (__request_region_locked(res, &iomem_resource, addr, size,
1770 name, 0))
1771 break;
1772
1773 if (dev) {
1774 dr->parent = &iomem_resource;
1775 dr->start = addr;
1776 dr->n = size;
1777 devres_add(dev, dr);
1778 }
1779
1780 res->desc = IORES_DESC_DEVICE_PRIVATE_MEMORY;
1781 write_unlock(&resource_lock);
1782
1783 /*
1784 * A driver is claiming this region so revoke any mappings.
1785 */
1786 revoke_iomem(res);
1787 return res;
1788 }
1789 write_unlock(&resource_lock);
1790
1791 free_resource(res);
1792 if (dr)
1793 devres_free(dr);
1794
1795 return ERR_PTR(-ERANGE);
1796 }
1797
1798 /**
1799 * devm_request_free_mem_region - find free region for device private memory
1800 *
1801 * @dev: device struct to bind the resource to
1802 * @size: size in bytes of the device memory to add
1803 * @base: resource tree to look in
1804 *
1805 * This function tries to find an empty range of physical address big enough to
1806 * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE
1807 * memory, which in turn allocates struct pages.
1808 */
devm_request_free_mem_region(struct device * dev,struct resource * base,unsigned long size)1809 struct resource *devm_request_free_mem_region(struct device *dev,
1810 struct resource *base, unsigned long size)
1811 {
1812 return __request_free_mem_region(dev, base, size, dev_name(dev));
1813 }
1814 EXPORT_SYMBOL_GPL(devm_request_free_mem_region);
1815
request_free_mem_region(struct resource * base,unsigned long size,const char * name)1816 struct resource *request_free_mem_region(struct resource *base,
1817 unsigned long size, const char *name)
1818 {
1819 return __request_free_mem_region(NULL, base, size, name);
1820 }
1821 EXPORT_SYMBOL_GPL(request_free_mem_region);
1822
1823 #endif /* CONFIG_DEVICE_PRIVATE */
1824
strict_iomem(char * str)1825 static int __init strict_iomem(char *str)
1826 {
1827 if (strstr(str, "relaxed"))
1828 strict_iomem_checks = 0;
1829 if (strstr(str, "strict"))
1830 strict_iomem_checks = 1;
1831 return 1;
1832 }
1833
iomem_fs_init_fs_context(struct fs_context * fc)1834 static int iomem_fs_init_fs_context(struct fs_context *fc)
1835 {
1836 return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM;
1837 }
1838
1839 static struct file_system_type iomem_fs_type = {
1840 .name = "iomem",
1841 .owner = THIS_MODULE,
1842 .init_fs_context = iomem_fs_init_fs_context,
1843 .kill_sb = kill_anon_super,
1844 };
1845
iomem_init_inode(void)1846 static int __init iomem_init_inode(void)
1847 {
1848 static struct vfsmount *iomem_vfs_mount;
1849 static int iomem_fs_cnt;
1850 struct inode *inode;
1851 int rc;
1852
1853 rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt);
1854 if (rc < 0) {
1855 pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc);
1856 return rc;
1857 }
1858
1859 inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb);
1860 if (IS_ERR(inode)) {
1861 rc = PTR_ERR(inode);
1862 pr_err("Cannot allocate inode for iomem: %d\n", rc);
1863 simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt);
1864 return rc;
1865 }
1866
1867 /*
1868 * Publish iomem revocation inode initialized.
1869 * Pairs with smp_load_acquire() in revoke_iomem().
1870 */
1871 smp_store_release(&iomem_inode, inode);
1872
1873 return 0;
1874 }
1875
1876 fs_initcall(iomem_init_inode);
1877
1878 __setup("iomem=", strict_iomem);
1879