diff options
author | David S. Miller <davem@sunset.davemloft.net> | 2007-05-15 20:03:54 -0400 |
---|---|---|
committer | David S. Miller <davem@sunset.davemloft.net> | 2007-05-15 23:23:02 -0400 |
commit | c7754d465b1feade85b5f1c4492781a30f6652a2 (patch) | |
tree | 9a3b6ccb18983c1ea389377028ca51c8170730ef /drivers/serial | |
parent | 7b104bcb8e460e45a1aebe3da9b86aacdb4cab12 (diff) |
[SPARC64]: Add hypervisor API negotiation and fix console bugs.
Hypervisor interfaces need to be negotiated in order to use
some API calls reliably. So add a small set of interfaces
to request API versions and query current settings.
This allows us to fix some bugs in the hypervisor console:
1) If we can negotiate API group CORE of at least major 1
minor 1 we can use con_read and con_write which can improve
console performance quite a bit.
2) When we do a console write request, we should hold the
spinlock around the whole request, not a byte at a time.
What would happen is that it's easy for output from
different cpus to get mixed with each other.
3) Use consistent udelay() based polling, udelay(1) each
loop with a limit of 1000 polls to handle stuck hypervisor
console.
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers/serial')
-rw-r--r-- | drivers/serial/sunhv.c | 276 |
1 files changed, 205 insertions, 71 deletions
diff --git a/drivers/serial/sunhv.c b/drivers/serial/sunhv.c index c3a6bd2e7950..96557e6dba60 100644 --- a/drivers/serial/sunhv.c +++ b/drivers/serial/sunhv.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* sunhv.c: Serial driver for SUN4V hypervisor console. | 1 | /* sunhv.c: Serial driver for SUN4V hypervisor console. |
2 | * | 2 | * |
3 | * Copyright (C) 2006 David S. Miller (davem@davemloft.net) | 3 | * Copyright (C) 2006, 2007 David S. Miller (davem@davemloft.net) |
4 | */ | 4 | */ |
5 | 5 | ||
6 | #include <linux/module.h> | 6 | #include <linux/module.h> |
@@ -35,57 +35,51 @@ | |||
35 | #define CON_BREAK ((long)-1) | 35 | #define CON_BREAK ((long)-1) |
36 | #define CON_HUP ((long)-2) | 36 | #define CON_HUP ((long)-2) |
37 | 37 | ||
38 | static inline long hypervisor_con_getchar(long *status) | 38 | #define IGNORE_BREAK 0x1 |
39 | { | 39 | #define IGNORE_ALL 0x2 |
40 | register unsigned long func asm("%o5"); | ||
41 | register unsigned long arg0 asm("%o0"); | ||
42 | register unsigned long arg1 asm("%o1"); | ||
43 | |||
44 | func = HV_FAST_CONS_GETCHAR; | ||
45 | arg0 = 0; | ||
46 | arg1 = 0; | ||
47 | __asm__ __volatile__("ta %6" | ||
48 | : "=&r" (func), "=&r" (arg0), "=&r" (arg1) | ||
49 | : "0" (func), "1" (arg0), "2" (arg1), | ||
50 | "i" (HV_FAST_TRAP)); | ||
51 | 40 | ||
52 | *status = arg0; | 41 | static char *con_write_page; |
42 | static char *con_read_page; | ||
53 | 43 | ||
54 | return (long) arg1; | 44 | static int hung_up = 0; |
55 | } | ||
56 | 45 | ||
57 | static inline long hypervisor_con_putchar(long ch) | 46 | static void transmit_chars_putchar(struct uart_port *port, struct circ_buf *xmit) |
58 | { | 47 | { |
59 | register unsigned long func asm("%o5"); | 48 | while (!uart_circ_empty(xmit)) { |
60 | register unsigned long arg0 asm("%o0"); | 49 | long status = sun4v_con_putchar(xmit->buf[xmit->tail]); |
61 | 50 | ||
62 | func = HV_FAST_CONS_PUTCHAR; | 51 | if (status != HV_EOK) |
63 | arg0 = ch; | 52 | break; |
64 | __asm__ __volatile__("ta %4" | ||
65 | : "=&r" (func), "=&r" (arg0) | ||
66 | : "0" (func), "1" (arg0), "i" (HV_FAST_TRAP)); | ||
67 | 53 | ||
68 | return (long) arg0; | 54 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
55 | port->icount.tx++; | ||
56 | } | ||
69 | } | 57 | } |
70 | 58 | ||
71 | #define IGNORE_BREAK 0x1 | 59 | static void transmit_chars_write(struct uart_port *port, struct circ_buf *xmit) |
72 | #define IGNORE_ALL 0x2 | 60 | { |
61 | while (!uart_circ_empty(xmit)) { | ||
62 | unsigned long ra = __pa(xmit->buf + xmit->tail); | ||
63 | unsigned long len, status, sent; | ||
73 | 64 | ||
74 | static int hung_up = 0; | 65 | len = CIRC_CNT_TO_END(xmit->head, xmit->tail, |
66 | UART_XMIT_SIZE); | ||
67 | status = sun4v_con_write(ra, len, &sent); | ||
68 | if (status != HV_EOK) | ||
69 | break; | ||
70 | xmit->tail = (xmit->tail + sent) & (UART_XMIT_SIZE - 1); | ||
71 | port->icount.tx += sent; | ||
72 | } | ||
73 | } | ||
75 | 74 | ||
76 | static struct tty_struct *receive_chars(struct uart_port *port) | 75 | static int receive_chars_getchar(struct uart_port *port, struct tty_struct *tty) |
77 | { | 76 | { |
78 | struct tty_struct *tty = NULL; | ||
79 | int saw_console_brk = 0; | 77 | int saw_console_brk = 0; |
80 | int limit = 10000; | 78 | int limit = 10000; |
81 | 79 | ||
82 | if (port->info != NULL) /* Unopened serial console */ | ||
83 | tty = port->info->tty; | ||
84 | |||
85 | while (limit-- > 0) { | 80 | while (limit-- > 0) { |
86 | long status; | 81 | long status; |
87 | long c = hypervisor_con_getchar(&status); | 82 | long c = sun4v_con_getchar(&status); |
88 | unsigned char flag; | ||
89 | 83 | ||
90 | if (status == HV_EWOULDBLOCK) | 84 | if (status == HV_EWOULDBLOCK) |
91 | break; | 85 | break; |
@@ -110,27 +104,90 @@ static struct tty_struct *receive_chars(struct uart_port *port) | |||
110 | continue; | 104 | continue; |
111 | } | 105 | } |
112 | 106 | ||
113 | flag = TTY_NORMAL; | ||
114 | port->icount.rx++; | 107 | port->icount.rx++; |
115 | if (c == CON_BREAK) { | ||
116 | port->icount.brk++; | ||
117 | if (uart_handle_break(port)) | ||
118 | continue; | ||
119 | flag = TTY_BREAK; | ||
120 | } | ||
121 | 108 | ||
122 | if (uart_handle_sysrq_char(port, c)) | 109 | if (uart_handle_sysrq_char(port, c)) |
123 | continue; | 110 | continue; |
124 | 111 | ||
125 | if ((port->ignore_status_mask & IGNORE_ALL) || | 112 | tty_insert_flip_char(tty, c, TTY_NORMAL); |
126 | ((port->ignore_status_mask & IGNORE_BREAK) && | 113 | } |
127 | (c == CON_BREAK))) | 114 | |
115 | return saw_console_brk; | ||
116 | } | ||
117 | |||
118 | static int receive_chars_read(struct uart_port *port, struct tty_struct *tty) | ||
119 | { | ||
120 | int saw_console_brk = 0; | ||
121 | int limit = 10000; | ||
122 | |||
123 | while (limit-- > 0) { | ||
124 | unsigned long ra = __pa(con_read_page); | ||
125 | unsigned long bytes_read, i; | ||
126 | long stat = sun4v_con_read(ra, PAGE_SIZE, &bytes_read); | ||
127 | |||
128 | if (stat != HV_EOK) { | ||
129 | bytes_read = 0; | ||
130 | |||
131 | if (stat == CON_BREAK) { | ||
132 | if (uart_handle_break(port)) | ||
133 | continue; | ||
134 | saw_console_brk = 1; | ||
135 | *con_read_page = 0; | ||
136 | bytes_read = 1; | ||
137 | } else if (stat == CON_HUP) { | ||
138 | hung_up = 1; | ||
139 | uart_handle_dcd_change(port, 0); | ||
140 | continue; | ||
141 | } else { | ||
142 | /* HV_EWOULDBLOCK, etc. */ | ||
143 | break; | ||
144 | } | ||
145 | } | ||
146 | |||
147 | if (hung_up) { | ||
148 | hung_up = 0; | ||
149 | uart_handle_dcd_change(port, 1); | ||
150 | } | ||
151 | |||
152 | for (i = 0; i < bytes_read; i++) | ||
153 | uart_handle_sysrq_char(port, con_read_page[i]); | ||
154 | |||
155 | if (tty == NULL) | ||
128 | continue; | 156 | continue; |
129 | 157 | ||
130 | tty_insert_flip_char(tty, c, flag); | 158 | port->icount.rx += bytes_read; |
159 | |||
160 | tty_insert_flip_string(tty, con_read_page, bytes_read); | ||
131 | } | 161 | } |
132 | 162 | ||
133 | if (saw_console_brk) | 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, struct tty_struct *tty); | ||
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_struct *receive_chars(struct uart_port *port) | ||
184 | { | ||
185 | struct tty_struct *tty = NULL; | ||
186 | |||
187 | if (port->info != NULL) /* Unopened serial console */ | ||
188 | tty = port->info->tty; | ||
189 | |||
190 | if (sunhv_ops->receive_chars(port, tty)) | ||
134 | sun_do_break(); | 191 | sun_do_break(); |
135 | 192 | ||
136 | return tty; | 193 | return tty; |
@@ -147,15 +204,7 @@ static void transmit_chars(struct uart_port *port) | |||
147 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) | 204 | if (uart_circ_empty(xmit) || uart_tx_stopped(port)) |
148 | return; | 205 | return; |
149 | 206 | ||
150 | while (!uart_circ_empty(xmit)) { | 207 | sunhv_ops->transmit_chars(port, xmit); |
151 | long status = hypervisor_con_putchar(xmit->buf[xmit->tail]); | ||
152 | |||
153 | if (status != HV_EOK) | ||
154 | break; | ||
155 | |||
156 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); | ||
157 | port->icount.tx++; | ||
158 | } | ||
159 | 208 | ||
160 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | 209 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
161 | uart_write_wakeup(port); | 210 | uart_write_wakeup(port); |
@@ -212,7 +261,7 @@ static void sunhv_start_tx(struct uart_port *port) | |||
212 | struct circ_buf *xmit = &port->info->xmit; | 261 | struct circ_buf *xmit = &port->info->xmit; |
213 | 262 | ||
214 | while (!uart_circ_empty(xmit)) { | 263 | while (!uart_circ_empty(xmit)) { |
215 | long status = hypervisor_con_putchar(xmit->buf[xmit->tail]); | 264 | long status = sun4v_con_putchar(xmit->buf[xmit->tail]); |
216 | 265 | ||
217 | if (status != HV_EOK) | 266 | if (status != HV_EOK) |
218 | break; | 267 | break; |
@@ -231,9 +280,10 @@ static void sunhv_send_xchar(struct uart_port *port, char ch) | |||
231 | spin_lock_irqsave(&port->lock, flags); | 280 | spin_lock_irqsave(&port->lock, flags); |
232 | 281 | ||
233 | while (limit-- > 0) { | 282 | while (limit-- > 0) { |
234 | long status = hypervisor_con_putchar(ch); | 283 | long status = sun4v_con_putchar(ch); |
235 | if (status == HV_EOK) | 284 | if (status == HV_EOK) |
236 | break; | 285 | break; |
286 | udelay(1); | ||
237 | } | 287 | } |
238 | 288 | ||
239 | spin_unlock_irqrestore(&port->lock, flags); | 289 | spin_unlock_irqrestore(&port->lock, flags); |
@@ -254,15 +304,15 @@ static void sunhv_break_ctl(struct uart_port *port, int break_state) | |||
254 | { | 304 | { |
255 | if (break_state) { | 305 | if (break_state) { |
256 | unsigned long flags; | 306 | unsigned long flags; |
257 | int limit = 1000000; | 307 | int limit = 10000; |
258 | 308 | ||
259 | spin_lock_irqsave(&port->lock, flags); | 309 | spin_lock_irqsave(&port->lock, flags); |
260 | 310 | ||
261 | while (limit-- > 0) { | 311 | while (limit-- > 0) { |
262 | long status = hypervisor_con_putchar(CON_BREAK); | 312 | long status = sun4v_con_putchar(CON_BREAK); |
263 | if (status == HV_EOK) | 313 | if (status == HV_EOK) |
264 | break; | 314 | break; |
265 | udelay(2); | 315 | udelay(1); |
266 | } | 316 | } |
267 | 317 | ||
268 | spin_unlock_irqrestore(&port->lock, flags); | 318 | spin_unlock_irqrestore(&port->lock, flags); |
@@ -359,38 +409,99 @@ static struct uart_driver sunhv_reg = { | |||
359 | 409 | ||
360 | static struct uart_port *sunhv_port; | 410 | static struct uart_port *sunhv_port; |
361 | 411 | ||
362 | static inline void sunhv_console_putchar(struct uart_port *port, char c) | 412 | /* Copy 's' into the con_write_page, decoding "\n" into |
413 | * "\r\n" along the way. We have to return two lengths | ||
414 | * because the caller needs to know how much to advance | ||
415 | * 's' and also how many bytes to output via con_write_page. | ||
416 | */ | ||
417 | static int fill_con_write_page(const char *s, unsigned int n, | ||
418 | unsigned long *page_bytes) | ||
419 | { | ||
420 | const char *orig_s = s; | ||
421 | char *p = con_write_page; | ||
422 | int left = PAGE_SIZE; | ||
423 | |||
424 | while (n--) { | ||
425 | if (*s == '\n') { | ||
426 | if (left < 2) | ||
427 | break; | ||
428 | *p++ = '\r'; | ||
429 | left--; | ||
430 | } else if (left < 1) | ||
431 | break; | ||
432 | *p++ = *s++; | ||
433 | left--; | ||
434 | } | ||
435 | *page_bytes = p - con_write_page; | ||
436 | return s - orig_s; | ||
437 | } | ||
438 | |||
439 | static void sunhv_console_write_paged(struct console *con, const char *s, unsigned n) | ||
363 | { | 440 | { |
441 | struct uart_port *port = sunhv_port; | ||
364 | unsigned long flags; | 442 | unsigned long flags; |
365 | int limit = 1000000; | ||
366 | 443 | ||
367 | spin_lock_irqsave(&port->lock, flags); | 444 | spin_lock_irqsave(&port->lock, flags); |
445 | while (n > 0) { | ||
446 | unsigned long ra = __pa(con_write_page); | ||
447 | unsigned long page_bytes; | ||
448 | unsigned int cpy = fill_con_write_page(s, n, | ||
449 | &page_bytes); | ||
450 | |||
451 | n -= cpy; | ||
452 | s += cpy; | ||
453 | while (page_bytes > 0) { | ||
454 | unsigned long written; | ||
455 | int limit = 1000000; | ||
456 | |||
457 | while (limit--) { | ||
458 | unsigned long stat; | ||
459 | |||
460 | stat = sun4v_con_write(ra, page_bytes, | ||
461 | &written); | ||
462 | if (stat == HV_EOK) | ||
463 | break; | ||
464 | udelay(1); | ||
465 | } | ||
466 | if (limit <= 0) | ||
467 | break; | ||
468 | page_bytes -= written; | ||
469 | ra += written; | ||
470 | } | ||
471 | } | ||
472 | spin_unlock_irqrestore(&port->lock, flags); | ||
473 | } | ||
474 | |||
475 | static inline void sunhv_console_putchar(struct uart_port *port, char c) | ||
476 | { | ||
477 | int limit = 1000000; | ||
368 | 478 | ||
369 | while (limit-- > 0) { | 479 | while (limit-- > 0) { |
370 | long status = hypervisor_con_putchar(c); | 480 | long status = sun4v_con_putchar(c); |
371 | if (status == HV_EOK) | 481 | if (status == HV_EOK) |
372 | break; | 482 | break; |
373 | udelay(2); | 483 | udelay(1); |
374 | } | 484 | } |
375 | |||
376 | spin_unlock_irqrestore(&port->lock, flags); | ||
377 | } | 485 | } |
378 | 486 | ||
379 | static void sunhv_console_write(struct console *con, const char *s, unsigned n) | 487 | static void sunhv_console_write_bychar(struct console *con, const char *s, unsigned n) |
380 | { | 488 | { |
381 | struct uart_port *port = sunhv_port; | 489 | struct uart_port *port = sunhv_port; |
490 | unsigned long flags; | ||
382 | int i; | 491 | int i; |
383 | 492 | ||
493 | spin_lock_irqsave(&port->lock, flags); | ||
384 | for (i = 0; i < n; i++) { | 494 | for (i = 0; i < n; i++) { |
385 | if (*s == '\n') | 495 | if (*s == '\n') |
386 | sunhv_console_putchar(port, '\r'); | 496 | sunhv_console_putchar(port, '\r'); |
387 | sunhv_console_putchar(port, *s++); | 497 | sunhv_console_putchar(port, *s++); |
388 | } | 498 | } |
499 | spin_unlock_irqrestore(&port->lock, flags); | ||
389 | } | 500 | } |
390 | 501 | ||
391 | static struct console sunhv_console = { | 502 | static struct console sunhv_console = { |
392 | .name = "ttyHV", | 503 | .name = "ttyHV", |
393 | .write = sunhv_console_write, | 504 | .write = sunhv_console_write_bychar, |
394 | .device = uart_console_device, | 505 | .device = uart_console_device, |
395 | .flags = CON_PRINTBUFFER, | 506 | .flags = CON_PRINTBUFFER, |
396 | .index = -1, | 507 | .index = -1, |
@@ -410,6 +521,7 @@ static inline struct console *SUNHV_CONSOLE(void) | |||
410 | static int __devinit hv_probe(struct of_device *op, const struct of_device_id *match) | 521 | static int __devinit hv_probe(struct of_device *op, const struct of_device_id *match) |
411 | { | 522 | { |
412 | struct uart_port *port; | 523 | struct uart_port *port; |
524 | unsigned long minor; | ||
413 | int err; | 525 | int err; |
414 | 526 | ||
415 | if (op->irqs[0] == 0xffffffff) | 527 | if (op->irqs[0] == 0xffffffff) |
@@ -419,6 +531,22 @@ static int __devinit hv_probe(struct of_device *op, const struct of_device_id *m | |||
419 | if (unlikely(!port)) | 531 | if (unlikely(!port)) |
420 | return -ENOMEM; | 532 | return -ENOMEM; |
421 | 533 | ||
534 | minor = 1; | ||
535 | if (sun4v_hvapi_register(HV_GRP_CORE, 1, &minor) == 0 && | ||
536 | minor >= 1) { | ||
537 | err = -ENOMEM; | ||
538 | con_write_page = kzalloc(PAGE_SIZE, GFP_KERNEL); | ||
539 | if (!con_write_page) | ||
540 | goto out_free_port; | ||
541 | |||
542 | con_read_page = kzalloc(PAGE_SIZE, GFP_KERNEL); | ||
543 | if (!con_read_page) | ||
544 | goto out_free_con_write_page; | ||
545 | |||
546 | sunhv_console.write = sunhv_console_write_paged; | ||
547 | sunhv_ops = &bywrite_ops; | ||
548 | } | ||
549 | |||
422 | sunhv_port = port; | 550 | sunhv_port = port; |
423 | 551 | ||
424 | port->line = 0; | 552 | port->line = 0; |
@@ -437,7 +565,7 @@ static int __devinit hv_probe(struct of_device *op, const struct of_device_id *m | |||
437 | 565 | ||
438 | err = uart_register_driver(&sunhv_reg); | 566 | err = uart_register_driver(&sunhv_reg); |
439 | if (err) | 567 | if (err) |
440 | goto out_free_port; | 568 | goto out_free_con_read_page; |
441 | 569 | ||
442 | sunhv_reg.tty_driver->name_base = sunhv_reg.minor - 64; | 570 | sunhv_reg.tty_driver->name_base = sunhv_reg.minor - 64; |
443 | sunserial_current_minor += 1; | 571 | sunserial_current_minor += 1; |
@@ -463,6 +591,12 @@ out_unregister_driver: | |||
463 | sunserial_current_minor -= 1; | 591 | sunserial_current_minor -= 1; |
464 | uart_unregister_driver(&sunhv_reg); | 592 | uart_unregister_driver(&sunhv_reg); |
465 | 593 | ||
594 | out_free_con_read_page: | ||
595 | kfree(con_read_page); | ||
596 | |||
597 | out_free_con_write_page: | ||
598 | kfree(con_write_page); | ||
599 | |||
466 | out_free_port: | 600 | out_free_port: |
467 | kfree(port); | 601 | kfree(port); |
468 | sunhv_port = NULL; | 602 | sunhv_port = NULL; |