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