nouveau_pm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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->volt_min) {
  58. ret = pm->voltage_set(dev, perflvl->volt_min);
  59. if (ret) {
  60. NV_ERROR(dev, "voltage_set %d failed: %d\n",
  61. perflvl->volt_min, 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->volt_min = ret;
  124. perflvl->volt_max = ret;
  125. }
  126. }
  127. return 0;
  128. }
  129. static void
  130. nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
  131. {
  132. char c[16], s[16], v[32], f[16], t[16];
  133. c[0] = '\0';
  134. if (perflvl->core)
  135. snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
  136. s[0] = '\0';
  137. if (perflvl->shader)
  138. snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
  139. v[0] = '\0';
  140. if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
  141. snprintf(v, sizeof(v), " voltage %dmV-%dmV",
  142. perflvl->volt_min / 1000, perflvl->volt_max / 1000);
  143. } else
  144. if (perflvl->volt_min) {
  145. snprintf(v, sizeof(v), " voltage %dmV",
  146. perflvl->volt_min / 1000);
  147. }
  148. f[0] = '\0';
  149. if (perflvl->fanspeed)
  150. snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
  151. t[0] = '\0';
  152. if (perflvl->timing)
  153. snprintf(t, sizeof(t), " timing %d", perflvl->timing->id);
  154. snprintf(ptr, len, "memory %dMHz%s%s%s%s%s\n", perflvl->memory / 1000,
  155. c, s, v, f, t);
  156. }
  157. static ssize_t
  158. nouveau_pm_get_perflvl_info(struct device *d,
  159. struct device_attribute *a, char *buf)
  160. {
  161. struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
  162. char *ptr = buf;
  163. int len = PAGE_SIZE;
  164. snprintf(ptr, len, "%d: ", perflvl->id);
  165. ptr += strlen(buf);
  166. len -= strlen(buf);
  167. nouveau_pm_perflvl_info(perflvl, ptr, len);
  168. return strlen(buf);
  169. }
  170. static ssize_t
  171. nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
  172. {
  173. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  174. struct drm_nouveau_private *dev_priv = dev->dev_private;
  175. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  176. struct nouveau_pm_level cur;
  177. int len = PAGE_SIZE, ret;
  178. char *ptr = buf;
  179. if (!pm->cur)
  180. snprintf(ptr, len, "setting: boot\n");
  181. else if (pm->cur == &pm->boot)
  182. snprintf(ptr, len, "setting: boot\nc: ");
  183. else
  184. snprintf(ptr, len, "setting: static %d\nc: ", pm->cur->id);
  185. ptr += strlen(buf);
  186. len -= strlen(buf);
  187. ret = nouveau_pm_perflvl_get(dev, &cur);
  188. if (ret == 0)
  189. nouveau_pm_perflvl_info(&cur, ptr, len);
  190. return strlen(buf);
  191. }
  192. static ssize_t
  193. nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
  194. const char *buf, size_t count)
  195. {
  196. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  197. int ret;
  198. ret = nouveau_pm_profile_set(dev, buf);
  199. if (ret)
  200. return ret;
  201. return strlen(buf);
  202. }
  203. static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
  204. nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
  205. static int
  206. nouveau_sysfs_init(struct drm_device *dev)
  207. {
  208. struct drm_nouveau_private *dev_priv = dev->dev_private;
  209. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  210. struct device *d = &dev->pdev->dev;
  211. int ret, i;
  212. ret = device_create_file(d, &dev_attr_performance_level);
  213. if (ret)
  214. return ret;
  215. for (i = 0; i < pm->nr_perflvl; i++) {
  216. struct nouveau_pm_level *perflvl = &pm->perflvl[i];
  217. perflvl->dev_attr.attr.name = perflvl->name;
  218. perflvl->dev_attr.attr.mode = S_IRUGO;
  219. perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
  220. perflvl->dev_attr.store = NULL;
  221. sysfs_attr_init(&perflvl->dev_attr.attr);
  222. ret = device_create_file(d, &perflvl->dev_attr);
  223. if (ret) {
  224. NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
  225. perflvl->id, i);
  226. perflvl->dev_attr.attr.name = NULL;
  227. nouveau_pm_fini(dev);
  228. return ret;
  229. }
  230. }
  231. return 0;
  232. }
  233. static void
  234. nouveau_sysfs_fini(struct drm_device *dev)
  235. {
  236. struct drm_nouveau_private *dev_priv = dev->dev_private;
  237. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  238. struct device *d = &dev->pdev->dev;
  239. int i;
  240. device_remove_file(d, &dev_attr_performance_level);
  241. for (i = 0; i < pm->nr_perflvl; i++) {
  242. struct nouveau_pm_level *pl = &pm->perflvl[i];
  243. if (!pl->dev_attr.attr.name)
  244. break;
  245. device_remove_file(d, &pl->dev_attr);
  246. }
  247. }
  248. #ifdef CONFIG_HWMON
  249. static ssize_t
  250. nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
  251. {
  252. struct drm_device *dev = dev_get_drvdata(d);
  253. struct drm_nouveau_private *dev_priv = dev->dev_private;
  254. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  255. return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
  256. }
  257. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  258. NULL, 0);
  259. static ssize_t
  260. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  261. {
  262. struct drm_device *dev = dev_get_drvdata(d);
  263. struct drm_nouveau_private *dev_priv = dev->dev_private;
  264. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  265. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  266. return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
  267. }
  268. static ssize_t
  269. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  270. const char *buf, size_t count)
  271. {
  272. struct drm_device *dev = dev_get_drvdata(d);
  273. struct drm_nouveau_private *dev_priv = dev->dev_private;
  274. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  275. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  276. long value;
  277. if (strict_strtol(buf, 10, &value) == -EINVAL)
  278. return count;
  279. temp->down_clock = value/1000;
  280. nouveau_temp_safety_checks(dev);
  281. return count;
  282. }
  283. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  284. nouveau_hwmon_set_max_temp,
  285. 0);
  286. static ssize_t
  287. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  288. char *buf)
  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. return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
  295. }
  296. static ssize_t
  297. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  298. const char *buf,
  299. size_t count)
  300. {
  301. struct drm_device *dev = dev_get_drvdata(d);
  302. struct drm_nouveau_private *dev_priv = dev->dev_private;
  303. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  304. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  305. long value;
  306. if (strict_strtol(buf, 10, &value) == -EINVAL)
  307. return count;
  308. temp->critical = value/1000;
  309. nouveau_temp_safety_checks(dev);
  310. return count;
  311. }
  312. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  313. nouveau_hwmon_critical_temp,
  314. nouveau_hwmon_set_critical_temp,
  315. 0);
  316. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  317. struct device_attribute *attr,
  318. char *buf)
  319. {
  320. return sprintf(buf, "nouveau\n");
  321. }
  322. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  323. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  324. struct device_attribute *attr,
  325. char *buf)
  326. {
  327. return sprintf(buf, "1000\n");
  328. }
  329. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  330. nouveau_hwmon_show_update_rate,
  331. NULL, 0);
  332. static struct attribute *hwmon_attributes[] = {
  333. &sensor_dev_attr_temp1_input.dev_attr.attr,
  334. &sensor_dev_attr_temp1_max.dev_attr.attr,
  335. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  336. &sensor_dev_attr_name.dev_attr.attr,
  337. &sensor_dev_attr_update_rate.dev_attr.attr,
  338. NULL
  339. };
  340. static const struct attribute_group hwmon_attrgroup = {
  341. .attrs = hwmon_attributes,
  342. };
  343. #endif
  344. static int
  345. nouveau_hwmon_init(struct drm_device *dev)
  346. {
  347. #ifdef CONFIG_HWMON
  348. struct drm_nouveau_private *dev_priv = dev->dev_private;
  349. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  350. struct device *hwmon_dev;
  351. int ret;
  352. if (!pm->temp_get)
  353. return -ENODEV;
  354. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  355. if (IS_ERR(hwmon_dev)) {
  356. ret = PTR_ERR(hwmon_dev);
  357. NV_ERROR(dev,
  358. "Unable to register hwmon device: %d\n", ret);
  359. return ret;
  360. }
  361. dev_set_drvdata(hwmon_dev, dev);
  362. ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  363. if (ret) {
  364. NV_ERROR(dev,
  365. "Unable to create hwmon sysfs file: %d\n", ret);
  366. hwmon_device_unregister(hwmon_dev);
  367. return ret;
  368. }
  369. pm->hwmon = hwmon_dev;
  370. #endif
  371. return 0;
  372. }
  373. static void
  374. nouveau_hwmon_fini(struct drm_device *dev)
  375. {
  376. #ifdef CONFIG_HWMON
  377. struct drm_nouveau_private *dev_priv = dev->dev_private;
  378. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  379. if (pm->hwmon) {
  380. sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  381. hwmon_device_unregister(pm->hwmon);
  382. }
  383. #endif
  384. }
  385. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  386. static int
  387. nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
  388. {
  389. struct drm_nouveau_private *dev_priv =
  390. container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
  391. struct drm_device *dev = dev_priv->dev;
  392. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  393. if (strcmp(entry->device_class, "ac_adapter") == 0) {
  394. bool ac = power_supply_is_system_supplied();
  395. NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
  396. }
  397. return NOTIFY_OK;
  398. }
  399. #endif
  400. int
  401. nouveau_pm_init(struct drm_device *dev)
  402. {
  403. struct drm_nouveau_private *dev_priv = dev->dev_private;
  404. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  405. char info[256];
  406. int ret, i;
  407. nouveau_mem_timing_init(dev);
  408. nouveau_volt_init(dev);
  409. nouveau_perf_init(dev);
  410. nouveau_temp_init(dev);
  411. NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
  412. for (i = 0; i < pm->nr_perflvl; i++) {
  413. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  414. NV_INFO(dev, "%d: %s", pm->perflvl[i].id, info);
  415. }
  416. /* determine current ("boot") performance level */
  417. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  418. if (ret == 0) {
  419. strncpy(pm->boot.name, "boot", 4);
  420. pm->cur = &pm->boot;
  421. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  422. NV_INFO(dev, "c: %s", info);
  423. }
  424. /* switch performance levels now if requested */
  425. if (nouveau_perflvl != NULL) {
  426. ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
  427. if (ret) {
  428. NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
  429. nouveau_perflvl, ret);
  430. }
  431. }
  432. nouveau_sysfs_init(dev);
  433. nouveau_hwmon_init(dev);
  434. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  435. pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
  436. register_acpi_notifier(&pm->acpi_nb);
  437. #endif
  438. return 0;
  439. }
  440. void
  441. nouveau_pm_fini(struct drm_device *dev)
  442. {
  443. struct drm_nouveau_private *dev_priv = dev->dev_private;
  444. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  445. if (pm->cur != &pm->boot)
  446. nouveau_pm_perflvl_set(dev, &pm->boot);
  447. nouveau_temp_fini(dev);
  448. nouveau_perf_fini(dev);
  449. nouveau_volt_fini(dev);
  450. nouveau_mem_timing_fini(dev);
  451. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  452. unregister_acpi_notifier(&pm->acpi_nb);
  453. #endif
  454. nouveau_hwmon_fini(dev);
  455. nouveau_sysfs_fini(dev);
  456. }
  457. void
  458. nouveau_pm_resume(struct drm_device *dev)
  459. {
  460. struct drm_nouveau_private *dev_priv = dev->dev_private;
  461. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  462. struct nouveau_pm_level *perflvl;
  463. if (!pm->cur || pm->cur == &pm->boot)
  464. return;
  465. perflvl = pm->cur;
  466. pm->cur = &pm->boot;
  467. nouveau_pm_perflvl_set(dev, perflvl);
  468. }