gaudi_hwmgr.c 2.6 KB
Newer Older
1 2 3 4 5 6 7 8
// SPDX-License-Identifier: GPL-2.0

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

#include "gaudiP.h"
9
#include "../include/gaudi/gaudi_fw_if.h"
10 11 12 13 14 15

void gaudi_set_pll_profile(struct hl_device *hdev, enum hl_pll_frequency freq)
{
	struct gaudi_device *gaudi = hdev->asic_specific;

	if (freq == PLL_LAST)
16
		hl_set_frequency(hdev, HL_GAUDI_MME_PLL, gaudi->max_freq_value);
17 18 19 20 21 22
}

int gaudi_get_clk_rate(struct hl_device *hdev, u32 *cur_clk, u32 *max_clk)
{
	long value;

23
	if (!hl_device_operational(hdev, NULL))
24 25
		return -ENODEV;

26
	value = hl_get_frequency(hdev, HL_GAUDI_MME_PLL, false);
27 28 29 30 31 32 33 34 35

	if (value < 0) {
		dev_err(hdev->dev, "Failed to retrieve device max clock %ld\n",
			value);
		return value;
	}

	*max_clk = (value / 1000 / 1000);

36
	value = hl_get_frequency(hdev, HL_GAUDI_MME_PLL, true);
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

	if (value < 0) {
		dev_err(hdev->dev,
			"Failed to retrieve device current clock %ld\n",
			value);
		return value;
	}

	*cur_clk = (value / 1000 / 1000);

	return 0;
}

static ssize_t clk_max_freq_mhz_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	struct gaudi_device *gaudi = hdev->asic_specific;
	long value;

57
	if (!hl_device_operational(hdev, NULL))
58 59
		return -ENODEV;

60
	value = hl_get_frequency(hdev, HL_GAUDI_MME_PLL, false);
61 62 63 64 65 66 67 68 69 70 71 72 73 74

	gaudi->max_freq_value = value;

	return sprintf(buf, "%lu\n", (value / 1000 / 1000));
}

static ssize_t clk_max_freq_mhz_store(struct device *dev,
		struct device_attribute *attr, const char *buf, size_t count)
{
	struct hl_device *hdev = dev_get_drvdata(dev);
	struct gaudi_device *gaudi = hdev->asic_specific;
	int rc;
	u64 value;

75
	if (!hl_device_operational(hdev, NULL)) {
76 77 78 79 80 81 82 83 84 85 86 87
		count = -ENODEV;
		goto fail;
	}

	rc = kstrtoull(buf, 0, &value);
	if (rc) {
		count = -EINVAL;
		goto fail;
	}

	gaudi->max_freq_value = value * 1000 * 1000;

88
	hl_set_frequency(hdev, HL_GAUDI_MME_PLL, gaudi->max_freq_value);
89 90 91 92 93 94 95 96 97 98 99

fail:
	return count;
}

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

100
	if (!hl_device_operational(hdev, NULL))
101 102
		return -ENODEV;

103
	value = hl_get_frequency(hdev, HL_GAUDI_MME_PLL, true);
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

	return sprintf(buf, "%lu\n", (value / 1000 / 1000));
}

static DEVICE_ATTR_RW(clk_max_freq_mhz);
static DEVICE_ATTR_RO(clk_cur_freq_mhz);

static struct attribute *gaudi_dev_attrs[] = {
	&dev_attr_clk_max_freq_mhz.attr,
	&dev_attr_clk_cur_freq_mhz.attr,
	NULL,
};

void gaudi_add_device_attr(struct hl_device *hdev,
			struct attribute_group *dev_attr_grp)
{
	dev_attr_grp->attrs = gaudi_dev_attrs;
}