Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * pmi driver
0004  *
0005  * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
0006  *
0007  * PMI (Platform Management Interrupt) is a way to communicate
0008  * with the BMC (Baseboard Management Controller) via interrupts.
0009  * Unlike IPMI it is bidirectional and has a low latency.
0010  *
0011  * Author: Christian Krafft <krafft@de.ibm.com>
0012  */
0013 
0014 #include <linux/interrupt.h>
0015 #include <linux/slab.h>
0016 #include <linux/completion.h>
0017 #include <linux/spinlock.h>
0018 #include <linux/module.h>
0019 #include <linux/workqueue.h>
0020 #include <linux/of_address.h>
0021 #include <linux/of_device.h>
0022 #include <linux/of_irq.h>
0023 #include <linux/of_platform.h>
0024 
0025 #include <asm/io.h>
0026 #include <asm/pmi.h>
0027 
0028 struct pmi_data {
0029     struct list_head    handler;
0030     spinlock_t      handler_spinlock;
0031     spinlock_t      pmi_spinlock;
0032     struct mutex        msg_mutex;
0033     pmi_message_t       msg;
0034     struct completion   *completion;
0035     struct platform_device  *dev;
0036     int         irq;
0037     u8 __iomem      *pmi_reg;
0038     struct work_struct  work;
0039 };
0040 
0041 static struct pmi_data *data;
0042 
0043 static irqreturn_t pmi_irq_handler(int irq, void *dev_id)
0044 {
0045     u8 type;
0046     int rc;
0047 
0048     spin_lock(&data->pmi_spinlock);
0049 
0050     type = ioread8(data->pmi_reg + PMI_READ_TYPE);
0051     pr_debug("pmi: got message of type %d\n", type);
0052 
0053     if (type & PMI_ACK && !data->completion) {
0054         printk(KERN_WARNING "pmi: got unexpected ACK message.\n");
0055         rc = -EIO;
0056         goto unlock;
0057     }
0058 
0059     if (data->completion && !(type & PMI_ACK)) {
0060         printk(KERN_WARNING "pmi: expected ACK, but got %d\n", type);
0061         rc = -EIO;
0062         goto unlock;
0063     }
0064 
0065     data->msg.type = type;
0066     data->msg.data0 = ioread8(data->pmi_reg + PMI_READ_DATA0);
0067     data->msg.data1 = ioread8(data->pmi_reg + PMI_READ_DATA1);
0068     data->msg.data2 = ioread8(data->pmi_reg + PMI_READ_DATA2);
0069     rc = 0;
0070 unlock:
0071     spin_unlock(&data->pmi_spinlock);
0072 
0073     if (rc == -EIO) {
0074         rc = IRQ_HANDLED;
0075         goto out;
0076     }
0077 
0078     if (data->msg.type & PMI_ACK) {
0079         complete(data->completion);
0080         rc = IRQ_HANDLED;
0081         goto out;
0082     }
0083 
0084     schedule_work(&data->work);
0085 
0086     rc = IRQ_HANDLED;
0087 out:
0088     return rc;
0089 }
0090 
0091 
0092 static const struct of_device_id pmi_match[] = {
0093     { .type = "ibm,pmi", .name = "ibm,pmi" },
0094     { .type = "ibm,pmi" },
0095     {},
0096 };
0097 
0098 MODULE_DEVICE_TABLE(of, pmi_match);
0099 
0100 static void pmi_notify_handlers(struct work_struct *work)
0101 {
0102     struct pmi_handler *handler;
0103 
0104     spin_lock(&data->handler_spinlock);
0105     list_for_each_entry(handler, &data->handler, node) {
0106         pr_debug("pmi: notifying handler %p\n", handler);
0107         if (handler->type == data->msg.type)
0108             handler->handle_pmi_message(data->msg);
0109     }
0110     spin_unlock(&data->handler_spinlock);
0111 }
0112 
0113 static int pmi_of_probe(struct platform_device *dev)
0114 {
0115     struct device_node *np = dev->dev.of_node;
0116     int rc;
0117 
0118     if (data) {
0119         printk(KERN_ERR "pmi: driver has already been initialized.\n");
0120         rc = -EBUSY;
0121         goto out;
0122     }
0123 
0124     data = kzalloc(sizeof(struct pmi_data), GFP_KERNEL);
0125     if (!data) {
0126         printk(KERN_ERR "pmi: could not allocate memory.\n");
0127         rc = -ENOMEM;
0128         goto out;
0129     }
0130 
0131     data->pmi_reg = of_iomap(np, 0);
0132     if (!data->pmi_reg) {
0133         printk(KERN_ERR "pmi: invalid register address.\n");
0134         rc = -EFAULT;
0135         goto error_cleanup_data;
0136     }
0137 
0138     INIT_LIST_HEAD(&data->handler);
0139 
0140     mutex_init(&data->msg_mutex);
0141     spin_lock_init(&data->pmi_spinlock);
0142     spin_lock_init(&data->handler_spinlock);
0143 
0144     INIT_WORK(&data->work, pmi_notify_handlers);
0145 
0146     data->dev = dev;
0147 
0148     data->irq = irq_of_parse_and_map(np, 0);
0149     if (!data->irq) {
0150         printk(KERN_ERR "pmi: invalid interrupt.\n");
0151         rc = -EFAULT;
0152         goto error_cleanup_iomap;
0153     }
0154 
0155     rc = request_irq(data->irq, pmi_irq_handler, 0, "pmi", NULL);
0156     if (rc) {
0157         printk(KERN_ERR "pmi: can't request IRQ %d: returned %d\n",
0158                 data->irq, rc);
0159         goto error_cleanup_iomap;
0160     }
0161 
0162     printk(KERN_INFO "pmi: found pmi device at addr %p.\n", data->pmi_reg);
0163 
0164     goto out;
0165 
0166 error_cleanup_iomap:
0167     iounmap(data->pmi_reg);
0168 
0169 error_cleanup_data:
0170     kfree(data);
0171 
0172 out:
0173     return rc;
0174 }
0175 
0176 static int pmi_of_remove(struct platform_device *dev)
0177 {
0178     struct pmi_handler *handler, *tmp;
0179 
0180     free_irq(data->irq, NULL);
0181     iounmap(data->pmi_reg);
0182 
0183     spin_lock(&data->handler_spinlock);
0184 
0185     list_for_each_entry_safe(handler, tmp, &data->handler, node)
0186         list_del(&handler->node);
0187 
0188     spin_unlock(&data->handler_spinlock);
0189 
0190     kfree(data);
0191     data = NULL;
0192 
0193     return 0;
0194 }
0195 
0196 static struct platform_driver pmi_of_platform_driver = {
0197     .probe      = pmi_of_probe,
0198     .remove     = pmi_of_remove,
0199     .driver = {
0200         .name = "pmi",
0201         .of_match_table = pmi_match,
0202     },
0203 };
0204 module_platform_driver(pmi_of_platform_driver);
0205 
0206 int pmi_send_message(pmi_message_t msg)
0207 {
0208     unsigned long flags;
0209     DECLARE_COMPLETION_ONSTACK(completion);
0210 
0211     if (!data)
0212         return -ENODEV;
0213 
0214     mutex_lock(&data->msg_mutex);
0215 
0216     data->msg = msg;
0217     pr_debug("pmi_send_message: msg is %08x\n", *(u32*)&msg);
0218 
0219     data->completion = &completion;
0220 
0221     spin_lock_irqsave(&data->pmi_spinlock, flags);
0222     iowrite8(msg.data0, data->pmi_reg + PMI_WRITE_DATA0);
0223     iowrite8(msg.data1, data->pmi_reg + PMI_WRITE_DATA1);
0224     iowrite8(msg.data2, data->pmi_reg + PMI_WRITE_DATA2);
0225     iowrite8(msg.type, data->pmi_reg + PMI_WRITE_TYPE);
0226     spin_unlock_irqrestore(&data->pmi_spinlock, flags);
0227 
0228     pr_debug("pmi_send_message: wait for completion\n");
0229 
0230     wait_for_completion_interruptible_timeout(data->completion,
0231                           PMI_TIMEOUT);
0232 
0233     data->completion = NULL;
0234 
0235     mutex_unlock(&data->msg_mutex);
0236 
0237     return 0;
0238 }
0239 EXPORT_SYMBOL_GPL(pmi_send_message);
0240 
0241 int pmi_register_handler(struct pmi_handler *handler)
0242 {
0243     if (!data)
0244         return -ENODEV;
0245 
0246     spin_lock(&data->handler_spinlock);
0247     list_add_tail(&handler->node, &data->handler);
0248     spin_unlock(&data->handler_spinlock);
0249 
0250     return 0;
0251 }
0252 EXPORT_SYMBOL_GPL(pmi_register_handler);
0253 
0254 void pmi_unregister_handler(struct pmi_handler *handler)
0255 {
0256     if (!data)
0257         return;
0258 
0259     pr_debug("pmi: unregistering handler %p\n", handler);
0260 
0261     spin_lock(&data->handler_spinlock);
0262     list_del(&handler->node);
0263     spin_unlock(&data->handler_spinlock);
0264 }
0265 EXPORT_SYMBOL_GPL(pmi_unregister_handler);
0266 
0267 MODULE_LICENSE("GPL");
0268 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
0269 MODULE_DESCRIPTION("IBM Platform Management Interrupt driver");