xref: /linux/drivers/gpu/drm/tyr/gpu.rs (revision cb8fef4b07f71da01843457b66e579ad248a625e)
1 // SPDX-License-Identifier: GPL-2.0 or MIT
2 
3 use core::ops::{
4     Deref,
5     DerefMut, //
6 };
7 use kernel::{
8     device::{
9         Bound,
10         Device, //
11     },
12     io::{
13         poll,
14         register::Array,
15         Io, //
16     },
17     prelude::*,
18     time::Delta,
19     transmute::AsBytes,
20     uapi, //
21 };
22 
23 use crate::{
24     driver::IoMem,
25     regs::{
26         gpu_control::*,
27         join_u64, //
28     }, //
29 };
30 
31 /// Struct containing information that can be queried by userspace. This is read from
32 /// the GPU's registers.
33 ///
34 /// # Invariants
35 ///
36 /// - The layout of this struct is identical to the C `struct drm_panthor_gpu_info`.
37 #[repr(transparent)]
38 #[derive(Clone, Copy)]
39 pub(crate) struct GpuInfo(pub(crate) uapi::drm_panthor_gpu_info);
40 
41 impl GpuInfo {
42     pub(crate) fn new(io: &IoMem<'_>) -> Self {
43         Self(uapi::drm_panthor_gpu_info {
44             gpu_id: io.read(GPU_ID).into_raw(),
45             gpu_rev: io.read(REVIDR).into_raw(),
46             csf_id: io.read(CSF_ID).into_raw(),
47             l2_features: io.read(L2_FEATURES).into_raw(),
48             tiler_features: io.read(TILER_FEATURES).into_raw(),
49             mem_features: io.read(MEM_FEATURES).into_raw(),
50             mmu_features: io.read(MMU_FEATURES).into_raw(),
51             thread_features: io.read(THREAD_FEATURES).into_raw(),
52             max_threads: io.read(THREAD_MAX_THREADS).into_raw(),
53             thread_max_workgroup_size: io.read(THREAD_MAX_WORKGROUP_SIZE).into_raw(),
54             thread_max_barrier_size: io.read(THREAD_MAX_BARRIER_SIZE).into_raw(),
55             coherency_features: io.read(COHERENCY_FEATURES).into_raw(),
56             texture_features: [
57                 io.read(TEXTURE_FEATURES::at(0)).supported_formats().get(),
58                 io.read(TEXTURE_FEATURES::at(1)).supported_formats().get(),
59                 io.read(TEXTURE_FEATURES::at(2)).supported_formats().get(),
60                 io.read(TEXTURE_FEATURES::at(3)).supported_formats().get(),
61             ],
62             as_present: io.read(AS_PRESENT).into_raw(),
63             selected_coherency: uapi::drm_panthor_gpu_coherency_DRM_PANTHOR_GPU_COHERENCY_NONE,
64             shader_present: join_u64(
65                 io.read(SHADER_PRESENT_LO).into_raw(),
66                 io.read(SHADER_PRESENT_HI).into_raw(),
67             ),
68             l2_present: join_u64(
69                 io.read(L2_PRESENT_LO).into_raw(),
70                 io.read(L2_PRESENT_HI).into_raw(),
71             ),
72             tiler_present: join_u64(
73                 io.read(TILER_PRESENT_LO).into_raw(),
74                 io.read(TILER_PRESENT_HI).into_raw(),
75             ),
76             core_features: io.read(CORE_FEATURES).into_raw(),
77             // Zero unsupported and newly added UAPI fields.
78             ..pin_init::zeroed()
79         })
80     }
81 
82     pub(crate) fn log(&self, dev: &Device<Bound>) {
83         let gpu_id = GPU_ID::from_raw(self.gpu_id);
84 
85         let model_name = if let Some(model) = GPU_MODELS.iter().find(|&f| {
86             f.arch_major == gpu_id.arch_major().get() && f.prod_major == gpu_id.prod_major().get()
87         }) {
88             model.name
89         } else {
90             "unknown"
91         };
92 
93         dev_info!(
94             dev,
95             "mali-{} GPU_ID 0x{:x} major 0x{:x} minor 0x{:x} status 0x{:x}",
96             model_name,
97             gpu_id.into_raw(),
98             gpu_id.ver_major().get(),
99             gpu_id.ver_minor().get(),
100             gpu_id.ver_status().get()
101         );
102 
103         dev_info!(
104             dev,
105             "Features: L2:{:#x} Tiler:{:#x} Mem:{:#x} MMU:{:#x} AS:{:#x}",
106             self.l2_features,
107             self.tiler_features,
108             self.mem_features,
109             self.mmu_features,
110             self.as_present,
111         );
112 
113         dev_info!(
114             dev,
115             "shader_present=0x{:016x} l2_present=0x{:016x} tiler_present=0x{:016x}",
116             self.shader_present,
117             self.l2_present,
118             self.tiler_present,
119         );
120     }
121 }
122 
123 impl Deref for GpuInfo {
124     type Target = uapi::drm_panthor_gpu_info;
125 
126     fn deref(&self) -> &Self::Target {
127         &self.0
128     }
129 }
130 
131 impl DerefMut for GpuInfo {
132     fn deref_mut(&mut self) -> &mut Self::Target {
133         &mut self.0
134     }
135 }
136 
137 // SAFETY: `GpuInfo`'s invariant guarantees that it is the same type that is
138 // already exposed to userspace by the C driver. This implies that it fulfills
139 // the requirements for `AsBytes`.
140 //
141 // This means:
142 //
143 // - No implicit padding,
144 // - No kernel pointers,
145 // - No interior mutability.
146 unsafe impl AsBytes for GpuInfo {}
147 
148 struct GpuModels {
149     name: &'static str,
150     arch_major: u32,
151     prod_major: u32,
152 }
153 
154 const GPU_MODELS: [GpuModels; 1] = [GpuModels {
155     name: "g610",
156     arch_major: 10,
157     prod_major: 7,
158 }];
159 
160 /// Powers on the l2 block.
161 pub(crate) fn l2_power_on(dev: &Device, io: &IoMem<'_>) -> Result {
162     io.write_reg(L2_PWRON_LO::zeroed().with_const_request::<1>());
163 
164     poll::read_poll_timeout(
165         || Ok(io.read(L2_READY_LO)),
166         |status| status.ready() == 1,
167         Delta::from_millis(1),
168         Delta::from_millis(100),
169     )
170     .inspect_err(|_| dev_err!(dev, "Failed to power on the GPU."))?;
171 
172     Ok(())
173 }
174