pmi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * pmi driver
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * PMI (Platform Management Interrupt) is a way to communicate
  7. * with the BMC (Baseboard Management Controller) via interrupts.
  8. * Unlike IPMI it is bidirectional and has a low latency.
  9. *
  10. * Author: Christian Krafft <krafft@de.ibm.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/interrupt.h>
  27. #include <linux/completion.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/workqueue.h>
  30. #include <linux/of_device.h>
  31. #include <linux/of_platform.h>
  32. #include <asm/io.h>
  33. #include <asm/pmi.h>
  34. #include <asm/prom.h>
  35. struct pmi_data {
  36. struct list_head handler;
  37. spinlock_t handler_spinlock;
  38. spinlock_t pmi_spinlock;
  39. struct mutex msg_mutex;
  40. pmi_message_t msg;
  41. struct completion *completion;
  42. struct of_device *dev;
  43. int irq;
  44. u8 __iomem *pmi_reg;
  45. struct work_struct work;
  46. };
  47. static struct pmi_data *data;
  48. static irqreturn_t pmi_irq_handler(int irq, void *dev_id)
  49. {
  50. u8 type;
  51. int rc;
  52. spin_lock(&data->pmi_spinlock);
  53. type = ioread8(data->pmi_reg + PMI_READ_TYPE);
  54. pr_debug("pmi: got message of type %d\n", type);
  55. if (type & PMI_ACK && !data->completion) {
  56. printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
  57. rc = -EIO;
  58. goto unlock;
  59. }
  60. if (data->completion && !(type & PMI_ACK)) {
  61. printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
  62. rc = -EIO;
  63. goto unlock;
  64. }
  65. data->msg.type = type;
  66. data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0);
  67. data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1);
  68. data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2);
  69. rc = 0;
  70. unlock:
  71. spin_unlock(&data->pmi_spinlock);
  72. if (rc == -EIO) {
  73. rc = IRQ_HANDLED;
  74. goto out;
  75. }
  76. if (data->msg.type & PMI_ACK) {
  77. complete(data->completion);
  78. rc = IRQ_HANDLED;
  79. goto out;
  80. }
  81. schedule_work(&data->work);
  82. rc = IRQ_HANDLED;
  83. out:
  84. return rc;
  85. }
  86. static struct of_device_id pmi_match[] = {
  87. { .type = "ibm,pmi", .name = "ibm,pmi" },
  88. { .type = "ibm,pmi" },
  89. {},
  90. };
  91. MODULE_DEVICE_TABLE(of, pmi_match);
  92. static void pmi_notify_handlers(struct work_struct *work)
  93. {
  94. struct pmi_handler *handler;
  95. spin_lock(&data->handler_spinlock);
  96. list_for_each_entry(handler, &data->handler, node) {
  97. pr_debug(KERN_INFO "pmi: notifying handler %p\n", handler);
  98. if (handler->type == data->msg.type)
  99. handler->handle_pmi_message(data->msg);
  100. }
  101. spin_unlock(&data->handler_spinlock);
  102. }
  103. static int pmi_of_probe(struct of_device *dev,
  104. const struct of_device_id *match)
  105. {
  106. struct device_node *np = dev->node;
  107. int rc;
  108. if (data) {
  109. printk(KERN_ERR "pmi: driver has already been initialized.\n");
  110. rc = -EBUSY;
  111. goto out;
  112. }
  113. data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL);
  114. if (!data) {
  115. printk(KERN_ERR "pmi: could not allocate memory.\n");
  116. rc = -ENOMEM;
  117. goto out;
  118. }
  119. data->pmi_reg = of_iomap(np, 0);
  120. if (!data->pmi_reg) {
  121. printk(KERN_ERR "pmi: invalid register address.\n");
  122. rc = -EFAULT;
  123. goto error_cleanup_data;
  124. }
  125. INIT_LIST_HEAD(&data->handler);
  126. mutex_init(&data->msg_mutex);
  127. spin_lock_init(&data->pmi_spinlock);
  128. spin_lock_init(&data->handler_spinlock);
  129. INIT_WORK(&data->work, pmi_notify_handlers);
  130. data->dev = dev;
  131. data->irq = irq_of_parse_and_map(np, 0);
  132. if (data->irq == NO_IRQ) {
  133. printk(KERN_ERR "pmi: invalid interrupt.\n");
  134. rc = -EFAULT;
  135. goto error_cleanup_iomap;
  136. }
  137. rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL);
  138. if (rc) {
  139. printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n",
  140. data->irq, rc);
  141. goto error_cleanup_iomap;
  142. }
  143. printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg);
  144. goto out;
  145. error_cleanup_iomap:
  146. iounmap(data->pmi_reg);
  147. error_cleanup_data:
  148. kfree(data);
  149. out:
  150. return rc;
  151. }
  152. static int pmi_of_remove(struct of_device *dev)
  153. {
  154. struct pmi_handler *handler, *tmp;
  155. free_irq(data->irq, NULL);
  156. iounmap(data->pmi_reg);
  157. spin_lock(&data->handler_spinlock);
  158. list_for_each_entry_safe(handler, tmp, &data->handler, node)
  159. list_del(&handler->node);
  160. spin_unlock(&data->handler_spinlock);
  161. kfree(data);
  162. data = NULL;
  163. return 0;
  164. }
  165. static struct of_platform_driver pmi_of_platform_driver = {
  166. .match_table = pmi_match,
  167. .probe = pmi_of_probe,
  168. .remove = pmi_of_remove,
  169. .driver = {
  170. .name = "pmi",
  171. },
  172. };
  173. static int __init pmi_module_init(void)
  174. {
  175. return of_register_platform_driver(&pmi_of_platform_driver);
  176. }
  177. module_init(pmi_module_init);
  178. static void __exit pmi_module_exit(void)
  179. {
  180. of_unregister_platform_driver(&pmi_of_platform_driver);
  181. }
  182. module_exit(pmi_module_exit);
  183. int pmi_send_message(pmi_message_t msg)
  184. {
  185. unsigned long flags;
  186. DECLARE_COMPLETION_ONSTACK(completion);
  187. if (!data)
  188. return -ENODEV;
  189. mutex_lock(&data->msg_mutex);
  190. data->msg = msg;
  191. pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
  192. data->completion = &completion;
  193. spin_lock_irqsave(&data->pmi_spinlock, flags);
  194. iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
  195. iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
  196. iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
  197. iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
  198. spin_unlock_irqrestore(&data->pmi_spinlock, flags);
  199. pr_debug("pmi_send_message: wait for completion\n");
  200. wait_for_completion_interruptible_timeout(data->completion,
  201. PMI_TIMEOUT);
  202. data->completion = NULL;
  203. mutex_unlock(&data->msg_mutex);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(pmi_send_message);
  207. int pmi_register_handler(struct pmi_handler *handler)
  208. {
  209. if (!data)
  210. return -ENODEV;
  211. spin_lock(&data->handler_spinlock);
  212. list_add_tail(&handler->node, &data->handler);
  213. spin_unlock(&data->handler_spinlock);
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(pmi_register_handler);
  217. void pmi_unregister_handler(struct pmi_handler *handler)
  218. {
  219. if (!data)
  220. return;
  221. pr_debug("pmi: unregistering handler %p\n", handler);
  222. spin_lock(&data->handler_spinlock);
  223. list_del(&handler->node);
  224. spin_unlock(&data->handler_spinlock);
  225. }
  226. EXPORT_SYMBOL_GPL(pmi_unregister_handler);
  227. MODULE_LICENSE("GPL");
  228. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
  229. MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");