1 // SPDX-License-Identifier: GPL-2.0-only
3 * OPAL Runtime Diagnostics interface driver
4 * Supported on POWERNV platform
6 * Copyright IBM Corporation 2015
9 #define pr_fmt(fmt) "opal-prd: " fmt
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/miscdevice.h>
17 #include <linux/of_address.h>
18 #include <linux/poll.h>
20 #include <linux/slab.h>
21 #include <asm/opal-prd.h>
24 #include <linux/uaccess.h>
29 struct opal_prd_msg_header header;
30 DECLARE_FLEX_ARRAY(u8, data);
35 * The msg member must be at the end of the struct, as it's followed by the
38 struct opal_prd_msg_queue_item {
39 struct list_head list;
40 struct opal_prd_msg msg;
43 static struct device_node *prd_node;
44 static LIST_HEAD(opal_prd_msg_queue);
45 static DEFINE_SPINLOCK(opal_prd_msg_queue_lock);
46 static DECLARE_WAIT_QUEUE_HEAD(opal_prd_msg_wait);
47 static atomic_t prd_usage;
49 static bool opal_prd_range_is_valid(uint64_t addr, uint64_t size)
51 struct device_node *parent, *node;
54 if (addr + size < addr)
57 parent = of_find_node_by_path("/reserved-memory");
63 for_each_child_of_node(parent, node) {
64 uint64_t range_addr, range_size, range_end;
68 addrp = of_get_address(node, 0, &range_size, NULL);
72 range_addr = of_read_number(addrp, 2);
73 range_end = range_addr + range_size;
75 label = of_get_property(node, "ibm,prd-label", NULL);
77 /* PRD ranges need a label */
81 if (range_end <= range_addr)
84 if (addr >= range_addr && addr + size <= range_end) {
95 static int opal_prd_open(struct inode *inode, struct file *file)
98 * Prevent multiple (separate) processes from concurrent interactions
99 * with the FW PRD channel
101 if (atomic_xchg(&prd_usage, 1) == 1)
108 * opal_prd_mmap - maps firmware-provided ranges into userspace
109 * @file: file structure for the device
110 * @vma: VMA to map the registers into
113 static int opal_prd_mmap(struct file *file, struct vm_area_struct *vma)
118 pr_devel("opal_prd_mmap(0x%016lx, 0x%016lx, 0x%lx, 0x%lx)\n",
119 vma->vm_start, vma->vm_end, vma->vm_pgoff,
122 addr = vma->vm_pgoff << PAGE_SHIFT;
123 size = vma->vm_end - vma->vm_start;
125 /* ensure we're mapping within one of the allowable ranges */
126 if (!opal_prd_range_is_valid(addr, size))
129 page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
130 size, vma->vm_page_prot);
132 return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, size,
136 static bool opal_msg_queue_empty(void)
141 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);
142 ret = list_empty(&opal_prd_msg_queue);
143 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);
148 static __poll_t opal_prd_poll(struct file *file,
149 struct poll_table_struct *wait)
151 poll_wait(file, &opal_prd_msg_wait, wait);
153 if (!opal_msg_queue_empty())
154 return EPOLLIN | EPOLLRDNORM;
159 static ssize_t opal_prd_read(struct file *file, char __user *buf,
160 size_t count, loff_t *ppos)
162 struct opal_prd_msg_queue_item *item;
167 /* we need at least a header's worth of data */
168 if (count < sizeof(item->msg.header))
178 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);
179 if (!list_empty(&opal_prd_msg_queue)) {
180 item = list_first_entry(&opal_prd_msg_queue,
181 struct opal_prd_msg_queue_item, list);
182 list_del(&item->list);
184 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);
189 if (file->f_flags & O_NONBLOCK)
192 rc = wait_event_interruptible(opal_prd_msg_wait,
193 !opal_msg_queue_empty());
198 size = be16_to_cpu(item->msg.header.size);
204 rc = copy_to_user(buf, &item->msg, size);
215 /* eep! re-queue at the head of the list */
216 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);
217 list_add(&item->list, &opal_prd_msg_queue);
218 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);
222 static ssize_t opal_prd_write(struct file *file, const char __user *buf,
223 size_t count, loff_t *ppos)
225 struct opal_prd_msg_header hdr;
226 struct opal_prd_msg *msg;
235 /* grab the header */
236 rc = copy_from_user(&hdr, buf, sizeof(hdr));
240 size = be16_to_cpu(hdr.size);
242 msg = memdup_user(buf, size);
246 rc = opal_prd_msg(msg);
248 pr_warn("write: opal_prd_msg returned %d\n", rc);
257 static int opal_prd_release(struct inode *inode, struct file *file)
259 struct opal_prd_msg msg;
261 msg.header.size = cpu_to_be16(sizeof(msg));
262 msg.header.type = OPAL_PRD_MSG_TYPE_FINI;
266 atomic_xchg(&prd_usage, 0);
271 static long opal_prd_ioctl(struct file *file, unsigned int cmd,
274 struct opal_prd_info info;
275 struct opal_prd_scom scom;
279 case OPAL_PRD_GET_INFO:
280 memset(&info, 0, sizeof(info));
281 info.version = OPAL_PRD_KERNEL_VERSION;
282 rc = copy_to_user((void __user *)param, &info, sizeof(info));
287 case OPAL_PRD_SCOM_READ:
288 rc = copy_from_user(&scom, (void __user *)param, sizeof(scom));
292 scom.rc = opal_xscom_read(scom.chip, scom.addr,
293 (__be64 *)&scom.data);
294 scom.data = be64_to_cpu(scom.data);
295 pr_devel("ioctl SCOM_READ: chip %llx addr %016llx data %016llx rc %lld\n",
296 scom.chip, scom.addr, scom.data, scom.rc);
298 rc = copy_to_user((void __user *)param, &scom, sizeof(scom));
303 case OPAL_PRD_SCOM_WRITE:
304 rc = copy_from_user(&scom, (void __user *)param, sizeof(scom));
308 scom.rc = opal_xscom_write(scom.chip, scom.addr, scom.data);
309 pr_devel("ioctl SCOM_WRITE: chip %llx addr %016llx data %016llx rc %lld\n",
310 scom.chip, scom.addr, scom.data, scom.rc);
312 rc = copy_to_user((void __user *)param, &scom, sizeof(scom));
324 static const struct file_operations opal_prd_fops = {
325 .open = opal_prd_open,
326 .mmap = opal_prd_mmap,
327 .poll = opal_prd_poll,
328 .read = opal_prd_read,
329 .write = opal_prd_write,
330 .unlocked_ioctl = opal_prd_ioctl,
331 .release = opal_prd_release,
332 .owner = THIS_MODULE,
335 static struct miscdevice opal_prd_dev = {
336 .minor = MISC_DYNAMIC_MINOR,
338 .fops = &opal_prd_fops,
342 static int opal_prd_msg_notifier(struct notifier_block *nb,
343 unsigned long msg_type, void *_msg)
345 struct opal_prd_msg_queue_item *item;
346 struct opal_prd_msg_header *hdr;
347 struct opal_msg *msg = _msg;
348 int msg_size, item_size;
351 if (msg_type != OPAL_MSG_PRD && msg_type != OPAL_MSG_PRD2)
354 /* Calculate total size of the message and item we need to store. The
355 * 'size' field in the header includes the header itself. */
356 hdr = (void *)msg->params;
357 msg_size = be16_to_cpu(hdr->size);
358 item_size = msg_size + sizeof(*item) - sizeof(item->msg);
360 item = kzalloc(item_size, GFP_ATOMIC);
364 memcpy(&item->msg.data, msg->params, msg_size);
366 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags);
367 list_add_tail(&item->list, &opal_prd_msg_queue);
368 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags);
370 wake_up_interruptible(&opal_prd_msg_wait);
375 static struct notifier_block opal_prd_event_nb = {
376 .notifier_call = opal_prd_msg_notifier,
381 static struct notifier_block opal_prd_event_nb2 = {
382 .notifier_call = opal_prd_msg_notifier,
387 static int opal_prd_probe(struct platform_device *pdev)
391 if (!pdev || !pdev->dev.of_node)
394 /* We should only have one prd driver instance per machine; ensure
395 * that we only get a valid probe on a single OF node.
400 prd_node = pdev->dev.of_node;
402 rc = opal_message_notifier_register(OPAL_MSG_PRD, &opal_prd_event_nb);
404 pr_err("Couldn't register event notifier\n");
408 rc = opal_message_notifier_register(OPAL_MSG_PRD2, &opal_prd_event_nb2);
410 pr_err("Couldn't register PRD2 event notifier\n");
411 opal_message_notifier_unregister(OPAL_MSG_PRD, &opal_prd_event_nb);
415 rc = misc_register(&opal_prd_dev);
417 pr_err("failed to register miscdev\n");
418 opal_message_notifier_unregister(OPAL_MSG_PRD,
420 opal_message_notifier_unregister(OPAL_MSG_PRD2,
421 &opal_prd_event_nb2);
428 static int opal_prd_remove(struct platform_device *pdev)
430 misc_deregister(&opal_prd_dev);
431 opal_message_notifier_unregister(OPAL_MSG_PRD, &opal_prd_event_nb);
432 opal_message_notifier_unregister(OPAL_MSG_PRD2, &opal_prd_event_nb2);
436 static const struct of_device_id opal_prd_match[] = {
437 { .compatible = "ibm,opal-prd" },
441 static struct platform_driver opal_prd_driver = {
444 .of_match_table = opal_prd_match,
446 .probe = opal_prd_probe,
447 .remove = opal_prd_remove,
450 module_platform_driver(opal_prd_driver);
452 MODULE_DEVICE_TABLE(of, opal_prd_match);
453 MODULE_DESCRIPTION("PowerNV OPAL runtime diagnostic driver");
454 MODULE_LICENSE("GPL");