blob: e7a0d8a1e9633e54bc4428a406e0d5cdb1f44446 [file] [log] [blame]
Carl van Schaikd157ad42018-07-03 11:34:44 +10001/*
2 * drivers/char/okl4_pipe.c
3 *
4 * Copyright (c) 2015 General Dynamics
5 * Copyright (c) 2015 Open Kernel Labs, Inc.
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 * OKL4 Microvisor Pipes driver.
12 *
13 * Clients using this driver must have vclient names of the form
14 * "pipe%d", where %d is the pipe number, which must be
15 * unique and less than MAX_PIPES.
16 */
17
18/* #define DEBUG 1 */
19/* #define VERBOSE_DEBUG 1 */
20
21#include <linux/init.h>
22#include <linux/err.h>
23#include <linux/string.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/interrupt.h>
27#include <linux/platform_device.h>
28#include <linux/mutex.h>
29#include <linux/delay.h>
30#include <linux/workqueue.h>
31#include <linux/slab.h>
32#include <linux/version.h>
33#include <linux/fs.h>
34#include <linux/cdev.h>
35#include <linux/poll.h>
36#include <linux/sched.h>
37#include <linux/wait.h>
38#include <linux/of.h>
39#include <asm/uaccess.h>
40#include <asm-generic/okl4_virq.h>
41
42#include <microvisor/microvisor.h>
43#if defined(CONFIG_OKL4_VIRTUALISATION)
44#include <asm/okl4-microvisor/okl4tags.h>
45#include <asm/okl4-microvisor/microvisor_bus.h>
46#include <asm/okl4-microvisor/virq.h>
47#endif
48
49#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,8,0)
50#define __devinit
51#define __devexit
52#define __devexit_p(x) x
53#endif
54
55#define DRIVER_NAME "okl4-pipe"
56#define DEVICE_NAME "okl4-pipe"
57
58#ifndef CONFIG_OF
59#error "okl4-pipe driver only supported on device tree kernels"
60#endif
61
62#define MAX_PIPES 8
63
64#ifdef CONFIG_OKL4_INTERLEAVED_PRIORITIES
65extern int vcpu_prio_normal;
66#endif
67
68static int okl4_pipe_major;
69static struct class *okl4_pipe_class;
70
71/* This can be extended if required */
72struct okl4_pipe_mv {
73 int pipe_id;
74};
75
76struct okl4_pipe {
77 struct okl4_pipe_data_buffer *write_buf;
78 okl4_kcap_t pipe_tx_kcap;
79 okl4_kcap_t pipe_rx_kcap;
80 int tx_irq;
81 int rx_irq;
82 size_t max_msg_size;
83 int ref_count;
84 struct mutex pipe_mutex;
85 spinlock_t pipe_lock;
86
87 struct platform_device *pdev;
88 struct cdev cdev;
89
90 bool reset;
91 bool tx_maybe_avail;
92 bool rx_maybe_avail;
93
94 wait_queue_head_t rx_wait_q;
95 wait_queue_head_t tx_wait_q;
96 wait_queue_head_t poll_wait_q;
97
98 char *rx_buf;
99 size_t rx_buf_count;
100};
101static struct okl4_pipe pipes[MAX_PIPES];
102
103static okl4_error_t
104okl4_pipe_control(okl4_kcap_t kcap, uint8_t control)
105{
106 okl4_pipe_control_t x = 0;
107
108 okl4_pipe_control_setdoop(&x, true);
109 okl4_pipe_control_setoperation(&x, control);
110 return _okl4_sys_pipe_control(kcap, x);
111}
112
113static irqreturn_t
114okl4_pipe_tx_irq(int irq, void *dev)
115{
116 struct okl4_pipe *pipe = dev;
117 okl4_pipe_state_t payload = okl4_get_virq_payload(irq);
118
119 spin_lock(&pipe->pipe_lock);
120 if (okl4_pipe_state_gettxavailable(&payload))
121 pipe->tx_maybe_avail = true;
122 if (okl4_pipe_state_getreset(&payload)) {
123 pipe->reset = true;
124 pipe->tx_maybe_avail = true;
125 }
126 spin_unlock(&pipe->pipe_lock);
127
128 wake_up_interruptible(&pipe->tx_wait_q);
129 wake_up_interruptible(&pipe->poll_wait_q);
130
131 return IRQ_HANDLED;
132}
133
134static irqreturn_t
135okl4_pipe_rx_irq(int irq, void *dev)
136{
137 struct okl4_pipe *pipe = dev;
138 okl4_pipe_state_t payload = okl4_get_virq_payload(irq);
139
140 spin_lock(&pipe->pipe_lock);
141 if (okl4_pipe_state_getrxavailable(&payload))
142 pipe->rx_maybe_avail = true;
143 if (okl4_pipe_state_getreset(&payload)) {
144 pipe->reset = true;
145 pipe->rx_maybe_avail = true;
146 }
147 spin_unlock(&pipe->pipe_lock);
148
149 wake_up_interruptible(&pipe->rx_wait_q);
150 wake_up_interruptible(&pipe->poll_wait_q);
151
152 return IRQ_HANDLED;
153}
154
155static ssize_t
156okl4_pipe_read(struct file *filp, char __user *buf, size_t count,
157 loff_t *f_pos)
158{
159 struct okl4_pipe_mv *priv = filp->private_data;
160 int pipe_id = priv->pipe_id;
161 struct okl4_pipe *pipe = &pipes[pipe_id];
162 struct _okl4_sys_pipe_recv_return recv_return;
163 uint32_t *buffer = NULL;
164 size_t recv = 0;
165
166 if (!count)
167 return 0;
168
169again:
170 if (pipe->reset)
171 return -EPIPE;
172
173 if (!pipe->rx_maybe_avail && (filp->f_flags & O_NONBLOCK))
174 return -EAGAIN;
175
176 if (wait_event_interruptible(pipe->rx_wait_q, pipe->rx_maybe_avail))
177 return -ERESTARTSYS;
178
179 if (mutex_lock_interruptible(&pipe->pipe_mutex))
180 return -ERESTARTSYS;
181
182 /* Receive buffered data first */
183 if (pipe->rx_buf_count) {
184 recv = min(pipe->rx_buf_count, count);
185
186 if (copy_to_user(buf, pipe->rx_buf, recv)) {
187 mutex_unlock(&pipe->pipe_mutex);
188 return -EFAULT;
189 }
190
191 pipe->rx_buf_count -= recv;
192
193 if (pipe->rx_buf_count) {
194 memmove(pipe->rx_buf, pipe->rx_buf + recv,
195 pipe->max_msg_size - recv);
196 }
197
198 buf += recv;
199 count -= recv;
200 if (!count) {
201 mutex_unlock(&pipe->pipe_mutex);
202 return recv;
203 }
204 }
205
206 buffer = kmalloc(pipe->max_msg_size + sizeof(uint32_t), GFP_KERNEL);
207
208 if (!buffer) {
209 mutex_unlock(&pipe->pipe_mutex);
210 return -ENOMEM;
211 }
212
213 while (count) {
214 okl4_error_t ret;
215 size_t size;
216
217 spin_lock_irq(&pipe->pipe_lock);
218 recv_return = _okl4_sys_pipe_recv(pipe->pipe_rx_kcap,
219 pipe->max_msg_size + sizeof(uint32_t),
220 (void *)buffer);
221 ret = recv_return.error;
222
223 if (ret == OKL4_ERROR_PIPE_NOT_READY ||
224 ret == OKL4_ERROR_PIPE_EMPTY) {
225 pipe->rx_maybe_avail = false;
226 if (!recv) {
227 if (!(filp->f_flags & O_NONBLOCK)) {
228 spin_unlock_irq(&pipe->pipe_lock);
229 mutex_unlock(&pipe->pipe_mutex);
230 kfree(buffer);
231 goto again;
232 }
233 recv = -EAGAIN;
234 }
235 goto error;
236 } else if (ret != OKL4_OK) {
237 dev_err(&pipe->pdev->dev,
238 "pipe send returned error %d in okl4_pipe driver!\n",
239 (int)ret);
240 if (!recv)
241 recv = -ENXIO;
242 goto error;
243 }
244
245 spin_unlock_irq(&pipe->pipe_lock);
246
247 size = buffer[0];
248 if (size > pipe->max_msg_size) {
249 /* pipe error */
250 if (!recv)
251 recv = -EPROTO;
252 goto out;
253 }
254
255 /* Save extra received data */
256 if (size > count) {
257 pipe->rx_buf_count = size - count;
258 memcpy(pipe->rx_buf, (char*)&buffer[1] + count,
259 size - count);
260 size = count;
261 }
262
263 if (copy_to_user(buf, &buffer[1], size)) {
264 if (!recv)
265 recv = -EFAULT;
266 goto out;
267 }
268
269
270 count -= size;
271 buf += size;
272 recv += size;
273 }
274out:
275 mutex_unlock(&pipe->pipe_mutex);
276
277 kfree(buffer);
278 return recv;
279error:
280 spin_unlock_irq(&pipe->pipe_lock);
281 goto out;
282}
283
284static ssize_t
285okl4_pipe_write(struct file *filp, const char __user *buf, size_t count,
286 loff_t *f_pos)
287{
288 struct okl4_pipe_mv *priv = filp->private_data;
289 int pipe_id = priv->pipe_id;
290 struct okl4_pipe *pipe = &pipes[pipe_id];
291 uint32_t *buffer = NULL;
292 size_t sent = 0;
293
294 if (!count)
295 return 0;
296
297again:
298 if (pipe->reset)
299 return -EPIPE;
300
301 if (!pipe->tx_maybe_avail && (filp->f_flags & O_NONBLOCK))
302 return -EAGAIN;
303
304 if (wait_event_interruptible(pipe->tx_wait_q, pipe->tx_maybe_avail))
305 return -ERESTARTSYS;
306
307 if (mutex_lock_interruptible(&pipe->pipe_mutex))
308 return -ERESTARTSYS;
309
310 buffer = kmalloc(pipe->max_msg_size + sizeof(uint32_t), GFP_KERNEL);
311
312 if (!buffer) {
313 mutex_unlock(&pipe->pipe_mutex);
314 return -ENOMEM;
315 }
316
317 while (count) {
318 okl4_error_t ret;
319 size_t size = min(count, pipe->max_msg_size);
320 size_t pipe_size = roundup(size + sizeof(uint32_t),
321 sizeof(uint32_t));
322
323 if (copy_from_user(&buffer[1], buf, size)) {
324 if (!sent)
325 sent = -EFAULT;
326 break;
327 }
328
329 buffer[0] = size;
330
331 spin_lock_irq(&pipe->pipe_lock);
332 ret = _okl4_sys_pipe_send(pipe->pipe_tx_kcap, pipe_size,
333 (void *)buffer);
334 if (ret == OKL4_ERROR_PIPE_NOT_READY ||
335 ret == OKL4_ERROR_PIPE_FULL) {
336 pipe->tx_maybe_avail = false;
337 spin_unlock_irq(&pipe->pipe_lock);
338 if (!sent) {
339 if (filp->f_flags & O_NONBLOCK) {
340 sent = -EAGAIN;
341 break;
342 }
343 mutex_unlock(&pipe->pipe_mutex);
344 kfree(buffer);
345 goto again;
346 }
347 break;
348 } else if (ret != OKL4_OK) {
349 dev_err(&pipe->pdev->dev,
350 "pipe send returned error %d in okl4_pipe driver!\n",
351 (int)ret);
352 if (!sent)
353 sent = -ENXIO;
354 spin_unlock_irq(&pipe->pipe_lock);
355 break;
356 }
357 spin_unlock_irq(&pipe->pipe_lock);
358
359 count -= size;
360 buf += size;
361 sent += size;
362 }
363 mutex_unlock(&pipe->pipe_mutex);
364
365 kfree(buffer);
366 return sent;
367}
368
369
370static unsigned int
371okl4_pipe_poll(struct file *filp, struct poll_table_struct *poll_table)
372{
373 struct okl4_pipe_mv *priv = filp->private_data;
374 int pipe_id = priv->pipe_id;
375 struct okl4_pipe *pipe = &pipes[pipe_id];
376 unsigned int ret = 0;
377
378 poll_wait(filp, &pipe->poll_wait_q, poll_table);
379
380 spin_lock_irq(&pipe->pipe_lock);
381
382 if (pipe->rx_maybe_avail)
383 ret |= POLLIN | POLLRDNORM;
384 if (pipe->tx_maybe_avail)
385 ret |= POLLOUT | POLLWRNORM;
386 if (pipe->reset)
387 ret = POLLHUP;
388
389 spin_unlock_irq(&pipe->pipe_lock);
390
391 return ret;
392}
393
394static int
395okl4_pipe_open(struct inode *inode, struct file *filp)
396{
397 struct okl4_pipe *pipe = container_of(inode->i_cdev,
398 struct okl4_pipe, cdev);
399 struct okl4_pipe_mv *priv = dev_get_drvdata(&pipe->pdev->dev);
400
401 filp->private_data = priv;
402 if (!pipe->ref_count) {
403 pipe->rx_buf = kmalloc(pipe->max_msg_size, GFP_KERNEL);
404 if (!pipe->rx_buf)
405 return -ENOMEM;
406
407 mutex_init(&pipe->pipe_mutex);
408 spin_lock_init(&pipe->pipe_lock);
409
410 pipe->rx_buf_count = 0;
411 pipe->reset = false;
412 pipe->tx_maybe_avail = true;
413 pipe->rx_maybe_avail = true;
414
415 okl4_pipe_control(pipe->pipe_tx_kcap,
416 OKL4_PIPE_CONTROL_OP_SET_TX_READY);
417 okl4_pipe_control(pipe->pipe_rx_kcap,
418 OKL4_PIPE_CONTROL_OP_SET_RX_READY);
419 }
420 pipe->ref_count++;
421 return 0;
422}
423
424static int
425okl4_pipe_close(struct inode *inode, struct file *filp)
426{
427 struct okl4_pipe *pipe = container_of(inode->i_cdev,
428 struct okl4_pipe, cdev);
429
430 pipe->ref_count--;
431 if (!pipe->ref_count) {
432 okl4_pipe_control(pipe->pipe_rx_kcap,
433 OKL4_PIPE_CONTROL_OP_RESET);
434 okl4_pipe_control(pipe->pipe_tx_kcap,
435 OKL4_PIPE_CONTROL_OP_RESET);
436
437 if (pipe->rx_buf)
438 kfree(pipe->rx_buf);
439 pipe->rx_buf = NULL;
440 pipe->rx_buf_count = 0;
441 }
442
443 return 0;
444}
445
446struct file_operations okl4_pipe_fops = {
447 .owner = THIS_MODULE,
448 .read = okl4_pipe_read,
449 .write = okl4_pipe_write,
450 .open = okl4_pipe_open,
451 .release = okl4_pipe_close,
452 .poll = okl4_pipe_poll,
453};
454
455static int __devinit
456okl4_pipe_probe(struct platform_device *pdev)
457{
458 struct okl4_pipe *pipe;
459 int err, pipe_id;
460 struct okl4_pipe_mv *priv;
461 dev_t dev_num;
462 struct device *device = NULL;
463 u32 reg[2];
464 struct resource *irq;
465
466 priv = devm_kzalloc(&pdev->dev, sizeof(struct okl4_pipe_mv),
467 GFP_KERNEL);
468 if (priv == NULL) {
469 err = -ENOMEM;
470 goto fail_alloc_priv;
471 }
472
473 dev_set_drvdata(&pdev->dev, priv);
474
475 pipe_id = of_alias_get_id(pdev->dev.of_node, "pipe");
476 if (pipe_id < 0) {
477 err = -ENXIO;
478 goto fail_pipe_id;
479 }
480
481 if (pipe_id < 0 || pipe_id >= MAX_PIPES) {
482 err = -ENXIO;
483 goto fail_pipe_id;
484 }
485
486 if (of_property_read_u32_array(pdev->dev.of_node, "reg", reg, 2)) {
487 dev_err(&pdev->dev, "need 2 reg resources\n");
488 err = -ENODEV;
489 goto fail_pipe_id;
490 }
491
492 /* Populate the private structure */
493 priv->pipe_id = pipe_id;
494
495 pipe = &pipes[pipe_id];
496
497 /* Set up and register the pipe device */
498 pipe->pdev = pdev;
499 dev_set_name(&pdev->dev, "%s%d", DEVICE_NAME, (int)pipe_id);
500
501 pipe->ref_count = 0;
502 pipe->pipe_tx_kcap = reg[0];
503 pipe->pipe_rx_kcap = reg[1];
504 pipe->max_msg_size = 64;
505
506 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
507 if (!irq) {
508 dev_err(&pdev->dev, "no tx irq resource?\n");
509 err = -ENODEV;
510 goto fail_irq_resource;
511 }
512 pipe->tx_irq = irq->start;
513 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
514 if (!irq) {
515 dev_err(&pdev->dev, "no rx irq resource?\n");
516 err = -ENODEV;
517 goto fail_irq_resource;
518 }
519 pipe->rx_irq = irq->start;
520
521 pipe->write_buf = kmalloc(sizeof(pipe->write_buf), GFP_KERNEL);
522 if (!pipe->write_buf) {
523 dev_err(&pdev->dev, "cannot allocate write buffer\n");
524 err = -ENOMEM;
525 goto fail_malloc_write;
526 }
527
528 init_waitqueue_head(&pipe->rx_wait_q);
529 init_waitqueue_head(&pipe->tx_wait_q);
530 init_waitqueue_head(&pipe->poll_wait_q);
531
532 err = devm_request_irq(&pdev->dev, pipe->rx_irq,
533 okl4_pipe_rx_irq, 0, dev_name(&pdev->dev),
534 pipe);
535 if (err) {
536 dev_err(&pdev->dev, "cannot register rx irq %d: %d\n",
537 (int)pipe->rx_irq, (int)err);
538 goto fail_request_rx_irq;
539 }
540
541 err = devm_request_irq(&pdev->dev, pipe->tx_irq,
542 okl4_pipe_tx_irq, 0, dev_name(&pdev->dev),
543 pipe);
544 if (err) {
545 dev_err(&pdev->dev, "cannot register tx irq %d: %d\n",
546 (int)pipe->tx_irq, (int)err);
547 goto fail_request_tx_irq;
548 }
549
550 dev_num = MKDEV(okl4_pipe_major, pipe_id);
551
552 cdev_init(&pipe->cdev, &okl4_pipe_fops);
553 pipe->cdev.owner = THIS_MODULE;
554 err = cdev_add(&pipe->cdev, dev_num, 1);
555 if (err) {
556 dev_err(&pdev->dev, "cannot add device: %d\n", (int)err);
557 goto fail_cdev_add;
558 }
559
560 device = device_create(okl4_pipe_class, NULL, dev_num, NULL,
561 DEVICE_NAME "%d", pipe_id);
562 if (IS_ERR(device)) {
563 err = PTR_ERR(device);
564 dev_err(&pdev->dev, "cannot create device: %d\n", (int)err);
565 goto fail_device_create;
566 }
567
568 return 0;
569
570fail_device_create:
571 cdev_del(&pipe->cdev);
572fail_cdev_add:
573 devm_free_irq(&pdev->dev, pipe->tx_irq, pipe);
574fail_request_tx_irq:
575 devm_free_irq(&pdev->dev, pipe->rx_irq, pipe);
576fail_request_rx_irq:
577 kfree(pipe->write_buf);
578fail_malloc_write:
579fail_irq_resource:
580fail_pipe_id:
581 dev_set_drvdata(&pdev->dev, NULL);
582 devm_kfree(&pdev->dev, priv);
583fail_alloc_priv:
584 return err;
585}
586
587static int __devexit
588okl4_pipe_remove(struct platform_device *pdev)
589{
590 struct okl4_pipe *pipe;
591 struct okl4_pipe_mv *priv = dev_get_drvdata(&pdev->dev);
592
593 if (priv->pipe_id < 0 || priv->pipe_id >= MAX_PIPES)
594 return -ENXIO;
595
596 pipe = &pipes[priv->pipe_id];
597
598 cdev_del(&pipe->cdev);
599
600 devm_free_irq(&pdev->dev, pipe->tx_irq, pipe);
601 devm_free_irq(&pdev->dev, pipe->rx_irq, pipe);
602
603 kfree(pipe->write_buf);
604
605 dev_set_drvdata(&pdev->dev, NULL);
606 devm_kfree(&pdev->dev, priv);
607
608 return 0;
609}
610
611static const struct of_device_id okl4_pipe_match[] = {
612 {
613 .compatible = "okl,pipe",
614 },
615 {},
616};
617MODULE_DEVICE_TABLE(of, okl4_pipe_match);
618
619static struct platform_driver okl4_pipe_driver = {
620 .probe = okl4_pipe_probe,
621 .remove = __devexit_p(okl4_pipe_remove),
622 .driver = {
623 .name = DRIVER_NAME,
624 .owner = THIS_MODULE,
625 .of_match_table = okl4_pipe_match,
626 },
627};
628
629static int __init
630okl4_pipe_init(void)
631{
632 int err;
633 dev_t dev_num = 0;
634
635 err = alloc_chrdev_region(&dev_num, 0, MAX_PIPES, DEVICE_NAME);
636 if (err < 0) {
637 printk("%s: cannot allocate device region\n", __func__);
638 goto fail_alloc_chrdev_region;
639 }
640 okl4_pipe_major = MAJOR(dev_num);
641
642 okl4_pipe_class = class_create(THIS_MODULE, DEVICE_NAME);
643 if (IS_ERR(okl4_pipe_class)) {
644 err = PTR_ERR(okl4_pipe_class);
645 goto fail_class_create;
646 }
647
648 /* Register the driver with the microvisor bus */
649 err = platform_driver_register(&okl4_pipe_driver);
650 if (err)
651 goto fail_driver_register;
652
653 return 0;
654
655fail_driver_register:
656 class_destroy(okl4_pipe_class);
657fail_class_create:
658 unregister_chrdev_region(dev_num, MAX_PIPES);
659fail_alloc_chrdev_region:
660 return err;
661}
662
663static void __exit
664okl4_pipe_exit(void)
665{
666 dev_t dev_num = MKDEV(okl4_pipe_major, 0);
667
668 platform_driver_unregister(&okl4_pipe_driver);
669 class_destroy(okl4_pipe_class);
670 unregister_chrdev_region(dev_num, MAX_PIPES);
671}
672
673module_init(okl4_pipe_init);
674module_exit(okl4_pipe_exit);
675
676MODULE_DESCRIPTION("OKL4 pipe driver");
677MODULE_AUTHOR("John Clarke <johnc@cog.systems>");