cpufreq.c 55 KB

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