1 /* 2 * linux/kernel/resource.c 3 * 4 * Copyright (C) 1999 Linus Torvalds 5 * Copyright (C) 1999 Martin Mares <mj@ucw.cz> 6 * 7 * Arbitrary resource management. 8 */ 9 10 #include <linux/module.h> 11 #include <linux/sched.h> 12 #include <linux/errno.h> 13 #include <linux/ioport.h> 14 #include <linux/init.h> 15 #include <linux/slab.h> 16 #include <linux/spinlock.h> 17 #include <linux/fs.h> 18 #include <linux/proc_fs.h> 19 #include <linux/seq_file.h> 20 #include <linux/device.h> 21 #include <asm/io.h> 22 23 24 struct resource ioport_resource = { 25 .name = "PCI IO", 26 .start = 0, 27 .end = IO_SPACE_LIMIT, 28 .flags = IORESOURCE_IO, 29 }; 30 EXPORT_SYMBOL(ioport_resource); 31 32 struct resource iomem_resource = { 33 .name = "PCI mem", 34 .start = 0, 35 .end = -1, 36 .flags = IORESOURCE_MEM, 37 }; 38 EXPORT_SYMBOL(iomem_resource); 39 40 static DEFINE_RWLOCK(resource_lock); 41 42 #ifdef CONFIG_PROC_FS 43 44 enum { MAX_IORES_LEVEL = 5 }; 45 46 static void *r_next(struct seq_file *m, void *v, loff_t *pos) 47 { 48 struct resource *p = v; 49 (*pos)++; 50 if (p->child) 51 return p->child; 52 while (!p->sibling && p->parent) 53 p = p->parent; 54 return p->sibling; 55 } 56 57 static void *r_start(struct seq_file *m, loff_t *pos) 58 __acquires(resource_lock) 59 { 60 struct resource *p = m->private; 61 loff_t l = 0; 62 read_lock(&resource_lock); 63 for (p = p->child; p && l < *pos; p = r_next(m, p, &l)) 64 ; 65 return p; 66 } 67 68 static void r_stop(struct seq_file *m, void *v) 69 __releases(resource_lock) 70 { 71 read_unlock(&resource_lock); 72 } 73 74 static int r_show(struct seq_file *m, void *v) 75 { 76 struct resource *root = m->private; 77 struct resource *r = v, *p; 78 int width = root->end < 0x10000 ? 4 : 8; 79 int depth; 80 81 for (depth = 0, p = r; depth < MAX_IORES_LEVEL; depth++, p = p->parent) 82 if (p->parent == root) 83 break; 84 seq_printf(m, "%*s%0*llx-%0*llx : %s\n", 85 depth * 2, "", 86 width, (unsigned long long) r->start, 87 width, (unsigned long long) r->end, 88 r->name ? r->name : "<BAD>"); 89 return 0; 90 } 91 92 static const struct seq_operations resource_op = { 93 .start = r_start, 94 .next = r_next, 95 .stop = r_stop, 96 .show = r_show, 97 }; 98 99 static int ioports_open(struct inode *inode, struct file *file) 100 { 101 int res = seq_open(file, &resource_op); 102 if (!res) { 103 struct seq_file *m = file->private_data; 104 m->private = &ioport_resource; 105 } 106 return res; 107 } 108 109 static int iomem_open(struct inode *inode, struct file *file) 110 { 111 int res = seq_open(file, &resource_op); 112 if (!res) { 113 struct seq_file *m = file->private_data; 114 m->private = &iomem_resource; 115 } 116 return res; 117 } 118 119 static const struct file_operations proc_ioports_operations = { 120 .open = ioports_open, 121 .read = seq_read, 122 .llseek = seq_lseek, 123 .release = seq_release, 124 }; 125 126 static const struct file_operations proc_iomem_operations = { 127 .open = iomem_open, 128 .read = seq_read, 129 .llseek = seq_lseek, 130 .release = seq_release, 131 }; 132 133 static int __init ioresources_init(void) 134 { 135 struct proc_dir_entry *entry; 136 137 entry = create_proc_entry("ioports", 0, NULL); 138 if (entry) 139 entry->proc_fops = &proc_ioports_operations; 140 entry = create_proc_entry("iomem", 0, NULL); 141 if (entry) 142 entry->proc_fops = &proc_iomem_operations; 143 return 0; 144 } 145 __initcall(ioresources_init); 146 147 #endif /* CONFIG_PROC_FS */ 148 149 /* Return the conflict entry if you can't request it */ 150 static struct resource * __request_resource(struct resource *root, struct resource *new) 151 { 152 resource_size_t start = new->start; 153 resource_size_t end = new->end; 154 struct resource *tmp, **p; 155 156 if (end < start) 157 return root; 158 if (start < root->start) 159 return root; 160 if (end > root->end) 161 return root; 162 p = &root->child; 163 for (;;) { 164 tmp = *p; 165 if (!tmp || tmp->start > end) { 166 new->sibling = tmp; 167 *p = new; 168 new->parent = root; 169 return NULL; 170 } 171 p = &tmp->sibling; 172 if (tmp->end < start) 173 continue; 174 return tmp; 175 } 176 } 177 178 static int __release_resource(struct resource *old) 179 { 180 struct resource *tmp, **p; 181 182 p = &old->parent->child; 183 for (;;) { 184 tmp = *p; 185 if (!tmp) 186 break; 187 if (tmp == old) { 188 *p = tmp->sibling; 189 old->parent = NULL; 190 return 0; 191 } 192 p = &tmp->sibling; 193 } 194 return -EINVAL; 195 } 196 197 /** 198 * request_resource - request and reserve an I/O or memory resource 199 * @root: root resource descriptor 200 * @new: resource descriptor desired by caller 201 * 202 * Returns 0 for success, negative error code on error. 203 */ 204 int request_resource(struct resource *root, struct resource *new) 205 { 206 struct resource *conflict; 207 208 write_lock(&resource_lock); 209 conflict = __request_resource(root, new); 210 write_unlock(&resource_lock); 211 return conflict ? -EBUSY : 0; 212 } 213 214 EXPORT_SYMBOL(request_resource); 215 216 /** 217 * ____request_resource - reserve a resource, with resource conflict returned 218 * @root: root resource descriptor 219 * @new: resource descriptor desired by caller 220 * 221 * Returns: 222 * On success, NULL is returned. 223 * On error, a pointer to the conflicting resource is returned. 224 */ 225 struct resource *____request_resource(struct resource *root, struct resource *new) 226 { 227 struct resource *conflict; 228 229 write_lock(&resource_lock); 230 conflict = __request_resource(root, new); 231 write_unlock(&resource_lock); 232 return conflict; 233 } 234 235 EXPORT_SYMBOL(____request_resource); 236 237 /** 238 * release_resource - release a previously reserved resource 239 * @old: resource pointer 240 */ 241 int release_resource(struct resource *old) 242 { 243 int retval; 244 245 write_lock(&resource_lock); 246 retval = __release_resource(old); 247 write_unlock(&resource_lock); 248 return retval; 249 } 250 251 EXPORT_SYMBOL(release_resource); 252 253 #ifdef CONFIG_MEMORY_HOTPLUG 254 /* 255 * Finds the lowest memory reosurce exists within [res->start.res->end) 256 * the caller must specify res->start, res->end, res->flags. 257 * If found, returns 0, res is overwritten, if not found, returns -1. 258 */ 259 int find_next_system_ram(struct resource *res) 260 { 261 resource_size_t start, end; 262 struct resource *p; 263 264 BUG_ON(!res); 265 266 start = res->start; 267 end = res->end; 268 BUG_ON(start >= end); 269 270 read_lock(&resource_lock); 271 for (p = iomem_resource.child; p ; p = p->sibling) { 272 /* system ram is just marked as IORESOURCE_MEM */ 273 if (p->flags != res->flags) 274 continue; 275 if (p->start > end) { 276 p = NULL; 277 break; 278 } 279 if ((p->end >= start) && (p->start < end)) 280 break; 281 } 282 read_unlock(&resource_lock); 283 if (!p) 284 return -1; 285 /* copy data */ 286 if (res->start < p->start) 287 res->start = p->start; 288 if (res->end > p->end) 289 res->end = p->end; 290 return 0; 291 } 292 #endif 293 294 /* 295 * Find empty slot in the resource tree given range and alignment. 296 */ 297 static int find_resource(struct resource *root, struct resource *new, 298 resource_size_t size, resource_size_t min, 299 resource_size_t max, resource_size_t align, 300 void (*alignf)(void *, struct resource *, 301 resource_size_t, resource_size_t), 302 void *alignf_data) 303 { 304 struct resource *this = root->child; 305 306 new->start = root->start; 307 /* 308 * Skip past an allocated resource that starts at 0, since the assignment 309 * of this->start - 1 to new->end below would cause an underflow. 310 */ 311 if (this && this->start == 0) { 312 new->start = this->end + 1; 313 this = this->sibling; 314 } 315 for(;;) { 316 if (this) 317 new->end = this->start - 1; 318 else 319 new->end = root->end; 320 if (new->start < min) 321 new->start = min; 322 if (new->end > max) 323 new->end = max; 324 new->start = ALIGN(new->start, align); 325 if (alignf) 326 alignf(alignf_data, new, size, align); 327 if (new->start < new->end && new->end - new->start >= size - 1) { 328 new->end = new->start + size - 1; 329 return 0; 330 } 331 if (!this) 332 break; 333 new->start = this->end + 1; 334 this = this->sibling; 335 } 336 return -EBUSY; 337 } 338 339 /** 340 * allocate_resource - allocate empty slot in the resource tree given range & alignment 341 * @root: root resource descriptor 342 * @new: resource descriptor desired by caller 343 * @size: requested resource region size 344 * @min: minimum size to allocate 345 * @max: maximum size to allocate 346 * @align: alignment requested, in bytes 347 * @alignf: alignment function, optional, called if not NULL 348 * @alignf_data: arbitrary data to pass to the @alignf function 349 */ 350 int allocate_resource(struct resource *root, struct resource *new, 351 resource_size_t size, resource_size_t min, 352 resource_size_t max, resource_size_t align, 353 void (*alignf)(void *, struct resource *, 354 resource_size_t, resource_size_t), 355 void *alignf_data) 356 { 357 int err; 358 359 write_lock(&resource_lock); 360 err = find_resource(root, new, size, min, max, align, alignf, alignf_data); 361 if (err >= 0 && __request_resource(root, new)) 362 err = -EBUSY; 363 write_unlock(&resource_lock); 364 return err; 365 } 366 367 EXPORT_SYMBOL(allocate_resource); 368 369 /** 370 * insert_resource - Inserts a resource in the resource tree 371 * @parent: parent of the new resource 372 * @new: new resource to insert 373 * 374 * Returns 0 on success, -EBUSY if the resource can't be inserted. 375 * 376 * This function is equivalent to request_resource when no conflict 377 * happens. If a conflict happens, and the conflicting resources 378 * entirely fit within the range of the new resource, then the new 379 * resource is inserted and the conflicting resources become children of 380 * the new resource. 381 */ 382 int insert_resource(struct resource *parent, struct resource *new) 383 { 384 int result; 385 struct resource *first, *next; 386 387 write_lock(&resource_lock); 388 389 for (;; parent = first) { 390 result = 0; 391 first = __request_resource(parent, new); 392 if (!first) 393 goto out; 394 395 result = -EBUSY; 396 if (first == parent) 397 goto out; 398 399 if ((first->start > new->start) || (first->end < new->end)) 400 break; 401 if ((first->start == new->start) && (first->end == new->end)) 402 break; 403 } 404 405 for (next = first; ; next = next->sibling) { 406 /* Partial overlap? Bad, and unfixable */ 407 if (next->start < new->start || next->end > new->end) 408 goto out; 409 if (!next->sibling) 410 break; 411 if (next->sibling->start > new->end) 412 break; 413 } 414 415 result = 0; 416 417 new->parent = parent; 418 new->sibling = next->sibling; 419 new->child = first; 420 421 next->sibling = NULL; 422 for (next = first; next; next = next->sibling) 423 next->parent = new; 424 425 if (parent->child == first) { 426 parent->child = new; 427 } else { 428 next = parent->child; 429 while (next->sibling != first) 430 next = next->sibling; 431 next->sibling = new; 432 } 433 434 out: 435 write_unlock(&resource_lock); 436 return result; 437 } 438 439 /** 440 * adjust_resource - modify a resource's start and size 441 * @res: resource to modify 442 * @start: new start value 443 * @size: new size 444 * 445 * Given an existing resource, change its start and size to match the 446 * arguments. Returns 0 on success, -EBUSY if it can't fit. 447 * Existing children of the resource are assumed to be immutable. 448 */ 449 int adjust_resource(struct resource *res, resource_size_t start, resource_size_t size) 450 { 451 struct resource *tmp, *parent = res->parent; 452 resource_size_t end = start + size - 1; 453 int result = -EBUSY; 454 455 write_lock(&resource_lock); 456 457 if ((start < parent->start) || (end > parent->end)) 458 goto out; 459 460 for (tmp = res->child; tmp; tmp = tmp->sibling) { 461 if ((tmp->start < start) || (tmp->end > end)) 462 goto out; 463 } 464 465 if (res->sibling && (res->sibling->start <= end)) 466 goto out; 467 468 tmp = parent->child; 469 if (tmp != res) { 470 while (tmp->sibling != res) 471 tmp = tmp->sibling; 472 if (start <= tmp->end) 473 goto out; 474 } 475 476 res->start = start; 477 res->end = end; 478 result = 0; 479 480 out: 481 write_unlock(&resource_lock); 482 return result; 483 } 484 485 EXPORT_SYMBOL(adjust_resource); 486 487 /* 488 * This is compatibility stuff for IO resources. 489 * 490 * Note how this, unlike the above, knows about 491 * the IO flag meanings (busy etc). 492 * 493 * request_region creates a new busy region. 494 * 495 * check_region returns non-zero if the area is already busy. 496 * 497 * release_region releases a matching busy region. 498 */ 499 500 /** 501 * __request_region - create a new busy resource region 502 * @parent: parent resource descriptor 503 * @start: resource start address 504 * @n: resource region size 505 * @name: reserving caller's ID string 506 */ 507 struct resource * __request_region(struct resource *parent, 508 resource_size_t start, resource_size_t n, 509 const char *name) 510 { 511 struct resource *res = kzalloc(sizeof(*res), GFP_KERNEL); 512 513 if (res) { 514 res->name = name; 515 res->start = start; 516 res->end = start + n - 1; 517 res->flags = IORESOURCE_BUSY; 518 519 write_lock(&resource_lock); 520 521 for (;;) { 522 struct resource *conflict; 523 524 conflict = __request_resource(parent, res); 525 if (!conflict) 526 break; 527 if (conflict != parent) { 528 parent = conflict; 529 if (!(conflict->flags & IORESOURCE_BUSY)) 530 continue; 531 } 532 533 /* Uhhuh, that didn't work out.. */ 534 kfree(res); 535 res = NULL; 536 break; 537 } 538 write_unlock(&resource_lock); 539 } 540 return res; 541 } 542 EXPORT_SYMBOL(__request_region); 543 544 /** 545 * __check_region - check if a resource region is busy or free 546 * @parent: parent resource descriptor 547 * @start: resource start address 548 * @n: resource region size 549 * 550 * Returns 0 if the region is free at the moment it is checked, 551 * returns %-EBUSY if the region is busy. 552 * 553 * NOTE: 554 * This function is deprecated because its use is racy. 555 * Even if it returns 0, a subsequent call to request_region() 556 * may fail because another driver etc. just allocated the region. 557 * Do NOT use it. It will be removed from the kernel. 558 */ 559 int __check_region(struct resource *parent, resource_size_t start, 560 resource_size_t n) 561 { 562 struct resource * res; 563 564 res = __request_region(parent, start, n, "check-region"); 565 if (!res) 566 return -EBUSY; 567 568 release_resource(res); 569 kfree(res); 570 return 0; 571 } 572 EXPORT_SYMBOL(__check_region); 573 574 /** 575 * __release_region - release a previously reserved resource region 576 * @parent: parent resource descriptor 577 * @start: resource start address 578 * @n: resource region size 579 * 580 * The described resource region must match a currently busy region. 581 */ 582 void __release_region(struct resource *parent, resource_size_t start, 583 resource_size_t n) 584 { 585 struct resource **p; 586 resource_size_t end; 587 588 p = &parent->child; 589 end = start + n - 1; 590 591 write_lock(&resource_lock); 592 593 for (;;) { 594 struct resource *res = *p; 595 596 if (!res) 597 break; 598 if (res->start <= start && res->end >= end) { 599 if (!(res->flags & IORESOURCE_BUSY)) { 600 p = &res->child; 601 continue; 602 } 603 if (res->start != start || res->end != end) 604 break; 605 *p = res->sibling; 606 write_unlock(&resource_lock); 607 kfree(res); 608 return; 609 } 610 p = &res->sibling; 611 } 612 613 write_unlock(&resource_lock); 614 615 printk(KERN_WARNING "Trying to free nonexistent resource " 616 "<%016llx-%016llx>\n", (unsigned long long)start, 617 (unsigned long long)end); 618 } 619 EXPORT_SYMBOL(__release_region); 620 621 /* 622 * Managed region resource 623 */ 624 struct region_devres { 625 struct resource *parent; 626 resource_size_t start; 627 resource_size_t n; 628 }; 629 630 static void devm_region_release(struct device *dev, void *res) 631 { 632 struct region_devres *this = res; 633 634 __release_region(this->parent, this->start, this->n); 635 } 636 637 static int devm_region_match(struct device *dev, void *res, void *match_data) 638 { 639 struct region_devres *this = res, *match = match_data; 640 641 return this->parent == match->parent && 642 this->start == match->start && this->n == match->n; 643 } 644 645 struct resource * __devm_request_region(struct device *dev, 646 struct resource *parent, resource_size_t start, 647 resource_size_t n, const char *name) 648 { 649 struct region_devres *dr = NULL; 650 struct resource *res; 651 652 dr = devres_alloc(devm_region_release, sizeof(struct region_devres), 653 GFP_KERNEL); 654 if (!dr) 655 return NULL; 656 657 dr->parent = parent; 658 dr->start = start; 659 dr->n = n; 660 661 res = __request_region(parent, start, n, name); 662 if (res) 663 devres_add(dev, dr); 664 else 665 devres_free(dr); 666 667 return res; 668 } 669 EXPORT_SYMBOL(__devm_request_region); 670 671 void __devm_release_region(struct device *dev, struct resource *parent, 672 resource_size_t start, resource_size_t n) 673 { 674 struct region_devres match_data = { parent, start, n }; 675 676 __release_region(parent, start, n); 677 WARN_ON(devres_destroy(dev, devm_region_release, devm_region_match, 678 &match_data)); 679 } 680 EXPORT_SYMBOL(__devm_release_region); 681 682 /* 683 * Called from init/main.c to reserve IO ports. 684 */ 685 #define MAXRESERVE 4 686 static int __init reserve_setup(char *str) 687 { 688 static int reserved; 689 static struct resource reserve[MAXRESERVE]; 690 691 for (;;) { 692 int io_start, io_num; 693 int x = reserved; 694 695 if (get_option (&str, &io_start) != 2) 696 break; 697 if (get_option (&str, &io_num) == 0) 698 break; 699 if (x < MAXRESERVE) { 700 struct resource *res = reserve + x; 701 res->name = "reserved"; 702 res->start = io_start; 703 res->end = io_start + io_num - 1; 704 res->flags = IORESOURCE_BUSY; 705 res->child = NULL; 706 if (request_resource(res->start >= 0x10000 ? &iomem_resource : &ioport_resource, res) == 0) 707 reserved = x+1; 708 } 709 } 710 return 1; 711 } 712 713 __setup("reserve=", reserve_setup); 714