pmi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/module.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/of_device.h>
  33. #include <linux/of_platform.h>
  34. #include <asm/io.h>
  35. #include <asm/pmi.h>
  36. #include <asm/prom.h>
  37. struct pmi_data {
  38. struct list_head handler;
  39. spinlock_t handler_spinlock;
  40. spinlock_t pmi_spinlock;
  41. struct mutex msg_mutex;
  42. pmi_message_t msg;
  43. struct completion *completion;
  44. struct platform_device *dev;
  45. int irq;
  46. u8 __iomem *pmi_reg;
  47. struct work_struct work;
  48. };
  49. static struct pmi_data *data;
  50. static irqreturn_t pmi_irq_handler(int irq, void *dev_id)
  51. {
  52. u8 type;
  53. int rc;
  54. spin_lock(&data->pmi_spinlock);
  55. type = ioread8(data->pmi_reg + PMI_READ_TYPE);
  56. pr_debug("pmi: got message of type %d\n", type);
  57. if (type & PMI_ACK && !data->completion) {
  58. printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
  59. rc = -EIO;
  60. goto unlock;
  61. }
  62. if (data->completion && !(type & PMI_ACK)) {
  63. printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
  64. rc = -EIO;
  65. goto unlock;
  66. }
  67. data->msg.type = type;
  68. data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0);
  69. data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1);
  70. data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2);
  71. rc = 0;
  72. unlock:
  73. spin_unlock(&data->pmi_spinlock);
  74. if (rc == -EIO) {
  75. rc = IRQ_HANDLED;
  76. goto out;
  77. }
  78. if (data->msg.type & PMI_ACK) {
  79. complete(data->completion);
  80. rc = IRQ_HANDLED;
  81. goto out;
  82. }
  83. schedule_work(&data->work);
  84. rc = IRQ_HANDLED;
  85. out:
  86. return rc;
  87. }
  88. static const struct of_device_id pmi_match[] = {
  89. { .type = "ibm,pmi", .name = "ibm,pmi" },
  90. { .type = "ibm,pmi" },
  91. {},
  92. };
  93. MODULE_DEVICE_TABLE(of, pmi_match);
  94. static void pmi_notify_handlers(struct work_struct *work)
  95. {
  96. struct pmi_handler *handler;
  97. spin_lock(&data->handler_spinlock);
  98. list_for_each_entry(handler, &data->handler, node) {
  99. pr_debug("pmi: notifying handler %p\n", handler);
  100. if (handler->type == data->msg.type)
  101. handler->handle_pmi_message(data->msg);
  102. }
  103. spin_unlock(&data->handler_spinlock);
  104. }
  105. static int pmi_of_probe(struct platform_device *dev)
  106. {
  107. struct device_node *np = dev->dev.of_node;
  108. int rc;
  109. if (data) {
  110. printk(KERN_ERR "pmi: driver has already been initialized.\n");
  111. rc = -EBUSY;
  112. goto out;
  113. }
  114. data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL);
  115. if (!data) {
  116. printk(KERN_ERR "pmi: could not allocate memory.\n");
  117. rc = -ENOMEM;
  118. goto out;
  119. }
  120. data->pmi_reg = of_iomap(np, 0);
  121. if (!data->pmi_reg) {
  122. printk(KERN_ERR "pmi: invalid register address.\n");
  123. rc = -EFAULT;
  124. goto error_cleanup_data;
  125. }
  126. INIT_LIST_HEAD(&data->handler);
  127. mutex_init(&data->msg_mutex);
  128. spin_lock_init(&data->pmi_spinlock);
  129. spin_lock_init(&data->handler_spinlock);
  130. INIT_WORK(&data->work, pmi_notify_handlers);
  131. data->dev = dev;
  132. data->irq = irq_of_parse_and_map(np, 0);
  133. if (!data->irq) {
  134. printk(KERN_ERR "pmi: invalid interrupt.\n");
  135. rc = -EFAULT;
  136. goto error_cleanup_iomap;
  137. }
  138. rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL);
  139. if (rc) {
  140. printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n",
  141. data->irq, rc);
  142. goto error_cleanup_iomap;
  143. }
  144. printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg);
  145. goto out;
  146. error_cleanup_iomap:
  147. iounmap(data->pmi_reg);
  148. error_cleanup_data:
  149. kfree(data);
  150. out:
  151. return rc;
  152. }
  153. static int pmi_of_remove(struct platform_device *dev)
  154. {
  155. struct pmi_handler *handler, *tmp;
  156. free_irq(data->irq, NULL);
  157. iounmap(data->pmi_reg);
  158. spin_lock(&data->handler_spinlock);
  159. list_for_each_entry_safe(handler, tmp, &data->handler, node)
  160. list_del(&handler->node);
  161. spin_unlock(&data->handler_spinlock);
  162. kfree(data);
  163. data = NULL;
  164. return 0;
  165. }
  166. static struct platform_driver pmi_of_platform_driver = {
  167. .probe = pmi_of_probe,
  168. .remove = pmi_of_remove,
  169. .driver = {
  170. .name = "pmi",
  171. .of_match_table = pmi_match,
  172. },
  173. };
  174. module_platform_driver(pmi_of_platform_driver);
  175. int pmi_send_message(pmi_message_t msg)
  176. {
  177. unsigned long flags;
  178. DECLARE_COMPLETION_ONSTACK(completion);
  179. if (!data)
  180. return -ENODEV;
  181. mutex_lock(&data->msg_mutex);
  182. data->msg = msg;
  183. pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
  184. data->completion = &completion;
  185. spin_lock_irqsave(&data->pmi_spinlock, flags);
  186. iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
  187. iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
  188. iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
  189. iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
  190. spin_unlock_irqrestore(&data->pmi_spinlock, flags);
  191. pr_debug("pmi_send_message: wait for completion\n");
  192. wait_for_completion_interruptible_timeout(data->completion,
  193. PMI_TIMEOUT);
  194. data->completion = NULL;
  195. mutex_unlock(&data->msg_mutex);
  196. return 0;
  197. }
  198. EXPORT_SYMBOL_GPL(pmi_send_message);
  199. int pmi_register_handler(struct pmi_handler *handler)
  200. {
  201. if (!data)
  202. return -ENODEV;
  203. spin_lock(&data->handler_spinlock);
  204. list_add_tail(&handler->node, &data->handler);
  205. spin_unlock(&data->handler_spinlock);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(pmi_register_handler);
  209. void pmi_unregister_handler(struct pmi_handler *handler)
  210. {
  211. if (!data)
  212. return;
  213. pr_debug("pmi: unregistering handler %p\n", handler);
  214. spin_lock(&data->handler_spinlock);
  215. list_del(&handler->node);
  216. spin_unlock(&data->handler_spinlock);
  217. }
  218. EXPORT_SYMBOL_GPL(pmi_unregister_handler);
  219. MODULE_LICENSE("GPL");
  220. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
  221. MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");