cpufreq.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165
  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/syscore_ops.h>
  28. #include <linux/tick.h>
  29. #include <trace/events/power.h>
  30. /**
  31. * The "cpufreq driver" - the arch- or hardware-dependent low
  32. * level driver of CPUFreq support, and its spinlock. This lock
  33. * also protects the cpufreq_cpu_data array.
  34. */
  35. static struct cpufreq_driver *cpufreq_driver;
  36. static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
  37. static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
  38. static DEFINE_RWLOCK(cpufreq_driver_lock);
  39. static DEFINE_MUTEX(cpufreq_governor_lock);
  40. static LIST_HEAD(cpufreq_policy_list);
  41. #ifdef CONFIG_HOTPLUG_CPU
  42. /* This one keeps track of the previously set governor of a removed CPU */
  43. static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
  44. #endif
  45. /*
  46. * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
  47. * all cpufreq/hotplug/workqueue/etc related lock issues.
  48. *
  49. * The rules for this semaphore:
  50. * - Any routine that wants to read from the policy structure will
  51. * do a down_read on this semaphore.
  52. * - Any routine that will write to the policy structure and/or may take away
  53. * the policy altogether (eg. CPU hotplug), will hold this lock in write
  54. * mode before doing so.
  55. *
  56. * Additional rules:
  57. * - Governor routines that can be called in cpufreq hotplug path should not
  58. * take this sem as top level hotplug notifier handler takes this.
  59. * - Lock should not be held across
  60. * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  61. */
  62. static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
  63. static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
  64. #define lock_policy_rwsem(mode, cpu) \
  65. static int lock_policy_rwsem_##mode(int cpu) \
  66. { \
  67. int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
  68. BUG_ON(policy_cpu == -1); \
  69. down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  70. \
  71. return 0; \
  72. }
  73. lock_policy_rwsem(read, cpu);
  74. lock_policy_rwsem(write, cpu);
  75. #define unlock_policy_rwsem(mode, cpu) \
  76. static void unlock_policy_rwsem_##mode(int cpu) \
  77. { \
  78. int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
  79. BUG_ON(policy_cpu == -1); \
  80. up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  81. }
  82. unlock_policy_rwsem(read, cpu);
  83. unlock_policy_rwsem(write, cpu);
  84. /*
  85. * rwsem to guarantee that cpufreq driver module doesn't unload during critical
  86. * sections
  87. */
  88. static DECLARE_RWSEM(cpufreq_rwsem);
  89. /* internal prototypes */
  90. static int __cpufreq_governor(struct cpufreq_policy *policy,
  91. unsigned int event);
  92. static unsigned int __cpufreq_get(unsigned int cpu);
  93. static void handle_update(struct work_struct *work);
  94. /**
  95. * Two notifier lists: the "policy" list is involved in the
  96. * validation process for a new CPU frequency policy; the
  97. * "transition" list for kernel code that needs to handle
  98. * changes to devices when the CPU clock speed changes.
  99. * The mutex locks both lists.
  100. */
  101. static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
  102. static struct srcu_notifier_head cpufreq_transition_notifier_list;
  103. static bool init_cpufreq_transition_notifier_list_called;
  104. static int __init init_cpufreq_transition_notifier_list(void)
  105. {
  106. srcu_init_notifier_head(&cpufreq_transition_notifier_list);
  107. init_cpufreq_transition_notifier_list_called = true;
  108. return 0;
  109. }
  110. pure_initcall(init_cpufreq_transition_notifier_list);
  111. static int off __read_mostly;
  112. static int cpufreq_disabled(void)
  113. {
  114. return off;
  115. }
  116. void disable_cpufreq(void)
  117. {
  118. off = 1;
  119. }
  120. static LIST_HEAD(cpufreq_governor_list);
  121. static DEFINE_MUTEX(cpufreq_governor_mutex);
  122. bool have_governor_per_policy(void)
  123. {
  124. return cpufreq_driver->have_governor_per_policy;
  125. }
  126. EXPORT_SYMBOL_GPL(have_governor_per_policy);
  127. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
  128. {
  129. if (have_governor_per_policy())
  130. return &policy->kobj;
  131. else
  132. return cpufreq_global_kobject;
  133. }
  134. EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
  135. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  136. {
  137. u64 idle_time;
  138. u64 cur_wall_time;
  139. u64 busy_time;
  140. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  141. busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
  142. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
  143. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
  144. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
  145. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
  146. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
  147. idle_time = cur_wall_time - busy_time;
  148. if (wall)
  149. *wall = cputime_to_usecs(cur_wall_time);
  150. return cputime_to_usecs(idle_time);
  151. }
  152. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
  153. {
  154. u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
  155. if (idle_time == -1ULL)
  156. return get_cpu_idle_time_jiffy(cpu, wall);
  157. else if (!io_busy)
  158. idle_time += get_cpu_iowait_time_us(cpu, wall);
  159. return idle_time;
  160. }
  161. EXPORT_SYMBOL_GPL(get_cpu_idle_time);
  162. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  163. {
  164. struct cpufreq_policy *policy = NULL;
  165. unsigned long flags;
  166. if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
  167. return NULL;
  168. if (!down_read_trylock(&cpufreq_rwsem))
  169. return NULL;
  170. /* get the cpufreq driver */
  171. read_lock_irqsave(&cpufreq_driver_lock, flags);
  172. if (cpufreq_driver) {
  173. /* get the CPU */
  174. policy = per_cpu(cpufreq_cpu_data, cpu);
  175. if (policy)
  176. kobject_get(&policy->kobj);
  177. }
  178. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  179. if (!policy)
  180. up_read(&cpufreq_rwsem);
  181. return policy;
  182. }
  183. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  184. void cpufreq_cpu_put(struct cpufreq_policy *policy)
  185. {
  186. if (cpufreq_disabled())
  187. return;
  188. kobject_put(&policy->kobj);
  189. up_read(&cpufreq_rwsem);
  190. }
  191. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  192. /*********************************************************************
  193. * EXTERNALLY AFFECTING FREQUENCY CHANGES *
  194. *********************************************************************/
  195. /**
  196. * adjust_jiffies - adjust the system "loops_per_jiffy"
  197. *
  198. * This function alters the system "loops_per_jiffy" for the clock
  199. * speed change. Note that loops_per_jiffy cannot be updated on SMP
  200. * systems as each CPU might be scaled differently. So, use the arch
  201. * per-CPU loops_per_jiffy value wherever possible.
  202. */
  203. #ifndef CONFIG_SMP
  204. static unsigned long l_p_j_ref;
  205. static unsigned int l_p_j_ref_freq;
  206. static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  207. {
  208. if (ci->flags & CPUFREQ_CONST_LOOPS)
  209. return;
  210. if (!l_p_j_ref_freq) {
  211. l_p_j_ref = loops_per_jiffy;
  212. l_p_j_ref_freq = ci->old;
  213. pr_debug("saving %lu as reference value for loops_per_jiffy; "
  214. "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
  215. }
  216. if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
  217. (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
  218. loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
  219. ci->new);
  220. pr_debug("scaling loops_per_jiffy to %lu "
  221. "for frequency %u kHz\n", loops_per_jiffy, ci->new);
  222. }
  223. }
  224. #else
  225. static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  226. {
  227. return;
  228. }
  229. #endif
  230. static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
  231. struct cpufreq_freqs *freqs, unsigned int state)
  232. {
  233. BUG_ON(irqs_disabled());
  234. if (cpufreq_disabled())
  235. return;
  236. freqs->flags = cpufreq_driver->flags;
  237. pr_debug("notification %u of frequency transition to %u kHz\n",
  238. state, freqs->new);
  239. switch (state) {
  240. case CPUFREQ_PRECHANGE:
  241. if (WARN(policy->transition_ongoing ==
  242. cpumask_weight(policy->cpus),
  243. "In middle of another frequency transition\n"))
  244. return;
  245. policy->transition_ongoing++;
  246. /* detect if the driver reported a value as "old frequency"
  247. * which is not equal to what the cpufreq core thinks is
  248. * "old frequency".
  249. */
  250. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  251. if ((policy) && (policy->cpu == freqs->cpu) &&
  252. (policy->cur) && (policy->cur != freqs->old)) {
  253. pr_debug("Warning: CPU frequency is"
  254. " %u, cpufreq assumed %u kHz.\n",
  255. freqs->old, policy->cur);
  256. freqs->old = policy->cur;
  257. }
  258. }
  259. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  260. CPUFREQ_PRECHANGE, freqs);
  261. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  262. break;
  263. case CPUFREQ_POSTCHANGE:
  264. if (WARN(!policy->transition_ongoing,
  265. "No frequency transition in progress\n"))
  266. return;
  267. policy->transition_ongoing--;
  268. adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
  269. pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
  270. (unsigned long)freqs->cpu);
  271. trace_cpu_frequency(freqs->new, freqs->cpu);
  272. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  273. CPUFREQ_POSTCHANGE, freqs);
  274. if (likely(policy) && likely(policy->cpu == freqs->cpu))
  275. policy->cur = freqs->new;
  276. break;
  277. }
  278. }
  279. /**
  280. * cpufreq_notify_transition - call notifier chain and adjust_jiffies
  281. * on frequency transition.
  282. *
  283. * This function calls the transition notifiers and the "adjust_jiffies"
  284. * function. It is called twice on all CPU frequency changes that have
  285. * external effects.
  286. */
  287. void cpufreq_notify_transition(struct cpufreq_policy *policy,
  288. struct cpufreq_freqs *freqs, unsigned int state)
  289. {
  290. for_each_cpu(freqs->cpu, policy->cpus)
  291. __cpufreq_notify_transition(policy, freqs, state);
  292. }
  293. EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
  294. /*********************************************************************
  295. * SYSFS INTERFACE *
  296. *********************************************************************/
  297. static struct cpufreq_governor *__find_governor(const char *str_governor)
  298. {
  299. struct cpufreq_governor *t;
  300. list_for_each_entry(t, &cpufreq_governor_list, governor_list)
  301. if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
  302. return t;
  303. return NULL;
  304. }
  305. /**
  306. * cpufreq_parse_governor - parse a governor string
  307. */
  308. static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
  309. struct cpufreq_governor **governor)
  310. {
  311. int err = -EINVAL;
  312. if (!cpufreq_driver)
  313. goto out;
  314. if (cpufreq_driver->setpolicy) {
  315. if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
  316. *policy = CPUFREQ_POLICY_PERFORMANCE;
  317. err = 0;
  318. } else if (!strnicmp(str_governor, "powersave",
  319. CPUFREQ_NAME_LEN)) {
  320. *policy = CPUFREQ_POLICY_POWERSAVE;
  321. err = 0;
  322. }
  323. } else if (cpufreq_driver->target) {
  324. struct cpufreq_governor *t;
  325. mutex_lock(&cpufreq_governor_mutex);
  326. t = __find_governor(str_governor);
  327. if (t == NULL) {
  328. int ret;
  329. mutex_unlock(&cpufreq_governor_mutex);
  330. ret = request_module("cpufreq_%s", str_governor);
  331. mutex_lock(&cpufreq_governor_mutex);
  332. if (ret == 0)
  333. t = __find_governor(str_governor);
  334. }
  335. if (t != NULL) {
  336. *governor = t;
  337. err = 0;
  338. }
  339. mutex_unlock(&cpufreq_governor_mutex);
  340. }
  341. out:
  342. return err;
  343. }
  344. /**
  345. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  346. * print out cpufreq information
  347. *
  348. * Write out information from cpufreq_driver->policy[cpu]; object must be
  349. * "unsigned int".
  350. */
  351. #define show_one(file_name, object) \
  352. static ssize_t show_##file_name \
  353. (struct cpufreq_policy *policy, char *buf) \
  354. { \
  355. return sprintf(buf, "%u\n", policy->object); \
  356. }
  357. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  358. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  359. show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
  360. show_one(scaling_min_freq, min);
  361. show_one(scaling_max_freq, max);
  362. show_one(scaling_cur_freq, cur);
  363. static int __cpufreq_set_policy(struct cpufreq_policy *policy,
  364. struct cpufreq_policy *new_policy);
  365. /**
  366. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  367. */
  368. #define store_one(file_name, object) \
  369. static ssize_t store_##file_name \
  370. (struct cpufreq_policy *policy, const char *buf, size_t count) \
  371. { \
  372. unsigned int ret; \
  373. struct cpufreq_policy new_policy; \
  374. \
  375. ret = cpufreq_get_policy(&new_policy, policy->cpu); \
  376. if (ret) \
  377. return -EINVAL; \
  378. \
  379. ret = sscanf(buf, "%u", &new_policy.object); \
  380. if (ret != 1) \
  381. return -EINVAL; \
  382. \
  383. ret = __cpufreq_set_policy(policy, &new_policy); \
  384. policy->user_policy.object = policy->object; \
  385. \
  386. return ret ? ret : count; \
  387. }
  388. store_one(scaling_min_freq, min);
  389. store_one(scaling_max_freq, max);
  390. /**
  391. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  392. */
  393. static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
  394. char *buf)
  395. {
  396. unsigned int cur_freq = __cpufreq_get(policy->cpu);
  397. if (!cur_freq)
  398. return sprintf(buf, "<unknown>");
  399. return sprintf(buf, "%u\n", cur_freq);
  400. }
  401. /**
  402. * show_scaling_governor - show the current policy for the specified CPU
  403. */
  404. static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
  405. {
  406. if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
  407. return sprintf(buf, "powersave\n");
  408. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  409. return sprintf(buf, "performance\n");
  410. else if (policy->governor)
  411. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
  412. policy->governor->name);
  413. return -EINVAL;
  414. }
  415. /**
  416. * store_scaling_governor - store policy for the specified CPU
  417. */
  418. static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
  419. const char *buf, size_t count)
  420. {
  421. unsigned int ret;
  422. char str_governor[16];
  423. struct cpufreq_policy new_policy;
  424. ret = cpufreq_get_policy(&new_policy, policy->cpu);
  425. if (ret)
  426. return ret;
  427. ret = sscanf(buf, "%15s", str_governor);
  428. if (ret != 1)
  429. return -EINVAL;
  430. if (cpufreq_parse_governor(str_governor, &new_policy.policy,
  431. &new_policy.governor))
  432. return -EINVAL;
  433. /*
  434. * Do not use cpufreq_set_policy here or the user_policy.max
  435. * will be wrongly overridden
  436. */
  437. ret = __cpufreq_set_policy(policy, &new_policy);
  438. policy->user_policy.policy = policy->policy;
  439. policy->user_policy.governor = policy->governor;
  440. if (ret)
  441. return ret;
  442. else
  443. return count;
  444. }
  445. /**
  446. * show_scaling_driver - show the cpufreq driver currently loaded
  447. */
  448. static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
  449. {
  450. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
  451. }
  452. /**
  453. * show_scaling_available_governors - show the available CPUfreq governors
  454. */
  455. static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
  456. char *buf)
  457. {
  458. ssize_t i = 0;
  459. struct cpufreq_governor *t;
  460. if (!cpufreq_driver->target) {
  461. i += sprintf(buf, "performance powersave");
  462. goto out;
  463. }
  464. list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
  465. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
  466. - (CPUFREQ_NAME_LEN + 2)))
  467. goto out;
  468. i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
  469. }
  470. out:
  471. i += sprintf(&buf[i], "\n");
  472. return i;
  473. }
  474. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
  475. {
  476. ssize_t i = 0;
  477. unsigned int cpu;
  478. for_each_cpu(cpu, mask) {
  479. if (i)
  480. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
  481. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
  482. if (i >= (PAGE_SIZE - 5))
  483. break;
  484. }
  485. i += sprintf(&buf[i], "\n");
  486. return i;
  487. }
  488. EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
  489. /**
  490. * show_related_cpus - show the CPUs affected by each transition even if
  491. * hw coordination is in use
  492. */
  493. static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
  494. {
  495. return cpufreq_show_cpus(policy->related_cpus, buf);
  496. }
  497. /**
  498. * show_affected_cpus - show the CPUs affected by each transition
  499. */
  500. static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
  501. {
  502. return cpufreq_show_cpus(policy->cpus, buf);
  503. }
  504. static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
  505. const char *buf, size_t count)
  506. {
  507. unsigned int freq = 0;
  508. unsigned int ret;
  509. if (!policy->governor || !policy->governor->store_setspeed)
  510. return -EINVAL;
  511. ret = sscanf(buf, "%u", &freq);
  512. if (ret != 1)
  513. return -EINVAL;
  514. policy->governor->store_setspeed(policy, freq);
  515. return count;
  516. }
  517. static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
  518. {
  519. if (!policy->governor || !policy->governor->show_setspeed)
  520. return sprintf(buf, "<unsupported>\n");
  521. return policy->governor->show_setspeed(policy, buf);
  522. }
  523. /**
  524. * show_bios_limit - show the current cpufreq HW/BIOS limitation
  525. */
  526. static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
  527. {
  528. unsigned int limit;
  529. int ret;
  530. if (cpufreq_driver->bios_limit) {
  531. ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
  532. if (!ret)
  533. return sprintf(buf, "%u\n", limit);
  534. }
  535. return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
  536. }
  537. cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
  538. cpufreq_freq_attr_ro(cpuinfo_min_freq);
  539. cpufreq_freq_attr_ro(cpuinfo_max_freq);
  540. cpufreq_freq_attr_ro(cpuinfo_transition_latency);
  541. cpufreq_freq_attr_ro(scaling_available_governors);
  542. cpufreq_freq_attr_ro(scaling_driver);
  543. cpufreq_freq_attr_ro(scaling_cur_freq);
  544. cpufreq_freq_attr_ro(bios_limit);
  545. cpufreq_freq_attr_ro(related_cpus);
  546. cpufreq_freq_attr_ro(affected_cpus);
  547. cpufreq_freq_attr_rw(scaling_min_freq);
  548. cpufreq_freq_attr_rw(scaling_max_freq);
  549. cpufreq_freq_attr_rw(scaling_governor);
  550. cpufreq_freq_attr_rw(scaling_setspeed);
  551. static struct attribute *default_attrs[] = {
  552. &cpuinfo_min_freq.attr,
  553. &cpuinfo_max_freq.attr,
  554. &cpuinfo_transition_latency.attr,
  555. &scaling_min_freq.attr,
  556. &scaling_max_freq.attr,
  557. &affected_cpus.attr,
  558. &related_cpus.attr,
  559. &scaling_governor.attr,
  560. &scaling_driver.attr,
  561. &scaling_available_governors.attr,
  562. &scaling_setspeed.attr,
  563. NULL
  564. };
  565. #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
  566. #define to_attr(a) container_of(a, struct freq_attr, attr)
  567. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  568. {
  569. struct cpufreq_policy *policy = to_policy(kobj);
  570. struct freq_attr *fattr = to_attr(attr);
  571. ssize_t ret = -EINVAL;
  572. if (!down_read_trylock(&cpufreq_rwsem))
  573. goto exit;
  574. if (lock_policy_rwsem_read(policy->cpu) < 0)
  575. goto up_read;
  576. if (fattr->show)
  577. ret = fattr->show(policy, buf);
  578. else
  579. ret = -EIO;
  580. unlock_policy_rwsem_read(policy->cpu);
  581. up_read:
  582. up_read(&cpufreq_rwsem);
  583. exit:
  584. return ret;
  585. }
  586. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  587. const char *buf, size_t count)
  588. {
  589. struct cpufreq_policy *policy = to_policy(kobj);
  590. struct freq_attr *fattr = to_attr(attr);
  591. ssize_t ret = -EINVAL;
  592. if (!down_read_trylock(&cpufreq_rwsem))
  593. goto exit;
  594. if (lock_policy_rwsem_write(policy->cpu) < 0)
  595. goto up_read;
  596. if (fattr->store)
  597. ret = fattr->store(policy, buf, count);
  598. else
  599. ret = -EIO;
  600. unlock_policy_rwsem_write(policy->cpu);
  601. up_read:
  602. up_read(&cpufreq_rwsem);
  603. exit:
  604. return ret;
  605. }
  606. static void cpufreq_sysfs_release(struct kobject *kobj)
  607. {
  608. struct cpufreq_policy *policy = to_policy(kobj);
  609. pr_debug("last reference is dropped\n");
  610. complete(&policy->kobj_unregister);
  611. }
  612. static const struct sysfs_ops sysfs_ops = {
  613. .show = show,
  614. .store = store,
  615. };
  616. static struct kobj_type ktype_cpufreq = {
  617. .sysfs_ops = &sysfs_ops,
  618. .default_attrs = default_attrs,
  619. .release = cpufreq_sysfs_release,
  620. };
  621. struct kobject *cpufreq_global_kobject;
  622. EXPORT_SYMBOL(cpufreq_global_kobject);
  623. static int cpufreq_global_kobject_usage;
  624. int cpufreq_get_global_kobject(void)
  625. {
  626. if (!cpufreq_global_kobject_usage++)
  627. return kobject_add(cpufreq_global_kobject,
  628. &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
  629. return 0;
  630. }
  631. EXPORT_SYMBOL(cpufreq_get_global_kobject);
  632. void cpufreq_put_global_kobject(void)
  633. {
  634. if (!--cpufreq_global_kobject_usage)
  635. kobject_del(cpufreq_global_kobject);
  636. }
  637. EXPORT_SYMBOL(cpufreq_put_global_kobject);
  638. int cpufreq_sysfs_create_file(const struct attribute *attr)
  639. {
  640. int ret = cpufreq_get_global_kobject();
  641. if (!ret) {
  642. ret = sysfs_create_file(cpufreq_global_kobject, attr);
  643. if (ret)
  644. cpufreq_put_global_kobject();
  645. }
  646. return ret;
  647. }
  648. EXPORT_SYMBOL(cpufreq_sysfs_create_file);
  649. void cpufreq_sysfs_remove_file(const struct attribute *attr)
  650. {
  651. sysfs_remove_file(cpufreq_global_kobject, attr);
  652. cpufreq_put_global_kobject();
  653. }
  654. EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
  655. /* symlink affected CPUs */
  656. static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
  657. {
  658. unsigned int j;
  659. int ret = 0;
  660. for_each_cpu(j, policy->cpus) {
  661. struct device *cpu_dev;
  662. if (j == policy->cpu)
  663. continue;
  664. pr_debug("Adding link for CPU: %u\n", j);
  665. cpu_dev = get_cpu_device(j);
  666. ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
  667. "cpufreq");
  668. if (ret)
  669. break;
  670. }
  671. return ret;
  672. }
  673. static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
  674. struct device *dev)
  675. {
  676. struct freq_attr **drv_attr;
  677. int ret = 0;
  678. /* prepare interface data */
  679. ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
  680. &dev->kobj, "cpufreq");
  681. if (ret)
  682. return ret;
  683. /* set up files for this cpu device */
  684. drv_attr = cpufreq_driver->attr;
  685. while ((drv_attr) && (*drv_attr)) {
  686. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  687. if (ret)
  688. goto err_out_kobj_put;
  689. drv_attr++;
  690. }
  691. if (cpufreq_driver->get) {
  692. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  693. if (ret)
  694. goto err_out_kobj_put;
  695. }
  696. if (cpufreq_driver->target) {
  697. ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  698. if (ret)
  699. goto err_out_kobj_put;
  700. }
  701. if (cpufreq_driver->bios_limit) {
  702. ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
  703. if (ret)
  704. goto err_out_kobj_put;
  705. }
  706. ret = cpufreq_add_dev_symlink(policy);
  707. if (ret)
  708. goto err_out_kobj_put;
  709. return ret;
  710. err_out_kobj_put:
  711. kobject_put(&policy->kobj);
  712. wait_for_completion(&policy->kobj_unregister);
  713. return ret;
  714. }
  715. static void cpufreq_init_policy(struct cpufreq_policy *policy)
  716. {
  717. struct cpufreq_policy new_policy;
  718. int ret = 0;
  719. memcpy(&new_policy, policy, sizeof(*policy));
  720. /* assure that the starting sequence is run in __cpufreq_set_policy */
  721. policy->governor = NULL;
  722. /* set default policy */
  723. ret = __cpufreq_set_policy(policy, &new_policy);
  724. policy->user_policy.policy = policy->policy;
  725. policy->user_policy.governor = policy->governor;
  726. if (ret) {
  727. pr_debug("setting policy failed\n");
  728. if (cpufreq_driver->exit)
  729. cpufreq_driver->exit(policy);
  730. }
  731. }
  732. #ifdef CONFIG_HOTPLUG_CPU
  733. static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
  734. unsigned int cpu, struct device *dev,
  735. bool frozen)
  736. {
  737. int ret = 0, has_target = !!cpufreq_driver->target;
  738. unsigned long flags;
  739. if (has_target) {
  740. ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
  741. if (ret) {
  742. pr_err("%s: Failed to stop governor\n", __func__);
  743. return ret;
  744. }
  745. }
  746. lock_policy_rwsem_write(policy->cpu);
  747. write_lock_irqsave(&cpufreq_driver_lock, flags);
  748. cpumask_set_cpu(cpu, policy->cpus);
  749. per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
  750. per_cpu(cpufreq_cpu_data, cpu) = policy;
  751. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  752. unlock_policy_rwsem_write(policy->cpu);
  753. if (has_target) {
  754. if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
  755. (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
  756. pr_err("%s: Failed to start governor\n", __func__);
  757. return ret;
  758. }
  759. }
  760. /* Don't touch sysfs links during light-weight init */
  761. if (!frozen)
  762. ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
  763. return ret;
  764. }
  765. #endif
  766. static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
  767. {
  768. struct cpufreq_policy *policy;
  769. unsigned long flags;
  770. write_lock_irqsave(&cpufreq_driver_lock, flags);
  771. policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
  772. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  773. return policy;
  774. }
  775. static struct cpufreq_policy *cpufreq_policy_alloc(void)
  776. {
  777. struct cpufreq_policy *policy;
  778. policy = kzalloc(sizeof(*policy), GFP_KERNEL);
  779. if (!policy)
  780. return NULL;
  781. if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
  782. goto err_free_policy;
  783. if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
  784. goto err_free_cpumask;
  785. INIT_LIST_HEAD(&policy->policy_list);
  786. return policy;
  787. err_free_cpumask:
  788. free_cpumask_var(policy->cpus);
  789. err_free_policy:
  790. kfree(policy);
  791. return NULL;
  792. }
  793. static void cpufreq_policy_free(struct cpufreq_policy *policy)
  794. {
  795. unsigned long flags;
  796. write_lock_irqsave(&cpufreq_driver_lock, flags);
  797. list_del(&policy->policy_list);
  798. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  799. free_cpumask_var(policy->related_cpus);
  800. free_cpumask_var(policy->cpus);
  801. kfree(policy);
  802. }
  803. static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
  804. bool frozen)
  805. {
  806. unsigned int j, cpu = dev->id;
  807. int ret = -ENOMEM;
  808. struct cpufreq_policy *policy;
  809. unsigned long flags;
  810. #ifdef CONFIG_HOTPLUG_CPU
  811. struct cpufreq_governor *gov;
  812. int sibling;
  813. #endif
  814. if (cpu_is_offline(cpu))
  815. return 0;
  816. pr_debug("adding CPU %u\n", cpu);
  817. #ifdef CONFIG_SMP
  818. /* check whether a different CPU already registered this
  819. * CPU because it is in the same boat. */
  820. policy = cpufreq_cpu_get(cpu);
  821. if (unlikely(policy)) {
  822. cpufreq_cpu_put(policy);
  823. return 0;
  824. }
  825. if (!down_read_trylock(&cpufreq_rwsem))
  826. return 0;
  827. #ifdef CONFIG_HOTPLUG_CPU
  828. /* Check if this cpu was hot-unplugged earlier and has siblings */
  829. read_lock_irqsave(&cpufreq_driver_lock, flags);
  830. for_each_online_cpu(sibling) {
  831. struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
  832. if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
  833. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  834. ret = cpufreq_add_policy_cpu(cp, cpu, dev, frozen);
  835. up_read(&cpufreq_rwsem);
  836. return ret;
  837. }
  838. }
  839. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  840. #endif
  841. #endif
  842. if (frozen)
  843. /* Restore the saved policy when doing light-weight init */
  844. policy = cpufreq_policy_restore(cpu);
  845. else
  846. policy = cpufreq_policy_alloc();
  847. if (!policy)
  848. goto nomem_out;
  849. policy->cpu = cpu;
  850. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  851. cpumask_copy(policy->cpus, cpumask_of(cpu));
  852. /* Initially set CPU itself as the policy_cpu */
  853. per_cpu(cpufreq_policy_cpu, cpu) = cpu;
  854. init_completion(&policy->kobj_unregister);
  855. INIT_WORK(&policy->update, handle_update);
  856. /* call driver. From then on the cpufreq must be able
  857. * to accept all calls to ->verify and ->setpolicy for this CPU
  858. */
  859. ret = cpufreq_driver->init(policy);
  860. if (ret) {
  861. pr_debug("initialization failed\n");
  862. goto err_set_policy_cpu;
  863. }
  864. /* related cpus should atleast have policy->cpus */
  865. cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
  866. /*
  867. * affected cpus must always be the one, which are online. We aren't
  868. * managing offline cpus here.
  869. */
  870. cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
  871. policy->user_policy.min = policy->min;
  872. policy->user_policy.max = policy->max;
  873. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  874. CPUFREQ_START, policy);
  875. #ifdef CONFIG_HOTPLUG_CPU
  876. gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
  877. if (gov) {
  878. policy->governor = gov;
  879. pr_debug("Restoring governor %s for cpu %d\n",
  880. policy->governor->name, cpu);
  881. }
  882. #endif
  883. write_lock_irqsave(&cpufreq_driver_lock, flags);
  884. for_each_cpu(j, policy->cpus) {
  885. per_cpu(cpufreq_cpu_data, j) = policy;
  886. per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
  887. }
  888. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  889. if (!frozen) {
  890. ret = cpufreq_add_dev_interface(policy, dev);
  891. if (ret)
  892. goto err_out_unregister;
  893. write_lock_irqsave(&cpufreq_driver_lock, flags);
  894. list_add(&policy->policy_list, &cpufreq_policy_list);
  895. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  896. }
  897. cpufreq_init_policy(policy);
  898. kobject_uevent(&policy->kobj, KOBJ_ADD);
  899. up_read(&cpufreq_rwsem);
  900. pr_debug("initialization complete\n");
  901. return 0;
  902. err_out_unregister:
  903. write_lock_irqsave(&cpufreq_driver_lock, flags);
  904. for_each_cpu(j, policy->cpus) {
  905. per_cpu(cpufreq_cpu_data, j) = NULL;
  906. if (j != cpu)
  907. per_cpu(cpufreq_policy_cpu, j) = -1;
  908. }
  909. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  910. err_set_policy_cpu:
  911. per_cpu(cpufreq_policy_cpu, cpu) = -1;
  912. cpufreq_policy_free(policy);
  913. nomem_out:
  914. up_read(&cpufreq_rwsem);
  915. return ret;
  916. }
  917. /**
  918. * cpufreq_add_dev - add a CPU device
  919. *
  920. * Adds the cpufreq interface for a CPU device.
  921. *
  922. * The Oracle says: try running cpufreq registration/unregistration concurrently
  923. * with with cpu hotplugging and all hell will break loose. Tried to clean this
  924. * mess up, but more thorough testing is needed. - Mathieu
  925. */
  926. static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
  927. {
  928. return __cpufreq_add_dev(dev, sif, false);
  929. }
  930. static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
  931. {
  932. int j;
  933. policy->last_cpu = policy->cpu;
  934. policy->cpu = cpu;
  935. for_each_cpu(j, policy->cpus)
  936. per_cpu(cpufreq_policy_cpu, j) = cpu;
  937. #ifdef CONFIG_CPU_FREQ_TABLE
  938. cpufreq_frequency_table_update_policy_cpu(policy);
  939. #endif
  940. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  941. CPUFREQ_UPDATE_POLICY_CPU, policy);
  942. }
  943. static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
  944. unsigned int old_cpu, bool frozen)
  945. {
  946. struct device *cpu_dev;
  947. unsigned long flags;
  948. int ret;
  949. /* first sibling now owns the new sysfs dir */
  950. cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
  951. /* Don't touch sysfs files during light-weight tear-down */
  952. if (frozen)
  953. return cpu_dev->id;
  954. sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
  955. ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
  956. if (ret) {
  957. pr_err("%s: Failed to move kobj: %d", __func__, ret);
  958. WARN_ON(lock_policy_rwsem_write(old_cpu));
  959. cpumask_set_cpu(old_cpu, policy->cpus);
  960. write_lock_irqsave(&cpufreq_driver_lock, flags);
  961. per_cpu(cpufreq_cpu_data, old_cpu) = policy;
  962. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  963. unlock_policy_rwsem_write(old_cpu);
  964. ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
  965. "cpufreq");
  966. return -EINVAL;
  967. }
  968. return cpu_dev->id;
  969. }
  970. /**
  971. * __cpufreq_remove_dev - remove a CPU device
  972. *
  973. * Removes the cpufreq interface for a CPU device.
  974. * Caller should already have policy_rwsem in write mode for this CPU.
  975. * This routine frees the rwsem before returning.
  976. */
  977. static int __cpufreq_remove_dev(struct device *dev,
  978. struct subsys_interface *sif, bool frozen)
  979. {
  980. unsigned int cpu = dev->id, cpus;
  981. int new_cpu, ret;
  982. unsigned long flags;
  983. struct cpufreq_policy *policy;
  984. struct kobject *kobj;
  985. struct completion *cmp;
  986. pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
  987. write_lock_irqsave(&cpufreq_driver_lock, flags);
  988. policy = per_cpu(cpufreq_cpu_data, cpu);
  989. per_cpu(cpufreq_cpu_data, cpu) = NULL;
  990. /* Save the policy somewhere when doing a light-weight tear-down */
  991. if (frozen)
  992. per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
  993. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  994. if (!policy) {
  995. pr_debug("%s: No cpu_data found\n", __func__);
  996. return -EINVAL;
  997. }
  998. if (cpufreq_driver->target) {
  999. ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
  1000. if (ret) {
  1001. pr_err("%s: Failed to stop governor\n", __func__);
  1002. return ret;
  1003. }
  1004. }
  1005. #ifdef CONFIG_HOTPLUG_CPU
  1006. if (!cpufreq_driver->setpolicy)
  1007. strncpy(per_cpu(cpufreq_cpu_governor, cpu),
  1008. policy->governor->name, CPUFREQ_NAME_LEN);
  1009. #endif
  1010. WARN_ON(lock_policy_rwsem_write(cpu));
  1011. cpus = cpumask_weight(policy->cpus);
  1012. if (cpus > 1)
  1013. cpumask_clear_cpu(cpu, policy->cpus);
  1014. unlock_policy_rwsem_write(cpu);
  1015. if (cpu != policy->cpu && !frozen) {
  1016. sysfs_remove_link(&dev->kobj, "cpufreq");
  1017. } else if (cpus > 1) {
  1018. new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
  1019. if (new_cpu >= 0) {
  1020. WARN_ON(lock_policy_rwsem_write(cpu));
  1021. update_policy_cpu(policy, new_cpu);
  1022. unlock_policy_rwsem_write(cpu);
  1023. if (!frozen) {
  1024. pr_debug("%s: policy Kobject moved to cpu: %d "
  1025. "from: %d\n",__func__, new_cpu, cpu);
  1026. }
  1027. }
  1028. }
  1029. /* If cpu is last user of policy, free policy */
  1030. if (cpus == 1) {
  1031. if (cpufreq_driver->target) {
  1032. ret = __cpufreq_governor(policy,
  1033. CPUFREQ_GOV_POLICY_EXIT);
  1034. if (ret) {
  1035. pr_err("%s: Failed to exit governor\n",
  1036. __func__);
  1037. return ret;
  1038. }
  1039. }
  1040. if (!frozen) {
  1041. lock_policy_rwsem_read(cpu);
  1042. kobj = &policy->kobj;
  1043. cmp = &policy->kobj_unregister;
  1044. unlock_policy_rwsem_read(cpu);
  1045. kobject_put(kobj);
  1046. /*
  1047. * We need to make sure that the underlying kobj is
  1048. * actually not referenced anymore by anybody before we
  1049. * proceed with unloading.
  1050. */
  1051. pr_debug("waiting for dropping of refcount\n");
  1052. wait_for_completion(cmp);
  1053. pr_debug("wait complete\n");
  1054. }
  1055. /*
  1056. * Perform the ->exit() even during light-weight tear-down,
  1057. * since this is a core component, and is essential for the
  1058. * subsequent light-weight ->init() to succeed.
  1059. */
  1060. if (cpufreq_driver->exit)
  1061. cpufreq_driver->exit(policy);
  1062. if (!frozen)
  1063. cpufreq_policy_free(policy);
  1064. } else {
  1065. if (cpufreq_driver->target) {
  1066. if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
  1067. (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
  1068. pr_err("%s: Failed to start governor\n",
  1069. __func__);
  1070. return ret;
  1071. }
  1072. }
  1073. }
  1074. per_cpu(cpufreq_policy_cpu, cpu) = -1;
  1075. return 0;
  1076. }
  1077. static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  1078. {
  1079. unsigned int cpu = dev->id;
  1080. int retval;
  1081. if (cpu_is_offline(cpu))
  1082. return 0;
  1083. retval = __cpufreq_remove_dev(dev, sif, false);
  1084. return retval;
  1085. }
  1086. static void handle_update(struct work_struct *work)
  1087. {
  1088. struct cpufreq_policy *policy =
  1089. container_of(work, struct cpufreq_policy, update);
  1090. unsigned int cpu = policy->cpu;
  1091. pr_debug("handle_update for cpu %u called\n", cpu);
  1092. cpufreq_update_policy(cpu);
  1093. }
  1094. /**
  1095. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
  1096. * in deep trouble.
  1097. * @cpu: cpu number
  1098. * @old_freq: CPU frequency the kernel thinks the CPU runs at
  1099. * @new_freq: CPU frequency the CPU actually runs at
  1100. *
  1101. * We adjust to current frequency first, and need to clean up later.
  1102. * So either call to cpufreq_update_policy() or schedule handle_update()).
  1103. */
  1104. static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
  1105. unsigned int new_freq)
  1106. {
  1107. struct cpufreq_policy *policy;
  1108. struct cpufreq_freqs freqs;
  1109. unsigned long flags;
  1110. pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
  1111. "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
  1112. freqs.old = old_freq;
  1113. freqs.new = new_freq;
  1114. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1115. policy = per_cpu(cpufreq_cpu_data, cpu);
  1116. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1117. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  1118. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  1119. }
  1120. /**
  1121. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  1122. * @cpu: CPU number
  1123. *
  1124. * This is the last known freq, without actually getting it from the driver.
  1125. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  1126. */
  1127. unsigned int cpufreq_quick_get(unsigned int cpu)
  1128. {
  1129. struct cpufreq_policy *policy;
  1130. unsigned int ret_freq = 0;
  1131. if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
  1132. return cpufreq_driver->get(cpu);
  1133. policy = cpufreq_cpu_get(cpu);
  1134. if (policy) {
  1135. ret_freq = policy->cur;
  1136. cpufreq_cpu_put(policy);
  1137. }
  1138. return ret_freq;
  1139. }
  1140. EXPORT_SYMBOL(cpufreq_quick_get);
  1141. /**
  1142. * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
  1143. * @cpu: CPU number
  1144. *
  1145. * Just return the max possible frequency for a given CPU.
  1146. */
  1147. unsigned int cpufreq_quick_get_max(unsigned int cpu)
  1148. {
  1149. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1150. unsigned int ret_freq = 0;
  1151. if (policy) {
  1152. ret_freq = policy->max;
  1153. cpufreq_cpu_put(policy);
  1154. }
  1155. return ret_freq;
  1156. }
  1157. EXPORT_SYMBOL(cpufreq_quick_get_max);
  1158. static unsigned int __cpufreq_get(unsigned int cpu)
  1159. {
  1160. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1161. unsigned int ret_freq = 0;
  1162. if (!cpufreq_driver->get)
  1163. return ret_freq;
  1164. ret_freq = cpufreq_driver->get(cpu);
  1165. if (ret_freq && policy->cur &&
  1166. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1167. /* verify no discrepancy between actual and
  1168. saved value exists */
  1169. if (unlikely(ret_freq != policy->cur)) {
  1170. cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
  1171. schedule_work(&policy->update);
  1172. }
  1173. }
  1174. return ret_freq;
  1175. }
  1176. /**
  1177. * cpufreq_get - get the current CPU frequency (in kHz)
  1178. * @cpu: CPU number
  1179. *
  1180. * Get the CPU current (static) CPU frequency
  1181. */
  1182. unsigned int cpufreq_get(unsigned int cpu)
  1183. {
  1184. unsigned int ret_freq = 0;
  1185. if (!down_read_trylock(&cpufreq_rwsem))
  1186. return 0;
  1187. if (unlikely(lock_policy_rwsem_read(cpu)))
  1188. goto out_policy;
  1189. ret_freq = __cpufreq_get(cpu);
  1190. unlock_policy_rwsem_read(cpu);
  1191. out_policy:
  1192. up_read(&cpufreq_rwsem);
  1193. return ret_freq;
  1194. }
  1195. EXPORT_SYMBOL(cpufreq_get);
  1196. static struct subsys_interface cpufreq_interface = {
  1197. .name = "cpufreq",
  1198. .subsys = &cpu_subsys,
  1199. .add_dev = cpufreq_add_dev,
  1200. .remove_dev = cpufreq_remove_dev,
  1201. };
  1202. /**
  1203. * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
  1204. *
  1205. * This function is only executed for the boot processor. The other CPUs
  1206. * have been put offline by means of CPU hotplug.
  1207. */
  1208. static int cpufreq_bp_suspend(void)
  1209. {
  1210. int ret = 0;
  1211. int cpu = smp_processor_id();
  1212. struct cpufreq_policy *policy;
  1213. pr_debug("suspending cpu %u\n", cpu);
  1214. /* If there's no policy for the boot CPU, we have nothing to do. */
  1215. policy = cpufreq_cpu_get(cpu);
  1216. if (!policy)
  1217. return 0;
  1218. if (cpufreq_driver->suspend) {
  1219. ret = cpufreq_driver->suspend(policy);
  1220. if (ret)
  1221. printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
  1222. "step on CPU %u\n", policy->cpu);
  1223. }
  1224. cpufreq_cpu_put(policy);
  1225. return ret;
  1226. }
  1227. /**
  1228. * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
  1229. *
  1230. * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
  1231. * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
  1232. * restored. It will verify that the current freq is in sync with
  1233. * what we believe it to be. This is a bit later than when it
  1234. * should be, but nonethteless it's better than calling
  1235. * cpufreq_driver->get() here which might re-enable interrupts...
  1236. *
  1237. * This function is only executed for the boot CPU. The other CPUs have not
  1238. * been turned on yet.
  1239. */
  1240. static void cpufreq_bp_resume(void)
  1241. {
  1242. int ret = 0;
  1243. int cpu = smp_processor_id();
  1244. struct cpufreq_policy *policy;
  1245. pr_debug("resuming cpu %u\n", cpu);
  1246. /* If there's no policy for the boot CPU, we have nothing to do. */
  1247. policy = cpufreq_cpu_get(cpu);
  1248. if (!policy)
  1249. return;
  1250. if (cpufreq_driver->resume) {
  1251. ret = cpufreq_driver->resume(policy);
  1252. if (ret) {
  1253. printk(KERN_ERR "cpufreq: resume failed in ->resume "
  1254. "step on CPU %u\n", policy->cpu);
  1255. goto fail;
  1256. }
  1257. }
  1258. schedule_work(&policy->update);
  1259. fail:
  1260. cpufreq_cpu_put(policy);
  1261. }
  1262. static struct syscore_ops cpufreq_syscore_ops = {
  1263. .suspend = cpufreq_bp_suspend,
  1264. .resume = cpufreq_bp_resume,
  1265. };
  1266. /**
  1267. * cpufreq_get_current_driver - return current driver's name
  1268. *
  1269. * Return the name string of the currently loaded cpufreq driver
  1270. * or NULL, if none.
  1271. */
  1272. const char *cpufreq_get_current_driver(void)
  1273. {
  1274. if (cpufreq_driver)
  1275. return cpufreq_driver->name;
  1276. return NULL;
  1277. }
  1278. EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
  1279. /*********************************************************************
  1280. * NOTIFIER LISTS INTERFACE *
  1281. *********************************************************************/
  1282. /**
  1283. * cpufreq_register_notifier - register a driver with cpufreq
  1284. * @nb: notifier function to register
  1285. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1286. *
  1287. * Add a driver to one of two lists: either a list of drivers that
  1288. * are notified about clock rate changes (once before and once after
  1289. * the transition), or a list of drivers that are notified about
  1290. * changes in cpufreq policy.
  1291. *
  1292. * This function may sleep, and has the same return conditions as
  1293. * blocking_notifier_chain_register.
  1294. */
  1295. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1296. {
  1297. int ret;
  1298. if (cpufreq_disabled())
  1299. return -EINVAL;
  1300. WARN_ON(!init_cpufreq_transition_notifier_list_called);
  1301. switch (list) {
  1302. case CPUFREQ_TRANSITION_NOTIFIER:
  1303. ret = srcu_notifier_chain_register(
  1304. &cpufreq_transition_notifier_list, nb);
  1305. break;
  1306. case CPUFREQ_POLICY_NOTIFIER:
  1307. ret = blocking_notifier_chain_register(
  1308. &cpufreq_policy_notifier_list, nb);
  1309. break;
  1310. default:
  1311. ret = -EINVAL;
  1312. }
  1313. return ret;
  1314. }
  1315. EXPORT_SYMBOL(cpufreq_register_notifier);
  1316. /**
  1317. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1318. * @nb: notifier block to be unregistered
  1319. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1320. *
  1321. * Remove a driver from the CPU frequency notifier list.
  1322. *
  1323. * This function may sleep, and has the same return conditions as
  1324. * blocking_notifier_chain_unregister.
  1325. */
  1326. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1327. {
  1328. int ret;
  1329. if (cpufreq_disabled())
  1330. return -EINVAL;
  1331. switch (list) {
  1332. case CPUFREQ_TRANSITION_NOTIFIER:
  1333. ret = srcu_notifier_chain_unregister(
  1334. &cpufreq_transition_notifier_list, nb);
  1335. break;
  1336. case CPUFREQ_POLICY_NOTIFIER:
  1337. ret = blocking_notifier_chain_unregister(
  1338. &cpufreq_policy_notifier_list, nb);
  1339. break;
  1340. default:
  1341. ret = -EINVAL;
  1342. }
  1343. return ret;
  1344. }
  1345. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1346. /*********************************************************************
  1347. * GOVERNORS *
  1348. *********************************************************************/
  1349. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1350. unsigned int target_freq,
  1351. unsigned int relation)
  1352. {
  1353. int retval = -EINVAL;
  1354. unsigned int old_target_freq = target_freq;
  1355. if (cpufreq_disabled())
  1356. return -ENODEV;
  1357. if (policy->transition_ongoing)
  1358. return -EBUSY;
  1359. /* Make sure that target_freq is within supported range */
  1360. if (target_freq > policy->max)
  1361. target_freq = policy->max;
  1362. if (target_freq < policy->min)
  1363. target_freq = policy->min;
  1364. pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
  1365. policy->cpu, target_freq, relation, old_target_freq);
  1366. if (target_freq == policy->cur)
  1367. return 0;
  1368. if (cpufreq_driver->target)
  1369. retval = cpufreq_driver->target(policy, target_freq, relation);
  1370. return retval;
  1371. }
  1372. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1373. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1374. unsigned int target_freq,
  1375. unsigned int relation)
  1376. {
  1377. int ret = -EINVAL;
  1378. if (unlikely(lock_policy_rwsem_write(policy->cpu)))
  1379. goto fail;
  1380. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1381. unlock_policy_rwsem_write(policy->cpu);
  1382. fail:
  1383. return ret;
  1384. }
  1385. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1386. /*
  1387. * when "event" is CPUFREQ_GOV_LIMITS
  1388. */
  1389. static int __cpufreq_governor(struct cpufreq_policy *policy,
  1390. unsigned int event)
  1391. {
  1392. int ret;
  1393. /* Only must be defined when default governor is known to have latency
  1394. restrictions, like e.g. conservative or ondemand.
  1395. That this is the case is already ensured in Kconfig
  1396. */
  1397. #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
  1398. struct cpufreq_governor *gov = &cpufreq_gov_performance;
  1399. #else
  1400. struct cpufreq_governor *gov = NULL;
  1401. #endif
  1402. if (policy->governor->max_transition_latency &&
  1403. policy->cpuinfo.transition_latency >
  1404. policy->governor->max_transition_latency) {
  1405. if (!gov)
  1406. return -EINVAL;
  1407. else {
  1408. printk(KERN_WARNING "%s governor failed, too long"
  1409. " transition latency of HW, fallback"
  1410. " to %s governor\n",
  1411. policy->governor->name,
  1412. gov->name);
  1413. policy->governor = gov;
  1414. }
  1415. }
  1416. if (event == CPUFREQ_GOV_POLICY_INIT)
  1417. if (!try_module_get(policy->governor->owner))
  1418. return -EINVAL;
  1419. pr_debug("__cpufreq_governor for CPU %u, event %u\n",
  1420. policy->cpu, event);
  1421. mutex_lock(&cpufreq_governor_lock);
  1422. if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
  1423. (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
  1424. mutex_unlock(&cpufreq_governor_lock);
  1425. if (event == CPUFREQ_GOV_POLICY_INIT)
  1426. module_put(policy->governor->owner);
  1427. return -EBUSY;
  1428. }
  1429. if (event == CPUFREQ_GOV_STOP)
  1430. policy->governor_enabled = false;
  1431. else if (event == CPUFREQ_GOV_START)
  1432. policy->governor_enabled = true;
  1433. mutex_unlock(&cpufreq_governor_lock);
  1434. ret = policy->governor->governor(policy, event);
  1435. if (!ret) {
  1436. if (event == CPUFREQ_GOV_POLICY_INIT)
  1437. policy->governor->initialized++;
  1438. else if (event == CPUFREQ_GOV_POLICY_EXIT)
  1439. policy->governor->initialized--;
  1440. } else {
  1441. /* Restore original values */
  1442. mutex_lock(&cpufreq_governor_lock);
  1443. if (event == CPUFREQ_GOV_STOP)
  1444. policy->governor_enabled = true;
  1445. else if (event == CPUFREQ_GOV_START)
  1446. policy->governor_enabled = false;
  1447. mutex_unlock(&cpufreq_governor_lock);
  1448. }
  1449. if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
  1450. ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
  1451. module_put(policy->governor->owner);
  1452. return ret;
  1453. }
  1454. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1455. {
  1456. int err;
  1457. if (!governor)
  1458. return -EINVAL;
  1459. if (cpufreq_disabled())
  1460. return -ENODEV;
  1461. mutex_lock(&cpufreq_governor_mutex);
  1462. governor->initialized = 0;
  1463. err = -EBUSY;
  1464. if (__find_governor(governor->name) == NULL) {
  1465. err = 0;
  1466. list_add(&governor->governor_list, &cpufreq_governor_list);
  1467. }
  1468. mutex_unlock(&cpufreq_governor_mutex);
  1469. return err;
  1470. }
  1471. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1472. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1473. {
  1474. #ifdef CONFIG_HOTPLUG_CPU
  1475. int cpu;
  1476. #endif
  1477. if (!governor)
  1478. return;
  1479. if (cpufreq_disabled())
  1480. return;
  1481. #ifdef CONFIG_HOTPLUG_CPU
  1482. for_each_present_cpu(cpu) {
  1483. if (cpu_online(cpu))
  1484. continue;
  1485. if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
  1486. strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
  1487. }
  1488. #endif
  1489. mutex_lock(&cpufreq_governor_mutex);
  1490. list_del(&governor->governor_list);
  1491. mutex_unlock(&cpufreq_governor_mutex);
  1492. return;
  1493. }
  1494. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1495. /*********************************************************************
  1496. * POLICY INTERFACE *
  1497. *********************************************************************/
  1498. /**
  1499. * cpufreq_get_policy - get the current cpufreq_policy
  1500. * @policy: struct cpufreq_policy into which the current cpufreq_policy
  1501. * is written
  1502. *
  1503. * Reads the current cpufreq policy.
  1504. */
  1505. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1506. {
  1507. struct cpufreq_policy *cpu_policy;
  1508. if (!policy)
  1509. return -EINVAL;
  1510. cpu_policy = cpufreq_cpu_get(cpu);
  1511. if (!cpu_policy)
  1512. return -EINVAL;
  1513. memcpy(policy, cpu_policy, sizeof(*policy));
  1514. cpufreq_cpu_put(cpu_policy);
  1515. return 0;
  1516. }
  1517. EXPORT_SYMBOL(cpufreq_get_policy);
  1518. /*
  1519. * data : current policy.
  1520. * policy : policy to be set.
  1521. */
  1522. static int __cpufreq_set_policy(struct cpufreq_policy *policy,
  1523. struct cpufreq_policy *new_policy)
  1524. {
  1525. int ret = 0, failed = 1;
  1526. pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
  1527. new_policy->min, new_policy->max);
  1528. memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
  1529. if (new_policy->min > policy->max || new_policy->max < policy->min) {
  1530. ret = -EINVAL;
  1531. goto error_out;
  1532. }
  1533. /* verify the cpu speed can be set within this limit */
  1534. ret = cpufreq_driver->verify(new_policy);
  1535. if (ret)
  1536. goto error_out;
  1537. /* adjust if necessary - all reasons */
  1538. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1539. CPUFREQ_ADJUST, new_policy);
  1540. /* adjust if necessary - hardware incompatibility*/
  1541. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1542. CPUFREQ_INCOMPATIBLE, new_policy);
  1543. /*
  1544. * verify the cpu speed can be set within this limit, which might be
  1545. * different to the first one
  1546. */
  1547. ret = cpufreq_driver->verify(new_policy);
  1548. if (ret)
  1549. goto error_out;
  1550. /* notification of the new policy */
  1551. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1552. CPUFREQ_NOTIFY, new_policy);
  1553. policy->min = new_policy->min;
  1554. policy->max = new_policy->max;
  1555. pr_debug("new min and max freqs are %u - %u kHz\n",
  1556. policy->min, policy->max);
  1557. if (cpufreq_driver->setpolicy) {
  1558. policy->policy = new_policy->policy;
  1559. pr_debug("setting range\n");
  1560. ret = cpufreq_driver->setpolicy(new_policy);
  1561. } else {
  1562. if (new_policy->governor != policy->governor) {
  1563. /* save old, working values */
  1564. struct cpufreq_governor *old_gov = policy->governor;
  1565. pr_debug("governor switch\n");
  1566. /* end old governor */
  1567. if (policy->governor) {
  1568. __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
  1569. unlock_policy_rwsem_write(new_policy->cpu);
  1570. __cpufreq_governor(policy,
  1571. CPUFREQ_GOV_POLICY_EXIT);
  1572. lock_policy_rwsem_write(new_policy->cpu);
  1573. }
  1574. /* start new governor */
  1575. policy->governor = new_policy->governor;
  1576. if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
  1577. if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
  1578. failed = 0;
  1579. } else {
  1580. unlock_policy_rwsem_write(new_policy->cpu);
  1581. __cpufreq_governor(policy,
  1582. CPUFREQ_GOV_POLICY_EXIT);
  1583. lock_policy_rwsem_write(new_policy->cpu);
  1584. }
  1585. }
  1586. if (failed) {
  1587. /* new governor failed, so re-start old one */
  1588. pr_debug("starting governor %s failed\n",
  1589. policy->governor->name);
  1590. if (old_gov) {
  1591. policy->governor = old_gov;
  1592. __cpufreq_governor(policy,
  1593. CPUFREQ_GOV_POLICY_INIT);
  1594. __cpufreq_governor(policy,
  1595. CPUFREQ_GOV_START);
  1596. }
  1597. ret = -EINVAL;
  1598. goto error_out;
  1599. }
  1600. /* might be a policy change, too, so fall through */
  1601. }
  1602. pr_debug("governor: change or update limits\n");
  1603. ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
  1604. }
  1605. error_out:
  1606. return ret;
  1607. }
  1608. /**
  1609. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1610. * @cpu: CPU which shall be re-evaluated
  1611. *
  1612. * Useful for policy notifiers which have different necessities
  1613. * at different times.
  1614. */
  1615. int cpufreq_update_policy(unsigned int cpu)
  1616. {
  1617. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1618. struct cpufreq_policy new_policy;
  1619. int ret;
  1620. if (!policy) {
  1621. ret = -ENODEV;
  1622. goto no_policy;
  1623. }
  1624. if (unlikely(lock_policy_rwsem_write(cpu))) {
  1625. ret = -EINVAL;
  1626. goto fail;
  1627. }
  1628. pr_debug("updating policy for CPU %u\n", cpu);
  1629. memcpy(&new_policy, policy, sizeof(*policy));
  1630. new_policy.min = policy->user_policy.min;
  1631. new_policy.max = policy->user_policy.max;
  1632. new_policy.policy = policy->user_policy.policy;
  1633. new_policy.governor = policy->user_policy.governor;
  1634. /*
  1635. * BIOS might change freq behind our back
  1636. * -> ask driver for current freq and notify governors about a change
  1637. */
  1638. if (cpufreq_driver->get) {
  1639. new_policy.cur = cpufreq_driver->get(cpu);
  1640. if (!policy->cur) {
  1641. pr_debug("Driver did not initialize current freq");
  1642. policy->cur = new_policy.cur;
  1643. } else {
  1644. if (policy->cur != new_policy.cur && cpufreq_driver->target)
  1645. cpufreq_out_of_sync(cpu, policy->cur,
  1646. new_policy.cur);
  1647. }
  1648. }
  1649. ret = __cpufreq_set_policy(policy, &new_policy);
  1650. unlock_policy_rwsem_write(cpu);
  1651. fail:
  1652. cpufreq_cpu_put(policy);
  1653. no_policy:
  1654. return ret;
  1655. }
  1656. EXPORT_SYMBOL(cpufreq_update_policy);
  1657. static int cpufreq_cpu_callback(struct notifier_block *nfb,
  1658. unsigned long action, void *hcpu)
  1659. {
  1660. unsigned int cpu = (unsigned long)hcpu;
  1661. struct device *dev;
  1662. bool frozen = false;
  1663. dev = get_cpu_device(cpu);
  1664. if (dev) {
  1665. if (action & CPU_TASKS_FROZEN)
  1666. frozen = true;
  1667. switch (action & ~CPU_TASKS_FROZEN) {
  1668. case CPU_ONLINE:
  1669. __cpufreq_add_dev(dev, NULL, frozen);
  1670. cpufreq_update_policy(cpu);
  1671. break;
  1672. case CPU_DOWN_PREPARE:
  1673. __cpufreq_remove_dev(dev, NULL, frozen);
  1674. break;
  1675. case CPU_DOWN_FAILED:
  1676. __cpufreq_add_dev(dev, NULL, frozen);
  1677. break;
  1678. }
  1679. }
  1680. return NOTIFY_OK;
  1681. }
  1682. static struct notifier_block __refdata cpufreq_cpu_notifier = {
  1683. .notifier_call = cpufreq_cpu_callback,
  1684. };
  1685. /*********************************************************************
  1686. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1687. *********************************************************************/
  1688. /**
  1689. * cpufreq_register_driver - register a CPU Frequency driver
  1690. * @driver_data: A struct cpufreq_driver containing the values#
  1691. * submitted by the CPU Frequency driver.
  1692. *
  1693. * Registers a CPU Frequency driver to this core code. This code
  1694. * returns zero on success, -EBUSY when another driver got here first
  1695. * (and isn't unregistered in the meantime).
  1696. *
  1697. */
  1698. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  1699. {
  1700. unsigned long flags;
  1701. int ret;
  1702. if (cpufreq_disabled())
  1703. return -ENODEV;
  1704. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1705. ((!driver_data->setpolicy) && (!driver_data->target)))
  1706. return -EINVAL;
  1707. pr_debug("trying to register driver %s\n", driver_data->name);
  1708. if (driver_data->setpolicy)
  1709. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  1710. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1711. if (cpufreq_driver) {
  1712. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1713. return -EBUSY;
  1714. }
  1715. cpufreq_driver = driver_data;
  1716. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1717. ret = subsys_interface_register(&cpufreq_interface);
  1718. if (ret)
  1719. goto err_null_driver;
  1720. if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  1721. int i;
  1722. ret = -ENODEV;
  1723. /* check for at least one working CPU */
  1724. for (i = 0; i < nr_cpu_ids; i++)
  1725. if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
  1726. ret = 0;
  1727. break;
  1728. }
  1729. /* if all ->init() calls failed, unregister */
  1730. if (ret) {
  1731. pr_debug("no CPU initialized for driver %s\n",
  1732. driver_data->name);
  1733. goto err_if_unreg;
  1734. }
  1735. }
  1736. register_hotcpu_notifier(&cpufreq_cpu_notifier);
  1737. pr_debug("driver %s up and running\n", driver_data->name);
  1738. return 0;
  1739. err_if_unreg:
  1740. subsys_interface_unregister(&cpufreq_interface);
  1741. err_null_driver:
  1742. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1743. cpufreq_driver = NULL;
  1744. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1745. return ret;
  1746. }
  1747. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  1748. /**
  1749. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  1750. *
  1751. * Unregister the current CPUFreq driver. Only call this if you have
  1752. * the right to do so, i.e. if you have succeeded in initialising before!
  1753. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  1754. * currently not initialised.
  1755. */
  1756. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  1757. {
  1758. unsigned long flags;
  1759. if (!cpufreq_driver || (driver != cpufreq_driver))
  1760. return -EINVAL;
  1761. pr_debug("unregistering driver %s\n", driver->name);
  1762. subsys_interface_unregister(&cpufreq_interface);
  1763. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  1764. down_write(&cpufreq_rwsem);
  1765. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1766. cpufreq_driver = NULL;
  1767. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1768. up_write(&cpufreq_rwsem);
  1769. return 0;
  1770. }
  1771. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  1772. static int __init cpufreq_core_init(void)
  1773. {
  1774. int cpu;
  1775. if (cpufreq_disabled())
  1776. return -ENODEV;
  1777. for_each_possible_cpu(cpu) {
  1778. per_cpu(cpufreq_policy_cpu, cpu) = -1;
  1779. init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
  1780. }
  1781. cpufreq_global_kobject = kobject_create();
  1782. BUG_ON(!cpufreq_global_kobject);
  1783. register_syscore_ops(&cpufreq_syscore_ops);
  1784. return 0;
  1785. }
  1786. core_initcall(cpufreq_core_init);