Skip to content

Commit c5d0666

Browse files
KAGA-KOKOgregkh
authored andcommitted
serial: stm32: Use port lock wrappers
When a serial port is used for kernel console output, then all modifications to the UART registers which are done from other contexts, e.g. getty, termios, are interference points for the kernel console. So far this has been ignored and the printk output is based on the principle of hope. The rework of the console infrastructure which aims to support threaded and atomic consoles, requires to mark sections which modify the UART registers as unsafe. This allows the atomic write function to make informed decisions and eventually to restore operational state. It also allows to prevent the regular UART code from modifying UART registers while printk output is in progress. All modifications of UART registers are guarded by the UART port lock, which provides an obvious synchronization point with the console infrastructure. To avoid adding this functionality to all UART drivers, wrap the spin_[un]lock*() invocations for uart_port::lock into helper functions which just contain the spin_[un]lock*() invocations for now. In a subsequent step these helpers will gain the console synchronization mechanisms. Converted with coccinelle. No functional change. Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: John Ogness <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 47d4dfd commit c5d0666

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

drivers/tty/serial/stm32-usart.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ static void stm32_usart_rx_dma_complete(void *arg)
537537
unsigned int size;
538538
unsigned long flags;
539539

540-
spin_lock_irqsave(&port->lock, flags);
540+
uart_port_lock_irqsave(port, &flags);
541541
size = stm32_usart_receive_chars(port, false);
542542
uart_unlock_and_check_sysrq_irqrestore(port, flags);
543543
if (size)
@@ -643,9 +643,9 @@ static void stm32_usart_tx_dma_complete(void *arg)
643643
stm32_usart_tx_dma_terminate(stm32port);
644644

645645
/* Let's see if we have pending data to send */
646-
spin_lock_irqsave(&port->lock, flags);
646+
uart_port_lock_irqsave(port, &flags);
647647
stm32_usart_transmit_chars(port);
648-
spin_unlock_irqrestore(&port->lock, flags);
648+
uart_port_unlock_irqrestore(port, flags);
649649
}
650650

651651
static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
@@ -889,7 +889,7 @@ static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
889889
if (!stm32_port->throttled) {
890890
if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_started(stm32_port)) ||
891891
((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_started(stm32_port))) {
892-
spin_lock(&port->lock);
892+
uart_port_lock(port);
893893
size = stm32_usart_receive_chars(port, false);
894894
uart_unlock_and_check_sysrq(port);
895895
if (size)
@@ -898,14 +898,14 @@ static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
898898
}
899899

900900
if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
901-
spin_lock(&port->lock);
901+
uart_port_lock(port);
902902
stm32_usart_transmit_chars(port);
903-
spin_unlock(&port->lock);
903+
uart_port_unlock(port);
904904
}
905905

906906
/* Receiver timeout irq for DMA RX */
907907
if (stm32_usart_rx_dma_started(stm32_port) && !stm32_port->throttled) {
908-
spin_lock(&port->lock);
908+
uart_port_lock(port);
909909
size = stm32_usart_receive_chars(port, false);
910910
uart_unlock_and_check_sysrq(port);
911911
if (size)
@@ -993,7 +993,7 @@ static void stm32_usart_throttle(struct uart_port *port)
993993
const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
994994
unsigned long flags;
995995

996-
spin_lock_irqsave(&port->lock, flags);
996+
uart_port_lock_irqsave(port, &flags);
997997

998998
/*
999999
* Pause DMA transfer, so the RX data gets queued into the FIFO.
@@ -1006,7 +1006,7 @@ static void stm32_usart_throttle(struct uart_port *port)
10061006
stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
10071007

10081008
stm32_port->throttled = true;
1009-
spin_unlock_irqrestore(&port->lock, flags);
1009+
uart_port_unlock_irqrestore(port, flags);
10101010
}
10111011

10121012
/* Unthrottle the remote, the input buffer can now accept data. */
@@ -1016,7 +1016,7 @@ static void stm32_usart_unthrottle(struct uart_port *port)
10161016
const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
10171017
unsigned long flags;
10181018

1019-
spin_lock_irqsave(&port->lock, flags);
1019+
uart_port_lock_irqsave(port, &flags);
10201020
stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
10211021
if (stm32_port->cr3_irq)
10221022
stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
@@ -1030,7 +1030,7 @@ static void stm32_usart_unthrottle(struct uart_port *port)
10301030
if (stm32_port->rx_ch)
10311031
stm32_usart_rx_dma_start_or_resume(port);
10321032

1033-
spin_unlock_irqrestore(&port->lock, flags);
1033+
uart_port_unlock_irqrestore(port, flags);
10341034
}
10351035

10361036
/* Receive stop */
@@ -1169,7 +1169,7 @@ static void stm32_usart_set_termios(struct uart_port *port,
11691169

11701170
baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
11711171

1172-
spin_lock_irqsave(&port->lock, flags);
1172+
uart_port_lock_irqsave(port, &flags);
11731173

11741174
ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
11751175
isr,
@@ -1360,7 +1360,7 @@ static void stm32_usart_set_termios(struct uart_port *port,
13601360
writel_relaxed(cr1, port->membase + ofs->cr1);
13611361

13621362
stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1363-
spin_unlock_irqrestore(&port->lock, flags);
1363+
uart_port_unlock_irqrestore(port, flags);
13641364

13651365
/* Handle modem control interrupts */
13661366
if (UART_ENABLE_MS(port, termios->c_cflag))
@@ -1410,9 +1410,9 @@ static void stm32_usart_pm(struct uart_port *port, unsigned int state,
14101410
pm_runtime_get_sync(port->dev);
14111411
break;
14121412
case UART_PM_STATE_OFF:
1413-
spin_lock_irqsave(&port->lock, flags);
1413+
uart_port_lock_irqsave(port, &flags);
14141414
stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
1415-
spin_unlock_irqrestore(&port->lock, flags);
1415+
uart_port_unlock_irqrestore(port, flags);
14161416
pm_runtime_put_sync(port->dev);
14171417
break;
14181418
}
@@ -1895,9 +1895,9 @@ static void stm32_usart_console_write(struct console *co, const char *s,
18951895
int locked = 1;
18961896

18971897
if (oops_in_progress)
1898-
locked = spin_trylock_irqsave(&port->lock, flags);
1898+
locked = uart_port_trylock_irqsave(port, &flags);
18991899
else
1900-
spin_lock_irqsave(&port->lock, flags);
1900+
uart_port_lock_irqsave(port, &flags);
19011901

19021902
/* Save and disable interrupts, enable the transmitter */
19031903
old_cr1 = readl_relaxed(port->membase + ofs->cr1);
@@ -1911,7 +1911,7 @@ static void stm32_usart_console_write(struct console *co, const char *s,
19111911
writel_relaxed(old_cr1, port->membase + ofs->cr1);
19121912

19131913
if (locked)
1914-
spin_unlock_irqrestore(&port->lock, flags);
1914+
uart_port_unlock_irqrestore(port, flags);
19151915
}
19161916

19171917
static int stm32_usart_console_setup(struct console *co, char *options)
@@ -2046,7 +2046,7 @@ static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
20462046
* low-power mode.
20472047
*/
20482048
if (stm32_port->rx_ch) {
2049-
spin_lock_irqsave(&port->lock, flags);
2049+
uart_port_lock_irqsave(port, &flags);
20502050
/* Poll data from DMA RX buffer if any */
20512051
if (!stm32_usart_rx_dma_pause(stm32_port))
20522052
size += stm32_usart_receive_chars(port, true);

0 commit comments

Comments
 (0)