xref: /linux/drivers/gpu/drm/nova/file.rs (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use crate::driver::{
4     DrmRegData,
5     NovaDevice,
6     NovaDriver, //
7 };
8 use crate::gem::NovaObject;
9 use kernel::{
10     alloc::flags::*,
11     drm::{
12         self,
13         gem::BaseObject,
14         Registered, //
15     },
16     num::casts::{
17         arch::IntoSafeCastArch,
18         IntoSafeCast, //
19     },
20     prelude::*,
21     transmute::AsBytes,
22     uaccess::UserSlice,
23     uapi,
24 };
25 
26 pub(crate) struct File;
27 
28 /// GPU information returned to userspace.
29 #[repr(transparent)]
30 struct GpuInfo(uapi::drm_nova_info_gpu);
31 
32 /// Copies `name` into the zero initialised, fixed size uAPI buffer `dst`, keeping it
33 /// NUL-terminated.
34 ///
35 /// Fails with [`ENAMETOOLONG`] if `name` does not fit in `dst` with room for the terminator.
36 fn copy_name(dst: &mut [u8], name: &str) -> Result {
37     let bytes = name.as_bytes();
38 
39     if bytes.len() >= dst.len() {
40         return Err(ENAMETOOLONG);
41     }
42     dst[..bytes.len()].copy_from_slice(bytes);
43 
44     Ok(())
45 }
46 
47 impl GpuInfo {
48     /// Collects the GPU information reported to userspace.
49     ///
50     /// This is fallible so that unexpected GSP behaviour, such as a malformed name string, is
51     /// reported to userspace instead of being silently replaced with a made up value. When adding
52     /// a field, take care that a value the GSP simply does not provide, for example because the
53     /// firmware predates it, does not cause a failure. Such fields must fall back to zero or
54     /// another documented default instead.
55     fn new(reg_data: &DrmRegData<'_>) -> Result<Self> {
56         let spec = reg_data.api.with(|api| api.get_ref().spec());
57         let gsp_static_info = reg_data.api.with(|api| api.get_ref().gsp_static_info());
58 
59         let mut info = uapi::drm_nova_info_gpu {
60             architecture: spec.chipset.arch().into(),
61             chipid: spec.chipset.into(),
62             vram_size: gsp_static_info.vram_size(),
63             vram_bar_size: reg_data.api.with(|api| api.bar1_size())?,
64             gpu_gid: gsp_static_info.gpu_gid,
65             ..pin_init::zeroed()
66         };
67 
68         copy_name(
69             &mut info.gpu_name,
70             gsp_static_info.gpu_name().map_err(|_| EINVAL)?,
71         )?;
72 
73         copy_name(
74             &mut info.gpu_short_name,
75             gsp_static_info.gpu_short_name().map_err(|_| EINVAL)?,
76         )?;
77 
78         Ok(Self(info))
79     }
80 }
81 
82 // SAFETY: `GpuInfo` has no interior mutability, and there are no uninitialized bytes.
83 unsafe impl AsBytes for GpuInfo {}
84 
85 fn write_info<T: AsBytes>(info: &mut uapi::drm_nova_info, value: T) -> Result {
86     // A NULL data pointer requests the size of the information structure.
87     if info.data == 0 {
88         info.size = size_of::<T>().try_into()?;
89         return Ok(());
90     }
91 
92     let mut writer = UserSlice::new(
93         UserPtr::from_addr(info.data.into_safe_cast_arch()),
94         info.size.into_safe_cast(),
95     )
96     .writer();
97 
98     info.size = writer.write_truncated(&value)?.try_into()?;
99 
100     Ok(())
101 }
102 
103 impl drm::file::DriverFile for File {
104     type Driver = NovaDriver;
105 
106     fn open(_dev: &NovaDevice) -> Result<Pin<KBox<Self>>> {
107         Ok(KBox::new(Self, GFP_KERNEL)?.into())
108     }
109 }
110 
111 impl File {
112     /// IOCTL: gem_create: Create a new DRM GEM object.
113     pub(crate) fn gem_create(
114         dev: &NovaDevice<Registered>,
115         _reg_data: &DrmRegData<'_>,
116         req: &mut uapi::drm_nova_gem_create,
117         file: &drm::File<File>,
118     ) -> Result<u32> {
119         let obj = NovaObject::new(dev, req.size.try_into()?)?;
120 
121         req.handle = obj.create_handle(file)?;
122 
123         Ok(0)
124     }
125 
126     /// IOCTL: gem_info: Query GEM metadata.
127     pub(crate) fn gem_info(
128         _dev: &NovaDevice<Registered>,
129         _reg_data: &DrmRegData<'_>,
130         req: &mut uapi::drm_nova_gem_info,
131         file: &drm::File<File>,
132     ) -> Result<u32> {
133         let bo = NovaObject::lookup_handle(file, req.handle)?;
134 
135         req.size = bo.size().try_into()?;
136 
137         Ok(0)
138     }
139 
140     /// IOCTL: info: Query device information.
141     pub(crate) fn info(
142         _dev: &NovaDevice<Registered>,
143         reg_data: &DrmRegData<'_>,
144         info: &mut uapi::drm_nova_info,
145         _file: &drm::File<File>,
146     ) -> Result<u32> {
147         match info.id {
148             uapi::DRM_NOVA_INFO_GPU => write_info(info, GpuInfo::new(reg_data)?)?,
149             _ => return Err(EINVAL),
150         }
151 
152         Ok(0)
153     }
154 }
155