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