radeon_pm.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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 <drm/drmP.h>
  24. #include "radeon.h"
  25. #include "avivod.h"
  26. #include "atom.h"
  27. #include <linux/power_supply.h>
  28. #include <linux/hwmon.h>
  29. #include <linux/hwmon-sysfs.h>
  30. #define RADEON_IDLE_LOOP_MS 100
  31. #define RADEON_RECLOCK_DELAY_MS 200
  32. #define RADEON_WAIT_VBLANK_TIMEOUT 200
  33. static const char *radeon_pm_state_type_name[5] = {
  34. "",
  35. "Powersave",
  36. "Battery",
  37. "Balanced",
  38. "Performance",
  39. };
  40. static void radeon_dynpm_idle_work_handler(struct work_struct *work);
  41. static int radeon_debugfs_pm_init(struct radeon_device *rdev);
  42. static bool radeon_pm_in_vbl(struct radeon_device *rdev);
  43. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish);
  44. static void radeon_pm_update_profile(struct radeon_device *rdev);
  45. static void radeon_pm_set_clocks(struct radeon_device *rdev);
  46. int radeon_pm_get_type_index(struct radeon_device *rdev,
  47. enum radeon_pm_state_type ps_type,
  48. int instance)
  49. {
  50. int i;
  51. int found_instance = -1;
  52. for (i = 0; i < rdev->pm.num_power_states; i++) {
  53. if (rdev->pm.power_state[i].type == ps_type) {
  54. found_instance++;
  55. if (found_instance == instance)
  56. return i;
  57. }
  58. }
  59. /* return default if no match */
  60. return rdev->pm.default_power_state_index;
  61. }
  62. void radeon_pm_acpi_event_handler(struct radeon_device *rdev)
  63. {
  64. if ((rdev->pm.pm_method == PM_METHOD_DPM) && rdev->pm.dpm_enabled) {
  65. mutex_lock(&rdev->pm.mutex);
  66. if (power_supply_is_system_supplied() > 0)
  67. rdev->pm.dpm.ac_power = true;
  68. else
  69. rdev->pm.dpm.ac_power = false;
  70. if (rdev->family == CHIP_ARUBA) {
  71. if (rdev->asic->dpm.enable_bapm)
  72. radeon_dpm_enable_bapm(rdev, rdev->pm.dpm.ac_power);
  73. }
  74. mutex_unlock(&rdev->pm.mutex);
  75. } else if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  76. if (rdev->pm.profile == PM_PROFILE_AUTO) {
  77. mutex_lock(&rdev->pm.mutex);
  78. radeon_pm_update_profile(rdev);
  79. radeon_pm_set_clocks(rdev);
  80. mutex_unlock(&rdev->pm.mutex);
  81. }
  82. }
  83. }
  84. static void radeon_pm_update_profile(struct radeon_device *rdev)
  85. {
  86. switch (rdev->pm.profile) {
  87. case PM_PROFILE_DEFAULT:
  88. rdev->pm.profile_index = PM_PROFILE_DEFAULT_IDX;
  89. break;
  90. case PM_PROFILE_AUTO:
  91. if (power_supply_is_system_supplied() > 0) {
  92. if (rdev->pm.active_crtc_count > 1)
  93. rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
  94. else
  95. rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
  96. } else {
  97. if (rdev->pm.active_crtc_count > 1)
  98. rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
  99. else
  100. rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
  101. }
  102. break;
  103. case PM_PROFILE_LOW:
  104. if (rdev->pm.active_crtc_count > 1)
  105. rdev->pm.profile_index = PM_PROFILE_LOW_MH_IDX;
  106. else
  107. rdev->pm.profile_index = PM_PROFILE_LOW_SH_IDX;
  108. break;
  109. case PM_PROFILE_MID:
  110. if (rdev->pm.active_crtc_count > 1)
  111. rdev->pm.profile_index = PM_PROFILE_MID_MH_IDX;
  112. else
  113. rdev->pm.profile_index = PM_PROFILE_MID_SH_IDX;
  114. break;
  115. case PM_PROFILE_HIGH:
  116. if (rdev->pm.active_crtc_count > 1)
  117. rdev->pm.profile_index = PM_PROFILE_HIGH_MH_IDX;
  118. else
  119. rdev->pm.profile_index = PM_PROFILE_HIGH_SH_IDX;
  120. break;
  121. }
  122. if (rdev->pm.active_crtc_count == 0) {
  123. rdev->pm.requested_power_state_index =
  124. rdev->pm.profiles[rdev->pm.profile_index].dpms_off_ps_idx;
  125. rdev->pm.requested_clock_mode_index =
  126. rdev->pm.profiles[rdev->pm.profile_index].dpms_off_cm_idx;
  127. } else {
  128. rdev->pm.requested_power_state_index =
  129. rdev->pm.profiles[rdev->pm.profile_index].dpms_on_ps_idx;
  130. rdev->pm.requested_clock_mode_index =
  131. rdev->pm.profiles[rdev->pm.profile_index].dpms_on_cm_idx;
  132. }
  133. }
  134. static void radeon_unmap_vram_bos(struct radeon_device *rdev)
  135. {
  136. struct radeon_bo *bo, *n;
  137. if (list_empty(&rdev->gem.objects))
  138. return;
  139. list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
  140. if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
  141. ttm_bo_unmap_virtual(&bo->tbo);
  142. }
  143. }
  144. static void radeon_sync_with_vblank(struct radeon_device *rdev)
  145. {
  146. if (rdev->pm.active_crtcs) {
  147. rdev->pm.vblank_sync = false;
  148. wait_event_timeout(
  149. rdev->irq.vblank_queue, rdev->pm.vblank_sync,
  150. msecs_to_jiffies(RADEON_WAIT_VBLANK_TIMEOUT));
  151. }
  152. }
  153. static void radeon_set_power_state(struct radeon_device *rdev)
  154. {
  155. u32 sclk, mclk;
  156. bool misc_after = false;
  157. if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
  158. (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
  159. return;
  160. if (radeon_gui_idle(rdev)) {
  161. sclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
  162. clock_info[rdev->pm.requested_clock_mode_index].sclk;
  163. if (sclk > rdev->pm.default_sclk)
  164. sclk = rdev->pm.default_sclk;
  165. /* starting with BTC, there is one state that is used for both
  166. * MH and SH. Difference is that we always use the high clock index for
  167. * mclk and vddci.
  168. */
  169. if ((rdev->pm.pm_method == PM_METHOD_PROFILE) &&
  170. (rdev->family >= CHIP_BARTS) &&
  171. rdev->pm.active_crtc_count &&
  172. ((rdev->pm.profile_index == PM_PROFILE_MID_MH_IDX) ||
  173. (rdev->pm.profile_index == PM_PROFILE_LOW_MH_IDX)))
  174. mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
  175. clock_info[rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx].mclk;
  176. else
  177. mclk = rdev->pm.power_state[rdev->pm.requested_power_state_index].
  178. clock_info[rdev->pm.requested_clock_mode_index].mclk;
  179. if (mclk > rdev->pm.default_mclk)
  180. mclk = rdev->pm.default_mclk;
  181. /* upvolt before raising clocks, downvolt after lowering clocks */
  182. if (sclk < rdev->pm.current_sclk)
  183. misc_after = true;
  184. radeon_sync_with_vblank(rdev);
  185. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  186. if (!radeon_pm_in_vbl(rdev))
  187. return;
  188. }
  189. radeon_pm_prepare(rdev);
  190. if (!misc_after)
  191. /* voltage, pcie lanes, etc.*/
  192. radeon_pm_misc(rdev);
  193. /* set engine clock */
  194. if (sclk != rdev->pm.current_sclk) {
  195. radeon_pm_debug_check_in_vbl(rdev, false);
  196. radeon_set_engine_clock(rdev, sclk);
  197. radeon_pm_debug_check_in_vbl(rdev, true);
  198. rdev->pm.current_sclk = sclk;
  199. DRM_DEBUG_DRIVER("Setting: e: %d\n", sclk);
  200. }
  201. /* set memory clock */
  202. if (rdev->asic->pm.set_memory_clock && (mclk != rdev->pm.current_mclk)) {
  203. radeon_pm_debug_check_in_vbl(rdev, false);
  204. radeon_set_memory_clock(rdev, mclk);
  205. radeon_pm_debug_check_in_vbl(rdev, true);
  206. rdev->pm.current_mclk = mclk;
  207. DRM_DEBUG_DRIVER("Setting: m: %d\n", mclk);
  208. }
  209. if (misc_after)
  210. /* voltage, pcie lanes, etc.*/
  211. radeon_pm_misc(rdev);
  212. radeon_pm_finish(rdev);
  213. rdev->pm.current_power_state_index = rdev->pm.requested_power_state_index;
  214. rdev->pm.current_clock_mode_index = rdev->pm.requested_clock_mode_index;
  215. } else
  216. DRM_DEBUG_DRIVER("pm: GUI not idle!!!\n");
  217. }
  218. static void radeon_pm_set_clocks(struct radeon_device *rdev)
  219. {
  220. int i, r;
  221. /* no need to take locks, etc. if nothing's going to change */
  222. if ((rdev->pm.requested_clock_mode_index == rdev->pm.current_clock_mode_index) &&
  223. (rdev->pm.requested_power_state_index == rdev->pm.current_power_state_index))
  224. return;
  225. mutex_lock(&rdev->ddev->struct_mutex);
  226. down_write(&rdev->pm.mclk_lock);
  227. mutex_lock(&rdev->ring_lock);
  228. /* wait for the rings to drain */
  229. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  230. struct radeon_ring *ring = &rdev->ring[i];
  231. if (!ring->ready) {
  232. continue;
  233. }
  234. r = radeon_fence_wait_empty(rdev, i);
  235. if (r) {
  236. /* needs a GPU reset dont reset here */
  237. mutex_unlock(&rdev->ring_lock);
  238. up_write(&rdev->pm.mclk_lock);
  239. mutex_unlock(&rdev->ddev->struct_mutex);
  240. return;
  241. }
  242. }
  243. radeon_unmap_vram_bos(rdev);
  244. if (rdev->irq.installed) {
  245. for (i = 0; i < rdev->num_crtc; i++) {
  246. if (rdev->pm.active_crtcs & (1 << i)) {
  247. rdev->pm.req_vblank |= (1 << i);
  248. drm_vblank_get(rdev->ddev, i);
  249. }
  250. }
  251. }
  252. radeon_set_power_state(rdev);
  253. if (rdev->irq.installed) {
  254. for (i = 0; i < rdev->num_crtc; i++) {
  255. if (rdev->pm.req_vblank & (1 << i)) {
  256. rdev->pm.req_vblank &= ~(1 << i);
  257. drm_vblank_put(rdev->ddev, i);
  258. }
  259. }
  260. }
  261. /* update display watermarks based on new power state */
  262. radeon_update_bandwidth_info(rdev);
  263. if (rdev->pm.active_crtc_count)
  264. radeon_bandwidth_update(rdev);
  265. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  266. mutex_unlock(&rdev->ring_lock);
  267. up_write(&rdev->pm.mclk_lock);
  268. mutex_unlock(&rdev->ddev->struct_mutex);
  269. }
  270. static void radeon_pm_print_states(struct radeon_device *rdev)
  271. {
  272. int i, j;
  273. struct radeon_power_state *power_state;
  274. struct radeon_pm_clock_info *clock_info;
  275. DRM_DEBUG_DRIVER("%d Power State(s)\n", rdev->pm.num_power_states);
  276. for (i = 0; i < rdev->pm.num_power_states; i++) {
  277. power_state = &rdev->pm.power_state[i];
  278. DRM_DEBUG_DRIVER("State %d: %s\n", i,
  279. radeon_pm_state_type_name[power_state->type]);
  280. if (i == rdev->pm.default_power_state_index)
  281. DRM_DEBUG_DRIVER("\tDefault");
  282. if ((rdev->flags & RADEON_IS_PCIE) && !(rdev->flags & RADEON_IS_IGP))
  283. DRM_DEBUG_DRIVER("\t%d PCIE Lanes\n", power_state->pcie_lanes);
  284. if (power_state->flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
  285. DRM_DEBUG_DRIVER("\tSingle display only\n");
  286. DRM_DEBUG_DRIVER("\t%d Clock Mode(s)\n", power_state->num_clock_modes);
  287. for (j = 0; j < power_state->num_clock_modes; j++) {
  288. clock_info = &(power_state->clock_info[j]);
  289. if (rdev->flags & RADEON_IS_IGP)
  290. DRM_DEBUG_DRIVER("\t\t%d e: %d\n",
  291. j,
  292. clock_info->sclk * 10);
  293. else
  294. DRM_DEBUG_DRIVER("\t\t%d e: %d\tm: %d\tv: %d\n",
  295. j,
  296. clock_info->sclk * 10,
  297. clock_info->mclk * 10,
  298. clock_info->voltage.voltage);
  299. }
  300. }
  301. }
  302. static ssize_t radeon_get_pm_profile(struct device *dev,
  303. struct device_attribute *attr,
  304. char *buf)
  305. {
  306. struct drm_device *ddev = dev_get_drvdata(dev);
  307. struct radeon_device *rdev = ddev->dev_private;
  308. int cp = rdev->pm.profile;
  309. return snprintf(buf, PAGE_SIZE, "%s\n",
  310. (cp == PM_PROFILE_AUTO) ? "auto" :
  311. (cp == PM_PROFILE_LOW) ? "low" :
  312. (cp == PM_PROFILE_MID) ? "mid" :
  313. (cp == PM_PROFILE_HIGH) ? "high" : "default");
  314. }
  315. static ssize_t radeon_set_pm_profile(struct device *dev,
  316. struct device_attribute *attr,
  317. const char *buf,
  318. size_t count)
  319. {
  320. struct drm_device *ddev = dev_get_drvdata(dev);
  321. struct radeon_device *rdev = ddev->dev_private;
  322. /* Can't set profile when the card is off */
  323. if ((rdev->flags & RADEON_IS_PX) &&
  324. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  325. return -EINVAL;
  326. mutex_lock(&rdev->pm.mutex);
  327. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  328. if (strncmp("default", buf, strlen("default")) == 0)
  329. rdev->pm.profile = PM_PROFILE_DEFAULT;
  330. else if (strncmp("auto", buf, strlen("auto")) == 0)
  331. rdev->pm.profile = PM_PROFILE_AUTO;
  332. else if (strncmp("low", buf, strlen("low")) == 0)
  333. rdev->pm.profile = PM_PROFILE_LOW;
  334. else if (strncmp("mid", buf, strlen("mid")) == 0)
  335. rdev->pm.profile = PM_PROFILE_MID;
  336. else if (strncmp("high", buf, strlen("high")) == 0)
  337. rdev->pm.profile = PM_PROFILE_HIGH;
  338. else {
  339. count = -EINVAL;
  340. goto fail;
  341. }
  342. radeon_pm_update_profile(rdev);
  343. radeon_pm_set_clocks(rdev);
  344. } else
  345. count = -EINVAL;
  346. fail:
  347. mutex_unlock(&rdev->pm.mutex);
  348. return count;
  349. }
  350. static ssize_t radeon_get_pm_method(struct device *dev,
  351. struct device_attribute *attr,
  352. char *buf)
  353. {
  354. struct drm_device *ddev = dev_get_drvdata(dev);
  355. struct radeon_device *rdev = ddev->dev_private;
  356. int pm = rdev->pm.pm_method;
  357. return snprintf(buf, PAGE_SIZE, "%s\n",
  358. (pm == PM_METHOD_DYNPM) ? "dynpm" :
  359. (pm == PM_METHOD_PROFILE) ? "profile" : "dpm");
  360. }
  361. static ssize_t radeon_set_pm_method(struct device *dev,
  362. struct device_attribute *attr,
  363. const char *buf,
  364. size_t count)
  365. {
  366. struct drm_device *ddev = dev_get_drvdata(dev);
  367. struct radeon_device *rdev = ddev->dev_private;
  368. /* Can't set method when the card is off */
  369. if ((rdev->flags & RADEON_IS_PX) &&
  370. (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
  371. count = -EINVAL;
  372. goto fail;
  373. }
  374. /* we don't support the legacy modes with dpm */
  375. if (rdev->pm.pm_method == PM_METHOD_DPM) {
  376. count = -EINVAL;
  377. goto fail;
  378. }
  379. if (strncmp("dynpm", buf, strlen("dynpm")) == 0) {
  380. mutex_lock(&rdev->pm.mutex);
  381. rdev->pm.pm_method = PM_METHOD_DYNPM;
  382. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  383. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  384. mutex_unlock(&rdev->pm.mutex);
  385. } else if (strncmp("profile", buf, strlen("profile")) == 0) {
  386. mutex_lock(&rdev->pm.mutex);
  387. /* disable dynpm */
  388. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  389. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  390. rdev->pm.pm_method = PM_METHOD_PROFILE;
  391. mutex_unlock(&rdev->pm.mutex);
  392. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  393. } else {
  394. count = -EINVAL;
  395. goto fail;
  396. }
  397. radeon_pm_compute_clocks(rdev);
  398. fail:
  399. return count;
  400. }
  401. static ssize_t radeon_get_dpm_state(struct device *dev,
  402. struct device_attribute *attr,
  403. char *buf)
  404. {
  405. struct drm_device *ddev = dev_get_drvdata(dev);
  406. struct radeon_device *rdev = ddev->dev_private;
  407. enum radeon_pm_state_type pm = rdev->pm.dpm.user_state;
  408. return snprintf(buf, PAGE_SIZE, "%s\n",
  409. (pm == POWER_STATE_TYPE_BATTERY) ? "battery" :
  410. (pm == POWER_STATE_TYPE_BALANCED) ? "balanced" : "performance");
  411. }
  412. static ssize_t radeon_set_dpm_state(struct device *dev,
  413. struct device_attribute *attr,
  414. const char *buf,
  415. size_t count)
  416. {
  417. struct drm_device *ddev = dev_get_drvdata(dev);
  418. struct radeon_device *rdev = ddev->dev_private;
  419. mutex_lock(&rdev->pm.mutex);
  420. if (strncmp("battery", buf, strlen("battery")) == 0)
  421. rdev->pm.dpm.user_state = POWER_STATE_TYPE_BATTERY;
  422. else if (strncmp("balanced", buf, strlen("balanced")) == 0)
  423. rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
  424. else if (strncmp("performance", buf, strlen("performance")) == 0)
  425. rdev->pm.dpm.user_state = POWER_STATE_TYPE_PERFORMANCE;
  426. else {
  427. mutex_unlock(&rdev->pm.mutex);
  428. count = -EINVAL;
  429. goto fail;
  430. }
  431. mutex_unlock(&rdev->pm.mutex);
  432. /* Can't set dpm state when the card is off */
  433. if (!(rdev->flags & RADEON_IS_PX) ||
  434. (ddev->switch_power_state == DRM_SWITCH_POWER_ON))
  435. radeon_pm_compute_clocks(rdev);
  436. fail:
  437. return count;
  438. }
  439. static ssize_t radeon_get_dpm_forced_performance_level(struct device *dev,
  440. struct device_attribute *attr,
  441. char *buf)
  442. {
  443. struct drm_device *ddev = dev_get_drvdata(dev);
  444. struct radeon_device *rdev = ddev->dev_private;
  445. enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
  446. if ((rdev->flags & RADEON_IS_PX) &&
  447. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  448. return snprintf(buf, PAGE_SIZE, "off\n");
  449. return snprintf(buf, PAGE_SIZE, "%s\n",
  450. (level == RADEON_DPM_FORCED_LEVEL_AUTO) ? "auto" :
  451. (level == RADEON_DPM_FORCED_LEVEL_LOW) ? "low" : "high");
  452. }
  453. static ssize_t radeon_set_dpm_forced_performance_level(struct device *dev,
  454. struct device_attribute *attr,
  455. const char *buf,
  456. size_t count)
  457. {
  458. struct drm_device *ddev = dev_get_drvdata(dev);
  459. struct radeon_device *rdev = ddev->dev_private;
  460. enum radeon_dpm_forced_level level;
  461. int ret = 0;
  462. /* Can't force performance level when the card is off */
  463. if ((rdev->flags & RADEON_IS_PX) &&
  464. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  465. return -EINVAL;
  466. mutex_lock(&rdev->pm.mutex);
  467. if (strncmp("low", buf, strlen("low")) == 0) {
  468. level = RADEON_DPM_FORCED_LEVEL_LOW;
  469. } else if (strncmp("high", buf, strlen("high")) == 0) {
  470. level = RADEON_DPM_FORCED_LEVEL_HIGH;
  471. } else if (strncmp("auto", buf, strlen("auto")) == 0) {
  472. level = RADEON_DPM_FORCED_LEVEL_AUTO;
  473. } else {
  474. count = -EINVAL;
  475. goto fail;
  476. }
  477. if (rdev->asic->dpm.force_performance_level) {
  478. if (rdev->pm.dpm.thermal_active) {
  479. count = -EINVAL;
  480. goto fail;
  481. }
  482. ret = radeon_dpm_force_performance_level(rdev, level);
  483. if (ret)
  484. count = -EINVAL;
  485. }
  486. fail:
  487. mutex_unlock(&rdev->pm.mutex);
  488. return count;
  489. }
  490. static DEVICE_ATTR(power_profile, S_IRUGO | S_IWUSR, radeon_get_pm_profile, radeon_set_pm_profile);
  491. static DEVICE_ATTR(power_method, S_IRUGO | S_IWUSR, radeon_get_pm_method, radeon_set_pm_method);
  492. static DEVICE_ATTR(power_dpm_state, S_IRUGO | S_IWUSR, radeon_get_dpm_state, radeon_set_dpm_state);
  493. static DEVICE_ATTR(power_dpm_force_performance_level, S_IRUGO | S_IWUSR,
  494. radeon_get_dpm_forced_performance_level,
  495. radeon_set_dpm_forced_performance_level);
  496. static ssize_t radeon_hwmon_show_temp(struct device *dev,
  497. struct device_attribute *attr,
  498. char *buf)
  499. {
  500. struct radeon_device *rdev = dev_get_drvdata(dev);
  501. struct drm_device *ddev = rdev->ddev;
  502. int temp;
  503. /* Can't get temperature when the card is off */
  504. if ((rdev->flags & RADEON_IS_PX) &&
  505. (ddev->switch_power_state != DRM_SWITCH_POWER_ON))
  506. return -EINVAL;
  507. if (rdev->asic->pm.get_temperature)
  508. temp = radeon_get_temperature(rdev);
  509. else
  510. temp = 0;
  511. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  512. }
  513. static ssize_t radeon_hwmon_show_temp_thresh(struct device *dev,
  514. struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct radeon_device *rdev = dev_get_drvdata(dev);
  518. int hyst = to_sensor_dev_attr(attr)->index;
  519. int temp;
  520. if (hyst)
  521. temp = rdev->pm.dpm.thermal.min_temp;
  522. else
  523. temp = rdev->pm.dpm.thermal.max_temp;
  524. return snprintf(buf, PAGE_SIZE, "%d\n", temp);
  525. }
  526. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, radeon_hwmon_show_temp, NULL, 0);
  527. static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 0);
  528. static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO, radeon_hwmon_show_temp_thresh, NULL, 1);
  529. static struct attribute *hwmon_attributes[] = {
  530. &sensor_dev_attr_temp1_input.dev_attr.attr,
  531. &sensor_dev_attr_temp1_crit.dev_attr.attr,
  532. &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
  533. NULL
  534. };
  535. static umode_t hwmon_attributes_visible(struct kobject *kobj,
  536. struct attribute *attr, int index)
  537. {
  538. struct device *dev = container_of(kobj, struct device, kobj);
  539. struct radeon_device *rdev = dev_get_drvdata(dev);
  540. /* Skip limit attributes if DPM is not enabled */
  541. if (rdev->pm.pm_method != PM_METHOD_DPM &&
  542. (attr == &sensor_dev_attr_temp1_crit.dev_attr.attr ||
  543. attr == &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr))
  544. return 0;
  545. return attr->mode;
  546. }
  547. static const struct attribute_group hwmon_attrgroup = {
  548. .attrs = hwmon_attributes,
  549. .is_visible = hwmon_attributes_visible,
  550. };
  551. static const struct attribute_group *hwmon_groups[] = {
  552. &hwmon_attrgroup,
  553. NULL
  554. };
  555. static int radeon_hwmon_init(struct radeon_device *rdev)
  556. {
  557. int err = 0;
  558. switch (rdev->pm.int_thermal_type) {
  559. case THERMAL_TYPE_RV6XX:
  560. case THERMAL_TYPE_RV770:
  561. case THERMAL_TYPE_EVERGREEN:
  562. case THERMAL_TYPE_NI:
  563. case THERMAL_TYPE_SUMO:
  564. case THERMAL_TYPE_SI:
  565. case THERMAL_TYPE_CI:
  566. case THERMAL_TYPE_KV:
  567. if (rdev->asic->pm.get_temperature == NULL)
  568. return err;
  569. rdev->pm.int_hwmon_dev = hwmon_device_register_with_groups(rdev->dev,
  570. "radeon", rdev,
  571. hwmon_groups);
  572. if (IS_ERR(rdev->pm.int_hwmon_dev)) {
  573. err = PTR_ERR(rdev->pm.int_hwmon_dev);
  574. dev_err(rdev->dev,
  575. "Unable to register hwmon device: %d\n", err);
  576. }
  577. break;
  578. default:
  579. break;
  580. }
  581. return err;
  582. }
  583. static void radeon_hwmon_fini(struct radeon_device *rdev)
  584. {
  585. if (rdev->pm.int_hwmon_dev)
  586. hwmon_device_unregister(rdev->pm.int_hwmon_dev);
  587. }
  588. static void radeon_dpm_thermal_work_handler(struct work_struct *work)
  589. {
  590. struct radeon_device *rdev =
  591. container_of(work, struct radeon_device,
  592. pm.dpm.thermal.work);
  593. /* switch to the thermal state */
  594. enum radeon_pm_state_type dpm_state = POWER_STATE_TYPE_INTERNAL_THERMAL;
  595. if (!rdev->pm.dpm_enabled)
  596. return;
  597. if (rdev->asic->pm.get_temperature) {
  598. int temp = radeon_get_temperature(rdev);
  599. if (temp < rdev->pm.dpm.thermal.min_temp)
  600. /* switch back the user state */
  601. dpm_state = rdev->pm.dpm.user_state;
  602. } else {
  603. if (rdev->pm.dpm.thermal.high_to_low)
  604. /* switch back the user state */
  605. dpm_state = rdev->pm.dpm.user_state;
  606. }
  607. mutex_lock(&rdev->pm.mutex);
  608. if (dpm_state == POWER_STATE_TYPE_INTERNAL_THERMAL)
  609. rdev->pm.dpm.thermal_active = true;
  610. else
  611. rdev->pm.dpm.thermal_active = false;
  612. rdev->pm.dpm.state = dpm_state;
  613. mutex_unlock(&rdev->pm.mutex);
  614. radeon_pm_compute_clocks(rdev);
  615. }
  616. static bool radeon_dpm_single_display(struct radeon_device *rdev)
  617. {
  618. bool single_display = (rdev->pm.dpm.new_active_crtc_count < 2) ?
  619. true : false;
  620. /* check if the vblank period is too short to adjust the mclk */
  621. if (single_display && rdev->asic->dpm.vblank_too_short) {
  622. if (radeon_dpm_vblank_too_short(rdev))
  623. single_display = false;
  624. }
  625. return single_display;
  626. }
  627. static struct radeon_ps *radeon_dpm_pick_power_state(struct radeon_device *rdev,
  628. enum radeon_pm_state_type dpm_state)
  629. {
  630. int i;
  631. struct radeon_ps *ps;
  632. u32 ui_class;
  633. bool single_display = radeon_dpm_single_display(rdev);
  634. /* certain older asics have a separare 3D performance state,
  635. * so try that first if the user selected performance
  636. */
  637. if (dpm_state == POWER_STATE_TYPE_PERFORMANCE)
  638. dpm_state = POWER_STATE_TYPE_INTERNAL_3DPERF;
  639. /* balanced states don't exist at the moment */
  640. if (dpm_state == POWER_STATE_TYPE_BALANCED)
  641. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  642. restart_search:
  643. /* Pick the best power state based on current conditions */
  644. for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
  645. ps = &rdev->pm.dpm.ps[i];
  646. ui_class = ps->class & ATOM_PPLIB_CLASSIFICATION_UI_MASK;
  647. switch (dpm_state) {
  648. /* user states */
  649. case POWER_STATE_TYPE_BATTERY:
  650. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BATTERY) {
  651. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  652. if (single_display)
  653. return ps;
  654. } else
  655. return ps;
  656. }
  657. break;
  658. case POWER_STATE_TYPE_BALANCED:
  659. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_BALANCED) {
  660. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  661. if (single_display)
  662. return ps;
  663. } else
  664. return ps;
  665. }
  666. break;
  667. case POWER_STATE_TYPE_PERFORMANCE:
  668. if (ui_class == ATOM_PPLIB_CLASSIFICATION_UI_PERFORMANCE) {
  669. if (ps->caps & ATOM_PPLIB_SINGLE_DISPLAY_ONLY) {
  670. if (single_display)
  671. return ps;
  672. } else
  673. return ps;
  674. }
  675. break;
  676. /* internal states */
  677. case POWER_STATE_TYPE_INTERNAL_UVD:
  678. if (rdev->pm.dpm.uvd_ps)
  679. return rdev->pm.dpm.uvd_ps;
  680. else
  681. break;
  682. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  683. if (ps->class & ATOM_PPLIB_CLASSIFICATION_SDSTATE)
  684. return ps;
  685. break;
  686. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  687. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HDSTATE)
  688. return ps;
  689. break;
  690. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  691. if (ps->class & ATOM_PPLIB_CLASSIFICATION_HD2STATE)
  692. return ps;
  693. break;
  694. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  695. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_MVC)
  696. return ps;
  697. break;
  698. case POWER_STATE_TYPE_INTERNAL_BOOT:
  699. return rdev->pm.dpm.boot_ps;
  700. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  701. if (ps->class & ATOM_PPLIB_CLASSIFICATION_THERMAL)
  702. return ps;
  703. break;
  704. case POWER_STATE_TYPE_INTERNAL_ACPI:
  705. if (ps->class & ATOM_PPLIB_CLASSIFICATION_ACPI)
  706. return ps;
  707. break;
  708. case POWER_STATE_TYPE_INTERNAL_ULV:
  709. if (ps->class2 & ATOM_PPLIB_CLASSIFICATION2_ULV)
  710. return ps;
  711. break;
  712. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  713. if (ps->class & ATOM_PPLIB_CLASSIFICATION_3DPERFORMANCE)
  714. return ps;
  715. break;
  716. default:
  717. break;
  718. }
  719. }
  720. /* use a fallback state if we didn't match */
  721. switch (dpm_state) {
  722. case POWER_STATE_TYPE_INTERNAL_UVD_SD:
  723. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  724. goto restart_search;
  725. case POWER_STATE_TYPE_INTERNAL_UVD_HD:
  726. case POWER_STATE_TYPE_INTERNAL_UVD_HD2:
  727. case POWER_STATE_TYPE_INTERNAL_UVD_MVC:
  728. if (rdev->pm.dpm.uvd_ps) {
  729. return rdev->pm.dpm.uvd_ps;
  730. } else {
  731. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  732. goto restart_search;
  733. }
  734. case POWER_STATE_TYPE_INTERNAL_THERMAL:
  735. dpm_state = POWER_STATE_TYPE_INTERNAL_ACPI;
  736. goto restart_search;
  737. case POWER_STATE_TYPE_INTERNAL_ACPI:
  738. dpm_state = POWER_STATE_TYPE_BATTERY;
  739. goto restart_search;
  740. case POWER_STATE_TYPE_BATTERY:
  741. case POWER_STATE_TYPE_BALANCED:
  742. case POWER_STATE_TYPE_INTERNAL_3DPERF:
  743. dpm_state = POWER_STATE_TYPE_PERFORMANCE;
  744. goto restart_search;
  745. default:
  746. break;
  747. }
  748. return NULL;
  749. }
  750. static void radeon_dpm_change_power_state_locked(struct radeon_device *rdev)
  751. {
  752. int i;
  753. struct radeon_ps *ps;
  754. enum radeon_pm_state_type dpm_state;
  755. int ret;
  756. bool single_display = radeon_dpm_single_display(rdev);
  757. /* if dpm init failed */
  758. if (!rdev->pm.dpm_enabled)
  759. return;
  760. if (rdev->pm.dpm.user_state != rdev->pm.dpm.state) {
  761. /* add other state override checks here */
  762. if ((!rdev->pm.dpm.thermal_active) &&
  763. (!rdev->pm.dpm.uvd_active))
  764. rdev->pm.dpm.state = rdev->pm.dpm.user_state;
  765. }
  766. dpm_state = rdev->pm.dpm.state;
  767. ps = radeon_dpm_pick_power_state(rdev, dpm_state);
  768. if (ps)
  769. rdev->pm.dpm.requested_ps = ps;
  770. else
  771. return;
  772. /* no need to reprogram if nothing changed unless we are on BTC+ */
  773. if (rdev->pm.dpm.current_ps == rdev->pm.dpm.requested_ps) {
  774. /* vce just modifies an existing state so force a change */
  775. if (ps->vce_active != rdev->pm.dpm.vce_active)
  776. goto force;
  777. /* user has made a display change (such as timing) */
  778. if (rdev->pm.dpm.single_display != single_display)
  779. goto force;
  780. if ((rdev->family < CHIP_BARTS) || (rdev->flags & RADEON_IS_IGP)) {
  781. /* for pre-BTC and APUs if the num crtcs changed but state is the same,
  782. * all we need to do is update the display configuration.
  783. */
  784. if (rdev->pm.dpm.new_active_crtcs != rdev->pm.dpm.current_active_crtcs) {
  785. /* update display watermarks based on new power state */
  786. radeon_bandwidth_update(rdev);
  787. /* update displays */
  788. radeon_dpm_display_configuration_changed(rdev);
  789. rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
  790. rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
  791. }
  792. return;
  793. } else {
  794. /* for BTC+ if the num crtcs hasn't changed and state is the same,
  795. * nothing to do, if the num crtcs is > 1 and state is the same,
  796. * update display configuration.
  797. */
  798. if (rdev->pm.dpm.new_active_crtcs ==
  799. rdev->pm.dpm.current_active_crtcs) {
  800. return;
  801. } else {
  802. if ((rdev->pm.dpm.current_active_crtc_count > 1) &&
  803. (rdev->pm.dpm.new_active_crtc_count > 1)) {
  804. /* update display watermarks based on new power state */
  805. radeon_bandwidth_update(rdev);
  806. /* update displays */
  807. radeon_dpm_display_configuration_changed(rdev);
  808. rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
  809. rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
  810. return;
  811. }
  812. }
  813. }
  814. }
  815. force:
  816. if (radeon_dpm == 1) {
  817. printk("switching from power state:\n");
  818. radeon_dpm_print_power_state(rdev, rdev->pm.dpm.current_ps);
  819. printk("switching to power state:\n");
  820. radeon_dpm_print_power_state(rdev, rdev->pm.dpm.requested_ps);
  821. }
  822. mutex_lock(&rdev->ddev->struct_mutex);
  823. down_write(&rdev->pm.mclk_lock);
  824. mutex_lock(&rdev->ring_lock);
  825. /* update whether vce is active */
  826. ps->vce_active = rdev->pm.dpm.vce_active;
  827. ret = radeon_dpm_pre_set_power_state(rdev);
  828. if (ret)
  829. goto done;
  830. /* update display watermarks based on new power state */
  831. radeon_bandwidth_update(rdev);
  832. /* update displays */
  833. radeon_dpm_display_configuration_changed(rdev);
  834. rdev->pm.dpm.current_active_crtcs = rdev->pm.dpm.new_active_crtcs;
  835. rdev->pm.dpm.current_active_crtc_count = rdev->pm.dpm.new_active_crtc_count;
  836. rdev->pm.dpm.single_display = single_display;
  837. /* wait for the rings to drain */
  838. for (i = 0; i < RADEON_NUM_RINGS; i++) {
  839. struct radeon_ring *ring = &rdev->ring[i];
  840. if (ring->ready)
  841. radeon_fence_wait_empty(rdev, i);
  842. }
  843. /* program the new power state */
  844. radeon_dpm_set_power_state(rdev);
  845. /* update current power state */
  846. rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps;
  847. radeon_dpm_post_set_power_state(rdev);
  848. if (rdev->asic->dpm.force_performance_level) {
  849. if (rdev->pm.dpm.thermal_active) {
  850. enum radeon_dpm_forced_level level = rdev->pm.dpm.forced_level;
  851. /* force low perf level for thermal */
  852. radeon_dpm_force_performance_level(rdev, RADEON_DPM_FORCED_LEVEL_LOW);
  853. /* save the user's level */
  854. rdev->pm.dpm.forced_level = level;
  855. } else {
  856. /* otherwise, user selected level */
  857. radeon_dpm_force_performance_level(rdev, rdev->pm.dpm.forced_level);
  858. }
  859. }
  860. done:
  861. mutex_unlock(&rdev->ring_lock);
  862. up_write(&rdev->pm.mclk_lock);
  863. mutex_unlock(&rdev->ddev->struct_mutex);
  864. }
  865. void radeon_dpm_enable_uvd(struct radeon_device *rdev, bool enable)
  866. {
  867. enum radeon_pm_state_type dpm_state;
  868. if (rdev->asic->dpm.powergate_uvd) {
  869. mutex_lock(&rdev->pm.mutex);
  870. /* don't powergate anything if we
  871. have active but pause streams */
  872. enable |= rdev->pm.dpm.sd > 0;
  873. enable |= rdev->pm.dpm.hd > 0;
  874. /* enable/disable UVD */
  875. radeon_dpm_powergate_uvd(rdev, !enable);
  876. mutex_unlock(&rdev->pm.mutex);
  877. } else {
  878. if (enable) {
  879. mutex_lock(&rdev->pm.mutex);
  880. rdev->pm.dpm.uvd_active = true;
  881. /* disable this for now */
  882. #if 0
  883. if ((rdev->pm.dpm.sd == 1) && (rdev->pm.dpm.hd == 0))
  884. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_SD;
  885. else if ((rdev->pm.dpm.sd == 2) && (rdev->pm.dpm.hd == 0))
  886. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  887. else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 1))
  888. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD;
  889. else if ((rdev->pm.dpm.sd == 0) && (rdev->pm.dpm.hd == 2))
  890. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD_HD2;
  891. else
  892. #endif
  893. dpm_state = POWER_STATE_TYPE_INTERNAL_UVD;
  894. rdev->pm.dpm.state = dpm_state;
  895. mutex_unlock(&rdev->pm.mutex);
  896. } else {
  897. mutex_lock(&rdev->pm.mutex);
  898. rdev->pm.dpm.uvd_active = false;
  899. mutex_unlock(&rdev->pm.mutex);
  900. }
  901. radeon_pm_compute_clocks(rdev);
  902. }
  903. }
  904. void radeon_dpm_enable_vce(struct radeon_device *rdev, bool enable)
  905. {
  906. if (enable) {
  907. mutex_lock(&rdev->pm.mutex);
  908. rdev->pm.dpm.vce_active = true;
  909. /* XXX select vce level based on ring/task */
  910. rdev->pm.dpm.vce_level = RADEON_VCE_LEVEL_AC_ALL;
  911. mutex_unlock(&rdev->pm.mutex);
  912. } else {
  913. mutex_lock(&rdev->pm.mutex);
  914. rdev->pm.dpm.vce_active = false;
  915. mutex_unlock(&rdev->pm.mutex);
  916. }
  917. radeon_pm_compute_clocks(rdev);
  918. }
  919. static void radeon_pm_suspend_old(struct radeon_device *rdev)
  920. {
  921. mutex_lock(&rdev->pm.mutex);
  922. if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  923. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE)
  924. rdev->pm.dynpm_state = DYNPM_STATE_SUSPENDED;
  925. }
  926. mutex_unlock(&rdev->pm.mutex);
  927. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  928. }
  929. static void radeon_pm_suspend_dpm(struct radeon_device *rdev)
  930. {
  931. mutex_lock(&rdev->pm.mutex);
  932. /* disable dpm */
  933. radeon_dpm_disable(rdev);
  934. /* reset the power state */
  935. rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
  936. rdev->pm.dpm_enabled = false;
  937. mutex_unlock(&rdev->pm.mutex);
  938. }
  939. void radeon_pm_suspend(struct radeon_device *rdev)
  940. {
  941. if (rdev->pm.pm_method == PM_METHOD_DPM)
  942. radeon_pm_suspend_dpm(rdev);
  943. else
  944. radeon_pm_suspend_old(rdev);
  945. }
  946. static void radeon_pm_resume_old(struct radeon_device *rdev)
  947. {
  948. /* set up the default clocks if the MC ucode is loaded */
  949. if ((rdev->family >= CHIP_BARTS) &&
  950. (rdev->family <= CHIP_CAYMAN) &&
  951. rdev->mc_fw) {
  952. if (rdev->pm.default_vddc)
  953. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  954. SET_VOLTAGE_TYPE_ASIC_VDDC);
  955. if (rdev->pm.default_vddci)
  956. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  957. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  958. if (rdev->pm.default_sclk)
  959. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  960. if (rdev->pm.default_mclk)
  961. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  962. }
  963. /* asic init will reset the default power state */
  964. mutex_lock(&rdev->pm.mutex);
  965. rdev->pm.current_power_state_index = rdev->pm.default_power_state_index;
  966. rdev->pm.current_clock_mode_index = 0;
  967. rdev->pm.current_sclk = rdev->pm.default_sclk;
  968. rdev->pm.current_mclk = rdev->pm.default_mclk;
  969. if (rdev->pm.power_state) {
  970. rdev->pm.current_vddc = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.voltage;
  971. rdev->pm.current_vddci = rdev->pm.power_state[rdev->pm.default_power_state_index].clock_info[0].voltage.vddci;
  972. }
  973. if (rdev->pm.pm_method == PM_METHOD_DYNPM
  974. && rdev->pm.dynpm_state == DYNPM_STATE_SUSPENDED) {
  975. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  976. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  977. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  978. }
  979. mutex_unlock(&rdev->pm.mutex);
  980. radeon_pm_compute_clocks(rdev);
  981. }
  982. static void radeon_pm_resume_dpm(struct radeon_device *rdev)
  983. {
  984. int ret;
  985. /* asic init will reset to the boot state */
  986. mutex_lock(&rdev->pm.mutex);
  987. rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
  988. radeon_dpm_setup_asic(rdev);
  989. ret = radeon_dpm_enable(rdev);
  990. mutex_unlock(&rdev->pm.mutex);
  991. if (ret)
  992. goto dpm_resume_fail;
  993. rdev->pm.dpm_enabled = true;
  994. return;
  995. dpm_resume_fail:
  996. DRM_ERROR("radeon: dpm resume failed\n");
  997. if ((rdev->family >= CHIP_BARTS) &&
  998. (rdev->family <= CHIP_CAYMAN) &&
  999. rdev->mc_fw) {
  1000. if (rdev->pm.default_vddc)
  1001. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  1002. SET_VOLTAGE_TYPE_ASIC_VDDC);
  1003. if (rdev->pm.default_vddci)
  1004. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  1005. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  1006. if (rdev->pm.default_sclk)
  1007. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  1008. if (rdev->pm.default_mclk)
  1009. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  1010. }
  1011. }
  1012. void radeon_pm_resume(struct radeon_device *rdev)
  1013. {
  1014. if (rdev->pm.pm_method == PM_METHOD_DPM)
  1015. radeon_pm_resume_dpm(rdev);
  1016. else
  1017. radeon_pm_resume_old(rdev);
  1018. }
  1019. static int radeon_pm_init_old(struct radeon_device *rdev)
  1020. {
  1021. int ret;
  1022. rdev->pm.profile = PM_PROFILE_DEFAULT;
  1023. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  1024. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  1025. rdev->pm.dynpm_can_upclock = true;
  1026. rdev->pm.dynpm_can_downclock = true;
  1027. rdev->pm.default_sclk = rdev->clock.default_sclk;
  1028. rdev->pm.default_mclk = rdev->clock.default_mclk;
  1029. rdev->pm.current_sclk = rdev->clock.default_sclk;
  1030. rdev->pm.current_mclk = rdev->clock.default_mclk;
  1031. rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  1032. if (rdev->bios) {
  1033. if (rdev->is_atom_bios)
  1034. radeon_atombios_get_power_modes(rdev);
  1035. else
  1036. radeon_combios_get_power_modes(rdev);
  1037. radeon_pm_print_states(rdev);
  1038. radeon_pm_init_profile(rdev);
  1039. /* set up the default clocks if the MC ucode is loaded */
  1040. if ((rdev->family >= CHIP_BARTS) &&
  1041. (rdev->family <= CHIP_CAYMAN) &&
  1042. rdev->mc_fw) {
  1043. if (rdev->pm.default_vddc)
  1044. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  1045. SET_VOLTAGE_TYPE_ASIC_VDDC);
  1046. if (rdev->pm.default_vddci)
  1047. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  1048. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  1049. if (rdev->pm.default_sclk)
  1050. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  1051. if (rdev->pm.default_mclk)
  1052. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  1053. }
  1054. }
  1055. /* set up the internal thermal sensor if applicable */
  1056. ret = radeon_hwmon_init(rdev);
  1057. if (ret)
  1058. return ret;
  1059. INIT_DELAYED_WORK(&rdev->pm.dynpm_idle_work, radeon_dynpm_idle_work_handler);
  1060. if (rdev->pm.num_power_states > 1) {
  1061. /* where's the best place to put these? */
  1062. ret = device_create_file(rdev->dev, &dev_attr_power_profile);
  1063. if (ret)
  1064. DRM_ERROR("failed to create device file for power profile\n");
  1065. ret = device_create_file(rdev->dev, &dev_attr_power_method);
  1066. if (ret)
  1067. DRM_ERROR("failed to create device file for power method\n");
  1068. if (radeon_debugfs_pm_init(rdev)) {
  1069. DRM_ERROR("Failed to register debugfs file for PM!\n");
  1070. }
  1071. DRM_INFO("radeon: power management initialized\n");
  1072. }
  1073. return 0;
  1074. }
  1075. static void radeon_dpm_print_power_states(struct radeon_device *rdev)
  1076. {
  1077. int i;
  1078. for (i = 0; i < rdev->pm.dpm.num_ps; i++) {
  1079. printk("== power state %d ==\n", i);
  1080. radeon_dpm_print_power_state(rdev, &rdev->pm.dpm.ps[i]);
  1081. }
  1082. }
  1083. static int radeon_pm_init_dpm(struct radeon_device *rdev)
  1084. {
  1085. int ret;
  1086. /* default to balanced state */
  1087. rdev->pm.dpm.state = POWER_STATE_TYPE_BALANCED;
  1088. rdev->pm.dpm.user_state = POWER_STATE_TYPE_BALANCED;
  1089. rdev->pm.dpm.forced_level = RADEON_DPM_FORCED_LEVEL_AUTO;
  1090. rdev->pm.default_sclk = rdev->clock.default_sclk;
  1091. rdev->pm.default_mclk = rdev->clock.default_mclk;
  1092. rdev->pm.current_sclk = rdev->clock.default_sclk;
  1093. rdev->pm.current_mclk = rdev->clock.default_mclk;
  1094. rdev->pm.int_thermal_type = THERMAL_TYPE_NONE;
  1095. if (rdev->bios && rdev->is_atom_bios)
  1096. radeon_atombios_get_power_modes(rdev);
  1097. else
  1098. return -EINVAL;
  1099. /* set up the internal thermal sensor if applicable */
  1100. ret = radeon_hwmon_init(rdev);
  1101. if (ret)
  1102. return ret;
  1103. INIT_WORK(&rdev->pm.dpm.thermal.work, radeon_dpm_thermal_work_handler);
  1104. mutex_lock(&rdev->pm.mutex);
  1105. radeon_dpm_init(rdev);
  1106. rdev->pm.dpm.current_ps = rdev->pm.dpm.requested_ps = rdev->pm.dpm.boot_ps;
  1107. if (radeon_dpm == 1)
  1108. radeon_dpm_print_power_states(rdev);
  1109. radeon_dpm_setup_asic(rdev);
  1110. ret = radeon_dpm_enable(rdev);
  1111. mutex_unlock(&rdev->pm.mutex);
  1112. if (ret)
  1113. goto dpm_failed;
  1114. rdev->pm.dpm_enabled = true;
  1115. ret = device_create_file(rdev->dev, &dev_attr_power_dpm_state);
  1116. if (ret)
  1117. DRM_ERROR("failed to create device file for dpm state\n");
  1118. ret = device_create_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
  1119. if (ret)
  1120. DRM_ERROR("failed to create device file for dpm state\n");
  1121. /* XXX: these are noops for dpm but are here for backwards compat */
  1122. ret = device_create_file(rdev->dev, &dev_attr_power_profile);
  1123. if (ret)
  1124. DRM_ERROR("failed to create device file for power profile\n");
  1125. ret = device_create_file(rdev->dev, &dev_attr_power_method);
  1126. if (ret)
  1127. DRM_ERROR("failed to create device file for power method\n");
  1128. if (radeon_debugfs_pm_init(rdev)) {
  1129. DRM_ERROR("Failed to register debugfs file for dpm!\n");
  1130. }
  1131. DRM_INFO("radeon: dpm initialized\n");
  1132. return 0;
  1133. dpm_failed:
  1134. rdev->pm.dpm_enabled = false;
  1135. if ((rdev->family >= CHIP_BARTS) &&
  1136. (rdev->family <= CHIP_CAYMAN) &&
  1137. rdev->mc_fw) {
  1138. if (rdev->pm.default_vddc)
  1139. radeon_atom_set_voltage(rdev, rdev->pm.default_vddc,
  1140. SET_VOLTAGE_TYPE_ASIC_VDDC);
  1141. if (rdev->pm.default_vddci)
  1142. radeon_atom_set_voltage(rdev, rdev->pm.default_vddci,
  1143. SET_VOLTAGE_TYPE_ASIC_VDDCI);
  1144. if (rdev->pm.default_sclk)
  1145. radeon_set_engine_clock(rdev, rdev->pm.default_sclk);
  1146. if (rdev->pm.default_mclk)
  1147. radeon_set_memory_clock(rdev, rdev->pm.default_mclk);
  1148. }
  1149. DRM_ERROR("radeon: dpm initialization failed\n");
  1150. return ret;
  1151. }
  1152. struct radeon_dpm_quirk {
  1153. u32 chip_vendor;
  1154. u32 chip_device;
  1155. u32 subsys_vendor;
  1156. u32 subsys_device;
  1157. };
  1158. /* cards with dpm stability problems */
  1159. static struct radeon_dpm_quirk radeon_dpm_quirk_list[] = {
  1160. /* TURKS - https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1386534 */
  1161. { PCI_VENDOR_ID_ATI, 0x6759, 0x1682, 0x3195 },
  1162. /* TURKS - https://bugzilla.kernel.org/show_bug.cgi?id=83731 */
  1163. { PCI_VENDOR_ID_ATI, 0x6840, 0x1179, 0xfb81 },
  1164. { 0, 0, 0, 0 },
  1165. };
  1166. int radeon_pm_init(struct radeon_device *rdev)
  1167. {
  1168. struct radeon_dpm_quirk *p = radeon_dpm_quirk_list;
  1169. bool disable_dpm = false;
  1170. /* Apply dpm quirks */
  1171. while (p && p->chip_device != 0) {
  1172. if (rdev->pdev->vendor == p->chip_vendor &&
  1173. rdev->pdev->device == p->chip_device &&
  1174. rdev->pdev->subsystem_vendor == p->subsys_vendor &&
  1175. rdev->pdev->subsystem_device == p->subsys_device) {
  1176. disable_dpm = true;
  1177. break;
  1178. }
  1179. ++p;
  1180. }
  1181. /* enable dpm on rv6xx+ */
  1182. switch (rdev->family) {
  1183. case CHIP_RV610:
  1184. case CHIP_RV630:
  1185. case CHIP_RV620:
  1186. case CHIP_RV635:
  1187. case CHIP_RV670:
  1188. case CHIP_RS780:
  1189. case CHIP_RS880:
  1190. case CHIP_RV770:
  1191. /* DPM requires the RLC, RV770+ dGPU requires SMC */
  1192. if (!rdev->rlc_fw)
  1193. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1194. else if ((rdev->family >= CHIP_RV770) &&
  1195. (!(rdev->flags & RADEON_IS_IGP)) &&
  1196. (!rdev->smc_fw))
  1197. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1198. else if (radeon_dpm == 1)
  1199. rdev->pm.pm_method = PM_METHOD_DPM;
  1200. else
  1201. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1202. break;
  1203. case CHIP_RV730:
  1204. case CHIP_RV710:
  1205. case CHIP_RV740:
  1206. case CHIP_CEDAR:
  1207. case CHIP_REDWOOD:
  1208. case CHIP_JUNIPER:
  1209. case CHIP_CYPRESS:
  1210. case CHIP_HEMLOCK:
  1211. case CHIP_PALM:
  1212. case CHIP_SUMO:
  1213. case CHIP_SUMO2:
  1214. case CHIP_BARTS:
  1215. case CHIP_TURKS:
  1216. case CHIP_CAICOS:
  1217. case CHIP_CAYMAN:
  1218. case CHIP_ARUBA:
  1219. case CHIP_TAHITI:
  1220. case CHIP_PITCAIRN:
  1221. case CHIP_VERDE:
  1222. case CHIP_OLAND:
  1223. case CHIP_HAINAN:
  1224. case CHIP_BONAIRE:
  1225. case CHIP_KABINI:
  1226. case CHIP_KAVERI:
  1227. case CHIP_HAWAII:
  1228. case CHIP_MULLINS:
  1229. /* DPM requires the RLC, RV770+ dGPU requires SMC */
  1230. if (!rdev->rlc_fw)
  1231. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1232. else if ((rdev->family >= CHIP_RV770) &&
  1233. (!(rdev->flags & RADEON_IS_IGP)) &&
  1234. (!rdev->smc_fw))
  1235. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1236. else if (disable_dpm && (radeon_dpm == -1))
  1237. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1238. else if (radeon_dpm == 0)
  1239. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1240. else
  1241. rdev->pm.pm_method = PM_METHOD_DPM;
  1242. break;
  1243. default:
  1244. /* default to profile method */
  1245. rdev->pm.pm_method = PM_METHOD_PROFILE;
  1246. break;
  1247. }
  1248. if (rdev->pm.pm_method == PM_METHOD_DPM)
  1249. return radeon_pm_init_dpm(rdev);
  1250. else
  1251. return radeon_pm_init_old(rdev);
  1252. }
  1253. int radeon_pm_late_init(struct radeon_device *rdev)
  1254. {
  1255. int ret = 0;
  1256. if (rdev->pm.pm_method == PM_METHOD_DPM) {
  1257. mutex_lock(&rdev->pm.mutex);
  1258. ret = radeon_dpm_late_enable(rdev);
  1259. mutex_unlock(&rdev->pm.mutex);
  1260. }
  1261. return ret;
  1262. }
  1263. static void radeon_pm_fini_old(struct radeon_device *rdev)
  1264. {
  1265. if (rdev->pm.num_power_states > 1) {
  1266. mutex_lock(&rdev->pm.mutex);
  1267. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  1268. rdev->pm.profile = PM_PROFILE_DEFAULT;
  1269. radeon_pm_update_profile(rdev);
  1270. radeon_pm_set_clocks(rdev);
  1271. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  1272. /* reset default clocks */
  1273. rdev->pm.dynpm_state = DYNPM_STATE_DISABLED;
  1274. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  1275. radeon_pm_set_clocks(rdev);
  1276. }
  1277. mutex_unlock(&rdev->pm.mutex);
  1278. cancel_delayed_work_sync(&rdev->pm.dynpm_idle_work);
  1279. device_remove_file(rdev->dev, &dev_attr_power_profile);
  1280. device_remove_file(rdev->dev, &dev_attr_power_method);
  1281. }
  1282. radeon_hwmon_fini(rdev);
  1283. kfree(rdev->pm.power_state);
  1284. }
  1285. static void radeon_pm_fini_dpm(struct radeon_device *rdev)
  1286. {
  1287. if (rdev->pm.num_power_states > 1) {
  1288. mutex_lock(&rdev->pm.mutex);
  1289. radeon_dpm_disable(rdev);
  1290. mutex_unlock(&rdev->pm.mutex);
  1291. device_remove_file(rdev->dev, &dev_attr_power_dpm_state);
  1292. device_remove_file(rdev->dev, &dev_attr_power_dpm_force_performance_level);
  1293. /* XXX backwards compat */
  1294. device_remove_file(rdev->dev, &dev_attr_power_profile);
  1295. device_remove_file(rdev->dev, &dev_attr_power_method);
  1296. }
  1297. radeon_dpm_fini(rdev);
  1298. radeon_hwmon_fini(rdev);
  1299. kfree(rdev->pm.power_state);
  1300. }
  1301. void radeon_pm_fini(struct radeon_device *rdev)
  1302. {
  1303. if (rdev->pm.pm_method == PM_METHOD_DPM)
  1304. radeon_pm_fini_dpm(rdev);
  1305. else
  1306. radeon_pm_fini_old(rdev);
  1307. }
  1308. static void radeon_pm_compute_clocks_old(struct radeon_device *rdev)
  1309. {
  1310. struct drm_device *ddev = rdev->ddev;
  1311. struct drm_crtc *crtc;
  1312. struct radeon_crtc *radeon_crtc;
  1313. if (rdev->pm.num_power_states < 2)
  1314. return;
  1315. mutex_lock(&rdev->pm.mutex);
  1316. rdev->pm.active_crtcs = 0;
  1317. rdev->pm.active_crtc_count = 0;
  1318. if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
  1319. list_for_each_entry(crtc,
  1320. &ddev->mode_config.crtc_list, head) {
  1321. radeon_crtc = to_radeon_crtc(crtc);
  1322. if (radeon_crtc->enabled) {
  1323. rdev->pm.active_crtcs |= (1 << radeon_crtc->crtc_id);
  1324. rdev->pm.active_crtc_count++;
  1325. }
  1326. }
  1327. }
  1328. if (rdev->pm.pm_method == PM_METHOD_PROFILE) {
  1329. radeon_pm_update_profile(rdev);
  1330. radeon_pm_set_clocks(rdev);
  1331. } else if (rdev->pm.pm_method == PM_METHOD_DYNPM) {
  1332. if (rdev->pm.dynpm_state != DYNPM_STATE_DISABLED) {
  1333. if (rdev->pm.active_crtc_count > 1) {
  1334. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  1335. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  1336. rdev->pm.dynpm_state = DYNPM_STATE_PAUSED;
  1337. rdev->pm.dynpm_planned_action = DYNPM_ACTION_DEFAULT;
  1338. radeon_pm_get_dynpm_state(rdev);
  1339. radeon_pm_set_clocks(rdev);
  1340. DRM_DEBUG_DRIVER("radeon: dynamic power management deactivated\n");
  1341. }
  1342. } else if (rdev->pm.active_crtc_count == 1) {
  1343. /* TODO: Increase clocks if needed for current mode */
  1344. if (rdev->pm.dynpm_state == DYNPM_STATE_MINIMUM) {
  1345. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  1346. rdev->pm.dynpm_planned_action = DYNPM_ACTION_UPCLOCK;
  1347. radeon_pm_get_dynpm_state(rdev);
  1348. radeon_pm_set_clocks(rdev);
  1349. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  1350. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  1351. } else if (rdev->pm.dynpm_state == DYNPM_STATE_PAUSED) {
  1352. rdev->pm.dynpm_state = DYNPM_STATE_ACTIVE;
  1353. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  1354. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  1355. DRM_DEBUG_DRIVER("radeon: dynamic power management activated\n");
  1356. }
  1357. } else { /* count == 0 */
  1358. if (rdev->pm.dynpm_state != DYNPM_STATE_MINIMUM) {
  1359. cancel_delayed_work(&rdev->pm.dynpm_idle_work);
  1360. rdev->pm.dynpm_state = DYNPM_STATE_MINIMUM;
  1361. rdev->pm.dynpm_planned_action = DYNPM_ACTION_MINIMUM;
  1362. radeon_pm_get_dynpm_state(rdev);
  1363. radeon_pm_set_clocks(rdev);
  1364. }
  1365. }
  1366. }
  1367. }
  1368. mutex_unlock(&rdev->pm.mutex);
  1369. }
  1370. static void radeon_pm_compute_clocks_dpm(struct radeon_device *rdev)
  1371. {
  1372. struct drm_device *ddev = rdev->ddev;
  1373. struct drm_crtc *crtc;
  1374. struct radeon_crtc *radeon_crtc;
  1375. if (!rdev->pm.dpm_enabled)
  1376. return;
  1377. mutex_lock(&rdev->pm.mutex);
  1378. /* update active crtc counts */
  1379. rdev->pm.dpm.new_active_crtcs = 0;
  1380. rdev->pm.dpm.new_active_crtc_count = 0;
  1381. if (rdev->num_crtc && rdev->mode_info.mode_config_initialized) {
  1382. list_for_each_entry(crtc,
  1383. &ddev->mode_config.crtc_list, head) {
  1384. radeon_crtc = to_radeon_crtc(crtc);
  1385. if (crtc->enabled) {
  1386. rdev->pm.dpm.new_active_crtcs |= (1 << radeon_crtc->crtc_id);
  1387. rdev->pm.dpm.new_active_crtc_count++;
  1388. }
  1389. }
  1390. }
  1391. /* update battery/ac status */
  1392. if (power_supply_is_system_supplied() > 0)
  1393. rdev->pm.dpm.ac_power = true;
  1394. else
  1395. rdev->pm.dpm.ac_power = false;
  1396. radeon_dpm_change_power_state_locked(rdev);
  1397. mutex_unlock(&rdev->pm.mutex);
  1398. }
  1399. void radeon_pm_compute_clocks(struct radeon_device *rdev)
  1400. {
  1401. if (rdev->pm.pm_method == PM_METHOD_DPM)
  1402. radeon_pm_compute_clocks_dpm(rdev);
  1403. else
  1404. radeon_pm_compute_clocks_old(rdev);
  1405. }
  1406. static bool radeon_pm_in_vbl(struct radeon_device *rdev)
  1407. {
  1408. int crtc, vpos, hpos, vbl_status;
  1409. bool in_vbl = true;
  1410. /* Iterate over all active crtc's. All crtc's must be in vblank,
  1411. * otherwise return in_vbl == false.
  1412. */
  1413. for (crtc = 0; (crtc < rdev->num_crtc) && in_vbl; crtc++) {
  1414. if (rdev->pm.active_crtcs & (1 << crtc)) {
  1415. vbl_status = radeon_get_crtc_scanoutpos(rdev->ddev, crtc, 0, &vpos, &hpos, NULL, NULL);
  1416. if ((vbl_status & DRM_SCANOUTPOS_VALID) &&
  1417. !(vbl_status & DRM_SCANOUTPOS_IN_VBLANK))
  1418. in_vbl = false;
  1419. }
  1420. }
  1421. return in_vbl;
  1422. }
  1423. static bool radeon_pm_debug_check_in_vbl(struct radeon_device *rdev, bool finish)
  1424. {
  1425. u32 stat_crtc = 0;
  1426. bool in_vbl = radeon_pm_in_vbl(rdev);
  1427. if (in_vbl == false)
  1428. DRM_DEBUG_DRIVER("not in vbl for pm change %08x at %s\n", stat_crtc,
  1429. finish ? "exit" : "entry");
  1430. return in_vbl;
  1431. }
  1432. static void radeon_dynpm_idle_work_handler(struct work_struct *work)
  1433. {
  1434. struct radeon_device *rdev;
  1435. int resched;
  1436. rdev = container_of(work, struct radeon_device,
  1437. pm.dynpm_idle_work.work);
  1438. resched = ttm_bo_lock_delayed_workqueue(&rdev->mman.bdev);
  1439. mutex_lock(&rdev->pm.mutex);
  1440. if (rdev->pm.dynpm_state == DYNPM_STATE_ACTIVE) {
  1441. int not_processed = 0;
  1442. int i;
  1443. for (i = 0; i < RADEON_NUM_RINGS; ++i) {
  1444. struct radeon_ring *ring = &rdev->ring[i];
  1445. if (ring->ready) {
  1446. not_processed += radeon_fence_count_emitted(rdev, i);
  1447. if (not_processed >= 3)
  1448. break;
  1449. }
  1450. }
  1451. if (not_processed >= 3) { /* should upclock */
  1452. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_DOWNCLOCK) {
  1453. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  1454. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  1455. rdev->pm.dynpm_can_upclock) {
  1456. rdev->pm.dynpm_planned_action =
  1457. DYNPM_ACTION_UPCLOCK;
  1458. rdev->pm.dynpm_action_timeout = jiffies +
  1459. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  1460. }
  1461. } else if (not_processed == 0) { /* should downclock */
  1462. if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_UPCLOCK) {
  1463. rdev->pm.dynpm_planned_action = DYNPM_ACTION_NONE;
  1464. } else if (rdev->pm.dynpm_planned_action == DYNPM_ACTION_NONE &&
  1465. rdev->pm.dynpm_can_downclock) {
  1466. rdev->pm.dynpm_planned_action =
  1467. DYNPM_ACTION_DOWNCLOCK;
  1468. rdev->pm.dynpm_action_timeout = jiffies +
  1469. msecs_to_jiffies(RADEON_RECLOCK_DELAY_MS);
  1470. }
  1471. }
  1472. /* Note, radeon_pm_set_clocks is called with static_switch set
  1473. * to false since we want to wait for vbl to avoid flicker.
  1474. */
  1475. if (rdev->pm.dynpm_planned_action != DYNPM_ACTION_NONE &&
  1476. jiffies > rdev->pm.dynpm_action_timeout) {
  1477. radeon_pm_get_dynpm_state(rdev);
  1478. radeon_pm_set_clocks(rdev);
  1479. }
  1480. schedule_delayed_work(&rdev->pm.dynpm_idle_work,
  1481. msecs_to_jiffies(RADEON_IDLE_LOOP_MS));
  1482. }
  1483. mutex_unlock(&rdev->pm.mutex);
  1484. ttm_bo_unlock_delayed_workqueue(&rdev->mman.bdev, resched);
  1485. }
  1486. /*
  1487. * Debugfs info
  1488. */
  1489. #if defined(CONFIG_DEBUG_FS)
  1490. static int radeon_debugfs_pm_info(struct seq_file *m, void *data)
  1491. {
  1492. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1493. struct drm_device *dev = node->minor->dev;
  1494. struct radeon_device *rdev = dev->dev_private;
  1495. struct drm_device *ddev = rdev->ddev;
  1496. if ((rdev->flags & RADEON_IS_PX) &&
  1497. (ddev->switch_power_state != DRM_SWITCH_POWER_ON)) {
  1498. seq_printf(m, "PX asic powered off\n");
  1499. } else if (rdev->pm.dpm_enabled) {
  1500. mutex_lock(&rdev->pm.mutex);
  1501. if (rdev->asic->dpm.debugfs_print_current_performance_level)
  1502. radeon_dpm_debugfs_print_current_performance_level(rdev, m);
  1503. else
  1504. seq_printf(m, "Debugfs support not implemented for this asic\n");
  1505. mutex_unlock(&rdev->pm.mutex);
  1506. } else {
  1507. seq_printf(m, "default engine clock: %u0 kHz\n", rdev->pm.default_sclk);
  1508. /* radeon_get_engine_clock is not reliable on APUs so just print the current clock */
  1509. if ((rdev->family >= CHIP_PALM) && (rdev->flags & RADEON_IS_IGP))
  1510. seq_printf(m, "current engine clock: %u0 kHz\n", rdev->pm.current_sclk);
  1511. else
  1512. seq_printf(m, "current engine clock: %u0 kHz\n", radeon_get_engine_clock(rdev));
  1513. seq_printf(m, "default memory clock: %u0 kHz\n", rdev->pm.default_mclk);
  1514. if (rdev->asic->pm.get_memory_clock)
  1515. seq_printf(m, "current memory clock: %u0 kHz\n", radeon_get_memory_clock(rdev));
  1516. if (rdev->pm.current_vddc)
  1517. seq_printf(m, "voltage: %u mV\n", rdev->pm.current_vddc);
  1518. if (rdev->asic->pm.get_pcie_lanes)
  1519. seq_printf(m, "PCIE lanes: %d\n", radeon_get_pcie_lanes(rdev));
  1520. }
  1521. return 0;
  1522. }
  1523. static struct drm_info_list radeon_pm_info_list[] = {
  1524. {"radeon_pm_info", radeon_debugfs_pm_info, 0, NULL},
  1525. };
  1526. #endif
  1527. static int radeon_debugfs_pm_init(struct radeon_device *rdev)
  1528. {
  1529. #if defined(CONFIG_DEBUG_FS)
  1530. return radeon_debugfs_add_files(rdev, radeon_pm_info_list, ARRAY_SIZE(radeon_pm_info_list));
  1531. #else
  1532. return 0;
  1533. #endif
  1534. }