cpufreq.c 68 KB

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