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