pmi.c 5.8 KB

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