dca-core.c 11 KB

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