cpufreq.c 67 KB

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