Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 1 | // Copyright 2023, 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 | //! High-level FDT functions. |
| 16 | |
Pierre-Clément Tosi | c7c2357 | 2024-10-02 12:49:54 +0100 | [diff] [blame] | 17 | pub mod pci; |
| 18 | |
Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 19 | use core::ops::Range; |
Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 20 | use libfdt::{self, Fdt, FdtError}; |
| 21 | |
| 22 | /// Represents information about a SWIOTLB buffer. |
| 23 | #[derive(Debug)] |
| 24 | pub struct SwiotlbInfo { |
| 25 | /// The address of the SWIOTLB buffer, if available. |
| 26 | pub addr: Option<usize>, |
| 27 | /// The size of the SWIOTLB buffer. |
| 28 | pub size: usize, |
| 29 | /// The alignment of the SWIOTLB buffer, if available. |
| 30 | pub align: Option<usize>, |
| 31 | } |
| 32 | |
| 33 | impl SwiotlbInfo { |
| 34 | /// Creates a `SwiotlbInfo` struct from the given device tree. |
Pierre-Clément Tosi | 3c5e7a7 | 2024-11-27 20:12:37 +0000 | [diff] [blame] | 35 | pub fn new_from_fdt(fdt: &Fdt) -> libfdt::Result<Option<SwiotlbInfo>> { |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 36 | let Some(node) = fdt.compatible_nodes(c"restricted-dma-pool")?.next() else { |
Pierre-Clément Tosi | 3c5e7a7 | 2024-11-27 20:12:37 +0000 | [diff] [blame] | 37 | return Ok(None); |
| 38 | }; |
Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 39 | let (addr, size, align) = if let Some(mut reg) = node.reg()? { |
Pierre-Clément Tosi | 3c5e7a7 | 2024-11-27 20:12:37 +0000 | [diff] [blame] | 40 | let reg = reg.next().ok_or(FdtError::BadValue)?; |
| 41 | let size = reg.size.ok_or(FdtError::BadValue)?; |
Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 42 | (Some(reg.addr.try_into().unwrap()), size.try_into().unwrap(), None) |
| 43 | } else { |
Alan Stokes | f46a17c | 2025-01-05 15:50:18 +0000 | [diff] [blame] | 44 | let size = node.getprop_u64(c"size")?.ok_or(FdtError::NotFound)?; |
| 45 | let align = node.getprop_u64(c"alignment")?.ok_or(FdtError::NotFound)?; |
Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 46 | (None, size.try_into().unwrap(), Some(align.try_into().unwrap())) |
| 47 | }; |
Pierre-Clément Tosi | 3c5e7a7 | 2024-11-27 20:12:37 +0000 | [diff] [blame] | 48 | Ok(Some(Self { addr, size, align })) |
Alice Wang | a397106 | 2023-06-13 11:48:53 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// Returns the fixed range of memory mapped by the SWIOTLB buffer, if available. |
| 52 | pub fn fixed_range(&self) -> Option<Range<usize>> { |
| 53 | self.addr.map(|addr| addr..addr + self.size) |
| 54 | } |
| 55 | } |