1 // SPDX-License-Identifier: GPL-2.0 2 /** 3 * PCI Endpoint *Controller* (EPC) library 4 * 5 * Copyright (C) 2017 Texas Instruments 6 * Author: Kishon Vijay Abraham I <kishon@ti.com> 7 */ 8 9 #include <linux/device.h> 10 #include <linux/slab.h> 11 #include <linux/module.h> 12 #include <linux/of_device.h> 13 14 #include <linux/pci-epc.h> 15 #include <linux/pci-epf.h> 16 #include <linux/pci-ep-cfs.h> 17 18 static struct class *pci_epc_class; 19 20 static void devm_pci_epc_release(struct device *dev, void *res) 21 { 22 struct pci_epc *epc = *(struct pci_epc **)res; 23 24 pci_epc_destroy(epc); 25 } 26 27 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data) 28 { 29 struct pci_epc **epc = res; 30 31 return *epc == match_data; 32 } 33 34 /** 35 * pci_epc_put() - release the PCI endpoint controller 36 * @epc: epc returned by pci_epc_get() 37 * 38 * release the refcount the caller obtained by invoking pci_epc_get() 39 */ 40 void pci_epc_put(struct pci_epc *epc) 41 { 42 if (!epc || IS_ERR(epc)) 43 return; 44 45 module_put(epc->ops->owner); 46 put_device(&epc->dev); 47 } 48 EXPORT_SYMBOL_GPL(pci_epc_put); 49 50 /** 51 * pci_epc_get() - get the PCI endpoint controller 52 * @epc_name: device name of the endpoint controller 53 * 54 * Invoke to get struct pci_epc * corresponding to the device name of the 55 * endpoint controller 56 */ 57 struct pci_epc *pci_epc_get(const char *epc_name) 58 { 59 int ret = -EINVAL; 60 struct pci_epc *epc; 61 struct device *dev; 62 struct class_dev_iter iter; 63 64 class_dev_iter_init(&iter, pci_epc_class, NULL, NULL); 65 while ((dev = class_dev_iter_next(&iter))) { 66 if (strcmp(epc_name, dev_name(dev))) 67 continue; 68 69 epc = to_pci_epc(dev); 70 if (!try_module_get(epc->ops->owner)) { 71 ret = -EINVAL; 72 goto err; 73 } 74 75 class_dev_iter_exit(&iter); 76 get_device(&epc->dev); 77 return epc; 78 } 79 80 err: 81 class_dev_iter_exit(&iter); 82 return ERR_PTR(ret); 83 } 84 EXPORT_SYMBOL_GPL(pci_epc_get); 85 86 /** 87 * pci_epc_get_first_free_bar() - helper to get first unreserved BAR 88 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap 89 * 90 * Invoke to get the first unreserved BAR that can be used for endpoint 91 * function. For any incorrect value in reserved_bar return '0'. 92 */ 93 unsigned int pci_epc_get_first_free_bar(const struct pci_epc_features 94 *epc_features) 95 { 96 int free_bar; 97 98 if (!epc_features) 99 return 0; 100 101 free_bar = ffz(epc_features->reserved_bar); 102 if (free_bar > 5) 103 return 0; 104 105 return free_bar; 106 } 107 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar); 108 109 /** 110 * pci_epc_get_features() - get the features supported by EPC 111 * @epc: the features supported by *this* EPC device will be returned 112 * @func_no: the features supported by the EPC device specific to the 113 * endpoint function with func_no will be returned 114 * 115 * Invoke to get the features provided by the EPC which may be 116 * specific to an endpoint function. Returns pci_epc_features on success 117 * and NULL for any failures. 118 */ 119 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc, 120 u8 func_no) 121 { 122 const struct pci_epc_features *epc_features; 123 unsigned long flags; 124 125 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 126 return NULL; 127 128 if (!epc->ops->get_features) 129 return NULL; 130 131 spin_lock_irqsave(&epc->lock, flags); 132 epc_features = epc->ops->get_features(epc, func_no); 133 spin_unlock_irqrestore(&epc->lock, flags); 134 135 return epc_features; 136 } 137 EXPORT_SYMBOL_GPL(pci_epc_get_features); 138 139 /** 140 * pci_epc_stop() - stop the PCI link 141 * @epc: the link of the EPC device that has to be stopped 142 * 143 * Invoke to stop the PCI link 144 */ 145 void pci_epc_stop(struct pci_epc *epc) 146 { 147 unsigned long flags; 148 149 if (IS_ERR(epc) || !epc->ops->stop) 150 return; 151 152 spin_lock_irqsave(&epc->lock, flags); 153 epc->ops->stop(epc); 154 spin_unlock_irqrestore(&epc->lock, flags); 155 } 156 EXPORT_SYMBOL_GPL(pci_epc_stop); 157 158 /** 159 * pci_epc_start() - start the PCI link 160 * @epc: the link of *this* EPC device has to be started 161 * 162 * Invoke to start the PCI link 163 */ 164 int pci_epc_start(struct pci_epc *epc) 165 { 166 int ret; 167 unsigned long flags; 168 169 if (IS_ERR(epc)) 170 return -EINVAL; 171 172 if (!epc->ops->start) 173 return 0; 174 175 spin_lock_irqsave(&epc->lock, flags); 176 ret = epc->ops->start(epc); 177 spin_unlock_irqrestore(&epc->lock, flags); 178 179 return ret; 180 } 181 EXPORT_SYMBOL_GPL(pci_epc_start); 182 183 /** 184 * pci_epc_raise_irq() - interrupt the host system 185 * @epc: the EPC device which has to interrupt the host 186 * @func_no: the endpoint function number in the EPC device 187 * @type: specify the type of interrupt; legacy, MSI or MSI-X 188 * @interrupt_num: the MSI or MSI-X interrupt number 189 * 190 * Invoke to raise an legacy, MSI or MSI-X interrupt 191 */ 192 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, 193 enum pci_epc_irq_type type, u16 interrupt_num) 194 { 195 int ret; 196 unsigned long flags; 197 198 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 199 return -EINVAL; 200 201 if (!epc->ops->raise_irq) 202 return 0; 203 204 spin_lock_irqsave(&epc->lock, flags); 205 ret = epc->ops->raise_irq(epc, func_no, type, interrupt_num); 206 spin_unlock_irqrestore(&epc->lock, flags); 207 208 return ret; 209 } 210 EXPORT_SYMBOL_GPL(pci_epc_raise_irq); 211 212 /** 213 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated 214 * @epc: the EPC device to which MSI interrupts was requested 215 * @func_no: the endpoint function number in the EPC device 216 * 217 * Invoke to get the number of MSI interrupts allocated by the RC 218 */ 219 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no) 220 { 221 int interrupt; 222 unsigned long flags; 223 224 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 225 return 0; 226 227 if (!epc->ops->get_msi) 228 return 0; 229 230 spin_lock_irqsave(&epc->lock, flags); 231 interrupt = epc->ops->get_msi(epc, func_no); 232 spin_unlock_irqrestore(&epc->lock, flags); 233 234 if (interrupt < 0) 235 return 0; 236 237 interrupt = 1 << interrupt; 238 239 return interrupt; 240 } 241 EXPORT_SYMBOL_GPL(pci_epc_get_msi); 242 243 /** 244 * pci_epc_set_msi() - set the number of MSI interrupt numbers required 245 * @epc: the EPC device on which MSI has to be configured 246 * @func_no: the endpoint function number in the EPC device 247 * @interrupts: number of MSI interrupts required by the EPF 248 * 249 * Invoke to set the required number of MSI interrupts. 250 */ 251 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts) 252 { 253 int ret; 254 u8 encode_int; 255 unsigned long flags; 256 257 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 258 interrupts > 32) 259 return -EINVAL; 260 261 if (!epc->ops->set_msi) 262 return 0; 263 264 encode_int = order_base_2(interrupts); 265 266 spin_lock_irqsave(&epc->lock, flags); 267 ret = epc->ops->set_msi(epc, func_no, encode_int); 268 spin_unlock_irqrestore(&epc->lock, flags); 269 270 return ret; 271 } 272 EXPORT_SYMBOL_GPL(pci_epc_set_msi); 273 274 /** 275 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated 276 * @epc: the EPC device to which MSI-X interrupts was requested 277 * @func_no: the endpoint function number in the EPC device 278 * 279 * Invoke to get the number of MSI-X interrupts allocated by the RC 280 */ 281 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no) 282 { 283 int interrupt; 284 unsigned long flags; 285 286 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 287 return 0; 288 289 if (!epc->ops->get_msix) 290 return 0; 291 292 spin_lock_irqsave(&epc->lock, flags); 293 interrupt = epc->ops->get_msix(epc, func_no); 294 spin_unlock_irqrestore(&epc->lock, flags); 295 296 if (interrupt < 0) 297 return 0; 298 299 return interrupt + 1; 300 } 301 EXPORT_SYMBOL_GPL(pci_epc_get_msix); 302 303 /** 304 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required 305 * @epc: the EPC device on which MSI-X has to be configured 306 * @func_no: the endpoint function number in the EPC device 307 * @interrupts: number of MSI-X interrupts required by the EPF 308 * 309 * Invoke to set the required number of MSI-X interrupts. 310 */ 311 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts) 312 { 313 int ret; 314 unsigned long flags; 315 316 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 317 interrupts < 1 || interrupts > 2048) 318 return -EINVAL; 319 320 if (!epc->ops->set_msix) 321 return 0; 322 323 spin_lock_irqsave(&epc->lock, flags); 324 ret = epc->ops->set_msix(epc, func_no, interrupts - 1); 325 spin_unlock_irqrestore(&epc->lock, flags); 326 327 return ret; 328 } 329 EXPORT_SYMBOL_GPL(pci_epc_set_msix); 330 331 /** 332 * pci_epc_unmap_addr() - unmap CPU address from PCI address 333 * @epc: the EPC device on which address is allocated 334 * @func_no: the endpoint function number in the EPC device 335 * @phys_addr: physical address of the local system 336 * 337 * Invoke to unmap the CPU address from PCI address. 338 */ 339 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, 340 phys_addr_t phys_addr) 341 { 342 unsigned long flags; 343 344 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 345 return; 346 347 if (!epc->ops->unmap_addr) 348 return; 349 350 spin_lock_irqsave(&epc->lock, flags); 351 epc->ops->unmap_addr(epc, func_no, phys_addr); 352 spin_unlock_irqrestore(&epc->lock, flags); 353 } 354 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr); 355 356 /** 357 * pci_epc_map_addr() - map CPU address to PCI address 358 * @epc: the EPC device on which address is allocated 359 * @func_no: the endpoint function number in the EPC device 360 * @phys_addr: physical address of the local system 361 * @pci_addr: PCI address to which the physical address should be mapped 362 * @size: the size of the allocation 363 * 364 * Invoke to map CPU address with PCI address. 365 */ 366 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, 367 phys_addr_t phys_addr, u64 pci_addr, size_t size) 368 { 369 int ret; 370 unsigned long flags; 371 372 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 373 return -EINVAL; 374 375 if (!epc->ops->map_addr) 376 return 0; 377 378 spin_lock_irqsave(&epc->lock, flags); 379 ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size); 380 spin_unlock_irqrestore(&epc->lock, flags); 381 382 return ret; 383 } 384 EXPORT_SYMBOL_GPL(pci_epc_map_addr); 385 386 /** 387 * pci_epc_clear_bar() - reset the BAR 388 * @epc: the EPC device for which the BAR has to be cleared 389 * @func_no: the endpoint function number in the EPC device 390 * @epf_bar: the struct epf_bar that contains the BAR information 391 * 392 * Invoke to reset the BAR of the endpoint device. 393 */ 394 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, 395 struct pci_epf_bar *epf_bar) 396 { 397 unsigned long flags; 398 399 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 400 (epf_bar->barno == BAR_5 && 401 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)) 402 return; 403 404 if (!epc->ops->clear_bar) 405 return; 406 407 spin_lock_irqsave(&epc->lock, flags); 408 epc->ops->clear_bar(epc, func_no, epf_bar); 409 spin_unlock_irqrestore(&epc->lock, flags); 410 } 411 EXPORT_SYMBOL_GPL(pci_epc_clear_bar); 412 413 /** 414 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space 415 * @epc: the EPC device on which BAR has to be configured 416 * @func_no: the endpoint function number in the EPC device 417 * @epf_bar: the struct epf_bar that contains the BAR information 418 * 419 * Invoke to configure the BAR of the endpoint device. 420 */ 421 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, 422 struct pci_epf_bar *epf_bar) 423 { 424 int ret; 425 unsigned long irq_flags; 426 int flags = epf_bar->flags; 427 428 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 429 (epf_bar->barno == BAR_5 && 430 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) || 431 (flags & PCI_BASE_ADDRESS_SPACE_IO && 432 flags & PCI_BASE_ADDRESS_IO_MASK) || 433 (upper_32_bits(epf_bar->size) && 434 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))) 435 return -EINVAL; 436 437 if (!epc->ops->set_bar) 438 return 0; 439 440 spin_lock_irqsave(&epc->lock, irq_flags); 441 ret = epc->ops->set_bar(epc, func_no, epf_bar); 442 spin_unlock_irqrestore(&epc->lock, irq_flags); 443 444 return ret; 445 } 446 EXPORT_SYMBOL_GPL(pci_epc_set_bar); 447 448 /** 449 * pci_epc_write_header() - write standard configuration header 450 * @epc: the EPC device to which the configuration header should be written 451 * @func_no: the endpoint function number in the EPC device 452 * @header: standard configuration header fields 453 * 454 * Invoke to write the configuration header to the endpoint controller. Every 455 * endpoint controller will have a dedicated location to which the standard 456 * configuration header would be written. The callback function should write 457 * the header fields to this dedicated location. 458 */ 459 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, 460 struct pci_epf_header *header) 461 { 462 int ret; 463 unsigned long flags; 464 465 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 466 return -EINVAL; 467 468 if (!epc->ops->write_header) 469 return 0; 470 471 spin_lock_irqsave(&epc->lock, flags); 472 ret = epc->ops->write_header(epc, func_no, header); 473 spin_unlock_irqrestore(&epc->lock, flags); 474 475 return ret; 476 } 477 EXPORT_SYMBOL_GPL(pci_epc_write_header); 478 479 /** 480 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller 481 * @epc: the EPC device to which the endpoint function should be added 482 * @epf: the endpoint function to be added 483 * 484 * A PCI endpoint device can have one or more functions. In the case of PCIe, 485 * the specification allows up to 8 PCIe endpoint functions. Invoke 486 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller. 487 */ 488 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf) 489 { 490 unsigned long flags; 491 492 if (epf->epc) 493 return -EBUSY; 494 495 if (IS_ERR(epc)) 496 return -EINVAL; 497 498 if (epf->func_no > epc->max_functions - 1) 499 return -EINVAL; 500 501 epf->epc = epc; 502 503 spin_lock_irqsave(&epc->lock, flags); 504 list_add_tail(&epf->list, &epc->pci_epf); 505 spin_unlock_irqrestore(&epc->lock, flags); 506 507 return 0; 508 } 509 EXPORT_SYMBOL_GPL(pci_epc_add_epf); 510 511 /** 512 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller 513 * @epc: the EPC device from which the endpoint function should be removed 514 * @epf: the endpoint function to be removed 515 * 516 * Invoke to remove PCI endpoint function from the endpoint controller. 517 */ 518 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf) 519 { 520 unsigned long flags; 521 522 if (!epc || IS_ERR(epc)) 523 return; 524 525 spin_lock_irqsave(&epc->lock, flags); 526 list_del(&epf->list); 527 spin_unlock_irqrestore(&epc->lock, flags); 528 } 529 EXPORT_SYMBOL_GPL(pci_epc_remove_epf); 530 531 /** 532 * pci_epc_linkup() - Notify the EPF device that EPC device has established a 533 * connection with the Root Complex. 534 * @epc: the EPC device which has established link with the host 535 * 536 * Invoke to Notify the EPF device that the EPC device has established a 537 * connection with the Root Complex. 538 */ 539 void pci_epc_linkup(struct pci_epc *epc) 540 { 541 unsigned long flags; 542 struct pci_epf *epf; 543 544 if (!epc || IS_ERR(epc)) 545 return; 546 547 spin_lock_irqsave(&epc->lock, flags); 548 list_for_each_entry(epf, &epc->pci_epf, list) 549 pci_epf_linkup(epf); 550 spin_unlock_irqrestore(&epc->lock, flags); 551 } 552 EXPORT_SYMBOL_GPL(pci_epc_linkup); 553 554 /** 555 * pci_epc_destroy() - destroy the EPC device 556 * @epc: the EPC device that has to be destroyed 557 * 558 * Invoke to destroy the PCI EPC device 559 */ 560 void pci_epc_destroy(struct pci_epc *epc) 561 { 562 pci_ep_cfs_remove_epc_group(epc->group); 563 device_unregister(&epc->dev); 564 kfree(epc); 565 } 566 EXPORT_SYMBOL_GPL(pci_epc_destroy); 567 568 /** 569 * devm_pci_epc_destroy() - destroy the EPC device 570 * @dev: device that wants to destroy the EPC 571 * @epc: the EPC device that has to be destroyed 572 * 573 * Invoke to destroy the devres associated with this 574 * pci_epc and destroy the EPC device. 575 */ 576 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc) 577 { 578 int r; 579 580 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match, 581 epc); 582 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n"); 583 } 584 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy); 585 586 /** 587 * __pci_epc_create() - create a new endpoint controller (EPC) device 588 * @dev: device that is creating the new EPC 589 * @ops: function pointers for performing EPC operations 590 * @owner: the owner of the module that creates the EPC device 591 * 592 * Invoke to create a new EPC device and add it to pci_epc class. 593 */ 594 struct pci_epc * 595 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, 596 struct module *owner) 597 { 598 int ret; 599 struct pci_epc *epc; 600 601 if (WARN_ON(!dev)) { 602 ret = -EINVAL; 603 goto err_ret; 604 } 605 606 epc = kzalloc(sizeof(*epc), GFP_KERNEL); 607 if (!epc) { 608 ret = -ENOMEM; 609 goto err_ret; 610 } 611 612 spin_lock_init(&epc->lock); 613 INIT_LIST_HEAD(&epc->pci_epf); 614 615 device_initialize(&epc->dev); 616 epc->dev.class = pci_epc_class; 617 epc->dev.parent = dev; 618 epc->ops = ops; 619 620 ret = dev_set_name(&epc->dev, "%s", dev_name(dev)); 621 if (ret) 622 goto put_dev; 623 624 ret = device_add(&epc->dev); 625 if (ret) 626 goto put_dev; 627 628 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev)); 629 630 return epc; 631 632 put_dev: 633 put_device(&epc->dev); 634 kfree(epc); 635 636 err_ret: 637 return ERR_PTR(ret); 638 } 639 EXPORT_SYMBOL_GPL(__pci_epc_create); 640 641 /** 642 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device 643 * @dev: device that is creating the new EPC 644 * @ops: function pointers for performing EPC operations 645 * @owner: the owner of the module that creates the EPC device 646 * 647 * Invoke to create a new EPC device and add it to pci_epc class. 648 * While at that, it also associates the device with the pci_epc using devres. 649 * On driver detach, release function is invoked on the devres data, 650 * then, devres data is freed. 651 */ 652 struct pci_epc * 653 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, 654 struct module *owner) 655 { 656 struct pci_epc **ptr, *epc; 657 658 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL); 659 if (!ptr) 660 return ERR_PTR(-ENOMEM); 661 662 epc = __pci_epc_create(dev, ops, owner); 663 if (!IS_ERR(epc)) { 664 *ptr = epc; 665 devres_add(dev, ptr); 666 } else { 667 devres_free(ptr); 668 } 669 670 return epc; 671 } 672 EXPORT_SYMBOL_GPL(__devm_pci_epc_create); 673 674 static int __init pci_epc_init(void) 675 { 676 pci_epc_class = class_create(THIS_MODULE, "pci_epc"); 677 if (IS_ERR(pci_epc_class)) { 678 pr_err("failed to create pci epc class --> %ld\n", 679 PTR_ERR(pci_epc_class)); 680 return PTR_ERR(pci_epc_class); 681 } 682 683 return 0; 684 } 685 module_init(pci_epc_init); 686 687 static void __exit pci_epc_exit(void) 688 { 689 class_destroy(pci_epc_class); 690 } 691 module_exit(pci_epc_exit); 692 693 MODULE_DESCRIPTION("PCI EPC Library"); 694 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>"); 695 MODULE_LICENSE("GPL v2"); 696