dca-core.c 6.2 KB

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