nouveau_pm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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_pwmfan_get(struct drm_device *dev)
  35. {
  36. struct drm_nouveau_private *dev_priv = dev->dev_private;
  37. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  38. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  39. struct dcb_gpio_entry *gpio = NULL;
  40. u32 divs, duty;
  41. int ret;
  42. if (!pm->pwm_get)
  43. return -ENODEV;
  44. gpio = nouveau_bios_gpio_entry(dev, DCB_GPIO_PWM_FAN);
  45. if (gpio) {
  46. ret = pm->pwm_get(dev, gpio, &divs, &duty);
  47. if (ret == 0) {
  48. divs = max(divs, duty);
  49. if (dev_priv->card_type <= NV_40 ||
  50. (gpio->state[0] & 1))
  51. duty = divs - duty;
  52. return (duty * 100) / divs;
  53. }
  54. return pgpio->get(dev, gpio->tag) * 100;
  55. }
  56. return -ENODEV;
  57. }
  58. static int
  59. nouveau_pwmfan_set(struct drm_device *dev, int percent)
  60. {
  61. struct drm_nouveau_private *dev_priv = dev->dev_private;
  62. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  63. struct dcb_gpio_entry *gpio;
  64. u32 divs, duty;
  65. if (!pm->pwm_set)
  66. return -ENODEV;
  67. gpio = nouveau_bios_gpio_entry(dev, DCB_GPIO_PWM_FAN);
  68. if (gpio) {
  69. divs = pm->pwm_divisor;
  70. if (pm->fan.pwm_freq) {
  71. /*XXX: PNVIO clock more than likely... */
  72. divs = 135000 / pm->fan.pwm_freq;
  73. if (dev_priv->chipset < 0xa3)
  74. divs /= 4;
  75. }
  76. duty = ((divs * percent) + 99) / 100;
  77. if (dev_priv->card_type <= NV_40 ||
  78. (gpio->state[0] & 1))
  79. duty = divs - duty;
  80. return pm->pwm_set(dev, gpio, divs, duty);
  81. }
  82. return -ENODEV;
  83. }
  84. static int
  85. nouveau_pm_perflvl_set(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  86. {
  87. struct drm_nouveau_private *dev_priv = dev->dev_private;
  88. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  89. void *state;
  90. int ret;
  91. if (perflvl == pm->cur)
  92. return 0;
  93. /*XXX: not on all boards, we should control based on temperature
  94. * on recent boards.. or maybe on some other factor we don't
  95. * know about?
  96. */
  97. if (perflvl->fanspeed) {
  98. ret = nouveau_pwmfan_set(dev, perflvl->fanspeed);
  99. if (ret && ret != -ENODEV)
  100. NV_ERROR(dev, "set fanspeed failed: %d\n", ret);
  101. }
  102. if (pm->voltage.supported && pm->voltage_set && perflvl->volt_min) {
  103. ret = pm->voltage_set(dev, perflvl->volt_min);
  104. if (ret) {
  105. NV_ERROR(dev, "voltage_set %d failed: %d\n",
  106. perflvl->volt_min, ret);
  107. }
  108. }
  109. state = pm->clocks_pre(dev, perflvl);
  110. if (IS_ERR(state))
  111. return PTR_ERR(state);
  112. pm->clocks_set(dev, state);
  113. pm->cur = perflvl;
  114. return 0;
  115. }
  116. static int
  117. nouveau_pm_profile_set(struct drm_device *dev, const char *profile)
  118. {
  119. struct drm_nouveau_private *dev_priv = dev->dev_private;
  120. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  121. struct nouveau_pm_level *perflvl = NULL;
  122. /* safety precaution, for now */
  123. if (nouveau_perflvl_wr != 7777)
  124. return -EPERM;
  125. if (!strncmp(profile, "boot", 4))
  126. perflvl = &pm->boot;
  127. else {
  128. int pl = simple_strtol(profile, NULL, 10);
  129. int i;
  130. for (i = 0; i < pm->nr_perflvl; i++) {
  131. if (pm->perflvl[i].id == pl) {
  132. perflvl = &pm->perflvl[i];
  133. break;
  134. }
  135. }
  136. if (!perflvl)
  137. return -EINVAL;
  138. }
  139. NV_INFO(dev, "setting performance level: %s\n", profile);
  140. return nouveau_pm_perflvl_set(dev, perflvl);
  141. }
  142. static int
  143. nouveau_pm_perflvl_get(struct drm_device *dev, struct nouveau_pm_level *perflvl)
  144. {
  145. struct drm_nouveau_private *dev_priv = dev->dev_private;
  146. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  147. int ret;
  148. memset(perflvl, 0, sizeof(*perflvl));
  149. ret = pm->clocks_get(dev, perflvl);
  150. if (ret)
  151. return ret;
  152. if (pm->voltage.supported && pm->voltage_get) {
  153. ret = pm->voltage_get(dev);
  154. if (ret > 0) {
  155. perflvl->volt_min = ret;
  156. perflvl->volt_max = ret;
  157. }
  158. }
  159. ret = nouveau_pwmfan_get(dev);
  160. if (ret > 0)
  161. perflvl->fanspeed = ret;
  162. return 0;
  163. }
  164. static void
  165. nouveau_pm_perflvl_info(struct nouveau_pm_level *perflvl, char *ptr, int len)
  166. {
  167. char c[16], s[16], v[32], f[16], t[16], m[16];
  168. c[0] = '\0';
  169. if (perflvl->core)
  170. snprintf(c, sizeof(c), " core %dMHz", perflvl->core / 1000);
  171. s[0] = '\0';
  172. if (perflvl->shader)
  173. snprintf(s, sizeof(s), " shader %dMHz", perflvl->shader / 1000);
  174. m[0] = '\0';
  175. if (perflvl->memory)
  176. snprintf(m, sizeof(m), " memory %dMHz", perflvl->memory / 1000);
  177. v[0] = '\0';
  178. if (perflvl->volt_min && perflvl->volt_min != perflvl->volt_max) {
  179. snprintf(v, sizeof(v), " voltage %dmV-%dmV",
  180. perflvl->volt_min / 1000, perflvl->volt_max / 1000);
  181. } else
  182. if (perflvl->volt_min) {
  183. snprintf(v, sizeof(v), " voltage %dmV",
  184. perflvl->volt_min / 1000);
  185. }
  186. f[0] = '\0';
  187. if (perflvl->fanspeed)
  188. snprintf(f, sizeof(f), " fanspeed %d%%", perflvl->fanspeed);
  189. t[0] = '\0';
  190. if (perflvl->timing)
  191. snprintf(t, sizeof(t), " timing %d", perflvl->timing->id);
  192. snprintf(ptr, len, "%s%s%s%s%s%s\n", c, s, m, t, v, f);
  193. }
  194. static ssize_t
  195. nouveau_pm_get_perflvl_info(struct device *d,
  196. struct device_attribute *a, char *buf)
  197. {
  198. struct nouveau_pm_level *perflvl = (struct nouveau_pm_level *)a;
  199. char *ptr = buf;
  200. int len = PAGE_SIZE;
  201. snprintf(ptr, len, "%d:", perflvl->id);
  202. ptr += strlen(buf);
  203. len -= strlen(buf);
  204. nouveau_pm_perflvl_info(perflvl, ptr, len);
  205. return strlen(buf);
  206. }
  207. static ssize_t
  208. nouveau_pm_get_perflvl(struct device *d, struct device_attribute *a, char *buf)
  209. {
  210. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  211. struct drm_nouveau_private *dev_priv = dev->dev_private;
  212. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  213. struct nouveau_pm_level cur;
  214. int len = PAGE_SIZE, ret;
  215. char *ptr = buf;
  216. if (!pm->cur)
  217. snprintf(ptr, len, "setting: boot\n");
  218. else if (pm->cur == &pm->boot)
  219. snprintf(ptr, len, "setting: boot\nc:");
  220. else
  221. snprintf(ptr, len, "setting: static %d\nc:", pm->cur->id);
  222. ptr += strlen(buf);
  223. len -= strlen(buf);
  224. ret = nouveau_pm_perflvl_get(dev, &cur);
  225. if (ret == 0)
  226. nouveau_pm_perflvl_info(&cur, ptr, len);
  227. return strlen(buf);
  228. }
  229. static ssize_t
  230. nouveau_pm_set_perflvl(struct device *d, struct device_attribute *a,
  231. const char *buf, size_t count)
  232. {
  233. struct drm_device *dev = pci_get_drvdata(to_pci_dev(d));
  234. int ret;
  235. ret = nouveau_pm_profile_set(dev, buf);
  236. if (ret)
  237. return ret;
  238. return strlen(buf);
  239. }
  240. static DEVICE_ATTR(performance_level, S_IRUGO | S_IWUSR,
  241. nouveau_pm_get_perflvl, nouveau_pm_set_perflvl);
  242. static int
  243. nouveau_sysfs_init(struct drm_device *dev)
  244. {
  245. struct drm_nouveau_private *dev_priv = dev->dev_private;
  246. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  247. struct device *d = &dev->pdev->dev;
  248. int ret, i;
  249. ret = device_create_file(d, &dev_attr_performance_level);
  250. if (ret)
  251. return ret;
  252. for (i = 0; i < pm->nr_perflvl; i++) {
  253. struct nouveau_pm_level *perflvl = &pm->perflvl[i];
  254. perflvl->dev_attr.attr.name = perflvl->name;
  255. perflvl->dev_attr.attr.mode = S_IRUGO;
  256. perflvl->dev_attr.show = nouveau_pm_get_perflvl_info;
  257. perflvl->dev_attr.store = NULL;
  258. sysfs_attr_init(&perflvl->dev_attr.attr);
  259. ret = device_create_file(d, &perflvl->dev_attr);
  260. if (ret) {
  261. NV_ERROR(dev, "failed pervlvl %d sysfs: %d\n",
  262. perflvl->id, i);
  263. perflvl->dev_attr.attr.name = NULL;
  264. nouveau_pm_fini(dev);
  265. return ret;
  266. }
  267. }
  268. return 0;
  269. }
  270. static void
  271. nouveau_sysfs_fini(struct drm_device *dev)
  272. {
  273. struct drm_nouveau_private *dev_priv = dev->dev_private;
  274. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  275. struct device *d = &dev->pdev->dev;
  276. int i;
  277. device_remove_file(d, &dev_attr_performance_level);
  278. for (i = 0; i < pm->nr_perflvl; i++) {
  279. struct nouveau_pm_level *pl = &pm->perflvl[i];
  280. if (!pl->dev_attr.attr.name)
  281. break;
  282. device_remove_file(d, &pl->dev_attr);
  283. }
  284. }
  285. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  286. static ssize_t
  287. nouveau_hwmon_show_temp(struct device *d, struct device_attribute *a, char *buf)
  288. {
  289. struct drm_device *dev = dev_get_drvdata(d);
  290. struct drm_nouveau_private *dev_priv = dev->dev_private;
  291. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  292. return snprintf(buf, PAGE_SIZE, "%d\n", pm->temp_get(dev)*1000);
  293. }
  294. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, nouveau_hwmon_show_temp,
  295. NULL, 0);
  296. static ssize_t
  297. nouveau_hwmon_max_temp(struct device *d, struct device_attribute *a, char *buf)
  298. {
  299. struct drm_device *dev = dev_get_drvdata(d);
  300. struct drm_nouveau_private *dev_priv = dev->dev_private;
  301. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  302. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  303. return snprintf(buf, PAGE_SIZE, "%d\n", temp->down_clock*1000);
  304. }
  305. static ssize_t
  306. nouveau_hwmon_set_max_temp(struct device *d, struct device_attribute *a,
  307. const char *buf, size_t count)
  308. {
  309. struct drm_device *dev = dev_get_drvdata(d);
  310. struct drm_nouveau_private *dev_priv = dev->dev_private;
  311. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  312. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  313. long value;
  314. if (strict_strtol(buf, 10, &value) == -EINVAL)
  315. return count;
  316. temp->down_clock = value/1000;
  317. nouveau_temp_safety_checks(dev);
  318. return count;
  319. }
  320. static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, nouveau_hwmon_max_temp,
  321. nouveau_hwmon_set_max_temp,
  322. 0);
  323. static ssize_t
  324. nouveau_hwmon_critical_temp(struct device *d, struct device_attribute *a,
  325. char *buf)
  326. {
  327. struct drm_device *dev = dev_get_drvdata(d);
  328. struct drm_nouveau_private *dev_priv = dev->dev_private;
  329. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  330. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  331. return snprintf(buf, PAGE_SIZE, "%d\n", temp->critical*1000);
  332. }
  333. static ssize_t
  334. nouveau_hwmon_set_critical_temp(struct device *d, struct device_attribute *a,
  335. const char *buf,
  336. size_t count)
  337. {
  338. struct drm_device *dev = dev_get_drvdata(d);
  339. struct drm_nouveau_private *dev_priv = dev->dev_private;
  340. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  341. struct nouveau_pm_threshold_temp *temp = &pm->threshold_temp;
  342. long value;
  343. if (strict_strtol(buf, 10, &value) == -EINVAL)
  344. return count;
  345. temp->critical = value/1000;
  346. nouveau_temp_safety_checks(dev);
  347. return count;
  348. }
  349. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO | S_IWUSR,
  350. nouveau_hwmon_critical_temp,
  351. nouveau_hwmon_set_critical_temp,
  352. 0);
  353. static ssize_t nouveau_hwmon_show_name(struct device *dev,
  354. struct device_attribute *attr,
  355. char *buf)
  356. {
  357. return sprintf(buf, "nouveau\n");
  358. }
  359. static SENSOR_DEVICE_ATTR(name, S_IRUGO, nouveau_hwmon_show_name, NULL, 0);
  360. static ssize_t nouveau_hwmon_show_update_rate(struct device *dev,
  361. struct device_attribute *attr,
  362. char *buf)
  363. {
  364. return sprintf(buf, "1000\n");
  365. }
  366. static SENSOR_DEVICE_ATTR(update_rate, S_IRUGO,
  367. nouveau_hwmon_show_update_rate,
  368. NULL, 0);
  369. static ssize_t
  370. nouveau_hwmon_show_fan0_input(struct device *d, struct device_attribute *attr,
  371. char *buf)
  372. {
  373. struct drm_device *dev = dev_get_drvdata(d);
  374. struct drm_nouveau_private *dev_priv = dev->dev_private;
  375. struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
  376. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  377. struct dcb_gpio_entry *gpio;
  378. u32 cycles, cur, prev;
  379. u64 start;
  380. gpio = nouveau_bios_gpio_entry(dev, DCB_GPIO_FAN_SENSE);
  381. if (!gpio)
  382. return -ENODEV;
  383. /* Monitor the GPIO input 0x3b for 250ms.
  384. * When the fan spins, it changes the value of GPIO FAN_SENSE.
  385. * We get 4 changes (0 -> 1 -> 0 -> 1 -> [...]) per complete rotation.
  386. */
  387. start = ptimer->read(dev);
  388. prev = pgpio->get(dev, DCB_GPIO_FAN_SENSE);
  389. cycles = 0;
  390. do {
  391. cur = pgpio->get(dev, DCB_GPIO_FAN_SENSE);
  392. if (prev != cur) {
  393. cycles++;
  394. prev = cur;
  395. }
  396. usleep_range(500, 1000); /* supports 0 < rpm < 7500 */
  397. } while (ptimer->read(dev) - start < 250000000);
  398. /* interpolate to get rpm */
  399. return sprintf(buf, "%i\n", cycles / 4 * 4 * 60);
  400. }
  401. static SENSOR_DEVICE_ATTR(fan0_input, S_IRUGO, nouveau_hwmon_show_fan0_input,
  402. NULL, 0);
  403. static ssize_t
  404. nouveau_hwmon_get_pwm0(struct device *d, struct device_attribute *a, char *buf)
  405. {
  406. struct drm_device *dev = dev_get_drvdata(d);
  407. int ret;
  408. ret = nouveau_pwmfan_get(dev);
  409. if (ret < 0)
  410. return ret;
  411. return sprintf(buf, "%i\n", ret);
  412. }
  413. static ssize_t
  414. nouveau_hwmon_set_pwm0(struct device *d, struct device_attribute *a,
  415. const char *buf, size_t count)
  416. {
  417. struct drm_device *dev = dev_get_drvdata(d);
  418. struct drm_nouveau_private *dev_priv = dev->dev_private;
  419. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  420. int ret = -ENODEV;
  421. long value;
  422. if (nouveau_perflvl_wr != 7777)
  423. return -EPERM;
  424. if (strict_strtol(buf, 10, &value) == -EINVAL)
  425. return -EINVAL;
  426. if (value < pm->fan.min_duty)
  427. value = pm->fan.min_duty;
  428. if (value > pm->fan.max_duty)
  429. value = pm->fan.max_duty;
  430. ret = nouveau_pwmfan_set(dev, value);
  431. if (ret)
  432. return ret;
  433. return count;
  434. }
  435. static SENSOR_DEVICE_ATTR(pwm0, S_IRUGO | S_IWUSR,
  436. nouveau_hwmon_get_pwm0,
  437. nouveau_hwmon_set_pwm0, 0);
  438. static ssize_t
  439. nouveau_hwmon_get_pwm0_min(struct device *d,
  440. struct device_attribute *a, char *buf)
  441. {
  442. struct drm_device *dev = dev_get_drvdata(d);
  443. struct drm_nouveau_private *dev_priv = dev->dev_private;
  444. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  445. return sprintf(buf, "%i\n", pm->fan.min_duty);
  446. }
  447. static ssize_t
  448. nouveau_hwmon_set_pwm0_min(struct device *d, struct device_attribute *a,
  449. const char *buf, size_t count)
  450. {
  451. struct drm_device *dev = dev_get_drvdata(d);
  452. struct drm_nouveau_private *dev_priv = dev->dev_private;
  453. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  454. long value;
  455. if (strict_strtol(buf, 10, &value) == -EINVAL)
  456. return -EINVAL;
  457. if (value < 0)
  458. value = 0;
  459. if (pm->fan.max_duty - value < 10)
  460. value = pm->fan.max_duty - 10;
  461. if (value < 10)
  462. pm->fan.min_duty = 10;
  463. else
  464. pm->fan.min_duty = value;
  465. return count;
  466. }
  467. static SENSOR_DEVICE_ATTR(pwm0_min, S_IRUGO | S_IWUSR,
  468. nouveau_hwmon_get_pwm0_min,
  469. nouveau_hwmon_set_pwm0_min, 0);
  470. static ssize_t
  471. nouveau_hwmon_get_pwm0_max(struct device *d,
  472. struct device_attribute *a, char *buf)
  473. {
  474. struct drm_device *dev = dev_get_drvdata(d);
  475. struct drm_nouveau_private *dev_priv = dev->dev_private;
  476. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  477. return sprintf(buf, "%i\n", pm->fan.max_duty);
  478. }
  479. static ssize_t
  480. nouveau_hwmon_set_pwm0_max(struct device *d, struct device_attribute *a,
  481. const char *buf, size_t count)
  482. {
  483. struct drm_device *dev = dev_get_drvdata(d);
  484. struct drm_nouveau_private *dev_priv = dev->dev_private;
  485. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  486. long value;
  487. if (strict_strtol(buf, 10, &value) == -EINVAL)
  488. return -EINVAL;
  489. if (value < 0)
  490. value = 0;
  491. if (value - pm->fan.min_duty < 10)
  492. value = pm->fan.min_duty + 10;
  493. if (value > 100)
  494. pm->fan.max_duty = 100;
  495. else
  496. pm->fan.max_duty = value;
  497. return count;
  498. }
  499. static SENSOR_DEVICE_ATTR(pwm0_max, S_IRUGO | S_IWUSR,
  500. nouveau_hwmon_get_pwm0_max,
  501. nouveau_hwmon_set_pwm0_max, 0);
  502. static struct attribute *hwmon_attributes[] = {
  503. &sensor_dev_attr_temp1_input.dev_attr.attr,
  504. &sensor_dev_attr_temp1_max.dev_attr.attr,
  505. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  506. &sensor_dev_attr_name.dev_attr.attr,
  507. &sensor_dev_attr_update_rate.dev_attr.attr,
  508. NULL
  509. };
  510. static struct attribute *hwmon_fan_rpm_attributes[] = {
  511. &sensor_dev_attr_fan0_input.dev_attr.attr,
  512. NULL
  513. };
  514. static struct attribute *hwmon_pwm_fan_attributes[] = {
  515. &sensor_dev_attr_pwm0.dev_attr.attr,
  516. &sensor_dev_attr_pwm0_min.dev_attr.attr,
  517. &sensor_dev_attr_pwm0_max.dev_attr.attr,
  518. NULL
  519. };
  520. static const struct attribute_group hwmon_attrgroup = {
  521. .attrs = hwmon_attributes,
  522. };
  523. static const struct attribute_group hwmon_fan_rpm_attrgroup = {
  524. .attrs = hwmon_fan_rpm_attributes,
  525. };
  526. static const struct attribute_group hwmon_pwm_fan_attrgroup = {
  527. .attrs = hwmon_pwm_fan_attributes,
  528. };
  529. #endif
  530. static int
  531. nouveau_hwmon_init(struct drm_device *dev)
  532. {
  533. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  534. struct drm_nouveau_private *dev_priv = dev->dev_private;
  535. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  536. struct device *hwmon_dev;
  537. int ret = 0;
  538. if (!pm->temp_get)
  539. return -ENODEV;
  540. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  541. if (IS_ERR(hwmon_dev)) {
  542. ret = PTR_ERR(hwmon_dev);
  543. NV_ERROR(dev,
  544. "Unable to register hwmon device: %d\n", ret);
  545. return ret;
  546. }
  547. dev_set_drvdata(hwmon_dev, dev);
  548. /* default sysfs entries */
  549. ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  550. if (ret) {
  551. if (ret)
  552. goto error;
  553. }
  554. /* if the card has a pwm fan */
  555. /*XXX: incorrect, need better detection for this, some boards have
  556. * the gpio entries for pwm fan control even when there's no
  557. * actual fan connected to it... therm table? */
  558. if (nouveau_pwmfan_get(dev) >= 0) {
  559. ret = sysfs_create_group(&dev->pdev->dev.kobj,
  560. &hwmon_pwm_fan_attrgroup);
  561. if (ret)
  562. goto error;
  563. }
  564. /* if the card can read the fan rpm */
  565. if (nouveau_bios_gpio_entry(dev, DCB_GPIO_FAN_SENSE)) {
  566. ret = sysfs_create_group(&dev->pdev->dev.kobj,
  567. &hwmon_fan_rpm_attrgroup);
  568. if (ret)
  569. goto error;
  570. }
  571. pm->hwmon = hwmon_dev;
  572. return 0;
  573. error:
  574. NV_ERROR(dev, "Unable to create some hwmon sysfs files: %d\n", ret);
  575. hwmon_device_unregister(hwmon_dev);
  576. pm->hwmon = NULL;
  577. return ret;
  578. #else
  579. pm->hwmon = NULL;
  580. return 0;
  581. #endif
  582. }
  583. static void
  584. nouveau_hwmon_fini(struct drm_device *dev)
  585. {
  586. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  587. struct drm_nouveau_private *dev_priv = dev->dev_private;
  588. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  589. if (pm->hwmon) {
  590. sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  591. sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_pwm_fan_attrgroup);
  592. sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_fan_rpm_attrgroup);
  593. hwmon_device_unregister(pm->hwmon);
  594. }
  595. #endif
  596. }
  597. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  598. static int
  599. nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
  600. {
  601. struct drm_nouveau_private *dev_priv =
  602. container_of(nb, struct drm_nouveau_private, engine.pm.acpi_nb);
  603. struct drm_device *dev = dev_priv->dev;
  604. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  605. if (strcmp(entry->device_class, "ac_adapter") == 0) {
  606. bool ac = power_supply_is_system_supplied();
  607. NV_DEBUG(dev, "power supply changed: %s\n", ac ? "AC" : "DC");
  608. }
  609. return NOTIFY_OK;
  610. }
  611. #endif
  612. int
  613. nouveau_pm_init(struct drm_device *dev)
  614. {
  615. struct drm_nouveau_private *dev_priv = dev->dev_private;
  616. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  617. char info[256];
  618. int ret, i;
  619. nouveau_mem_timing_init(dev);
  620. nouveau_volt_init(dev);
  621. nouveau_perf_init(dev);
  622. nouveau_temp_init(dev);
  623. NV_INFO(dev, "%d available performance level(s)\n", pm->nr_perflvl);
  624. for (i = 0; i < pm->nr_perflvl; i++) {
  625. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  626. NV_INFO(dev, "%d:%s", pm->perflvl[i].id, info);
  627. }
  628. /* determine current ("boot") performance level */
  629. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  630. if (ret == 0) {
  631. strncpy(pm->boot.name, "boot", 4);
  632. pm->cur = &pm->boot;
  633. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  634. NV_INFO(dev, "c:%s", info);
  635. }
  636. /* switch performance levels now if requested */
  637. if (nouveau_perflvl != NULL) {
  638. ret = nouveau_pm_profile_set(dev, nouveau_perflvl);
  639. if (ret) {
  640. NV_ERROR(dev, "error setting perflvl \"%s\": %d\n",
  641. nouveau_perflvl, ret);
  642. }
  643. }
  644. nouveau_sysfs_init(dev);
  645. nouveau_hwmon_init(dev);
  646. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  647. pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
  648. register_acpi_notifier(&pm->acpi_nb);
  649. #endif
  650. return 0;
  651. }
  652. void
  653. nouveau_pm_fini(struct drm_device *dev)
  654. {
  655. struct drm_nouveau_private *dev_priv = dev->dev_private;
  656. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  657. if (pm->cur != &pm->boot)
  658. nouveau_pm_perflvl_set(dev, &pm->boot);
  659. nouveau_temp_fini(dev);
  660. nouveau_perf_fini(dev);
  661. nouveau_volt_fini(dev);
  662. nouveau_mem_timing_fini(dev);
  663. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  664. unregister_acpi_notifier(&pm->acpi_nb);
  665. #endif
  666. nouveau_hwmon_fini(dev);
  667. nouveau_sysfs_fini(dev);
  668. }
  669. void
  670. nouveau_pm_resume(struct drm_device *dev)
  671. {
  672. struct drm_nouveau_private *dev_priv = dev->dev_private;
  673. struct nouveau_pm_engine *pm = &dev_priv->engine.pm;
  674. struct nouveau_pm_level *perflvl;
  675. if (!pm->cur || pm->cur == &pm->boot)
  676. return;
  677. perflvl = pm->cur;
  678. pm->cur = &pm->boot;
  679. nouveau_pm_perflvl_set(dev, perflvl);
  680. }