Repository navigation
Expand file tree
/
Copy pathneuron_sysfs_platform.c
More file actions
308 lines (263 loc) · 9.13 KB
/
Copy pathneuron_sysfs_platform.c
File metadata and controls
308 lines (263 loc) · 9.13 KB
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
298
299
300
301
302
303
304
305
306
307
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright 2026, Amazon.com, Inc. or its affiliates. All Rights Reserved
*/
#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/kobject.h>
#include <linux/pci.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/version.h>
#include "neuron_arch.h"
#include "neuron_device.h"
#include "neuron_dhal.h"
#include "neuron_pci.h"
#include "neuron_platform.h"
static const char *const family_names[] = {
[NEURON_PLATFORM_FAMILY_NONE] = "none",
[NEURON_PLATFORM_FAMILY_INF2] = "inf2",
[NEURON_PLATFORM_FAMILY_TRN1] = "trn1",
[NEURON_PLATFORM_FAMILY_TRN1N] = "trn1n",
[NEURON_PLATFORM_FAMILY_TRN2] = "trn2",
[NEURON_PLATFORM_FAMILY_TRN2_ULTRASERVER] = "trn2_ultraserver",
[NEURON_PLATFORM_FAMILY_TRN3_GEN1_ULTRASERVER] = "trn3_gen1_ultraserver",
[NEURON_PLATFORM_FAMILY_TRN3_GEN2_ULTRASERVER] = "trn3_gen2_ultraserver",
};
static_assert(ARRAY_SIZE(family_names) == NEURON_PLATFORM_FAMILY_TRN3_GEN2_ULTRASERVER + 1,
"family_names out of sync with enum neuron_platform_family");
static const char *const intra_topology_names[] = {
[NEURON_INTRA_RACK_TOPOLOGY_NONE] = "none",
[NEURON_INTRA_RACK_TOPOLOGY_RING] = "ring",
[NEURON_INTRA_RACK_TOPOLOGY_TORUS] = "torus",
[NEURON_INTRA_RACK_TOPOLOGY_SWITCH] = "switch",
};
static_assert(ARRAY_SIZE(intra_topology_names) == NEURON_INTRA_RACK_TOPOLOGY_SWITCH + 1,
"intra_topology_names out of sync with enum neuron_intra_rack_topology");
static const char *const inter_topology_names[] = {
[NEURON_INTER_RACK_TOPOLOGY_NONE] = "none",
[NEURON_INTER_RACK_TOPOLOGY_P2P] = "p2p",
};
static_assert(ARRAY_SIZE(inter_topology_names) == NEURON_INTER_RACK_TOPOLOGY_P2P + 1,
"inter_topology_names out of sync with enum neuron_inter_rack_topology");
static const char *const switch_type_names[] = {
[NEURON_SWITCH_TYPE_NONE] = "none",
[NEURON_SWITCH_TYPE_V1] = "v1",
[NEURON_SWITCH_TYPE_V2] = "v2",
};
static_assert(ARRAY_SIZE(switch_type_names) == NEURON_SWITCH_TYPE_V2 + 1,
"switch_type_names out of sync with enum neuron_switch_type");
static const char *lookup_name(const char *const *tbl, size_t tbl_sz, unsigned int idx)
{
if (idx >= tbl_sz || !tbl[idx])
return "none";
return tbl[idx];
}
#if (!defined(RHEL_RELEASE_CODE) && (LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0))) || (defined(RHEL_RELEASE_CODE) && (RHEL_RELEASE_CODE >= RHEL_RELEASE_VERSION(9, 5)))
#define NPLATFORM_CLASS_SHOW_ARGS const struct class *cls, const struct class_attribute *attr
#else
#define NPLATFORM_CLASS_SHOW_ARGS struct class *cls, struct class_attribute *attr
#endif
static ssize_t soc_version_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
if (!ndhal)
return 0;
return dhal_sysfs_emit(buf, "%d\n", ndhal->ndhal_arch.arch);
}
static ssize_t sub_version_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
unsigned int sub_version = NEURON_SOC_SUB_VERSION_A0;
if (!ndhal)
return 0;
if (ndhal->ndhal_arch.arch == NEURON_ARCH_V4 && narch_get_revision() >= 0x02)
sub_version = NEURON_SOC_SUB_VERSION_B1;
return dhal_sysfs_emit(buf, "%u\n", sub_version);
}
static CLASS_ATTR_RO(sub_version);
static ssize_t hbm_capacity_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
if (!ndhal)
return 0;
return dhal_sysfs_emit(buf, "%llu\n",
ndhal->ndhal_address_map.hbm_active_size >> 30);
}
static CLASS_ATTR_RO(hbm_capacity);
/*
* neuron_soc/version and efa/version share the same attribute name in
* different groups; declared with __ATTR to keep unique C symbols.
*/
static struct class_attribute class_attr_soc_version = __ATTR(version, S_IRUGO, soc_version_show, NULL);
static struct attribute *neuron_soc_attrs[] = {
&class_attr_soc_version.attr,
&class_attr_sub_version.attr,
&class_attr_hbm_capacity.attr,
NULL,
};
const struct attribute_group neuron_soc_group = {
.name = "neuron_soc",
.attrs = neuron_soc_attrs,
};
static ssize_t efa_version_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
return dhal_sysfs_emit(buf, "%d\n", neuron_pci_get_efa_version());
}
static struct class_attribute class_attr_efa_version = __ATTR(version, S_IRUGO, efa_version_show, NULL);
static struct attribute *efa_attrs[] = {
&class_attr_efa_version.attr,
NULL,
};
const struct attribute_group efa_group = {
.name = "efa",
.attrs = efa_attrs,
};
static ssize_t family_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
enum neuron_platform_family fam;
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
fam = ndhal->ndhal_arch.platform->family;
return dhal_sysfs_emit(buf, "%s\n",
lookup_name(family_names, ARRAY_SIZE(family_names), fam));
}
static CLASS_ATTR_RO(family);
static ssize_t num_chips_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
return dhal_sysfs_emit(buf, "%u\n", ndhal->ndhal_arch.platform->device_count);
}
static CLASS_ATTR_RO(num_chips);
static ssize_t num_sleds_per_server_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
return dhal_sysfs_emit(buf, "%u\n",
ndhal->ndhal_arch.platform->num_sleds_per_server);
}
static CLASS_ATTR_RO(num_sleds_per_server);
static ssize_t num_chips_per_sled_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
return dhal_sysfs_emit(buf, "%u\n",
ndhal->ndhal_arch.platform->num_chips_per_sled);
}
static CLASS_ATTR_RO(num_chips_per_sled);
static ssize_t num_cores_per_chip_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
if (!ndhal)
return 0;
return dhal_sysfs_emit(buf, "%d\n", ndhal->ndhal_address_map.nc_per_device);
}
static CLASS_ATTR_RO(num_cores_per_chip);
static ssize_t supported_lnc_degrees_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
unsigned long mask;
int bit;
ssize_t n = 0;
if (!ndhal)
return 0;
mask = ndhal->ndhal_address_map.supported_lnc_degrees;
for_each_set_bit(bit, &mask, BITS_PER_LONG)
n += scnprintf(buf + n, PAGE_SIZE - n, "%s%d", n ? " " : "", bit);
n += scnprintf(buf + n, PAGE_SIZE - n, "\n");
return n;
}
static CLASS_ATTR_RO(supported_lnc_degrees);
static ssize_t intra_rack_topology_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
enum neuron_intra_rack_topology t;
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
t = ndhal->ndhal_arch.platform->interconnect.intra_rack.topology;
return dhal_sysfs_emit(buf, "%s\n",
lookup_name(intra_topology_names,
ARRAY_SIZE(intra_topology_names), t));
}
static CLASS_ATTR_RO(intra_rack_topology);
static ssize_t intra_rack_switch_type_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
enum neuron_switch_type s;
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
s = ndhal->ndhal_arch.platform->interconnect.intra_rack.switch_type;
return dhal_sysfs_emit(buf, "%s\n",
lookup_name(switch_type_names,
ARRAY_SIZE(switch_type_names), s));
}
static CLASS_ATTR_RO(intra_rack_switch_type);
static ssize_t inter_rack_topology_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
enum neuron_inter_rack_topology t;
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
t = ndhal->ndhal_arch.platform->interconnect.inter_rack.topology;
return dhal_sysfs_emit(buf, "%s\n",
lookup_name(inter_topology_names,
ARRAY_SIZE(inter_topology_names), t));
}
static CLASS_ATTR_RO(inter_rack_topology);
static ssize_t inter_rack_switch_type_show(NPLATFORM_CLASS_SHOW_ARGS, char *buf)
{
enum neuron_switch_type s;
if (!ndhal || !ndhal->ndhal_arch.platform)
return 0;
s = ndhal->ndhal_arch.platform->interconnect.inter_rack.switch_type;
return dhal_sysfs_emit(buf, "%s\n",
lookup_name(switch_type_names,
ARRAY_SIZE(switch_type_names), s));
}
static CLASS_ATTR_RO(inter_rack_switch_type);
static struct attribute *platform_attrs[] = {
&class_attr_family.attr,
&class_attr_num_chips.attr,
&class_attr_num_sleds_per_server.attr,
&class_attr_num_chips_per_sled.attr,
&class_attr_num_cores_per_chip.attr,
&class_attr_supported_lnc_degrees.attr,
&class_attr_intra_rack_topology.attr,
&class_attr_intra_rack_switch_type.attr,
&class_attr_inter_rack_topology.attr,
&class_attr_inter_rack_switch_type.attr,
NULL,
};
const struct attribute_group platform_group = {
.name = "platform",
.attrs = platform_attrs,
};
int nplatform_sysfs_add_efa_symlink(struct neuron_device *nd, struct kobject *nc_kobj, int nc_id)
{
const struct neuron_efa_bdf_map *m;
const char *bdf;
struct pci_dev *pdev;
unsigned int dom, bus, slot, func;
int global_nc_id;
int ret;
if (!ndhal || !ndhal->ndhal_arch.platform) {
return 0;
}
m = &ndhal->ndhal_arch.platform->nc_efa_bdf_tbl;
if (m->count == 0) {
return 0;
}
global_nc_id = nd->device_index * ndhal->ndhal_address_map.nc_per_device + nc_id;
if (global_nc_id >= m->count) {
return -ENOENT;
}
bdf = m->bdfs[global_nc_id];
if (!bdf) {
return -ENOENT;
}
if (sscanf(bdf, "%x:%x:%x.%x", &dom, &bus, &slot, &func) != 4) {
return -EINVAL;
}
pdev = pci_get_domain_bus_and_slot(dom, bus, PCI_DEVFN(slot, func));
if (!pdev) {
return -ENODEV;
}
ret = sysfs_create_link(nc_kobj, &pdev->dev.kobj, "efa_device");
pci_dev_put(pdev);
return ret;
}