123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Copyright 2016-2022 HabanaLabs, Ltd.
- * All Rights Reserved.
- */
- #include "goyaP.h"
- void goya_set_pll_profile(struct hl_device *hdev, enum hl_pll_frequency freq)
- {
- struct goya_device *goya = hdev->asic_specific;
- if (!hdev->pdev)
- return;
- switch (freq) {
- case PLL_HIGH:
- hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, hdev->high_pll);
- hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, hdev->high_pll);
- hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, hdev->high_pll);
- break;
- case PLL_LOW:
- hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, GOYA_PLL_FREQ_LOW);
- hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, GOYA_PLL_FREQ_LOW);
- hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, GOYA_PLL_FREQ_LOW);
- break;
- case PLL_LAST:
- hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, goya->mme_clk);
- hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, goya->tpc_clk);
- hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, goya->ic_clk);
- break;
- default:
- dev_err(hdev->dev, "unknown frequency setting\n");
- }
- }
- static ssize_t mme_clk_show(struct device *dev, struct device_attribute *attr,
- char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- long value;
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- value = hl_fw_get_frequency(hdev, HL_GOYA_MME_PLL, false);
- if (value < 0)
- return value;
- return sprintf(buf, "%lu\n", value);
- }
- static ssize_t mme_clk_store(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t count)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- struct goya_device *goya = hdev->asic_specific;
- int rc;
- long value;
- if (!hl_device_operational(hdev, NULL)) {
- count = -ENODEV;
- goto fail;
- }
- if (goya->pm_mng_profile == PM_AUTO) {
- count = -EPERM;
- goto fail;
- }
- rc = kstrtoul(buf, 0, &value);
- if (rc) {
- count = -EINVAL;
- goto fail;
- }
- hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, value);
- goya->mme_clk = value;
- fail:
- return count;
- }
- static ssize_t tpc_clk_show(struct device *dev, struct device_attribute *attr,
- char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- long value;
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- value = hl_fw_get_frequency(hdev, HL_GOYA_TPC_PLL, false);
- if (value < 0)
- return value;
- return sprintf(buf, "%lu\n", value);
- }
- static ssize_t tpc_clk_store(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t count)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- struct goya_device *goya = hdev->asic_specific;
- int rc;
- long value;
- if (!hl_device_operational(hdev, NULL)) {
- count = -ENODEV;
- goto fail;
- }
- if (goya->pm_mng_profile == PM_AUTO) {
- count = -EPERM;
- goto fail;
- }
- rc = kstrtoul(buf, 0, &value);
- if (rc) {
- count = -EINVAL;
- goto fail;
- }
- hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, value);
- goya->tpc_clk = value;
- fail:
- return count;
- }
- static ssize_t ic_clk_show(struct device *dev, struct device_attribute *attr,
- char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- long value;
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- value = hl_fw_get_frequency(hdev, HL_GOYA_IC_PLL, false);
- if (value < 0)
- return value;
- return sprintf(buf, "%lu\n", value);
- }
- static ssize_t ic_clk_store(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t count)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- struct goya_device *goya = hdev->asic_specific;
- int rc;
- long value;
- if (!hl_device_operational(hdev, NULL)) {
- count = -ENODEV;
- goto fail;
- }
- if (goya->pm_mng_profile == PM_AUTO) {
- count = -EPERM;
- goto fail;
- }
- rc = kstrtoul(buf, 0, &value);
- if (rc) {
- count = -EINVAL;
- goto fail;
- }
- hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, value);
- goya->ic_clk = value;
- fail:
- return count;
- }
- static ssize_t mme_clk_curr_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- long value;
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- value = hl_fw_get_frequency(hdev, HL_GOYA_MME_PLL, true);
- if (value < 0)
- return value;
- return sprintf(buf, "%lu\n", value);
- }
- static ssize_t tpc_clk_curr_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- long value;
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- value = hl_fw_get_frequency(hdev, HL_GOYA_TPC_PLL, true);
- if (value < 0)
- return value;
- return sprintf(buf, "%lu\n", value);
- }
- static ssize_t ic_clk_curr_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- long value;
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- value = hl_fw_get_frequency(hdev, HL_GOYA_IC_PLL, true);
- if (value < 0)
- return value;
- return sprintf(buf, "%lu\n", value);
- }
- static ssize_t pm_mng_profile_show(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- struct goya_device *goya = hdev->asic_specific;
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- return sprintf(buf, "%s\n",
- (goya->pm_mng_profile == PM_AUTO) ? "auto" :
- (goya->pm_mng_profile == PM_MANUAL) ? "manual" :
- "unknown");
- }
- static ssize_t pm_mng_profile_store(struct device *dev,
- struct device_attribute *attr, const char *buf, size_t count)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- struct goya_device *goya = hdev->asic_specific;
- if (!hl_device_operational(hdev, NULL)) {
- count = -ENODEV;
- goto out;
- }
- mutex_lock(&hdev->fpriv_list_lock);
- if (hdev->is_compute_ctx_active) {
- dev_err(hdev->dev,
- "Can't change PM profile while compute context is opened on the device\n");
- count = -EPERM;
- goto unlock_mutex;
- }
- if (strncmp("auto", buf, strlen("auto")) == 0) {
- /* Make sure we are in LOW PLL when changing modes */
- if (goya->pm_mng_profile == PM_MANUAL) {
- goya->curr_pll_profile = PLL_HIGH;
- goya->pm_mng_profile = PM_AUTO;
- goya_set_frequency(hdev, PLL_LOW);
- }
- } else if (strncmp("manual", buf, strlen("manual")) == 0) {
- if (goya->pm_mng_profile == PM_AUTO) {
- /* Must release the lock because the work thread also
- * takes this lock. But before we release it, set
- * the mode to manual so nothing will change if a user
- * suddenly opens the device
- */
- goya->pm_mng_profile = PM_MANUAL;
- mutex_unlock(&hdev->fpriv_list_lock);
- /* Flush the current work so we can return to the user
- * knowing that he is the only one changing frequencies
- */
- if (goya->goya_work)
- flush_delayed_work(&goya->goya_work->work_freq);
- return count;
- }
- } else {
- dev_err(hdev->dev, "value should be auto or manual\n");
- count = -EINVAL;
- }
- unlock_mutex:
- mutex_unlock(&hdev->fpriv_list_lock);
- out:
- return count;
- }
- static ssize_t high_pll_show(struct device *dev, struct device_attribute *attr,
- char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- if (!hl_device_operational(hdev, NULL))
- return -ENODEV;
- return sprintf(buf, "%u\n", hdev->high_pll);
- }
- static ssize_t high_pll_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;
- if (!hl_device_operational(hdev, NULL)) {
- count = -ENODEV;
- goto out;
- }
- rc = kstrtoul(buf, 0, &value);
- if (rc) {
- count = -EINVAL;
- goto out;
- }
- hdev->high_pll = value;
- out:
- return count;
- }
- static DEVICE_ATTR_RW(high_pll);
- static DEVICE_ATTR_RW(ic_clk);
- static DEVICE_ATTR_RO(ic_clk_curr);
- static DEVICE_ATTR_RW(mme_clk);
- static DEVICE_ATTR_RO(mme_clk_curr);
- static DEVICE_ATTR_RW(pm_mng_profile);
- static DEVICE_ATTR_RW(tpc_clk);
- static DEVICE_ATTR_RO(tpc_clk_curr);
- static struct attribute *goya_clk_dev_attrs[] = {
- &dev_attr_high_pll.attr,
- &dev_attr_ic_clk.attr,
- &dev_attr_ic_clk_curr.attr,
- &dev_attr_mme_clk.attr,
- &dev_attr_mme_clk_curr.attr,
- &dev_attr_pm_mng_profile.attr,
- &dev_attr_tpc_clk.attr,
- &dev_attr_tpc_clk_curr.attr,
- NULL,
- };
- static ssize_t infineon_ver_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct hl_device *hdev = dev_get_drvdata(dev);
- struct cpucp_info *cpucp_info;
- cpucp_info = &hdev->asic_prop.cpucp_info;
- return sprintf(buf, "%#04x\n", le32_to_cpu(cpucp_info->infineon_version));
- }
- static DEVICE_ATTR_RO(infineon_ver);
- static struct attribute *goya_vrm_dev_attrs[] = {
- &dev_attr_infineon_ver.attr,
- NULL,
- };
- void goya_add_device_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp,
- struct attribute_group *dev_vrm_attr_grp)
- {
- dev_clk_attr_grp->attrs = goya_clk_dev_attrs;
- dev_vrm_attr_grp->attrs = goya_vrm_dev_attrs;
- }
|