1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/device.h> 3 #include <linux/pci.h> 4 #include "pci.h" 5 6 /* 7 * On the state of PCI's devres implementation: 8 * 9 * The older devres API for PCI has two significant problems: 10 * 11 * 1. It is very strongly tied to the statically allocated mapping table in 12 * struct pcim_iomap_devres below. This is mostly solved in the sense of the 13 * pcim_ functions in this file providing things like ranged mapping by 14 * bypassing this table, whereas the functions that were present in the old 15 * API still enter the mapping addresses into the table for users of the old 16 * API. 17 * 18 * 2. The region-request-functions in pci.c do become managed IF the device has 19 * been enabled with pcim_enable_device() instead of pci_enable_device(). 20 * This resulted in the API becoming inconsistent: Some functions have an 21 * obviously managed counter-part (e.g., pci_iomap() <-> pcim_iomap()), 22 * whereas some don't and are never managed, while others don't and are 23 * _sometimes_ managed (e.g. pci_request_region()). 24 * 25 * Consequently, in the new API, region requests performed by the pcim_ 26 * functions are automatically cleaned up through the devres callback 27 * pcim_addr_resource_release(). 28 * 29 * Users of pcim_enable_device() + pci_*region*() are redirected in 30 * pci.c to the managed functions here in this file. This isn't exactly 31 * perfect, but the only alternative way would be to port ALL drivers 32 * using said combination to pcim_ functions. 33 * 34 * TODO: 35 * Remove the legacy table entirely once all calls to pcim_iomap_table() in 36 * the kernel have been removed. 37 */ 38 39 /* 40 * Legacy struct storing addresses to whole mapped BARs. 41 */ 42 struct pcim_iomap_devres { 43 void __iomem *table[PCI_STD_NUM_BARS]; 44 }; 45 46 /* Used to restore the old INTx state on driver detach. */ 47 struct pcim_intx_devres { 48 int orig_intx; 49 }; 50 51 enum pcim_addr_devres_type { 52 /* Default initializer. */ 53 PCIM_ADDR_DEVRES_TYPE_INVALID, 54 55 /* A requested region spanning an entire BAR. */ 56 PCIM_ADDR_DEVRES_TYPE_REGION, 57 58 /* 59 * A requested region spanning an entire BAR, and a mapping for 60 * the entire BAR. 61 */ 62 PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING, 63 64 /* 65 * A mapping within a BAR, either spanning the whole BAR or just a 66 * range. Without a requested region. 67 */ 68 PCIM_ADDR_DEVRES_TYPE_MAPPING, 69 }; 70 71 /* 72 * This struct envelops IO or MEM addresses, i.e., mappings and region 73 * requests, because those are very frequently requested and released 74 * together. 75 */ 76 struct pcim_addr_devres { 77 enum pcim_addr_devres_type type; 78 void __iomem *baseaddr; 79 unsigned long offset; 80 unsigned long len; 81 int bar; 82 }; 83 84 static inline void pcim_addr_devres_clear(struct pcim_addr_devres *res) 85 { 86 memset(res, 0, sizeof(*res)); 87 res->bar = -1; 88 } 89 90 /* 91 * The following functions, __pcim_*_region*, exist as counterparts to the 92 * versions from pci.c - which, unfortunately, can be in "hybrid mode", i.e., 93 * sometimes managed, sometimes not. 94 * 95 * To separate the APIs cleanly, we define our own, simplified versions here. 96 */ 97 98 /** 99 * __pcim_request_region_range - Request a ranged region 100 * @pdev: PCI device the region belongs to 101 * @bar: BAR the range is within 102 * @offset: offset from the BAR's start address 103 * @maxlen: length in bytes, beginning at @offset 104 * @name: name of the driver requesting the resource 105 * @req_flags: flags for the request, e.g., for kernel-exclusive requests 106 * 107 * Returns: 0 on success, a negative error code on failure. 108 * 109 * Request a range within a device's PCI BAR. Sanity check the input. 110 */ 111 static int __pcim_request_region_range(struct pci_dev *pdev, int bar, 112 unsigned long offset, 113 unsigned long maxlen, 114 const char *name, int req_flags) 115 { 116 resource_size_t start = pci_resource_start(pdev, bar); 117 resource_size_t len = pci_resource_len(pdev, bar); 118 unsigned long dev_flags = pci_resource_flags(pdev, bar); 119 120 if (start == 0 || len == 0) /* Unused BAR. */ 121 return 0; 122 if (len <= offset) 123 return -EINVAL; 124 125 start += offset; 126 len -= offset; 127 128 if (len > maxlen && maxlen != 0) 129 len = maxlen; 130 131 if (dev_flags & IORESOURCE_IO) { 132 if (!request_region(start, len, name)) 133 return -EBUSY; 134 } else if (dev_flags & IORESOURCE_MEM) { 135 if (!__request_mem_region(start, len, name, req_flags)) 136 return -EBUSY; 137 } else { 138 /* That's not a device we can request anything on. */ 139 return -ENODEV; 140 } 141 142 return 0; 143 } 144 145 static void __pcim_release_region_range(struct pci_dev *pdev, int bar, 146 unsigned long offset, 147 unsigned long maxlen) 148 { 149 resource_size_t start = pci_resource_start(pdev, bar); 150 resource_size_t len = pci_resource_len(pdev, bar); 151 unsigned long flags = pci_resource_flags(pdev, bar); 152 153 if (len <= offset || start == 0) 154 return; 155 156 if (len == 0 || maxlen == 0) /* This an unused BAR. Do nothing. */ 157 return; 158 159 start += offset; 160 len -= offset; 161 162 if (len > maxlen) 163 len = maxlen; 164 165 if (flags & IORESOURCE_IO) 166 release_region(start, len); 167 else if (flags & IORESOURCE_MEM) 168 release_mem_region(start, len); 169 } 170 171 static int __pcim_request_region(struct pci_dev *pdev, int bar, 172 const char *name, int flags) 173 { 174 unsigned long offset = 0; 175 unsigned long len = pci_resource_len(pdev, bar); 176 177 return __pcim_request_region_range(pdev, bar, offset, len, name, flags); 178 } 179 180 static void __pcim_release_region(struct pci_dev *pdev, int bar) 181 { 182 unsigned long offset = 0; 183 unsigned long len = pci_resource_len(pdev, bar); 184 185 __pcim_release_region_range(pdev, bar, offset, len); 186 } 187 188 static void pcim_addr_resource_release(struct device *dev, void *resource_raw) 189 { 190 struct pci_dev *pdev = to_pci_dev(dev); 191 struct pcim_addr_devres *res = resource_raw; 192 193 switch (res->type) { 194 case PCIM_ADDR_DEVRES_TYPE_REGION: 195 __pcim_release_region(pdev, res->bar); 196 break; 197 case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: 198 pci_iounmap(pdev, res->baseaddr); 199 __pcim_release_region(pdev, res->bar); 200 break; 201 case PCIM_ADDR_DEVRES_TYPE_MAPPING: 202 pci_iounmap(pdev, res->baseaddr); 203 break; 204 default: 205 break; 206 } 207 } 208 209 static struct pcim_addr_devres *pcim_addr_devres_alloc(struct pci_dev *pdev) 210 { 211 struct pcim_addr_devres *res; 212 213 res = devres_alloc_node(pcim_addr_resource_release, sizeof(*res), 214 GFP_KERNEL, dev_to_node(&pdev->dev)); 215 if (res) 216 pcim_addr_devres_clear(res); 217 return res; 218 } 219 220 /* Just for consistency and readability. */ 221 static inline void pcim_addr_devres_free(struct pcim_addr_devres *res) 222 { 223 devres_free(res); 224 } 225 226 /* 227 * Used by devres to identify a pcim_addr_devres. 228 */ 229 static int pcim_addr_resources_match(struct device *dev, 230 void *a_raw, void *b_raw) 231 { 232 struct pcim_addr_devres *a, *b; 233 234 a = a_raw; 235 b = b_raw; 236 237 if (a->type != b->type) 238 return 0; 239 240 switch (a->type) { 241 case PCIM_ADDR_DEVRES_TYPE_REGION: 242 case PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING: 243 return a->bar == b->bar; 244 case PCIM_ADDR_DEVRES_TYPE_MAPPING: 245 return a->baseaddr == b->baseaddr; 246 default: 247 return 0; 248 } 249 } 250 251 static void devm_pci_unmap_iospace(struct device *dev, void *ptr) 252 { 253 struct resource **res = ptr; 254 255 pci_unmap_iospace(*res); 256 } 257 258 /** 259 * devm_pci_remap_iospace - Managed pci_remap_iospace() 260 * @dev: Generic device to remap IO address for 261 * @res: Resource describing the I/O space 262 * @phys_addr: physical address of range to be mapped 263 * 264 * Managed pci_remap_iospace(). Map is automatically unmapped on driver 265 * detach. 266 */ 267 int devm_pci_remap_iospace(struct device *dev, const struct resource *res, 268 phys_addr_t phys_addr) 269 { 270 const struct resource **ptr; 271 int error; 272 273 ptr = devres_alloc(devm_pci_unmap_iospace, sizeof(*ptr), GFP_KERNEL); 274 if (!ptr) 275 return -ENOMEM; 276 277 error = pci_remap_iospace(res, phys_addr); 278 if (error) { 279 devres_free(ptr); 280 } else { 281 *ptr = res; 282 devres_add(dev, ptr); 283 } 284 285 return error; 286 } 287 EXPORT_SYMBOL(devm_pci_remap_iospace); 288 289 /** 290 * devm_pci_remap_cfgspace - Managed pci_remap_cfgspace() 291 * @dev: Generic device to remap IO address for 292 * @offset: Resource address to map 293 * @size: Size of map 294 * 295 * Managed pci_remap_cfgspace(). Map is automatically unmapped on driver 296 * detach. 297 */ 298 void __iomem *devm_pci_remap_cfgspace(struct device *dev, 299 resource_size_t offset, 300 resource_size_t size) 301 { 302 void __iomem **ptr, *addr; 303 304 ptr = devres_alloc(devm_ioremap_release, sizeof(*ptr), GFP_KERNEL); 305 if (!ptr) 306 return NULL; 307 308 addr = pci_remap_cfgspace(offset, size); 309 if (addr) { 310 *ptr = addr; 311 devres_add(dev, ptr); 312 } else 313 devres_free(ptr); 314 315 return addr; 316 } 317 EXPORT_SYMBOL(devm_pci_remap_cfgspace); 318 319 /** 320 * devm_pci_remap_cfg_resource - check, request region and ioremap cfg resource 321 * @dev: generic device to handle the resource for 322 * @res: configuration space resource to be handled 323 * 324 * Checks that a resource is a valid memory region, requests the memory 325 * region and ioremaps with pci_remap_cfgspace() API that ensures the 326 * proper PCI configuration space memory attributes are guaranteed. 327 * 328 * All operations are managed and will be undone on driver detach. 329 * 330 * Returns a pointer to the remapped memory or an IOMEM_ERR_PTR() encoded error 331 * code on failure. Usage example:: 332 * 333 * res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 334 * base = devm_pci_remap_cfg_resource(&pdev->dev, res); 335 * if (IS_ERR(base)) 336 * return PTR_ERR(base); 337 */ 338 void __iomem *devm_pci_remap_cfg_resource(struct device *dev, 339 struct resource *res) 340 { 341 resource_size_t size; 342 const char *name; 343 void __iomem *dest_ptr; 344 345 BUG_ON(!dev); 346 347 if (!res || resource_type(res) != IORESOURCE_MEM) { 348 dev_err(dev, "invalid resource\n"); 349 return IOMEM_ERR_PTR(-EINVAL); 350 } 351 352 size = resource_size(res); 353 354 if (res->name) 355 name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev), 356 res->name); 357 else 358 name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL); 359 if (!name) 360 return IOMEM_ERR_PTR(-ENOMEM); 361 362 if (!devm_request_mem_region(dev, res->start, size, name)) { 363 dev_err(dev, "can't request region for resource %pR\n", res); 364 return IOMEM_ERR_PTR(-EBUSY); 365 } 366 367 dest_ptr = devm_pci_remap_cfgspace(dev, res->start, size); 368 if (!dest_ptr) { 369 dev_err(dev, "ioremap failed for resource %pR\n", res); 370 devm_release_mem_region(dev, res->start, size); 371 dest_ptr = IOMEM_ERR_PTR(-ENOMEM); 372 } 373 374 return dest_ptr; 375 } 376 EXPORT_SYMBOL(devm_pci_remap_cfg_resource); 377 378 static void __pcim_clear_mwi(void *pdev_raw) 379 { 380 struct pci_dev *pdev = pdev_raw; 381 382 pci_clear_mwi(pdev); 383 } 384 385 /** 386 * pcim_set_mwi - a device-managed pci_set_mwi() 387 * @pdev: the PCI device for which MWI is enabled 388 * 389 * Managed pci_set_mwi(). 390 * 391 * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 392 */ 393 int pcim_set_mwi(struct pci_dev *pdev) 394 { 395 int ret; 396 397 ret = devm_add_action(&pdev->dev, __pcim_clear_mwi, pdev); 398 if (ret != 0) 399 return ret; 400 401 ret = pci_set_mwi(pdev); 402 if (ret != 0) 403 devm_remove_action(&pdev->dev, __pcim_clear_mwi, pdev); 404 405 return ret; 406 } 407 EXPORT_SYMBOL(pcim_set_mwi); 408 409 static inline bool mask_contains_bar(int mask, int bar) 410 { 411 return mask & BIT(bar); 412 } 413 414 static void pcim_intx_restore(struct device *dev, void *data) 415 { 416 struct pci_dev *pdev = to_pci_dev(dev); 417 struct pcim_intx_devres *res = data; 418 419 pci_intx(pdev, res->orig_intx); 420 } 421 422 static struct pcim_intx_devres *get_or_create_intx_devres(struct device *dev) 423 { 424 struct pcim_intx_devres *res; 425 426 res = devres_find(dev, pcim_intx_restore, NULL, NULL); 427 if (res) 428 return res; 429 430 res = devres_alloc(pcim_intx_restore, sizeof(*res), GFP_KERNEL); 431 if (res) 432 devres_add(dev, res); 433 434 return res; 435 } 436 437 /** 438 * pcim_intx - managed pci_intx() 439 * @pdev: the PCI device to operate on 440 * @enable: boolean: whether to enable or disable PCI INTx 441 * 442 * Returns: 0 on success, -ENOMEM on error. 443 * 444 * Enable/disable PCI INTx for device @pdev. 445 * Restore the original state on driver detach. 446 */ 447 int pcim_intx(struct pci_dev *pdev, int enable) 448 { 449 struct pcim_intx_devres *res; 450 451 res = get_or_create_intx_devres(&pdev->dev); 452 if (!res) 453 return -ENOMEM; 454 455 res->orig_intx = !enable; 456 pci_intx(pdev, enable); 457 458 return 0; 459 } 460 EXPORT_SYMBOL_GPL(pcim_intx); 461 462 static void pcim_disable_device(void *pdev_raw) 463 { 464 struct pci_dev *pdev = pdev_raw; 465 466 if (!pdev->pinned) 467 pci_disable_device(pdev); 468 469 pdev->is_managed = false; 470 } 471 472 /** 473 * pcim_enable_device - Managed pci_enable_device() 474 * @pdev: PCI device to be initialized 475 * 476 * Returns: 0 on success, negative error code on failure. 477 * 478 * Managed pci_enable_device(). Device will automatically be disabled on 479 * driver detach. 480 */ 481 int pcim_enable_device(struct pci_dev *pdev) 482 { 483 int ret; 484 485 ret = devm_add_action(&pdev->dev, pcim_disable_device, pdev); 486 if (ret != 0) 487 return ret; 488 489 /* 490 * We prefer removing the action in case of an error over 491 * devm_add_action_or_reset() because the latter could theoretically be 492 * disturbed by users having pinned the device too soon. 493 */ 494 ret = pci_enable_device(pdev); 495 if (ret != 0) { 496 devm_remove_action(&pdev->dev, pcim_disable_device, pdev); 497 return ret; 498 } 499 500 pdev->is_managed = true; 501 502 return ret; 503 } 504 EXPORT_SYMBOL(pcim_enable_device); 505 506 /** 507 * pcim_pin_device - Pin managed PCI device 508 * @pdev: PCI device to pin 509 * 510 * Pin managed PCI device @pdev. Pinned device won't be disabled on driver 511 * detach. @pdev must have been enabled with pcim_enable_device(). 512 */ 513 void pcim_pin_device(struct pci_dev *pdev) 514 { 515 pdev->pinned = true; 516 } 517 EXPORT_SYMBOL(pcim_pin_device); 518 519 static void pcim_iomap_release(struct device *gendev, void *res) 520 { 521 /* 522 * Do nothing. This is legacy code. 523 * 524 * Cleanup of the mappings is now done directly through the callbacks 525 * registered when creating them. 526 */ 527 } 528 529 /** 530 * pcim_iomap_table - access iomap allocation table (DEPRECATED) 531 * @pdev: PCI device to access iomap table for 532 * 533 * Returns: 534 * Const pointer to array of __iomem pointers on success, NULL on failure. 535 * 536 * Access iomap allocation table for @dev. If iomap table doesn't 537 * exist and @pdev is managed, it will be allocated. All iomaps 538 * recorded in the iomap table are automatically unmapped on driver 539 * detach. 540 * 541 * This function might sleep when the table is first allocated but can 542 * be safely called without context and guaranteed to succeed once 543 * allocated. 544 * 545 * This function is DEPRECATED. Do not use it in new code. Instead, obtain a 546 * mapping's address directly from one of the pcim_* mapping functions. For 547 * example: 548 * void __iomem \*mappy = pcim_iomap(pdev, bar, length); 549 */ 550 void __iomem * const *pcim_iomap_table(struct pci_dev *pdev) 551 { 552 struct pcim_iomap_devres *dr, *new_dr; 553 554 dr = devres_find(&pdev->dev, pcim_iomap_release, NULL, NULL); 555 if (dr) 556 return dr->table; 557 558 new_dr = devres_alloc_node(pcim_iomap_release, sizeof(*new_dr), GFP_KERNEL, 559 dev_to_node(&pdev->dev)); 560 if (!new_dr) 561 return NULL; 562 dr = devres_get(&pdev->dev, new_dr, NULL, NULL); 563 return dr->table; 564 } 565 EXPORT_SYMBOL(pcim_iomap_table); 566 567 /* 568 * Fill the legacy mapping-table, so that drivers using the old API can 569 * still get a BAR's mapping address through pcim_iomap_table(). 570 */ 571 static int pcim_add_mapping_to_legacy_table(struct pci_dev *pdev, 572 void __iomem *mapping, int bar) 573 { 574 void __iomem **legacy_iomap_table; 575 576 if (bar >= PCI_STD_NUM_BARS) 577 return -EINVAL; 578 579 legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); 580 if (!legacy_iomap_table) 581 return -ENOMEM; 582 583 /* The legacy mechanism doesn't allow for duplicate mappings. */ 584 WARN_ON(legacy_iomap_table[bar]); 585 586 legacy_iomap_table[bar] = mapping; 587 588 return 0; 589 } 590 591 /* 592 * Remove a mapping. The table only contains whole-BAR mappings, so this will 593 * never interfere with ranged mappings. 594 */ 595 static void pcim_remove_mapping_from_legacy_table(struct pci_dev *pdev, 596 void __iomem *addr) 597 { 598 int bar; 599 void __iomem **legacy_iomap_table; 600 601 legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); 602 if (!legacy_iomap_table) 603 return; 604 605 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { 606 if (legacy_iomap_table[bar] == addr) { 607 legacy_iomap_table[bar] = NULL; 608 return; 609 } 610 } 611 } 612 613 /* 614 * The same as pcim_remove_mapping_from_legacy_table(), but identifies the 615 * mapping by its BAR index. 616 */ 617 static void pcim_remove_bar_from_legacy_table(struct pci_dev *pdev, int bar) 618 { 619 void __iomem **legacy_iomap_table; 620 621 if (bar >= PCI_STD_NUM_BARS) 622 return; 623 624 legacy_iomap_table = (void __iomem **)pcim_iomap_table(pdev); 625 if (!legacy_iomap_table) 626 return; 627 628 legacy_iomap_table[bar] = NULL; 629 } 630 631 /** 632 * pcim_iomap - Managed pcim_iomap() 633 * @pdev: PCI device to iomap for 634 * @bar: BAR to iomap 635 * @maxlen: Maximum length of iomap 636 * 637 * Returns: __iomem pointer on success, NULL on failure. 638 * 639 * Managed pci_iomap(). Map is automatically unmapped on driver detach. If 640 * desired, unmap manually only with pcim_iounmap(). 641 * 642 * This SHOULD only be used once per BAR. 643 * 644 * NOTE: 645 * Contrary to the other pcim_* functions, this function does not return an 646 * IOMEM_ERR_PTR() on failure, but a simple NULL. This is done for backwards 647 * compatibility. 648 */ 649 void __iomem *pcim_iomap(struct pci_dev *pdev, int bar, unsigned long maxlen) 650 { 651 void __iomem *mapping; 652 struct pcim_addr_devres *res; 653 654 res = pcim_addr_devres_alloc(pdev); 655 if (!res) 656 return NULL; 657 res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING; 658 659 mapping = pci_iomap(pdev, bar, maxlen); 660 if (!mapping) 661 goto err_iomap; 662 res->baseaddr = mapping; 663 664 if (pcim_add_mapping_to_legacy_table(pdev, mapping, bar) != 0) 665 goto err_table; 666 667 devres_add(&pdev->dev, res); 668 return mapping; 669 670 err_table: 671 pci_iounmap(pdev, mapping); 672 err_iomap: 673 pcim_addr_devres_free(res); 674 return NULL; 675 } 676 EXPORT_SYMBOL(pcim_iomap); 677 678 /** 679 * pcim_iounmap - Managed pci_iounmap() 680 * @pdev: PCI device to iounmap for 681 * @addr: Address to unmap 682 * 683 * Managed pci_iounmap(). @addr must have been mapped using a pcim_* mapping 684 * function. 685 */ 686 void pcim_iounmap(struct pci_dev *pdev, void __iomem *addr) 687 { 688 struct pcim_addr_devres res_searched; 689 690 pcim_addr_devres_clear(&res_searched); 691 res_searched.type = PCIM_ADDR_DEVRES_TYPE_MAPPING; 692 res_searched.baseaddr = addr; 693 694 if (devres_release(&pdev->dev, pcim_addr_resource_release, 695 pcim_addr_resources_match, &res_searched) != 0) { 696 /* Doesn't exist. User passed nonsense. */ 697 return; 698 } 699 700 pcim_remove_mapping_from_legacy_table(pdev, addr); 701 } 702 EXPORT_SYMBOL(pcim_iounmap); 703 704 /** 705 * pcim_iomap_region - Request and iomap a PCI BAR 706 * @pdev: PCI device to map IO resources for 707 * @bar: Index of a BAR to map 708 * @name: Name of the driver requesting the resource 709 * 710 * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. 711 * 712 * Mapping and region will get automatically released on driver detach. If 713 * desired, release manually only with pcim_iounmap_region(). 714 */ 715 void __iomem *pcim_iomap_region(struct pci_dev *pdev, int bar, 716 const char *name) 717 { 718 int ret; 719 struct pcim_addr_devres *res; 720 721 res = pcim_addr_devres_alloc(pdev); 722 if (!res) 723 return IOMEM_ERR_PTR(-ENOMEM); 724 725 res->type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; 726 res->bar = bar; 727 728 ret = __pcim_request_region(pdev, bar, name, 0); 729 if (ret != 0) 730 goto err_region; 731 732 res->baseaddr = pci_iomap(pdev, bar, 0); 733 if (!res->baseaddr) { 734 ret = -EINVAL; 735 goto err_iomap; 736 } 737 738 devres_add(&pdev->dev, res); 739 return res->baseaddr; 740 741 err_iomap: 742 __pcim_release_region(pdev, bar); 743 err_region: 744 pcim_addr_devres_free(res); 745 746 return IOMEM_ERR_PTR(ret); 747 } 748 EXPORT_SYMBOL(pcim_iomap_region); 749 750 /** 751 * pcim_iounmap_region - Unmap and release a PCI BAR 752 * @pdev: PCI device to operate on 753 * @bar: Index of BAR to unmap and release 754 * 755 * Unmap a BAR and release its region manually. Only pass BARs that were 756 * previously mapped by pcim_iomap_region(). 757 */ 758 void pcim_iounmap_region(struct pci_dev *pdev, int bar) 759 { 760 struct pcim_addr_devres res_searched; 761 762 pcim_addr_devres_clear(&res_searched); 763 res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION_MAPPING; 764 res_searched.bar = bar; 765 766 devres_release(&pdev->dev, pcim_addr_resource_release, 767 pcim_addr_resources_match, &res_searched); 768 } 769 EXPORT_SYMBOL(pcim_iounmap_region); 770 771 /** 772 * pcim_iomap_regions - Request and iomap PCI BARs (DEPRECATED) 773 * @pdev: PCI device to map IO resources for 774 * @mask: Mask of BARs to request and iomap 775 * @name: Name of the driver requesting the resources 776 * 777 * Returns: 0 on success, negative error code on failure. 778 * 779 * Request and iomap regions specified by @mask. 780 * 781 * This function is DEPRECATED. Do not use it in new code. 782 * Use pcim_iomap_region() instead. 783 */ 784 int pcim_iomap_regions(struct pci_dev *pdev, int mask, const char *name) 785 { 786 int ret; 787 int bar; 788 void __iomem *mapping; 789 790 for (bar = 0; bar < DEVICE_COUNT_RESOURCE; bar++) { 791 if (!mask_contains_bar(mask, bar)) 792 continue; 793 794 mapping = pcim_iomap_region(pdev, bar, name); 795 if (IS_ERR(mapping)) { 796 ret = PTR_ERR(mapping); 797 goto err; 798 } 799 ret = pcim_add_mapping_to_legacy_table(pdev, mapping, bar); 800 if (ret != 0) 801 goto err; 802 } 803 804 return 0; 805 806 err: 807 while (--bar >= 0) { 808 pcim_iounmap_region(pdev, bar); 809 pcim_remove_bar_from_legacy_table(pdev, bar); 810 } 811 812 return ret; 813 } 814 EXPORT_SYMBOL(pcim_iomap_regions); 815 816 static int _pcim_request_region(struct pci_dev *pdev, int bar, const char *name, 817 int request_flags) 818 { 819 int ret; 820 struct pcim_addr_devres *res; 821 822 res = pcim_addr_devres_alloc(pdev); 823 if (!res) 824 return -ENOMEM; 825 res->type = PCIM_ADDR_DEVRES_TYPE_REGION; 826 res->bar = bar; 827 828 ret = __pcim_request_region(pdev, bar, name, request_flags); 829 if (ret != 0) { 830 pcim_addr_devres_free(res); 831 return ret; 832 } 833 834 devres_add(&pdev->dev, res); 835 return 0; 836 } 837 838 /** 839 * pcim_request_region - Request a PCI BAR 840 * @pdev: PCI device to request region for 841 * @bar: Index of BAR to request 842 * @name: Name of the driver requesting the resource 843 * 844 * Returns: 0 on success, a negative error code on failure. 845 * 846 * Request region specified by @bar. 847 * 848 * The region will automatically be released on driver detach. If desired, 849 * release manually only with pcim_release_region(). 850 */ 851 int pcim_request_region(struct pci_dev *pdev, int bar, const char *name) 852 { 853 return _pcim_request_region(pdev, bar, name, 0); 854 } 855 EXPORT_SYMBOL(pcim_request_region); 856 857 /** 858 * pcim_request_region_exclusive - Request a PCI BAR exclusively 859 * @pdev: PCI device to request region for 860 * @bar: Index of BAR to request 861 * @name: Name of the driver requesting the resource 862 * 863 * Returns: 0 on success, a negative error code on failure. 864 * 865 * Request region specified by @bar exclusively. 866 * 867 * The region will automatically be released on driver detach. If desired, 868 * release manually only with pcim_release_region(). 869 */ 870 int pcim_request_region_exclusive(struct pci_dev *pdev, int bar, const char *name) 871 { 872 return _pcim_request_region(pdev, bar, name, IORESOURCE_EXCLUSIVE); 873 } 874 875 /** 876 * pcim_release_region - Release a PCI BAR 877 * @pdev: PCI device to operate on 878 * @bar: Index of BAR to release 879 * 880 * Release a region manually that was previously requested by 881 * pcim_request_region(). 882 */ 883 void pcim_release_region(struct pci_dev *pdev, int bar) 884 { 885 struct pcim_addr_devres res_searched; 886 887 pcim_addr_devres_clear(&res_searched); 888 res_searched.type = PCIM_ADDR_DEVRES_TYPE_REGION; 889 res_searched.bar = bar; 890 891 devres_release(&pdev->dev, pcim_addr_resource_release, 892 pcim_addr_resources_match, &res_searched); 893 } 894 895 896 /** 897 * pcim_release_all_regions - Release all regions of a PCI-device 898 * @pdev: the PCI device 899 * 900 * Release all regions previously requested through pcim_request_region() 901 * or pcim_request_all_regions(). 902 * 903 * Can be called from any context, i.e., not necessarily as a counterpart to 904 * pcim_request_all_regions(). 905 */ 906 static void pcim_release_all_regions(struct pci_dev *pdev) 907 { 908 int bar; 909 910 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) 911 pcim_release_region(pdev, bar); 912 } 913 914 /** 915 * pcim_request_all_regions - Request all regions 916 * @pdev: PCI device to map IO resources for 917 * @name: name of the driver requesting the resources 918 * 919 * Returns: 0 on success, negative error code on failure. 920 * 921 * Requested regions will automatically be released at driver detach. If 922 * desired, release individual regions with pcim_release_region() or all of 923 * them at once with pcim_release_all_regions(). 924 */ 925 int pcim_request_all_regions(struct pci_dev *pdev, const char *name) 926 { 927 int ret; 928 int bar; 929 930 for (bar = 0; bar < PCI_STD_NUM_BARS; bar++) { 931 ret = pcim_request_region(pdev, bar, name); 932 if (ret != 0) 933 goto err; 934 } 935 936 return 0; 937 938 err: 939 pcim_release_all_regions(pdev); 940 941 return ret; 942 } 943 EXPORT_SYMBOL(pcim_request_all_regions); 944 945 /** 946 * pcim_iounmap_regions - Unmap and release PCI BARs (DEPRECATED) 947 * @pdev: PCI device to map IO resources for 948 * @mask: Mask of BARs to unmap and release 949 * 950 * Unmap and release regions specified by @mask. 951 * 952 * This function is DEPRECATED. Do not use it in new code. 953 * Use pcim_iounmap_region() instead. 954 */ 955 void pcim_iounmap_regions(struct pci_dev *pdev, int mask) 956 { 957 int i; 958 959 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 960 if (!mask_contains_bar(mask, i)) 961 continue; 962 963 pcim_iounmap_region(pdev, i); 964 pcim_remove_bar_from_legacy_table(pdev, i); 965 } 966 } 967 EXPORT_SYMBOL(pcim_iounmap_regions); 968 969 /** 970 * pcim_iomap_range - Create a ranged __iomap mapping within a PCI BAR 971 * @pdev: PCI device to map IO resources for 972 * @bar: Index of the BAR 973 * @offset: Offset from the begin of the BAR 974 * @len: Length in bytes for the mapping 975 * 976 * Returns: __iomem pointer on success, an IOMEM_ERR_PTR on failure. 977 * 978 * Creates a new IO-Mapping within the specified @bar, ranging from @offset to 979 * @offset + @len. 980 * 981 * The mapping will automatically get unmapped on driver detach. If desired, 982 * release manually only with pcim_iounmap(). 983 */ 984 void __iomem *pcim_iomap_range(struct pci_dev *pdev, int bar, 985 unsigned long offset, unsigned long len) 986 { 987 void __iomem *mapping; 988 struct pcim_addr_devres *res; 989 990 res = pcim_addr_devres_alloc(pdev); 991 if (!res) 992 return IOMEM_ERR_PTR(-ENOMEM); 993 994 mapping = pci_iomap_range(pdev, bar, offset, len); 995 if (!mapping) { 996 pcim_addr_devres_free(res); 997 return IOMEM_ERR_PTR(-EINVAL); 998 } 999 1000 res->type = PCIM_ADDR_DEVRES_TYPE_MAPPING; 1001 res->baseaddr = mapping; 1002 1003 /* 1004 * Ranged mappings don't get added to the legacy-table, since the table 1005 * only ever keeps track of whole BARs. 1006 */ 1007 1008 devres_add(&pdev->dev, res); 1009 return mapping; 1010 } 1011 EXPORT_SYMBOL(pcim_iomap_range); 1012