cpufreq.c 79 KB

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