radeon_pm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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->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. mutex_lock(&rdev->cp.mutex);
  223. /* gui idle int has issues on older chips it seems */
  224. if (rdev->family >= CHIP_R600) {
  225. if (rdev->irq.installed) {
  226. /* wait for GPU idle */
  227. rdev->pm.gui_idle = false;
  228. rdev->irq.gui_idle = true;
  229. radeon_irq_set(rdev);
  230. wait_event_interruptible_timeout(
  231. rdev->irq.idle_queue, rdev->pm.gui_idle,
  232. msecs_to_jiffies(RADEON_WAIT_IDLE_TIMEOUT));
  233. rdev->irq.gui_idle = false;
  234. radeon_irq_set(rdev);
  235. }
  236. } else {
  237. if (rdev->cp.ready) {
  238. struct radeon_fence *fence;
  239. radeon_ring_alloc(rdev, 64);
  240. radeon_fence_create(rdev, &fence);
  241. radeon_fence_emit(rdev, fence);
  242. radeon_ring_commit(rdev);
  243. radeon_fence_wait(fence, false);
  244. radeon_fence_unref(&fence);
  245. }
  246. }
  247. radeon_unmap_vram_bos(rdev);
  248. if (rdev->irq.installed) {
  249. for (i = 0; i < rdev->num_crtc; i++) {
  250. if (rdev->pm.active_crtcs & (1 << i)) {
  251. rdev->pm.req_vblank |= (1 << i);
  252. drm_vblank_get(rdev->ddev, i);
  253. }
  254. }
  255. }
  256. radeon_set_power_state(rdev);
  257. if (rdev->irq.installed) {
  258. for (i = 0; i < rdev->num_crtc; i++) {
  259. if (rdev->pm.req_vblank & (1 << i)) {
  260. rdev->pm.req_vblank &= ~(1 << i);
  261. drm_vblank_put(rdev->ddev, i);
  262. }
  263. }
  264. }
  265. /* update display watermarks based on new power state */
  266. radeon_update_bandwidth_info(rdev);
  267. if (rdev->pm.active_crtc_count)
  268. radeon_bandwidth_update(rdev);
  269. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  270. mutex_unlock(&rdev->cp.mutex);
  271. mutex_unlock(&rdev->vram_mutex);
  272. mutex_unlock(&rdev->ddev->struct_mutex);
  273. }
  274. static void radeon_pm_print_states(struct radeon_device *rdev)
  275. {
  276. int i, j;
  277. struct radeon_power_state *power_state;
  278. struct radeon_pm_clock_info *clock_info;
  279. DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
  280. for (i = 0; i < rdev->pm.num_power_states; i++) {
  281. power_state = &rdev->pm.power_state[i];
  282. DRM_DEBUG_DRIVER("State %d: %s\n", i,
  283. radeon_pm_state_type_name[power_state->type]);
  284. if (i == rdev->pm.default_power_state_index)
  285. DRM_DEBUG_DRIVER("\tDefault");
  286. if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
  287. DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
  288. if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
  289. DRM_DEBUG_DRIVER("\tSingle display only\n");
  290. DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
  291. for (j = 0; j < power_state->num_clock_modes; j++) {
  292. clock_info = &(power_state->clock_info[j]);
  293. if (rdev->flags & RADEON_IS_IGP)
  294. DRM_DEBUG_DRIVER("\t\t%d e: %d%s\n",
  295. j,
  296. clock_info->sclk * 10,
  297. clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
  298. else
  299. DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d%s\n",
  300. j,
  301. clock_info->sclk * 10,
  302. clock_info->mclk * 10,
  303. clock_info->voltage.voltage,
  304. clock_info->flags & RADEON_PM_MODE_NO_DISPLAY ? "\tNo display only" : "");
  305. }
  306. }
  307. }
  308. static ssize_t radeon_get_pm_profile(struct device *dev,
  309. struct device_attribute *attr,
  310. char *buf)
  311. {
  312. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  313. struct radeon_device *rdev = ddev->dev_private;
  314. int cp = rdev->pm.profile;
  315. return snprintf(buf, PAGE_SIZE, "%s\n",
  316. (cp == PM_PROFILE_AUTO) ? "auto" :
  317. (cp == PM_PROFILE_LOW) ? "low" :
  318. (cp == PM_PROFILE_MID) ? "mid" :
  319. (cp == PM_PROFILE_HIGH) ? "high" : "default");
  320. }
  321. static ssize_t radeon_set_pm_profile(struct device *dev,
  322. struct device_attribute *attr,
  323. const char *buf,
  324. size_t count)
  325. {
  326. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  327. struct radeon_device *rdev = ddev->dev_private;
  328. mutex_lock(&rdev->pm.mutex);
  329. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  330. if (strncmp("default", buf, strlen("default")) == 0)
  331. rdev->pm.profile = PM_PROFILE_DEFAULT;
  332. else if (strncmp("auto", buf, strlen("auto")) == 0)
  333. rdev->pm.profile = PM_PROFILE_AUTO;
  334. else if (strncmp("low", buf, strlen("low")) == 0)
  335. rdev->pm.profile = PM_PROFILE_LOW;
  336. else if (strncmp("mid", buf, strlen("mid")) == 0)
  337. rdev->pm.profile = PM_PROFILE_MID;
  338. else if (strncmp("high", buf, strlen("high")) == 0)
  339. rdev->pm.profile = PM_PROFILE_HIGH;
  340. else {
  341. count = -EINVAL;
  342. goto fail;
  343. }
  344. radeon_pm_update_profile(rdev);
  345. radeon_pm_set_clocks(rdev);
  346. } else
  347. count = -EINVAL;
  348. fail:
  349. mutex_unlock(&rdev->pm.mutex);
  350. return count;
  351. }
  352. static ssize_t radeon_get_pm_method(struct device *dev,
  353. struct device_attribute *attr,
  354. char *buf)
  355. {
  356. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  357. struct radeon_device *rdev = ddev->dev_private;
  358. int pm = rdev->pm.pm_method;
  359. return snprintf(buf, PAGE_SIZE, "%s\n",
  360. (pm == PM_METHOD_DYNPM) ? "dynpm" : "profile");
  361. }
  362. static ssize_t radeon_set_pm_method(struct device *dev,
  363. struct device_attribute *attr,
  364. const char *buf,
  365. size_t count)
  366. {
  367. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  368. struct radeon_device *rdev = ddev->dev_private;
  369. if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
  370. mutex_lock(&rdev->pm.mutex);
  371. rdev->pm.pm_method = PM_METHOD_DYNPM;
  372. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  373. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  374. mutex_unlock(&rdev->pm.mutex);
  375. } else if (strncmp("profile", buf, strlen("profile")) == 0) {
  376. mutex_lock(&rdev->pm.mutex);
  377. /* disable dynpm */
  378. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  379. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  380. rdev->pm.pm_method = PM_METHOD_PROFILE;
  381. mutex_unlock(&rdev->pm.mutex);
  382. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  383. } else {
  384. count = -EINVAL;
  385. goto fail;
  386. }
  387. radeon_pm_compute_clocks(rdev);
  388. fail:
  389. return count;
  390. }
  391. static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
  392. static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
  393. static ssize_t radeon_hwmon_show_temp(struct device *dev,
  394. struct device_attribute *attr,
  395. char *buf)
  396. {
  397. struct drm_device *ddev = pci_get_drvdata(to_pci_dev(dev));
  398. struct radeon_device *rdev = ddev->dev_private;
  399. int temp;
  400. switch (rdev->pm.int_thermal_type) {
  401. case THERMAL_TYPE_RV6XX:
  402. temp = rv6xx_get_temp(rdev);
  403. break;
  404. case THERMAL_TYPE_RV770:
  405. temp = rv770_get_temp(rdev);
  406. break;
  407. case THERMAL_TYPE_EVERGREEN:
  408. case THERMAL_TYPE_NI:
  409. temp = evergreen_get_temp(rdev);
  410. break;
  411. case THERMAL_TYPE_SUMO:
  412. temp = sumo_get_temp(rdev);
  413. break;
  414. default:
  415. temp = 0;
  416. break;
  417. }
  418. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  419. }
  420. static ssize_t radeon_hwmon_show_name(struct device *dev,
  421. struct device_attribute *attr,
  422. char *buf)
  423. {
  424. return sprintf(buf, "radeon\n");
  425. }
  426. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
  427. static SENSOR_DEVICE_ATTR(name, S_IRUGO, radeon_hwmon_show_name, NULL, 0);
  428. static struct attribute *hwmon_attributes[] = {
  429. &sensor_dev_attr_temp1_input.dev_attr.attr,
  430. &sensor_dev_attr_name.dev_attr.attr,
  431. NULL
  432. };
  433. static const struct attribute_group hwmon_attrgroup = {
  434. .attrs = hwmon_attributes,
  435. };
  436. static int radeon_hwmon_init(struct radeon_device *rdev)
  437. {
  438. int err = 0;
  439. rdev->pm.int_hwmon_dev = NULL;
  440. switch (rdev->pm.int_thermal_type) {
  441. case THERMAL_TYPE_RV6XX:
  442. case THERMAL_TYPE_RV770:
  443. case THERMAL_TYPE_EVERGREEN:
  444. case THERMAL_TYPE_NI:
  445. case THERMAL_TYPE_SUMO:
  446. rdev->pm.int_hwmon_dev = hwmon_device_register(rdev->dev);
  447. if (IS_ERR(rdev->pm.int_hwmon_dev)) {
  448. err = PTR_ERR(rdev->pm.int_hwmon_dev);
  449. dev_err(rdev->dev,
  450. "Unable to register hwmon device: %d\n", err);
  451. break;
  452. }
  453. dev_set_drvdata(rdev->pm.int_hwmon_dev, rdev->ddev);
  454. err = sysfs_create_group(&rdev->pm.int_hwmon_dev->kobj,
  455. &hwmon_attrgroup);
  456. if (err) {
  457. dev_err(rdev->dev,
  458. "Unable to create hwmon sysfs file: %d\n", err);
  459. hwmon_device_unregister(rdev->dev);
  460. }
  461. break;
  462. default:
  463. break;
  464. }
  465. return err;
  466. }
  467. static void radeon_hwmon_fini(struct radeon_device *rdev)
  468. {
  469. if (rdev->pm.int_hwmon_dev) {
  470. sysfs_remove_group(&rdev->pm.int_hwmon_dev->kobj, &hwmon_attrgroup);
  471. hwmon_device_unregister(rdev->pm.int_hwmon_dev);
  472. }
  473. }
  474. void radeon_pm_suspend(struct radeon_device *rdev)
  475. {
  476. mutex_lock(&rdev->pm.mutex);
  477. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  478. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
  479. rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
  480. }
  481. mutex_unlock(&rdev->pm.mutex);
  482. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  483. }
  484. void radeon_pm_resume(struct radeon_device *rdev)
  485. {
  486. /* set up the default clocks if the MC ucode is loaded */
  487. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  488. if (rdev->pm.default_vddc)
  489. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  490. SET_VOLTAGE_TYPE_ASIC_VDDC);
  491. if (rdev->pm.default_vddci)
  492. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  493. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  494. if (rdev->pm.default_sclk)
  495. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  496. if (rdev->pm.default_mclk)
  497. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  498. }
  499. /* asic init will reset the default power state */
  500. mutex_lock(&rdev->pm.mutex);
  501. rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
  502. rdev->pm.current_clock_mode_index = 0;
  503. rdev->pm.current_sclk = rdev->pm.default_sclk;
  504. rdev->pm.current_mclk = rdev->pm.default_mclk;
  505. rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
  506. rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
  507. if (rdev->pm.pm_method == PM_METHOD_DYNPM
  508. && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
  509. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  510. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  511. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  512. }
  513. mutex_unlock(&rdev->pm.mutex);
  514. radeon_pm_compute_clocks(rdev);
  515. }
  516. int radeon_pm_init(struct radeon_device *rdev)
  517. {
  518. int ret;
  519. /* default to profile method */
  520. rdev->pm.pm_method = PM_METHOD_PROFILE;
  521. rdev->pm.profile = PM_PROFILE_DEFAULT;
  522. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  523. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  524. rdev->pm.dynpm_can_upclock = true;
  525. rdev->pm.dynpm_can_downclock = true;
  526. rdev->pm.default_sclk = rdev->clock.default_sclk;
  527. rdev->pm.default_mclk = rdev->clock.default_mclk;
  528. rdev->pm.current_sclk = rdev->clock.default_sclk;
  529. rdev->pm.current_mclk = rdev->clock.default_mclk;
  530. rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  531. if (rdev->bios) {
  532. if (rdev->is_atom_bios)
  533. radeon_atombios_get_power_modes(rdev);
  534. else
  535. radeon_combios_get_power_modes(rdev);
  536. radeon_pm_print_states(rdev);
  537. radeon_pm_init_profile(rdev);
  538. /* set up the default clocks if the MC ucode is loaded */
  539. if (ASIC_IS_DCE5(rdev) && rdev->mc_fw) {
  540. if (rdev->pm.default_vddc)
  541. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  542. SET_VOLTAGE_TYPE_ASIC_VDDC);
  543. if (rdev->pm.default_vddci)
  544. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  545. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  546. if (rdev->pm.default_sclk)
  547. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  548. if (rdev->pm.default_mclk)
  549. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  550. }
  551. }
  552. /* set up the internal thermal sensor if applicable */
  553. ret = radeon_hwmon_init(rdev);
  554. if (ret)
  555. return ret;
  556. INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
  557. if (rdev->pm.num_power_states > 1) {
  558. /* where's the best place to put these? */
  559. ret = device_create_file(rdev->dev, &dev_attr_power_profile);
  560. if (ret)
  561. DRM_ERROR("failed to create device file for power profile\n");
  562. ret = device_create_file(rdev->dev, &dev_attr_power_method);
  563. if (ret)
  564. DRM_ERROR("failed to create device file for power method\n");
  565. #ifdef CONFIG_ACPI
  566. rdev->acpi_nb.notifier_call = radeon_acpi_event;
  567. register_acpi_notifier(&rdev->acpi_nb);
  568. #endif
  569. if (radeon_debugfs_pm_init(rdev)) {
  570. DRM_ERROR("Failed to register debugfs file for PM!\n");
  571. }
  572. DRM_INFO("radeon: power management initialized\n");
  573. }
  574. return 0;
  575. }
  576. void radeon_pm_fini(struct radeon_device *rdev)
  577. {
  578. if (rdev->pm.num_power_states > 1) {
  579. mutex_lock(&rdev->pm.mutex);
  580. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  581. rdev->pm.profile = PM_PROFILE_DEFAULT;
  582. radeon_pm_update_profile(rdev);
  583. radeon_pm_set_clocks(rdev);
  584. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  585. /* reset default clocks */
  586. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  587. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  588. radeon_pm_set_clocks(rdev);
  589. }
  590. mutex_unlock(&rdev->pm.mutex);
  591. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  592. device_remove_file(rdev->dev, &dev_attr_power_profile);
  593. device_remove_file(rdev->dev, &dev_attr_power_method);
  594. #ifdef CONFIG_ACPI
  595. unregister_acpi_notifier(&rdev->acpi_nb);
  596. #endif
  597. }
  598. if (rdev->pm.power_state)
  599. kfree(rdev->pm.power_state);
  600. radeon_hwmon_fini(rdev);
  601. }
  602. void radeon_pm_compute_clocks(struct radeon_device *rdev)
  603. {
  604. struct drm_device *ddev = rdev->ddev;
  605. struct drm_crtc *crtc;
  606. struct radeon_crtc *radeon_crtc;
  607. if (rdev->pm.num_power_states < 2)
  608. return;
  609. mutex_lock(&rdev->pm.mutex);
  610. rdev->pm.active_crtcs = 0;
  611. rdev->pm.active_crtc_count = 0;
  612. list_for_each_entry(crtc,
  613. &ddev->mode_config.crtc_list, head) {
  614. radeon_crtc = to_radeon_crtc(crtc);
  615. if (radeon_crtc->enabled) {
  616. rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
  617. rdev->pm.active_crtc_count++;
  618. }
  619. }
  620. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  621. radeon_pm_update_profile(rdev);
  622. radeon_pm_set_clocks(rdev);
  623. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  624. if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
  625. if (rdev->pm.active_crtc_count > 1) {
  626. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  627. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  628. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  629. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  630. radeon_pm_get_dynpm_state(rdev);
  631. radeon_pm_set_clocks(rdev);
  632. DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
  633. }
  634. } else if (rdev->pm.active_crtc_count == 1) {
  635. /* TODO: Increase clocks if needed for current mode */
  636. if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
  637. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  638. rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
  639. radeon_pm_get_dynpm_state(rdev);
  640. radeon_pm_set_clocks(rdev);
  641. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  642. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  643. } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
  644. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  645. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  646. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  647. DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
  648. }
  649. } else { /* count == 0 */
  650. if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
  651. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  652. rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
  653. rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
  654. radeon_pm_get_dynpm_state(rdev);
  655. radeon_pm_set_clocks(rdev);
  656. }
  657. }
  658. }
  659. }
  660. mutex_unlock(&rdev->pm.mutex);
  661. }
  662. static bool radeon_pm_in_vbl(struct radeon_device *rdev)
  663. {
  664. int crtc, vpos, hpos, vbl_status;
  665. bool in_vbl = true;
  666. /* Iterate over all active crtc's. All crtc's must be in vblank,
  667. * otherwise return in_vbl == false.
  668. */
  669. for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
  670. if (rdev->pm.active_crtcs & (1 << crtc)) {
  671. vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, &vpos, &hpos);
  672. if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
  673. !(vbl_status & DRM_SCANOUTPOS_INVBL))
  674. in_vbl = false;
  675. }
  676. }
  677. return in_vbl;
  678. }
  679. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
  680. {
  681. u32 stat_crtc = 0;
  682. bool in_vbl = radeon_pm_in_vbl(rdev);
  683. if (in_vbl == false)
  684. DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
  685. finish ? "exit" : "entry");
  686. return in_vbl;
  687. }
  688. static void radeon_dynpm_idle_work_handler(struct work_struct *work)
  689. {
  690. struct radeon_device *rdev;
  691. int resched;
  692. rdev = container_of(work, struct radeon_device,
  693. pm.dynpm_idle_work.work);
  694. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  695. mutex_lock(&rdev->pm.mutex);
  696. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  697. unsigned long irq_flags;
  698. int not_processed = 0;
  699. read_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
  700. if (!list_empty(&rdev->fence_drv.emited)) {
  701. struct list_head *ptr;
  702. list_for_each(ptr, &rdev->fence_drv.emited) {
  703. /* count up to 3, that's enought info */
  704. if (++not_processed >= 3)
  705. break;
  706. }
  707. }
  708. read_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
  709. if (not_processed >= 3) { /* should upclock */
  710. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
  711. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  712. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  713. rdev->pm.dynpm_can_upclock) {
  714. rdev->pm.dynpm_planned_action =
  715. DYNPM_ACTION_UPCLOCK;
  716. rdev->pm.dynpm_action_timeout = jiffies +
  717. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  718. }
  719. } else if (not_processed == 0) { /* should downclock */
  720. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
  721. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  722. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  723. rdev->pm.dynpm_can_downclock) {
  724. rdev->pm.dynpm_planned_action =
  725. DYNPM_ACTION_DOWNCLOCK;
  726. rdev->pm.dynpm_action_timeout = jiffies +
  727. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  728. }
  729. }
  730. /* Note, radeon_pm_set_clocks is called with static_switch set
  731. * to false since we want to wait for vbl to avoid flicker.
  732. */
  733. if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
  734. jiffies > rdev->pm.dynpm_action_timeout) {
  735. radeon_pm_get_dynpm_state(rdev);
  736. radeon_pm_set_clocks(rdev);
  737. }
  738. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  739. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  740. }
  741. mutex_unlock(&rdev->pm.mutex);
  742. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  743. }
  744. /*
  745. * Debugfs info
  746. */
  747. #if defined(CONFIG_DEBUG_FS)
  748. static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
  749. {
  750. struct drm_info_node *node = (struct drm_info_node *) m->private;
  751. struct drm_device *dev = node->minor->dev;
  752. struct radeon_device *rdev = dev->dev_private;
  753. seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
  754. seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
  755. seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
  756. if (rdev->asic->get_memory_clock)
  757. seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
  758. if (rdev->pm.current_vddc)
  759. seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
  760. if (rdev->asic->get_pcie_lanes)
  761. seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
  762. return 0;
  763. }
  764. static struct drm_info_list radeon_pm_info_list[] = {
  765. {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
  766. };
  767. #endif
  768. static int radeon_debugfs_pm_init(struct radeon_device *rdev)
  769. {
  770. #if defined(CONFIG_DEBUG_FS)
  771. return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
  772. #else
  773. return 0;
  774. #endif
  775. }