cpufreq.c 66 KB

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