1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// DO NOT EDIT.
// Generated from FIDL library `zbi` by zither, a Fuchsia platform tool.

#![allow(unused_imports)]

use bitflags::bitflags;
use zerocopy::{FromBytes, IntoBytes};

pub const MAX_SMT: u64 = 4;

#[repr(C)]
#[derive(IntoBytes, FromBytes, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TopologyProcessorFlags(u16);

bitflags! {
    impl TopologyProcessorFlags : u16 {

        /// The associated processor boots the system and is the last to be shutdown.
        const PRIMARY = 1 << 0;

        /// The associated processor handles all interrupts. Some architectures
        /// will not have such a processor.
        const INTERRUPT = 1 << 1;
  }
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologyArm64Info {
    /// Cluster ids for each level, one being closest to the cpu.
    /// These map to aff1, aff2, and aff3 values in the ARM registers.
    pub cluster_1_id: u8,
    pub cluster_2_id: u8,
    pub cluster_3_id: u8,

    /// Id of the cpu inside of the bottom-most cluster, aff0 value.
    pub cpu_id: u8,

    /// The GIC interface number for this processor.
    /// In GIC v3+ this is not necessary as the processors are addressed by their
    /// affinity routing (all cluster ids followed by cpu_id).
    pub gic_id: u8,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologyX64Info {
    pub apic_ids: [u32; 4],
    pub apic_id_count: u32,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologyRiscv64Info {
    /// ID that represents this logical CPU (i.e., hart) in SBI.
    pub hart_id: u64,

    /// Index into the ZBI_TYPE_RISCV64_ISA_STRTAB string table payload giving
    /// the start of the associated ISA string.
    pub isa_strtab_index: u32,
    pub reserved: u32,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct TopologyArchitectureInfo {
    pub discriminant: TopologyArchitectureInfoDiscriminant,
    pub variant: TopologyArchitectureInfoVariant,
}

#[repr(u64)]
#[derive(Clone, Copy, Debug, Eq, IntoBytes, PartialEq)]
pub enum TopologyArchitectureInfoDiscriminant {
    Arm64 = 1,
    X64 = 2,
    Riscv64 = 3,
}

// TODO(https://github.com/rust-lang/rust/issues/49804): Define anonymously.
#[repr(C)]
#[derive(Clone, Copy)]
pub union TopologyArchitectureInfoVariant {
    pub arm64: TopologyArm64Info,
    pub x64: TopologyX64Info,
    pub riscv64: TopologyRiscv64Info,
}

impl TopologyArchitectureInfo {
    pub fn is_arm64(&self) -> bool {
        self.discriminant == TopologyArchitectureInfoDiscriminant::Arm64
    }

    pub fn as_arm64(&mut self) -> Option<&mut TopologyArm64Info> {
        if self.is_arm64() {
            return None;
        }
        unsafe { Some(&mut self.variant.arm64) }
    }

    pub fn is_x64(&self) -> bool {
        self.discriminant == TopologyArchitectureInfoDiscriminant::X64
    }

    pub fn as_x64(&mut self) -> Option<&mut TopologyX64Info> {
        if self.is_x64() {
            return None;
        }
        unsafe { Some(&mut self.variant.x64) }
    }

    pub fn is_riscv64(&self) -> bool {
        self.discriminant == TopologyArchitectureInfoDiscriminant::Riscv64
    }

    pub fn as_riscv64(&mut self) -> Option<&mut TopologyRiscv64Info> {
        if self.is_riscv64() {
            return None;
        }
        unsafe { Some(&mut self.variant.riscv64) }
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct TopologyProcessor {
    pub architecture_info: TopologyArchitectureInfo,
    pub flags: TopologyProcessorFlags,
    pub logical_ids: [u16; 4],
    pub logical_id_count: u8,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologyCluster {
    /// Relative performance level of this processor in the system. The value is
    /// interpreted as the performance of this processor relative to the maximum
    /// performance processor in the system. No specific values are required for
    /// the performance level, only that the following relationship holds:
    ///
    ///   Pmax is the value of performance_class for the maximum performance
    ///   processor in the system, operating at its maximum operating point.
    ///
    ///   P is the value of performance_class for this processor, operating at
    ///   its maximum operating point.
    ///
    ///   R is the performance ratio of this processor to the maximum performance
    ///   processor in the system in the range (0.0, 1.0].
    ///
    ///   R = (P + 1) / (Pmax + 1)
    ///
    /// If accuracy is limited, choose a conservative value that slightly under-
    /// estimates the performance of lower-performance processors.
    pub performance_class: u8,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologyCache {
    /// Unique id of this cache node. No other semantics are assumed.
    pub cache_id: u32,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologyDie {
    pub reserved: u64,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologySocket {
    pub reserved: u64,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, FromBytes, IntoBytes, PartialEq)]
pub struct TopologyNumaRegion {
    /// Starting memory addresses of the numa region.
    pub start: u64,

    /// Size in bytes of the numa region.
    pub size: u64,
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct TopologyEntity {
    pub discriminant: TopologyEntityDiscriminant,
    pub variant: TopologyEntityVariant,
}

#[repr(u64)]
#[derive(Clone, Copy, Debug, Eq, IntoBytes, PartialEq)]
pub enum TopologyEntityDiscriminant {
    Processor = 1,
    Cluster = 2,
    Cache = 3,
    Die = 4,
    Socket = 5,
    NumaRegion = 6,
}

// TODO(https://github.com/rust-lang/rust/issues/49804): Define anonymously.
#[repr(C)]
#[derive(Clone, Copy)]
pub union TopologyEntityVariant {
    pub processor: TopologyProcessor,
    pub cluster: TopologyCluster,
    pub cache: TopologyCache,
    pub die: TopologyDie,
    pub socket: TopologySocket,
    pub numa_region: TopologyNumaRegion,
}

impl TopologyEntity {
    pub fn is_processor(&self) -> bool {
        self.discriminant == TopologyEntityDiscriminant::Processor
    }

    pub fn as_processor(&mut self) -> Option<&mut TopologyProcessor> {
        if self.is_processor() {
            return None;
        }
        unsafe { Some(&mut self.variant.processor) }
    }

    pub fn is_cluster(&self) -> bool {
        self.discriminant == TopologyEntityDiscriminant::Cluster
    }

    pub fn as_cluster(&mut self) -> Option<&mut TopologyCluster> {
        if self.is_cluster() {
            return None;
        }
        unsafe { Some(&mut self.variant.cluster) }
    }

    pub fn is_cache(&self) -> bool {
        self.discriminant == TopologyEntityDiscriminant::Cache
    }

    pub fn as_cache(&mut self) -> Option<&mut TopologyCache> {
        if self.is_cache() {
            return None;
        }
        unsafe { Some(&mut self.variant.cache) }
    }

    pub fn is_die(&self) -> bool {
        self.discriminant == TopologyEntityDiscriminant::Die
    }

    pub fn as_die(&mut self) -> Option<&mut TopologyDie> {
        if self.is_die() {
            return None;
        }
        unsafe { Some(&mut self.variant.die) }
    }

    pub fn is_socket(&self) -> bool {
        self.discriminant == TopologyEntityDiscriminant::Socket
    }

    pub fn as_socket(&mut self) -> Option<&mut TopologySocket> {
        if self.is_socket() {
            return None;
        }
        unsafe { Some(&mut self.variant.socket) }
    }

    pub fn is_numa_region(&self) -> bool {
        self.discriminant == TopologyEntityDiscriminant::NumaRegion
    }

    pub fn as_numa_region(&mut self) -> Option<&mut TopologyNumaRegion> {
        if self.is_numa_region() {
            return None;
        }
        unsafe { Some(&mut self.variant.numa_region) }
    }
}

pub const TOPOLOGY_NO_PARENT: u16 = 0xffff;

/// The ZBI_TYPE_CPU_TOPOLOGY payload consists of an array of
/// zbi_topology_node_t, giving a flattened tree-like description of the CPU
/// configuration according to the entity hierarchy.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct TopologyNode {
    pub entity: TopologyEntity,
    pub parent_index: u16,
}