opal-prd.c 9.7 KB

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