pmi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <asm/of_device.h>
  31. #include <asm/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 int pmi_irq_handler(int irq, void *dev_id)
  48. {
  49. struct pmi_data *data;
  50. u8 type;
  51. int rc;
  52. data = dev_id;
  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_data *data;
  96. struct pmi_handler *handler;
  97. data = container_of(work, struct pmi_data, work);
  98. spin_lock(&data->handler_spinlock);
  99. list_for_each_entry(handler, &data->handler, node) {
  100. pr_debug(KERN_INFO "pmi: notifying handler %p\n", handler);
  101. if (handler->type == data->msg.type)
  102. handler->handle_pmi_message(data->dev, data->msg);
  103. }
  104. spin_unlock(&data->handler_spinlock);
  105. }
  106. static int pmi_of_probe(struct of_device *dev,
  107. const struct of_device_id *match)
  108. {
  109. struct device_node *np = dev->node;
  110. struct pmi_data *data;
  111. int rc;
  112. data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL);
  113. if (!data) {
  114. printk(KERN_ERR "pmi: could not allocate memory.\n");
  115. rc = -ENOMEM;
  116. goto out;
  117. }
  118. data->pmi_reg = of_iomap(np, 0);
  119. if (!data->pmi_reg) {
  120. printk(KERN_ERR "pmi: invalid register address.\n");
  121. rc = -EFAULT;
  122. goto error_cleanup_data;
  123. }
  124. INIT_LIST_HEAD(&data->handler);
  125. mutex_init(&data->msg_mutex);
  126. spin_lock_init(&data->pmi_spinlock);
  127. spin_lock_init(&data->handler_spinlock);
  128. INIT_WORK(&data->work, pmi_notify_handlers);
  129. dev->dev.driver_data = data;
  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", data);
  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_data *data;
  155. struct pmi_handler *handler, *tmp;
  156. data = dev->dev.driver_data;
  157. free_irq(data->irq, data);
  158. iounmap(data->pmi_reg);
  159. spin_lock(&data->handler_spinlock);
  160. list_for_each_entry_safe(handler, tmp, &data->handler, node)
  161. list_del(&handler->node);
  162. spin_unlock(&data->handler_spinlock);
  163. kfree(dev->dev.driver_data);
  164. return 0;
  165. }
  166. static struct of_platform_driver pmi_of_platform_driver = {
  167. .name = "pmi",
  168. .match_table = pmi_match,
  169. .probe = pmi_of_probe,
  170. .remove = pmi_of_remove
  171. };
  172. static int __init pmi_module_init(void)
  173. {
  174. return of_register_platform_driver(&pmi_of_platform_driver);
  175. }
  176. module_init(pmi_module_init);
  177. static void __exit pmi_module_exit(void)
  178. {
  179. of_unregister_platform_driver(&pmi_of_platform_driver);
  180. }
  181. module_exit(pmi_module_exit);
  182. void pmi_send_message(struct of_device *device, pmi_message_t msg)
  183. {
  184. struct pmi_data *data;
  185. unsigned long flags;
  186. DECLARE_COMPLETION_ONSTACK(completion);
  187. data = device->dev.driver_data;
  188. mutex_lock(&data->msg_mutex);
  189. data->msg = msg;
  190. pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
  191. data->completion = &completion;
  192. spin_lock_irqsave(&data->pmi_spinlock, flags);
  193. iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
  194. iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
  195. iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
  196. iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
  197. spin_unlock_irqrestore(&data->pmi_spinlock, flags);
  198. pr_debug("pmi_send_message: wait for completion\n");
  199. wait_for_completion_interruptible_timeout(data->completion,
  200. PMI_TIMEOUT);
  201. data->completion = NULL;
  202. mutex_unlock(&data->msg_mutex);
  203. }
  204. EXPORT_SYMBOL_GPL(pmi_send_message);
  205. void pmi_register_handler(struct of_device *device,
  206. struct pmi_handler *handler)
  207. {
  208. struct pmi_data *data;
  209. data = device->dev.driver_data;
  210. if (!data)
  211. return;
  212. spin_lock(&data->handler_spinlock);
  213. list_add_tail(&handler->node, &data->handler);
  214. spin_unlock(&data->handler_spinlock);
  215. }
  216. EXPORT_SYMBOL_GPL(pmi_register_handler);
  217. void pmi_unregister_handler(struct of_device *device,
  218. struct pmi_handler *handler)
  219. {
  220. struct pmi_data *data;
  221. data = device->dev.driver_data;
  222. if (!data)
  223. return;
  224. pr_debug("pmi: unregistering handler %p\n", handler);
  225. spin_lock(&data->handler_spinlock);
  226. list_del(&handler->node);
  227. spin_unlock(&data->handler_spinlock);
  228. }
  229. EXPORT_SYMBOL_GPL(pmi_unregister_handler);
  230. MODULE_LICENSE("GPL");
  231. MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
  232. MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");