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