nouveau_pm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Copyright 2010 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "drmP.h"
  25. #include "nouveau_drv.h"
  26. #include "nouveau_pm.h"
  27. #include <linux/hwmon.h>
  28. #include <linux/hwmon-sysfs.h>
  29. static int
  30. nouveau_pm_clock_set(struct drm_device *dev, struct nouveau_pm_level *perflvl,
  31. u8 id, u32 khz)
  32. {
  33. struct drm_nouveau_private *dev_priv = dev->dev_private;
  34. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  35. void *pre_state;
  36. if (khz == 0)
  37. return 0;
  38. pre_state = pm->clock_pre(dev, perflvl, id, khz);
  39. if (IS_ERR(pre_state))
  40. return PTR_ERR(pre_state);
  41. if (pre_state)
  42. pm->clock_set(dev, pre_state);
  43. return 0;
  44. }
  45. static int
  46. nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  47. {
  48. struct drm_nouveau_private *dev_priv = dev->dev_private;
  49. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  50. int ret;
  51. if (perflvl == pm->cur)
  52. return 0;
  53. if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
  54. ret = pm->voltage_set(dev, perflvl->voltage);
  55. if (ret) {
  56. NV_ERROR(dev, "voltage_set %d failed: %d\n",
  57. perflvl->voltage, ret);
  58. }
  59. }
  60. nouveau_pm_clock_set(dev, perflvl, PLL_CORE, perflvl->core);
  61. nouveau_pm_clock_set(dev, perflvl, PLL_SHADER, perflvl->shader);
  62. nouveau_pm_clock_set(dev, perflvl, PLL_MEMORY, perflvl->memory);
  63. nouveau_pm_clock_set(dev, perflvl, PLL_UNK05, perflvl->unk05);
  64. pm->cur = perflvl;
  65. return 0;
  66. }
  67. static int
  68. nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
  69. {
  70. struct drm_nouveau_private *dev_priv = dev->dev_private;
  71. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  72. struct nouveau_pm_level *perflvl = NULL;
  73. /* safety precaution, for now */
  74. if (nouveau_perflvl_wr != 7777)
  75. return -EPERM;
  76. if (!pm->clock_set)
  77. return -EINVAL;
  78. if (!strncmp(profile, "boot", 4))
  79. perflvl = &pm->boot;
  80. else {
  81. int pl = simple_strtol(profile, NULL, 10);
  82. int i;
  83. for (i = 0; i < pm->nr_perflvl; i++) {
  84. if (pm->perflvl[i].id == pl) {
  85. perflvl = &pm->perflvl[i];
  86. break;
  87. }
  88. }
  89. if (!perflvl)
  90. return -EINVAL;
  91. }
  92. NV_INFO(dev, "setting performance level: %s\n", profile);
  93. return nouveau_pm_perflvl_set(dev, perflvl);
  94. }
  95. static int
  96. nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  97. {
  98. struct drm_nouveau_private *dev_priv = dev->dev_private;
  99. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  100. int ret;
  101. if (!pm->clock_get)
  102. return -EINVAL;
  103. memset(perflvl, 0, sizeof(*perflvl));
  104. ret = pm->clock_get(dev, PLL_CORE);
  105. if (ret > 0)
  106. perflvl->core = ret;
  107. ret = pm->clock_get(dev, PLL_MEMORY);
  108. if (ret > 0)
  109. perflvl->memory = ret;
  110. ret = pm->clock_get(dev, PLL_SHADER);
  111. if (ret > 0)
  112. perflvl->shader = ret;
  113. ret = pm->clock_get(dev, PLL_UNK05);
  114. if (ret > 0)
  115. perflvl->unk05 = ret;
  116. if (pm->voltage.supported && pm->voltage_get) {
  117. ret = pm->voltage_get(dev);
  118. if (ret > 0)
  119. perflvl->voltage = ret;
  120. }
  121. return 0;
  122. }
  123. static void
  124. nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
  125. {
  126. char c[16], s[16], v[16], f[16];
  127. c[0] = '\0';
  128. if (perflvl->core)
  129. snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
  130. s[0] = '\0';
  131. if (perflvl->shader)
  132. snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
  133. v[0] = '\0';
  134. if (perflvl->voltage)
  135. snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
  136. f[0] = '\0';
  137. if (perflvl->fanspeed)
  138. snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
  139. snprintf(ptr, len, "memory %dMHz%s%s%s%s\n", perflvl->memory / 1000,
  140. c, s, v, f);
  141. }
  142. static ssize_t
  143. nouveau_pm_get_perflvl_info(struct device *d,
  144. struct device_attribute *a, char *buf)
  145. {
  146. struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
  147. char *ptr = buf;
  148. int len = PAGE_SIZE;
  149. snprintf(ptr, len, "%d: ", perflvl->id);
  150. ptr += strlen(buf);
  151. len -= strlen(buf);
  152. nouveau_pm_perflvl_info(perflvl, ptr, len);
  153. return strlen(buf);
  154. }
  155. static ssize_t
  156. nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
  157. {
  158. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  159. struct drm_nouveau_private *dev_priv = dev->dev_private;
  160. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  161. struct nouveau_pm_level cur;
  162. int len = PAGE_SIZE, ret;
  163. char *ptr = buf;
  164. if (!pm->cur)
  165. snprintf(ptr, len, "setting: boot\n");
  166. else if (pm->cur == &pm->boot)
  167. snprintf(ptr, len, "setting: boot\nc: ");
  168. else
  169. snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
  170. ptr += strlen(buf);
  171. len -= strlen(buf);
  172. ret = nouveau_pm_perflvl_get(dev, &cur);
  173. if (ret == 0)
  174. nouveau_pm_perflvl_info(&cur, ptr, len);
  175. return strlen(buf);
  176. }
  177. static ssize_t
  178. nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
  179. const char *buf, size_t count)
  180. {
  181. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  182. int ret;
  183. ret = nouveau_pm_profile_set(dev, buf);
  184. if (ret)
  185. return ret;
  186. return strlen(buf);
  187. }
  188. static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
  189. nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
  190. static int
  191. nouveau_sysfs_init(struct drm_device *dev)
  192. {
  193. struct drm_nouveau_private *dev_priv = dev->dev_private;
  194. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  195. struct device *d = &dev->pdev->dev;
  196. int ret, i;
  197. ret = device_create_file(d, &dev_attr_performance_level);
  198. if (ret)
  199. return ret;
  200. for (i = 0; i < pm->nr_perflvl; i++) {
  201. struct nouveau_pm_level *perflvl = &pm->perflvl[i];
  202. perflvl->dev_attr.attr.name = perflvl->name;
  203. perflvl->dev_attr.attr.mode = S_IRUGO;
  204. perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
  205. perflvl->dev_attr.store = NULL;
  206. sysfs_attr_init(&perflvl->dev_attr.attr);
  207. ret = device_create_file(d, &perflvl->dev_attr);
  208. if (ret) {
  209. NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
  210. perflvl->id, i);
  211. perflvl->dev_attr.attr.name = NULL;
  212. nouveau_pm_fini(dev);
  213. return ret;
  214. }
  215. }
  216. return 0;
  217. }
  218. static void
  219. nouveau_sysfs_fini(struct drm_device *dev)
  220. {
  221. struct drm_nouveau_private *dev_priv = dev->dev_private;
  222. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  223. struct device *d = &dev->pdev->dev;
  224. int i;
  225. device_remove_file(d, &dev_attr_performance_level);
  226. for (i = 0; i < pm->nr_perflvl; i++) {
  227. struct nouveau_pm_level *pl = &pm->perflvl[i];
  228. if (!pl->dev_attr.attr.name)
  229. break;
  230. device_remove_file(d, &pl->dev_attr);
  231. }
  232. }
  233. #ifdef CONFIG_HWMON
  234. static ssize_t
  235. nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
  236. {
  237. struct drm_device *dev = dev_get_drvdata(d);
  238. struct drm_nouveau_private *dev_priv = dev->dev_private;
  239. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  240. return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
  241. }
  242. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  243. NULL, 0);
  244. static ssize_t
  245. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  246. {
  247. struct drm_device *dev = dev_get_drvdata(d);
  248. struct drm_nouveau_private *dev_priv = dev->dev_private;
  249. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  250. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  251. return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
  252. }
  253. static ssize_t
  254. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  255. const char *buf, size_t count)
  256. {
  257. struct drm_device *dev = dev_get_drvdata(d);
  258. struct drm_nouveau_private *dev_priv = dev->dev_private;
  259. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  260. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  261. long value;
  262. if (strict_strtol(buf, 10, &value) == -EINVAL)
  263. return count;
  264. temp->down_clock = value/1000;
  265. nouveau_temp_safety_checks(dev);
  266. return count;
  267. }
  268. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  269. nouveau_hwmon_set_max_temp,
  270. 0);
  271. static ssize_t
  272. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  273. char *buf)
  274. {
  275. struct drm_device *dev = dev_get_drvdata(d);
  276. struct drm_nouveau_private *dev_priv = dev->dev_private;
  277. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  278. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  279. return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
  280. }
  281. static ssize_t
  282. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  283. const char *buf,
  284. size_t count)
  285. {
  286. struct drm_device *dev = dev_get_drvdata(d);
  287. struct drm_nouveau_private *dev_priv = dev->dev_private;
  288. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  289. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  290. long value;
  291. if (strict_strtol(buf, 10, &value) == -EINVAL)
  292. return count;
  293. temp->critical = value/1000;
  294. nouveau_temp_safety_checks(dev);
  295. return count;
  296. }
  297. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  298. nouveau_hwmon_critical_temp,
  299. nouveau_hwmon_set_critical_temp,
  300. 0);
  301. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  302. struct device_attribute *attr,
  303. char *buf)
  304. {
  305. return sprintf(buf, "nouveau\n");
  306. }
  307. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  308. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  309. struct device_attribute *attr,
  310. char *buf)
  311. {
  312. return sprintf(buf, "1000\n");
  313. }
  314. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  315. nouveau_hwmon_show_update_rate,
  316. NULL, 0);
  317. static struct attribute *hwmon_attributes[] = {
  318. &sensor_dev_attr_temp1_input.dev_attr.attr,
  319. &sensor_dev_attr_temp1_max.dev_attr.attr,
  320. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  321. &sensor_dev_attr_name.dev_attr.attr,
  322. &sensor_dev_attr_update_rate.dev_attr.attr,
  323. NULL
  324. };
  325. static const struct attribute_group hwmon_attrgroup = {
  326. .attrs = hwmon_attributes,
  327. };
  328. #endif
  329. static int
  330. nouveau_hwmon_init(struct drm_device *dev)
  331. {
  332. #ifdef CONFIG_HWMON
  333. struct drm_nouveau_private *dev_priv = dev->dev_private;
  334. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  335. struct device *hwmon_dev;
  336. int ret;
  337. if (!pm->temp_get)
  338. return -ENODEV;
  339. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  340. if (IS_ERR(hwmon_dev)) {
  341. ret = PTR_ERR(hwmon_dev);
  342. NV_ERROR(dev,
  343. "Unable to register hwmon device: %d\n", ret);
  344. return ret;
  345. }
  346. dev_set_drvdata(hwmon_dev, dev);
  347. ret = sysfs_create_group(&hwmon_dev->kobj,
  348. &hwmon_attrgroup);
  349. if (ret) {
  350. NV_ERROR(dev,
  351. "Unable to create hwmon sysfs file: %d\n", ret);
  352. hwmon_device_unregister(hwmon_dev);
  353. return ret;
  354. }
  355. pm->hwmon = hwmon_dev;
  356. #endif
  357. return 0;
  358. }
  359. static void
  360. nouveau_hwmon_fini(struct drm_device *dev)
  361. {
  362. #ifdef CONFIG_HWMON
  363. struct drm_nouveau_private *dev_priv = dev->dev_private;
  364. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  365. if (pm->hwmon) {
  366. sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
  367. hwmon_device_unregister(pm->hwmon);
  368. }
  369. #endif
  370. }
  371. int
  372. nouveau_pm_init(struct drm_device *dev)
  373. {
  374. struct drm_nouveau_private *dev_priv = dev->dev_private;
  375. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  376. char info[256];
  377. int ret, i;
  378. nouveau_volt_init(dev);
  379. nouveau_perf_init(dev);
  380. nouveau_temp_init(dev);
  381. nouveau_mem_timing_init(dev);
  382. NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
  383. for (i = 0; i < pm->nr_perflvl; i++) {
  384. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  385. NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
  386. }
  387. /* determine current ("boot") performance level */
  388. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  389. if (ret == 0) {
  390. pm->cur = &pm->boot;
  391. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  392. NV_INFO(dev, "c: %s", info);
  393. }
  394. /* switch performance levels now if requested */
  395. if (nouveau_perflvl != NULL) {
  396. ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
  397. if (ret) {
  398. NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
  399. nouveau_perflvl, ret);
  400. }
  401. }
  402. nouveau_sysfs_init(dev);
  403. nouveau_hwmon_init(dev);
  404. return 0;
  405. }
  406. void
  407. nouveau_pm_fini(struct drm_device *dev)
  408. {
  409. struct drm_nouveau_private *dev_priv = dev->dev_private;
  410. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  411. if (pm->cur != &pm->boot)
  412. nouveau_pm_perflvl_set(dev, &pm->boot);
  413. nouveau_mem_timing_fini(dev);
  414. nouveau_temp_fini(dev);
  415. nouveau_perf_fini(dev);
  416. nouveau_volt_fini(dev);
  417. nouveau_hwmon_fini(dev);
  418. nouveau_sysfs_fini(dev);
  419. }
  420. void
  421. nouveau_pm_resume(struct drm_device *dev)
  422. {
  423. struct drm_nouveau_private *dev_priv = dev->dev_private;
  424. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  425. struct nouveau_pm_level *perflvl;
  426. if (pm->cur == &pm->boot)
  427. return;
  428. perflvl = pm->cur;
  429. pm->cur = &pm->boot;
  430. nouveau_pm_perflvl_set(dev, perflvl);
  431. }