cpufreq.c 67 KB

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