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