amd-pstate.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * amd-pstate.c - AMD Processor P-state Frequency Driver
  4. *
  5. * Copyright (C) 2021 Advanced Micro Devices, Inc. All Rights Reserved.
  6. *
  7. * Author: Huang Rui <ray.huang@amd.com>
  8. *
  9. * AMD P-State introduces a new CPU performance scaling design for AMD
  10. * processors using the ACPI Collaborative Performance and Power Control (CPPC)
  11. * feature which works with the AMD SMU firmware providing a finer grained
  12. * frequency control range. It is to replace the legacy ACPI P-States control,
  13. * allows a flexible, low-latency interface for the Linux kernel to directly
  14. * communicate the performance hints to hardware.
  15. *
  16. * AMD P-State is supported on recent AMD Zen base CPU series include some of
  17. * Zen2 and Zen3 processors. _CPC needs to be present in the ACPI tables of AMD
  18. * P-State supported system. And there are two types of hardware implementations
  19. * for AMD P-State: 1) Full MSR Solution and 2) Shared Memory Solution.
  20. * X86_FEATURE_CPPC CPU feature flag is used to distinguish the different types.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/smp.h>
  27. #include <linux/sched.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/compiler.h>
  30. #include <linux/dmi.h>
  31. #include <linux/slab.h>
  32. #include <linux/acpi.h>
  33. #include <linux/io.h>
  34. #include <linux/delay.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/static_call.h>
  37. #include <linux/topology.h>
  38. #include <acpi/processor.h>
  39. #include <acpi/cppc_acpi.h>
  40. #include <asm/msr.h>
  41. #include <asm/processor.h>
  42. #include <asm/cpufeature.h>
  43. #include <asm/cpu_device_id.h>
  44. #include "amd-pstate.h"
  45. #include "amd-pstate-trace.h"
  46. #define AMD_PSTATE_TRANSITION_LATENCY 20000
  47. #define AMD_PSTATE_TRANSITION_DELAY 1000
  48. #define AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY 600
  49. #define AMD_CPPC_EPP_PERFORMANCE 0x00
  50. #define AMD_CPPC_EPP_BALANCE_PERFORMANCE 0x80
  51. #define AMD_CPPC_EPP_BALANCE_POWERSAVE 0xBF
  52. #define AMD_CPPC_EPP_POWERSAVE 0xFF
  53. static const char * const amd_pstate_mode_string[] = {
  54. [AMD_PSTATE_UNDEFINED] = "undefined",
  55. [AMD_PSTATE_DISABLE] = "disable",
  56. [AMD_PSTATE_PASSIVE] = "passive",
  57. [AMD_PSTATE_ACTIVE] = "active",
  58. [AMD_PSTATE_GUIDED] = "guided",
  59. NULL,
  60. };
  61. const char *amd_pstate_get_mode_string(enum amd_pstate_mode mode)
  62. {
  63. if (mode < 0 || mode >= AMD_PSTATE_MAX)
  64. return NULL;
  65. return amd_pstate_mode_string[mode];
  66. }
  67. EXPORT_SYMBOL_GPL(amd_pstate_get_mode_string);
  68. struct quirk_entry {
  69. u32 nominal_freq;
  70. u32 lowest_freq;
  71. };
  72. static struct cpufreq_driver *current_pstate_driver;
  73. static struct cpufreq_driver amd_pstate_driver;
  74. static struct cpufreq_driver amd_pstate_epp_driver;
  75. static int cppc_state = AMD_PSTATE_UNDEFINED;
  76. static bool cppc_enabled;
  77. static bool amd_pstate_prefcore = true;
  78. static struct quirk_entry *quirks;
  79. /*
  80. * AMD Energy Preference Performance (EPP)
  81. * The EPP is used in the CCLK DPM controller to drive
  82. * the frequency that a core is going to operate during
  83. * short periods of activity. EPP values will be utilized for
  84. * different OS profiles (balanced, performance, power savings)
  85. * display strings corresponding to EPP index in the
  86. * energy_perf_strings[]
  87. * index String
  88. *-------------------------------------
  89. * 0 default
  90. * 1 performance
  91. * 2 balance_performance
  92. * 3 balance_power
  93. * 4 power
  94. */
  95. enum energy_perf_value_index {
  96. EPP_INDEX_DEFAULT = 0,
  97. EPP_INDEX_PERFORMANCE,
  98. EPP_INDEX_BALANCE_PERFORMANCE,
  99. EPP_INDEX_BALANCE_POWERSAVE,
  100. EPP_INDEX_POWERSAVE,
  101. };
  102. static const char * const energy_perf_strings[] = {
  103. [EPP_INDEX_DEFAULT] = "default",
  104. [EPP_INDEX_PERFORMANCE] = "performance",
  105. [EPP_INDEX_BALANCE_PERFORMANCE] = "balance_performance",
  106. [EPP_INDEX_BALANCE_POWERSAVE] = "balance_power",
  107. [EPP_INDEX_POWERSAVE] = "power",
  108. NULL
  109. };
  110. static unsigned int epp_values[] = {
  111. [EPP_INDEX_DEFAULT] = 0,
  112. [EPP_INDEX_PERFORMANCE] = AMD_CPPC_EPP_PERFORMANCE,
  113. [EPP_INDEX_BALANCE_PERFORMANCE] = AMD_CPPC_EPP_BALANCE_PERFORMANCE,
  114. [EPP_INDEX_BALANCE_POWERSAVE] = AMD_CPPC_EPP_BALANCE_POWERSAVE,
  115. [EPP_INDEX_POWERSAVE] = AMD_CPPC_EPP_POWERSAVE,
  116. };
  117. typedef int (*cppc_mode_transition_fn)(int);
  118. static struct quirk_entry quirk_amd_7k62 = {
  119. .nominal_freq = 2600,
  120. .lowest_freq = 550,
  121. };
  122. static int __init dmi_matched_7k62_bios_bug(const struct dmi_system_id *dmi)
  123. {
  124. /**
  125. * match the broken bios for family 17h processor support CPPC V2
  126. * broken BIOS lack of nominal_freq and lowest_freq capabilities
  127. * definition in ACPI tables
  128. */
  129. if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
  130. quirks = dmi->driver_data;
  131. pr_info("Overriding nominal and lowest frequencies for %s\n", dmi->ident);
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. static const struct dmi_system_id amd_pstate_quirks_table[] __initconst = {
  137. {
  138. .callback = dmi_matched_7k62_bios_bug,
  139. .ident = "AMD EPYC 7K62",
  140. .matches = {
  141. DMI_MATCH(DMI_BIOS_VERSION, "5.14"),
  142. DMI_MATCH(DMI_BIOS_RELEASE, "12/12/2019"),
  143. },
  144. .driver_data = &quirk_amd_7k62,
  145. },
  146. {}
  147. };
  148. MODULE_DEVICE_TABLE(dmi, amd_pstate_quirks_table);
  149. static inline int get_mode_idx_from_str(const char *str, size_t size)
  150. {
  151. int i;
  152. for (i=0; i < AMD_PSTATE_MAX; i++) {
  153. if (!strncmp(str, amd_pstate_mode_string[i], size))
  154. return i;
  155. }
  156. return -EINVAL;
  157. }
  158. static DEFINE_MUTEX(amd_pstate_limits_lock);
  159. static DEFINE_MUTEX(amd_pstate_driver_lock);
  160. static s16 amd_pstate_get_epp(struct amd_cpudata *cpudata, u64 cppc_req_cached)
  161. {
  162. u64 epp;
  163. int ret;
  164. if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
  165. if (!cppc_req_cached) {
  166. epp = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
  167. &cppc_req_cached);
  168. if (epp)
  169. return epp;
  170. }
  171. epp = (cppc_req_cached >> 24) & 0xFF;
  172. } else {
  173. ret = cppc_get_epp_perf(cpudata->cpu, &epp);
  174. if (ret < 0) {
  175. pr_debug("Could not retrieve energy perf value (%d)\n", ret);
  176. return -EIO;
  177. }
  178. }
  179. return (s16)(epp & 0xff);
  180. }
  181. static int amd_pstate_get_energy_pref_index(struct amd_cpudata *cpudata)
  182. {
  183. s16 epp;
  184. int index = -EINVAL;
  185. epp = amd_pstate_get_epp(cpudata, 0);
  186. if (epp < 0)
  187. return epp;
  188. switch (epp) {
  189. case AMD_CPPC_EPP_PERFORMANCE:
  190. index = EPP_INDEX_PERFORMANCE;
  191. break;
  192. case AMD_CPPC_EPP_BALANCE_PERFORMANCE:
  193. index = EPP_INDEX_BALANCE_PERFORMANCE;
  194. break;
  195. case AMD_CPPC_EPP_BALANCE_POWERSAVE:
  196. index = EPP_INDEX_BALANCE_POWERSAVE;
  197. break;
  198. case AMD_CPPC_EPP_POWERSAVE:
  199. index = EPP_INDEX_POWERSAVE;
  200. break;
  201. default:
  202. break;
  203. }
  204. return index;
  205. }
  206. static void pstate_update_perf(struct amd_cpudata *cpudata, u32 min_perf,
  207. u32 des_perf, u32 max_perf, bool fast_switch)
  208. {
  209. if (fast_switch)
  210. wrmsrl(MSR_AMD_CPPC_REQ, READ_ONCE(cpudata->cppc_req_cached));
  211. else
  212. wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ,
  213. READ_ONCE(cpudata->cppc_req_cached));
  214. }
  215. DEFINE_STATIC_CALL(amd_pstate_update_perf, pstate_update_perf);
  216. static inline void amd_pstate_update_perf(struct amd_cpudata *cpudata,
  217. u32 min_perf, u32 des_perf,
  218. u32 max_perf, bool fast_switch)
  219. {
  220. static_call(amd_pstate_update_perf)(cpudata, min_perf, des_perf,
  221. max_perf, fast_switch);
  222. }
  223. static int amd_pstate_set_epp(struct amd_cpudata *cpudata, u32 epp)
  224. {
  225. int ret;
  226. struct cppc_perf_ctrls perf_ctrls;
  227. if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
  228. u64 value = READ_ONCE(cpudata->cppc_req_cached);
  229. value &= ~GENMASK_ULL(31, 24);
  230. value |= (u64)epp << 24;
  231. WRITE_ONCE(cpudata->cppc_req_cached, value);
  232. ret = wrmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, value);
  233. if (!ret)
  234. cpudata->epp_cached = epp;
  235. } else {
  236. amd_pstate_update_perf(cpudata, cpudata->min_limit_perf, 0U,
  237. cpudata->max_limit_perf, false);
  238. perf_ctrls.energy_perf = epp;
  239. ret = cppc_set_epp_perf(cpudata->cpu, &perf_ctrls, 1);
  240. if (ret) {
  241. pr_debug("failed to set energy perf value (%d)\n", ret);
  242. return ret;
  243. }
  244. cpudata->epp_cached = epp;
  245. }
  246. return ret;
  247. }
  248. static int amd_pstate_set_energy_pref_index(struct amd_cpudata *cpudata,
  249. int pref_index)
  250. {
  251. int epp = -EINVAL;
  252. int ret;
  253. if (!pref_index)
  254. epp = cpudata->epp_default;
  255. if (epp == -EINVAL)
  256. epp = epp_values[pref_index];
  257. if (epp > 0 && cpudata->policy == CPUFREQ_POLICY_PERFORMANCE) {
  258. pr_debug("EPP cannot be set under performance policy\n");
  259. return -EBUSY;
  260. }
  261. ret = amd_pstate_set_epp(cpudata, epp);
  262. return ret;
  263. }
  264. static inline int pstate_enable(bool enable)
  265. {
  266. int ret, cpu;
  267. unsigned long logical_proc_id_mask = 0;
  268. if (enable == cppc_enabled)
  269. return 0;
  270. for_each_present_cpu(cpu) {
  271. unsigned long logical_id = topology_logical_package_id(cpu);
  272. if (test_bit(logical_id, &logical_proc_id_mask))
  273. continue;
  274. set_bit(logical_id, &logical_proc_id_mask);
  275. ret = wrmsrl_safe_on_cpu(cpu, MSR_AMD_CPPC_ENABLE,
  276. enable);
  277. if (ret)
  278. return ret;
  279. }
  280. cppc_enabled = enable;
  281. return 0;
  282. }
  283. static int cppc_enable(bool enable)
  284. {
  285. int cpu, ret = 0;
  286. struct cppc_perf_ctrls perf_ctrls;
  287. if (enable == cppc_enabled)
  288. return 0;
  289. for_each_present_cpu(cpu) {
  290. ret = cppc_set_enable(cpu, enable);
  291. if (ret)
  292. return ret;
  293. /* Enable autonomous mode for EPP */
  294. if (cppc_state == AMD_PSTATE_ACTIVE) {
  295. /* Set desired perf as zero to allow EPP firmware control */
  296. perf_ctrls.desired_perf = 0;
  297. ret = cppc_set_perf(cpu, &perf_ctrls);
  298. if (ret)
  299. return ret;
  300. }
  301. }
  302. cppc_enabled = enable;
  303. return ret;
  304. }
  305. DEFINE_STATIC_CALL(amd_pstate_enable, pstate_enable);
  306. static inline int amd_pstate_enable(bool enable)
  307. {
  308. return static_call(amd_pstate_enable)(enable);
  309. }
  310. static int pstate_init_perf(struct amd_cpudata *cpudata)
  311. {
  312. u64 cap1;
  313. int ret = rdmsrl_safe_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1,
  314. &cap1);
  315. if (ret)
  316. return ret;
  317. WRITE_ONCE(cpudata->highest_perf, AMD_CPPC_HIGHEST_PERF(cap1));
  318. WRITE_ONCE(cpudata->max_limit_perf, AMD_CPPC_HIGHEST_PERF(cap1));
  319. WRITE_ONCE(cpudata->nominal_perf, AMD_CPPC_NOMINAL_PERF(cap1));
  320. WRITE_ONCE(cpudata->lowest_nonlinear_perf, AMD_CPPC_LOWNONLIN_PERF(cap1));
  321. WRITE_ONCE(cpudata->lowest_perf, AMD_CPPC_LOWEST_PERF(cap1));
  322. WRITE_ONCE(cpudata->prefcore_ranking, AMD_CPPC_HIGHEST_PERF(cap1));
  323. WRITE_ONCE(cpudata->min_limit_perf, AMD_CPPC_LOWEST_PERF(cap1));
  324. return 0;
  325. }
  326. static int cppc_init_perf(struct amd_cpudata *cpudata)
  327. {
  328. struct cppc_perf_caps cppc_perf;
  329. int ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
  330. if (ret)
  331. return ret;
  332. WRITE_ONCE(cpudata->highest_perf, cppc_perf.highest_perf);
  333. WRITE_ONCE(cpudata->max_limit_perf, cppc_perf.highest_perf);
  334. WRITE_ONCE(cpudata->nominal_perf, cppc_perf.nominal_perf);
  335. WRITE_ONCE(cpudata->lowest_nonlinear_perf,
  336. cppc_perf.lowest_nonlinear_perf);
  337. WRITE_ONCE(cpudata->lowest_perf, cppc_perf.lowest_perf);
  338. WRITE_ONCE(cpudata->prefcore_ranking, cppc_perf.highest_perf);
  339. WRITE_ONCE(cpudata->min_limit_perf, cppc_perf.lowest_perf);
  340. if (cppc_state == AMD_PSTATE_ACTIVE)
  341. return 0;
  342. ret = cppc_get_auto_sel_caps(cpudata->cpu, &cppc_perf);
  343. if (ret) {
  344. pr_warn("failed to get auto_sel, ret: %d\n", ret);
  345. return 0;
  346. }
  347. ret = cppc_set_auto_sel(cpudata->cpu,
  348. (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
  349. if (ret)
  350. pr_warn("failed to set auto_sel, ret: %d\n", ret);
  351. return ret;
  352. }
  353. DEFINE_STATIC_CALL(amd_pstate_init_perf, pstate_init_perf);
  354. static inline int amd_pstate_init_perf(struct amd_cpudata *cpudata)
  355. {
  356. return static_call(amd_pstate_init_perf)(cpudata);
  357. }
  358. static void cppc_update_perf(struct amd_cpudata *cpudata,
  359. u32 min_perf, u32 des_perf,
  360. u32 max_perf, bool fast_switch)
  361. {
  362. struct cppc_perf_ctrls perf_ctrls;
  363. perf_ctrls.max_perf = max_perf;
  364. perf_ctrls.min_perf = min_perf;
  365. perf_ctrls.desired_perf = des_perf;
  366. cppc_set_perf(cpudata->cpu, &perf_ctrls);
  367. }
  368. static inline bool amd_pstate_sample(struct amd_cpudata *cpudata)
  369. {
  370. u64 aperf, mperf, tsc;
  371. unsigned long flags;
  372. local_irq_save(flags);
  373. rdmsrl(MSR_IA32_APERF, aperf);
  374. rdmsrl(MSR_IA32_MPERF, mperf);
  375. tsc = rdtsc();
  376. if (cpudata->prev.mperf == mperf || cpudata->prev.tsc == tsc) {
  377. local_irq_restore(flags);
  378. return false;
  379. }
  380. local_irq_restore(flags);
  381. cpudata->cur.aperf = aperf;
  382. cpudata->cur.mperf = mperf;
  383. cpudata->cur.tsc = tsc;
  384. cpudata->cur.aperf -= cpudata->prev.aperf;
  385. cpudata->cur.mperf -= cpudata->prev.mperf;
  386. cpudata->cur.tsc -= cpudata->prev.tsc;
  387. cpudata->prev.aperf = aperf;
  388. cpudata->prev.mperf = mperf;
  389. cpudata->prev.tsc = tsc;
  390. cpudata->freq = div64_u64((cpudata->cur.aperf * cpu_khz), cpudata->cur.mperf);
  391. return true;
  392. }
  393. static void amd_pstate_update(struct amd_cpudata *cpudata, u32 min_perf,
  394. u32 des_perf, u32 max_perf, bool fast_switch, int gov_flags)
  395. {
  396. unsigned long max_freq;
  397. struct cpufreq_policy *policy = cpufreq_cpu_get(cpudata->cpu);
  398. u64 prev = READ_ONCE(cpudata->cppc_req_cached);
  399. u32 nominal_perf = READ_ONCE(cpudata->nominal_perf);
  400. u64 value = prev;
  401. if (!policy)
  402. return;
  403. min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
  404. cpudata->max_limit_perf);
  405. max_perf = clamp_t(unsigned long, max_perf, cpudata->min_limit_perf,
  406. cpudata->max_limit_perf);
  407. des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
  408. max_freq = READ_ONCE(cpudata->max_limit_freq);
  409. policy->cur = div_u64(des_perf * max_freq, max_perf);
  410. if ((cppc_state == AMD_PSTATE_GUIDED) && (gov_flags & CPUFREQ_GOV_DYNAMIC_SWITCHING)) {
  411. min_perf = des_perf;
  412. des_perf = 0;
  413. }
  414. value &= ~AMD_CPPC_MIN_PERF(~0L);
  415. value |= AMD_CPPC_MIN_PERF(min_perf);
  416. value &= ~AMD_CPPC_DES_PERF(~0L);
  417. value |= AMD_CPPC_DES_PERF(des_perf);
  418. /* limit the max perf when core performance boost feature is disabled */
  419. if (!cpudata->boost_supported)
  420. max_perf = min_t(unsigned long, nominal_perf, max_perf);
  421. value &= ~AMD_CPPC_MAX_PERF(~0L);
  422. value |= AMD_CPPC_MAX_PERF(max_perf);
  423. if (trace_amd_pstate_perf_enabled() && amd_pstate_sample(cpudata)) {
  424. trace_amd_pstate_perf(min_perf, des_perf, max_perf, cpudata->freq,
  425. cpudata->cur.mperf, cpudata->cur.aperf, cpudata->cur.tsc,
  426. cpudata->cpu, (value != prev), fast_switch);
  427. }
  428. if (value == prev)
  429. goto cpufreq_policy_put;
  430. WRITE_ONCE(cpudata->cppc_req_cached, value);
  431. amd_pstate_update_perf(cpudata, min_perf, des_perf,
  432. max_perf, fast_switch);
  433. cpufreq_policy_put:
  434. cpufreq_cpu_put(policy);
  435. }
  436. static int amd_pstate_verify(struct cpufreq_policy_data *policy)
  437. {
  438. cpufreq_verify_within_cpu_limits(policy);
  439. return 0;
  440. }
  441. static int amd_pstate_update_min_max_limit(struct cpufreq_policy *policy)
  442. {
  443. u32 max_limit_perf, min_limit_perf, lowest_perf, max_perf;
  444. struct amd_cpudata *cpudata = policy->driver_data;
  445. if (cpudata->boost_supported && !policy->boost_enabled)
  446. max_perf = READ_ONCE(cpudata->nominal_perf);
  447. else
  448. max_perf = READ_ONCE(cpudata->highest_perf);
  449. max_limit_perf = div_u64(policy->max * max_perf, policy->cpuinfo.max_freq);
  450. min_limit_perf = div_u64(policy->min * max_perf, policy->cpuinfo.max_freq);
  451. lowest_perf = READ_ONCE(cpudata->lowest_perf);
  452. if (min_limit_perf < lowest_perf)
  453. min_limit_perf = lowest_perf;
  454. if (max_limit_perf < min_limit_perf)
  455. max_limit_perf = min_limit_perf;
  456. WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
  457. WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
  458. WRITE_ONCE(cpudata->max_limit_freq, policy->max);
  459. WRITE_ONCE(cpudata->min_limit_freq, policy->min);
  460. return 0;
  461. }
  462. static int amd_pstate_update_freq(struct cpufreq_policy *policy,
  463. unsigned int target_freq, bool fast_switch)
  464. {
  465. struct cpufreq_freqs freqs;
  466. struct amd_cpudata *cpudata = policy->driver_data;
  467. unsigned long max_perf, min_perf, des_perf, cap_perf;
  468. if (!cpudata->max_freq)
  469. return -ENODEV;
  470. if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
  471. amd_pstate_update_min_max_limit(policy);
  472. cap_perf = READ_ONCE(cpudata->highest_perf);
  473. min_perf = READ_ONCE(cpudata->lowest_perf);
  474. max_perf = cap_perf;
  475. freqs.old = policy->cur;
  476. freqs.new = target_freq;
  477. des_perf = DIV_ROUND_CLOSEST(target_freq * cap_perf,
  478. cpudata->max_freq);
  479. WARN_ON(fast_switch && !policy->fast_switch_enabled);
  480. /*
  481. * If fast_switch is desired, then there aren't any registered
  482. * transition notifiers. See comment for
  483. * cpufreq_enable_fast_switch().
  484. */
  485. if (!fast_switch)
  486. cpufreq_freq_transition_begin(policy, &freqs);
  487. amd_pstate_update(cpudata, min_perf, des_perf,
  488. max_perf, fast_switch, policy->governor->flags);
  489. if (!fast_switch)
  490. cpufreq_freq_transition_end(policy, &freqs, false);
  491. return 0;
  492. }
  493. static int amd_pstate_target(struct cpufreq_policy *policy,
  494. unsigned int target_freq,
  495. unsigned int relation)
  496. {
  497. return amd_pstate_update_freq(policy, target_freq, false);
  498. }
  499. static unsigned int amd_pstate_fast_switch(struct cpufreq_policy *policy,
  500. unsigned int target_freq)
  501. {
  502. if (!amd_pstate_update_freq(policy, target_freq, true))
  503. return target_freq;
  504. return policy->cur;
  505. }
  506. static void amd_pstate_adjust_perf(unsigned int cpu,
  507. unsigned long _min_perf,
  508. unsigned long target_perf,
  509. unsigned long capacity)
  510. {
  511. unsigned long max_perf, min_perf, des_perf,
  512. cap_perf, lowest_nonlinear_perf;
  513. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  514. struct amd_cpudata *cpudata;
  515. if (!policy)
  516. return;
  517. cpudata = policy->driver_data;
  518. if (policy->min != cpudata->min_limit_freq || policy->max != cpudata->max_limit_freq)
  519. amd_pstate_update_min_max_limit(policy);
  520. cap_perf = READ_ONCE(cpudata->highest_perf);
  521. lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
  522. des_perf = cap_perf;
  523. if (target_perf < capacity)
  524. des_perf = DIV_ROUND_UP(cap_perf * target_perf, capacity);
  525. min_perf = READ_ONCE(cpudata->lowest_perf);
  526. if (_min_perf < capacity)
  527. min_perf = DIV_ROUND_UP(cap_perf * _min_perf, capacity);
  528. if (min_perf < lowest_nonlinear_perf)
  529. min_perf = lowest_nonlinear_perf;
  530. max_perf = cap_perf;
  531. if (max_perf < min_perf)
  532. max_perf = min_perf;
  533. des_perf = clamp_t(unsigned long, des_perf, min_perf, max_perf);
  534. amd_pstate_update(cpudata, min_perf, des_perf, max_perf, true,
  535. policy->governor->flags);
  536. cpufreq_cpu_put(policy);
  537. }
  538. static int amd_pstate_cpu_boost_update(struct cpufreq_policy *policy, bool on)
  539. {
  540. struct amd_cpudata *cpudata = policy->driver_data;
  541. u32 nominal_freq, max_freq;
  542. int ret = 0;
  543. nominal_freq = READ_ONCE(cpudata->nominal_freq);
  544. max_freq = READ_ONCE(cpudata->max_freq);
  545. if (on)
  546. policy->cpuinfo.max_freq = max_freq;
  547. else if (policy->cpuinfo.max_freq > nominal_freq * 1000)
  548. policy->cpuinfo.max_freq = nominal_freq * 1000;
  549. policy->max = policy->cpuinfo.max_freq;
  550. if (cppc_state == AMD_PSTATE_PASSIVE) {
  551. ret = freq_qos_update_request(&cpudata->req[1], policy->cpuinfo.max_freq);
  552. if (ret < 0)
  553. pr_debug("Failed to update freq constraint: CPU%d\n", cpudata->cpu);
  554. }
  555. return ret < 0 ? ret : 0;
  556. }
  557. static int amd_pstate_set_boost(struct cpufreq_policy *policy, int state)
  558. {
  559. struct amd_cpudata *cpudata = policy->driver_data;
  560. int ret;
  561. if (!cpudata->boost_supported) {
  562. pr_err("Boost mode is not supported by this processor or SBIOS\n");
  563. return -EOPNOTSUPP;
  564. }
  565. ret = amd_pstate_cpu_boost_update(policy, state);
  566. WRITE_ONCE(cpudata->boost_state, !ret ? state : false);
  567. policy->boost_enabled = !ret ? state : false;
  568. refresh_frequency_limits(policy);
  569. return ret;
  570. }
  571. static int amd_pstate_init_boost_support(struct amd_cpudata *cpudata)
  572. {
  573. u64 boost_val;
  574. int ret = -1;
  575. /*
  576. * If platform has no CPB support or disable it, initialize current driver
  577. * boost_enabled state to be false, it is not an error for cpufreq core to handle.
  578. */
  579. if (!cpu_feature_enabled(X86_FEATURE_CPB)) {
  580. pr_debug_once("Boost CPB capabilities not present in the processor\n");
  581. ret = 0;
  582. goto exit_err;
  583. }
  584. /* at least one CPU supports CPB, even if others fail later on to set up */
  585. current_pstate_driver->boost_enabled = true;
  586. ret = rdmsrl_on_cpu(cpudata->cpu, MSR_K7_HWCR, &boost_val);
  587. if (ret) {
  588. pr_err_once("failed to read initial CPU boost state!\n");
  589. ret = -EIO;
  590. goto exit_err;
  591. }
  592. if (!(boost_val & MSR_K7_HWCR_CPB_DIS))
  593. cpudata->boost_supported = true;
  594. return 0;
  595. exit_err:
  596. cpudata->boost_supported = false;
  597. return ret;
  598. }
  599. static void amd_perf_ctl_reset(unsigned int cpu)
  600. {
  601. wrmsrl_on_cpu(cpu, MSR_AMD_PERF_CTL, 0);
  602. }
  603. /*
  604. * Set amd-pstate preferred core enable can't be done directly from cpufreq callbacks
  605. * due to locking, so queue the work for later.
  606. */
  607. static void amd_pstste_sched_prefcore_workfn(struct work_struct *work)
  608. {
  609. sched_set_itmt_support();
  610. }
  611. static DECLARE_WORK(sched_prefcore_work, amd_pstste_sched_prefcore_workfn);
  612. #define CPPC_MAX_PERF U8_MAX
  613. static void amd_pstate_init_prefcore(struct amd_cpudata *cpudata)
  614. {
  615. /* user disabled or not detected */
  616. if (!amd_pstate_prefcore)
  617. return;
  618. cpudata->hw_prefcore = true;
  619. /*
  620. * The priorities can be set regardless of whether or not
  621. * sched_set_itmt_support(true) has been called and it is valid to
  622. * update them at any time after it has been called.
  623. */
  624. sched_set_itmt_core_prio((int)READ_ONCE(cpudata->highest_perf), cpudata->cpu);
  625. schedule_work(&sched_prefcore_work);
  626. }
  627. static void amd_pstate_update_limits(unsigned int cpu)
  628. {
  629. struct cpufreq_policy *policy = NULL;
  630. struct amd_cpudata *cpudata;
  631. u32 prev_high = 0, cur_high = 0;
  632. int ret;
  633. bool highest_perf_changed = false;
  634. if (!amd_pstate_prefcore)
  635. return;
  636. policy = cpufreq_cpu_get(cpu);
  637. if (!policy)
  638. return;
  639. cpudata = policy->driver_data;
  640. guard(mutex)(&amd_pstate_driver_lock);
  641. ret = amd_get_highest_perf(cpu, &cur_high);
  642. if (ret) {
  643. cpufreq_cpu_put(policy);
  644. return;
  645. }
  646. prev_high = READ_ONCE(cpudata->prefcore_ranking);
  647. highest_perf_changed = (prev_high != cur_high);
  648. if (highest_perf_changed) {
  649. WRITE_ONCE(cpudata->prefcore_ranking, cur_high);
  650. if (cur_high < CPPC_MAX_PERF)
  651. sched_set_itmt_core_prio((int)cur_high, cpu);
  652. }
  653. cpufreq_cpu_put(policy);
  654. if (!highest_perf_changed)
  655. cpufreq_update_policy(cpu);
  656. }
  657. /*
  658. * Get pstate transition delay time from ACPI tables that firmware set
  659. * instead of using hardcode value directly.
  660. */
  661. static u32 amd_pstate_get_transition_delay_us(unsigned int cpu)
  662. {
  663. u32 transition_delay_ns;
  664. transition_delay_ns = cppc_get_transition_latency(cpu);
  665. if (transition_delay_ns == CPUFREQ_ETERNAL) {
  666. if (cpu_feature_enabled(X86_FEATURE_FAST_CPPC))
  667. return AMD_PSTATE_FAST_CPPC_TRANSITION_DELAY;
  668. else
  669. return AMD_PSTATE_TRANSITION_DELAY;
  670. }
  671. return transition_delay_ns / NSEC_PER_USEC;
  672. }
  673. /*
  674. * Get pstate transition latency value from ACPI tables that firmware
  675. * set instead of using hardcode value directly.
  676. */
  677. static u32 amd_pstate_get_transition_latency(unsigned int cpu)
  678. {
  679. u32 transition_latency;
  680. transition_latency = cppc_get_transition_latency(cpu);
  681. if (transition_latency == CPUFREQ_ETERNAL)
  682. return AMD_PSTATE_TRANSITION_LATENCY;
  683. return transition_latency;
  684. }
  685. /*
  686. * amd_pstate_init_freq: Initialize the max_freq, min_freq,
  687. * nominal_freq and lowest_nonlinear_freq for
  688. * the @cpudata object.
  689. *
  690. * Requires: highest_perf, lowest_perf, nominal_perf and
  691. * lowest_nonlinear_perf members of @cpudata to be
  692. * initialized.
  693. *
  694. * Returns 0 on success, non-zero value on failure.
  695. */
  696. static int amd_pstate_init_freq(struct amd_cpudata *cpudata)
  697. {
  698. int ret;
  699. u32 min_freq, max_freq;
  700. u64 numerator;
  701. u32 nominal_perf, nominal_freq;
  702. u32 lowest_nonlinear_perf, lowest_nonlinear_freq;
  703. u32 boost_ratio, lowest_nonlinear_ratio;
  704. struct cppc_perf_caps cppc_perf;
  705. ret = cppc_get_perf_caps(cpudata->cpu, &cppc_perf);
  706. if (ret)
  707. return ret;
  708. if (quirks && quirks->lowest_freq)
  709. min_freq = quirks->lowest_freq * 1000;
  710. else
  711. min_freq = cppc_perf.lowest_freq * 1000;
  712. if (quirks && quirks->nominal_freq)
  713. nominal_freq = quirks->nominal_freq ;
  714. else
  715. nominal_freq = cppc_perf.nominal_freq;
  716. nominal_perf = READ_ONCE(cpudata->nominal_perf);
  717. ret = amd_get_boost_ratio_numerator(cpudata->cpu, &numerator);
  718. if (ret)
  719. return ret;
  720. boost_ratio = div_u64(numerator << SCHED_CAPACITY_SHIFT, nominal_perf);
  721. max_freq = (nominal_freq * boost_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
  722. lowest_nonlinear_perf = READ_ONCE(cpudata->lowest_nonlinear_perf);
  723. lowest_nonlinear_ratio = div_u64(lowest_nonlinear_perf << SCHED_CAPACITY_SHIFT,
  724. nominal_perf);
  725. lowest_nonlinear_freq = (nominal_freq * lowest_nonlinear_ratio >> SCHED_CAPACITY_SHIFT) * 1000;
  726. WRITE_ONCE(cpudata->min_freq, min_freq);
  727. WRITE_ONCE(cpudata->lowest_nonlinear_freq, lowest_nonlinear_freq);
  728. WRITE_ONCE(cpudata->nominal_freq, nominal_freq);
  729. WRITE_ONCE(cpudata->max_freq, max_freq);
  730. /**
  731. * Below values need to be initialized correctly, otherwise driver will fail to load
  732. * max_freq is calculated according to (nominal_freq * highest_perf)/nominal_perf
  733. * lowest_nonlinear_freq is a value between [min_freq, nominal_freq]
  734. * Check _CPC in ACPI table objects if any values are incorrect
  735. */
  736. if (min_freq <= 0 || max_freq <= 0 || nominal_freq <= 0 || min_freq > max_freq) {
  737. pr_err("min_freq(%d) or max_freq(%d) or nominal_freq(%d) value is incorrect\n",
  738. min_freq, max_freq, nominal_freq * 1000);
  739. return -EINVAL;
  740. }
  741. if (lowest_nonlinear_freq <= min_freq || lowest_nonlinear_freq > nominal_freq * 1000) {
  742. pr_err("lowest_nonlinear_freq(%d) value is out of range [min_freq(%d), nominal_freq(%d)]\n",
  743. lowest_nonlinear_freq, min_freq, nominal_freq * 1000);
  744. return -EINVAL;
  745. }
  746. return 0;
  747. }
  748. static int amd_pstate_cpu_init(struct cpufreq_policy *policy)
  749. {
  750. int min_freq, max_freq, ret;
  751. struct device *dev;
  752. struct amd_cpudata *cpudata;
  753. /*
  754. * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
  755. * which is ideal for initialization process.
  756. */
  757. amd_perf_ctl_reset(policy->cpu);
  758. dev = get_cpu_device(policy->cpu);
  759. if (!dev)
  760. return -ENODEV;
  761. cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
  762. if (!cpudata)
  763. return -ENOMEM;
  764. cpudata->cpu = policy->cpu;
  765. ret = amd_pstate_init_perf(cpudata);
  766. if (ret)
  767. goto free_cpudata1;
  768. amd_pstate_init_prefcore(cpudata);
  769. ret = amd_pstate_init_freq(cpudata);
  770. if (ret)
  771. goto free_cpudata1;
  772. ret = amd_pstate_init_boost_support(cpudata);
  773. if (ret)
  774. goto free_cpudata1;
  775. min_freq = READ_ONCE(cpudata->min_freq);
  776. max_freq = READ_ONCE(cpudata->max_freq);
  777. policy->cpuinfo.transition_latency = amd_pstate_get_transition_latency(policy->cpu);
  778. policy->transition_delay_us = amd_pstate_get_transition_delay_us(policy->cpu);
  779. policy->min = min_freq;
  780. policy->max = max_freq;
  781. policy->cpuinfo.min_freq = min_freq;
  782. policy->cpuinfo.max_freq = max_freq;
  783. policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
  784. /* It will be updated by governor */
  785. policy->cur = policy->cpuinfo.min_freq;
  786. if (cpu_feature_enabled(X86_FEATURE_CPPC))
  787. policy->fast_switch_possible = true;
  788. ret = freq_qos_add_request(&policy->constraints, &cpudata->req[0],
  789. FREQ_QOS_MIN, policy->cpuinfo.min_freq);
  790. if (ret < 0) {
  791. dev_err(dev, "Failed to add min-freq constraint (%d)\n", ret);
  792. goto free_cpudata1;
  793. }
  794. ret = freq_qos_add_request(&policy->constraints, &cpudata->req[1],
  795. FREQ_QOS_MAX, policy->cpuinfo.max_freq);
  796. if (ret < 0) {
  797. dev_err(dev, "Failed to add max-freq constraint (%d)\n", ret);
  798. goto free_cpudata2;
  799. }
  800. cpudata->max_limit_freq = max_freq;
  801. cpudata->min_limit_freq = min_freq;
  802. policy->driver_data = cpudata;
  803. if (!current_pstate_driver->adjust_perf)
  804. current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
  805. return 0;
  806. free_cpudata2:
  807. freq_qos_remove_request(&cpudata->req[0]);
  808. free_cpudata1:
  809. kfree(cpudata);
  810. return ret;
  811. }
  812. static void amd_pstate_cpu_exit(struct cpufreq_policy *policy)
  813. {
  814. struct amd_cpudata *cpudata = policy->driver_data;
  815. freq_qos_remove_request(&cpudata->req[1]);
  816. freq_qos_remove_request(&cpudata->req[0]);
  817. policy->fast_switch_possible = false;
  818. kfree(cpudata);
  819. }
  820. static int amd_pstate_cpu_resume(struct cpufreq_policy *policy)
  821. {
  822. int ret;
  823. ret = amd_pstate_enable(true);
  824. if (ret)
  825. pr_err("failed to enable amd-pstate during resume, return %d\n", ret);
  826. return ret;
  827. }
  828. static int amd_pstate_cpu_suspend(struct cpufreq_policy *policy)
  829. {
  830. int ret;
  831. ret = amd_pstate_enable(false);
  832. if (ret)
  833. pr_err("failed to disable amd-pstate during suspend, return %d\n", ret);
  834. return ret;
  835. }
  836. /* Sysfs attributes */
  837. /*
  838. * This frequency is to indicate the maximum hardware frequency.
  839. * If boost is not active but supported, the frequency will be larger than the
  840. * one in cpuinfo.
  841. */
  842. static ssize_t show_amd_pstate_max_freq(struct cpufreq_policy *policy,
  843. char *buf)
  844. {
  845. int max_freq;
  846. struct amd_cpudata *cpudata = policy->driver_data;
  847. max_freq = READ_ONCE(cpudata->max_freq);
  848. if (max_freq < 0)
  849. return max_freq;
  850. return sysfs_emit(buf, "%u\n", max_freq);
  851. }
  852. static ssize_t show_amd_pstate_lowest_nonlinear_freq(struct cpufreq_policy *policy,
  853. char *buf)
  854. {
  855. int freq;
  856. struct amd_cpudata *cpudata = policy->driver_data;
  857. freq = READ_ONCE(cpudata->lowest_nonlinear_freq);
  858. if (freq < 0)
  859. return freq;
  860. return sysfs_emit(buf, "%u\n", freq);
  861. }
  862. /*
  863. * In some of ASICs, the highest_perf is not the one in the _CPC table, so we
  864. * need to expose it to sysfs.
  865. */
  866. static ssize_t show_amd_pstate_highest_perf(struct cpufreq_policy *policy,
  867. char *buf)
  868. {
  869. u32 perf;
  870. struct amd_cpudata *cpudata = policy->driver_data;
  871. perf = READ_ONCE(cpudata->highest_perf);
  872. return sysfs_emit(buf, "%u\n", perf);
  873. }
  874. static ssize_t show_amd_pstate_prefcore_ranking(struct cpufreq_policy *policy,
  875. char *buf)
  876. {
  877. u32 perf;
  878. struct amd_cpudata *cpudata = policy->driver_data;
  879. perf = READ_ONCE(cpudata->prefcore_ranking);
  880. return sysfs_emit(buf, "%u\n", perf);
  881. }
  882. static ssize_t show_amd_pstate_hw_prefcore(struct cpufreq_policy *policy,
  883. char *buf)
  884. {
  885. bool hw_prefcore;
  886. struct amd_cpudata *cpudata = policy->driver_data;
  887. hw_prefcore = READ_ONCE(cpudata->hw_prefcore);
  888. return sysfs_emit(buf, "%s\n", str_enabled_disabled(hw_prefcore));
  889. }
  890. static ssize_t show_energy_performance_available_preferences(
  891. struct cpufreq_policy *policy, char *buf)
  892. {
  893. int i = 0;
  894. int offset = 0;
  895. struct amd_cpudata *cpudata = policy->driver_data;
  896. if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
  897. return sysfs_emit_at(buf, offset, "%s\n",
  898. energy_perf_strings[EPP_INDEX_PERFORMANCE]);
  899. while (energy_perf_strings[i] != NULL)
  900. offset += sysfs_emit_at(buf, offset, "%s ", energy_perf_strings[i++]);
  901. offset += sysfs_emit_at(buf, offset, "\n");
  902. return offset;
  903. }
  904. static ssize_t store_energy_performance_preference(
  905. struct cpufreq_policy *policy, const char *buf, size_t count)
  906. {
  907. struct amd_cpudata *cpudata = policy->driver_data;
  908. char str_preference[21];
  909. ssize_t ret;
  910. ret = sscanf(buf, "%20s", str_preference);
  911. if (ret != 1)
  912. return -EINVAL;
  913. ret = match_string(energy_perf_strings, -1, str_preference);
  914. if (ret < 0)
  915. return -EINVAL;
  916. guard(mutex)(&amd_pstate_limits_lock);
  917. ret = amd_pstate_set_energy_pref_index(cpudata, ret);
  918. return ret ? ret : count;
  919. }
  920. static ssize_t show_energy_performance_preference(
  921. struct cpufreq_policy *policy, char *buf)
  922. {
  923. struct amd_cpudata *cpudata = policy->driver_data;
  924. int preference;
  925. preference = amd_pstate_get_energy_pref_index(cpudata);
  926. if (preference < 0)
  927. return preference;
  928. return sysfs_emit(buf, "%s\n", energy_perf_strings[preference]);
  929. }
  930. static void amd_pstate_driver_cleanup(void)
  931. {
  932. amd_pstate_enable(false);
  933. cppc_state = AMD_PSTATE_DISABLE;
  934. current_pstate_driver = NULL;
  935. }
  936. static int amd_pstate_register_driver(int mode)
  937. {
  938. int ret;
  939. if (mode == AMD_PSTATE_PASSIVE || mode == AMD_PSTATE_GUIDED)
  940. current_pstate_driver = &amd_pstate_driver;
  941. else if (mode == AMD_PSTATE_ACTIVE)
  942. current_pstate_driver = &amd_pstate_epp_driver;
  943. else
  944. return -EINVAL;
  945. cppc_state = mode;
  946. ret = amd_pstate_enable(true);
  947. if (ret) {
  948. pr_err("failed to enable cppc during amd-pstate driver registration, return %d\n",
  949. ret);
  950. amd_pstate_driver_cleanup();
  951. return ret;
  952. }
  953. ret = cpufreq_register_driver(current_pstate_driver);
  954. if (ret) {
  955. amd_pstate_driver_cleanup();
  956. return ret;
  957. }
  958. return 0;
  959. }
  960. static int amd_pstate_unregister_driver(int dummy)
  961. {
  962. cpufreq_unregister_driver(current_pstate_driver);
  963. amd_pstate_driver_cleanup();
  964. return 0;
  965. }
  966. static int amd_pstate_change_mode_without_dvr_change(int mode)
  967. {
  968. int cpu = 0;
  969. cppc_state = mode;
  970. if (cpu_feature_enabled(X86_FEATURE_CPPC) || cppc_state == AMD_PSTATE_ACTIVE)
  971. return 0;
  972. for_each_present_cpu(cpu) {
  973. cppc_set_auto_sel(cpu, (cppc_state == AMD_PSTATE_PASSIVE) ? 0 : 1);
  974. }
  975. return 0;
  976. }
  977. static int amd_pstate_change_driver_mode(int mode)
  978. {
  979. int ret;
  980. ret = amd_pstate_unregister_driver(0);
  981. if (ret)
  982. return ret;
  983. ret = amd_pstate_register_driver(mode);
  984. if (ret)
  985. return ret;
  986. return 0;
  987. }
  988. static cppc_mode_transition_fn mode_state_machine[AMD_PSTATE_MAX][AMD_PSTATE_MAX] = {
  989. [AMD_PSTATE_DISABLE] = {
  990. [AMD_PSTATE_DISABLE] = NULL,
  991. [AMD_PSTATE_PASSIVE] = amd_pstate_register_driver,
  992. [AMD_PSTATE_ACTIVE] = amd_pstate_register_driver,
  993. [AMD_PSTATE_GUIDED] = amd_pstate_register_driver,
  994. },
  995. [AMD_PSTATE_PASSIVE] = {
  996. [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
  997. [AMD_PSTATE_PASSIVE] = NULL,
  998. [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
  999. [AMD_PSTATE_GUIDED] = amd_pstate_change_mode_without_dvr_change,
  1000. },
  1001. [AMD_PSTATE_ACTIVE] = {
  1002. [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
  1003. [AMD_PSTATE_PASSIVE] = amd_pstate_change_driver_mode,
  1004. [AMD_PSTATE_ACTIVE] = NULL,
  1005. [AMD_PSTATE_GUIDED] = amd_pstate_change_driver_mode,
  1006. },
  1007. [AMD_PSTATE_GUIDED] = {
  1008. [AMD_PSTATE_DISABLE] = amd_pstate_unregister_driver,
  1009. [AMD_PSTATE_PASSIVE] = amd_pstate_change_mode_without_dvr_change,
  1010. [AMD_PSTATE_ACTIVE] = amd_pstate_change_driver_mode,
  1011. [AMD_PSTATE_GUIDED] = NULL,
  1012. },
  1013. };
  1014. static ssize_t amd_pstate_show_status(char *buf)
  1015. {
  1016. if (!current_pstate_driver)
  1017. return sysfs_emit(buf, "disable\n");
  1018. return sysfs_emit(buf, "%s\n", amd_pstate_mode_string[cppc_state]);
  1019. }
  1020. int amd_pstate_update_status(const char *buf, size_t size)
  1021. {
  1022. int mode_idx;
  1023. if (size > strlen("passive") || size < strlen("active"))
  1024. return -EINVAL;
  1025. mode_idx = get_mode_idx_from_str(buf, size);
  1026. if (mode_idx < 0 || mode_idx >= AMD_PSTATE_MAX)
  1027. return -EINVAL;
  1028. if (mode_state_machine[cppc_state][mode_idx])
  1029. return mode_state_machine[cppc_state][mode_idx](mode_idx);
  1030. return 0;
  1031. }
  1032. EXPORT_SYMBOL_GPL(amd_pstate_update_status);
  1033. static ssize_t status_show(struct device *dev,
  1034. struct device_attribute *attr, char *buf)
  1035. {
  1036. guard(mutex)(&amd_pstate_driver_lock);
  1037. return amd_pstate_show_status(buf);
  1038. }
  1039. static ssize_t status_store(struct device *a, struct device_attribute *b,
  1040. const char *buf, size_t count)
  1041. {
  1042. char *p = memchr(buf, '\n', count);
  1043. int ret;
  1044. guard(mutex)(&amd_pstate_driver_lock);
  1045. ret = amd_pstate_update_status(buf, p ? p - buf : count);
  1046. return ret < 0 ? ret : count;
  1047. }
  1048. static ssize_t prefcore_show(struct device *dev,
  1049. struct device_attribute *attr, char *buf)
  1050. {
  1051. return sysfs_emit(buf, "%s\n", str_enabled_disabled(amd_pstate_prefcore));
  1052. }
  1053. cpufreq_freq_attr_ro(amd_pstate_max_freq);
  1054. cpufreq_freq_attr_ro(amd_pstate_lowest_nonlinear_freq);
  1055. cpufreq_freq_attr_ro(amd_pstate_highest_perf);
  1056. cpufreq_freq_attr_ro(amd_pstate_prefcore_ranking);
  1057. cpufreq_freq_attr_ro(amd_pstate_hw_prefcore);
  1058. cpufreq_freq_attr_rw(energy_performance_preference);
  1059. cpufreq_freq_attr_ro(energy_performance_available_preferences);
  1060. static DEVICE_ATTR_RW(status);
  1061. static DEVICE_ATTR_RO(prefcore);
  1062. static struct freq_attr *amd_pstate_attr[] = {
  1063. &amd_pstate_max_freq,
  1064. &amd_pstate_lowest_nonlinear_freq,
  1065. &amd_pstate_highest_perf,
  1066. &amd_pstate_prefcore_ranking,
  1067. &amd_pstate_hw_prefcore,
  1068. NULL,
  1069. };
  1070. static struct freq_attr *amd_pstate_epp_attr[] = {
  1071. &amd_pstate_max_freq,
  1072. &amd_pstate_lowest_nonlinear_freq,
  1073. &amd_pstate_highest_perf,
  1074. &amd_pstate_prefcore_ranking,
  1075. &amd_pstate_hw_prefcore,
  1076. &energy_performance_preference,
  1077. &energy_performance_available_preferences,
  1078. NULL,
  1079. };
  1080. static struct attribute *pstate_global_attributes[] = {
  1081. &dev_attr_status.attr,
  1082. &dev_attr_prefcore.attr,
  1083. NULL
  1084. };
  1085. static const struct attribute_group amd_pstate_global_attr_group = {
  1086. .name = "amd_pstate",
  1087. .attrs = pstate_global_attributes,
  1088. };
  1089. static bool amd_pstate_acpi_pm_profile_server(void)
  1090. {
  1091. switch (acpi_gbl_FADT.preferred_profile) {
  1092. case PM_ENTERPRISE_SERVER:
  1093. case PM_SOHO_SERVER:
  1094. case PM_PERFORMANCE_SERVER:
  1095. return true;
  1096. }
  1097. return false;
  1098. }
  1099. static bool amd_pstate_acpi_pm_profile_undefined(void)
  1100. {
  1101. if (acpi_gbl_FADT.preferred_profile == PM_UNSPECIFIED)
  1102. return true;
  1103. if (acpi_gbl_FADT.preferred_profile >= NR_PM_PROFILES)
  1104. return true;
  1105. return false;
  1106. }
  1107. static int amd_pstate_epp_cpu_init(struct cpufreq_policy *policy)
  1108. {
  1109. int min_freq, max_freq, ret;
  1110. struct amd_cpudata *cpudata;
  1111. struct device *dev;
  1112. u64 value;
  1113. /*
  1114. * Resetting PERF_CTL_MSR will put the CPU in P0 frequency,
  1115. * which is ideal for initialization process.
  1116. */
  1117. amd_perf_ctl_reset(policy->cpu);
  1118. dev = get_cpu_device(policy->cpu);
  1119. if (!dev)
  1120. return -ENODEV;
  1121. cpudata = kzalloc(sizeof(*cpudata), GFP_KERNEL);
  1122. if (!cpudata)
  1123. return -ENOMEM;
  1124. cpudata->cpu = policy->cpu;
  1125. cpudata->epp_policy = 0;
  1126. ret = amd_pstate_init_perf(cpudata);
  1127. if (ret)
  1128. goto free_cpudata1;
  1129. amd_pstate_init_prefcore(cpudata);
  1130. ret = amd_pstate_init_freq(cpudata);
  1131. if (ret)
  1132. goto free_cpudata1;
  1133. ret = amd_pstate_init_boost_support(cpudata);
  1134. if (ret)
  1135. goto free_cpudata1;
  1136. min_freq = READ_ONCE(cpudata->min_freq);
  1137. max_freq = READ_ONCE(cpudata->max_freq);
  1138. policy->cpuinfo.min_freq = min_freq;
  1139. policy->cpuinfo.max_freq = max_freq;
  1140. /* It will be updated by governor */
  1141. policy->cur = policy->cpuinfo.min_freq;
  1142. policy->driver_data = cpudata;
  1143. cpudata->epp_cached = cpudata->epp_default = amd_pstate_get_epp(cpudata, 0);
  1144. policy->min = policy->cpuinfo.min_freq;
  1145. policy->max = policy->cpuinfo.max_freq;
  1146. policy->boost_enabled = READ_ONCE(cpudata->boost_supported);
  1147. /*
  1148. * Set the policy to provide a valid fallback value in case
  1149. * the default cpufreq governor is neither powersave nor performance.
  1150. */
  1151. if (amd_pstate_acpi_pm_profile_server() ||
  1152. amd_pstate_acpi_pm_profile_undefined())
  1153. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  1154. else
  1155. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  1156. if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
  1157. ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_REQ, &value);
  1158. if (ret)
  1159. return ret;
  1160. WRITE_ONCE(cpudata->cppc_req_cached, value);
  1161. ret = rdmsrl_on_cpu(cpudata->cpu, MSR_AMD_CPPC_CAP1, &value);
  1162. if (ret)
  1163. return ret;
  1164. WRITE_ONCE(cpudata->cppc_cap1_cached, value);
  1165. }
  1166. return 0;
  1167. free_cpudata1:
  1168. kfree(cpudata);
  1169. return ret;
  1170. }
  1171. static void amd_pstate_epp_cpu_exit(struct cpufreq_policy *policy)
  1172. {
  1173. struct amd_cpudata *cpudata = policy->driver_data;
  1174. if (cpudata) {
  1175. kfree(cpudata);
  1176. policy->driver_data = NULL;
  1177. }
  1178. pr_debug("CPU %d exiting\n", policy->cpu);
  1179. }
  1180. static int amd_pstate_epp_update_limit(struct cpufreq_policy *policy)
  1181. {
  1182. struct amd_cpudata *cpudata = policy->driver_data;
  1183. u32 max_perf, min_perf, min_limit_perf, max_limit_perf;
  1184. u64 value;
  1185. s16 epp;
  1186. if (cpudata->boost_supported && !policy->boost_enabled)
  1187. max_perf = READ_ONCE(cpudata->nominal_perf);
  1188. else
  1189. max_perf = READ_ONCE(cpudata->highest_perf);
  1190. min_perf = READ_ONCE(cpudata->lowest_perf);
  1191. max_limit_perf = div_u64(policy->max * max_perf, policy->cpuinfo.max_freq);
  1192. min_limit_perf = div_u64(policy->min * max_perf, policy->cpuinfo.max_freq);
  1193. if (min_limit_perf < min_perf)
  1194. min_limit_perf = min_perf;
  1195. if (max_limit_perf < min_limit_perf)
  1196. max_limit_perf = min_limit_perf;
  1197. WRITE_ONCE(cpudata->max_limit_perf, max_limit_perf);
  1198. WRITE_ONCE(cpudata->min_limit_perf, min_limit_perf);
  1199. max_perf = clamp_t(unsigned long, max_perf, cpudata->min_limit_perf,
  1200. cpudata->max_limit_perf);
  1201. min_perf = clamp_t(unsigned long, min_perf, cpudata->min_limit_perf,
  1202. cpudata->max_limit_perf);
  1203. value = READ_ONCE(cpudata->cppc_req_cached);
  1204. if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
  1205. min_perf = min(cpudata->nominal_perf, max_perf);
  1206. /* Initial min/max values for CPPC Performance Controls Register */
  1207. value &= ~AMD_CPPC_MIN_PERF(~0L);
  1208. value |= AMD_CPPC_MIN_PERF(min_perf);
  1209. value &= ~AMD_CPPC_MAX_PERF(~0L);
  1210. value |= AMD_CPPC_MAX_PERF(max_perf);
  1211. /* CPPC EPP feature require to set zero to the desire perf bit */
  1212. value &= ~AMD_CPPC_DES_PERF(~0L);
  1213. value |= AMD_CPPC_DES_PERF(0);
  1214. cpudata->epp_policy = cpudata->policy;
  1215. /* Get BIOS pre-defined epp value */
  1216. epp = amd_pstate_get_epp(cpudata, value);
  1217. if (epp < 0) {
  1218. /**
  1219. * This return value can only be negative for shared_memory
  1220. * systems where EPP register read/write not supported.
  1221. */
  1222. return epp;
  1223. }
  1224. if (cpudata->policy == CPUFREQ_POLICY_PERFORMANCE)
  1225. epp = 0;
  1226. /* Set initial EPP value */
  1227. if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
  1228. value &= ~GENMASK_ULL(31, 24);
  1229. value |= (u64)epp << 24;
  1230. }
  1231. WRITE_ONCE(cpudata->cppc_req_cached, value);
  1232. return amd_pstate_set_epp(cpudata, epp);
  1233. }
  1234. static int amd_pstate_epp_set_policy(struct cpufreq_policy *policy)
  1235. {
  1236. struct amd_cpudata *cpudata = policy->driver_data;
  1237. int ret;
  1238. if (!policy->cpuinfo.max_freq)
  1239. return -ENODEV;
  1240. pr_debug("set_policy: cpuinfo.max %u policy->max %u\n",
  1241. policy->cpuinfo.max_freq, policy->max);
  1242. cpudata->policy = policy->policy;
  1243. ret = amd_pstate_epp_update_limit(policy);
  1244. if (ret)
  1245. return ret;
  1246. /*
  1247. * policy->cur is never updated with the amd_pstate_epp driver, but it
  1248. * is used as a stale frequency value. So, keep it within limits.
  1249. */
  1250. policy->cur = policy->min;
  1251. return 0;
  1252. }
  1253. static void amd_pstate_epp_reenable(struct amd_cpudata *cpudata)
  1254. {
  1255. u64 max_perf;
  1256. int ret;
  1257. ret = amd_pstate_enable(true);
  1258. if (ret)
  1259. pr_err("failed to enable amd pstate during resume, return %d\n", ret);
  1260. max_perf = READ_ONCE(cpudata->highest_perf);
  1261. amd_pstate_update_perf(cpudata, 0, 0, max_perf, false);
  1262. amd_pstate_set_epp(cpudata, cpudata->epp_cached);
  1263. }
  1264. static int amd_pstate_epp_cpu_online(struct cpufreq_policy *policy)
  1265. {
  1266. struct amd_cpudata *cpudata = policy->driver_data;
  1267. pr_debug("AMD CPU Core %d going online\n", cpudata->cpu);
  1268. amd_pstate_epp_reenable(cpudata);
  1269. cpudata->suspended = false;
  1270. return 0;
  1271. }
  1272. static int amd_pstate_epp_cpu_offline(struct cpufreq_policy *policy)
  1273. {
  1274. struct amd_cpudata *cpudata = policy->driver_data;
  1275. int min_perf;
  1276. if (cpudata->suspended)
  1277. return 0;
  1278. min_perf = READ_ONCE(cpudata->lowest_perf);
  1279. guard(mutex)(&amd_pstate_limits_lock);
  1280. amd_pstate_update_perf(cpudata, min_perf, 0, min_perf, false);
  1281. amd_pstate_set_epp(cpudata, AMD_CPPC_EPP_BALANCE_POWERSAVE);
  1282. return 0;
  1283. }
  1284. static int amd_pstate_epp_verify_policy(struct cpufreq_policy_data *policy)
  1285. {
  1286. cpufreq_verify_within_cpu_limits(policy);
  1287. pr_debug("policy_max =%d, policy_min=%d\n", policy->max, policy->min);
  1288. return 0;
  1289. }
  1290. static int amd_pstate_epp_suspend(struct cpufreq_policy *policy)
  1291. {
  1292. struct amd_cpudata *cpudata = policy->driver_data;
  1293. int ret;
  1294. /* avoid suspending when EPP is not enabled */
  1295. if (cppc_state != AMD_PSTATE_ACTIVE)
  1296. return 0;
  1297. /* set this flag to avoid setting core offline*/
  1298. cpudata->suspended = true;
  1299. /* disable CPPC in lowlevel firmware */
  1300. ret = amd_pstate_enable(false);
  1301. if (ret)
  1302. pr_err("failed to suspend, return %d\n", ret);
  1303. return 0;
  1304. }
  1305. static int amd_pstate_epp_resume(struct cpufreq_policy *policy)
  1306. {
  1307. struct amd_cpudata *cpudata = policy->driver_data;
  1308. if (cpudata->suspended) {
  1309. guard(mutex)(&amd_pstate_limits_lock);
  1310. /* enable amd pstate from suspend state*/
  1311. amd_pstate_epp_reenable(cpudata);
  1312. cpudata->suspended = false;
  1313. }
  1314. return 0;
  1315. }
  1316. static struct cpufreq_driver amd_pstate_driver = {
  1317. .flags = CPUFREQ_CONST_LOOPS | CPUFREQ_NEED_UPDATE_LIMITS,
  1318. .verify = amd_pstate_verify,
  1319. .target = amd_pstate_target,
  1320. .fast_switch = amd_pstate_fast_switch,
  1321. .init = amd_pstate_cpu_init,
  1322. .exit = amd_pstate_cpu_exit,
  1323. .suspend = amd_pstate_cpu_suspend,
  1324. .resume = amd_pstate_cpu_resume,
  1325. .set_boost = amd_pstate_set_boost,
  1326. .update_limits = amd_pstate_update_limits,
  1327. .name = "amd-pstate",
  1328. .attr = amd_pstate_attr,
  1329. };
  1330. static struct cpufreq_driver amd_pstate_epp_driver = {
  1331. .flags = CPUFREQ_CONST_LOOPS,
  1332. .verify = amd_pstate_epp_verify_policy,
  1333. .setpolicy = amd_pstate_epp_set_policy,
  1334. .init = amd_pstate_epp_cpu_init,
  1335. .exit = amd_pstate_epp_cpu_exit,
  1336. .offline = amd_pstate_epp_cpu_offline,
  1337. .online = amd_pstate_epp_cpu_online,
  1338. .suspend = amd_pstate_epp_suspend,
  1339. .resume = amd_pstate_epp_resume,
  1340. .update_limits = amd_pstate_update_limits,
  1341. .set_boost = amd_pstate_set_boost,
  1342. .name = "amd-pstate-epp",
  1343. .attr = amd_pstate_epp_attr,
  1344. };
  1345. static int __init amd_pstate_set_driver(int mode_idx)
  1346. {
  1347. if (mode_idx >= AMD_PSTATE_DISABLE && mode_idx < AMD_PSTATE_MAX) {
  1348. cppc_state = mode_idx;
  1349. if (cppc_state == AMD_PSTATE_DISABLE)
  1350. pr_info("driver is explicitly disabled\n");
  1351. if (cppc_state == AMD_PSTATE_ACTIVE)
  1352. current_pstate_driver = &amd_pstate_epp_driver;
  1353. if (cppc_state == AMD_PSTATE_PASSIVE || cppc_state == AMD_PSTATE_GUIDED)
  1354. current_pstate_driver = &amd_pstate_driver;
  1355. return 0;
  1356. }
  1357. return -EINVAL;
  1358. }
  1359. /**
  1360. * CPPC function is not supported for family ID 17H with model_ID ranging from 0x10 to 0x2F.
  1361. * show the debug message that helps to check if the CPU has CPPC support for loading issue.
  1362. */
  1363. static bool amd_cppc_supported(void)
  1364. {
  1365. struct cpuinfo_x86 *c = &cpu_data(0);
  1366. bool warn = false;
  1367. if ((boot_cpu_data.x86 == 0x17) && (boot_cpu_data.x86_model < 0x30)) {
  1368. pr_debug_once("CPPC feature is not supported by the processor\n");
  1369. return false;
  1370. }
  1371. /*
  1372. * If the CPPC feature is disabled in the BIOS for processors
  1373. * that support MSR-based CPPC, the AMD Pstate driver may not
  1374. * function correctly.
  1375. *
  1376. * For such processors, check the CPPC flag and display a
  1377. * warning message if the platform supports CPPC.
  1378. *
  1379. * Note: The code check below will not abort the driver
  1380. * registration process because of the code is added for
  1381. * debugging purposes. Besides, it may still be possible for
  1382. * the driver to work using the shared-memory mechanism.
  1383. */
  1384. if (!cpu_feature_enabled(X86_FEATURE_CPPC)) {
  1385. if (cpu_feature_enabled(X86_FEATURE_ZEN2)) {
  1386. switch (c->x86_model) {
  1387. case 0x60 ... 0x6F:
  1388. case 0x80 ... 0xAF:
  1389. warn = true;
  1390. break;
  1391. }
  1392. } else if (cpu_feature_enabled(X86_FEATURE_ZEN3) ||
  1393. cpu_feature_enabled(X86_FEATURE_ZEN4)) {
  1394. switch (c->x86_model) {
  1395. case 0x10 ... 0x1F:
  1396. case 0x40 ... 0xAF:
  1397. warn = true;
  1398. break;
  1399. }
  1400. } else if (cpu_feature_enabled(X86_FEATURE_ZEN5)) {
  1401. warn = true;
  1402. }
  1403. }
  1404. if (warn)
  1405. pr_warn_once("The CPPC feature is supported but currently disabled by the BIOS.\n"
  1406. "Please enable it if your BIOS has the CPPC option.\n");
  1407. return true;
  1408. }
  1409. static int __init amd_pstate_init(void)
  1410. {
  1411. struct device *dev_root;
  1412. int ret;
  1413. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  1414. return -ENODEV;
  1415. /* show debug message only if CPPC is not supported */
  1416. if (!amd_cppc_supported())
  1417. return -EOPNOTSUPP;
  1418. /* show warning message when BIOS broken or ACPI disabled */
  1419. if (!acpi_cpc_valid()) {
  1420. pr_warn_once("the _CPC object is not present in SBIOS or ACPI disabled\n");
  1421. return -ENODEV;
  1422. }
  1423. /* don't keep reloading if cpufreq_driver exists */
  1424. if (cpufreq_get_current_driver())
  1425. return -EEXIST;
  1426. quirks = NULL;
  1427. /* check if this machine need CPPC quirks */
  1428. dmi_check_system(amd_pstate_quirks_table);
  1429. /*
  1430. * determine the driver mode from the command line or kernel config.
  1431. * If no command line input is provided, cppc_state will be AMD_PSTATE_UNDEFINED.
  1432. * command line options will override the kernel config settings.
  1433. */
  1434. if (cppc_state == AMD_PSTATE_UNDEFINED) {
  1435. /* Disable on the following configs by default:
  1436. * 1. Undefined platforms
  1437. * 2. Server platforms
  1438. */
  1439. if (amd_pstate_acpi_pm_profile_undefined() ||
  1440. amd_pstate_acpi_pm_profile_server()) {
  1441. pr_info("driver load is disabled, boot with specific mode to enable this\n");
  1442. return -ENODEV;
  1443. }
  1444. /* get driver mode from kernel config option [1:4] */
  1445. cppc_state = CONFIG_X86_AMD_PSTATE_DEFAULT_MODE;
  1446. }
  1447. switch (cppc_state) {
  1448. case AMD_PSTATE_DISABLE:
  1449. pr_info("driver load is disabled, boot with specific mode to enable this\n");
  1450. return -ENODEV;
  1451. case AMD_PSTATE_PASSIVE:
  1452. case AMD_PSTATE_ACTIVE:
  1453. case AMD_PSTATE_GUIDED:
  1454. ret = amd_pstate_set_driver(cppc_state);
  1455. if (ret)
  1456. return ret;
  1457. break;
  1458. default:
  1459. return -EINVAL;
  1460. }
  1461. /* capability check */
  1462. if (cpu_feature_enabled(X86_FEATURE_CPPC)) {
  1463. pr_debug("AMD CPPC MSR based functionality is supported\n");
  1464. if (cppc_state != AMD_PSTATE_ACTIVE)
  1465. current_pstate_driver->adjust_perf = amd_pstate_adjust_perf;
  1466. } else {
  1467. pr_debug("AMD CPPC shared memory based functionality is supported\n");
  1468. static_call_update(amd_pstate_enable, cppc_enable);
  1469. static_call_update(amd_pstate_init_perf, cppc_init_perf);
  1470. static_call_update(amd_pstate_update_perf, cppc_update_perf);
  1471. }
  1472. if (amd_pstate_prefcore) {
  1473. ret = amd_detect_prefcore(&amd_pstate_prefcore);
  1474. if (ret)
  1475. return ret;
  1476. }
  1477. /* enable amd pstate feature */
  1478. ret = amd_pstate_enable(true);
  1479. if (ret) {
  1480. pr_err("failed to enable driver mode(%d)\n", cppc_state);
  1481. return ret;
  1482. }
  1483. ret = cpufreq_register_driver(current_pstate_driver);
  1484. if (ret) {
  1485. pr_err("failed to register with return %d\n", ret);
  1486. goto disable_driver;
  1487. }
  1488. dev_root = bus_get_dev_root(&cpu_subsys);
  1489. if (dev_root) {
  1490. ret = sysfs_create_group(&dev_root->kobj, &amd_pstate_global_attr_group);
  1491. put_device(dev_root);
  1492. if (ret) {
  1493. pr_err("sysfs attribute export failed with error %d.\n", ret);
  1494. goto global_attr_free;
  1495. }
  1496. }
  1497. return ret;
  1498. global_attr_free:
  1499. cpufreq_unregister_driver(current_pstate_driver);
  1500. disable_driver:
  1501. amd_pstate_enable(false);
  1502. return ret;
  1503. }
  1504. device_initcall(amd_pstate_init);
  1505. static int __init amd_pstate_param(char *str)
  1506. {
  1507. size_t size;
  1508. int mode_idx;
  1509. if (!str)
  1510. return -EINVAL;
  1511. size = strlen(str);
  1512. mode_idx = get_mode_idx_from_str(str, size);
  1513. return amd_pstate_set_driver(mode_idx);
  1514. }
  1515. static int __init amd_prefcore_param(char *str)
  1516. {
  1517. if (!strcmp(str, "disable"))
  1518. amd_pstate_prefcore = false;
  1519. return 0;
  1520. }
  1521. early_param("amd_pstate", amd_pstate_param);
  1522. early_param("amd_prefcore", amd_prefcore_param);
  1523. MODULE_AUTHOR("Huang Rui <ray.huang@amd.com>");
  1524. MODULE_DESCRIPTION("AMD Processor P-state Frequency Driver");