bcm27xx-userland: update to latest version
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0267-tty-amba-pl011-Make-TX-optimisation-conditional.patch
1 From 76e24a2218069fadb28c0c2f5d813302ad5f85a3 Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.org>
3 Date: Thu, 11 Jul 2019 13:13:39 +0100
4 Subject: [PATCH] tty: amba-pl011: Make TX optimisation conditional
5
6 pl011_tx_chars takes a "from_irq" parameter to reduce the number of
7 register accesses. When from_irq is true the function assumes that the
8 FIFO is half empty and writes up to half a FIFO's worth of bytes
9 without polling the FIFO status register, the reasoning being that
10 the function is being called as a result of the TX interrupt being
11 raised. This logic would work were it not for the fact that
12 pl011_rx_chars, called from pl011_int before pl011_tx_chars, releases
13 the spinlock before calling tty_flip_buffer_push.
14
15 A user thread writing to the UART claims the spinlock and ultimately
16 calls pl011_tx_chars with from_irq set to false. This reverts to the
17 older logic that polls the FIFO status register before sending every
18 byte. If this happen on an SMP system during the section of the IRQ
19 handler where the spinlock has been released, then by the time the TX
20 interrupt handler is called, the FIFO may already be full, and any
21 further writes are likely to be lost.
22
23 The fix involves adding a per-port flag that is true iff running from
24 within the interrupt handler and the spinlock has not yet been released.
25 This flag is then used as the value for the from_irq parameter of
26 pl011_tx_chars, causing polling to be used in the unsafe case.
27
28 Fixes: 1e84d22322ce ("serial/amba-pl011: Refactor and simplify TX FIFO handling")
29
30 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
31 ---
32 drivers/tty/serial/amba-pl011.c | 7 ++++++-
33 1 file changed, 6 insertions(+), 1 deletion(-)
34
35 --- a/drivers/tty/serial/amba-pl011.c
36 +++ b/drivers/tty/serial/amba-pl011.c
37 @@ -270,6 +270,7 @@ struct uart_amba_port {
38 unsigned int old_cr; /* state during shutdown */
39 unsigned int fixed_baud; /* vendor-set fixed baud rate */
40 char type[12];
41 + bool irq_locked; /* in irq, unreleased lock */
42 #ifdef CONFIG_DMA_ENGINE
43 /* DMA stuff */
44 bool using_tx_dma;
45 @@ -813,6 +814,7 @@ __acquires(&uap->port.lock)
46 if (!uap->using_tx_dma)
47 return;
48
49 + uap->irq_locked = 0;
50 dmaengine_terminate_async(uap->dmatx.chan);
51
52 if (uap->dmatx.queued) {
53 @@ -939,6 +941,7 @@ static void pl011_dma_rx_chars(struct ua
54 fifotaken = pl011_fifo_to_tty(uap);
55 }
56
57 + uap->irq_locked = 0;
58 spin_unlock(&uap->port.lock);
59 dev_vdbg(uap->port.dev,
60 "Took %d chars from DMA buffer and %d chars from the FIFO\n",
61 @@ -1347,6 +1350,7 @@ __acquires(&uap->port.lock)
62 {
63 pl011_fifo_to_tty(uap);
64
65 + uap->irq_locked = 0;
66 spin_unlock(&uap->port.lock);
67 tty_flip_buffer_push(&uap->port.state->port);
68 /*
69 @@ -1482,6 +1486,7 @@ static irqreturn_t pl011_int(int irq, vo
70 int handled = 0;
71
72 spin_lock_irqsave(&uap->port.lock, flags);
73 + uap->irq_locked = 1;
74 status = pl011_read(uap, REG_RIS) & uap->im;
75 if (status) {
76 do {
77 @@ -1501,7 +1506,7 @@ static irqreturn_t pl011_int(int irq, vo
78 UART011_CTSMIS|UART011_RIMIS))
79 pl011_modem_status(uap);
80 if (status & UART011_TXIS)
81 - pl011_tx_chars(uap, true);
82 + pl011_tx_chars(uap, uap->irq_locked);
83
84 if (pass_counter-- == 0)
85 break;