Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

gpu: nova-core: convert Gsp::new() to use CoherentBox

Convert libos (LibosMemoryRegionInitArgument) and rmargs
(GspArgumentsPadded) to use CoherentBox / Coherent::init() and simplify
the initialization. This also avoids separate initialization on the
stack.

Reviewed-by: Gary Guo <gary@garyguo.net>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://patch.msgid.link/20260320194626.36263-8-dakr@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

+65 -44
+21 -26
drivers/gpu/nova-core/gsp.rs
··· 5 5 use kernel::{ 6 6 device, 7 7 dma::{ 8 + Coherent, 8 9 CoherentAllocation, 10 + CoherentBox, 9 11 DmaAddress, // 10 12 }, 11 - dma_write, 12 13 pci, 13 14 prelude::*, 14 15 transmute::AsBytes, // ··· 107 106 #[pin_data] 108 107 pub(crate) struct Gsp { 109 108 /// Libos arguments. 110 - pub(crate) libos: CoherentAllocation<LibosMemoryRegionInitArgument>, 109 + pub(crate) libos: Coherent<[LibosMemoryRegionInitArgument]>, 111 110 /// Init log buffer. 112 111 loginit: LogBuffer, 113 112 /// Interrupts log buffer. ··· 118 117 #[pin] 119 118 pub(crate) cmdq: Cmdq, 120 119 /// RM arguments. 121 - rmargs: CoherentAllocation<GspArgumentsPadded>, 120 + rmargs: Coherent<GspArgumentsPadded>, 122 121 } 123 122 124 123 impl Gsp { ··· 127 126 pin_init::pin_init_scope(move || { 128 127 let dev = pdev.as_ref(); 129 128 129 + // Initialise the logging structures. The OpenRM equivalents are in: 130 + // _kgspInitLibosLoggingStructures (allocates memory for buffers) 131 + // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array) 130 132 Ok(try_pin_init!(Self { 131 - libos: CoherentAllocation::<LibosMemoryRegionInitArgument>::alloc_coherent( 132 - dev, 133 - GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(), 134 - GFP_KERNEL | __GFP_ZERO, 135 - )?, 136 133 loginit: LogBuffer::new(dev)?, 137 134 logintr: LogBuffer::new(dev)?, 138 135 logrm: LogBuffer::new(dev)?, 139 136 cmdq <- Cmdq::new(dev), 140 - rmargs: CoherentAllocation::<GspArgumentsPadded>::alloc_coherent( 141 - dev, 142 - 1, 143 - GFP_KERNEL | __GFP_ZERO, 144 - )?, 145 - _: { 146 - // Initialise the logging structures. The OpenRM equivalents are in: 147 - // _kgspInitLibosLoggingStructures (allocates memory for buffers) 148 - // kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array) 149 - dma_write!( 150 - libos, [0]?, LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0) 151 - ); 152 - dma_write!( 153 - libos, [1]?, LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0) 154 - ); 155 - dma_write!(libos, [2]?, LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0)); 156 - dma_write!(rmargs, [0]?.inner, fw::GspArgumentsCached::new(&cmdq)); 157 - dma_write!(libos, [3]?, LibosMemoryRegionInitArgument::new("RMARGS", rmargs)); 137 + rmargs: Coherent::init(dev, GFP_KERNEL, GspArgumentsPadded::new(&cmdq))?, 138 + libos: { 139 + let mut libos = CoherentBox::zeroed_slice( 140 + dev, 141 + GSP_PAGE_SIZE / size_of::<LibosMemoryRegionInitArgument>(), 142 + GFP_KERNEL, 143 + )?; 144 + 145 + libos.init_at(0, LibosMemoryRegionInitArgument::new("LOGINIT", &loginit.0))?; 146 + libos.init_at(1, LibosMemoryRegionInitArgument::new("LOGINTR", &logintr.0))?; 147 + libos.init_at(2, LibosMemoryRegionInitArgument::new("LOGRM", &logrm.0))?; 148 + libos.init_at(3, LibosMemoryRegionInitArgument::new("RMARGS", rmargs))?; 149 + 150 + libos.into() 158 151 }, 159 152 })) 160 153 })
+44 -18
drivers/gpu/nova-core/gsp/fw.rs
··· 9 9 use core::ops::Range; 10 10 11 11 use kernel::{ 12 - dma::CoherentAllocation, 12 + dma::Coherent, 13 13 prelude::*, 14 14 ptr::{ 15 15 Alignable, 16 - Alignment, // 16 + Alignment, 17 + KnownSize, // 17 18 }, 18 19 sizes::{ 19 20 SZ_128K, ··· 649 648 /// The memory allocated for the arguments must remain until the GSP sends the 650 649 /// init_done RPC. 651 650 #[repr(transparent)] 652 - pub(crate) struct LibosMemoryRegionInitArgument(bindings::LibosMemoryRegionInitArgument); 651 + pub(crate) struct LibosMemoryRegionInitArgument { 652 + inner: bindings::LibosMemoryRegionInitArgument, 653 + } 653 654 654 655 // SAFETY: Padding is explicit and does not contain uninitialized data. 655 656 unsafe impl AsBytes for LibosMemoryRegionInitArgument {} ··· 661 658 unsafe impl FromBytes for LibosMemoryRegionInitArgument {} 662 659 663 660 impl LibosMemoryRegionInitArgument { 664 - pub(crate) fn new<A: AsBytes + FromBytes>( 661 + pub(crate) fn new<'a, A: AsBytes + FromBytes + KnownSize + ?Sized>( 665 662 name: &'static str, 666 - obj: &CoherentAllocation<A>, 667 - ) -> Self { 663 + obj: &'a Coherent<A>, 664 + ) -> impl Init<Self> + 'a { 668 665 /// Generates the `ID8` identifier required for some GSP objects. 669 666 fn id8(name: &str) -> u64 { 670 667 let mut bytes = [0u8; core::mem::size_of::<u64>()]; ··· 676 673 u64::from_ne_bytes(bytes) 677 674 } 678 675 679 - Self(bindings::LibosMemoryRegionInitArgument { 676 + #[allow(non_snake_case)] 677 + let init_inner = init!(bindings::LibosMemoryRegionInitArgument { 680 678 id8: id8(name), 681 679 pa: obj.dma_handle(), 682 680 size: num::usize_as_u64(obj.size()), ··· 687 683 loc: num::u32_into_u8::< 688 684 { bindings::LibosMemoryRegionLoc_LIBOS_MEMORY_REGION_LOC_SYSMEM }, 689 685 >(), 690 - ..Default::default() 686 + ..Zeroable::init_zeroed() 687 + }); 688 + 689 + init!(LibosMemoryRegionInitArgument { 690 + inner <- init_inner, 691 691 }) 692 692 } 693 693 } ··· 870 862 871 863 /// Arguments for GSP startup. 872 864 #[repr(transparent)] 873 - pub(crate) struct GspArgumentsCached(bindings::GSP_ARGUMENTS_CACHED); 865 + #[derive(Zeroable)] 866 + pub(crate) struct GspArgumentsCached { 867 + inner: bindings::GSP_ARGUMENTS_CACHED, 868 + } 874 869 875 870 impl GspArgumentsCached { 876 871 /// Creates the arguments for starting the GSP up using `cmdq` as its command queue. 877 - pub(crate) fn new(cmdq: &Cmdq) -> Self { 878 - Self(bindings::GSP_ARGUMENTS_CACHED { 879 - messageQueueInitArguments: MessageQueueInitArguments::new(cmdq).0, 872 + pub(crate) fn new(cmdq: &Cmdq) -> impl Init<Self> + '_ { 873 + #[allow(non_snake_case)] 874 + let init_inner = init!(bindings::GSP_ARGUMENTS_CACHED { 875 + messageQueueInitArguments <- MessageQueueInitArguments::new(cmdq), 880 876 bDmemStack: 1, 881 - ..Default::default() 877 + ..Zeroable::init_zeroed() 878 + }); 879 + 880 + init!(GspArgumentsCached { 881 + inner <- init_inner, 882 882 }) 883 883 } 884 884 } ··· 898 882 /// must all be a multiple of GSP_PAGE_SIZE in size, so add padding to force it 899 883 /// to that size. 900 884 #[repr(C)] 885 + #[derive(Zeroable)] 901 886 pub(crate) struct GspArgumentsPadded { 902 887 pub(crate) inner: GspArgumentsCached, 903 888 _padding: [u8; GSP_PAGE_SIZE - core::mem::size_of::<bindings::GSP_ARGUMENTS_CACHED>()], 889 + } 890 + 891 + impl GspArgumentsPadded { 892 + pub(crate) fn new(cmdq: &Cmdq) -> impl Init<Self> + '_ { 893 + init!(GspArgumentsPadded { 894 + inner <- GspArgumentsCached::new(cmdq), 895 + ..Zeroable::init_zeroed() 896 + }) 897 + } 904 898 } 905 899 906 900 // SAFETY: Padding is explicit and will not contain uninitialized data. ··· 921 895 unsafe impl FromBytes for GspArgumentsPadded {} 922 896 923 897 /// Init arguments for the message queue. 924 - #[repr(transparent)] 925 - struct MessageQueueInitArguments(bindings::MESSAGE_QUEUE_INIT_ARGUMENTS); 898 + type MessageQueueInitArguments = bindings::MESSAGE_QUEUE_INIT_ARGUMENTS; 926 899 927 900 impl MessageQueueInitArguments { 928 901 /// Creates a new init arguments structure for `cmdq`. 929 - fn new(cmdq: &Cmdq) -> Self { 930 - Self(bindings::MESSAGE_QUEUE_INIT_ARGUMENTS { 902 + #[allow(non_snake_case)] 903 + fn new(cmdq: &Cmdq) -> impl Init<Self> + '_ { 904 + init!(MessageQueueInitArguments { 931 905 sharedMemPhysAddr: cmdq.dma_handle(), 932 906 pageTableEntryCount: num::usize_into_u32::<{ Cmdq::NUM_PTES }>(), 933 907 cmdQueueOffset: num::usize_as_u64(Cmdq::CMDQ_OFFSET), 934 908 statQueueOffset: num::usize_as_u64(Cmdq::STATQ_OFFSET), 935 - ..Default::default() 909 + ..Zeroable::init_zeroed() 936 910 }) 937 911 } 938 912 }