blob: aaf354e6c1ab9d91bae7f7a289df3df89aa6ad8b [file] [log] [blame]
Alice Wanga3971062023-06-13 11:48:53 +00001// 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 Tosic7c23572024-10-02 12:49:54 +010017pub mod pci;
18
Alice Wanga3971062023-06-13 11:48:53 +000019use core::ops::Range;
Pierre-Clément Tosi1bf532b2023-11-13 11:06:20 +000020use cstr::cstr;
Alice Wanga3971062023-06-13 11:48:53 +000021use libfdt::{self, Fdt, FdtError};
22
23/// Represents information about a SWIOTLB buffer.
24#[derive(Debug)]
25pub struct SwiotlbInfo {
26 /// The address of the SWIOTLB buffer, if available.
27 pub addr: Option<usize>,
28 /// The size of the SWIOTLB buffer.
29 pub size: usize,
30 /// The alignment of the SWIOTLB buffer, if available.
31 pub align: Option<usize>,
32}
33
34impl SwiotlbInfo {
35 /// Creates a `SwiotlbInfo` struct from the given device tree.
Pierre-Clément Tosi3c5e7a72024-11-27 20:12:37 +000036 pub fn new_from_fdt(fdt: &Fdt) -> libfdt::Result<Option<SwiotlbInfo>> {
37 let Some(node) = fdt.compatible_nodes(cstr!("restricted-dma-pool"))?.next() else {
38 return Ok(None);
39 };
Alice Wanga3971062023-06-13 11:48:53 +000040 let (addr, size, align) = if let Some(mut reg) = node.reg()? {
Pierre-Clément Tosi3c5e7a72024-11-27 20:12:37 +000041 let reg = reg.next().ok_or(FdtError::BadValue)?;
42 let size = reg.size.ok_or(FdtError::BadValue)?;
Alice Wanga3971062023-06-13 11:48:53 +000043 (Some(reg.addr.try_into().unwrap()), size.try_into().unwrap(), None)
44 } else {
45 let size = node.getprop_u64(cstr!("size"))?.ok_or(FdtError::NotFound)?;
46 let align = node.getprop_u64(cstr!("alignment"))?.ok_or(FdtError::NotFound)?;
47 (None, size.try_into().unwrap(), Some(align.try_into().unwrap()))
48 };
Pierre-Clément Tosi3c5e7a72024-11-27 20:12:37 +000049 Ok(Some(Self { addr, size, align }))
Alice Wanga3971062023-06-13 11:48:53 +000050 }
51
52 /// Returns the fixed range of memory mapped by the SWIOTLB buffer, if available.
53 pub fn fixed_range(&self) -> Option<Range<usize>> {
54 self.addr.map(|addr| addr..addr + self.size)
55 }
56}