cpufreq.c 53 KB

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