nouveau_pm.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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);
  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_pwm1_enable(struct device *d,
  422. struct device_attribute *a, char *buf)
  423. {
  424. struct drm_device *dev = dev_get_drvdata(d);
  425. struct nouveau_drm *drm = nouveau_drm(dev);
  426. struct nouveau_therm *therm = nouveau_therm(drm->device);
  427. int ret;
  428. ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MODE);
  429. if (ret < 0)
  430. return ret;
  431. return sprintf(buf, "%i\n", ret);
  432. }
  433. static ssize_t
  434. nouveau_hwmon_set_pwm1_enable(struct device *d, struct device_attribute *a,
  435. const char *buf, size_t count)
  436. {
  437. struct drm_device *dev = dev_get_drvdata(d);
  438. struct nouveau_drm *drm = nouveau_drm(dev);
  439. struct nouveau_therm *therm = nouveau_therm(drm->device);
  440. long value;
  441. int ret;
  442. if (strict_strtol(buf, 10, &value) == -EINVAL)
  443. return -EINVAL;
  444. ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MODE, value);
  445. if (ret)
  446. return ret;
  447. else
  448. return count;
  449. }
  450. static SENSOR_DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
  451. nouveau_hwmon_get_pwm1_enable,
  452. nouveau_hwmon_set_pwm1_enable, 0);
  453. static ssize_t
  454. nouveau_hwmon_get_pwm1(struct device *d, struct device_attribute *a, char *buf)
  455. {
  456. struct drm_device *dev = dev_get_drvdata(d);
  457. struct nouveau_drm *drm = nouveau_drm(dev);
  458. struct nouveau_therm *therm = nouveau_therm(drm->device);
  459. int ret;
  460. ret = therm->fan_get(therm);
  461. if (ret < 0)
  462. return ret;
  463. return sprintf(buf, "%i\n", ret);
  464. }
  465. static ssize_t
  466. nouveau_hwmon_set_pwm1(struct device *d, struct device_attribute *a,
  467. const char *buf, size_t count)
  468. {
  469. struct drm_device *dev = dev_get_drvdata(d);
  470. struct nouveau_drm *drm = nouveau_drm(dev);
  471. struct nouveau_therm *therm = nouveau_therm(drm->device);
  472. int ret = -ENODEV;
  473. long value;
  474. if (nouveau_perflvl_wr != 7777)
  475. return -EPERM;
  476. if (kstrtol(buf, 10, &value) == -EINVAL)
  477. return -EINVAL;
  478. ret = therm->fan_set(therm, value);
  479. if (ret)
  480. return ret;
  481. return count;
  482. }
  483. static SENSOR_DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR,
  484. nouveau_hwmon_get_pwm1,
  485. nouveau_hwmon_set_pwm1, 0);
  486. static ssize_t
  487. nouveau_hwmon_get_pwm1_min(struct device *d,
  488. struct device_attribute *a, char *buf)
  489. {
  490. struct drm_device *dev = dev_get_drvdata(d);
  491. struct nouveau_drm *drm = nouveau_drm(dev);
  492. struct nouveau_therm *therm = nouveau_therm(drm->device);
  493. int ret;
  494. ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY);
  495. if (ret < 0)
  496. return ret;
  497. return sprintf(buf, "%i\n", ret);
  498. }
  499. static ssize_t
  500. nouveau_hwmon_set_pwm1_min(struct device *d, struct device_attribute *a,
  501. const char *buf, size_t count)
  502. {
  503. struct drm_device *dev = dev_get_drvdata(d);
  504. struct nouveau_drm *drm = nouveau_drm(dev);
  505. struct nouveau_therm *therm = nouveau_therm(drm->device);
  506. long value;
  507. int ret;
  508. if (kstrtol(buf, 10, &value) == -EINVAL)
  509. return -EINVAL;
  510. ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MIN_DUTY, value);
  511. if (ret < 0)
  512. return ret;
  513. return count;
  514. }
  515. static SENSOR_DEVICE_ATTR(pwm1_min, S_IRUGO | S_IWUSR,
  516. nouveau_hwmon_get_pwm1_min,
  517. nouveau_hwmon_set_pwm1_min, 0);
  518. static ssize_t
  519. nouveau_hwmon_get_pwm1_max(struct device *d,
  520. struct device_attribute *a, char *buf)
  521. {
  522. struct drm_device *dev = dev_get_drvdata(d);
  523. struct nouveau_drm *drm = nouveau_drm(dev);
  524. struct nouveau_therm *therm = nouveau_therm(drm->device);
  525. int ret;
  526. ret = therm->attr_get(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY);
  527. if (ret < 0)
  528. return ret;
  529. return sprintf(buf, "%i\n", ret);
  530. }
  531. static ssize_t
  532. nouveau_hwmon_set_pwm1_max(struct device *d, struct device_attribute *a,
  533. const char *buf, size_t count)
  534. {
  535. struct drm_device *dev = dev_get_drvdata(d);
  536. struct nouveau_drm *drm = nouveau_drm(dev);
  537. struct nouveau_therm *therm = nouveau_therm(drm->device);
  538. long value;
  539. int ret;
  540. if (kstrtol(buf, 10, &value) == -EINVAL)
  541. return -EINVAL;
  542. ret = therm->attr_set(therm, NOUVEAU_THERM_ATTR_FAN_MAX_DUTY, value);
  543. if (ret < 0)
  544. return ret;
  545. return count;
  546. }
  547. static SENSOR_DEVICE_ATTR(pwm1_max, S_IRUGO | S_IWUSR,
  548. nouveau_hwmon_get_pwm1_max,
  549. nouveau_hwmon_set_pwm1_max, 0);
  550. static struct attribute *hwmon_attributes[] = {
  551. &sensor_dev_attr_temp1_input.dev_attr.attr,
  552. &sensor_dev_attr_temp1_max.dev_attr.attr,
  553. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  554. &sensor_dev_attr_name.dev_attr.attr,
  555. &sensor_dev_attr_update_rate.dev_attr.attr,
  556. NULL
  557. };
  558. static struct attribute *hwmon_fan_rpm_attributes[] = {
  559. &sensor_dev_attr_fan0_input.dev_attr.attr,
  560. NULL
  561. };
  562. static struct attribute *hwmon_pwm_fan_attributes[] = {
  563. &sensor_dev_attr_pwm1_enable.dev_attr.attr,
  564. &sensor_dev_attr_pwm1.dev_attr.attr,
  565. &sensor_dev_attr_pwm1_min.dev_attr.attr,
  566. &sensor_dev_attr_pwm1_max.dev_attr.attr,
  567. NULL
  568. };
  569. static const struct attribute_group hwmon_attrgroup = {
  570. .attrs = hwmon_attributes,
  571. };
  572. static const struct attribute_group hwmon_fan_rpm_attrgroup = {
  573. .attrs = hwmon_fan_rpm_attributes,
  574. };
  575. static const struct attribute_group hwmon_pwm_fan_attrgroup = {
  576. .attrs = hwmon_pwm_fan_attributes,
  577. };
  578. #endif
  579. static int
  580. nouveau_hwmon_init(struct drm_device *dev)
  581. {
  582. struct nouveau_pm *pm = nouveau_pm(dev);
  583. struct nouveau_drm *drm = nouveau_drm(dev);
  584. struct nouveau_therm *therm = nouveau_therm(drm->device);
  585. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  586. struct device *hwmon_dev;
  587. int ret = 0;
  588. if (!therm || !therm->temp_get || !therm->attr_get ||
  589. !therm->attr_set || therm->temp_get(therm) < 0)
  590. return -ENODEV;
  591. hwmon_dev = hwmon_device_register(&dev->pdev->dev);
  592. if (IS_ERR(hwmon_dev)) {
  593. ret = PTR_ERR(hwmon_dev);
  594. NV_ERROR(drm, "Unable to register hwmon device: %d\n", ret);
  595. return ret;
  596. }
  597. dev_set_drvdata(hwmon_dev, dev);
  598. /* default sysfs entries */
  599. ret = sysfs_create_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  600. if (ret) {
  601. if (ret)
  602. goto error;
  603. }
  604. /* if the card has a pwm fan */
  605. /*XXX: incorrect, need better detection for this, some boards have
  606. * the gpio entries for pwm fan control even when there's no
  607. * actual fan connected to it... therm table? */
  608. if (therm->fan_get && therm->fan_get(therm) >= 0) {
  609. ret = sysfs_create_group(&dev->pdev->dev.kobj,
  610. &hwmon_pwm_fan_attrgroup);
  611. if (ret)
  612. goto error;
  613. }
  614. /* if the card can read the fan rpm */
  615. if (therm->fan_sense(therm) >= 0) {
  616. ret = sysfs_create_group(&dev->pdev->dev.kobj,
  617. &hwmon_fan_rpm_attrgroup);
  618. if (ret)
  619. goto error;
  620. }
  621. pm->hwmon = hwmon_dev;
  622. return 0;
  623. error:
  624. NV_ERROR(drm, "Unable to create some hwmon sysfs files: %d\n", ret);
  625. hwmon_device_unregister(hwmon_dev);
  626. pm->hwmon = NULL;
  627. return ret;
  628. #else
  629. pm->hwmon = NULL;
  630. return 0;
  631. #endif
  632. }
  633. static void
  634. nouveau_hwmon_fini(struct drm_device *dev)
  635. {
  636. #if defined(CONFIG_HWMON) || (defined(MODULE) && defined(CONFIG_HWMON_MODULE))
  637. struct nouveau_pm *pm = nouveau_pm(dev);
  638. if (pm->hwmon) {
  639. sysfs_remove_group(&dev->pdev->dev.kobj, &hwmon_attrgroup);
  640. sysfs_remove_group(&dev->pdev->dev.kobj,
  641. &hwmon_pwm_fan_attrgroup);
  642. sysfs_remove_group(&dev->pdev->dev.kobj,
  643. &hwmon_fan_rpm_attrgroup);
  644. hwmon_device_unregister(pm->hwmon);
  645. }
  646. #endif
  647. }
  648. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  649. static int
  650. nouveau_pm_acpi_event(struct notifier_block *nb, unsigned long val, void *data)
  651. {
  652. struct nouveau_pm *pm = container_of(nb, struct nouveau_pm, acpi_nb);
  653. struct nouveau_drm *drm = nouveau_drm(pm->dev);
  654. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  655. if (strcmp(entry->device_class, "ac_adapter") == 0) {
  656. bool ac = power_supply_is_system_supplied();
  657. NV_DEBUG(drm, "power supply changed: %s\n", ac ? "AC" : "DC");
  658. nouveau_pm_trigger(pm->dev);
  659. }
  660. return NOTIFY_OK;
  661. }
  662. #endif
  663. int
  664. nouveau_pm_init(struct drm_device *dev)
  665. {
  666. struct nouveau_device *device = nouveau_dev(dev);
  667. struct nouveau_drm *drm = nouveau_drm(dev);
  668. struct nouveau_pm *pm;
  669. char info[256];
  670. int ret, i;
  671. pm = drm->pm = kzalloc(sizeof(*pm), GFP_KERNEL);
  672. if (!pm)
  673. return -ENOMEM;
  674. pm->dev = dev;
  675. if (device->card_type < NV_40) {
  676. pm->clocks_get = nv04_pm_clocks_get;
  677. pm->clocks_pre = nv04_pm_clocks_pre;
  678. pm->clocks_set = nv04_pm_clocks_set;
  679. if (nouveau_gpio(drm->device)) {
  680. pm->voltage_get = nouveau_voltage_gpio_get;
  681. pm->voltage_set = nouveau_voltage_gpio_set;
  682. }
  683. } else
  684. if (device->card_type < NV_50) {
  685. pm->clocks_get = nv40_pm_clocks_get;
  686. pm->clocks_pre = nv40_pm_clocks_pre;
  687. pm->clocks_set = nv40_pm_clocks_set;
  688. pm->voltage_get = nouveau_voltage_gpio_get;
  689. pm->voltage_set = nouveau_voltage_gpio_set;
  690. } else
  691. if (device->card_type < NV_C0) {
  692. if (device->chipset < 0xa3 ||
  693. device->chipset == 0xaa ||
  694. device->chipset == 0xac) {
  695. pm->clocks_get = nv50_pm_clocks_get;
  696. pm->clocks_pre = nv50_pm_clocks_pre;
  697. pm->clocks_set = nv50_pm_clocks_set;
  698. } else {
  699. pm->clocks_get = nva3_pm_clocks_get;
  700. pm->clocks_pre = nva3_pm_clocks_pre;
  701. pm->clocks_set = nva3_pm_clocks_set;
  702. }
  703. pm->voltage_get = nouveau_voltage_gpio_get;
  704. pm->voltage_set = nouveau_voltage_gpio_set;
  705. } else
  706. if (device->card_type < NV_E0) {
  707. pm->clocks_get = nvc0_pm_clocks_get;
  708. pm->clocks_pre = nvc0_pm_clocks_pre;
  709. pm->clocks_set = nvc0_pm_clocks_set;
  710. pm->voltage_get = nouveau_voltage_gpio_get;
  711. pm->voltage_set = nouveau_voltage_gpio_set;
  712. }
  713. /* parse aux tables from vbios */
  714. nouveau_volt_init(dev);
  715. INIT_LIST_HEAD(&pm->profiles);
  716. /* determine current ("boot") performance level */
  717. ret = nouveau_pm_perflvl_get(dev, &pm->boot);
  718. if (ret) {
  719. NV_ERROR(drm, "failed to determine boot perflvl\n");
  720. return ret;
  721. }
  722. strncpy(pm->boot.name, "boot", 4);
  723. strncpy(pm->boot.profile.name, "boot", 4);
  724. pm->boot.profile.func = &nouveau_pm_static_profile_func;
  725. list_add(&pm->boot.profile.head, &pm->profiles);
  726. pm->profile_ac = &pm->boot.profile;
  727. pm->profile_dc = &pm->boot.profile;
  728. pm->profile = &pm->boot.profile;
  729. pm->cur = &pm->boot;
  730. /* add performance levels from vbios */
  731. nouveau_perf_init(dev);
  732. /* display available performance levels */
  733. NV_INFO(drm, "%d available performance level(s)\n", pm->nr_perflvl);
  734. for (i = 0; i < pm->nr_perflvl; i++) {
  735. nouveau_pm_perflvl_info(&pm->perflvl[i], info, sizeof(info));
  736. NV_INFO(drm, "%d:%s", pm->perflvl[i].id, info);
  737. }
  738. nouveau_pm_perflvl_info(&pm->boot, info, sizeof(info));
  739. NV_INFO(drm, "c:%s", info);
  740. /* switch performance levels now if requested */
  741. if (nouveau_perflvl != NULL)
  742. nouveau_pm_profile_set(dev, nouveau_perflvl);
  743. nouveau_sysfs_init(dev);
  744. nouveau_hwmon_init(dev);
  745. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  746. pm->acpi_nb.notifier_call = nouveau_pm_acpi_event;
  747. register_acpi_notifier(&pm->acpi_nb);
  748. #endif
  749. return 0;
  750. }
  751. void
  752. nouveau_pm_fini(struct drm_device *dev)
  753. {
  754. struct nouveau_pm *pm = nouveau_pm(dev);
  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_perf_fini(dev);
  763. nouveau_volt_fini(dev);
  764. #if defined(CONFIG_ACPI) && defined(CONFIG_POWER_SUPPLY)
  765. unregister_acpi_notifier(&pm->acpi_nb);
  766. #endif
  767. nouveau_hwmon_fini(dev);
  768. nouveau_sysfs_fini(dev);
  769. nouveau_drm(dev)->pm = NULL;
  770. kfree(pm);
  771. }
  772. void
  773. nouveau_pm_resume(struct drm_device *dev)
  774. {
  775. struct nouveau_pm *pm = nouveau_pm(dev);
  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. }