radeon_pm.c 27 KB

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