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