dca-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59
  16. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called COPYING.
  20. */
  21. /*
  22. * This driver supports an interface for DCA clients and providers to meet.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/notifier.h>
  26. #include <linux/device.h>
  27. #include <linux/dca.h>
  28. #include <linux/slab.h>
  29. #define DCA_VERSION "1.12.1"
  30. MODULE_VERSION(DCA_VERSION);
  31. MODULE_LICENSE("GPL");
  32. MODULE_AUTHOR("Intel Corporation");
  33. static DEFINE_SPINLOCK(dca_lock);
  34. static LIST_HEAD(dca_domains);
  35. static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
  36. static int dca_providers_blocked;
  37. static struct pci_bus *dca_pci_rc_from_dev(struct device *dev)
  38. {
  39. struct pci_dev *pdev = to_pci_dev(dev);
  40. struct pci_bus *bus = pdev->bus;
  41. while (bus->parent)
  42. bus = bus->parent;
  43. return bus;
  44. }
  45. static struct dca_domain *dca_allocate_domain(struct pci_bus *rc)
  46. {
  47. struct dca_domain *domain;
  48. domain = kzalloc(sizeof(*domain), GFP_NOWAIT);
  49. if (!domain)
  50. return NULL;
  51. INIT_LIST_HEAD(&domain->dca_providers);
  52. domain->pci_rc = rc;
  53. return domain;
  54. }
  55. static void dca_free_domain(struct dca_domain *domain)
  56. {
  57. list_del(&domain->node);
  58. kfree(domain);
  59. }
  60. static int dca_provider_ioat_ver_3_0(struct device *dev)
  61. {
  62. struct pci_dev *pdev = to_pci_dev(dev);
  63. return ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
  64. ((pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG0) ||
  65. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG1) ||
  66. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG2) ||
  67. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG3) ||
  68. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG4) ||
  69. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG5) ||
  70. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG6) ||
  71. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG7)));
  72. }
  73. static void unregister_dca_providers(void)
  74. {
  75. struct dca_provider *dca, *_dca;
  76. struct list_head unregistered_providers;
  77. struct dca_domain *domain;
  78. unsigned long flags;
  79. blocking_notifier_call_chain(&dca_provider_chain,
  80. DCA_PROVIDER_REMOVE, NULL);
  81. INIT_LIST_HEAD(&unregistered_providers);
  82. spin_lock_irqsave(&dca_lock, flags);
  83. if (list_empty(&dca_domains)) {
  84. spin_unlock_irqrestore(&dca_lock, flags);
  85. return;
  86. }
  87. /* at this point only one domain in the list is expected */
  88. domain = list_first_entry(&dca_domains, struct dca_domain, node);
  89. if (!domain)
  90. return;
  91. list_for_each_entry_safe(dca, _dca, &domain->dca_providers, node) {
  92. list_del(&dca->node);
  93. list_add(&dca->node, &unregistered_providers);
  94. }
  95. dca_free_domain(domain);
  96. spin_unlock_irqrestore(&dca_lock, flags);
  97. list_for_each_entry_safe(dca, _dca, &unregistered_providers, node) {
  98. dca_sysfs_remove_provider(dca);
  99. list_del(&dca->node);
  100. }
  101. }
  102. static struct dca_domain *dca_find_domain(struct pci_bus *rc)
  103. {
  104. struct dca_domain *domain;
  105. list_for_each_entry(domain, &dca_domains, node)
  106. if (domain->pci_rc == rc)
  107. return domain;
  108. return NULL;
  109. }
  110. static struct dca_domain *dca_get_domain(struct device *dev)
  111. {
  112. struct pci_bus *rc;
  113. struct dca_domain *domain;
  114. rc = dca_pci_rc_from_dev(dev);
  115. domain = dca_find_domain(rc);
  116. if (!domain) {
  117. if (dca_provider_ioat_ver_3_0(dev) && !list_empty(&dca_domains)) {
  118. dca_providers_blocked = 1;
  119. } else {
  120. domain = dca_allocate_domain(rc);
  121. if (domain)
  122. list_add(&domain->node, &dca_domains);
  123. }
  124. }
  125. return domain;
  126. }
  127. static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
  128. {
  129. struct dca_provider *dca;
  130. struct pci_bus *rc;
  131. struct dca_domain *domain;
  132. if (dev) {
  133. rc = dca_pci_rc_from_dev(dev);
  134. domain = dca_find_domain(rc);
  135. if (!domain)
  136. return NULL;
  137. } else {
  138. if (!list_empty(&dca_domains))
  139. domain = list_first_entry(&dca_domains,
  140. struct dca_domain,
  141. node);
  142. else
  143. return NULL;
  144. }
  145. list_for_each_entry(dca, &domain->dca_providers, node)
  146. if ((!dev) || (dca->ops->dev_managed(dca, dev)))
  147. return dca;
  148. return NULL;
  149. }
  150. /**
  151. * dca_add_requester - add a dca client to the list
  152. * @dev - the device that wants dca service
  153. */
  154. int dca_add_requester(struct device *dev)
  155. {
  156. struct dca_provider *dca;
  157. int err, slot = -ENODEV;
  158. unsigned long flags;
  159. struct pci_bus *pci_rc;
  160. struct dca_domain *domain;
  161. if (!dev)
  162. return -EFAULT;
  163. spin_lock_irqsave(&dca_lock, flags);
  164. /* check if the requester has not been added already */
  165. dca = dca_find_provider_by_dev(dev);
  166. if (dca) {
  167. spin_unlock_irqrestore(&dca_lock, flags);
  168. return -EEXIST;
  169. }
  170. pci_rc = dca_pci_rc_from_dev(dev);
  171. domain = dca_find_domain(pci_rc);
  172. if (!domain) {
  173. spin_unlock_irqrestore(&dca_lock, flags);
  174. return -ENODEV;
  175. }
  176. list_for_each_entry(dca, &domain->dca_providers, node) {
  177. slot = dca->ops->add_requester(dca, dev);
  178. if (slot >= 0)
  179. break;
  180. }
  181. spin_unlock_irqrestore(&dca_lock, flags);
  182. if (slot < 0)
  183. return slot;
  184. err = dca_sysfs_add_req(dca, dev, slot);
  185. if (err) {
  186. spin_lock_irqsave(&dca_lock, flags);
  187. if (dca == dca_find_provider_by_dev(dev))
  188. dca->ops->remove_requester(dca, dev);
  189. spin_unlock_irqrestore(&dca_lock, flags);
  190. return err;
  191. }
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(dca_add_requester);
  195. /**
  196. * dca_remove_requester - remove a dca client from the list
  197. * @dev - the device that wants dca service
  198. */
  199. int dca_remove_requester(struct device *dev)
  200. {
  201. struct dca_provider *dca;
  202. int slot;
  203. unsigned long flags;
  204. if (!dev)
  205. return -EFAULT;
  206. spin_lock_irqsave(&dca_lock, flags);
  207. dca = dca_find_provider_by_dev(dev);
  208. if (!dca) {
  209. spin_unlock_irqrestore(&dca_lock, flags);
  210. return -ENODEV;
  211. }
  212. slot = dca->ops->remove_requester(dca, dev);
  213. spin_unlock_irqrestore(&dca_lock, flags);
  214. if (slot < 0)
  215. return slot;
  216. dca_sysfs_remove_req(dca, slot);
  217. return 0;
  218. }
  219. EXPORT_SYMBOL_GPL(dca_remove_requester);
  220. /**
  221. * dca_common_get_tag - return the dca tag (serves both new and old api)
  222. * @dev - the device that wants dca service
  223. * @cpu - the cpuid as returned by get_cpu()
  224. */
  225. u8 dca_common_get_tag(struct device *dev, int cpu)
  226. {
  227. struct dca_provider *dca;
  228. u8 tag;
  229. unsigned long flags;
  230. spin_lock_irqsave(&dca_lock, flags);
  231. dca = dca_find_provider_by_dev(dev);
  232. if (!dca) {
  233. spin_unlock_irqrestore(&dca_lock, flags);
  234. return -ENODEV;
  235. }
  236. tag = dca->ops->get_tag(dca, dev, cpu);
  237. spin_unlock_irqrestore(&dca_lock, flags);
  238. return tag;
  239. }
  240. /**
  241. * dca3_get_tag - return the dca tag to the requester device
  242. * for the given cpu (new api)
  243. * @dev - the device that wants dca service
  244. * @cpu - the cpuid as returned by get_cpu()
  245. */
  246. u8 dca3_get_tag(struct device *dev, int cpu)
  247. {
  248. if (!dev)
  249. return -EFAULT;
  250. return dca_common_get_tag(dev, cpu);
  251. }
  252. EXPORT_SYMBOL_GPL(dca3_get_tag);
  253. /**
  254. * dca_get_tag - return the dca tag for the given cpu (old api)
  255. * @cpu - the cpuid as returned by get_cpu()
  256. */
  257. u8 dca_get_tag(int cpu)
  258. {
  259. struct device *dev = NULL;
  260. return dca_common_get_tag(dev, cpu);
  261. }
  262. EXPORT_SYMBOL_GPL(dca_get_tag);
  263. /**
  264. * alloc_dca_provider - get data struct for describing a dca provider
  265. * @ops - pointer to struct of dca operation function pointers
  266. * @priv_size - size of extra mem to be added for provider's needs
  267. */
  268. struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
  269. {
  270. struct dca_provider *dca;
  271. int alloc_size;
  272. alloc_size = (sizeof(*dca) + priv_size);
  273. dca = kzalloc(alloc_size, GFP_KERNEL);
  274. if (!dca)
  275. return NULL;
  276. dca->ops = ops;
  277. return dca;
  278. }
  279. EXPORT_SYMBOL_GPL(alloc_dca_provider);
  280. /**
  281. * free_dca_provider - release the dca provider data struct
  282. * @ops - pointer to struct of dca operation function pointers
  283. * @priv_size - size of extra mem to be added for provider's needs
  284. */
  285. void free_dca_provider(struct dca_provider *dca)
  286. {
  287. kfree(dca);
  288. }
  289. EXPORT_SYMBOL_GPL(free_dca_provider);
  290. /**
  291. * register_dca_provider - register a dca provider
  292. * @dca - struct created by alloc_dca_provider()
  293. * @dev - device providing dca services
  294. */
  295. int register_dca_provider(struct dca_provider *dca, struct device *dev)
  296. {
  297. int err;
  298. unsigned long flags;
  299. struct dca_domain *domain;
  300. spin_lock_irqsave(&dca_lock, flags);
  301. if (dca_providers_blocked) {
  302. spin_unlock_irqrestore(&dca_lock, flags);
  303. return -ENODEV;
  304. }
  305. spin_unlock_irqrestore(&dca_lock, flags);
  306. err = dca_sysfs_add_provider(dca, dev);
  307. if (err)
  308. return err;
  309. spin_lock_irqsave(&dca_lock, flags);
  310. domain = dca_get_domain(dev);
  311. if (!domain) {
  312. if (dca_providers_blocked) {
  313. spin_unlock_irqrestore(&dca_lock, flags);
  314. dca_sysfs_remove_provider(dca);
  315. unregister_dca_providers();
  316. } else {
  317. spin_unlock_irqrestore(&dca_lock, flags);
  318. }
  319. return -ENODEV;
  320. }
  321. list_add(&dca->node, &domain->dca_providers);
  322. spin_unlock_irqrestore(&dca_lock, flags);
  323. blocking_notifier_call_chain(&dca_provider_chain,
  324. DCA_PROVIDER_ADD, NULL);
  325. return 0;
  326. }
  327. EXPORT_SYMBOL_GPL(register_dca_provider);
  328. /**
  329. * unregister_dca_provider - remove a dca provider
  330. * @dca - struct created by alloc_dca_provider()
  331. */
  332. void unregister_dca_provider(struct dca_provider *dca, struct device *dev)
  333. {
  334. unsigned long flags;
  335. struct pci_bus *pci_rc;
  336. struct dca_domain *domain;
  337. blocking_notifier_call_chain(&dca_provider_chain,
  338. DCA_PROVIDER_REMOVE, NULL);
  339. spin_lock_irqsave(&dca_lock, flags);
  340. list_del(&dca->node);
  341. pci_rc = dca_pci_rc_from_dev(dev);
  342. domain = dca_find_domain(pci_rc);
  343. if (list_empty(&domain->dca_providers))
  344. dca_free_domain(domain);
  345. spin_unlock_irqrestore(&dca_lock, flags);
  346. dca_sysfs_remove_provider(dca);
  347. }
  348. EXPORT_SYMBOL_GPL(unregister_dca_provider);
  349. /**
  350. * dca_register_notify - register a client's notifier callback
  351. */
  352. void dca_register_notify(struct notifier_block *nb)
  353. {
  354. blocking_notifier_chain_register(&dca_provider_chain, nb);
  355. }
  356. EXPORT_SYMBOL_GPL(dca_register_notify);
  357. /**
  358. * dca_unregister_notify - remove a client's notifier callback
  359. */
  360. void dca_unregister_notify(struct notifier_block *nb)
  361. {
  362. blocking_notifier_chain_unregister(&dca_provider_chain, nb);
  363. }
  364. EXPORT_SYMBOL_GPL(dca_unregister_notify);
  365. static int __init dca_init(void)
  366. {
  367. pr_info("dca service started, version %s\n", DCA_VERSION);
  368. return dca_sysfs_init();
  369. }
  370. static void __exit dca_exit(void)
  371. {
  372. dca_sysfs_exit();
  373. }
  374. arch_initcall(dca_init);
  375. module_exit(dca_exit);