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 unsigned long free_bar; 97 98 if (!epc_features) 99 return 0; 100 101 /* Find if the reserved BAR is also a 64-bit BAR */ 102 free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit; 103 104 /* Set the adjacent bit if the reserved BAR is also a 64-bit BAR */ 105 free_bar <<= 1; 106 free_bar |= epc_features->reserved_bar; 107 108 /* Now find the free BAR */ 109 free_bar = ffz(free_bar); 110 if (free_bar > 5) 111 return 0; 112 113 return free_bar; 114 } 115 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar); 116 117 /** 118 * pci_epc_get_features() - get the features supported by EPC 119 * @epc: the features supported by *this* EPC device will be returned 120 * @func_no: the features supported by the EPC device specific to the 121 * endpoint function with func_no will be returned 122 * 123 * Invoke to get the features provided by the EPC which may be 124 * specific to an endpoint function. Returns pci_epc_features on success 125 * and NULL for any failures. 126 */ 127 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc, 128 u8 func_no) 129 { 130 const struct pci_epc_features *epc_features; 131 132 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 133 return NULL; 134 135 if (!epc->ops->get_features) 136 return NULL; 137 138 mutex_lock(&epc->lock); 139 epc_features = epc->ops->get_features(epc, func_no); 140 mutex_unlock(&epc->lock); 141 142 return epc_features; 143 } 144 EXPORT_SYMBOL_GPL(pci_epc_get_features); 145 146 /** 147 * pci_epc_stop() - stop the PCI link 148 * @epc: the link of the EPC device that has to be stopped 149 * 150 * Invoke to stop the PCI link 151 */ 152 void pci_epc_stop(struct pci_epc *epc) 153 { 154 if (IS_ERR(epc) || !epc->ops->stop) 155 return; 156 157 mutex_lock(&epc->lock); 158 epc->ops->stop(epc); 159 mutex_unlock(&epc->lock); 160 } 161 EXPORT_SYMBOL_GPL(pci_epc_stop); 162 163 /** 164 * pci_epc_start() - start the PCI link 165 * @epc: the link of *this* EPC device has to be started 166 * 167 * Invoke to start the PCI link 168 */ 169 int pci_epc_start(struct pci_epc *epc) 170 { 171 int ret; 172 173 if (IS_ERR(epc)) 174 return -EINVAL; 175 176 if (!epc->ops->start) 177 return 0; 178 179 mutex_lock(&epc->lock); 180 ret = epc->ops->start(epc); 181 mutex_unlock(&epc->lock); 182 183 return ret; 184 } 185 EXPORT_SYMBOL_GPL(pci_epc_start); 186 187 /** 188 * pci_epc_raise_irq() - interrupt the host system 189 * @epc: the EPC device which has to interrupt the host 190 * @func_no: the endpoint function number in the EPC device 191 * @type: specify the type of interrupt; legacy, MSI or MSI-X 192 * @interrupt_num: the MSI or MSI-X interrupt number 193 * 194 * Invoke to raise an legacy, MSI or MSI-X interrupt 195 */ 196 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, 197 enum pci_epc_irq_type type, u16 interrupt_num) 198 { 199 int ret; 200 201 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 202 return -EINVAL; 203 204 if (!epc->ops->raise_irq) 205 return 0; 206 207 mutex_lock(&epc->lock); 208 ret = epc->ops->raise_irq(epc, func_no, type, interrupt_num); 209 mutex_unlock(&epc->lock); 210 211 return ret; 212 } 213 EXPORT_SYMBOL_GPL(pci_epc_raise_irq); 214 215 /** 216 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated 217 * @epc: the EPC device to which MSI interrupts was requested 218 * @func_no: the endpoint function number in the EPC device 219 * 220 * Invoke to get the number of MSI interrupts allocated by the RC 221 */ 222 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no) 223 { 224 int interrupt; 225 226 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 227 return 0; 228 229 if (!epc->ops->get_msi) 230 return 0; 231 232 mutex_lock(&epc->lock); 233 interrupt = epc->ops->get_msi(epc, func_no); 234 mutex_unlock(&epc->lock); 235 236 if (interrupt < 0) 237 return 0; 238 239 interrupt = 1 << interrupt; 240 241 return interrupt; 242 } 243 EXPORT_SYMBOL_GPL(pci_epc_get_msi); 244 245 /** 246 * pci_epc_set_msi() - set the number of MSI interrupt numbers required 247 * @epc: the EPC device on which MSI has to be configured 248 * @func_no: the endpoint function number in the EPC device 249 * @interrupts: number of MSI interrupts required by the EPF 250 * 251 * Invoke to set the required number of MSI interrupts. 252 */ 253 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts) 254 { 255 int ret; 256 u8 encode_int; 257 258 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 259 interrupts > 32) 260 return -EINVAL; 261 262 if (!epc->ops->set_msi) 263 return 0; 264 265 encode_int = order_base_2(interrupts); 266 267 mutex_lock(&epc->lock); 268 ret = epc->ops->set_msi(epc, func_no, encode_int); 269 mutex_unlock(&epc->lock); 270 271 return ret; 272 } 273 EXPORT_SYMBOL_GPL(pci_epc_set_msi); 274 275 /** 276 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated 277 * @epc: the EPC device to which MSI-X interrupts was requested 278 * @func_no: the endpoint function number in the EPC device 279 * 280 * Invoke to get the number of MSI-X interrupts allocated by the RC 281 */ 282 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no) 283 { 284 int interrupt; 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 mutex_lock(&epc->lock); 293 interrupt = epc->ops->get_msix(epc, func_no); 294 mutex_unlock(&epc->lock); 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 * @bir: BAR where the MSI-X table resides 309 * @offset: Offset pointing to the start of MSI-X table 310 * 311 * Invoke to set the required number of MSI-X interrupts. 312 */ 313 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts, 314 enum pci_barno bir, u32 offset) 315 { 316 int ret; 317 318 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 319 interrupts < 1 || interrupts > 2048) 320 return -EINVAL; 321 322 if (!epc->ops->set_msix) 323 return 0; 324 325 mutex_lock(&epc->lock); 326 ret = epc->ops->set_msix(epc, func_no, interrupts - 1, bir, offset); 327 mutex_unlock(&epc->lock); 328 329 return ret; 330 } 331 EXPORT_SYMBOL_GPL(pci_epc_set_msix); 332 333 /** 334 * pci_epc_unmap_addr() - unmap CPU address from PCI address 335 * @epc: the EPC device on which address is allocated 336 * @func_no: the endpoint function number in the EPC device 337 * @phys_addr: physical address of the local system 338 * 339 * Invoke to unmap the CPU address from PCI address. 340 */ 341 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, 342 phys_addr_t phys_addr) 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 mutex_lock(&epc->lock); 351 epc->ops->unmap_addr(epc, func_no, phys_addr); 352 mutex_unlock(&epc->lock); 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 371 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 372 return -EINVAL; 373 374 if (!epc->ops->map_addr) 375 return 0; 376 377 mutex_lock(&epc->lock); 378 ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size); 379 mutex_unlock(&epc->lock); 380 381 return ret; 382 } 383 EXPORT_SYMBOL_GPL(pci_epc_map_addr); 384 385 /** 386 * pci_epc_clear_bar() - reset the BAR 387 * @epc: the EPC device for which the BAR has to be cleared 388 * @func_no: the endpoint function number in the EPC device 389 * @epf_bar: the struct epf_bar that contains the BAR information 390 * 391 * Invoke to reset the BAR of the endpoint device. 392 */ 393 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, 394 struct pci_epf_bar *epf_bar) 395 { 396 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 397 (epf_bar->barno == BAR_5 && 398 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)) 399 return; 400 401 if (!epc->ops->clear_bar) 402 return; 403 404 mutex_lock(&epc->lock); 405 epc->ops->clear_bar(epc, func_no, epf_bar); 406 mutex_unlock(&epc->lock); 407 } 408 EXPORT_SYMBOL_GPL(pci_epc_clear_bar); 409 410 /** 411 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space 412 * @epc: the EPC device on which BAR has to be configured 413 * @func_no: the endpoint function number in the EPC device 414 * @epf_bar: the struct epf_bar that contains the BAR information 415 * 416 * Invoke to configure the BAR of the endpoint device. 417 */ 418 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, 419 struct pci_epf_bar *epf_bar) 420 { 421 int ret; 422 int flags = epf_bar->flags; 423 424 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 425 (epf_bar->barno == BAR_5 && 426 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) || 427 (flags & PCI_BASE_ADDRESS_SPACE_IO && 428 flags & PCI_BASE_ADDRESS_IO_MASK) || 429 (upper_32_bits(epf_bar->size) && 430 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))) 431 return -EINVAL; 432 433 if (!epc->ops->set_bar) 434 return 0; 435 436 mutex_lock(&epc->lock); 437 ret = epc->ops->set_bar(epc, func_no, epf_bar); 438 mutex_unlock(&epc->lock); 439 440 return ret; 441 } 442 EXPORT_SYMBOL_GPL(pci_epc_set_bar); 443 444 /** 445 * pci_epc_write_header() - write standard configuration header 446 * @epc: the EPC device to which the configuration header should be written 447 * @func_no: the endpoint function number in the EPC device 448 * @header: standard configuration header fields 449 * 450 * Invoke to write the configuration header to the endpoint controller. Every 451 * endpoint controller will have a dedicated location to which the standard 452 * configuration header would be written. The callback function should write 453 * the header fields to this dedicated location. 454 */ 455 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, 456 struct pci_epf_header *header) 457 { 458 int ret; 459 460 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 461 return -EINVAL; 462 463 if (!epc->ops->write_header) 464 return 0; 465 466 mutex_lock(&epc->lock); 467 ret = epc->ops->write_header(epc, func_no, header); 468 mutex_unlock(&epc->lock); 469 470 return ret; 471 } 472 EXPORT_SYMBOL_GPL(pci_epc_write_header); 473 474 /** 475 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller 476 * @epc: the EPC device to which the endpoint function should be added 477 * @epf: the endpoint function to be added 478 * 479 * A PCI endpoint device can have one or more functions. In the case of PCIe, 480 * the specification allows up to 8 PCIe endpoint functions. Invoke 481 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller. 482 */ 483 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf) 484 { 485 u32 func_no; 486 int ret = 0; 487 488 if (epf->epc) 489 return -EBUSY; 490 491 if (IS_ERR(epc)) 492 return -EINVAL; 493 494 mutex_lock(&epc->lock); 495 func_no = find_first_zero_bit(&epc->function_num_map, 496 BITS_PER_LONG); 497 if (func_no >= BITS_PER_LONG) { 498 ret = -EINVAL; 499 goto ret; 500 } 501 502 if (func_no > epc->max_functions - 1) { 503 dev_err(&epc->dev, "Exceeding max supported Function Number\n"); 504 ret = -EINVAL; 505 goto ret; 506 } 507 508 set_bit(func_no, &epc->function_num_map); 509 epf->func_no = func_no; 510 epf->epc = epc; 511 512 list_add_tail(&epf->list, &epc->pci_epf); 513 514 ret: 515 mutex_unlock(&epc->lock); 516 517 return ret; 518 } 519 EXPORT_SYMBOL_GPL(pci_epc_add_epf); 520 521 /** 522 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller 523 * @epc: the EPC device from which the endpoint function should be removed 524 * @epf: the endpoint function to be removed 525 * 526 * Invoke to remove PCI endpoint function from the endpoint controller. 527 */ 528 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf) 529 { 530 if (!epc || IS_ERR(epc) || !epf) 531 return; 532 533 mutex_lock(&epc->lock); 534 clear_bit(epf->func_no, &epc->function_num_map); 535 list_del(&epf->list); 536 epf->epc = NULL; 537 mutex_unlock(&epc->lock); 538 } 539 EXPORT_SYMBOL_GPL(pci_epc_remove_epf); 540 541 /** 542 * pci_epc_linkup() - Notify the EPF device that EPC device has established a 543 * connection with the Root Complex. 544 * @epc: the EPC device which has established link with the host 545 * 546 * Invoke to Notify the EPF device that the EPC device has established a 547 * connection with the Root Complex. 548 */ 549 void pci_epc_linkup(struct pci_epc *epc) 550 { 551 if (!epc || IS_ERR(epc)) 552 return; 553 554 atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL); 555 } 556 EXPORT_SYMBOL_GPL(pci_epc_linkup); 557 558 /** 559 * pci_epc_init_notify() - Notify the EPF device that EPC device's core 560 * initialization is completed. 561 * @epc: the EPC device whose core initialization is completeds 562 * 563 * Invoke to Notify the EPF device that the EPC device's initialization 564 * is completed. 565 */ 566 void pci_epc_init_notify(struct pci_epc *epc) 567 { 568 if (!epc || IS_ERR(epc)) 569 return; 570 571 atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL); 572 } 573 EXPORT_SYMBOL_GPL(pci_epc_init_notify); 574 575 /** 576 * pci_epc_destroy() - destroy the EPC device 577 * @epc: the EPC device that has to be destroyed 578 * 579 * Invoke to destroy the PCI EPC device 580 */ 581 void pci_epc_destroy(struct pci_epc *epc) 582 { 583 pci_ep_cfs_remove_epc_group(epc->group); 584 device_unregister(&epc->dev); 585 kfree(epc); 586 } 587 EXPORT_SYMBOL_GPL(pci_epc_destroy); 588 589 /** 590 * devm_pci_epc_destroy() - destroy the EPC device 591 * @dev: device that wants to destroy the EPC 592 * @epc: the EPC device that has to be destroyed 593 * 594 * Invoke to destroy the devres associated with this 595 * pci_epc and destroy the EPC device. 596 */ 597 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc) 598 { 599 int r; 600 601 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match, 602 epc); 603 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n"); 604 } 605 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy); 606 607 /** 608 * __pci_epc_create() - create a new endpoint controller (EPC) device 609 * @dev: device that is creating the new EPC 610 * @ops: function pointers for performing EPC operations 611 * @owner: the owner of the module that creates the EPC device 612 * 613 * Invoke to create a new EPC device and add it to pci_epc class. 614 */ 615 struct pci_epc * 616 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, 617 struct module *owner) 618 { 619 int ret; 620 struct pci_epc *epc; 621 622 if (WARN_ON(!dev)) { 623 ret = -EINVAL; 624 goto err_ret; 625 } 626 627 epc = kzalloc(sizeof(*epc), GFP_KERNEL); 628 if (!epc) { 629 ret = -ENOMEM; 630 goto err_ret; 631 } 632 633 mutex_init(&epc->lock); 634 INIT_LIST_HEAD(&epc->pci_epf); 635 ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier); 636 637 device_initialize(&epc->dev); 638 epc->dev.class = pci_epc_class; 639 epc->dev.parent = dev; 640 epc->ops = ops; 641 642 ret = dev_set_name(&epc->dev, "%s", dev_name(dev)); 643 if (ret) 644 goto put_dev; 645 646 ret = device_add(&epc->dev); 647 if (ret) 648 goto put_dev; 649 650 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev)); 651 652 return epc; 653 654 put_dev: 655 put_device(&epc->dev); 656 kfree(epc); 657 658 err_ret: 659 return ERR_PTR(ret); 660 } 661 EXPORT_SYMBOL_GPL(__pci_epc_create); 662 663 /** 664 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device 665 * @dev: device that is creating the new EPC 666 * @ops: function pointers for performing EPC operations 667 * @owner: the owner of the module that creates the EPC device 668 * 669 * Invoke to create a new EPC device and add it to pci_epc class. 670 * While at that, it also associates the device with the pci_epc using devres. 671 * On driver detach, release function is invoked on the devres data, 672 * then, devres data is freed. 673 */ 674 struct pci_epc * 675 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops, 676 struct module *owner) 677 { 678 struct pci_epc **ptr, *epc; 679 680 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL); 681 if (!ptr) 682 return ERR_PTR(-ENOMEM); 683 684 epc = __pci_epc_create(dev, ops, owner); 685 if (!IS_ERR(epc)) { 686 *ptr = epc; 687 devres_add(dev, ptr); 688 } else { 689 devres_free(ptr); 690 } 691 692 return epc; 693 } 694 EXPORT_SYMBOL_GPL(__devm_pci_epc_create); 695 696 static int __init pci_epc_init(void) 697 { 698 pci_epc_class = class_create(THIS_MODULE, "pci_epc"); 699 if (IS_ERR(pci_epc_class)) { 700 pr_err("failed to create pci epc class --> %ld\n", 701 PTR_ERR(pci_epc_class)); 702 return PTR_ERR(pci_epc_class); 703 } 704 705 return 0; 706 } 707 module_init(pci_epc_init); 708 709 static void __exit pci_epc_exit(void) 710 { 711 class_destroy(pci_epc_class); 712 } 713 module_exit(pci_epc_exit); 714 715 MODULE_DESCRIPTION("PCI EPC Library"); 716 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>"); 717 MODULE_LICENSE("GPL v2"); 718