2 * PCI Endpoint *Controller* (EPC) library
4 * Copyright (C) 2017 Texas Instruments
5 * Author: Kishon Vijay Abraham I <kishon@ti.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 of
9 * the License as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/device.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
25 #include <linux/pci-epc.h>
26 #include <linux/pci-epf.h>
27 #include <linux/pci-ep-cfs.h>
29 static struct class *pci_epc_class;
31 static void devm_pci_epc_release(struct device *dev, void *res)
33 struct pci_epc *epc = *(struct pci_epc **)res;
38 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
40 struct pci_epc **epc = res;
42 return *epc == match_data;
46 * pci_epc_put() - release the PCI endpoint controller
47 * @epc: epc returned by pci_epc_get()
49 * release the refcount the caller obtained by invoking pci_epc_get()
51 void pci_epc_put(struct pci_epc *epc)
53 if (!epc || IS_ERR(epc))
56 module_put(epc->ops->owner);
57 put_device(&epc->dev);
59 EXPORT_SYMBOL_GPL(pci_epc_put);
62 * pci_epc_get() - get the PCI endpoint controller
63 * @epc_name: device name of the endpoint controller
65 * Invoke to get struct pci_epc * corresponding to the device name of the
68 struct pci_epc *pci_epc_get(const char *epc_name)
73 struct class_dev_iter iter;
75 class_dev_iter_init(&iter, pci_epc_class, NULL, NULL);
76 while ((dev = class_dev_iter_next(&iter))) {
77 if (strcmp(epc_name, dev_name(dev)))
80 epc = to_pci_epc(dev);
81 if (!try_module_get(epc->ops->owner)) {
86 class_dev_iter_exit(&iter);
87 get_device(&epc->dev);
92 class_dev_iter_exit(&iter);
95 EXPORT_SYMBOL_GPL(pci_epc_get);
98 * pci_epc_stop() - stop the PCI link
99 * @epc: the link of the EPC device that has to be stopped
101 * Invoke to stop the PCI link
103 void pci_epc_stop(struct pci_epc *epc)
107 if (IS_ERR(epc) || !epc->ops->stop)
110 spin_lock_irqsave(&epc->lock, flags);
112 spin_unlock_irqrestore(&epc->lock, flags);
114 EXPORT_SYMBOL_GPL(pci_epc_stop);
117 * pci_epc_start() - start the PCI link
118 * @epc: the link of *this* EPC device has to be started
120 * Invoke to start the PCI link
122 int pci_epc_start(struct pci_epc *epc)
130 if (!epc->ops->start)
133 spin_lock_irqsave(&epc->lock, flags);
134 ret = epc->ops->start(epc);
135 spin_unlock_irqrestore(&epc->lock, flags);
139 EXPORT_SYMBOL_GPL(pci_epc_start);
142 * pci_epc_raise_irq() - interrupt the host system
143 * @epc: the EPC device which has to interrupt the host
144 * @type: specify the type of interrupt; legacy or MSI
145 * @interrupt_num: the MSI interrupt number
147 * Invoke to raise an MSI or legacy interrupt
149 int pci_epc_raise_irq(struct pci_epc *epc, enum pci_epc_irq_type type,
158 if (!epc->ops->raise_irq)
161 spin_lock_irqsave(&epc->lock, flags);
162 ret = epc->ops->raise_irq(epc, type, interrupt_num);
163 spin_unlock_irqrestore(&epc->lock, flags);
167 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
170 * pci_epc_get_msi() - get the number of MSI interrupt numbers allocated
171 * @epc: the EPC device to which MSI interrupts was requested
173 * Invoke to get the number of MSI interrupts allocated by the RC
175 int pci_epc_get_msi(struct pci_epc *epc)
183 if (!epc->ops->get_msi)
186 spin_lock_irqsave(&epc->lock, flags);
187 interrupt = epc->ops->get_msi(epc);
188 spin_unlock_irqrestore(&epc->lock, flags);
193 interrupt = 1 << interrupt;
197 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
200 * pci_epc_set_msi() - set the number of MSI interrupt numbers required
201 * @epc: the EPC device on which MSI has to be configured
202 * @interrupts: number of MSI interrupts required by the EPF
204 * Invoke to set the required number of MSI interrupts.
206 int pci_epc_set_msi(struct pci_epc *epc, u8 interrupts)
215 if (!epc->ops->set_msi)
218 encode_int = order_base_2(interrupts);
220 spin_lock_irqsave(&epc->lock, flags);
221 ret = epc->ops->set_msi(epc, encode_int);
222 spin_unlock_irqrestore(&epc->lock, flags);
226 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
229 * pci_epc_unmap_addr() - unmap CPU address from PCI address
230 * @epc: the EPC device on which address is allocated
231 * @phys_addr: physical address of the local system
233 * Invoke to unmap the CPU address from PCI address.
235 void pci_epc_unmap_addr(struct pci_epc *epc, phys_addr_t phys_addr)
242 if (!epc->ops->unmap_addr)
245 spin_lock_irqsave(&epc->lock, flags);
246 epc->ops->unmap_addr(epc, phys_addr);
247 spin_unlock_irqrestore(&epc->lock, flags);
249 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
252 * pci_epc_map_addr() - map CPU address to PCI address
253 * @epc: the EPC device on which address is allocated
254 * @phys_addr: physical address of the local system
255 * @pci_addr: PCI address to which the physical address should be mapped
256 * @size: the size of the allocation
258 * Invoke to map CPU address with PCI address.
260 int pci_epc_map_addr(struct pci_epc *epc, phys_addr_t phys_addr,
261 u64 pci_addr, size_t size)
269 if (!epc->ops->map_addr)
272 spin_lock_irqsave(&epc->lock, flags);
273 ret = epc->ops->map_addr(epc, phys_addr, pci_addr, size);
274 spin_unlock_irqrestore(&epc->lock, flags);
278 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
281 * pci_epc_clear_bar() - reset the BAR
282 * @epc: the EPC device for which the BAR has to be cleared
283 * @bar: the BAR number that has to be reset
285 * Invoke to reset the BAR of the endpoint device.
287 void pci_epc_clear_bar(struct pci_epc *epc, int bar)
294 if (!epc->ops->clear_bar)
297 spin_lock_irqsave(&epc->lock, flags);
298 epc->ops->clear_bar(epc, bar);
299 spin_unlock_irqrestore(&epc->lock, flags);
301 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
304 * pci_epc_set_bar() - configure BAR in order for host to assign PCI addr space
305 * @epc: the EPC device on which BAR has to be configured
306 * @bar: the BAR number that has to be configured
307 * @size: the size of the addr space
308 * @flags: specify memory allocation/io allocation/32bit address/64 bit address
310 * Invoke to configure the BAR of the endpoint device.
312 int pci_epc_set_bar(struct pci_epc *epc, enum pci_barno bar,
313 dma_addr_t bar_phys, size_t size, int flags)
316 unsigned long irq_flags;
321 if (!epc->ops->set_bar)
324 spin_lock_irqsave(&epc->lock, irq_flags);
325 ret = epc->ops->set_bar(epc, bar, bar_phys, size, flags);
326 spin_unlock_irqrestore(&epc->lock, irq_flags);
330 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
333 * pci_epc_write_header() - write standard configuration header
334 * @epc: the EPC device to which the configuration header should be written
335 * @header: standard configuration header fields
337 * Invoke to write the configuration header to the endpoint controller. Every
338 * endpoint controller will have a dedicated location to which the standard
339 * configuration header would be written. The callback function should write
340 * the header fields to this dedicated location.
342 int pci_epc_write_header(struct pci_epc *epc, struct pci_epf_header *header)
350 if (!epc->ops->write_header)
353 spin_lock_irqsave(&epc->lock, flags);
354 ret = epc->ops->write_header(epc, header);
355 spin_unlock_irqrestore(&epc->lock, flags);
359 EXPORT_SYMBOL_GPL(pci_epc_write_header);
362 * pci_epc_add_epf() - bind PCI endpoint function to an endpoint controller
363 * @epc: the EPC device to which the endpoint function should be added
364 * @epf: the endpoint function to be added
366 * A PCI endpoint device can have one or more functions. In the case of PCIe,
367 * the specification allows up to 8 PCIe endpoint functions. Invoke
368 * pci_epc_add_epf() to add a PCI endpoint function to an endpoint controller.
370 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf)
380 if (epf->func_no > epc->max_functions - 1)
385 spin_lock_irqsave(&epc->lock, flags);
386 list_add_tail(&epf->list, &epc->pci_epf);
387 spin_unlock_irqrestore(&epc->lock, flags);
391 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
394 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
395 * @epc: the EPC device from which the endpoint function should be removed
396 * @epf: the endpoint function to be removed
398 * Invoke to remove PCI endpoint function from the endpoint controller.
400 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
404 if (!epc || IS_ERR(epc))
407 spin_lock_irqsave(&epc->lock, flags);
408 list_del(&epf->list);
409 spin_unlock_irqrestore(&epc->lock, flags);
411 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
414 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
415 * connection with the Root Complex.
416 * @epc: the EPC device which has established link with the host
418 * Invoke to Notify the EPF device that the EPC device has established a
419 * connection with the Root Complex.
421 void pci_epc_linkup(struct pci_epc *epc)
426 if (!epc || IS_ERR(epc))
429 spin_lock_irqsave(&epc->lock, flags);
430 list_for_each_entry(epf, &epc->pci_epf, list)
432 spin_unlock_irqrestore(&epc->lock, flags);
434 EXPORT_SYMBOL_GPL(pci_epc_linkup);
437 * pci_epc_destroy() - destroy the EPC device
438 * @epc: the EPC device that has to be destroyed
440 * Invoke to destroy the PCI EPC device
442 void pci_epc_destroy(struct pci_epc *epc)
444 pci_ep_cfs_remove_epc_group(epc->group);
445 device_unregister(&epc->dev);
448 EXPORT_SYMBOL_GPL(pci_epc_destroy);
451 * devm_pci_epc_destroy() - destroy the EPC device
452 * @dev: device that wants to destroy the EPC
453 * @epc: the EPC device that has to be destroyed
455 * Invoke to destroy the devres associated with this
456 * pci_epc and destroy the EPC device.
458 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
462 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
464 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
466 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
469 * __pci_epc_create() - create a new endpoint controller (EPC) device
470 * @dev: device that is creating the new EPC
471 * @ops: function pointers for performing EPC operations
472 * @owner: the owner of the module that creates the EPC device
474 * Invoke to create a new EPC device and add it to pci_epc class.
477 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
478 struct module *owner)
488 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
494 spin_lock_init(&epc->lock);
495 INIT_LIST_HEAD(&epc->pci_epf);
497 device_initialize(&epc->dev);
498 epc->dev.class = pci_epc_class;
499 epc->dev.parent = dev;
502 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
506 ret = device_add(&epc->dev);
510 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
515 put_device(&epc->dev);
521 EXPORT_SYMBOL_GPL(__pci_epc_create);
524 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
525 * @dev: device that is creating the new EPC
526 * @ops: function pointers for performing EPC operations
527 * @owner: the owner of the module that creates the EPC device
529 * Invoke to create a new EPC device and add it to pci_epc class.
530 * While at that, it also associates the device with the pci_epc using devres.
531 * On driver detach, release function is invoked on the devres data,
532 * then, devres data is freed.
535 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
536 struct module *owner)
538 struct pci_epc **ptr, *epc;
540 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
542 return ERR_PTR(-ENOMEM);
544 epc = __pci_epc_create(dev, ops, owner);
547 devres_add(dev, ptr);
554 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
556 static int __init pci_epc_init(void)
558 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
559 if (IS_ERR(pci_epc_class)) {
560 pr_err("failed to create pci epc class --> %ld\n",
561 PTR_ERR(pci_epc_class));
562 return PTR_ERR(pci_epc_class);
567 module_init(pci_epc_init);
569 static void __exit pci_epc_exit(void)
571 class_destroy(pci_epc_class);
573 module_exit(pci_epc_exit);
575 MODULE_DESCRIPTION("PCI EPC Library");
576 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
577 MODULE_LICENSE("GPL v2");