sunhv.c 13.5 KB
Newer Older
1 2
/* sunhv.c: Serial driver for SUN4V hypervisor console.
 *
3
 * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>
#include <linux/circ_buf.h>
#include <linux/serial.h>
#include <linux/sysrq.h>
#include <linux/console.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/init.h>

#include <asm/hypervisor.h>
#include <asm/spitfire.h>
23 24
#include <asm/prom.h>
#include <asm/of_device.h>
25
#include <asm/irq.h>
26 27 28 29 30 31 32 33 34 35 36 37

#if defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif

#include <linux/serial_core.h>

#include "suncore.h"

#define CON_BREAK	((long)-1)
#define CON_HUP		((long)-2)

38 39
#define IGNORE_BREAK	0x1
#define IGNORE_ALL	0x2
40

41 42
static char *con_write_page;
static char *con_read_page;
43

44
static int hung_up = 0;
45

46
static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit)
47
{
48 49
	while (!uart_circ_empty(xmit)) {
		long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
50

51 52
		if (status != HV_EOK)
			break;
53

54 55 56
		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		port->icount.tx++;
	}
57 58
}

59 60 61 62 63
static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit)
{
	while (!uart_circ_empty(xmit)) {
		unsigned long ra = __pa(xmit->buf + xmit->tail);
		unsigned long len, status, sent;
64

65 66 67 68 69 70 71 72 73
		len = CIRC_CNT_TO_END(xmit->head, xmit->tail,
				      UART_XMIT_SIZE);
		status = sun4v_con_write(ra, len, &sent);
		if (status != HV_EOK)
			break;
		xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1);
		port->icount.tx += sent;
	}
}
74

75
static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty)
76 77 78 79 80 81
{
	int saw_console_brk = 0;
	int limit = 10000;

	while (limit-- > 0) {
		long status;
82
		long c = sun4v_con_getchar(&status);
83 84 85 86 87

		if (status == HV_EWOULDBLOCK)
			break;

		if (c == CON_BREAK) {
88 89
			if (uart_handle_break(port))
				continue;
90 91 92 93 94 95 96 97 98 99 100 101 102
			saw_console_brk = 1;
			c = 0;
		}

		if (c == CON_HUP) {
			hung_up = 1;
			uart_handle_dcd_change(port, 0);
		} else if (hung_up) {
			hung_up = 0;
			uart_handle_dcd_change(port, 1);
		}

		if (tty == NULL) {
103
			uart_handle_sysrq_char(port, c);
104 105 106 107 108
			continue;
		}

		port->icount.rx++;

109
		if (uart_handle_sysrq_char(port, c))
110 111
			continue;

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		tty_insert_flip_char(tty, c, TTY_NORMAL);
	}

	return saw_console_brk;
}

static int receive_chars_read(struct uart_port *port, struct tty_struct *tty)
{
	int saw_console_brk = 0;
	int limit = 10000;

	while (limit-- > 0) {
		unsigned long ra = __pa(con_read_page);
		unsigned long bytes_read, i;
		long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read);

		if (stat != HV_EOK) {
			bytes_read = 0;

			if (stat == CON_BREAK) {
				if (uart_handle_break(port))
					continue;
				saw_console_brk = 1;
				*con_read_page = 0;
				bytes_read = 1;
			} else if (stat == CON_HUP) {
				hung_up = 1;
				uart_handle_dcd_change(port, 0);
				continue;
			} else {
				/* HV_EWOULDBLOCK, etc.  */
				break;
			}
		}

		if (hung_up) {
			hung_up = 0;
			uart_handle_dcd_change(port, 1);
		}

		for (i = 0; i < bytes_read; i++)
			uart_handle_sysrq_char(port, con_read_page[i]);

		if (tty == NULL)
156 157
			continue;

158 159 160
		port->icount.rx += bytes_read;

		tty_insert_flip_string(tty, con_read_page, bytes_read);
161 162
	}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	return saw_console_brk;
}

struct sunhv_ops {
	void (*transmit_chars)(struct uart_port *port, struct circ_buf *xmit);
	int (*receive_chars)(struct uart_port *port, struct tty_struct *tty);
};

static struct sunhv_ops bychar_ops = {
	.transmit_chars = transmit_chars_putchar,
	.receive_chars = receive_chars_getchar,
};

static struct sunhv_ops bywrite_ops = {
	.transmit_chars = transmit_chars_write,
	.receive_chars = receive_chars_read,
};

static struct sunhv_ops *sunhv_ops = &bychar_ops;

static struct tty_struct *receive_chars(struct uart_port *port)
{
	struct tty_struct *tty = NULL;

	if (port->info != NULL)		/* Unopened serial console */
		tty = port->info->tty;

	if (sunhv_ops->receive_chars(port, tty))
191 192 193 194 195 196 197
		sun_do_break();

	return tty;
}

static void transmit_chars(struct uart_port *port)
{
198 199 200 201
	struct circ_buf *xmit;

	if (!port->info)
		return;
202

203
	xmit = &port->info->xmit;
204 205 206
	if (uart_circ_empty(xmit) || uart_tx_stopped(port))
		return;

207
	sunhv_ops->transmit_chars(port, xmit);
208 209 210 211 212

	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
		uart_write_wakeup(port);
}

213
static irqreturn_t sunhv_interrupt(int irq, void *dev_id)
214 215 216 217 218 219
{
	struct uart_port *port = dev_id;
	struct tty_struct *tty;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
220
	tty = receive_chars(port);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	transmit_chars(port);
	spin_unlock_irqrestore(&port->lock, flags);

	if (tty)
		tty_flip_buffer_push(tty);

	return IRQ_HANDLED;
}

/* port->lock is not held.  */
static unsigned int sunhv_tx_empty(struct uart_port *port)
{
	/* Transmitter is always empty for us.  If the circ buffer
	 * is non-empty or there is an x_char pending, our caller
	 * will do the right thing and ignore what we return here.
	 */
	return TIOCSER_TEMT;
}

/* port->lock held by caller.  */
static void sunhv_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
	return;
}

/* port->lock is held by caller and interrupts are disabled.  */
static unsigned int sunhv_get_mctrl(struct uart_port *port)
{
	return TIOCM_DSR | TIOCM_CAR | TIOCM_CTS;
}

/* port->lock held by caller.  */
static void sunhv_stop_tx(struct uart_port *port)
{
	return;
}

/* port->lock held by caller.  */
static void sunhv_start_tx(struct uart_port *port)
{
	struct circ_buf *xmit = &port->info->xmit;

	while (!uart_circ_empty(xmit)) {
264
		long status = sun4v_con_putchar(xmit->buf[xmit->tail]);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

		if (status != HV_EOK)
			break;

		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
		port->icount.tx++;
	}
}

/* port->lock is not held.  */
static void sunhv_send_xchar(struct uart_port *port, char ch)
{
	unsigned long flags;
	int limit = 10000;

	spin_lock_irqsave(&port->lock, flags);

	while (limit-- > 0) {
283
		long status = sun4v_con_putchar(ch);
284 285
		if (status == HV_EOK)
			break;
286
		udelay(1);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
	}

	spin_unlock_irqrestore(&port->lock, flags);
}

/* port->lock held by caller.  */
static void sunhv_stop_rx(struct uart_port *port)
{
}

/* port->lock held by caller.  */
static void sunhv_enable_ms(struct uart_port *port)
{
}

/* port->lock is not held.  */
static void sunhv_break_ctl(struct uart_port *port, int break_state)
{
	if (break_state) {
		unsigned long flags;
307
		int limit = 10000;
308 309 310 311

		spin_lock_irqsave(&port->lock, flags);

		while (limit-- > 0) {
312
			long status = sun4v_con_putchar(CON_BREAK);
313 314
			if (status == HV_EOK)
				break;
315
			udelay(1);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
		}

		spin_unlock_irqrestore(&port->lock, flags);
	}
}

/* port->lock is not held.  */
static int sunhv_startup(struct uart_port *port)
{
	return 0;
}

/* port->lock is not held.  */
static void sunhv_shutdown(struct uart_port *port)
{
}

/* port->lock is not held.  */
A
Alan Cox 已提交
334 335
static void sunhv_set_termios(struct uart_port *port, struct ktermios *termios,
			      struct ktermios *old)
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
{
	unsigned int baud = uart_get_baud_rate(port, termios, old, 0, 4000000);
	unsigned int quot = uart_get_divisor(port, baud);
	unsigned int iflag, cflag;
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);

	iflag = termios->c_iflag;
	cflag = termios->c_cflag;

	port->ignore_status_mask = 0;
	if (iflag & IGNBRK)
		port->ignore_status_mask |= IGNORE_BREAK;
	if ((cflag & CREAD) == 0)
		port->ignore_status_mask |= IGNORE_ALL;

	/* XXX */
	uart_update_timeout(port, cflag,
			    (port->uartclk / (16 * quot)));

	spin_unlock_irqrestore(&port->lock, flags);
}

static const char *sunhv_type(struct uart_port *port)
{
	return "SUN4V HCONS";
}

static void sunhv_release_port(struct uart_port *port)
{
}

static int sunhv_request_port(struct uart_port *port)
{
	return 0;
}

static void sunhv_config_port(struct uart_port *port, int flags)
{
}

static int sunhv_verify_port(struct uart_port *port, struct serial_struct *ser)
{
	return -EINVAL;
}

static struct uart_ops sunhv_pops = {
	.tx_empty	= sunhv_tx_empty,
	.set_mctrl	= sunhv_set_mctrl,
	.get_mctrl	= sunhv_get_mctrl,
	.stop_tx	= sunhv_stop_tx,
	.start_tx	= sunhv_start_tx,
	.send_xchar	= sunhv_send_xchar,
	.stop_rx	= sunhv_stop_rx,
	.enable_ms	= sunhv_enable_ms,
	.break_ctl	= sunhv_break_ctl,
	.startup	= sunhv_startup,
	.shutdown	= sunhv_shutdown,
	.set_termios	= sunhv_set_termios,
	.type		= sunhv_type,
	.release_port	= sunhv_release_port,
	.request_port	= sunhv_request_port,
	.config_port	= sunhv_config_port,
	.verify_port	= sunhv_verify_port,
};

static struct uart_driver sunhv_reg = {
	.owner			= THIS_MODULE,
	.driver_name		= "serial",
	.dev_name		= "ttyS",
	.major			= TTY_MAJOR,
};

static struct uart_port *sunhv_port;

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/* Copy 's' into the con_write_page, decoding "\n" into
 * "\r\n" along the way.  We have to return two lengths
 * because the caller needs to know how much to advance
 * 's' and also how many bytes to output via con_write_page.
 */
static int fill_con_write_page(const char *s, unsigned int n,
			       unsigned long *page_bytes)
{
	const char *orig_s = s;
	char *p = con_write_page;
	int left = PAGE_SIZE;

	while (n--) {
		if (*s == '\n') {
			if (left < 2)
				break;
			*p++ = '\r';
			left--;
		} else if (left < 1)
			break;
		*p++ = *s++;
		left--;
	}
	*page_bytes = p - con_write_page;
	return s - orig_s;
}

static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n)
440
{
441
	struct uart_port *port = sunhv_port;
442 443 444
	unsigned long flags;

	spin_lock_irqsave(&port->lock, flags);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
	while (n > 0) {
		unsigned long ra = __pa(con_write_page);
		unsigned long page_bytes;
		unsigned int cpy = fill_con_write_page(s, n,
						       &page_bytes);

		n -= cpy;
		s += cpy;
		while (page_bytes > 0) {
			unsigned long written;
			int limit = 1000000;

			while (limit--) {
				unsigned long stat;

				stat = sun4v_con_write(ra, page_bytes,
						       &written);
				if (stat == HV_EOK)
					break;
				udelay(1);
			}
			if (limit <= 0)
				break;
			page_bytes -= written;
			ra += written;
		}
	}
	spin_unlock_irqrestore(&port->lock, flags);
}

static inline void sunhv_console_putchar(struct uart_port *port, char c)
{
	int limit = 1000000;
478 479

	while (limit-- > 0) {
480
		long status = sun4v_con_putchar(c);
481 482
		if (status == HV_EOK)
			break;
483
		udelay(1);
484 485 486
	}
}

487
static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n)
488 489
{
	struct uart_port *port = sunhv_port;
490
	unsigned long flags;
491 492
	int i;

493
	spin_lock_irqsave(&port->lock, flags);
494 495 496 497 498
	for (i = 0; i < n; i++) {
		if (*s == '\n')
			sunhv_console_putchar(port, '\r');
		sunhv_console_putchar(port, *s++);
	}
499
	spin_unlock_irqrestore(&port->lock, flags);
500 501 502
}

static struct console sunhv_console = {
D
David S. Miller 已提交
503
	.name	=	"ttyHV",
504
	.write	=	sunhv_console_write_bychar,
505 506 507 508 509 510
	.device	=	uart_console_device,
	.flags	=	CON_PRINTBUFFER,
	.index	=	-1,
	.data	=	&sunhv_reg,
};

D
David S. Miller 已提交
511
static inline struct console *SUNHV_CONSOLE(void)
512 513
{
	if (con_is_present())
D
David S. Miller 已提交
514
		return NULL;
515 516

	sunhv_console.index = 0;
D
David S. Miller 已提交
517 518

	return &sunhv_console;
519 520
}

521
static int __devinit hv_probe(struct of_device *op, const struct of_device_id *match)
522 523
{
	struct uart_port *port;
524
	unsigned long minor;
525
	int err;
526

527
	if (op->irqs[0] == 0xffffffff)
528 529
		return -ENODEV;

530
	port = kzalloc(sizeof(struct uart_port), GFP_KERNEL);
531 532 533
	if (unlikely(!port))
		return -ENOMEM;

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	minor = 1;
	if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 &&
	    minor >= 1) {
		err = -ENOMEM;
		con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
		if (!con_write_page)
			goto out_free_port;

		con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL);
		if (!con_read_page)
			goto out_free_con_write_page;

		sunhv_console.write = sunhv_console_write_paged;
		sunhv_ops = &bywrite_ops;
	}

550
	sunhv_port = port;
551

552 553 554 555 556
	port->line = 0;
	port->ops = &sunhv_pops;
	port->type = PORT_SUNHV;
	port->uartclk = ( 29491200 / 16 ); /* arbitrary */

557 558
	port->membase = (unsigned char __iomem *) __pa(port);

559 560 561
	port->irq = op->irqs[0];

	port->dev = &op->dev;
562 563 564 565

	sunhv_reg.minor = sunserial_current_minor;
	sunhv_reg.nr = 1;

566 567
	err = uart_register_driver(&sunhv_reg);
	if (err)
568
		goto out_free_con_read_page;
569

570
	sunhv_reg.tty_driver->name_base = sunhv_reg.minor - 64;
571 572
	sunserial_current_minor += 1;

D
David S. Miller 已提交
573 574
	sunhv_reg.cons = SUNHV_CONSOLE();

575 576 577
	err = uart_add_one_port(&sunhv_reg, port);
	if (err)
		goto out_unregister_driver;
578

579 580 581
	err = request_irq(port->irq, sunhv_interrupt, 0, "hvcons", port);
	if (err)
		goto out_remove_port;
582

583
	dev_set_drvdata(&op->dev, port);
584 585

	return 0;
586 587 588 589 590 591 592 593

out_remove_port:
	uart_remove_one_port(&sunhv_reg, port);

out_unregister_driver:
	sunserial_current_minor -= 1;
	uart_unregister_driver(&sunhv_reg);

594 595 596 597 598 599
out_free_con_read_page:
	kfree(con_read_page);

out_free_con_write_page:
	kfree(con_write_page);

600 601 602 603
out_free_port:
	kfree(port);
	sunhv_port = NULL;
	return err;
604 605
}

606
static int __devexit hv_remove(struct of_device *dev)
607
{
608
	struct uart_port *port = dev_get_drvdata(&dev->dev);
609

610 611
	free_irq(port->irq, port);

612 613
	uart_remove_one_port(&sunhv_reg, port);

614
	sunserial_current_minor -= 1;
615 616
	uart_unregister_driver(&sunhv_reg);

617
	kfree(port);
618
	sunhv_port = NULL;
619 620 621 622 623 624 625 626 627 628 629

	dev_set_drvdata(&dev->dev, NULL);

	return 0;
}

static struct of_device_id hv_match[] = {
	{
		.name = "console",
		.compatible = "qcn",
	},
630 631 632 633
	{
		.name = "console",
		.compatible = "SUNW,sun4v-console",
	},
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
	{},
};
MODULE_DEVICE_TABLE(of, hv_match);

static struct of_platform_driver hv_driver = {
	.name		= "hv",
	.match_table	= hv_match,
	.probe		= hv_probe,
	.remove		= __devexit_p(hv_remove),
};

static int __init sunhv_init(void)
{
	if (tlb_type != hypervisor)
		return -ENODEV;

	return of_register_driver(&hv_driver, &of_bus_type);
}

static void __exit sunhv_exit(void)
{
	of_unregister_driver(&hv_driver);
656 657 658 659 660 661
}

module_init(sunhv_init);
module_exit(sunhv_exit);

MODULE_AUTHOR("David S. Miller");
662 663
MODULE_DESCRIPTION("SUN4V Hypervisor console driver");
MODULE_VERSION("2.0");
664
MODULE_LICENSE("GPL");