radeon_pm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * Permission is hereby granted, free of charge, to any person obtaining a
  3. * copy of this software and associated documentation files (the "Software"),
  4. * to deal in the Software without restriction, including without limitation
  5. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. * and/or sell copies of the Software, and to permit persons to whom the
  7. * Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  16. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  18. * OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * Authors: Rafał Miłecki <zajec5@gmail.com>
  21. * Alex Deucher <alexdeucher@gmail.com>
  22. */
  23. #include "drmP.h"
  24. #include "radeon.h"
  25. #include "avivod.h"
  26. #include "atom.h"
  27. #ifdef CONFIG_ACPI
  28. #include <linux/acpi.h>
  29. #endif
  30. #include <linux/power_supply.h>
  31. #include <linux/hwmon.h>
  32. #include <linux/hwmon-sysfs.h>
  33. #define RADEON_IDLE_LOOP_MS 100
  34. #define RADEON_RECLOCK_DELAY_MS 200
  35. #define RADEON_WAIT_VBLANK_TIMEOUT 200
  36. static const char *radeon_pm_state_type_name[5] = {
  37. "Default",
  38. "Powersave",
  39. "Battery",
  40. "Balanced",
  41. "Performance",
  42. };
  43. static void radeon_dynpm_idle_work_handler(struct work_struct *work);
  44. static int radeon_debugfs_pm_init(struct radeon_device *rdev);
  45. static bool radeon_pm_in_vbl(struct radeon_device *rdev);
  46. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
  47. static void radeon_pm_update_profile(struct radeon_device *rdev);
  48. static void radeon_pm_set_clocks(struct radeon_device *rdev);
  49. #define ACPI_AC_CLASS "ac_adapter"
  50. int radeon_pm_get_type_index(struct radeon_device *rdev,
  51. enum radeon_pm_state_type ps_type,
  52. int instance)
  53. {
  54. int i;
  55. int found_instance = -1;
  56. for (i = 0; i < rdev->pm.num_power_states; i++) {
  57. if (rdev->pm.power_state[i].type == ps_type) {
  58. found_instance++;
  59. if (found_instance == instance)
  60. return i;
  61. }
  62. }
  63. /* return default if no match */
  64. return rdev->pm.default_power_state_index;
  65. }
  66. #ifdef CONFIG_ACPI
  67. static int radeon_acpi_event(struct notifier_block *nb,
  68. unsigned long val,
  69. void *data)
  70. {
  71. struct radeon_device *rdev = container_of(nb, struct radeon_device, acpi_nb);
  72. struct acpi_bus_event *entry = (struct acpi_bus_event *)data;
  73. if (strcmp(entry->device_class, ACPI_AC_CLASS) == 0) {
  74. if (power_supply_is_system_supplied() > 0)
  75. DRM_DEBUG_DRIVER("pm: AC\n");
  76. else
  77. DRM_DEBUG_DRIVER("pm: DC\n");
  78. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  79. if (rdev->pm.profile == PM_PROFILE_AUTO) {
  80. mutex_lock(&rdev->pm.mutex);
  81. radeon_pm_update_profile(rdev);
  82. radeon_pm_set_clocks(rdev);
  83. mutex_unlock(&rdev->pm.mutex);
  84. }
  85. }
  86. }
  87. return NOTIFY_OK;
  88. }
  89. #endif
  90. static void radeon_pm_update_profile(struct radeon_device *rdev)
  91. {
  92. switch (rdev->pm.profile) {
  93. case PM_PROFILE_DEFAULT:
  94. rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
  95. break;
  96. case PM_PROFILE_AUTO:
  97. if (power_supply_is_system_supplied() > 0) {
  98. if (rdev->pm.active_crtc_count > 1)
  99. rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
  100. else
  101. rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
  102. } else {
  103. if (rdev->pm.active_crtc_count > 1)
  104. rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
  105. else
  106. rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
  107. }
  108. break;
  109. case PM_PROFILE_LOW:
  110. if (rdev->pm.active_crtc_count > 1)
  111. rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
  112. else
  113. rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
  114. break;
  115. case PM_PROFILE_MID:
  116. if (rdev->pm.active_crtc_count > 1)
  117. rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
  118. else
  119. rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
  120. break;
  121. case PM_PROFILE_HIGH:
  122. if (rdev->pm.active_crtc_count > 1)
  123. rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
  124. else
  125. rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
  126. break;
  127. }
  128. if (rdev->pm.active_crtc_count == 0) {
  129. rdev->pm.requested_power_state_index =
  130. rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
  131. rdev->pm.requested_clock_mode_index =
  132. rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
  133. } else {
  134. rdev->pm.requested_power_state_index =
  135. rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
  136. rdev->pm.requested_clock_mode_index =
  137. rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
  138. }
  139. }
  140. static void radeon_unmap_vram_bos(struct radeon_device *rdev)
  141. {
  142. struct radeon_bo *bo, *n;
  143. if (list_empty(&rdev->gem.objects))
  144. return;
  145. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  146. if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
  147. ttm_bo_unmap_virtual(&bo->tbo);
  148. }
  149. }
  150. static void radeon_sync_with_vblank(struct radeon_device *rdev)
  151. {
  152. if (rdev->pm.active_crtcs) {
  153. rdev->pm.vblank_sync = false;
  154. wait_event_timeout(
  155. rdev->irq.vblank_queue, rdev->pm.vblank_sync,
  156. msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
  157. }
  158. }
  159. static void radeon_set_power_state(struct radeon_device *rdev)
  160. {
  161. u32 sclk, mclk;
  162. bool misc_after = false;
  163. if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
  164. (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
  165. return;
  166. if (radeon_gui_idle(rdev)) {
  167. sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
  168. clock_info[rdev->pm.requested_clock_mode_index].sclk;
  169. if (sclk > rdev->pm.default_sclk)
  170. sclk = rdev->pm.default_sclk;
  171. mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
  172. clock_info[rdev->pm.requested_clock_mode_index].mclk;
  173. if (mclk > rdev->pm.default_mclk)
  174. mclk = rdev->pm.default_mclk;
  175. /* upvolt before raising clocks, downvolt after lowering clocks */
  176. if (sclk < rdev->pm.current_sclk)
  177. misc_after = true;
  178. radeon_sync_with_vblank(rdev);
  179. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  180. if (!radeon_pm_in_vbl(rdev))
  181. return;
  182. }
  183. radeon_pm_prepare(rdev);
  184. if (!misc_after)
  185. /* voltage, pcie lanes, etc.*/
  186. radeon_pm_misc(rdev);
  187. /* set engine clock */
  188. if (sclk != rdev->pm.current_sclk) {
  189. radeon_pm_debug_check_in_vbl(rdev, false);
  190. radeon_set_engine_clock(rdev, sclk);
  191. radeon_pm_debug_check_in_vbl(rdev, true);
  192. rdev->pm.current_sclk = sclk;
  193. DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
  194. }
  195. /* set memory clock */
  196. if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
  197. radeon_pm_debug_check_in_vbl(rdev, false);
  198. radeon_set_memory_clock(rdev, mclk);
  199. radeon_pm_debug_check_in_vbl(rdev, true);
  200. rdev->pm.current_mclk = mclk;
  201. DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
  202. }
  203. if (misc_after)
  204. /* voltage, pcie lanes, etc.*/
  205. radeon_pm_misc(rdev);
  206. radeon_pm_finish(rdev);
  207. rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
  208. rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
  209. } else
  210. DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
  211. }
  212. static void radeon_pm_set_clocks(struct radeon_device *rdev)
  213. {
  214. int i;
  215. /* no need to take locks, etc. if nothing's going to change */
  216. if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
  217. (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
  218. return;
  219. mutex_lock(&rdev->ddev->struct_mutex);
  220. down_write(&rdev->pm.mclk_lock);
  221. mutex_lock(&rdev->ring_lock);
  222. /* gui idle int has issues on older chips it seems */
  223. if (rdev->family >= CHIP_R600) {
  224. if (rdev->irq.installed) {
  225. /* wait for GPU to become idle */
  226. radeon_irq_kms_wait_gui_idle(rdev);
  227. }
  228. } else {
  229. struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
  230. if (ring->ready) {
  231. radeon_fence_wait_empty_locked(rdev, RADEON_RING_TYPE_GFX_INDEX);
  232. }
  233. }
  234. radeon_unmap_vram_bos(rdev);
  235. if (rdev->irq.installed) {
  236. for (i = 0; i < rdev->num_crtc; i++) {
  237. if (rdev->pm.active_crtcs & (1 << i)) {
  238. rdev->pm.req_vblank |= (1 << i);
  239. drm_vblank_get(rdev->ddev, i);
  240. }
  241. }
  242. }
  243. radeon_set_power_state(rdev);
  244. if (rdev->irq.installed) {
  245. for (i = 0; i < rdev->num_crtc; i++) {
  246. if (rdev->pm.req_vblank & (1 << i)) {
  247. rdev->pm.req_vblank &= ~(1 << i);
  248. drm_vblank_put(rdev->ddev, i);
  249. }
  250. }
  251. }
  252. /* update display watermarks based on new power state */
  253. radeon_update_bandwidth_info(rdev);
  254. if (rdev->pm.active_crtc_count)
  255. radeon_bandwidth_update(rdev);
  256. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  257. mutex_unlock(&rdev->ring_lock);
  258. up_write(&rdev->pm.mclk_lock);
  259. mutex_unlock(&rdev->ddev->struct_mutex);
  260. }
  261. static void radeon_pm_print_states(struct radeon_device *rdev)
  262. {
  263. int i, j;
  264. struct radeon_power_state *power_state;
  265. struct radeon_pm_clock_info *clock_info;
  266. DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
  267. for (i = 0; i < rdev->pm.num_power_states; i++) {
  268. power_state = &rdev->pm.power_state[i];
  269. DRM_DEBUG_DRIVER("State %d: %s\n", i,
  270. radeon_pm_state_type_name[power_state->type]);
  271. if (i == rdev->pm.default_power_state_index)
  272. DRM_DEBUG_DRIVER("\tDefault");
  273. if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
  274. DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
  275. if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
  276. DRM_DEBUG_DRIVER("\tSingle display only\n");
  277. DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
  278. for (j = 0; j < power_state->num_clock_modes; j++) {
  279. clock_info = &(power_state->clock_info[j]);
  280. if (rdev->flags & RADEON_IS_IGP)
  281. DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
  282. j,
  283. clock_info->sclk * 10,
  284. clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
  285. else
  286. DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
  287. j,
  288. clock_info->sclk * 10,
  289. clock_info->mclk * 10,
  290. clock_info->voltage.voltage,
  291. clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
  292. }
  293. }
  294. }
  295. static ssize_t radeon_get_pm_profile(struct device *dev,
  296. struct device_attribute *attr,
  297. char *buf)
  298. {
  299. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  300. struct radeon_device *rdev = ddev->dev_private;
  301. int cp = rdev->pm.profile;
  302. return snprintf(buf, PAGE_SIZE, "%s\n",
  303. (cp == PM_PROFILE_AUTO) ? "auto" :
  304. (cp == PM_PROFILE_LOW) ? "low" :
  305. (cp == PM_PROFILE_MID) ? "mid" :
  306. (cp == PM_PROFILE_HIGH) ? "high" : "default");
  307. }
  308. static ssize_t radeon_set_pm_profile(struct device *dev,
  309. struct device_attribute *attr,
  310. const char *buf,
  311. size_t count)
  312. {
  313. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  314. struct radeon_device *rdev = ddev->dev_private;
  315. mutex_lock(&rdev->pm.mutex);
  316. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  317. if (strncmp("default", buf, strlen("default")) == 0)
  318. rdev->pm.profile = PM_PROFILE_DEFAULT;
  319. else if (strncmp("auto", buf, strlen("auto")) == 0)
  320. rdev->pm.profile = PM_PROFILE_AUTO;
  321. else if (strncmp("low", buf, strlen("low")) == 0)
  322. rdev->pm.profile = PM_PROFILE_LOW;
  323. else if (strncmp("mid", buf, strlen("mid")) == 0)
  324. rdev->pm.profile = PM_PROFILE_MID;
  325. else if (strncmp("high", buf, strlen("high")) == 0)
  326. rdev->pm.profile = PM_PROFILE_HIGH;
  327. else {
  328. count = -EINVAL;
  329. goto fail;
  330. }
  331. radeon_pm_update_profile(rdev);
  332. radeon_pm_set_clocks(rdev);
  333. } else
  334. count = -EINVAL;
  335. fail:
  336. mutex_unlock(&rdev->pm.mutex);
  337. return count;
  338. }
  339. static ssize_t radeon_get_pm_method(struct device *dev,
  340. struct device_attribute *attr,
  341. char *buf)
  342. {
  343. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  344. struct radeon_device *rdev = ddev->dev_private;
  345. int pm = rdev->pm.pm_method;
  346. return snprintf(buf, PAGE_SIZE, "%s\n",
  347. (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
  348. }
  349. static ssize_t radeon_set_pm_method(struct device *dev,
  350. struct device_attribute *attr,
  351. const char *buf,
  352. size_t count)
  353. {
  354. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  355. struct radeon_device *rdev = ddev->dev_private;
  356. if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
  357. mutex_lock(&rdev->pm.mutex);
  358. rdev->pm.pm_method = PM_METHOD_DYNPM;
  359. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  360. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  361. mutex_unlock(&rdev->pm.mutex);
  362. } else if (strncmp("profile", buf, strlen("profile")) == 0) {
  363. mutex_lock(&rdev->pm.mutex);
  364. /* disable dynpm */
  365. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  366. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  367. rdev->pm.pm_method = PM_METHOD_PROFILE;
  368. mutex_unlock(&rdev->pm.mutex);
  369. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  370. } else {
  371. count = -EINVAL;
  372. goto fail;
  373. }
  374. radeon_pm_compute_clocks(rdev);
  375. fail:
  376. return count;
  377. }
  378. static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
  379. static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
  380. static ssize_t radeon_hwmon_show_temp(struct device *dev,
  381. struct device_attribute *attr,
  382. char *buf)
  383. {
  384. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  385. struct radeon_device *rdev = ddev->dev_private;
  386. int temp;
  387. switch (rdev->pm.int_thermal_type) {
  388. case THERMAL_TYPE_RV6XX:
  389. temp = rv6xx_get_temp(rdev);
  390. break;
  391. case THERMAL_TYPE_RV770:
  392. temp = rv770_get_temp(rdev);
  393. break;
  394. case THERMAL_TYPE_EVERGREEN:
  395. case THERMAL_TYPE_NI:
  396. temp = evergreen_get_temp(rdev);
  397. break;
  398. case THERMAL_TYPE_SUMO:
  399. temp = sumo_get_temp(rdev);
  400. break;
  401. case THERMAL_TYPE_SI:
  402. temp = si_get_temp(rdev);
  403. break;
  404. default:
  405. temp = 0;
  406. break;
  407. }
  408. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  409. }
  410. static ssize_t radeon_hwmon_show_name(struct device *dev,
  411. struct device_attribute *attr,
  412. char *buf)
  413. {
  414. return sprintf(buf, "radeon\n");
  415. }
  416. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
  417. static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
  418. static struct attribute *hwmon_attributes[] = {
  419. &sensor_dev_attr_temp1_input.dev_attr.attr,
  420. &sensor_dev_attr_name.dev_attr.attr,
  421. NULL
  422. };
  423. static const struct attribute_group hwmon_attrgroup = {
  424. .attrs = hwmon_attributes,
  425. };
  426. static int radeon_hwmon_init(struct radeon_device *rdev)
  427. {
  428. int err = 0;
  429. rdev->pm.int_hwmon_dev = NULL;
  430. switch (rdev->pm.int_thermal_type) {
  431. case THERMAL_TYPE_RV6XX:
  432. case THERMAL_TYPE_RV770:
  433. case THERMAL_TYPE_EVERGREEN:
  434. case THERMAL_TYPE_NI:
  435. case THERMAL_TYPE_SUMO:
  436. case THERMAL_TYPE_SI:
  437. /* No support for TN yet */
  438. if (rdev->family == CHIP_ARUBA)
  439. return err;
  440. rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
  441. if (IS_ERR(rdev->pm.int_hwmon_dev)) {
  442. err = PTR_ERR(rdev->pm.int_hwmon_dev);
  443. dev_err(rdev->dev,
  444. "Unable to register hwmon device: %d\n", err);
  445. break;
  446. }
  447. dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
  448. err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
  449. &hwmon_attrgroup);
  450. if (err) {
  451. dev_err(rdev->dev,
  452. "Unable to create hwmon sysfs file: %d\n", err);
  453. hwmon_device_unregister(rdev->dev);
  454. }
  455. break;
  456. default:
  457. break;
  458. }
  459. return err;
  460. }
  461. static void radeon_hwmon_fini(struct radeon_device *rdev)
  462. {
  463. if (rdev->pm.int_hwmon_dev) {
  464. sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
  465. hwmon_device_unregister(rdev->pm.int_hwmon_dev);
  466. }
  467. }
  468. void radeon_pm_suspend(struct radeon_device *rdev)
  469. {
  470. mutex_lock(&rdev->pm.mutex);
  471. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  472. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
  473. rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
  474. }
  475. mutex_unlock(&rdev->pm.mutex);
  476. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  477. }
  478. void radeon_pm_resume(struct radeon_device *rdev)
  479. {
  480. /* set up the default clocks if the MC ucode is loaded */
  481. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  482. if (rdev->pm.default_vddc)
  483. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  484. SET_VOLTAGE_TYPE_ASIC_VDDC);
  485. if (rdev->pm.default_vddci)
  486. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  487. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  488. if (rdev->pm.default_sclk)
  489. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  490. if (rdev->pm.default_mclk)
  491. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  492. }
  493. /* asic init will reset the default power state */
  494. mutex_lock(&rdev->pm.mutex);
  495. rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
  496. rdev->pm.current_clock_mode_index = 0;
  497. rdev->pm.current_sclk = rdev->pm.default_sclk;
  498. rdev->pm.current_mclk = rdev->pm.default_mclk;
  499. rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
  500. rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
  501. if (rdev->pm.pm_method == PM_METHOD_DYNPM
  502. && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
  503. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  504. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  505. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  506. }
  507. mutex_unlock(&rdev->pm.mutex);
  508. radeon_pm_compute_clocks(rdev);
  509. }
  510. int radeon_pm_init(struct radeon_device *rdev)
  511. {
  512. int ret;
  513. /* default to profile method */
  514. rdev->pm.pm_method = PM_METHOD_PROFILE;
  515. rdev->pm.profile = PM_PROFILE_DEFAULT;
  516. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  517. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  518. rdev->pm.dynpm_can_upclock = true;
  519. rdev->pm.dynpm_can_downclock = true;
  520. rdev->pm.default_sclk = rdev->clock.default_sclk;
  521. rdev->pm.default_mclk = rdev->clock.default_mclk;
  522. rdev->pm.current_sclk = rdev->clock.default_sclk;
  523. rdev->pm.current_mclk = rdev->clock.default_mclk;
  524. rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  525. if (rdev->bios) {
  526. if (rdev->is_atom_bios)
  527. radeon_atombios_get_power_modes(rdev);
  528. else
  529. radeon_combios_get_power_modes(rdev);
  530. radeon_pm_print_states(rdev);
  531. radeon_pm_init_profile(rdev);
  532. /* set up the default clocks if the MC ucode is loaded */
  533. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  534. if (rdev->pm.default_vddc)
  535. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  536. SET_VOLTAGE_TYPE_ASIC_VDDC);
  537. if (rdev->pm.default_vddci)
  538. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  539. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  540. if (rdev->pm.default_sclk)
  541. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  542. if (rdev->pm.default_mclk)
  543. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  544. }
  545. }
  546. /* set up the internal thermal sensor if applicable */
  547. ret = radeon_hwmon_init(rdev);
  548. if (ret)
  549. return ret;
  550. INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
  551. if (rdev->pm.num_power_states > 1) {
  552. /* where's the best place to put these? */
  553. ret = device_create_file(rdev->dev, &dev_attr_power_profile);
  554. if (ret)
  555. DRM_ERROR("failed to create device file for power profile\n");
  556. ret = device_create_file(rdev->dev, &dev_attr_power_method);
  557. if (ret)
  558. DRM_ERROR("failed to create device file for power method\n");
  559. #ifdef CONFIG_ACPI
  560. rdev->acpi_nb.notifier_call = radeon_acpi_event;
  561. register_acpi_notifier(&rdev->acpi_nb);
  562. #endif
  563. if (radeon_debugfs_pm_init(rdev)) {
  564. DRM_ERROR("Failed to register debugfs file for PM!\n");
  565. }
  566. DRM_INFO("radeon: power management initialized\n");
  567. }
  568. return 0;
  569. }
  570. void radeon_pm_fini(struct radeon_device *rdev)
  571. {
  572. if (rdev->pm.num_power_states > 1) {
  573. mutex_lock(&rdev->pm.mutex);
  574. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  575. rdev->pm.profile = PM_PROFILE_DEFAULT;
  576. radeon_pm_update_profile(rdev);
  577. radeon_pm_set_clocks(rdev);
  578. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  579. /* reset default clocks */
  580. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  581. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  582. radeon_pm_set_clocks(rdev);
  583. }
  584. mutex_unlock(&rdev->pm.mutex);
  585. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  586. device_remove_file(rdev->dev, &dev_attr_power_profile);
  587. device_remove_file(rdev->dev, &dev_attr_power_method);
  588. #ifdef CONFIG_ACPI
  589. unregister_acpi_notifier(&rdev->acpi_nb);
  590. #endif
  591. }
  592. if (rdev->pm.power_state)
  593. kfree(rdev->pm.power_state);
  594. radeon_hwmon_fini(rdev);
  595. }
  596. void radeon_pm_compute_clocks(struct radeon_device *rdev)
  597. {
  598. struct drm_device *ddev = rdev->ddev;
  599. struct drm_crtc *crtc;
  600. struct radeon_crtc *radeon_crtc;
  601. if (rdev->pm.num_power_states < 2)
  602. return;
  603. mutex_lock(&rdev->pm.mutex);
  604. rdev->pm.active_crtcs = 0;
  605. rdev->pm.active_crtc_count = 0;
  606. list_for_each_entry(crtc,
  607. &ddev->mode_config.crtc_list, head) {
  608. radeon_crtc = to_radeon_crtc(crtc);
  609. if (radeon_crtc->enabled) {
  610. rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
  611. rdev->pm.active_crtc_count++;
  612. }
  613. }
  614. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  615. radeon_pm_update_profile(rdev);
  616. radeon_pm_set_clocks(rdev);
  617. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  618. if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
  619. if (rdev->pm.active_crtc_count > 1) {
  620. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  621. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  622. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  623. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  624. radeon_pm_get_dynpm_state(rdev);
  625. radeon_pm_set_clocks(rdev);
  626. DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
  627. }
  628. } else if (rdev->pm.active_crtc_count == 1) {
  629. /* TODO: Increase clocks if needed for current mode */
  630. if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
  631. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  632. rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
  633. radeon_pm_get_dynpm_state(rdev);
  634. radeon_pm_set_clocks(rdev);
  635. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  636. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  637. } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
  638. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  639. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  640. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  641. DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
  642. }
  643. } else { /* count == 0 */
  644. if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
  645. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  646. rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
  647. rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
  648. radeon_pm_get_dynpm_state(rdev);
  649. radeon_pm_set_clocks(rdev);
  650. }
  651. }
  652. }
  653. }
  654. mutex_unlock(&rdev->pm.mutex);
  655. }
  656. static bool radeon_pm_in_vbl(struct radeon_device *rdev)
  657. {
  658. int crtc, vpos, hpos, vbl_status;
  659. bool in_vbl = true;
  660. /* Iterate over all active crtc's. All crtc's must be in vblank,
  661. * otherwise return in_vbl == false.
  662. */
  663. for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
  664. if (rdev->pm.active_crtcs & (1 << crtc)) {
  665. vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
  666. if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
  667. !(vbl_status & DRM_SCANOUTPOS_INVBL))
  668. in_vbl = false;
  669. }
  670. }
  671. return in_vbl;
  672. }
  673. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
  674. {
  675. u32 stat_crtc = 0;
  676. bool in_vbl = radeon_pm_in_vbl(rdev);
  677. if (in_vbl == false)
  678. DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
  679. finish ? "exit" : "entry");
  680. return in_vbl;
  681. }
  682. static void radeon_dynpm_idle_work_handler(struct work_struct *work)
  683. {
  684. struct radeon_device *rdev;
  685. int resched;
  686. rdev = container_of(work, struct radeon_device,
  687. pm.dynpm_idle_work.work);
  688. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  689. mutex_lock(&rdev->pm.mutex);
  690. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  691. int not_processed = 0;
  692. int i;
  693. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  694. struct radeon_ring *ring = &rdev->ring[i];
  695. if (ring->ready) {
  696. not_processed += radeon_fence_count_emitted(rdev, i);
  697. if (not_processed >= 3)
  698. break;
  699. }
  700. }
  701. if (not_processed >= 3) { /* should upclock */
  702. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
  703. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  704. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  705. rdev->pm.dynpm_can_upclock) {
  706. rdev->pm.dynpm_planned_action =
  707. DYNPM_ACTION_UPCLOCK;
  708. rdev->pm.dynpm_action_timeout = jiffies +
  709. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  710. }
  711. } else if (not_processed == 0) { /* should downclock */
  712. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
  713. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  714. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  715. rdev->pm.dynpm_can_downclock) {
  716. rdev->pm.dynpm_planned_action =
  717. DYNPM_ACTION_DOWNCLOCK;
  718. rdev->pm.dynpm_action_timeout = jiffies +
  719. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  720. }
  721. }
  722. /* Note, radeon_pm_set_clocks is called with static_switch set
  723. * to false since we want to wait for vbl to avoid flicker.
  724. */
  725. if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
  726. jiffies > rdev->pm.dynpm_action_timeout) {
  727. radeon_pm_get_dynpm_state(rdev);
  728. radeon_pm_set_clocks(rdev);
  729. }
  730. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  731. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  732. }
  733. mutex_unlock(&rdev->pm.mutex);
  734. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  735. }
  736. /*
  737. * Debugfs info
  738. */
  739. #if defined(CONFIG_DEBUG_FS)
  740. static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
  741. {
  742. struct drm_info_node *node = (struct drm_info_node *) m->private;
  743. struct drm_device *dev = node->minor->dev;
  744. struct radeon_device *rdev = dev->dev_private;
  745. seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
  746. seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
  747. seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
  748. if (rdev->asic->pm.get_memory_clock)
  749. seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
  750. if (rdev->pm.current_vddc)
  751. seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
  752. if (rdev->asic->pm.get_pcie_lanes)
  753. seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
  754. return 0;
  755. }
  756. static struct drm_info_list radeon_pm_info_list[] = {
  757. {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
  758. };
  759. #endif
  760. static int radeon_debugfs_pm_init(struct radeon_device *rdev)
  761. {
  762. #if defined(CONFIG_DEBUG_FS)
  763. return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
  764. #else
  765. return 0;
  766. #endif
  767. }