cpufreq.c 67 KB

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