cpufreq.c 66 KB

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