4 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
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.
10 * Author: Christian Krafft <krafft@de.ibm.com>
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)
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.
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.
27 #include <linux/interrupt.h>
28 #include <linux/slab.h>
29 #include <linux/completion.h>
30 #include <linux/spinlock.h>
31 #include <linux/module.h>
32 #include <linux/workqueue.h>
33 #include <linux/of_device.h>
34 #include <linux/of_platform.h>
41 struct list_head handler;
42 spinlock_t handler_spinlock;
43 spinlock_t pmi_spinlock;
44 struct mutex msg_mutex;
46 struct completion *completion;
47 struct platform_device *dev;
50 struct work_struct work;
53 static struct pmi_data *data;
55 static irqreturn_t pmi_irq_handler(int irq, void *dev_id)
60 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);
65 if (type & PMI_ACK && !data->completion) {
66 printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
71 if (data->completion && !(type & PMI_ACK)) {
72 printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
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);
83 spin_unlock(&data->pmi_spinlock);
90 if (data->msg.type & PMI_ACK) {
91 complete(data->completion);
96 schedule_work(&data->work);
104 static struct of_device_id pmi_match[] = {
105 { .type = "ibm,pmi", .name = "ibm,pmi" },
106 { .type = "ibm,pmi" },
110 MODULE_DEVICE_TABLE(of, pmi_match);
112 static void pmi_notify_handlers(struct work_struct *work)
114 struct pmi_handler *handler;
116 spin_lock(&data->handler_spinlock);
117 list_for_each_entry(handler, &data->handler, node) {
118 pr_debug("pmi: notifying handler %p\n", handler);
119 if (handler->type == data->msg.type)
120 handler->handle_pmi_message(data->msg);
122 spin_unlock(&data->handler_spinlock);
125 static int pmi_of_probe(struct platform_device *dev)
127 struct device_node *np = dev->dev.of_node;
131 printk(KERN_ERR "pmi: driver has already been initialized.\n");
136 data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL);
138 printk(KERN_ERR "pmi: could not allocate memory.\n");
143 data->pmi_reg = of_iomap(np, 0);
144 if (!data->pmi_reg) {
145 printk(KERN_ERR "pmi: invalid register address.\n");
147 goto error_cleanup_data;
150 INIT_LIST_HEAD(&data->handler);
152 mutex_init(&data->msg_mutex);
153 spin_lock_init(&data->pmi_spinlock);
154 spin_lock_init(&data->handler_spinlock);
156 INIT_WORK(&data->work, pmi_notify_handlers);
160 data->irq = irq_of_parse_and_map(np, 0);
161 if (data->irq == NO_IRQ) {
162 printk(KERN_ERR "pmi: invalid interrupt.\n");
164 goto error_cleanup_iomap;
167 rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL);
169 printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n",
171 goto error_cleanup_iomap;
174 printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg);
179 iounmap(data->pmi_reg);
188 static int pmi_of_remove(struct platform_device *dev)
190 struct pmi_handler *handler, *tmp;
192 free_irq(data->irq, NULL);
193 iounmap(data->pmi_reg);
195 spin_lock(&data->handler_spinlock);
197 list_for_each_entry_safe(handler, tmp, &data->handler, node)
198 list_del(&handler->node);
200 spin_unlock(&data->handler_spinlock);
208 static struct platform_driver pmi_of_platform_driver = {
209 .probe = pmi_of_probe,
210 .remove = pmi_of_remove,
213 .owner = THIS_MODULE,
214 .of_match_table = pmi_match,
217 module_platform_driver(pmi_of_platform_driver);
219 int pmi_send_message(pmi_message_t msg)
222 DECLARE_COMPLETION_ONSTACK(completion);
227 mutex_lock(&data->msg_mutex);
230 pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
232 data->completion = &completion;
234 spin_lock_irqsave(&data->pmi_spinlock, flags);
235 iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
236 iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
237 iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
238 iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
239 spin_unlock_irqrestore(&data->pmi_spinlock, flags);
241 pr_debug("pmi_send_message: wait for completion\n");
243 wait_for_completion_interruptible_timeout(data->completion,
246 data->completion = NULL;
248 mutex_unlock(&data->msg_mutex);
252 EXPORT_SYMBOL_GPL(pmi_send_message);
254 int pmi_register_handler(struct pmi_handler *handler)
259 spin_lock(&data->handler_spinlock);
260 list_add_tail(&handler->node, &data->handler);
261 spin_unlock(&data->handler_spinlock);
265 EXPORT_SYMBOL_GPL(pmi_register_handler);
267 void pmi_unregister_handler(struct pmi_handler *handler)
272 pr_debug("pmi: unregistering handler %p\n", handler);
274 spin_lock(&data->handler_spinlock);
275 list_del(&handler->node);
276 spin_unlock(&data->handler_spinlock);
278 EXPORT_SYMBOL_GPL(pmi_unregister_handler);
280 MODULE_LICENSE("GPL");
281 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
282 MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");