Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tty/powerpc: introduce the ePAPR embedded hypervisor byte channel driver

The ePAPR embedded hypervisor specification provides an API for "byte
channels", which are serial-like virtual devices for sending and receiving
streams of bytes. This driver provides Linux kernel support for byte
channels via three distinct interfaces:

1) An early-console (udbg) driver. This provides early console output
through a byte channel. The byte channel handle must be specified in a
Kconfig option.

2) A normal console driver. Output is sent to the byte channel designated
for stdout in the device tree. The console driver is for handling kernel
printk calls.

3) A tty driver, which is used to handle user-space input and output. The
byte channel used for the console is designated as the default tty.

Signed-off-by: Timur Tabi <timur@freescale.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Timur Tabi and committed by
Greg Kroah-Hartman
dcd83aaf fcb8ce5c

+926
+1
arch/powerpc/include/asm/udbg.h
··· 54 54 extern void __init udbg_init_cpm(void); 55 55 extern void __init udbg_init_usbgecko(void); 56 56 extern void __init udbg_init_wsp(void); 57 + extern void __init udbg_init_ehv_bc(void); 57 58 58 59 #endif /* __KERNEL__ */ 59 60 #endif /* _ASM_POWERPC_UDBG_H */
+2
arch/powerpc/kernel/udbg.c
··· 67 67 udbg_init_usbgecko(); 68 68 #elif defined(CONFIG_PPC_EARLY_DEBUG_WSP) 69 69 udbg_init_wsp(); 70 + #elif defined(CONFIG_PPC_EARLY_DEBUG_EHV_BC) 71 + udbg_init_ehv_bc(); 70 72 #endif 71 73 72 74 #ifdef CONFIG_PPC_EARLY_DEBUG
+34
drivers/tty/Kconfig
··· 350 350 351 351 If you select this option, you need to select 352 352 "Trace data router for MIPI P1149.7 cJTAG standard". 353 + 354 + config PPC_EPAPR_HV_BYTECHAN 355 + tristate "ePAPR hypervisor byte channel driver" 356 + depends on PPC 357 + help 358 + This driver creates /dev entries for each ePAPR hypervisor byte 359 + channel, thereby allowing applications to communicate with byte 360 + channels as if they were serial ports. 361 + 362 + config PPC_EARLY_DEBUG_EHV_BC 363 + bool "Early console (udbg) support for ePAPR hypervisors" 364 + depends on PPC_EPAPR_HV_BYTECHAN 365 + help 366 + Select this option to enable early console (a.k.a. "udbg") support 367 + via an ePAPR byte channel. You also need to choose the byte channel 368 + handle below. 369 + 370 + config PPC_EARLY_DEBUG_EHV_BC_HANDLE 371 + int "Byte channel handle for early console (udbg)" 372 + depends on PPC_EARLY_DEBUG_EHV_BC 373 + default 0 374 + help 375 + If you want early console (udbg) output through a byte channel, 376 + specify the handle of the byte channel to use. 377 + 378 + For this to work, the byte channel driver must be compiled 379 + in-kernel, not as a module. 380 + 381 + Note that only one early console driver can be enabled, so don't 382 + enable any others if you enable this one. 383 + 384 + If the number you specify is not a valid byte channel handle, then 385 + there simply will be no early console output. This is true also 386 + if you don't boot under a hypervisor at all.
+1
drivers/tty/Makefile
··· 26 26 obj-$(CONFIG_SYNCLINK_GT) += synclink_gt.o 27 27 obj-$(CONFIG_SYNCLINKMP) += synclinkmp.o 28 28 obj-$(CONFIG_SYNCLINK) += synclink.o 29 + obj-$(CONFIG_PPC_EPAPR_HV_BYTECHAN) += ehv_bytechan.o 29 30 30 31 obj-y += ipwireless/
+888
drivers/tty/ehv_bytechan.c
··· 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 + 229 + /* Check if we're running as a guest of a hypervisor */ 230 + if (!(mfmsr() & MSR_GS)) 231 + return; 232 + 233 + /* Verify the byte channel handle */ 234 + ret = ev_byte_channel_poll(CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE, 235 + &rx_count, &tx_count); 236 + if (ret) 237 + return; 238 + 239 + udbg_putc = ehv_bc_udbg_putc; 240 + register_early_udbg_console(); 241 + 242 + udbg_printf("ehv-bc: early console using byte channel handle %u\n", 243 + CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE); 244 + } 245 + 246 + #endif 247 + 248 + /****************************** CONSOLE DRIVER ******************************/ 249 + 250 + static struct tty_driver *ehv_bc_driver; 251 + 252 + /* 253 + * Byte channel console sending worker function. 254 + * 255 + * For consoles, if the output buffer is full, we should just spin until it 256 + * clears. 257 + */ 258 + static int ehv_bc_console_byte_channel_send(unsigned int handle, const char *s, 259 + unsigned int count) 260 + { 261 + unsigned int len; 262 + int ret = 0; 263 + 264 + while (count) { 265 + len = min_t(unsigned int, count, EV_BYTE_CHANNEL_MAX_BYTES); 266 + do { 267 + ret = ev_byte_channel_send(handle, &len, s); 268 + } while (ret == EV_EAGAIN); 269 + count -= len; 270 + s += len; 271 + } 272 + 273 + return ret; 274 + } 275 + 276 + /* 277 + * write a string to the console 278 + * 279 + * This function gets called to write a string from the kernel, typically from 280 + * a printk(). This function spins until all data is written. 281 + * 282 + * We copy the data to a temporary buffer because we need to insert a \r in 283 + * front of every \n. It's more efficient to copy the data to the buffer than 284 + * it is to make multiple hcalls for each character or each newline. 285 + */ 286 + static void ehv_bc_console_write(struct console *co, const char *s, 287 + unsigned int count) 288 + { 289 + unsigned int handle = (unsigned int)co->data; 290 + char s2[EV_BYTE_CHANNEL_MAX_BYTES]; 291 + unsigned int i, j = 0; 292 + char c; 293 + 294 + for (i = 0; i < count; i++) { 295 + c = *s++; 296 + 297 + if (c == '\n') 298 + s2[j++] = '\r'; 299 + 300 + s2[j++] = c; 301 + if (j >= (EV_BYTE_CHANNEL_MAX_BYTES - 1)) { 302 + if (ehv_bc_console_byte_channel_send(handle, s2, j)) 303 + return; 304 + j = 0; 305 + } 306 + } 307 + 308 + if (j) 309 + ehv_bc_console_byte_channel_send(handle, s2, j); 310 + } 311 + 312 + /* 313 + * When /dev/console is opened, the kernel iterates the console list looking 314 + * for one with ->device and then calls that method. On success, it expects 315 + * the passed-in int* to contain the minor number to use. 316 + */ 317 + static struct tty_driver *ehv_bc_console_device(struct console *co, int *index) 318 + { 319 + *index = co->index; 320 + 321 + return ehv_bc_driver; 322 + } 323 + 324 + static struct console ehv_bc_console = { 325 + .name = "ttyEHV", 326 + .write = ehv_bc_console_write, 327 + .device = ehv_bc_console_device, 328 + .flags = CON_PRINTBUFFER | CON_ENABLED, 329 + }; 330 + 331 + /* 332 + * Console initialization 333 + * 334 + * This is the first function that is called after the device tree is 335 + * available, so here is where we determine the byte channel handle and IRQ for 336 + * stdout/stdin, even though that information is used by the tty and character 337 + * drivers. 338 + */ 339 + static int __init ehv_bc_console_init(void) 340 + { 341 + if (!find_console_handle()) { 342 + pr_debug("ehv-bc: stdout is not a byte channel\n"); 343 + return -ENODEV; 344 + } 345 + 346 + #ifdef CONFIG_PPC_EARLY_DEBUG_EHV_BC 347 + /* Print a friendly warning if the user chose the wrong byte channel 348 + * handle for udbg. 349 + */ 350 + if (stdout_bc != CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE) 351 + pr_warning("ehv-bc: udbg handle %u is not the stdout handle\n", 352 + CONFIG_PPC_EARLY_DEBUG_EHV_BC_HANDLE); 353 + #endif 354 + 355 + ehv_bc_console.data = (void *)stdout_bc; 356 + 357 + /* add_preferred_console() must be called before register_console(), 358 + otherwise it won't work. However, we don't want to enumerate all the 359 + byte channels here, either, since we only care about one. */ 360 + 361 + add_preferred_console(ehv_bc_console.name, ehv_bc_console.index, NULL); 362 + register_console(&ehv_bc_console); 363 + 364 + pr_info("ehv-bc: registered console driver for byte channel %u\n", 365 + stdout_bc); 366 + 367 + return 0; 368 + } 369 + console_initcall(ehv_bc_console_init); 370 + 371 + /******************************** TTY DRIVER ********************************/ 372 + 373 + /* 374 + * byte channel receive interupt handler 375 + * 376 + * This ISR is called whenever data is available on a byte channel. 377 + */ 378 + static irqreturn_t ehv_bc_tty_rx_isr(int irq, void *data) 379 + { 380 + struct ehv_bc_data *bc = data; 381 + struct tty_struct *ttys = tty_port_tty_get(&bc->port); 382 + unsigned int rx_count, tx_count, len; 383 + int count; 384 + char buffer[EV_BYTE_CHANNEL_MAX_BYTES]; 385 + int ret; 386 + 387 + /* ttys could be NULL during a hangup */ 388 + if (!ttys) 389 + return IRQ_HANDLED; 390 + 391 + /* Find out how much data needs to be read, and then ask the TTY layer 392 + * if it can handle that much. We want to ensure that every byte we 393 + * read from the byte channel will be accepted by the TTY layer. 394 + */ 395 + ev_byte_channel_poll(bc->handle, &rx_count, &tx_count); 396 + count = tty_buffer_request_room(ttys, rx_count); 397 + 398 + /* 'count' is the maximum amount of data the TTY layer can accept at 399 + * this time. However, during testing, I was never able to get 'count' 400 + * to be less than 'rx_count'. I'm not sure whether I'm calling it 401 + * correctly. 402 + */ 403 + 404 + while (count > 0) { 405 + len = min_t(unsigned int, count, sizeof(buffer)); 406 + 407 + /* Read some data from the byte channel. This function will 408 + * never return more than EV_BYTE_CHANNEL_MAX_BYTES bytes. 409 + */ 410 + ev_byte_channel_receive(bc->handle, &len, buffer); 411 + 412 + /* 'len' is now the amount of data that's been received. 'len' 413 + * can't be zero, and most likely it's equal to one. 414 + */ 415 + 416 + /* Pass the received data to the tty layer. */ 417 + ret = tty_insert_flip_string(ttys, buffer, len); 418 + 419 + /* 'ret' is the number of bytes that the TTY layer accepted. 420 + * If it's not equal to 'len', then it means the buffer is 421 + * full, which should never happen. If it does happen, we can 422 + * exit gracefully, but we drop the last 'len - ret' characters 423 + * that we read from the byte channel. 424 + */ 425 + if (ret != len) 426 + break; 427 + 428 + count -= len; 429 + } 430 + 431 + /* Tell the tty layer that we're done. */ 432 + tty_flip_buffer_push(ttys); 433 + 434 + tty_kref_put(ttys); 435 + 436 + return IRQ_HANDLED; 437 + } 438 + 439 + /* 440 + * dequeue the transmit buffer to the hypervisor 441 + * 442 + * This function, which can be called in interrupt context, dequeues as much 443 + * data as possible from the transmit buffer to the byte channel. 444 + */ 445 + static void ehv_bc_tx_dequeue(struct ehv_bc_data *bc) 446 + { 447 + unsigned int count; 448 + unsigned int len, ret; 449 + unsigned long flags; 450 + 451 + do { 452 + spin_lock_irqsave(&bc->lock, flags); 453 + len = min_t(unsigned int, 454 + CIRC_CNT_TO_END(bc->head, bc->tail, BUF_SIZE), 455 + EV_BYTE_CHANNEL_MAX_BYTES); 456 + 457 + ret = ev_byte_channel_send(bc->handle, &len, bc->buf + bc->tail); 458 + 459 + /* 'len' is valid only if the return code is 0 or EV_EAGAIN */ 460 + if (!ret || (ret == EV_EAGAIN)) 461 + bc->tail = (bc->tail + len) & (BUF_SIZE - 1); 462 + 463 + count = CIRC_CNT(bc->head, bc->tail, BUF_SIZE); 464 + spin_unlock_irqrestore(&bc->lock, flags); 465 + } while (count && !ret); 466 + 467 + spin_lock_irqsave(&bc->lock, flags); 468 + if (CIRC_CNT(bc->head, bc->tail, BUF_SIZE)) 469 + /* 470 + * If we haven't emptied the buffer, then enable the TX IRQ. 471 + * We'll get an interrupt when there's more room in the 472 + * hypervisor's output buffer. 473 + */ 474 + enable_tx_interrupt(bc); 475 + else 476 + disable_tx_interrupt(bc); 477 + spin_unlock_irqrestore(&bc->lock, flags); 478 + } 479 + 480 + /* 481 + * byte channel transmit interupt handler 482 + * 483 + * This ISR is called whenever space becomes available for transmitting 484 + * characters on a byte channel. 485 + */ 486 + static irqreturn_t ehv_bc_tty_tx_isr(int irq, void *data) 487 + { 488 + struct ehv_bc_data *bc = data; 489 + struct tty_struct *ttys = tty_port_tty_get(&bc->port); 490 + 491 + ehv_bc_tx_dequeue(bc); 492 + if (ttys) { 493 + tty_wakeup(ttys); 494 + tty_kref_put(ttys); 495 + } 496 + 497 + return IRQ_HANDLED; 498 + } 499 + 500 + /* 501 + * This function is called when the tty layer has data for us send. We store 502 + * the data first in a circular buffer, and then dequeue as much of that data 503 + * as possible. 504 + * 505 + * We don't need to worry about whether there is enough room in the buffer for 506 + * all the data. The purpose of ehv_bc_tty_write_room() is to tell the tty 507 + * layer how much data it can safely send to us. We guarantee that 508 + * ehv_bc_tty_write_room() will never lie, so the tty layer will never send us 509 + * too much data. 510 + */ 511 + static int ehv_bc_tty_write(struct tty_struct *ttys, const unsigned char *s, 512 + int count) 513 + { 514 + struct ehv_bc_data *bc = ttys->driver_data; 515 + unsigned long flags; 516 + unsigned int len; 517 + unsigned int written = 0; 518 + 519 + while (1) { 520 + spin_lock_irqsave(&bc->lock, flags); 521 + len = CIRC_SPACE_TO_END(bc->head, bc->tail, BUF_SIZE); 522 + if (count < len) 523 + len = count; 524 + if (len) { 525 + memcpy(bc->buf + bc->head, s, len); 526 + bc->head = (bc->head + len) & (BUF_SIZE - 1); 527 + } 528 + spin_unlock_irqrestore(&bc->lock, flags); 529 + if (!len) 530 + break; 531 + 532 + s += len; 533 + count -= len; 534 + written += len; 535 + } 536 + 537 + ehv_bc_tx_dequeue(bc); 538 + 539 + return written; 540 + } 541 + 542 + /* 543 + * This function can be called multiple times for a given tty_struct, which is 544 + * why we initialize bc->ttys in ehv_bc_tty_port_activate() instead. 545 + * 546 + * The tty layer will still call this function even if the device was not 547 + * registered (i.e. tty_register_device() was not called). This happens 548 + * because tty_register_device() is optional and some legacy drivers don't 549 + * use it. So we need to check for that. 550 + */ 551 + static int ehv_bc_tty_open(struct tty_struct *ttys, struct file *filp) 552 + { 553 + struct ehv_bc_data *bc = &bcs[ttys->index]; 554 + 555 + if (!bc->dev) 556 + return -ENODEV; 557 + 558 + return tty_port_open(&bc->port, ttys, filp); 559 + } 560 + 561 + /* 562 + * Amazingly, if ehv_bc_tty_open() returns an error code, the tty layer will 563 + * still call this function to close the tty device. So we can't assume that 564 + * the tty port has been initialized. 565 + */ 566 + static void ehv_bc_tty_close(struct tty_struct *ttys, struct file *filp) 567 + { 568 + struct ehv_bc_data *bc = &bcs[ttys->index]; 569 + 570 + if (bc->dev) 571 + tty_port_close(&bc->port, ttys, filp); 572 + } 573 + 574 + /* 575 + * Return the amount of space in the output buffer 576 + * 577 + * This is actually a contract between the driver and the tty layer outlining 578 + * how much write room the driver can guarantee will be sent OR BUFFERED. This 579 + * driver MUST honor the return value. 580 + */ 581 + static int ehv_bc_tty_write_room(struct tty_struct *ttys) 582 + { 583 + struct ehv_bc_data *bc = ttys->driver_data; 584 + unsigned long flags; 585 + int count; 586 + 587 + spin_lock_irqsave(&bc->lock, flags); 588 + count = CIRC_SPACE(bc->head, bc->tail, BUF_SIZE); 589 + spin_unlock_irqrestore(&bc->lock, flags); 590 + 591 + return count; 592 + } 593 + 594 + /* 595 + * Stop sending data to the tty layer 596 + * 597 + * This function is called when the tty layer's input buffers are getting full, 598 + * so the driver should stop sending it data. The easiest way to do this is to 599 + * disable the RX IRQ, which will prevent ehv_bc_tty_rx_isr() from being 600 + * called. 601 + * 602 + * The hypervisor will continue to queue up any incoming data. If there is any 603 + * data in the queue when the RX interrupt is enabled, we'll immediately get an 604 + * RX interrupt. 605 + */ 606 + static void ehv_bc_tty_throttle(struct tty_struct *ttys) 607 + { 608 + struct ehv_bc_data *bc = ttys->driver_data; 609 + 610 + disable_irq(bc->rx_irq); 611 + } 612 + 613 + /* 614 + * Resume sending data to the tty layer 615 + * 616 + * This function is called after previously calling ehv_bc_tty_throttle(). The 617 + * tty layer's input buffers now have more room, so the driver can resume 618 + * sending it data. 619 + */ 620 + static void ehv_bc_tty_unthrottle(struct tty_struct *ttys) 621 + { 622 + struct ehv_bc_data *bc = ttys->driver_data; 623 + 624 + /* If there is any data in the queue when the RX interrupt is enabled, 625 + * we'll immediately get an RX interrupt. 626 + */ 627 + enable_irq(bc->rx_irq); 628 + } 629 + 630 + static void ehv_bc_tty_hangup(struct tty_struct *ttys) 631 + { 632 + struct ehv_bc_data *bc = ttys->driver_data; 633 + 634 + ehv_bc_tx_dequeue(bc); 635 + tty_port_hangup(&bc->port); 636 + } 637 + 638 + /* 639 + * TTY driver operations 640 + * 641 + * If we could ask the hypervisor how much data is still in the TX buffer, or 642 + * at least how big the TX buffers are, then we could implement the 643 + * .wait_until_sent and .chars_in_buffer functions. 644 + */ 645 + static const struct tty_operations ehv_bc_ops = { 646 + .open = ehv_bc_tty_open, 647 + .close = ehv_bc_tty_close, 648 + .write = ehv_bc_tty_write, 649 + .write_room = ehv_bc_tty_write_room, 650 + .throttle = ehv_bc_tty_throttle, 651 + .unthrottle = ehv_bc_tty_unthrottle, 652 + .hangup = ehv_bc_tty_hangup, 653 + }; 654 + 655 + /* 656 + * initialize the TTY port 657 + * 658 + * This function will only be called once, no matter how many times 659 + * ehv_bc_tty_open() is called. That's why we register the ISR here, and also 660 + * why we initialize tty_struct-related variables here. 661 + */ 662 + static int ehv_bc_tty_port_activate(struct tty_port *port, 663 + struct tty_struct *ttys) 664 + { 665 + struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port); 666 + int ret; 667 + 668 + ttys->driver_data = bc; 669 + 670 + ret = request_irq(bc->rx_irq, ehv_bc_tty_rx_isr, 0, "ehv-bc", bc); 671 + if (ret < 0) { 672 + dev_err(bc->dev, "could not request rx irq %u (ret=%i)\n", 673 + bc->rx_irq, ret); 674 + return ret; 675 + } 676 + 677 + /* request_irq also enables the IRQ */ 678 + bc->tx_irq_enabled = 1; 679 + 680 + ret = request_irq(bc->tx_irq, ehv_bc_tty_tx_isr, 0, "ehv-bc", bc); 681 + if (ret < 0) { 682 + dev_err(bc->dev, "could not request tx irq %u (ret=%i)\n", 683 + bc->tx_irq, ret); 684 + free_irq(bc->rx_irq, bc); 685 + return ret; 686 + } 687 + 688 + /* The TX IRQ is enabled only when we can't write all the data to the 689 + * byte channel at once, so by default it's disabled. 690 + */ 691 + disable_tx_interrupt(bc); 692 + 693 + return 0; 694 + } 695 + 696 + static void ehv_bc_tty_port_shutdown(struct tty_port *port) 697 + { 698 + struct ehv_bc_data *bc = container_of(port, struct ehv_bc_data, port); 699 + 700 + free_irq(bc->tx_irq, bc); 701 + free_irq(bc->rx_irq, bc); 702 + } 703 + 704 + static const struct tty_port_operations ehv_bc_tty_port_ops = { 705 + .activate = ehv_bc_tty_port_activate, 706 + .shutdown = ehv_bc_tty_port_shutdown, 707 + }; 708 + 709 + static int __devinit ehv_bc_tty_probe(struct platform_device *pdev) 710 + { 711 + struct device_node *np = pdev->dev.of_node; 712 + struct ehv_bc_data *bc; 713 + const uint32_t *iprop; 714 + unsigned int handle; 715 + int ret; 716 + static unsigned int index = 1; 717 + unsigned int i; 718 + 719 + iprop = of_get_property(np, "hv-handle", NULL); 720 + if (!iprop) { 721 + dev_err(&pdev->dev, "no 'hv-handle' property in %s node\n", 722 + np->name); 723 + return -ENODEV; 724 + } 725 + 726 + /* We already told the console layer that the index for the console 727 + * device is zero, so we need to make sure that we use that index when 728 + * we probe the console byte channel node. 729 + */ 730 + handle = be32_to_cpu(*iprop); 731 + i = (handle == stdout_bc) ? 0 : index++; 732 + bc = &bcs[i]; 733 + 734 + bc->handle = handle; 735 + bc->head = 0; 736 + bc->tail = 0; 737 + spin_lock_init(&bc->lock); 738 + 739 + bc->rx_irq = irq_of_parse_and_map(np, 0); 740 + bc->tx_irq = irq_of_parse_and_map(np, 1); 741 + if ((bc->rx_irq == NO_IRQ) || (bc->tx_irq == NO_IRQ)) { 742 + dev_err(&pdev->dev, "no 'interrupts' property in %s node\n", 743 + np->name); 744 + ret = -ENODEV; 745 + goto error; 746 + } 747 + 748 + bc->dev = tty_register_device(ehv_bc_driver, i, &pdev->dev); 749 + if (IS_ERR(bc->dev)) { 750 + ret = PTR_ERR(bc->dev); 751 + dev_err(&pdev->dev, "could not register tty (ret=%i)\n", ret); 752 + goto error; 753 + } 754 + 755 + tty_port_init(&bc->port); 756 + bc->port.ops = &ehv_bc_tty_port_ops; 757 + 758 + dev_set_drvdata(&pdev->dev, bc); 759 + 760 + dev_info(&pdev->dev, "registered /dev/%s%u for byte channel %u\n", 761 + ehv_bc_driver->name, i, bc->handle); 762 + 763 + return 0; 764 + 765 + error: 766 + irq_dispose_mapping(bc->tx_irq); 767 + irq_dispose_mapping(bc->rx_irq); 768 + 769 + memset(bc, 0, sizeof(struct ehv_bc_data)); 770 + return ret; 771 + } 772 + 773 + static int ehv_bc_tty_remove(struct platform_device *pdev) 774 + { 775 + struct ehv_bc_data *bc = dev_get_drvdata(&pdev->dev); 776 + 777 + tty_unregister_device(ehv_bc_driver, bc - bcs); 778 + 779 + irq_dispose_mapping(bc->tx_irq); 780 + irq_dispose_mapping(bc->rx_irq); 781 + 782 + return 0; 783 + } 784 + 785 + static const struct of_device_id ehv_bc_tty_of_ids[] = { 786 + { .compatible = "epapr,hv-byte-channel" }, 787 + {} 788 + }; 789 + 790 + static struct platform_driver ehv_bc_tty_driver = { 791 + .driver = { 792 + .owner = THIS_MODULE, 793 + .name = "ehv-bc", 794 + .of_match_table = ehv_bc_tty_of_ids, 795 + }, 796 + .probe = ehv_bc_tty_probe, 797 + .remove = ehv_bc_tty_remove, 798 + }; 799 + 800 + /** 801 + * ehv_bc_init - ePAPR hypervisor byte channel driver initialization 802 + * 803 + * This function is called when this module is loaded. 804 + */ 805 + static int __init ehv_bc_init(void) 806 + { 807 + struct device_node *np; 808 + unsigned int count = 0; /* Number of elements in bcs[] */ 809 + int ret; 810 + 811 + pr_info("ePAPR hypervisor byte channel driver\n"); 812 + 813 + /* Count the number of byte channels */ 814 + for_each_compatible_node(np, NULL, "epapr,hv-byte-channel") 815 + count++; 816 + 817 + if (!count) 818 + return -ENODEV; 819 + 820 + /* The array index of an element in bcs[] is the same as the tty index 821 + * for that element. If you know the address of an element in the 822 + * array, then you can use pointer math (e.g. "bc - bcs") to get its 823 + * tty index. 824 + */ 825 + bcs = kzalloc(count * sizeof(struct ehv_bc_data), GFP_KERNEL); 826 + if (!bcs) 827 + return -ENOMEM; 828 + 829 + ehv_bc_driver = alloc_tty_driver(count); 830 + if (!ehv_bc_driver) { 831 + ret = -ENOMEM; 832 + goto error; 833 + } 834 + 835 + ehv_bc_driver->owner = THIS_MODULE; 836 + ehv_bc_driver->driver_name = "ehv-bc"; 837 + ehv_bc_driver->name = ehv_bc_console.name; 838 + ehv_bc_driver->type = TTY_DRIVER_TYPE_CONSOLE; 839 + ehv_bc_driver->subtype = SYSTEM_TYPE_CONSOLE; 840 + ehv_bc_driver->init_termios = tty_std_termios; 841 + ehv_bc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; 842 + tty_set_operations(ehv_bc_driver, &ehv_bc_ops); 843 + 844 + ret = tty_register_driver(ehv_bc_driver); 845 + if (ret) { 846 + pr_err("ehv-bc: could not register tty driver (ret=%i)\n", ret); 847 + goto error; 848 + } 849 + 850 + ret = platform_driver_register(&ehv_bc_tty_driver); 851 + if (ret) { 852 + pr_err("ehv-bc: could not register platform driver (ret=%i)\n", 853 + ret); 854 + goto error; 855 + } 856 + 857 + return 0; 858 + 859 + error: 860 + if (ehv_bc_driver) { 861 + tty_unregister_driver(ehv_bc_driver); 862 + put_tty_driver(ehv_bc_driver); 863 + } 864 + 865 + kfree(bcs); 866 + 867 + return ret; 868 + } 869 + 870 + 871 + /** 872 + * ehv_bc_exit - ePAPR hypervisor byte channel driver termination 873 + * 874 + * This function is called when this driver is unloaded. 875 + */ 876 + static void __exit ehv_bc_exit(void) 877 + { 878 + tty_unregister_driver(ehv_bc_driver); 879 + put_tty_driver(ehv_bc_driver); 880 + kfree(bcs); 881 + } 882 + 883 + module_init(ehv_bc_init); 884 + module_exit(ehv_bc_exit); 885 + 886 + MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 887 + MODULE_DESCRIPTION("ePAPR hypervisor byte channel driver"); 888 + MODULE_LICENSE("GPL v2");