cpufreq.c 53 KB

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