1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
12 long hl_get_frequency(struct hl_device
*hdev
, u32 pll_index
, bool curr
)
14 struct cpucp_packet pkt
;
18 memset(&pkt
, 0, sizeof(pkt
));
21 pkt
.ctl
= cpu_to_le32(CPUCP_PACKET_FREQUENCY_CURR_GET
<<
22 CPUCP_PKT_CTL_OPCODE_SHIFT
);
24 pkt
.ctl
= cpu_to_le32(CPUCP_PACKET_FREQUENCY_GET
<<
25 CPUCP_PKT_CTL_OPCODE_SHIFT
);
26 pkt
.pll_index
= cpu_to_le32(pll_index
);
28 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
33 "Failed to get frequency of PLL %d, error %d\n",
41 void hl_set_frequency(struct hl_device
*hdev
, u32 pll_index
, u64 freq
)
43 struct cpucp_packet pkt
;
46 memset(&pkt
, 0, sizeof(pkt
));
48 pkt
.ctl
= cpu_to_le32(CPUCP_PACKET_FREQUENCY_SET
<<
49 CPUCP_PKT_CTL_OPCODE_SHIFT
);
50 pkt
.pll_index
= cpu_to_le32(pll_index
);
51 pkt
.value
= cpu_to_le64(freq
);
53 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
58 "Failed to set frequency to PLL %d, error %d\n",
62 u64
hl_get_max_power(struct hl_device
*hdev
)
64 struct cpucp_packet pkt
;
68 memset(&pkt
, 0, sizeof(pkt
));
70 pkt
.ctl
= cpu_to_le32(CPUCP_PACKET_MAX_POWER_GET
<<
71 CPUCP_PKT_CTL_OPCODE_SHIFT
);
73 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
77 dev_err(hdev
->dev
, "Failed to get max power, error %d\n", rc
);
84 void hl_set_max_power(struct hl_device
*hdev
)
86 struct cpucp_packet pkt
;
89 memset(&pkt
, 0, sizeof(pkt
));
91 pkt
.ctl
= cpu_to_le32(CPUCP_PACKET_MAX_POWER_SET
<<
92 CPUCP_PKT_CTL_OPCODE_SHIFT
);
93 pkt
.value
= cpu_to_le64(hdev
->max_power
);
95 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
99 dev_err(hdev
->dev
, "Failed to set max power, error %d\n", rc
);
102 static ssize_t
uboot_ver_show(struct device
*dev
, struct device_attribute
*attr
,
105 struct hl_device
*hdev
= dev_get_drvdata(dev
);
107 return sprintf(buf
, "%s\n", hdev
->asic_prop
.uboot_ver
);
110 static ssize_t
armcp_kernel_ver_show(struct device
*dev
,
111 struct device_attribute
*attr
, char *buf
)
113 struct hl_device
*hdev
= dev_get_drvdata(dev
);
115 return sprintf(buf
, "%s", hdev
->asic_prop
.cpucp_info
.kernel_version
);
118 static ssize_t
armcp_ver_show(struct device
*dev
, struct device_attribute
*attr
,
121 struct hl_device
*hdev
= dev_get_drvdata(dev
);
123 return sprintf(buf
, "%s\n", hdev
->asic_prop
.cpucp_info
.cpucp_version
);
126 static ssize_t
cpld_ver_show(struct device
*dev
, struct device_attribute
*attr
,
129 struct hl_device
*hdev
= dev_get_drvdata(dev
);
131 return sprintf(buf
, "0x%08x\n",
132 hdev
->asic_prop
.cpucp_info
.cpld_version
);
135 static ssize_t
cpucp_kernel_ver_show(struct device
*dev
,
136 struct device_attribute
*attr
, char *buf
)
138 struct hl_device
*hdev
= dev_get_drvdata(dev
);
140 return sprintf(buf
, "%s", hdev
->asic_prop
.cpucp_info
.kernel_version
);
143 static ssize_t
cpucp_ver_show(struct device
*dev
, struct device_attribute
*attr
,
146 struct hl_device
*hdev
= dev_get_drvdata(dev
);
148 return sprintf(buf
, "%s\n", hdev
->asic_prop
.cpucp_info
.cpucp_version
);
151 static ssize_t
infineon_ver_show(struct device
*dev
,
152 struct device_attribute
*attr
, char *buf
)
154 struct hl_device
*hdev
= dev_get_drvdata(dev
);
156 return sprintf(buf
, "0x%04x\n",
157 hdev
->asic_prop
.cpucp_info
.infineon_version
);
160 static ssize_t
fuse_ver_show(struct device
*dev
, struct device_attribute
*attr
,
163 struct hl_device
*hdev
= dev_get_drvdata(dev
);
165 return sprintf(buf
, "%s\n", hdev
->asic_prop
.cpucp_info
.fuse_version
);
168 static ssize_t
thermal_ver_show(struct device
*dev
,
169 struct device_attribute
*attr
, char *buf
)
171 struct hl_device
*hdev
= dev_get_drvdata(dev
);
173 return sprintf(buf
, "%s", hdev
->asic_prop
.cpucp_info
.thermal_version
);
176 static ssize_t
preboot_btl_ver_show(struct device
*dev
,
177 struct device_attribute
*attr
, char *buf
)
179 struct hl_device
*hdev
= dev_get_drvdata(dev
);
181 return sprintf(buf
, "%s\n", hdev
->asic_prop
.preboot_ver
);
184 static ssize_t
soft_reset_store(struct device
*dev
,
185 struct device_attribute
*attr
, const char *buf
,
188 struct hl_device
*hdev
= dev_get_drvdata(dev
);
192 rc
= kstrtoul(buf
, 0, &value
);
199 if (!hdev
->supports_soft_reset
) {
200 dev_err(hdev
->dev
, "Device does not support soft-reset\n");
204 dev_warn(hdev
->dev
, "Soft-Reset requested through sysfs\n");
206 hl_device_reset(hdev
, false, false);
212 static ssize_t
hard_reset_store(struct device
*dev
,
213 struct device_attribute
*attr
,
214 const char *buf
, size_t count
)
216 struct hl_device
*hdev
= dev_get_drvdata(dev
);
220 rc
= kstrtoul(buf
, 0, &value
);
227 dev_warn(hdev
->dev
, "Hard-Reset requested through sysfs\n");
229 hl_device_reset(hdev
, true, false);
235 static ssize_t
device_type_show(struct device
*dev
,
236 struct device_attribute
*attr
, char *buf
)
238 struct hl_device
*hdev
= dev_get_drvdata(dev
);
241 switch (hdev
->asic_type
) {
249 dev_err(hdev
->dev
, "Unrecognized ASIC type %d\n",
254 return sprintf(buf
, "%s\n", str
);
257 static ssize_t
pci_addr_show(struct device
*dev
, struct device_attribute
*attr
,
260 struct hl_device
*hdev
= dev_get_drvdata(dev
);
262 return sprintf(buf
, "%04x:%02x:%02x.%x\n",
263 pci_domain_nr(hdev
->pdev
->bus
),
264 hdev
->pdev
->bus
->number
,
265 PCI_SLOT(hdev
->pdev
->devfn
),
266 PCI_FUNC(hdev
->pdev
->devfn
));
269 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
272 struct hl_device
*hdev
= dev_get_drvdata(dev
);
275 if (atomic_read(&hdev
->in_reset
))
277 else if (hdev
->disabled
)
279 else if (hdev
->needs_reset
)
284 return sprintf(buf
, "%s\n", str
);
287 static ssize_t
soft_reset_cnt_show(struct device
*dev
,
288 struct device_attribute
*attr
, char *buf
)
290 struct hl_device
*hdev
= dev_get_drvdata(dev
);
292 return sprintf(buf
, "%d\n", hdev
->soft_reset_cnt
);
295 static ssize_t
hard_reset_cnt_show(struct device
*dev
,
296 struct device_attribute
*attr
, char *buf
)
298 struct hl_device
*hdev
= dev_get_drvdata(dev
);
300 return sprintf(buf
, "%d\n", hdev
->hard_reset_cnt
);
303 static ssize_t
max_power_show(struct device
*dev
, struct device_attribute
*attr
,
306 struct hl_device
*hdev
= dev_get_drvdata(dev
);
309 if (!hl_device_operational(hdev
, NULL
))
312 val
= hl_get_max_power(hdev
);
314 return sprintf(buf
, "%lu\n", val
);
317 static ssize_t
max_power_store(struct device
*dev
,
318 struct device_attribute
*attr
, const char *buf
, size_t count
)
320 struct hl_device
*hdev
= dev_get_drvdata(dev
);
324 if (!hl_device_operational(hdev
, NULL
)) {
329 rc
= kstrtoul(buf
, 0, &value
);
336 hdev
->max_power
= value
;
337 hl_set_max_power(hdev
);
343 static ssize_t
eeprom_read_handler(struct file
*filp
, struct kobject
*kobj
,
344 struct bin_attribute
*attr
, char *buf
, loff_t offset
,
347 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
348 struct hl_device
*hdev
= dev_get_drvdata(dev
);
352 if (!hl_device_operational(hdev
, NULL
))
358 data
= kzalloc(max_size
, GFP_KERNEL
);
362 rc
= hdev
->asic_funcs
->get_eeprom_data(hdev
, data
, max_size
);
366 memcpy(buf
, data
, max_size
);
374 static DEVICE_ATTR_RO(armcp_kernel_ver
);
375 static DEVICE_ATTR_RO(armcp_ver
);
376 static DEVICE_ATTR_RO(cpld_ver
);
377 static DEVICE_ATTR_RO(cpucp_kernel_ver
);
378 static DEVICE_ATTR_RO(cpucp_ver
);
379 static DEVICE_ATTR_RO(device_type
);
380 static DEVICE_ATTR_RO(fuse_ver
);
381 static DEVICE_ATTR_WO(hard_reset
);
382 static DEVICE_ATTR_RO(hard_reset_cnt
);
383 static DEVICE_ATTR_RO(infineon_ver
);
384 static DEVICE_ATTR_RW(max_power
);
385 static DEVICE_ATTR_RO(pci_addr
);
386 static DEVICE_ATTR_RO(preboot_btl_ver
);
387 static DEVICE_ATTR_WO(soft_reset
);
388 static DEVICE_ATTR_RO(soft_reset_cnt
);
389 static DEVICE_ATTR_RO(status
);
390 static DEVICE_ATTR_RO(thermal_ver
);
391 static DEVICE_ATTR_RO(uboot_ver
);
393 static struct bin_attribute bin_attr_eeprom
= {
394 .attr
= {.name
= "eeprom", .mode
= (0444)},
396 .read
= eeprom_read_handler
399 static struct attribute
*hl_dev_attrs
[] = {
400 &dev_attr_armcp_kernel_ver
.attr
,
401 &dev_attr_armcp_ver
.attr
,
402 &dev_attr_cpld_ver
.attr
,
403 &dev_attr_cpucp_kernel_ver
.attr
,
404 &dev_attr_cpucp_ver
.attr
,
405 &dev_attr_device_type
.attr
,
406 &dev_attr_fuse_ver
.attr
,
407 &dev_attr_hard_reset
.attr
,
408 &dev_attr_hard_reset_cnt
.attr
,
409 &dev_attr_infineon_ver
.attr
,
410 &dev_attr_max_power
.attr
,
411 &dev_attr_pci_addr
.attr
,
412 &dev_attr_preboot_btl_ver
.attr
,
413 &dev_attr_soft_reset
.attr
,
414 &dev_attr_soft_reset_cnt
.attr
,
415 &dev_attr_status
.attr
,
416 &dev_attr_thermal_ver
.attr
,
417 &dev_attr_uboot_ver
.attr
,
421 static struct bin_attribute
*hl_dev_bin_attrs
[] = {
426 static struct attribute_group hl_dev_attr_group
= {
427 .attrs
= hl_dev_attrs
,
428 .bin_attrs
= hl_dev_bin_attrs
,
431 static struct attribute_group hl_dev_clks_attr_group
;
433 static const struct attribute_group
*hl_dev_attr_groups
[] = {
435 &hl_dev_clks_attr_group
,
439 int hl_sysfs_init(struct hl_device
*hdev
)
443 if (hdev
->asic_type
== ASIC_GOYA
)
444 hdev
->pm_mng_profile
= PM_AUTO
;
446 hdev
->pm_mng_profile
= PM_MANUAL
;
448 hdev
->max_power
= hdev
->asic_prop
.max_power_default
;
450 hdev
->asic_funcs
->add_device_attr(hdev
, &hl_dev_clks_attr_group
);
452 rc
= device_add_groups(hdev
->dev
, hl_dev_attr_groups
);
455 "Failed to add groups to device, error %d\n", rc
);
462 void hl_sysfs_fini(struct hl_device
*hdev
)
464 device_remove_groups(hdev
->dev
, hl_dev_attr_groups
);