cpufreq.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  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_cputime64(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 = cputime_to_usecs(cur_wall_time);
  126. return cputime_to_usecs(idle_time);
  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, bool notify)
  907. {
  908. struct kobject *kobj;
  909. struct completion *cmp;
  910. if (notify)
  911. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  912. CPUFREQ_REMOVE_POLICY, policy);
  913. down_write(&policy->rwsem);
  914. cpufreq_stats_free_table(policy);
  915. kobj = &policy->kobj;
  916. cmp = &policy->kobj_unregister;
  917. up_write(&policy->rwsem);
  918. kobject_put(kobj);
  919. /*
  920. * We need to make sure that the underlying kobj is
  921. * actually not referenced anymore by anybody before we
  922. * proceed with unloading.
  923. */
  924. pr_debug("waiting for dropping of refcount\n");
  925. wait_for_completion(cmp);
  926. pr_debug("wait complete\n");
  927. }
  928. static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
  929. {
  930. unsigned long flags;
  931. int cpu;
  932. /* Remove policy from list */
  933. write_lock_irqsave(&cpufreq_driver_lock, flags);
  934. list_del(&policy->policy_list);
  935. for_each_cpu(cpu, policy->related_cpus)
  936. per_cpu(cpufreq_cpu_data, cpu) = NULL;
  937. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  938. cpufreq_policy_put_kobj(policy, notify);
  939. free_cpumask_var(policy->real_cpus);
  940. free_cpumask_var(policy->related_cpus);
  941. free_cpumask_var(policy->cpus);
  942. kfree(policy);
  943. }
  944. static int cpufreq_online(unsigned int cpu)
  945. {
  946. struct cpufreq_policy *policy;
  947. bool new_policy;
  948. unsigned long flags;
  949. unsigned int j;
  950. int ret;
  951. pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
  952. /* Check if this CPU already has a policy to manage it */
  953. policy = per_cpu(cpufreq_cpu_data, cpu);
  954. if (policy) {
  955. WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
  956. if (!policy_is_inactive(policy))
  957. return cpufreq_add_policy_cpu(policy, cpu);
  958. /* This is the only online CPU for the policy. Start over. */
  959. new_policy = false;
  960. down_write(&policy->rwsem);
  961. policy->cpu = cpu;
  962. policy->governor = NULL;
  963. up_write(&policy->rwsem);
  964. } else {
  965. new_policy = true;
  966. policy = cpufreq_policy_alloc(cpu);
  967. if (!policy)
  968. return -ENOMEM;
  969. }
  970. cpumask_copy(policy->cpus, cpumask_of(cpu));
  971. /* call driver. From then on the cpufreq must be able
  972. * to accept all calls to ->verify and ->setpolicy for this CPU
  973. */
  974. ret = cpufreq_driver->init(policy);
  975. if (ret) {
  976. pr_debug("initialization failed\n");
  977. goto out_free_policy;
  978. }
  979. down_write(&policy->rwsem);
  980. if (new_policy) {
  981. /* related_cpus should at least include policy->cpus. */
  982. cpumask_copy(policy->related_cpus, policy->cpus);
  983. /* Clear mask of registered CPUs */
  984. cpumask_clear(policy->real_cpus);
  985. }
  986. /*
  987. * affected cpus must always be the one, which are online. We aren't
  988. * managing offline cpus here.
  989. */
  990. cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
  991. if (new_policy) {
  992. policy->user_policy.min = policy->min;
  993. policy->user_policy.max = policy->max;
  994. write_lock_irqsave(&cpufreq_driver_lock, flags);
  995. for_each_cpu(j, policy->related_cpus)
  996. per_cpu(cpufreq_cpu_data, j) = policy;
  997. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  998. } else {
  999. policy->min = policy->user_policy.min;
  1000. policy->max = policy->user_policy.max;
  1001. }
  1002. if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
  1003. policy->cur = cpufreq_driver->get(policy->cpu);
  1004. if (!policy->cur) {
  1005. pr_err("%s: ->get() failed\n", __func__);
  1006. goto out_exit_policy;
  1007. }
  1008. }
  1009. /*
  1010. * Sometimes boot loaders set CPU frequency to a value outside of
  1011. * frequency table present with cpufreq core. In such cases CPU might be
  1012. * unstable if it has to run on that frequency for long duration of time
  1013. * and so its better to set it to a frequency which is specified in
  1014. * freq-table. This also makes cpufreq stats inconsistent as
  1015. * cpufreq-stats would fail to register because current frequency of CPU
  1016. * isn't found in freq-table.
  1017. *
  1018. * Because we don't want this change to effect boot process badly, we go
  1019. * for the next freq which is >= policy->cur ('cur' must be set by now,
  1020. * otherwise we will end up setting freq to lowest of the table as 'cur'
  1021. * is initialized to zero).
  1022. *
  1023. * We are passing target-freq as "policy->cur - 1" otherwise
  1024. * __cpufreq_driver_target() would simply fail, as policy->cur will be
  1025. * equal to target-freq.
  1026. */
  1027. if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
  1028. && has_target()) {
  1029. /* Are we running at unknown frequency ? */
  1030. ret = cpufreq_frequency_table_get_index(policy, policy->cur);
  1031. if (ret == -EINVAL) {
  1032. /* Warn user and fix it */
  1033. pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
  1034. __func__, policy->cpu, policy->cur);
  1035. ret = __cpufreq_driver_target(policy, policy->cur - 1,
  1036. CPUFREQ_RELATION_L);
  1037. /*
  1038. * Reaching here after boot in a few seconds may not
  1039. * mean that system will remain stable at "unknown"
  1040. * frequency for longer duration. Hence, a BUG_ON().
  1041. */
  1042. BUG_ON(ret);
  1043. pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
  1044. __func__, policy->cpu, policy->cur);
  1045. }
  1046. }
  1047. if (new_policy) {
  1048. ret = cpufreq_add_dev_interface(policy);
  1049. if (ret)
  1050. goto out_exit_policy;
  1051. cpufreq_stats_create_table(policy);
  1052. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1053. CPUFREQ_CREATE_POLICY, policy);
  1054. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1055. list_add(&policy->policy_list, &cpufreq_policy_list);
  1056. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1057. }
  1058. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1059. CPUFREQ_START, policy);
  1060. ret = cpufreq_init_policy(policy);
  1061. if (ret) {
  1062. pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
  1063. __func__, cpu, ret);
  1064. /* cpufreq_policy_free() will notify based on this */
  1065. new_policy = false;
  1066. goto out_exit_policy;
  1067. }
  1068. up_write(&policy->rwsem);
  1069. kobject_uevent(&policy->kobj, KOBJ_ADD);
  1070. /* Callback for handling stuff after policy is ready */
  1071. if (cpufreq_driver->ready)
  1072. cpufreq_driver->ready(policy);
  1073. pr_debug("initialization complete\n");
  1074. return 0;
  1075. out_exit_policy:
  1076. up_write(&policy->rwsem);
  1077. if (cpufreq_driver->exit)
  1078. cpufreq_driver->exit(policy);
  1079. out_free_policy:
  1080. cpufreq_policy_free(policy, !new_policy);
  1081. return ret;
  1082. }
  1083. static int cpufreq_offline(unsigned int cpu);
  1084. /**
  1085. * cpufreq_add_dev - the cpufreq interface for a CPU device.
  1086. * @dev: CPU device.
  1087. * @sif: Subsystem interface structure pointer (not used)
  1088. */
  1089. static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
  1090. {
  1091. struct cpufreq_policy *policy;
  1092. unsigned cpu = dev->id;
  1093. int ret;
  1094. dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
  1095. if (cpu_online(cpu)) {
  1096. ret = cpufreq_online(cpu);
  1097. if (ret)
  1098. return ret;
  1099. }
  1100. /* Create sysfs link on CPU registration */
  1101. policy = per_cpu(cpufreq_cpu_data, cpu);
  1102. if (!policy || cpumask_test_and_set_cpu(cpu, policy->real_cpus))
  1103. return 0;
  1104. ret = add_cpu_dev_symlink(policy, dev);
  1105. if (ret) {
  1106. cpumask_clear_cpu(cpu, policy->real_cpus);
  1107. cpufreq_offline(cpu);
  1108. }
  1109. return ret;
  1110. }
  1111. static int cpufreq_offline(unsigned int cpu)
  1112. {
  1113. struct cpufreq_policy *policy;
  1114. int ret;
  1115. pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
  1116. policy = cpufreq_cpu_get_raw(cpu);
  1117. if (!policy) {
  1118. pr_debug("%s: No cpu_data found\n", __func__);
  1119. return 0;
  1120. }
  1121. down_write(&policy->rwsem);
  1122. if (has_target())
  1123. cpufreq_stop_governor(policy);
  1124. cpumask_clear_cpu(cpu, policy->cpus);
  1125. if (policy_is_inactive(policy)) {
  1126. if (has_target())
  1127. strncpy(policy->last_governor, policy->governor->name,
  1128. CPUFREQ_NAME_LEN);
  1129. else
  1130. policy->last_policy = policy->policy;
  1131. } else if (cpu == policy->cpu) {
  1132. /* Nominate new CPU */
  1133. policy->cpu = cpumask_any(policy->cpus);
  1134. }
  1135. /* Start governor again for active policy */
  1136. if (!policy_is_inactive(policy)) {
  1137. if (has_target()) {
  1138. ret = cpufreq_start_governor(policy);
  1139. if (ret)
  1140. pr_err("%s: Failed to start governor\n", __func__);
  1141. }
  1142. goto unlock;
  1143. }
  1144. if (cpufreq_driver->stop_cpu)
  1145. cpufreq_driver->stop_cpu(policy);
  1146. if (has_target())
  1147. cpufreq_exit_governor(policy);
  1148. /*
  1149. * Perform the ->exit() even during light-weight tear-down,
  1150. * since this is a core component, and is essential for the
  1151. * subsequent light-weight ->init() to succeed.
  1152. */
  1153. if (cpufreq_driver->exit) {
  1154. cpufreq_driver->exit(policy);
  1155. policy->freq_table = NULL;
  1156. }
  1157. unlock:
  1158. up_write(&policy->rwsem);
  1159. return 0;
  1160. }
  1161. /**
  1162. * cpufreq_remove_dev - remove a CPU device
  1163. *
  1164. * Removes the cpufreq interface for a CPU device.
  1165. */
  1166. static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  1167. {
  1168. unsigned int cpu = dev->id;
  1169. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1170. if (!policy)
  1171. return;
  1172. if (cpu_online(cpu))
  1173. cpufreq_offline(cpu);
  1174. cpumask_clear_cpu(cpu, policy->real_cpus);
  1175. remove_cpu_dev_symlink(policy, dev);
  1176. if (cpumask_empty(policy->real_cpus))
  1177. cpufreq_policy_free(policy, true);
  1178. }
  1179. /**
  1180. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
  1181. * in deep trouble.
  1182. * @policy: policy managing CPUs
  1183. * @new_freq: CPU frequency the CPU actually runs at
  1184. *
  1185. * We adjust to current frequency first, and need to clean up later.
  1186. * So either call to cpufreq_update_policy() or schedule handle_update()).
  1187. */
  1188. static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
  1189. unsigned int new_freq)
  1190. {
  1191. struct cpufreq_freqs freqs;
  1192. pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
  1193. policy->cur, new_freq);
  1194. freqs.old = policy->cur;
  1195. freqs.new = new_freq;
  1196. cpufreq_freq_transition_begin(policy, &freqs);
  1197. cpufreq_freq_transition_end(policy, &freqs, 0);
  1198. }
  1199. /**
  1200. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  1201. * @cpu: CPU number
  1202. *
  1203. * This is the last known freq, without actually getting it from the driver.
  1204. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  1205. */
  1206. unsigned int cpufreq_quick_get(unsigned int cpu)
  1207. {
  1208. struct cpufreq_policy *policy;
  1209. unsigned int ret_freq = 0;
  1210. unsigned long flags;
  1211. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1212. if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
  1213. ret_freq = cpufreq_driver->get(cpu);
  1214. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1215. return ret_freq;
  1216. }
  1217. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1218. policy = cpufreq_cpu_get(cpu);
  1219. if (policy) {
  1220. ret_freq = policy->cur;
  1221. cpufreq_cpu_put(policy);
  1222. }
  1223. return ret_freq;
  1224. }
  1225. EXPORT_SYMBOL(cpufreq_quick_get);
  1226. /**
  1227. * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
  1228. * @cpu: CPU number
  1229. *
  1230. * Just return the max possible frequency for a given CPU.
  1231. */
  1232. unsigned int cpufreq_quick_get_max(unsigned int cpu)
  1233. {
  1234. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1235. unsigned int ret_freq = 0;
  1236. if (policy) {
  1237. ret_freq = policy->max;
  1238. cpufreq_cpu_put(policy);
  1239. }
  1240. return ret_freq;
  1241. }
  1242. EXPORT_SYMBOL(cpufreq_quick_get_max);
  1243. static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
  1244. {
  1245. unsigned int ret_freq = 0;
  1246. if (!cpufreq_driver->get)
  1247. return ret_freq;
  1248. ret_freq = cpufreq_driver->get(policy->cpu);
  1249. /*
  1250. * Updating inactive policies is invalid, so avoid doing that. Also
  1251. * if fast frequency switching is used with the given policy, the check
  1252. * against policy->cur is pointless, so skip it in that case too.
  1253. */
  1254. if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
  1255. return ret_freq;
  1256. if (ret_freq && policy->cur &&
  1257. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1258. /* verify no discrepancy between actual and
  1259. saved value exists */
  1260. if (unlikely(ret_freq != policy->cur)) {
  1261. cpufreq_out_of_sync(policy, ret_freq);
  1262. schedule_work(&policy->update);
  1263. }
  1264. }
  1265. return ret_freq;
  1266. }
  1267. /**
  1268. * cpufreq_get - get the current CPU frequency (in kHz)
  1269. * @cpu: CPU number
  1270. *
  1271. * Get the CPU current (static) CPU frequency
  1272. */
  1273. unsigned int cpufreq_get(unsigned int cpu)
  1274. {
  1275. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1276. unsigned int ret_freq = 0;
  1277. if (policy) {
  1278. down_read(&policy->rwsem);
  1279. ret_freq = __cpufreq_get(policy);
  1280. up_read(&policy->rwsem);
  1281. cpufreq_cpu_put(policy);
  1282. }
  1283. return ret_freq;
  1284. }
  1285. EXPORT_SYMBOL(cpufreq_get);
  1286. static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
  1287. {
  1288. unsigned int new_freq;
  1289. new_freq = cpufreq_driver->get(policy->cpu);
  1290. if (!new_freq)
  1291. return 0;
  1292. if (!policy->cur) {
  1293. pr_debug("cpufreq: Driver did not initialize current freq\n");
  1294. policy->cur = new_freq;
  1295. } else if (policy->cur != new_freq && has_target()) {
  1296. cpufreq_out_of_sync(policy, new_freq);
  1297. }
  1298. return new_freq;
  1299. }
  1300. static struct subsys_interface cpufreq_interface = {
  1301. .name = "cpufreq",
  1302. .subsys = &cpu_subsys,
  1303. .add_dev = cpufreq_add_dev,
  1304. .remove_dev = cpufreq_remove_dev,
  1305. };
  1306. /*
  1307. * In case platform wants some specific frequency to be configured
  1308. * during suspend..
  1309. */
  1310. int cpufreq_generic_suspend(struct cpufreq_policy *policy)
  1311. {
  1312. int ret;
  1313. if (!policy->suspend_freq) {
  1314. pr_debug("%s: suspend_freq not defined\n", __func__);
  1315. return 0;
  1316. }
  1317. pr_debug("%s: Setting suspend-freq: %u\n", __func__,
  1318. policy->suspend_freq);
  1319. ret = __cpufreq_driver_target(policy, policy->suspend_freq,
  1320. CPUFREQ_RELATION_H);
  1321. if (ret)
  1322. pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
  1323. __func__, policy->suspend_freq, ret);
  1324. return ret;
  1325. }
  1326. EXPORT_SYMBOL(cpufreq_generic_suspend);
  1327. /**
  1328. * cpufreq_suspend() - Suspend CPUFreq governors
  1329. *
  1330. * Called during system wide Suspend/Hibernate cycles for suspending governors
  1331. * as some platforms can't change frequency after this point in suspend cycle.
  1332. * Because some of the devices (like: i2c, regulators, etc) they use for
  1333. * changing frequency are suspended quickly after this point.
  1334. */
  1335. void cpufreq_suspend(void)
  1336. {
  1337. struct cpufreq_policy *policy;
  1338. if (!cpufreq_driver)
  1339. return;
  1340. if (!has_target() && !cpufreq_driver->suspend)
  1341. goto suspend;
  1342. pr_debug("%s: Suspending Governors\n", __func__);
  1343. for_each_active_policy(policy) {
  1344. if (has_target()) {
  1345. down_write(&policy->rwsem);
  1346. cpufreq_stop_governor(policy);
  1347. up_write(&policy->rwsem);
  1348. }
  1349. if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
  1350. pr_err("%s: Failed to suspend driver: %p\n", __func__,
  1351. policy);
  1352. }
  1353. suspend:
  1354. cpufreq_suspended = true;
  1355. }
  1356. /**
  1357. * cpufreq_resume() - Resume CPUFreq governors
  1358. *
  1359. * Called during system wide Suspend/Hibernate cycle for resuming governors that
  1360. * are suspended with cpufreq_suspend().
  1361. */
  1362. void cpufreq_resume(void)
  1363. {
  1364. struct cpufreq_policy *policy;
  1365. int ret;
  1366. if (!cpufreq_driver)
  1367. return;
  1368. cpufreq_suspended = false;
  1369. if (!has_target() && !cpufreq_driver->resume)
  1370. return;
  1371. pr_debug("%s: Resuming Governors\n", __func__);
  1372. for_each_active_policy(policy) {
  1373. if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
  1374. pr_err("%s: Failed to resume driver: %p\n", __func__,
  1375. policy);
  1376. } else if (has_target()) {
  1377. down_write(&policy->rwsem);
  1378. ret = cpufreq_start_governor(policy);
  1379. up_write(&policy->rwsem);
  1380. if (ret)
  1381. pr_err("%s: Failed to start governor for policy: %p\n",
  1382. __func__, policy);
  1383. }
  1384. }
  1385. }
  1386. /**
  1387. * cpufreq_get_current_driver - return current driver's name
  1388. *
  1389. * Return the name string of the currently loaded cpufreq driver
  1390. * or NULL, if none.
  1391. */
  1392. const char *cpufreq_get_current_driver(void)
  1393. {
  1394. if (cpufreq_driver)
  1395. return cpufreq_driver->name;
  1396. return NULL;
  1397. }
  1398. EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
  1399. /**
  1400. * cpufreq_get_driver_data - return current driver data
  1401. *
  1402. * Return the private data of the currently loaded cpufreq
  1403. * driver, or NULL if no cpufreq driver is loaded.
  1404. */
  1405. void *cpufreq_get_driver_data(void)
  1406. {
  1407. if (cpufreq_driver)
  1408. return cpufreq_driver->driver_data;
  1409. return NULL;
  1410. }
  1411. EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
  1412. /*********************************************************************
  1413. * NOTIFIER LISTS INTERFACE *
  1414. *********************************************************************/
  1415. /**
  1416. * cpufreq_register_notifier - register a driver with cpufreq
  1417. * @nb: notifier function to register
  1418. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1419. *
  1420. * Add a driver to one of two lists: either a list of drivers that
  1421. * are notified about clock rate changes (once before and once after
  1422. * the transition), or a list of drivers that are notified about
  1423. * changes in cpufreq policy.
  1424. *
  1425. * This function may sleep, and has the same return conditions as
  1426. * blocking_notifier_chain_register.
  1427. */
  1428. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1429. {
  1430. int ret;
  1431. if (cpufreq_disabled())
  1432. return -EINVAL;
  1433. WARN_ON(!init_cpufreq_transition_notifier_list_called);
  1434. switch (list) {
  1435. case CPUFREQ_TRANSITION_NOTIFIER:
  1436. mutex_lock(&cpufreq_fast_switch_lock);
  1437. if (cpufreq_fast_switch_count > 0) {
  1438. mutex_unlock(&cpufreq_fast_switch_lock);
  1439. return -EBUSY;
  1440. }
  1441. ret = srcu_notifier_chain_register(
  1442. &cpufreq_transition_notifier_list, nb);
  1443. if (!ret)
  1444. cpufreq_fast_switch_count--;
  1445. mutex_unlock(&cpufreq_fast_switch_lock);
  1446. break;
  1447. case CPUFREQ_POLICY_NOTIFIER:
  1448. ret = blocking_notifier_chain_register(
  1449. &cpufreq_policy_notifier_list, nb);
  1450. break;
  1451. default:
  1452. ret = -EINVAL;
  1453. }
  1454. return ret;
  1455. }
  1456. EXPORT_SYMBOL(cpufreq_register_notifier);
  1457. /**
  1458. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1459. * @nb: notifier block to be unregistered
  1460. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1461. *
  1462. * Remove a driver from the CPU frequency notifier list.
  1463. *
  1464. * This function may sleep, and has the same return conditions as
  1465. * blocking_notifier_chain_unregister.
  1466. */
  1467. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1468. {
  1469. int ret;
  1470. if (cpufreq_disabled())
  1471. return -EINVAL;
  1472. switch (list) {
  1473. case CPUFREQ_TRANSITION_NOTIFIER:
  1474. mutex_lock(&cpufreq_fast_switch_lock);
  1475. ret = srcu_notifier_chain_unregister(
  1476. &cpufreq_transition_notifier_list, nb);
  1477. if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
  1478. cpufreq_fast_switch_count++;
  1479. mutex_unlock(&cpufreq_fast_switch_lock);
  1480. break;
  1481. case CPUFREQ_POLICY_NOTIFIER:
  1482. ret = blocking_notifier_chain_unregister(
  1483. &cpufreq_policy_notifier_list, nb);
  1484. break;
  1485. default:
  1486. ret = -EINVAL;
  1487. }
  1488. return ret;
  1489. }
  1490. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1491. /*********************************************************************
  1492. * GOVERNORS *
  1493. *********************************************************************/
  1494. /**
  1495. * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
  1496. * @policy: cpufreq policy to switch the frequency for.
  1497. * @target_freq: New frequency to set (may be approximate).
  1498. *
  1499. * Carry out a fast frequency switch without sleeping.
  1500. *
  1501. * The driver's ->fast_switch() callback invoked by this function must be
  1502. * suitable for being called from within RCU-sched read-side critical sections
  1503. * and it is expected to select the minimum available frequency greater than or
  1504. * equal to @target_freq (CPUFREQ_RELATION_L).
  1505. *
  1506. * This function must not be called if policy->fast_switch_enabled is unset.
  1507. *
  1508. * Governors calling this function must guarantee that it will never be invoked
  1509. * twice in parallel for the same policy and that it will never be called in
  1510. * parallel with either ->target() or ->target_index() for the same policy.
  1511. *
  1512. * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
  1513. * callback to indicate an error condition, the hardware configuration must be
  1514. * preserved.
  1515. */
  1516. unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
  1517. unsigned int target_freq)
  1518. {
  1519. target_freq = clamp_val(target_freq, policy->min, policy->max);
  1520. return cpufreq_driver->fast_switch(policy, target_freq);
  1521. }
  1522. EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
  1523. /* Must set freqs->new to intermediate frequency */
  1524. static int __target_intermediate(struct cpufreq_policy *policy,
  1525. struct cpufreq_freqs *freqs, int index)
  1526. {
  1527. int ret;
  1528. freqs->new = cpufreq_driver->get_intermediate(policy, index);
  1529. /* We don't need to switch to intermediate freq */
  1530. if (!freqs->new)
  1531. return 0;
  1532. pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
  1533. __func__, policy->cpu, freqs->old, freqs->new);
  1534. cpufreq_freq_transition_begin(policy, freqs);
  1535. ret = cpufreq_driver->target_intermediate(policy, index);
  1536. cpufreq_freq_transition_end(policy, freqs, ret);
  1537. if (ret)
  1538. pr_err("%s: Failed to change to intermediate frequency: %d\n",
  1539. __func__, ret);
  1540. return ret;
  1541. }
  1542. static int __target_index(struct cpufreq_policy *policy, int index)
  1543. {
  1544. struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
  1545. unsigned int intermediate_freq = 0;
  1546. unsigned int newfreq = policy->freq_table[index].frequency;
  1547. int retval = -EINVAL;
  1548. bool notify;
  1549. if (newfreq == policy->cur)
  1550. return 0;
  1551. notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
  1552. if (notify) {
  1553. /* Handle switching to intermediate frequency */
  1554. if (cpufreq_driver->get_intermediate) {
  1555. retval = __target_intermediate(policy, &freqs, index);
  1556. if (retval)
  1557. return retval;
  1558. intermediate_freq = freqs.new;
  1559. /* Set old freq to intermediate */
  1560. if (intermediate_freq)
  1561. freqs.old = freqs.new;
  1562. }
  1563. freqs.new = newfreq;
  1564. pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
  1565. __func__, policy->cpu, freqs.old, freqs.new);
  1566. cpufreq_freq_transition_begin(policy, &freqs);
  1567. }
  1568. retval = cpufreq_driver->target_index(policy, index);
  1569. if (retval)
  1570. pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
  1571. retval);
  1572. if (notify) {
  1573. cpufreq_freq_transition_end(policy, &freqs, retval);
  1574. /*
  1575. * Failed after setting to intermediate freq? Driver should have
  1576. * reverted back to initial frequency and so should we. Check
  1577. * here for intermediate_freq instead of get_intermediate, in
  1578. * case we haven't switched to intermediate freq at all.
  1579. */
  1580. if (unlikely(retval && intermediate_freq)) {
  1581. freqs.old = intermediate_freq;
  1582. freqs.new = policy->restore_freq;
  1583. cpufreq_freq_transition_begin(policy, &freqs);
  1584. cpufreq_freq_transition_end(policy, &freqs, 0);
  1585. }
  1586. }
  1587. return retval;
  1588. }
  1589. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1590. unsigned int target_freq,
  1591. unsigned int relation)
  1592. {
  1593. unsigned int old_target_freq = target_freq;
  1594. int index;
  1595. if (cpufreq_disabled())
  1596. return -ENODEV;
  1597. /* Make sure that target_freq is within supported range */
  1598. target_freq = clamp_val(target_freq, policy->min, policy->max);
  1599. pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
  1600. policy->cpu, target_freq, relation, old_target_freq);
  1601. /*
  1602. * This might look like a redundant call as we are checking it again
  1603. * after finding index. But it is left intentionally for cases where
  1604. * exactly same freq is called again and so we can save on few function
  1605. * calls.
  1606. */
  1607. if (target_freq == policy->cur)
  1608. return 0;
  1609. /* Save last value to restore later on errors */
  1610. policy->restore_freq = policy->cur;
  1611. if (cpufreq_driver->target)
  1612. return cpufreq_driver->target(policy, target_freq, relation);
  1613. if (!cpufreq_driver->target_index)
  1614. return -EINVAL;
  1615. index = cpufreq_frequency_table_target(policy, target_freq, relation);
  1616. return __target_index(policy, index);
  1617. }
  1618. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1619. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1620. unsigned int target_freq,
  1621. unsigned int relation)
  1622. {
  1623. int ret = -EINVAL;
  1624. down_write(&policy->rwsem);
  1625. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1626. up_write(&policy->rwsem);
  1627. return ret;
  1628. }
  1629. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1630. __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
  1631. {
  1632. return NULL;
  1633. }
  1634. static int cpufreq_init_governor(struct cpufreq_policy *policy)
  1635. {
  1636. int ret;
  1637. /* Don't start any governor operations if we are entering suspend */
  1638. if (cpufreq_suspended)
  1639. return 0;
  1640. /*
  1641. * Governor might not be initiated here if ACPI _PPC changed
  1642. * notification happened, so check it.
  1643. */
  1644. if (!policy->governor)
  1645. return -EINVAL;
  1646. if (policy->governor->max_transition_latency &&
  1647. policy->cpuinfo.transition_latency >
  1648. policy->governor->max_transition_latency) {
  1649. struct cpufreq_governor *gov = cpufreq_fallback_governor();
  1650. if (gov) {
  1651. pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
  1652. policy->governor->name, gov->name);
  1653. policy->governor = gov;
  1654. } else {
  1655. return -EINVAL;
  1656. }
  1657. }
  1658. if (!try_module_get(policy->governor->owner))
  1659. return -EINVAL;
  1660. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1661. if (policy->governor->init) {
  1662. ret = policy->governor->init(policy);
  1663. if (ret) {
  1664. module_put(policy->governor->owner);
  1665. return ret;
  1666. }
  1667. }
  1668. return 0;
  1669. }
  1670. static void cpufreq_exit_governor(struct cpufreq_policy *policy)
  1671. {
  1672. if (cpufreq_suspended || !policy->governor)
  1673. return;
  1674. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1675. if (policy->governor->exit)
  1676. policy->governor->exit(policy);
  1677. module_put(policy->governor->owner);
  1678. }
  1679. static int cpufreq_start_governor(struct cpufreq_policy *policy)
  1680. {
  1681. int ret;
  1682. if (cpufreq_suspended)
  1683. return 0;
  1684. if (!policy->governor)
  1685. return -EINVAL;
  1686. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1687. if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
  1688. cpufreq_update_current_freq(policy);
  1689. if (policy->governor->start) {
  1690. ret = policy->governor->start(policy);
  1691. if (ret)
  1692. return ret;
  1693. }
  1694. if (policy->governor->limits)
  1695. policy->governor->limits(policy);
  1696. return 0;
  1697. }
  1698. static void cpufreq_stop_governor(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->stop)
  1704. policy->governor->stop(policy);
  1705. }
  1706. static void cpufreq_governor_limits(struct cpufreq_policy *policy)
  1707. {
  1708. if (cpufreq_suspended || !policy->governor)
  1709. return;
  1710. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1711. if (policy->governor->limits)
  1712. policy->governor->limits(policy);
  1713. }
  1714. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1715. {
  1716. int err;
  1717. if (!governor)
  1718. return -EINVAL;
  1719. if (cpufreq_disabled())
  1720. return -ENODEV;
  1721. mutex_lock(&cpufreq_governor_mutex);
  1722. err = -EBUSY;
  1723. if (!find_governor(governor->name)) {
  1724. err = 0;
  1725. list_add(&governor->governor_list, &cpufreq_governor_list);
  1726. }
  1727. mutex_unlock(&cpufreq_governor_mutex);
  1728. return err;
  1729. }
  1730. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1731. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1732. {
  1733. struct cpufreq_policy *policy;
  1734. unsigned long flags;
  1735. if (!governor)
  1736. return;
  1737. if (cpufreq_disabled())
  1738. return;
  1739. /* clear last_governor for all inactive policies */
  1740. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1741. for_each_inactive_policy(policy) {
  1742. if (!strcmp(policy->last_governor, governor->name)) {
  1743. policy->governor = NULL;
  1744. strcpy(policy->last_governor, "\0");
  1745. }
  1746. }
  1747. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1748. mutex_lock(&cpufreq_governor_mutex);
  1749. list_del(&governor->governor_list);
  1750. mutex_unlock(&cpufreq_governor_mutex);
  1751. return;
  1752. }
  1753. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1754. /*********************************************************************
  1755. * POLICY INTERFACE *
  1756. *********************************************************************/
  1757. /**
  1758. * cpufreq_get_policy - get the current cpufreq_policy
  1759. * @policy: struct cpufreq_policy into which the current cpufreq_policy
  1760. * is written
  1761. *
  1762. * Reads the current cpufreq policy.
  1763. */
  1764. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1765. {
  1766. struct cpufreq_policy *cpu_policy;
  1767. if (!policy)
  1768. return -EINVAL;
  1769. cpu_policy = cpufreq_cpu_get(cpu);
  1770. if (!cpu_policy)
  1771. return -EINVAL;
  1772. memcpy(policy, cpu_policy, sizeof(*policy));
  1773. cpufreq_cpu_put(cpu_policy);
  1774. return 0;
  1775. }
  1776. EXPORT_SYMBOL(cpufreq_get_policy);
  1777. /*
  1778. * policy : current policy.
  1779. * new_policy: policy to be set.
  1780. */
  1781. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  1782. struct cpufreq_policy *new_policy)
  1783. {
  1784. struct cpufreq_governor *old_gov;
  1785. int ret;
  1786. pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
  1787. new_policy->cpu, new_policy->min, new_policy->max);
  1788. memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
  1789. /*
  1790. * This check works well when we store new min/max freq attributes,
  1791. * because new_policy is a copy of policy with one field updated.
  1792. */
  1793. if (new_policy->min > new_policy->max)
  1794. return -EINVAL;
  1795. /* verify the cpu speed can be set within this limit */
  1796. ret = cpufreq_driver->verify(new_policy);
  1797. if (ret)
  1798. return ret;
  1799. /* adjust if necessary - all reasons */
  1800. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1801. CPUFREQ_ADJUST, new_policy);
  1802. /*
  1803. * verify the cpu speed can be set within this limit, which might be
  1804. * different to the first one
  1805. */
  1806. ret = cpufreq_driver->verify(new_policy);
  1807. if (ret)
  1808. return ret;
  1809. /* notification of the new policy */
  1810. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1811. CPUFREQ_NOTIFY, new_policy);
  1812. policy->min = new_policy->min;
  1813. policy->max = new_policy->max;
  1814. policy->cached_target_freq = UINT_MAX;
  1815. pr_debug("new min and max freqs are %u - %u kHz\n",
  1816. policy->min, policy->max);
  1817. if (cpufreq_driver->setpolicy) {
  1818. policy->policy = new_policy->policy;
  1819. pr_debug("setting range\n");
  1820. return cpufreq_driver->setpolicy(new_policy);
  1821. }
  1822. if (new_policy->governor == policy->governor) {
  1823. pr_debug("cpufreq: governor limits update\n");
  1824. cpufreq_governor_limits(policy);
  1825. return 0;
  1826. }
  1827. pr_debug("governor switch\n");
  1828. /* save old, working values */
  1829. old_gov = policy->governor;
  1830. /* end old governor */
  1831. if (old_gov) {
  1832. cpufreq_stop_governor(policy);
  1833. cpufreq_exit_governor(policy);
  1834. }
  1835. /* start new governor */
  1836. policy->governor = new_policy->governor;
  1837. ret = cpufreq_init_governor(policy);
  1838. if (!ret) {
  1839. ret = cpufreq_start_governor(policy);
  1840. if (!ret) {
  1841. pr_debug("cpufreq: governor change\n");
  1842. return 0;
  1843. }
  1844. cpufreq_exit_governor(policy);
  1845. }
  1846. /* new governor failed, so re-start old one */
  1847. pr_debug("starting governor %s failed\n", policy->governor->name);
  1848. if (old_gov) {
  1849. policy->governor = old_gov;
  1850. if (cpufreq_init_governor(policy))
  1851. policy->governor = NULL;
  1852. else
  1853. cpufreq_start_governor(policy);
  1854. }
  1855. return ret;
  1856. }
  1857. /**
  1858. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1859. * @cpu: CPU which shall be re-evaluated
  1860. *
  1861. * Useful for policy notifiers which have different necessities
  1862. * at different times.
  1863. */
  1864. int cpufreq_update_policy(unsigned int cpu)
  1865. {
  1866. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1867. struct cpufreq_policy new_policy;
  1868. int ret;
  1869. if (!policy)
  1870. return -ENODEV;
  1871. down_write(&policy->rwsem);
  1872. pr_debug("updating policy for CPU %u\n", cpu);
  1873. memcpy(&new_policy, policy, sizeof(*policy));
  1874. new_policy.min = policy->user_policy.min;
  1875. new_policy.max = policy->user_policy.max;
  1876. /*
  1877. * BIOS might change freq behind our back
  1878. * -> ask driver for current freq and notify governors about a change
  1879. */
  1880. if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
  1881. if (cpufreq_suspended) {
  1882. ret = -EAGAIN;
  1883. goto unlock;
  1884. }
  1885. new_policy.cur = cpufreq_update_current_freq(policy);
  1886. if (WARN_ON(!new_policy.cur)) {
  1887. ret = -EIO;
  1888. goto unlock;
  1889. }
  1890. }
  1891. ret = cpufreq_set_policy(policy, &new_policy);
  1892. unlock:
  1893. up_write(&policy->rwsem);
  1894. cpufreq_cpu_put(policy);
  1895. return ret;
  1896. }
  1897. EXPORT_SYMBOL(cpufreq_update_policy);
  1898. /*********************************************************************
  1899. * BOOST *
  1900. *********************************************************************/
  1901. static int cpufreq_boost_set_sw(int state)
  1902. {
  1903. struct cpufreq_policy *policy;
  1904. int ret = -EINVAL;
  1905. for_each_active_policy(policy) {
  1906. if (!policy->freq_table)
  1907. continue;
  1908. ret = cpufreq_frequency_table_cpuinfo(policy,
  1909. policy->freq_table);
  1910. if (ret) {
  1911. pr_err("%s: Policy frequency update failed\n",
  1912. __func__);
  1913. break;
  1914. }
  1915. down_write(&policy->rwsem);
  1916. policy->user_policy.max = policy->max;
  1917. cpufreq_governor_limits(policy);
  1918. up_write(&policy->rwsem);
  1919. }
  1920. return ret;
  1921. }
  1922. int cpufreq_boost_trigger_state(int state)
  1923. {
  1924. unsigned long flags;
  1925. int ret = 0;
  1926. if (cpufreq_driver->boost_enabled == state)
  1927. return 0;
  1928. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1929. cpufreq_driver->boost_enabled = state;
  1930. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1931. ret = cpufreq_driver->set_boost(state);
  1932. if (ret) {
  1933. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1934. cpufreq_driver->boost_enabled = !state;
  1935. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1936. pr_err("%s: Cannot %s BOOST\n",
  1937. __func__, state ? "enable" : "disable");
  1938. }
  1939. return ret;
  1940. }
  1941. static bool cpufreq_boost_supported(void)
  1942. {
  1943. return likely(cpufreq_driver) && cpufreq_driver->set_boost;
  1944. }
  1945. static int create_boost_sysfs_file(void)
  1946. {
  1947. int ret;
  1948. ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
  1949. if (ret)
  1950. pr_err("%s: cannot register global BOOST sysfs file\n",
  1951. __func__);
  1952. return ret;
  1953. }
  1954. static void remove_boost_sysfs_file(void)
  1955. {
  1956. if (cpufreq_boost_supported())
  1957. sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
  1958. }
  1959. int cpufreq_enable_boost_support(void)
  1960. {
  1961. if (!cpufreq_driver)
  1962. return -EINVAL;
  1963. if (cpufreq_boost_supported())
  1964. return 0;
  1965. cpufreq_driver->set_boost = cpufreq_boost_set_sw;
  1966. /* This will get removed on driver unregister */
  1967. return create_boost_sysfs_file();
  1968. }
  1969. EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
  1970. int cpufreq_boost_enabled(void)
  1971. {
  1972. return cpufreq_driver->boost_enabled;
  1973. }
  1974. EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
  1975. /*********************************************************************
  1976. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1977. *********************************************************************/
  1978. static enum cpuhp_state hp_online;
  1979. static int cpuhp_cpufreq_online(unsigned int cpu)
  1980. {
  1981. cpufreq_online(cpu);
  1982. return 0;
  1983. }
  1984. static int cpuhp_cpufreq_offline(unsigned int cpu)
  1985. {
  1986. cpufreq_offline(cpu);
  1987. return 0;
  1988. }
  1989. /**
  1990. * cpufreq_register_driver - register a CPU Frequency driver
  1991. * @driver_data: A struct cpufreq_driver containing the values#
  1992. * submitted by the CPU Frequency driver.
  1993. *
  1994. * Registers a CPU Frequency driver to this core code. This code
  1995. * returns zero on success, -EEXIST when another driver got here first
  1996. * (and isn't unregistered in the meantime).
  1997. *
  1998. */
  1999. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  2000. {
  2001. unsigned long flags;
  2002. int ret;
  2003. if (cpufreq_disabled())
  2004. return -ENODEV;
  2005. if (!driver_data || !driver_data->verify || !driver_data->init ||
  2006. !(driver_data->setpolicy || driver_data->target_index ||
  2007. driver_data->target) ||
  2008. (driver_data->setpolicy && (driver_data->target_index ||
  2009. driver_data->target)) ||
  2010. (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
  2011. return -EINVAL;
  2012. pr_debug("trying to register driver %s\n", driver_data->name);
  2013. /* Protect against concurrent CPU online/offline. */
  2014. get_online_cpus();
  2015. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2016. if (cpufreq_driver) {
  2017. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2018. ret = -EEXIST;
  2019. goto out;
  2020. }
  2021. cpufreq_driver = driver_data;
  2022. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2023. if (driver_data->setpolicy)
  2024. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  2025. if (cpufreq_boost_supported()) {
  2026. ret = create_boost_sysfs_file();
  2027. if (ret)
  2028. goto err_null_driver;
  2029. }
  2030. ret = subsys_interface_register(&cpufreq_interface);
  2031. if (ret)
  2032. goto err_boost_unreg;
  2033. if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
  2034. list_empty(&cpufreq_policy_list)) {
  2035. /* if all ->init() calls failed, unregister */
  2036. ret = -ENODEV;
  2037. pr_debug("%s: No CPU initialized for driver %s\n", __func__,
  2038. driver_data->name);
  2039. goto err_if_unreg;
  2040. }
  2041. ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "cpufreq:online",
  2042. cpuhp_cpufreq_online,
  2043. cpuhp_cpufreq_offline);
  2044. if (ret < 0)
  2045. goto err_if_unreg;
  2046. hp_online = ret;
  2047. ret = 0;
  2048. pr_debug("driver %s up and running\n", driver_data->name);
  2049. goto out;
  2050. err_if_unreg:
  2051. subsys_interface_unregister(&cpufreq_interface);
  2052. err_boost_unreg:
  2053. remove_boost_sysfs_file();
  2054. err_null_driver:
  2055. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2056. cpufreq_driver = NULL;
  2057. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2058. out:
  2059. put_online_cpus();
  2060. return ret;
  2061. }
  2062. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  2063. /**
  2064. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  2065. *
  2066. * Unregister the current CPUFreq driver. Only call this if you have
  2067. * the right to do so, i.e. if you have succeeded in initialising before!
  2068. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  2069. * currently not initialised.
  2070. */
  2071. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  2072. {
  2073. unsigned long flags;
  2074. if (!cpufreq_driver || (driver != cpufreq_driver))
  2075. return -EINVAL;
  2076. pr_debug("unregistering driver %s\n", driver->name);
  2077. /* Protect against concurrent cpu hotplug */
  2078. get_online_cpus();
  2079. subsys_interface_unregister(&cpufreq_interface);
  2080. remove_boost_sysfs_file();
  2081. cpuhp_remove_state_nocalls(hp_online);
  2082. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2083. cpufreq_driver = NULL;
  2084. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2085. put_online_cpus();
  2086. return 0;
  2087. }
  2088. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  2089. /*
  2090. * Stop cpufreq at shutdown to make sure it isn't holding any locks
  2091. * or mutexes when secondary CPUs are halted.
  2092. */
  2093. static struct syscore_ops cpufreq_syscore_ops = {
  2094. .shutdown = cpufreq_suspend,
  2095. };
  2096. struct kobject *cpufreq_global_kobject;
  2097. EXPORT_SYMBOL(cpufreq_global_kobject);
  2098. static int __init cpufreq_core_init(void)
  2099. {
  2100. if (cpufreq_disabled())
  2101. return -ENODEV;
  2102. cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
  2103. BUG_ON(!cpufreq_global_kobject);
  2104. register_syscore_ops(&cpufreq_syscore_ops);
  2105. return 0;
  2106. }
  2107. core_initcall(cpufreq_core_init);