xref: /linux/drivers/gpu/nova-core/api.rs (revision e727d1c382a0cb2e10fd7aabef9217d1a37c80c7)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Nova-core auxbus data. Contains all the methods used by the auxbus drivers
4 //! to interact with nova-core.
5 
6 use core::pin::Pin;
7 
8 use kernel::{
9     auxiliary,
10     device::Bound,
11     pci,
12     prelude::*,
13     types::ForLt, //
14 };
15 
16 pub use crate::gpu::Spec;
17 
18 use crate::gpu::Gpu;
19 use crate::gsp::commands::GetGspStaticInfoReply;
20 
21 /// API handle for the auxiliary bus child drivers to interact with nova-core.
22 pub struct NovaCoreApi<'bound> {
23     pub(crate) gpu: Pin<&'bound Gpu<'bound>>,
24     pub(crate) pdev: &'bound pci::Device<Bound>,
25 }
26 
27 impl NovaCoreApi<'_> {
28     /// Obtain a [`NovaCoreApi`] handle from an auxiliary device registered
29     /// by nova-core.
30     pub fn of(adev: &auxiliary::Device<Bound>) -> Result<NovaCoreApiHandle<'_>> {
31         NovaCoreApiHandle::of(adev)
32     }
33 
34     /// Returns the GPU [`GetGspStaticInfoReply`].
35     pub fn gsp_static_info(&self) -> &GetGspStaticInfoReply {
36         &self.gpu.gsp_static_info
37     }
38 
39     /// Returns the GPU [`Spec`].
40     pub fn spec(&self) -> &Spec {
41         &self.gpu.spec
42     }
43 
44     /// Returns the size of the PCIe BAR used for accessing VRAM, typically
45     /// BAR1.
46     pub fn bar1_size(&self) -> Result<u64> {
47         self.pdev.resource_len(1)
48     }
49 }
50 
51 /// Expose a handle to nova-core API
52 pub struct NovaCoreApiHandle<'a> {
53     adev: &'a auxiliary::Device<Bound>,
54 }
55 
56 impl<'a> NovaCoreApiHandle<'a> {
57     fn of(adev: &'a auxiliary::Device<Bound>) -> Result<Self> {
58         adev.registration_data_with::<ForLt!(NovaCoreApi<'_>), ()>(|_| ())?;
59         Ok(Self { adev })
60     }
61 
62     /// Access the [`NovaCoreApi`] through a closure.
63     pub fn with<R>(&self, f: impl for<'b> FnOnce(Pin<&'a NovaCoreApi<'b>>) -> R) -> R {
64         self.adev
65             .registration_data_with::<ForLt!(NovaCoreApi<'_>), R>(f)
66             .expect("TypeId was validated in NovaCoreApiHandle::of()")
67     }
68 }
69