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