xref: /linux/drivers/gpu/drm/nova/file.rs (revision e727d1c382a0cb2e10fd7aabef9217d1a37c80c7)
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             gpu_gid: gsp_static_info.gpu_gid,
64             ..pin_init::zeroed()
65         };
66 
67         copy_name(
68             &mut info.gpu_name,
69             gsp_static_info.gpu_name().map_err(|_| EINVAL)?,
70         )?;
71 
72         copy_name(
73             &mut info.gpu_short_name,
74             gsp_static_info.gpu_short_name().map_err(|_| EINVAL)?,
75         )?;
76 
77         Ok(Self(info))
78     }
79 }
80 
81 // SAFETY: `GpuInfo` has no interior mutability, and there are no uninitialized bytes.
82 unsafe impl AsBytes for GpuInfo {}
83 
84 fn write_info<T: AsBytes>(info: &mut uapi::drm_nova_info, value: T) -> Result {
85     // A NULL data pointer requests the size of the information structure.
86     if info.data == 0 {
87         info.size = size_of::<T>().try_into()?;
88         return Ok(());
89     }
90 
91     let mut writer = UserSlice::new(
92         UserPtr::from_addr(info.data.into_safe_cast_arch()),
93         info.size.into_safe_cast(),
94     )
95     .writer();
96 
97     info.size = writer.write_truncated(&value)?.try_into()?;
98 
99     Ok(())
100 }
101 
102 impl drm::file::DriverFile for File {
103     type Driver = NovaDriver;
104 
105     fn open(_dev: &NovaDevice) -> Result<Pin<KBox<Self>>> {
106         Ok(KBox::new(Self, GFP_KERNEL)?.into())
107     }
108 }
109 
110 impl File {
111     /// IOCTL: get_param: Query GPU / driver metadata.
112     pub(crate) fn get_param(
113         _dev: &NovaDevice<Registered>,
114         reg_data: &DrmRegData<'_>,
115         getparam: &mut uapi::drm_nova_getparam,
116         _file: &drm::File<File>,
117     ) -> Result<u32> {
118         let value = match getparam.param.try_into()? {
119             uapi::NOVA_GETPARAM_VRAM_BAR_SIZE => reg_data.api.with(|api| api.bar1_size())?,
120             _ => return Err(EINVAL),
121         };
122 
123         getparam.value = Into::<u64>::into(value);
124 
125         Ok(0)
126     }
127 
128     /// IOCTL: gem_create: Create a new DRM GEM object.
129     pub(crate) fn gem_create(
130         dev: &NovaDevice<Registered>,
131         _reg_data: &DrmRegData<'_>,
132         req: &mut uapi::drm_nova_gem_create,
133         file: &drm::File<File>,
134     ) -> Result<u32> {
135         let obj = NovaObject::new(dev, req.size.try_into()?)?;
136 
137         req.handle = obj.create_handle(file)?;
138 
139         Ok(0)
140     }
141 
142     /// IOCTL: gem_info: Query GEM metadata.
143     pub(crate) fn gem_info(
144         _dev: &NovaDevice<Registered>,
145         _reg_data: &DrmRegData<'_>,
146         req: &mut uapi::drm_nova_gem_info,
147         file: &drm::File<File>,
148     ) -> Result<u32> {
149         let bo = NovaObject::lookup_handle(file, req.handle)?;
150 
151         req.size = bo.size().try_into()?;
152 
153         Ok(0)
154     }
155 
156     /// IOCTL: info: Query device information.
157     pub(crate) fn info(
158         _dev: &NovaDevice<Registered>,
159         reg_data: &DrmRegData<'_>,
160         info: &mut uapi::drm_nova_info,
161         _file: &drm::File<File>,
162     ) -> Result<u32> {
163         match info.id {
164             uapi::DRM_NOVA_INFO_GPU => write_info(info, GpuInfo::new(reg_data)?)?,
165             _ => return Err(EINVAL),
166         }
167 
168         Ok(0)
169     }
170 }
171