nouveau_pm.c 23 KB

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