1 /* sunhv.c: Serial driver for SUN4V hypervisor console. 2 * 3 * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net) 4 */ 5 6 #include <linux/kernel.h> 7 #include <linux/errno.h> 8 #include <linux/tty.h> 9 #include <linux/tty_flip.h> 10 #include <linux/major.h> 11 #include <linux/circ_buf.h> 12 #include <linux/serial.h> 13 #include <linux/sysrq.h> 14 #include <linux/console.h> 15 #include <linux/spinlock.h> 16 #include <linux/slab.h> 17 #include <linux/delay.h> 18 #include <linux/init.h> 19 #include <linux/of_device.h> 20 21 #include <asm/hypervisor.h> 22 #include <asm/spitfire.h> 23 #include <asm/prom.h> 24 #include <asm/irq.h> 25 #include <asm/setup.h> 26 27 #if defined(CONFIG_MAGIC_SYSRQ) 28 #define SUPPORT_SYSRQ 29 #endif 30 31 #include <linux/serial_core.h> 32 #include <linux/sunserialcore.h> 33 34 #define CON_BREAK ((long)-1) 35 #define CON_HUP ((long)-2) 36 37 #define IGNORE_BREAK 0x1 38 #define IGNORE_ALL 0x2 39 40 static char *con_write_page; 41 static char *con_read_page; 42 43 static int hung_up = 0; 44 45 static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit) 46 { 47 while (!uart_circ_empty(xmit)) { 48 long status = sun4v_con_putchar(xmit->buf[xmit->tail]); 49 50 if (status != HV_EOK) 51 break; 52 53 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 54 port->icount.tx++; 55 } 56 } 57 58 static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit) 59 { 60 while (!uart_circ_empty(xmit)) { 61 unsigned long ra = __pa(xmit->buf + xmit->tail); 62 unsigned long len, status, sent; 63 64 len = CIRC_CNT_TO_END(xmit->head, xmit->tail, 65 UART_XMIT_SIZE); 66 status = sun4v_con_write(ra, len, &sent); 67 if (status != HV_EOK) 68 break; 69 xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1); 70 port->icount.tx += sent; 71 } 72 } 73 74 static int receive_chars_getchar(struct uart_port *port) 75 { 76 int saw_console_brk = 0; 77 int limit = 10000; 78 79 while (limit-- > 0) { 80 long status; 81 long c = sun4v_con_getchar(&status); 82 83 if (status == HV_EWOULDBLOCK) 84 break; 85 86 if (c == CON_BREAK) { 87 if (uart_handle_break(port)) 88 continue; 89 saw_console_brk = 1; 90 c = 0; 91 } 92 93 if (c == CON_HUP) { 94 hung_up = 1; 95 uart_handle_dcd_change(port, 0); 96 } else if (hung_up) { 97 hung_up = 0; 98 uart_handle_dcd_change(port, 1); 99 } 100 101 if (port->state == NULL) { 102 uart_handle_sysrq_char(port, c); 103 continue; 104 } 105 106 port->icount.rx++; 107 108 if (uart_handle_sysrq_char(port, c)) 109 continue; 110 111 tty_insert_flip_char(&port->state->port, c, TTY_NORMAL); 112 } 113 114 return saw_console_brk; 115 } 116 117 static int receive_chars_read(struct uart_port *port) 118 { 119 int saw_console_brk = 0; 120 int limit = 10000; 121 122 while (limit-- > 0) { 123 unsigned long ra = __pa(con_read_page); 124 unsigned long bytes_read, i; 125 long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read); 126 127 if (stat != HV_EOK) { 128 bytes_read = 0; 129 130 if (stat == CON_BREAK) { 131 if (uart_handle_break(port)) 132 continue; 133 saw_console_brk = 1; 134 *con_read_page = 0; 135 bytes_read = 1; 136 } else if (stat == CON_HUP) { 137 hung_up = 1; 138 uart_handle_dcd_change(port, 0); 139 continue; 140 } else { 141 /* HV_EWOULDBLOCK, etc. */ 142 break; 143 } 144 } 145 146 if (hung_up) { 147 hung_up = 0; 148 uart_handle_dcd_change(port, 1); 149 } 150 151 for (i = 0; i < bytes_read; i++) 152 uart_handle_sysrq_char(port, con_read_page[i]); 153 154 if (port->state == NULL) 155 continue; 156 157 port->icount.rx += bytes_read; 158 159 tty_insert_flip_string(&port->state->port, con_read_page, 160 bytes_read); 161 } 162 163 return saw_console_brk; 164 } 165 166 struct sunhv_ops { 167 void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit); 168 int (*receive_chars)(struct uart_port *port); 169 }; 170 171 static struct sunhv_ops bychar_ops = { 172 .transmit_chars = transmit_chars_putchar, 173 .receive_chars = receive_chars_getchar, 174 }; 175 176 static struct sunhv_ops bywrite_ops = { 177 .transmit_chars = transmit_chars_write, 178 .receive_chars = receive_chars_read, 179 }; 180 181 static struct sunhv_ops *sunhv_ops = &bychar_ops; 182 183 static struct tty_port *receive_chars(struct uart_port *port) 184 { 185 struct tty_port *tport = NULL; 186 187 if (port->state != NULL) /* Unopened serial console */ 188 tport = &port->state->port; 189 190 if (sunhv_ops->receive_chars(port)) 191 sun_do_break(); 192 193 return tport; 194 } 195 196 static void transmit_chars(struct uart_port *port) 197 { 198 struct circ_buf *xmit; 199 200 if (!port->state) 201 return; 202 203 xmit = &port->state->xmit; 204 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) 205 return; 206 207 sunhv_ops->transmit_chars(port, xmit); 208 209 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 210 uart_write_wakeup(port); 211 } 212 213 static irqreturn_t sunhv_interrupt(int irq, void *dev_id) 214 { 215 struct uart_port *port = dev_id; 216 struct tty_port *tport; 217 unsigned long flags; 218 219 spin_lock_irqsave(&port->lock, flags); 220 tport = receive_chars(port); 221 transmit_chars(port); 222 spin_unlock_irqrestore(&port->lock, flags); 223 224 if (tport) 225 tty_flip_buffer_push(tport); 226 227 return IRQ_HANDLED; 228 } 229 230 /* port->lock is not held. */ 231 static unsigned int sunhv_tx_empty(struct uart_port *port) 232 { 233 /* Transmitter is always empty for us. If the circ buffer 234 * is non-empty or there is an x_char pending, our caller 235 * will do the right thing and ignore what we return here. 236 */ 237 return TIOCSER_TEMT; 238 } 239 240 /* port->lock held by caller. */ 241 static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl) 242 { 243 return; 244 } 245 246 /* port->lock is held by caller and interrupts are disabled. */ 247 static unsigned int sunhv_get_mctrl(struct uart_port *port) 248 { 249 return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS; 250 } 251 252 /* port->lock held by caller. */ 253 static void sunhv_stop_tx(struct uart_port *port) 254 { 255 return; 256 } 257 258 /* port->lock held by caller. */ 259 static void sunhv_start_tx(struct uart_port *port) 260 { 261 transmit_chars(port); 262 } 263 264 /* port->lock is not held. */ 265 static void sunhv_send_xchar(struct uart_port *port, char ch) 266 { 267 unsigned long flags; 268 int limit = 10000; 269 270 if (ch == __DISABLED_CHAR) 271 return; 272 273 spin_lock_irqsave(&port->lock, flags); 274 275 while (limit-- > 0) { 276 long status = sun4v_con_putchar(ch); 277 if (status == HV_EOK) 278 break; 279 udelay(1); 280 } 281 282 spin_unlock_irqrestore(&port->lock, flags); 283 } 284 285 /* port->lock held by caller. */ 286 static void sunhv_stop_rx(struct uart_port *port) 287 { 288 } 289 290 /* port->lock is not held. */ 291 static void sunhv_break_ctl(struct uart_port *port, int break_state) 292 { 293 if (break_state) { 294 unsigned long flags; 295 int limit = 10000; 296 297 spin_lock_irqsave(&port->lock, flags); 298 299 while (limit-- > 0) { 300 long status = sun4v_con_putchar(CON_BREAK); 301 if (status == HV_EOK) 302 break; 303 udelay(1); 304 } 305 306 spin_unlock_irqrestore(&port->lock, flags); 307 } 308 } 309 310 /* port->lock is not held. */ 311 static int sunhv_startup(struct uart_port *port) 312 { 313 return 0; 314 } 315 316 /* port->lock is not held. */ 317 static void sunhv_shutdown(struct uart_port *port) 318 { 319 } 320 321 /* port->lock is not held. */ 322 static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios, 323 struct ktermios *old) 324 { 325 unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000); 326 unsigned int quot = uart_get_divisor(port, baud); 327 unsigned int iflag, cflag; 328 unsigned long flags; 329 330 spin_lock_irqsave(&port->lock, flags); 331 332 iflag = termios->c_iflag; 333 cflag = termios->c_cflag; 334 335 port->ignore_status_mask = 0; 336 if (iflag & IGNBRK) 337 port->ignore_status_mask |= IGNORE_BREAK; 338 if ((cflag & CREAD) == 0) 339 port->ignore_status_mask |= IGNORE_ALL; 340 341 /* XXX */ 342 uart_update_timeout(port, cflag, 343 (port->uartclk / (16 * quot))); 344 345 spin_unlock_irqrestore(&port->lock, flags); 346 } 347 348 static const char *sunhv_type(struct uart_port *port) 349 { 350 return "SUN4V HCONS"; 351 } 352 353 static void sunhv_release_port(struct uart_port *port) 354 { 355 } 356 357 static int sunhv_request_port(struct uart_port *port) 358 { 359 return 0; 360 } 361 362 static void sunhv_config_port(struct uart_port *port, int flags) 363 { 364 } 365 366 static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser) 367 { 368 return -EINVAL; 369 } 370 371 static struct uart_ops sunhv_pops = { 372 .tx_empty = sunhv_tx_empty, 373 .set_mctrl = sunhv_set_mctrl, 374 .get_mctrl = sunhv_get_mctrl, 375 .stop_tx = sunhv_stop_tx, 376 .start_tx = sunhv_start_tx, 377 .send_xchar = sunhv_send_xchar, 378 .stop_rx = sunhv_stop_rx, 379 .break_ctl = sunhv_break_ctl, 380 .startup = sunhv_startup, 381 .shutdown = sunhv_shutdown, 382 .set_termios = sunhv_set_termios, 383 .type = sunhv_type, 384 .release_port = sunhv_release_port, 385 .request_port = sunhv_request_port, 386 .config_port = sunhv_config_port, 387 .verify_port = sunhv_verify_port, 388 }; 389 390 static struct uart_driver sunhv_reg = { 391 .owner = THIS_MODULE, 392 .driver_name = "sunhv", 393 .dev_name = "ttyS", 394 .major = TTY_MAJOR, 395 }; 396 397 static struct uart_port *sunhv_port; 398 399 /* Copy 's' into the con_write_page, decoding "\n" into 400 * "\r\n" along the way. We have to return two lengths 401 * because the caller needs to know how much to advance 402 * 's' and also how many bytes to output via con_write_page. 403 */ 404 static int fill_con_write_page(const char *s, unsigned int n, 405 unsigned long *page_bytes) 406 { 407 const char *orig_s = s; 408 char *p = con_write_page; 409 int left = PAGE_SIZE; 410 411 while (n--) { 412 if (*s == '\n') { 413 if (left < 2) 414 break; 415 *p++ = '\r'; 416 left--; 417 } else if (left < 1) 418 break; 419 *p++ = *s++; 420 left--; 421 } 422 *page_bytes = p - con_write_page; 423 return s - orig_s; 424 } 425 426 static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n) 427 { 428 struct uart_port *port = sunhv_port; 429 unsigned long flags; 430 int locked = 1; 431 432 if (port->sysrq || oops_in_progress) 433 locked = spin_trylock_irqsave(&port->lock, flags); 434 else 435 spin_lock_irqsave(&port->lock, flags); 436 437 while (n > 0) { 438 unsigned long ra = __pa(con_write_page); 439 unsigned long page_bytes; 440 unsigned int cpy = fill_con_write_page(s, n, 441 &page_bytes); 442 443 n -= cpy; 444 s += cpy; 445 while (page_bytes > 0) { 446 unsigned long written; 447 int limit = 1000000; 448 449 while (limit--) { 450 unsigned long stat; 451 452 stat = sun4v_con_write(ra, page_bytes, 453 &written); 454 if (stat == HV_EOK) 455 break; 456 udelay(1); 457 } 458 if (limit < 0) 459 break; 460 page_bytes -= written; 461 ra += written; 462 } 463 } 464 465 if (locked) 466 spin_unlock_irqrestore(&port->lock, flags); 467 } 468 469 static inline void sunhv_console_putchar(struct uart_port *port, char c) 470 { 471 int limit = 1000000; 472 473 while (limit-- > 0) { 474 long status = sun4v_con_putchar(c); 475 if (status == HV_EOK) 476 break; 477 udelay(1); 478 } 479 } 480 481 static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n) 482 { 483 struct uart_port *port = sunhv_port; 484 unsigned long flags; 485 int i, locked = 1; 486 487 if (port->sysrq || oops_in_progress) 488 locked = spin_trylock_irqsave(&port->lock, flags); 489 else 490 spin_lock_irqsave(&port->lock, flags); 491 if (port->sysrq) { 492 locked = 0; 493 } else if (oops_in_progress) { 494 locked = spin_trylock(&port->lock); 495 } else 496 spin_lock(&port->lock); 497 498 for (i = 0; i < n; i++) { 499 if (*s == '\n') 500 sunhv_console_putchar(port, '\r'); 501 sunhv_console_putchar(port, *s++); 502 } 503 504 if (locked) 505 spin_unlock_irqrestore(&port->lock, flags); 506 } 507 508 static struct console sunhv_console = { 509 .name = "ttyHV", 510 .write = sunhv_console_write_bychar, 511 .device = uart_console_device, 512 .flags = CON_PRINTBUFFER, 513 .index = -1, 514 .data = &sunhv_reg, 515 }; 516 517 static int hv_probe(struct platform_device *op) 518 { 519 struct uart_port *port; 520 unsigned long minor; 521 int err; 522 523 if (op->archdata.irqs[0] == 0xffffffff) 524 return -ENODEV; 525 526 port = kzalloc(sizeof(struct uart_port), GFP_KERNEL); 527 if (unlikely(!port)) 528 return -ENOMEM; 529 530 minor = 1; 531 if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 && 532 minor >= 1) { 533 err = -ENOMEM; 534 con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 535 if (!con_write_page) 536 goto out_free_port; 537 538 con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL); 539 if (!con_read_page) 540 goto out_free_con_write_page; 541 542 sunhv_console.write = sunhv_console_write_paged; 543 sunhv_ops = &bywrite_ops; 544 } 545 546 sunhv_port = port; 547 548 port->line = 0; 549 port->ops = &sunhv_pops; 550 port->type = PORT_SUNHV; 551 port->uartclk = ( 29491200 / 16 ); /* arbitrary */ 552 553 port->membase = (unsigned char __iomem *) __pa(port); 554 555 port->irq = op->archdata.irqs[0]; 556 557 port->dev = &op->dev; 558 559 err = sunserial_register_minors(&sunhv_reg, 1); 560 if (err) 561 goto out_free_con_read_page; 562 563 sunserial_console_match(&sunhv_console, op->dev.of_node, 564 &sunhv_reg, port->line, false); 565 566 err = uart_add_one_port(&sunhv_reg, port); 567 if (err) 568 goto out_unregister_driver; 569 570 err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port); 571 if (err) 572 goto out_remove_port; 573 574 platform_set_drvdata(op, port); 575 576 return 0; 577 578 out_remove_port: 579 uart_remove_one_port(&sunhv_reg, port); 580 581 out_unregister_driver: 582 sunserial_unregister_minors(&sunhv_reg, 1); 583 584 out_free_con_read_page: 585 kfree(con_read_page); 586 587 out_free_con_write_page: 588 kfree(con_write_page); 589 590 out_free_port: 591 kfree(port); 592 sunhv_port = NULL; 593 return err; 594 } 595 596 static int hv_remove(struct platform_device *dev) 597 { 598 struct uart_port *port = platform_get_drvdata(dev); 599 600 free_irq(port->irq, port); 601 602 uart_remove_one_port(&sunhv_reg, port); 603 604 sunserial_unregister_minors(&sunhv_reg, 1); 605 606 kfree(port); 607 sunhv_port = NULL; 608 609 return 0; 610 } 611 612 static const struct of_device_id hv_match[] = { 613 { 614 .name = "console", 615 .compatible = "qcn", 616 }, 617 { 618 .name = "console", 619 .compatible = "SUNW,sun4v-console", 620 }, 621 {}, 622 }; 623 624 static struct platform_driver hv_driver = { 625 .driver = { 626 .name = "hv", 627 .of_match_table = hv_match, 628 }, 629 .probe = hv_probe, 630 .remove = hv_remove, 631 }; 632 633 static int __init sunhv_init(void) 634 { 635 if (tlb_type != hypervisor) 636 return -ENODEV; 637 638 return platform_driver_register(&hv_driver); 639 } 640 device_initcall(sunhv_init); 641 642 #if 0 /* ...def MODULE ; never supported as such */ 643 MODULE_AUTHOR("David S. Miller"); 644 MODULE_DESCRIPTION("SUN4V Hypervisor console driver"); 645 MODULE_VERSION("2.0"); 646 MODULE_LICENSE("GPL"); 647 #endif 648