pmi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/slab.h>
  28. #include <linux/completion.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_platform.h>
  33. #include <asm/io.h>
  34. #include <asm/pmi.h>
  35. #include <asm/prom.h>
  36. struct pmi_data {
  37. struct list_head handler;
  38. spinlock_t handler_spinlock;
  39. spinlock_t pmi_spinlock;
  40. struct mutex msg_mutex;
  41. pmi_message_t msg;
  42. struct completion *completion;
  43. struct platform_device *dev;
  44. int irq;
  45. u8 __iomem *pmi_reg;
  46. struct work_struct work;
  47. };
  48. static struct pmi_data *data;
  49. static irqreturn_t pmi_irq_handler(int irq, void *dev_id)
  50. {
  51. u8 type;
  52. int rc;
  53. spin_lock(&data->pmi_spinlock);
  54. type = ioread8(data->pmi_reg + PMI_READ_TYPE);
  55. pr_debug("pmi: got message of type %d\n", type);
  56. if (type & PMI_ACK && !data->completion) {
  57. printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
  58. rc = -EIO;
  59. goto unlock;
  60. }
  61. if (data->completion && !(type & PMI_ACK)) {
  62. printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
  63. rc = -EIO;
  64. goto unlock;
  65. }
  66. data->msg.type = type;
  67. data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0);
  68. data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1);
  69. data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2);
  70. rc = 0;
  71. unlock:
  72. spin_unlock(&data->pmi_spinlock);
  73. if (rc == -EIO) {
  74. rc = IRQ_HANDLED;
  75. goto out;
  76. }
  77. if (data->msg.type & PMI_ACK) {
  78. complete(data->completion);
  79. rc = IRQ_HANDLED;
  80. goto out;
  81. }
  82. schedule_work(&data->work);
  83. rc = IRQ_HANDLED;
  84. out:
  85. return rc;
  86. }
  87. static struct of_device_id pmi_match[] = {
  88. { .type = "ibm,pmi", .name = "ibm,pmi" },
  89. { .type = "ibm,pmi" },
  90. {},
  91. };
  92. MODULE_DEVICE_TABLE(of, pmi_match);
  93. static void pmi_notify_handlers(struct work_struct *work)
  94. {
  95. struct pmi_handler *handler;
  96. spin_lock(&data->handler_spinlock);
  97. list_for_each_entry(handler, &data->handler, node) {
  98. pr_debug("pmi: notifying handler %p\n", handler);
  99. if (handler->type == data->msg.type)
  100. handler->handle_pmi_message(data->msg);
  101. }
  102. spin_unlock(&data->handler_spinlock);
  103. }
  104. static int pmi_of_probe(struct platform_device *dev)
  105. {
  106. struct device_node *np = dev->dev.of_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 platform_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 platform_driver pmi_of_platform_driver = {
  166. .probe = pmi_of_probe,
  167. .remove = pmi_of_remove,
  168. .driver = {
  169. .name = "pmi",
  170. .owner = THIS_MODULE,
  171. .of_match_table = pmi_match,
  172. },
  173. };
  174. static int __init pmi_module_init(void)
  175. {
  176. return platform_driver_register(&pmi_of_platform_driver);
  177. }
  178. module_init(pmi_module_init);
  179. static void __exit pmi_module_exit(void)
  180. {
  181. platform_driver_unregister(&pmi_of_platform_driver);
  182. }
  183. module_exit(pmi_module_exit);
  184. int pmi_send_message(pmi_message_t msg)
  185. {
  186. unsigned long flags;
  187. DECLARE_COMPLETION_ONSTACK(completion);
  188. if (!data)
  189. return -ENODEV;
  190. mutex_lock(&data->msg_mutex);
  191. data->msg = msg;
  192. pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
  193. data->completion = &completion;
  194. spin_lock_irqsave(&data->pmi_spinlock, flags);
  195. iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
  196. iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
  197. iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
  198. iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
  199. spin_unlock_irqrestore(&data->pmi_spinlock, flags);
  200. pr_debug("pmi_send_message: wait for completion\n");
  201. wait_for_completion_interruptible_timeout(data->completion,
  202. PMI_TIMEOUT);
  203. data->completion = NULL;
  204. mutex_unlock(&data->msg_mutex);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(pmi_send_message);
  208. int pmi_register_handler(struct pmi_handler *handler)
  209. {
  210. if (!data)
  211. return -ENODEV;
  212. spin_lock(&data->handler_spinlock);
  213. list_add_tail(&handler->node, &data->handler);
  214. spin_unlock(&data->handler_spinlock);
  215. return 0;
  216. }
  217. EXPORT_SYMBOL_GPL(pmi_register_handler);
  218. void pmi_unregister_handler(struct pmi_handler *handler)
  219. {
  220. if (!data)
  221. return;
  222. pr_debug("pmi: unregistering handler %p\n", handler);
  223. spin_lock(&data->handler_spinlock);
  224. list_del(&handler->node);
  225. spin_unlock(&data->handler_spinlock);
  226. }
  227. EXPORT_SYMBOL_GPL(pmi_unregister_handler);
  228. MODULE_LICENSE("GPL");
  229. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
  230. MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");