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