radeon_pm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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_NI:
  429. case THERMAL_TYPE_SUMO:
  430. rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
  431. if (IS_ERR(rdev->pm.int_hwmon_dev)) {
  432. err = PTR_ERR(rdev->pm.int_hwmon_dev);
  433. dev_err(rdev->dev,
  434. "Unable to register hwmon device: %d\n", err);
  435. break;
  436. }
  437. dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
  438. err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
  439. &hwmon_attrgroup);
  440. if (err) {
  441. dev_err(rdev->dev,
  442. "Unable to create hwmon sysfs file: %d\n", err);
  443. hwmon_device_unregister(rdev->dev);
  444. }
  445. break;
  446. default:
  447. break;
  448. }
  449. return err;
  450. }
  451. static void radeon_hwmon_fini(struct radeon_device *rdev)
  452. {
  453. if (rdev->pm.int_hwmon_dev) {
  454. sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
  455. hwmon_device_unregister(rdev->pm.int_hwmon_dev);
  456. }
  457. }
  458. void radeon_pm_suspend(struct radeon_device *rdev)
  459. {
  460. mutex_lock(&rdev->pm.mutex);
  461. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  462. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
  463. rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
  464. }
  465. mutex_unlock(&rdev->pm.mutex);
  466. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  467. }
  468. void radeon_pm_resume(struct radeon_device *rdev)
  469. {
  470. /* set up the default clocks if the MC ucode is loaded */
  471. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  472. if (rdev->pm.default_vddc)
  473. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  474. SET_VOLTAGE_TYPE_ASIC_VDDC);
  475. if (rdev->pm.default_vddci)
  476. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  477. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  478. if (rdev->pm.default_sclk)
  479. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  480. if (rdev->pm.default_mclk)
  481. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  482. }
  483. /* asic init will reset the default power state */
  484. mutex_lock(&rdev->pm.mutex);
  485. rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
  486. rdev->pm.current_clock_mode_index = 0;
  487. rdev->pm.current_sclk = rdev->pm.default_sclk;
  488. rdev->pm.current_mclk = rdev->pm.default_mclk;
  489. rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
  490. rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
  491. if (rdev->pm.pm_method == PM_METHOD_DYNPM
  492. && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
  493. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  494. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  495. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  496. }
  497. mutex_unlock(&rdev->pm.mutex);
  498. radeon_pm_compute_clocks(rdev);
  499. }
  500. int radeon_pm_init(struct radeon_device *rdev)
  501. {
  502. int ret;
  503. /* default to profile method */
  504. rdev->pm.pm_method = PM_METHOD_PROFILE;
  505. rdev->pm.profile = PM_PROFILE_DEFAULT;
  506. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  507. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  508. rdev->pm.dynpm_can_upclock = true;
  509. rdev->pm.dynpm_can_downclock = true;
  510. rdev->pm.default_sclk = rdev->clock.default_sclk;
  511. rdev->pm.default_mclk = rdev->clock.default_mclk;
  512. rdev->pm.current_sclk = rdev->clock.default_sclk;
  513. rdev->pm.current_mclk = rdev->clock.default_mclk;
  514. rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  515. if (rdev->bios) {
  516. if (rdev->is_atom_bios)
  517. radeon_atombios_get_power_modes(rdev);
  518. else
  519. radeon_combios_get_power_modes(rdev);
  520. radeon_pm_print_states(rdev);
  521. radeon_pm_init_profile(rdev);
  522. /* set up the default clocks if the MC ucode is loaded */
  523. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  524. if (rdev->pm.default_vddc)
  525. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  526. SET_VOLTAGE_TYPE_ASIC_VDDC);
  527. if (rdev->pm.default_vddci)
  528. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  529. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  530. if (rdev->pm.default_sclk)
  531. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  532. if (rdev->pm.default_mclk)
  533. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  534. }
  535. }
  536. /* set up the internal thermal sensor if applicable */
  537. ret = radeon_hwmon_init(rdev);
  538. if (ret)
  539. return ret;
  540. INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
  541. if (rdev->pm.num_power_states > 1) {
  542. /* where's the best place to put these? */
  543. ret = device_create_file(rdev->dev, &dev_attr_power_profile);
  544. if (ret)
  545. DRM_ERROR("failed to create device file for power profile\n");
  546. ret = device_create_file(rdev->dev, &dev_attr_power_method);
  547. if (ret)
  548. DRM_ERROR("failed to create device file for power method\n");
  549. #ifdef CONFIG_ACPI
  550. rdev->acpi_nb.notifier_call = radeon_acpi_event;
  551. register_acpi_notifier(&rdev->acpi_nb);
  552. #endif
  553. if (radeon_debugfs_pm_init(rdev)) {
  554. DRM_ERROR("Failed to register debugfs file for PM!\n");
  555. }
  556. DRM_INFO("radeon: power management initialized\n");
  557. }
  558. return 0;
  559. }
  560. void radeon_pm_fini(struct radeon_device *rdev)
  561. {
  562. if (rdev->pm.num_power_states > 1) {
  563. mutex_lock(&rdev->pm.mutex);
  564. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  565. rdev->pm.profile = PM_PROFILE_DEFAULT;
  566. radeon_pm_update_profile(rdev);
  567. radeon_pm_set_clocks(rdev);
  568. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  569. /* reset default clocks */
  570. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  571. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  572. radeon_pm_set_clocks(rdev);
  573. }
  574. mutex_unlock(&rdev->pm.mutex);
  575. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  576. device_remove_file(rdev->dev, &dev_attr_power_profile);
  577. device_remove_file(rdev->dev, &dev_attr_power_method);
  578. #ifdef CONFIG_ACPI
  579. unregister_acpi_notifier(&rdev->acpi_nb);
  580. #endif
  581. }
  582. if (rdev->pm.power_state)
  583. kfree(rdev->pm.power_state);
  584. radeon_hwmon_fini(rdev);
  585. }
  586. void radeon_pm_compute_clocks(struct radeon_device *rdev)
  587. {
  588. struct drm_device *ddev = rdev->ddev;
  589. struct drm_crtc *crtc;
  590. struct radeon_crtc *radeon_crtc;
  591. if (rdev->pm.num_power_states < 2)
  592. return;
  593. mutex_lock(&rdev->pm.mutex);
  594. rdev->pm.active_crtcs = 0;
  595. rdev->pm.active_crtc_count = 0;
  596. list_for_each_entry(crtc,
  597. &ddev->mode_config.crtc_list, head) {
  598. radeon_crtc = to_radeon_crtc(crtc);
  599. if (radeon_crtc->enabled) {
  600. rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
  601. rdev->pm.active_crtc_count++;
  602. }
  603. }
  604. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  605. radeon_pm_update_profile(rdev);
  606. radeon_pm_set_clocks(rdev);
  607. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  608. if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
  609. if (rdev->pm.active_crtc_count > 1) {
  610. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  611. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  612. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  613. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  614. radeon_pm_get_dynpm_state(rdev);
  615. radeon_pm_set_clocks(rdev);
  616. DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
  617. }
  618. } else if (rdev->pm.active_crtc_count == 1) {
  619. /* TODO: Increase clocks if needed for current mode */
  620. if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
  621. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  622. rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
  623. radeon_pm_get_dynpm_state(rdev);
  624. radeon_pm_set_clocks(rdev);
  625. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  626. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  627. } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
  628. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  629. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  630. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  631. DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
  632. }
  633. } else { /* count == 0 */
  634. if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
  635. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  636. rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
  637. rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
  638. radeon_pm_get_dynpm_state(rdev);
  639. radeon_pm_set_clocks(rdev);
  640. }
  641. }
  642. }
  643. }
  644. mutex_unlock(&rdev->pm.mutex);
  645. }
  646. static bool radeon_pm_in_vbl(struct radeon_device *rdev)
  647. {
  648. int crtc, vpos, hpos, vbl_status;
  649. bool in_vbl = true;
  650. /* Iterate over all active crtc's. All crtc's must be in vblank,
  651. * otherwise return in_vbl == false.
  652. */
  653. for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
  654. if (rdev->pm.active_crtcs & (1 << crtc)) {
  655. vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
  656. if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
  657. !(vbl_status & DRM_SCANOUTPOS_INVBL))
  658. in_vbl = false;
  659. }
  660. }
  661. return in_vbl;
  662. }
  663. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
  664. {
  665. u32 stat_crtc = 0;
  666. bool in_vbl = radeon_pm_in_vbl(rdev);
  667. if (in_vbl == false)
  668. DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
  669. finish ? "exit" : "entry");
  670. return in_vbl;
  671. }
  672. static void radeon_dynpm_idle_work_handler(struct work_struct *work)
  673. {
  674. struct radeon_device *rdev;
  675. int resched;
  676. rdev = container_of(work, struct radeon_device,
  677. pm.dynpm_idle_work.work);
  678. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  679. mutex_lock(&rdev->pm.mutex);
  680. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  681. unsigned long irq_flags;
  682. int not_processed = 0;
  683. read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
  684. if (!list_empty(&rdev->fence_drv.emited)) {
  685. struct list_head *ptr;
  686. list_for_each(ptr, &rdev->fence_drv.emited) {
  687. /* count up to 3, that's enought info */
  688. if (++not_processed >= 3)
  689. break;
  690. }
  691. }
  692. read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
  693. if (not_processed >= 3) { /* should upclock */
  694. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
  695. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  696. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  697. rdev->pm.dynpm_can_upclock) {
  698. rdev->pm.dynpm_planned_action =
  699. DYNPM_ACTION_UPCLOCK;
  700. rdev->pm.dynpm_action_timeout = jiffies +
  701. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  702. }
  703. } else if (not_processed == 0) { /* should downclock */
  704. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
  705. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  706. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  707. rdev->pm.dynpm_can_downclock) {
  708. rdev->pm.dynpm_planned_action =
  709. DYNPM_ACTION_DOWNCLOCK;
  710. rdev->pm.dynpm_action_timeout = jiffies +
  711. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  712. }
  713. }
  714. /* Note, radeon_pm_set_clocks is called with static_switch set
  715. * to false since we want to wait for vbl to avoid flicker.
  716. */
  717. if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
  718. jiffies > rdev->pm.dynpm_action_timeout) {
  719. radeon_pm_get_dynpm_state(rdev);
  720. radeon_pm_set_clocks(rdev);
  721. }
  722. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  723. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  724. }
  725. mutex_unlock(&rdev->pm.mutex);
  726. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  727. }
  728. /*
  729. * Debugfs info
  730. */
  731. #if defined(CONFIG_DEBUG_FS)
  732. static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
  733. {
  734. struct drm_info_node *node = (struct drm_info_node *) m->private;
  735. struct drm_device *dev = node->minor->dev;
  736. struct radeon_device *rdev = dev->dev_private;
  737. seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
  738. seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
  739. seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
  740. if (rdev->asic->get_memory_clock)
  741. seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
  742. if (rdev->pm.current_vddc)
  743. seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
  744. if (rdev->asic->get_pcie_lanes)
  745. seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
  746. return 0;
  747. }
  748. static struct drm_info_list radeon_pm_info_list[] = {
  749. {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
  750. };
  751. #endif
  752. static int radeon_debugfs_pm_init(struct radeon_device *rdev)
  753. {
  754. #if defined(CONFIG_DEBUG_FS)
  755. return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
  756. #else
  757. return 0;
  758. #endif
  759. }