pmi.c 7.0 KB

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