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