nouveau_pm.c 24 KB

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