dca-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. #include <linux/module.h>
  30. #define DCA_VERSION "1.12.1"
  31. MODULE_VERSION(DCA_VERSION);
  32. MODULE_LICENSE("GPL");
  33. MODULE_AUTHOR("Intel Corporation");
  34. static DEFINE_RAW_SPINLOCK(dca_lock);
  35. static LIST_HEAD(dca_domains);
  36. static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
  37. static int dca_providers_blocked;
  38. static struct pci_bus *dca_pci_rc_from_dev(struct device *dev)
  39. {
  40. struct pci_dev *pdev = to_pci_dev(dev);
  41. struct pci_bus *bus = pdev->bus;
  42. while (bus->parent)
  43. bus = bus->parent;
  44. return bus;
  45. }
  46. static struct dca_domain *dca_allocate_domain(struct pci_bus *rc)
  47. {
  48. struct dca_domain *domain;
  49. domain = kzalloc(sizeof(*domain), GFP_NOWAIT);
  50. if (!domain)
  51. return NULL;
  52. INIT_LIST_HEAD(&domain->dca_providers);
  53. domain->pci_rc = rc;
  54. return domain;
  55. }
  56. static void dca_free_domain(struct dca_domain *domain)
  57. {
  58. list_del(&domain->node);
  59. kfree(domain);
  60. }
  61. static int dca_provider_ioat_ver_3_0(struct device *dev)
  62. {
  63. struct pci_dev *pdev = to_pci_dev(dev);
  64. return ((pdev->vendor == PCI_VENDOR_ID_INTEL) &&
  65. ((pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG0) ||
  66. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG1) ||
  67. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG2) ||
  68. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG3) ||
  69. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG4) ||
  70. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG5) ||
  71. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG6) ||
  72. (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_TBG7)));
  73. }
  74. static void unregister_dca_providers(void)
  75. {
  76. struct dca_provider *dca, *_dca;
  77. struct list_head unregistered_providers;
  78. struct dca_domain *domain;
  79. unsigned long flags;
  80. blocking_notifier_call_chain(&dca_provider_chain,
  81. DCA_PROVIDER_REMOVE, NULL);
  82. INIT_LIST_HEAD(&unregistered_providers);
  83. raw_spin_lock_irqsave(&dca_lock, flags);
  84. if (list_empty(&dca_domains)) {
  85. raw_spin_unlock_irqrestore(&dca_lock, flags);
  86. return;
  87. }
  88. /* at this point only one domain in the list is expected */
  89. domain = list_first_entry(&dca_domains, struct dca_domain, node);
  90. list_for_each_entry_safe(dca, _dca, &domain->dca_providers, node)
  91. list_move(&dca->node, &unregistered_providers);
  92. dca_free_domain(domain);
  93. raw_spin_unlock_irqrestore(&dca_lock, flags);
  94. list_for_each_entry_safe(dca, _dca, &unregistered_providers, node) {
  95. dca_sysfs_remove_provider(dca);
  96. list_del(&dca->node);
  97. }
  98. }
  99. static struct dca_domain *dca_find_domain(struct pci_bus *rc)
  100. {
  101. struct dca_domain *domain;
  102. list_for_each_entry(domain, &dca_domains, node)
  103. if (domain->pci_rc == rc)
  104. return domain;
  105. return NULL;
  106. }
  107. static struct dca_domain *dca_get_domain(struct device *dev)
  108. {
  109. struct pci_bus *rc;
  110. struct dca_domain *domain;
  111. rc = dca_pci_rc_from_dev(dev);
  112. domain = dca_find_domain(rc);
  113. if (!domain) {
  114. if (dca_provider_ioat_ver_3_0(dev) && !list_empty(&dca_domains))
  115. dca_providers_blocked = 1;
  116. }
  117. return domain;
  118. }
  119. static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
  120. {
  121. struct dca_provider *dca;
  122. struct pci_bus *rc;
  123. struct dca_domain *domain;
  124. if (dev) {
  125. rc = dca_pci_rc_from_dev(dev);
  126. domain = dca_find_domain(rc);
  127. if (!domain)
  128. return NULL;
  129. } else {
  130. if (!list_empty(&dca_domains))
  131. domain = list_first_entry(&dca_domains,
  132. struct dca_domain,
  133. node);
  134. else
  135. return NULL;
  136. }
  137. list_for_each_entry(dca, &domain->dca_providers, node)
  138. if ((!dev) || (dca->ops->dev_managed(dca, dev)))
  139. return dca;
  140. return NULL;
  141. }
  142. /**
  143. * dca_add_requester - add a dca client to the list
  144. * @dev - the device that wants dca service
  145. */
  146. int dca_add_requester(struct device *dev)
  147. {
  148. struct dca_provider *dca;
  149. int err, slot = -ENODEV;
  150. unsigned long flags;
  151. struct pci_bus *pci_rc;
  152. struct dca_domain *domain;
  153. if (!dev)
  154. return -EFAULT;
  155. raw_spin_lock_irqsave(&dca_lock, flags);
  156. /* check if the requester has not been added already */
  157. dca = dca_find_provider_by_dev(dev);
  158. if (dca) {
  159. raw_spin_unlock_irqrestore(&dca_lock, flags);
  160. return -EEXIST;
  161. }
  162. pci_rc = dca_pci_rc_from_dev(dev);
  163. domain = dca_find_domain(pci_rc);
  164. if (!domain) {
  165. raw_spin_unlock_irqrestore(&dca_lock, flags);
  166. return -ENODEV;
  167. }
  168. list_for_each_entry(dca, &domain->dca_providers, node) {
  169. slot = dca->ops->add_requester(dca, dev);
  170. if (slot >= 0)
  171. break;
  172. }
  173. raw_spin_unlock_irqrestore(&dca_lock, flags);
  174. if (slot < 0)
  175. return slot;
  176. err = dca_sysfs_add_req(dca, dev, slot);
  177. if (err) {
  178. raw_spin_lock_irqsave(&dca_lock, flags);
  179. if (dca == dca_find_provider_by_dev(dev))
  180. dca->ops->remove_requester(dca, dev);
  181. raw_spin_unlock_irqrestore(&dca_lock, flags);
  182. return err;
  183. }
  184. return 0;
  185. }
  186. EXPORT_SYMBOL_GPL(dca_add_requester);
  187. /**
  188. * dca_remove_requester - remove a dca client from the list
  189. * @dev - the device that wants dca service
  190. */
  191. int dca_remove_requester(struct device *dev)
  192. {
  193. struct dca_provider *dca;
  194. int slot;
  195. unsigned long flags;
  196. if (!dev)
  197. return -EFAULT;
  198. raw_spin_lock_irqsave(&dca_lock, flags);
  199. dca = dca_find_provider_by_dev(dev);
  200. if (!dca) {
  201. raw_spin_unlock_irqrestore(&dca_lock, flags);
  202. return -ENODEV;
  203. }
  204. slot = dca->ops->remove_requester(dca, dev);
  205. raw_spin_unlock_irqrestore(&dca_lock, flags);
  206. if (slot < 0)
  207. return slot;
  208. dca_sysfs_remove_req(dca, slot);
  209. return 0;
  210. }
  211. EXPORT_SYMBOL_GPL(dca_remove_requester);
  212. /**
  213. * dca_common_get_tag - return the dca tag (serves both new and old api)
  214. * @dev - the device that wants dca service
  215. * @cpu - the cpuid as returned by get_cpu()
  216. */
  217. static u8 dca_common_get_tag(struct device *dev, int cpu)
  218. {
  219. struct dca_provider *dca;
  220. u8 tag;
  221. unsigned long flags;
  222. raw_spin_lock_irqsave(&dca_lock, flags);
  223. dca = dca_find_provider_by_dev(dev);
  224. if (!dca) {
  225. raw_spin_unlock_irqrestore(&dca_lock, flags);
  226. return -ENODEV;
  227. }
  228. tag = dca->ops->get_tag(dca, dev, cpu);
  229. raw_spin_unlock_irqrestore(&dca_lock, flags);
  230. return tag;
  231. }
  232. /**
  233. * dca3_get_tag - return the dca tag to the requester device
  234. * for the given cpu (new api)
  235. * @dev - the device that wants dca service
  236. * @cpu - the cpuid as returned by get_cpu()
  237. */
  238. u8 dca3_get_tag(struct device *dev, int cpu)
  239. {
  240. if (!dev)
  241. return -EFAULT;
  242. return dca_common_get_tag(dev, cpu);
  243. }
  244. EXPORT_SYMBOL_GPL(dca3_get_tag);
  245. /**
  246. * dca_get_tag - return the dca tag for the given cpu (old api)
  247. * @cpu - the cpuid as returned by get_cpu()
  248. */
  249. u8 dca_get_tag(int cpu)
  250. {
  251. struct device *dev = NULL;
  252. return dca_common_get_tag(dev, cpu);
  253. }
  254. EXPORT_SYMBOL_GPL(dca_get_tag);
  255. /**
  256. * alloc_dca_provider - get data struct for describing a dca provider
  257. * @ops - pointer to struct of dca operation function pointers
  258. * @priv_size - size of extra mem to be added for provider's needs
  259. */
  260. struct dca_provider *alloc_dca_provider(const struct dca_ops *ops,
  261. int priv_size)
  262. {
  263. struct dca_provider *dca;
  264. int alloc_size;
  265. alloc_size = (sizeof(*dca) + priv_size);
  266. dca = kzalloc(alloc_size, GFP_KERNEL);
  267. if (!dca)
  268. return NULL;
  269. dca->ops = ops;
  270. return dca;
  271. }
  272. EXPORT_SYMBOL_GPL(alloc_dca_provider);
  273. /**
  274. * free_dca_provider - release the dca provider data struct
  275. * @ops - pointer to struct of dca operation function pointers
  276. * @priv_size - size of extra mem to be added for provider's needs
  277. */
  278. void free_dca_provider(struct dca_provider *dca)
  279. {
  280. kfree(dca);
  281. }
  282. EXPORT_SYMBOL_GPL(free_dca_provider);
  283. /**
  284. * register_dca_provider - register a dca provider
  285. * @dca - struct created by alloc_dca_provider()
  286. * @dev - device providing dca services
  287. */
  288. int register_dca_provider(struct dca_provider *dca, struct device *dev)
  289. {
  290. int err;
  291. unsigned long flags;
  292. struct dca_domain *domain, *newdomain = NULL;
  293. raw_spin_lock_irqsave(&dca_lock, flags);
  294. if (dca_providers_blocked) {
  295. raw_spin_unlock_irqrestore(&dca_lock, flags);
  296. return -ENODEV;
  297. }
  298. raw_spin_unlock_irqrestore(&dca_lock, flags);
  299. err = dca_sysfs_add_provider(dca, dev);
  300. if (err)
  301. return err;
  302. raw_spin_lock_irqsave(&dca_lock, flags);
  303. domain = dca_get_domain(dev);
  304. if (!domain) {
  305. struct pci_bus *rc;
  306. if (dca_providers_blocked) {
  307. raw_spin_unlock_irqrestore(&dca_lock, flags);
  308. dca_sysfs_remove_provider(dca);
  309. unregister_dca_providers();
  310. return -ENODEV;
  311. }
  312. raw_spin_unlock_irqrestore(&dca_lock, flags);
  313. rc = dca_pci_rc_from_dev(dev);
  314. newdomain = dca_allocate_domain(rc);
  315. if (!newdomain)
  316. return -ENODEV;
  317. raw_spin_lock_irqsave(&dca_lock, flags);
  318. /* Recheck, we might have raced after dropping the lock */
  319. domain = dca_get_domain(dev);
  320. if (!domain) {
  321. domain = newdomain;
  322. newdomain = NULL;
  323. list_add(&domain->node, &dca_domains);
  324. }
  325. }
  326. list_add(&dca->node, &domain->dca_providers);
  327. raw_spin_unlock_irqrestore(&dca_lock, flags);
  328. blocking_notifier_call_chain(&dca_provider_chain,
  329. DCA_PROVIDER_ADD, NULL);
  330. kfree(newdomain);
  331. return 0;
  332. }
  333. EXPORT_SYMBOL_GPL(register_dca_provider);
  334. /**
  335. * unregister_dca_provider - remove a dca provider
  336. * @dca - struct created by alloc_dca_provider()
  337. */
  338. void unregister_dca_provider(struct dca_provider *dca, struct device *dev)
  339. {
  340. unsigned long flags;
  341. struct pci_bus *pci_rc;
  342. struct dca_domain *domain;
  343. blocking_notifier_call_chain(&dca_provider_chain,
  344. DCA_PROVIDER_REMOVE, NULL);
  345. raw_spin_lock_irqsave(&dca_lock, flags);
  346. if (list_empty(&dca_domains)) {
  347. raw_spin_unlock_irqrestore(&dca_lock, flags);
  348. return;
  349. }
  350. list_del(&dca->node);
  351. pci_rc = dca_pci_rc_from_dev(dev);
  352. domain = dca_find_domain(pci_rc);
  353. if (list_empty(&domain->dca_providers))
  354. dca_free_domain(domain);
  355. raw_spin_unlock_irqrestore(&dca_lock, flags);
  356. dca_sysfs_remove_provider(dca);
  357. }
  358. EXPORT_SYMBOL_GPL(unregister_dca_provider);
  359. /**
  360. * dca_register_notify - register a client's notifier callback
  361. */
  362. void dca_register_notify(struct notifier_block *nb)
  363. {
  364. blocking_notifier_chain_register(&dca_provider_chain, nb);
  365. }
  366. EXPORT_SYMBOL_GPL(dca_register_notify);
  367. /**
  368. * dca_unregister_notify - remove a client's notifier callback
  369. */
  370. void dca_unregister_notify(struct notifier_block *nb)
  371. {
  372. blocking_notifier_chain_unregister(&dca_provider_chain, nb);
  373. }
  374. EXPORT_SYMBOL_GPL(dca_unregister_notify);
  375. static int __init dca_init(void)
  376. {
  377. pr_info("dca service started, version %s\n", DCA_VERSION);
  378. return dca_sysfs_init();
  379. }
  380. static void __exit dca_exit(void)
  381. {
  382. dca_sysfs_exit();
  383. }
  384. arch_initcall(dca_init);
  385. module_exit(dca_exit);