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 | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame^] | 24 | // ADDRESS is the base of the MMIO region for a UART and must be mapped as device memory. |
| 25 | static ADDRESS: SpinMutex<OnceCell<usize>> = SpinMutex::new(OnceCell::new()); |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 26 | static CONSOLE: SpinMutex<Option<Uart>> = SpinMutex::new(None); |
| 27 | |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame^] | 28 | /// Initialises the global instance of the UART driver. |
| 29 | /// |
| 30 | /// This must be called before using the `print!` and `println!` macros. |
| 31 | /// |
| 32 | /// # Safety |
| 33 | /// |
| 34 | /// This must be called with the base of a UART, mapped as device memory and (if necessary) shared |
| 35 | /// with the host as MMIO. |
| 36 | pub unsafe fn init(base_address: usize) { |
| 37 | // Remember the valid address, for emergency console accesses. |
| 38 | ADDRESS.lock().set(base_address).expect("console::init() called more than once"); |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 39 | |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame^] | 40 | // Initialize the console driver, for normal console accesses. |
| 41 | let mut console = CONSOLE.lock(); |
| 42 | assert!(console.is_none(), "console::init() called more than once"); |
| 43 | // SAFETY: base_address must be the base of a mapped UART. |
| 44 | console.replace(unsafe { Uart::new(base_address) }); |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 47 | /// Writes a formatted string followed by a newline to the console. |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 48 | /// |
| 49 | /// Panics if [`init`] was not called first. |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 50 | pub(crate) fn writeln(format_args: Arguments) { |
| 51 | let mut guard = CONSOLE.lock(); |
| 52 | let uart = guard.as_mut().unwrap(); |
| 53 | |
| 54 | write(uart, format_args).unwrap(); |
| 55 | let _ = uart.write_str("\n"); |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 58 | /// Reinitializes the 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] | 59 | /// |
| 60 | /// This is intended for use in situations where the UART may be in an unknown state or the global |
| 61 | /// instance may be locked, such as in an exception handler or panic handler. |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 62 | pub fn ewriteln(format_args: Arguments) { |
Pierre-Clément Tosi | 8e92d1a | 2024-06-18 16:15:14 +0100 | [diff] [blame^] | 63 | let Some(cell) = ADDRESS.try_lock() else { return }; |
| 64 | let Some(addr) = cell.get() else { return }; |
| 65 | |
| 66 | // SAFETY: addr contains the base of a mapped UART, passed in init(). |
| 67 | let mut uart = unsafe { Uart::new(*addr) }; |
| 68 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 69 | let _ = write(&mut uart, format_args); |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 70 | let _ = uart.write_str("\n"); |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 73 | /// Prints the given formatted string to the console, followed by a newline. |
| 74 | /// |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 75 | /// 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] | 76 | /// use `eprintln!` instead. |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 77 | macro_rules! println { |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 78 | ($($arg:tt)*) => ($crate::console::writeln(format_args!($($arg)*))); |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 81 | pub(crate) use println; // Make it available in this crate. |
| 82 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 83 | /// Prints the given string followed by a newline to the console in an emergency, such as an |
| 84 | /// exception handler. |
| 85 | /// |
| 86 | /// Never panics. |
| 87 | #[macro_export] |
| 88 | macro_rules! eprintln { |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 89 | ($($arg:tt)*) => ($crate::console::ewriteln(format_args!($($arg)*))); |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 90 | } |