^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* ePAPR hypervisor byte channel device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright 2009-2011 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Timur Tabi <timur@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This driver support three distinct interfaces, all of which are related to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * ePAPR hypervisor byte channels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * 1) An early-console (udbg) driver. This provides early console output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * through a byte channel. The byte channel handle must be specified in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Kconfig option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * 2) A normal console driver. Output is sent to the byte channel designated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * for stdout in the device tree. The console driver is for handling kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * printk calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * 3) A tty driver, which is used to handle user-space input and output. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * byte channel used for the console is designated as the default tty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/epapr_hcalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/of_irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/console.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <linux/circ_buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <asm/udbg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* The size of the transmit circular buffer. This must be a power of two. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define BUF_SIZE 2048
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* Per-byte channel private data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct ehv_bc_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct tty_port port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) uint32_t handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int rx_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) unsigned int tx_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) spinlock_t lock; /* lock for transmit buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned char buf[BUF_SIZE]; /* transmit circular buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int head; /* circular buffer head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int tail; /* circular buffer tail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int tx_irq_enabled; /* true == TX interrupt is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Array of byte channel objects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static struct ehv_bc_data *bcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Byte channel handle for stdout (and stdin), taken from device tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static unsigned int stdout_bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* Virtual IRQ for the byte channel handle for stdin, taken from device tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static unsigned int stdout_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /**************************** SUPPORT FUNCTIONS ****************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * Enable the transmit interrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * Unlike a serial device, byte channels have no mechanism for disabling their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * own receive or transmit interrupts. To emulate that feature, we toggle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * the IRQ in the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * We cannot just blindly call enable_irq() or disable_irq(), because these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * calls are reference counted. This means that we cannot call enable_irq()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * if interrupts are already enabled. This can happen in two situations:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * 1. The tty layer makes two back-to-back calls to ehv_bc_tty_write()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * 2. A transmit interrupt occurs while executing ehv_bc_tx_dequeue()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * To work around this, we keep a flag to tell us if the IRQ is enabled or not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void enable_tx_interrupt(struct ehv_bc_data *bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (!bc->tx_irq_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) enable_irq(bc->tx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) bc->tx_irq_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static void disable_tx_interrupt(struct ehv_bc_data *bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (bc->tx_irq_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) disable_irq_nosync(bc->tx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) bc->tx_irq_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * find the byte channel handle to use for the console
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * The byte channel to be used for the console is specified via a "stdout"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * property in the /chosen node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int find_console_handle(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct device_node *np = of_stdout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) const uint32_t *iprop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* We don't care what the aliased node is actually called. We only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * care if it's compatible with "epapr,hv-byte-channel", because that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * indicates that it's a byte channel node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!np || !of_device_is_compatible(np, "epapr,hv-byte-channel"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) stdout_irq = irq_of_parse_and_map(np, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (stdout_irq == NO_IRQ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) pr_err("ehv-bc: no 'interrupts' property in %pOF node\n", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * The 'hv-handle' property contains the handle for this byte channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) iprop = of_get_property(np, "hv-handle", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (!iprop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pr_err("ehv-bc: no 'hv-handle' property in %pOFn node\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) stdout_bc = be32_to_cpu(*iprop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static unsigned int local_ev_byte_channel_send(unsigned int handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int *count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) const char *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned int c = *count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (c < sizeof(buffer)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) memcpy(buffer, p, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) memset(&buffer[c], 0, sizeof(buffer) - c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) p = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return ev_byte_channel_send(handle, count, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*************************** EARLY CONSOLE DRIVER ***************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * send a byte to a byte channel, wait if necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * This function sends a byte to a byte channel, and it waits and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * retries if the byte channel is full. It returns if the character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * has been sent, or if some error has occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void byte_channel_spin_send(const char data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int ret, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) ret = local_ev_byte_channel_send(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) &count, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) } while (ret == EV_EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * The udbg subsystem calls this function to display a single character.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * We convert CR to a CR/LF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static void ehv_bc_udbg_putc(char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (c == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) byte_channel_spin_send('\r');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) byte_channel_spin_send(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * early console initialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * PowerPC kernels support an early printk console, also known as udbg.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * This function must be called via the ppc_md.init_early function pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * At this point, the device tree has been unflattened, so we can obtain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * byte channel handle for stdout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * We only support displaying of characters (putc). We do not support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * keyboard input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void __init udbg_init_ehv_bc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned int rx_count, tx_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) unsigned int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Verify the byte channel handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) &rx_count, &tx_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) udbg_putc = ehv_bc_udbg_putc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) register_early_udbg_console();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) udbg_printf("ehv-bc: early console using byte channel handle %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /****************************** CONSOLE DRIVER ******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static struct tty_driver *ehv_bc_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * Byte channel console sending worker function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * For consoles, if the output buffer is full, we should just spin until it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * clears.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) ret = local_ev_byte_channel_send(handle, &len, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) } while (ret == EV_EAGAIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) count -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) s += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * write a string to the console
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * This function gets called to write a string from the kernel, typically from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * a printk(). This function spins until all data is written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * We copy the data to a temporary buffer because we need to insert a \r in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * front of every \n. It's more efficient to copy the data to the buffer than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * it is to make multiple hcalls for each character or each newline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static void ehv_bc_console_write(struct console *co, const char *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) char s2[EV_BYTE_CHANNEL_MAX_BYTES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) unsigned int i, j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) c = *s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (c == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) s2[j++] = '\r';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) s2[j++] = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (ehv_bc_console_byte_channel_send(stdout_bc, s2, j))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) j = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (j)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) ehv_bc_console_byte_channel_send(stdout_bc, s2, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * When /dev/console is opened, the kernel iterates the console list looking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * for one with ->device and then calls that method. On success, it expects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * the passed-in int* to contain the minor number to use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static struct tty_driver *ehv_bc_console_device(struct console *co, int *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *index = co->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return ehv_bc_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static struct console ehv_bc_console = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) .name = "ttyEHV",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) .write = ehv_bc_console_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) .device = ehv_bc_console_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) .flags = CON_PRINTBUFFER | CON_ENABLED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * Console initialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * This is the first function that is called after the device tree is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * available, so here is where we determine the byte channel handle and IRQ for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * stdout/stdin, even though that information is used by the tty and character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * drivers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int __init ehv_bc_console_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!find_console_handle()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) pr_debug("ehv-bc: stdout is not a byte channel\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* Print a friendly warning if the user chose the wrong byte channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * handle for udbg.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) pr_warn("ehv-bc: udbg handle %u is not the stdout handle\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* add_preferred_console() must be called before register_console(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) otherwise it won't work. However, we don't want to enumerate all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) byte channels here, either, since we only care about one. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) register_console(&ehv_bc_console);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) pr_info("ehv-bc: registered console driver for byte channel %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) stdout_bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) console_initcall(ehv_bc_console_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /******************************** TTY DRIVER ********************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * byte channel receive interrupt handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * This ISR is called whenever data is available on a byte channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) struct ehv_bc_data *bc = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) unsigned int rx_count, tx_count, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) char buffer[EV_BYTE_CHANNEL_MAX_BYTES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /* Find out how much data needs to be read, and then ask the TTY layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * if it can handle that much. We want to ensure that every byte we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) * read from the byte channel will be accepted by the TTY layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) ev_byte_channel_poll(bc->handle, &rx_count, &tx_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) count = tty_buffer_request_room(&bc->port, rx_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) /* 'count' is the maximum amount of data the TTY layer can accept at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * this time. However, during testing, I was never able to get 'count'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * to be less than 'rx_count'. I'm not sure whether I'm calling it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) while (count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) len = min_t(unsigned int, count, sizeof(buffer));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* Read some data from the byte channel. This function will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ev_byte_channel_receive(bc->handle, &len, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /* 'len' is now the amount of data that's been received. 'len'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * can't be zero, and most likely it's equal to one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* Pass the received data to the tty layer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ret = tty_insert_flip_string(&bc->port, buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) /* 'ret' is the number of bytes that the TTY layer accepted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * If it's not equal to 'len', then it means the buffer is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) * full, which should never happen. If it does happen, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * exit gracefully, but we drop the last 'len - ret' characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * that we read from the byte channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (ret != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) count -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) /* Tell the tty layer that we're done. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) tty_flip_buffer_push(&bc->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * dequeue the transmit buffer to the hypervisor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * This function, which can be called in interrupt context, dequeues as much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * data as possible from the transmit buffer to the byte channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) unsigned int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) unsigned int len, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) spin_lock_irqsave(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) len = min_t(unsigned int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) EV_BYTE_CHANNEL_MAX_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) ret = local_ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* 'len' is valid only if the return code is 0 or EV_EAGAIN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (!ret || (ret == EV_EAGAIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) bc->tail = (bc->tail + len) & (BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) spin_unlock_irqrestore(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) } while (count && !ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) spin_lock_irqsave(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * If we haven't emptied the buffer, then enable the TX IRQ.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * We'll get an interrupt when there's more room in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * hypervisor's output buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) enable_tx_interrupt(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) disable_tx_interrupt(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) spin_unlock_irqrestore(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * byte channel transmit interrupt handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * This ISR is called whenever space becomes available for transmitting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * characters on a byte channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct ehv_bc_data *bc = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) ehv_bc_tx_dequeue(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) tty_port_tty_wakeup(&bc->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * This function is called when the tty layer has data for us send. We store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * the data first in a circular buffer, and then dequeue as much of that data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * as possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * We don't need to worry about whether there is enough room in the buffer for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * layer how much data it can safely send to us. We guarantee that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * too much data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct ehv_bc_data *bc = ttys->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) unsigned int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) unsigned int written = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) spin_lock_irqsave(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (count < len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) len = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) memcpy(bc->buf + bc->head, s, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) bc->head = (bc->head + len) & (BUF_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) spin_unlock_irqrestore(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) s += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) count -= len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) written += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) ehv_bc_tx_dequeue(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * This function can be called multiple times for a given tty_struct, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * The tty layer will still call this function even if the device was not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * registered (i.e. tty_register_device() was not called). This happens
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * because tty_register_device() is optional and some legacy drivers don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * use it. So we need to check for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) struct ehv_bc_data *bc = &bcs[ttys->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (!bc->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) return tty_port_open(&bc->port, ttys, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) * still call this function to close the tty device. So we can't assume that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) * the tty port has been initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) struct ehv_bc_data *bc = &bcs[ttys->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (bc->dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) tty_port_close(&bc->port, ttys, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * Return the amount of space in the output buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * This is actually a contract between the driver and the tty layer outlining
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * how much write room the driver can guarantee will be sent OR BUFFERED. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * driver MUST honor the return value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) static int ehv_bc_tty_write_room(struct tty_struct *ttys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) struct ehv_bc_data *bc = ttys->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) spin_lock_irqsave(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) spin_unlock_irqrestore(&bc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * Stop sending data to the tty layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * This function is called when the tty layer's input buffers are getting full,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * so the driver should stop sending it data. The easiest way to do this is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * The hypervisor will continue to queue up any incoming data. If there is any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * data in the queue when the RX interrupt is enabled, we'll immediately get an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * RX interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) static void ehv_bc_tty_throttle(struct tty_struct *ttys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) struct ehv_bc_data *bc = ttys->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) disable_irq(bc->rx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) * Resume sending data to the tty layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * This function is called after previously calling ehv_bc_tty_throttle(). The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * tty layer's input buffers now have more room, so the driver can resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * sending it data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) static void ehv_bc_tty_unthrottle(struct tty_struct *ttys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) struct ehv_bc_data *bc = ttys->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) /* If there is any data in the queue when the RX interrupt is enabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * we'll immediately get an RX interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) enable_irq(bc->rx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) static void ehv_bc_tty_hangup(struct tty_struct *ttys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) struct ehv_bc_data *bc = ttys->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) ehv_bc_tx_dequeue(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) tty_port_hangup(&bc->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) * TTY driver operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) * If we could ask the hypervisor how much data is still in the TX buffer, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) * at least how big the TX buffers are, then we could implement the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * .wait_until_sent and .chars_in_buffer functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) static const struct tty_operations ehv_bc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) .open = ehv_bc_tty_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) .close = ehv_bc_tty_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) .write = ehv_bc_tty_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) .write_room = ehv_bc_tty_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) .throttle = ehv_bc_tty_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) .unthrottle = ehv_bc_tty_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) .hangup = ehv_bc_tty_hangup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) * initialize the TTY port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * This function will only be called once, no matter how many times
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * ehv_bc_tty_open() is called. That's why we register the ISR here, and also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * why we initialize tty_struct-related variables here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static int ehv_bc_tty_port_activate(struct tty_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) struct tty_struct *ttys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) ttys->driver_data = bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) bc->rx_irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /* request_irq also enables the IRQ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) bc->tx_irq_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) bc->tx_irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) free_irq(bc->rx_irq, bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) /* The TX IRQ is enabled only when we can't write all the data to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * byte channel at once, so by default it's disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) disable_tx_interrupt(bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) static void ehv_bc_tty_port_shutdown(struct tty_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) free_irq(bc->tx_irq, bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) free_irq(bc->rx_irq, bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) static const struct tty_port_operations ehv_bc_tty_port_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) .activate = ehv_bc_tty_port_activate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) .shutdown = ehv_bc_tty_port_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static int ehv_bc_tty_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) struct ehv_bc_data *bc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) const uint32_t *iprop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) unsigned int handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) static unsigned int index = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) iprop = of_get_property(np, "hv-handle", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) if (!iprop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) dev_err(&pdev->dev, "no 'hv-handle' property in %pOFn node\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /* We already told the console layer that the index for the console
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * device is zero, so we need to make sure that we use that index when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * we probe the console byte channel node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) handle = be32_to_cpu(*iprop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) i = (handle == stdout_bc) ? 0 : index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) bc = &bcs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) bc->handle = handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) bc->head = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) bc->tail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) spin_lock_init(&bc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) bc->rx_irq = irq_of_parse_and_map(np, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) bc->tx_irq = irq_of_parse_and_map(np, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) dev_err(&pdev->dev, "no 'interrupts' property in %pOFn node\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) tty_port_init(&bc->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) bc->port.ops = &ehv_bc_tty_port_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) bc->dev = tty_port_register_device(&bc->port, ehv_bc_driver, i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) &pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (IS_ERR(bc->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) ret = PTR_ERR(bc->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) dev_set_drvdata(&pdev->dev, bc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) ehv_bc_driver->name, i, bc->handle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) tty_port_destroy(&bc->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) irq_dispose_mapping(bc->tx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) irq_dispose_mapping(bc->rx_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) memset(bc, 0, sizeof(struct ehv_bc_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) static const struct of_device_id ehv_bc_tty_of_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) { .compatible = "epapr,hv-byte-channel" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) static struct platform_driver ehv_bc_tty_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) .name = "ehv-bc",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) .of_match_table = ehv_bc_tty_of_ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) .suppress_bind_attrs = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) .probe = ehv_bc_tty_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) * ehv_bc_init - ePAPR hypervisor byte channel driver initialization
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) * This function is called when this driver is loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) static int __init ehv_bc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) unsigned int count = 0; /* Number of elements in bcs[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) pr_info("ePAPR hypervisor byte channel driver\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) /* Count the number of byte channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) for_each_compatible_node(np, NULL, "epapr,hv-byte-channel")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) /* The array index of an element in bcs[] is the same as the tty index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) * for that element. If you know the address of an element in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * array, then you can use pointer math (e.g. "bc - bcs") to get its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * tty index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) bcs = kcalloc(count, sizeof(struct ehv_bc_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (!bcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) ehv_bc_driver = alloc_tty_driver(count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (!ehv_bc_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) goto err_free_bcs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) ehv_bc_driver->driver_name = "ehv-bc";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) ehv_bc_driver->name = ehv_bc_console.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) ehv_bc_driver->init_termios = tty_std_termios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) tty_set_operations(ehv_bc_driver, &ehv_bc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) ret = tty_register_driver(ehv_bc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) goto err_put_tty_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) ret = platform_driver_register(&ehv_bc_tty_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) pr_err("ehv-bc: could not register platform driver (ret=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) goto err_deregister_tty_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) err_deregister_tty_driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) tty_unregister_driver(ehv_bc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) err_put_tty_driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) put_tty_driver(ehv_bc_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) err_free_bcs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) kfree(bcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) device_initcall(ehv_bc_init);