goya_hwmgr.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2016-2022 HabanaLabs, Ltd.
  4. * All Rights Reserved.
  5. */
  6. #include "goyaP.h"
  7. void goya_set_pll_profile(struct hl_device *hdev, enum hl_pll_frequency freq)
  8. {
  9. struct goya_device *goya = hdev->asic_specific;
  10. if (!hdev->pdev)
  11. return;
  12. switch (freq) {
  13. case PLL_HIGH:
  14. hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, hdev->high_pll);
  15. hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, hdev->high_pll);
  16. hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, hdev->high_pll);
  17. break;
  18. case PLL_LOW:
  19. hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, GOYA_PLL_FREQ_LOW);
  20. hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, GOYA_PLL_FREQ_LOW);
  21. hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, GOYA_PLL_FREQ_LOW);
  22. break;
  23. case PLL_LAST:
  24. hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, goya->mme_clk);
  25. hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, goya->tpc_clk);
  26. hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, goya->ic_clk);
  27. break;
  28. default:
  29. dev_err(hdev->dev, "unknown frequency setting\n");
  30. }
  31. }
  32. static ssize_t mme_clk_show(struct device *dev, struct device_attribute *attr,
  33. char *buf)
  34. {
  35. struct hl_device *hdev = dev_get_drvdata(dev);
  36. long value;
  37. if (!hl_device_operational(hdev, NULL))
  38. return -ENODEV;
  39. value = hl_fw_get_frequency(hdev, HL_GOYA_MME_PLL, false);
  40. if (value < 0)
  41. return value;
  42. return sprintf(buf, "%lu\n", value);
  43. }
  44. static ssize_t mme_clk_store(struct device *dev, struct device_attribute *attr,
  45. const char *buf, size_t count)
  46. {
  47. struct hl_device *hdev = dev_get_drvdata(dev);
  48. struct goya_device *goya = hdev->asic_specific;
  49. int rc;
  50. long value;
  51. if (!hl_device_operational(hdev, NULL)) {
  52. count = -ENODEV;
  53. goto fail;
  54. }
  55. if (goya->pm_mng_profile == PM_AUTO) {
  56. count = -EPERM;
  57. goto fail;
  58. }
  59. rc = kstrtoul(buf, 0, &value);
  60. if (rc) {
  61. count = -EINVAL;
  62. goto fail;
  63. }
  64. hl_fw_set_frequency(hdev, HL_GOYA_MME_PLL, value);
  65. goya->mme_clk = value;
  66. fail:
  67. return count;
  68. }
  69. static ssize_t tpc_clk_show(struct device *dev, struct device_attribute *attr,
  70. char *buf)
  71. {
  72. struct hl_device *hdev = dev_get_drvdata(dev);
  73. long value;
  74. if (!hl_device_operational(hdev, NULL))
  75. return -ENODEV;
  76. value = hl_fw_get_frequency(hdev, HL_GOYA_TPC_PLL, false);
  77. if (value < 0)
  78. return value;
  79. return sprintf(buf, "%lu\n", value);
  80. }
  81. static ssize_t tpc_clk_store(struct device *dev, struct device_attribute *attr,
  82. const char *buf, size_t count)
  83. {
  84. struct hl_device *hdev = dev_get_drvdata(dev);
  85. struct goya_device *goya = hdev->asic_specific;
  86. int rc;
  87. long value;
  88. if (!hl_device_operational(hdev, NULL)) {
  89. count = -ENODEV;
  90. goto fail;
  91. }
  92. if (goya->pm_mng_profile == PM_AUTO) {
  93. count = -EPERM;
  94. goto fail;
  95. }
  96. rc = kstrtoul(buf, 0, &value);
  97. if (rc) {
  98. count = -EINVAL;
  99. goto fail;
  100. }
  101. hl_fw_set_frequency(hdev, HL_GOYA_TPC_PLL, value);
  102. goya->tpc_clk = value;
  103. fail:
  104. return count;
  105. }
  106. static ssize_t ic_clk_show(struct device *dev, struct device_attribute *attr,
  107. char *buf)
  108. {
  109. struct hl_device *hdev = dev_get_drvdata(dev);
  110. long value;
  111. if (!hl_device_operational(hdev, NULL))
  112. return -ENODEV;
  113. value = hl_fw_get_frequency(hdev, HL_GOYA_IC_PLL, false);
  114. if (value < 0)
  115. return value;
  116. return sprintf(buf, "%lu\n", value);
  117. }
  118. static ssize_t ic_clk_store(struct device *dev, struct device_attribute *attr,
  119. const char *buf, size_t count)
  120. {
  121. struct hl_device *hdev = dev_get_drvdata(dev);
  122. struct goya_device *goya = hdev->asic_specific;
  123. int rc;
  124. long value;
  125. if (!hl_device_operational(hdev, NULL)) {
  126. count = -ENODEV;
  127. goto fail;
  128. }
  129. if (goya->pm_mng_profile == PM_AUTO) {
  130. count = -EPERM;
  131. goto fail;
  132. }
  133. rc = kstrtoul(buf, 0, &value);
  134. if (rc) {
  135. count = -EINVAL;
  136. goto fail;
  137. }
  138. hl_fw_set_frequency(hdev, HL_GOYA_IC_PLL, value);
  139. goya->ic_clk = value;
  140. fail:
  141. return count;
  142. }
  143. static ssize_t mme_clk_curr_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct hl_device *hdev = dev_get_drvdata(dev);
  147. long value;
  148. if (!hl_device_operational(hdev, NULL))
  149. return -ENODEV;
  150. value = hl_fw_get_frequency(hdev, HL_GOYA_MME_PLL, true);
  151. if (value < 0)
  152. return value;
  153. return sprintf(buf, "%lu\n", value);
  154. }
  155. static ssize_t tpc_clk_curr_show(struct device *dev,
  156. struct device_attribute *attr, char *buf)
  157. {
  158. struct hl_device *hdev = dev_get_drvdata(dev);
  159. long value;
  160. if (!hl_device_operational(hdev, NULL))
  161. return -ENODEV;
  162. value = hl_fw_get_frequency(hdev, HL_GOYA_TPC_PLL, true);
  163. if (value < 0)
  164. return value;
  165. return sprintf(buf, "%lu\n", value);
  166. }
  167. static ssize_t ic_clk_curr_show(struct device *dev,
  168. struct device_attribute *attr, char *buf)
  169. {
  170. struct hl_device *hdev = dev_get_drvdata(dev);
  171. long value;
  172. if (!hl_device_operational(hdev, NULL))
  173. return -ENODEV;
  174. value = hl_fw_get_frequency(hdev, HL_GOYA_IC_PLL, true);
  175. if (value < 0)
  176. return value;
  177. return sprintf(buf, "%lu\n", value);
  178. }
  179. static ssize_t pm_mng_profile_show(struct device *dev,
  180. struct device_attribute *attr, char *buf)
  181. {
  182. struct hl_device *hdev = dev_get_drvdata(dev);
  183. struct goya_device *goya = hdev->asic_specific;
  184. if (!hl_device_operational(hdev, NULL))
  185. return -ENODEV;
  186. return sprintf(buf, "%s\n",
  187. (goya->pm_mng_profile == PM_AUTO) ? "auto" :
  188. (goya->pm_mng_profile == PM_MANUAL) ? "manual" :
  189. "unknown");
  190. }
  191. static ssize_t pm_mng_profile_store(struct device *dev,
  192. struct device_attribute *attr, const char *buf, size_t count)
  193. {
  194. struct hl_device *hdev = dev_get_drvdata(dev);
  195. struct goya_device *goya = hdev->asic_specific;
  196. if (!hl_device_operational(hdev, NULL)) {
  197. count = -ENODEV;
  198. goto out;
  199. }
  200. mutex_lock(&hdev->fpriv_list_lock);
  201. if (hdev->is_compute_ctx_active) {
  202. dev_err(hdev->dev,
  203. "Can't change PM profile while compute context is opened on the device\n");
  204. count = -EPERM;
  205. goto unlock_mutex;
  206. }
  207. if (strncmp("auto", buf, strlen("auto")) == 0) {
  208. /* Make sure we are in LOW PLL when changing modes */
  209. if (goya->pm_mng_profile == PM_MANUAL) {
  210. goya->curr_pll_profile = PLL_HIGH;
  211. goya->pm_mng_profile = PM_AUTO;
  212. goya_set_frequency(hdev, PLL_LOW);
  213. }
  214. } else if (strncmp("manual", buf, strlen("manual")) == 0) {
  215. if (goya->pm_mng_profile == PM_AUTO) {
  216. /* Must release the lock because the work thread also
  217. * takes this lock. But before we release it, set
  218. * the mode to manual so nothing will change if a user
  219. * suddenly opens the device
  220. */
  221. goya->pm_mng_profile = PM_MANUAL;
  222. mutex_unlock(&hdev->fpriv_list_lock);
  223. /* Flush the current work so we can return to the user
  224. * knowing that he is the only one changing frequencies
  225. */
  226. if (goya->goya_work)
  227. flush_delayed_work(&goya->goya_work->work_freq);
  228. return count;
  229. }
  230. } else {
  231. dev_err(hdev->dev, "value should be auto or manual\n");
  232. count = -EINVAL;
  233. }
  234. unlock_mutex:
  235. mutex_unlock(&hdev->fpriv_list_lock);
  236. out:
  237. return count;
  238. }
  239. static ssize_t high_pll_show(struct device *dev, struct device_attribute *attr,
  240. char *buf)
  241. {
  242. struct hl_device *hdev = dev_get_drvdata(dev);
  243. if (!hl_device_operational(hdev, NULL))
  244. return -ENODEV;
  245. return sprintf(buf, "%u\n", hdev->high_pll);
  246. }
  247. static ssize_t high_pll_store(struct device *dev, struct device_attribute *attr,
  248. const char *buf, size_t count)
  249. {
  250. struct hl_device *hdev = dev_get_drvdata(dev);
  251. long value;
  252. int rc;
  253. if (!hl_device_operational(hdev, NULL)) {
  254. count = -ENODEV;
  255. goto out;
  256. }
  257. rc = kstrtoul(buf, 0, &value);
  258. if (rc) {
  259. count = -EINVAL;
  260. goto out;
  261. }
  262. hdev->high_pll = value;
  263. out:
  264. return count;
  265. }
  266. static DEVICE_ATTR_RW(high_pll);
  267. static DEVICE_ATTR_RW(ic_clk);
  268. static DEVICE_ATTR_RO(ic_clk_curr);
  269. static DEVICE_ATTR_RW(mme_clk);
  270. static DEVICE_ATTR_RO(mme_clk_curr);
  271. static DEVICE_ATTR_RW(pm_mng_profile);
  272. static DEVICE_ATTR_RW(tpc_clk);
  273. static DEVICE_ATTR_RO(tpc_clk_curr);
  274. static struct attribute *goya_clk_dev_attrs[] = {
  275. &dev_attr_high_pll.attr,
  276. &dev_attr_ic_clk.attr,
  277. &dev_attr_ic_clk_curr.attr,
  278. &dev_attr_mme_clk.attr,
  279. &dev_attr_mme_clk_curr.attr,
  280. &dev_attr_pm_mng_profile.attr,
  281. &dev_attr_tpc_clk.attr,
  282. &dev_attr_tpc_clk_curr.attr,
  283. NULL,
  284. };
  285. static ssize_t infineon_ver_show(struct device *dev, struct device_attribute *attr, char *buf)
  286. {
  287. struct hl_device *hdev = dev_get_drvdata(dev);
  288. struct cpucp_info *cpucp_info;
  289. cpucp_info = &hdev->asic_prop.cpucp_info;
  290. return sprintf(buf, "%#04x\n", le32_to_cpu(cpucp_info->infineon_version));
  291. }
  292. static DEVICE_ATTR_RO(infineon_ver);
  293. static struct attribute *goya_vrm_dev_attrs[] = {
  294. &dev_attr_infineon_ver.attr,
  295. NULL,
  296. };
  297. void goya_add_device_attr(struct hl_device *hdev, struct attribute_group *dev_clk_attr_grp,
  298. struct attribute_group *dev_vrm_attr_grp)
  299. {
  300. dev_clk_attr_grp->attrs = goya_clk_dev_attrs;
  301. dev_vrm_attr_grp->attrs = goya_vrm_dev_attrs;
  302. }