cpufreq.c 67 KB

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