cpufreq.c 78 KB

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