Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/powernv: Add opal-prd channel

This change adds a char device to access the "PRD" (processor runtime
diagnostics) channel to OPAL firmware.

Includes contributions from Vaidyanathan Srinivasan, Neelesh Gupta &
Vishal Kulkarni.

Signed-off-by: Neelesh Gupta <neelegup@linux.vnet.ibm.com>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Acked-by: Stewart Smith <stewart@linux.vnet.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>

authored by

Jeremy Kerr and committed by
Michael Ellerman
0d7cd855 594fcb9e

+535 -2
+20 -1
arch/powerpc/include/asm/opal-api.h
··· 153 153 #define OPAL_FLASH_READ 110 154 154 #define OPAL_FLASH_WRITE 111 155 155 #define OPAL_FLASH_ERASE 112 156 - #define OPAL_LAST 112 156 + #define OPAL_PRD_MSG 113 157 + #define OPAL_LAST 113 157 158 158 159 /* Device tree flags */ 159 160 ··· 360 359 OPAL_MSG_SHUTDOWN, /* params[0] = 1 reboot, 0 shutdown */ 361 360 OPAL_MSG_HMI_EVT, 362 361 OPAL_MSG_DPO, 362 + OPAL_MSG_PRD, 363 363 OPAL_MSG_TYPE_MAX, 364 364 }; 365 365 ··· 682 680 __be64 line; 683 681 __be64 line_len; 684 682 } oppanel_line_t; 683 + 684 + enum opal_prd_msg_type { 685 + OPAL_PRD_MSG_TYPE_INIT = 0, /* HBRT --> OPAL */ 686 + OPAL_PRD_MSG_TYPE_FINI, /* HBRT/kernel --> OPAL */ 687 + OPAL_PRD_MSG_TYPE_ATTN, /* HBRT <-- OPAL */ 688 + OPAL_PRD_MSG_TYPE_ATTN_ACK, /* HBRT --> OPAL */ 689 + OPAL_PRD_MSG_TYPE_OCC_ERROR, /* HBRT <-- OPAL */ 690 + OPAL_PRD_MSG_TYPE_OCC_RESET, /* HBRT <-- OPAL */ 691 + }; 692 + 693 + struct opal_prd_msg_header { 694 + uint8_t type; 695 + uint8_t pad[1]; 696 + __be16 size; 697 + }; 698 + 699 + struct opal_prd_msg; 685 700 686 701 /* 687 702 * SG entries
+1
arch/powerpc/include/asm/opal.h
··· 194 194 uint64_t *msg_len); 195 195 int64_t opal_i2c_request(uint64_t async_token, uint32_t bus_id, 196 196 struct opal_i2c_request *oreq); 197 + int64_t opal_prd_msg(struct opal_prd_msg *msg); 197 198 198 199 int64_t opal_flash_read(uint64_t id, uint64_t offset, uint64_t buf, 199 200 uint64_t size, uint64_t token);
+58
arch/powerpc/include/uapi/asm/opal-prd.h
··· 1 + /* 2 + * OPAL Runtime Diagnostics interface driver 3 + * Supported on POWERNV platform 4 + * 5 + * (C) Copyright IBM 2015 6 + * 7 + * Author: Vaidyanathan Srinivasan <svaidy at linux.vnet.ibm.com> 8 + * Author: Jeremy Kerr <jk@ozlabs.org> 9 + * 10 + * This program is free software; you can redistribute it and/or modify 11 + * it under the terms of the GNU General Public License as published by 12 + * the Free Software Foundation; either version 2, or (at your option) 13 + * any later version. 14 + * 15 + * This program is distributed in the hope that it will be useful, 16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 + * GNU General Public License for more details. 19 + */ 20 + 21 + #ifndef _UAPI_ASM_POWERPC_OPAL_PRD_H_ 22 + #define _UAPI_ASM_POWERPC_OPAL_PRD_H_ 23 + 24 + #include <linux/types.h> 25 + 26 + /** 27 + * The version of the kernel interface of the PRD system. This describes the 28 + * interface available for the /dev/opal-prd device. The actual PRD message 29 + * layout and content is private to the firmware <--> userspace interface, so 30 + * is not covered by this versioning. 31 + * 32 + * Future interface versions are backwards-compatible; if a later kernel 33 + * version is encountered, functionality provided in earlier versions 34 + * will work. 35 + */ 36 + #define OPAL_PRD_KERNEL_VERSION 1 37 + 38 + #define OPAL_PRD_GET_INFO _IOR('o', 0x01, struct opal_prd_info) 39 + #define OPAL_PRD_SCOM_READ _IOR('o', 0x02, struct opal_prd_scom) 40 + #define OPAL_PRD_SCOM_WRITE _IOW('o', 0x03, struct opal_prd_scom) 41 + 42 + #ifndef __ASSEMBLY__ 43 + 44 + struct opal_prd_info { 45 + __u64 version; 46 + __u64 reserved[3]; 47 + }; 48 + 49 + struct opal_prd_scom { 50 + __u64 chip; 51 + __u64 addr; 52 + __u64 data; 53 + __s64 rc; 54 + }; 55 + 56 + #endif /* __ASSEMBLY__ */ 57 + 58 + #endif /* _UAPI_ASM_POWERPC_OPAL_PRD_H */
+7
arch/powerpc/platforms/powernv/Kconfig
··· 19 19 select CPU_FREQ_GOV_CONSERVATIVE 20 20 select PPC_DOORBELL 21 21 default y 22 + 23 + config OPAL_PRD 24 + tristate 'OPAL PRD driver' 25 + depends on PPC_POWERNV 26 + help 27 + This enables the opal-prd driver, a facility to run processor 28 + recovery diagnostics on OpenPower machines
+1
arch/powerpc/platforms/powernv/Makefile
··· 9 9 obj-$(CONFIG_PPC_SCOM) += opal-xscom.o 10 10 obj-$(CONFIG_MEMORY_FAILURE) += opal-memory-errors.o 11 11 obj-$(CONFIG_TRACEPOINTS) += opal-tracepoints.o 12 + obj-$(CONFIG_OPAL_PRD) += opal-prd.o
+444
arch/powerpc/platforms/powernv/opal-prd.c
··· 1 + /* 2 + * OPAL Runtime Diagnostics interface driver 3 + * Supported on POWERNV platform 4 + * 5 + * Copyright IBM Corporation 2015 6 + * 7 + * This program is free software; you can redistribute it and/or modify 8 + * it under the terms of the GNU General Public License version 2 as 9 + * published by the Free Software Foundation. 10 + * 11 + * This program is distributed in the hope that it will be useful, 12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + * GNU General Public License for more details. 15 + */ 16 + 17 + #define pr_fmt(fmt) "opal-prd: " fmt 18 + 19 + #include <linux/kernel.h> 20 + #include <linux/module.h> 21 + #include <linux/platform_device.h> 22 + #include <linux/miscdevice.h> 23 + #include <linux/fs.h> 24 + #include <linux/of.h> 25 + #include <linux/of_address.h> 26 + #include <linux/poll.h> 27 + #include <linux/mm.h> 28 + #include <linux/slab.h> 29 + #include <asm/opal-prd.h> 30 + #include <asm/opal.h> 31 + #include <asm/io.h> 32 + #include <asm/uaccess.h> 33 + 34 + 35 + struct opal_prd_msg_queue_item { 36 + struct opal_prd_msg_header msg; 37 + struct list_head list; 38 + }; 39 + 40 + static struct device_node *prd_node; 41 + static LIST_HEAD(opal_prd_msg_queue); 42 + static DEFINE_SPINLOCK(opal_prd_msg_queue_lock); 43 + static DECLARE_WAIT_QUEUE_HEAD(opal_prd_msg_wait); 44 + static atomic_t prd_usage; 45 + 46 + static bool opal_prd_range_is_valid(uint64_t addr, uint64_t size) 47 + { 48 + struct device_node *parent, *node; 49 + bool found; 50 + 51 + if (addr + size < addr) 52 + return false; 53 + 54 + parent = of_find_node_by_path("/reserved-memory"); 55 + if (!parent) 56 + return false; 57 + 58 + found = false; 59 + 60 + for_each_child_of_node(parent, node) { 61 + uint64_t range_addr, range_size, range_end; 62 + const __be32 *addrp; 63 + const char *label; 64 + 65 + addrp = of_get_address(node, 0, &range_size, NULL); 66 + 67 + range_addr = of_read_number(addrp, 2); 68 + range_end = range_addr + range_size; 69 + 70 + label = of_get_property(node, "ibm,prd-label", NULL); 71 + 72 + /* PRD ranges need a label */ 73 + if (!label) 74 + continue; 75 + 76 + if (range_end <= range_addr) 77 + continue; 78 + 79 + if (addr >= range_addr && addr + size <= range_end) { 80 + found = true; 81 + of_node_put(node); 82 + break; 83 + } 84 + } 85 + 86 + of_node_put(parent); 87 + return found; 88 + } 89 + 90 + static int opal_prd_open(struct inode *inode, struct file *file) 91 + { 92 + /* 93 + * Prevent multiple (separate) processes from concurrent interactions 94 + * with the FW PRD channel 95 + */ 96 + if (atomic_xchg(&prd_usage, 1) == 1) 97 + return -EBUSY; 98 + 99 + return 0; 100 + } 101 + 102 + /* 103 + * opal_prd_mmap - maps firmware-provided ranges into userspace 104 + * @file: file structure for the device 105 + * @vma: VMA to map the registers into 106 + */ 107 + 108 + static int opal_prd_mmap(struct file *file, struct vm_area_struct *vma) 109 + { 110 + size_t addr, size; 111 + int rc; 112 + 113 + pr_devel("opal_prd_mmap(0x%016lx, 0x%016lx, 0x%lx, 0x%lx)\n", 114 + vma->vm_start, vma->vm_end, vma->vm_pgoff, 115 + vma->vm_flags); 116 + 117 + addr = vma->vm_pgoff << PAGE_SHIFT; 118 + size = vma->vm_end - vma->vm_start; 119 + 120 + /* ensure we're mapping within one of the allowable ranges */ 121 + if (!opal_prd_range_is_valid(addr, size)) 122 + return -EINVAL; 123 + 124 + vma->vm_page_prot = __pgprot(pgprot_val(phys_mem_access_prot(file, 125 + vma->vm_pgoff, 126 + size, vma->vm_page_prot)) 127 + | _PAGE_SPECIAL); 128 + 129 + rc = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, size, 130 + vma->vm_page_prot); 131 + 132 + return rc; 133 + } 134 + 135 + static bool opal_msg_queue_empty(void) 136 + { 137 + unsigned long flags; 138 + bool ret; 139 + 140 + spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 141 + ret = list_empty(&opal_prd_msg_queue); 142 + spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 143 + 144 + return ret; 145 + } 146 + 147 + static unsigned int opal_prd_poll(struct file *file, 148 + struct poll_table_struct *wait) 149 + { 150 + poll_wait(file, &opal_prd_msg_wait, wait); 151 + 152 + if (!opal_msg_queue_empty()) 153 + return POLLIN | POLLRDNORM; 154 + 155 + return 0; 156 + } 157 + 158 + static ssize_t opal_prd_read(struct file *file, char __user *buf, 159 + size_t count, loff_t *ppos) 160 + { 161 + struct opal_prd_msg_queue_item *item; 162 + unsigned long flags; 163 + ssize_t size, err; 164 + int rc; 165 + 166 + /* we need at least a header's worth of data */ 167 + if (count < sizeof(item->msg)) 168 + return -EINVAL; 169 + 170 + if (*ppos) 171 + return -ESPIPE; 172 + 173 + item = NULL; 174 + 175 + for (;;) { 176 + 177 + spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 178 + if (!list_empty(&opal_prd_msg_queue)) { 179 + item = list_first_entry(&opal_prd_msg_queue, 180 + struct opal_prd_msg_queue_item, list); 181 + list_del(&item->list); 182 + } 183 + spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 184 + 185 + if (item) 186 + break; 187 + 188 + if (file->f_flags & O_NONBLOCK) 189 + return -EAGAIN; 190 + 191 + rc = wait_event_interruptible(opal_prd_msg_wait, 192 + !opal_msg_queue_empty()); 193 + if (rc) 194 + return -EINTR; 195 + } 196 + 197 + size = be16_to_cpu(item->msg.size); 198 + if (size > count) { 199 + err = -EINVAL; 200 + goto err_requeue; 201 + } 202 + 203 + rc = copy_to_user(buf, &item->msg, size); 204 + if (rc) { 205 + err = -EFAULT; 206 + goto err_requeue; 207 + } 208 + 209 + kfree(item); 210 + 211 + return size; 212 + 213 + err_requeue: 214 + /* eep! re-queue at the head of the list */ 215 + spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 216 + list_add(&item->list, &opal_prd_msg_queue); 217 + spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 218 + return err; 219 + } 220 + 221 + static ssize_t opal_prd_write(struct file *file, const char __user *buf, 222 + size_t count, loff_t *ppos) 223 + { 224 + struct opal_prd_msg_header hdr; 225 + ssize_t size; 226 + void *msg; 227 + int rc; 228 + 229 + size = sizeof(hdr); 230 + 231 + if (count < size) 232 + return -EINVAL; 233 + 234 + /* grab the header */ 235 + rc = copy_from_user(&hdr, buf, sizeof(hdr)); 236 + if (rc) 237 + return -EFAULT; 238 + 239 + size = be16_to_cpu(hdr.size); 240 + 241 + msg = kmalloc(size, GFP_KERNEL); 242 + if (!msg) 243 + return -ENOMEM; 244 + 245 + rc = copy_from_user(msg, buf, size); 246 + if (rc) { 247 + size = -EFAULT; 248 + goto out_free; 249 + } 250 + 251 + rc = opal_prd_msg(msg); 252 + if (rc) { 253 + pr_warn("write: opal_prd_msg returned %d\n", rc); 254 + size = -EIO; 255 + } 256 + 257 + out_free: 258 + kfree(msg); 259 + 260 + return size; 261 + } 262 + 263 + static int opal_prd_release(struct inode *inode, struct file *file) 264 + { 265 + struct opal_prd_msg_header msg; 266 + 267 + msg.size = cpu_to_be16(sizeof(msg)); 268 + msg.type = OPAL_PRD_MSG_TYPE_FINI; 269 + 270 + opal_prd_msg((struct opal_prd_msg *)&msg); 271 + 272 + atomic_xchg(&prd_usage, 0); 273 + 274 + return 0; 275 + } 276 + 277 + static long opal_prd_ioctl(struct file *file, unsigned int cmd, 278 + unsigned long param) 279 + { 280 + struct opal_prd_info info; 281 + struct opal_prd_scom scom; 282 + int rc = 0; 283 + 284 + switch (cmd) { 285 + case OPAL_PRD_GET_INFO: 286 + memset(&info, 0, sizeof(info)); 287 + info.version = OPAL_PRD_KERNEL_VERSION; 288 + rc = copy_to_user((void __user *)param, &info, sizeof(info)); 289 + if (rc) 290 + return -EFAULT; 291 + break; 292 + 293 + case OPAL_PRD_SCOM_READ: 294 + rc = copy_from_user(&scom, (void __user *)param, sizeof(scom)); 295 + if (rc) 296 + return -EFAULT; 297 + 298 + scom.rc = opal_xscom_read(scom.chip, scom.addr, 299 + (__be64 *)&scom.data); 300 + scom.data = be64_to_cpu(scom.data); 301 + pr_devel("ioctl SCOM_READ: chip %llx addr %016llx data %016llx rc %lld\n", 302 + scom.chip, scom.addr, scom.data, scom.rc); 303 + 304 + rc = copy_to_user((void __user *)param, &scom, sizeof(scom)); 305 + if (rc) 306 + return -EFAULT; 307 + break; 308 + 309 + case OPAL_PRD_SCOM_WRITE: 310 + rc = copy_from_user(&scom, (void __user *)param, sizeof(scom)); 311 + if (rc) 312 + return -EFAULT; 313 + 314 + scom.rc = opal_xscom_write(scom.chip, scom.addr, scom.data); 315 + pr_devel("ioctl SCOM_WRITE: chip %llx addr %016llx data %016llx rc %lld\n", 316 + scom.chip, scom.addr, scom.data, scom.rc); 317 + 318 + rc = copy_to_user((void __user *)param, &scom, sizeof(scom)); 319 + if (rc) 320 + return -EFAULT; 321 + break; 322 + 323 + default: 324 + rc = -EINVAL; 325 + } 326 + 327 + return rc; 328 + } 329 + 330 + static const struct file_operations opal_prd_fops = { 331 + .open = opal_prd_open, 332 + .mmap = opal_prd_mmap, 333 + .poll = opal_prd_poll, 334 + .read = opal_prd_read, 335 + .write = opal_prd_write, 336 + .unlocked_ioctl = opal_prd_ioctl, 337 + .release = opal_prd_release, 338 + .owner = THIS_MODULE, 339 + }; 340 + 341 + static struct miscdevice opal_prd_dev = { 342 + .minor = MISC_DYNAMIC_MINOR, 343 + .name = "opal-prd", 344 + .fops = &opal_prd_fops, 345 + }; 346 + 347 + /* opal interface */ 348 + static int opal_prd_msg_notifier(struct notifier_block *nb, 349 + unsigned long msg_type, void *_msg) 350 + { 351 + struct opal_prd_msg_queue_item *item; 352 + struct opal_prd_msg_header *hdr; 353 + struct opal_msg *msg = _msg; 354 + unsigned long flags; 355 + int size; 356 + 357 + if (msg_type != OPAL_MSG_PRD) 358 + return 0; 359 + 360 + /* Calculate total size of the item we need to store. The 'size' field 361 + * in the header includes the header itself. */ 362 + hdr = (void *)msg->params; 363 + size = (sizeof(*item) - sizeof(item->msg)) + be16_to_cpu(hdr->size); 364 + 365 + item = kzalloc(size, GFP_ATOMIC); 366 + if (!item) 367 + return -ENOMEM; 368 + 369 + memcpy(&item->msg, msg->params, size); 370 + 371 + spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 372 + list_add_tail(&item->list, &opal_prd_msg_queue); 373 + spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 374 + 375 + wake_up_interruptible(&opal_prd_msg_wait); 376 + 377 + return 0; 378 + } 379 + 380 + static struct notifier_block opal_prd_event_nb = { 381 + .notifier_call = opal_prd_msg_notifier, 382 + .next = NULL, 383 + .priority = 0, 384 + }; 385 + 386 + static int opal_prd_probe(struct platform_device *pdev) 387 + { 388 + int rc; 389 + 390 + if (!pdev || !pdev->dev.of_node) 391 + return -ENODEV; 392 + 393 + /* We should only have one prd driver instance per machine; ensure 394 + * that we only get a valid probe on a single OF node. 395 + */ 396 + if (prd_node) 397 + return -EBUSY; 398 + 399 + prd_node = pdev->dev.of_node; 400 + 401 + rc = opal_message_notifier_register(OPAL_MSG_PRD, &opal_prd_event_nb); 402 + if (rc) { 403 + pr_err("Couldn't register event notifier\n"); 404 + return rc; 405 + } 406 + 407 + rc = misc_register(&opal_prd_dev); 408 + if (rc) { 409 + pr_err("failed to register miscdev\n"); 410 + opal_message_notifier_unregister(OPAL_MSG_PRD, 411 + &opal_prd_event_nb); 412 + return rc; 413 + } 414 + 415 + return 0; 416 + } 417 + 418 + static int opal_prd_remove(struct platform_device *pdev) 419 + { 420 + misc_deregister(&opal_prd_dev); 421 + opal_message_notifier_unregister(OPAL_MSG_PRD, &opal_prd_event_nb); 422 + return 0; 423 + } 424 + 425 + static const struct of_device_id opal_prd_match[] = { 426 + { .compatible = "ibm,opal-prd" }, 427 + { }, 428 + }; 429 + 430 + static struct platform_driver opal_prd_driver = { 431 + .driver = { 432 + .name = "opal-prd", 433 + .owner = THIS_MODULE, 434 + .of_match_table = opal_prd_match, 435 + }, 436 + .probe = opal_prd_probe, 437 + .remove = opal_prd_remove, 438 + }; 439 + 440 + module_platform_driver(opal_prd_driver); 441 + 442 + MODULE_DEVICE_TABLE(of, opal_prd_match); 443 + MODULE_DESCRIPTION("PowerNV OPAL runtime diagnostic driver"); 444 + MODULE_LICENSE("GPL");
+1
arch/powerpc/platforms/powernv/opal-wrappers.S
··· 296 296 OPAL_CALL(opal_flash_read, OPAL_FLASH_READ); 297 297 OPAL_CALL(opal_flash_write, OPAL_FLASH_WRITE); 298 298 OPAL_CALL(opal_flash_erase, OPAL_FLASH_ERASE); 299 + OPAL_CALL(opal_prd_msg, OPAL_PRD_MSG);
+3 -1
arch/powerpc/platforms/powernv/opal.c
··· 711 711 opal_msglog_init(); 712 712 } 713 713 714 - /* Initialize platform devices: IPMI backend & flash interface */ 714 + /* Initialize platform devices: IPMI backend, PRD & flash interface */ 715 715 opal_pdev_init(opal_node, "ibm,opal-ipmi"); 716 716 opal_pdev_init(opal_node, "ibm,opal-flash"); 717 + opal_pdev_init(opal_node, "ibm,opal-prd"); 717 718 718 719 return 0; 719 720 } ··· 753 752 EXPORT_SYMBOL_GPL(opal_flash_read); 754 753 EXPORT_SYMBOL_GPL(opal_flash_write); 755 754 EXPORT_SYMBOL_GPL(opal_flash_erase); 755 + EXPORT_SYMBOL_GPL(opal_prd_msg); 756 756 757 757 /* Convert a region of vmalloc memory to an opal sg list */ 758 758 struct opal_sg_list *opal_vmalloc_to_sg_list(void *vmalloc_addr,