nouveau_pm.c 24 KB

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