dca-core.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. MODULE_LICENSE("GPL");
  29. /* For now we're assuming a single, global, DCA provider for the system. */
  30. static DEFINE_SPINLOCK(dca_lock);
  31. static struct dca_provider *global_dca = NULL;
  32. /**
  33. * dca_add_requester - add a dca client to the list
  34. * @dev - the device that wants dca service
  35. */
  36. int dca_add_requester(struct device *dev)
  37. {
  38. int err, slot;
  39. if (!global_dca)
  40. return -ENODEV;
  41. spin_lock(&dca_lock);
  42. slot = global_dca->ops->add_requester(global_dca, dev);
  43. spin_unlock(&dca_lock);
  44. if (slot < 0)
  45. return slot;
  46. err = dca_sysfs_add_req(global_dca, dev, slot);
  47. if (err) {
  48. spin_lock(&dca_lock);
  49. global_dca->ops->remove_requester(global_dca, dev);
  50. spin_unlock(&dca_lock);
  51. return err;
  52. }
  53. return 0;
  54. }
  55. EXPORT_SYMBOL_GPL(dca_add_requester);
  56. /**
  57. * dca_remove_requester - remove a dca client from the list
  58. * @dev - the device that wants dca service
  59. */
  60. int dca_remove_requester(struct device *dev)
  61. {
  62. int slot;
  63. if (!global_dca)
  64. return -ENODEV;
  65. spin_lock(&dca_lock);
  66. slot = global_dca->ops->remove_requester(global_dca, dev);
  67. spin_unlock(&dca_lock);
  68. if (slot < 0)
  69. return slot;
  70. dca_sysfs_remove_req(global_dca, slot);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(dca_remove_requester);
  74. /**
  75. * dca_get_tag - return the dca tag for the given cpu
  76. * @cpu - the cpuid as returned by get_cpu()
  77. */
  78. u8 dca_get_tag(int cpu)
  79. {
  80. if (!global_dca)
  81. return -ENODEV;
  82. return global_dca->ops->get_tag(global_dca, cpu);
  83. }
  84. EXPORT_SYMBOL_GPL(dca_get_tag);
  85. /**
  86. * alloc_dca_provider - get data struct for describing a dca provider
  87. * @ops - pointer to struct of dca operation function pointers
  88. * @priv_size - size of extra mem to be added for provider's needs
  89. */
  90. struct dca_provider *alloc_dca_provider(struct dca_ops *ops, int priv_size)
  91. {
  92. struct dca_provider *dca;
  93. int alloc_size;
  94. alloc_size = (sizeof(*dca) + priv_size);
  95. dca = kzalloc(alloc_size, GFP_KERNEL);
  96. if (!dca)
  97. return NULL;
  98. dca->ops = ops;
  99. return dca;
  100. }
  101. EXPORT_SYMBOL_GPL(alloc_dca_provider);
  102. /**
  103. * free_dca_provider - release the dca provider data struct
  104. * @ops - pointer to struct of dca operation function pointers
  105. * @priv_size - size of extra mem to be added for provider's needs
  106. */
  107. void free_dca_provider(struct dca_provider *dca)
  108. {
  109. kfree(dca);
  110. }
  111. EXPORT_SYMBOL_GPL(free_dca_provider);
  112. static BLOCKING_NOTIFIER_HEAD(dca_provider_chain);
  113. /**
  114. * register_dca_provider - register a dca provider
  115. * @dca - struct created by alloc_dca_provider()
  116. * @dev - device providing dca services
  117. */
  118. int register_dca_provider(struct dca_provider *dca, struct device *dev)
  119. {
  120. int err;
  121. if (global_dca)
  122. return -EEXIST;
  123. err = dca_sysfs_add_provider(dca, dev);
  124. if (err)
  125. return err;
  126. global_dca = dca;
  127. blocking_notifier_call_chain(&dca_provider_chain,
  128. DCA_PROVIDER_ADD, NULL);
  129. return 0;
  130. }
  131. EXPORT_SYMBOL_GPL(register_dca_provider);
  132. /**
  133. * unregister_dca_provider - remove a dca provider
  134. * @dca - struct created by alloc_dca_provider()
  135. */
  136. void unregister_dca_provider(struct dca_provider *dca)
  137. {
  138. if (!global_dca)
  139. return;
  140. blocking_notifier_call_chain(&dca_provider_chain,
  141. DCA_PROVIDER_REMOVE, NULL);
  142. global_dca = NULL;
  143. dca_sysfs_remove_provider(dca);
  144. }
  145. EXPORT_SYMBOL_GPL(unregister_dca_provider);
  146. /**
  147. * dca_register_notify - register a client's notifier callback
  148. */
  149. void dca_register_notify(struct notifier_block *nb)
  150. {
  151. blocking_notifier_chain_register(&dca_provider_chain, nb);
  152. }
  153. EXPORT_SYMBOL_GPL(dca_register_notify);
  154. /**
  155. * dca_unregister_notify - remove a client's notifier callback
  156. */
  157. void dca_unregister_notify(struct notifier_block *nb)
  158. {
  159. blocking_notifier_chain_unregister(&dca_provider_chain, nb);
  160. }
  161. EXPORT_SYMBOL_GPL(dca_unregister_notify);
  162. static int __init dca_init(void)
  163. {
  164. return dca_sysfs_init();
  165. }
  166. static void __exit dca_exit(void)
  167. {
  168. dca_sysfs_exit();
  169. }
  170. module_init(dca_init);
  171. module_exit(dca_exit);