|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// Copyright (C) 2022 Linutronix GmbH, John Ogness |
| 3 | +// Copyright (C) 2022 Intel, Thomas Gleixner |
| 4 | + |
| 5 | +#include <linux/kernel.h> |
| 6 | +#include <linux/console.h> |
| 7 | +#include "internal.h" |
| 8 | +/* |
| 9 | + * Printk console printing implementation for consoles which does not depend |
| 10 | + * on the legacy style console_lock mechanism. |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * nbcon_state_set - Helper function to set the console state |
| 15 | + * @con: Console to update |
| 16 | + * @new: The new state to write |
| 17 | + * |
| 18 | + * Only to be used when the console is not yet or no longer visible in the |
| 19 | + * system. Otherwise use nbcon_state_try_cmpxchg(). |
| 20 | + */ |
| 21 | +static inline void nbcon_state_set(struct console *con, struct nbcon_state *new) |
| 22 | +{ |
| 23 | + atomic_set(&ACCESS_PRIVATE(con, nbcon_state), new->atom); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * nbcon_state_read - Helper function to read the console state |
| 28 | + * @con: Console to read |
| 29 | + * @state: The state to store the result |
| 30 | + */ |
| 31 | +static inline void nbcon_state_read(struct console *con, struct nbcon_state *state) |
| 32 | +{ |
| 33 | + state->atom = atomic_read(&ACCESS_PRIVATE(con, nbcon_state)); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * nbcon_state_try_cmpxchg() - Helper function for atomic_try_cmpxchg() on console state |
| 38 | + * @con: Console to update |
| 39 | + * @cur: Old/expected state |
| 40 | + * @new: New state |
| 41 | + * |
| 42 | + * Return: True on success. False on fail and @cur is updated. |
| 43 | + */ |
| 44 | +static inline bool nbcon_state_try_cmpxchg(struct console *con, struct nbcon_state *cur, |
| 45 | + struct nbcon_state *new) |
| 46 | +{ |
| 47 | + return atomic_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_state), &cur->atom, new->atom); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * nbcon_init - Initialize the nbcon console specific data |
| 52 | + * @con: Console to initialize |
| 53 | + */ |
| 54 | +void nbcon_init(struct console *con) |
| 55 | +{ |
| 56 | + struct nbcon_state state = { }; |
| 57 | + |
| 58 | + nbcon_state_set(con, &state); |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * nbcon_cleanup - Cleanup the nbcon console specific data |
| 63 | + * @con: Console to cleanup |
| 64 | + */ |
| 65 | +void nbcon_cleanup(struct console *con) |
| 66 | +{ |
| 67 | + struct nbcon_state state = { }; |
| 68 | + |
| 69 | + nbcon_state_set(con, &state); |
| 70 | +} |
0 commit comments