cpufreq.c 67 KB

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