OMAP2+: PM/serial: hold console semaphore while OMAP UARTs are disabled

The console semaphore must be held while the OMAP UART devices are
disabled, lest a console write cause an ARM abort (and a kernel crash)
when the underlying console device is inaccessible. These crashes
only occur when the console is on one of the OMAP internal serial
ports.

While this problem has been latent in the PM idle loop for some time,
the crash was not triggerable with an unmodified kernel until commit
6f251e9db1093c187addc309b5f2f7fe3efd2995 ("OMAP: UART: omap_device
conversions, remove implicit 8520 assumptions"). After this patch, a
console write often occurs after the console UART has been disabled in
the idle loop, crashing the system. Several users have encountered
this bug:

http://www.mail-archive.com/linux-omap@vger.kernel.org/msg38396.html

http://www.mail-archive.com/linux-omap@vger.kernel.org/msg36602.html

The same commit also introduced new code that disabled the UARTs
during init, in omap_serial_init_port(). The kernel will also crash
in this code when earlyconsole and extra debugging is enabled:

http://www.mail-archive.com/linux-omap@vger.kernel.org/msg36411.html

The minimal fix for the -rc series is to hold the console semaphore
while the OMAP UARTs are disabled. This is a somewhat overbroad fix,
since the console may not be located on an OMAP UART, as is the case
with the GPMC UART on Zoom3. While it is technically possible to
determine which devices the console or earlyconsole is actually
running on, it is not a trivial problem to solve, and the code to do
so is not really appropriate for the -rc series.

The right long-term fix is to ensure that no code outside of the OMAP
serial driver can disable an OMAP UART. As I understand it, code to
implement this is under development by TI.

This patch is a collaboration between Paul Walmsley <paul@pwsan.com>
and Tony Lindgren <tony@atomide.com>. Thanks to Ming Lei
<tom.leiming@gmail.com> and Pramod <pramod.gurav@ti.com> for their
feedback on earlier versions of this patch.

Signed-off-by: Paul Walmsley <paul@pwsan.com>
Signed-off-by: Tony Lindgren <tony@atomide.com>
Acked-by: Kevin Hilman <khilman@deeprootsystems.com>
Cc: Ming Lei <tom.leiming@gmail.com>
Cc: Pramod <pramod.gurav@ti.com>
Cc: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Cc: Jean Pihet <jean.pihet@newoldbits.com>
Cc: Govindraj.R <govindraj.raja@ti.com>

authored by

Paul Walmsley and committed by
Tony Lindgren
0d8e2d0d f910043c

+22
+7
arch/arm/mach-omap2/pm24xx.c
··· 30 30 #include <linux/irq.h> 31 31 #include <linux/time.h> 32 32 #include <linux/gpio.h> 33 + #include <linux/console.h> 33 34 34 35 #include <asm/mach/time.h> 35 36 #include <asm/mach/irq.h> ··· 119 118 if (omap_irq_pending()) 120 119 goto no_sleep; 121 120 121 + /* Block console output in case it is on one of the OMAP UARTs */ 122 + if (try_acquire_console_sem()) 123 + goto no_sleep; 124 + 122 125 omap_uart_prepare_idle(0); 123 126 omap_uart_prepare_idle(1); 124 127 omap_uart_prepare_idle(2); ··· 135 130 omap_uart_resume_idle(2); 136 131 omap_uart_resume_idle(1); 137 132 omap_uart_resume_idle(0); 133 + 134 + release_console_sem(); 138 135 139 136 no_sleep: 140 137 if (omap2_pm_debug) {
+10
arch/arm/mach-omap2/pm34xx.c
··· 28 28 #include <linux/clk.h> 29 29 #include <linux/delay.h> 30 30 #include <linux/slab.h> 31 + #include <linux/console.h> 31 32 32 33 #include <plat/sram.h> 33 34 #include <plat/clockdomain.h> ··· 386 385 omap3_enable_io_chain(); 387 386 } 388 387 388 + /* Block console output in case it is on one of the OMAP UARTs */ 389 + if (per_next_state < PWRDM_POWER_ON || 390 + core_next_state < PWRDM_POWER_ON) 391 + if (try_acquire_console_sem()) 392 + goto console_still_active; 393 + 389 394 /* PER */ 390 395 if (per_next_state < PWRDM_POWER_ON) { 391 396 omap_uart_prepare_idle(2); ··· 470 463 omap_uart_resume_idle(3); 471 464 } 472 465 466 + release_console_sem(); 467 + 468 + console_still_active: 473 469 /* Disable IO-PAD and IO-CHAIN wakeup */ 474 470 if (omap3_has_io_wakeup() && 475 471 (per_next_state < PWRDM_POWER_ON ||
+5
arch/arm/mach-omap2/serial.c
··· 27 27 #include <linux/slab.h> 28 28 #include <linux/serial_8250.h> 29 29 #include <linux/pm_runtime.h> 30 + #include <linux/console.h> 30 31 31 32 #ifdef CONFIG_SERIAL_OMAP 32 33 #include <plat/omap-serial.h> ··· 808 807 809 808 oh->dev_attr = uart; 810 809 810 + acquire_console_sem(); /* in case the earlycon is on the UART */ 811 + 811 812 /* 812 813 * Because of early UART probing, UART did not get idled 813 814 * on init. Now that omap_device is ready, ensure full idle ··· 833 830 uart->timeout = (30 * HZ); 834 831 omap_uart_block_sleep(uart); 835 832 uart->timeout = DEFAULT_TIMEOUT; 833 + 834 + release_console_sem(); 836 835 837 836 if ((cpu_is_omap34xx() && uart->padconf) || 838 837 (uart->wk_en && uart->wk_mask)) {