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>) -> Result<impl PinInit<Self, Error>> { 123 let dev = pdev.as_ref(); 124 let libos = CoherentAllocation::<LibosMemoryRegionInitArgument>::alloc_coherent( 125 dev, 126 GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(), 127 GFP_KERNEL | __GFP_ZERO, 128 )?; 129 130 // Initialise the logging structures. The OpenRM equivalents are in: 131 // _kgspInitLibosLoggingStructures (allocates memory for buffers) 132 // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array) 133 let loginit = LogBuffer::new(dev)?; 134 dma_write!(libos[0] = LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0))?; 135 136 let logintr = LogBuffer::new(dev)?; 137 dma_write!(libos[1] = LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0))?; 138 139 let logrm = LogBuffer::new(dev)?; 140 dma_write!(libos[2] = LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0))?; 141 142 let cmdq = Cmdq::new(dev)?; 143 144 let rmargs = CoherentAllocation::<GspArgumentsCached>::alloc_coherent( 145 dev, 146 1, 147 GFP_KERNEL | __GFP_ZERO, 148 )?; 149 dma_write!(rmargs[0] = fw::GspArgumentsCached::new(&cmdq))?; 150 dma_write!(libos[3] = LibosMemoryRegionInitArgument::new("RMARGS", &rmargs))?; 151 152 Ok(try_pin_init!(Self { 153 libos, 154 loginit, 155 logintr, 156 logrm, 157 rmargs, 158 cmdq, 159 })) 160 } 161 } 162