nouveau_pm.c 14 KB

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