cpufreq.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  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. __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
  139. unsigned long max_freq)
  140. {
  141. }
  142. EXPORT_SYMBOL_GPL(arch_set_freq_scale);
  143. /*
  144. * This is a generic cpufreq init() routine which can be used by cpufreq
  145. * drivers of SMP systems. It will do following:
  146. * - validate & show freq table passed
  147. * - set policies transition latency
  148. * - policy->cpus with all possible CPUs
  149. */
  150. int cpufreq_generic_init(struct cpufreq_policy *policy,
  151. struct cpufreq_frequency_table *table,
  152. unsigned int transition_latency)
  153. {
  154. policy->freq_table = table;
  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. unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
  445. {
  446. unsigned int latency;
  447. if (policy->transition_delay_us)
  448. return policy->transition_delay_us;
  449. latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
  450. if (latency) {
  451. /*
  452. * For platforms that can change the frequency very fast (< 10
  453. * us), the above formula gives a decent transition delay. But
  454. * for platforms where transition_latency is in milliseconds, it
  455. * ends up giving unrealistic values.
  456. *
  457. * Cap the default transition delay to 10 ms, which seems to be
  458. * a reasonable amount of time after which we should reevaluate
  459. * the frequency.
  460. */
  461. return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
  462. }
  463. return LATENCY_MULTIPLIER;
  464. }
  465. EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
  466. /*********************************************************************
  467. * SYSFS INTERFACE *
  468. *********************************************************************/
  469. static ssize_t show_boost(struct kobject *kobj,
  470. struct attribute *attr, char *buf)
  471. {
  472. return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
  473. }
  474. static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
  475. const char *buf, size_t count)
  476. {
  477. int ret, enable;
  478. ret = sscanf(buf, "%d", &enable);
  479. if (ret != 1 || enable < 0 || enable > 1)
  480. return -EINVAL;
  481. if (cpufreq_boost_trigger_state(enable)) {
  482. pr_err("%s: Cannot %s BOOST!\n",
  483. __func__, enable ? "enable" : "disable");
  484. return -EINVAL;
  485. }
  486. pr_debug("%s: cpufreq BOOST %s\n",
  487. __func__, enable ? "enabled" : "disabled");
  488. return count;
  489. }
  490. define_one_global_rw(boost);
  491. static struct cpufreq_governor *find_governor(const char *str_governor)
  492. {
  493. struct cpufreq_governor *t;
  494. for_each_governor(t)
  495. if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
  496. return t;
  497. return NULL;
  498. }
  499. /**
  500. * cpufreq_parse_governor - parse a governor string
  501. */
  502. static int cpufreq_parse_governor(char *str_governor,
  503. struct cpufreq_policy *policy)
  504. {
  505. if (cpufreq_driver->setpolicy) {
  506. if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
  507. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  508. return 0;
  509. }
  510. if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
  511. policy->policy = CPUFREQ_POLICY_POWERSAVE;
  512. return 0;
  513. }
  514. } else {
  515. struct cpufreq_governor *t;
  516. mutex_lock(&cpufreq_governor_mutex);
  517. t = find_governor(str_governor);
  518. if (!t) {
  519. int ret;
  520. mutex_unlock(&cpufreq_governor_mutex);
  521. ret = request_module("cpufreq_%s", str_governor);
  522. if (ret)
  523. return -EINVAL;
  524. mutex_lock(&cpufreq_governor_mutex);
  525. t = find_governor(str_governor);
  526. }
  527. if (t && !try_module_get(t->owner))
  528. t = NULL;
  529. mutex_unlock(&cpufreq_governor_mutex);
  530. if (t) {
  531. policy->governor = t;
  532. return 0;
  533. }
  534. }
  535. return -EINVAL;
  536. }
  537. /**
  538. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  539. * print out cpufreq information
  540. *
  541. * Write out information from cpufreq_driver->policy[cpu]; object must be
  542. * "unsigned int".
  543. */
  544. #define show_one(file_name, object) \
  545. static ssize_t show_##file_name \
  546. (struct cpufreq_policy *policy, char *buf) \
  547. { \
  548. return sprintf(buf, "%u\n", policy->object); \
  549. }
  550. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  551. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  552. show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
  553. show_one(scaling_min_freq, min);
  554. show_one(scaling_max_freq, max);
  555. __weak unsigned int arch_freq_get_on_cpu(int cpu)
  556. {
  557. return 0;
  558. }
  559. static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
  560. {
  561. ssize_t ret;
  562. unsigned int freq;
  563. freq = arch_freq_get_on_cpu(policy->cpu);
  564. if (freq)
  565. ret = sprintf(buf, "%u\n", freq);
  566. else if (cpufreq_driver && cpufreq_driver->setpolicy &&
  567. cpufreq_driver->get)
  568. ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
  569. else
  570. ret = sprintf(buf, "%u\n", policy->cur);
  571. return ret;
  572. }
  573. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  574. struct cpufreq_policy *new_policy);
  575. /**
  576. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  577. */
  578. #define store_one(file_name, object) \
  579. static ssize_t store_##file_name \
  580. (struct cpufreq_policy *policy, const char *buf, size_t count) \
  581. { \
  582. int ret, temp; \
  583. struct cpufreq_policy new_policy; \
  584. \
  585. memcpy(&new_policy, policy, sizeof(*policy)); \
  586. \
  587. ret = sscanf(buf, "%u", &new_policy.object); \
  588. if (ret != 1) \
  589. return -EINVAL; \
  590. \
  591. temp = new_policy.object; \
  592. ret = cpufreq_set_policy(policy, &new_policy); \
  593. if (!ret) \
  594. policy->user_policy.object = temp; \
  595. \
  596. return ret ? ret : count; \
  597. }
  598. store_one(scaling_min_freq, min);
  599. store_one(scaling_max_freq, max);
  600. /**
  601. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  602. */
  603. static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
  604. char *buf)
  605. {
  606. unsigned int cur_freq = __cpufreq_get(policy);
  607. if (cur_freq)
  608. return sprintf(buf, "%u\n", cur_freq);
  609. return sprintf(buf, "<unknown>\n");
  610. }
  611. /**
  612. * show_scaling_governor - show the current policy for the specified CPU
  613. */
  614. static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
  615. {
  616. if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
  617. return sprintf(buf, "powersave\n");
  618. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  619. return sprintf(buf, "performance\n");
  620. else if (policy->governor)
  621. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
  622. policy->governor->name);
  623. return -EINVAL;
  624. }
  625. /**
  626. * store_scaling_governor - store policy for the specified CPU
  627. */
  628. static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
  629. const char *buf, size_t count)
  630. {
  631. int ret;
  632. char str_governor[16];
  633. struct cpufreq_policy new_policy;
  634. memcpy(&new_policy, policy, sizeof(*policy));
  635. ret = sscanf(buf, "%15s", str_governor);
  636. if (ret != 1)
  637. return -EINVAL;
  638. if (cpufreq_parse_governor(str_governor, &new_policy))
  639. return -EINVAL;
  640. ret = cpufreq_set_policy(policy, &new_policy);
  641. if (new_policy.governor)
  642. module_put(new_policy.governor->owner);
  643. return ret ? ret : count;
  644. }
  645. /**
  646. * show_scaling_driver - show the cpufreq driver currently loaded
  647. */
  648. static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
  649. {
  650. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
  651. }
  652. /**
  653. * show_scaling_available_governors - show the available CPUfreq governors
  654. */
  655. static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
  656. char *buf)
  657. {
  658. ssize_t i = 0;
  659. struct cpufreq_governor *t;
  660. if (!has_target()) {
  661. i += sprintf(buf, "performance powersave");
  662. goto out;
  663. }
  664. for_each_governor(t) {
  665. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
  666. - (CPUFREQ_NAME_LEN + 2)))
  667. goto out;
  668. i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
  669. }
  670. out:
  671. i += sprintf(&buf[i], "\n");
  672. return i;
  673. }
  674. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
  675. {
  676. ssize_t i = 0;
  677. unsigned int cpu;
  678. for_each_cpu(cpu, mask) {
  679. if (i)
  680. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
  681. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
  682. if (i >= (PAGE_SIZE - 5))
  683. break;
  684. }
  685. i += sprintf(&buf[i], "\n");
  686. return i;
  687. }
  688. EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
  689. /**
  690. * show_related_cpus - show the CPUs affected by each transition even if
  691. * hw coordination is in use
  692. */
  693. static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
  694. {
  695. return cpufreq_show_cpus(policy->related_cpus, buf);
  696. }
  697. /**
  698. * show_affected_cpus - show the CPUs affected by each transition
  699. */
  700. static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
  701. {
  702. return cpufreq_show_cpus(policy->cpus, buf);
  703. }
  704. static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
  705. const char *buf, size_t count)
  706. {
  707. unsigned int freq = 0;
  708. unsigned int ret;
  709. if (!policy->governor || !policy->governor->store_setspeed)
  710. return -EINVAL;
  711. ret = sscanf(buf, "%u", &freq);
  712. if (ret != 1)
  713. return -EINVAL;
  714. policy->governor->store_setspeed(policy, freq);
  715. return count;
  716. }
  717. static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
  718. {
  719. if (!policy->governor || !policy->governor->show_setspeed)
  720. return sprintf(buf, "<unsupported>\n");
  721. return policy->governor->show_setspeed(policy, buf);
  722. }
  723. /**
  724. * show_bios_limit - show the current cpufreq HW/BIOS limitation
  725. */
  726. static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
  727. {
  728. unsigned int limit;
  729. int ret;
  730. if (cpufreq_driver->bios_limit) {
  731. ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
  732. if (!ret)
  733. return sprintf(buf, "%u\n", limit);
  734. }
  735. return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
  736. }
  737. cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
  738. cpufreq_freq_attr_ro(cpuinfo_min_freq);
  739. cpufreq_freq_attr_ro(cpuinfo_max_freq);
  740. cpufreq_freq_attr_ro(cpuinfo_transition_latency);
  741. cpufreq_freq_attr_ro(scaling_available_governors);
  742. cpufreq_freq_attr_ro(scaling_driver);
  743. cpufreq_freq_attr_ro(scaling_cur_freq);
  744. cpufreq_freq_attr_ro(bios_limit);
  745. cpufreq_freq_attr_ro(related_cpus);
  746. cpufreq_freq_attr_ro(affected_cpus);
  747. cpufreq_freq_attr_rw(scaling_min_freq);
  748. cpufreq_freq_attr_rw(scaling_max_freq);
  749. cpufreq_freq_attr_rw(scaling_governor);
  750. cpufreq_freq_attr_rw(scaling_setspeed);
  751. static struct attribute *default_attrs[] = {
  752. &cpuinfo_min_freq.attr,
  753. &cpuinfo_max_freq.attr,
  754. &cpuinfo_transition_latency.attr,
  755. &scaling_min_freq.attr,
  756. &scaling_max_freq.attr,
  757. &affected_cpus.attr,
  758. &related_cpus.attr,
  759. &scaling_governor.attr,
  760. &scaling_driver.attr,
  761. &scaling_available_governors.attr,
  762. &scaling_setspeed.attr,
  763. NULL
  764. };
  765. #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
  766. #define to_attr(a) container_of(a, struct freq_attr, attr)
  767. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  768. {
  769. struct cpufreq_policy *policy = to_policy(kobj);
  770. struct freq_attr *fattr = to_attr(attr);
  771. ssize_t ret;
  772. down_read(&policy->rwsem);
  773. ret = fattr->show(policy, buf);
  774. up_read(&policy->rwsem);
  775. return ret;
  776. }
  777. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  778. const char *buf, size_t count)
  779. {
  780. struct cpufreq_policy *policy = to_policy(kobj);
  781. struct freq_attr *fattr = to_attr(attr);
  782. ssize_t ret = -EINVAL;
  783. cpus_read_lock();
  784. if (cpu_online(policy->cpu)) {
  785. down_write(&policy->rwsem);
  786. ret = fattr->store(policy, buf, count);
  787. up_write(&policy->rwsem);
  788. }
  789. cpus_read_unlock();
  790. return ret;
  791. }
  792. static void cpufreq_sysfs_release(struct kobject *kobj)
  793. {
  794. struct cpufreq_policy *policy = to_policy(kobj);
  795. pr_debug("last reference is dropped\n");
  796. complete(&policy->kobj_unregister);
  797. }
  798. static const struct sysfs_ops sysfs_ops = {
  799. .show = show,
  800. .store = store,
  801. };
  802. static struct kobj_type ktype_cpufreq = {
  803. .sysfs_ops = &sysfs_ops,
  804. .default_attrs = default_attrs,
  805. .release = cpufreq_sysfs_release,
  806. };
  807. static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
  808. {
  809. struct device *dev = get_cpu_device(cpu);
  810. if (!dev)
  811. return;
  812. if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
  813. return;
  814. dev_dbg(dev, "%s: Adding symlink\n", __func__);
  815. if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
  816. dev_err(dev, "cpufreq symlink creation failed\n");
  817. }
  818. static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
  819. struct device *dev)
  820. {
  821. dev_dbg(dev, "%s: Removing symlink\n", __func__);
  822. sysfs_remove_link(&dev->kobj, "cpufreq");
  823. }
  824. static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
  825. {
  826. struct freq_attr **drv_attr;
  827. int ret = 0;
  828. /* set up files for this cpu device */
  829. drv_attr = cpufreq_driver->attr;
  830. while (drv_attr && *drv_attr) {
  831. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  832. if (ret)
  833. return ret;
  834. drv_attr++;
  835. }
  836. if (cpufreq_driver->get) {
  837. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  838. if (ret)
  839. return ret;
  840. }
  841. ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  842. if (ret)
  843. return ret;
  844. if (cpufreq_driver->bios_limit) {
  845. ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
  846. if (ret)
  847. return ret;
  848. }
  849. return 0;
  850. }
  851. __weak struct cpufreq_governor *cpufreq_default_governor(void)
  852. {
  853. return NULL;
  854. }
  855. static int cpufreq_init_policy(struct cpufreq_policy *policy)
  856. {
  857. struct cpufreq_governor *gov = NULL;
  858. struct cpufreq_policy new_policy;
  859. memcpy(&new_policy, policy, sizeof(*policy));
  860. /* Update governor of new_policy to the governor used before hotplug */
  861. gov = find_governor(policy->last_governor);
  862. if (gov) {
  863. pr_debug("Restoring governor %s for cpu %d\n",
  864. policy->governor->name, policy->cpu);
  865. } else {
  866. gov = cpufreq_default_governor();
  867. if (!gov)
  868. return -ENODATA;
  869. }
  870. new_policy.governor = gov;
  871. /* Use the default policy if there is no last_policy. */
  872. if (cpufreq_driver->setpolicy) {
  873. if (policy->last_policy)
  874. new_policy.policy = policy->last_policy;
  875. else
  876. cpufreq_parse_governor(gov->name, &new_policy);
  877. }
  878. /* set default policy */
  879. return cpufreq_set_policy(policy, &new_policy);
  880. }
  881. static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
  882. {
  883. int ret = 0;
  884. /* Has this CPU been taken care of already? */
  885. if (cpumask_test_cpu(cpu, policy->cpus))
  886. return 0;
  887. down_write(&policy->rwsem);
  888. if (has_target())
  889. cpufreq_stop_governor(policy);
  890. cpumask_set_cpu(cpu, policy->cpus);
  891. if (has_target()) {
  892. ret = cpufreq_start_governor(policy);
  893. if (ret)
  894. pr_err("%s: Failed to start governor\n", __func__);
  895. }
  896. up_write(&policy->rwsem);
  897. return ret;
  898. }
  899. static void handle_update(struct work_struct *work)
  900. {
  901. struct cpufreq_policy *policy =
  902. container_of(work, struct cpufreq_policy, update);
  903. unsigned int cpu = policy->cpu;
  904. pr_debug("handle_update for cpu %u called\n", cpu);
  905. cpufreq_update_policy(cpu);
  906. }
  907. static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
  908. {
  909. struct cpufreq_policy *policy;
  910. int ret;
  911. policy = kzalloc(sizeof(*policy), GFP_KERNEL);
  912. if (!policy)
  913. return NULL;
  914. if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
  915. goto err_free_policy;
  916. if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
  917. goto err_free_cpumask;
  918. if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
  919. goto err_free_rcpumask;
  920. ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
  921. cpufreq_global_kobject, "policy%u", cpu);
  922. if (ret) {
  923. pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
  924. goto err_free_real_cpus;
  925. }
  926. INIT_LIST_HEAD(&policy->policy_list);
  927. init_rwsem(&policy->rwsem);
  928. spin_lock_init(&policy->transition_lock);
  929. init_waitqueue_head(&policy->transition_wait);
  930. init_completion(&policy->kobj_unregister);
  931. INIT_WORK(&policy->update, handle_update);
  932. policy->cpu = cpu;
  933. return policy;
  934. err_free_real_cpus:
  935. free_cpumask_var(policy->real_cpus);
  936. err_free_rcpumask:
  937. free_cpumask_var(policy->related_cpus);
  938. err_free_cpumask:
  939. free_cpumask_var(policy->cpus);
  940. err_free_policy:
  941. kfree(policy);
  942. return NULL;
  943. }
  944. static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
  945. {
  946. struct kobject *kobj;
  947. struct completion *cmp;
  948. down_write(&policy->rwsem);
  949. cpufreq_stats_free_table(policy);
  950. kobj = &policy->kobj;
  951. cmp = &policy->kobj_unregister;
  952. up_write(&policy->rwsem);
  953. kobject_put(kobj);
  954. /*
  955. * We need to make sure that the underlying kobj is
  956. * actually not referenced anymore by anybody before we
  957. * proceed with unloading.
  958. */
  959. pr_debug("waiting for dropping of refcount\n");
  960. wait_for_completion(cmp);
  961. pr_debug("wait complete\n");
  962. }
  963. static void cpufreq_policy_free(struct cpufreq_policy *policy)
  964. {
  965. unsigned long flags;
  966. int cpu;
  967. /* Remove policy from list */
  968. write_lock_irqsave(&cpufreq_driver_lock, flags);
  969. list_del(&policy->policy_list);
  970. for_each_cpu(cpu, policy->related_cpus)
  971. per_cpu(cpufreq_cpu_data, cpu) = NULL;
  972. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  973. cpufreq_policy_put_kobj(policy);
  974. free_cpumask_var(policy->real_cpus);
  975. free_cpumask_var(policy->related_cpus);
  976. free_cpumask_var(policy->cpus);
  977. kfree(policy);
  978. }
  979. static int cpufreq_online(unsigned int cpu)
  980. {
  981. struct cpufreq_policy *policy;
  982. bool new_policy;
  983. unsigned long flags;
  984. unsigned int j;
  985. int ret;
  986. pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
  987. /* Check if this CPU already has a policy to manage it */
  988. policy = per_cpu(cpufreq_cpu_data, cpu);
  989. if (policy) {
  990. WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
  991. if (!policy_is_inactive(policy))
  992. return cpufreq_add_policy_cpu(policy, cpu);
  993. /* This is the only online CPU for the policy. Start over. */
  994. new_policy = false;
  995. down_write(&policy->rwsem);
  996. policy->cpu = cpu;
  997. policy->governor = NULL;
  998. up_write(&policy->rwsem);
  999. } else {
  1000. new_policy = true;
  1001. policy = cpufreq_policy_alloc(cpu);
  1002. if (!policy)
  1003. return -ENOMEM;
  1004. }
  1005. cpumask_copy(policy->cpus, cpumask_of(cpu));
  1006. /* call driver. From then on the cpufreq must be able
  1007. * to accept all calls to ->verify and ->setpolicy for this CPU
  1008. */
  1009. ret = cpufreq_driver->init(policy);
  1010. if (ret) {
  1011. pr_debug("initialization failed\n");
  1012. goto out_free_policy;
  1013. }
  1014. ret = cpufreq_table_validate_and_sort(policy);
  1015. if (ret)
  1016. goto out_exit_policy;
  1017. down_write(&policy->rwsem);
  1018. if (new_policy) {
  1019. /* related_cpus should at least include policy->cpus. */
  1020. cpumask_copy(policy->related_cpus, policy->cpus);
  1021. }
  1022. /*
  1023. * affected cpus must always be the one, which are online. We aren't
  1024. * managing offline cpus here.
  1025. */
  1026. cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
  1027. if (new_policy) {
  1028. policy->user_policy.min = policy->min;
  1029. policy->user_policy.max = policy->max;
  1030. for_each_cpu(j, policy->related_cpus) {
  1031. per_cpu(cpufreq_cpu_data, j) = policy;
  1032. add_cpu_dev_symlink(policy, j);
  1033. }
  1034. } else {
  1035. policy->min = policy->user_policy.min;
  1036. policy->max = policy->user_policy.max;
  1037. }
  1038. if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
  1039. policy->cur = cpufreq_driver->get(policy->cpu);
  1040. if (!policy->cur) {
  1041. pr_err("%s: ->get() failed\n", __func__);
  1042. goto out_destroy_policy;
  1043. }
  1044. }
  1045. /*
  1046. * Sometimes boot loaders set CPU frequency to a value outside of
  1047. * frequency table present with cpufreq core. In such cases CPU might be
  1048. * unstable if it has to run on that frequency for long duration of time
  1049. * and so its better to set it to a frequency which is specified in
  1050. * freq-table. This also makes cpufreq stats inconsistent as
  1051. * cpufreq-stats would fail to register because current frequency of CPU
  1052. * isn't found in freq-table.
  1053. *
  1054. * Because we don't want this change to effect boot process badly, we go
  1055. * for the next freq which is >= policy->cur ('cur' must be set by now,
  1056. * otherwise we will end up setting freq to lowest of the table as 'cur'
  1057. * is initialized to zero).
  1058. *
  1059. * We are passing target-freq as "policy->cur - 1" otherwise
  1060. * __cpufreq_driver_target() would simply fail, as policy->cur will be
  1061. * equal to target-freq.
  1062. */
  1063. if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
  1064. && has_target()) {
  1065. /* Are we running at unknown frequency ? */
  1066. ret = cpufreq_frequency_table_get_index(policy, policy->cur);
  1067. if (ret == -EINVAL) {
  1068. /* Warn user and fix it */
  1069. pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
  1070. __func__, policy->cpu, policy->cur);
  1071. ret = __cpufreq_driver_target(policy, policy->cur - 1,
  1072. CPUFREQ_RELATION_L);
  1073. /*
  1074. * Reaching here after boot in a few seconds may not
  1075. * mean that system will remain stable at "unknown"
  1076. * frequency for longer duration. Hence, a BUG_ON().
  1077. */
  1078. BUG_ON(ret);
  1079. pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
  1080. __func__, policy->cpu, policy->cur);
  1081. }
  1082. }
  1083. if (new_policy) {
  1084. ret = cpufreq_add_dev_interface(policy);
  1085. if (ret)
  1086. goto out_destroy_policy;
  1087. cpufreq_stats_create_table(policy);
  1088. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1089. list_add(&policy->policy_list, &cpufreq_policy_list);
  1090. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1091. }
  1092. ret = cpufreq_init_policy(policy);
  1093. if (ret) {
  1094. pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
  1095. __func__, cpu, ret);
  1096. /* cpufreq_policy_free() will notify based on this */
  1097. new_policy = false;
  1098. goto out_destroy_policy;
  1099. }
  1100. up_write(&policy->rwsem);
  1101. kobject_uevent(&policy->kobj, KOBJ_ADD);
  1102. /* Callback for handling stuff after policy is ready */
  1103. if (cpufreq_driver->ready)
  1104. cpufreq_driver->ready(policy);
  1105. pr_debug("initialization complete\n");
  1106. return 0;
  1107. out_destroy_policy:
  1108. for_each_cpu(j, policy->real_cpus)
  1109. remove_cpu_dev_symlink(policy, get_cpu_device(j));
  1110. up_write(&policy->rwsem);
  1111. out_exit_policy:
  1112. if (cpufreq_driver->exit)
  1113. cpufreq_driver->exit(policy);
  1114. out_free_policy:
  1115. cpufreq_policy_free(policy);
  1116. return ret;
  1117. }
  1118. /**
  1119. * cpufreq_add_dev - the cpufreq interface for a CPU device.
  1120. * @dev: CPU device.
  1121. * @sif: Subsystem interface structure pointer (not used)
  1122. */
  1123. static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
  1124. {
  1125. struct cpufreq_policy *policy;
  1126. unsigned cpu = dev->id;
  1127. int ret;
  1128. dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
  1129. if (cpu_online(cpu)) {
  1130. ret = cpufreq_online(cpu);
  1131. if (ret)
  1132. return ret;
  1133. }
  1134. /* Create sysfs link on CPU registration */
  1135. policy = per_cpu(cpufreq_cpu_data, cpu);
  1136. if (policy)
  1137. add_cpu_dev_symlink(policy, cpu);
  1138. return 0;
  1139. }
  1140. static int cpufreq_offline(unsigned int cpu)
  1141. {
  1142. struct cpufreq_policy *policy;
  1143. int ret;
  1144. pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
  1145. policy = cpufreq_cpu_get_raw(cpu);
  1146. if (!policy) {
  1147. pr_debug("%s: No cpu_data found\n", __func__);
  1148. return 0;
  1149. }
  1150. down_write(&policy->rwsem);
  1151. if (has_target())
  1152. cpufreq_stop_governor(policy);
  1153. cpumask_clear_cpu(cpu, policy->cpus);
  1154. if (policy_is_inactive(policy)) {
  1155. if (has_target())
  1156. strncpy(policy->last_governor, policy->governor->name,
  1157. CPUFREQ_NAME_LEN);
  1158. else
  1159. policy->last_policy = policy->policy;
  1160. } else if (cpu == policy->cpu) {
  1161. /* Nominate new CPU */
  1162. policy->cpu = cpumask_any(policy->cpus);
  1163. }
  1164. /* Start governor again for active policy */
  1165. if (!policy_is_inactive(policy)) {
  1166. if (has_target()) {
  1167. ret = cpufreq_start_governor(policy);
  1168. if (ret)
  1169. pr_err("%s: Failed to start governor\n", __func__);
  1170. }
  1171. goto unlock;
  1172. }
  1173. if (cpufreq_driver->stop_cpu)
  1174. cpufreq_driver->stop_cpu(policy);
  1175. if (has_target())
  1176. cpufreq_exit_governor(policy);
  1177. /*
  1178. * Perform the ->exit() even during light-weight tear-down,
  1179. * since this is a core component, and is essential for the
  1180. * subsequent light-weight ->init() to succeed.
  1181. */
  1182. if (cpufreq_driver->exit) {
  1183. cpufreq_driver->exit(policy);
  1184. policy->freq_table = NULL;
  1185. }
  1186. unlock:
  1187. up_write(&policy->rwsem);
  1188. return 0;
  1189. }
  1190. /**
  1191. * cpufreq_remove_dev - remove a CPU device
  1192. *
  1193. * Removes the cpufreq interface for a CPU device.
  1194. */
  1195. static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  1196. {
  1197. unsigned int cpu = dev->id;
  1198. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1199. if (!policy)
  1200. return;
  1201. if (cpu_online(cpu))
  1202. cpufreq_offline(cpu);
  1203. cpumask_clear_cpu(cpu, policy->real_cpus);
  1204. remove_cpu_dev_symlink(policy, dev);
  1205. if (cpumask_empty(policy->real_cpus))
  1206. cpufreq_policy_free(policy);
  1207. }
  1208. /**
  1209. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
  1210. * in deep trouble.
  1211. * @policy: policy managing CPUs
  1212. * @new_freq: CPU frequency the CPU actually runs at
  1213. *
  1214. * We adjust to current frequency first, and need to clean up later.
  1215. * So either call to cpufreq_update_policy() or schedule handle_update()).
  1216. */
  1217. static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
  1218. unsigned int new_freq)
  1219. {
  1220. struct cpufreq_freqs freqs;
  1221. pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
  1222. policy->cur, new_freq);
  1223. freqs.old = policy->cur;
  1224. freqs.new = new_freq;
  1225. cpufreq_freq_transition_begin(policy, &freqs);
  1226. cpufreq_freq_transition_end(policy, &freqs, 0);
  1227. }
  1228. /**
  1229. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  1230. * @cpu: CPU number
  1231. *
  1232. * This is the last known freq, without actually getting it from the driver.
  1233. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  1234. */
  1235. unsigned int cpufreq_quick_get(unsigned int cpu)
  1236. {
  1237. struct cpufreq_policy *policy;
  1238. unsigned int ret_freq = 0;
  1239. unsigned long flags;
  1240. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1241. if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
  1242. ret_freq = cpufreq_driver->get(cpu);
  1243. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1244. return ret_freq;
  1245. }
  1246. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1247. policy = cpufreq_cpu_get(cpu);
  1248. if (policy) {
  1249. ret_freq = policy->cur;
  1250. cpufreq_cpu_put(policy);
  1251. }
  1252. return ret_freq;
  1253. }
  1254. EXPORT_SYMBOL(cpufreq_quick_get);
  1255. /**
  1256. * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
  1257. * @cpu: CPU number
  1258. *
  1259. * Just return the max possible frequency for a given CPU.
  1260. */
  1261. unsigned int cpufreq_quick_get_max(unsigned int cpu)
  1262. {
  1263. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1264. unsigned int ret_freq = 0;
  1265. if (policy) {
  1266. ret_freq = policy->max;
  1267. cpufreq_cpu_put(policy);
  1268. }
  1269. return ret_freq;
  1270. }
  1271. EXPORT_SYMBOL(cpufreq_quick_get_max);
  1272. static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
  1273. {
  1274. unsigned int ret_freq = 0;
  1275. if (!cpufreq_driver->get)
  1276. return ret_freq;
  1277. ret_freq = cpufreq_driver->get(policy->cpu);
  1278. /*
  1279. * Updating inactive policies is invalid, so avoid doing that. Also
  1280. * if fast frequency switching is used with the given policy, the check
  1281. * against policy->cur is pointless, so skip it in that case too.
  1282. */
  1283. if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
  1284. return ret_freq;
  1285. if (ret_freq && policy->cur &&
  1286. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1287. /* verify no discrepancy between actual and
  1288. saved value exists */
  1289. if (unlikely(ret_freq != policy->cur)) {
  1290. cpufreq_out_of_sync(policy, ret_freq);
  1291. schedule_work(&policy->update);
  1292. }
  1293. }
  1294. return ret_freq;
  1295. }
  1296. /**
  1297. * cpufreq_get - get the current CPU frequency (in kHz)
  1298. * @cpu: CPU number
  1299. *
  1300. * Get the CPU current (static) CPU frequency
  1301. */
  1302. unsigned int cpufreq_get(unsigned int cpu)
  1303. {
  1304. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1305. unsigned int ret_freq = 0;
  1306. if (policy) {
  1307. down_read(&policy->rwsem);
  1308. if (!policy_is_inactive(policy))
  1309. ret_freq = __cpufreq_get(policy);
  1310. up_read(&policy->rwsem);
  1311. cpufreq_cpu_put(policy);
  1312. }
  1313. return ret_freq;
  1314. }
  1315. EXPORT_SYMBOL(cpufreq_get);
  1316. static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
  1317. {
  1318. unsigned int new_freq;
  1319. new_freq = cpufreq_driver->get(policy->cpu);
  1320. if (!new_freq)
  1321. return 0;
  1322. if (!policy->cur) {
  1323. pr_debug("cpufreq: Driver did not initialize current freq\n");
  1324. policy->cur = new_freq;
  1325. } else if (policy->cur != new_freq && has_target()) {
  1326. cpufreq_out_of_sync(policy, new_freq);
  1327. }
  1328. return new_freq;
  1329. }
  1330. static struct subsys_interface cpufreq_interface = {
  1331. .name = "cpufreq",
  1332. .subsys = &cpu_subsys,
  1333. .add_dev = cpufreq_add_dev,
  1334. .remove_dev = cpufreq_remove_dev,
  1335. };
  1336. /*
  1337. * In case platform wants some specific frequency to be configured
  1338. * during suspend..
  1339. */
  1340. int cpufreq_generic_suspend(struct cpufreq_policy *policy)
  1341. {
  1342. int ret;
  1343. if (!policy->suspend_freq) {
  1344. pr_debug("%s: suspend_freq not defined\n", __func__);
  1345. return 0;
  1346. }
  1347. pr_debug("%s: Setting suspend-freq: %u\n", __func__,
  1348. policy->suspend_freq);
  1349. ret = __cpufreq_driver_target(policy, policy->suspend_freq,
  1350. CPUFREQ_RELATION_H);
  1351. if (ret)
  1352. pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
  1353. __func__, policy->suspend_freq, ret);
  1354. return ret;
  1355. }
  1356. EXPORT_SYMBOL(cpufreq_generic_suspend);
  1357. /**
  1358. * cpufreq_suspend() - Suspend CPUFreq governors
  1359. *
  1360. * Called during system wide Suspend/Hibernate cycles for suspending governors
  1361. * as some platforms can't change frequency after this point in suspend cycle.
  1362. * Because some of the devices (like: i2c, regulators, etc) they use for
  1363. * changing frequency are suspended quickly after this point.
  1364. */
  1365. void cpufreq_suspend(void)
  1366. {
  1367. struct cpufreq_policy *policy;
  1368. if (!cpufreq_driver)
  1369. return;
  1370. if (!has_target() && !cpufreq_driver->suspend)
  1371. goto suspend;
  1372. pr_debug("%s: Suspending Governors\n", __func__);
  1373. for_each_active_policy(policy) {
  1374. if (has_target()) {
  1375. down_write(&policy->rwsem);
  1376. cpufreq_stop_governor(policy);
  1377. up_write(&policy->rwsem);
  1378. }
  1379. if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
  1380. pr_err("%s: Failed to suspend driver: %p\n", __func__,
  1381. policy);
  1382. }
  1383. suspend:
  1384. cpufreq_suspended = true;
  1385. }
  1386. /**
  1387. * cpufreq_resume() - Resume CPUFreq governors
  1388. *
  1389. * Called during system wide Suspend/Hibernate cycle for resuming governors that
  1390. * are suspended with cpufreq_suspend().
  1391. */
  1392. void cpufreq_resume(void)
  1393. {
  1394. struct cpufreq_policy *policy;
  1395. int ret;
  1396. if (!cpufreq_driver)
  1397. return;
  1398. if (unlikely(!cpufreq_suspended))
  1399. return;
  1400. cpufreq_suspended = false;
  1401. if (!has_target() && !cpufreq_driver->resume)
  1402. return;
  1403. pr_debug("%s: Resuming Governors\n", __func__);
  1404. for_each_active_policy(policy) {
  1405. if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
  1406. pr_err("%s: Failed to resume driver: %p\n", __func__,
  1407. policy);
  1408. } else if (has_target()) {
  1409. down_write(&policy->rwsem);
  1410. ret = cpufreq_start_governor(policy);
  1411. up_write(&policy->rwsem);
  1412. if (ret)
  1413. pr_err("%s: Failed to start governor for policy: %p\n",
  1414. __func__, policy);
  1415. }
  1416. }
  1417. }
  1418. /**
  1419. * cpufreq_get_current_driver - return current driver's name
  1420. *
  1421. * Return the name string of the currently loaded cpufreq driver
  1422. * or NULL, if none.
  1423. */
  1424. const char *cpufreq_get_current_driver(void)
  1425. {
  1426. if (cpufreq_driver)
  1427. return cpufreq_driver->name;
  1428. return NULL;
  1429. }
  1430. EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
  1431. /**
  1432. * cpufreq_get_driver_data - return current driver data
  1433. *
  1434. * Return the private data of the currently loaded cpufreq
  1435. * driver, or NULL if no cpufreq driver is loaded.
  1436. */
  1437. void *cpufreq_get_driver_data(void)
  1438. {
  1439. if (cpufreq_driver)
  1440. return cpufreq_driver->driver_data;
  1441. return NULL;
  1442. }
  1443. EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
  1444. /*********************************************************************
  1445. * NOTIFIER LISTS INTERFACE *
  1446. *********************************************************************/
  1447. /**
  1448. * cpufreq_register_notifier - register a driver with cpufreq
  1449. * @nb: notifier function to register
  1450. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1451. *
  1452. * Add a driver to one of two lists: either a list of drivers that
  1453. * are notified about clock rate changes (once before and once after
  1454. * the transition), or a list of drivers that are notified about
  1455. * changes in cpufreq policy.
  1456. *
  1457. * This function may sleep, and has the same return conditions as
  1458. * blocking_notifier_chain_register.
  1459. */
  1460. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1461. {
  1462. int ret;
  1463. if (cpufreq_disabled())
  1464. return -EINVAL;
  1465. WARN_ON(!init_cpufreq_transition_notifier_list_called);
  1466. switch (list) {
  1467. case CPUFREQ_TRANSITION_NOTIFIER:
  1468. mutex_lock(&cpufreq_fast_switch_lock);
  1469. if (cpufreq_fast_switch_count > 0) {
  1470. mutex_unlock(&cpufreq_fast_switch_lock);
  1471. return -EBUSY;
  1472. }
  1473. ret = srcu_notifier_chain_register(
  1474. &cpufreq_transition_notifier_list, nb);
  1475. if (!ret)
  1476. cpufreq_fast_switch_count--;
  1477. mutex_unlock(&cpufreq_fast_switch_lock);
  1478. break;
  1479. case CPUFREQ_POLICY_NOTIFIER:
  1480. ret = blocking_notifier_chain_register(
  1481. &cpufreq_policy_notifier_list, nb);
  1482. break;
  1483. default:
  1484. ret = -EINVAL;
  1485. }
  1486. return ret;
  1487. }
  1488. EXPORT_SYMBOL(cpufreq_register_notifier);
  1489. /**
  1490. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1491. * @nb: notifier block to be unregistered
  1492. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1493. *
  1494. * Remove a driver from the CPU frequency notifier list.
  1495. *
  1496. * This function may sleep, and has the same return conditions as
  1497. * blocking_notifier_chain_unregister.
  1498. */
  1499. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1500. {
  1501. int ret;
  1502. if (cpufreq_disabled())
  1503. return -EINVAL;
  1504. switch (list) {
  1505. case CPUFREQ_TRANSITION_NOTIFIER:
  1506. mutex_lock(&cpufreq_fast_switch_lock);
  1507. ret = srcu_notifier_chain_unregister(
  1508. &cpufreq_transition_notifier_list, nb);
  1509. if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
  1510. cpufreq_fast_switch_count++;
  1511. mutex_unlock(&cpufreq_fast_switch_lock);
  1512. break;
  1513. case CPUFREQ_POLICY_NOTIFIER:
  1514. ret = blocking_notifier_chain_unregister(
  1515. &cpufreq_policy_notifier_list, nb);
  1516. break;
  1517. default:
  1518. ret = -EINVAL;
  1519. }
  1520. return ret;
  1521. }
  1522. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1523. /*********************************************************************
  1524. * GOVERNORS *
  1525. *********************************************************************/
  1526. /**
  1527. * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
  1528. * @policy: cpufreq policy to switch the frequency for.
  1529. * @target_freq: New frequency to set (may be approximate).
  1530. *
  1531. * Carry out a fast frequency switch without sleeping.
  1532. *
  1533. * The driver's ->fast_switch() callback invoked by this function must be
  1534. * suitable for being called from within RCU-sched read-side critical sections
  1535. * and it is expected to select the minimum available frequency greater than or
  1536. * equal to @target_freq (CPUFREQ_RELATION_L).
  1537. *
  1538. * This function must not be called if policy->fast_switch_enabled is unset.
  1539. *
  1540. * Governors calling this function must guarantee that it will never be invoked
  1541. * twice in parallel for the same policy and that it will never be called in
  1542. * parallel with either ->target() or ->target_index() for the same policy.
  1543. *
  1544. * Returns the actual frequency set for the CPU.
  1545. *
  1546. * If 0 is returned by the driver's ->fast_switch() callback to indicate an
  1547. * error condition, the hardware configuration must be preserved.
  1548. */
  1549. unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
  1550. unsigned int target_freq)
  1551. {
  1552. target_freq = clamp_val(target_freq, policy->min, policy->max);
  1553. return cpufreq_driver->fast_switch(policy, target_freq);
  1554. }
  1555. EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
  1556. /* Must set freqs->new to intermediate frequency */
  1557. static int __target_intermediate(struct cpufreq_policy *policy,
  1558. struct cpufreq_freqs *freqs, int index)
  1559. {
  1560. int ret;
  1561. freqs->new = cpufreq_driver->get_intermediate(policy, index);
  1562. /* We don't need to switch to intermediate freq */
  1563. if (!freqs->new)
  1564. return 0;
  1565. pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
  1566. __func__, policy->cpu, freqs->old, freqs->new);
  1567. cpufreq_freq_transition_begin(policy, freqs);
  1568. ret = cpufreq_driver->target_intermediate(policy, index);
  1569. cpufreq_freq_transition_end(policy, freqs, ret);
  1570. if (ret)
  1571. pr_err("%s: Failed to change to intermediate frequency: %d\n",
  1572. __func__, ret);
  1573. return ret;
  1574. }
  1575. static int __target_index(struct cpufreq_policy *policy, int index)
  1576. {
  1577. struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
  1578. unsigned int intermediate_freq = 0;
  1579. unsigned int newfreq = policy->freq_table[index].frequency;
  1580. int retval = -EINVAL;
  1581. bool notify;
  1582. if (newfreq == policy->cur)
  1583. return 0;
  1584. notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
  1585. if (notify) {
  1586. /* Handle switching to intermediate frequency */
  1587. if (cpufreq_driver->get_intermediate) {
  1588. retval = __target_intermediate(policy, &freqs, index);
  1589. if (retval)
  1590. return retval;
  1591. intermediate_freq = freqs.new;
  1592. /* Set old freq to intermediate */
  1593. if (intermediate_freq)
  1594. freqs.old = freqs.new;
  1595. }
  1596. freqs.new = newfreq;
  1597. pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
  1598. __func__, policy->cpu, freqs.old, freqs.new);
  1599. cpufreq_freq_transition_begin(policy, &freqs);
  1600. }
  1601. retval = cpufreq_driver->target_index(policy, index);
  1602. if (retval)
  1603. pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
  1604. retval);
  1605. if (notify) {
  1606. cpufreq_freq_transition_end(policy, &freqs, retval);
  1607. /*
  1608. * Failed after setting to intermediate freq? Driver should have
  1609. * reverted back to initial frequency and so should we. Check
  1610. * here for intermediate_freq instead of get_intermediate, in
  1611. * case we haven't switched to intermediate freq at all.
  1612. */
  1613. if (unlikely(retval && intermediate_freq)) {
  1614. freqs.old = intermediate_freq;
  1615. freqs.new = policy->restore_freq;
  1616. cpufreq_freq_transition_begin(policy, &freqs);
  1617. cpufreq_freq_transition_end(policy, &freqs, 0);
  1618. }
  1619. }
  1620. return retval;
  1621. }
  1622. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1623. unsigned int target_freq,
  1624. unsigned int relation)
  1625. {
  1626. unsigned int old_target_freq = target_freq;
  1627. int index;
  1628. if (cpufreq_disabled())
  1629. return -ENODEV;
  1630. /* Make sure that target_freq is within supported range */
  1631. target_freq = clamp_val(target_freq, policy->min, policy->max);
  1632. pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
  1633. policy->cpu, target_freq, relation, old_target_freq);
  1634. /*
  1635. * This might look like a redundant call as we are checking it again
  1636. * after finding index. But it is left intentionally for cases where
  1637. * exactly same freq is called again and so we can save on few function
  1638. * calls.
  1639. */
  1640. if (target_freq == policy->cur)
  1641. return 0;
  1642. /* Save last value to restore later on errors */
  1643. policy->restore_freq = policy->cur;
  1644. if (cpufreq_driver->target)
  1645. return cpufreq_driver->target(policy, target_freq, relation);
  1646. if (!cpufreq_driver->target_index)
  1647. return -EINVAL;
  1648. index = cpufreq_frequency_table_target(policy, target_freq, relation);
  1649. return __target_index(policy, index);
  1650. }
  1651. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1652. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1653. unsigned int target_freq,
  1654. unsigned int relation)
  1655. {
  1656. int ret = -EINVAL;
  1657. down_write(&policy->rwsem);
  1658. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1659. up_write(&policy->rwsem);
  1660. return ret;
  1661. }
  1662. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1663. __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
  1664. {
  1665. return NULL;
  1666. }
  1667. static int cpufreq_init_governor(struct cpufreq_policy *policy)
  1668. {
  1669. int ret;
  1670. /* Don't start any governor operations if we are entering suspend */
  1671. if (cpufreq_suspended)
  1672. return 0;
  1673. /*
  1674. * Governor might not be initiated here if ACPI _PPC changed
  1675. * notification happened, so check it.
  1676. */
  1677. if (!policy->governor)
  1678. return -EINVAL;
  1679. /* Platform doesn't want dynamic frequency switching ? */
  1680. if (policy->governor->dynamic_switching &&
  1681. cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
  1682. struct cpufreq_governor *gov = cpufreq_fallback_governor();
  1683. if (gov) {
  1684. pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
  1685. policy->governor->name, gov->name);
  1686. policy->governor = gov;
  1687. } else {
  1688. return -EINVAL;
  1689. }
  1690. }
  1691. if (!try_module_get(policy->governor->owner))
  1692. return -EINVAL;
  1693. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1694. if (policy->governor->init) {
  1695. ret = policy->governor->init(policy);
  1696. if (ret) {
  1697. module_put(policy->governor->owner);
  1698. return ret;
  1699. }
  1700. }
  1701. return 0;
  1702. }
  1703. static void cpufreq_exit_governor(struct cpufreq_policy *policy)
  1704. {
  1705. if (cpufreq_suspended || !policy->governor)
  1706. return;
  1707. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1708. if (policy->governor->exit)
  1709. policy->governor->exit(policy);
  1710. module_put(policy->governor->owner);
  1711. }
  1712. static int cpufreq_start_governor(struct cpufreq_policy *policy)
  1713. {
  1714. int ret;
  1715. if (cpufreq_suspended)
  1716. return 0;
  1717. if (!policy->governor)
  1718. return -EINVAL;
  1719. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1720. if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
  1721. cpufreq_update_current_freq(policy);
  1722. if (policy->governor->start) {
  1723. ret = policy->governor->start(policy);
  1724. if (ret)
  1725. return ret;
  1726. }
  1727. if (policy->governor->limits)
  1728. policy->governor->limits(policy);
  1729. return 0;
  1730. }
  1731. static void cpufreq_stop_governor(struct cpufreq_policy *policy)
  1732. {
  1733. if (cpufreq_suspended || !policy->governor)
  1734. return;
  1735. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1736. if (policy->governor->stop)
  1737. policy->governor->stop(policy);
  1738. }
  1739. static void cpufreq_governor_limits(struct cpufreq_policy *policy)
  1740. {
  1741. if (cpufreq_suspended || !policy->governor)
  1742. return;
  1743. pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
  1744. if (policy->governor->limits)
  1745. policy->governor->limits(policy);
  1746. }
  1747. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1748. {
  1749. int err;
  1750. if (!governor)
  1751. return -EINVAL;
  1752. if (cpufreq_disabled())
  1753. return -ENODEV;
  1754. mutex_lock(&cpufreq_governor_mutex);
  1755. err = -EBUSY;
  1756. if (!find_governor(governor->name)) {
  1757. err = 0;
  1758. list_add(&governor->governor_list, &cpufreq_governor_list);
  1759. }
  1760. mutex_unlock(&cpufreq_governor_mutex);
  1761. return err;
  1762. }
  1763. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1764. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1765. {
  1766. struct cpufreq_policy *policy;
  1767. unsigned long flags;
  1768. if (!governor)
  1769. return;
  1770. if (cpufreq_disabled())
  1771. return;
  1772. /* clear last_governor for all inactive policies */
  1773. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1774. for_each_inactive_policy(policy) {
  1775. if (!strcmp(policy->last_governor, governor->name)) {
  1776. policy->governor = NULL;
  1777. strcpy(policy->last_governor, "\0");
  1778. }
  1779. }
  1780. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1781. mutex_lock(&cpufreq_governor_mutex);
  1782. list_del(&governor->governor_list);
  1783. mutex_unlock(&cpufreq_governor_mutex);
  1784. }
  1785. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1786. /*********************************************************************
  1787. * POLICY INTERFACE *
  1788. *********************************************************************/
  1789. /**
  1790. * cpufreq_get_policy - get the current cpufreq_policy
  1791. * @policy: struct cpufreq_policy into which the current cpufreq_policy
  1792. * is written
  1793. *
  1794. * Reads the current cpufreq policy.
  1795. */
  1796. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1797. {
  1798. struct cpufreq_policy *cpu_policy;
  1799. if (!policy)
  1800. return -EINVAL;
  1801. cpu_policy = cpufreq_cpu_get(cpu);
  1802. if (!cpu_policy)
  1803. return -EINVAL;
  1804. memcpy(policy, cpu_policy, sizeof(*policy));
  1805. cpufreq_cpu_put(cpu_policy);
  1806. return 0;
  1807. }
  1808. EXPORT_SYMBOL(cpufreq_get_policy);
  1809. /*
  1810. * policy : current policy.
  1811. * new_policy: policy to be set.
  1812. */
  1813. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  1814. struct cpufreq_policy *new_policy)
  1815. {
  1816. struct cpufreq_governor *old_gov;
  1817. int ret;
  1818. pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
  1819. new_policy->cpu, new_policy->min, new_policy->max);
  1820. memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
  1821. /*
  1822. * This check works well when we store new min/max freq attributes,
  1823. * because new_policy is a copy of policy with one field updated.
  1824. */
  1825. if (new_policy->min > new_policy->max)
  1826. return -EINVAL;
  1827. /* verify the cpu speed can be set within this limit */
  1828. ret = cpufreq_driver->verify(new_policy);
  1829. if (ret)
  1830. return ret;
  1831. /* adjust if necessary - all reasons */
  1832. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1833. CPUFREQ_ADJUST, new_policy);
  1834. /*
  1835. * verify the cpu speed can be set within this limit, which might be
  1836. * different to the first one
  1837. */
  1838. ret = cpufreq_driver->verify(new_policy);
  1839. if (ret)
  1840. return ret;
  1841. /* notification of the new policy */
  1842. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1843. CPUFREQ_NOTIFY, new_policy);
  1844. policy->min = new_policy->min;
  1845. policy->max = new_policy->max;
  1846. policy->cached_target_freq = UINT_MAX;
  1847. pr_debug("new min and max freqs are %u - %u kHz\n",
  1848. policy->min, policy->max);
  1849. if (cpufreq_driver->setpolicy) {
  1850. policy->policy = new_policy->policy;
  1851. pr_debug("setting range\n");
  1852. return cpufreq_driver->setpolicy(new_policy);
  1853. }
  1854. if (new_policy->governor == policy->governor) {
  1855. pr_debug("cpufreq: governor limits update\n");
  1856. cpufreq_governor_limits(policy);
  1857. return 0;
  1858. }
  1859. pr_debug("governor switch\n");
  1860. /* save old, working values */
  1861. old_gov = policy->governor;
  1862. /* end old governor */
  1863. if (old_gov) {
  1864. cpufreq_stop_governor(policy);
  1865. cpufreq_exit_governor(policy);
  1866. }
  1867. /* start new governor */
  1868. policy->governor = new_policy->governor;
  1869. ret = cpufreq_init_governor(policy);
  1870. if (!ret) {
  1871. ret = cpufreq_start_governor(policy);
  1872. if (!ret) {
  1873. pr_debug("cpufreq: governor change\n");
  1874. return 0;
  1875. }
  1876. cpufreq_exit_governor(policy);
  1877. }
  1878. /* new governor failed, so re-start old one */
  1879. pr_debug("starting governor %s failed\n", policy->governor->name);
  1880. if (old_gov) {
  1881. policy->governor = old_gov;
  1882. if (cpufreq_init_governor(policy))
  1883. policy->governor = NULL;
  1884. else
  1885. cpufreq_start_governor(policy);
  1886. }
  1887. return ret;
  1888. }
  1889. /**
  1890. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1891. * @cpu: CPU which shall be re-evaluated
  1892. *
  1893. * Useful for policy notifiers which have different necessities
  1894. * at different times.
  1895. */
  1896. void cpufreq_update_policy(unsigned int cpu)
  1897. {
  1898. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1899. struct cpufreq_policy new_policy;
  1900. if (!policy)
  1901. return;
  1902. down_write(&policy->rwsem);
  1903. if (policy_is_inactive(policy))
  1904. goto unlock;
  1905. pr_debug("updating policy for CPU %u\n", cpu);
  1906. memcpy(&new_policy, policy, sizeof(*policy));
  1907. new_policy.min = policy->user_policy.min;
  1908. new_policy.max = policy->user_policy.max;
  1909. /*
  1910. * BIOS might change freq behind our back
  1911. * -> ask driver for current freq and notify governors about a change
  1912. */
  1913. if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
  1914. if (cpufreq_suspended)
  1915. goto unlock;
  1916. new_policy.cur = cpufreq_update_current_freq(policy);
  1917. if (WARN_ON(!new_policy.cur))
  1918. goto unlock;
  1919. }
  1920. cpufreq_set_policy(policy, &new_policy);
  1921. unlock:
  1922. up_write(&policy->rwsem);
  1923. cpufreq_cpu_put(policy);
  1924. }
  1925. EXPORT_SYMBOL(cpufreq_update_policy);
  1926. /*********************************************************************
  1927. * BOOST *
  1928. *********************************************************************/
  1929. static int cpufreq_boost_set_sw(int state)
  1930. {
  1931. struct cpufreq_policy *policy;
  1932. int ret = -EINVAL;
  1933. for_each_active_policy(policy) {
  1934. if (!policy->freq_table)
  1935. continue;
  1936. ret = cpufreq_frequency_table_cpuinfo(policy,
  1937. policy->freq_table);
  1938. if (ret) {
  1939. pr_err("%s: Policy frequency update failed\n",
  1940. __func__);
  1941. break;
  1942. }
  1943. down_write(&policy->rwsem);
  1944. policy->user_policy.max = policy->max;
  1945. cpufreq_governor_limits(policy);
  1946. up_write(&policy->rwsem);
  1947. }
  1948. return ret;
  1949. }
  1950. int cpufreq_boost_trigger_state(int state)
  1951. {
  1952. unsigned long flags;
  1953. int ret = 0;
  1954. if (cpufreq_driver->boost_enabled == state)
  1955. return 0;
  1956. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1957. cpufreq_driver->boost_enabled = state;
  1958. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1959. ret = cpufreq_driver->set_boost(state);
  1960. if (ret) {
  1961. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1962. cpufreq_driver->boost_enabled = !state;
  1963. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1964. pr_err("%s: Cannot %s BOOST\n",
  1965. __func__, state ? "enable" : "disable");
  1966. }
  1967. return ret;
  1968. }
  1969. static bool cpufreq_boost_supported(void)
  1970. {
  1971. return likely(cpufreq_driver) && cpufreq_driver->set_boost;
  1972. }
  1973. static int create_boost_sysfs_file(void)
  1974. {
  1975. int ret;
  1976. ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
  1977. if (ret)
  1978. pr_err("%s: cannot register global BOOST sysfs file\n",
  1979. __func__);
  1980. return ret;
  1981. }
  1982. static void remove_boost_sysfs_file(void)
  1983. {
  1984. if (cpufreq_boost_supported())
  1985. sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
  1986. }
  1987. int cpufreq_enable_boost_support(void)
  1988. {
  1989. if (!cpufreq_driver)
  1990. return -EINVAL;
  1991. if (cpufreq_boost_supported())
  1992. return 0;
  1993. cpufreq_driver->set_boost = cpufreq_boost_set_sw;
  1994. /* This will get removed on driver unregister */
  1995. return create_boost_sysfs_file();
  1996. }
  1997. EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
  1998. int cpufreq_boost_enabled(void)
  1999. {
  2000. return cpufreq_driver->boost_enabled;
  2001. }
  2002. EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
  2003. /*********************************************************************
  2004. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  2005. *********************************************************************/
  2006. static enum cpuhp_state hp_online;
  2007. static int cpuhp_cpufreq_online(unsigned int cpu)
  2008. {
  2009. cpufreq_online(cpu);
  2010. return 0;
  2011. }
  2012. static int cpuhp_cpufreq_offline(unsigned int cpu)
  2013. {
  2014. cpufreq_offline(cpu);
  2015. return 0;
  2016. }
  2017. /**
  2018. * cpufreq_register_driver - register a CPU Frequency driver
  2019. * @driver_data: A struct cpufreq_driver containing the values#
  2020. * submitted by the CPU Frequency driver.
  2021. *
  2022. * Registers a CPU Frequency driver to this core code. This code
  2023. * returns zero on success, -EEXIST when another driver got here first
  2024. * (and isn't unregistered in the meantime).
  2025. *
  2026. */
  2027. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  2028. {
  2029. unsigned long flags;
  2030. int ret;
  2031. if (cpufreq_disabled())
  2032. return -ENODEV;
  2033. if (!driver_data || !driver_data->verify || !driver_data->init ||
  2034. !(driver_data->setpolicy || driver_data->target_index ||
  2035. driver_data->target) ||
  2036. (driver_data->setpolicy && (driver_data->target_index ||
  2037. driver_data->target)) ||
  2038. (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
  2039. return -EINVAL;
  2040. pr_debug("trying to register driver %s\n", driver_data->name);
  2041. /* Protect against concurrent CPU online/offline. */
  2042. cpus_read_lock();
  2043. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2044. if (cpufreq_driver) {
  2045. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2046. ret = -EEXIST;
  2047. goto out;
  2048. }
  2049. cpufreq_driver = driver_data;
  2050. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2051. if (driver_data->setpolicy)
  2052. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  2053. if (cpufreq_boost_supported()) {
  2054. ret = create_boost_sysfs_file();
  2055. if (ret)
  2056. goto err_null_driver;
  2057. }
  2058. ret = subsys_interface_register(&cpufreq_interface);
  2059. if (ret)
  2060. goto err_boost_unreg;
  2061. if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
  2062. list_empty(&cpufreq_policy_list)) {
  2063. /* if all ->init() calls failed, unregister */
  2064. ret = -ENODEV;
  2065. pr_debug("%s: No CPU initialized for driver %s\n", __func__,
  2066. driver_data->name);
  2067. goto err_if_unreg;
  2068. }
  2069. ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
  2070. "cpufreq:online",
  2071. cpuhp_cpufreq_online,
  2072. cpuhp_cpufreq_offline);
  2073. if (ret < 0)
  2074. goto err_if_unreg;
  2075. hp_online = ret;
  2076. ret = 0;
  2077. pr_debug("driver %s up and running\n", driver_data->name);
  2078. goto out;
  2079. err_if_unreg:
  2080. subsys_interface_unregister(&cpufreq_interface);
  2081. err_boost_unreg:
  2082. remove_boost_sysfs_file();
  2083. err_null_driver:
  2084. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2085. cpufreq_driver = NULL;
  2086. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2087. out:
  2088. cpus_read_unlock();
  2089. return ret;
  2090. }
  2091. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  2092. /**
  2093. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  2094. *
  2095. * Unregister the current CPUFreq driver. Only call this if you have
  2096. * the right to do so, i.e. if you have succeeded in initialising before!
  2097. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  2098. * currently not initialised.
  2099. */
  2100. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  2101. {
  2102. unsigned long flags;
  2103. if (!cpufreq_driver || (driver != cpufreq_driver))
  2104. return -EINVAL;
  2105. pr_debug("unregistering driver %s\n", driver->name);
  2106. /* Protect against concurrent cpu hotplug */
  2107. cpus_read_lock();
  2108. subsys_interface_unregister(&cpufreq_interface);
  2109. remove_boost_sysfs_file();
  2110. cpuhp_remove_state_nocalls_cpuslocked(hp_online);
  2111. write_lock_irqsave(&cpufreq_driver_lock, flags);
  2112. cpufreq_driver = NULL;
  2113. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  2114. cpus_read_unlock();
  2115. return 0;
  2116. }
  2117. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  2118. /*
  2119. * Stop cpufreq at shutdown to make sure it isn't holding any locks
  2120. * or mutexes when secondary CPUs are halted.
  2121. */
  2122. static struct syscore_ops cpufreq_syscore_ops = {
  2123. .shutdown = cpufreq_suspend,
  2124. };
  2125. struct kobject *cpufreq_global_kobject;
  2126. EXPORT_SYMBOL(cpufreq_global_kobject);
  2127. static int __init cpufreq_core_init(void)
  2128. {
  2129. if (cpufreq_disabled())
  2130. return -ENODEV;
  2131. cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
  2132. BUG_ON(!cpufreq_global_kobject);
  2133. register_syscore_ops(&cpufreq_syscore_ops);
  2134. return 0;
  2135. }
  2136. module_param(off, int, 0444);
  2137. core_initcall(cpufreq_core_init);