radeon_pm.c 56 KB

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