nouveau_pm.c 22 KB

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