nouveau_pm.c 22 KB

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