nouveau_pm.c 15 KB

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