1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Endpoint *Controller* (EPC) library
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/device.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
18 static struct class *pci_epc_class;
20 static void devm_pci_epc_release(struct device *dev, void *res)
22 struct pci_epc *epc = *(struct pci_epc **)res;
27 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
29 struct pci_epc **epc = res;
31 return *epc == match_data;
35 * pci_epc_put() - release the PCI endpoint controller
36 * @epc: epc returned by pci_epc_get()
38 * release the refcount the caller obtained by invoking pci_epc_get()
40 void pci_epc_put(struct pci_epc *epc)
42 if (!epc || IS_ERR(epc))
45 module_put(epc->ops->owner);
46 put_device(&epc->dev);
48 EXPORT_SYMBOL_GPL(pci_epc_put);
51 * pci_epc_get() - get the PCI endpoint controller
52 * @epc_name: device name of the endpoint controller
54 * Invoke to get struct pci_epc * corresponding to the device name of the
57 struct pci_epc *pci_epc_get(const char *epc_name)
62 struct class_dev_iter iter;
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)))
69 epc = to_pci_epc(dev);
70 if (!try_module_get(epc->ops->owner)) {
75 class_dev_iter_exit(&iter);
76 get_device(&epc->dev);
81 class_dev_iter_exit(&iter);
84 EXPORT_SYMBOL_GPL(pci_epc_get);
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
90 * Invoke to get the first unreserved BAR that can be used by the endpoint
91 * function. For any incorrect value in reserved_bar return '0'.
94 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
96 return pci_epc_get_next_free_bar(epc_features, BAR_0);
98 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
101 * pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
102 * @epc_features: pci_epc_features structure that holds the reserved bar bitmap
103 * @bar: the starting BAR number from where unreserved BAR should be searched
105 * Invoke to get the next unreserved BAR starting from @bar that can be used
106 * for endpoint function. For any incorrect value in reserved_bar return '0'.
108 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
109 *epc_features, enum pci_barno bar)
111 unsigned long free_bar;
116 /* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
117 if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
120 /* Find if the reserved BAR is also a 64-bit BAR */
121 free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
123 /* Set the adjacent bit if the reserved BAR is also a 64-bit BAR */
125 free_bar |= epc_features->reserved_bar;
127 free_bar = find_next_zero_bit(&free_bar, 6, bar);
133 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
136 * pci_epc_get_features() - get the features supported by EPC
137 * @epc: the features supported by *this* EPC device will be returned
138 * @func_no: the features supported by the EPC device specific to the
139 * endpoint function with func_no will be returned
141 * Invoke to get the features provided by the EPC which may be
142 * specific to an endpoint function. Returns pci_epc_features on success
143 * and NULL for any failures.
145 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
148 const struct pci_epc_features *epc_features;
150 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
153 if (!epc->ops->get_features)
156 mutex_lock(&epc->lock);
157 epc_features = epc->ops->get_features(epc, func_no);
158 mutex_unlock(&epc->lock);
162 EXPORT_SYMBOL_GPL(pci_epc_get_features);
165 * pci_epc_stop() - stop the PCI link
166 * @epc: the link of the EPC device that has to be stopped
168 * Invoke to stop the PCI link
170 void pci_epc_stop(struct pci_epc *epc)
172 if (IS_ERR(epc) || !epc->ops->stop)
175 mutex_lock(&epc->lock);
177 mutex_unlock(&epc->lock);
179 EXPORT_SYMBOL_GPL(pci_epc_stop);
182 * pci_epc_start() - start the PCI link
183 * @epc: the link of *this* EPC device has to be started
185 * Invoke to start the PCI link
187 int pci_epc_start(struct pci_epc *epc)
194 if (!epc->ops->start)
197 mutex_lock(&epc->lock);
198 ret = epc->ops->start(epc);
199 mutex_unlock(&epc->lock);
203 EXPORT_SYMBOL_GPL(pci_epc_start);
206 * pci_epc_raise_irq() - interrupt the host system
207 * @epc: the EPC device which has to interrupt the host
208 * @func_no: the endpoint function number in the EPC device
209 * @type: specify the type of interrupt; legacy, MSI or MSI-X
210 * @interrupt_num: the MSI or MSI-X interrupt number
212 * Invoke to raise an legacy, MSI or MSI-X interrupt
214 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no,
215 enum pci_epc_irq_type type, u16 interrupt_num)
219 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
222 if (!epc->ops->raise_irq)
225 mutex_lock(&epc->lock);
226 ret = epc->ops->raise_irq(epc, func_no, type, interrupt_num);
227 mutex_unlock(&epc->lock);
231 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
234 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
235 * @epc: the EPC device to which MSI interrupts was requested
236 * @func_no: the endpoint function number in the EPC device
238 * Invoke to get the number of MSI interrupts allocated by the RC
240 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no)
244 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
247 if (!epc->ops->get_msi)
250 mutex_lock(&epc->lock);
251 interrupt = epc->ops->get_msi(epc, func_no);
252 mutex_unlock(&epc->lock);
257 interrupt = 1 << interrupt;
261 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
264 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
265 * @epc: the EPC device on which MSI has to be configured
266 * @func_no: the endpoint function number in the EPC device
267 * @interrupts: number of MSI interrupts required by the EPF
269 * Invoke to set the required number of MSI interrupts.
271 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 interrupts)
276 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
280 if (!epc->ops->set_msi)
283 encode_int = order_base_2(interrupts);
285 mutex_lock(&epc->lock);
286 ret = epc->ops->set_msi(epc, func_no, encode_int);
287 mutex_unlock(&epc->lock);
291 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
294 * pci_epc_get_msix() - get the number of MSI-X interrupt numbers allocated
295 * @epc: the EPC device to which MSI-X interrupts was requested
296 * @func_no: the endpoint function number in the EPC device
298 * Invoke to get the number of MSI-X interrupts allocated by the RC
300 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no)
304 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
307 if (!epc->ops->get_msix)
310 mutex_lock(&epc->lock);
311 interrupt = epc->ops->get_msix(epc, func_no);
312 mutex_unlock(&epc->lock);
317 return interrupt + 1;
319 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
322 * pci_epc_set_msix() - set the number of MSI-X interrupt numbers required
323 * @epc: the EPC device on which MSI-X has to be configured
324 * @func_no: the endpoint function number in the EPC device
325 * @interrupts: number of MSI-X interrupts required by the EPF
326 * @bir: BAR where the MSI-X table resides
327 * @offset: Offset pointing to the start of MSI-X table
329 * Invoke to set the required number of MSI-X interrupts.
331 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u16 interrupts,
332 enum pci_barno bir, u32 offset)
336 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
337 interrupts < 1 || interrupts > 2048)
340 if (!epc->ops->set_msix)
343 mutex_lock(&epc->lock);
344 ret = epc->ops->set_msix(epc, func_no, interrupts - 1, bir, offset);
345 mutex_unlock(&epc->lock);
349 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
352 * pci_epc_unmap_addr() - unmap CPU address from PCI address
353 * @epc: the EPC device on which address is allocated
354 * @func_no: the endpoint function number in the EPC device
355 * @phys_addr: physical address of the local system
357 * Invoke to unmap the CPU address from PCI address.
359 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no,
360 phys_addr_t phys_addr)
362 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
365 if (!epc->ops->unmap_addr)
368 mutex_lock(&epc->lock);
369 epc->ops->unmap_addr(epc, func_no, phys_addr);
370 mutex_unlock(&epc->lock);
372 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
375 * pci_epc_map_addr() - map CPU address to PCI address
376 * @epc: the EPC device on which address is allocated
377 * @func_no: the endpoint function number in the EPC device
378 * @phys_addr: physical address of the local system
379 * @pci_addr: PCI address to which the physical address should be mapped
380 * @size: the size of the allocation
382 * Invoke to map CPU address with PCI address.
384 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no,
385 phys_addr_t phys_addr, u64 pci_addr, size_t size)
389 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
392 if (!epc->ops->map_addr)
395 mutex_lock(&epc->lock);
396 ret = epc->ops->map_addr(epc, func_no, phys_addr, pci_addr, size);
397 mutex_unlock(&epc->lock);
401 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
404 * pci_epc_clear_bar() - reset the BAR
405 * @epc: the EPC device for which the BAR has to be cleared
406 * @func_no: the endpoint function number in the EPC device
407 * @epf_bar: the struct epf_bar that contains the BAR information
409 * Invoke to reset the BAR of the endpoint device.
411 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no,
412 struct pci_epf_bar *epf_bar)
414 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
415 (epf_bar->barno == BAR_5 &&
416 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
419 if (!epc->ops->clear_bar)
422 mutex_lock(&epc->lock);
423 epc->ops->clear_bar(epc, func_no, epf_bar);
424 mutex_unlock(&epc->lock);
426 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
429 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
430 * @epc: the EPC device on which BAR has to be configured
431 * @func_no: the endpoint function number in the EPC device
432 * @epf_bar: the struct epf_bar that contains the BAR information
434 * Invoke to configure the BAR of the endpoint device.
436 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no,
437 struct pci_epf_bar *epf_bar)
440 int flags = epf_bar->flags;
442 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
443 (epf_bar->barno == BAR_5 &&
444 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
445 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
446 flags & PCI_BASE_ADDRESS_IO_MASK) ||
447 (upper_32_bits(epf_bar->size) &&
448 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
451 if (!epc->ops->set_bar)
454 mutex_lock(&epc->lock);
455 ret = epc->ops->set_bar(epc, func_no, epf_bar);
456 mutex_unlock(&epc->lock);
460 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
463 * pci_epc_write_header() - write standard configuration header
464 * @epc: the EPC device to which the configuration header should be written
465 * @func_no: the endpoint function number in the EPC device
466 * @header: standard configuration header fields
468 * Invoke to write the configuration header to the endpoint controller. Every
469 * endpoint controller will have a dedicated location to which the standard
470 * configuration header would be written. The callback function should write
471 * the header fields to this dedicated location.
473 int pci_epc_write_header(struct pci_epc *epc, u8 func_no,
474 struct pci_epf_header *header)
478 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
481 if (!epc->ops->write_header)
484 mutex_lock(&epc->lock);
485 ret = epc->ops->write_header(epc, func_no, header);
486 mutex_unlock(&epc->lock);
490 EXPORT_SYMBOL_GPL(pci_epc_write_header);
493 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
494 * @epc: the EPC device to which the endpoint function should be added
495 * @epf: the endpoint function to be added
497 * A PCI endpoint device can have one or more functions. In the case of PCIe,
498 * the specification allows up to 8 PCIe endpoint functions. Invoke
499 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
501 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
512 mutex_lock(&epc->lock);
513 func_no = find_first_zero_bit(&epc->function_num_map,
515 if (func_no >= BITS_PER_LONG) {
520 if (func_no > epc->max_functions - 1) {
521 dev_err(&epc->dev, "Exceeding max supported Function Number\n");
526 set_bit(func_no, &epc->function_num_map);
527 epf->func_no = func_no;
530 list_add_tail(&epf->list, &epc->pci_epf);
533 mutex_unlock(&epc->lock);
537 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
540 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
541 * @epc: the EPC device from which the endpoint function should be removed
542 * @epf: the endpoint function to be removed
544 * Invoke to remove PCI endpoint function from the endpoint controller.
546 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
548 if (!epc || IS_ERR(epc) || !epf)
551 mutex_lock(&epc->lock);
552 clear_bit(epf->func_no, &epc->function_num_map);
553 list_del(&epf->list);
555 mutex_unlock(&epc->lock);
557 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
560 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
561 * connection with the Root Complex.
562 * @epc: the EPC device which has established link with the host
564 * Invoke to Notify the EPF device that the EPC device has established a
565 * connection with the Root Complex.
567 void pci_epc_linkup(struct pci_epc *epc)
569 if (!epc || IS_ERR(epc))
572 atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL);
574 EXPORT_SYMBOL_GPL(pci_epc_linkup);
577 * pci_epc_init_notify() - Notify the EPF device that EPC device's core
578 * initialization is completed.
579 * @epc: the EPC device whose core initialization is completeds
581 * Invoke to Notify the EPF device that the EPC device's initialization
584 void pci_epc_init_notify(struct pci_epc *epc)
586 if (!epc || IS_ERR(epc))
589 atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL);
591 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
594 * pci_epc_destroy() - destroy the EPC device
595 * @epc: the EPC device that has to be destroyed
597 * Invoke to destroy the PCI EPC device
599 void pci_epc_destroy(struct pci_epc *epc)
601 pci_ep_cfs_remove_epc_group(epc->group);
602 device_unregister(&epc->dev);
605 EXPORT_SYMBOL_GPL(pci_epc_destroy);
608 * devm_pci_epc_destroy() - destroy the EPC device
609 * @dev: device that wants to destroy the EPC
610 * @epc: the EPC device that has to be destroyed
612 * Invoke to destroy the devres associated with this
613 * pci_epc and destroy the EPC device.
615 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
619 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
621 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
623 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
626 * __pci_epc_create() - create a new endpoint controller (EPC) device
627 * @dev: device that is creating the new EPC
628 * @ops: function pointers for performing EPC operations
629 * @owner: the owner of the module that creates the EPC device
631 * Invoke to create a new EPC device and add it to pci_epc class.
634 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
635 struct module *owner)
645 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
651 mutex_init(&epc->lock);
652 INIT_LIST_HEAD(&epc->pci_epf);
653 ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier);
655 device_initialize(&epc->dev);
656 epc->dev.class = pci_epc_class;
657 epc->dev.parent = dev;
660 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
664 ret = device_add(&epc->dev);
668 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
673 put_device(&epc->dev);
679 EXPORT_SYMBOL_GPL(__pci_epc_create);
682 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
683 * @dev: device that is creating the new EPC
684 * @ops: function pointers for performing EPC operations
685 * @owner: the owner of the module that creates the EPC device
687 * Invoke to create a new EPC device and add it to pci_epc class.
688 * While at that, it also associates the device with the pci_epc using devres.
689 * On driver detach, release function is invoked on the devres data,
690 * then, devres data is freed.
693 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
694 struct module *owner)
696 struct pci_epc **ptr, *epc;
698 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
700 return ERR_PTR(-ENOMEM);
702 epc = __pci_epc_create(dev, ops, owner);
705 devres_add(dev, ptr);
712 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
714 static int __init pci_epc_init(void)
716 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
717 if (IS_ERR(pci_epc_class)) {
718 pr_err("failed to create pci epc class --> %ld\n",
719 PTR_ERR(pci_epc_class));
720 return PTR_ERR(pci_epc_class);
725 module_init(pci_epc_init);
727 static void __exit pci_epc_exit(void)
729 class_destroy(pci_epc_class);
731 module_exit(pci_epc_exit);
733 MODULE_DESCRIPTION("PCI EPC Library");
734 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
735 MODULE_LICENSE("GPL v2");