dca-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. list_for_each_entry_safe(dca, _dca, &domain->dca_providers, node)
  90. list_move(&dca->node, &unregistered_providers);
  91. dca_free_domain(domain);
  92. spin_unlock_irqrestore(&dca_lock, flags);
  93. list_for_each_entry_safe(dca, _dca, &unregistered_providers, node) {
  94. dca_sysfs_remove_provider(dca);
  95. list_del(&dca->node);
  96. }
  97. }
  98. static struct dca_domain *dca_find_domain(struct pci_bus *rc)
  99. {
  100. struct dca_domain *domain;
  101. list_for_each_entry(domain, &dca_domains, node)
  102. if (domain->pci_rc == rc)
  103. return domain;
  104. return NULL;
  105. }
  106. static struct dca_domain *dca_get_domain(struct device *dev)
  107. {
  108. struct pci_bus *rc;
  109. struct dca_domain *domain;
  110. rc = dca_pci_rc_from_dev(dev);
  111. domain = dca_find_domain(rc);
  112. if (!domain) {
  113. if (dca_provider_ioat_ver_3_0(dev) && !list_empty(&dca_domains)) {
  114. dca_providers_blocked = 1;
  115. } else {
  116. domain = dca_allocate_domain(rc);
  117. if (domain)
  118. list_add(&domain->node, &dca_domains);
  119. }
  120. }
  121. return domain;
  122. }
  123. static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
  124. {
  125. struct dca_provider *dca;
  126. struct pci_bus *rc;
  127. struct dca_domain *domain;
  128. if (dev) {
  129. rc = dca_pci_rc_from_dev(dev);
  130. domain = dca_find_domain(rc);
  131. if (!domain)
  132. return NULL;
  133. } else {
  134. if (!list_empty(&dca_domains))
  135. domain = list_first_entry(&dca_domains,
  136. struct dca_domain,
  137. node);
  138. else
  139. return NULL;
  140. }
  141. list_for_each_entry(dca, &domain->dca_providers, node)
  142. if ((!dev) || (dca->ops->dev_managed(dca, dev)))
  143. return dca;
  144. return NULL;
  145. }
  146. /**
  147. * dca_add_requester - add a dca client to the list
  148. * @dev - the device that wants dca service
  149. */
  150. int dca_add_requester(struct device *dev)
  151. {
  152. struct dca_provider *dca;
  153. int err, slot = -ENODEV;
  154. unsigned long flags;
  155. struct pci_bus *pci_rc;
  156. struct dca_domain *domain;
  157. if (!dev)
  158. return -EFAULT;
  159. spin_lock_irqsave(&dca_lock, flags);
  160. /* check if the requester has not been added already */
  161. dca = dca_find_provider_by_dev(dev);
  162. if (dca) {
  163. spin_unlock_irqrestore(&dca_lock, flags);
  164. return -EEXIST;
  165. }
  166. pci_rc = dca_pci_rc_from_dev(dev);
  167. domain = dca_find_domain(pci_rc);
  168. if (!domain) {
  169. spin_unlock_irqrestore(&dca_lock, flags);
  170. return -ENODEV;
  171. }
  172. list_for_each_entry(dca, &domain->dca_providers, node) {
  173. slot = dca->ops->add_requester(dca, dev);
  174. if (slot >= 0)
  175. break;
  176. }
  177. spin_unlock_irqrestore(&dca_lock, flags);
  178. if (slot < 0)
  179. return slot;
  180. err = dca_sysfs_add_req(dca, dev, slot);
  181. if (err) {
  182. spin_lock_irqsave(&dca_lock, flags);
  183. if (dca == dca_find_provider_by_dev(dev))
  184. dca->ops->remove_requester(dca, dev);
  185. spin_unlock_irqrestore(&dca_lock, flags);
  186. return err;
  187. }
  188. return 0;
  189. }
  190. EXPORT_SYMBOL_GPL(dca_add_requester);
  191. /**
  192. * dca_remove_requester - remove a dca client from the list
  193. * @dev - the device that wants dca service
  194. */
  195. int dca_remove_requester(struct device *dev)
  196. {
  197. struct dca_provider *dca;
  198. int slot;
  199. unsigned long flags;
  200. if (!dev)
  201. return -EFAULT;
  202. spin_lock_irqsave(&dca_lock, flags);
  203. dca = dca_find_provider_by_dev(dev);
  204. if (!dca) {
  205. spin_unlock_irqrestore(&dca_lock, flags);
  206. return -ENODEV;
  207. }
  208. slot = dca->ops->remove_requester(dca, dev);
  209. spin_unlock_irqrestore(&dca_lock, flags);
  210. if (slot < 0)
  211. return slot;
  212. dca_sysfs_remove_req(dca, slot);
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(dca_remove_requester);
  216. /**
  217. * dca_common_get_tag - return the dca tag (serves both new and old api)
  218. * @dev - the device that wants dca service
  219. * @cpu - the cpuid as returned by get_cpu()
  220. */
  221. u8 dca_common_get_tag(struct device *dev, int cpu)
  222. {
  223. struct dca_provider *dca;
  224. u8 tag;
  225. unsigned long flags;
  226. spin_lock_irqsave(&dca_lock, flags);
  227. dca = dca_find_provider_by_dev(dev);
  228. if (!dca) {
  229. spin_unlock_irqrestore(&dca_lock, flags);
  230. return -ENODEV;
  231. }
  232. tag = dca->ops->get_tag(dca, dev, cpu);
  233. spin_unlock_irqrestore(&dca_lock, flags);
  234. return tag;
  235. }
  236. /**
  237. * dca3_get_tag - return the dca tag to the requester device
  238. * for the given cpu (new api)
  239. * @dev - the device that wants dca service
  240. * @cpu - the cpuid as returned by get_cpu()
  241. */
  242. u8 dca3_get_tag(struct device *dev, int cpu)
  243. {
  244. if (!dev)
  245. return -EFAULT;
  246. return dca_common_get_tag(dev, cpu);
  247. }
  248. EXPORT_SYMBOL_GPL(dca3_get_tag);
  249. /**
  250. * dca_get_tag - return the dca tag for the given cpu (old api)
  251. * @cpu - the cpuid as returned by get_cpu()
  252. */
  253. u8 dca_get_tag(int cpu)
  254. {
  255. struct device *dev = NULL;
  256. return dca_common_get_tag(dev, cpu);
  257. }
  258. EXPORT_SYMBOL_GPL(dca_get_tag);
  259. /**
  260. * alloc_dca_provider - get data struct for describing a dca provider
  261. * @ops - pointer to struct of dca operation function pointers
  262. * @priv_size - size of extra mem to be added for provider's needs
  263. */
  264. struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
  265. {
  266. struct dca_provider *dca;
  267. int alloc_size;
  268. alloc_size = (sizeof(*dca) + priv_size);
  269. dca = kzalloc(alloc_size, GFP_KERNEL);
  270. if (!dca)
  271. return NULL;
  272. dca->ops = ops;
  273. return dca;
  274. }
  275. EXPORT_SYMBOL_GPL(alloc_dca_provider);
  276. /**
  277. * free_dca_provider - release the dca provider data struct
  278. * @ops - pointer to struct of dca operation function pointers
  279. * @priv_size - size of extra mem to be added for provider's needs
  280. */
  281. void free_dca_provider(struct dca_provider *dca)
  282. {
  283. kfree(dca);
  284. }
  285. EXPORT_SYMBOL_GPL(free_dca_provider);
  286. /**
  287. * register_dca_provider - register a dca provider
  288. * @dca - struct created by alloc_dca_provider()
  289. * @dev - device providing dca services
  290. */
  291. int register_dca_provider(struct dca_provider *dca, struct device *dev)
  292. {
  293. int err;
  294. unsigned long flags;
  295. struct dca_domain *domain;
  296. spin_lock_irqsave(&dca_lock, flags);
  297. if (dca_providers_blocked) {
  298. spin_unlock_irqrestore(&dca_lock, flags);
  299. return -ENODEV;
  300. }
  301. spin_unlock_irqrestore(&dca_lock, flags);
  302. err = dca_sysfs_add_provider(dca, dev);
  303. if (err)
  304. return err;
  305. spin_lock_irqsave(&dca_lock, flags);
  306. domain = dca_get_domain(dev);
  307. if (!domain) {
  308. if (dca_providers_blocked) {
  309. spin_unlock_irqrestore(&dca_lock, flags);
  310. dca_sysfs_remove_provider(dca);
  311. unregister_dca_providers();
  312. } else {
  313. spin_unlock_irqrestore(&dca_lock, flags);
  314. }
  315. return -ENODEV;
  316. }
  317. list_add(&dca->node, &domain->dca_providers);
  318. spin_unlock_irqrestore(&dca_lock, flags);
  319. blocking_notifier_call_chain(&dca_provider_chain,
  320. DCA_PROVIDER_ADD, NULL);
  321. return 0;
  322. }
  323. EXPORT_SYMBOL_GPL(register_dca_provider);
  324. /**
  325. * unregister_dca_provider - remove a dca provider
  326. * @dca - struct created by alloc_dca_provider()
  327. */
  328. void unregister_dca_provider(struct dca_provider *dca, struct device *dev)
  329. {
  330. unsigned long flags;
  331. struct pci_bus *pci_rc;
  332. struct dca_domain *domain;
  333. blocking_notifier_call_chain(&dca_provider_chain,
  334. DCA_PROVIDER_REMOVE, NULL);
  335. spin_lock_irqsave(&dca_lock, flags);
  336. list_del(&dca->node);
  337. pci_rc = dca_pci_rc_from_dev(dev);
  338. domain = dca_find_domain(pci_rc);
  339. if (list_empty(&domain->dca_providers))
  340. dca_free_domain(domain);
  341. spin_unlock_irqrestore(&dca_lock, flags);
  342. dca_sysfs_remove_provider(dca);
  343. }
  344. EXPORT_SYMBOL_GPL(unregister_dca_provider);
  345. /**
  346. * dca_register_notify - register a client's notifier callback
  347. */
  348. void dca_register_notify(struct notifier_block *nb)
  349. {
  350. blocking_notifier_chain_register(&dca_provider_chain, nb);
  351. }
  352. EXPORT_SYMBOL_GPL(dca_register_notify);
  353. /**
  354. * dca_unregister_notify - remove a client's notifier callback
  355. */
  356. void dca_unregister_notify(struct notifier_block *nb)
  357. {
  358. blocking_notifier_chain_unregister(&dca_provider_chain, nb);
  359. }
  360. EXPORT_SYMBOL_GPL(dca_unregister_notify);
  361. static int __init dca_init(void)
  362. {
  363. pr_info("dca service started, version %s\n", DCA_VERSION);
  364. return dca_sysfs_init();
  365. }
  366. static void __exit dca_exit(void)
  367. {
  368. dca_sysfs_exit();
  369. }
  370. arch_initcall(dca_init);
  371. module_exit(dca_exit);