cpufreq.c 53 KB

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