radeon_pm.c 27 KB

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