Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Console driver for 8250 UART. |
| 16 | |
| 17 | use crate::uart::Uart; |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 18 | use core::{ |
| 19 | cell::OnceCell, |
| 20 | fmt::{write, Arguments, Write}, |
| 21 | }; |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 22 | use spin::mutex::SpinMutex; |
| 23 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 24 | // Arbitrary limit on the number of consoles that can be registered. |
| 25 | // |
| 26 | // Matches the UART count in crosvm. |
| 27 | const MAX_CONSOLES: usize = 4; |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 28 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 29 | static CONSOLES: [SpinMutex<Option<Uart>>; MAX_CONSOLES] = |
| 30 | [SpinMutex::new(None), SpinMutex::new(None), SpinMutex::new(None), SpinMutex::new(None)]; |
| 31 | static ADDRESSES: [SpinMutex<OnceCell<usize>>; MAX_CONSOLES] = [ |
| 32 | SpinMutex::new(OnceCell::new()), |
| 33 | SpinMutex::new(OnceCell::new()), |
| 34 | SpinMutex::new(OnceCell::new()), |
| 35 | SpinMutex::new(OnceCell::new()), |
| 36 | ]; |
| 37 | |
| 38 | /// Index of the console used by default for logging. |
| 39 | pub const DEFAULT_CONSOLE_INDEX: usize = 0; |
| 40 | |
| 41 | /// Index of the console used by default for emergency logging. |
| 42 | pub const DEFAULT_EMERGENCY_CONSOLE_INDEX: usize = DEFAULT_CONSOLE_INDEX; |
| 43 | |
| 44 | /// Initialises the global instance(s) of the UART driver. |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 45 | /// |
| 46 | /// This must be called before using the `print!` and `println!` macros. |
| 47 | /// |
| 48 | /// # Safety |
| 49 | /// |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 50 | /// This must be called once with the bases of UARTs, mapped as device memory and (if necessary) |
| 51 | /// shared with the host as MMIO, to which no other references must be held. |
| 52 | pub unsafe fn init(base_addresses: &[usize]) { |
| 53 | for (i, &base_address) in base_addresses.iter().enumerate() { |
| 54 | // Remember the valid address, for emergency console accesses. |
| 55 | ADDRESSES[i].lock().set(base_address).expect("console::init() called more than once"); |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 56 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 57 | // Initialize the console driver, for normal console accesses. |
| 58 | let mut console = CONSOLES[i].lock(); |
| 59 | assert!(console.is_none(), "console::init() called more than once"); |
| 60 | // SAFETY: base_address must be the base of a mapped UART. |
| 61 | console.replace(unsafe { Uart::new(base_address) }); |
| 62 | } |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 65 | /// Writes a formatted string followed by a newline to the n-th console. |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 66 | /// |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 67 | /// Panics if the n-th console was not initialized by calling [`init`] first. |
Pierre-Clément Tosi | b5a3ab1 | 2023-09-15 11:18:38 +0100 | [diff] [blame^] | 68 | pub fn writeln(n: usize, format_args: Arguments) { |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 69 | let mut guard = CONSOLES[n].lock(); |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 70 | let uart = guard.as_mut().unwrap(); |
| 71 | |
| 72 | write(uart, format_args).unwrap(); |
| 73 | let _ = uart.write_str("\n"); |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 76 | /// Reinitializes the n-th UART driver and writes a formatted string followed by a newline to it. |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 77 | /// |
| 78 | /// This is intended for use in situations where the UART may be in an unknown state or the global |
| 79 | /// instance may be locked, such as in an exception handler or panic handler. |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 80 | pub fn ewriteln(n: usize, format_args: Arguments) { |
| 81 | let Some(cell) = ADDRESSES[n].try_lock() else { return }; |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame] | 82 | let Some(addr) = cell.get() else { return }; |
| 83 | |
| 84 | // SAFETY: addr contains the base of a mapped UART, passed in init(). |
| 85 | let mut uart = unsafe { Uart::new(*addr) }; |
| 86 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 87 | let _ = write(&mut uart, format_args); |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 88 | let _ = uart.write_str("\n"); |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Pierre-Clément Tosi | b5a3ab1 | 2023-09-15 11:18:38 +0100 | [diff] [blame^] | 91 | /// Prints the given formatted string to the n-th console, followed by a newline. |
| 92 | /// |
| 93 | /// Panics if the console has not yet been initialized. May hang if used in an exception context; |
| 94 | /// use `eprintln!` instead. |
| 95 | #[macro_export] |
| 96 | macro_rules! console_writeln { |
| 97 | ($n:expr, $($arg:tt)*) => ({ |
| 98 | $crate::console::writeln($n, format_args!($($arg)*)) |
| 99 | }) |
| 100 | } |
| 101 | |
| 102 | pub(crate) use console_writeln; |
| 103 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 104 | /// Prints the given formatted string to the console, followed by a newline. |
| 105 | /// |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 106 | /// Panics if the console has not yet been initialized. May hang if used in an exception context; |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 107 | /// use `eprintln!` instead. |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 108 | macro_rules! println { |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 109 | ($($arg:tt)*) => ({ |
Pierre-Clément Tosi | b5a3ab1 | 2023-09-15 11:18:38 +0100 | [diff] [blame^] | 110 | $crate::console::console_writeln!($crate::console::DEFAULT_CONSOLE_INDEX, $($arg)*) |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 111 | }) |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 114 | pub(crate) use println; // Make it available in this crate. |
| 115 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 116 | /// Prints the given string followed by a newline to the console in an emergency, such as an |
| 117 | /// exception handler. |
| 118 | /// |
| 119 | /// Never panics. |
| 120 | #[macro_export] |
| 121 | macro_rules! eprintln { |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 122 | ($($arg:tt)*) => ({ |
| 123 | $crate::console::ewriteln($crate::console::DEFAULT_EMERGENCY_CONSOLE_INDEX, format_args!($($arg)*)) |
| 124 | }) |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 125 | } |