nouveau_pm.c 22 KB

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