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