From patchwork Tue Jul 7 15:05:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sumit Garg X-Patchwork-Id: 235015 Delivered-To: patches@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp997437ilg; Tue, 7 Jul 2020 08:05:50 -0700 (PDT) X-Received: by 2002:aca:d60c:: with SMTP id n12mr3370554oig.167.1594134350516; Tue, 07 Jul 2020 08:05:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594134350; cv=none; d=google.com; s=arc-20160816; b=0i7zLRuOQCtOq0YvMUfdwkN6E+ELV1BVGX+N4LKEen8yZMBJSUVM4rbcPpA1Qv7Vit JLrFFE5nhC9E2UqmVLxNBN9CAd0q5/uHKDNyeaWnC0X6jpSwJTzfgtrbsNVGqgPAXkxy zs8+O2n9Q9CrP5RXddw/O6EJNoXnKbq9HPx1HsN6owV7m8FqaGh9VaIV6LF9AO0ZB/Dg HAJSqR6z0SNDRr+lbqIIPI9B+6AB+S2cDvrMF8ysLHhn6gPKaq+znAJTDSDZwDK12y+S MyxNqkPas7jkecz9+u2oou0Ee+rX69OFWTmXnZWMcH/yN7jm50r1kWlbGXVhUTwWtBtw qy3A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PF+W7HY3w0Eb0axwi89t/ACyKgPGheukJTEkjWnsErY=; b=wwdlRJ3Ulbqs6phfZbdbWDFmUjry3VhhSj0BkdSCkfyYNmHivaAYwQLQ1OMHkbBFuv 4YV0/jT+8XbIdmX5F4GfwNp+7DOd6jS17/1dQRWXRvTIif5zoDd1D3y8dI3jUyxVs/Kb Hr44W4sDT2waPC9O8A4Au0aLql8pkhP6ZyE3sZxTBhBxBErzvRXDdCUhi5WhO8w1ZipH R+9FabGuu8Mg0UCXKQLzg4mjvwMjpdGKEXBpJh6Kce6ighMfwYVE5q4ZbC9+A+UcSgx8 /bzEoogjkeV4HkOAREpQpBao8p5lhbaD6NXmLYY6OvTkS5MEMfoxndJ05PgWsfbh/MpF SDsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Xu5Hkrc+; spf=pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=sumit.garg@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id r1sor14094519ote.58.2020.07.07.08.05.50 for (Google Transport Security); Tue, 07 Jul 2020 08:05:50 -0700 (PDT) Received-SPF: pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Xu5Hkrc+; spf=pass (google.com: domain of sumit.garg@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=sumit.garg@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=PF+W7HY3w0Eb0axwi89t/ACyKgPGheukJTEkjWnsErY=; b=Xu5Hkrc+9ojHOONv31XWiLSniGIuYs6QuHXRXoawJJ5j4+/EMvsfHA180QORi5e4pH Q93gV2GiftaijagJl+FxLPaLK8UU1Hp84mTmjkZB6QUsbqeHXETMHd8Ic+0Bckh5c9dU lk2AHjqyguflxcPBg8XUC8VGivUGb2g41poTBLZwxgB9/pgEap6SxYMwqnfxCMOEfEyO 6oeVZ5a24Zfkl1rnJqxC7/dRXr6mWd+GGBK6c9WNj9gAh9o+5VXbiTREs1J/9mhx67KF HNS5jViHlqkPEhBqNAMmLzKz/jb0CApRmhpJcZerjj8fSBOJWtQfqxt1gy/vymneVVF/ vGaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=PF+W7HY3w0Eb0axwi89t/ACyKgPGheukJTEkjWnsErY=; b=N/kVaQN3WY12ulfsdjc2boXiFU/14fuAdg44z6D938A0H04G8VDWG2VhSWoxB3jU26 +LqZL+vkcYg+fmDaZ3rd/72m6zTjBZh/ZfhUxR+C6tooM7RoPIdK9DiGUti0zL82Fsr+ eFbYKeEFzOT5WmXCXC/KJd3K6iJcRwH4ZX/HG9r+PINK1BDqsqRLscX0I2DsBqWv35Vz cOvcKatSwYy5YPK40+otFZ3DmOHnDHesqkKBRVFLeWDbNmPul1bbMM795kaDI81mIms2 WX9sqyF5g+alOOMJqV9lnzfyW8a2bOKYTNu9WcPKcZ2oqNiCZ4QPqYyQX1dNkw5HKVYP OC6Q== X-Gm-Message-State: AOAM531EKl3Y64d5TYTjp0/JvBSBAhkzsPg5bkZ06wF3L9fzGktMbpYW /MOXbbmQthiulSumS5RaIo0Wh+ze68I81w== X-Google-Smtp-Source: ABdhPJzbQcy1TqYiN1liLuuWa2Hwfgf+g5ng9a+85OVndUnDNT+JLUGXlxSLv2UnTM4cCk5p4kxvnA== X-Received: by 2002:a17:90a:9a98:: with SMTP id e24mr4829032pjp.141.1594134350085; Tue, 07 Jul 2020 08:05:50 -0700 (PDT) Return-Path: Received: from localhost.localdomain ([117.252.67.186]) by smtp.gmail.com with ESMTPSA id y18sm23400656pff.10.2020.07.07.08.05.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 07 Jul 2020 08:05:49 -0700 (PDT) From: Sumit Garg To: daniel.thompson@linaro.org Cc: patches@linaro.org, Sumit Garg Subject: [RFC INTERNAL 2/2] serial: amba-pl011: Allow uart interrupt to be NMI Date: Tue, 7 Jul 2020 20:35:25 +0530 Message-Id: <1594134325-7912-2-git-send-email-sumit.garg@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1594134325-7912-1-git-send-email-sumit.garg@linaro.org> References: <1594134325-7912-1-git-send-email-sumit.garg@linaro.org> With the advent on pseudo NMIs on arm64 its now possible to request serial interrupt as an NMI. So allow uart interrupt to be requested as an NMI in polling mode. This feaure is especially useful for NMI debugging where the serial device is operating in polling mode. Currently only RX interrupt has been enabled as NMI and TX operates in polling mode which has a performance overhead when compared with TX operating in interrupt mode. Signed-off-by: Sumit Garg --- drivers/tty/serial/amba-pl011.c | 177 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 2 deletions(-) -- 2.7.4 diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c index 8efd7c2..05bfe9f 100644 --- a/drivers/tty/serial/amba-pl011.c +++ b/drivers/tty/serial/amba-pl011.c @@ -41,6 +41,10 @@ #include #include #include +#include +#include +#include +#include #include "amba-pl011.h" @@ -273,6 +277,12 @@ struct uart_amba_port { struct pl011_dmatx_data dmatx; bool dma_probed; #endif +#ifdef CONFIG_CONSOLE_POLL + bool poll_mode_active; + struct irq_work nmi_work; + DECLARE_KFIFO(nmi_fifo, unsigned int, 256); + DECLARE_KFIFO(nmi_fifo_flag, unsigned int, 256); +#endif }; static unsigned int pl011_reg_to_offset(const struct uart_amba_port *uap, @@ -347,6 +357,13 @@ static int pl011_fifo_to_tty(struct uart_amba_port *uap) if (uart_handle_sysrq_char(&uap->port, ch & 255)) continue; +#ifdef CONFIG_CONSOLE_POLL + if (in_nmi()) { + kfifo_in(&uap->nmi_fifo, &ch, 1); + kfifo_in(&uap->nmi_fifo_flag, &flag, 1); + continue; + } +#endif uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag); } @@ -1286,6 +1303,10 @@ static void pl011_stop_tx(struct uart_port *port) struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port); +#ifdef CONFIG_CONSOLE_POLL + if (uap->poll_mode_active) + return; +#endif uap->im &= ~UART011_TXIM; pl011_write(uap->im, uap, REG_IMSC); pl011_dma_tx_stop(uap); @@ -1302,11 +1323,21 @@ static void pl011_start_tx_pio(struct uart_amba_port *uap) } } +#ifdef CONFIG_CONSOLE_POLL +static void pl011_poll_tx_chars(struct uart_amba_port *uap); +#endif + static void pl011_start_tx(struct uart_port *port) { struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port); +#ifdef CONFIG_CONSOLE_POLL + if (uap->poll_mode_active) { + pl011_poll_tx_chars(uap); + return; + } +#endif if (!pl011_dma_tx_start(uap)) pl011_start_tx_pio(uap); } @@ -1316,6 +1347,10 @@ static void pl011_stop_rx(struct uart_port *port) struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port); +#ifdef CONFIG_CONSOLE_POLL + if (uap->poll_mode_active) + return; +#endif uap->im &= ~(UART011_RXIM|UART011_RTIM|UART011_FEIM| UART011_PEIM|UART011_BEIM|UART011_OEIM); pl011_write(uap->im, uap, REG_IMSC); @@ -1637,6 +1672,128 @@ static void pl011_put_poll_char(struct uart_port *port, pl011_write(ch, uap, REG_DR); } +static void pl011_poll_tx_chars(struct uart_amba_port *uap) +{ + struct circ_buf *xmit = &uap->port.state->xmit; + + if (uap->port.x_char) { + pl011_put_poll_char(&uap->port, uap->port.x_char); + uap->port.x_char = 0; + } + + while (!uart_circ_empty(xmit)) { + pl011_put_poll_char(&uap->port, xmit->buf[xmit->tail]); + uap->port.icount.tx++; + + xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); + } +} + +static void pl011_nmi_tty_work(struct irq_work *nmi_work) +{ + struct uart_amba_port *uap = + container_of(nmi_work, struct uart_amba_port, nmi_work); + unsigned int ch, flag; + + if (!uap->port.state || !tty_port_active(&uap->port.state->port)) { + kfifo_reset(&uap->nmi_fifo); + kfifo_reset(&uap->nmi_fifo_flag); + return; + } + + if (unlikely(!kfifo_len(&uap->nmi_fifo) || + !kfifo_len(&uap->nmi_fifo_flag))) + return; + + spin_lock(&uap->port.lock); + while (kfifo_out(&uap->nmi_fifo, &ch, 1) && + kfifo_out(&uap->nmi_fifo_flag, &flag, 1)) + uart_insert_char(&uap->port, ch, UART011_DR_OE, ch, flag); + spin_unlock(&uap->port.lock); + + tty_flip_buffer_push(&uap->port.state->port); +} + +static void pl011_nmi_rx_chars(struct uart_amba_port *uap) +{ + pl011_fifo_to_tty(uap); + irq_work_queue(&uap->nmi_work); +} + +static irqreturn_t pl011_nmi_int(int irq, void *dev_id) +{ + struct uart_amba_port *uap = dev_id; + unsigned int status, pass_counter = AMBA_ISR_PASS_LIMIT; + int handled = 0; + + status = pl011_read(uap, REG_MIS); + if (status) { + do { + check_apply_cts_event_workaround(uap); + + pl011_write(status, uap, REG_ICR); + + if (status) + pl011_nmi_rx_chars(uap); + + if (pass_counter-- == 0) + break; + + status = pl011_read(uap, REG_MIS); + } while (status != 0); + handled = 1; + } + + return IRQ_RETVAL(handled); +} + +static int pl011_allocate_nmi(struct uart_amba_port *uap) +{ + int ret; + + irq_set_status_flags(uap->port.irq, IRQ_NOAUTOEN); + ret = request_nmi(uap->port.irq, pl011_nmi_int, IRQF_PERCPU, + "uart-pl011", uap); + if (ret) { + irq_clear_status_flags(uap->port.irq, IRQ_NOAUTOEN); + return ret; + } + + enable_irq(uap->port.irq); + + return ret; +} + +static int pl011_hwinit(struct uart_port *port); + +static int pl011_poll_init(struct uart_port *port) +{ + struct uart_amba_port *uap = + container_of(port, struct uart_amba_port, port); + int retval; + + retval = pl011_hwinit(port); + if (retval) + goto clk_dis; + + /* In case NMI isn't supported, fallback to normal interrupt mode */ + retval = pl011_allocate_nmi(uap); + if (retval) + return 0; + + INIT_KFIFO(uap->nmi_fifo); + INIT_KFIFO(uap->nmi_fifo_flag); + init_irq_work(&uap->nmi_work, pl011_nmi_tty_work); + + uap->poll_mode_active = true; + + return 0; + + clk_dis: + clk_disable_unprepare(uap->clk); + return retval; +} + #endif /* CONFIG_CONSOLE_POLL */ static int pl011_hwinit(struct uart_port *port) @@ -1748,6 +1905,10 @@ static int pl011_startup(struct uart_port *port) unsigned int cr; int retval; +#ifdef CONFIG_CONSOLE_POLL + if (uap->poll_mode_active) + return 0; +#endif retval = pl011_hwinit(port); if (retval) goto clk_dis; @@ -1790,6 +1951,10 @@ static int sbsa_uart_startup(struct uart_port *port) container_of(port, struct uart_amba_port, port); int retval; +#ifdef CONFIG_CONSOLE_POLL + if (uap->poll_mode_active) + return 0; +#endif retval = pl011_hwinit(port); if (retval) return retval; @@ -1859,6 +2024,10 @@ static void pl011_shutdown(struct uart_port *port) struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port); +#ifdef CONFIG_CONSOLE_POLL + if (uap->poll_mode_active) + return; +#endif pl011_disable_interrupts(uap); pl011_dma_shutdown(uap); @@ -1891,6 +2060,10 @@ static void sbsa_uart_shutdown(struct uart_port *port) struct uart_amba_port *uap = container_of(port, struct uart_amba_port, port); +#ifdef CONFIG_CONSOLE_POLL + if (uap->poll_mode_active) + return; +#endif pl011_disable_interrupts(uap); free_irq(uap->port.irq, uap); @@ -2142,7 +2315,7 @@ static const struct uart_ops amba_pl011_pops = { .config_port = pl011_config_port, .verify_port = pl011_verify_port, #ifdef CONFIG_CONSOLE_POLL - .poll_init = pl011_hwinit, + .poll_init = pl011_poll_init, .poll_get_char = pl011_get_poll_char, .poll_put_char = pl011_put_poll_char, #endif @@ -2173,7 +2346,7 @@ static const struct uart_ops sbsa_uart_pops = { .config_port = pl011_config_port, .verify_port = pl011_verify_port, #ifdef CONFIG_CONSOLE_POLL - .poll_init = pl011_hwinit, + .poll_init = pl011_poll_init, .poll_get_char = pl011_get_poll_char, .poll_put_char = pl011_put_poll_char, #endif