nouveau_pm.c 24 KB

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