dca-core.c 11 KB

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