cpufreq.c 67 KB

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