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