cpufreq.c 66 KB

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