nouveau_pm.c 14 KB

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