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 /** 36 * The msg member must be at the end of the struct, as it's followed by the 37 * message data. 38 */ 39 struct opal_prd_msg_queue_item { 40 struct list_head list; 41 struct opal_prd_msg_header msg; 42 }; 43 44 static struct device_node *prd_node; 45 static LIST_HEAD(opal_prd_msg_queue); 46 static DEFINE_SPINLOCK(opal_prd_msg_queue_lock); 47 static DECLARE_WAIT_QUEUE_HEAD(opal_prd_msg_wait); 48 static atomic_t prd_usage; 49 50 static bool opal_prd_range_is_valid(uint64_t addr, uint64_t size) 51 { 52 struct device_node *parent, *node; 53 bool found; 54 55 if (addr + size < addr) 56 return false; 57 58 parent = of_find_node_by_path("/reserved-memory"); 59 if (!parent) 60 return false; 61 62 found = false; 63 64 for_each_child_of_node(parent, node) { 65 uint64_t range_addr, range_size, range_end; 66 const __be32 *addrp; 67 const char *label; 68 69 addrp = of_get_address(node, 0, &range_size, NULL); 70 71 range_addr = of_read_number(addrp, 2); 72 range_end = range_addr + range_size; 73 74 label = of_get_property(node, "ibm,prd-label", NULL); 75 76 /* PRD ranges need a label */ 77 if (!label) 78 continue; 79 80 if (range_end <= range_addr) 81 continue; 82 83 if (addr >= range_addr && addr + size <= range_end) { 84 found = true; 85 of_node_put(node); 86 break; 87 } 88 } 89 90 of_node_put(parent); 91 return found; 92 } 93 94 static int opal_prd_open(struct inode *inode, struct file *file) 95 { 96 /* 97 * Prevent multiple (separate) processes from concurrent interactions 98 * with the FW PRD channel 99 */ 100 if (atomic_xchg(&prd_usage, 1) == 1) 101 return -EBUSY; 102 103 return 0; 104 } 105 106 /* 107 * opal_prd_mmap - maps firmware-provided ranges into userspace 108 * @file: file structure for the device 109 * @vma: VMA to map the registers into 110 */ 111 112 static int opal_prd_mmap(struct file *file, struct vm_area_struct *vma) 113 { 114 size_t addr, size; 115 int rc; 116 117 pr_devel("opal_prd_mmap(0x%016lx, 0x%016lx, 0x%lx, 0x%lx)\n", 118 vma->vm_start, vma->vm_end, vma->vm_pgoff, 119 vma->vm_flags); 120 121 addr = vma->vm_pgoff << PAGE_SHIFT; 122 size = vma->vm_end - vma->vm_start; 123 124 /* ensure we're mapping within one of the allowable ranges */ 125 if (!opal_prd_range_is_valid(addr, size)) 126 return -EINVAL; 127 128 vma->vm_page_prot = __pgprot(pgprot_val(phys_mem_access_prot(file, 129 vma->vm_pgoff, 130 size, vma->vm_page_prot)) 131 | _PAGE_SPECIAL); 132 133 rc = remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, size, 134 vma->vm_page_prot); 135 136 return rc; 137 } 138 139 static bool opal_msg_queue_empty(void) 140 { 141 unsigned long flags; 142 bool ret; 143 144 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 145 ret = list_empty(&opal_prd_msg_queue); 146 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 147 148 return ret; 149 } 150 151 static unsigned int opal_prd_poll(struct file *file, 152 struct poll_table_struct *wait) 153 { 154 poll_wait(file, &opal_prd_msg_wait, wait); 155 156 if (!opal_msg_queue_empty()) 157 return POLLIN | POLLRDNORM; 158 159 return 0; 160 } 161 162 static ssize_t opal_prd_read(struct file *file, char __user *buf, 163 size_t count, loff_t *ppos) 164 { 165 struct opal_prd_msg_queue_item *item; 166 unsigned long flags; 167 ssize_t size, err; 168 int rc; 169 170 /* we need at least a header's worth of data */ 171 if (count < sizeof(item->msg)) 172 return -EINVAL; 173 174 if (*ppos) 175 return -ESPIPE; 176 177 item = NULL; 178 179 for (;;) { 180 181 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 182 if (!list_empty(&opal_prd_msg_queue)) { 183 item = list_first_entry(&opal_prd_msg_queue, 184 struct opal_prd_msg_queue_item, list); 185 list_del(&item->list); 186 } 187 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 188 189 if (item) 190 break; 191 192 if (file->f_flags & O_NONBLOCK) 193 return -EAGAIN; 194 195 rc = wait_event_interruptible(opal_prd_msg_wait, 196 !opal_msg_queue_empty()); 197 if (rc) 198 return -EINTR; 199 } 200 201 size = be16_to_cpu(item->msg.size); 202 if (size > count) { 203 err = -EINVAL; 204 goto err_requeue; 205 } 206 207 rc = copy_to_user(buf, &item->msg, size); 208 if (rc) { 209 err = -EFAULT; 210 goto err_requeue; 211 } 212 213 kfree(item); 214 215 return size; 216 217 err_requeue: 218 /* eep! re-queue at the head of the list */ 219 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 220 list_add(&item->list, &opal_prd_msg_queue); 221 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 222 return err; 223 } 224 225 static ssize_t opal_prd_write(struct file *file, const char __user *buf, 226 size_t count, loff_t *ppos) 227 { 228 struct opal_prd_msg_header hdr; 229 ssize_t size; 230 void *msg; 231 int rc; 232 233 size = sizeof(hdr); 234 235 if (count < size) 236 return -EINVAL; 237 238 /* grab the header */ 239 rc = copy_from_user(&hdr, buf, sizeof(hdr)); 240 if (rc) 241 return -EFAULT; 242 243 size = be16_to_cpu(hdr.size); 244 245 msg = kmalloc(size, GFP_KERNEL); 246 if (!msg) 247 return -ENOMEM; 248 249 rc = copy_from_user(msg, buf, size); 250 if (rc) { 251 size = -EFAULT; 252 goto out_free; 253 } 254 255 rc = opal_prd_msg(msg); 256 if (rc) { 257 pr_warn("write: opal_prd_msg returned %d\n", rc); 258 size = -EIO; 259 } 260 261 out_free: 262 kfree(msg); 263 264 return size; 265 } 266 267 static int opal_prd_release(struct inode *inode, struct file *file) 268 { 269 struct opal_prd_msg_header msg; 270 271 msg.size = cpu_to_be16(sizeof(msg)); 272 msg.type = OPAL_PRD_MSG_TYPE_FINI; 273 274 opal_prd_msg((struct opal_prd_msg *)&msg); 275 276 atomic_xchg(&prd_usage, 0); 277 278 return 0; 279 } 280 281 static long opal_prd_ioctl(struct file *file, unsigned int cmd, 282 unsigned long param) 283 { 284 struct opal_prd_info info; 285 struct opal_prd_scom scom; 286 int rc = 0; 287 288 switch (cmd) { 289 case OPAL_PRD_GET_INFO: 290 memset(&info, 0, sizeof(info)); 291 info.version = OPAL_PRD_KERNEL_VERSION; 292 rc = copy_to_user((void __user *)param, &info, sizeof(info)); 293 if (rc) 294 return -EFAULT; 295 break; 296 297 case OPAL_PRD_SCOM_READ: 298 rc = copy_from_user(&scom, (void __user *)param, sizeof(scom)); 299 if (rc) 300 return -EFAULT; 301 302 scom.rc = opal_xscom_read(scom.chip, scom.addr, 303 (__be64 *)&scom.data); 304 scom.data = be64_to_cpu(scom.data); 305 pr_devel("ioctl SCOM_READ: chip %llx addr %016llx data %016llx rc %lld\n", 306 scom.chip, scom.addr, scom.data, scom.rc); 307 308 rc = copy_to_user((void __user *)param, &scom, sizeof(scom)); 309 if (rc) 310 return -EFAULT; 311 break; 312 313 case OPAL_PRD_SCOM_WRITE: 314 rc = copy_from_user(&scom, (void __user *)param, sizeof(scom)); 315 if (rc) 316 return -EFAULT; 317 318 scom.rc = opal_xscom_write(scom.chip, scom.addr, scom.data); 319 pr_devel("ioctl SCOM_WRITE: chip %llx addr %016llx data %016llx rc %lld\n", 320 scom.chip, scom.addr, scom.data, scom.rc); 321 322 rc = copy_to_user((void __user *)param, &scom, sizeof(scom)); 323 if (rc) 324 return -EFAULT; 325 break; 326 327 default: 328 rc = -EINVAL; 329 } 330 331 return rc; 332 } 333 334 static const struct file_operations opal_prd_fops = { 335 .open = opal_prd_open, 336 .mmap = opal_prd_mmap, 337 .poll = opal_prd_poll, 338 .read = opal_prd_read, 339 .write = opal_prd_write, 340 .unlocked_ioctl = opal_prd_ioctl, 341 .release = opal_prd_release, 342 .owner = THIS_MODULE, 343 }; 344 345 static struct miscdevice opal_prd_dev = { 346 .minor = MISC_DYNAMIC_MINOR, 347 .name = "opal-prd", 348 .fops = &opal_prd_fops, 349 }; 350 351 /* opal interface */ 352 static int opal_prd_msg_notifier(struct notifier_block *nb, 353 unsigned long msg_type, void *_msg) 354 { 355 struct opal_prd_msg_queue_item *item; 356 struct opal_prd_msg_header *hdr; 357 struct opal_msg *msg = _msg; 358 int msg_size, item_size; 359 unsigned long flags; 360 361 if (msg_type != OPAL_MSG_PRD) 362 return 0; 363 364 /* Calculate total size of the message and item we need to store. The 365 * 'size' field in the header includes the header itself. */ 366 hdr = (void *)msg->params; 367 msg_size = be16_to_cpu(hdr->size); 368 item_size = msg_size + sizeof(*item) - sizeof(item->msg); 369 370 item = kzalloc(item_size, GFP_ATOMIC); 371 if (!item) 372 return -ENOMEM; 373 374 memcpy(&item->msg, msg->params, msg_size); 375 376 spin_lock_irqsave(&opal_prd_msg_queue_lock, flags); 377 list_add_tail(&item->list, &opal_prd_msg_queue); 378 spin_unlock_irqrestore(&opal_prd_msg_queue_lock, flags); 379 380 wake_up_interruptible(&opal_prd_msg_wait); 381 382 return 0; 383 } 384 385 static struct notifier_block opal_prd_event_nb = { 386 .notifier_call = opal_prd_msg_notifier, 387 .next = NULL, 388 .priority = 0, 389 }; 390 391 static int opal_prd_probe(struct platform_device *pdev) 392 { 393 int rc; 394 395 if (!pdev || !pdev->dev.of_node) 396 return -ENODEV; 397 398 /* We should only have one prd driver instance per machine; ensure 399 * that we only get a valid probe on a single OF node. 400 */ 401 if (prd_node) 402 return -EBUSY; 403 404 prd_node = pdev->dev.of_node; 405 406 rc = opal_message_notifier_register(OPAL_MSG_PRD, &opal_prd_event_nb); 407 if (rc) { 408 pr_err("Couldn't register event notifier\n"); 409 return rc; 410 } 411 412 rc = misc_register(&opal_prd_dev); 413 if (rc) { 414 pr_err("failed to register miscdev\n"); 415 opal_message_notifier_unregister(OPAL_MSG_PRD, 416 &opal_prd_event_nb); 417 return rc; 418 } 419 420 return 0; 421 } 422 423 static int opal_prd_remove(struct platform_device *pdev) 424 { 425 misc_deregister(&opal_prd_dev); 426 opal_message_notifier_unregister(OPAL_MSG_PRD, &opal_prd_event_nb); 427 return 0; 428 } 429 430 static const struct of_device_id opal_prd_match[] = { 431 { .compatible = "ibm,opal-prd" }, 432 { }, 433 }; 434 435 static struct platform_driver opal_prd_driver = { 436 .driver = { 437 .name = "opal-prd", 438 .owner = THIS_MODULE, 439 .of_match_table = opal_prd_match, 440 }, 441 .probe = opal_prd_probe, 442 .remove = opal_prd_remove, 443 }; 444 445 module_platform_driver(opal_prd_driver); 446 447 MODULE_DEVICE_TABLE(of, opal_prd_match); 448 MODULE_DESCRIPTION("PowerNV OPAL runtime diagnostic driver"); 449 MODULE_LICENSE("GPL"); 450