cpufreq.c 67 KB

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