nouveau_pm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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, u8 id, u32 khz)
  31. {
  32. struct drm_nouveau_private *dev_priv = dev->dev_private;
  33. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  34. void *pre_state;
  35. if (khz == 0)
  36. return 0;
  37. pre_state = pm->clock_pre(dev, id, khz);
  38. if (IS_ERR(pre_state))
  39. return PTR_ERR(pre_state);
  40. if (pre_state)
  41. pm->clock_set(dev, pre_state);
  42. return 0;
  43. }
  44. static int
  45. nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  46. {
  47. struct drm_nouveau_private *dev_priv = dev->dev_private;
  48. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  49. int ret;
  50. if (perflvl == pm->cur)
  51. return 0;
  52. if (pm->voltage.supported && pm->voltage_set && perflvl->voltage) {
  53. ret = pm->voltage_set(dev, perflvl->voltage);
  54. if (ret) {
  55. NV_ERROR(dev, "voltage_set %d failed: %d\n",
  56. perflvl->voltage, ret);
  57. }
  58. }
  59. nouveau_pm_clock_set(dev, PLL_CORE, perflvl->core);
  60. nouveau_pm_clock_set(dev, PLL_SHADER, perflvl->shader);
  61. nouveau_pm_clock_set(dev, PLL_MEMORY, perflvl->memory);
  62. nouveau_pm_clock_set(dev, PLL_UNK05, perflvl->unk05);
  63. pm->cur = perflvl;
  64. return 0;
  65. }
  66. static int
  67. nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
  68. {
  69. struct drm_nouveau_private *dev_priv = dev->dev_private;
  70. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  71. struct nouveau_pm_level *perflvl = NULL;
  72. /* safety precaution, for now */
  73. if (nouveau_perflvl_wr != 7777)
  74. return -EPERM;
  75. if (!pm->clock_set)
  76. return -EINVAL;
  77. if (!strncmp(profile, "boot", 4))
  78. perflvl = &pm->boot;
  79. else {
  80. int pl = simple_strtol(profile, NULL, 10);
  81. int i;
  82. for (i = 0; i < pm->nr_perflvl; i++) {
  83. if (pm->perflvl[i].id == pl) {
  84. perflvl = &pm->perflvl[i];
  85. break;
  86. }
  87. }
  88. if (!perflvl)
  89. return -EINVAL;
  90. }
  91. NV_INFO(dev, "setting performance level: %s\n", profile);
  92. return nouveau_pm_perflvl_set(dev, perflvl);
  93. }
  94. static int
  95. nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  96. {
  97. struct drm_nouveau_private *dev_priv = dev->dev_private;
  98. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  99. int ret;
  100. if (!pm->clock_get)
  101. return -EINVAL;
  102. memset(perflvl, 0, sizeof(*perflvl));
  103. ret = pm->clock_get(dev, PLL_CORE);
  104. if (ret > 0)
  105. perflvl->core = ret;
  106. ret = pm->clock_get(dev, PLL_MEMORY);
  107. if (ret > 0)
  108. perflvl->memory = ret;
  109. ret = pm->clock_get(dev, PLL_SHADER);
  110. if (ret > 0)
  111. perflvl->shader = ret;
  112. ret = pm->clock_get(dev, PLL_UNK05);
  113. if (ret > 0)
  114. perflvl->unk05 = ret;
  115. if (pm->voltage.supported && pm->voltage_get) {
  116. ret = pm->voltage_get(dev);
  117. if (ret > 0)
  118. perflvl->voltage = ret;
  119. }
  120. return 0;
  121. }
  122. static void
  123. nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
  124. {
  125. char c[16], s[16], v[16], f[16];
  126. c[0] = '\0';
  127. if (perflvl->core)
  128. snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
  129. s[0] = '\0';
  130. if (perflvl->shader)
  131. snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
  132. v[0] = '\0';
  133. if (perflvl->voltage)
  134. snprintf(v, sizeof(v), " voltage %dmV", perflvl->voltage * 10);
  135. f[0] = '\0';
  136. if (perflvl->fanspeed)
  137. snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
  138. snprintf(ptr, len, "memory %dMHz%s%s%s%s\n", perflvl->memory / 1000,
  139. c, s, v, f);
  140. }
  141. static ssize_t
  142. nouveau_pm_get_perflvl_info(struct device *d,
  143. struct device_attribute *a, char *buf)
  144. {
  145. struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
  146. char *ptr = buf;
  147. int len = PAGE_SIZE;
  148. snprintf(ptr, len, "%d: ", perflvl->id);
  149. ptr += strlen(buf);
  150. len -= strlen(buf);
  151. nouveau_pm_perflvl_info(perflvl, ptr, len);
  152. return strlen(buf);
  153. }
  154. static ssize_t
  155. nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
  156. {
  157. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  158. struct drm_nouveau_private *dev_priv = dev->dev_private;
  159. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  160. struct nouveau_pm_level cur;
  161. int len = PAGE_SIZE, ret;
  162. char *ptr = buf;
  163. if (!pm->cur)
  164. snprintf(ptr, len, "setting: boot\n");
  165. else if (pm->cur == &pm->boot)
  166. snprintf(ptr, len, "setting: boot\nc: ");
  167. else
  168. snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
  169. ptr += strlen(buf);
  170. len -= strlen(buf);
  171. ret = nouveau_pm_perflvl_get(dev, &cur);
  172. if (ret == 0)
  173. nouveau_pm_perflvl_info(&cur, ptr, len);
  174. return strlen(buf);
  175. }
  176. static ssize_t
  177. nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
  178. const char *buf, size_t count)
  179. {
  180. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  181. int ret;
  182. ret = nouveau_pm_profile_set(dev, buf);
  183. if (ret)
  184. return ret;
  185. return strlen(buf);
  186. }
  187. static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
  188. nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
  189. static int
  190. nouveau_sysfs_init(struct drm_device *dev)
  191. {
  192. struct drm_nouveau_private *dev_priv = dev->dev_private;
  193. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  194. struct device *d = &dev->pdev->dev;
  195. int ret, i;
  196. ret = device_create_file(d, &dev_attr_performance_level);
  197. if (ret)
  198. return ret;
  199. for (i = 0; i < pm->nr_perflvl; i++) {
  200. struct nouveau_pm_level *perflvl = &pm->perflvl[i];
  201. perflvl->dev_attr.attr.name = perflvl->name;
  202. perflvl->dev_attr.attr.mode = S_IRUGO;
  203. perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
  204. perflvl->dev_attr.store = NULL;
  205. sysfs_attr_init(&perflvl->dev_attr.attr);
  206. ret = device_create_file(d, &perflvl->dev_attr);
  207. if (ret) {
  208. NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
  209. perflvl->id, i);
  210. perflvl->dev_attr.attr.name = NULL;
  211. nouveau_pm_fini(dev);
  212. return ret;
  213. }
  214. }
  215. return 0;
  216. }
  217. static void
  218. nouveau_sysfs_fini(struct drm_device *dev)
  219. {
  220. struct drm_nouveau_private *dev_priv = dev->dev_private;
  221. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  222. struct device *d = &dev->pdev->dev;
  223. int i;
  224. device_remove_file(d, &dev_attr_performance_level);
  225. for (i = 0; i < pm->nr_perflvl; i++) {
  226. struct nouveau_pm_level *pl = &pm->perflvl[i];
  227. if (!pl->dev_attr.attr.name)
  228. break;
  229. device_remove_file(d, &pl->dev_attr);
  230. }
  231. }
  232. static ssize_t
  233. nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
  234. {
  235. struct drm_device *dev = dev_get_drvdata(d);
  236. struct drm_nouveau_private *dev_priv = dev->dev_private;
  237. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  238. return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
  239. }
  240. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  241. NULL, 0);
  242. static ssize_t
  243. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  244. {
  245. struct drm_device *dev = dev_get_drvdata(d);
  246. struct drm_nouveau_private *dev_priv = dev->dev_private;
  247. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  248. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  249. return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
  250. }
  251. static ssize_t
  252. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  253. const char *buf, size_t count)
  254. {
  255. struct drm_device *dev = dev_get_drvdata(d);
  256. struct drm_nouveau_private *dev_priv = dev->dev_private;
  257. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  258. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  259. long value;
  260. if (strict_strtol(buf, 10, &value) == -EINVAL)
  261. return count;
  262. temp->down_clock = value/1000;
  263. nouveau_temp_safety_checks(dev);
  264. return count;
  265. }
  266. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  267. nouveau_hwmon_set_max_temp,
  268. 0);
  269. static ssize_t
  270. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  271. char *buf)
  272. {
  273. struct drm_device *dev = dev_get_drvdata(d);
  274. struct drm_nouveau_private *dev_priv = dev->dev_private;
  275. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  276. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  277. return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
  278. }
  279. static ssize_t
  280. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  281. const char *buf,
  282. size_t count)
  283. {
  284. struct drm_device *dev = dev_get_drvdata(d);
  285. struct drm_nouveau_private *dev_priv = dev->dev_private;
  286. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  287. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  288. long value;
  289. if (strict_strtol(buf, 10, &value) == -EINVAL)
  290. return count;
  291. temp->critical = value/1000;
  292. nouveau_temp_safety_checks(dev);
  293. return count;
  294. }
  295. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  296. nouveau_hwmon_critical_temp,
  297. nouveau_hwmon_set_critical_temp,
  298. 0);
  299. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  300. struct device_attribute *attr,
  301. char *buf)
  302. {
  303. return sprintf(buf, "nouveau\n");
  304. }
  305. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  306. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  307. struct device_attribute *attr,
  308. char *buf)
  309. {
  310. return sprintf(buf, "1000\n");
  311. }
  312. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  313. nouveau_hwmon_show_update_rate,
  314. NULL, 0);
  315. static struct attribute *hwmon_attributes[] = {
  316. &sensor_dev_attr_temp1_input.dev_attr.attr,
  317. &sensor_dev_attr_temp1_max.dev_attr.attr,
  318. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  319. &sensor_dev_attr_name.dev_attr.attr,
  320. &sensor_dev_attr_update_rate.dev_attr.attr,
  321. NULL
  322. };
  323. static const struct attribute_group hwmon_attrgroup = {
  324. .attrs = hwmon_attributes,
  325. };
  326. static int
  327. nouveau_hwmon_init(struct drm_device *dev)
  328. {
  329. struct drm_nouveau_private *dev_priv = dev->dev_private;
  330. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  331. struct device *hwmon_dev;
  332. int ret;
  333. if (!pm->temp_get)
  334. return -ENODEV;
  335. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  336. if (IS_ERR(hwmon_dev)) {
  337. ret = PTR_ERR(hwmon_dev);
  338. NV_ERROR(dev,
  339. "Unable to register hwmon device: %d\n", ret);
  340. return ret;
  341. }
  342. dev_set_drvdata(hwmon_dev, dev);
  343. ret = sysfs_create_group(&hwmon_dev->kobj,
  344. &hwmon_attrgroup);
  345. if (ret) {
  346. NV_ERROR(dev,
  347. "Unable to create hwmon sysfs file: %d\n", ret);
  348. hwmon_device_unregister(hwmon_dev);
  349. return ret;
  350. }
  351. pm->hwmon = hwmon_dev;
  352. return 0;
  353. }
  354. static void
  355. nouveau_hwmon_fini(struct drm_device *dev)
  356. {
  357. struct drm_nouveau_private *dev_priv = dev->dev_private;
  358. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  359. if (pm->hwmon) {
  360. sysfs_remove_group(&pm->hwmon->kobj, &hwmon_attrgroup);
  361. hwmon_device_unregister(pm->hwmon);
  362. }
  363. }
  364. int
  365. nouveau_pm_init(struct drm_device *dev)
  366. {
  367. struct drm_nouveau_private *dev_priv = dev->dev_private;
  368. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  369. char info[256];
  370. int ret, i;
  371. nouveau_volt_init(dev);
  372. nouveau_perf_init(dev);
  373. nouveau_temp_init(dev);
  374. nouveau_mem_timing_init(dev);
  375. NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
  376. for (i = 0; i < pm->nr_perflvl; i++) {
  377. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  378. NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
  379. }
  380. /* determine current ("boot") performance level */
  381. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  382. if (ret == 0) {
  383. pm->cur = &pm->boot;
  384. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  385. NV_INFO(dev, "c: %s", info);
  386. }
  387. /* switch performance levels now if requested */
  388. if (nouveau_perflvl != NULL) {
  389. ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
  390. if (ret) {
  391. NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
  392. nouveau_perflvl, ret);
  393. }
  394. }
  395. nouveau_sysfs_init(dev);
  396. nouveau_hwmon_init(dev);
  397. return 0;
  398. }
  399. void
  400. nouveau_pm_fini(struct drm_device *dev)
  401. {
  402. struct drm_nouveau_private *dev_priv = dev->dev_private;
  403. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  404. if (pm->cur != &pm->boot)
  405. nouveau_pm_perflvl_set(dev, &pm->boot);
  406. nouveau_mem_timing_fini(dev);
  407. nouveau_temp_fini(dev);
  408. nouveau_perf_fini(dev);
  409. nouveau_volt_fini(dev);
  410. nouveau_hwmon_fini(dev);
  411. nouveau_sysfs_fini(dev);
  412. }
  413. void
  414. nouveau_pm_resume(struct drm_device *dev)
  415. {
  416. struct drm_nouveau_private *dev_priv = dev->dev_private;
  417. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  418. struct nouveau_pm_level *perflvl;
  419. if (pm->cur == &pm->boot)
  420. return;
  421. perflvl = pm->cur;
  422. pm->cur = &pm->boot;
  423. nouveau_pm_perflvl_set(dev, perflvl);
  424. }