cpufreq.c 63 KB

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