cpufreq.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541
  1. /*
  2. * linux/drivers/cpufreq/cpufreq.c
  3. *
  4. * Copyright (C) 2001 Russell King
  5. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  6. * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
  7. *
  8. * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
  9. * Added handling for CPU hotplug
  10. * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
  11. * Fix handling for CPU hotplug -- affected CPUs
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/cpu.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/slab.h>
  27. #include <linux/suspend.h>
  28. #include <linux/syscore_ops.h>
  29. #include <linux/tick.h>
  30. #include <trace/events/power.h>
  31. static LIST_HEAD(cpufreq_policy_list);
  32. static inline bool policy_is_inactive(struct cpufreq_policy *policy)
  33. {
  34. return cpumask_empty(policy->cpus);
  35. }
  36. /* Macros to iterate over CPU policies */
  37. #define for_each_suitable_policy(__policy, __active) \
  38. list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
  39. if ((__active) == !policy_is_inactive(__policy))
  40. #define for_each_active_policy(__policy) \
  41. for_each_suitable_policy(__policy, true)
  42. #define for_each_inactive_policy(__policy) \
  43. for_each_suitable_policy(__policy, false)
  44. #define for_each_policy(__policy) \
  45. list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
  46. /* Iterate over governors */
  47. static LIST_HEAD(cpufreq_governor_list);
  48. #define for_each_governor(__governor) \
  49. list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
  50. /**
  51. * The "cpufreq driver" - the arch- or hardware-dependent low
  52. * level driver of CPUFreq support, and its spinlock. This lock
  53. * also protects the cpufreq_cpu_data array.
  54. */
  55. static struct cpufreq_driver *cpufreq_driver;
  56. static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
  57. static DEFINE_RWLOCK(cpufreq_driver_lock);
  58. /* Flag to suspend/resume CPUFreq governors */
  59. static bool cpufreq_suspended;
  60. static inline bool has_target(void)
  61. {
  62. return cpufreq_driver->target_index || cpufreq_driver->target;
  63. }
  64. /* internal prototypes */
  65. static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
  66. static int cpufreq_init_governor(struct cpufreq_policy *policy);
  67. static void cpufreq_exit_governor(struct cpufreq_policy *policy);
  68. static int cpufreq_start_governor(struct cpufreq_policy *policy);
  69. static void cpufreq_stop_governor(struct cpufreq_policy *policy);
  70. static void cpufreq_governor_limits(struct cpufreq_policy *policy);
  71. /**
  72. * Two notifier lists: the "policy" list is involved in the
  73. * validation process for a new CPU frequency policy; the
  74. * "transition" list for kernel code that needs to handle
  75. * changes to devices when the CPU clock speed changes.
  76. * The mutex locks both lists.
  77. */
  78. static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
  79. static struct srcu_notifier_head cpufreq_transition_notifier_list;
  80. static bool init_cpufreq_transition_notifier_list_called;
  81. static int __init init_cpufreq_transition_notifier_list(void)
  82. {
  83. srcu_init_notifier_head(&cpufreq_transition_notifier_list);
  84. init_cpufreq_transition_notifier_list_called = true;
  85. return 0;
  86. }
  87. pure_initcall(init_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. static DEFINE_MUTEX(cpufreq_governor_mutex);
  98. bool have_governor_per_policy(void)
  99. {
  100. return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
  101. }
  102. EXPORT_SYMBOL_GPL(have_governor_per_policy);
  103. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
  104. {
  105. if (have_governor_per_policy())
  106. return &policy->kobj;
  107. else
  108. return cpufreq_global_kobject;
  109. }
  110. EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
  111. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  112. {
  113. u64 idle_time;
  114. u64 cur_wall_time;
  115. u64 busy_time;
  116. cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
  117. busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
  118. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
  119. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
  120. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
  121. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
  122. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
  123. idle_time = cur_wall_time - busy_time;
  124. if (wall)
  125. *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
  126. return div_u64(idle_time, NSEC_PER_USEC);
  127. }
  128. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
  129. {
  130. u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
  131. if (idle_time == -1ULL)
  132. return get_cpu_idle_time_jiffy(cpu, wall);
  133. else if (!io_busy)
  134. idle_time += get_cpu_iowait_time_us(cpu, wall);
  135. return idle_time;
  136. }
  137. EXPORT_SYMBOL_GPL(get_cpu_idle_time);
  138. /*
  139. * This is a generic cpufreq init() routine which can be used by cpufreq
  140. * drivers of SMP systems. It will do following:
  141. * - validate & show freq table passed
  142. * - set policies transition latency
  143. * - policy->cpus with all possible CPUs
  144. */
  145. int cpufreq_generic_init(struct cpufreq_policy *policy,
  146. struct cpufreq_frequency_table *table,
  147. unsigned int transition_latency)
  148. {
  149. int ret;
  150. ret = cpufreq_table_validate_and_show(policy, table);
  151. if (ret) {
  152. pr_err("%s: invalid frequency table: %d\n", __func__, ret);
  153. return ret;
  154. }
  155. policy->cpuinfo.transition_latency = transition_latency;
  156. /*
  157. * The driver only supports the SMP configuration where all processors
  158. * share the clock and voltage and clock.
  159. */
  160. cpumask_setall(policy->cpus);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL_GPL(cpufreq_generic_init);
  164. struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
  165. {
  166. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  167. return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
  168. }
  169. EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
  170. unsigned int cpufreq_generic_get(unsigned int cpu)
  171. {
  172. struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
  173. if (!policy || IS_ERR(policy->clk)) {
  174. pr_err("%s: No %s associated to cpu: %d\n",
  175. __func__, policy ? "clk" : "policy", cpu);
  176. return 0;
  177. }
  178. return clk_get_rate(policy->clk) / 1000;
  179. }
  180. EXPORT_SYMBOL_GPL(cpufreq_generic_get);
  181. /**
  182. * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
  183. *
  184. * @cpu: cpu to find policy for.
  185. *
  186. * This returns policy for 'cpu', returns NULL if it doesn't exist.
  187. * It also increments the kobject reference count to mark it busy and so would
  188. * require a corresponding call to cpufreq_cpu_put() to decrement it back.
  189. * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
  190. * freed as that depends on the kobj count.
  191. *
  192. * Return: A valid policy on success, otherwise NULL on failure.
  193. */
  194. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  195. {
  196. struct cpufreq_policy *policy = NULL;
  197. unsigned long flags;
  198. if (WARN_ON(cpu >= nr_cpu_ids))
  199. return NULL;
  200. /* get the cpufreq driver */
  201. read_lock_irqsave(&cpufreq_driver_lock, flags);
  202. if (cpufreq_driver) {
  203. /* get the CPU */
  204. policy = cpufreq_cpu_get_raw(cpu);
  205. if (policy)
  206. kobject_get(&policy->kobj);
  207. }
  208. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  209. return policy;
  210. }
  211. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  212. /**
  213. * cpufreq_cpu_put: Decrements the usage count of a policy
  214. *
  215. * @policy: policy earlier returned by cpufreq_cpu_get().
  216. *
  217. * This decrements the kobject reference count incremented earlier by calling
  218. * cpufreq_cpu_get().
  219. */
  220. void cpufreq_cpu_put(struct cpufreq_policy *policy)
  221. {
  222. kobject_put(&policy->kobj);
  223. }
  224. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  225. /*********************************************************************
  226. * EXTERNALLY AFFECTING FREQUENCY CHANGES *
  227. *********************************************************************/
  228. /**
  229. * adjust_jiffies - adjust the system "loops_per_jiffy"
  230. *
  231. * This function alters the system "loops_per_jiffy" for the clock
  232. * speed change. Note that loops_per_jiffy cannot be updated on SMP
  233. * systems as each CPU might be scaled differently. So, use the arch
  234. * per-CPU loops_per_jiffy value wherever possible.
  235. */
  236. static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  237. {
  238. #ifndef CONFIG_SMP
  239. static unsigned long l_p_j_ref;
  240. static unsigned int l_p_j_ref_freq;
  241. if (ci->flags & CPUFREQ_CONST_LOOPS)
  242. return;
  243. if (!l_p_j_ref_freq) {
  244. l_p_j_ref = loops_per_jiffy;
  245. l_p_j_ref_freq = ci->old;
  246. pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
  247. l_p_j_ref, l_p_j_ref_freq);
  248. }
  249. if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
  250. loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
  251. ci->new);
  252. pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
  253. loops_per_jiffy, ci->new);
  254. }
  255. #endif
  256. }
  257. static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
  258. struct cpufreq_freqs *freqs, unsigned int state)
  259. {
  260. BUG_ON(irqs_disabled());
  261. if (cpufreq_disabled())
  262. return;
  263. freqs->flags = cpufreq_driver->flags;
  264. pr_debug("notification %u of frequency transition to %u kHz\n",
  265. state, freqs->new);
  266. switch (state) {
  267. case CPUFREQ_PRECHANGE:
  268. /* detect if the driver reported a value as "old frequency"
  269. * which is not equal to what the cpufreq core thinks is
  270. * "old frequency".
  271. */
  272. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  273. if ((policy) && (policy->cpu == freqs->cpu) &&
  274. (policy->cur) && (policy->cur != freqs->old)) {
  275. pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
  276. freqs->old, policy->cur);
  277. freqs->old = policy->cur;
  278. }
  279. }
  280. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  281. CPUFREQ_PRECHANGE, freqs);
  282. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  283. break;
  284. case CPUFREQ_POSTCHANGE:
  285. adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
  286. pr_debug("FREQ: %lu - CPU: %lu\n",
  287. (unsigned long)freqs->new, (unsigned long)freqs->cpu);
  288. trace_cpu_frequency(freqs->new, freqs->cpu);
  289. cpufreq_stats_record_transition(policy, freqs->new);
  290. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  291. CPUFREQ_POSTCHANGE, freqs);
  292. if (likely(policy) && likely(policy->cpu == freqs->cpu))
  293. policy->cur = freqs->new;
  294. break;
  295. }
  296. }
  297. /**
  298. * cpufreq_notify_transition - call notifier chain and adjust_jiffies
  299. * on frequency transition.
  300. *
  301. * This function calls the transition notifiers and the "adjust_jiffies"
  302. * function. It is called twice on all CPU frequency changes that have
  303. * external effects.
  304. */
  305. static void cpufreq_notify_transition(struct cpufreq_policy *policy,
  306. struct cpufreq_freqs *freqs, unsigned int state)
  307. {
  308. for_each_cpu(freqs->cpu, policy->cpus)
  309. __cpufreq_notify_transition(policy, freqs, state);
  310. }
  311. /* Do post notifications when there are chances that transition has failed */
  312. static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
  313. struct cpufreq_freqs *freqs, int transition_failed)
  314. {
  315. cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
  316. if (!transition_failed)
  317. return;
  318. swap(freqs->old, freqs->new);
  319. cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
  320. cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
  321. }
  322. void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
  323. struct cpufreq_freqs *freqs)
  324. {
  325. /*
  326. * Catch double invocations of _begin() which lead to self-deadlock.
  327. * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
  328. * doesn't invoke _begin() on their behalf, and hence the chances of
  329. * double invocations are very low. Moreover, there are scenarios
  330. * where these checks can emit false-positive warnings in these
  331. * drivers; so we avoid that by skipping them altogether.
  332. */
  333. WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
  334. && current == policy->transition_task);
  335. wait:
  336. wait_event(policy->transition_wait, !policy->transition_ongoing);
  337. spin_lock(&policy->transition_lock);
  338. if (unlikely(policy->transition_ongoing)) {
  339. spin_unlock(&policy->transition_lock);
  340. goto wait;
  341. }
  342. policy->transition_ongoing = true;
  343. policy->transition_task = current;
  344. spin_unlock(&policy->transition_lock);
  345. cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
  346. }
  347. EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
  348. void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
  349. struct cpufreq_freqs *freqs, int transition_failed)
  350. {
  351. if (unlikely(WARN_ON(!policy->transition_ongoing)))
  352. return;
  353. cpufreq_notify_post_transition(policy, freqs, transition_failed);
  354. policy->transition_ongoing = false;
  355. policy->transition_task = NULL;
  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("%pF\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. /**
  418. * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
  419. * one.
  420. * @target_freq: target frequency to resolve.
  421. *
  422. * The target to driver frequency mapping is cached in the policy.
  423. *
  424. * Return: Lowest driver-supported frequency greater than or equal to the
  425. * given target_freq, subject to policy (min/max) and driver limitations.
  426. */
  427. unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
  428. unsigned int target_freq)
  429. {
  430. target_freq = clamp_val(target_freq, policy->min, policy->max);
  431. policy->cached_target_freq = target_freq;
  432. if (cpufreq_driver->target_index) {
  433. int idx;
  434. idx = cpufreq_frequency_table_target(policy, target_freq,
  435. CPUFREQ_RELATION_L);
  436. policy->cached_resolved_idx = idx;
  437. return policy->freq_table[idx].frequency;
  438. }
  439. if (cpufreq_driver->resolve_freq)
  440. return cpufreq_driver->resolve_freq(policy, target_freq);
  441. return target_freq;
  442. }
  443. EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
  444. /*********************************************************************
  445. * SYSFS INTERFACE *
  446. *********************************************************************/
  447. static ssize_t show_boost(struct kobject *kobj,
  448. struct attribute *attr, char *buf)
  449. {
  450. return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
  451. }
  452. static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
  453. const char *buf, size_t count)
  454. {
  455. int ret, enable;
  456. ret = sscanf(buf, "%d", &enable);
  457. if (ret != 1 || enable < 0 || enable > 1)
  458. return -EINVAL;
  459. if (cpufreq_boost_trigger_state(enable)) {
  460. pr_err("%s: Cannot %s BOOST!\n",
  461. __func__, enable ? "enable" : "disable");
  462. return -EINVAL;
  463. }
  464. pr_debug("%s: cpufreq BOOST %s\n",
  465. __func__, enable ? "enabled" : "disabled");
  466. return count;
  467. }
  468. define_one_global_rw(boost);
  469. static struct cpufreq_governor *find_governor(const char *str_governor)
  470. {
  471. struct cpufreq_governor *t;
  472. for_each_governor(t)
  473. if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
  474. return t;
  475. return NULL;
  476. }
  477. /**
  478. * cpufreq_parse_governor - parse a governor string
  479. */
  480. static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
  481. struct cpufreq_governor **governor)
  482. {
  483. int err = -EINVAL;
  484. if (cpufreq_driver->setpolicy) {
  485. if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
  486. *policy = CPUFREQ_POLICY_PERFORMANCE;
  487. err = 0;
  488. } else if (!strncasecmp(str_governor, "powersave",
  489. CPUFREQ_NAME_LEN)) {
  490. *policy = CPUFREQ_POLICY_POWERSAVE;
  491. err = 0;
  492. }
  493. } else {
  494. struct cpufreq_governor *t;
  495. mutex_lock(&cpufreq_governor_mutex);
  496. t = find_governor(str_governor);
  497. if (t == NULL) {
  498. int ret;
  499. mutex_unlock(&cpufreq_governor_mutex);
  500. ret = request_module("cpufreq_%s", str_governor);
  501. mutex_lock(&cpufreq_governor_mutex);
  502. if (ret == 0)
  503. t = find_governor(str_governor);
  504. }
  505. if (t != NULL) {
  506. *governor = t;
  507. err = 0;
  508. }
  509. mutex_unlock(&cpufreq_governor_mutex);
  510. }
  511. return err;
  512. }
  513. /**
  514. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  515. * print out cpufreq information
  516. *
  517. * Write out information from cpufreq_driver->policy[cpu]; object must be
  518. * "unsigned int".
  519. */
  520. #define show_one(file_name, object) \
  521. static ssize_t show_##file_name \
  522. (struct cpufreq_policy *policy, char *buf) \
  523. { \
  524. return sprintf(buf, "%u\n", policy->object); \
  525. }
  526. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  527. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  528. show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
  529. show_one(scaling_min_freq, min);
  530. show_one(scaling_max_freq, max);
  531. static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
  532. {
  533. ssize_t ret;
  534. if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
  535. ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
  536. else
  537. ret = sprintf(buf, "%u\n", policy->cur);
  538. return ret;
  539. }
  540. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  541. struct cpufreq_policy *new_policy);
  542. /**
  543. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  544. */
  545. #define store_one(file_name, object) \
  546. static ssize_t store_##file_name \
  547. (struct cpufreq_policy *policy, const char *buf, size_t count) \
  548. { \
  549. int ret, temp; \
  550. struct cpufreq_policy new_policy; \
  551. \
  552. memcpy(&new_policy, policy, sizeof(*policy)); \
  553. \
  554. ret = sscanf(buf, "%u", &new_policy.object); \
  555. if (ret != 1) \
  556. return -EINVAL; \
  557. \
  558. temp = new_policy.object; \
  559. ret = cpufreq_set_policy(policy, &new_policy); \
  560. if (!ret) \
  561. policy->user_policy.object = temp; \
  562. \
  563. return ret ? ret : count; \
  564. }
  565. store_one(scaling_min_freq, min);
  566. store_one(scaling_max_freq, max);
  567. /**
  568. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  569. */
  570. static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
  571. char *buf)
  572. {
  573. unsigned int cur_freq = __cpufreq_get(policy);
  574. if (cur_freq)
  575. return sprintf(buf, "%u\n", cur_freq);
  576. return sprintf(buf, "<unknown>\n");
  577. }
  578. /**
  579. * show_scaling_governor - show the current policy for the specified CPU
  580. */
  581. static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
  582. {
  583. if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
  584. return sprintf(buf, "powersave\n");
  585. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  586. return sprintf(buf, "performance\n");
  587. else if (policy->governor)
  588. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
  589. policy->governor->name);
  590. return -EINVAL;
  591. }
  592. /**
  593. * store_scaling_governor - store policy for the specified CPU
  594. */
  595. static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
  596. const char *buf, size_t count)
  597. {
  598. int ret;
  599. char str_governor[16];
  600. struct cpufreq_policy new_policy;
  601. memcpy(&new_policy, policy, sizeof(*policy));
  602. ret = sscanf(buf, "%15s", str_governor);
  603. if (ret != 1)
  604. return -EINVAL;
  605. if (cpufreq_parse_governor(str_governor, &new_policy.policy,
  606. &new_policy.governor))
  607. return -EINVAL;
  608. ret = cpufreq_set_policy(policy, &new_policy);
  609. return ret ? ret : count;
  610. }
  611. /**
  612. * show_scaling_driver - show the cpufreq driver currently loaded
  613. */
  614. static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
  615. {
  616. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
  617. }
  618. /**
  619. * show_scaling_available_governors - show the available CPUfreq governors
  620. */
  621. static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
  622. char *buf)
  623. {
  624. ssize_t i = 0;
  625. struct cpufreq_governor *t;
  626. if (!has_target()) {
  627. i += sprintf(buf, "performance powersave");
  628. goto out;
  629. }
  630. for_each_governor(t) {
  631. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
  632. - (CPUFREQ_NAME_LEN + 2)))
  633. goto out;
  634. i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
  635. }
  636. out:
  637. i += sprintf(&buf[i], "\n");
  638. return i;
  639. }
  640. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
  641. {
  642. ssize_t i = 0;
  643. unsigned int cpu;
  644. for_each_cpu(cpu, mask) {
  645. if (i)
  646. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
  647. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
  648. if (i >= (PAGE_SIZE - 5))
  649. break;
  650. }
  651. i += sprintf(&buf[i], "\n");
  652. return i;
  653. }
  654. EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
  655. /**
  656. * show_related_cpus - show the CPUs affected by each transition even if
  657. * hw coordination is in use
  658. */
  659. static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
  660. {
  661. return cpufreq_show_cpus(policy->related_cpus, buf);
  662. }
  663. /**
  664. * show_affected_cpus - show the CPUs affected by each transition
  665. */
  666. static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
  667. {
  668. return cpufreq_show_cpus(policy->cpus, buf);
  669. }
  670. static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
  671. const char *buf, size_t count)
  672. {
  673. unsigned int freq = 0;
  674. unsigned int ret;
  675. if (!policy->governor || !policy->governor->store_setspeed)
  676. return -EINVAL;
  677. ret = sscanf(buf, "%u", &freq);
  678. if (ret != 1)
  679. return -EINVAL;
  680. policy->governor->store_setspeed(policy, freq);
  681. return count;
  682. }
  683. static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
  684. {
  685. if (!policy->governor || !policy->governor->show_setspeed)
  686. return sprintf(buf, "<unsupported>\n");
  687. return policy->governor->show_setspeed(policy, buf);
  688. }
  689. /**
  690. * show_bios_limit - show the current cpufreq HW/BIOS limitation
  691. */
  692. static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
  693. {
  694. unsigned int limit;
  695. int ret;
  696. if (cpufreq_driver->bios_limit) {
  697. ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
  698. if (!ret)
  699. return sprintf(buf, "%u\n", limit);
  700. }
  701. return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
  702. }
  703. cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
  704. cpufreq_freq_attr_ro(cpuinfo_min_freq);
  705. cpufreq_freq_attr_ro(cpuinfo_max_freq);
  706. cpufreq_freq_attr_ro(cpuinfo_transition_latency);
  707. cpufreq_freq_attr_ro(scaling_available_governors);
  708. cpufreq_freq_attr_ro(scaling_driver);
  709. cpufreq_freq_attr_ro(scaling_cur_freq);
  710. cpufreq_freq_attr_ro(bios_limit);
  711. cpufreq_freq_attr_ro(related_cpus);
  712. cpufreq_freq_attr_ro(affected_cpus);
  713. cpufreq_freq_attr_rw(scaling_min_freq);
  714. cpufreq_freq_attr_rw(scaling_max_freq);
  715. cpufreq_freq_attr_rw(scaling_governor);
  716. cpufreq_freq_attr_rw(scaling_setspeed);
  717. static struct attribute *default_attrs[] = {
  718. &cpuinfo_min_freq.attr,
  719. &cpuinfo_max_freq.attr,
  720. &cpuinfo_transition_latency.attr,
  721. &scaling_min_freq.attr,
  722. &scaling_max_freq.attr,
  723. &affected_cpus.attr,
  724. &related_cpus.attr,
  725. &scaling_governor.attr,
  726. &scaling_driver.attr,
  727. &scaling_available_governors.attr,
  728. &scaling_setspeed.attr,
  729. NULL
  730. };
  731. #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
  732. #define to_attr(a) container_of(a, struct freq_attr, attr)
  733. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  734. {
  735. struct cpufreq_policy *policy = to_policy(kobj);
  736. struct freq_attr *fattr = to_attr(attr);
  737. ssize_t ret;
  738. down_read(&policy->rwsem);
  739. ret = fattr->show(policy, buf);
  740. up_read(&policy->rwsem);
  741. return ret;
  742. }
  743. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  744. const char *buf, size_t count)
  745. {
  746. struct cpufreq_policy *policy = to_policy(kobj);
  747. struct freq_attr *fattr = to_attr(attr);
  748. ssize_t ret = -EINVAL;
  749. get_online_cpus();
  750. if (cpu_online(policy->cpu)) {
  751. down_write(&policy->rwsem);
  752. ret = fattr->store(policy, buf, count);
  753. up_write(&policy->rwsem);
  754. }
  755. put_online_cpus();
  756. return ret;
  757. }
  758. static void cpufreq_sysfs_release(struct kobject *kobj)
  759. {
  760. struct cpufreq_policy *policy = to_policy(kobj);
  761. pr_debug("last reference is dropped\n");
  762. complete(&policy->kobj_unregister);
  763. }
  764. static const struct sysfs_ops sysfs_ops = {
  765. .show = show,
  766. .store = store,
  767. };
  768. static struct kobj_type ktype_cpufreq = {
  769. .sysfs_ops = &sysfs_ops,
  770. .default_attrs = default_attrs,
  771. .release = cpufreq_sysfs_release,
  772. };
  773. static int add_cpu_dev_symlink(struct cpufreq_policy *policy,
  774. struct device *dev)
  775. {
  776. dev_dbg(dev, "%s: Adding symlink\n", __func__);
  777. return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
  778. }
  779. static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
  780. struct device *dev)
  781. {
  782. dev_dbg(dev, "%s: Removing symlink\n", __func__);
  783. sysfs_remove_link(&dev->kobj, "cpufreq");
  784. }
  785. static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
  786. {
  787. struct freq_attr **drv_attr;
  788. int ret = 0;
  789. /* set up files for this cpu device */
  790. drv_attr = cpufreq_driver->attr;
  791. while (drv_attr && *drv_attr) {
  792. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  793. if (ret)
  794. return ret;
  795. drv_attr++;
  796. }
  797. if (cpufreq_driver->get) {
  798. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  799. if (ret)
  800. return ret;
  801. }
  802. ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  803. if (ret)
  804. return ret;
  805. if (cpufreq_driver->bios_limit) {
  806. ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
  807. if (ret)
  808. return ret;
  809. }
  810. return 0;
  811. }
  812. __weak struct cpufreq_governor *cpufreq_default_governor(void)
  813. {
  814. return NULL;
  815. }
  816. static int cpufreq_init_policy(struct cpufreq_policy *policy)
  817. {
  818. struct cpufreq_governor *gov = NULL;
  819. struct cpufreq_policy new_policy;
  820. memcpy(&new_policy, policy, sizeof(*policy));
  821. /* Update governor of new_policy to the governor used before hotplug */
  822. gov = find_governor(policy->last_governor);
  823. if (gov) {
  824. pr_debug("Restoring governor %s for cpu %d\n",
  825. policy->governor->name, policy->cpu);
  826. } else {
  827. gov = cpufreq_default_governor();
  828. if (!gov)
  829. return -ENODATA;
  830. }
  831. new_policy.governor = gov;
  832. /* Use the default policy if there is no last_policy. */
  833. if (cpufreq_driver->setpolicy) {
  834. if (policy->last_policy)
  835. new_policy.policy = policy->last_policy;
  836. else
  837. cpufreq_parse_governor(gov->name, &new_policy.policy,
  838. NULL);
  839. }
  840. /* set default policy */
  841. return cpufreq_set_policy(policy, &new_policy);
  842. }
  843. static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
  844. {
  845. int ret = 0;
  846. /* Has this CPU been taken care of already? */
  847. if (cpumask_test_cpu(cpu, policy->cpus))
  848. return 0;
  849. down_write(&policy->rwsem);
  850. if (has_target())
  851. cpufreq_stop_governor(policy);
  852. cpumask_set_cpu(cpu, policy->cpus);
  853. if (has_target()) {
  854. ret = cpufreq_start_governor(policy);
  855. if (ret)
  856. pr_err("%s: Failed to start governor\n", __func__);
  857. }
  858. up_write(&policy->rwsem);
  859. return ret;
  860. }
  861. static void handle_update(struct work_struct *work)
  862. {
  863. struct cpufreq_policy *policy =
  864. container_of(work, struct cpufreq_policy, update);
  865. unsigned int cpu = policy->cpu;
  866. pr_debug("handle_update for cpu %u called\n", cpu);
  867. cpufreq_update_policy(cpu);
  868. }
  869. static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
  870. {
  871. struct cpufreq_policy *policy;
  872. int ret;
  873. policy = kzalloc(sizeof(*policy), GFP_KERNEL);
  874. if (!policy)
  875. return NULL;
  876. if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
  877. goto err_free_policy;
  878. if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
  879. goto err_free_cpumask;
  880. if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
  881. goto err_free_rcpumask;
  882. ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
  883. cpufreq_global_kobject, "policy%u", cpu);
  884. if (ret) {
  885. pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
  886. goto err_free_real_cpus;
  887. }
  888. INIT_LIST_HEAD(&policy->policy_list);
  889. init_rwsem(&policy->rwsem);
  890. spin_lock_init(&policy->transition_lock);
  891. init_waitqueue_head(&policy->transition_wait);
  892. init_completion(&policy->kobj_unregister);
  893. INIT_WORK(&policy->update, handle_update);
  894. policy->cpu = cpu;
  895. return policy;
  896. err_free_real_cpus:
  897. free_cpumask_var(policy->real_cpus);
  898. err_free_rcpumask:
  899. free_cpumask_var(policy->related_cpus);
  900. err_free_cpumask:
  901. free_cpumask_var(policy->cpus);
  902. err_free_policy:
  903. kfree(policy);
  904. return NULL;
  905. }
  906. static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
  907. {
  908. struct kobject *kobj;
  909. struct completion *cmp;
  910. down_write(&policy->rwsem);
  911. cpufreq_stats_free_table(policy);
  912. kobj = &policy->kobj;
  913. cmp = &policy->kobj_unregister;
  914. up_write(&policy->rwsem);
  915. kobject_put(kobj);
  916. /*
  917. * We need to make sure that the underlying kobj is
  918. * actually not referenced anymore by anybody before we
  919. * proceed with unloading.
  920. */
  921. pr_debug("waiting for dropping of refcount\n");
  922. wait_for_completion(cmp);
  923. pr_debug("wait complete\n");
  924. }
  925. static void cpufreq_policy_free(struct cpufreq_policy *policy)
  926. {
  927. unsigned long flags;
  928. int cpu;
  929. /* Remove policy from list */
  930. write_lock_irqsave(&cpufreq_driver_lock, flags);
  931. list_del(&policy->policy_list);
  932. for_each_cpu(cpu, policy->related_cpus)
  933. per_cpu(cpufreq_cpu_data, cpu) = NULL;
  934. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  935. cpufreq_policy_put_kobj(policy);
  936. free_cpumask_var(policy->real_cpus);
  937. free_cpumask_var(policy->related_cpus);
  938. free_cpumask_var(policy->cpus);
  939. kfree(policy);
  940. }
  941. static int cpufreq_online(unsigned int cpu)
  942. {
  943. struct cpufreq_policy *policy;
  944. bool new_policy;
  945. unsigned long flags;
  946. unsigned int j;
  947. int ret;
  948. pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
  949. /* Check if this CPU already has a policy to manage it */
  950. policy = per_cpu(cpufreq_cpu_data, cpu);
  951. if (policy) {
  952. WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
  953. if (!policy_is_inactive(policy))
  954. return cpufreq_add_policy_cpu(policy, cpu);
  955. /* This is the only online CPU for the policy. Start over. */
  956. new_policy = false;
  957. down_write(&policy->rwsem);
  958. policy->cpu = cpu;
  959. policy->governor = NULL;
  960. up_write(&policy->rwsem);
  961. } else {
  962. new_policy = true;
  963. policy = cpufreq_policy_alloc(cpu);
  964. if (!policy)
  965. return -ENOMEM;
  966. }
  967. cpumask_copy(policy->cpus, cpumask_of(cpu));
  968. /* call driver. From then on the cpufreq must be able
  969. * to accept all calls to ->verify and ->setpolicy for this CPU
  970. */
  971. ret = cpufreq_driver->init(policy);
  972. if (ret) {
  973. pr_debug("initialization failed\n");
  974. goto out_free_policy;
  975. }
  976. down_write(&policy->rwsem);
  977. if (new_policy) {
  978. /* related_cpus should at least include policy->cpus. */
  979. cpumask_copy(policy->related_cpus, policy->cpus);
  980. }
  981. /*
  982. * affected cpus must always be the one, which are online. We aren't
  983. * managing offline cpus here.
  984. */
  985. cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
  986. if (new_policy) {
  987. policy->user_policy.min = policy->min;
  988. policy->user_policy.max = policy->max;
  989. write_lock_irqsave(&cpufreq_driver_lock, flags);
  990. for_each_cpu(j, policy->related_cpus)
  991. per_cpu(cpufreq_cpu_data, j) = policy;
  992. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  993. } else {
  994. policy->min = policy->user_policy.min;
  995. policy->max = policy->user_policy.max;
  996. }
  997. if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
  998. policy->cur = cpufreq_driver->get(policy->cpu);
  999. if (!policy->cur) {
  1000. pr_err("%s: ->get() failed\n", __func__);
  1001. goto out_exit_policy;
  1002. }
  1003. }
  1004. /*
  1005. * Sometimes boot loaders set CPU frequency to a value outside of
  1006. * frequency table present with cpufreq core. In such cases CPU might be
  1007. * unstable if it has to run on that frequency for long duration of time
  1008. * and so its better to set it to a frequency which is specified in
  1009. * freq-table. This also makes cpufreq stats inconsistent as
  1010. * cpufreq-stats would fail to register because current frequency of CPU
  1011. * isn't found in freq-table.
  1012. *
  1013. * Because we don't want this change to effect boot process badly, we go
  1014. * for the next freq which is >= policy->cur ('cur' must be set by now,
  1015. * otherwise we will end up setting freq to lowest of the table as 'cur'
  1016. * is initialized to zero).
  1017. *
  1018. * We are passing target-freq as "policy->cur - 1" otherwise
  1019. * __cpufreq_driver_target() would simply fail, as policy->cur will be
  1020. * equal to target-freq.
  1021. */
  1022. if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
  1023. && has_target()) {
  1024. /* Are we running at unknown frequency ? */
  1025. ret = cpufreq_frequency_table_get_index(policy, policy->cur);
  1026. if (ret == -EINVAL) {
  1027. /* Warn user and fix it */
  1028. pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
  1029. __func__, policy->cpu, policy->cur);
  1030. ret = __cpufreq_driver_target(policy, policy->cur - 1,
  1031. CPUFREQ_RELATION_L);
  1032. /*
  1033. * Reaching here after boot in a few seconds may not
  1034. * mean that system will remain stable at "unknown"
  1035. * frequency for longer duration. Hence, a BUG_ON().
  1036. */
  1037. BUG_ON(ret);
  1038. pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
  1039. __func__, policy->cpu, policy->cur);
  1040. }
  1041. }
  1042. if (new_policy) {
  1043. ret = cpufreq_add_dev_interface(policy);
  1044. if (ret)
  1045. goto out_exit_policy;
  1046. cpufreq_stats_create_table(policy);
  1047. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1048. list_add(&policy->policy_list, &cpufreq_policy_list);
  1049. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1050. }
  1051. ret = cpufreq_init_policy(policy);
  1052. if (ret) {
  1053. pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
  1054. __func__, cpu, ret);
  1055. /* cpufreq_policy_free() will notify based on this */
  1056. new_policy = false;
  1057. goto out_exit_policy;
  1058. }
  1059. up_write(&policy->rwsem);
  1060. kobject_uevent(&policy->kobj, KOBJ_ADD);
  1061. /* Callback for handling stuff after policy is ready */
  1062. if (cpufreq_driver->ready)
  1063. cpufreq_driver->ready(policy);
  1064. pr_debug("initialization complete\n");
  1065. return 0;
  1066. out_exit_policy:
  1067. up_write(&policy->rwsem);
  1068. if (cpufreq_driver->exit)
  1069. cpufreq_driver->exit(policy);
  1070. out_free_policy:
  1071. cpufreq_policy_free(policy);
  1072. return ret;
  1073. }
  1074. static int cpufreq_offline(unsigned int cpu);
  1075. /**
  1076. * cpufreq_add_dev - the cpufreq interface for a CPU device.
  1077. * @dev: CPU device.
  1078. * @sif: Subsystem interface structure pointer (not used)
  1079. */
  1080. static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
  1081. {
  1082. struct cpufreq_policy *policy;
  1083. unsigned cpu = dev->id;
  1084. int ret;
  1085. dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
  1086. if (cpu_online(cpu)) {
  1087. ret = cpufreq_online(cpu);
  1088. if (ret)
  1089. return ret;
  1090. }
  1091. /* Create sysfs link on CPU registration */
  1092. policy = per_cpu(cpufreq_cpu_data, cpu);
  1093. if (!policy || cpumask_test_and_set_cpu(cpu, policy->real_cpus))
  1094. return 0;
  1095. ret = add_cpu_dev_symlink(policy, dev);
  1096. if (ret) {
  1097. cpumask_clear_cpu(cpu, policy->real_cpus);
  1098. cpufreq_offline(cpu);
  1099. }
  1100. return ret;
  1101. }
  1102. static int cpufreq_offline(unsigned int cpu)
  1103. {
  1104. struct cpufreq_policy *policy;
  1105. int ret;
  1106. pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
  1107. policy = cpufreq_cpu_get_raw(cpu);
  1108. if (!policy) {
  1109. pr_debug("%s: No cpu_data found\n", __func__);
  1110. return 0;
  1111. }
  1112. down_write(&policy->rwsem);
  1113. if (has_target())
  1114. cpufreq_stop_governor(policy);
  1115. cpumask_clear_cpu(cpu, policy->cpus);
  1116. if (policy_is_inactive(policy)) {
  1117. if (has_target())
  1118. strncpy(policy->last_governor, policy->governor->name,
  1119. CPUFREQ_NAME_LEN);
  1120. else
  1121. policy->last_policy = policy->policy;
  1122. } else if (cpu == policy->cpu) {
  1123. /* Nominate new CPU */
  1124. policy->cpu = cpumask_any(policy->cpus);
  1125. }
  1126. /* Start governor again for active policy */
  1127. if (!policy_is_inactive(policy)) {
  1128. if (has_target()) {
  1129. ret = cpufreq_start_governor(policy);
  1130. if (ret)
  1131. pr_err("%s: Failed to start governor\n", __func__);
  1132. }
  1133. goto unlock;
  1134. }
  1135. if (cpufreq_driver->stop_cpu)
  1136. cpufreq_driver->stop_cpu(policy);
  1137. if (has_target())
  1138. cpufreq_exit_governor(policy);
  1139. /*
  1140. * Perform the ->exit() even during light-weight tear-down,
  1141. * since this is a core component, and is essential for the
  1142. * subsequent light-weight ->init() to succeed.
  1143. */
  1144. if (cpufreq_driver->exit) {
  1145. cpufreq_driver->exit(policy);
  1146. policy->freq_table = NULL;
  1147. }
  1148. unlock:
  1149. up_write(&policy->rwsem);
  1150. return 0;
  1151. }
  1152. /**
  1153. * cpufreq_remove_dev - remove a CPU device
  1154. *
  1155. * Removes the cpufreq interface for a CPU device.
  1156. */
  1157. static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  1158. {
  1159. unsigned int cpu = dev->id;
  1160. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1161. if (!policy)
  1162. return;
  1163. if (cpu_online(cpu))
  1164. cpufreq_offline(cpu);
  1165. cpumask_clear_cpu(cpu, policy->real_cpus);
  1166. remove_cpu_dev_symlink(policy, dev);
  1167. if (cpumask_empty(policy->real_cpus))
  1168. cpufreq_policy_free(policy);
  1169. }
  1170. /**
  1171. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
  1172. * in deep trouble.
  1173. * @policy: policy managing CPUs
  1174. * @new_freq: CPU frequency the CPU actually runs at
  1175. *
  1176. * We adjust to current frequency first, and need to clean up later.
  1177. * So either call to cpufreq_update_policy() or schedule handle_update()).
  1178. */
  1179. static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
  1180. unsigned int new_freq)
  1181. {
  1182. struct cpufreq_freqs freqs;
  1183. pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
  1184. policy->cur, new_freq);
  1185. freqs.old = policy->cur;
  1186. freqs.new = new_freq;
  1187. cpufreq_freq_transition_begin(policy, &freqs);
  1188. cpufreq_freq_transition_end(policy, &freqs, 0);
  1189. }
  1190. /**
  1191. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  1192. * @cpu: CPU number
  1193. *
  1194. * This is the last known freq, without actually getting it from the driver.
  1195. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  1196. */
  1197. unsigned int cpufreq_quick_get(unsigned int cpu)
  1198. {
  1199. struct cpufreq_policy *policy;
  1200. unsigned int ret_freq = 0;
  1201. unsigned long flags;
  1202. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1203. if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
  1204. ret_freq = cpufreq_driver->get(cpu);
  1205. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1206. return ret_freq;
  1207. }
  1208. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1209. policy = cpufreq_cpu_get(cpu);
  1210. if (policy) {
  1211. ret_freq = policy->cur;
  1212. cpufreq_cpu_put(policy);
  1213. }
  1214. return ret_freq;
  1215. }
  1216. EXPORT_SYMBOL(cpufreq_quick_get);
  1217. /**
  1218. * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
  1219. * @cpu: CPU number
  1220. *
  1221. * Just return the max possible frequency for a given CPU.
  1222. */
  1223. unsigned int cpufreq_quick_get_max(unsigned int cpu)
  1224. {
  1225. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1226. unsigned int ret_freq = 0;
  1227. if (policy) {
  1228. ret_freq = policy->max;
  1229. cpufreq_cpu_put(policy);
  1230. }
  1231. return ret_freq;
  1232. }
  1233. EXPORT_SYMBOL(cpufreq_quick_get_max);
  1234. static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
  1235. {
  1236. unsigned int ret_freq = 0;
  1237. if (!cpufreq_driver->get)
  1238. return ret_freq;
  1239. ret_freq = cpufreq_driver->get(policy->cpu);
  1240. /*
  1241. * Updating inactive policies is invalid, so avoid doing that. Also
  1242. * if fast frequency switching is used with the given policy, the check
  1243. * against policy->cur is pointless, so skip it in that case too.
  1244. */
  1245. if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
  1246. return ret_freq;
  1247. if (ret_freq && policy->cur &&
  1248. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1249. /* verify no discrepancy between actual and
  1250. saved value exists */
  1251. if (unlikely(ret_freq != policy->cur)) {
  1252. cpufreq_out_of_sync(policy, ret_freq);
  1253. schedule_work(&policy->update);
  1254. }
  1255. }
  1256. return ret_freq;
  1257. }
  1258. /**
  1259. * cpufreq_get - get the current CPU frequency (in kHz)
  1260. * @cpu: CPU number
  1261. *
  1262. * Get the CPU current (static) CPU frequency
  1263. */
  1264. unsigned int cpufreq_get(unsigned int cpu)
  1265. {
  1266. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1267. unsigned int ret_freq = 0;
  1268. if (policy) {
  1269. down_read(&policy->rwsem);
  1270. if (!policy_is_inactive(policy))
  1271. ret_freq = __cpufreq_get(policy);
  1272. up_read(&policy->rwsem);
  1273. cpufreq_cpu_put(policy);
  1274. }
  1275. return ret_freq;
  1276. }
  1277. EXPORT_SYMBOL(cpufreq_get);
  1278. static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
  1279. {
  1280. unsigned int new_freq;
  1281. new_freq = cpufreq_driver->get(policy->cpu);
  1282. if (!new_freq)
  1283. return 0;
  1284. if (!policy->cur) {
  1285. pr_debug("cpufreq: Driver did not initialize current freq\n");
  1286. policy->cur = new_freq;
  1287. } else if (policy->cur != new_freq && has_target()) {
  1288. cpufreq_out_of_sync(policy, new_freq);
  1289. }
  1290. return new_freq;
  1291. }
  1292. static struct subsys_interface cpufreq_interface = {
  1293. .name = "cpufreq",
  1294. .subsys = &cpu_subsys,
  1295. .add_dev = cpufreq_add_dev,
  1296. .remove_dev = cpufreq_remove_dev,
  1297. };
  1298. /*
  1299. * In case platform wants some specific frequency to be configured
  1300. * during suspend..
  1301. */
  1302. int cpufreq_generic_suspend(struct cpufreq_policy *policy)
  1303. {
  1304. int ret;
  1305. if (!policy->suspend_freq) {
  1306. pr_debug("%s: suspend_freq not defined\n", __func__);
  1307. return 0;
  1308. }
  1309. pr_debug("%s: Setting suspend-freq: %u\n", __func__,
  1310. policy->suspend_freq);
  1311. ret = __cpufreq_driver_target(policy, policy->suspend_freq,
  1312. CPUFREQ_RELATION_H);
  1313. if (ret)
  1314. pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
  1315. __func__, policy->suspend_freq, ret);
  1316. return ret;
  1317. }
  1318. EXPORT_SYMBOL(cpufreq_generic_suspend);
  1319. /**
  1320. * cpufreq_suspend() - Suspend CPUFreq governors
  1321. *
  1322. * Called during system wide Suspend/Hibernate cycles for suspending governors
  1323. * as some platforms can't change frequency after this point in suspend cycle.
  1324. * Because some of the devices (like: i2c, regulators, etc) they use for
  1325. * changing frequency are suspended quickly after this point.
  1326. */
  1327. void cpufreq_suspend(void)
  1328. {
  1329. struct cpufreq_policy *policy;
  1330. if (!cpufreq_driver)
  1331. return;
  1332. if (!has_target() && !cpufreq_driver->suspend)
  1333. goto suspend;
  1334. pr_debug("%s: Suspending Governors\n", __func__);
  1335. for_each_active_policy(policy) {
  1336. if (has_target()) {
  1337. down_write(&policy->rwsem);
  1338. cpufreq_stop_governor(policy);
  1339. up_write(&policy->rwsem);
  1340. }
  1341. if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
  1342. pr_err("%s: Failed to suspend driver: %p\n", __func__,
  1343. policy);
  1344. }
  1345. suspend:
  1346. cpufreq_suspended = true;
  1347. }
  1348. /**
  1349. * cpufreq_resume() - Resume CPUFreq governors
  1350. *
  1351. * Called during system wide Suspend/Hibernate cycle for resuming governors that
  1352. * are suspended with cpufreq_suspend().
  1353. */
  1354. void cpufreq_resume(void)
  1355. {
  1356. struct cpufreq_policy *policy;
  1357. int ret;
  1358. if (!cpufreq_driver)
  1359. return;
  1360. cpufreq_suspended = false;
  1361. if (!has_target() && !cpufreq_driver->resume)
  1362. return;
  1363. pr_debug("%s: Resuming Governors\n", __func__);
  1364. for_each_active_policy(policy) {
  1365. if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
  1366. pr_err("%s: Failed to resume driver: %p\n", __func__,
  1367. policy);
  1368. } else if (has_target()) {
  1369. down_write(&policy->rwsem);
  1370. ret = cpufreq_start_governor(policy);
  1371. up_write(&policy->rwsem);
  1372. if (ret)
  1373. pr_err("%s: Failed to start governor for policy: %p\n",
  1374. __func__, policy);
  1375. }
  1376. }
  1377. }
  1378. /**
  1379. * cpufreq_get_current_driver - return current driver's name
  1380. *
  1381. * Return the name string of the currently loaded cpufreq driver
  1382. * or NULL, if none.
  1383. */
  1384. const char *cpufreq_get_current_driver(void)
  1385. {
  1386. if (cpufreq_driver)
  1387. return cpufreq_driver->name;
  1388. return NULL;
  1389. }
  1390. EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
  1391. /**
  1392. * cpufreq_get_driver_data - return current driver data
  1393. *
  1394. * Return the private data of the currently loaded cpufreq
  1395. * driver, or NULL if no cpufreq driver is loaded.
  1396. */
  1397. void *cpufreq_get_driver_data(void)
  1398. {
  1399. if (cpufreq_driver)
  1400. return cpufreq_driver->driver_data;
  1401. return NULL;
  1402. }
  1403. EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
  1404. /*********************************************************************
  1405. * NOTIFIER LISTS INTERFACE *
  1406. *********************************************************************/
  1407. /**
  1408. * cpufreq_register_notifier - register a driver with cpufreq
  1409. * @nb: notifier function to register
  1410. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1411. *
  1412. * Add a driver to one of two lists: either a list of drivers that
  1413. * are notified about clock rate changes (once before and once after
  1414. * the transition), or a list of drivers that are notified about
  1415. * changes in cpufreq policy.
  1416. *
  1417. * This function may sleep, and has the same return conditions as
  1418. * blocking_notifier_chain_register.
  1419. */
  1420. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1421. {
  1422. int ret;
  1423. if (cpufreq_disabled())
  1424. return -EINVAL;
  1425. WARN_ON(!init_cpufreq_transition_notifier_list_called);
  1426. switch (list) {
  1427. case CPUFREQ_TRANSITION_NOTIFIER:
  1428. mutex_lock(&cpufreq_fast_switch_lock);
  1429. if (cpufreq_fast_switch_count > 0) {
  1430. mutex_unlock(&cpufreq_fast_switch_lock);
  1431. return -EBUSY;
  1432. }
  1433. ret = srcu_notifier_chain_register(
  1434. &cpufreq_transition_notifier_list, nb);
  1435. if (!ret)
  1436. cpufreq_fast_switch_count--;
  1437. mutex_unlock(&cpufreq_fast_switch_lock);
  1438. break;
  1439. case CPUFREQ_POLICY_NOTIFIER:
  1440. ret = blocking_notifier_chain_register(
  1441. &cpufreq_policy_notifier_list, nb);
  1442. break;
  1443. default:
  1444. ret = -EINVAL;
  1445. }
  1446. return ret;
  1447. }
  1448. EXPORT_SYMBOL(cpufreq_register_notifier);
  1449. /**
  1450. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1451. * @nb: notifier block to be unregistered
  1452. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1453. *
  1454. * Remove a driver from the CPU frequency notifier list.
  1455. *
  1456. * This function may sleep, and has the same return conditions as
  1457. * blocking_notifier_chain_unregister.
  1458. */
  1459. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1460. {
  1461. int ret;
  1462. if (cpufreq_disabled())
  1463. return -EINVAL;
  1464. switch (list) {
  1465. case CPUFREQ_TRANSITION_NOTIFIER:
  1466. mutex_lock(&cpufreq_fast_switch_lock);
  1467. ret = srcu_notifier_chain_unregister(
  1468. &cpufreq_transition_notifier_list, nb);
  1469. if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
  1470. cpufreq_fast_switch_count++;
  1471. mutex_unlock(&cpufreq_fast_switch_lock);
  1472. break;
  1473. case CPUFREQ_POLICY_NOTIFIER:
  1474. ret = blocking_notifier_chain_unregister(
  1475. &cpufreq_policy_notifier_list, nb);
  1476. break;
  1477. default:
  1478. ret = -EINVAL;
  1479. }
  1480. return ret;
  1481. }
  1482. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1483. /*********************************************************************
  1484. * GOVERNORS *
  1485. *********************************************************************/
  1486. /**
  1487. * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
  1488. * @policy: cpufreq policy to switch the frequency for.
  1489. * @target_freq: New frequency to set (may be approximate).
  1490. *
  1491. * Carry out a fast frequency switch without sleeping.
  1492. *
  1493. * The driver's ->fast_switch() callback invoked by this function must be
  1494. * suitable for being called from within RCU-sched read-side critical sections
  1495. * and it is expected to select the minimum available frequency greater than or
  1496. * equal to @target_freq (CPUFREQ_RELATION_L).
  1497. *
  1498. * This function must not be called if policy->fast_switch_enabled is unset.
  1499. *
  1500. * Governors calling this function must guarantee that it will never be invoked
  1501. * twice in parallel for the same policy and that it will never be called in
  1502. * parallel with either ->target() or ->target_index() for the same policy.
  1503. *
  1504. * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
  1505. * callback to indicate an error condition, the hardware configuration must be
  1506. * preserved.
  1507. */
  1508. unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
  1509. unsigned int target_freq)
  1510. {
  1511. target_freq = clamp_val(target_freq, policy->min, policy->max);
  1512. return cpufreq_driver->fast_switch(policy, target_freq);
  1513. }
  1514. EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
  1515. /* Must set freqs->new to intermediate frequency */
  1516. static int __target_intermediate(struct cpufreq_policy *policy,
  1517. struct cpufreq_freqs *freqs, int index)
  1518. {
  1519. int ret;
  1520. freqs->new = cpufreq_driver->get_intermediate(policy, index);
  1521. /* We don't need to switch to intermediate freq */
  1522. if (!freqs->new)
  1523. return 0;
  1524. pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
  1525. __func__, policy->cpu, freqs->old, freqs->new);
  1526. cpufreq_freq_transition_begin(policy, freqs);
  1527. ret = cpufreq_driver->target_intermediate(policy, index);
  1528. cpufreq_freq_transition_end(policy, freqs, ret);
  1529. if (ret)
  1530. pr_err("%s: Failed to change to intermediate frequency: %d\n",
  1531. __func__, ret);
  1532. return ret;
  1533. }
  1534. static int __target_index(struct cpufreq_policy *policy, int index)
  1535. {
  1536. struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
  1537. unsigned int intermediate_freq = 0;
  1538. unsigned int newfreq = policy->freq_table[index].frequency;
  1539. int retval = -EINVAL;
  1540. bool notify;
  1541. if (newfreq == policy->cur)
  1542. return 0;
  1543. notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
  1544. if (notify) {
  1545. /* Handle switching to intermediate frequency */
  1546. if (cpufreq_driver->get_intermediate) {
  1547. retval = __target_intermediate(policy, &freqs, index);
  1548. if (retval)
  1549. return retval;
  1550. intermediate_freq = freqs.new;
  1551. /* Set old freq to intermediate */
  1552. if (intermediate_freq)
  1553. freqs.old = freqs.new;
  1554. }
  1555. freqs.new = newfreq;
  1556. pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
  1557. __func__, policy->cpu, freqs.old, freqs.new);
  1558. cpufreq_freq_transition_begin(policy, &freqs);
  1559. }
  1560. retval = cpufreq_driver->target_index(policy, index);
  1561. if (retval)
  1562. pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
  1563. retval);
  1564. if (notify) {
  1565. cpufreq_freq_transition_end(policy, &freqs, retval);
  1566. /*
  1567. * Failed after setting to intermediate freq? Driver should have
  1568. * reverted back to initial frequency and so should we. Check
  1569. * here for intermediate_freq instead of get_intermediate, in
  1570. * case we haven't switched to intermediate freq at all.
  1571. */
  1572. if (unlikely(retval && intermediate_freq)) {
  1573. freqs.old = intermediate_freq;
  1574. freqs.new = policy->restore_freq;
  1575. cpufreq_freq_transition_begin(policy, &freqs);
  1576. cpufreq_freq_transition_end(policy, &freqs, 0);
  1577. }
  1578. }
  1579. return retval;
  1580. }
  1581. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1582. unsigned int target_freq,
  1583. unsigned int relation)
  1584. {
  1585. unsigned int old_target_freq = target_freq;
  1586. int index;
  1587. if (cpufreq_disabled())
  1588. return -ENODEV;
  1589. /* Make sure that target_freq is within supported range */
  1590. target_freq = clamp_val(target_freq, policy->min, policy->max);
  1591. pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
  1592. policy->cpu, target_freq, relation, old_target_freq);
  1593. /*
  1594. * This might look like a redundant call as we are checking it again
  1595. * after finding index. But it is left intentionally for cases where
  1596. * exactly same freq is called again and so we can save on few function
  1597. * calls.
  1598. */
  1599. if (target_freq == policy->cur)
  1600. return 0;
  1601. /* Save last value to restore later on errors */
  1602. policy->restore_freq = policy->cur;
  1603. if (cpufreq_driver->target)
  1604. return cpufreq_driver->target(policy, target_freq, relation);
  1605. if (!cpufreq_driver->target_index)
  1606. return -EINVAL;
  1607. index = cpufreq_frequency_table_target(policy, target_freq, relation);
  1608. return __target_index(policy, index);
  1609. }
  1610. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1611. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1612. unsigned int target_freq,
  1613. unsigned int relation)
  1614. {
  1615. int ret = -EINVAL;
  1616. down_write(&policy->rwsem);
  1617. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1618. up_write(&policy->rwsem);
  1619. return ret;
  1620. }
  1621. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1622. __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
  1623. {
  1624. return NULL;
  1625. }
  1626. static int cpufreq_init_governor(struct cpufreq_policy *policy)
  1627. {
  1628. int ret;
  1629. /* Don't start any governor operations if we are entering suspend */
  1630. if (cpufreq_suspended)
  1631. return 0;
  1632. /*
  1633. * Governor might not be initiated here if ACPI _PPC changed
  1634. * notification happened, so check it.
  1635. */
  1636. if (!policy->governor)
  1637. return -EINVAL;
  1638. if (policy->governor->max_transition_latency &&
  1639. policy->cpuinfo.transition_latency >
  1640. policy->governor->max_transition_latency) {
  1641. struct cpufreq_governor *gov = cpufreq_fallback_governor();
  1642. if (gov) {
  1643. pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
  1644. policy->governor->name, gov->name);
  1645. policy->governor = gov;
  1646. } else {
  1647. return -EINVAL;
  1648. }
  1649. }
  1650. if (!try_module_get(policy->governor->owner))
  1651. return -EINVAL;
  1652. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1653. if (policy->governor->init) {
  1654. ret = policy->governor->init(policy);
  1655. if (ret) {
  1656. module_put(policy->governor->owner);
  1657. return ret;
  1658. }
  1659. }
  1660. return 0;
  1661. }
  1662. static void cpufreq_exit_governor(struct cpufreq_policy *policy)
  1663. {
  1664. if (cpufreq_suspended || !policy->governor)
  1665. return;
  1666. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1667. if (policy->governor->exit)
  1668. policy->governor->exit(policy);
  1669. module_put(policy->governor->owner);
  1670. }
  1671. static int cpufreq_start_governor(struct cpufreq_policy *policy)
  1672. {
  1673. int ret;
  1674. if (cpufreq_suspended)
  1675. return 0;
  1676. if (!policy->governor)
  1677. return -EINVAL;
  1678. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1679. if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
  1680. cpufreq_update_current_freq(policy);
  1681. if (policy->governor->start) {
  1682. ret = policy->governor->start(policy);
  1683. if (ret)
  1684. return ret;
  1685. }
  1686. if (policy->governor->limits)
  1687. policy->governor->limits(policy);
  1688. return 0;
  1689. }
  1690. static void cpufreq_stop_governor(struct cpufreq_policy *policy)
  1691. {
  1692. if (cpufreq_suspended || !policy->governor)
  1693. return;
  1694. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1695. if (policy->governor->stop)
  1696. policy->governor->stop(policy);
  1697. }
  1698. static void cpufreq_governor_limits(struct cpufreq_policy *policy)
  1699. {
  1700. if (cpufreq_suspended || !policy->governor)
  1701. return;
  1702. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1703. if (policy->governor->limits)
  1704. policy->governor->limits(policy);
  1705. }
  1706. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1707. {
  1708. int err;
  1709. if (!governor)
  1710. return -EINVAL;
  1711. if (cpufreq_disabled())
  1712. return -ENODEV;
  1713. mutex_lock(&cpufreq_governor_mutex);
  1714. err = -EBUSY;
  1715. if (!find_governor(governor->name)) {
  1716. err = 0;
  1717. list_add(&governor->governor_list, &cpufreq_governor_list);
  1718. }
  1719. mutex_unlock(&cpufreq_governor_mutex);
  1720. return err;
  1721. }
  1722. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1723. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1724. {
  1725. struct cpufreq_policy *policy;
  1726. unsigned long flags;
  1727. if (!governor)
  1728. return;
  1729. if (cpufreq_disabled())
  1730. return;
  1731. /* clear last_governor for all inactive policies */
  1732. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1733. for_each_inactive_policy(policy) {
  1734. if (!strcmp(policy->last_governor, governor->name)) {
  1735. policy->governor = NULL;
  1736. strcpy(policy->last_governor, "\0");
  1737. }
  1738. }
  1739. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1740. mutex_lock(&cpufreq_governor_mutex);
  1741. list_del(&governor->governor_list);
  1742. mutex_unlock(&cpufreq_governor_mutex);
  1743. return;
  1744. }
  1745. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1746. /*********************************************************************
  1747. * POLICY INTERFACE *
  1748. *********************************************************************/
  1749. /**
  1750. * cpufreq_get_policy - get the current cpufreq_policy
  1751. * @policy: struct cpufreq_policy into which the current cpufreq_policy
  1752. * is written
  1753. *
  1754. * Reads the current cpufreq policy.
  1755. */
  1756. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1757. {
  1758. struct cpufreq_policy *cpu_policy;
  1759. if (!policy)
  1760. return -EINVAL;
  1761. cpu_policy = cpufreq_cpu_get(cpu);
  1762. if (!cpu_policy)
  1763. return -EINVAL;
  1764. memcpy(policy, cpu_policy, sizeof(*policy));
  1765. cpufreq_cpu_put(cpu_policy);
  1766. return 0;
  1767. }
  1768. EXPORT_SYMBOL(cpufreq_get_policy);
  1769. /*
  1770. * policy : current policy.
  1771. * new_policy: policy to be set.
  1772. */
  1773. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  1774. struct cpufreq_policy *new_policy)
  1775. {
  1776. struct cpufreq_governor *old_gov;
  1777. int ret;
  1778. pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
  1779. new_policy->cpu, new_policy->min, new_policy->max);
  1780. memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
  1781. /*
  1782. * This check works well when we store new min/max freq attributes,
  1783. * because new_policy is a copy of policy with one field updated.
  1784. */
  1785. if (new_policy->min > new_policy->max)
  1786. return -EINVAL;
  1787. /* verify the cpu speed can be set within this limit */
  1788. ret = cpufreq_driver->verify(new_policy);
  1789. if (ret)
  1790. return ret;
  1791. /* adjust if necessary - all reasons */
  1792. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1793. CPUFREQ_ADJUST, new_policy);
  1794. /*
  1795. * verify the cpu speed can be set within this limit, which might be
  1796. * different to the first one
  1797. */
  1798. ret = cpufreq_driver->verify(new_policy);
  1799. if (ret)
  1800. return ret;
  1801. /* notification of the new policy */
  1802. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1803. CPUFREQ_NOTIFY, new_policy);
  1804. policy->min = new_policy->min;
  1805. policy->max = new_policy->max;
  1806. policy->cached_target_freq = UINT_MAX;
  1807. pr_debug("new min and max freqs are %u - %u kHz\n",
  1808. policy->min, policy->max);
  1809. if (cpufreq_driver->setpolicy) {
  1810. policy->policy = new_policy->policy;
  1811. pr_debug("setting range\n");
  1812. return cpufreq_driver->setpolicy(new_policy);
  1813. }
  1814. if (new_policy->governor == policy->governor) {
  1815. pr_debug("cpufreq: governor limits update\n");
  1816. cpufreq_governor_limits(policy);
  1817. return 0;
  1818. }
  1819. pr_debug("governor switch\n");
  1820. /* save old, working values */
  1821. old_gov = policy->governor;
  1822. /* end old governor */
  1823. if (old_gov) {
  1824. cpufreq_stop_governor(policy);
  1825. cpufreq_exit_governor(policy);
  1826. }
  1827. /* start new governor */
  1828. policy->governor = new_policy->governor;
  1829. ret = cpufreq_init_governor(policy);
  1830. if (!ret) {
  1831. ret = cpufreq_start_governor(policy);
  1832. if (!ret) {
  1833. pr_debug("cpufreq: governor change\n");
  1834. return 0;
  1835. }
  1836. cpufreq_exit_governor(policy);
  1837. }
  1838. /* new governor failed, so re-start old one */
  1839. pr_debug("starting governor %s failed\n", policy->governor->name);
  1840. if (old_gov) {
  1841. policy->governor = old_gov;
  1842. if (cpufreq_init_governor(policy))
  1843. policy->governor = NULL;
  1844. else
  1845. cpufreq_start_governor(policy);
  1846. }
  1847. return ret;
  1848. }
  1849. /**
  1850. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1851. * @cpu: CPU which shall be re-evaluated
  1852. *
  1853. * Useful for policy notifiers which have different necessities
  1854. * at different times.
  1855. */
  1856. void cpufreq_update_policy(unsigned int cpu)
  1857. {
  1858. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1859. struct cpufreq_policy new_policy;
  1860. if (!policy)
  1861. return;
  1862. down_write(&policy->rwsem);
  1863. if (policy_is_inactive(policy))
  1864. goto unlock;
  1865. pr_debug("updating policy for CPU %u\n", cpu);
  1866. memcpy(&new_policy, policy, sizeof(*policy));
  1867. new_policy.min = policy->user_policy.min;
  1868. new_policy.max = policy->user_policy.max;
  1869. /*
  1870. * BIOS might change freq behind our back
  1871. * -> ask driver for current freq and notify governors about a change
  1872. */
  1873. if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
  1874. if (cpufreq_suspended)
  1875. goto unlock;
  1876. new_policy.cur = cpufreq_update_current_freq(policy);
  1877. if (WARN_ON(!new_policy.cur))
  1878. goto unlock;
  1879. }
  1880. cpufreq_set_policy(policy, &new_policy);
  1881. unlock:
  1882. up_write(&policy->rwsem);
  1883. cpufreq_cpu_put(policy);
  1884. }
  1885. EXPORT_SYMBOL(cpufreq_update_policy);
  1886. /*********************************************************************
  1887. * BOOST *
  1888. *********************************************************************/
  1889. static int cpufreq_boost_set_sw(int state)
  1890. {
  1891. struct cpufreq_policy *policy;
  1892. int ret = -EINVAL;
  1893. for_each_active_policy(policy) {
  1894. if (!policy->freq_table)
  1895. continue;
  1896. ret = cpufreq_frequency_table_cpuinfo(policy,
  1897. policy->freq_table);
  1898. if (ret) {
  1899. pr_err("%s: Policy frequency update failed\n",
  1900. __func__);
  1901. break;
  1902. }
  1903. down_write(&policy->rwsem);
  1904. policy->user_policy.max = policy->max;
  1905. cpufreq_governor_limits(policy);
  1906. up_write(&policy->rwsem);
  1907. }
  1908. return ret;
  1909. }
  1910. int cpufreq_boost_trigger_state(int state)
  1911. {
  1912. unsigned long flags;
  1913. int ret = 0;
  1914. if (cpufreq_driver->boost_enabled == state)
  1915. return 0;
  1916. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1917. cpufreq_driver->boost_enabled = state;
  1918. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1919. ret = cpufreq_driver->set_boost(state);
  1920. if (ret) {
  1921. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1922. cpufreq_driver->boost_enabled = !state;
  1923. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1924. pr_err("%s: Cannot %s BOOST\n",
  1925. __func__, state ? "enable" : "disable");
  1926. }
  1927. return ret;
  1928. }
  1929. static bool cpufreq_boost_supported(void)
  1930. {
  1931. return likely(cpufreq_driver) && cpufreq_driver->set_boost;
  1932. }
  1933. static int create_boost_sysfs_file(void)
  1934. {
  1935. int ret;
  1936. ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
  1937. if (ret)
  1938. pr_err("%s: cannot register global BOOST sysfs file\n",
  1939. __func__);
  1940. return ret;
  1941. }
  1942. static void remove_boost_sysfs_file(void)
  1943. {
  1944. if (cpufreq_boost_supported())
  1945. sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
  1946. }
  1947. int cpufreq_enable_boost_support(void)
  1948. {
  1949. if (!cpufreq_driver)
  1950. return -EINVAL;
  1951. if (cpufreq_boost_supported())
  1952. return 0;
  1953. cpufreq_driver->set_boost = cpufreq_boost_set_sw;
  1954. /* This will get removed on driver unregister */
  1955. return create_boost_sysfs_file();
  1956. }
  1957. EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
  1958. int cpufreq_boost_enabled(void)
  1959. {
  1960. return cpufreq_driver->boost_enabled;
  1961. }
  1962. EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
  1963. /*********************************************************************
  1964. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1965. *********************************************************************/
  1966. static enum cpuhp_state hp_online;
  1967. /**
  1968. * cpufreq_register_driver - register a CPU Frequency driver
  1969. * @driver_data: A struct cpufreq_driver containing the values#
  1970. * submitted by the CPU Frequency driver.
  1971. *
  1972. * Registers a CPU Frequency driver to this core code. This code
  1973. * returns zero on success, -EEXIST when another driver got here first
  1974. * (and isn't unregistered in the meantime).
  1975. *
  1976. */
  1977. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  1978. {
  1979. unsigned long flags;
  1980. int ret;
  1981. if (cpufreq_disabled())
  1982. return -ENODEV;
  1983. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1984. !(driver_data->setpolicy || driver_data->target_index ||
  1985. driver_data->target) ||
  1986. (driver_data->setpolicy && (driver_data->target_index ||
  1987. driver_data->target)) ||
  1988. (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
  1989. return -EINVAL;
  1990. pr_debug("trying to register driver %s\n", driver_data->name);
  1991. /* Protect against concurrent CPU online/offline. */
  1992. get_online_cpus();
  1993. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1994. if (cpufreq_driver) {
  1995. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1996. ret = -EEXIST;
  1997. goto out;
  1998. }
  1999. cpufreq_driver = driver_data;
  2000. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2001. if (driver_data->setpolicy)
  2002. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  2003. if (cpufreq_boost_supported()) {
  2004. ret = create_boost_sysfs_file();
  2005. if (ret)
  2006. goto err_null_driver;
  2007. }
  2008. ret = subsys_interface_register(&cpufreq_interface);
  2009. if (ret)
  2010. goto err_boost_unreg;
  2011. if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
  2012. list_empty(&cpufreq_policy_list)) {
  2013. /* if all ->init() calls failed, unregister */
  2014. pr_debug("%s: No CPU initialized for driver %s\n", __func__,
  2015. driver_data->name);
  2016. goto err_if_unreg;
  2017. }
  2018. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "cpufreq:online",
  2019. cpufreq_online,
  2020. cpufreq_offline);
  2021. if (ret < 0)
  2022. goto err_if_unreg;
  2023. hp_online = ret;
  2024. ret = 0;
  2025. pr_debug("driver %s up and running\n", driver_data->name);
  2026. goto out;
  2027. err_if_unreg:
  2028. subsys_interface_unregister(&cpufreq_interface);
  2029. err_boost_unreg:
  2030. remove_boost_sysfs_file();
  2031. err_null_driver:
  2032. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2033. cpufreq_driver = NULL;
  2034. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2035. out:
  2036. put_online_cpus();
  2037. return ret;
  2038. }
  2039. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  2040. /**
  2041. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  2042. *
  2043. * Unregister the current CPUFreq driver. Only call this if you have
  2044. * the right to do so, i.e. if you have succeeded in initialising before!
  2045. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  2046. * currently not initialised.
  2047. */
  2048. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  2049. {
  2050. unsigned long flags;
  2051. if (!cpufreq_driver || (driver != cpufreq_driver))
  2052. return -EINVAL;
  2053. pr_debug("unregistering driver %s\n", driver->name);
  2054. /* Protect against concurrent cpu hotplug */
  2055. get_online_cpus();
  2056. subsys_interface_unregister(&cpufreq_interface);
  2057. remove_boost_sysfs_file();
  2058. cpuhp_remove_state_nocalls(hp_online);
  2059. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2060. cpufreq_driver = NULL;
  2061. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2062. put_online_cpus();
  2063. return 0;
  2064. }
  2065. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  2066. /*
  2067. * Stop cpufreq at shutdown to make sure it isn't holding any locks
  2068. * or mutexes when secondary CPUs are halted.
  2069. */
  2070. static struct syscore_ops cpufreq_syscore_ops = {
  2071. .shutdown = cpufreq_suspend,
  2072. };
  2073. struct kobject *cpufreq_global_kobject;
  2074. EXPORT_SYMBOL(cpufreq_global_kobject);
  2075. static int __init cpufreq_core_init(void)
  2076. {
  2077. if (cpufreq_disabled())
  2078. return -ENODEV;
  2079. cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
  2080. BUG_ON(!cpufreq_global_kobject);
  2081. register_syscore_ops(&cpufreq_syscore_ops);
  2082. return 0;
  2083. }
  2084. module_param(off, int, 0444);
  2085. core_initcall(cpufreq_core_init);