nouveau_pm.c 22 KB

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