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/dma-mapping.h>
22 #include <linux/slab.h>
23 #include <linux/module.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)
384 dma_set_coherent_mask(&epf->dev, epc->dev.coherent_dma_mask);
385 epf->dev.dma_mask = epc->dev.dma_mask;
387 spin_lock_irqsave(&epc->lock, flags);
388 list_add_tail(&epf->list, &epc->pci_epf);
389 spin_unlock_irqrestore(&epc->lock, flags);
393 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
396 * pci_epc_remove_epf() - remove PCI endpoint function from endpoint controller
397 * @epc: the EPC device from which the endpoint function should be removed
398 * @epf: the endpoint function to be removed
400 * Invoke to remove PCI endpoint function from the endpoint controller.
402 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf)
406 if (!epc || IS_ERR(epc))
409 spin_lock_irqsave(&epc->lock, flags);
410 list_del(&epf->list);
411 spin_unlock_irqrestore(&epc->lock, flags);
413 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
416 * pci_epc_linkup() - Notify the EPF device that EPC device has established a
417 * connection with the Root Complex.
418 * @epc: the EPC device which has established link with the host
420 * Invoke to Notify the EPF device that the EPC device has established a
421 * connection with the Root Complex.
423 void pci_epc_linkup(struct pci_epc *epc)
428 if (!epc || IS_ERR(epc))
431 spin_lock_irqsave(&epc->lock, flags);
432 list_for_each_entry(epf, &epc->pci_epf, list)
434 spin_unlock_irqrestore(&epc->lock, flags);
436 EXPORT_SYMBOL_GPL(pci_epc_linkup);
439 * pci_epc_destroy() - destroy the EPC device
440 * @epc: the EPC device that has to be destroyed
442 * Invoke to destroy the PCI EPC device
444 void pci_epc_destroy(struct pci_epc *epc)
446 pci_ep_cfs_remove_epc_group(epc->group);
447 device_unregister(&epc->dev);
450 EXPORT_SYMBOL_GPL(pci_epc_destroy);
453 * devm_pci_epc_destroy() - destroy the EPC device
454 * @dev: device that wants to destroy the EPC
455 * @epc: the EPC device that has to be destroyed
457 * Invoke to destroy the devres associated with this
458 * pci_epc and destroy the EPC device.
460 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
464 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
466 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
468 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
471 * __pci_epc_create() - create a new endpoint controller (EPC) device
472 * @dev: device that is creating the new EPC
473 * @ops: function pointers for performing EPC operations
474 * @owner: the owner of the module that creates the EPC device
476 * Invoke to create a new EPC device and add it to pci_epc class.
479 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
480 struct module *owner)
490 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
496 spin_lock_init(&epc->lock);
497 INIT_LIST_HEAD(&epc->pci_epf);
499 device_initialize(&epc->dev);
500 dma_set_coherent_mask(&epc->dev, dev->coherent_dma_mask);
501 epc->dev.class = pci_epc_class;
502 epc->dev.dma_mask = dev->dma_mask;
505 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
509 ret = device_add(&epc->dev);
513 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
518 put_device(&epc->dev);
524 EXPORT_SYMBOL_GPL(__pci_epc_create);
527 * __devm_pci_epc_create() - create a new endpoint controller (EPC) device
528 * @dev: device that is creating the new EPC
529 * @ops: function pointers for performing EPC operations
530 * @owner: the owner of the module that creates the EPC device
532 * Invoke to create a new EPC device and add it to pci_epc class.
533 * While at that, it also associates the device with the pci_epc using devres.
534 * On driver detach, release function is invoked on the devres data,
535 * then, devres data is freed.
538 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
539 struct module *owner)
541 struct pci_epc **ptr, *epc;
543 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
545 return ERR_PTR(-ENOMEM);
547 epc = __pci_epc_create(dev, ops, owner);
550 devres_add(dev, ptr);
557 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
559 static int __init pci_epc_init(void)
561 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
562 if (IS_ERR(pci_epc_class)) {
563 pr_err("failed to create pci epc class --> %ld\n",
564 PTR_ERR(pci_epc_class));
565 return PTR_ERR(pci_epc_class);
570 module_init(pci_epc_init);
572 static void __exit pci_epc_exit(void)
574 class_destroy(pci_epc_class);
576 module_exit(pci_epc_exit);
578 MODULE_DESCRIPTION("PCI EPC Library");
579 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
580 MODULE_LICENSE("GPL v2");