radeon_pm.c 57 KB

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