1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/err.h>
30#include <linux/interrupt.h>
31#include <linux/fs.h>
32#include <linux/poll.h>
33#include <asm/epapr_hcalls.h>
34#include <linux/of.h>
35#include <linux/platform_device.h>
36#include <linux/cdev.h>
37#include <linux/console.h>
38#include <linux/tty.h>
39#include <linux/tty_flip.h>
40#include <linux/circ_buf.h>
41#include <asm/udbg.h>
42
43
44#define BUF_SIZE 2048
45
46
47struct ehv_bc_data {
48 struct device *dev;
49 struct tty_port port;
50 uint32_t handle;
51 unsigned int rx_irq;
52 unsigned int tx_irq;
53
54 spinlock_t lock;
55 unsigned char buf[BUF_SIZE];
56 unsigned int head;
57 unsigned int tail;
58
59 int tx_irq_enabled;
60};
61
62
63static struct ehv_bc_data *bcs;
64
65
66static unsigned int stdout_bc;
67
68
69static unsigned int stdout_irq;
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89static void enable_tx_interrupt(struct ehv_bc_data *bc)
90{
91 if (!bc->tx_irq_enabled) {
92 enable_irq(bc->tx_irq);
93 bc->tx_irq_enabled = 1;
94 }
95}
96
97static void disable_tx_interrupt(struct ehv_bc_data *bc)
98{
99 if (bc->tx_irq_enabled) {
100 disable_irq_nosync(bc->tx_irq);
101 bc->tx_irq_enabled = 0;
102 }
103}
104
105
106
107
108
109
110
111
112
113static int find_console_handle(void)
114{
115 struct device_node *np, *np2;
116 const char *sprop = NULL;
117 const uint32_t *iprop;
118
119 np = of_find_node_by_path("/chosen");
120 if (np)
121 sprop = of_get_property(np, "stdout-path", NULL);
122
123 if (!np || !sprop) {
124 of_node_put(np);
125 np = of_find_node_by_name(NULL, "aliases");
126 if (np)
127 sprop = of_get_property(np, "stdout", NULL);
128 }
129
130 if (!sprop) {
131 of_node_put(np);
132 return 0;
133 }
134
135
136
137
138
139
140
141 np2 = of_find_node_by_path(sprop);
142 of_node_put(np);
143 np = np2;
144 if (!np) {
145 pr_warning("ehv-bc: stdout node '%s' does not exist\n", sprop);
146 return 0;
147 }
148
149
150 if (!of_device_is_compatible(np, "epapr,hv-byte-channel")) {
151 of_node_put(np);
152 return 0;
153 }
154
155 stdout_irq = irq_of_parse_and_map(np, 0);
156 if (stdout_irq == NO_IRQ) {
157 pr_err("ehv-bc: no 'interrupts' property in %s node\n", sprop);
158 of_node_put(np);
159 return 0;
160 }
161
162
163
164
165 iprop = of_get_property(np, "hv-handle", NULL);
166 if (!iprop) {
167 pr_err("ehv-bc: no 'hv-handle' property in %s node\n",
168 np->name);
169 of_node_put(np);
170 return 0;
171 }
172 stdout_bc = be32_to_cpu(*iprop);
173
174 of_node_put(np);
175 return 1;
176}
177
178
179
180#ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
181
182
183
184
185
186
187
188
189
190static void byte_channel_spin_send(const char data)
191{
192 int ret, count;
193
194 do {
195 count = 1;
196 ret = ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
197 &count, &data);
198 } while (ret == EV_EAGAIN);
199}
200
201
202
203
204
205static void ehv_bc_udbg_putc(char c)
206{
207 if (c == '\n')
208 byte_channel_spin_send('\r');
209
210 byte_channel_spin_send(c);
211}
212
213
214
215
216
217
218
219
220
221
222
223
224void __init udbg_init_ehv_bc(void)
225{
226 unsigned int rx_count, tx_count;
227 unsigned int ret;
228
229
230 ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
231 &rx_count, &tx_count);
232 if (ret)
233 return;
234
235 udbg_putc = ehv_bc_udbg_putc;
236 register_early_udbg_console();
237
238 udbg_printf("ehv-bc: early console using byte channel handle %u\n",
239 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
240}
241
242#endif
243
244
245
246static struct tty_driver *ehv_bc_driver;
247
248
249
250
251
252
253
254static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
255 unsigned int count)
256{
257 unsigned int len;
258 int ret = 0;
259
260 while (count) {
261 len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
262 do {
263 ret = ev_byte_channel_send(handle, &len, s);
264 } while (ret == EV_EAGAIN);
265 count -= len;
266 s += len;
267 }
268
269 return ret;
270}
271
272
273
274
275
276
277
278
279
280
281
282static void ehv_bc_console_write(struct console *co, const char *s,
283 unsigned int count)
284{
285 char s2[EV_BYTE_CHANNEL_MAX_BYTES];
286 unsigned int i, j = 0;
287 char c;
288
289 for (i = 0; i < count; i++) {
290 c = *s++;
291
292 if (c == '\n')
293 s2[j++] = '\r';
294
295 s2[j++] = c;
296 if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
297 if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
298 return;
299 j = 0;
300 }
301 }
302
303 if (j)
304 ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
305}
306
307
308
309
310
311
312static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
313{
314 *index = co->index;
315
316 return ehv_bc_driver;
317}
318
319static struct console ehv_bc_console = {
320 .name = "ttyEHV",
321 .write = ehv_bc_console_write,
322 .device = ehv_bc_console_device,
323 .flags = CON_PRINTBUFFER | CON_ENABLED,
324};
325
326
327
328
329
330
331
332
333
334static int __init ehv_bc_console_init(void)
335{
336 if (!find_console_handle()) {
337 pr_debug("ehv-bc: stdout is not a byte channel\n");
338 return -ENODEV;
339 }
340
341#ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
342
343
344
345 if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
346 pr_warning("ehv-bc: udbg handle %u is not the stdout handle\n",
347 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
348#endif
349
350
351
352
353
354 add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
355 register_console(&ehv_bc_console);
356
357 pr_info("ehv-bc: registered console driver for byte channel %u\n",
358 stdout_bc);
359
360 return 0;
361}
362console_initcall(ehv_bc_console_init);
363
364
365
366
367
368
369
370
371static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
372{
373 struct ehv_bc_data *bc = data;
374 struct tty_struct *ttys = tty_port_tty_get(&bc->port);
375 unsigned int rx_count, tx_count, len;
376 int count;
377 char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
378 int ret;
379
380
381 if (!ttys)
382 return IRQ_HANDLED;
383
384
385
386
387
388 ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
389 count = tty_buffer_request_room(ttys, rx_count);
390
391
392
393
394
395
396
397 while (count > 0) {
398 len = min_t(unsigned int, count, sizeof(buffer));
399
400
401
402
403 ev_byte_channel_receive(bc->handle, &len, buffer);
404
405
406
407
408
409
410 ret = tty_insert_flip_string(ttys, buffer, len);
411
412
413
414
415
416
417
418 if (ret != len)
419 break;
420
421 count -= len;
422 }
423
424
425 tty_flip_buffer_push(ttys);
426
427 tty_kref_put(ttys);
428
429 return IRQ_HANDLED;
430}
431
432
433
434
435
436
437
438static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
439{
440 unsigned int count;
441 unsigned int len, ret;
442 unsigned long flags;
443
444 do {
445 spin_lock_irqsave(&bc->lock, flags);
446 len = min_t(unsigned int,
447 CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
448 EV_BYTE_CHANNEL_MAX_BYTES);
449
450 ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
451
452
453 if (!ret || (ret == EV_EAGAIN))
454 bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
455
456 count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
457 spin_unlock_irqrestore(&bc->lock, flags);
458 } while (count && !ret);
459
460 spin_lock_irqsave(&bc->lock, flags);
461 if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
462
463
464
465
466
467 enable_tx_interrupt(bc);
468 else
469 disable_tx_interrupt(bc);
470 spin_unlock_irqrestore(&bc->lock, flags);
471}
472
473
474
475
476
477
478
479static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
480{
481 struct ehv_bc_data *bc = data;
482 struct tty_struct *ttys = tty_port_tty_get(&bc->port);
483
484 ehv_bc_tx_dequeue(bc);
485 if (ttys) {
486 tty_wakeup(ttys);
487 tty_kref_put(ttys);
488 }
489
490 return IRQ_HANDLED;
491}
492
493
494
495
496
497
498
499
500
501
502
503
504static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s,
505 int count)
506{
507 struct ehv_bc_data *bc = ttys->driver_data;
508 unsigned long flags;
509 unsigned int len;
510 unsigned int written = 0;
511
512 while (1) {
513 spin_lock_irqsave(&bc->lock, flags);
514 len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
515 if (count < len)
516 len = count;
517 if (len) {
518 memcpy(bc->buf + bc->head, s, len);
519 bc->head = (bc->head + len) & (BUF_SIZE - 1);
520 }
521 spin_unlock_irqrestore(&bc->lock, flags);
522 if (!len)
523 break;
524
525 s += len;
526 count -= len;
527 written += len;
528 }
529
530 ehv_bc_tx_dequeue(bc);
531
532 return written;
533}
534
535
536
537
538
539
540
541
542
543
544static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
545{
546 struct ehv_bc_data *bc = &bcs[ttys->index];
547
548 if (!bc->dev)
549 return -ENODEV;
550
551 return tty_port_open(&bc->port, ttys, filp);
552}
553
554
555
556
557
558
559static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
560{
561 struct ehv_bc_data *bc = &bcs[ttys->index];
562
563 if (bc->dev)
564 tty_port_close(&bc->port, ttys, filp);
565}
566
567
568
569
570
571
572
573
574static int ehv_bc_tty_write_room(struct tty_struct *ttys)
575{
576 struct ehv_bc_data *bc = ttys->driver_data;
577 unsigned long flags;
578 int count;
579
580 spin_lock_irqsave(&bc->lock, flags);
581 count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
582 spin_unlock_irqrestore(&bc->lock, flags);
583
584 return count;
585}
586
587
588
589
590
591
592
593
594
595
596
597
598
599static void ehv_bc_tty_throttle(struct tty_struct *ttys)
600{
601 struct ehv_bc_data *bc = ttys->driver_data;
602
603 disable_irq(bc->rx_irq);
604}
605
606
607
608
609
610
611
612
613static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
614{
615 struct ehv_bc_data *bc = ttys->driver_data;
616
617
618
619
620 enable_irq(bc->rx_irq);
621}
622
623static void ehv_bc_tty_hangup(struct tty_struct *ttys)
624{
625 struct ehv_bc_data *bc = ttys->driver_data;
626
627 ehv_bc_tx_dequeue(bc);
628 tty_port_hangup(&bc->port);
629}
630
631
632
633
634
635
636
637
638static const struct tty_operations ehv_bc_ops = {
639 .open = ehv_bc_tty_open,
640 .close = ehv_bc_tty_close,
641 .write = ehv_bc_tty_write,
642 .write_room = ehv_bc_tty_write_room,
643 .throttle = ehv_bc_tty_throttle,
644 .unthrottle = ehv_bc_tty_unthrottle,
645 .hangup = ehv_bc_tty_hangup,
646};
647
648
649
650
651
652
653
654
655static int ehv_bc_tty_port_activate(struct tty_port *port,
656 struct tty_struct *ttys)
657{
658 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
659 int ret;
660
661 ttys->driver_data = bc;
662
663 ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
664 if (ret < 0) {
665 dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
666 bc->rx_irq, ret);
667 return ret;
668 }
669
670
671 bc->tx_irq_enabled = 1;
672
673 ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
674 if (ret < 0) {
675 dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
676 bc->tx_irq, ret);
677 free_irq(bc->rx_irq, bc);
678 return ret;
679 }
680
681
682
683
684 disable_tx_interrupt(bc);
685
686 return 0;
687}
688
689static void ehv_bc_tty_port_shutdown(struct tty_port *port)
690{
691 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
692
693 free_irq(bc->tx_irq, bc);
694 free_irq(bc->rx_irq, bc);
695}
696
697static const struct tty_port_operations ehv_bc_tty_port_ops = {
698 .activate = ehv_bc_tty_port_activate,
699 .shutdown = ehv_bc_tty_port_shutdown,
700};
701
702static int __devinit ehv_bc_tty_probe(struct platform_device *pdev)
703{
704 struct device_node *np = pdev->dev.of_node;
705 struct ehv_bc_data *bc;
706 const uint32_t *iprop;
707 unsigned int handle;
708 int ret;
709 static unsigned int index = 1;
710 unsigned int i;
711
712 iprop = of_get_property(np, "hv-handle", NULL);
713 if (!iprop) {
714 dev_err(&pdev->dev, "no 'hv-handle' property in %s node\n",
715 np->name);
716 return -ENODEV;
717 }
718
719
720
721
722
723 handle = be32_to_cpu(*iprop);
724 i = (handle == stdout_bc) ? 0 : index++;
725 bc = &bcs[i];
726
727 bc->handle = handle;
728 bc->head = 0;
729 bc->tail = 0;
730 spin_lock_init(&bc->lock);
731
732 bc->rx_irq = irq_of_parse_and_map(np, 0);
733 bc->tx_irq = irq_of_parse_and_map(np, 1);
734 if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) {
735 dev_err(&pdev->dev, "no 'interrupts' property in %s node\n",
736 np->name);
737 ret = -ENODEV;
738 goto error;
739 }
740
741 bc->dev = tty_register_device(ehv_bc_driver, i, &pdev->dev);
742 if (IS_ERR(bc->dev)) {
743 ret = PTR_ERR(bc->dev);
744 dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
745 goto error;
746 }
747
748 tty_port_init(&bc->port);
749 bc->port.ops = &ehv_bc_tty_port_ops;
750
751 dev_set_drvdata(&pdev->dev, bc);
752
753 dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
754 ehv_bc_driver->name, i, bc->handle);
755
756 return 0;
757
758error:
759 irq_dispose_mapping(bc->tx_irq);
760 irq_dispose_mapping(bc->rx_irq);
761
762 memset(bc, 0, sizeof(struct ehv_bc_data));
763 return ret;
764}
765
766static int ehv_bc_tty_remove(struct platform_device *pdev)
767{
768 struct ehv_bc_data *bc = dev_get_drvdata(&pdev->dev);
769
770 tty_unregister_device(ehv_bc_driver, bc - bcs);
771
772 irq_dispose_mapping(bc->tx_irq);
773 irq_dispose_mapping(bc->rx_irq);
774
775 return 0;
776}
777
778static const struct of_device_id ehv_bc_tty_of_ids[] = {
779 { .compatible = "epapr,hv-byte-channel" },
780 {}
781};
782
783static struct platform_driver ehv_bc_tty_driver = {
784 .driver = {
785 .owner = THIS_MODULE,
786 .name = "ehv-bc",
787 .of_match_table = ehv_bc_tty_of_ids,
788 },
789 .probe = ehv_bc_tty_probe,
790 .remove = ehv_bc_tty_remove,
791};
792
793
794
795
796
797
798static int __init ehv_bc_init(void)
799{
800 struct device_node *np;
801 unsigned int count = 0;
802 int ret;
803
804 pr_info("ePAPR hypervisor byte channel driver\n");
805
806
807 for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
808 count++;
809
810 if (!count)
811 return -ENODEV;
812
813
814
815
816
817
818 bcs = kzalloc(count * sizeof(struct ehv_bc_data), GFP_KERNEL);
819 if (!bcs)
820 return -ENOMEM;
821
822 ehv_bc_driver = alloc_tty_driver(count);
823 if (!ehv_bc_driver) {
824 ret = -ENOMEM;
825 goto error;
826 }
827
828 ehv_bc_driver->driver_name = "ehv-bc";
829 ehv_bc_driver->name = ehv_bc_console.name;
830 ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE;
831 ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE;
832 ehv_bc_driver->init_termios = tty_std_termios;
833 ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
834 tty_set_operations(ehv_bc_driver, &ehv_bc_ops);
835
836 ret = tty_register_driver(ehv_bc_driver);
837 if (ret) {
838 pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
839 goto error;
840 }
841
842 ret = platform_driver_register(&ehv_bc_tty_driver);
843 if (ret) {
844 pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
845 ret);
846 goto error;
847 }
848
849 return 0;
850
851error:
852 if (ehv_bc_driver) {
853 tty_unregister_driver(ehv_bc_driver);
854 put_tty_driver(ehv_bc_driver);
855 }
856
857 kfree(bcs);
858
859 return ret;
860}
861
862
863
864
865
866
867
868static void __exit ehv_bc_exit(void)
869{
870 tty_unregister_driver(ehv_bc_driver);
871 put_tty_driver(ehv_bc_driver);
872 kfree(bcs);
873}
874
875module_init(ehv_bc_init);
876module_exit(ehv_bc_exit);
877
878MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
879MODULE_DESCRIPTION("ePAPR hypervisor byte channel driver");
880MODULE_LICENSE("GPL v2");
881