dca-core.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #define DCA_VERSION "1.8"
  29. MODULE_VERSION(DCA_VERSION);
  30. MODULE_LICENSE("GPL");
  31. MODULE_AUTHOR("Intel Corporation");
  32. static DEFINE_SPINLOCK(dca_lock);
  33. static LIST_HEAD(dca_providers);
  34. static struct dca_provider *dca_find_provider_by_dev(struct device *dev)
  35. {
  36. struct dca_provider *dca, *ret = NULL;
  37. list_for_each_entry(dca, &dca_providers, node) {
  38. if ((!dev) || (dca->ops->dev_managed(dca, dev))) {
  39. ret = dca;
  40. break;
  41. }
  42. }
  43. return ret;
  44. }
  45. /**
  46. * dca_add_requester - add a dca client to the list
  47. * @dev - the device that wants dca service
  48. */
  49. int dca_add_requester(struct device *dev)
  50. {
  51. struct dca_provider *dca;
  52. int err, slot = -ENODEV;
  53. unsigned long flags;
  54. if (!dev)
  55. return -EFAULT;
  56. spin_lock_irqsave(&dca_lock, flags);
  57. /* check if the requester has not been added already */
  58. dca = dca_find_provider_by_dev(dev);
  59. if (dca) {
  60. spin_unlock_irqrestore(&dca_lock, flags);
  61. return -EEXIST;
  62. }
  63. list_for_each_entry(dca, &dca_providers, node) {
  64. slot = dca->ops->add_requester(dca, dev);
  65. if (slot >= 0)
  66. break;
  67. }
  68. spin_unlock_irqrestore(&dca_lock, flags);
  69. if (slot < 0)
  70. return slot;
  71. err = dca_sysfs_add_req(dca, dev, slot);
  72. if (err) {
  73. spin_lock_irqsave(&dca_lock, flags);
  74. if (dca == dca_find_provider_by_dev(dev))
  75. dca->ops->remove_requester(dca, dev);
  76. spin_unlock_irqrestore(&dca_lock, flags);
  77. return err;
  78. }
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(dca_add_requester);
  82. /**
  83. * dca_remove_requester - remove a dca client from the list
  84. * @dev - the device that wants dca service
  85. */
  86. int dca_remove_requester(struct device *dev)
  87. {
  88. struct dca_provider *dca;
  89. int slot;
  90. unsigned long flags;
  91. if (!dev)
  92. return -EFAULT;
  93. spin_lock_irqsave(&dca_lock, flags);
  94. dca = dca_find_provider_by_dev(dev);
  95. if (!dca) {
  96. spin_unlock_irqrestore(&dca_lock, flags);
  97. return -ENODEV;
  98. }
  99. slot = dca->ops->remove_requester(dca, dev);
  100. spin_unlock_irqrestore(&dca_lock, flags);
  101. if (slot < 0)
  102. return slot;
  103. dca_sysfs_remove_req(dca, slot);
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(dca_remove_requester);
  107. /**
  108. * dca_common_get_tag - return the dca tag (serves both new and old api)
  109. * @dev - the device that wants dca service
  110. * @cpu - the cpuid as returned by get_cpu()
  111. */
  112. u8 dca_common_get_tag(struct device *dev, int cpu)
  113. {
  114. struct dca_provider *dca;
  115. u8 tag;
  116. unsigned long flags;
  117. spin_lock_irqsave(&dca_lock, flags);
  118. dca = dca_find_provider_by_dev(dev);
  119. if (!dca) {
  120. spin_unlock_irqrestore(&dca_lock, flags);
  121. return -ENODEV;
  122. }
  123. tag = dca->ops->get_tag(dca, dev, cpu);
  124. spin_unlock_irqrestore(&dca_lock, flags);
  125. return tag;
  126. }
  127. /**
  128. * dca3_get_tag - return the dca tag to the requester device
  129. * for the given cpu (new api)
  130. * @dev - the device that wants dca service
  131. * @cpu - the cpuid as returned by get_cpu()
  132. */
  133. u8 dca3_get_tag(struct device *dev, int cpu)
  134. {
  135. if (!dev)
  136. return -EFAULT;
  137. return dca_common_get_tag(dev, cpu);
  138. }
  139. EXPORT_SYMBOL_GPL(dca3_get_tag);
  140. /**
  141. * dca_get_tag - return the dca tag for the given cpu (old api)
  142. * @cpu - the cpuid as returned by get_cpu()
  143. */
  144. u8 dca_get_tag(int cpu)
  145. {
  146. struct device *dev = NULL;
  147. return dca_common_get_tag(dev, cpu);
  148. }
  149. EXPORT_SYMBOL_GPL(dca_get_tag);
  150. /**
  151. * alloc_dca_provider - get data struct for describing a dca provider
  152. * @ops - pointer to struct of dca operation function pointers
  153. * @priv_size - size of extra mem to be added for provider's needs
  154. */
  155. struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
  156. {
  157. struct dca_provider *dca;
  158. int alloc_size;
  159. alloc_size = (sizeof(*dca) + priv_size);
  160. dca = kzalloc(alloc_size, GFP_KERNEL);
  161. if (!dca)
  162. return NULL;
  163. dca->ops = ops;
  164. return dca;
  165. }
  166. EXPORT_SYMBOL_GPL(alloc_dca_provider);
  167. /**
  168. * free_dca_provider - release the dca provider data struct
  169. * @ops - pointer to struct of dca operation function pointers
  170. * @priv_size - size of extra mem to be added for provider's needs
  171. */
  172. void free_dca_provider(struct dca_provider *dca)
  173. {
  174. kfree(dca);
  175. }
  176. EXPORT_SYMBOL_GPL(free_dca_provider);
  177. static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
  178. /**
  179. * register_dca_provider - register a dca provider
  180. * @dca - struct created by alloc_dca_provider()
  181. * @dev - device providing dca services
  182. */
  183. int register_dca_provider(struct dca_provider *dca, struct device *dev)
  184. {
  185. int err;
  186. unsigned long flags;
  187. err = dca_sysfs_add_provider(dca, dev);
  188. if (err)
  189. return err;
  190. spin_lock_irqsave(&dca_lock, flags);
  191. list_add(&dca->node, &dca_providers);
  192. spin_unlock_irqrestore(&dca_lock, flags);
  193. blocking_notifier_call_chain(&dca_provider_chain,
  194. DCA_PROVIDER_ADD, NULL);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL_GPL(register_dca_provider);
  198. /**
  199. * unregister_dca_provider - remove a dca provider
  200. * @dca - struct created by alloc_dca_provider()
  201. */
  202. void unregister_dca_provider(struct dca_provider *dca)
  203. {
  204. unsigned long flags;
  205. blocking_notifier_call_chain(&dca_provider_chain,
  206. DCA_PROVIDER_REMOVE, NULL);
  207. spin_lock_irqsave(&dca_lock, flags);
  208. list_del(&dca->node);
  209. spin_unlock_irqrestore(&dca_lock, flags);
  210. dca_sysfs_remove_provider(dca);
  211. }
  212. EXPORT_SYMBOL_GPL(unregister_dca_provider);
  213. /**
  214. * dca_register_notify - register a client's notifier callback
  215. */
  216. void dca_register_notify(struct notifier_block *nb)
  217. {
  218. blocking_notifier_chain_register(&dca_provider_chain, nb);
  219. }
  220. EXPORT_SYMBOL_GPL(dca_register_notify);
  221. /**
  222. * dca_unregister_notify - remove a client's notifier callback
  223. */
  224. void dca_unregister_notify(struct notifier_block *nb)
  225. {
  226. blocking_notifier_chain_unregister(&dca_provider_chain, nb);
  227. }
  228. EXPORT_SYMBOL_GPL(dca_unregister_notify);
  229. static int __init dca_init(void)
  230. {
  231. printk(KERN_ERR "dca service started, version %s\n", DCA_VERSION);
  232. return dca_sysfs_init();
  233. }
  234. static void __exit dca_exit(void)
  235. {
  236. dca_sysfs_exit();
  237. }
  238. arch_initcall(dca_init);
  239. module_exit(dca_exit);