cpufreq.c 66 KB

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