cpufreq.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/cpufreq/cpufreq.c
  4. *
  5. * Copyright (C) 2001 Russell King
  6. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  7. * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
  8. *
  9. * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
  10. * Added handling for CPU hotplug
  11. * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
  12. * Fix handling for CPU hotplug -- affected CPUs
  13. */
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/cpu.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/cpu_cooling.h>
  18. #include <linux/delay.h>
  19. #include <linux/device.h>
  20. #include <linux/init.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/module.h>
  23. #include <linux/mutex.h>
  24. #include <linux/pm_qos.h>
  25. #include <linux/slab.h>
  26. #include <linux/string_choices.h>
  27. #include <linux/suspend.h>
  28. #include <linux/syscore_ops.h>
  29. #include <linux/tick.h>
  30. #include <linux/units.h>
  31. #include <trace/events/power.h>
  32. static LIST_HEAD(cpufreq_policy_list);
  33. /* Macros to iterate over CPU policies */
  34. #define for_each_suitable_policy(__policy, __active) \
  35. list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
  36. if ((__active) == !policy_is_inactive(__policy))
  37. #define for_each_active_policy(__policy) \
  38. for_each_suitable_policy(__policy, true)
  39. #define for_each_inactive_policy(__policy) \
  40. for_each_suitable_policy(__policy, false)
  41. /* Iterate over governors */
  42. static LIST_HEAD(cpufreq_governor_list);
  43. #define for_each_governor(__governor) \
  44. list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
  45. static char default_governor[CPUFREQ_NAME_LEN];
  46. /*
  47. * The "cpufreq driver" - the arch- or hardware-dependent low
  48. * level driver of CPUFreq support, and its spinlock. This lock
  49. * also protects the cpufreq_cpu_data array.
  50. */
  51. static struct cpufreq_driver *cpufreq_driver;
  52. static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
  53. static DEFINE_RWLOCK(cpufreq_driver_lock);
  54. static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance);
  55. bool cpufreq_supports_freq_invariance(void)
  56. {
  57. return static_branch_likely(&cpufreq_freq_invariance);
  58. }
  59. /* Flag to suspend/resume CPUFreq governors */
  60. static bool cpufreq_suspended;
  61. static inline bool has_target(void)
  62. {
  63. return cpufreq_driver->target_index || cpufreq_driver->target;
  64. }
  65. bool has_target_index(void)
  66. {
  67. return !!cpufreq_driver->target_index;
  68. }
  69. /* internal prototypes */
  70. static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
  71. static int cpufreq_init_governor(struct cpufreq_policy *policy);
  72. static void cpufreq_exit_governor(struct cpufreq_policy *policy);
  73. static void cpufreq_governor_limits(struct cpufreq_policy *policy);
  74. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  75. struct cpufreq_governor *new_gov,
  76. unsigned int new_pol);
  77. static bool cpufreq_boost_supported(void);
  78. static int cpufreq_boost_trigger_state(int state);
  79. /*
  80. * Two notifier lists: the "policy" list is involved in the
  81. * validation process for a new CPU frequency policy; the
  82. * "transition" list for kernel code that needs to handle
  83. * changes to devices when the CPU clock speed changes.
  84. * The mutex locks both lists.
  85. */
  86. static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
  87. SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
  88. static int off __read_mostly;
  89. static int cpufreq_disabled(void)
  90. {
  91. return off;
  92. }
  93. void disable_cpufreq(void)
  94. {
  95. off = 1;
  96. }
  97. EXPORT_SYMBOL_GPL(disable_cpufreq);
  98. static DEFINE_MUTEX(cpufreq_governor_mutex);
  99. bool have_governor_per_policy(void)
  100. {
  101. return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
  102. }
  103. EXPORT_SYMBOL_GPL(have_governor_per_policy);
  104. static struct kobject *cpufreq_global_kobject;
  105. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
  106. {
  107. if (have_governor_per_policy())
  108. return &policy->kobj;
  109. else
  110. return cpufreq_global_kobject;
  111. }
  112. EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
  113. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  114. {
  115. struct kernel_cpustat kcpustat;
  116. u64 cur_wall_time;
  117. u64 idle_time;
  118. u64 busy_time;
  119. cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
  120. kcpustat_cpu_fetch(&kcpustat, cpu);
  121. busy_time = kcpustat.cpustat[CPUTIME_USER];
  122. busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
  123. busy_time += kcpustat.cpustat[CPUTIME_IRQ];
  124. busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
  125. busy_time += kcpustat.cpustat[CPUTIME_STEAL];
  126. busy_time += kcpustat.cpustat[CPUTIME_NICE];
  127. idle_time = cur_wall_time - busy_time;
  128. if (wall)
  129. *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
  130. return div_u64(idle_time, NSEC_PER_USEC);
  131. }
  132. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
  133. {
  134. u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
  135. if (idle_time == -1ULL)
  136. return get_cpu_idle_time_jiffy(cpu, wall);
  137. else if (!io_busy)
  138. idle_time += get_cpu_iowait_time_us(cpu, wall);
  139. return idle_time;
  140. }
  141. EXPORT_SYMBOL_GPL(get_cpu_idle_time);
  142. /*
  143. * This is a generic cpufreq init() routine which can be used by cpufreq
  144. * drivers of SMP systems. It will do following:
  145. * - validate & show freq table passed
  146. * - set policies transition latency
  147. * - policy->cpus with all possible CPUs
  148. */
  149. void cpufreq_generic_init(struct cpufreq_policy *policy,
  150. struct cpufreq_frequency_table *table,
  151. unsigned int transition_latency)
  152. {
  153. policy->freq_table = table;
  154. policy->cpuinfo.transition_latency = transition_latency;
  155. /*
  156. * The driver only supports the SMP configuration where all processors
  157. * share the clock and voltage and clock.
  158. */
  159. cpumask_setall(policy->cpus);
  160. }
  161. EXPORT_SYMBOL_GPL(cpufreq_generic_init);
  162. struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
  163. {
  164. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  165. return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
  166. }
  167. EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
  168. struct cpufreq_policy *cpufreq_cpu_policy(unsigned int cpu)
  169. {
  170. return per_cpu(cpufreq_cpu_data, cpu);
  171. }
  172. EXPORT_SYMBOL_GPL(cpufreq_cpu_policy);
  173. unsigned int cpufreq_generic_get(unsigned int cpu)
  174. {
  175. struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
  176. if (!policy || IS_ERR(policy->clk)) {
  177. pr_err("%s: No %s associated to cpu: %d\n",
  178. __func__, policy ? "clk" : "policy", cpu);
  179. return 0;
  180. }
  181. return clk_get_rate(policy->clk) / 1000;
  182. }
  183. EXPORT_SYMBOL_GPL(cpufreq_generic_get);
  184. /**
  185. * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
  186. * @cpu: CPU to find the policy for.
  187. *
  188. * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
  189. * the kobject reference counter of that policy. Return a valid policy on
  190. * success or NULL on failure.
  191. *
  192. * The policy returned by this function has to be released with the help of
  193. * cpufreq_cpu_put() to balance its kobject reference counter properly.
  194. */
  195. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  196. {
  197. struct cpufreq_policy *policy = NULL;
  198. unsigned long flags;
  199. if (WARN_ON(cpu >= nr_cpu_ids))
  200. return NULL;
  201. /* get the cpufreq driver */
  202. read_lock_irqsave(&cpufreq_driver_lock, flags);
  203. if (cpufreq_driver) {
  204. /* get the CPU */
  205. policy = cpufreq_cpu_get_raw(cpu);
  206. if (policy)
  207. kobject_get(&policy->kobj);
  208. }
  209. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  210. return policy;
  211. }
  212. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  213. /**
  214. * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
  215. * @policy: cpufreq policy returned by cpufreq_cpu_get().
  216. */
  217. void cpufreq_cpu_put(struct cpufreq_policy *policy)
  218. {
  219. kobject_put(&policy->kobj);
  220. }
  221. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  222. /*********************************************************************
  223. * EXTERNALLY AFFECTING FREQUENCY CHANGES *
  224. *********************************************************************/
  225. /**
  226. * adjust_jiffies - Adjust the system "loops_per_jiffy".
  227. * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
  228. * @ci: Frequency change information.
  229. *
  230. * This function alters the system "loops_per_jiffy" for the clock
  231. * speed change. Note that loops_per_jiffy cannot be updated on SMP
  232. * systems as each CPU might be scaled differently. So, use the arch
  233. * per-CPU loops_per_jiffy value wherever possible.
  234. */
  235. static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  236. {
  237. #ifndef CONFIG_SMP
  238. static unsigned long l_p_j_ref;
  239. static unsigned int l_p_j_ref_freq;
  240. if (ci->flags & CPUFREQ_CONST_LOOPS)
  241. return;
  242. if (!l_p_j_ref_freq) {
  243. l_p_j_ref = loops_per_jiffy;
  244. l_p_j_ref_freq = ci->old;
  245. pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
  246. l_p_j_ref, l_p_j_ref_freq);
  247. }
  248. if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
  249. loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
  250. ci->new);
  251. pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
  252. loops_per_jiffy, ci->new);
  253. }
  254. #endif
  255. }
  256. /**
  257. * cpufreq_notify_transition - Notify frequency transition and adjust jiffies.
  258. * @policy: cpufreq policy to enable fast frequency switching for.
  259. * @freqs: contain details of the frequency update.
  260. * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
  261. *
  262. * This function calls the transition notifiers and adjust_jiffies().
  263. *
  264. * It is called twice on all CPU frequency changes that have external effects.
  265. */
  266. static void cpufreq_notify_transition(struct cpufreq_policy *policy,
  267. struct cpufreq_freqs *freqs,
  268. unsigned int state)
  269. {
  270. int cpu;
  271. BUG_ON(irqs_disabled());
  272. if (cpufreq_disabled())
  273. return;
  274. freqs->policy = policy;
  275. freqs->flags = cpufreq_driver->flags;
  276. pr_debug("notification %u of frequency transition to %u kHz\n",
  277. state, freqs->new);
  278. switch (state) {
  279. case CPUFREQ_PRECHANGE:
  280. /*
  281. * Detect if the driver reported a value as "old frequency"
  282. * which is not equal to what the cpufreq core thinks is
  283. * "old frequency".
  284. */
  285. if (policy->cur && policy->cur != freqs->old) {
  286. pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
  287. freqs->old, policy->cur);
  288. freqs->old = policy->cur;
  289. }
  290. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  291. CPUFREQ_PRECHANGE, freqs);
  292. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  293. break;
  294. case CPUFREQ_POSTCHANGE:
  295. adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
  296. pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
  297. cpumask_pr_args(policy->cpus));
  298. for_each_cpu(cpu, policy->cpus)
  299. trace_cpu_frequency(freqs->new, cpu);
  300. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  301. CPUFREQ_POSTCHANGE, freqs);
  302. cpufreq_stats_record_transition(policy, freqs->new);
  303. policy->cur = freqs->new;
  304. }
  305. }
  306. /* Do post notifications when there are chances that transition has failed */
  307. static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
  308. struct cpufreq_freqs *freqs, int transition_failed)
  309. {
  310. cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
  311. if (!transition_failed)
  312. return;
  313. swap(freqs->old, freqs->new);
  314. cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
  315. cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
  316. }
  317. void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
  318. struct cpufreq_freqs *freqs)
  319. {
  320. /*
  321. * Catch double invocations of _begin() which lead to self-deadlock.
  322. * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
  323. * doesn't invoke _begin() on their behalf, and hence the chances of
  324. * double invocations are very low. Moreover, there are scenarios
  325. * where these checks can emit false-positive warnings in these
  326. * drivers; so we avoid that by skipping them altogether.
  327. */
  328. WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
  329. && current == policy->transition_task);
  330. wait:
  331. wait_event(policy->transition_wait, !policy->transition_ongoing);
  332. spin_lock(&policy->transition_lock);
  333. if (unlikely(policy->transition_ongoing)) {
  334. spin_unlock(&policy->transition_lock);
  335. goto wait;
  336. }
  337. policy->transition_ongoing = true;
  338. policy->transition_task = current;
  339. spin_unlock(&policy->transition_lock);
  340. cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
  341. }
  342. EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
  343. void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
  344. struct cpufreq_freqs *freqs, int transition_failed)
  345. {
  346. if (WARN_ON(!policy->transition_ongoing))
  347. return;
  348. cpufreq_notify_post_transition(policy, freqs, transition_failed);
  349. arch_set_freq_scale(policy->related_cpus,
  350. policy->cur,
  351. arch_scale_freq_ref(policy->cpu));
  352. spin_lock(&policy->transition_lock);
  353. policy->transition_ongoing = false;
  354. policy->transition_task = NULL;
  355. spin_unlock(&policy->transition_lock);
  356. wake_up(&policy->transition_wait);
  357. }
  358. EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
  359. /*
  360. * Fast frequency switching status count. Positive means "enabled", negative
  361. * means "disabled" and 0 means "not decided yet".
  362. */
  363. static int cpufreq_fast_switch_count;
  364. static DEFINE_MUTEX(cpufreq_fast_switch_lock);
  365. static void cpufreq_list_transition_notifiers(void)
  366. {
  367. struct notifier_block *nb;
  368. pr_info("Registered transition notifiers:\n");
  369. mutex_lock(&cpufreq_transition_notifier_list.mutex);
  370. for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
  371. pr_info("%pS\n", nb->notifier_call);
  372. mutex_unlock(&cpufreq_transition_notifier_list.mutex);
  373. }
  374. /**
  375. * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
  376. * @policy: cpufreq policy to enable fast frequency switching for.
  377. *
  378. * Try to enable fast frequency switching for @policy.
  379. *
  380. * The attempt will fail if there is at least one transition notifier registered
  381. * at this point, as fast frequency switching is quite fundamentally at odds
  382. * with transition notifiers. Thus if successful, it will make registration of
  383. * transition notifiers fail going forward.
  384. */
  385. void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
  386. {
  387. lockdep_assert_held(&policy->rwsem);
  388. if (!policy->fast_switch_possible)
  389. return;
  390. mutex_lock(&cpufreq_fast_switch_lock);
  391. if (cpufreq_fast_switch_count >= 0) {
  392. cpufreq_fast_switch_count++;
  393. policy->fast_switch_enabled = true;
  394. } else {
  395. pr_warn("CPU%u: Fast frequency switching not enabled\n",
  396. policy->cpu);
  397. cpufreq_list_transition_notifiers();
  398. }
  399. mutex_unlock(&cpufreq_fast_switch_lock);
  400. }
  401. EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
  402. /**
  403. * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
  404. * @policy: cpufreq policy to disable fast frequency switching for.
  405. */
  406. void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
  407. {
  408. mutex_lock(&cpufreq_fast_switch_lock);
  409. if (policy->fast_switch_enabled) {
  410. policy->fast_switch_enabled = false;
  411. if (!WARN_ON(cpufreq_fast_switch_count <= 0))
  412. cpufreq_fast_switch_count--;
  413. }
  414. mutex_unlock(&cpufreq_fast_switch_lock);
  415. }
  416. EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
  417. static unsigned int __resolve_freq(struct cpufreq_policy *policy,
  418. unsigned int target_freq,
  419. unsigned int min, unsigned int max,
  420. unsigned int relation)
  421. {
  422. unsigned int idx;
  423. target_freq = clamp_val(target_freq, min, max);
  424. if (!policy->freq_table)
  425. return target_freq;
  426. idx = cpufreq_frequency_table_target(policy, target_freq, min, max, relation);
  427. policy->cached_resolved_idx = idx;
  428. policy->cached_target_freq = target_freq;
  429. return policy->freq_table[idx].frequency;
  430. }
  431. /**
  432. * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
  433. * one.
  434. * @policy: associated policy to interrogate
  435. * @target_freq: target frequency to resolve.
  436. *
  437. * The target to driver frequency mapping is cached in the policy.
  438. *
  439. * Return: Lowest driver-supported frequency greater than or equal to the
  440. * given target_freq, subject to policy (min/max) and driver limitations.
  441. */
  442. unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
  443. unsigned int target_freq)
  444. {
  445. unsigned int min = READ_ONCE(policy->min);
  446. unsigned int max = READ_ONCE(policy->max);
  447. /*
  448. * If this function runs in parallel with cpufreq_set_policy(), it may
  449. * read policy->min before the update and policy->max after the update
  450. * or the other way around, so there is no ordering guarantee.
  451. *
  452. * Resolve this by always honoring the max (in case it comes from
  453. * thermal throttling or similar).
  454. */
  455. if (unlikely(min > max))
  456. min = max;
  457. return __resolve_freq(policy, target_freq, min, max, CPUFREQ_RELATION_LE);
  458. }
  459. EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
  460. unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
  461. {
  462. unsigned int latency;
  463. if (policy->transition_delay_us)
  464. return policy->transition_delay_us;
  465. latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
  466. if (latency)
  467. /* Give a 50% breathing room between updates */
  468. return latency + (latency >> 1);
  469. return USEC_PER_MSEC;
  470. }
  471. EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
  472. /*********************************************************************
  473. * SYSFS INTERFACE *
  474. *********************************************************************/
  475. static ssize_t show_boost(struct kobject *kobj,
  476. struct kobj_attribute *attr, char *buf)
  477. {
  478. return sysfs_emit(buf, "%d\n", cpufreq_driver->boost_enabled);
  479. }
  480. static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
  481. const char *buf, size_t count)
  482. {
  483. bool enable;
  484. if (kstrtobool(buf, &enable))
  485. return -EINVAL;
  486. if (cpufreq_boost_trigger_state(enable)) {
  487. pr_err("%s: Cannot %s BOOST!\n",
  488. __func__, str_enable_disable(enable));
  489. return -EINVAL;
  490. }
  491. pr_debug("%s: cpufreq BOOST %s\n",
  492. __func__, str_enabled_disabled(enable));
  493. return count;
  494. }
  495. define_one_global_rw(boost);
  496. static ssize_t show_local_boost(struct cpufreq_policy *policy, char *buf)
  497. {
  498. return sysfs_emit(buf, "%d\n", policy->boost_enabled);
  499. }
  500. static int policy_set_boost(struct cpufreq_policy *policy, bool enable)
  501. {
  502. int ret;
  503. if (policy->boost_enabled == enable)
  504. return 0;
  505. policy->boost_enabled = enable;
  506. ret = cpufreq_driver->set_boost(policy, enable);
  507. if (ret)
  508. policy->boost_enabled = !policy->boost_enabled;
  509. return ret;
  510. }
  511. static ssize_t store_local_boost(struct cpufreq_policy *policy,
  512. const char *buf, size_t count)
  513. {
  514. int ret;
  515. bool enable;
  516. if (kstrtobool(buf, &enable))
  517. return -EINVAL;
  518. if (!cpufreq_driver->boost_enabled)
  519. return -EINVAL;
  520. if (!policy->boost_supported)
  521. return -EINVAL;
  522. ret = policy_set_boost(policy, enable);
  523. if (!ret)
  524. return count;
  525. return ret;
  526. }
  527. static struct freq_attr local_boost = __ATTR(boost, 0644, show_local_boost, store_local_boost);
  528. static struct cpufreq_governor *find_governor(const char *str_governor)
  529. {
  530. struct cpufreq_governor *t;
  531. for_each_governor(t)
  532. if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
  533. return t;
  534. return NULL;
  535. }
  536. static struct cpufreq_governor *get_governor(const char *str_governor)
  537. {
  538. struct cpufreq_governor *t;
  539. mutex_lock(&cpufreq_governor_mutex);
  540. t = find_governor(str_governor);
  541. if (!t)
  542. goto unlock;
  543. if (!try_module_get(t->owner))
  544. t = NULL;
  545. unlock:
  546. mutex_unlock(&cpufreq_governor_mutex);
  547. return t;
  548. }
  549. static unsigned int cpufreq_parse_policy(char *str_governor)
  550. {
  551. if (!strncasecmp(str_governor, "performance", strlen("performance")))
  552. return CPUFREQ_POLICY_PERFORMANCE;
  553. if (!strncasecmp(str_governor, "powersave", strlen("powersave")))
  554. return CPUFREQ_POLICY_POWERSAVE;
  555. return CPUFREQ_POLICY_UNKNOWN;
  556. }
  557. /**
  558. * cpufreq_parse_governor - parse a governor string only for has_target()
  559. * @str_governor: Governor name.
  560. */
  561. static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
  562. {
  563. struct cpufreq_governor *t;
  564. t = get_governor(str_governor);
  565. if (t)
  566. return t;
  567. if (request_module("cpufreq_%s", str_governor))
  568. return NULL;
  569. return get_governor(str_governor);
  570. }
  571. /*
  572. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  573. * print out cpufreq information
  574. *
  575. * Write out information from cpufreq_driver->policy[cpu]; object must be
  576. * "unsigned int".
  577. */
  578. #define show_one(file_name, object) \
  579. static ssize_t show_##file_name \
  580. (struct cpufreq_policy *policy, char *buf) \
  581. { \
  582. return sysfs_emit(buf, "%u\n", policy->object); \
  583. }
  584. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  585. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  586. show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
  587. show_one(scaling_min_freq, min);
  588. show_one(scaling_max_freq, max);
  589. __weak int arch_freq_get_on_cpu(int cpu)
  590. {
  591. return -EOPNOTSUPP;
  592. }
  593. static inline bool cpufreq_avg_freq_supported(struct cpufreq_policy *policy)
  594. {
  595. return arch_freq_get_on_cpu(policy->cpu) != -EOPNOTSUPP;
  596. }
  597. static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
  598. {
  599. ssize_t ret;
  600. int freq;
  601. freq = IS_ENABLED(CONFIG_CPUFREQ_ARCH_CUR_FREQ)
  602. ? arch_freq_get_on_cpu(policy->cpu)
  603. : 0;
  604. if (freq > 0)
  605. ret = sysfs_emit(buf, "%u\n", freq);
  606. else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
  607. ret = sysfs_emit(buf, "%u\n", cpufreq_driver->get(policy->cpu));
  608. else
  609. ret = sysfs_emit(buf, "%u\n", policy->cur);
  610. return ret;
  611. }
  612. /*
  613. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  614. */
  615. #define store_one(file_name, object) \
  616. static ssize_t store_##file_name \
  617. (struct cpufreq_policy *policy, const char *buf, size_t count) \
  618. { \
  619. unsigned long val; \
  620. int ret; \
  621. \
  622. ret = kstrtoul(buf, 0, &val); \
  623. if (ret) \
  624. return ret; \
  625. \
  626. ret = freq_qos_update_request(policy->object##_freq_req, val);\
  627. return ret >= 0 ? count : ret; \
  628. }
  629. store_one(scaling_min_freq, min);
  630. store_one(scaling_max_freq, max);
  631. /*
  632. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  633. */
  634. static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
  635. char *buf)
  636. {
  637. unsigned int cur_freq = __cpufreq_get(policy);
  638. if (cur_freq)
  639. return sysfs_emit(buf, "%u\n", cur_freq);
  640. return sysfs_emit(buf, "<unknown>\n");
  641. }
  642. /*
  643. * show_cpuinfo_avg_freq - average CPU frequency as detected by hardware
  644. */
  645. static ssize_t show_cpuinfo_avg_freq(struct cpufreq_policy *policy,
  646. char *buf)
  647. {
  648. int avg_freq = arch_freq_get_on_cpu(policy->cpu);
  649. if (avg_freq > 0)
  650. return sysfs_emit(buf, "%u\n", avg_freq);
  651. return avg_freq != 0 ? avg_freq : -EINVAL;
  652. }
  653. /*
  654. * show_scaling_governor - show the current policy for the specified CPU
  655. */
  656. static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
  657. {
  658. if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
  659. return sysfs_emit(buf, "powersave\n");
  660. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  661. return sysfs_emit(buf, "performance\n");
  662. else if (policy->governor)
  663. return sysfs_emit(buf, "%s\n", policy->governor->name);
  664. return -EINVAL;
  665. }
  666. /*
  667. * store_scaling_governor - store policy for the specified CPU
  668. */
  669. static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
  670. const char *buf, size_t count)
  671. {
  672. char str_governor[CPUFREQ_NAME_LEN];
  673. int ret;
  674. ret = sscanf(buf, "%15s", str_governor);
  675. if (ret != 1)
  676. return -EINVAL;
  677. if (cpufreq_driver->setpolicy) {
  678. unsigned int new_pol;
  679. new_pol = cpufreq_parse_policy(str_governor);
  680. if (!new_pol)
  681. return -EINVAL;
  682. ret = cpufreq_set_policy(policy, NULL, new_pol);
  683. } else {
  684. struct cpufreq_governor *new_gov;
  685. new_gov = cpufreq_parse_governor(str_governor);
  686. if (!new_gov)
  687. return -EINVAL;
  688. ret = cpufreq_set_policy(policy, new_gov,
  689. CPUFREQ_POLICY_UNKNOWN);
  690. module_put(new_gov->owner);
  691. }
  692. return ret ? ret : count;
  693. }
  694. /*
  695. * show_scaling_driver - show the cpufreq driver currently loaded
  696. */
  697. static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
  698. {
  699. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
  700. }
  701. /*
  702. * show_scaling_available_governors - show the available CPUfreq governors
  703. */
  704. static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
  705. char *buf)
  706. {
  707. ssize_t i = 0;
  708. struct cpufreq_governor *t;
  709. if (!has_target()) {
  710. i += sysfs_emit(buf, "performance powersave");
  711. goto out;
  712. }
  713. mutex_lock(&cpufreq_governor_mutex);
  714. for_each_governor(t) {
  715. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
  716. - (CPUFREQ_NAME_LEN + 2)))
  717. break;
  718. i += sysfs_emit_at(buf, i, "%s ", t->name);
  719. }
  720. mutex_unlock(&cpufreq_governor_mutex);
  721. out:
  722. i += sysfs_emit_at(buf, i, "\n");
  723. return i;
  724. }
  725. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
  726. {
  727. ssize_t i = 0;
  728. unsigned int cpu;
  729. for_each_cpu(cpu, mask) {
  730. i += sysfs_emit_at(buf, i, "%u ", cpu);
  731. if (i >= (PAGE_SIZE - 5))
  732. break;
  733. }
  734. /* Remove the extra space at the end */
  735. i--;
  736. i += sysfs_emit_at(buf, i, "\n");
  737. return i;
  738. }
  739. EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
  740. /*
  741. * show_related_cpus - show the CPUs affected by each transition even if
  742. * hw coordination is in use
  743. */
  744. static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
  745. {
  746. return cpufreq_show_cpus(policy->related_cpus, buf);
  747. }
  748. /*
  749. * show_affected_cpus - show the CPUs affected by each transition
  750. */
  751. static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
  752. {
  753. return cpufreq_show_cpus(policy->cpus, buf);
  754. }
  755. static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
  756. const char *buf, size_t count)
  757. {
  758. unsigned int freq = 0;
  759. int ret;
  760. if (!policy->governor || !policy->governor->store_setspeed)
  761. return -EINVAL;
  762. ret = kstrtouint(buf, 0, &freq);
  763. if (ret)
  764. return ret;
  765. policy->governor->store_setspeed(policy, freq);
  766. return count;
  767. }
  768. static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
  769. {
  770. if (!policy->governor || !policy->governor->show_setspeed)
  771. return sysfs_emit(buf, "<unsupported>\n");
  772. return policy->governor->show_setspeed(policy, buf);
  773. }
  774. /*
  775. * show_bios_limit - show the current cpufreq HW/BIOS limitation
  776. */
  777. static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
  778. {
  779. unsigned int limit;
  780. int ret;
  781. ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
  782. if (!ret)
  783. return sysfs_emit(buf, "%u\n", limit);
  784. return sysfs_emit(buf, "%u\n", policy->cpuinfo.max_freq);
  785. }
  786. cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
  787. cpufreq_freq_attr_ro(cpuinfo_avg_freq);
  788. cpufreq_freq_attr_ro(cpuinfo_min_freq);
  789. cpufreq_freq_attr_ro(cpuinfo_max_freq);
  790. cpufreq_freq_attr_ro(cpuinfo_transition_latency);
  791. cpufreq_freq_attr_ro(scaling_available_governors);
  792. cpufreq_freq_attr_ro(scaling_driver);
  793. cpufreq_freq_attr_ro(scaling_cur_freq);
  794. cpufreq_freq_attr_ro(bios_limit);
  795. cpufreq_freq_attr_ro(related_cpus);
  796. cpufreq_freq_attr_ro(affected_cpus);
  797. cpufreq_freq_attr_rw(scaling_min_freq);
  798. cpufreq_freq_attr_rw(scaling_max_freq);
  799. cpufreq_freq_attr_rw(scaling_governor);
  800. cpufreq_freq_attr_rw(scaling_setspeed);
  801. static struct attribute *cpufreq_attrs[] = {
  802. &cpuinfo_min_freq.attr,
  803. &cpuinfo_max_freq.attr,
  804. &cpuinfo_transition_latency.attr,
  805. &scaling_cur_freq.attr,
  806. &scaling_min_freq.attr,
  807. &scaling_max_freq.attr,
  808. &affected_cpus.attr,
  809. &related_cpus.attr,
  810. &scaling_governor.attr,
  811. &scaling_driver.attr,
  812. &scaling_available_governors.attr,
  813. &scaling_setspeed.attr,
  814. NULL
  815. };
  816. ATTRIBUTE_GROUPS(cpufreq);
  817. #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
  818. #define to_attr(a) container_of(a, struct freq_attr, attr)
  819. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  820. {
  821. struct cpufreq_policy *policy = to_policy(kobj);
  822. struct freq_attr *fattr = to_attr(attr);
  823. if (!fattr->show)
  824. return -EIO;
  825. guard(cpufreq_policy_read)(policy);
  826. if (likely(!policy_is_inactive(policy)))
  827. return fattr->show(policy, buf);
  828. return -EBUSY;
  829. }
  830. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  831. const char *buf, size_t count)
  832. {
  833. struct cpufreq_policy *policy = to_policy(kobj);
  834. struct freq_attr *fattr = to_attr(attr);
  835. if (!fattr->store)
  836. return -EIO;
  837. guard(cpufreq_policy_write)(policy);
  838. if (likely(!policy_is_inactive(policy)))
  839. return fattr->store(policy, buf, count);
  840. return -EBUSY;
  841. }
  842. static void cpufreq_sysfs_release(struct kobject *kobj)
  843. {
  844. struct cpufreq_policy *policy = to_policy(kobj);
  845. pr_debug("last reference is dropped\n");
  846. complete(&policy->kobj_unregister);
  847. }
  848. static const struct sysfs_ops sysfs_ops = {
  849. .show = show,
  850. .store = store,
  851. };
  852. static const struct kobj_type ktype_cpufreq = {
  853. .sysfs_ops = &sysfs_ops,
  854. .default_groups = cpufreq_groups,
  855. .release = cpufreq_sysfs_release,
  856. };
  857. static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu,
  858. struct device *dev)
  859. {
  860. if (unlikely(!dev))
  861. return;
  862. if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
  863. return;
  864. dev_dbg(dev, "%s: Adding symlink\n", __func__);
  865. if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
  866. dev_err(dev, "cpufreq symlink creation failed\n");
  867. }
  868. static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu,
  869. struct device *dev)
  870. {
  871. dev_dbg(dev, "%s: Removing symlink\n", __func__);
  872. sysfs_remove_link(&dev->kobj, "cpufreq");
  873. cpumask_clear_cpu(cpu, policy->real_cpus);
  874. }
  875. static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
  876. {
  877. struct freq_attr **drv_attr;
  878. int ret = 0;
  879. /* Attributes that need freq_table */
  880. if (policy->freq_table) {
  881. ret = sysfs_create_file(&policy->kobj,
  882. &cpufreq_freq_attr_scaling_available_freqs.attr);
  883. if (ret)
  884. return ret;
  885. if (cpufreq_boost_supported()) {
  886. ret = sysfs_create_file(&policy->kobj,
  887. &cpufreq_freq_attr_scaling_boost_freqs.attr);
  888. if (ret)
  889. return ret;
  890. }
  891. }
  892. /* set up files for this cpu device */
  893. drv_attr = cpufreq_driver->attr;
  894. while (drv_attr && *drv_attr) {
  895. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  896. if (ret)
  897. return ret;
  898. drv_attr++;
  899. }
  900. if (cpufreq_driver->get) {
  901. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  902. if (ret)
  903. return ret;
  904. }
  905. if (cpufreq_avg_freq_supported(policy)) {
  906. ret = sysfs_create_file(&policy->kobj, &cpuinfo_avg_freq.attr);
  907. if (ret)
  908. return ret;
  909. }
  910. if (cpufreq_driver->bios_limit) {
  911. ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
  912. if (ret)
  913. return ret;
  914. }
  915. if (cpufreq_boost_supported()) {
  916. ret = sysfs_create_file(&policy->kobj, &local_boost.attr);
  917. if (ret)
  918. return ret;
  919. }
  920. return 0;
  921. }
  922. static int cpufreq_init_policy(struct cpufreq_policy *policy)
  923. {
  924. struct cpufreq_governor *gov = NULL;
  925. unsigned int pol = CPUFREQ_POLICY_UNKNOWN;
  926. int ret;
  927. if (has_target()) {
  928. /* Update policy governor to the one used before hotplug. */
  929. if (policy->last_governor[0] != '\0')
  930. gov = get_governor(policy->last_governor);
  931. if (gov) {
  932. pr_debug("Restoring governor %s for cpu %d\n",
  933. gov->name, policy->cpu);
  934. } else {
  935. gov = get_governor(default_governor);
  936. }
  937. if (!gov) {
  938. gov = cpufreq_default_governor();
  939. __module_get(gov->owner);
  940. }
  941. } else {
  942. /* Use the default policy if there is no last_policy. */
  943. if (policy->last_policy) {
  944. pol = policy->last_policy;
  945. } else {
  946. pol = cpufreq_parse_policy(default_governor);
  947. /*
  948. * In case the default governor is neither "performance"
  949. * nor "powersave", fall back to the initial policy
  950. * value set by the driver.
  951. */
  952. if (pol == CPUFREQ_POLICY_UNKNOWN)
  953. pol = policy->policy;
  954. }
  955. if (pol != CPUFREQ_POLICY_PERFORMANCE &&
  956. pol != CPUFREQ_POLICY_POWERSAVE)
  957. return -ENODATA;
  958. }
  959. ret = cpufreq_set_policy(policy, gov, pol);
  960. if (gov)
  961. module_put(gov->owner);
  962. return ret;
  963. }
  964. static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
  965. {
  966. int ret = 0;
  967. /* Has this CPU been taken care of already? */
  968. if (cpumask_test_cpu(cpu, policy->cpus))
  969. return 0;
  970. guard(cpufreq_policy_write)(policy);
  971. if (has_target())
  972. cpufreq_stop_governor(policy);
  973. cpumask_set_cpu(cpu, policy->cpus);
  974. if (has_target()) {
  975. ret = cpufreq_start_governor(policy);
  976. if (ret)
  977. pr_err("%s: Failed to start governor\n", __func__);
  978. }
  979. return ret;
  980. }
  981. void refresh_frequency_limits(struct cpufreq_policy *policy)
  982. {
  983. if (!policy_is_inactive(policy)) {
  984. pr_debug("updating policy for CPU %u\n", policy->cpu);
  985. cpufreq_set_policy(policy, policy->governor, policy->policy);
  986. }
  987. }
  988. EXPORT_SYMBOL(refresh_frequency_limits);
  989. static void handle_update(struct work_struct *work)
  990. {
  991. struct cpufreq_policy *policy =
  992. container_of(work, struct cpufreq_policy, update);
  993. pr_debug("handle_update for cpu %u called\n", policy->cpu);
  994. guard(cpufreq_policy_write)(policy);
  995. refresh_frequency_limits(policy);
  996. }
  997. static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
  998. void *data)
  999. {
  1000. struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
  1001. schedule_work(&policy->update);
  1002. return 0;
  1003. }
  1004. static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
  1005. void *data)
  1006. {
  1007. struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
  1008. schedule_work(&policy->update);
  1009. return 0;
  1010. }
  1011. static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
  1012. {
  1013. struct kobject *kobj;
  1014. struct completion *cmp;
  1015. scoped_guard(cpufreq_policy_write, policy) {
  1016. cpufreq_stats_free_table(policy);
  1017. kobj = &policy->kobj;
  1018. cmp = &policy->kobj_unregister;
  1019. }
  1020. kobject_put(kobj);
  1021. /*
  1022. * We need to make sure that the underlying kobj is
  1023. * actually not referenced anymore by anybody before we
  1024. * proceed with unloading.
  1025. */
  1026. pr_debug("waiting for dropping of refcount\n");
  1027. wait_for_completion(cmp);
  1028. pr_debug("wait complete\n");
  1029. }
  1030. static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
  1031. {
  1032. struct cpufreq_policy *policy;
  1033. struct device *dev = get_cpu_device(cpu);
  1034. int ret;
  1035. if (!dev)
  1036. return NULL;
  1037. policy = kzalloc_obj(*policy);
  1038. if (!policy)
  1039. return NULL;
  1040. if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
  1041. goto err_free_policy;
  1042. if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
  1043. goto err_free_cpumask;
  1044. if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
  1045. goto err_free_rcpumask;
  1046. init_completion(&policy->kobj_unregister);
  1047. ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
  1048. cpufreq_global_kobject, "policy%u", cpu);
  1049. if (ret) {
  1050. dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
  1051. /*
  1052. * The entire policy object will be freed below, but the extra
  1053. * memory allocated for the kobject name needs to be freed by
  1054. * releasing the kobject.
  1055. */
  1056. kobject_put(&policy->kobj);
  1057. goto err_free_real_cpus;
  1058. }
  1059. init_rwsem(&policy->rwsem);
  1060. freq_constraints_init(&policy->constraints);
  1061. policy->nb_min.notifier_call = cpufreq_notifier_min;
  1062. policy->nb_max.notifier_call = cpufreq_notifier_max;
  1063. ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN,
  1064. &policy->nb_min);
  1065. if (ret) {
  1066. dev_err(dev, "Failed to register MIN QoS notifier: %d (CPU%u)\n",
  1067. ret, cpu);
  1068. goto err_kobj_remove;
  1069. }
  1070. ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX,
  1071. &policy->nb_max);
  1072. if (ret) {
  1073. dev_err(dev, "Failed to register MAX QoS notifier: %d (CPU%u)\n",
  1074. ret, cpu);
  1075. goto err_min_qos_notifier;
  1076. }
  1077. INIT_LIST_HEAD(&policy->policy_list);
  1078. spin_lock_init(&policy->transition_lock);
  1079. init_waitqueue_head(&policy->transition_wait);
  1080. INIT_WORK(&policy->update, handle_update);
  1081. return policy;
  1082. err_min_qos_notifier:
  1083. freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
  1084. &policy->nb_min);
  1085. err_kobj_remove:
  1086. cpufreq_policy_put_kobj(policy);
  1087. err_free_real_cpus:
  1088. free_cpumask_var(policy->real_cpus);
  1089. err_free_rcpumask:
  1090. free_cpumask_var(policy->related_cpus);
  1091. err_free_cpumask:
  1092. free_cpumask_var(policy->cpus);
  1093. err_free_policy:
  1094. kfree(policy);
  1095. return NULL;
  1096. }
  1097. static void cpufreq_policy_free(struct cpufreq_policy *policy)
  1098. {
  1099. unsigned long flags;
  1100. int cpu;
  1101. /*
  1102. * The callers must ensure the policy is inactive by now, to avoid any
  1103. * races with show()/store() callbacks.
  1104. */
  1105. if (unlikely(!policy_is_inactive(policy)))
  1106. pr_warn("%s: Freeing active policy\n", __func__);
  1107. /* Remove policy from list */
  1108. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1109. list_del(&policy->policy_list);
  1110. for_each_cpu(cpu, policy->related_cpus)
  1111. per_cpu(cpufreq_cpu_data, cpu) = NULL;
  1112. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1113. freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX,
  1114. &policy->nb_max);
  1115. freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
  1116. &policy->nb_min);
  1117. /* Cancel any pending policy->update work before freeing the policy. */
  1118. cancel_work_sync(&policy->update);
  1119. if (policy->max_freq_req) {
  1120. /*
  1121. * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY
  1122. * notification, since CPUFREQ_CREATE_POLICY notification was
  1123. * sent after adding max_freq_req earlier.
  1124. */
  1125. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1126. CPUFREQ_REMOVE_POLICY, policy);
  1127. freq_qos_remove_request(policy->max_freq_req);
  1128. }
  1129. freq_qos_remove_request(policy->min_freq_req);
  1130. kfree(policy->min_freq_req);
  1131. cpufreq_policy_put_kobj(policy);
  1132. free_cpumask_var(policy->real_cpus);
  1133. free_cpumask_var(policy->related_cpus);
  1134. free_cpumask_var(policy->cpus);
  1135. kfree(policy);
  1136. }
  1137. static int cpufreq_policy_online(struct cpufreq_policy *policy,
  1138. unsigned int cpu, bool new_policy)
  1139. {
  1140. unsigned long flags;
  1141. unsigned int j;
  1142. int ret;
  1143. guard(cpufreq_policy_write)(policy);
  1144. policy->cpu = cpu;
  1145. policy->governor = NULL;
  1146. if (!new_policy && cpufreq_driver->online) {
  1147. /* Recover policy->cpus using related_cpus */
  1148. cpumask_copy(policy->cpus, policy->related_cpus);
  1149. ret = cpufreq_driver->online(policy);
  1150. if (ret) {
  1151. pr_debug("%s: %d: initialization failed\n", __func__,
  1152. __LINE__);
  1153. goto out_exit_policy;
  1154. }
  1155. } else {
  1156. cpumask_copy(policy->cpus, cpumask_of(cpu));
  1157. /*
  1158. * Call driver. From then on the cpufreq must be able
  1159. * to accept all calls to ->verify and ->setpolicy for this CPU.
  1160. */
  1161. ret = cpufreq_driver->init(policy);
  1162. if (ret) {
  1163. pr_debug("%s: %d: initialization failed\n", __func__,
  1164. __LINE__);
  1165. goto out_clear_policy;
  1166. }
  1167. /*
  1168. * The initialization has succeeded and the policy is online.
  1169. * If there is a problem with its frequency table, take it
  1170. * offline and drop it.
  1171. */
  1172. ret = cpufreq_table_validate_and_sort(policy);
  1173. if (ret)
  1174. goto out_offline_policy;
  1175. /* related_cpus should at least include policy->cpus. */
  1176. cpumask_copy(policy->related_cpus, policy->cpus);
  1177. }
  1178. /*
  1179. * affected cpus must always be the one, which are online. We aren't
  1180. * managing offline cpus here.
  1181. */
  1182. cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
  1183. if (new_policy) {
  1184. for_each_cpu(j, policy->related_cpus) {
  1185. per_cpu(cpufreq_cpu_data, j) = policy;
  1186. add_cpu_dev_symlink(policy, j, get_cpu_device(j));
  1187. }
  1188. policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
  1189. GFP_KERNEL);
  1190. if (!policy->min_freq_req) {
  1191. ret = -ENOMEM;
  1192. goto out_destroy_policy;
  1193. }
  1194. ret = freq_qos_add_request(&policy->constraints,
  1195. policy->min_freq_req, FREQ_QOS_MIN,
  1196. FREQ_QOS_MIN_DEFAULT_VALUE);
  1197. if (ret < 0) {
  1198. /*
  1199. * So we don't call freq_qos_remove_request() for an
  1200. * uninitialized request.
  1201. */
  1202. kfree(policy->min_freq_req);
  1203. policy->min_freq_req = NULL;
  1204. goto out_destroy_policy;
  1205. }
  1206. /*
  1207. * This must be initialized right here to avoid calling
  1208. * freq_qos_remove_request() on uninitialized request in case
  1209. * of errors.
  1210. */
  1211. policy->max_freq_req = policy->min_freq_req + 1;
  1212. ret = freq_qos_add_request(&policy->constraints,
  1213. policy->max_freq_req, FREQ_QOS_MAX,
  1214. FREQ_QOS_MAX_DEFAULT_VALUE);
  1215. if (ret < 0) {
  1216. policy->max_freq_req = NULL;
  1217. goto out_destroy_policy;
  1218. }
  1219. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1220. CPUFREQ_CREATE_POLICY, policy);
  1221. } else {
  1222. ret = freq_qos_update_request(policy->max_freq_req, policy->max);
  1223. if (ret < 0)
  1224. goto out_destroy_policy;
  1225. }
  1226. if (cpufreq_driver->get && has_target()) {
  1227. policy->cur = cpufreq_driver->get(policy->cpu);
  1228. if (!policy->cur) {
  1229. ret = -EIO;
  1230. pr_err("%s: ->get() failed\n", __func__);
  1231. goto out_destroy_policy;
  1232. }
  1233. }
  1234. /*
  1235. * Sometimes boot loaders set CPU frequency to a value outside of
  1236. * frequency table present with cpufreq core. In such cases CPU might be
  1237. * unstable if it has to run on that frequency for long duration of time
  1238. * and so its better to set it to a frequency which is specified in
  1239. * freq-table. This also makes cpufreq stats inconsistent as
  1240. * cpufreq-stats would fail to register because current frequency of CPU
  1241. * isn't found in freq-table.
  1242. *
  1243. * Because we don't want this change to effect boot process badly, we go
  1244. * for the next freq which is >= policy->cur ('cur' must be set by now,
  1245. * otherwise we will end up setting freq to lowest of the table as 'cur'
  1246. * is initialized to zero).
  1247. *
  1248. * We are passing target-freq as "policy->cur - 1" otherwise
  1249. * __cpufreq_driver_target() would simply fail, as policy->cur will be
  1250. * equal to target-freq.
  1251. */
  1252. if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
  1253. && has_target()) {
  1254. unsigned int old_freq = policy->cur;
  1255. /* Are we running at unknown frequency ? */
  1256. ret = cpufreq_frequency_table_get_index(policy, old_freq);
  1257. if (ret == -EINVAL) {
  1258. ret = __cpufreq_driver_target(policy, old_freq - 1,
  1259. CPUFREQ_RELATION_L);
  1260. /*
  1261. * Reaching here after boot in a few seconds may not
  1262. * mean that system will remain stable at "unknown"
  1263. * frequency for longer duration. Hence, a BUG_ON().
  1264. */
  1265. BUG_ON(ret);
  1266. pr_info("%s: CPU%d: Running at unlisted initial frequency: %u kHz, changing to: %u kHz\n",
  1267. __func__, policy->cpu, old_freq, policy->cur);
  1268. }
  1269. }
  1270. if (new_policy) {
  1271. ret = cpufreq_add_dev_interface(policy);
  1272. if (ret)
  1273. goto out_destroy_policy;
  1274. cpufreq_stats_create_table(policy);
  1275. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1276. list_add(&policy->policy_list, &cpufreq_policy_list);
  1277. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1278. /*
  1279. * Register with the energy model before
  1280. * em_rebuild_sched_domains() is called, which will result
  1281. * in rebuilding of the sched domains, which should only be done
  1282. * once the energy model is properly initialized for the policy
  1283. * first.
  1284. *
  1285. * Also, this should be called before the policy is registered
  1286. * with cooling framework.
  1287. */
  1288. if (cpufreq_driver->register_em)
  1289. cpufreq_driver->register_em(policy);
  1290. }
  1291. ret = cpufreq_init_policy(policy);
  1292. if (ret) {
  1293. pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
  1294. __func__, cpu, ret);
  1295. goto out_destroy_policy;
  1296. }
  1297. return 0;
  1298. out_destroy_policy:
  1299. for_each_cpu(j, policy->real_cpus)
  1300. remove_cpu_dev_symlink(policy, j, get_cpu_device(j));
  1301. out_offline_policy:
  1302. if (cpufreq_driver->offline)
  1303. cpufreq_driver->offline(policy);
  1304. out_exit_policy:
  1305. if (cpufreq_driver->exit)
  1306. cpufreq_driver->exit(policy);
  1307. out_clear_policy:
  1308. cpumask_clear(policy->cpus);
  1309. return ret;
  1310. }
  1311. static int cpufreq_online(unsigned int cpu)
  1312. {
  1313. struct cpufreq_policy *policy;
  1314. bool new_policy;
  1315. int ret;
  1316. pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
  1317. /* Check if this CPU already has a policy to manage it */
  1318. policy = per_cpu(cpufreq_cpu_data, cpu);
  1319. if (policy) {
  1320. WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
  1321. if (!policy_is_inactive(policy))
  1322. return cpufreq_add_policy_cpu(policy, cpu);
  1323. /* This is the only online CPU for the policy. Start over. */
  1324. new_policy = false;
  1325. } else {
  1326. new_policy = true;
  1327. policy = cpufreq_policy_alloc(cpu);
  1328. if (!policy)
  1329. return -ENOMEM;
  1330. }
  1331. ret = cpufreq_policy_online(policy, cpu, new_policy);
  1332. if (ret) {
  1333. cpufreq_policy_free(policy);
  1334. return ret;
  1335. }
  1336. kobject_uevent(&policy->kobj, KOBJ_ADD);
  1337. /* Callback for handling stuff after policy is ready */
  1338. if (cpufreq_driver->ready)
  1339. cpufreq_driver->ready(policy);
  1340. /* Register cpufreq cooling only for a new policy */
  1341. if (new_policy && cpufreq_thermal_control_enabled(cpufreq_driver))
  1342. policy->cdev = of_cpufreq_cooling_register(policy);
  1343. /*
  1344. * Let the per-policy boost flag mirror the cpufreq_driver boost during
  1345. * initialization for a new policy. For an existing policy, maintain the
  1346. * previous boost value unless global boost is disabled.
  1347. */
  1348. if (cpufreq_driver->set_boost && policy->boost_supported &&
  1349. (new_policy || !cpufreq_boost_enabled())) {
  1350. ret = policy_set_boost(policy, cpufreq_boost_enabled());
  1351. if (ret) {
  1352. /* If the set_boost fails, the online operation is not affected */
  1353. pr_info("%s: CPU%d: Cannot %s BOOST\n", __func__, policy->cpu,
  1354. str_enable_disable(cpufreq_boost_enabled()));
  1355. }
  1356. }
  1357. pr_debug("initialization complete\n");
  1358. return 0;
  1359. }
  1360. /**
  1361. * cpufreq_add_dev - the cpufreq interface for a CPU device.
  1362. * @dev: CPU device.
  1363. * @sif: Subsystem interface structure pointer (not used)
  1364. */
  1365. static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
  1366. {
  1367. struct cpufreq_policy *policy;
  1368. unsigned cpu = dev->id;
  1369. int ret;
  1370. dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
  1371. if (cpu_online(cpu)) {
  1372. ret = cpufreq_online(cpu);
  1373. if (ret)
  1374. return ret;
  1375. }
  1376. /* Create sysfs link on CPU registration */
  1377. policy = per_cpu(cpufreq_cpu_data, cpu);
  1378. if (policy)
  1379. add_cpu_dev_symlink(policy, cpu, dev);
  1380. return 0;
  1381. }
  1382. static void __cpufreq_offline(unsigned int cpu, struct cpufreq_policy *policy)
  1383. {
  1384. int ret;
  1385. if (has_target())
  1386. cpufreq_stop_governor(policy);
  1387. cpumask_clear_cpu(cpu, policy->cpus);
  1388. if (!policy_is_inactive(policy)) {
  1389. /* Nominate a new CPU if necessary. */
  1390. if (cpu == policy->cpu)
  1391. policy->cpu = cpumask_any(policy->cpus);
  1392. /* Start the governor again for the active policy. */
  1393. if (has_target()) {
  1394. ret = cpufreq_start_governor(policy);
  1395. if (ret)
  1396. pr_err("%s: Failed to start governor\n", __func__);
  1397. }
  1398. return;
  1399. }
  1400. if (has_target()) {
  1401. strscpy(policy->last_governor, policy->governor->name,
  1402. CPUFREQ_NAME_LEN);
  1403. cpufreq_exit_governor(policy);
  1404. } else {
  1405. policy->last_policy = policy->policy;
  1406. }
  1407. /*
  1408. * Perform the ->offline() during light-weight tear-down, as
  1409. * that allows fast recovery when the CPU comes back.
  1410. */
  1411. if (cpufreq_driver->offline) {
  1412. cpufreq_driver->offline(policy);
  1413. return;
  1414. }
  1415. if (cpufreq_driver->exit)
  1416. cpufreq_driver->exit(policy);
  1417. policy->freq_table = NULL;
  1418. }
  1419. static int cpufreq_offline(unsigned int cpu)
  1420. {
  1421. struct cpufreq_policy *policy;
  1422. pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
  1423. policy = cpufreq_cpu_get_raw(cpu);
  1424. if (!policy) {
  1425. pr_debug("%s: No cpu_data found\n", __func__);
  1426. return 0;
  1427. }
  1428. guard(cpufreq_policy_write)(policy);
  1429. __cpufreq_offline(cpu, policy);
  1430. return 0;
  1431. }
  1432. /*
  1433. * cpufreq_remove_dev - remove a CPU device
  1434. *
  1435. * Removes the cpufreq interface for a CPU device.
  1436. */
  1437. static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  1438. {
  1439. unsigned int cpu = dev->id;
  1440. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1441. if (!policy)
  1442. return;
  1443. scoped_guard(cpufreq_policy_write, policy) {
  1444. if (cpu_online(cpu))
  1445. __cpufreq_offline(cpu, policy);
  1446. remove_cpu_dev_symlink(policy, cpu, dev);
  1447. if (!cpumask_empty(policy->real_cpus))
  1448. return;
  1449. /*
  1450. * Unregister cpufreq cooling once all the CPUs of the policy
  1451. * are removed.
  1452. */
  1453. if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
  1454. cpufreq_cooling_unregister(policy->cdev);
  1455. policy->cdev = NULL;
  1456. }
  1457. /* We did light-weight exit earlier, do full tear down now */
  1458. if (cpufreq_driver->offline && cpufreq_driver->exit)
  1459. cpufreq_driver->exit(policy);
  1460. }
  1461. cpufreq_policy_free(policy);
  1462. }
  1463. /**
  1464. * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference.
  1465. * @policy: Policy managing CPUs.
  1466. * @new_freq: New CPU frequency.
  1467. *
  1468. * Adjust to the current frequency first and clean up later by either calling
  1469. * cpufreq_update_policy(), or scheduling handle_update().
  1470. */
  1471. static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
  1472. unsigned int new_freq)
  1473. {
  1474. struct cpufreq_freqs freqs;
  1475. pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
  1476. policy->cur, new_freq);
  1477. freqs.old = policy->cur;
  1478. freqs.new = new_freq;
  1479. cpufreq_freq_transition_begin(policy, &freqs);
  1480. cpufreq_freq_transition_end(policy, &freqs, 0);
  1481. }
  1482. static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
  1483. {
  1484. unsigned int new_freq;
  1485. if (!cpufreq_driver->get)
  1486. return 0;
  1487. new_freq = cpufreq_driver->get(policy->cpu);
  1488. if (!new_freq)
  1489. return 0;
  1490. /*
  1491. * If fast frequency switching is used with the given policy, the check
  1492. * against policy->cur is pointless, so skip it in that case.
  1493. */
  1494. if (policy->fast_switch_enabled || !has_target())
  1495. return new_freq;
  1496. if (policy->cur != new_freq) {
  1497. /*
  1498. * For some platforms, the frequency returned by hardware may be
  1499. * slightly different from what is provided in the frequency
  1500. * table, for example hardware may return 499 MHz instead of 500
  1501. * MHz. In such cases it is better to avoid getting into
  1502. * unnecessary frequency updates.
  1503. */
  1504. if (abs(policy->cur - new_freq) < KHZ_PER_MHZ)
  1505. return policy->cur;
  1506. cpufreq_out_of_sync(policy, new_freq);
  1507. if (update)
  1508. schedule_work(&policy->update);
  1509. }
  1510. return new_freq;
  1511. }
  1512. /**
  1513. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  1514. * @cpu: CPU number
  1515. *
  1516. * This is the last known freq, without actually getting it from the driver.
  1517. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  1518. */
  1519. unsigned int cpufreq_quick_get(unsigned int cpu)
  1520. {
  1521. unsigned long flags;
  1522. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1523. if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
  1524. unsigned int ret_freq = cpufreq_driver->get(cpu);
  1525. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1526. return ret_freq;
  1527. }
  1528. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1529. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  1530. if (policy)
  1531. return policy->cur;
  1532. return 0;
  1533. }
  1534. EXPORT_SYMBOL(cpufreq_quick_get);
  1535. /**
  1536. * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
  1537. * @cpu: CPU number
  1538. *
  1539. * Just return the max possible frequency for a given CPU.
  1540. */
  1541. unsigned int cpufreq_quick_get_max(unsigned int cpu)
  1542. {
  1543. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  1544. if (policy)
  1545. return policy->max;
  1546. return 0;
  1547. }
  1548. EXPORT_SYMBOL(cpufreq_quick_get_max);
  1549. /**
  1550. * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU
  1551. * @cpu: CPU number
  1552. *
  1553. * The default return value is the max_freq field of cpuinfo.
  1554. */
  1555. __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
  1556. {
  1557. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  1558. if (policy)
  1559. return policy->cpuinfo.max_freq;
  1560. return 0;
  1561. }
  1562. EXPORT_SYMBOL(cpufreq_get_hw_max_freq);
  1563. static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
  1564. {
  1565. if (unlikely(policy_is_inactive(policy)))
  1566. return 0;
  1567. return cpufreq_verify_current_freq(policy, true);
  1568. }
  1569. /**
  1570. * cpufreq_get - get the current CPU frequency (in kHz)
  1571. * @cpu: CPU number
  1572. *
  1573. * Get the CPU current (static) CPU frequency
  1574. */
  1575. unsigned int cpufreq_get(unsigned int cpu)
  1576. {
  1577. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  1578. if (!policy)
  1579. return 0;
  1580. guard(cpufreq_policy_read)(policy);
  1581. return __cpufreq_get(policy);
  1582. }
  1583. EXPORT_SYMBOL(cpufreq_get);
  1584. static struct subsys_interface cpufreq_interface = {
  1585. .name = "cpufreq",
  1586. .subsys = &cpu_subsys,
  1587. .add_dev = cpufreq_add_dev,
  1588. .remove_dev = cpufreq_remove_dev,
  1589. };
  1590. /*
  1591. * In case platform wants some specific frequency to be configured
  1592. * during suspend..
  1593. */
  1594. int cpufreq_generic_suspend(struct cpufreq_policy *policy)
  1595. {
  1596. int ret;
  1597. if (!policy->suspend_freq) {
  1598. pr_debug("%s: suspend_freq not defined\n", __func__);
  1599. return 0;
  1600. }
  1601. pr_debug("%s: Setting suspend-freq: %u\n", __func__,
  1602. policy->suspend_freq);
  1603. ret = __cpufreq_driver_target(policy, policy->suspend_freq,
  1604. CPUFREQ_RELATION_H);
  1605. if (ret)
  1606. pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
  1607. __func__, policy->suspend_freq, ret);
  1608. return ret;
  1609. }
  1610. EXPORT_SYMBOL(cpufreq_generic_suspend);
  1611. /**
  1612. * cpufreq_suspend() - Suspend CPUFreq governors.
  1613. *
  1614. * Called during system wide Suspend/Hibernate cycles for suspending governors
  1615. * as some platforms can't change frequency after this point in suspend cycle.
  1616. * Because some of the devices (like: i2c, regulators, etc) they use for
  1617. * changing frequency are suspended quickly after this point.
  1618. */
  1619. void cpufreq_suspend(void)
  1620. {
  1621. struct cpufreq_policy *policy;
  1622. if (!cpufreq_driver)
  1623. return;
  1624. if (!has_target() && !cpufreq_driver->suspend)
  1625. goto suspend;
  1626. pr_debug("%s: Suspending Governors\n", __func__);
  1627. for_each_active_policy(policy) {
  1628. if (has_target()) {
  1629. scoped_guard(cpufreq_policy_write, policy) {
  1630. cpufreq_stop_governor(policy);
  1631. }
  1632. }
  1633. if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
  1634. pr_err("%s: Failed to suspend driver: %s\n", __func__,
  1635. cpufreq_driver->name);
  1636. }
  1637. suspend:
  1638. cpufreq_suspended = true;
  1639. }
  1640. /**
  1641. * cpufreq_resume() - Resume CPUFreq governors.
  1642. *
  1643. * Called during system wide Suspend/Hibernate cycle for resuming governors that
  1644. * are suspended with cpufreq_suspend().
  1645. */
  1646. void cpufreq_resume(void)
  1647. {
  1648. struct cpufreq_policy *policy;
  1649. int ret;
  1650. if (!cpufreq_driver)
  1651. return;
  1652. if (unlikely(!cpufreq_suspended))
  1653. return;
  1654. cpufreq_suspended = false;
  1655. if (!has_target() && !cpufreq_driver->resume)
  1656. return;
  1657. pr_debug("%s: Resuming Governors\n", __func__);
  1658. for_each_active_policy(policy) {
  1659. if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
  1660. pr_err("%s: Failed to resume driver: %s\n", __func__,
  1661. cpufreq_driver->name);
  1662. } else if (has_target()) {
  1663. scoped_guard(cpufreq_policy_write, policy) {
  1664. ret = cpufreq_start_governor(policy);
  1665. }
  1666. if (ret)
  1667. pr_err("%s: Failed to start governor for CPU%u's policy\n",
  1668. __func__, policy->cpu);
  1669. }
  1670. }
  1671. }
  1672. /**
  1673. * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones.
  1674. * @flags: Flags to test against the current cpufreq driver's flags.
  1675. *
  1676. * Assumes that the driver is there, so callers must ensure that this is the
  1677. * case.
  1678. */
  1679. bool cpufreq_driver_test_flags(u16 flags)
  1680. {
  1681. return !!(cpufreq_driver->flags & flags);
  1682. }
  1683. /**
  1684. * cpufreq_get_current_driver - Return the current driver's name.
  1685. *
  1686. * Return the name string of the currently registered cpufreq driver or NULL if
  1687. * none.
  1688. */
  1689. const char *cpufreq_get_current_driver(void)
  1690. {
  1691. if (cpufreq_driver)
  1692. return cpufreq_driver->name;
  1693. return NULL;
  1694. }
  1695. EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
  1696. /**
  1697. * cpufreq_get_driver_data - Return current driver data.
  1698. *
  1699. * Return the private data of the currently registered cpufreq driver, or NULL
  1700. * if no cpufreq driver has been registered.
  1701. */
  1702. void *cpufreq_get_driver_data(void)
  1703. {
  1704. if (cpufreq_driver)
  1705. return cpufreq_driver->driver_data;
  1706. return NULL;
  1707. }
  1708. EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
  1709. /*********************************************************************
  1710. * NOTIFIER LISTS INTERFACE *
  1711. *********************************************************************/
  1712. /**
  1713. * cpufreq_register_notifier - Register a notifier with cpufreq.
  1714. * @nb: notifier function to register.
  1715. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
  1716. *
  1717. * Add a notifier to one of two lists: either a list of notifiers that run on
  1718. * clock rate changes (once before and once after every transition), or a list
  1719. * of notifiers that ron on cpufreq policy changes.
  1720. *
  1721. * This function may sleep and it has the same return values as
  1722. * blocking_notifier_chain_register().
  1723. */
  1724. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1725. {
  1726. int ret;
  1727. if (cpufreq_disabled())
  1728. return -EINVAL;
  1729. switch (list) {
  1730. case CPUFREQ_TRANSITION_NOTIFIER:
  1731. mutex_lock(&cpufreq_fast_switch_lock);
  1732. if (cpufreq_fast_switch_count > 0) {
  1733. mutex_unlock(&cpufreq_fast_switch_lock);
  1734. return -EBUSY;
  1735. }
  1736. ret = srcu_notifier_chain_register(
  1737. &cpufreq_transition_notifier_list, nb);
  1738. if (!ret)
  1739. cpufreq_fast_switch_count--;
  1740. mutex_unlock(&cpufreq_fast_switch_lock);
  1741. break;
  1742. case CPUFREQ_POLICY_NOTIFIER:
  1743. ret = blocking_notifier_chain_register(
  1744. &cpufreq_policy_notifier_list, nb);
  1745. break;
  1746. default:
  1747. ret = -EINVAL;
  1748. }
  1749. return ret;
  1750. }
  1751. EXPORT_SYMBOL(cpufreq_register_notifier);
  1752. /**
  1753. * cpufreq_unregister_notifier - Unregister a notifier from cpufreq.
  1754. * @nb: notifier block to be unregistered.
  1755. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
  1756. *
  1757. * Remove a notifier from one of the cpufreq notifier lists.
  1758. *
  1759. * This function may sleep and it has the same return values as
  1760. * blocking_notifier_chain_unregister().
  1761. */
  1762. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1763. {
  1764. int ret;
  1765. if (cpufreq_disabled())
  1766. return -EINVAL;
  1767. switch (list) {
  1768. case CPUFREQ_TRANSITION_NOTIFIER:
  1769. mutex_lock(&cpufreq_fast_switch_lock);
  1770. ret = srcu_notifier_chain_unregister(
  1771. &cpufreq_transition_notifier_list, nb);
  1772. if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
  1773. cpufreq_fast_switch_count++;
  1774. mutex_unlock(&cpufreq_fast_switch_lock);
  1775. break;
  1776. case CPUFREQ_POLICY_NOTIFIER:
  1777. ret = blocking_notifier_chain_unregister(
  1778. &cpufreq_policy_notifier_list, nb);
  1779. break;
  1780. default:
  1781. ret = -EINVAL;
  1782. }
  1783. return ret;
  1784. }
  1785. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1786. /*********************************************************************
  1787. * GOVERNORS *
  1788. *********************************************************************/
  1789. /**
  1790. * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
  1791. * @policy: cpufreq policy to switch the frequency for.
  1792. * @target_freq: New frequency to set (may be approximate).
  1793. *
  1794. * Carry out a fast frequency switch without sleeping.
  1795. *
  1796. * The driver's ->fast_switch() callback invoked by this function must be
  1797. * suitable for being called from within RCU-sched read-side critical sections
  1798. * and it is expected to select the minimum available frequency greater than or
  1799. * equal to @target_freq (CPUFREQ_RELATION_L).
  1800. *
  1801. * This function must not be called if policy->fast_switch_enabled is unset.
  1802. *
  1803. * Governors calling this function must guarantee that it will never be invoked
  1804. * twice in parallel for the same policy and that it will never be called in
  1805. * parallel with either ->target() or ->target_index() for the same policy.
  1806. *
  1807. * Returns the actual frequency set for the CPU.
  1808. *
  1809. * If 0 is returned by the driver's ->fast_switch() callback to indicate an
  1810. * error condition, the hardware configuration must be preserved.
  1811. */
  1812. unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
  1813. unsigned int target_freq)
  1814. {
  1815. unsigned int freq;
  1816. int cpu;
  1817. target_freq = clamp_val(target_freq, policy->min, policy->max);
  1818. freq = cpufreq_driver->fast_switch(policy, target_freq);
  1819. if (!freq)
  1820. return 0;
  1821. policy->cur = freq;
  1822. arch_set_freq_scale(policy->related_cpus, freq,
  1823. arch_scale_freq_ref(policy->cpu));
  1824. cpufreq_stats_record_transition(policy, freq);
  1825. if (trace_cpu_frequency_enabled()) {
  1826. for_each_cpu(cpu, policy->cpus)
  1827. trace_cpu_frequency(freq, cpu);
  1828. }
  1829. return freq;
  1830. }
  1831. EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
  1832. /**
  1833. * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go.
  1834. * @cpu: Target CPU.
  1835. * @min_perf: Minimum (required) performance level (units of @capacity).
  1836. * @target_perf: Target (desired) performance level (units of @capacity).
  1837. * @capacity: Capacity of the target CPU.
  1838. *
  1839. * Carry out a fast performance level switch of @cpu without sleeping.
  1840. *
  1841. * The driver's ->adjust_perf() callback invoked by this function must be
  1842. * suitable for being called from within RCU-sched read-side critical sections
  1843. * and it is expected to select a suitable performance level equal to or above
  1844. * @min_perf and preferably equal to or below @target_perf.
  1845. *
  1846. * This function must not be called if policy->fast_switch_enabled is unset.
  1847. *
  1848. * Governors calling this function must guarantee that it will never be invoked
  1849. * twice in parallel for the same CPU and that it will never be called in
  1850. * parallel with either ->target() or ->target_index() or ->fast_switch() for
  1851. * the same CPU.
  1852. */
  1853. void cpufreq_driver_adjust_perf(unsigned int cpu,
  1854. unsigned long min_perf,
  1855. unsigned long target_perf,
  1856. unsigned long capacity)
  1857. {
  1858. cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity);
  1859. }
  1860. /**
  1861. * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback.
  1862. *
  1863. * Return 'true' if the ->adjust_perf callback is present for the
  1864. * current driver or 'false' otherwise.
  1865. */
  1866. bool cpufreq_driver_has_adjust_perf(void)
  1867. {
  1868. return !!cpufreq_driver->adjust_perf;
  1869. }
  1870. /* Must set freqs->new to intermediate frequency */
  1871. static int __target_intermediate(struct cpufreq_policy *policy,
  1872. struct cpufreq_freqs *freqs, int index)
  1873. {
  1874. int ret;
  1875. freqs->new = cpufreq_driver->get_intermediate(policy, index);
  1876. /* We don't need to switch to intermediate freq */
  1877. if (!freqs->new)
  1878. return 0;
  1879. pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
  1880. __func__, policy->cpu, freqs->old, freqs->new);
  1881. cpufreq_freq_transition_begin(policy, freqs);
  1882. ret = cpufreq_driver->target_intermediate(policy, index);
  1883. cpufreq_freq_transition_end(policy, freqs, ret);
  1884. if (ret)
  1885. pr_err("%s: Failed to change to intermediate frequency: %d\n",
  1886. __func__, ret);
  1887. return ret;
  1888. }
  1889. static int __target_index(struct cpufreq_policy *policy, int index)
  1890. {
  1891. struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
  1892. unsigned int restore_freq, intermediate_freq = 0;
  1893. unsigned int newfreq = policy->freq_table[index].frequency;
  1894. int retval = -EINVAL;
  1895. bool notify;
  1896. if (newfreq == policy->cur)
  1897. return 0;
  1898. /* Save last value to restore later on errors */
  1899. restore_freq = policy->cur;
  1900. notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
  1901. if (notify) {
  1902. /* Handle switching to intermediate frequency */
  1903. if (cpufreq_driver->get_intermediate) {
  1904. retval = __target_intermediate(policy, &freqs, index);
  1905. if (retval)
  1906. return retval;
  1907. intermediate_freq = freqs.new;
  1908. /* Set old freq to intermediate */
  1909. if (intermediate_freq)
  1910. freqs.old = freqs.new;
  1911. }
  1912. freqs.new = newfreq;
  1913. pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
  1914. __func__, policy->cpu, freqs.old, freqs.new);
  1915. cpufreq_freq_transition_begin(policy, &freqs);
  1916. }
  1917. retval = cpufreq_driver->target_index(policy, index);
  1918. if (retval)
  1919. pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
  1920. retval);
  1921. if (notify) {
  1922. cpufreq_freq_transition_end(policy, &freqs, retval);
  1923. /*
  1924. * Failed after setting to intermediate freq? Driver should have
  1925. * reverted back to initial frequency and so should we. Check
  1926. * here for intermediate_freq instead of get_intermediate, in
  1927. * case we haven't switched to intermediate freq at all.
  1928. */
  1929. if (unlikely(retval && intermediate_freq)) {
  1930. freqs.old = intermediate_freq;
  1931. freqs.new = restore_freq;
  1932. cpufreq_freq_transition_begin(policy, &freqs);
  1933. cpufreq_freq_transition_end(policy, &freqs, 0);
  1934. }
  1935. }
  1936. return retval;
  1937. }
  1938. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1939. unsigned int target_freq,
  1940. unsigned int relation)
  1941. {
  1942. unsigned int old_target_freq = target_freq;
  1943. if (cpufreq_disabled())
  1944. return -ENODEV;
  1945. target_freq = __resolve_freq(policy, target_freq, policy->min,
  1946. policy->max, relation);
  1947. pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
  1948. policy->cpu, target_freq, relation, old_target_freq);
  1949. /*
  1950. * This might look like a redundant call as we are checking it again
  1951. * after finding index. But it is left intentionally for cases where
  1952. * exactly same freq is called again and so we can save on few function
  1953. * calls.
  1954. */
  1955. if (target_freq == policy->cur &&
  1956. !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS))
  1957. return 0;
  1958. if (cpufreq_driver->target) {
  1959. /*
  1960. * If the driver hasn't setup a single inefficient frequency,
  1961. * it's unlikely it knows how to decode CPUFREQ_RELATION_E.
  1962. */
  1963. if (!policy->efficiencies_available)
  1964. relation &= ~CPUFREQ_RELATION_E;
  1965. return cpufreq_driver->target(policy, target_freq, relation);
  1966. }
  1967. if (!cpufreq_driver->target_index)
  1968. return -EINVAL;
  1969. return __target_index(policy, policy->cached_resolved_idx);
  1970. }
  1971. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1972. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1973. unsigned int target_freq,
  1974. unsigned int relation)
  1975. {
  1976. guard(cpufreq_policy_write)(policy);
  1977. return __cpufreq_driver_target(policy, target_freq, relation);
  1978. }
  1979. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1980. __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
  1981. {
  1982. return NULL;
  1983. }
  1984. static int cpufreq_init_governor(struct cpufreq_policy *policy)
  1985. {
  1986. int ret;
  1987. /* Don't start any governor operations if we are entering suspend */
  1988. if (cpufreq_suspended)
  1989. return 0;
  1990. /*
  1991. * Governor might not be initiated here if ACPI _PPC changed
  1992. * notification happened, so check it.
  1993. */
  1994. if (!policy->governor)
  1995. return -EINVAL;
  1996. /* Platform doesn't want dynamic frequency switching ? */
  1997. if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING &&
  1998. cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
  1999. struct cpufreq_governor *gov = cpufreq_fallback_governor();
  2000. if (gov) {
  2001. pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
  2002. policy->governor->name, gov->name);
  2003. policy->governor = gov;
  2004. } else {
  2005. return -EINVAL;
  2006. }
  2007. }
  2008. if (!try_module_get(policy->governor->owner))
  2009. return -EINVAL;
  2010. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  2011. if (policy->governor->init) {
  2012. ret = policy->governor->init(policy);
  2013. if (ret) {
  2014. module_put(policy->governor->owner);
  2015. return ret;
  2016. }
  2017. }
  2018. policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET);
  2019. return 0;
  2020. }
  2021. static void cpufreq_exit_governor(struct cpufreq_policy *policy)
  2022. {
  2023. if (cpufreq_suspended || !policy->governor)
  2024. return;
  2025. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  2026. if (policy->governor->exit)
  2027. policy->governor->exit(policy);
  2028. module_put(policy->governor->owner);
  2029. }
  2030. int cpufreq_start_governor(struct cpufreq_policy *policy)
  2031. {
  2032. int ret;
  2033. if (cpufreq_suspended)
  2034. return 0;
  2035. if (!policy->governor)
  2036. return -EINVAL;
  2037. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  2038. cpufreq_verify_current_freq(policy, false);
  2039. if (policy->governor->start) {
  2040. ret = policy->governor->start(policy);
  2041. if (ret)
  2042. return ret;
  2043. }
  2044. if (policy->governor->limits)
  2045. policy->governor->limits(policy);
  2046. return 0;
  2047. }
  2048. void cpufreq_stop_governor(struct cpufreq_policy *policy)
  2049. {
  2050. if (cpufreq_suspended || !policy->governor)
  2051. return;
  2052. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  2053. if (policy->governor->stop)
  2054. policy->governor->stop(policy);
  2055. }
  2056. static void cpufreq_governor_limits(struct cpufreq_policy *policy)
  2057. {
  2058. if (cpufreq_suspended || !policy->governor)
  2059. return;
  2060. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  2061. if (policy->governor->limits)
  2062. policy->governor->limits(policy);
  2063. }
  2064. int cpufreq_register_governor(struct cpufreq_governor *governor)
  2065. {
  2066. int err;
  2067. if (!governor)
  2068. return -EINVAL;
  2069. if (cpufreq_disabled())
  2070. return -ENODEV;
  2071. mutex_lock(&cpufreq_governor_mutex);
  2072. err = -EBUSY;
  2073. if (!find_governor(governor->name)) {
  2074. err = 0;
  2075. list_add(&governor->governor_list, &cpufreq_governor_list);
  2076. }
  2077. mutex_unlock(&cpufreq_governor_mutex);
  2078. return err;
  2079. }
  2080. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  2081. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  2082. {
  2083. struct cpufreq_policy *policy;
  2084. unsigned long flags;
  2085. if (!governor)
  2086. return;
  2087. if (cpufreq_disabled())
  2088. return;
  2089. /* clear last_governor for all inactive policies */
  2090. read_lock_irqsave(&cpufreq_driver_lock, flags);
  2091. for_each_inactive_policy(policy) {
  2092. if (!strcmp(policy->last_governor, governor->name)) {
  2093. policy->governor = NULL;
  2094. policy->last_governor[0] = '\0';
  2095. }
  2096. }
  2097. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2098. mutex_lock(&cpufreq_governor_mutex);
  2099. list_del(&governor->governor_list);
  2100. mutex_unlock(&cpufreq_governor_mutex);
  2101. }
  2102. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  2103. /*********************************************************************
  2104. * POLICY INTERFACE *
  2105. *********************************************************************/
  2106. DEFINE_PER_CPU(unsigned long, cpufreq_pressure);
  2107. /**
  2108. * cpufreq_update_pressure() - Update cpufreq pressure for CPUs
  2109. * @policy: cpufreq policy of the CPUs.
  2110. *
  2111. * Update the value of cpufreq pressure for all @cpus in the policy.
  2112. */
  2113. static void cpufreq_update_pressure(struct cpufreq_policy *policy)
  2114. {
  2115. unsigned long max_capacity, capped_freq, pressure;
  2116. u32 max_freq;
  2117. int cpu;
  2118. cpu = cpumask_first(policy->related_cpus);
  2119. max_freq = arch_scale_freq_ref(cpu);
  2120. capped_freq = policy->max;
  2121. /*
  2122. * Handle properly the boost frequencies, which should simply clean
  2123. * the cpufreq pressure value.
  2124. */
  2125. if (max_freq <= capped_freq) {
  2126. pressure = 0;
  2127. } else {
  2128. max_capacity = arch_scale_cpu_capacity(cpu);
  2129. pressure = max_capacity -
  2130. mult_frac(max_capacity, capped_freq, max_freq);
  2131. }
  2132. for_each_cpu(cpu, policy->related_cpus)
  2133. WRITE_ONCE(per_cpu(cpufreq_pressure, cpu), pressure);
  2134. }
  2135. /**
  2136. * cpufreq_set_policy - Modify cpufreq policy parameters.
  2137. * @policy: Policy object to modify.
  2138. * @new_gov: Policy governor pointer.
  2139. * @new_pol: Policy value (for drivers with built-in governors).
  2140. *
  2141. * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
  2142. * limits to be set for the policy, update @policy with the verified limits
  2143. * values and either invoke the driver's ->setpolicy() callback (if present) or
  2144. * carry out a governor update for @policy. That is, run the current governor's
  2145. * ->limits() callback (if @new_gov points to the same object as the one in
  2146. * @policy) or replace the governor for @policy with @new_gov.
  2147. *
  2148. * The cpuinfo part of @policy is not updated by this function.
  2149. */
  2150. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  2151. struct cpufreq_governor *new_gov,
  2152. unsigned int new_pol)
  2153. {
  2154. struct cpufreq_policy_data new_data;
  2155. struct cpufreq_governor *old_gov;
  2156. int ret;
  2157. memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
  2158. new_data.freq_table = policy->freq_table;
  2159. new_data.cpu = policy->cpu;
  2160. /*
  2161. * PM QoS framework collects all the requests from users and provide us
  2162. * the final aggregated value here.
  2163. */
  2164. new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
  2165. new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
  2166. pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
  2167. new_data.cpu, new_data.min, new_data.max);
  2168. /*
  2169. * Verify that the CPU speed can be set within these limits and make sure
  2170. * that min <= max.
  2171. */
  2172. ret = cpufreq_driver->verify(&new_data);
  2173. if (ret)
  2174. return ret;
  2175. /*
  2176. * Resolve policy min/max to available frequencies. It ensures
  2177. * no frequency resolution will neither overshoot the requested maximum
  2178. * nor undershoot the requested minimum.
  2179. *
  2180. * Avoid storing intermediate values in policy->max or policy->min and
  2181. * compiler optimizations around them because they may be accessed
  2182. * concurrently by cpufreq_driver_resolve_freq() during the update.
  2183. */
  2184. WRITE_ONCE(policy->max, __resolve_freq(policy, new_data.max,
  2185. new_data.min, new_data.max,
  2186. CPUFREQ_RELATION_H));
  2187. new_data.min = __resolve_freq(policy, new_data.min, new_data.min,
  2188. new_data.max, CPUFREQ_RELATION_L);
  2189. WRITE_ONCE(policy->min, new_data.min > policy->max ? policy->max : new_data.min);
  2190. trace_cpu_frequency_limits(policy);
  2191. cpufreq_update_pressure(policy);
  2192. policy->cached_target_freq = UINT_MAX;
  2193. pr_debug("new min and max freqs are %u - %u kHz\n",
  2194. policy->min, policy->max);
  2195. if (cpufreq_driver->setpolicy) {
  2196. policy->policy = new_pol;
  2197. pr_debug("setting range\n");
  2198. return cpufreq_driver->setpolicy(policy);
  2199. }
  2200. if (new_gov == policy->governor) {
  2201. pr_debug("governor limits update\n");
  2202. cpufreq_governor_limits(policy);
  2203. return 0;
  2204. }
  2205. pr_debug("governor switch\n");
  2206. /* save old, working values */
  2207. old_gov = policy->governor;
  2208. /* end old governor */
  2209. if (old_gov) {
  2210. cpufreq_stop_governor(policy);
  2211. cpufreq_exit_governor(policy);
  2212. }
  2213. /* start new governor */
  2214. policy->governor = new_gov;
  2215. ret = cpufreq_init_governor(policy);
  2216. if (!ret) {
  2217. ret = cpufreq_start_governor(policy);
  2218. if (!ret) {
  2219. pr_debug("governor change\n");
  2220. return 0;
  2221. }
  2222. cpufreq_exit_governor(policy);
  2223. }
  2224. /* new governor failed, so re-start old one */
  2225. pr_debug("starting governor %s failed\n", policy->governor->name);
  2226. if (old_gov) {
  2227. policy->governor = old_gov;
  2228. if (cpufreq_init_governor(policy)) {
  2229. policy->governor = NULL;
  2230. } else if (cpufreq_start_governor(policy)) {
  2231. cpufreq_exit_governor(policy);
  2232. policy->governor = NULL;
  2233. }
  2234. }
  2235. return ret;
  2236. }
  2237. static void cpufreq_policy_refresh(struct cpufreq_policy *policy)
  2238. {
  2239. guard(cpufreq_policy_write)(policy);
  2240. /*
  2241. * BIOS might change freq behind our back
  2242. * -> ask driver for current freq and notify governors about a change
  2243. */
  2244. if (cpufreq_driver->get && has_target() &&
  2245. (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
  2246. return;
  2247. refresh_frequency_limits(policy);
  2248. }
  2249. /**
  2250. * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
  2251. * @cpu: CPU to re-evaluate the policy for.
  2252. *
  2253. * Update the current frequency for the cpufreq policy of @cpu and use
  2254. * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
  2255. * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
  2256. * for the policy in question, among other things.
  2257. */
  2258. void cpufreq_update_policy(unsigned int cpu)
  2259. {
  2260. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  2261. if (!policy)
  2262. return;
  2263. cpufreq_policy_refresh(policy);
  2264. }
  2265. EXPORT_SYMBOL(cpufreq_update_policy);
  2266. /**
  2267. * cpufreq_update_limits - Update policy limits for a given CPU.
  2268. * @cpu: CPU to update the policy limits for.
  2269. *
  2270. * Invoke the driver's ->update_limits callback if present or call
  2271. * cpufreq_policy_refresh() for @cpu.
  2272. */
  2273. void cpufreq_update_limits(unsigned int cpu)
  2274. {
  2275. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  2276. if (!policy)
  2277. return;
  2278. if (cpufreq_driver->update_limits)
  2279. cpufreq_driver->update_limits(policy);
  2280. else
  2281. cpufreq_policy_refresh(policy);
  2282. }
  2283. EXPORT_SYMBOL_GPL(cpufreq_update_limits);
  2284. /*********************************************************************
  2285. * BOOST *
  2286. *********************************************************************/
  2287. int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state)
  2288. {
  2289. int ret;
  2290. if (!policy->freq_table)
  2291. return -ENXIO;
  2292. ret = cpufreq_frequency_table_cpuinfo(policy);
  2293. if (ret) {
  2294. pr_err("%s: Policy frequency update failed\n", __func__);
  2295. return ret;
  2296. }
  2297. ret = freq_qos_update_request(policy->max_freq_req, policy->max);
  2298. if (ret < 0)
  2299. return ret;
  2300. return 0;
  2301. }
  2302. EXPORT_SYMBOL_GPL(cpufreq_boost_set_sw);
  2303. static int cpufreq_boost_trigger_state(int state)
  2304. {
  2305. struct cpufreq_policy *policy;
  2306. unsigned long flags;
  2307. int ret = -EOPNOTSUPP;
  2308. /*
  2309. * Don't compare 'cpufreq_driver->boost_enabled' with 'state' here to
  2310. * make sure all policies are in sync with global boost flag.
  2311. */
  2312. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2313. cpufreq_driver->boost_enabled = state;
  2314. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2315. cpus_read_lock();
  2316. for_each_active_policy(policy) {
  2317. if (!policy->boost_supported)
  2318. continue;
  2319. ret = policy_set_boost(policy, state);
  2320. if (unlikely(ret))
  2321. break;
  2322. }
  2323. cpus_read_unlock();
  2324. if (likely(!ret))
  2325. return 0;
  2326. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2327. cpufreq_driver->boost_enabled = !state;
  2328. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2329. pr_err("%s: Cannot %s BOOST\n",
  2330. __func__, str_enable_disable(state));
  2331. return ret;
  2332. }
  2333. static bool cpufreq_boost_supported(void)
  2334. {
  2335. return cpufreq_driver->set_boost;
  2336. }
  2337. static int create_boost_sysfs_file(void)
  2338. {
  2339. int ret;
  2340. ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
  2341. if (ret)
  2342. pr_err("%s: cannot register global BOOST sysfs file\n",
  2343. __func__);
  2344. return ret;
  2345. }
  2346. static void remove_boost_sysfs_file(void)
  2347. {
  2348. if (cpufreq_boost_supported())
  2349. sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
  2350. }
  2351. bool cpufreq_boost_enabled(void)
  2352. {
  2353. return cpufreq_driver->boost_enabled;
  2354. }
  2355. EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
  2356. /*********************************************************************
  2357. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  2358. *********************************************************************/
  2359. static enum cpuhp_state hp_online;
  2360. static int cpuhp_cpufreq_online(unsigned int cpu)
  2361. {
  2362. cpufreq_online(cpu);
  2363. return 0;
  2364. }
  2365. static int cpuhp_cpufreq_offline(unsigned int cpu)
  2366. {
  2367. cpufreq_offline(cpu);
  2368. return 0;
  2369. }
  2370. /**
  2371. * cpufreq_register_driver - register a CPU Frequency driver
  2372. * @driver_data: A struct cpufreq_driver containing the values#
  2373. * submitted by the CPU Frequency driver.
  2374. *
  2375. * Registers a CPU Frequency driver to this core code. This code
  2376. * returns zero on success, -EEXIST when another driver got here first
  2377. * (and isn't unregistered in the meantime).
  2378. *
  2379. */
  2380. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  2381. {
  2382. unsigned long flags;
  2383. int ret;
  2384. if (cpufreq_disabled())
  2385. return -ENODEV;
  2386. /*
  2387. * The cpufreq core depends heavily on the availability of device
  2388. * structure, make sure they are available before proceeding further.
  2389. */
  2390. if (!get_cpu_device(0))
  2391. return -EPROBE_DEFER;
  2392. if (!driver_data || !driver_data->verify || !driver_data->init ||
  2393. (driver_data->target_index && driver_data->target) ||
  2394. (!!driver_data->setpolicy == (driver_data->target_index || driver_data->target)) ||
  2395. (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
  2396. (!driver_data->online != !driver_data->offline) ||
  2397. (driver_data->adjust_perf && !driver_data->fast_switch))
  2398. return -EINVAL;
  2399. pr_debug("trying to register driver %s\n", driver_data->name);
  2400. /* Protect against concurrent CPU online/offline. */
  2401. cpus_read_lock();
  2402. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2403. if (cpufreq_driver) {
  2404. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2405. ret = -EEXIST;
  2406. goto out;
  2407. }
  2408. cpufreq_driver = driver_data;
  2409. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2410. if (driver_data->setpolicy)
  2411. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  2412. if (cpufreq_boost_supported()) {
  2413. ret = create_boost_sysfs_file();
  2414. if (ret)
  2415. goto err_null_driver;
  2416. }
  2417. /*
  2418. * Mark support for the scheduler's frequency invariance engine for
  2419. * drivers that implement target(), target_index() or fast_switch().
  2420. */
  2421. if (!cpufreq_driver->setpolicy) {
  2422. static_branch_enable_cpuslocked(&cpufreq_freq_invariance);
  2423. pr_debug("cpufreq: supports frequency invariance\n");
  2424. }
  2425. ret = subsys_interface_register(&cpufreq_interface);
  2426. if (ret)
  2427. goto err_boost_unreg;
  2428. if (unlikely(list_empty(&cpufreq_policy_list))) {
  2429. /* if all ->init() calls failed, unregister */
  2430. ret = -ENODEV;
  2431. pr_debug("%s: No CPU initialized for driver %s\n", __func__,
  2432. driver_data->name);
  2433. goto err_if_unreg;
  2434. }
  2435. ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
  2436. "cpufreq:online",
  2437. cpuhp_cpufreq_online,
  2438. cpuhp_cpufreq_offline);
  2439. if (ret < 0)
  2440. goto err_if_unreg;
  2441. hp_online = ret;
  2442. ret = 0;
  2443. pr_debug("driver %s up and running\n", driver_data->name);
  2444. goto out;
  2445. err_if_unreg:
  2446. subsys_interface_unregister(&cpufreq_interface);
  2447. err_boost_unreg:
  2448. if (!cpufreq_driver->setpolicy)
  2449. static_branch_disable_cpuslocked(&cpufreq_freq_invariance);
  2450. remove_boost_sysfs_file();
  2451. err_null_driver:
  2452. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2453. cpufreq_driver = NULL;
  2454. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2455. out:
  2456. cpus_read_unlock();
  2457. return ret;
  2458. }
  2459. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  2460. /*
  2461. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  2462. *
  2463. * Unregister the current CPUFreq driver. Only call this if you have
  2464. * the right to do so, i.e. if you have succeeded in initialising before!
  2465. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  2466. * currently not initialised.
  2467. */
  2468. void cpufreq_unregister_driver(struct cpufreq_driver *driver)
  2469. {
  2470. unsigned long flags;
  2471. if (WARN_ON(!cpufreq_driver || (driver != cpufreq_driver)))
  2472. return;
  2473. pr_debug("unregistering driver %s\n", driver->name);
  2474. /* Protect against concurrent cpu hotplug */
  2475. cpus_read_lock();
  2476. subsys_interface_unregister(&cpufreq_interface);
  2477. remove_boost_sysfs_file();
  2478. static_branch_disable_cpuslocked(&cpufreq_freq_invariance);
  2479. cpuhp_remove_state_nocalls_cpuslocked(hp_online);
  2480. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2481. cpufreq_driver = NULL;
  2482. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2483. cpus_read_unlock();
  2484. }
  2485. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  2486. static int __init cpufreq_core_init(void)
  2487. {
  2488. struct cpufreq_governor *gov = cpufreq_default_governor();
  2489. struct device *dev_root;
  2490. if (cpufreq_disabled())
  2491. return -ENODEV;
  2492. dev_root = bus_get_dev_root(&cpu_subsys);
  2493. if (dev_root) {
  2494. cpufreq_global_kobject = kobject_create_and_add("cpufreq", &dev_root->kobj);
  2495. put_device(dev_root);
  2496. }
  2497. BUG_ON(!cpufreq_global_kobject);
  2498. if (!strlen(default_governor))
  2499. strscpy(default_governor, gov->name, CPUFREQ_NAME_LEN);
  2500. return 0;
  2501. }
  2502. static bool cpufreq_policy_is_good_for_eas(unsigned int cpu)
  2503. {
  2504. struct cpufreq_policy *policy __free(put_cpufreq_policy) = cpufreq_cpu_get(cpu);
  2505. if (!policy) {
  2506. pr_debug("cpufreq policy not set for CPU: %d\n", cpu);
  2507. return false;
  2508. }
  2509. return sugov_is_governor(policy);
  2510. }
  2511. bool cpufreq_ready_for_eas(const struct cpumask *cpu_mask)
  2512. {
  2513. unsigned int cpu;
  2514. /* Do not attempt EAS if schedutil is not being used. */
  2515. for_each_cpu(cpu, cpu_mask) {
  2516. if (!cpufreq_policy_is_good_for_eas(cpu)) {
  2517. pr_debug("rd %*pbl: schedutil is mandatory for EAS\n",
  2518. cpumask_pr_args(cpu_mask));
  2519. return false;
  2520. }
  2521. }
  2522. return true;
  2523. }
  2524. module_param(off, int, 0444);
  2525. module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444);
  2526. core_initcall(cpufreq_core_init);