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