1 // SPDX-License-Identifier: GPL-2.0
3 * PCI Endpoint *Function* (EPF) library
5 * Copyright (C) 2017 Texas Instruments
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
9 #include <linux/device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
14 #include <linux/pci-epc.h>
15 #include <linux/pci-epf.h>
16 #include <linux/pci-ep-cfs.h>
18 static DEFINE_MUTEX(pci_epf_mutex);
20 static struct bus_type pci_epf_bus_type;
21 static const struct device_type pci_epf_type;
24 * pci_epf_type_add_cfs() - Help function drivers to expose function specific
25 * attributes in configfs
26 * @epf: the EPF device that has to be configured using configfs
27 * @group: the parent configfs group (corresponding to entries in
30 * Invoke to expose function specific attributes in configfs. If the function
31 * driver does not have anything to expose (attributes configured by user),
34 struct config_group *pci_epf_type_add_cfs(struct pci_epf *epf,
35 struct config_group *group)
37 struct config_group *epf_type_group;
40 dev_err(&epf->dev, "epf device not bound to driver\n");
44 if (!epf->driver->ops->add_cfs)
47 mutex_lock(&epf->lock);
48 epf_type_group = epf->driver->ops->add_cfs(epf, group);
49 mutex_unlock(&epf->lock);
51 return epf_type_group;
53 EXPORT_SYMBOL_GPL(pci_epf_type_add_cfs);
56 * pci_epf_unbind() - Notify the function driver that the binding between the
57 * EPF device and EPC device has been lost
58 * @epf: the EPF device which has lost the binding with the EPC device
60 * Invoke to notify the function driver that the binding between the EPF device
61 * and EPC device has been lost.
63 void pci_epf_unbind(struct pci_epf *epf)
66 dev_WARN(&epf->dev, "epf device not bound to driver\n");
70 mutex_lock(&epf->lock);
71 epf->driver->ops->unbind(epf);
72 mutex_unlock(&epf->lock);
73 module_put(epf->driver->owner);
75 EXPORT_SYMBOL_GPL(pci_epf_unbind);
78 * pci_epf_bind() - Notify the function driver that the EPF device has been
79 * bound to a EPC device
80 * @epf: the EPF device which has been bound to the EPC device
82 * Invoke to notify the function driver that it has been bound to a EPC device
84 int pci_epf_bind(struct pci_epf *epf)
89 dev_WARN(&epf->dev, "epf device not bound to driver\n");
93 if (!try_module_get(epf->driver->owner))
96 mutex_lock(&epf->lock);
97 ret = epf->driver->ops->bind(epf);
98 mutex_unlock(&epf->lock);
102 EXPORT_SYMBOL_GPL(pci_epf_bind);
105 * pci_epf_free_space() - free the allocated PCI EPF register space
106 * @epf: the EPF device from whom to free the memory
107 * @addr: the virtual address of the PCI EPF register space
108 * @bar: the BAR number corresponding to the register space
109 * @type: Identifies if the allocated space is for primary EPC or secondary EPC
111 * Invoke to free the allocated PCI EPF register space.
113 void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar,
114 enum pci_epc_interface_type type)
116 struct device *dev = epf->epc->dev.parent;
117 struct pci_epf_bar *epf_bar;
123 if (type == PRIMARY_INTERFACE) {
128 epf_bar = epf->sec_epc_bar;
131 dev = epc->dev.parent;
132 dma_free_coherent(dev, epf_bar[bar].size, addr,
133 epf_bar[bar].phys_addr);
135 epf_bar[bar].phys_addr = 0;
136 epf_bar[bar].addr = NULL;
137 epf_bar[bar].size = 0;
138 epf_bar[bar].barno = 0;
139 epf_bar[bar].flags = 0;
141 EXPORT_SYMBOL_GPL(pci_epf_free_space);
144 * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
145 * @epf: the EPF device to whom allocate the memory
146 * @size: the size of the memory that has to be allocated
147 * @bar: the BAR number corresponding to the allocated register space
148 * @align: alignment size for the allocation region
149 * @type: Identifies if the allocation is for primary EPC or secondary EPC
151 * Invoke to allocate memory for the PCI EPF register space.
153 void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar,
154 size_t align, enum pci_epc_interface_type type)
156 struct pci_epf_bar *epf_bar;
157 dma_addr_t phys_addr;
166 size = ALIGN(size, align);
168 size = roundup_pow_of_two(size);
170 if (type == PRIMARY_INTERFACE) {
175 epf_bar = epf->sec_epc_bar;
178 dev = epc->dev.parent;
179 space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
181 dev_err(dev, "failed to allocate mem space\n");
185 epf_bar[bar].phys_addr = phys_addr;
186 epf_bar[bar].addr = space;
187 epf_bar[bar].size = size;
188 epf_bar[bar].barno = bar;
189 epf_bar[bar].flags |= upper_32_bits(size) ?
190 PCI_BASE_ADDRESS_MEM_TYPE_64 :
191 PCI_BASE_ADDRESS_MEM_TYPE_32;
195 EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
197 static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
199 struct config_group *group, *tmp;
201 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
204 mutex_lock(&pci_epf_mutex);
205 list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
206 pci_ep_cfs_remove_epf_group(group);
207 list_del(&driver->epf_group);
208 mutex_unlock(&pci_epf_mutex);
212 * pci_epf_unregister_driver() - unregister the PCI EPF driver
213 * @driver: the PCI EPF driver that has to be unregistered
215 * Invoke to unregister the PCI EPF driver.
217 void pci_epf_unregister_driver(struct pci_epf_driver *driver)
219 pci_epf_remove_cfs(driver);
220 driver_unregister(&driver->driver);
222 EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
224 static int pci_epf_add_cfs(struct pci_epf_driver *driver)
226 struct config_group *group;
227 const struct pci_epf_device_id *id;
229 if (!IS_ENABLED(CONFIG_PCI_ENDPOINT_CONFIGFS))
232 INIT_LIST_HEAD(&driver->epf_group);
234 id = driver->id_table;
235 while (id->name[0]) {
236 group = pci_ep_cfs_add_epf_group(id->name);
238 pci_epf_remove_cfs(driver);
239 return PTR_ERR(group);
242 mutex_lock(&pci_epf_mutex);
243 list_add_tail(&group->group_entry, &driver->epf_group);
244 mutex_unlock(&pci_epf_mutex);
252 * __pci_epf_register_driver() - register a new PCI EPF driver
253 * @driver: structure representing PCI EPF driver
254 * @owner: the owner of the module that registers the PCI EPF driver
256 * Invoke to register a new PCI EPF driver.
258 int __pci_epf_register_driver(struct pci_epf_driver *driver,
259 struct module *owner)
266 if (!driver->ops->bind || !driver->ops->unbind)
269 driver->driver.bus = &pci_epf_bus_type;
270 driver->driver.owner = owner;
272 ret = driver_register(&driver->driver);
276 pci_epf_add_cfs(driver);
280 EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
283 * pci_epf_destroy() - destroy the created PCI EPF device
284 * @epf: the PCI EPF device that has to be destroyed.
286 * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
288 void pci_epf_destroy(struct pci_epf *epf)
290 device_unregister(&epf->dev);
292 EXPORT_SYMBOL_GPL(pci_epf_destroy);
295 * pci_epf_create() - create a new PCI EPF device
296 * @name: the name of the PCI EPF device. This name will be used to bind the
297 * the EPF device to a EPF driver
299 * Invoke to create a new PCI EPF device by providing the name of the function
302 struct pci_epf *pci_epf_create(const char *name)
309 epf = kzalloc(sizeof(*epf), GFP_KERNEL);
311 return ERR_PTR(-ENOMEM);
313 len = strchrnul(name, '.') - name;
314 epf->name = kstrndup(name, len, GFP_KERNEL);
317 return ERR_PTR(-ENOMEM);
321 device_initialize(dev);
322 dev->bus = &pci_epf_bus_type;
323 dev->type = &pci_epf_type;
324 mutex_init(&epf->lock);
326 ret = dev_set_name(dev, "%s", name);
332 ret = device_add(dev);
340 EXPORT_SYMBOL_GPL(pci_epf_create);
342 static void pci_epf_dev_release(struct device *dev)
344 struct pci_epf *epf = to_pci_epf(dev);
350 static const struct device_type pci_epf_type = {
351 .release = pci_epf_dev_release,
355 pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
357 while (id->name[0]) {
358 if (strcmp(epf->name, id->name) == 0)
366 static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
368 struct pci_epf *epf = to_pci_epf(dev);
369 struct pci_epf_driver *driver = to_pci_epf_driver(drv);
371 if (driver->id_table)
372 return pci_epf_match_id(driver->id_table, epf);
374 return !strcmp(epf->name, drv->name);
377 static int pci_epf_device_probe(struct device *dev)
379 struct pci_epf *epf = to_pci_epf(dev);
380 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
385 epf->driver = driver;
387 return driver->probe(epf);
390 static int pci_epf_device_remove(struct device *dev)
393 struct pci_epf *epf = to_pci_epf(dev);
394 struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
397 ret = driver->remove(epf);
403 static struct bus_type pci_epf_bus_type = {
405 .match = pci_epf_device_match,
406 .probe = pci_epf_device_probe,
407 .remove = pci_epf_device_remove,
410 static int __init pci_epf_init(void)
414 ret = bus_register(&pci_epf_bus_type);
416 pr_err("failed to register pci epf bus --> %d\n", ret);
422 module_init(pci_epf_init);
424 static void __exit pci_epf_exit(void)
426 bus_unregister(&pci_epf_bus_type);
428 module_exit(pci_epf_exit);
430 MODULE_DESCRIPTION("PCI EPF Library");
431 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
432 MODULE_LICENSE("GPL v2");