Path: blob/master/arch/powerpc/platforms/powernv/opal-prd.c
26481 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* OPAL Runtime Diagnostics interface driver3* Supported on POWERNV platform4*5* Copyright IBM Corporation 20156*/78#define pr_fmt(fmt) "opal-prd: " fmt910#include <linux/kernel.h>11#include <linux/module.h>12#include <linux/platform_device.h>13#include <linux/miscdevice.h>14#include <linux/fs.h>15#include <linux/of.h>16#include <linux/of_address.h>17#include <linux/poll.h>18#include <linux/mm.h>19#include <linux/slab.h>20#include <asm/opal-prd.h>21#include <asm/opal.h>22#include <asm/io.h>23#include <linux/uaccess.h>242526struct opal_prd_msg {27union {28struct opal_prd_msg_header header;29DECLARE_FLEX_ARRAY(u8, data);30};31};3233/*34* The msg member must be at the end of the struct, as it's followed by the35* message data.36*/37struct opal_prd_msg_queue_item {38struct list_head list;39struct opal_prd_msg msg;40};4142static struct device_node *prd_node;43static LIST_HEAD(opal_prd_msg_queue);44static DEFINE_SPINLOCK(opal_prd_msg_queue_lock);45static DECLARE_WAIT_QUEUE_HEAD(opal_prd_msg_wait);46static atomic_t prd_usage;4748static bool opal_prd_range_is_valid(uint64_t addr, uint64_t size)49{50struct device_node *parent, *node;51bool found;5253if (addr + size < addr)54return false;5556parent = of_find_node_by_path("/reserved-memory");57if (!parent)58return false;5960found = false;6162for_each_child_of_node(parent, node) {63uint64_t range_addr, range_size, range_end;64const __be32 *addrp;65const char *label;6667addrp = of_get_address(node, 0, &range_size, NULL);68if (!addrp)69continue;7071range_addr = of_read_number(addrp, 2);72range_end = range_addr + range_size;7374label = of_get_property(node, "ibm,prd-label", NULL);7576/* PRD ranges need a label */77if (!label)78continue;7980if (range_end <= range_addr)81continue;8283if (addr >= range_addr && addr + size <= range_end) {84found = true;85of_node_put(node);86break;87}88}8990of_node_put(parent);91return found;92}9394static int opal_prd_open(struct inode *inode, struct file *file)95{96/*97* Prevent multiple (separate) processes from concurrent interactions98* with the FW PRD channel99*/100if (atomic_xchg(&prd_usage, 1) == 1)101return -EBUSY;102103return 0;104}105106/*107* opal_prd_mmap - maps firmware-provided ranges into userspace108* @file: file structure for the device109* @vma: VMA to map the registers into110*/111112static int opal_prd_mmap(struct file *file, struct vm_area_struct *vma)113{114size_t addr, size;115pgprot_t page_prot;116117pr_devel("opal_prd_mmap(0x%016lx, 0x%016lx, 0x%lx, 0x%lx)\n",118vma->vm_start, vma->vm_end, vma->vm_pgoff,119vma->vm_flags);120121addr = vma->vm_pgoff << PAGE_SHIFT;122size = vma->vm_end - vma->vm_start;123124/* ensure we're mapping within one of the allowable ranges */125if (!opal_prd_range_is_valid(addr, size))126return -EINVAL;127128page_prot = phys_mem_access_prot(file, vma->vm_pgoff,129size, vma->vm_page_prot);130131return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, size,132page_prot);133}134135static bool opal_msg_queue_empty(void)136{137unsigned long flags;138bool ret;139140spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);141ret = list_empty(&opal_prd_msg_queue);142spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);143144return ret;145}146147static __poll_t opal_prd_poll(struct file *file,148struct poll_table_struct *wait)149{150poll_wait(file, &opal_prd_msg_wait, wait);151152if (!opal_msg_queue_empty())153return EPOLLIN | EPOLLRDNORM;154155return 0;156}157158static ssize_t opal_prd_read(struct file *file, char __user *buf,159size_t count, loff_t *ppos)160{161struct opal_prd_msg_queue_item *item;162unsigned long flags;163ssize_t size, err;164int rc;165166/* we need at least a header's worth of data */167if (count < sizeof(item->msg.header))168return -EINVAL;169170if (*ppos)171return -ESPIPE;172173item = NULL;174175for (;;) {176177spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);178if (!list_empty(&opal_prd_msg_queue)) {179item = list_first_entry(&opal_prd_msg_queue,180struct opal_prd_msg_queue_item, list);181list_del(&item->list);182}183spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);184185if (item)186break;187188if (file->f_flags & O_NONBLOCK)189return -EAGAIN;190191rc = wait_event_interruptible(opal_prd_msg_wait,192!opal_msg_queue_empty());193if (rc)194return -EINTR;195}196197size = be16_to_cpu(item->msg.header.size);198if (size > count) {199err = -EINVAL;200goto err_requeue;201}202203rc = copy_to_user(buf, &item->msg, size);204if (rc) {205err = -EFAULT;206goto err_requeue;207}208209kfree(item);210211return size;212213err_requeue:214/* eep! re-queue at the head of the list */215spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);216list_add(&item->list, &opal_prd_msg_queue);217spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);218return err;219}220221static ssize_t opal_prd_write(struct file *file, const char __user *buf,222size_t count, loff_t *ppos)223{224struct opal_prd_msg_header hdr;225struct opal_prd_msg *msg;226ssize_t size;227int rc;228229size = sizeof(hdr);230231if (count < size)232return -EINVAL;233234/* grab the header */235rc = copy_from_user(&hdr, buf, sizeof(hdr));236if (rc)237return -EFAULT;238239size = be16_to_cpu(hdr.size);240241msg = memdup_user(buf, size);242if (IS_ERR(msg))243return PTR_ERR(msg);244245rc = opal_prd_msg(msg);246if (rc) {247pr_warn("write: opal_prd_msg returned %d\n", rc);248size = -EIO;249}250251kfree(msg);252253return size;254}255256static int opal_prd_release(struct inode *inode, struct file *file)257{258struct opal_prd_msg msg;259260msg.header.size = cpu_to_be16(sizeof(msg));261msg.header.type = OPAL_PRD_MSG_TYPE_FINI;262263opal_prd_msg(&msg);264265atomic_xchg(&prd_usage, 0);266267return 0;268}269270static long opal_prd_ioctl(struct file *file, unsigned int cmd,271unsigned long param)272{273struct opal_prd_info info;274struct opal_prd_scom scom;275int rc = 0;276277switch (cmd) {278case OPAL_PRD_GET_INFO:279memset(&info, 0, sizeof(info));280info.version = OPAL_PRD_KERNEL_VERSION;281rc = copy_to_user((void __user *)param, &info, sizeof(info));282if (rc)283return -EFAULT;284break;285286case OPAL_PRD_SCOM_READ:287rc = copy_from_user(&scom, (void __user *)param, sizeof(scom));288if (rc)289return -EFAULT;290291scom.rc = opal_xscom_read(scom.chip, scom.addr,292(__be64 *)&scom.data);293scom.data = be64_to_cpu(scom.data);294pr_devel("ioctl SCOM_READ: chip %llx addr %016llx data %016llx rc %lld\n",295scom.chip, scom.addr, scom.data, scom.rc);296297rc = copy_to_user((void __user *)param, &scom, sizeof(scom));298if (rc)299return -EFAULT;300break;301302case OPAL_PRD_SCOM_WRITE:303rc = copy_from_user(&scom, (void __user *)param, sizeof(scom));304if (rc)305return -EFAULT;306307scom.rc = opal_xscom_write(scom.chip, scom.addr, scom.data);308pr_devel("ioctl SCOM_WRITE: chip %llx addr %016llx data %016llx rc %lld\n",309scom.chip, scom.addr, scom.data, scom.rc);310311rc = copy_to_user((void __user *)param, &scom, sizeof(scom));312if (rc)313return -EFAULT;314break;315316default:317rc = -EINVAL;318}319320return rc;321}322323static const struct file_operations opal_prd_fops = {324.open = opal_prd_open,325.mmap = opal_prd_mmap,326.poll = opal_prd_poll,327.read = opal_prd_read,328.write = opal_prd_write,329.unlocked_ioctl = opal_prd_ioctl,330.release = opal_prd_release,331.owner = THIS_MODULE,332};333334static struct miscdevice opal_prd_dev = {335.minor = MISC_DYNAMIC_MINOR,336.name = "opal-prd",337.fops = &opal_prd_fops,338};339340/* opal interface */341static int opal_prd_msg_notifier(struct notifier_block *nb,342unsigned long msg_type, void *_msg)343{344struct opal_prd_msg_queue_item *item;345struct opal_prd_msg_header *hdr;346struct opal_msg *msg = _msg;347int msg_size, item_size;348unsigned long flags;349350if (msg_type != OPAL_MSG_PRD && msg_type != OPAL_MSG_PRD2)351return 0;352353/* Calculate total size of the message and item we need to store. The354* 'size' field in the header includes the header itself. */355hdr = (void *)msg->params;356msg_size = be16_to_cpu(hdr->size);357item_size = msg_size + sizeof(*item) - sizeof(item->msg);358359item = kzalloc(item_size, GFP_ATOMIC);360if (!item)361return -ENOMEM;362363memcpy(&item->msg.data, msg->params, msg_size);364365spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);366list_add_tail(&item->list, &opal_prd_msg_queue);367spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);368369wake_up_interruptible(&opal_prd_msg_wait);370371return 0;372}373374static struct notifier_block opal_prd_event_nb = {375.notifier_call = opal_prd_msg_notifier,376.next = NULL,377.priority = 0,378};379380static struct notifier_block opal_prd_event_nb2 = {381.notifier_call = opal_prd_msg_notifier,382.next = NULL,383.priority = 0,384};385386static int opal_prd_probe(struct platform_device *pdev)387{388int rc;389390if (!pdev || !pdev->dev.of_node)391return -ENODEV;392393/* We should only have one prd driver instance per machine; ensure394* that we only get a valid probe on a single OF node.395*/396if (prd_node)397return -EBUSY;398399prd_node = pdev->dev.of_node;400401rc = opal_message_notifier_register(OPAL_MSG_PRD, &opal_prd_event_nb);402if (rc) {403pr_err("Couldn't register event notifier\n");404return rc;405}406407rc = opal_message_notifier_register(OPAL_MSG_PRD2, &opal_prd_event_nb2);408if (rc) {409pr_err("Couldn't register PRD2 event notifier\n");410opal_message_notifier_unregister(OPAL_MSG_PRD, &opal_prd_event_nb);411return rc;412}413414rc = misc_register(&opal_prd_dev);415if (rc) {416pr_err("failed to register miscdev\n");417opal_message_notifier_unregister(OPAL_MSG_PRD,418&opal_prd_event_nb);419opal_message_notifier_unregister(OPAL_MSG_PRD2,420&opal_prd_event_nb2);421return rc;422}423424return 0;425}426427static void opal_prd_remove(struct platform_device *pdev)428{429misc_deregister(&opal_prd_dev);430opal_message_notifier_unregister(OPAL_MSG_PRD, &opal_prd_event_nb);431opal_message_notifier_unregister(OPAL_MSG_PRD2, &opal_prd_event_nb2);432}433434static const struct of_device_id opal_prd_match[] = {435{ .compatible = "ibm,opal-prd" },436{ },437};438439static struct platform_driver opal_prd_driver = {440.driver = {441.name = "opal-prd",442.of_match_table = opal_prd_match,443},444.probe = opal_prd_probe,445.remove = opal_prd_remove,446};447448module_platform_driver(opal_prd_driver);449450MODULE_DEVICE_TABLE(of, opal_prd_match);451MODULE_DESCRIPTION("PowerNV OPAL runtime diagnostic driver");452MODULE_LICENSE("GPL");453454455