@@ -711,14 +711,24 @@ static void serial8250_set_sleep(struct uart_8250_port *p, int sleep)
711
711
serial8250_rpm_put (p );
712
712
}
713
713
714
- static void serial8250_clear_IER (struct uart_8250_port * up )
714
+ /*
715
+ * Only to be used directly by the callback helper serial8250_console_write(),
716
+ * which may not require the port lock. Use serial8250_clear_IER() instead for
717
+ * all other cases.
718
+ */
719
+ static void __serial8250_clear_IER (struct uart_8250_port * up )
715
720
{
716
721
if (up -> capabilities & UART_CAP_UUE )
717
722
serial_out (up , UART_IER , UART_IER_UUE );
718
723
else
719
724
serial_out (up , UART_IER , 0 );
720
725
}
721
726
727
+ static inline void serial8250_clear_IER (struct uart_8250_port * up )
728
+ {
729
+ __serial8250_clear_IER (up );
730
+ }
731
+
722
732
#ifdef CONFIG_SERIAL_8250_RSA
723
733
/*
724
734
* Attempts to turn on the RSA FIFO. Returns zero on failure.
@@ -1406,9 +1416,6 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier)
1406
1416
{
1407
1417
unsigned char mcr = serial8250_in_MCR (p );
1408
1418
1409
- /* Port locked to synchronize UART_IER access against the console. */
1410
- lockdep_assert_held_once (& p -> port .lock );
1411
-
1412
1419
if (p -> port .rs485 .flags & SER_RS485_RTS_AFTER_SEND )
1413
1420
mcr |= UART_MCR_RTS ;
1414
1421
else
@@ -1424,6 +1431,16 @@ void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier)
1424
1431
serial8250_clear_and_reinit_fifos (p );
1425
1432
1426
1433
if (toggle_ier ) {
1434
+ /*
1435
+ * Port locked to synchronize UART_IER access against
1436
+ * the console. The lockdep_assert must be restricted
1437
+ * to this condition because only here is it
1438
+ * guaranteed that the port lock is held. The other
1439
+ * hardware access in this function is synchronized
1440
+ * by console ownership.
1441
+ */
1442
+ lockdep_assert_held_once (& p -> port .lock );
1443
+
1427
1444
p -> ier |= UART_IER_RLSI | UART_IER_RDI ;
1428
1445
serial_port_out (& p -> port , UART_IER , p -> ier );
1429
1446
}
@@ -3303,7 +3320,11 @@ EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3303
3320
3304
3321
static void serial8250_console_putchar (struct uart_port * port , unsigned char ch )
3305
3322
{
3323
+ struct uart_8250_port * up = up_to_u8250p (port );
3324
+
3306
3325
serial_port_out (port , UART_TX , ch );
3326
+
3327
+ up -> console_line_ended = (ch == '\n' );
3307
3328
}
3308
3329
3309
3330
static void serial8250_console_wait_putchar (struct uart_port * port , unsigned char ch )
@@ -3340,11 +3361,22 @@ static void serial8250_console_restore(struct uart_8250_port *up)
3340
3361
serial8250_out_MCR (up , up -> mcr | UART_MCR_DTR | UART_MCR_RTS );
3341
3362
}
3342
3363
3343
- static void fifo_wait_for_lsr (struct uart_8250_port * up , unsigned int count )
3364
+ static void fifo_wait_for_lsr (struct uart_8250_port * up ,
3365
+ struct nbcon_write_context * wctxt ,
3366
+ unsigned int count )
3344
3367
{
3345
3368
unsigned int i ;
3346
3369
3347
3370
for (i = 0 ; i < count ; i ++ ) {
3371
+ /*
3372
+ * Pass the ownership as quickly as possible to a higher
3373
+ * priority context. Otherwise, its attempt to take over
3374
+ * the ownership might timeout. The new owner will wait
3375
+ * for UART_LSR_THRE before reusing the fifo.
3376
+ */
3377
+ if (!nbcon_can_proceed (wctxt ))
3378
+ return ;
3379
+
3348
3380
if (wait_for_lsr (up , UART_LSR_THRE ))
3349
3381
return ;
3350
3382
}
@@ -3357,27 +3389,38 @@ static void fifo_wait_for_lsr(struct uart_8250_port *up, unsigned int count)
3357
3389
* to get empty.
3358
3390
*/
3359
3391
static void serial8250_console_fifo_write (struct uart_8250_port * up ,
3360
- const char * s , unsigned int count )
3392
+ struct nbcon_write_context * wctxt )
3361
3393
{
3362
- const char * end = s + count ;
3363
3394
unsigned int fifosize = up -> tx_loadsz ;
3364
3395
struct uart_port * port = & up -> port ;
3396
+ const char * s = wctxt -> outbuf ;
3397
+ const char * end = s + wctxt -> len ;
3365
3398
unsigned int tx_count = 0 ;
3366
3399
bool cr_sent = false;
3367
3400
unsigned int i ;
3368
3401
3369
3402
while (s != end ) {
3370
3403
/* Allow timeout for each byte of a possibly full FIFO */
3371
- fifo_wait_for_lsr (up , fifosize );
3404
+ fifo_wait_for_lsr (up , wctxt , fifosize );
3372
3405
3406
+ /*
3407
+ * Fill the FIFO. If a handover or takeover occurs, writing
3408
+ * must be aborted since wctxt->outbuf and wctxt->len are no
3409
+ * longer valid.
3410
+ */
3373
3411
for (i = 0 ; i < fifosize && s != end ; ++ i ) {
3412
+ if (!nbcon_enter_unsafe (wctxt ))
3413
+ return ;
3414
+
3374
3415
if (* s == '\n' && !cr_sent ) {
3375
3416
serial8250_console_putchar (port , '\r' );
3376
3417
cr_sent = true;
3377
3418
} else {
3378
3419
serial8250_console_putchar (port , * s ++ );
3379
3420
cr_sent = false;
3380
3421
}
3422
+
3423
+ nbcon_exit_unsafe (wctxt );
3381
3424
}
3382
3425
tx_count = i ;
3383
3426
}
@@ -3386,39 +3429,57 @@ static void serial8250_console_fifo_write(struct uart_8250_port *up,
3386
3429
* Allow timeout for each byte written since the caller will only wait
3387
3430
* for UART_LSR_BOTH_EMPTY using the timeout of a single character
3388
3431
*/
3389
- fifo_wait_for_lsr (up , tx_count );
3432
+ fifo_wait_for_lsr (up , wctxt , tx_count );
3433
+ }
3434
+
3435
+ static void serial8250_console_byte_write (struct uart_8250_port * up ,
3436
+ struct nbcon_write_context * wctxt )
3437
+ {
3438
+ struct uart_port * port = & up -> port ;
3439
+ const char * s = wctxt -> outbuf ;
3440
+ const char * end = s + wctxt -> len ;
3441
+
3442
+ /*
3443
+ * Write out the message. If a handover or takeover occurs, writing
3444
+ * must be aborted since wctxt->outbuf and wctxt->len are no longer
3445
+ * valid.
3446
+ */
3447
+ while (s != end ) {
3448
+ if (!nbcon_enter_unsafe (wctxt ))
3449
+ return ;
3450
+
3451
+ uart_console_write (port , s ++ , 1 , serial8250_console_wait_putchar );
3452
+
3453
+ nbcon_exit_unsafe (wctxt );
3454
+ }
3390
3455
}
3391
3456
3392
3457
/*
3393
- * Print a string to the serial port trying not to disturb
3394
- * any possible real use of the port...
3395
- *
3396
- * The console_lock must be held when we get here.
3458
+ * Print a string to the serial port trying not to disturb
3459
+ * any possible real use of the port...
3397
3460
*
3398
- * Doing runtime PM is really a bad idea for the kernel console.
3399
- * Thus, we assume the function is called when device is powered up.
3461
+ * Doing runtime PM is really a bad idea for the kernel console.
3462
+ * Thus, assume it is called when device is powered up.
3400
3463
*/
3401
- void serial8250_console_write (struct uart_8250_port * up , const char * s ,
3402
- unsigned int count )
3464
+ void serial8250_console_write (struct uart_8250_port * up ,
3465
+ struct nbcon_write_context * wctxt ,
3466
+ bool is_atomic )
3403
3467
{
3404
3468
struct uart_8250_em485 * em485 = up -> em485 ;
3405
3469
struct uart_port * port = & up -> port ;
3406
- unsigned long flags ;
3407
- unsigned int ier , use_fifo ;
3408
- int locked = 1 ;
3409
-
3410
- touch_nmi_watchdog ();
3470
+ unsigned int ier ;
3471
+ bool use_fifo ;
3411
3472
3412
- if (oops_in_progress )
3413
- locked = uart_port_trylock_irqsave (port , & flags );
3414
- else
3415
- uart_port_lock_irqsave (port , & flags );
3473
+ if (!nbcon_enter_unsafe (wctxt ))
3474
+ return ;
3416
3475
3417
3476
/*
3418
- * First save the IER then disable the interrupts
3477
+ * First, save the IER, then disable the interrupts. The special
3478
+ * variant to clear the IER is used because console printing may
3479
+ * occur without holding the port lock.
3419
3480
*/
3420
3481
ier = serial_port_in (port , UART_IER );
3421
- serial8250_clear_IER (up );
3482
+ __serial8250_clear_IER (up );
3422
3483
3423
3484
/* check scratch reg to see if port powered off during system sleep */
3424
3485
if (up -> canary && (up -> canary != serial_port_in (port , UART_SCR ))) {
@@ -3432,6 +3493,18 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
3432
3493
mdelay (port -> rs485 .delay_rts_before_send );
3433
3494
}
3434
3495
3496
+ /* If ownership was lost, no writing is allowed */
3497
+ if (!nbcon_can_proceed (wctxt ))
3498
+ goto skip_write ;
3499
+
3500
+ /*
3501
+ * If console printer did not fully output the previous line, it must
3502
+ * have been handed or taken over. Insert a newline in order to
3503
+ * maintain clean output.
3504
+ */
3505
+ if (!up -> console_line_ended )
3506
+ uart_console_write (port , "\n" , 1 , serial8250_console_wait_putchar );
3507
+
3435
3508
use_fifo = (up -> capabilities & UART_CAP_FIFO ) &&
3436
3509
/*
3437
3510
* BCM283x requires to check the fifo
@@ -3452,10 +3525,23 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
3452
3525
*/
3453
3526
!(up -> port .flags & UPF_CONS_FLOW );
3454
3527
3528
+ nbcon_exit_unsafe (wctxt );
3529
+
3455
3530
if (likely (use_fifo ))
3456
- serial8250_console_fifo_write (up , s , count );
3531
+ serial8250_console_fifo_write (up , wctxt );
3457
3532
else
3458
- uart_console_write (port , s , count , serial8250_console_wait_putchar );
3533
+ serial8250_console_byte_write (up , wctxt );
3534
+ skip_write :
3535
+ /*
3536
+ * If ownership was lost, this context must reacquire ownership and
3537
+ * re-enter the unsafe section in order to perform final actions
3538
+ * (such as re-enabling interrupts).
3539
+ */
3540
+ if (!nbcon_can_proceed (wctxt )) {
3541
+ do {
3542
+ nbcon_reacquire_nobuf (wctxt );
3543
+ } while (!nbcon_enter_unsafe (wctxt ));
3544
+ }
3459
3545
3460
3546
/*
3461
3547
* Finally, wait for transmitter to become empty
@@ -3478,11 +3564,18 @@ void serial8250_console_write(struct uart_8250_port *up, const char *s,
3478
3564
* call it if we have saved something in the saved flags
3479
3565
* while processing with interrupts off.
3480
3566
*/
3481
- if (up -> msr_saved_flags )
3482
- serial8250_modem_status (up );
3567
+ if (up -> msr_saved_flags ) {
3568
+ /*
3569
+ * For atomic, it must be deferred to irq_work because this
3570
+ * may be a context that does not permit waking up tasks.
3571
+ */
3572
+ if (is_atomic )
3573
+ irq_work_queue (& up -> modem_status_work );
3574
+ else
3575
+ serial8250_modem_status (up );
3576
+ }
3483
3577
3484
- if (locked )
3485
- uart_port_unlock_irqrestore (port , flags );
3578
+ nbcon_exit_unsafe (wctxt );
3486
3579
}
3487
3580
3488
3581
static unsigned int probe_baud (struct uart_port * port )
@@ -3500,8 +3593,24 @@ static unsigned int probe_baud(struct uart_port *port)
3500
3593
return (port -> uartclk / 16 ) / quot ;
3501
3594
}
3502
3595
3596
+ /*
3597
+ * irq_work handler to perform modem control. Only triggered via
3598
+ * ->write_atomic() callback because it may be in a scheduler or
3599
+ * NMI context, unable to wake tasks.
3600
+ */
3601
+ static void modem_status_handler (struct irq_work * iwp )
3602
+ {
3603
+ struct uart_8250_port * up = container_of (iwp , struct uart_8250_port , modem_status_work );
3604
+ struct uart_port * port = & up -> port ;
3605
+
3606
+ uart_port_lock (port );
3607
+ serial8250_modem_status (up );
3608
+ uart_port_unlock (port );
3609
+ }
3610
+
3503
3611
int serial8250_console_setup (struct uart_port * port , char * options , bool probe )
3504
3612
{
3613
+ struct uart_8250_port * up = up_to_u8250p (port );
3505
3614
int baud = 9600 ;
3506
3615
int bits = 8 ;
3507
3616
int parity = 'n' ;
@@ -3511,6 +3620,9 @@ int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3511
3620
if (!port -> iobase && !port -> membase )
3512
3621
return - ENODEV ;
3513
3622
3623
+ up -> console_line_ended = true;
3624
+ init_irq_work (& up -> modem_status_work , modem_status_handler );
3625
+
3514
3626
if (options )
3515
3627
uart_parse_options (options , & baud , & parity , & bits , & flow );
3516
3628
else if (probe )
0 commit comments