sysfs.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright 2016-2019 HabanaLabs, Ltd.
 * All Rights Reserved.
 */

#include "habanalabs.h"

#include <linux/pci.h>

12
long hl_get_frequency(struct hl_device *hdev, u32 pll_index,
13
								bool curr)
14
{
15
	struct cpucp_packet pkt;
16
	u32 used_pll_idx;
17
	u64 result;
18 19
	int rc;

20 21 22 23
	rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
	if (rc)
		return rc;

24 25 26
	memset(&pkt, 0, sizeof(pkt));

	if (curr)
27 28
		pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_CURR_GET <<
						CPUCP_PKT_CTL_OPCODE_SHIFT);
29
	else
30 31
		pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_GET <<
						CPUCP_PKT_CTL_OPCODE_SHIFT);
32
	pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
33 34

	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
35
						0, &result);
36 37 38 39

	if (rc) {
		dev_err(hdev->dev,
			"Failed to get frequency of PLL %d, error %d\n",
40
			used_pll_idx, rc);
41
		return rc;
42 43
	}

44
	return (long) result;
45 46
}

47
void hl_set_frequency(struct hl_device *hdev, u32 pll_index,
48
								u64 freq)
49
{
50
	struct cpucp_packet pkt;
51
	u32 used_pll_idx;
52 53
	int rc;

54 55 56 57
	rc = get_used_pll_index(hdev, pll_index, &used_pll_idx);
	if (rc)
		return;

58 59
	memset(&pkt, 0, sizeof(pkt));

60 61
	pkt.ctl = cpu_to_le32(CPUCP_PACKET_FREQUENCY_SET <<
					CPUCP_PKT_CTL_OPCODE_SHIFT);
62
	pkt.pll_index = cpu_to_le32((u32)used_pll_idx);
63
	pkt.value = cpu_to_le64(freq);
64 65

	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
66
						0, NULL);
67 68 69 70

	if (rc)
		dev_err(hdev->dev,
			"Failed to set frequency to PLL %d, error %d\n",
71
			used_pll_idx, rc);
72 73 74 75
}

u64 hl_get_max_power(struct hl_device *hdev)
{
76
	struct cpucp_packet pkt;
77
	u64 result;
78 79 80 81
	int rc;

	memset(&pkt, 0, sizeof(pkt));

82 83
	pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_GET <<
				CPUCP_PKT_CTL_OPCODE_SHIFT);
84 85

	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
86
						0, &result);
87 88 89

	if (rc) {
		dev_err(hdev->dev, "Failed to get max power, error %d\n", rc);
90
		return (u64) rc;
91 92 93 94 95
	}

	return result;
}

96
void hl_set_max_power(struct hl_device *hdev)
97
{
98
	struct cpucp_packet pkt;
99 100 101 102
	int rc;

	memset(&pkt, 0, sizeof(pkt));

103 104
	pkt.ctl = cpu_to_le32(CPUCP_PACKET_MAX_POWER_SET <<
				CPUCP_PKT_CTL_OPCODE_SHIFT);
105
	pkt.value = cpu_to_le64(hdev->max_power);
106 107

	rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
108
						0, NULL);
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

	if (rc)
		dev_err(hdev->dev, "Failed to set max power, error %d\n", rc);
}

static ssize_t uboot_ver_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "%s\n", hdev->asic_prop.uboot_ver);
}

static ssize_t armcp_kernel_ver_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

127
	return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
128 129 130 131 132 133 134
}

static ssize_t armcp_ver_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

135
	return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
136 137 138 139 140 141 142 143
}

static ssize_t cpld_ver_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "0x%08x\n",
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
			hdev->asic_prop.cpucp_info.cpld_version);
}

static ssize_t cpucp_kernel_ver_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.kernel_version);
}

static ssize_t cpucp_ver_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.cpucp_version);
161 162 163 164 165 166 167 168
}

static ssize_t infineon_ver_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "0x%04x\n",
169
			hdev->asic_prop.cpucp_info.infineon_version);
170 171 172 173 174 175 176
}

static ssize_t fuse_ver_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

177
	return sprintf(buf, "%s\n", hdev->asic_prop.cpucp_info.fuse_version);
178 179 180 181 182 183 184
}

static ssize_t thermal_ver_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

185
	return sprintf(buf, "%s", hdev->asic_prop.cpucp_info.thermal_version);
186 187 188 189 190 191 192 193 194 195
}

static ssize_t preboot_btl_ver_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "%s\n", hdev->asic_prop.preboot_ver);
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
static ssize_t soft_reset_store(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	long value;
	int rc;

	rc = kstrtoul(buf, 0, &value);

	if (rc) {
		count = -EINVAL;
		goto out;
	}

211 212 213 214 215
	if (!hdev->supports_soft_reset) {
		dev_err(hdev->dev, "Device does not support soft-reset\n");
		goto out;
	}

216 217
	dev_warn(hdev->dev, "Soft-Reset requested through sysfs\n");

218
	hl_device_reset(hdev, 0);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

out:
	return count;
}

static ssize_t hard_reset_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	long value;
	int rc;

	rc = kstrtoul(buf, 0, &value);

	if (rc) {
		count = -EINVAL;
		goto out;
	}

239 240
	dev_warn(hdev->dev, "Hard-Reset requested through sysfs\n");

241
	hl_device_reset(hdev, HL_RESET_HARD);
242 243 244 245 246

out:
	return count;
}

247 248 249 250 251 252 253 254 255 256
static ssize_t device_type_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	char *str;

	switch (hdev->asic_type) {
	case ASIC_GOYA:
		str = "GOYA";
		break;
257 258 259
	case ASIC_GAUDI:
		str = "GAUDI";
		break;
260 261 262
	case ASIC_GAUDI_SEC:
		str = "GAUDI SEC";
		break;
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
	default:
		dev_err(hdev->dev, "Unrecognized ASIC type %d\n",
				hdev->asic_type);
		return -EINVAL;
	}

	return sprintf(buf, "%s\n", str);
}

static ssize_t pci_addr_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "%04x:%02x:%02x.%x\n",
			pci_domain_nr(hdev->pdev->bus),
			hdev->pdev->bus->number,
			PCI_SLOT(hdev->pdev->devfn),
			PCI_FUNC(hdev->pdev->devfn));
}

static ssize_t status_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	char *str;

290 291 292
	if (atomic_read(&hdev->in_reset))
		str = "In reset";
	else if (hdev->disabled)
293
		str = "Malfunction";
294 295
	else if (hdev->needs_reset)
		str = "Needs Reset";
296 297 298 299 300 301
	else
		str = "Operational";

	return sprintf(buf, "%s\n", str);
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
static ssize_t soft_reset_cnt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", hdev->soft_reset_cnt);
}

static ssize_t hard_reset_cnt_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);

	return sprintf(buf, "%d\n", hdev->hard_reset_cnt);
}

318 319 320 321 322 323
static ssize_t max_power_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	long val;

324
	if (!hl_device_operational(hdev, NULL))
325 326 327 328 329 330 331 332 333 334 335 336 337 338
		return -ENODEV;

	val = hl_get_max_power(hdev);

	return sprintf(buf, "%lu\n", val);
}

static ssize_t max_power_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	unsigned long value;
	int rc;

339
	if (!hl_device_operational(hdev, NULL)) {
340 341 342 343 344 345 346 347 348 349 350 351
		count = -ENODEV;
		goto out;
	}

	rc = kstrtoul(buf, 0, &value);

	if (rc) {
		count = -EINVAL;
		goto out;
	}

	hdev->max_power = value;
352
	hl_set_max_power(hdev);
353 354 355 356 357 358 359 360 361

out:
	return count;
}

static ssize_t eeprom_read_handler(struct file *filp, struct kobject *kobj,
			struct bin_attribute *attr, char *buf, loff_t offset,
			size_t max_size)
{
362
	struct device *dev = kobj_to_dev(kobj);
363 364 365 366
	struct hl_device *hdev = dev_get_drvdata(dev);
	char *data;
	int rc;

367
	if (!hl_device_operational(hdev, NULL))
368 369
		return -ENODEV;

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	if (!max_size)
		return -EINVAL;

	data = kzalloc(max_size, GFP_KERNEL);
	if (!data)
		return -ENOMEM;

	rc = hdev->asic_funcs->get_eeprom_data(hdev, data, max_size);
	if (rc)
		goto out;

	memcpy(buf, data, max_size);

out:
	kfree(data);

	return max_size;
}

static DEVICE_ATTR_RO(armcp_kernel_ver);
static DEVICE_ATTR_RO(armcp_ver);
static DEVICE_ATTR_RO(cpld_ver);
392 393
static DEVICE_ATTR_RO(cpucp_kernel_ver);
static DEVICE_ATTR_RO(cpucp_ver);
394 395
static DEVICE_ATTR_RO(device_type);
static DEVICE_ATTR_RO(fuse_ver);
396 397
static DEVICE_ATTR_WO(hard_reset);
static DEVICE_ATTR_RO(hard_reset_cnt);
398 399 400 401
static DEVICE_ATTR_RO(infineon_ver);
static DEVICE_ATTR_RW(max_power);
static DEVICE_ATTR_RO(pci_addr);
static DEVICE_ATTR_RO(preboot_btl_ver);
402 403
static DEVICE_ATTR_WO(soft_reset);
static DEVICE_ATTR_RO(soft_reset_cnt);
404 405 406 407 408 409 410 411 412 413 414 415 416 417
static DEVICE_ATTR_RO(status);
static DEVICE_ATTR_RO(thermal_ver);
static DEVICE_ATTR_RO(uboot_ver);

static struct bin_attribute bin_attr_eeprom = {
	.attr = {.name = "eeprom", .mode = (0444)},
	.size = PAGE_SIZE,
	.read = eeprom_read_handler
};

static struct attribute *hl_dev_attrs[] = {
	&dev_attr_armcp_kernel_ver.attr,
	&dev_attr_armcp_ver.attr,
	&dev_attr_cpld_ver.attr,
418 419
	&dev_attr_cpucp_kernel_ver.attr,
	&dev_attr_cpucp_ver.attr,
420 421
	&dev_attr_device_type.attr,
	&dev_attr_fuse_ver.attr,
422 423
	&dev_attr_hard_reset.attr,
	&dev_attr_hard_reset_cnt.attr,
424 425 426 427
	&dev_attr_infineon_ver.attr,
	&dev_attr_max_power.attr,
	&dev_attr_pci_addr.attr,
	&dev_attr_preboot_btl_ver.attr,
428 429
	&dev_attr_soft_reset.attr,
	&dev_attr_soft_reset_cnt.attr,
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	&dev_attr_status.attr,
	&dev_attr_thermal_ver.attr,
	&dev_attr_uboot_ver.attr,
	NULL,
};

static struct bin_attribute *hl_dev_bin_attrs[] = {
	&bin_attr_eeprom,
	NULL
};

static struct attribute_group hl_dev_attr_group = {
	.attrs = hl_dev_attrs,
	.bin_attrs = hl_dev_bin_attrs,
};

static struct attribute_group hl_dev_clks_attr_group;

static const struct attribute_group *hl_dev_attr_groups[] = {
	&hl_dev_attr_group,
	&hl_dev_clks_attr_group,
	NULL,
};

int hl_sysfs_init(struct hl_device *hdev)
{
	int rc;

458 459 460 461
	if (hdev->asic_type == ASIC_GOYA)
		hdev->pm_mng_profile = PM_AUTO;
	else
		hdev->pm_mng_profile = PM_MANUAL;
462

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	hdev->max_power = hdev->asic_prop.max_power_default;

	hdev->asic_funcs->add_device_attr(hdev, &hl_dev_clks_attr_group);

	rc = device_add_groups(hdev->dev, hl_dev_attr_groups);
	if (rc) {
		dev_err(hdev->dev,
			"Failed to add groups to device, error %d\n", rc);
		return rc;
	}

	return 0;
}

void hl_sysfs_fini(struct hl_device *hdev)
{
	device_remove_groups(hdev->dev, hl_dev_attr_groups);
}