Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +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 | //! High-level FDT functions. |
| 16 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 17 | use crate::cstr; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 18 | use crate::helpers::flatten; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 19 | use crate::helpers::GUEST_PAGE_SIZE; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 20 | use crate::helpers::SIZE_4KB; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 21 | use crate::RebootReason; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 22 | use core::ffi::CStr; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 23 | use core::mem::size_of; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 24 | use core::ops::Range; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 25 | use fdtpci::PciMemoryFlags; |
| 26 | use fdtpci::PciRangeType; |
| 27 | use libfdt::AddressRange; |
| 28 | use libfdt::CellIterator; |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 29 | use libfdt::Fdt; |
| 30 | use libfdt::FdtError; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 31 | use libfdt::FdtNode; |
Jiyong Park | 8331612 | 2023-03-21 09:39:39 +0900 | [diff] [blame] | 32 | use log::debug; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 33 | use log::error; |
| 34 | use tinyvec::ArrayVec; |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 35 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 36 | /// Extract from /config the address range containing the pre-loaded kernel. Absence of /config is |
| 37 | /// not an error. |
| 38 | fn read_kernel_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> { |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 39 | let addr = cstr!("kernel-address"); |
| 40 | let size = cstr!("kernel-size"); |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 41 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 42 | if let Some(config) = fdt.node(cstr!("/config"))? { |
Pierre-Clément Tosi | c3811b8 | 2022-11-29 11:24:16 +0000 | [diff] [blame] | 43 | if let (Some(addr), Some(size)) = (config.getprop_u32(addr)?, config.getprop_u32(size)?) { |
| 44 | let addr = addr as usize; |
| 45 | let size = size as usize; |
| 46 | |
| 47 | return Ok(Some(addr..(addr + size))); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | Ok(None) |
| 52 | } |
| 53 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 54 | /// Extract from /chosen the address range containing the pre-loaded ramdisk. Absence is not an |
| 55 | /// error as there can be initrd-less VM. |
| 56 | fn read_initrd_range_from(fdt: &Fdt) -> libfdt::Result<Option<Range<usize>>> { |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 57 | let start = cstr!("linux,initrd-start"); |
| 58 | let end = cstr!("linux,initrd-end"); |
Pierre-Clément Tosi | a0934c1 | 2022-11-25 20:54:11 +0000 | [diff] [blame] | 59 | |
| 60 | if let Some(chosen) = fdt.chosen()? { |
| 61 | if let (Some(start), Some(end)) = (chosen.getprop_u32(start)?, chosen.getprop_u32(end)?) { |
| 62 | return Ok(Some((start as usize)..(end as usize))); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Ok(None) |
| 67 | } |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 68 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 69 | fn patch_initrd_range(fdt: &mut Fdt, initrd_range: &Range<usize>) -> libfdt::Result<()> { |
| 70 | let start = u32::try_from(initrd_range.start).unwrap(); |
| 71 | let end = u32::try_from(initrd_range.end).unwrap(); |
| 72 | |
| 73 | let mut node = fdt.chosen_mut()?.ok_or(FdtError::NotFound)?; |
| 74 | node.setprop(cstr!("linux,initrd-start"), &start.to_be_bytes())?; |
| 75 | node.setprop(cstr!("linux,initrd-end"), &end.to_be_bytes())?; |
| 76 | Ok(()) |
| 77 | } |
| 78 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 79 | /// Read the first range in /memory node in DT |
| 80 | fn read_memory_range_from(fdt: &Fdt) -> libfdt::Result<Range<usize>> { |
| 81 | fdt.memory()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound) |
| 82 | } |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 83 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 84 | /// Check if memory range is ok |
| 85 | fn validate_memory_range(range: &Range<usize>) -> Result<(), RebootReason> { |
| 86 | let base = range.start; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 87 | if base as u64 != DeviceTreeInfo::RAM_BASE_ADDR { |
| 88 | error!("Memory base address {:#x} is not {:#x}", base, DeviceTreeInfo::RAM_BASE_ADDR); |
| 89 | return Err(RebootReason::InvalidFdt); |
| 90 | } |
| 91 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 92 | let size = range.len(); |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 93 | if size % GUEST_PAGE_SIZE != 0 { |
| 94 | error!("Memory size {:#x} is not a multiple of page size {:#x}", size, GUEST_PAGE_SIZE); |
| 95 | return Err(RebootReason::InvalidFdt); |
| 96 | } |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 97 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 98 | if size == 0 { |
| 99 | error!("Memory size is 0"); |
| 100 | return Err(RebootReason::InvalidFdt); |
| 101 | } |
| 102 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 105 | fn patch_memory_range(fdt: &mut Fdt, memory_range: &Range<usize>) -> libfdt::Result<()> { |
| 106 | let size = memory_range.len() as u64; |
| 107 | fdt.node_mut(cstr!("/memory"))?.ok_or(FdtError::NotFound)?.setprop_inplace( |
| 108 | cstr!("reg"), |
| 109 | flatten(&[DeviceTreeInfo::RAM_BASE_ADDR.to_be_bytes(), size.to_be_bytes()]), |
| 110 | ) |
| 111 | } |
| 112 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 113 | /// Read the number of CPUs from DT |
| 114 | fn read_num_cpus_from(fdt: &Fdt) -> libfdt::Result<usize> { |
| 115 | Ok(fdt.compatible_nodes(cstr!("arm,arm-v8"))?.count()) |
| 116 | } |
| 117 | |
| 118 | /// Validate number of CPUs |
| 119 | fn validate_num_cpus(num_cpus: usize) -> Result<(), RebootReason> { |
| 120 | if num_cpus == 0 { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 121 | error!("Number of CPU can't be 0"); |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 122 | return Err(RebootReason::InvalidFdt); |
| 123 | } |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 124 | if DeviceTreeInfo::GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus.try_into().unwrap()).is_none() { |
| 125 | error!("Too many CPUs for gic: {}", num_cpus); |
| 126 | return Err(RebootReason::InvalidFdt); |
| 127 | } |
| 128 | Ok(()) |
| 129 | } |
| 130 | |
| 131 | /// Patch DT by keeping `num_cpus` number of arm,arm-v8 compatible nodes, and pruning the rest. |
| 132 | fn patch_num_cpus(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> { |
| 133 | let cpu = cstr!("arm,arm-v8"); |
| 134 | let mut next = fdt.root_mut()?.next_compatible(cpu)?; |
| 135 | for _ in 0..num_cpus { |
| 136 | next = if let Some(current) = next { |
| 137 | current.next_compatible(cpu)? |
| 138 | } else { |
| 139 | return Err(FdtError::NoSpace); |
| 140 | }; |
| 141 | } |
| 142 | while let Some(current) = next { |
| 143 | next = current.delete_and_next_compatible(cpu)?; |
| 144 | } |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 145 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #[derive(Debug)] |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 149 | struct PciInfo { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 150 | ranges: [PciAddrRange; 2], |
| 151 | irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]>, |
| 152 | irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]>, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 155 | impl PciInfo { |
| 156 | const IRQ_MASK_CELLS: usize = 4; |
| 157 | const IRQ_MAP_CELLS: usize = 10; |
| 158 | const MAX_IRQS: usize = 8; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 161 | type PciAddrRange = AddressRange<(u32, u64), u64, u64>; |
| 162 | type PciIrqMask = [u32; PciInfo::IRQ_MASK_CELLS]; |
| 163 | type PciIrqMap = [u32; PciInfo::IRQ_MAP_CELLS]; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 164 | |
| 165 | /// Iterator that takes N cells as a chunk |
| 166 | struct CellChunkIterator<'a, const N: usize> { |
| 167 | cells: CellIterator<'a>, |
| 168 | } |
| 169 | |
| 170 | impl<'a, const N: usize> CellChunkIterator<'a, N> { |
| 171 | fn new(cells: CellIterator<'a>) -> Self { |
| 172 | Self { cells } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | impl<'a, const N: usize> Iterator for CellChunkIterator<'a, N> { |
| 177 | type Item = [u32; N]; |
| 178 | fn next(&mut self) -> Option<Self::Item> { |
| 179 | let mut ret: Self::Item = [0; N]; |
| 180 | for i in ret.iter_mut() { |
| 181 | *i = self.cells.next()?; |
| 182 | } |
| 183 | Some(ret) |
| 184 | } |
| 185 | } |
| 186 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 187 | /// Read pci host controller ranges, irq maps, and irq map masks from DT |
| 188 | fn read_pci_info_from(fdt: &Fdt) -> libfdt::Result<PciInfo> { |
| 189 | let node = |
| 190 | fdt.compatible_nodes(cstr!("pci-host-cam-generic"))?.next().ok_or(FdtError::NotFound)?; |
| 191 | |
| 192 | let mut ranges = node.ranges::<(u32, u64), u64, u64>()?.ok_or(FdtError::NotFound)?; |
| 193 | let range0 = ranges.next().ok_or(FdtError::NotFound)?; |
| 194 | let range1 = ranges.next().ok_or(FdtError::NotFound)?; |
| 195 | |
| 196 | let irq_masks = node.getprop_cells(cstr!("interrupt-map-mask"))?.ok_or(FdtError::NotFound)?; |
| 197 | let irq_masks = CellChunkIterator::<{ PciInfo::IRQ_MASK_CELLS }>::new(irq_masks); |
| 198 | let irq_masks: ArrayVec<[PciIrqMask; PciInfo::MAX_IRQS]> = |
| 199 | irq_masks.take(PciInfo::MAX_IRQS).collect(); |
| 200 | |
| 201 | let irq_maps = node.getprop_cells(cstr!("interrupt-map"))?.ok_or(FdtError::NotFound)?; |
| 202 | let irq_maps = CellChunkIterator::<{ PciInfo::IRQ_MAP_CELLS }>::new(irq_maps); |
| 203 | let irq_maps: ArrayVec<[PciIrqMap; PciInfo::MAX_IRQS]> = |
| 204 | irq_maps.take(PciInfo::MAX_IRQS).collect(); |
| 205 | |
| 206 | Ok(PciInfo { ranges: [range0, range1], irq_masks, irq_maps }) |
| 207 | } |
| 208 | |
| 209 | fn validate_pci_info(pci_info: &PciInfo) -> Result<(), RebootReason> { |
| 210 | for range in pci_info.ranges.iter() { |
| 211 | validate_pci_addr_range(range)?; |
| 212 | } |
| 213 | for irq_mask in pci_info.irq_masks.iter() { |
| 214 | validate_pci_irq_mask(irq_mask)?; |
| 215 | } |
| 216 | for (idx, irq_map) in pci_info.irq_maps.iter().enumerate() { |
| 217 | validate_pci_irq_map(irq_map, idx)?; |
| 218 | } |
| 219 | Ok(()) |
| 220 | } |
| 221 | |
| 222 | fn validate_pci_addr_range(range: &PciAddrRange) -> Result<(), RebootReason> { |
| 223 | let mem_flags = PciMemoryFlags(range.addr.0); |
| 224 | let range_type = mem_flags.range_type(); |
| 225 | let prefetchable = mem_flags.prefetchable(); |
| 226 | let bus_addr = range.addr.1; |
| 227 | let cpu_addr = range.parent_addr; |
| 228 | let size = range.size; |
| 229 | |
| 230 | if range_type != PciRangeType::Memory64 { |
| 231 | error!("Invalid range type {:?} for bus address {:#x} in PCI node", range_type, bus_addr); |
| 232 | return Err(RebootReason::InvalidFdt); |
| 233 | } |
| 234 | if prefetchable { |
| 235 | error!("PCI bus address {:#x} in PCI node is prefetchable", bus_addr); |
| 236 | return Err(RebootReason::InvalidFdt); |
| 237 | } |
| 238 | // Enforce ID bus-to-cpu mappings, as used by crosvm. |
| 239 | if bus_addr != cpu_addr { |
| 240 | error!("PCI bus address: {:#x} is different from CPU address: {:#x}", bus_addr, cpu_addr); |
| 241 | return Err(RebootReason::InvalidFdt); |
| 242 | } |
| 243 | |
| 244 | if bus_addr.checked_add(size).is_none() { |
| 245 | error!("PCI address range size {:#x} too big", size); |
| 246 | return Err(RebootReason::InvalidFdt); |
| 247 | } |
| 248 | |
| 249 | Ok(()) |
| 250 | } |
| 251 | |
| 252 | fn validate_pci_irq_mask(irq_mask: &PciIrqMask) -> Result<(), RebootReason> { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 253 | const IRQ_MASK_ADDR_HI: u32 = 0xf800; |
| 254 | const IRQ_MASK_ADDR_ME: u32 = 0x0; |
| 255 | const IRQ_MASK_ADDR_LO: u32 = 0x0; |
| 256 | const IRQ_MASK_ANY_IRQ: u32 = 0x7; |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 257 | const EXPECTED: PciIrqMask = |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 258 | [IRQ_MASK_ADDR_HI, IRQ_MASK_ADDR_ME, IRQ_MASK_ADDR_LO, IRQ_MASK_ANY_IRQ]; |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 259 | if *irq_mask != EXPECTED { |
| 260 | error!("Invalid PCI irq mask {:#?}", irq_mask); |
| 261 | return Err(RebootReason::InvalidFdt); |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 262 | } |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 263 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 266 | fn validate_pci_irq_map(irq_map: &PciIrqMap, idx: usize) -> Result<(), RebootReason> { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 267 | const PCI_DEVICE_IDX: usize = 11; |
| 268 | const PCI_IRQ_ADDR_ME: u32 = 0; |
| 269 | const PCI_IRQ_ADDR_LO: u32 = 0; |
| 270 | const PCI_IRQ_INTC: u32 = 1; |
| 271 | const AARCH64_IRQ_BASE: u32 = 4; // from external/crosvm/aarch64/src/lib.rs |
| 272 | const GIC_SPI: u32 = 0; |
| 273 | const IRQ_TYPE_LEVEL_HIGH: u32 = 4; |
| 274 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 275 | let pci_addr = (irq_map[0], irq_map[1], irq_map[2]); |
| 276 | let pci_irq_number = irq_map[3]; |
| 277 | let _controller_phandle = irq_map[4]; // skipped. |
| 278 | let gic_addr = (irq_map[5], irq_map[6]); // address-cells is <2> for GIC |
| 279 | // interrupt-cells is <3> for GIC |
| 280 | let gic_peripheral_interrupt_type = irq_map[7]; |
| 281 | let gic_irq_number = irq_map[8]; |
| 282 | let gic_irq_type = irq_map[9]; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 283 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 284 | let phys_hi: u32 = (0x1 << PCI_DEVICE_IDX) * (idx + 1) as u32; |
| 285 | let expected_pci_addr = (phys_hi, PCI_IRQ_ADDR_ME, PCI_IRQ_ADDR_LO); |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 286 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 287 | if pci_addr != expected_pci_addr { |
| 288 | error!("PCI device address {:#x} {:#x} {:#x} in interrupt-map is different from expected address \ |
| 289 | {:#x} {:#x} {:#x}", |
| 290 | pci_addr.0, pci_addr.1, pci_addr.2, expected_pci_addr.0, expected_pci_addr.1, expected_pci_addr.2); |
| 291 | return Err(RebootReason::InvalidFdt); |
| 292 | } |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 293 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 294 | if pci_irq_number != PCI_IRQ_INTC { |
| 295 | error!( |
| 296 | "PCI INT# {:#x} in interrupt-map is different from expected value {:#x}", |
| 297 | pci_irq_number, PCI_IRQ_INTC |
| 298 | ); |
| 299 | return Err(RebootReason::InvalidFdt); |
| 300 | } |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 301 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 302 | if gic_addr != (0, 0) { |
| 303 | error!( |
| 304 | "GIC address {:#x} {:#x} in interrupt-map is different from expected address \ |
| 305 | {:#x} {:#x}", |
| 306 | gic_addr.0, gic_addr.1, 0, 0 |
| 307 | ); |
| 308 | return Err(RebootReason::InvalidFdt); |
| 309 | } |
| 310 | |
| 311 | if gic_peripheral_interrupt_type != GIC_SPI { |
| 312 | error!("GIC peripheral interrupt type {:#x} in interrupt-map is different from expected value \ |
| 313 | {:#x}", gic_peripheral_interrupt_type, GIC_SPI); |
| 314 | return Err(RebootReason::InvalidFdt); |
| 315 | } |
| 316 | |
| 317 | let irq_nr: u32 = AARCH64_IRQ_BASE + (idx as u32); |
| 318 | if gic_irq_number != irq_nr { |
| 319 | error!( |
| 320 | "GIC irq number {:#x} in interrupt-map is unexpected. Expected {:#x}", |
| 321 | gic_irq_number, irq_nr |
| 322 | ); |
| 323 | return Err(RebootReason::InvalidFdt); |
| 324 | } |
| 325 | |
| 326 | if gic_irq_type != IRQ_TYPE_LEVEL_HIGH { |
| 327 | error!( |
| 328 | "IRQ type in {:#x} is invalid. Must be LEVEL_HIGH {:#x}", |
| 329 | gic_irq_type, IRQ_TYPE_LEVEL_HIGH |
| 330 | ); |
| 331 | return Err(RebootReason::InvalidFdt); |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 332 | } |
| 333 | Ok(()) |
| 334 | } |
| 335 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 336 | fn patch_pci_info(fdt: &mut Fdt, pci_info: &PciInfo) -> libfdt::Result<()> { |
| 337 | let mut node = fdt |
| 338 | .root_mut()? |
| 339 | .next_compatible(cstr!("pci-host-cam-generic"))? |
| 340 | .ok_or(FdtError::NotFound)?; |
| 341 | |
| 342 | let irq_masks_size = pci_info.irq_masks.len() * size_of::<PciIrqMask>(); |
| 343 | node.trimprop(cstr!("interrupt-map-mask"), irq_masks_size)?; |
| 344 | |
| 345 | let irq_maps_size = pci_info.irq_maps.len() * size_of::<PciIrqMap>(); |
| 346 | node.trimprop(cstr!("interrupt-map"), irq_maps_size)?; |
| 347 | |
| 348 | node.setprop_inplace( |
| 349 | cstr!("ranges"), |
| 350 | flatten(&[pci_info.ranges[0].to_cells(), pci_info.ranges[1].to_cells()]), |
| 351 | ) |
| 352 | } |
| 353 | |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 354 | #[derive(Default, Debug)] |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 355 | struct SerialInfo { |
| 356 | addrs: ArrayVec<[u64; Self::MAX_SERIALS]>, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | impl SerialInfo { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 360 | const MAX_SERIALS: usize = 4; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 363 | fn read_serial_info_from(fdt: &Fdt) -> libfdt::Result<SerialInfo> { |
| 364 | let mut addrs: ArrayVec<[u64; SerialInfo::MAX_SERIALS]> = Default::default(); |
| 365 | for node in fdt.compatible_nodes(cstr!("ns16550a"))?.take(SerialInfo::MAX_SERIALS) { |
| 366 | let reg = node.reg()?.ok_or(FdtError::NotFound)?.next().ok_or(FdtError::NotFound)?; |
| 367 | addrs.push(reg.addr); |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 368 | } |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 369 | Ok(SerialInfo { addrs }) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 372 | /// Patch the DT by deleting the ns16550a compatible nodes whose address are unknown |
| 373 | fn patch_serial_info(fdt: &mut Fdt, serial_info: &SerialInfo) -> libfdt::Result<()> { |
| 374 | let name = cstr!("ns16550a"); |
| 375 | let mut next = fdt.root_mut()?.next_compatible(name); |
| 376 | while let Some(current) = next? { |
| 377 | let reg = FdtNode::from_mut(¤t) |
| 378 | .reg()? |
| 379 | .ok_or(FdtError::NotFound)? |
| 380 | .next() |
| 381 | .ok_or(FdtError::NotFound)?; |
| 382 | next = if !serial_info.addrs.contains(®.addr) { |
| 383 | current.delete_and_next_compatible(name) |
| 384 | } else { |
| 385 | current.next_compatible(name) |
| 386 | } |
| 387 | } |
| 388 | Ok(()) |
| 389 | } |
| 390 | |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 391 | #[derive(Debug)] |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 392 | struct SwiotlbInfo { |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 393 | size: u64, |
| 394 | align: u64, |
| 395 | } |
| 396 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 397 | fn read_swiotlb_info_from(fdt: &Fdt) -> libfdt::Result<SwiotlbInfo> { |
| 398 | let node = |
| 399 | fdt.compatible_nodes(cstr!("restricted-dma-pool"))?.next().ok_or(FdtError::NotFound)?; |
| 400 | let size = node.getprop_u64(cstr!("size"))?.ok_or(FdtError::NotFound)?; |
| 401 | let align = node.getprop_u64(cstr!("alignment"))?.ok_or(FdtError::NotFound)?; |
| 402 | Ok(SwiotlbInfo { size, align }) |
| 403 | } |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 404 | |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 405 | fn validate_swiotlb_info(swiotlb_info: &SwiotlbInfo) -> Result<(), RebootReason> { |
| 406 | let size = swiotlb_info.size; |
| 407 | let align = swiotlb_info.align; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 408 | |
| 409 | if size == 0 || (size % GUEST_PAGE_SIZE as u64) != 0 { |
| 410 | error!("Invalid swiotlb size {:#x}", size); |
| 411 | return Err(RebootReason::InvalidFdt); |
| 412 | } |
| 413 | |
| 414 | if (align % GUEST_PAGE_SIZE as u64) != 0 { |
| 415 | error!("Invalid swiotlb alignment {:#x}", align); |
| 416 | return Err(RebootReason::InvalidFdt); |
| 417 | } |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 418 | Ok(()) |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 421 | fn patch_swiotlb_info(fdt: &mut Fdt, swiotlb_info: &SwiotlbInfo) -> libfdt::Result<()> { |
| 422 | let mut node = |
| 423 | fdt.root_mut()?.next_compatible(cstr!("restricted-dma-pool"))?.ok_or(FdtError::NotFound)?; |
| 424 | node.setprop_inplace(cstr!("size"), &swiotlb_info.size.to_be_bytes())?; |
| 425 | node.setprop_inplace(cstr!("alignment"), &swiotlb_info.align.to_be_bytes())?; |
| 426 | Ok(()) |
| 427 | } |
| 428 | |
| 429 | fn patch_gic(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> { |
| 430 | let node = fdt.compatible_nodes(cstr!("arm,gic-v3"))?.next().ok_or(FdtError::NotFound)?; |
| 431 | let mut ranges = node.reg()?.ok_or(FdtError::NotFound)?; |
| 432 | let range0 = ranges.next().ok_or(FdtError::NotFound)?; |
| 433 | let mut range1 = ranges.next().ok_or(FdtError::NotFound)?; |
| 434 | |
| 435 | let addr = range0.addr; |
| 436 | // SAFETY - doesn't overflow. checked in validate_num_cpus |
| 437 | let size: u64 = |
| 438 | DeviceTreeInfo::GIC_REDIST_SIZE_PER_CPU.checked_mul(num_cpus.try_into().unwrap()).unwrap(); |
| 439 | |
| 440 | // range1 is just below range0 |
| 441 | range1.addr = addr - size; |
| 442 | range1.size = Some(size); |
| 443 | |
| 444 | let range0 = range0.to_cells(); |
| 445 | let range1 = range1.to_cells(); |
| 446 | let value = [ |
| 447 | range0.0, // addr |
| 448 | range0.1.unwrap(), //size |
| 449 | range1.0, // addr |
| 450 | range1.1.unwrap(), //size |
| 451 | ]; |
| 452 | |
| 453 | let mut node = |
| 454 | fdt.root_mut()?.next_compatible(cstr!("arm,gic-v3"))?.ok_or(FdtError::NotFound)?; |
| 455 | node.setprop_inplace(cstr!("reg"), flatten(&value)) |
| 456 | } |
| 457 | |
| 458 | fn patch_timer(fdt: &mut Fdt, num_cpus: usize) -> libfdt::Result<()> { |
| 459 | const NUM_INTERRUPTS: usize = 4; |
| 460 | const CELLS_PER_INTERRUPT: usize = 3; |
| 461 | let node = fdt.compatible_nodes(cstr!("arm,armv8-timer"))?.next().ok_or(FdtError::NotFound)?; |
| 462 | let interrupts = node.getprop_cells(cstr!("interrupts"))?.ok_or(FdtError::NotFound)?; |
| 463 | let mut value: ArrayVec<[u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT]> = |
| 464 | interrupts.take(NUM_INTERRUPTS * CELLS_PER_INTERRUPT).collect(); |
| 465 | |
| 466 | let num_cpus: u32 = num_cpus.try_into().unwrap(); |
| 467 | let cpu_mask: u32 = (((0x1 << num_cpus) - 1) & 0xff) << 8; |
| 468 | for v in value.iter_mut().skip(2).step_by(CELLS_PER_INTERRUPT) { |
| 469 | *v |= cpu_mask; |
| 470 | } |
| 471 | for v in value.iter_mut() { |
| 472 | *v = v.to_be(); |
| 473 | } |
| 474 | |
| 475 | // SAFETY - array size is the same |
| 476 | let value = unsafe { |
| 477 | core::mem::transmute::< |
| 478 | [u32; NUM_INTERRUPTS * CELLS_PER_INTERRUPT], |
| 479 | [u8; NUM_INTERRUPTS * CELLS_PER_INTERRUPT * size_of::<u32>()], |
| 480 | >(value.into_inner()) |
| 481 | }; |
| 482 | |
| 483 | let mut node = |
| 484 | fdt.root_mut()?.next_compatible(cstr!("arm,armv8-timer"))?.ok_or(FdtError::NotFound)?; |
| 485 | node.setprop_inplace(cstr!("interrupts"), value.as_slice()) |
| 486 | } |
| 487 | |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 488 | #[derive(Debug)] |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 489 | pub struct DeviceTreeInfo { |
| 490 | pub kernel_range: Option<Range<usize>>, |
| 491 | pub initrd_range: Option<Range<usize>>, |
| 492 | pub memory_range: Range<usize>, |
| 493 | num_cpus: usize, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 494 | pci_info: PciInfo, |
| 495 | serial_info: SerialInfo, |
| 496 | swiotlb_info: SwiotlbInfo, |
| 497 | } |
| 498 | |
| 499 | impl DeviceTreeInfo { |
| 500 | const RAM_BASE_ADDR: u64 = 0x8000_0000; |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 501 | const GIC_REDIST_SIZE_PER_CPU: u64 = (32 * SIZE_4KB) as u64; |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 504 | pub fn sanitize_device_tree(fdt: &mut Fdt) -> Result<DeviceTreeInfo, RebootReason> { |
Jiyong Park | 8331612 | 2023-03-21 09:39:39 +0900 | [diff] [blame] | 505 | let info = parse_device_tree(fdt)?; |
| 506 | debug!("Device tree info: {:?}", info); |
| 507 | |
| 508 | // TODO: replace fdt with the template DT |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 509 | patch_device_tree(fdt, &info)?; |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 510 | Ok(info) |
Jiyong Park | 8331612 | 2023-03-21 09:39:39 +0900 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | fn parse_device_tree(fdt: &libfdt::Fdt) -> Result<DeviceTreeInfo, RebootReason> { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 514 | let kernel_range = read_kernel_range_from(fdt).map_err(|e| { |
| 515 | error!("Failed to read kernel range from DT: {e}"); |
| 516 | RebootReason::InvalidFdt |
| 517 | })?; |
| 518 | |
| 519 | let initrd_range = read_initrd_range_from(fdt).map_err(|e| { |
| 520 | error!("Failed to read initrd range from DT: {e}"); |
| 521 | RebootReason::InvalidFdt |
| 522 | })?; |
| 523 | |
| 524 | let memory_range = read_memory_range_from(fdt).map_err(|e| { |
| 525 | error!("Failed to read memory range from DT: {e}"); |
| 526 | RebootReason::InvalidFdt |
| 527 | })?; |
| 528 | validate_memory_range(&memory_range)?; |
| 529 | |
| 530 | let num_cpus = read_num_cpus_from(fdt).map_err(|e| { |
| 531 | error!("Failed to read num cpus from DT: {e}"); |
| 532 | RebootReason::InvalidFdt |
| 533 | })?; |
| 534 | validate_num_cpus(num_cpus)?; |
| 535 | |
| 536 | let pci_info = read_pci_info_from(fdt).map_err(|e| { |
| 537 | error!("Failed to read pci info from DT: {e}"); |
| 538 | RebootReason::InvalidFdt |
| 539 | })?; |
| 540 | validate_pci_info(&pci_info)?; |
| 541 | |
| 542 | let serial_info = read_serial_info_from(fdt).map_err(|e| { |
| 543 | error!("Failed to read serial info from DT: {e}"); |
| 544 | RebootReason::InvalidFdt |
| 545 | })?; |
| 546 | |
| 547 | let swiotlb_info = read_swiotlb_info_from(fdt).map_err(|e| { |
| 548 | error!("Failed to read swiotlb info from DT: {e}"); |
| 549 | RebootReason::InvalidFdt |
| 550 | })?; |
| 551 | validate_swiotlb_info(&swiotlb_info)?; |
| 552 | |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 553 | Ok(DeviceTreeInfo { |
Jiyong Park | 6a8789a | 2023-03-21 14:50:59 +0900 | [diff] [blame] | 554 | kernel_range, |
| 555 | initrd_range, |
| 556 | memory_range, |
| 557 | num_cpus, |
| 558 | pci_info, |
| 559 | serial_info, |
| 560 | swiotlb_info, |
Jiyong Park | 00ceff3 | 2023-03-13 05:43:23 +0000 | [diff] [blame] | 561 | }) |
| 562 | } |
| 563 | |
Jiyong Park | 9c63cd1 | 2023-03-21 17:53:07 +0900 | [diff] [blame^] | 564 | fn patch_device_tree(fdt: &mut Fdt, info: &DeviceTreeInfo) -> Result<(), RebootReason> { |
| 565 | if let Some(initrd_range) = &info.initrd_range { |
| 566 | patch_initrd_range(fdt, initrd_range).map_err(|e| { |
| 567 | error!("Failed to patch initrd range to DT: {e}"); |
| 568 | RebootReason::InvalidFdt |
| 569 | })?; |
| 570 | } |
| 571 | patch_memory_range(fdt, &info.memory_range).map_err(|e| { |
| 572 | error!("Failed to patch memory range to DT: {e}"); |
| 573 | RebootReason::InvalidFdt |
| 574 | })?; |
| 575 | patch_num_cpus(fdt, info.num_cpus).map_err(|e| { |
| 576 | error!("Failed to patch cpus to DT: {e}"); |
| 577 | RebootReason::InvalidFdt |
| 578 | })?; |
| 579 | patch_pci_info(fdt, &info.pci_info).map_err(|e| { |
| 580 | error!("Failed to patch pci info to DT: {e}"); |
| 581 | RebootReason::InvalidFdt |
| 582 | })?; |
| 583 | patch_serial_info(fdt, &info.serial_info).map_err(|e| { |
| 584 | error!("Failed to patch serial info to DT: {e}"); |
| 585 | RebootReason::InvalidFdt |
| 586 | })?; |
| 587 | patch_swiotlb_info(fdt, &info.swiotlb_info).map_err(|e| { |
| 588 | error!("Failed to patch swiotlb info to DT: {e}"); |
| 589 | RebootReason::InvalidFdt |
| 590 | })?; |
| 591 | patch_gic(fdt, info.num_cpus).map_err(|e| { |
| 592 | error!("Failed to patch gic info to DT: {e}"); |
| 593 | RebootReason::InvalidFdt |
| 594 | })?; |
| 595 | patch_timer(fdt, info.num_cpus).map_err(|e| { |
| 596 | error!("Failed to patch timer info to DT: {e}"); |
| 597 | RebootReason::InvalidFdt |
| 598 | })?; |
| 599 | Ok(()) |
| 600 | } |
| 601 | |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 602 | /// Modifies the input DT according to the fields of the configuration. |
| 603 | pub fn modify_for_next_stage( |
| 604 | fdt: &mut Fdt, |
| 605 | bcc: &[u8], |
| 606 | new_instance: bool, |
| 607 | strict_boot: bool, |
| 608 | ) -> libfdt::Result<()> { |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 609 | fdt.unpack()?; |
| 610 | |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 611 | add_dice_node(fdt, bcc.as_ptr() as usize, bcc.len())?; |
| 612 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 613 | set_or_clear_chosen_flag(fdt, cstr!("avf,strict-boot"), strict_boot)?; |
| 614 | set_or_clear_chosen_flag(fdt, cstr!("avf,new-instance"), new_instance)?; |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 615 | |
| 616 | fdt.pack()?; |
| 617 | |
| 618 | Ok(()) |
| 619 | } |
| 620 | |
| 621 | /// Add a "google,open-dice"-compatible reserved-memory node to the tree. |
| 622 | fn add_dice_node(fdt: &mut Fdt, addr: usize, size: usize) -> libfdt::Result<()> { |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 623 | // We reject DTs with missing reserved-memory node as validation should have checked that the |
| 624 | // "swiotlb" subnode (compatible = "restricted-dma-pool") was present. |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 625 | let mut reserved_memory = |
| 626 | fdt.node_mut(cstr!("/reserved-memory"))?.ok_or(libfdt::FdtError::NotFound)?; |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 627 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 628 | let mut dice = reserved_memory.add_subnode(cstr!("dice"))?; |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 629 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 630 | dice.appendprop(cstr!("compatible"), b"google,open-dice\0")?; |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 631 | |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 632 | dice.appendprop(cstr!("no-map"), &[])?; |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 633 | |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 634 | let addr = addr.try_into().unwrap(); |
| 635 | let size = size.try_into().unwrap(); |
Jiyong Park | b87f330 | 2023-03-21 10:03:11 +0900 | [diff] [blame] | 636 | dice.appendprop_addrrange(cstr!("reg"), addr, size)?; |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 637 | |
Pierre-Clément Tosi | 4ba7966 | 2023-02-13 11:22:41 +0000 | [diff] [blame] | 638 | Ok(()) |
| 639 | } |
| 640 | |
| 641 | fn set_or_clear_chosen_flag(fdt: &mut Fdt, flag: &CStr, value: bool) -> libfdt::Result<()> { |
| 642 | // TODO(b/249054080): Refactor to not panic if the DT doesn't contain a /chosen node. |
| 643 | let mut chosen = fdt.chosen_mut()?.unwrap(); |
| 644 | if value { |
| 645 | chosen.setprop_empty(flag)?; |
| 646 | } else { |
| 647 | match chosen.delprop(flag) { |
| 648 | Ok(()) | Err(FdtError::NotFound) => (), |
| 649 | Err(e) => return Err(e), |
| 650 | } |
| 651 | } |
Pierre-Clément Tosi | db74cb1 | 2022-12-08 13:56:25 +0000 | [diff] [blame] | 652 | |
| 653 | Ok(()) |
| 654 | } |