1 // SPDX-License-Identifier: GPL-2.0
2 /* ePAPR hypervisor byte channel device driver
4 * Copyright 2009-2011 Freescale Semiconductor, Inc.
6 * Author: Timur Tabi <timur@freescale.com>
8 * This driver support three distinct interfaces, all of which are related to
9 * ePAPR hypervisor byte channels.
11 * 1) An early-console (udbg) driver. This provides early console output
12 * through a byte channel. The byte channel handle must be specified in a
15 * 2) A normal console driver. Output is sent to the byte channel designated
16 * for stdout in the device tree. The console driver is for handling kernel
19 * 3) A tty driver, which is used to handle user-space input and output. The
20 * byte channel used for the console is designated as the default tty.
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/interrupt.h>
28 #include <linux/poll.h>
29 #include <asm/epapr_hcalls.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>
40 /* The size of the transmit circular buffer. This must be a power of two. */
43 /* Per-byte channel private data */
51 spinlock_t lock; /* lock for transmit buffer */
52 unsigned char buf[BUF_SIZE]; /* transmit circular buffer */
53 unsigned int head; /* circular buffer head */
54 unsigned int tail; /* circular buffer tail */
56 int tx_irq_enabled; /* true == TX interrupt is enabled */
59 /* Array of byte channel objects */
60 static struct ehv_bc_data *bcs;
62 /* Byte channel handle for stdout (and stdin), taken from device tree */
63 static unsigned int stdout_bc;
65 /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
66 static unsigned int stdout_irq;
68 /**************************** SUPPORT FUNCTIONS ****************************/
71 * Enable the transmit interrupt
73 * Unlike a serial device, byte channels have no mechanism for disabling their
74 * own receive or transmit interrupts. To emulate that feature, we toggle
75 * the IRQ in the kernel.
77 * We cannot just blindly call enable_irq() or disable_irq(), because these
78 * calls are reference counted. This means that we cannot call enable_irq()
79 * if interrupts are already enabled. This can happen in two situations:
81 * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write()
82 * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue()
84 * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
86 static void enable_tx_interrupt(struct ehv_bc_data *bc)
88 if (!bc->tx_irq_enabled) {
89 enable_irq(bc->tx_irq);
90 bc->tx_irq_enabled = 1;
94 static void disable_tx_interrupt(struct ehv_bc_data *bc)
96 if (bc->tx_irq_enabled) {
97 disable_irq_nosync(bc->tx_irq);
98 bc->tx_irq_enabled = 0;
103 * find the byte channel handle to use for the console
105 * The byte channel to be used for the console is specified via a "stdout"
106 * property in the /chosen node.
108 static int find_console_handle(void)
110 struct device_node *np = of_stdout;
111 const uint32_t *iprop;
113 /* We don't care what the aliased node is actually called. We only
114 * care if it's compatible with "epapr,hv-byte-channel", because that
115 * indicates that it's a byte channel node.
117 if (!np || !of_device_is_compatible(np, "epapr,hv-byte-channel"))
120 stdout_irq = irq_of_parse_and_map(np, 0);
122 pr_err("ehv-bc: no 'interrupts' property in %pOF node\n", np);
127 * The 'hv-handle' property contains the handle for this byte channel.
129 iprop = of_get_property(np, "hv-handle", NULL);
131 pr_err("ehv-bc: no 'hv-handle' property in %pOFn node\n",
135 stdout_bc = be32_to_cpu(*iprop);
139 static unsigned int local_ev_byte_channel_send(unsigned int handle,
143 char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
144 unsigned int c = *count;
146 if (c < sizeof(buffer)) {
147 memcpy(buffer, p, c);
148 memset(&buffer[c], 0, sizeof(buffer) - c);
151 return ev_byte_channel_send(handle, count, p);
154 /*************************** EARLY CONSOLE DRIVER ***************************/
156 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
159 * send a byte to a byte channel, wait if necessary
161 * This function sends a byte to a byte channel, and it waits and
162 * retries if the byte channel is full. It returns if the character
163 * has been sent, or if some error has occurred.
166 static void byte_channel_spin_send(const char data)
172 ret = local_ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
174 } while (ret == EV_EAGAIN);
178 * The udbg subsystem calls this function to display a single character.
179 * We convert CR to a CR/LF.
181 static void ehv_bc_udbg_putc(char c)
184 byte_channel_spin_send('\r');
186 byte_channel_spin_send(c);
190 * early console initialization
192 * PowerPC kernels support an early printk console, also known as udbg.
193 * This function must be called via the ppc_md.init_early function pointer.
194 * At this point, the device tree has been unflattened, so we can obtain the
195 * byte channel handle for stdout.
197 * We only support displaying of characters (putc). We do not support
200 void __init udbg_init_ehv_bc(void)
202 unsigned int rx_count, tx_count;
205 /* Verify the byte channel handle */
206 ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
207 &rx_count, &tx_count);
211 udbg_putc = ehv_bc_udbg_putc;
212 register_early_udbg_console();
214 udbg_printf("ehv-bc: early console using byte channel handle %u\n",
215 CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
220 /****************************** CONSOLE DRIVER ******************************/
222 static struct tty_driver *ehv_bc_driver;
225 * Byte channel console sending worker function.
227 * For consoles, if the output buffer is full, we should just spin until it
230 static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
237 len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
239 ret = local_ev_byte_channel_send(handle, &len, s);
240 } while (ret == EV_EAGAIN);
249 * write a string to the console
251 * This function gets called to write a string from the kernel, typically from
252 * a printk(). This function spins until all data is written.
254 * We copy the data to a temporary buffer because we need to insert a \r in
255 * front of every \n. It's more efficient to copy the data to the buffer than
256 * it is to make multiple hcalls for each character or each newline.
258 static void ehv_bc_console_write(struct console *co, const char *s,
261 char s2[EV_BYTE_CHANNEL_MAX_BYTES];
262 unsigned int i, j = 0;
265 for (i = 0; i < count; i++) {
272 if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
273 if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
280 ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
284 * When /dev/console is opened, the kernel iterates the console list looking
285 * for one with ->device and then calls that method. On success, it expects
286 * the passed-in int* to contain the minor number to use.
288 static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
292 return ehv_bc_driver;
295 static struct console ehv_bc_console = {
297 .write = ehv_bc_console_write,
298 .device = ehv_bc_console_device,
299 .flags = CON_PRINTBUFFER | CON_ENABLED,
303 * Console initialization
305 * This is the first function that is called after the device tree is
306 * available, so here is where we determine the byte channel handle and IRQ for
307 * stdout/stdin, even though that information is used by the tty and character
310 static int __init ehv_bc_console_init(void)
312 if (!find_console_handle()) {
313 pr_debug("ehv-bc: stdout is not a byte channel\n");
317 #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
318 /* Print a friendly warning if the user chose the wrong byte channel
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);
326 /* add_preferred_console() must be called before register_console(),
327 otherwise it won't work. However, we don't want to enumerate all the
328 byte channels here, either, since we only care about one. */
330 add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
331 register_console(&ehv_bc_console);
333 pr_info("ehv-bc: registered console driver for byte channel %u\n",
338 console_initcall(ehv_bc_console_init);
340 /******************************** TTY DRIVER ********************************/
343 * byte channel receive interrupt handler
345 * This ISR is called whenever data is available on a byte channel.
347 static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
349 struct ehv_bc_data *bc = data;
350 unsigned int rx_count, tx_count, len;
352 char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
355 /* Find out how much data needs to be read, and then ask the TTY layer
356 * if it can handle that much. We want to ensure that every byte we
357 * read from the byte channel will be accepted by the TTY layer.
359 ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
360 count = tty_buffer_request_room(&bc->port, rx_count);
362 /* 'count' is the maximum amount of data the TTY layer can accept at
363 * this time. However, during testing, I was never able to get 'count'
364 * to be less than 'rx_count'. I'm not sure whether I'm calling it
369 len = min_t(unsigned int, count, sizeof(buffer));
371 /* Read some data from the byte channel. This function will
372 * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
374 ev_byte_channel_receive(bc->handle, &len, buffer);
376 /* 'len' is now the amount of data that's been received. 'len'
377 * can't be zero, and most likely it's equal to one.
380 /* Pass the received data to the tty layer. */
381 ret = tty_insert_flip_string(&bc->port, buffer, len);
383 /* 'ret' is the number of bytes that the TTY layer accepted.
384 * If it's not equal to 'len', then it means the buffer is
385 * full, which should never happen. If it does happen, we can
386 * exit gracefully, but we drop the last 'len - ret' characters
387 * that we read from the byte channel.
395 /* Tell the tty layer that we're done. */
396 tty_flip_buffer_push(&bc->port);
402 * dequeue the transmit buffer to the hypervisor
404 * This function, which can be called in interrupt context, dequeues as much
405 * data as possible from the transmit buffer to the byte channel.
407 static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
410 unsigned int len, ret;
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);
419 ret = local_ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
421 /* 'len' is valid only if the return code is 0 or EV_EAGAIN */
422 if (!ret || (ret == EV_EAGAIN))
423 bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
425 count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
426 spin_unlock_irqrestore(&bc->lock, flags);
427 } while (count && !ret);
429 spin_lock_irqsave(&bc->lock, flags);
430 if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
432 * If we haven't emptied the buffer, then enable the TX IRQ.
433 * We'll get an interrupt when there's more room in the
434 * hypervisor's output buffer.
436 enable_tx_interrupt(bc);
438 disable_tx_interrupt(bc);
439 spin_unlock_irqrestore(&bc->lock, flags);
443 * byte channel transmit interrupt handler
445 * This ISR is called whenever space becomes available for transmitting
446 * characters on a byte channel.
448 static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
450 struct ehv_bc_data *bc = data;
452 ehv_bc_tx_dequeue(bc);
453 tty_port_tty_wakeup(&bc->port);
459 * This function is called when the tty layer has data for us send. We store
460 * the data first in a circular buffer, and then dequeue as much of that data
463 * We don't need to worry about whether there is enough room in the buffer for
464 * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty
465 * layer how much data it can safely send to us. We guarantee that
466 * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
469 static ssize_t ehv_bc_tty_write(struct tty_struct *ttys, const u8 *s,
472 struct ehv_bc_data *bc = ttys->driver_data;
475 unsigned int written = 0;
478 spin_lock_irqsave(&bc->lock, flags);
479 len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
483 memcpy(bc->buf + bc->head, s, len);
484 bc->head = (bc->head + len) & (BUF_SIZE - 1);
486 spin_unlock_irqrestore(&bc->lock, flags);
495 ehv_bc_tx_dequeue(bc);
501 * This function can be called multiple times for a given tty_struct, which is
502 * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
504 * The tty layer will still call this function even if the device was not
505 * registered (i.e. tty_register_device() was not called). This happens
506 * because tty_register_device() is optional and some legacy drivers don't
507 * use it. So we need to check for that.
509 static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
511 struct ehv_bc_data *bc = &bcs[ttys->index];
516 return tty_port_open(&bc->port, ttys, filp);
520 * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
521 * still call this function to close the tty device. So we can't assume that
522 * the tty port has been initialized.
524 static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
526 struct ehv_bc_data *bc = &bcs[ttys->index];
529 tty_port_close(&bc->port, ttys, filp);
533 * Return the amount of space in the output buffer
535 * This is actually a contract between the driver and the tty layer outlining
536 * how much write room the driver can guarantee will be sent OR BUFFERED. This
537 * driver MUST honor the return value.
539 static unsigned int ehv_bc_tty_write_room(struct tty_struct *ttys)
541 struct ehv_bc_data *bc = ttys->driver_data;
545 spin_lock_irqsave(&bc->lock, flags);
546 count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
547 spin_unlock_irqrestore(&bc->lock, flags);
553 * Stop sending data to the tty layer
555 * This function is called when the tty layer's input buffers are getting full,
556 * so the driver should stop sending it data. The easiest way to do this is to
557 * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
560 * The hypervisor will continue to queue up any incoming data. If there is any
561 * data in the queue when the RX interrupt is enabled, we'll immediately get an
564 static void ehv_bc_tty_throttle(struct tty_struct *ttys)
566 struct ehv_bc_data *bc = ttys->driver_data;
568 disable_irq(bc->rx_irq);
572 * Resume sending data to the tty layer
574 * This function is called after previously calling ehv_bc_tty_throttle(). The
575 * tty layer's input buffers now have more room, so the driver can resume
578 static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
580 struct ehv_bc_data *bc = ttys->driver_data;
582 /* If there is any data in the queue when the RX interrupt is enabled,
583 * we'll immediately get an RX interrupt.
585 enable_irq(bc->rx_irq);
588 static void ehv_bc_tty_hangup(struct tty_struct *ttys)
590 struct ehv_bc_data *bc = ttys->driver_data;
592 ehv_bc_tx_dequeue(bc);
593 tty_port_hangup(&bc->port);
597 * TTY driver operations
599 * If we could ask the hypervisor how much data is still in the TX buffer, or
600 * at least how big the TX buffers are, then we could implement the
601 * .wait_until_sent and .chars_in_buffer functions.
603 static 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,
614 * initialize the TTY port
616 * This function will only be called once, no matter how many times
617 * ehv_bc_tty_open() is called. That's why we register the ISR here, and also
618 * why we initialize tty_struct-related variables here.
620 static int ehv_bc_tty_port_activate(struct tty_port *port,
621 struct tty_struct *ttys)
623 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
626 ttys->driver_data = bc;
628 ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
630 dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
635 /* request_irq also enables the IRQ */
636 bc->tx_irq_enabled = 1;
638 ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
640 dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
642 free_irq(bc->rx_irq, bc);
646 /* The TX IRQ is enabled only when we can't write all the data to the
647 * byte channel at once, so by default it's disabled.
649 disable_tx_interrupt(bc);
654 static void ehv_bc_tty_port_shutdown(struct tty_port *port)
656 struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
658 free_irq(bc->tx_irq, bc);
659 free_irq(bc->rx_irq, bc);
662 static const struct tty_port_operations ehv_bc_tty_port_ops = {
663 .activate = ehv_bc_tty_port_activate,
664 .shutdown = ehv_bc_tty_port_shutdown,
667 static int ehv_bc_tty_probe(struct platform_device *pdev)
669 struct device_node *np = pdev->dev.of_node;
670 struct ehv_bc_data *bc;
671 const uint32_t *iprop;
674 static unsigned int index = 1;
677 iprop = of_get_property(np, "hv-handle", NULL);
679 dev_err(&pdev->dev, "no 'hv-handle' property in %pOFn node\n",
684 /* We already told the console layer that the index for the console
685 * device is zero, so we need to make sure that we use that index when
686 * we probe the console byte channel node.
688 handle = be32_to_cpu(*iprop);
689 i = (handle == stdout_bc) ? 0 : index++;
695 spin_lock_init(&bc->lock);
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 || !bc->tx_irq) {
700 dev_err(&pdev->dev, "no 'interrupts' property in %pOFn node\n",
706 tty_port_init(&bc->port);
707 bc->port.ops = &ehv_bc_tty_port_ops;
709 bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i,
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);
717 dev_set_drvdata(&pdev->dev, bc);
719 dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
720 ehv_bc_driver->name, i, bc->handle);
725 tty_port_destroy(&bc->port);
726 irq_dispose_mapping(bc->tx_irq);
727 irq_dispose_mapping(bc->rx_irq);
729 memset(bc, 0, sizeof(struct ehv_bc_data));
733 static const struct of_device_id ehv_bc_tty_of_ids[] = {
734 { .compatible = "epapr,hv-byte-channel" },
738 static struct platform_driver ehv_bc_tty_driver = {
741 .of_match_table = ehv_bc_tty_of_ids,
742 .suppress_bind_attrs = true,
744 .probe = ehv_bc_tty_probe,
748 * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
750 * This function is called when this driver is loaded.
752 static int __init ehv_bc_init(void)
754 struct tty_driver *driver;
755 struct device_node *np;
756 unsigned int count = 0; /* Number of elements in bcs[] */
759 pr_info("ePAPR hypervisor byte channel driver\n");
761 /* Count the number of byte channels */
762 for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
768 /* The array index of an element in bcs[] is the same as the tty index
769 * for that element. If you know the address of an element in the
770 * array, then you can use pointer math (e.g. "bc - bcs") to get its
773 bcs = kcalloc(count, sizeof(struct ehv_bc_data), GFP_KERNEL);
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);
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);
791 ret = tty_register_driver(driver);
793 pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
794 goto err_tty_driver_kref_put;
797 ehv_bc_driver = driver;
799 ret = platform_driver_register(&ehv_bc_tty_driver);
801 pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
803 goto err_deregister_tty_driver;
808 err_deregister_tty_driver:
809 ehv_bc_driver = NULL;
810 tty_unregister_driver(driver);
811 err_tty_driver_kref_put:
812 tty_driver_kref_put(driver);
818 device_initcall(ehv_bc_init);