1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
10 #include <linux/pci.h>
12 #define SET_CLK_PKT_TIMEOUT 1000000 /* 1s */
13 #define SET_PWR_PKT_TIMEOUT 1000000 /* 1s */
15 long hl_get_frequency(struct hl_device
*hdev
, u32 pll_index
, bool curr
)
17 struct armcp_packet pkt
;
21 memset(&pkt
, 0, sizeof(pkt
));
24 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_FREQUENCY_CURR_GET
<<
25 ARMCP_PKT_CTL_OPCODE_SHIFT
);
27 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_FREQUENCY_GET
<<
28 ARMCP_PKT_CTL_OPCODE_SHIFT
);
29 pkt
.pll_index
= cpu_to_le32(pll_index
);
31 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
32 SET_CLK_PKT_TIMEOUT
, &result
);
36 "Failed to get frequency of PLL %d, error %d\n",
44 void hl_set_frequency(struct hl_device
*hdev
, u32 pll_index
, u64 freq
)
46 struct armcp_packet pkt
;
49 memset(&pkt
, 0, sizeof(pkt
));
51 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_FREQUENCY_SET
<<
52 ARMCP_PKT_CTL_OPCODE_SHIFT
);
53 pkt
.pll_index
= cpu_to_le32(pll_index
);
54 pkt
.value
= cpu_to_le64(freq
);
56 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
57 SET_CLK_PKT_TIMEOUT
, NULL
);
61 "Failed to set frequency to PLL %d, error %d\n",
65 u64
hl_get_max_power(struct hl_device
*hdev
)
67 struct armcp_packet pkt
;
71 memset(&pkt
, 0, sizeof(pkt
));
73 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_MAX_POWER_GET
<<
74 ARMCP_PKT_CTL_OPCODE_SHIFT
);
76 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
77 SET_PWR_PKT_TIMEOUT
, &result
);
80 dev_err(hdev
->dev
, "Failed to get max power, error %d\n", rc
);
87 void hl_set_max_power(struct hl_device
*hdev
, u64 value
)
89 struct armcp_packet pkt
;
92 memset(&pkt
, 0, sizeof(pkt
));
94 pkt
.ctl
= cpu_to_le32(ARMCP_PACKET_MAX_POWER_SET
<<
95 ARMCP_PKT_CTL_OPCODE_SHIFT
);
96 pkt
.value
= cpu_to_le64(value
);
98 rc
= hdev
->asic_funcs
->send_cpu_message(hdev
, (u32
*) &pkt
, sizeof(pkt
),
99 SET_PWR_PKT_TIMEOUT
, NULL
);
102 dev_err(hdev
->dev
, "Failed to set max power, error %d\n", rc
);
105 static ssize_t
uboot_ver_show(struct device
*dev
, struct device_attribute
*attr
,
108 struct hl_device
*hdev
= dev_get_drvdata(dev
);
110 return sprintf(buf
, "%s\n", hdev
->asic_prop
.uboot_ver
);
113 static ssize_t
armcp_kernel_ver_show(struct device
*dev
,
114 struct device_attribute
*attr
, char *buf
)
116 struct hl_device
*hdev
= dev_get_drvdata(dev
);
118 return sprintf(buf
, "%s", hdev
->asic_prop
.armcp_info
.kernel_version
);
121 static ssize_t
armcp_ver_show(struct device
*dev
, struct device_attribute
*attr
,
124 struct hl_device
*hdev
= dev_get_drvdata(dev
);
126 return sprintf(buf
, "%s\n", hdev
->asic_prop
.armcp_info
.armcp_version
);
129 static ssize_t
cpld_ver_show(struct device
*dev
, struct device_attribute
*attr
,
132 struct hl_device
*hdev
= dev_get_drvdata(dev
);
134 return sprintf(buf
, "0x%08x\n",
135 hdev
->asic_prop
.armcp_info
.cpld_version
);
138 static ssize_t
infineon_ver_show(struct device
*dev
,
139 struct device_attribute
*attr
, char *buf
)
141 struct hl_device
*hdev
= dev_get_drvdata(dev
);
143 return sprintf(buf
, "0x%04x\n",
144 hdev
->asic_prop
.armcp_info
.infineon_version
);
147 static ssize_t
fuse_ver_show(struct device
*dev
, struct device_attribute
*attr
,
150 struct hl_device
*hdev
= dev_get_drvdata(dev
);
152 return sprintf(buf
, "%s\n", hdev
->asic_prop
.armcp_info
.fuse_version
);
155 static ssize_t
thermal_ver_show(struct device
*dev
,
156 struct device_attribute
*attr
, char *buf
)
158 struct hl_device
*hdev
= dev_get_drvdata(dev
);
160 return sprintf(buf
, "%s", hdev
->asic_prop
.armcp_info
.thermal_version
);
163 static ssize_t
preboot_btl_ver_show(struct device
*dev
,
164 struct device_attribute
*attr
, char *buf
)
166 struct hl_device
*hdev
= dev_get_drvdata(dev
);
168 return sprintf(buf
, "%s\n", hdev
->asic_prop
.preboot_ver
);
171 static ssize_t
soft_reset_store(struct device
*dev
,
172 struct device_attribute
*attr
, const char *buf
,
175 struct hl_device
*hdev
= dev_get_drvdata(dev
);
179 rc
= kstrtoul(buf
, 0, &value
);
186 hl_device_reset(hdev
, false, false);
192 static ssize_t
hard_reset_store(struct device
*dev
,
193 struct device_attribute
*attr
,
194 const char *buf
, size_t count
)
196 struct hl_device
*hdev
= dev_get_drvdata(dev
);
200 rc
= kstrtoul(buf
, 0, &value
);
207 hl_device_reset(hdev
, true, false);
213 static ssize_t
device_type_show(struct device
*dev
,
214 struct device_attribute
*attr
, char *buf
)
216 struct hl_device
*hdev
= dev_get_drvdata(dev
);
219 switch (hdev
->asic_type
) {
224 dev_err(hdev
->dev
, "Unrecognized ASIC type %d\n",
229 return sprintf(buf
, "%s\n", str
);
232 static ssize_t
pci_addr_show(struct device
*dev
, struct device_attribute
*attr
,
235 struct hl_device
*hdev
= dev_get_drvdata(dev
);
237 return sprintf(buf
, "%04x:%02x:%02x.%x\n",
238 pci_domain_nr(hdev
->pdev
->bus
),
239 hdev
->pdev
->bus
->number
,
240 PCI_SLOT(hdev
->pdev
->devfn
),
241 PCI_FUNC(hdev
->pdev
->devfn
));
244 static ssize_t
status_show(struct device
*dev
, struct device_attribute
*attr
,
247 struct hl_device
*hdev
= dev_get_drvdata(dev
);
250 if (atomic_read(&hdev
->in_reset
))
252 else if (hdev
->disabled
)
257 return sprintf(buf
, "%s\n", str
);
260 static ssize_t
soft_reset_cnt_show(struct device
*dev
,
261 struct device_attribute
*attr
, char *buf
)
263 struct hl_device
*hdev
= dev_get_drvdata(dev
);
265 return sprintf(buf
, "%d\n", hdev
->soft_reset_cnt
);
268 static ssize_t
hard_reset_cnt_show(struct device
*dev
,
269 struct device_attribute
*attr
, char *buf
)
271 struct hl_device
*hdev
= dev_get_drvdata(dev
);
273 return sprintf(buf
, "%d\n", hdev
->hard_reset_cnt
);
276 static ssize_t
max_power_show(struct device
*dev
, struct device_attribute
*attr
,
279 struct hl_device
*hdev
= dev_get_drvdata(dev
);
282 if (hl_device_disabled_or_in_reset(hdev
))
285 val
= hl_get_max_power(hdev
);
287 return sprintf(buf
, "%lu\n", val
);
290 static ssize_t
max_power_store(struct device
*dev
,
291 struct device_attribute
*attr
, const char *buf
, size_t count
)
293 struct hl_device
*hdev
= dev_get_drvdata(dev
);
297 if (hl_device_disabled_or_in_reset(hdev
)) {
302 rc
= kstrtoul(buf
, 0, &value
);
309 hdev
->max_power
= value
;
310 hl_set_max_power(hdev
, value
);
316 static ssize_t
eeprom_read_handler(struct file
*filp
, struct kobject
*kobj
,
317 struct bin_attribute
*attr
, char *buf
, loff_t offset
,
320 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
321 struct hl_device
*hdev
= dev_get_drvdata(dev
);
328 data
= kzalloc(max_size
, GFP_KERNEL
);
332 rc
= hdev
->asic_funcs
->get_eeprom_data(hdev
, data
, max_size
);
336 memcpy(buf
, data
, max_size
);
344 static DEVICE_ATTR_RO(armcp_kernel_ver
);
345 static DEVICE_ATTR_RO(armcp_ver
);
346 static DEVICE_ATTR_RO(cpld_ver
);
347 static DEVICE_ATTR_RO(device_type
);
348 static DEVICE_ATTR_RO(fuse_ver
);
349 static DEVICE_ATTR_WO(hard_reset
);
350 static DEVICE_ATTR_RO(hard_reset_cnt
);
351 static DEVICE_ATTR_RO(infineon_ver
);
352 static DEVICE_ATTR_RW(max_power
);
353 static DEVICE_ATTR_RO(pci_addr
);
354 static DEVICE_ATTR_RO(preboot_btl_ver
);
355 static DEVICE_ATTR_WO(soft_reset
);
356 static DEVICE_ATTR_RO(soft_reset_cnt
);
357 static DEVICE_ATTR_RO(status
);
358 static DEVICE_ATTR_RO(thermal_ver
);
359 static DEVICE_ATTR_RO(uboot_ver
);
361 static struct bin_attribute bin_attr_eeprom
= {
362 .attr
= {.name
= "eeprom", .mode
= (0444)},
364 .read
= eeprom_read_handler
367 static struct attribute
*hl_dev_attrs
[] = {
368 &dev_attr_armcp_kernel_ver
.attr
,
369 &dev_attr_armcp_ver
.attr
,
370 &dev_attr_cpld_ver
.attr
,
371 &dev_attr_device_type
.attr
,
372 &dev_attr_fuse_ver
.attr
,
373 &dev_attr_hard_reset
.attr
,
374 &dev_attr_hard_reset_cnt
.attr
,
375 &dev_attr_infineon_ver
.attr
,
376 &dev_attr_max_power
.attr
,
377 &dev_attr_pci_addr
.attr
,
378 &dev_attr_preboot_btl_ver
.attr
,
379 &dev_attr_soft_reset
.attr
,
380 &dev_attr_soft_reset_cnt
.attr
,
381 &dev_attr_status
.attr
,
382 &dev_attr_thermal_ver
.attr
,
383 &dev_attr_uboot_ver
.attr
,
387 static struct bin_attribute
*hl_dev_bin_attrs
[] = {
392 static struct attribute_group hl_dev_attr_group
= {
393 .attrs
= hl_dev_attrs
,
394 .bin_attrs
= hl_dev_bin_attrs
,
397 static struct attribute_group hl_dev_clks_attr_group
;
399 static const struct attribute_group
*hl_dev_attr_groups
[] = {
401 &hl_dev_clks_attr_group
,
405 int hl_sysfs_init(struct hl_device
*hdev
)
409 hdev
->pm_mng_profile
= PM_AUTO
;
410 hdev
->max_power
= hdev
->asic_prop
.max_power_default
;
412 hdev
->asic_funcs
->add_device_attr(hdev
, &hl_dev_clks_attr_group
);
414 rc
= device_add_groups(hdev
->dev
, hl_dev_attr_groups
);
417 "Failed to add groups to device, error %d\n", rc
);
424 void hl_sysfs_fini(struct hl_device
*hdev
)
426 device_remove_groups(hdev
->dev
, hl_dev_attr_groups
);