From patchwork Wed Oct 11 19:18:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Martin X-Patchwork-Id: 10000401 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id AEFEB60244 for ; Wed, 11 Oct 2017 19:21:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 92B8328B49 for ; Wed, 11 Oct 2017 19:21:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8742B28B4D; Wed, 11 Oct 2017 19:21:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8F4BB28B49 for ; Wed, 11 Oct 2017 19:21:02 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2MWW-0002Bg-LO; Wed, 11 Oct 2017 19:18:32 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e2MWU-0002BV-KL for xen-devel@lists.xenproject.org; Wed, 11 Oct 2017 19:18:30 +0000 Received: from [193.109.254.147] by server-8.bemta-6.messagelabs.com id 36/6E-13910-58E6ED95; Wed, 11 Oct 2017 19:18:29 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrIIsWRWlGSWpSXmKPExsVysyfVTbcp716 kwaZt1hbft0xmcmD0OPzhCksAYxRrZl5SfkUCa0bT6UnsBT+SK56ve8LWwLjIt4uRi0NIYAOj xOO3e1ghnFOMEq8+P2DsYuTgYBFQlXg4o7KLkZODTUBdYtuKM2wgtoiAkcSLR5dZQOqZBdYwS nxbvwisWViggVFi07V2RpAqXgE7ibn9HcwQUxuZJVYc3sgMkRCUODnzCQuIzSygJXHj30smkG 3MAtISy/9xgIQ5BQIl5l+/wg5iiwqoSFyZ8JZ9AiPfLCTds5B0z0LoXsDIvIpRozi1qCy1SNf YUC+pKDM9oyQ3MTNH19DATC83tbg4MT01JzGpWC85P3cTIzDgGIBgB2PTosBDjJIcTEqivA9j 7kUK8SXlp1RmJBZnxBeV5qQWH2KU4eBQkuBdkwuUEyxKTU+tSMvMAYY+TFqCg0dJhDcSJM1bX JCYW5yZDpE6xWjMseL67T9MHI9u3P3DJMSSl5+XKiXOWwVSKgBSmlGaBzcIFpOXGGWlhHkZgU 4T4ilILcrNLEGVf8UozsGoJMwbAzKFJzOvBG7fK6BTmIBOEU27A3JKSSJCSqqBMedO4Kmvecx LI8x6Wc04ZLS5JkzsNv9588IylhKeI5P3tHsJXJRIntH592TLI+M5+8/OEwsK5Mn5/2HCRT8B /4J5G1pSZrSsXJP0ddnvJx3XYj647F4jdTP9wZTNRy57RvM/mj9hY1+tdfXWoPBvZQsVlpdrr hZYv8ep9elzxf+/5Ha0tAn5lCmxFGckGmoxFxUnAgDR+GqdxAIAAA== X-Env-Sender: Dave.Martin@arm.com X-Msg-Ref: server-6.tower-27.messagelabs.com!1507749506!111417980!1 X-Originating-IP: [217.140.101.70] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.45; banners=-,-,- X-VirusChecked: Checked Received: (qmail 17574 invoked from network); 11 Oct 2017 19:18:26 -0000 Received: from foss.arm.com (HELO foss.arm.com) (217.140.101.70) by server-6.tower-27.messagelabs.com with SMTP; 11 Oct 2017 19:18:26 -0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6F664F; Wed, 11 Oct 2017 12:18:25 -0700 (PDT) Received: from e103592.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5FB733F599; Wed, 11 Oct 2017 12:18:24 -0700 (PDT) Date: Wed, 11 Oct 2017 20:18:21 +0100 From: Dave Martin To: Bhupinder Thakur Message-ID: <20171011191821.GF19485@e103592.cambridge.arm.com> References: <1506068606-17066-1-git-send-email-bhupinder.thakur@linaro.org> <1506068606-17066-28-git-send-email-bhupinder.thakur@linaro.org> <20170926143816.GB17434@e103592.cambridge.arm.com> <20171011100802.GA19485@e103592.cambridge.arm.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) Cc: Xen-devel , Julien Grall , Stefano Stabellini , Andre Przywara Subject: Re: [Xen-devel] [PATCH 27/27 v10] xen/arm: vpl011: Correct the logic for asserting/de-asserting SBSA UART TX interrupt X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP On Wed, Oct 11, 2017 at 07:21:43PM +0530, Bhupinder Thakur wrote: > On 11 October 2017 at 15:38, Dave Martin wrote: > > On Wed, Oct 11, 2017 at 01:28:44PM +0530, Bhupinder Thakur wrote: > >> Hi Dave, > >> > >> On 26 September 2017 at 20:08, Dave Martin wrote: > >> > On Fri, Sep 22, 2017 at 01:53:26PM +0530, Bhupinder Thakur wrote: > >> >> This patch fixes the issue observed when pl011 patches were tested on > >> >> the junos hardware by Andre/Julien. It was observed that when large output is > >> >> generated such as on running 'find /', output was getting truncated intermittently > >> >> due to OUT ring buffer getting full. > >> >> > >> >> This issue was due to the fact that the SBSA UART driver expects that when > >> >> a TX interrupt is asserted then the FIFO queue should be atleast half empty and > >> >> that it can write N bytes in the FIFO, where N is half the FIFO queue size, without > >> >> the bytes getting dropped due to FIFO getting full. > >> >> > >> >> The SBSA UART emulation logic was asserting the TX interrupt as soon as > >> >> any space became available in the FIFO and the SBSA UART driver tried to write > >> >> more data (upto 16 bytes) in the FIFO expecting that there is enough space > >> >> available leading to dropped bytes. > >> >> > >> >> The SBSA spec [1] does not specify when the TX interrupt should be asserted > >> >> or de-asserted. Due to lack of clarity on the expected behavior, it is > >> >> assumed for now that TX interrupt should be asserted only when the FIFO goes > >> >> half empty. > >> >> > >> >> TBD: Once the SBSA spec is updated with the expected behavior, the implementation > >> >> will be modified to align with the spec requirement. > >> >> > >> >> [1] http://infocenter.arm.com/help/topic/com.arm.doc.ddi0183f/DDI0183.pdf > >> >> > >> >> Signed-off-by: Bhupinder Thakur > >> >> --- > >> >> CC: Julien Grall > >> >> CC: Andre Przywara > >> >> CC: Stefano Stabellini > >> > > >> > (Taking a quick look at this because I remember fighthing with FIFO > >> > behaviour issues when hacking the Linux driver -- but beware, I'm not a > >> > Xen guy...) > >> > > >> > > >> > Should this patch be flattened into the patches is fixes? Keeping > >> > known-wrong code in the series does not help reviewers (but maybe it's > >> > the Xen way). > >> > > >> >> Changes since v8: > >> >> - Used variables fifo_level/fifo_threshold for more clarity > >> >> - Added a new macro SBSA_UART_FIFO_SIZE instead of using a magic number > >> > > >> > What's sizeof(intf->in), sizeof(intf->out)? > >> > > >> > For correct operation, you assume that the total ring buffer size is >= > >> > SBSA_UART_FIFO_SIZE, but nothing enforces this. If the xencons ring > >> > buffer size is set elsewhere and can't be chosen by a driver, you may at > >> > least add a build-time assert to check that it's big enough. > >> > > >> I will add an assert to check this condition. > >> > >> > [...] > >> > > >> >> @@ -144,28 +148,41 @@ static void vpl011_write_data(struct domain *d, uint8_t data) > >> > > >> > [...] > >> > > >> >> + * Clear the TXI bit if the fifo level exceeds fifo_size/2 mark which > >> >> + * is the trigger level for asserting/de-assterting the TX interrupt. > >> >> */ > >> >> - vpl011->uartfr |= BUSY; > >> >> + fifo_threshold = sizeof (intf->out) - SBSA_UART_FIFO_SIZE/2; > >> >> + > >> >> + if ( fifo_level <= fifo_threshold ) > >> >> + vpl011->uartris |= TXI; > >> >> + else > >> >> + vpl011->uartris &= ~TXI; > >> >> } > >> >> + else > >> >> + gprintk(XENLOG_ERR, "vpl011: Unexpected OUT ring buffer full\n"); > >> >> > >> >> vpl011->uartfr &= ~TXFE; > >> >> > >> > > >> > [...] > >> > > >> >> @@ -353,37 +370,51 @@ static void vpl011_data_avail(struct domain *d) > >> >> > >> >> smp_rmb(); > >> >> > >> >> - in_ring_qsize = xencons_queued(in_prod, > >> >> + in_fifo_level = xencons_queued(in_prod, > >> >> in_cons, > >> >> sizeof(intf->in)); > >> >> > >> >> - out_ring_qsize = xencons_queued(out_prod, > >> >> + out_fifo_level = xencons_queued(out_prod, > >> >> out_cons, > >> >> sizeof(intf->out)); > >> >> > >> >> /* Update the uart rx state if the buffer is not empty. */ > >> >> - if ( in_ring_qsize != 0 ) > >> >> + if ( in_fifo_level != 0 ) > >> >> { > >> >> vpl011->uartfr &= ~RXFE; > >> >> - if ( in_ring_qsize == sizeof(intf->in) ) > >> >> + > >> >> + if ( in_fifo_level == sizeof(intf->in) ) > >> >> vpl011->uartfr |= RXFF; > >> >> + > >> >> vpl011->uartris |= RXI; > >> >> } > >> >> > >> >> /* Update the uart tx state if the buffer is not full. */ > >> >> - if ( out_ring_qsize != sizeof(intf->out) ) > >> >> + if ( out_fifo_level != sizeof(intf->out) ) > >> >> { > >> >> + unsigned int out_fifo_threshold; > >> >> + > >> >> vpl011->uartfr &= ~TXFF; > >> >> - vpl011->uartris |= TXI; > >> >> > >> >> /* > >> >> - * Clear the BUSY bit as soon as space becomes available > >> >> + * Clear the BUSY bit as soon as space becomes avaliable > >> >> * so that the SBSA UART driver can start writing more data > >> >> * without any further delay. > >> >> */ > >> >> vpl011->uartfr &= ~BUSY; > >> >> > >> >> - if ( out_ring_qsize == 0 ) > >> >> + /* > >> >> + * Set the TXI bit only when there is space for fifo_size/2 bytes which > >> >> + * is the trigger level for asserting/de-assterting the TX interrupt. > >> >> + */ > >> >> + out_fifo_threshold = sizeof(intf->out) - SBSA_UART_FIFO_SIZE/2; > >> >> + > >> >> + if ( out_fifo_level <= out_fifo_threshold ) > >> >> + vpl011->uartris |= TXI; > >> >> + else > >> >> + vpl011->uartris &= ~TXI; > >> > > >> > Should this logic be factored out? You do the same thing in > >> > _write_data(). > >> I will add a common function to set the TXI bit. > >> > > >> > Also, is there a reason why you implement the trigger threshold logic on > >> > the TX side only? It looks inconsistent now. > >> I did try with RX FIFO threshold also but it seems the current pl011 > >> driver does not > >> poll on the RX FIFO and just waits for the RX interrupt trigger to > >> start processing the RX data. > >> This makes RX very slow and if the RX FIFO is not filled sufficiently, > >> it does not read data further. > >> > > >> > I think a real PL011 implements the trigger logic in exactly the same > >> > way for RX and TX (except for swapping >= for <= in the threshold > >> > comparison). > >> > > >> > > >> > It doesn't look like the Linux pl011 driver relies on a correctly > >> > implemented RX trigger level today, but it may have done in the past -- > >> > I did some hacking in this area at some point, but can't remember the > >> > details now. > >> > > >> The current pl011 driver > >> > Asserting RXI whenever the RX FIFO is nonempty would result in excessive > >> > interrupts if you are streaming the data from a slow source (such as a > >> > real UART) and pushing the chars one by one to the emulated UART: the > >> > guest would take an IRQ on each char rather than waiting until the RX > >> > FIFO is half-full. > >> > > >> I agree it is an overhead. This may be an issue with the driver which > >> is solely depending on the RX > >> interrupt. I think it should switch to polling if there are no > >> interrupts received recently. > > > > Hmmm, good point, but isn't that what the receive timeout interrupt is > > supposed to be for? > > > > The Linux driver seems to rely on the receive timeout interrupt > > to recover from an RX stall when the FIFO is not empty but also not full > > enough to trigger the RX FIFO interrupt. > > > > Does your driver actually implement the receive timeout interrupt? > > I'm not very familiar with the code, so I may have missed it. > > This patch emulates the SBSA UART spec, which is a subset of the pl011 > UART. The SBSA spec [1], Appendix B does not define the requirement of > supporting RX timeout interrupt. I took another look at the SBSA spec -- it is certainly vague/ambiguous in this area. There is no statement that you must implement all the interrupts, but there is also no statement that you are allowed to not implement any of them. The RX FIFO interrupt and receive timeout interrupt are not treated differently in the spec. I think that an SBSA UART client driver _could_ legitimately assume that the RX FIFO interrupt is never asserted until the proper trigger threshold is reached, but this does not mean that there is any driver out in the wild that actually does this. A quick hack to the Linux PL011 driver suggests that polling the RXFE status before reading each character can safely be optimised away after a RX FIFO interrupt, until 16 or more chars have been read from the FIFO. This seems to work with the real PL011 on Juno r0 and the emulated PL011 in the ARM fast model. I don't plan to upstream this hack though. Probably there are already SBSA UART implementations out there that are incompatible on this. So for now, it's really a judgement call. I will raise a clarification request on the SBSA spec, but I suspect that you can get away with the current approach. It would be a good idea to add comments explaining the design decisions / shortcuts here, since this issue may come up again later. Cheers ---Dave My dodgy Linux driver hack below -- the debugfs stuff just provides a way to prove that both types of interrupt actually happen. --8<-- diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl011.c index 111e6a9..36e00cb 100644 --- a/drivers/tty/serial/amba-pl011.c +++ b/drivers/tty/serial/amba-pl011.c @@ -59,6 +59,7 @@ #include #include #include +#include #include "amba-pl011.h" @@ -73,6 +74,8 @@ #define UART_DR_ERROR (UART011_DR_OE|UART011_DR_BE|UART011_DR_PE|UART011_DR_FE) #define UART_DUMMY_DR_RX (1 << 16) +static u32 rxis_count, rtis_count; + static u16 pl011_std_offsets[REG_ARRAY_SIZE] = { [REG_DR] = UART01x_DR, [REG_FR] = UART01x_FR, @@ -325,15 +328,13 @@ static void pl011_write(unsigned int val, const struct uart_amba_port *uap, * inserts them into the TTY layer. Returns the number of characters * read from the FIFO. */ -static int pl011_fifo_to_tty(struct uart_amba_port *uap) +static int pl011_fifo_to_tty(struct uart_amba_port *uap, int avail) { - u16 status; unsigned int ch, flag, max_count = 256; int fifotaken = 0; while (max_count--) { - status = pl011_read(uap, REG_FR); - if (status & UART01x_FR_RXFE) + if (avail-- <= 0 && (pl011_read(uap, REG_FR) & UART01x_FR_RXFE)) break; /* Take chars from the FIFO and update status */ @@ -954,7 +955,7 @@ static void pl011_dma_rx_chars(struct uart_amba_port *uap, * will always find the error in the FIFO, never in the DMA * buffer. */ - fifotaken = pl011_fifo_to_tty(uap); + fifotaken = pl011_fifo_to_tty(uap, 0); } spin_unlock(&uap->port.lock); @@ -1361,11 +1362,11 @@ static void pl011_enable_ms(struct uart_port *port) pl011_write(uap->im, uap, REG_IMSC); } -static void pl011_rx_chars(struct uart_amba_port *uap) +static void pl011_rx_chars(struct uart_amba_port *uap, int avail) __releases(&uap->port.lock) __acquires(&uap->port.lock) { - pl011_fifo_to_tty(uap); + pl011_fifo_to_tty(uap, avail); spin_unlock(&uap->port.lock); tty_flip_buffer_push(&uap->port.state->port); @@ -1515,8 +1516,15 @@ static irqreturn_t pl011_int(int irq, void *dev_id) if (status & (UART011_RTIS|UART011_RXIS)) { if (pl011_dma_rx_running(uap)) pl011_dma_rx_irq(uap); - else - pl011_rx_chars(uap); + else { + if (status & UART011_RXIS) + ++rxis_count; + else + ++rtis_count; + + pl011_rx_chars(uap, + (status & UART011_RXIS) ? 16 : 1); + } } if (status & (UART011_DSRMIS|UART011_DCDMIS| UART011_CTSMIS|UART011_RIMIS)) @@ -2822,6 +2830,9 @@ static int __init pl011_init(void) { printk(KERN_INFO "Serial: AMBA PL011 UART driver\n"); + debugfs_create_u32("pl011-rxis", 0444, NULL, &rxis_count); + debugfs_create_u32("pl011-rtis", 0444, NULL, &rtis_count); + if (platform_driver_register(&arm_sbsa_uart_platform_driver)) pr_warn("could not register SBSA UART platform driver\n"); return amba_driver_register(&pl011_driver);