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 resource_size_t simple_align_resource(void *data, 625 const struct resource *avail, 626 resource_size_t size, 627 resource_size_t align) 628 { 629 return avail->start; 630 } 631 632 static void resource_clip(struct resource *res, resource_size_t min, 633 resource_size_t max) 634 { 635 if (res->start < min) 636 res->start = min; 637 if (res->end > max) 638 res->end = max; 639 } 640 641 /* 642 * Find empty space in the resource tree with the given range and 643 * alignment constraints 644 */ 645 static int __find_resource_space(struct resource *root, struct resource *old, 646 struct resource *new, resource_size_t size, 647 struct resource_constraint *constraint) 648 { 649 struct resource *this = root->child; 650 struct resource tmp = *new, avail, alloc; 651 652 tmp.start = root->start; 653 /* 654 * Skip past an allocated resource that starts at 0, since the assignment 655 * of this->start - 1 to tmp->end below would cause an underflow. 656 */ 657 if (this && this->start == root->start) { 658 tmp.start = (this == old) ? old->start : this->end + 1; 659 this = this->sibling; 660 } 661 for(;;) { 662 if (this) 663 tmp.end = (this == old) ? this->end : this->start - 1; 664 else 665 tmp.end = root->end; 666 667 if (tmp.end < tmp.start) 668 goto next; 669 670 resource_clip(&tmp, constraint->min, constraint->max); 671 arch_remove_reservations(&tmp); 672 673 /* Check for overflow after ALIGN() */ 674 avail.start = ALIGN(tmp.start, constraint->align); 675 avail.end = tmp.end; 676 avail.flags = new->flags & ~IORESOURCE_UNSET; 677 if (avail.start >= tmp.start) { 678 alloc.flags = avail.flags; 679 alloc.start = constraint->alignf(constraint->alignf_data, &avail, 680 size, constraint->align); 681 alloc.end = alloc.start + size - 1; 682 if (alloc.start <= alloc.end && 683 resource_contains(&avail, &alloc)) { 684 new->start = alloc.start; 685 new->end = alloc.end; 686 return 0; 687 } 688 } 689 690 next: if (!this || this->end == root->end) 691 break; 692 693 if (this != old) 694 tmp.start = this->end + 1; 695 this = this->sibling; 696 } 697 return -EBUSY; 698 } 699 700 /** 701 * find_resource_space - Find empty space in the resource tree 702 * @root: Root resource descriptor 703 * @new: Resource descriptor awaiting an empty resource space 704 * @size: The minimum size of the empty space 705 * @constraint: The range and alignment constraints to be met 706 * 707 * Finds an empty space under @root in the resource tree satisfying range and 708 * alignment @constraints. 709 * 710 * Return: 711 * * %0 - if successful, @new members start, end, and flags are altered. 712 * * %-EBUSY - if no empty space was found. 713 */ 714 static int find_resource_space(struct resource *root, struct resource *new, 715 resource_size_t size, 716 struct resource_constraint *constraint) 717 { 718 return __find_resource_space(root, NULL, new, size, constraint); 719 } 720 721 /** 722 * reallocate_resource - allocate a slot in the resource tree given range & alignment. 723 * The resource will be relocated if the new size cannot be reallocated in the 724 * current location. 725 * 726 * @root: root resource descriptor 727 * @old: resource descriptor desired by caller 728 * @newsize: new size of the resource descriptor 729 * @constraint: the size and alignment constraints to be met. 730 */ 731 static int reallocate_resource(struct resource *root, struct resource *old, 732 resource_size_t newsize, 733 struct resource_constraint *constraint) 734 { 735 int err=0; 736 struct resource new = *old; 737 struct resource *conflict; 738 739 write_lock(&resource_lock); 740 741 if ((err = __find_resource_space(root, old, &new, newsize, constraint))) 742 goto out; 743 744 if (resource_contains(&new, old)) { 745 old->start = new.start; 746 old->end = new.end; 747 goto out; 748 } 749 750 if (old->child) { 751 err = -EBUSY; 752 goto out; 753 } 754 755 if (resource_contains(old, &new)) { 756 old->start = new.start; 757 old->end = new.end; 758 } else { 759 __release_resource(old, true); 760 *old = new; 761 conflict = __request_resource(root, old); 762 BUG_ON(conflict); 763 } 764 out: 765 write_unlock(&resource_lock); 766 return err; 767 } 768 769 770 /** 771 * allocate_resource - allocate empty slot in the resource tree given range & alignment. 772 * The resource will be reallocated with a new size if it was already allocated 773 * @root: root resource descriptor 774 * @new: resource descriptor desired by caller 775 * @size: requested resource region size 776 * @min: minimum boundary to allocate 777 * @max: maximum boundary to allocate 778 * @align: alignment requested, in bytes 779 * @alignf: alignment function, optional, called if not NULL 780 * @alignf_data: arbitrary data to pass to the @alignf function 781 */ 782 int allocate_resource(struct resource *root, struct resource *new, 783 resource_size_t size, resource_size_t min, 784 resource_size_t max, resource_size_t align, 785 resource_alignf alignf, 786 void *alignf_data) 787 { 788 int err; 789 struct resource_constraint constraint; 790 791 if (!alignf) 792 alignf = simple_align_resource; 793 794 constraint.min = min; 795 constraint.max = max; 796 constraint.align = align; 797 constraint.alignf = alignf; 798 constraint.alignf_data = alignf_data; 799 800 if ( new->parent ) { 801 /* resource is already allocated, try reallocating with 802 the new constraints */ 803 return reallocate_resource(root, new, size, &constraint); 804 } 805 806 write_lock(&resource_lock); 807 err = find_resource_space(root, new, size, &constraint); 808 if (err >= 0 && __request_resource(root, new)) 809 err = -EBUSY; 810 write_unlock(&resource_lock); 811 return err; 812 } 813 814 EXPORT_SYMBOL(allocate_resource); 815 816 /** 817 * lookup_resource - find an existing resource by a resource start address 818 * @root: root resource descriptor 819 * @start: resource start address 820 * 821 * Returns a pointer to the resource if found, NULL otherwise 822 */ 823 struct resource *lookup_resource(struct resource *root, resource_size_t start) 824 { 825 struct resource *res; 826 827 read_lock(&resource_lock); 828 for (res = root->child; res; res = res->sibling) { 829 if (res->start == start) 830 break; 831 } 832 read_unlock(&resource_lock); 833 834 return res; 835 } 836 837 /* 838 * Insert a resource into the resource tree. If successful, return NULL, 839 * otherwise return the conflicting resource (compare to __request_resource()) 840 */ 841 static struct resource * __insert_resource(struct resource *parent, struct resource *new) 842 { 843 struct resource *first, *next; 844 845 for (;; parent = first) { 846 first = __request_resource(parent, new); 847 if (!first) 848 return first; 849 850 if (first == parent) 851 return first; 852 if (WARN_ON(first == new)) /* duplicated insertion */ 853 return first; 854 855 if ((first->start > new->start) || (first->end < new->end)) 856 break; 857 if ((first->start == new->start) && (first->end == new->end)) 858 break; 859 } 860 861 for (next = first; ; next = next->sibling) { 862 /* Partial overlap? Bad, and unfixable */ 863 if (next->start < new->start || next->end > new->end) 864 return next; 865 if (!next->sibling) 866 break; 867 if (next->sibling->start > new->end) 868 break; 869 } 870 871 new->parent = parent; 872 new->sibling = next->sibling; 873 new->child = first; 874 875 next->sibling = NULL; 876 for (next = first; next; next = next->sibling) 877 next->parent = new; 878 879 if (parent->child == first) { 880 parent->child = new; 881 } else { 882 next = parent->child; 883 while (next->sibling != first) 884 next = next->sibling; 885 next->sibling = new; 886 } 887 return NULL; 888 } 889 890 /** 891 * insert_resource_conflict - Inserts resource in the resource tree 892 * @parent: parent of the new resource 893 * @new: new resource to insert 894 * 895 * Returns 0 on success, conflict resource if the resource can't be inserted. 896 * 897 * This function is equivalent to request_resource_conflict when no conflict 898 * happens. If a conflict happens, and the conflicting resources 899 * entirely fit within the range of the new resource, then the new 900 * resource is inserted and the conflicting resources become children of 901 * the new resource. 902 * 903 * This function is intended for producers of resources, such as FW modules 904 * and bus drivers. 905 */ 906 struct resource *insert_resource_conflict(struct resource *parent, struct resource *new) 907 { 908 struct resource *conflict; 909 910 write_lock(&resource_lock); 911 conflict = __insert_resource(parent, new); 912 write_unlock(&resource_lock); 913 return conflict; 914 } 915 916 /** 917 * insert_resource - Inserts a resource in the resource tree 918 * @parent: parent of the new resource 919 * @new: new resource to insert 920 * 921 * Returns 0 on success, -EBUSY if the resource can't be inserted. 922 * 923 * This function is intended for producers of resources, such as FW modules 924 * and bus drivers. 925 */ 926 int insert_resource(struct resource *parent, struct resource *new) 927 { 928 struct resource *conflict; 929 930 conflict = insert_resource_conflict(parent, new); 931 return conflict ? -EBUSY : 0; 932 } 933 EXPORT_SYMBOL_GPL(insert_resource); 934 935 /** 936 * insert_resource_expand_to_fit - Insert a resource into the resource tree 937 * @root: root resource descriptor 938 * @new: new resource to insert 939 * 940 * Insert a resource into the resource tree, possibly expanding it in order 941 * to make it encompass any conflicting resources. 942 */ 943 void insert_resource_expand_to_fit(struct resource *root, struct resource *new) 944 { 945 if (new->parent) 946 return; 947 948 write_lock(&resource_lock); 949 for (;;) { 950 struct resource *conflict; 951 952 conflict = __insert_resource(root, new); 953 if (!conflict) 954 break; 955 if (conflict == root) 956 break; 957 958 /* Ok, expand resource to cover the conflict, then try again .. */ 959 if (conflict->start < new->start) 960 new->start = conflict->start; 961 if (conflict->end > new->end) 962 new->end = conflict->end; 963 964 pr_info("Expanded resource %s due to conflict with %s\n", new->name, conflict->name); 965 } 966 write_unlock(&resource_lock); 967 } 968 /* 969 * Not for general consumption, only early boot memory map parsing, PCI 970 * resource discovery, and late discovery of CXL resources are expected 971 * to use this interface. The former are built-in and only the latter, 972 * CXL, is a module. 973 */ 974 EXPORT_SYMBOL_NS_GPL(insert_resource_expand_to_fit, CXL); 975 976 /** 977 * remove_resource - Remove a resource in the resource tree 978 * @old: resource to remove 979 * 980 * Returns 0 on success, -EINVAL if the resource is not valid. 981 * 982 * This function removes a resource previously inserted by insert_resource() 983 * or insert_resource_conflict(), and moves the children (if any) up to 984 * where they were before. insert_resource() and insert_resource_conflict() 985 * insert a new resource, and move any conflicting resources down to the 986 * children of the new resource. 987 * 988 * insert_resource(), insert_resource_conflict() and remove_resource() are 989 * intended for producers of resources, such as FW modules and bus drivers. 990 */ 991 int remove_resource(struct resource *old) 992 { 993 int retval; 994 995 write_lock(&resource_lock); 996 retval = __release_resource(old, false); 997 write_unlock(&resource_lock); 998 return retval; 999 } 1000 EXPORT_SYMBOL_GPL(remove_resource); 1001 1002 static int __adjust_resource(struct resource *res, resource_size_t start, 1003 resource_size_t size) 1004 { 1005 struct resource *tmp, *parent = res->parent; 1006 resource_size_t end = start + size - 1; 1007 int result = -EBUSY; 1008 1009 if (!parent) 1010 goto skip; 1011 1012 if ((start < parent->start) || (end > parent->end)) 1013 goto out; 1014 1015 if (res->sibling && (res->sibling->start <= end)) 1016 goto out; 1017 1018 tmp = parent->child; 1019 if (tmp != res) { 1020 while (tmp->sibling != res) 1021 tmp = tmp->sibling; 1022 if (start <= tmp->end) 1023 goto out; 1024 } 1025 1026 skip: 1027 for (tmp = res->child; tmp; tmp = tmp->sibling) 1028 if ((tmp->start < start) || (tmp->end > end)) 1029 goto out; 1030 1031 res->start = start; 1032 res->end = end; 1033 result = 0; 1034 1035 out: 1036 return result; 1037 } 1038 1039 /** 1040 * adjust_resource - modify a resource's start and size 1041 * @res: resource to modify 1042 * @start: new start value 1043 * @size: new size 1044 * 1045 * Given an existing resource, change its start and size to match the 1046 * arguments. Returns 0 on success, -EBUSY if it can't fit. 1047 * Existing children of the resource are assumed to be immutable. 1048 */ 1049 int adjust_resource(struct resource *res, resource_size_t start, 1050 resource_size_t size) 1051 { 1052 int result; 1053 1054 write_lock(&resource_lock); 1055 result = __adjust_resource(res, start, size); 1056 write_unlock(&resource_lock); 1057 return result; 1058 } 1059 EXPORT_SYMBOL(adjust_resource); 1060 1061 static void __init 1062 __reserve_region_with_split(struct resource *root, resource_size_t start, 1063 resource_size_t end, const char *name) 1064 { 1065 struct resource *parent = root; 1066 struct resource *conflict; 1067 struct resource *res = alloc_resource(GFP_ATOMIC); 1068 struct resource *next_res = NULL; 1069 int type = resource_type(root); 1070 1071 if (!res) 1072 return; 1073 1074 res->name = name; 1075 res->start = start; 1076 res->end = end; 1077 res->flags = type | IORESOURCE_BUSY; 1078 res->desc = IORES_DESC_NONE; 1079 1080 while (1) { 1081 1082 conflict = __request_resource(parent, res); 1083 if (!conflict) { 1084 if (!next_res) 1085 break; 1086 res = next_res; 1087 next_res = NULL; 1088 continue; 1089 } 1090 1091 /* conflict covered whole area */ 1092 if (conflict->start <= res->start && 1093 conflict->end >= res->end) { 1094 free_resource(res); 1095 WARN_ON(next_res); 1096 break; 1097 } 1098 1099 /* failed, split and try again */ 1100 if (conflict->start > res->start) { 1101 end = res->end; 1102 res->end = conflict->start - 1; 1103 if (conflict->end < end) { 1104 next_res = alloc_resource(GFP_ATOMIC); 1105 if (!next_res) { 1106 free_resource(res); 1107 break; 1108 } 1109 next_res->name = name; 1110 next_res->start = conflict->end + 1; 1111 next_res->end = end; 1112 next_res->flags = type | IORESOURCE_BUSY; 1113 next_res->desc = IORES_DESC_NONE; 1114 } 1115 } else { 1116 res->start = conflict->end + 1; 1117 } 1118 } 1119 1120 } 1121 1122 void __init 1123 reserve_region_with_split(struct resource *root, resource_size_t start, 1124 resource_size_t end, const char *name) 1125 { 1126 int abort = 0; 1127 1128 write_lock(&resource_lock); 1129 if (root->start > start || root->end < end) { 1130 pr_err("requested range [0x%llx-0x%llx] not in root %pr\n", 1131 (unsigned long long)start, (unsigned long long)end, 1132 root); 1133 if (start > root->end || end < root->start) 1134 abort = 1; 1135 else { 1136 if (end > root->end) 1137 end = root->end; 1138 if (start < root->start) 1139 start = root->start; 1140 pr_err("fixing request to [0x%llx-0x%llx]\n", 1141 (unsigned long long)start, 1142 (unsigned long long)end); 1143 } 1144 dump_stack(); 1145 } 1146 if (!abort) 1147 __reserve_region_with_split(root, start, end, name); 1148 write_unlock(&resource_lock); 1149 } 1150 1151 /** 1152 * resource_alignment - calculate resource's alignment 1153 * @res: resource pointer 1154 * 1155 * Returns alignment on success, 0 (invalid alignment) on failure. 1156 */ 1157 resource_size_t resource_alignment(struct resource *res) 1158 { 1159 switch (res->flags & (IORESOURCE_SIZEALIGN | IORESOURCE_STARTALIGN)) { 1160 case IORESOURCE_SIZEALIGN: 1161 return resource_size(res); 1162 case IORESOURCE_STARTALIGN: 1163 return res->start; 1164 default: 1165 return 0; 1166 } 1167 } 1168 1169 /* 1170 * This is compatibility stuff for IO resources. 1171 * 1172 * Note how this, unlike the above, knows about 1173 * the IO flag meanings (busy etc). 1174 * 1175 * request_region creates a new busy region. 1176 * 1177 * release_region releases a matching busy region. 1178 */ 1179 1180 static DECLARE_WAIT_QUEUE_HEAD(muxed_resource_wait); 1181 1182 static struct inode *iomem_inode; 1183 1184 #ifdef CONFIG_IO_STRICT_DEVMEM 1185 static void revoke_iomem(struct resource *res) 1186 { 1187 /* pairs with smp_store_release() in iomem_init_inode() */ 1188 struct inode *inode = smp_load_acquire(&iomem_inode); 1189 1190 /* 1191 * Check that the initialization has completed. Losing the race 1192 * is ok because it means drivers are claiming resources before 1193 * the fs_initcall level of init and prevent iomem_get_mapping users 1194 * from establishing mappings. 1195 */ 1196 if (!inode) 1197 return; 1198 1199 /* 1200 * The expectation is that the driver has successfully marked 1201 * the resource busy by this point, so devmem_is_allowed() 1202 * should start returning false, however for performance this 1203 * does not iterate the entire resource range. 1204 */ 1205 if (devmem_is_allowed(PHYS_PFN(res->start)) && 1206 devmem_is_allowed(PHYS_PFN(res->end))) { 1207 /* 1208 * *cringe* iomem=relaxed says "go ahead, what's the 1209 * worst that can happen?" 1210 */ 1211 return; 1212 } 1213 1214 unmap_mapping_range(inode->i_mapping, res->start, resource_size(res), 1); 1215 } 1216 #else 1217 static void revoke_iomem(struct resource *res) {} 1218 #endif 1219 1220 struct address_space *iomem_get_mapping(void) 1221 { 1222 /* 1223 * This function is only called from file open paths, hence guaranteed 1224 * that fs_initcalls have completed and no need to check for NULL. But 1225 * since revoke_iomem can be called before the initcall we still need 1226 * the barrier to appease checkers. 1227 */ 1228 return smp_load_acquire(&iomem_inode)->i_mapping; 1229 } 1230 1231 static int __request_region_locked(struct resource *res, struct resource *parent, 1232 resource_size_t start, resource_size_t n, 1233 const char *name, int flags) 1234 { 1235 DECLARE_WAITQUEUE(wait, current); 1236 1237 res->name = name; 1238 res->start = start; 1239 res->end = start + n - 1; 1240 1241 for (;;) { 1242 struct resource *conflict; 1243 1244 res->flags = resource_type(parent) | resource_ext_type(parent); 1245 res->flags |= IORESOURCE_BUSY | flags; 1246 res->desc = parent->desc; 1247 1248 conflict = __request_resource(parent, res); 1249 if (!conflict) 1250 break; 1251 /* 1252 * mm/hmm.c reserves physical addresses which then 1253 * become unavailable to other users. Conflicts are 1254 * not expected. Warn to aid debugging if encountered. 1255 */ 1256 if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) { 1257 pr_warn("Unaddressable device %s %pR conflicts with %pR", 1258 conflict->name, conflict, res); 1259 } 1260 if (conflict != parent) { 1261 if (!(conflict->flags & IORESOURCE_BUSY)) { 1262 parent = conflict; 1263 continue; 1264 } 1265 } 1266 if (conflict->flags & flags & IORESOURCE_MUXED) { 1267 add_wait_queue(&muxed_resource_wait, &wait); 1268 write_unlock(&resource_lock); 1269 set_current_state(TASK_UNINTERRUPTIBLE); 1270 schedule(); 1271 remove_wait_queue(&muxed_resource_wait, &wait); 1272 write_lock(&resource_lock); 1273 continue; 1274 } 1275 /* Uhhuh, that didn't work out.. */ 1276 return -EBUSY; 1277 } 1278 1279 return 0; 1280 } 1281 1282 /** 1283 * __request_region - create a new busy resource region 1284 * @parent: parent resource descriptor 1285 * @start: resource start address 1286 * @n: resource region size 1287 * @name: reserving caller's ID string 1288 * @flags: IO resource flags 1289 */ 1290 struct resource *__request_region(struct resource *parent, 1291 resource_size_t start, resource_size_t n, 1292 const char *name, int flags) 1293 { 1294 struct resource *res = alloc_resource(GFP_KERNEL); 1295 int ret; 1296 1297 if (!res) 1298 return NULL; 1299 1300 write_lock(&resource_lock); 1301 ret = __request_region_locked(res, parent, start, n, name, flags); 1302 write_unlock(&resource_lock); 1303 1304 if (ret) { 1305 free_resource(res); 1306 return NULL; 1307 } 1308 1309 if (parent == &iomem_resource) 1310 revoke_iomem(res); 1311 1312 return res; 1313 } 1314 EXPORT_SYMBOL(__request_region); 1315 1316 /** 1317 * __release_region - release a previously reserved resource region 1318 * @parent: parent resource descriptor 1319 * @start: resource start address 1320 * @n: resource region size 1321 * 1322 * The described resource region must match a currently busy region. 1323 */ 1324 void __release_region(struct resource *parent, resource_size_t start, 1325 resource_size_t n) 1326 { 1327 struct resource **p; 1328 resource_size_t end; 1329 1330 p = &parent->child; 1331 end = start + n - 1; 1332 1333 write_lock(&resource_lock); 1334 1335 for (;;) { 1336 struct resource *res = *p; 1337 1338 if (!res) 1339 break; 1340 if (res->start <= start && res->end >= end) { 1341 if (!(res->flags & IORESOURCE_BUSY)) { 1342 p = &res->child; 1343 continue; 1344 } 1345 if (res->start != start || res->end != end) 1346 break; 1347 *p = res->sibling; 1348 write_unlock(&resource_lock); 1349 if (res->flags & IORESOURCE_MUXED) 1350 wake_up(&muxed_resource_wait); 1351 free_resource(res); 1352 return; 1353 } 1354 p = &res->sibling; 1355 } 1356 1357 write_unlock(&resource_lock); 1358 1359 pr_warn("Trying to free nonexistent resource <%pa-%pa>\n", &start, &end); 1360 } 1361 EXPORT_SYMBOL(__release_region); 1362 1363 #ifdef CONFIG_MEMORY_HOTREMOVE 1364 /** 1365 * release_mem_region_adjustable - release a previously reserved memory region 1366 * @start: resource start address 1367 * @size: resource region size 1368 * 1369 * This interface is intended for memory hot-delete. The requested region 1370 * is released from a currently busy memory resource. The requested region 1371 * must either match exactly or fit into a single busy resource entry. In 1372 * the latter case, the remaining resource is adjusted accordingly. 1373 * Existing children of the busy memory resource must be immutable in the 1374 * request. 1375 * 1376 * Note: 1377 * - Additional release conditions, such as overlapping region, can be 1378 * supported after they are confirmed as valid cases. 1379 * - When a busy memory resource gets split into two entries, the code 1380 * assumes that all children remain in the lower address entry for 1381 * simplicity. Enhance this logic when necessary. 1382 */ 1383 void release_mem_region_adjustable(resource_size_t start, resource_size_t size) 1384 { 1385 struct resource *parent = &iomem_resource; 1386 struct resource *new_res = NULL; 1387 bool alloc_nofail = false; 1388 struct resource **p; 1389 struct resource *res; 1390 resource_size_t end; 1391 1392 end = start + size - 1; 1393 if (WARN_ON_ONCE((start < parent->start) || (end > parent->end))) 1394 return; 1395 1396 /* 1397 * We free up quite a lot of memory on memory hotunplug (esp., memap), 1398 * just before releasing the region. This is highly unlikely to 1399 * fail - let's play save and make it never fail as the caller cannot 1400 * perform any error handling (e.g., trying to re-add memory will fail 1401 * similarly). 1402 */ 1403 retry: 1404 new_res = alloc_resource(GFP_KERNEL | (alloc_nofail ? __GFP_NOFAIL : 0)); 1405 1406 p = &parent->child; 1407 write_lock(&resource_lock); 1408 1409 while ((res = *p)) { 1410 if (res->start >= end) 1411 break; 1412 1413 /* look for the next resource if it does not fit into */ 1414 if (res->start > start || res->end < end) { 1415 p = &res->sibling; 1416 continue; 1417 } 1418 1419 if (!(res->flags & IORESOURCE_MEM)) 1420 break; 1421 1422 if (!(res->flags & IORESOURCE_BUSY)) { 1423 p = &res->child; 1424 continue; 1425 } 1426 1427 /* found the target resource; let's adjust accordingly */ 1428 if (res->start == start && res->end == end) { 1429 /* free the whole entry */ 1430 *p = res->sibling; 1431 free_resource(res); 1432 } else if (res->start == start && res->end != end) { 1433 /* adjust the start */ 1434 WARN_ON_ONCE(__adjust_resource(res, end + 1, 1435 res->end - end)); 1436 } else if (res->start != start && res->end == end) { 1437 /* adjust the end */ 1438 WARN_ON_ONCE(__adjust_resource(res, res->start, 1439 start - res->start)); 1440 } else { 1441 /* split into two entries - we need a new resource */ 1442 if (!new_res) { 1443 new_res = alloc_resource(GFP_ATOMIC); 1444 if (!new_res) { 1445 alloc_nofail = true; 1446 write_unlock(&resource_lock); 1447 goto retry; 1448 } 1449 } 1450 new_res->name = res->name; 1451 new_res->start = end + 1; 1452 new_res->end = res->end; 1453 new_res->flags = res->flags; 1454 new_res->desc = res->desc; 1455 new_res->parent = res->parent; 1456 new_res->sibling = res->sibling; 1457 new_res->child = NULL; 1458 1459 if (WARN_ON_ONCE(__adjust_resource(res, res->start, 1460 start - res->start))) 1461 break; 1462 res->sibling = new_res; 1463 new_res = NULL; 1464 } 1465 1466 break; 1467 } 1468 1469 write_unlock(&resource_lock); 1470 free_resource(new_res); 1471 } 1472 #endif /* CONFIG_MEMORY_HOTREMOVE */ 1473 1474 #ifdef CONFIG_MEMORY_HOTPLUG 1475 static bool system_ram_resources_mergeable(struct resource *r1, 1476 struct resource *r2) 1477 { 1478 /* We assume either r1 or r2 is IORESOURCE_SYSRAM_MERGEABLE. */ 1479 return r1->flags == r2->flags && r1->end + 1 == r2->start && 1480 r1->name == r2->name && r1->desc == r2->desc && 1481 !r1->child && !r2->child; 1482 } 1483 1484 /** 1485 * merge_system_ram_resource - mark the System RAM resource mergeable and try to 1486 * merge it with adjacent, mergeable resources 1487 * @res: resource descriptor 1488 * 1489 * This interface is intended for memory hotplug, whereby lots of contiguous 1490 * system ram resources are added (e.g., via add_memory*()) by a driver, and 1491 * the actual resource boundaries are not of interest (e.g., it might be 1492 * relevant for DIMMs). Only resources that are marked mergeable, that have the 1493 * same parent, and that don't have any children are considered. All mergeable 1494 * resources must be immutable during the request. 1495 * 1496 * Note: 1497 * - The caller has to make sure that no pointers to resources that are 1498 * marked mergeable are used anymore after this call - the resource might 1499 * be freed and the pointer might be stale! 1500 * - release_mem_region_adjustable() will split on demand on memory hotunplug 1501 */ 1502 void merge_system_ram_resource(struct resource *res) 1503 { 1504 const unsigned long flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY; 1505 struct resource *cur; 1506 1507 if (WARN_ON_ONCE((res->flags & flags) != flags)) 1508 return; 1509 1510 write_lock(&resource_lock); 1511 res->flags |= IORESOURCE_SYSRAM_MERGEABLE; 1512 1513 /* Try to merge with next item in the list. */ 1514 cur = res->sibling; 1515 if (cur && system_ram_resources_mergeable(res, cur)) { 1516 res->end = cur->end; 1517 res->sibling = cur->sibling; 1518 free_resource(cur); 1519 } 1520 1521 /* Try to merge with previous item in the list. */ 1522 cur = res->parent->child; 1523 while (cur && cur->sibling != res) 1524 cur = cur->sibling; 1525 if (cur && system_ram_resources_mergeable(cur, res)) { 1526 cur->end = res->end; 1527 cur->sibling = res->sibling; 1528 free_resource(res); 1529 } 1530 write_unlock(&resource_lock); 1531 } 1532 #endif /* CONFIG_MEMORY_HOTPLUG */ 1533 1534 /* 1535 * Managed region resource 1536 */ 1537 static void devm_resource_release(struct device *dev, void *ptr) 1538 { 1539 struct resource **r = ptr; 1540 1541 release_resource(*r); 1542 } 1543 1544 /** 1545 * devm_request_resource() - request and reserve an I/O or memory resource 1546 * @dev: device for which to request the resource 1547 * @root: root of the resource tree from which to request the resource 1548 * @new: descriptor of the resource to request 1549 * 1550 * This is a device-managed version of request_resource(). There is usually 1551 * no need to release resources requested by this function explicitly since 1552 * that will be taken care of when the device is unbound from its driver. 1553 * If for some reason the resource needs to be released explicitly, because 1554 * of ordering issues for example, drivers must call devm_release_resource() 1555 * rather than the regular release_resource(). 1556 * 1557 * When a conflict is detected between any existing resources and the newly 1558 * requested resource, an error message will be printed. 1559 * 1560 * Returns 0 on success or a negative error code on failure. 1561 */ 1562 int devm_request_resource(struct device *dev, struct resource *root, 1563 struct resource *new) 1564 { 1565 struct resource *conflict, **ptr; 1566 1567 ptr = devres_alloc(devm_resource_release, sizeof(*ptr), GFP_KERNEL); 1568 if (!ptr) 1569 return -ENOMEM; 1570 1571 *ptr = new; 1572 1573 conflict = request_resource_conflict(root, new); 1574 if (conflict) { 1575 dev_err(dev, "resource collision: %pR conflicts with %s %pR\n", 1576 new, conflict->name, conflict); 1577 devres_free(ptr); 1578 return -EBUSY; 1579 } 1580 1581 devres_add(dev, ptr); 1582 return 0; 1583 } 1584 EXPORT_SYMBOL(devm_request_resource); 1585 1586 static int devm_resource_match(struct device *dev, void *res, void *data) 1587 { 1588 struct resource **ptr = res; 1589 1590 return *ptr == data; 1591 } 1592 1593 /** 1594 * devm_release_resource() - release a previously requested resource 1595 * @dev: device for which to release the resource 1596 * @new: descriptor of the resource to release 1597 * 1598 * Releases a resource previously requested using devm_request_resource(). 1599 */ 1600 void devm_release_resource(struct device *dev, struct resource *new) 1601 { 1602 WARN_ON(devres_release(dev, devm_resource_release, devm_resource_match, 1603 new)); 1604 } 1605 EXPORT_SYMBOL(devm_release_resource); 1606 1607 struct region_devres { 1608 struct resource *parent; 1609 resource_size_t start; 1610 resource_size_t n; 1611 }; 1612 1613 static void devm_region_release(struct device *dev, void *res) 1614 { 1615 struct region_devres *this = res; 1616 1617 __release_region(this->parent, this->start, this->n); 1618 } 1619 1620 static int devm_region_match(struct device *dev, void *res, void *match_data) 1621 { 1622 struct region_devres *this = res, *match = match_data; 1623 1624 return this->parent == match->parent && 1625 this->start == match->start && this->n == match->n; 1626 } 1627 1628 struct resource * 1629 __devm_request_region(struct device *dev, struct resource *parent, 1630 resource_size_t start, resource_size_t n, const char *name) 1631 { 1632 struct region_devres *dr = NULL; 1633 struct resource *res; 1634 1635 dr = devres_alloc(devm_region_release, sizeof(struct region_devres), 1636 GFP_KERNEL); 1637 if (!dr) 1638 return NULL; 1639 1640 dr->parent = parent; 1641 dr->start = start; 1642 dr->n = n; 1643 1644 res = __request_region(parent, start, n, name, 0); 1645 if (res) 1646 devres_add(dev, dr); 1647 else 1648 devres_free(dr); 1649 1650 return res; 1651 } 1652 EXPORT_SYMBOL(__devm_request_region); 1653 1654 void __devm_release_region(struct device *dev, struct resource *parent, 1655 resource_size_t start, resource_size_t n) 1656 { 1657 struct region_devres match_data = { parent, start, n }; 1658 1659 __release_region(parent, start, n); 1660 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match, 1661 &match_data)); 1662 } 1663 EXPORT_SYMBOL(__devm_release_region); 1664 1665 /* 1666 * Reserve I/O ports or memory based on "reserve=" kernel parameter. 1667 */ 1668 #define MAXRESERVE 4 1669 static int __init reserve_setup(char *str) 1670 { 1671 static int reserved; 1672 static struct resource reserve[MAXRESERVE]; 1673 1674 for (;;) { 1675 unsigned int io_start, io_num; 1676 int x = reserved; 1677 struct resource *parent; 1678 1679 if (get_option(&str, &io_start) != 2) 1680 break; 1681 if (get_option(&str, &io_num) == 0) 1682 break; 1683 if (x < MAXRESERVE) { 1684 struct resource *res = reserve + x; 1685 1686 /* 1687 * If the region starts below 0x10000, we assume it's 1688 * I/O port space; otherwise assume it's memory. 1689 */ 1690 if (io_start < 0x10000) { 1691 res->flags = IORESOURCE_IO; 1692 parent = &ioport_resource; 1693 } else { 1694 res->flags = IORESOURCE_MEM; 1695 parent = &iomem_resource; 1696 } 1697 res->name = "reserved"; 1698 res->start = io_start; 1699 res->end = io_start + io_num - 1; 1700 res->flags |= IORESOURCE_BUSY; 1701 res->desc = IORES_DESC_NONE; 1702 res->child = NULL; 1703 if (request_resource(parent, res) == 0) 1704 reserved = x+1; 1705 } 1706 } 1707 return 1; 1708 } 1709 __setup("reserve=", reserve_setup); 1710 1711 /* 1712 * Check if the requested addr and size spans more than any slot in the 1713 * iomem resource tree. 1714 */ 1715 int iomem_map_sanity_check(resource_size_t addr, unsigned long size) 1716 { 1717 resource_size_t end = addr + size - 1; 1718 struct resource *p; 1719 int err = 0; 1720 1721 read_lock(&resource_lock); 1722 for_each_resource(&iomem_resource, p, false) { 1723 /* 1724 * We can probably skip the resources without 1725 * IORESOURCE_IO attribute? 1726 */ 1727 if (p->start > end) 1728 continue; 1729 if (p->end < addr) 1730 continue; 1731 if (PFN_DOWN(p->start) <= PFN_DOWN(addr) && 1732 PFN_DOWN(p->end) >= PFN_DOWN(end)) 1733 continue; 1734 /* 1735 * if a resource is "BUSY", it's not a hardware resource 1736 * but a driver mapping of such a resource; we don't want 1737 * to warn for those; some drivers legitimately map only 1738 * partial hardware resources. (example: vesafb) 1739 */ 1740 if (p->flags & IORESOURCE_BUSY) 1741 continue; 1742 1743 pr_warn("resource sanity check: requesting [mem %pa-%pa], which spans more than %s %pR\n", 1744 &addr, &end, p->name, p); 1745 err = -1; 1746 break; 1747 } 1748 read_unlock(&resource_lock); 1749 1750 return err; 1751 } 1752 1753 #ifdef CONFIG_STRICT_DEVMEM 1754 static int strict_iomem_checks = 1; 1755 #else 1756 static int strict_iomem_checks; 1757 #endif 1758 1759 /* 1760 * Check if an address is exclusive to the kernel and must not be mapped to 1761 * user space, for example, via /dev/mem. 1762 * 1763 * Returns true if exclusive to the kernel, otherwise returns false. 1764 */ 1765 bool resource_is_exclusive(struct resource *root, u64 addr, resource_size_t size) 1766 { 1767 const unsigned int exclusive_system_ram = IORESOURCE_SYSTEM_RAM | 1768 IORESOURCE_EXCLUSIVE; 1769 bool skip_children = false, err = false; 1770 struct resource *p; 1771 1772 read_lock(&resource_lock); 1773 for_each_resource(root, p, skip_children) { 1774 if (p->start >= addr + size) 1775 break; 1776 if (p->end < addr) { 1777 skip_children = true; 1778 continue; 1779 } 1780 skip_children = false; 1781 1782 /* 1783 * IORESOURCE_SYSTEM_RAM resources are exclusive if 1784 * IORESOURCE_EXCLUSIVE is set, even if they 1785 * are not busy and even if "iomem=relaxed" is set. The 1786 * responsible driver dynamically adds/removes system RAM within 1787 * such an area and uncontrolled access is dangerous. 1788 */ 1789 if ((p->flags & exclusive_system_ram) == exclusive_system_ram) { 1790 err = true; 1791 break; 1792 } 1793 1794 /* 1795 * A resource is exclusive if IORESOURCE_EXCLUSIVE is set 1796 * or CONFIG_IO_STRICT_DEVMEM is enabled and the 1797 * resource is busy. 1798 */ 1799 if (!strict_iomem_checks || !(p->flags & IORESOURCE_BUSY)) 1800 continue; 1801 if (IS_ENABLED(CONFIG_IO_STRICT_DEVMEM) 1802 || p->flags & IORESOURCE_EXCLUSIVE) { 1803 err = true; 1804 break; 1805 } 1806 } 1807 read_unlock(&resource_lock); 1808 1809 return err; 1810 } 1811 1812 bool iomem_is_exclusive(u64 addr) 1813 { 1814 return resource_is_exclusive(&iomem_resource, addr & PAGE_MASK, 1815 PAGE_SIZE); 1816 } 1817 1818 struct resource_entry *resource_list_create_entry(struct resource *res, 1819 size_t extra_size) 1820 { 1821 struct resource_entry *entry; 1822 1823 entry = kzalloc(sizeof(*entry) + extra_size, GFP_KERNEL); 1824 if (entry) { 1825 INIT_LIST_HEAD(&entry->node); 1826 entry->res = res ? res : &entry->__res; 1827 } 1828 1829 return entry; 1830 } 1831 EXPORT_SYMBOL(resource_list_create_entry); 1832 1833 void resource_list_free(struct list_head *head) 1834 { 1835 struct resource_entry *entry, *tmp; 1836 1837 list_for_each_entry_safe(entry, tmp, head, node) 1838 resource_list_destroy_entry(entry); 1839 } 1840 EXPORT_SYMBOL(resource_list_free); 1841 1842 #ifdef CONFIG_GET_FREE_REGION 1843 #define GFR_DESCENDING (1UL << 0) 1844 #define GFR_REQUEST_REGION (1UL << 1) 1845 #define GFR_DEFAULT_ALIGN (1UL << PA_SECTION_SHIFT) 1846 1847 static resource_size_t gfr_start(struct resource *base, resource_size_t size, 1848 resource_size_t align, unsigned long flags) 1849 { 1850 if (flags & GFR_DESCENDING) { 1851 resource_size_t end; 1852 1853 end = min_t(resource_size_t, base->end, 1854 (1ULL << MAX_PHYSMEM_BITS) - 1); 1855 return end - size + 1; 1856 } 1857 1858 return ALIGN(base->start, align); 1859 } 1860 1861 static bool gfr_continue(struct resource *base, resource_size_t addr, 1862 resource_size_t size, unsigned long flags) 1863 { 1864 if (flags & GFR_DESCENDING) 1865 return addr > size && addr >= base->start; 1866 /* 1867 * In the ascend case be careful that the last increment by 1868 * @size did not wrap 0. 1869 */ 1870 return addr > addr - size && 1871 addr <= min_t(resource_size_t, base->end, 1872 (1ULL << MAX_PHYSMEM_BITS) - 1); 1873 } 1874 1875 static resource_size_t gfr_next(resource_size_t addr, resource_size_t size, 1876 unsigned long flags) 1877 { 1878 if (flags & GFR_DESCENDING) 1879 return addr - size; 1880 return addr + size; 1881 } 1882 1883 static void remove_free_mem_region(void *_res) 1884 { 1885 struct resource *res = _res; 1886 1887 if (res->parent) 1888 remove_resource(res); 1889 free_resource(res); 1890 } 1891 1892 static struct resource * 1893 get_free_mem_region(struct device *dev, struct resource *base, 1894 resource_size_t size, const unsigned long align, 1895 const char *name, const unsigned long desc, 1896 const unsigned long flags) 1897 { 1898 resource_size_t addr; 1899 struct resource *res; 1900 struct region_devres *dr = NULL; 1901 1902 size = ALIGN(size, align); 1903 1904 res = alloc_resource(GFP_KERNEL); 1905 if (!res) 1906 return ERR_PTR(-ENOMEM); 1907 1908 if (dev && (flags & GFR_REQUEST_REGION)) { 1909 dr = devres_alloc(devm_region_release, 1910 sizeof(struct region_devres), GFP_KERNEL); 1911 if (!dr) { 1912 free_resource(res); 1913 return ERR_PTR(-ENOMEM); 1914 } 1915 } else if (dev) { 1916 if (devm_add_action_or_reset(dev, remove_free_mem_region, res)) 1917 return ERR_PTR(-ENOMEM); 1918 } 1919 1920 write_lock(&resource_lock); 1921 for (addr = gfr_start(base, size, align, flags); 1922 gfr_continue(base, addr, align, flags); 1923 addr = gfr_next(addr, align, flags)) { 1924 if (__region_intersects(base, addr, size, 0, IORES_DESC_NONE) != 1925 REGION_DISJOINT) 1926 continue; 1927 1928 if (flags & GFR_REQUEST_REGION) { 1929 if (__request_region_locked(res, &iomem_resource, addr, 1930 size, name, 0)) 1931 break; 1932 1933 if (dev) { 1934 dr->parent = &iomem_resource; 1935 dr->start = addr; 1936 dr->n = size; 1937 devres_add(dev, dr); 1938 } 1939 1940 res->desc = desc; 1941 write_unlock(&resource_lock); 1942 1943 1944 /* 1945 * A driver is claiming this region so revoke any 1946 * mappings. 1947 */ 1948 revoke_iomem(res); 1949 } else { 1950 res->start = addr; 1951 res->end = addr + size - 1; 1952 res->name = name; 1953 res->desc = desc; 1954 res->flags = IORESOURCE_MEM; 1955 1956 /* 1957 * Only succeed if the resource hosts an exclusive 1958 * range after the insert 1959 */ 1960 if (__insert_resource(base, res) || res->child) 1961 break; 1962 1963 write_unlock(&resource_lock); 1964 } 1965 1966 return res; 1967 } 1968 write_unlock(&resource_lock); 1969 1970 if (flags & GFR_REQUEST_REGION) { 1971 free_resource(res); 1972 devres_free(dr); 1973 } else if (dev) 1974 devm_release_action(dev, remove_free_mem_region, res); 1975 1976 return ERR_PTR(-ERANGE); 1977 } 1978 1979 /** 1980 * devm_request_free_mem_region - find free region for device private memory 1981 * 1982 * @dev: device struct to bind the resource to 1983 * @size: size in bytes of the device memory to add 1984 * @base: resource tree to look in 1985 * 1986 * This function tries to find an empty range of physical address big enough to 1987 * contain the new resource, so that it can later be hotplugged as ZONE_DEVICE 1988 * memory, which in turn allocates struct pages. 1989 */ 1990 struct resource *devm_request_free_mem_region(struct device *dev, 1991 struct resource *base, unsigned long size) 1992 { 1993 unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION; 1994 1995 return get_free_mem_region(dev, base, size, GFR_DEFAULT_ALIGN, 1996 dev_name(dev), 1997 IORES_DESC_DEVICE_PRIVATE_MEMORY, flags); 1998 } 1999 EXPORT_SYMBOL_GPL(devm_request_free_mem_region); 2000 2001 struct resource *request_free_mem_region(struct resource *base, 2002 unsigned long size, const char *name) 2003 { 2004 unsigned long flags = GFR_DESCENDING | GFR_REQUEST_REGION; 2005 2006 return get_free_mem_region(NULL, base, size, GFR_DEFAULT_ALIGN, name, 2007 IORES_DESC_DEVICE_PRIVATE_MEMORY, flags); 2008 } 2009 EXPORT_SYMBOL_GPL(request_free_mem_region); 2010 2011 /** 2012 * alloc_free_mem_region - find a free region relative to @base 2013 * @base: resource that will parent the new resource 2014 * @size: size in bytes of memory to allocate from @base 2015 * @align: alignment requirements for the allocation 2016 * @name: resource name 2017 * 2018 * Buses like CXL, that can dynamically instantiate new memory regions, 2019 * need a method to allocate physical address space for those regions. 2020 * Allocate and insert a new resource to cover a free, unclaimed by a 2021 * descendant of @base, range in the span of @base. 2022 */ 2023 struct resource *alloc_free_mem_region(struct resource *base, 2024 unsigned long size, unsigned long align, 2025 const char *name) 2026 { 2027 /* Default of ascending direction and insert resource */ 2028 unsigned long flags = 0; 2029 2030 return get_free_mem_region(NULL, base, size, align, name, 2031 IORES_DESC_NONE, flags); 2032 } 2033 EXPORT_SYMBOL_NS_GPL(alloc_free_mem_region, CXL); 2034 #endif /* CONFIG_GET_FREE_REGION */ 2035 2036 static int __init strict_iomem(char *str) 2037 { 2038 if (strstr(str, "relaxed")) 2039 strict_iomem_checks = 0; 2040 if (strstr(str, "strict")) 2041 strict_iomem_checks = 1; 2042 return 1; 2043 } 2044 2045 static int iomem_fs_init_fs_context(struct fs_context *fc) 2046 { 2047 return init_pseudo(fc, DEVMEM_MAGIC) ? 0 : -ENOMEM; 2048 } 2049 2050 static struct file_system_type iomem_fs_type = { 2051 .name = "iomem", 2052 .owner = THIS_MODULE, 2053 .init_fs_context = iomem_fs_init_fs_context, 2054 .kill_sb = kill_anon_super, 2055 }; 2056 2057 static int __init iomem_init_inode(void) 2058 { 2059 static struct vfsmount *iomem_vfs_mount; 2060 static int iomem_fs_cnt; 2061 struct inode *inode; 2062 int rc; 2063 2064 rc = simple_pin_fs(&iomem_fs_type, &iomem_vfs_mount, &iomem_fs_cnt); 2065 if (rc < 0) { 2066 pr_err("Cannot mount iomem pseudo filesystem: %d\n", rc); 2067 return rc; 2068 } 2069 2070 inode = alloc_anon_inode(iomem_vfs_mount->mnt_sb); 2071 if (IS_ERR(inode)) { 2072 rc = PTR_ERR(inode); 2073 pr_err("Cannot allocate inode for iomem: %d\n", rc); 2074 simple_release_fs(&iomem_vfs_mount, &iomem_fs_cnt); 2075 return rc; 2076 } 2077 2078 /* 2079 * Publish iomem revocation inode initialized. 2080 * Pairs with smp_load_acquire() in revoke_iomem(). 2081 */ 2082 smp_store_release(&iomem_inode, inode); 2083 2084 return 0; 2085 } 2086 2087 fs_initcall(iomem_init_inode); 2088 2089 __setup("iomem=", strict_iomem); 2090