nouveau_pm.c 24 KB

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