1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
20c57dfccSAnton Vorontsov /*
30c57dfccSAnton Vorontsov * KGDB NMI serial console
40c57dfccSAnton Vorontsov *
50c57dfccSAnton Vorontsov * Copyright 2010 Google, Inc.
60c57dfccSAnton Vorontsov * Arve Hjønnevåg <arve@android.com>
70c57dfccSAnton Vorontsov * Colin Cross <ccross@android.com>
80c57dfccSAnton Vorontsov * Copyright 2012 Linaro Ltd.
90c57dfccSAnton Vorontsov * Anton Vorontsov <anton.vorontsov@linaro.org>
100c57dfccSAnton Vorontsov */
110c57dfccSAnton Vorontsov
120c57dfccSAnton Vorontsov #include <linux/kernel.h>
130c57dfccSAnton Vorontsov #include <linux/module.h>
140c57dfccSAnton Vorontsov #include <linux/compiler.h>
150c57dfccSAnton Vorontsov #include <linux/slab.h>
160c57dfccSAnton Vorontsov #include <linux/errno.h>
170c57dfccSAnton Vorontsov #include <linux/atomic.h>
180c57dfccSAnton Vorontsov #include <linux/console.h>
190c57dfccSAnton Vorontsov #include <linux/tty.h>
200c57dfccSAnton Vorontsov #include <linux/tty_driver.h>
210c57dfccSAnton Vorontsov #include <linux/tty_flip.h>
2216559ae4SGreg Kroah-Hartman #include <linux/serial_core.h>
230c57dfccSAnton Vorontsov #include <linux/interrupt.h>
240c57dfccSAnton Vorontsov #include <linux/hrtimer.h>
250c57dfccSAnton Vorontsov #include <linux/tick.h>
260c57dfccSAnton Vorontsov #include <linux/kfifo.h>
270c57dfccSAnton Vorontsov #include <linux/kgdb.h>
280c57dfccSAnton Vorontsov #include <linux/kdb.h>
290c57dfccSAnton Vorontsov
300c57dfccSAnton Vorontsov static int kgdb_nmi_knock = 1;
310c57dfccSAnton Vorontsov module_param_named(knock, kgdb_nmi_knock, int, 0600);
320c57dfccSAnton Vorontsov MODULE_PARM_DESC(knock, "if set to 1 (default), the special '$3#33' command " \
330c57dfccSAnton Vorontsov "must be used to enter the debugger; when set to 0, " \
340c57dfccSAnton Vorontsov "hitting return key is enough to enter the debugger; " \
350c57dfccSAnton Vorontsov "when set to -1, the debugger is entered immediately " \
360c57dfccSAnton Vorontsov "upon NMI");
370c57dfccSAnton Vorontsov
380c57dfccSAnton Vorontsov static char *kgdb_nmi_magic = "$3#33";
390c57dfccSAnton Vorontsov module_param_named(magic, kgdb_nmi_magic, charp, 0600);
400c57dfccSAnton Vorontsov MODULE_PARM_DESC(magic, "magic sequence to enter NMI debugger (default $3#33)");
410c57dfccSAnton Vorontsov
42c8b29f04SDaniel Thompson static atomic_t kgdb_nmi_num_readers = ATOMIC_INIT(0);
430c57dfccSAnton Vorontsov
kgdb_nmi_console_setup(struct console * co,char * options)44bd71a1c0SDaniel Thompson static int kgdb_nmi_console_setup(struct console *co, char *options)
45bd71a1c0SDaniel Thompson {
46287f03c0SDaniel Thompson arch_kgdb_ops.enable_nmi(1);
47287f03c0SDaniel Thompson
48bd71a1c0SDaniel Thompson /* The NMI console uses the dbg_io_ops to issue console messages. To
49bd71a1c0SDaniel Thompson * avoid duplicate messages during kdb sessions we must inform kdb's
50bd71a1c0SDaniel Thompson * I/O utilities that messages sent to the console will automatically
51bd71a1c0SDaniel Thompson * be displayed on the dbg_io.
52bd71a1c0SDaniel Thompson */
535946d1f5SSumit Garg dbg_io_ops->cons = co;
54bd71a1c0SDaniel Thompson
55bd71a1c0SDaniel Thompson return 0;
56bd71a1c0SDaniel Thompson }
57bd71a1c0SDaniel Thompson
kgdb_nmi_console_write(struct console * co,const char * s,uint c)580c57dfccSAnton Vorontsov static void kgdb_nmi_console_write(struct console *co, const char *s, uint c)
590c57dfccSAnton Vorontsov {
600c57dfccSAnton Vorontsov int i;
610c57dfccSAnton Vorontsov
620c57dfccSAnton Vorontsov for (i = 0; i < c; i++)
630c57dfccSAnton Vorontsov dbg_io_ops->write_char(s[i]);
640c57dfccSAnton Vorontsov }
650c57dfccSAnton Vorontsov
660c57dfccSAnton Vorontsov static struct tty_driver *kgdb_nmi_tty_driver;
670c57dfccSAnton Vorontsov
kgdb_nmi_console_device(struct console * co,int * idx)680c57dfccSAnton Vorontsov static struct tty_driver *kgdb_nmi_console_device(struct console *co, int *idx)
690c57dfccSAnton Vorontsov {
700c57dfccSAnton Vorontsov *idx = co->index;
710c57dfccSAnton Vorontsov return kgdb_nmi_tty_driver;
720c57dfccSAnton Vorontsov }
730c57dfccSAnton Vorontsov
740c57dfccSAnton Vorontsov static struct console kgdb_nmi_console = {
750c57dfccSAnton Vorontsov .name = "ttyNMI",
76bd71a1c0SDaniel Thompson .setup = kgdb_nmi_console_setup,
770c57dfccSAnton Vorontsov .write = kgdb_nmi_console_write,
780c57dfccSAnton Vorontsov .device = kgdb_nmi_console_device,
79287f03c0SDaniel Thompson .flags = CON_PRINTBUFFER | CON_ANYTIME,
800c57dfccSAnton Vorontsov .index = -1,
810c57dfccSAnton Vorontsov };
820c57dfccSAnton Vorontsov
830c57dfccSAnton Vorontsov /*
840c57dfccSAnton Vorontsov * This is usually the maximum rate on debug ports. We make fifo large enough
850c57dfccSAnton Vorontsov * to make copy-pasting to the terminal usable.
860c57dfccSAnton Vorontsov */
870c57dfccSAnton Vorontsov #define KGDB_NMI_BAUD 115200
880c57dfccSAnton Vorontsov #define KGDB_NMI_FIFO_SIZE roundup_pow_of_two(KGDB_NMI_BAUD / 8 / HZ)
890c57dfccSAnton Vorontsov
900c57dfccSAnton Vorontsov struct kgdb_nmi_tty_priv {
910c57dfccSAnton Vorontsov struct tty_port port;
928a0ff60fSDaniel Thompson struct timer_list timer;
930c57dfccSAnton Vorontsov STRUCT_KFIFO(char, KGDB_NMI_FIFO_SIZE) fifo;
940c57dfccSAnton Vorontsov };
950c57dfccSAnton Vorontsov
960c57dfccSAnton Vorontsov static struct tty_port *kgdb_nmi_port;
970c57dfccSAnton Vorontsov
kgdb_tty_recv(int ch)980c57dfccSAnton Vorontsov static void kgdb_tty_recv(int ch)
990c57dfccSAnton Vorontsov {
1000c57dfccSAnton Vorontsov struct kgdb_nmi_tty_priv *priv;
1010c57dfccSAnton Vorontsov char c = ch;
1020c57dfccSAnton Vorontsov
1030c57dfccSAnton Vorontsov if (!kgdb_nmi_port || ch < 0)
1040c57dfccSAnton Vorontsov return;
1050c57dfccSAnton Vorontsov /*
1068a0ff60fSDaniel Thompson * Can't use port->tty->driver_data as tty might be not there. Timer
1070c57dfccSAnton Vorontsov * will check for tty and will get the ref, but here we don't have to
1080c57dfccSAnton Vorontsov * do that, and actually, we can't: we're in NMI context, no locks are
1090c57dfccSAnton Vorontsov * possible.
1100c57dfccSAnton Vorontsov */
11106d18289SDaniel Thompson priv = container_of(kgdb_nmi_port, struct kgdb_nmi_tty_priv, port);
1120c57dfccSAnton Vorontsov kfifo_in(&priv->fifo, &c, 1);
1130c57dfccSAnton Vorontsov }
1140c57dfccSAnton Vorontsov
kgdb_nmi_poll_one_knock(void)1150c57dfccSAnton Vorontsov static int kgdb_nmi_poll_one_knock(void)
1160c57dfccSAnton Vorontsov {
1170c57dfccSAnton Vorontsov static int n;
1181e49b095SColin Ian King int c;
1190c57dfccSAnton Vorontsov const char *magic = kgdb_nmi_magic;
1200c57dfccSAnton Vorontsov size_t m = strlen(magic);
121a4282b86SZheng Bin bool printch = false;
1220c57dfccSAnton Vorontsov
1230c57dfccSAnton Vorontsov c = dbg_io_ops->read_char();
1240c57dfccSAnton Vorontsov if (c == NO_POLL_CHAR)
1250c57dfccSAnton Vorontsov return c;
1260c57dfccSAnton Vorontsov
1270c57dfccSAnton Vorontsov if (!kgdb_nmi_knock && (c == '\r' || c == '\n')) {
1280c57dfccSAnton Vorontsov return 1;
1290c57dfccSAnton Vorontsov } else if (c == magic[n]) {
1300c57dfccSAnton Vorontsov n = (n + 1) % m;
1310c57dfccSAnton Vorontsov if (!n)
1320c57dfccSAnton Vorontsov return 1;
133a4282b86SZheng Bin printch = true;
1340c57dfccSAnton Vorontsov } else {
1350c57dfccSAnton Vorontsov n = 0;
1360c57dfccSAnton Vorontsov }
1370c57dfccSAnton Vorontsov
138c8b29f04SDaniel Thompson if (atomic_read(&kgdb_nmi_num_readers)) {
1390c57dfccSAnton Vorontsov kgdb_tty_recv(c);
1400c57dfccSAnton Vorontsov return 0;
1410c57dfccSAnton Vorontsov }
1420c57dfccSAnton Vorontsov
1430c57dfccSAnton Vorontsov if (printch) {
1440c57dfccSAnton Vorontsov kdb_printf("%c", c);
1450c57dfccSAnton Vorontsov return 0;
1460c57dfccSAnton Vorontsov }
1470c57dfccSAnton Vorontsov
1480c57dfccSAnton Vorontsov kdb_printf("\r%s %s to enter the debugger> %*s",
1490c57dfccSAnton Vorontsov kgdb_nmi_knock ? "Type" : "Hit",
1500c57dfccSAnton Vorontsov kgdb_nmi_knock ? magic : "<return>", (int)m, "");
1510c57dfccSAnton Vorontsov while (m--)
1520c57dfccSAnton Vorontsov kdb_printf("\b");
1530c57dfccSAnton Vorontsov return 0;
1540c57dfccSAnton Vorontsov }
1550c57dfccSAnton Vorontsov
1560c57dfccSAnton Vorontsov /**
1570c57dfccSAnton Vorontsov * kgdb_nmi_poll_knock - Check if it is time to enter the debugger
1580c57dfccSAnton Vorontsov *
1590c57dfccSAnton Vorontsov * "Serial ports are often noisy, especially when muxed over another port (we
1600c57dfccSAnton Vorontsov * often use serial over the headset connector). Noise on the async command
1610c57dfccSAnton Vorontsov * line just causes characters that are ignored, on a command line that blocked
1620c57dfccSAnton Vorontsov * execution noise would be catastrophic." -- Colin Cross
1630c57dfccSAnton Vorontsov *
1640c57dfccSAnton Vorontsov * So, this function implements KGDB/KDB knocking on the serial line: we won't
1650c57dfccSAnton Vorontsov * enter the debugger until we receive a known magic phrase (which is actually
1660c57dfccSAnton Vorontsov * "$3#33", known as "escape to KDB" command. There is also a relaxed variant
1670c57dfccSAnton Vorontsov * of knocking, i.e. just pressing the return key is enough to enter the
1680c57dfccSAnton Vorontsov * debugger. And if knocking is disabled, the function always returns 1.
1690c57dfccSAnton Vorontsov */
kgdb_nmi_poll_knock(void)1700c57dfccSAnton Vorontsov bool kgdb_nmi_poll_knock(void)
1710c57dfccSAnton Vorontsov {
1720c57dfccSAnton Vorontsov if (kgdb_nmi_knock < 0)
173cbc7f6bdSJoe Perches return true;
1740c57dfccSAnton Vorontsov
1750c57dfccSAnton Vorontsov while (1) {
1760c57dfccSAnton Vorontsov int ret;
1770c57dfccSAnton Vorontsov
1780c57dfccSAnton Vorontsov ret = kgdb_nmi_poll_one_knock();
1790c57dfccSAnton Vorontsov if (ret == NO_POLL_CHAR)
180cbc7f6bdSJoe Perches return false;
1810c57dfccSAnton Vorontsov else if (ret == 1)
1820c57dfccSAnton Vorontsov break;
1830c57dfccSAnton Vorontsov }
184cbc7f6bdSJoe Perches return true;
1850c57dfccSAnton Vorontsov }
1860c57dfccSAnton Vorontsov
1870c57dfccSAnton Vorontsov /*
1880c57dfccSAnton Vorontsov * The tasklet is cheap, it does not cause wakeups when reschedules itself,
1890c57dfccSAnton Vorontsov * instead it waits for the next tick.
1900c57dfccSAnton Vorontsov */
kgdb_nmi_tty_receiver(struct timer_list * t)191e99e88a9SKees Cook static void kgdb_nmi_tty_receiver(struct timer_list *t)
1920c57dfccSAnton Vorontsov {
193e99e88a9SKees Cook struct kgdb_nmi_tty_priv *priv = from_timer(priv, t, timer);
1940c57dfccSAnton Vorontsov char ch;
1950c57dfccSAnton Vorontsov
1968a0ff60fSDaniel Thompson priv->timer.expires = jiffies + (HZ/100);
1978a0ff60fSDaniel Thompson add_timer(&priv->timer);
1980c57dfccSAnton Vorontsov
199c8b29f04SDaniel Thompson if (likely(!atomic_read(&kgdb_nmi_num_readers) ||
200c8b29f04SDaniel Thompson !kfifo_len(&priv->fifo)))
2010c57dfccSAnton Vorontsov return;
2020c57dfccSAnton Vorontsov
2030c57dfccSAnton Vorontsov while (kfifo_out(&priv->fifo, &ch, 1))
20492a19f9cSJiri Slaby tty_insert_flip_char(&priv->port, ch, TTY_NORMAL);
2052e124b4aSJiri Slaby tty_flip_buffer_push(&priv->port);
2060c57dfccSAnton Vorontsov }
2070c57dfccSAnton Vorontsov
kgdb_nmi_tty_activate(struct tty_port * port,struct tty_struct * tty)2080c57dfccSAnton Vorontsov static int kgdb_nmi_tty_activate(struct tty_port *port, struct tty_struct *tty)
2090c57dfccSAnton Vorontsov {
21006d18289SDaniel Thompson struct kgdb_nmi_tty_priv *priv =
21106d18289SDaniel Thompson container_of(port, struct kgdb_nmi_tty_priv, port);
2120c57dfccSAnton Vorontsov
2130c57dfccSAnton Vorontsov kgdb_nmi_port = port;
2148a0ff60fSDaniel Thompson priv->timer.expires = jiffies + (HZ/100);
2158a0ff60fSDaniel Thompson add_timer(&priv->timer);
2168a0ff60fSDaniel Thompson
2170c57dfccSAnton Vorontsov return 0;
2180c57dfccSAnton Vorontsov }
2190c57dfccSAnton Vorontsov
kgdb_nmi_tty_shutdown(struct tty_port * port)2200c57dfccSAnton Vorontsov static void kgdb_nmi_tty_shutdown(struct tty_port *port)
2210c57dfccSAnton Vorontsov {
22206d18289SDaniel Thompson struct kgdb_nmi_tty_priv *priv =
22306d18289SDaniel Thompson container_of(port, struct kgdb_nmi_tty_priv, port);
2240c57dfccSAnton Vorontsov
2258a0ff60fSDaniel Thompson del_timer(&priv->timer);
2260c57dfccSAnton Vorontsov kgdb_nmi_port = NULL;
2270c57dfccSAnton Vorontsov }
2280c57dfccSAnton Vorontsov
2290c57dfccSAnton Vorontsov static const struct tty_port_operations kgdb_nmi_tty_port_ops = {
2300c57dfccSAnton Vorontsov .activate = kgdb_nmi_tty_activate,
2310c57dfccSAnton Vorontsov .shutdown = kgdb_nmi_tty_shutdown,
2320c57dfccSAnton Vorontsov };
2330c57dfccSAnton Vorontsov
kgdb_nmi_tty_install(struct tty_driver * drv,struct tty_struct * tty)2340c57dfccSAnton Vorontsov static int kgdb_nmi_tty_install(struct tty_driver *drv, struct tty_struct *tty)
2350c57dfccSAnton Vorontsov {
2360c57dfccSAnton Vorontsov struct kgdb_nmi_tty_priv *priv;
2370c57dfccSAnton Vorontsov int ret;
2380c57dfccSAnton Vorontsov
2390c57dfccSAnton Vorontsov priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2400c57dfccSAnton Vorontsov if (!priv)
2410c57dfccSAnton Vorontsov return -ENOMEM;
2420c57dfccSAnton Vorontsov
2430c57dfccSAnton Vorontsov INIT_KFIFO(priv->fifo);
244e99e88a9SKees Cook timer_setup(&priv->timer, kgdb_nmi_tty_receiver, 0);
2450c57dfccSAnton Vorontsov tty_port_init(&priv->port);
2460c57dfccSAnton Vorontsov priv->port.ops = &kgdb_nmi_tty_port_ops;
2470c57dfccSAnton Vorontsov tty->driver_data = priv;
2480c57dfccSAnton Vorontsov
2490c57dfccSAnton Vorontsov ret = tty_port_install(&priv->port, drv, tty);
2500c57dfccSAnton Vorontsov if (ret) {
2510c57dfccSAnton Vorontsov pr_err("%s: can't install tty port: %d\n", __func__, ret);
2520c57dfccSAnton Vorontsov goto err;
2530c57dfccSAnton Vorontsov }
2540c57dfccSAnton Vorontsov return 0;
2550c57dfccSAnton Vorontsov err:
256191c5f10SJiri Slaby tty_port_destroy(&priv->port);
2570c57dfccSAnton Vorontsov kfree(priv);
2580c57dfccSAnton Vorontsov return ret;
2590c57dfccSAnton Vorontsov }
2600c57dfccSAnton Vorontsov
kgdb_nmi_tty_cleanup(struct tty_struct * tty)2610c57dfccSAnton Vorontsov static void kgdb_nmi_tty_cleanup(struct tty_struct *tty)
2620c57dfccSAnton Vorontsov {
2630c57dfccSAnton Vorontsov struct kgdb_nmi_tty_priv *priv = tty->driver_data;
2640c57dfccSAnton Vorontsov
2650c57dfccSAnton Vorontsov tty->driver_data = NULL;
266191c5f10SJiri Slaby tty_port_destroy(&priv->port);
2670c57dfccSAnton Vorontsov kfree(priv);
2680c57dfccSAnton Vorontsov }
2690c57dfccSAnton Vorontsov
kgdb_nmi_tty_open(struct tty_struct * tty,struct file * file)2700c57dfccSAnton Vorontsov static int kgdb_nmi_tty_open(struct tty_struct *tty, struct file *file)
2710c57dfccSAnton Vorontsov {
2720c57dfccSAnton Vorontsov struct kgdb_nmi_tty_priv *priv = tty->driver_data;
273c8b29f04SDaniel Thompson unsigned int mode = file->f_flags & O_ACCMODE;
274c8b29f04SDaniel Thompson int ret;
2750c57dfccSAnton Vorontsov
276c8b29f04SDaniel Thompson ret = tty_port_open(&priv->port, tty, file);
277c8b29f04SDaniel Thompson if (!ret && (mode == O_RDONLY || mode == O_RDWR))
278c8b29f04SDaniel Thompson atomic_inc(&kgdb_nmi_num_readers);
279c8b29f04SDaniel Thompson
280c8b29f04SDaniel Thompson return ret;
2810c57dfccSAnton Vorontsov }
2820c57dfccSAnton Vorontsov
kgdb_nmi_tty_close(struct tty_struct * tty,struct file * file)2830c57dfccSAnton Vorontsov static void kgdb_nmi_tty_close(struct tty_struct *tty, struct file *file)
2840c57dfccSAnton Vorontsov {
2850c57dfccSAnton Vorontsov struct kgdb_nmi_tty_priv *priv = tty->driver_data;
286c8b29f04SDaniel Thompson unsigned int mode = file->f_flags & O_ACCMODE;
287c8b29f04SDaniel Thompson
288c8b29f04SDaniel Thompson if (mode == O_RDONLY || mode == O_RDWR)
289c8b29f04SDaniel Thompson atomic_dec(&kgdb_nmi_num_readers);
2900c57dfccSAnton Vorontsov
2910c57dfccSAnton Vorontsov tty_port_close(&priv->port, tty, file);
2920c57dfccSAnton Vorontsov }
2930c57dfccSAnton Vorontsov
kgdb_nmi_tty_hangup(struct tty_struct * tty)2940c57dfccSAnton Vorontsov static void kgdb_nmi_tty_hangup(struct tty_struct *tty)
2950c57dfccSAnton Vorontsov {
2960c57dfccSAnton Vorontsov struct kgdb_nmi_tty_priv *priv = tty->driver_data;
2970c57dfccSAnton Vorontsov
2980c57dfccSAnton Vorontsov tty_port_hangup(&priv->port);
2990c57dfccSAnton Vorontsov }
3000c57dfccSAnton Vorontsov
kgdb_nmi_tty_write_room(struct tty_struct * tty)30103b3b1a2SJiri Slaby static unsigned int kgdb_nmi_tty_write_room(struct tty_struct *tty)
3020c57dfccSAnton Vorontsov {
3030c57dfccSAnton Vorontsov /* Actually, we can handle any amount as we use polled writes. */
3040c57dfccSAnton Vorontsov return 2048;
3050c57dfccSAnton Vorontsov }
3060c57dfccSAnton Vorontsov
kgdb_nmi_tty_write(struct tty_struct * tty,const u8 * buf,size_t c)307*95713967SJiri Slaby (SUSE) static ssize_t kgdb_nmi_tty_write(struct tty_struct *tty, const u8 *buf,
308*95713967SJiri Slaby (SUSE) size_t c)
3090c57dfccSAnton Vorontsov {
3100c57dfccSAnton Vorontsov int i;
3110c57dfccSAnton Vorontsov
3120c57dfccSAnton Vorontsov for (i = 0; i < c; i++)
3130c57dfccSAnton Vorontsov dbg_io_ops->write_char(buf[i]);
3140c57dfccSAnton Vorontsov return c;
3150c57dfccSAnton Vorontsov }
3160c57dfccSAnton Vorontsov
3170c57dfccSAnton Vorontsov static const struct tty_operations kgdb_nmi_tty_ops = {
3180c57dfccSAnton Vorontsov .open = kgdb_nmi_tty_open,
3190c57dfccSAnton Vorontsov .close = kgdb_nmi_tty_close,
3200c57dfccSAnton Vorontsov .install = kgdb_nmi_tty_install,
3210c57dfccSAnton Vorontsov .cleanup = kgdb_nmi_tty_cleanup,
3220c57dfccSAnton Vorontsov .hangup = kgdb_nmi_tty_hangup,
3230c57dfccSAnton Vorontsov .write_room = kgdb_nmi_tty_write_room,
3240c57dfccSAnton Vorontsov .write = kgdb_nmi_tty_write,
3250c57dfccSAnton Vorontsov };
3260c57dfccSAnton Vorontsov
kgdb_register_nmi_console(void)3270c57dfccSAnton Vorontsov int kgdb_register_nmi_console(void)
3280c57dfccSAnton Vorontsov {
3290c57dfccSAnton Vorontsov int ret;
3300c57dfccSAnton Vorontsov
3310c57dfccSAnton Vorontsov if (!arch_kgdb_ops.enable_nmi)
3320c57dfccSAnton Vorontsov return 0;
3330c57dfccSAnton Vorontsov
33439b7b42bSJiri Slaby kgdb_nmi_tty_driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
33539b7b42bSJiri Slaby if (IS_ERR(kgdb_nmi_tty_driver)) {
3360c57dfccSAnton Vorontsov pr_err("%s: cannot allocate tty\n", __func__);
33739b7b42bSJiri Slaby return PTR_ERR(kgdb_nmi_tty_driver);
3380c57dfccSAnton Vorontsov }
3390c57dfccSAnton Vorontsov kgdb_nmi_tty_driver->driver_name = "ttyNMI";
3400c57dfccSAnton Vorontsov kgdb_nmi_tty_driver->name = "ttyNMI";
3410c57dfccSAnton Vorontsov kgdb_nmi_tty_driver->num = 1;
3420c57dfccSAnton Vorontsov kgdb_nmi_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
3430c57dfccSAnton Vorontsov kgdb_nmi_tty_driver->subtype = SERIAL_TYPE_NORMAL;
3440c57dfccSAnton Vorontsov kgdb_nmi_tty_driver->init_termios = tty_std_termios;
3450c57dfccSAnton Vorontsov tty_termios_encode_baud_rate(&kgdb_nmi_tty_driver->init_termios,
3460c57dfccSAnton Vorontsov KGDB_NMI_BAUD, KGDB_NMI_BAUD);
3470c57dfccSAnton Vorontsov tty_set_operations(kgdb_nmi_tty_driver, &kgdb_nmi_tty_ops);
3480c57dfccSAnton Vorontsov
3490c57dfccSAnton Vorontsov ret = tty_register_driver(kgdb_nmi_tty_driver);
3500c57dfccSAnton Vorontsov if (ret) {
3510c57dfccSAnton Vorontsov pr_err("%s: can't register tty driver: %d\n", __func__, ret);
3520c57dfccSAnton Vorontsov goto err_drv_reg;
3530c57dfccSAnton Vorontsov }
3540c57dfccSAnton Vorontsov
3550c57dfccSAnton Vorontsov register_console(&kgdb_nmi_console);
3560c57dfccSAnton Vorontsov
3570c57dfccSAnton Vorontsov return 0;
3580c57dfccSAnton Vorontsov err_drv_reg:
3599f90a4ddSJiri Slaby tty_driver_kref_put(kgdb_nmi_tty_driver);
3600c57dfccSAnton Vorontsov return ret;
3610c57dfccSAnton Vorontsov }
3620c57dfccSAnton Vorontsov EXPORT_SYMBOL_GPL(kgdb_register_nmi_console);
3630c57dfccSAnton Vorontsov
kgdb_unregister_nmi_console(void)3640c57dfccSAnton Vorontsov int kgdb_unregister_nmi_console(void)
3650c57dfccSAnton Vorontsov {
3660c57dfccSAnton Vorontsov int ret;
3670c57dfccSAnton Vorontsov
3680c57dfccSAnton Vorontsov if (!arch_kgdb_ops.enable_nmi)
3690c57dfccSAnton Vorontsov return 0;
3700c57dfccSAnton Vorontsov arch_kgdb_ops.enable_nmi(0);
3710c57dfccSAnton Vorontsov
3720c57dfccSAnton Vorontsov ret = unregister_console(&kgdb_nmi_console);
3730c57dfccSAnton Vorontsov if (ret)
3740c57dfccSAnton Vorontsov return ret;
3750c57dfccSAnton Vorontsov
3766c2e6317SJiri Slaby tty_unregister_driver(kgdb_nmi_tty_driver);
3779f90a4ddSJiri Slaby tty_driver_kref_put(kgdb_nmi_tty_driver);
3780c57dfccSAnton Vorontsov
3790c57dfccSAnton Vorontsov return 0;
3800c57dfccSAnton Vorontsov }
3810c57dfccSAnton Vorontsov EXPORT_SYMBOL_GPL(kgdb_unregister_nmi_console);
382