1 // SPDX-License-Identifier: GPL-2.0 2 3 mod boot; 4 5 use kernel::{ 6 device, 7 dma::{ 8 CoherentAllocation, 9 DmaAddress, // 10 }, 11 dma_write, 12 pci, 13 prelude::*, 14 transmute::AsBytes, // 15 }; 16 17 pub(crate) mod cmdq; 18 pub(crate) mod commands; 19 mod fw; 20 mod sequencer; 21 22 pub(crate) use fw::{ 23 GspFwWprMeta, 24 LibosParams, // 25 }; 26 27 use crate::{ 28 gsp::cmdq::Cmdq, 29 gsp::fw::{ 30 GspArgumentsPadded, 31 LibosMemoryRegionInitArgument, // 32 }, 33 num, 34 }; 35 36 pub(crate) const GSP_PAGE_SHIFT: usize = 12; 37 pub(crate) const GSP_PAGE_SIZE: usize = 1 << GSP_PAGE_SHIFT; 38 39 /// Number of GSP pages to use in a RM log buffer. 40 const RM_LOG_BUFFER_NUM_PAGES: usize = 0x10; 41 42 /// Array of page table entries, as understood by the GSP bootloader. 43 #[repr(C)] 44 struct PteArray<const NUM_ENTRIES: usize>([u64; NUM_ENTRIES]); 45 46 /// SAFETY: arrays of `u64` implement `AsBytes` and we are but a wrapper around one. 47 unsafe impl<const NUM_ENTRIES: usize> AsBytes for PteArray<NUM_ENTRIES> {} 48 49 impl<const NUM_PAGES: usize> PteArray<NUM_PAGES> { 50 /// Returns the page table entry for `index`, for a mapping starting at `start`. 51 // TODO: Replace with `IoView` projection once available. entry(start: DmaAddress, index: usize) -> Result<u64>52 fn entry(start: DmaAddress, index: usize) -> Result<u64> { 53 start 54 .checked_add(num::usize_as_u64(index) << GSP_PAGE_SHIFT) 55 .ok_or(EOVERFLOW) 56 } 57 } 58 59 /// The logging buffers are byte queues that contain encoded printf-like 60 /// messages from GSP-RM. They need to be decoded by a special application 61 /// that can parse the buffers. 62 /// 63 /// The 'loginit' buffer contains logs from early GSP-RM init and 64 /// exception dumps. The 'logrm' buffer contains the subsequent logs. Both are 65 /// written to directly by GSP-RM and can be any multiple of GSP_PAGE_SIZE. 66 /// 67 /// The physical address map for the log buffer is stored in the buffer 68 /// itself, starting with offset 1. Offset 0 contains the "put" pointer (pp). 69 /// Initially, pp is equal to 0. If the buffer has valid logging data in it, 70 /// then pp points to index into the buffer where the next logging entry will 71 /// be written. Therefore, the logging data is valid if: 72 /// 1 <= pp < sizeof(buffer)/sizeof(u64) 73 struct LogBuffer(CoherentAllocation<u8>); 74 75 impl LogBuffer { 76 /// Creates a new `LogBuffer` mapped on `dev`. new(dev: &device::Device<device::Bound>) -> Result<Self>77 fn new(dev: &device::Device<device::Bound>) -> Result<Self> { 78 const NUM_PAGES: usize = RM_LOG_BUFFER_NUM_PAGES; 79 80 let mut obj = Self(CoherentAllocation::<u8>::alloc_coherent( 81 dev, 82 NUM_PAGES * GSP_PAGE_SIZE, 83 GFP_KERNEL | __GFP_ZERO, 84 )?); 85 86 let start_addr = obj.0.dma_handle(); 87 88 // SAFETY: `obj` has just been created and we are its sole user. 89 let pte_region = unsafe { 90 obj.0 91 .as_slice_mut(size_of::<u64>(), NUM_PAGES * size_of::<u64>())? 92 }; 93 94 // Write values one by one to avoid an on-stack instance of `PteArray`. 95 for (i, chunk) in pte_region.chunks_exact_mut(size_of::<u64>()).enumerate() { 96 let pte_value = PteArray::<0>::entry(start_addr, i)?; 97 98 chunk.copy_from_slice(&pte_value.to_ne_bytes()); 99 } 100 101 Ok(obj) 102 } 103 } 104 105 /// GSP runtime data. 106 #[pin_data] 107 pub(crate) struct Gsp { 108 /// Libos arguments. 109 pub(crate) libos: CoherentAllocation<LibosMemoryRegionInitArgument>, 110 /// Init log buffer. 111 loginit: LogBuffer, 112 /// Interrupts log buffer. 113 logintr: LogBuffer, 114 /// RM log buffer. 115 logrm: LogBuffer, 116 /// Command queue. 117 pub(crate) cmdq: Cmdq, 118 /// RM arguments. 119 rmargs: CoherentAllocation<GspArgumentsPadded>, 120 } 121 122 impl Gsp { 123 // Creates an in-place initializer for a `Gsp` manager for `pdev`. new(pdev: &pci::Device<device::Bound>) -> impl PinInit<Self, Error> + '_124 pub(crate) fn new(pdev: &pci::Device<device::Bound>) -> impl PinInit<Self, Error> + '_ { 125 pin_init::pin_init_scope(move || { 126 let dev = pdev.as_ref(); 127 128 Ok(try_pin_init!(Self { 129 libos: CoherentAllocation::<LibosMemoryRegionInitArgument>::alloc_coherent( 130 dev, 131 GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(), 132 GFP_KERNEL | __GFP_ZERO, 133 )?, 134 loginit: LogBuffer::new(dev)?, 135 logintr: LogBuffer::new(dev)?, 136 logrm: LogBuffer::new(dev)?, 137 cmdq: Cmdq::new(dev)?, 138 rmargs: CoherentAllocation::<GspArgumentsPadded>::alloc_coherent( 139 dev, 140 1, 141 GFP_KERNEL | __GFP_ZERO, 142 )?, 143 _: { 144 // Initialise the logging structures. The OpenRM equivalents are in: 145 // _kgspInitLibosLoggingStructures (allocates memory for buffers) 146 // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array) 147 dma_write!( 148 libos, [0]?, LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0) 149 ); 150 dma_write!( 151 libos, [1]?, LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0) 152 ); 153 dma_write!(libos, [2]?, LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0)); 154 dma_write!(rmargs, [0]?.inner, fw::GspArgumentsCached::new(cmdq)); 155 dma_write!(libos, [3]?, LibosMemoryRegionInitArgument::new("RMARGS", rmargs)); 156 }, 157 })) 158 }) 159 } 160 } 161