Skip to main content

ttkmd_if/
ioctl.rs

1// SPDX-FileCopyrightText: © 2023 Tenstorrent Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4const TENSTORRENT_IOCTL_MAGIC: usize = 0xFA;
5
6use nix::request_code_none;
7
8#[derive(Debug)]
9#[repr(C)]
10pub struct GetDeviceInfoIn {
11    pub output_size_bytes: u32,
12}
13
14impl Default for GetDeviceInfoIn {
15    fn default() -> Self {
16        Self {
17            output_size_bytes: std::mem::size_of::<GetDeviceInfoOut>() as u32,
18        }
19    }
20}
21
22#[derive(Default, Debug)]
23#[repr(C)]
24pub struct GetDeviceInfoOut {
25    pub output_size_bytes: u32,
26    pub vendor_id: u16,
27    pub device_id: u16,
28    pub subsystem_vendor_id: u16,
29    pub subsystem_id: u16,
30    pub bus_dev_fn: u16,            // [0:2] function, [3:7] device, [8:15] bus
31    pub max_dma_buf_size_log2: u16, // Since 1.0
32    pub pci_domain: u16,            // Since 1.23
33}
34
35#[derive(Default, Debug)]
36#[repr(C)]
37pub struct GetDeviceInfo {
38    pub input: GetDeviceInfoIn,
39    pub output: GetDeviceInfoOut,
40}
41
42nix::ioctl_readwrite_bad!(
43    get_device_info,
44    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 0),
45    GetDeviceInfo
46);
47
48#[derive(Debug, Default, Clone, Copy)]
49#[repr(C)]
50pub struct Mapping {
51    pub mapping_id: u32,
52    _reserved: u32,
53    pub mapping_base: u64,
54    pub mapping_size: u64,
55}
56
57#[derive(Debug, Default)]
58#[repr(C)]
59pub struct QueryMappingsIn {
60    pub output_mapping_count: u32,
61    _reserved: u32,
62}
63
64#[derive(Debug)]
65#[repr(C)]
66pub struct QueryMappingsOut<const N: usize> {
67    pub mappings: [Mapping; N],
68}
69
70impl<const N: usize> Default for QueryMappingsOut<N> {
71    fn default() -> Self {
72        Self {
73            mappings: [Mapping::default(); N],
74        }
75    }
76}
77
78#[derive(Debug)]
79#[repr(C)]
80pub struct QueryMappings<const N: usize> {
81    pub input: QueryMappingsIn,
82    pub output: QueryMappingsOut<N>,
83}
84
85impl<const N: usize> Default for QueryMappings<N> {
86    fn default() -> Self {
87        Self {
88            input: QueryMappingsIn {
89                output_mapping_count: N as u32,
90                ..Default::default()
91            },
92            output: QueryMappingsOut::<N>::default(),
93        }
94    }
95}
96
97/// # Safety
98///
99/// You must make sure that data is a valid pointer and that the file descriptor is valid
100pub unsafe fn query_mappings<const N: usize>(
101    fd: nix::libc::c_int,
102    data: *mut QueryMappings<N>,
103) -> nix::Result<nix::libc::c_int> {
104    nix::convert_ioctl_res!(nix::libc::ioctl(
105        fd,
106        request_code_none!(TENSTORRENT_IOCTL_MAGIC, 2) as nix::sys::ioctl::ioctl_num_type,
107        data
108    ))
109}
110
111#[derive(Default)]
112#[repr(C)]
113pub struct AllocateDmaBufferIn {
114    pub requested_size: u32,
115    pub buf_index: u8, // [0,TENSTORRENT_MAX_DMA_BUFS)
116    _reserved0: [u8; 3],
117    _reserved1: [u64; 2],
118}
119
120#[derive(Default)]
121#[repr(C)]
122pub struct AllocateDmaBufferOut {
123    pub physical_address: u64,
124    pub mapping_offset: u64,
125    pub size: u32,
126    _reserved0: u32,
127    _reserved1: [u64; 2],
128}
129
130#[derive(Default)]
131#[repr(C)]
132pub struct AllocateDmaBuffer {
133    pub input: AllocateDmaBufferIn,
134    pub output: AllocateDmaBufferOut,
135}
136
137nix::ioctl_readwrite_bad!(
138    allocate_dma_buffer,
139    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 3),
140    AllocateDmaBuffer
141);
142
143#[derive(Default)]
144#[repr(C)]
145pub struct FreeDmaBufferIn;
146#[derive(Default)]
147#[repr(C)]
148pub struct FreeDmaBufferOut;
149
150#[derive(Default)]
151#[repr(C)]
152pub struct FreeDmaBuffer {
153    pub input: FreeDmaBufferIn,
154    pub output: FreeDmaBufferOut,
155}
156
157nix::ioctl_readwrite_bad!(
158    free_dma_buffer,
159    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 4),
160    FreeDmaBuffer
161);
162
163#[repr(C)]
164pub struct GetDriverInfoIn {
165    pub output_size_bytes: u32,
166}
167
168impl Default for GetDriverInfoIn {
169    fn default() -> Self {
170        Self {
171            output_size_bytes: std::mem::size_of::<GetDriverInfoOut>() as u32,
172        }
173    }
174}
175
176#[derive(Default)]
177#[repr(C)]
178pub struct GetDriverInfoOut {
179    pub output_size_bytes: u32,
180    pub driver_version: u32,
181}
182
183#[derive(Default)]
184#[repr(C)]
185pub struct GetDriverInfo {
186    pub input: GetDriverInfoIn,
187    pub output: GetDriverInfoOut,
188}
189
190nix::ioctl_readwrite_bad!(
191    get_driver_info,
192    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 5),
193    GetDriverInfo
194);
195
196pub const RESET_DEVICE_RESTORE_STATE: u32 = 0;
197pub const RESET_DEVICE_RESET_PCIE_LINK: u32 = 1;
198pub const RESET_DEVICE_RESET_CONFIG_WRITE: u32 = 2;
199
200#[repr(C)]
201pub struct ResetDeviceIn {
202    pub output_size_bytes: u32,
203    pub flags: u32,
204}
205
206impl Default for ResetDeviceIn {
207    fn default() -> Self {
208        Self {
209            output_size_bytes: std::mem::size_of::<Self>() as u32,
210            flags: 0,
211        }
212    }
213}
214
215#[derive(Default)]
216#[repr(C)]
217pub struct ResetDeviceOut {
218    pub output_size_bytes: u32,
219    pub result: u32,
220}
221
222#[derive(Default)]
223#[repr(C)]
224pub struct ResetDevice {
225    pub input: ResetDeviceIn,
226    pub output: ResetDeviceOut,
227}
228
229nix::ioctl_readwrite_bad!(
230    reset_device,
231    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 6),
232    ResetDevice
233);
234
235#[derive(Default)]
236#[repr(C)]
237pub struct AllocateTlbIn {
238    pub size: u64,
239    pub reserved: u64,
240}
241
242#[derive(Default)]
243#[repr(C)]
244pub struct AllocateTlbOut {
245    pub id: u32,
246    pub reserved0: u32,
247    pub mmap_offset_uc: u64,
248    pub mmap_offset_wc: u64,
249    pub reserved1: u64,
250}
251
252#[derive(Default)]
253#[repr(C)]
254pub struct AllocateTlb {
255    pub input: AllocateTlbIn,
256    pub output: AllocateTlbOut,
257}
258
259nix::ioctl_readwrite_bad!(
260    allocate_tlb,
261    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 11),
262    AllocateTlb
263);
264
265#[derive(Default)]
266#[repr(C)]
267pub struct FreeTlbIn {
268    pub id: u32,
269}
270
271#[derive(Default)]
272#[repr(C)]
273pub struct FreeTlbOut {}
274
275#[derive(Default)]
276#[repr(C)]
277pub struct FreeTlb {
278    pub input: FreeTlbIn,
279    pub output: FreeTlbOut,
280}
281
282nix::ioctl_readwrite_bad!(
283    free_tlb,
284    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 12),
285    FreeTlb
286);
287
288#[derive(Default)]
289#[repr(C)]
290pub struct NocTlbConfig {
291    pub addr: u64,
292    pub x_end: u16,
293    pub y_end: u16,
294    pub x_start: u16,
295    pub y_start: u16,
296    pub noc: u8,
297    pub mcast: u8,
298    pub ordering: u8,
299    pub linked: u8,
300    pub static_vc: u8,
301    pub reserved0: [u8; 3],
302    pub reserved1: [u32; 2],
303}
304
305#[derive(Default)]
306#[repr(C)]
307pub struct ConfigureTlbIn {
308    pub id: u32,
309    pub config: NocTlbConfig,
310}
311
312#[derive(Default)]
313#[repr(C)]
314pub struct ConfigureTlbOut {
315    pub reserved: u64,
316}
317
318#[derive(Default)]
319#[repr(C)]
320pub struct ConfigureTlb {
321    pub input: ConfigureTlbIn,
322    pub output: ConfigureTlbOut,
323}
324
325nix::ioctl_readwrite_bad!(
326    configure_tlb,
327    request_code_none!(TENSTORRENT_IOCTL_MAGIC, 13),
328    ConfigureTlb
329);