radeon_pm.c 26 KB

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