]>
Commit | Line | Data |
---|---|---|
0d7cd855 JK |
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"); |