cpufreq.c 66 KB

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