pci-epf-core.c 8.4 KB

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