nouveau_pm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. 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. return snprintf(buf, PAGE_SIZE, "%d\n", nouveau_temp_get(dev)*1000);
  237. }
  238. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  239. NULL, 0);
  240. static ssize_t
  241. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  242. {
  243. struct drm_device *dev = dev_get_drvdata(d);
  244. struct drm_nouveau_private *dev_priv = dev->dev_private;
  245. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  246. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  247. return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
  248. }
  249. static ssize_t
  250. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  251. const char *buf, size_t count)
  252. {
  253. struct drm_device *dev = dev_get_drvdata(d);
  254. struct drm_nouveau_private *dev_priv = dev->dev_private;
  255. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  256. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  257. long value;
  258. if (strict_strtoul(buf, 10, &value) == -EINVAL)
  259. return count;
  260. temp->down_clock = value/1000;
  261. nouveau_temp_safety_checks(dev);
  262. return count;
  263. }
  264. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  265. nouveau_hwmon_set_max_temp,
  266. 0);
  267. static ssize_t
  268. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  269. char *buf)
  270. {
  271. struct drm_device *dev = dev_get_drvdata(d);
  272. struct drm_nouveau_private *dev_priv = dev->dev_private;
  273. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  274. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  275. return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
  276. }
  277. static ssize_t
  278. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  279. const char *buf,
  280. size_t count)
  281. {
  282. struct drm_device *dev = dev_get_drvdata(d);
  283. struct drm_nouveau_private *dev_priv = dev->dev_private;
  284. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  285. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  286. long value;
  287. if (strict_strtoul(buf, 10, &value) == -EINVAL)
  288. return count;
  289. temp->critical = value/1000;
  290. nouveau_temp_safety_checks(dev);
  291. return count;
  292. }
  293. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  294. nouveau_hwmon_critical_temp,
  295. nouveau_hwmon_set_critical_temp,
  296. 0);
  297. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  298. struct device_attribute *attr,
  299. char *buf)
  300. {
  301. return sprintf(buf, "nouveau\n");
  302. }
  303. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  304. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  305. struct device_attribute *attr,
  306. char *buf)
  307. {
  308. return sprintf(buf, "1000\n");
  309. }
  310. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  311. nouveau_hwmon_show_update_rate,
  312. NULL, 0);
  313. static struct attribute *hwmon_attributes[] = {
  314. &sensor_dev_attr_temp1_input.dev_attr.attr,
  315. &sensor_dev_attr_temp1_max.dev_attr.attr,
  316. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  317. &sensor_dev_attr_name.dev_attr.attr,
  318. &sensor_dev_attr_update_rate.dev_attr.attr,
  319. NULL
  320. };
  321. static const struct attribute_group hwmon_attrgroup = {
  322. .attrs = hwmon_attributes,
  323. };
  324. static int
  325. nouveau_hwmon_init(struct drm_device *dev)
  326. {
  327. struct drm_nouveau_private *dev_priv = dev->dev_private;
  328. struct device *hwmon_dev;
  329. int ret;
  330. dev_priv->int_hwmon_dev = NULL;
  331. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  332. if (IS_ERR(hwmon_dev)) {
  333. ret = PTR_ERR(hwmon_dev);
  334. NV_ERROR(dev,
  335. "Unable to register hwmon device: %d\n", ret);
  336. return ret;
  337. }
  338. dev_set_drvdata(hwmon_dev, dev);
  339. ret = sysfs_create_group(&hwmon_dev->kobj,
  340. &hwmon_attrgroup);
  341. if (ret) {
  342. NV_ERROR(dev,
  343. "Unable to create hwmon sysfs file: %d\n", ret);
  344. hwmon_device_unregister(hwmon_dev);
  345. return ret;
  346. }
  347. dev_priv->int_hwmon_dev = hwmon_dev;
  348. return 0;
  349. }
  350. static void
  351. nouveau_hwmon_fini(struct drm_device *dev)
  352. {
  353. struct drm_nouveau_private *dev_priv = dev->dev_private;
  354. if (dev_priv->int_hwmon_dev) {
  355. sysfs_remove_group(&dev_priv->int_hwmon_dev->kobj,
  356. &hwmon_attrgroup);
  357. hwmon_device_unregister(dev_priv->int_hwmon_dev);
  358. }
  359. }
  360. int
  361. nouveau_pm_init(struct drm_device *dev)
  362. {
  363. struct drm_nouveau_private *dev_priv = dev->dev_private;
  364. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  365. char info[256];
  366. int ret, i;
  367. nouveau_volt_init(dev);
  368. nouveau_perf_init(dev);
  369. nouveau_temp_init(dev);
  370. NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
  371. for (i = 0; i < pm->nr_perflvl; i++) {
  372. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  373. NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
  374. }
  375. /* determine current ("boot") performance level */
  376. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  377. if (ret == 0) {
  378. pm->cur = &pm->boot;
  379. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  380. NV_INFO(dev, "c: %s", info);
  381. }
  382. /* switch performance levels now if requested */
  383. if (nouveau_perflvl != NULL) {
  384. ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
  385. if (ret) {
  386. NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
  387. nouveau_perflvl, ret);
  388. }
  389. }
  390. nouveau_sysfs_init(dev);
  391. nouveau_hwmon_init(dev);
  392. return 0;
  393. }
  394. void
  395. nouveau_pm_fini(struct drm_device *dev)
  396. {
  397. struct drm_nouveau_private *dev_priv = dev->dev_private;
  398. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  399. if (pm->cur != &pm->boot)
  400. nouveau_pm_perflvl_set(dev, &pm->boot);
  401. nouveau_perf_fini(dev);
  402. nouveau_volt_fini(dev);
  403. nouveau_temp_fini(dev);
  404. nouveau_hwmon_fini(dev);
  405. nouveau_sysfs_fini(dev);
  406. }
  407. void
  408. nouveau_pm_resume(struct drm_device *dev)
  409. {
  410. struct drm_nouveau_private *dev_priv = dev->dev_private;
  411. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  412. struct nouveau_pm_level *perflvl;
  413. if (pm->cur == &pm->boot)
  414. return;
  415. perflvl = pm->cur;
  416. pm->cur = &pm->boot;
  417. nouveau_pm_perflvl_set(dev, perflvl);
  418. }