Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 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 | |
| 27 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 29 | #include <linux/completion.h> |
| 30 | #include <linux/spinlock.h> |
Paul Gortmaker | 7dfe293 | 2011-05-27 13:23:32 -0400 | [diff] [blame] | 31 | #include <linux/module.h> |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 32 | #include <linux/workqueue.h> |
Jon Loeliger | eb8f276 | 2007-11-14 04:13:09 +1100 | [diff] [blame] | 33 | #include <linux/of_device.h> |
| 34 | #include <linux/of_platform.h> |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 35 | |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 36 | #include <asm/io.h> |
| 37 | #include <asm/pmi.h> |
Christian Krafft | 6bf05fd | 2007-04-23 21:35:45 +0200 | [diff] [blame] | 38 | #include <asm/prom.h> |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 39 | |
| 40 | struct pmi_data { |
| 41 | struct list_head handler; |
| 42 | spinlock_t handler_spinlock; |
| 43 | spinlock_t pmi_spinlock; |
| 44 | struct mutex msg_mutex; |
| 45 | pmi_message_t msg; |
| 46 | struct completion *completion; |
Grant Likely | a454dc5 | 2010-07-22 15:52:34 -0600 | [diff] [blame] | 47 | struct platform_device *dev; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 48 | int irq; |
| 49 | u8 __iomem *pmi_reg; |
| 50 | struct work_struct work; |
| 51 | }; |
| 52 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 53 | static struct pmi_data *data; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 54 | |
Stephen Rothwell | fb24744 | 2009-03-18 17:08:52 +0000 | [diff] [blame] | 55 | static irqreturn_t pmi_irq_handler(int irq, void *dev_id) |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 56 | { |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 57 | u8 type; |
| 58 | int rc; |
| 59 | |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 60 | spin_lock(&data->pmi_spinlock); |
| 61 | |
| 62 | type = ioread8(data->pmi_reg + PMI_READ_TYPE); |
| 63 | pr_debug("pmi: got message of type %d\n", type); |
| 64 | |
| 65 | if (type & PMI_ACK && !data->completion) { |
| 66 | printk(KERN_WARNING "pmi: got unexpected ACK message.\n"); |
| 67 | rc = -EIO; |
| 68 | goto unlock; |
| 69 | } |
| 70 | |
| 71 | if (data->completion && !(type & PMI_ACK)) { |
| 72 | printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type); |
| 73 | rc = -EIO; |
| 74 | goto unlock; |
| 75 | } |
| 76 | |
| 77 | data->msg.type = type; |
| 78 | data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0); |
| 79 | data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1); |
| 80 | data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2); |
| 81 | rc = 0; |
| 82 | unlock: |
| 83 | spin_unlock(&data->pmi_spinlock); |
| 84 | |
| 85 | if (rc == -EIO) { |
| 86 | rc = IRQ_HANDLED; |
| 87 | goto out; |
| 88 | } |
| 89 | |
| 90 | if (data->msg.type & PMI_ACK) { |
| 91 | complete(data->completion); |
| 92 | rc = IRQ_HANDLED; |
| 93 | goto out; |
| 94 | } |
| 95 | |
| 96 | schedule_work(&data->work); |
| 97 | |
| 98 | rc = IRQ_HANDLED; |
| 99 | out: |
| 100 | return rc; |
| 101 | } |
| 102 | |
| 103 | |
Uwe Kleine-König | ce6d73c | 2014-09-10 21:56:38 +0200 | [diff] [blame] | 104 | static const struct of_device_id pmi_match[] = { |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 105 | { .type = "ibm,pmi", .name = "ibm,pmi" }, |
Christian Krafft | 4a065f9 | 2007-04-23 21:35:44 +0200 | [diff] [blame] | 106 | { .type = "ibm,pmi" }, |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 107 | {}, |
| 108 | }; |
| 109 | |
| 110 | MODULE_DEVICE_TABLE(of, pmi_match); |
| 111 | |
| 112 | static void pmi_notify_handlers(struct work_struct *work) |
| 113 | { |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 114 | struct pmi_handler *handler; |
| 115 | |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 116 | spin_lock(&data->handler_spinlock); |
| 117 | list_for_each_entry(handler, &data->handler, node) { |
Joe Perches | 689fd14 | 2010-09-11 19:10:53 +0000 | [diff] [blame] | 118 | pr_debug("pmi: notifying handler %p\n", handler); |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 119 | if (handler->type == data->msg.type) |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 120 | handler->handle_pmi_message(data->msg); |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 121 | } |
| 122 | spin_unlock(&data->handler_spinlock); |
| 123 | } |
| 124 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 125 | static int pmi_of_probe(struct platform_device *dev) |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 126 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 127 | struct device_node *np = dev->dev.of_node; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 128 | int rc; |
| 129 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 130 | if (data) { |
| 131 | printk(KERN_ERR "pmi: driver has already been initialized.\n"); |
| 132 | rc = -EBUSY; |
| 133 | goto out; |
| 134 | } |
| 135 | |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 136 | data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL); |
| 137 | if (!data) { |
| 138 | printk(KERN_ERR "pmi: could not allocate memory.\n"); |
| 139 | rc = -ENOMEM; |
| 140 | goto out; |
| 141 | } |
| 142 | |
Christian Krafft | 6bf05fd | 2007-04-23 21:35:45 +0200 | [diff] [blame] | 143 | data->pmi_reg = of_iomap(np, 0); |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 144 | if (!data->pmi_reg) { |
| 145 | printk(KERN_ERR "pmi: invalid register address.\n"); |
| 146 | rc = -EFAULT; |
| 147 | goto error_cleanup_data; |
| 148 | } |
| 149 | |
| 150 | INIT_LIST_HEAD(&data->handler); |
| 151 | |
| 152 | mutex_init(&data->msg_mutex); |
| 153 | spin_lock_init(&data->pmi_spinlock); |
| 154 | spin_lock_init(&data->handler_spinlock); |
| 155 | |
| 156 | INIT_WORK(&data->work, pmi_notify_handlers); |
| 157 | |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 158 | data->dev = dev; |
| 159 | |
| 160 | data->irq = irq_of_parse_and_map(np, 0); |
| 161 | if (data->irq == NO_IRQ) { |
| 162 | printk(KERN_ERR "pmi: invalid interrupt.\n"); |
| 163 | rc = -EFAULT; |
| 164 | goto error_cleanup_iomap; |
| 165 | } |
| 166 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 167 | rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL); |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 168 | if (rc) { |
| 169 | printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n", |
| 170 | data->irq, rc); |
| 171 | goto error_cleanup_iomap; |
| 172 | } |
| 173 | |
| 174 | printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg); |
| 175 | |
| 176 | goto out; |
| 177 | |
| 178 | error_cleanup_iomap: |
| 179 | iounmap(data->pmi_reg); |
| 180 | |
| 181 | error_cleanup_data: |
| 182 | kfree(data); |
| 183 | |
| 184 | out: |
| 185 | return rc; |
| 186 | } |
| 187 | |
Grant Likely | a454dc5 | 2010-07-22 15:52:34 -0600 | [diff] [blame] | 188 | static int pmi_of_remove(struct platform_device *dev) |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 189 | { |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 190 | struct pmi_handler *handler, *tmp; |
| 191 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 192 | free_irq(data->irq, NULL); |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 193 | iounmap(data->pmi_reg); |
| 194 | |
| 195 | spin_lock(&data->handler_spinlock); |
| 196 | |
| 197 | list_for_each_entry_safe(handler, tmp, &data->handler, node) |
| 198 | list_del(&handler->node); |
| 199 | |
| 200 | spin_unlock(&data->handler_spinlock); |
| 201 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 202 | kfree(data); |
| 203 | data = NULL; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
Grant Likely | 0000612 | 2011-02-22 19:59:54 -0700 | [diff] [blame] | 208 | static struct platform_driver pmi_of_platform_driver = { |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 209 | .probe = pmi_of_probe, |
Stephen Rothwell | 84dd467 | 2007-10-11 15:19:03 +1000 | [diff] [blame] | 210 | .remove = pmi_of_remove, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 211 | .driver = { |
| 212 | .name = "pmi", |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 213 | .of_match_table = pmi_match, |
Stephen Rothwell | 84dd467 | 2007-10-11 15:19:03 +1000 | [diff] [blame] | 214 | }, |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 215 | }; |
Srinivas Kandagatla | 40c935a | 2012-10-10 08:32:30 +0000 | [diff] [blame] | 216 | module_platform_driver(pmi_of_platform_driver); |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 217 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 218 | int pmi_send_message(pmi_message_t msg) |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 219 | { |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 220 | unsigned long flags; |
| 221 | DECLARE_COMPLETION_ONSTACK(completion); |
| 222 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 223 | if (!data) |
| 224 | return -ENODEV; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 225 | |
| 226 | mutex_lock(&data->msg_mutex); |
| 227 | |
| 228 | data->msg = msg; |
| 229 | pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg); |
| 230 | |
| 231 | data->completion = &completion; |
| 232 | |
| 233 | spin_lock_irqsave(&data->pmi_spinlock, flags); |
| 234 | iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0); |
| 235 | iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1); |
| 236 | iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2); |
| 237 | iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE); |
| 238 | spin_unlock_irqrestore(&data->pmi_spinlock, flags); |
| 239 | |
| 240 | pr_debug("pmi_send_message: wait for completion\n"); |
| 241 | |
| 242 | wait_for_completion_interruptible_timeout(data->completion, |
| 243 | PMI_TIMEOUT); |
| 244 | |
| 245 | data->completion = NULL; |
| 246 | |
| 247 | mutex_unlock(&data->msg_mutex); |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 248 | |
| 249 | return 0; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 250 | } |
| 251 | EXPORT_SYMBOL_GPL(pmi_send_message); |
| 252 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 253 | int pmi_register_handler(struct pmi_handler *handler) |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 254 | { |
Christian Krafft | 79baf4a | 2007-04-23 21:35:43 +0200 | [diff] [blame] | 255 | if (!data) |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 256 | return -ENODEV; |
Christian Krafft | 79baf4a | 2007-04-23 21:35:43 +0200 | [diff] [blame] | 257 | |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 258 | spin_lock(&data->handler_spinlock); |
| 259 | list_add_tail(&handler->node, &data->handler); |
| 260 | spin_unlock(&data->handler_spinlock); |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 261 | |
| 262 | return 0; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 263 | } |
| 264 | EXPORT_SYMBOL_GPL(pmi_register_handler); |
| 265 | |
Christian Krafft | 813f907 | 2007-07-20 21:39:18 +0200 | [diff] [blame] | 266 | void pmi_unregister_handler(struct pmi_handler *handler) |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 267 | { |
Christian Krafft | 79baf4a | 2007-04-23 21:35:43 +0200 | [diff] [blame] | 268 | if (!data) |
| 269 | return; |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 270 | |
| 271 | pr_debug("pmi: unregistering handler %p\n", handler); |
| 272 | |
Christian Krafft | 0e82664 | 2007-02-14 14:09:45 +0100 | [diff] [blame] | 273 | spin_lock(&data->handler_spinlock); |
| 274 | list_del(&handler->node); |
| 275 | spin_unlock(&data->handler_spinlock); |
| 276 | } |
| 277 | EXPORT_SYMBOL_GPL(pmi_unregister_handler); |
| 278 | |
| 279 | MODULE_LICENSE("GPL"); |
| 280 | MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>"); |
| 281 | MODULE_DESCRIPTION("IBM Platform Management Interrupt driver"); |