Linux Audio

Check our new training course

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