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 GspArgumentsCached, 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 /// Creates a new page table array mapping `NUM_PAGES` GSP pages starting at address `start`. 51 fn new(start: DmaAddress) -> Result<Self> { 52 let mut ptes = [0u64; NUM_PAGES]; 53 for (i, pte) in ptes.iter_mut().enumerate() { 54 *pte = start 55 .checked_add(num::usize_as_u64(i) << GSP_PAGE_SHIFT) 56 .ok_or(EOVERFLOW)?; 57 } 58 59 Ok(Self(ptes)) 60 } 61 } 62 63 /// The logging buffers are byte queues that contain encoded printf-like 64 /// messages from GSP-RM. They need to be decoded by a special application 65 /// that can parse the buffers. 66 /// 67 /// The 'loginit' buffer contains logs from early GSP-RM init and 68 /// exception dumps. The 'logrm' buffer contains the subsequent logs. Both are 69 /// written to directly by GSP-RM and can be any multiple of GSP_PAGE_SIZE. 70 /// 71 /// The physical address map for the log buffer is stored in the buffer 72 /// itself, starting with offset 1. Offset 0 contains the "put" pointer (pp). 73 /// Initially, pp is equal to 0. If the buffer has valid logging data in it, 74 /// then pp points to index into the buffer where the next logging entry will 75 /// be written. Therefore, the logging data is valid if: 76 /// 1 <= pp < sizeof(buffer)/sizeof(u64) 77 struct LogBuffer(CoherentAllocation<u8>); 78 79 impl LogBuffer { 80 /// Creates a new `LogBuffer` mapped on `dev`. 81 fn new(dev: &device::Device<device::Bound>) -> Result<Self> { 82 const NUM_PAGES: usize = RM_LOG_BUFFER_NUM_PAGES; 83 84 let mut obj = Self(CoherentAllocation::<u8>::alloc_coherent( 85 dev, 86 NUM_PAGES * GSP_PAGE_SIZE, 87 GFP_KERNEL | __GFP_ZERO, 88 )?); 89 let ptes = PteArray::<NUM_PAGES>::new(obj.0.dma_handle())?; 90 91 // SAFETY: `obj` has just been created and we are its sole user. 92 unsafe { 93 // Copy the self-mapping PTE at the expected location. 94 obj.0 95 .as_slice_mut(size_of::<u64>(), size_of_val(&ptes))? 96 .copy_from_slice(ptes.as_bytes()) 97 }; 98 99 Ok(obj) 100 } 101 } 102 103 /// GSP runtime data. 104 #[pin_data] 105 pub(crate) struct Gsp { 106 /// Libos arguments. 107 pub(crate) libos: CoherentAllocation<LibosMemoryRegionInitArgument>, 108 /// Init log buffer. 109 loginit: LogBuffer, 110 /// Interrupts log buffer. 111 logintr: LogBuffer, 112 /// RM log buffer. 113 logrm: LogBuffer, 114 /// Command queue. 115 pub(crate) cmdq: Cmdq, 116 /// RM arguments. 117 rmargs: CoherentAllocation<GspArgumentsCached>, 118 } 119 120 impl Gsp { 121 // Creates an in-place initializer for a `Gsp` manager for `pdev`. 122 pub(crate) fn new(pdev: &pci::Device<device::Bound>) -> impl PinInit<Self, Error> + '_ { 123 pin_init::pin_init_scope(move || { 124 let dev = pdev.as_ref(); 125 126 Ok(try_pin_init!(Self { 127 libos: CoherentAllocation::<LibosMemoryRegionInitArgument>::alloc_coherent( 128 dev, 129 GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(), 130 GFP_KERNEL | __GFP_ZERO, 131 )?, 132 loginit: LogBuffer::new(dev)?, 133 logintr: LogBuffer::new(dev)?, 134 logrm: LogBuffer::new(dev)?, 135 cmdq: Cmdq::new(dev)?, 136 rmargs: CoherentAllocation::<GspArgumentsCached>::alloc_coherent( 137 dev, 138 1, 139 GFP_KERNEL | __GFP_ZERO, 140 )?, 141 _: { 142 // Initialise the logging structures. The OpenRM equivalents are in: 143 // _kgspInitLibosLoggingStructures (allocates memory for buffers) 144 // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array) 145 dma_write!( 146 libos[0] = LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0) 147 )?; 148 dma_write!( 149 libos[1] = LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0) 150 )?; 151 dma_write!(libos[2] = LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0))?; 152 dma_write!(rmargs[0] = fw::GspArgumentsCached::new(cmdq))?; 153 dma_write!(libos[3] = LibosMemoryRegionInitArgument::new("RMARGS", rmargs))?; 154 }, 155 })) 156 }) 157 } 158 } 159