pci-epf-core.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * PCI Endpoint *Function* (EPF) library
  4. *
  5. * Copyright (C) 2017 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/pci-epc.h>
  13. #include <linux/pci-epf.h>
  14. #include <linux/pci-ep-cfs.h>
  15. static struct bus_type pci_epf_bus_type;
  16. static const struct device_type pci_epf_type;
  17. /**
  18. * pci_epf_linkup() - Notify the function driver that EPC device has
  19. * established a connection with the Root Complex.
  20. * @epf: the EPF device bound to the EPC device which has established
  21. * the connection with the host
  22. *
  23. * Invoke to notify the function driver that EPC device has established
  24. * a connection with the Root Complex.
  25. */
  26. void pci_epf_linkup(struct pci_epf *epf)
  27. {
  28. if (!epf->driver) {
  29. dev_WARN(&epf->dev, "epf device not bound to driver\n");
  30. return;
  31. }
  32. epf->driver->ops->linkup(epf);
  33. }
  34. EXPORT_SYMBOL_GPL(pci_epf_linkup);
  35. /**
  36. * pci_epf_unbind() - Notify the function driver that the binding between the
  37. * EPF device and EPC device has been lost
  38. * @epf: the EPF device which has lost the binding with the EPC device
  39. *
  40. * Invoke to notify the function driver that the binding between the EPF device
  41. * and EPC device has been lost.
  42. */
  43. void pci_epf_unbind(struct pci_epf *epf)
  44. {
  45. if (!epf->driver) {
  46. dev_WARN(&epf->dev, "epf device not bound to driver\n");
  47. return;
  48. }
  49. epf->driver->ops->unbind(epf);
  50. module_put(epf->driver->owner);
  51. }
  52. EXPORT_SYMBOL_GPL(pci_epf_unbind);
  53. /**
  54. * pci_epf_bind() - Notify the function driver that the EPF device has been
  55. * bound to a EPC device
  56. * @epf: the EPF device which has been bound to the EPC device
  57. *
  58. * Invoke to notify the function driver that it has been bound to a EPC device
  59. */
  60. int pci_epf_bind(struct pci_epf *epf)
  61. {
  62. if (!epf->driver) {
  63. dev_WARN(&epf->dev, "epf device not bound to driver\n");
  64. return -EINVAL;
  65. }
  66. if (!try_module_get(epf->driver->owner))
  67. return -EAGAIN;
  68. return epf->driver->ops->bind(epf);
  69. }
  70. EXPORT_SYMBOL_GPL(pci_epf_bind);
  71. /**
  72. * pci_epf_free_space() - free the allocated PCI EPF register space
  73. * @addr: the virtual address of the PCI EPF register space
  74. * @bar: the BAR number corresponding to the register space
  75. *
  76. * Invoke to free the allocated PCI EPF register space.
  77. */
  78. void pci_epf_free_space(struct pci_epf *epf, void *addr, enum pci_barno bar)
  79. {
  80. struct device *dev = epf->epc->dev.parent;
  81. if (!addr)
  82. return;
  83. dma_free_coherent(dev, epf->bar[bar].size, addr,
  84. epf->bar[bar].phys_addr);
  85. epf->bar[bar].phys_addr = 0;
  86. epf->bar[bar].size = 0;
  87. epf->bar[bar].barno = 0;
  88. epf->bar[bar].flags = 0;
  89. }
  90. EXPORT_SYMBOL_GPL(pci_epf_free_space);
  91. /**
  92. * pci_epf_alloc_space() - allocate memory for the PCI EPF register space
  93. * @size: the size of the memory that has to be allocated
  94. * @bar: the BAR number corresponding to the allocated register space
  95. *
  96. * Invoke to allocate memory for the PCI EPF register space.
  97. */
  98. void *pci_epf_alloc_space(struct pci_epf *epf, size_t size, enum pci_barno bar)
  99. {
  100. void *space;
  101. struct device *dev = epf->epc->dev.parent;
  102. dma_addr_t phys_addr;
  103. if (size < 128)
  104. size = 128;
  105. size = roundup_pow_of_two(size);
  106. space = dma_alloc_coherent(dev, size, &phys_addr, GFP_KERNEL);
  107. if (!space) {
  108. dev_err(dev, "failed to allocate mem space\n");
  109. return NULL;
  110. }
  111. epf->bar[bar].phys_addr = phys_addr;
  112. epf->bar[bar].size = size;
  113. epf->bar[bar].barno = bar;
  114. epf->bar[bar].flags = PCI_BASE_ADDRESS_SPACE_MEMORY;
  115. return space;
  116. }
  117. EXPORT_SYMBOL_GPL(pci_epf_alloc_space);
  118. /**
  119. * pci_epf_unregister_driver() - unregister the PCI EPF driver
  120. * @driver: the PCI EPF driver that has to be unregistered
  121. *
  122. * Invoke to unregister the PCI EPF driver.
  123. */
  124. void pci_epf_unregister_driver(struct pci_epf_driver *driver)
  125. {
  126. pci_ep_cfs_remove_epf_group(driver->group);
  127. driver_unregister(&driver->driver);
  128. }
  129. EXPORT_SYMBOL_GPL(pci_epf_unregister_driver);
  130. /**
  131. * __pci_epf_register_driver() - register a new PCI EPF driver
  132. * @driver: structure representing PCI EPF driver
  133. * @owner: the owner of the module that registers the PCI EPF driver
  134. *
  135. * Invoke to register a new PCI EPF driver.
  136. */
  137. int __pci_epf_register_driver(struct pci_epf_driver *driver,
  138. struct module *owner)
  139. {
  140. int ret;
  141. if (!driver->ops)
  142. return -EINVAL;
  143. if (!driver->ops->bind || !driver->ops->unbind || !driver->ops->linkup)
  144. return -EINVAL;
  145. driver->driver.bus = &pci_epf_bus_type;
  146. driver->driver.owner = owner;
  147. ret = driver_register(&driver->driver);
  148. if (ret)
  149. return ret;
  150. driver->group = pci_ep_cfs_add_epf_group(driver->driver.name);
  151. return 0;
  152. }
  153. EXPORT_SYMBOL_GPL(__pci_epf_register_driver);
  154. /**
  155. * pci_epf_destroy() - destroy the created PCI EPF device
  156. * @epf: the PCI EPF device that has to be destroyed.
  157. *
  158. * Invoke to destroy the PCI EPF device created by invoking pci_epf_create().
  159. */
  160. void pci_epf_destroy(struct pci_epf *epf)
  161. {
  162. device_unregister(&epf->dev);
  163. }
  164. EXPORT_SYMBOL_GPL(pci_epf_destroy);
  165. /**
  166. * pci_epf_create() - create a new PCI EPF device
  167. * @name: the name of the PCI EPF device. This name will be used to bind the
  168. * the EPF device to a EPF driver
  169. *
  170. * Invoke to create a new PCI EPF device by providing the name of the function
  171. * device.
  172. */
  173. struct pci_epf *pci_epf_create(const char *name)
  174. {
  175. int ret;
  176. struct pci_epf *epf;
  177. struct device *dev;
  178. int len;
  179. epf = kzalloc(sizeof(*epf), GFP_KERNEL);
  180. if (!epf)
  181. return ERR_PTR(-ENOMEM);
  182. len = strchrnul(name, '.') - name;
  183. epf->name = kstrndup(name, len, GFP_KERNEL);
  184. if (!epf->name) {
  185. kfree(epf);
  186. return ERR_PTR(-ENOMEM);
  187. }
  188. dev = &epf->dev;
  189. device_initialize(dev);
  190. dev->bus = &pci_epf_bus_type;
  191. dev->type = &pci_epf_type;
  192. ret = dev_set_name(dev, "%s", name);
  193. if (ret) {
  194. put_device(dev);
  195. return ERR_PTR(ret);
  196. }
  197. ret = device_add(dev);
  198. if (ret) {
  199. put_device(dev);
  200. return ERR_PTR(ret);
  201. }
  202. return epf;
  203. }
  204. EXPORT_SYMBOL_GPL(pci_epf_create);
  205. const struct pci_epf_device_id *
  206. pci_epf_match_device(const struct pci_epf_device_id *id, struct pci_epf *epf)
  207. {
  208. if (!id || !epf)
  209. return NULL;
  210. while (*id->name) {
  211. if (strcmp(epf->name, id->name) == 0)
  212. return id;
  213. id++;
  214. }
  215. return NULL;
  216. }
  217. EXPORT_SYMBOL_GPL(pci_epf_match_device);
  218. static void pci_epf_dev_release(struct device *dev)
  219. {
  220. struct pci_epf *epf = to_pci_epf(dev);
  221. kfree(epf->name);
  222. kfree(epf);
  223. }
  224. static const struct device_type pci_epf_type = {
  225. .release = pci_epf_dev_release,
  226. };
  227. static int
  228. pci_epf_match_id(const struct pci_epf_device_id *id, const struct pci_epf *epf)
  229. {
  230. while (id->name[0]) {
  231. if (strcmp(epf->name, id->name) == 0)
  232. return true;
  233. id++;
  234. }
  235. return false;
  236. }
  237. static int pci_epf_device_match(struct device *dev, struct device_driver *drv)
  238. {
  239. struct pci_epf *epf = to_pci_epf(dev);
  240. struct pci_epf_driver *driver = to_pci_epf_driver(drv);
  241. if (driver->id_table)
  242. return pci_epf_match_id(driver->id_table, epf);
  243. return !strcmp(epf->name, drv->name);
  244. }
  245. static int pci_epf_device_probe(struct device *dev)
  246. {
  247. struct pci_epf *epf = to_pci_epf(dev);
  248. struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
  249. if (!driver->probe)
  250. return -ENODEV;
  251. epf->driver = driver;
  252. return driver->probe(epf);
  253. }
  254. static int pci_epf_device_remove(struct device *dev)
  255. {
  256. int ret = 0;
  257. struct pci_epf *epf = to_pci_epf(dev);
  258. struct pci_epf_driver *driver = to_pci_epf_driver(dev->driver);
  259. if (driver->remove)
  260. ret = driver->remove(epf);
  261. epf->driver = NULL;
  262. return ret;
  263. }
  264. static struct bus_type pci_epf_bus_type = {
  265. .name = "pci-epf",
  266. .match = pci_epf_device_match,
  267. .probe = pci_epf_device_probe,
  268. .remove = pci_epf_device_remove,
  269. };
  270. static int __init pci_epf_init(void)
  271. {
  272. int ret;
  273. ret = bus_register(&pci_epf_bus_type);
  274. if (ret) {
  275. pr_err("failed to register pci epf bus --> %d\n", ret);
  276. return ret;
  277. }
  278. return 0;
  279. }
  280. module_init(pci_epf_init);
  281. static void __exit pci_epf_exit(void)
  282. {
  283. bus_unregister(&pci_epf_bus_type);
  284. }
  285. module_exit(pci_epf_exit);
  286. MODULE_DESCRIPTION("PCI EPF Library");
  287. MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
  288. MODULE_LICENSE("GPL v2");