cpufreq.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
  31. "cpufreq-core", msg)
  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 struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
  39. static DEFINE_SPINLOCK(cpufreq_driver_lock);
  40. /*
  41. * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
  42. * all cpufreq/hotplug/workqueue/etc related lock issues.
  43. *
  44. * The rules for this semaphore:
  45. * - Any routine that wants to read from the policy structure will
  46. * do a down_read on this semaphore.
  47. * - Any routine that will write to the policy structure and/or may take away
  48. * the policy altogether (eg. CPU hotplug), will hold this lock in write
  49. * mode before doing so.
  50. *
  51. * Additional rules:
  52. * - All holders of the lock should check to make sure that the CPU they
  53. * are concerned with are online after they get the lock.
  54. * - Governor routines that can be called in cpufreq hotplug path should not
  55. * take this sem as top level hotplug notifier handler takes this.
  56. */
  57. static DEFINE_PER_CPU(int, policy_cpu);
  58. static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
  59. #define lock_policy_rwsem(mode, cpu) \
  60. int lock_policy_rwsem_##mode \
  61. (int cpu) \
  62. { \
  63. int policy_cpu = per_cpu(policy_cpu, cpu); \
  64. BUG_ON(policy_cpu == -1); \
  65. down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  66. if (unlikely(!cpu_online(cpu))) { \
  67. up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  68. return -1; \
  69. } \
  70. \
  71. return 0; \
  72. }
  73. lock_policy_rwsem(read, cpu);
  74. EXPORT_SYMBOL_GPL(lock_policy_rwsem_read);
  75. lock_policy_rwsem(write, cpu);
  76. EXPORT_SYMBOL_GPL(lock_policy_rwsem_write);
  77. void unlock_policy_rwsem_read(int cpu)
  78. {
  79. int policy_cpu = per_cpu(policy_cpu, cpu);
  80. BUG_ON(policy_cpu == -1);
  81. up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
  82. }
  83. EXPORT_SYMBOL_GPL(unlock_policy_rwsem_read);
  84. void unlock_policy_rwsem_write(int cpu)
  85. {
  86. int policy_cpu = per_cpu(policy_cpu, cpu);
  87. BUG_ON(policy_cpu == -1);
  88. up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
  89. }
  90. EXPORT_SYMBOL_GPL(unlock_policy_rwsem_write);
  91. /* internal prototypes */
  92. static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
  93. static unsigned int __cpufreq_get(unsigned int cpu);
  94. static void handle_update(struct work_struct *work);
  95. /**
  96. * Two notifier lists: the "policy" list is involved in the
  97. * validation process for a new CPU frequency policy; the
  98. * "transition" list for kernel code that needs to handle
  99. * changes to devices when the CPU clock speed changes.
  100. * The mutex locks both lists.
  101. */
  102. static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
  103. static struct srcu_notifier_head cpufreq_transition_notifier_list;
  104. static int __init init_cpufreq_transition_notifier_list(void)
  105. {
  106. srcu_init_notifier_head(&cpufreq_transition_notifier_list);
  107. return 0;
  108. }
  109. pure_initcall(init_cpufreq_transition_notifier_list);
  110. static LIST_HEAD(cpufreq_governor_list);
  111. static DEFINE_MUTEX (cpufreq_governor_mutex);
  112. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  113. {
  114. struct cpufreq_policy *data;
  115. unsigned long flags;
  116. if (cpu >= NR_CPUS)
  117. goto err_out;
  118. /* get the cpufreq driver */
  119. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  120. if (!cpufreq_driver)
  121. goto err_out_unlock;
  122. if (!try_module_get(cpufreq_driver->owner))
  123. goto err_out_unlock;
  124. /* get the CPU */
  125. data = cpufreq_cpu_data[cpu];
  126. if (!data)
  127. goto err_out_put_module;
  128. if (!kobject_get(&data->kobj))
  129. goto err_out_put_module;
  130. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  131. return data;
  132. err_out_put_module:
  133. module_put(cpufreq_driver->owner);
  134. err_out_unlock:
  135. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  136. err_out:
  137. return NULL;
  138. }
  139. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  140. void cpufreq_cpu_put(struct cpufreq_policy *data)
  141. {
  142. kobject_put(&data->kobj);
  143. module_put(cpufreq_driver->owner);
  144. }
  145. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  146. /*********************************************************************
  147. * UNIFIED DEBUG HELPERS *
  148. *********************************************************************/
  149. #ifdef CONFIG_CPU_FREQ_DEBUG
  150. /* what part(s) of the CPUfreq subsystem are debugged? */
  151. static unsigned int debug;
  152. /* is the debug output ratelimit'ed using printk_ratelimit? User can
  153. * set or modify this value.
  154. */
  155. static unsigned int debug_ratelimit = 1;
  156. /* is the printk_ratelimit'ing enabled? It's enabled after a successful
  157. * loading of a cpufreq driver, temporarily disabled when a new policy
  158. * is set, and disabled upon cpufreq driver removal
  159. */
  160. static unsigned int disable_ratelimit = 1;
  161. static DEFINE_SPINLOCK(disable_ratelimit_lock);
  162. static void cpufreq_debug_enable_ratelimit(void)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&disable_ratelimit_lock, flags);
  166. if (disable_ratelimit)
  167. disable_ratelimit--;
  168. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  169. }
  170. static void cpufreq_debug_disable_ratelimit(void)
  171. {
  172. unsigned long flags;
  173. spin_lock_irqsave(&disable_ratelimit_lock, flags);
  174. disable_ratelimit++;
  175. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  176. }
  177. void cpufreq_debug_printk(unsigned int type, const char *prefix,
  178. const char *fmt, ...)
  179. {
  180. char s[256];
  181. va_list args;
  182. unsigned int len;
  183. unsigned long flags;
  184. WARN_ON(!prefix);
  185. if (type & debug) {
  186. spin_lock_irqsave(&disable_ratelimit_lock, flags);
  187. if (!disable_ratelimit && debug_ratelimit
  188. && !printk_ratelimit()) {
  189. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  190. return;
  191. }
  192. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  193. len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
  194. va_start(args, fmt);
  195. len += vsnprintf(&s[len], (256 - len), fmt, args);
  196. va_end(args);
  197. printk(s);
  198. WARN_ON(len < 5);
  199. }
  200. }
  201. EXPORT_SYMBOL(cpufreq_debug_printk);
  202. module_param(debug, uint, 0644);
  203. MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
  204. " 2 to debug drivers, and 4 to debug governors.");
  205. module_param(debug_ratelimit, uint, 0644);
  206. MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
  207. " set to 0 to disable ratelimiting.");
  208. #else /* !CONFIG_CPU_FREQ_DEBUG */
  209. static inline void cpufreq_debug_enable_ratelimit(void) { return; }
  210. static inline void cpufreq_debug_disable_ratelimit(void) { return; }
  211. #endif /* CONFIG_CPU_FREQ_DEBUG */
  212. /*********************************************************************
  213. * EXTERNALLY AFFECTING FREQUENCY CHANGES *
  214. *********************************************************************/
  215. /**
  216. * adjust_jiffies - adjust the system "loops_per_jiffy"
  217. *
  218. * This function alters the system "loops_per_jiffy" for the clock
  219. * speed change. Note that loops_per_jiffy cannot be updated on SMP
  220. * systems as each CPU might be scaled differently. So, use the arch
  221. * per-CPU loops_per_jiffy value wherever possible.
  222. */
  223. #ifndef CONFIG_SMP
  224. static unsigned long l_p_j_ref;
  225. static unsigned int l_p_j_ref_freq;
  226. static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  227. {
  228. if (ci->flags & CPUFREQ_CONST_LOOPS)
  229. return;
  230. if (!l_p_j_ref_freq) {
  231. l_p_j_ref = loops_per_jiffy;
  232. l_p_j_ref_freq = ci->old;
  233. dprintk("saving %lu as reference value for loops_per_jiffy;"
  234. "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
  235. }
  236. if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
  237. (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
  238. (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
  239. loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
  240. ci->new);
  241. dprintk("scaling loops_per_jiffy to %lu"
  242. "for frequency %u kHz\n", loops_per_jiffy, ci->new);
  243. }
  244. }
  245. #else
  246. static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  247. {
  248. return;
  249. }
  250. #endif
  251. /**
  252. * cpufreq_notify_transition - call notifier chain and adjust_jiffies
  253. * on frequency transition.
  254. *
  255. * This function calls the transition notifiers and the "adjust_jiffies"
  256. * function. It is called twice on all CPU frequency changes that have
  257. * external effects.
  258. */
  259. void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
  260. {
  261. struct cpufreq_policy *policy;
  262. BUG_ON(irqs_disabled());
  263. freqs->flags = cpufreq_driver->flags;
  264. dprintk("notification %u of frequency transition to %u kHz\n",
  265. state, freqs->new);
  266. policy = cpufreq_cpu_data[freqs->cpu];
  267. switch (state) {
  268. case CPUFREQ_PRECHANGE:
  269. /* detect if the driver reported a value as "old frequency"
  270. * which is not equal to what the cpufreq core thinks is
  271. * "old frequency".
  272. */
  273. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  274. if ((policy) && (policy->cpu == freqs->cpu) &&
  275. (policy->cur) && (policy->cur != freqs->old)) {
  276. dprintk("Warning: CPU frequency is"
  277. " %u, cpufreq assumed %u kHz.\n",
  278. freqs->old, policy->cur);
  279. freqs->old = policy->cur;
  280. }
  281. }
  282. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  283. CPUFREQ_PRECHANGE, freqs);
  284. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  285. break;
  286. case CPUFREQ_POSTCHANGE:
  287. adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
  288. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  289. CPUFREQ_POSTCHANGE, freqs);
  290. if (likely(policy) && likely(policy->cpu == freqs->cpu))
  291. policy->cur = freqs->new;
  292. break;
  293. }
  294. }
  295. EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
  296. /*********************************************************************
  297. * SYSFS INTERFACE *
  298. *********************************************************************/
  299. static struct cpufreq_governor *__find_governor(const char *str_governor)
  300. {
  301. struct cpufreq_governor *t;
  302. list_for_each_entry(t, &cpufreq_governor_list, governor_list)
  303. if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
  304. return t;
  305. return NULL;
  306. }
  307. /**
  308. * cpufreq_parse_governor - parse a governor string
  309. */
  310. static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
  311. struct cpufreq_governor **governor)
  312. {
  313. int err = -EINVAL;
  314. if (!cpufreq_driver)
  315. goto out;
  316. if (cpufreq_driver->setpolicy) {
  317. if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
  318. *policy = CPUFREQ_POLICY_PERFORMANCE;
  319. err = 0;
  320. } else if (!strnicmp(str_governor, "powersave",
  321. CPUFREQ_NAME_LEN)) {
  322. *policy = CPUFREQ_POLICY_POWERSAVE;
  323. err = 0;
  324. }
  325. } else if (cpufreq_driver->target) {
  326. struct cpufreq_governor *t;
  327. mutex_lock(&cpufreq_governor_mutex);
  328. t = __find_governor(str_governor);
  329. if (t == NULL) {
  330. char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
  331. str_governor);
  332. if (name) {
  333. int ret;
  334. mutex_unlock(&cpufreq_governor_mutex);
  335. ret = request_module(name);
  336. mutex_lock(&cpufreq_governor_mutex);
  337. if (ret == 0)
  338. t = __find_governor(str_governor);
  339. }
  340. kfree(name);
  341. }
  342. if (t != NULL) {
  343. *governor = t;
  344. err = 0;
  345. }
  346. mutex_unlock(&cpufreq_governor_mutex);
  347. }
  348. out:
  349. return err;
  350. }
  351. /* drivers/base/cpu.c */
  352. extern struct sysdev_class cpu_sysdev_class;
  353. /**
  354. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  355. * print out cpufreq information
  356. *
  357. * Write out information from cpufreq_driver->policy[cpu]; object must be
  358. * "unsigned int".
  359. */
  360. #define show_one(file_name, object) \
  361. static ssize_t show_##file_name \
  362. (struct cpufreq_policy * policy, char *buf) \
  363. { \
  364. return sprintf (buf, "%u\n", policy->object); \
  365. }
  366. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  367. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  368. show_one(scaling_min_freq, min);
  369. show_one(scaling_max_freq, max);
  370. show_one(scaling_cur_freq, cur);
  371. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  372. struct cpufreq_policy *policy);
  373. /**
  374. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  375. */
  376. #define store_one(file_name, object) \
  377. static ssize_t store_##file_name \
  378. (struct cpufreq_policy * policy, const char *buf, size_t count) \
  379. { \
  380. unsigned int ret = -EINVAL; \
  381. struct cpufreq_policy new_policy; \
  382. \
  383. ret = cpufreq_get_policy(&new_policy, policy->cpu); \
  384. if (ret) \
  385. return -EINVAL; \
  386. \
  387. ret = sscanf (buf, "%u", &new_policy.object); \
  388. if (ret != 1) \
  389. return -EINVAL; \
  390. \
  391. ret = __cpufreq_set_policy(policy, &new_policy); \
  392. policy->user_policy.object = policy->object; \
  393. \
  394. return ret ? ret : count; \
  395. }
  396. store_one(scaling_min_freq,min);
  397. store_one(scaling_max_freq,max);
  398. /**
  399. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  400. */
  401. static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy,
  402. char *buf)
  403. {
  404. unsigned int cur_freq = __cpufreq_get(policy->cpu);
  405. if (!cur_freq)
  406. return sprintf(buf, "<unknown>");
  407. return sprintf(buf, "%u\n", cur_freq);
  408. }
  409. /**
  410. * show_scaling_governor - show the current policy for the specified CPU
  411. */
  412. static ssize_t show_scaling_governor (struct cpufreq_policy * policy,
  413. char *buf)
  414. {
  415. if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
  416. return sprintf(buf, "powersave\n");
  417. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  418. return sprintf(buf, "performance\n");
  419. else if (policy->governor)
  420. return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
  421. return -EINVAL;
  422. }
  423. /**
  424. * store_scaling_governor - store policy for the specified CPU
  425. */
  426. static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
  427. const char *buf, size_t count)
  428. {
  429. unsigned int ret = -EINVAL;
  430. char str_governor[16];
  431. struct cpufreq_policy new_policy;
  432. ret = cpufreq_get_policy(&new_policy, policy->cpu);
  433. if (ret)
  434. return ret;
  435. ret = sscanf (buf, "%15s", str_governor);
  436. if (ret != 1)
  437. return -EINVAL;
  438. if (cpufreq_parse_governor(str_governor, &new_policy.policy,
  439. &new_policy.governor))
  440. return -EINVAL;
  441. /* Do not use cpufreq_set_policy here or the user_policy.max
  442. will be wrongly overridden */
  443. ret = __cpufreq_set_policy(policy, &new_policy);
  444. policy->user_policy.policy = policy->policy;
  445. policy->user_policy.governor = policy->governor;
  446. if (ret)
  447. return ret;
  448. else
  449. return count;
  450. }
  451. /**
  452. * show_scaling_driver - show the cpufreq driver currently loaded
  453. */
  454. static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
  455. {
  456. return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
  457. }
  458. /**
  459. * show_scaling_available_governors - show the available CPUfreq governors
  460. */
  461. static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy,
  462. char *buf)
  463. {
  464. ssize_t i = 0;
  465. struct cpufreq_governor *t;
  466. if (!cpufreq_driver->target) {
  467. i += sprintf(buf, "performance powersave");
  468. goto out;
  469. }
  470. list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
  471. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
  472. goto out;
  473. i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
  474. }
  475. out:
  476. i += sprintf(&buf[i], "\n");
  477. return i;
  478. }
  479. /**
  480. * show_affected_cpus - show the CPUs affected by each transition
  481. */
  482. static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
  483. {
  484. ssize_t i = 0;
  485. unsigned int cpu;
  486. for_each_cpu_mask(cpu, policy->cpus) {
  487. if (i)
  488. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
  489. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
  490. if (i >= (PAGE_SIZE - 5))
  491. break;
  492. }
  493. i += sprintf(&buf[i], "\n");
  494. return i;
  495. }
  496. #define define_one_ro(_name) \
  497. static struct freq_attr _name = \
  498. __ATTR(_name, 0444, show_##_name, NULL)
  499. #define define_one_ro0400(_name) \
  500. static struct freq_attr _name = \
  501. __ATTR(_name, 0400, show_##_name, NULL)
  502. #define define_one_rw(_name) \
  503. static struct freq_attr _name = \
  504. __ATTR(_name, 0644, show_##_name, store_##_name)
  505. define_one_ro0400(cpuinfo_cur_freq);
  506. define_one_ro(cpuinfo_min_freq);
  507. define_one_ro(cpuinfo_max_freq);
  508. define_one_ro(scaling_available_governors);
  509. define_one_ro(scaling_driver);
  510. define_one_ro(scaling_cur_freq);
  511. define_one_ro(affected_cpus);
  512. define_one_rw(scaling_min_freq);
  513. define_one_rw(scaling_max_freq);
  514. define_one_rw(scaling_governor);
  515. static struct attribute * default_attrs[] = {
  516. &cpuinfo_min_freq.attr,
  517. &cpuinfo_max_freq.attr,
  518. &scaling_min_freq.attr,
  519. &scaling_max_freq.attr,
  520. &affected_cpus.attr,
  521. &scaling_governor.attr,
  522. &scaling_driver.attr,
  523. &scaling_available_governors.attr,
  524. NULL
  525. };
  526. #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
  527. #define to_attr(a) container_of(a,struct freq_attr,attr)
  528. static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
  529. {
  530. struct cpufreq_policy * policy = to_policy(kobj);
  531. struct freq_attr * fattr = to_attr(attr);
  532. ssize_t ret;
  533. policy = cpufreq_cpu_get(policy->cpu);
  534. if (!policy)
  535. return -EINVAL;
  536. if (lock_policy_rwsem_read(policy->cpu) < 0)
  537. return -EINVAL;
  538. if (fattr->show)
  539. ret = fattr->show(policy, buf);
  540. else
  541. ret = -EIO;
  542. unlock_policy_rwsem_read(policy->cpu);
  543. cpufreq_cpu_put(policy);
  544. return ret;
  545. }
  546. static ssize_t store(struct kobject * kobj, struct attribute * attr,
  547. const char * buf, size_t count)
  548. {
  549. struct cpufreq_policy * policy = to_policy(kobj);
  550. struct freq_attr * fattr = to_attr(attr);
  551. ssize_t ret;
  552. policy = cpufreq_cpu_get(policy->cpu);
  553. if (!policy)
  554. return -EINVAL;
  555. if (lock_policy_rwsem_write(policy->cpu) < 0)
  556. return -EINVAL;
  557. if (fattr->store)
  558. ret = fattr->store(policy, buf, count);
  559. else
  560. ret = -EIO;
  561. unlock_policy_rwsem_write(policy->cpu);
  562. cpufreq_cpu_put(policy);
  563. return ret;
  564. }
  565. static void cpufreq_sysfs_release(struct kobject * kobj)
  566. {
  567. struct cpufreq_policy * policy = to_policy(kobj);
  568. dprintk("last reference is dropped\n");
  569. complete(&policy->kobj_unregister);
  570. }
  571. static struct sysfs_ops sysfs_ops = {
  572. .show = show,
  573. .store = store,
  574. };
  575. static struct kobj_type ktype_cpufreq = {
  576. .sysfs_ops = &sysfs_ops,
  577. .default_attrs = default_attrs,
  578. .release = cpufreq_sysfs_release,
  579. };
  580. /**
  581. * cpufreq_add_dev - add a CPU device
  582. *
  583. * Adds the cpufreq interface for a CPU device.
  584. */
  585. static int cpufreq_add_dev (struct sys_device * sys_dev)
  586. {
  587. unsigned int cpu = sys_dev->id;
  588. int ret = 0;
  589. struct cpufreq_policy new_policy;
  590. struct cpufreq_policy *policy;
  591. struct freq_attr **drv_attr;
  592. struct sys_device *cpu_sys_dev;
  593. unsigned long flags;
  594. unsigned int j;
  595. #ifdef CONFIG_SMP
  596. struct cpufreq_policy *managed_policy;
  597. #endif
  598. if (cpu_is_offline(cpu))
  599. return 0;
  600. cpufreq_debug_disable_ratelimit();
  601. dprintk("adding CPU %u\n", cpu);
  602. #ifdef CONFIG_SMP
  603. /* check whether a different CPU already registered this
  604. * CPU because it is in the same boat. */
  605. policy = cpufreq_cpu_get(cpu);
  606. if (unlikely(policy)) {
  607. cpufreq_cpu_put(policy);
  608. cpufreq_debug_enable_ratelimit();
  609. return 0;
  610. }
  611. #endif
  612. if (!try_module_get(cpufreq_driver->owner)) {
  613. ret = -EINVAL;
  614. goto module_out;
  615. }
  616. policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
  617. if (!policy) {
  618. ret = -ENOMEM;
  619. goto nomem_out;
  620. }
  621. policy->cpu = cpu;
  622. policy->cpus = cpumask_of_cpu(cpu);
  623. /* Initially set CPU itself as the policy_cpu */
  624. per_cpu(policy_cpu, cpu) = cpu;
  625. lock_policy_rwsem_write(cpu);
  626. init_completion(&policy->kobj_unregister);
  627. INIT_WORK(&policy->update, handle_update);
  628. /* call driver. From then on the cpufreq must be able
  629. * to accept all calls to ->verify and ->setpolicy for this CPU
  630. */
  631. ret = cpufreq_driver->init(policy);
  632. if (ret) {
  633. dprintk("initialization failed\n");
  634. unlock_policy_rwsem_write(cpu);
  635. goto err_out;
  636. }
  637. policy->user_policy.min = policy->cpuinfo.min_freq;
  638. policy->user_policy.max = policy->cpuinfo.max_freq;
  639. policy->user_policy.governor = policy->governor;
  640. #ifdef CONFIG_SMP
  641. for_each_cpu_mask(j, policy->cpus) {
  642. if (cpu == j)
  643. continue;
  644. /* check for existing affected CPUs. They may not be aware
  645. * of it due to CPU Hotplug.
  646. */
  647. managed_policy = cpufreq_cpu_get(j);
  648. if (unlikely(managed_policy)) {
  649. /* Set proper policy_cpu */
  650. unlock_policy_rwsem_write(cpu);
  651. per_cpu(policy_cpu, cpu) = managed_policy->cpu;
  652. if (lock_policy_rwsem_write(cpu) < 0)
  653. goto err_out_driver_exit;
  654. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  655. managed_policy->cpus = policy->cpus;
  656. cpufreq_cpu_data[cpu] = managed_policy;
  657. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  658. dprintk("CPU already managed, adding link\n");
  659. ret = sysfs_create_link(&sys_dev->kobj,
  660. &managed_policy->kobj,
  661. "cpufreq");
  662. if (ret) {
  663. unlock_policy_rwsem_write(cpu);
  664. goto err_out_driver_exit;
  665. }
  666. cpufreq_debug_enable_ratelimit();
  667. ret = 0;
  668. unlock_policy_rwsem_write(cpu);
  669. goto err_out_driver_exit; /* call driver->exit() */
  670. }
  671. }
  672. #endif
  673. memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
  674. /* prepare interface data */
  675. policy->kobj.parent = &sys_dev->kobj;
  676. policy->kobj.ktype = &ktype_cpufreq;
  677. strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
  678. ret = kobject_register(&policy->kobj);
  679. if (ret) {
  680. unlock_policy_rwsem_write(cpu);
  681. goto err_out_driver_exit;
  682. }
  683. /* set up files for this cpu device */
  684. drv_attr = cpufreq_driver->attr;
  685. while ((drv_attr) && (*drv_attr)) {
  686. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  687. if (ret)
  688. goto err_out_driver_exit;
  689. drv_attr++;
  690. }
  691. if (cpufreq_driver->get){
  692. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  693. if (ret)
  694. goto err_out_driver_exit;
  695. }
  696. if (cpufreq_driver->target){
  697. ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  698. if (ret)
  699. goto err_out_driver_exit;
  700. }
  701. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  702. for_each_cpu_mask(j, policy->cpus) {
  703. cpufreq_cpu_data[j] = policy;
  704. per_cpu(policy_cpu, j) = policy->cpu;
  705. }
  706. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  707. /* symlink affected CPUs */
  708. for_each_cpu_mask(j, policy->cpus) {
  709. if (j == cpu)
  710. continue;
  711. if (!cpu_online(j))
  712. continue;
  713. dprintk("CPU %u already managed, adding link\n", j);
  714. cpufreq_cpu_get(cpu);
  715. cpu_sys_dev = get_cpu_sysdev(j);
  716. ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
  717. "cpufreq");
  718. if (ret) {
  719. unlock_policy_rwsem_write(cpu);
  720. goto err_out_unregister;
  721. }
  722. }
  723. policy->governor = NULL; /* to assure that the starting sequence is
  724. * run in cpufreq_set_policy */
  725. /* set default policy */
  726. ret = __cpufreq_set_policy(policy, &new_policy);
  727. policy->user_policy.policy = policy->policy;
  728. unlock_policy_rwsem_write(cpu);
  729. if (ret) {
  730. dprintk("setting policy failed\n");
  731. goto err_out_unregister;
  732. }
  733. module_put(cpufreq_driver->owner);
  734. dprintk("initialization complete\n");
  735. cpufreq_debug_enable_ratelimit();
  736. return 0;
  737. err_out_unregister:
  738. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  739. for_each_cpu_mask(j, policy->cpus)
  740. cpufreq_cpu_data[j] = NULL;
  741. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  742. kobject_unregister(&policy->kobj);
  743. wait_for_completion(&policy->kobj_unregister);
  744. err_out_driver_exit:
  745. if (cpufreq_driver->exit)
  746. cpufreq_driver->exit(policy);
  747. err_out:
  748. kfree(policy);
  749. nomem_out:
  750. module_put(cpufreq_driver->owner);
  751. module_out:
  752. cpufreq_debug_enable_ratelimit();
  753. return ret;
  754. }
  755. /**
  756. * __cpufreq_remove_dev - remove a CPU device
  757. *
  758. * Removes the cpufreq interface for a CPU device.
  759. * Caller should already have policy_rwsem in write mode for this CPU.
  760. * This routine frees the rwsem before returning.
  761. */
  762. static int __cpufreq_remove_dev (struct sys_device * sys_dev)
  763. {
  764. unsigned int cpu = sys_dev->id;
  765. unsigned long flags;
  766. struct cpufreq_policy *data;
  767. #ifdef CONFIG_SMP
  768. struct sys_device *cpu_sys_dev;
  769. unsigned int j;
  770. #endif
  771. cpufreq_debug_disable_ratelimit();
  772. dprintk("unregistering CPU %u\n", cpu);
  773. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  774. data = cpufreq_cpu_data[cpu];
  775. if (!data) {
  776. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  777. cpufreq_debug_enable_ratelimit();
  778. unlock_policy_rwsem_write(cpu);
  779. return -EINVAL;
  780. }
  781. cpufreq_cpu_data[cpu] = NULL;
  782. #ifdef CONFIG_SMP
  783. /* if this isn't the CPU which is the parent of the kobj, we
  784. * only need to unlink, put and exit
  785. */
  786. if (unlikely(cpu != data->cpu)) {
  787. dprintk("removing link\n");
  788. cpu_clear(cpu, data->cpus);
  789. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  790. sysfs_remove_link(&sys_dev->kobj, "cpufreq");
  791. cpufreq_cpu_put(data);
  792. cpufreq_debug_enable_ratelimit();
  793. unlock_policy_rwsem_write(cpu);
  794. return 0;
  795. }
  796. #endif
  797. if (!kobject_get(&data->kobj)) {
  798. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  799. cpufreq_debug_enable_ratelimit();
  800. unlock_policy_rwsem_write(cpu);
  801. return -EFAULT;
  802. }
  803. #ifdef CONFIG_SMP
  804. /* if we have other CPUs still registered, we need to unlink them,
  805. * or else wait_for_completion below will lock up. Clean the
  806. * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
  807. * links afterwards.
  808. */
  809. if (unlikely(cpus_weight(data->cpus) > 1)) {
  810. for_each_cpu_mask(j, data->cpus) {
  811. if (j == cpu)
  812. continue;
  813. cpufreq_cpu_data[j] = NULL;
  814. }
  815. }
  816. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  817. if (unlikely(cpus_weight(data->cpus) > 1)) {
  818. for_each_cpu_mask(j, data->cpus) {
  819. if (j == cpu)
  820. continue;
  821. dprintk("removing link for cpu %u\n", j);
  822. cpu_sys_dev = get_cpu_sysdev(j);
  823. sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
  824. cpufreq_cpu_put(data);
  825. }
  826. }
  827. #else
  828. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  829. #endif
  830. if (cpufreq_driver->target)
  831. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  832. unlock_policy_rwsem_write(cpu);
  833. kobject_unregister(&data->kobj);
  834. kobject_put(&data->kobj);
  835. /* we need to make sure that the underlying kobj is actually
  836. * not referenced anymore by anybody before we proceed with
  837. * unloading.
  838. */
  839. dprintk("waiting for dropping of refcount\n");
  840. wait_for_completion(&data->kobj_unregister);
  841. dprintk("wait complete\n");
  842. if (cpufreq_driver->exit)
  843. cpufreq_driver->exit(data);
  844. kfree(data);
  845. cpufreq_debug_enable_ratelimit();
  846. return 0;
  847. }
  848. static int cpufreq_remove_dev (struct sys_device * sys_dev)
  849. {
  850. unsigned int cpu = sys_dev->id;
  851. int retval;
  852. if (cpu_is_offline(cpu))
  853. return 0;
  854. if (unlikely(lock_policy_rwsem_write(cpu)))
  855. BUG();
  856. retval = __cpufreq_remove_dev(sys_dev);
  857. return retval;
  858. }
  859. static void handle_update(struct work_struct *work)
  860. {
  861. struct cpufreq_policy *policy =
  862. container_of(work, struct cpufreq_policy, update);
  863. unsigned int cpu = policy->cpu;
  864. dprintk("handle_update for cpu %u called\n", cpu);
  865. cpufreq_update_policy(cpu);
  866. }
  867. /**
  868. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
  869. * @cpu: cpu number
  870. * @old_freq: CPU frequency the kernel thinks the CPU runs at
  871. * @new_freq: CPU frequency the CPU actually runs at
  872. *
  873. * We adjust to current frequency first, and need to clean up later. So either call
  874. * to cpufreq_update_policy() or schedule handle_update()).
  875. */
  876. static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
  877. unsigned int new_freq)
  878. {
  879. struct cpufreq_freqs freqs;
  880. dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
  881. "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
  882. freqs.cpu = cpu;
  883. freqs.old = old_freq;
  884. freqs.new = new_freq;
  885. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  886. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  887. }
  888. /**
  889. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  890. * @cpu: CPU number
  891. *
  892. * This is the last known freq, without actually getting it from the driver.
  893. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  894. */
  895. unsigned int cpufreq_quick_get(unsigned int cpu)
  896. {
  897. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  898. unsigned int ret_freq = 0;
  899. if (policy) {
  900. if (unlikely(lock_policy_rwsem_read(cpu)))
  901. return ret_freq;
  902. ret_freq = policy->cur;
  903. unlock_policy_rwsem_read(cpu);
  904. cpufreq_cpu_put(policy);
  905. }
  906. return (ret_freq);
  907. }
  908. EXPORT_SYMBOL(cpufreq_quick_get);
  909. static unsigned int __cpufreq_get(unsigned int cpu)
  910. {
  911. struct cpufreq_policy *policy = cpufreq_cpu_data[cpu];
  912. unsigned int ret_freq = 0;
  913. if (!cpufreq_driver->get)
  914. return (ret_freq);
  915. ret_freq = cpufreq_driver->get(cpu);
  916. if (ret_freq && policy->cur &&
  917. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  918. /* verify no discrepancy between actual and
  919. saved value exists */
  920. if (unlikely(ret_freq != policy->cur)) {
  921. cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
  922. schedule_work(&policy->update);
  923. }
  924. }
  925. return (ret_freq);
  926. }
  927. /**
  928. * cpufreq_get - get the current CPU frequency (in kHz)
  929. * @cpu: CPU number
  930. *
  931. * Get the CPU current (static) CPU frequency
  932. */
  933. unsigned int cpufreq_get(unsigned int cpu)
  934. {
  935. unsigned int ret_freq = 0;
  936. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  937. if (!policy)
  938. goto out;
  939. if (unlikely(lock_policy_rwsem_read(cpu)))
  940. goto out_policy;
  941. ret_freq = __cpufreq_get(cpu);
  942. unlock_policy_rwsem_read(cpu);
  943. out_policy:
  944. cpufreq_cpu_put(policy);
  945. out:
  946. return (ret_freq);
  947. }
  948. EXPORT_SYMBOL(cpufreq_get);
  949. /**
  950. * cpufreq_suspend - let the low level driver prepare for suspend
  951. */
  952. static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
  953. {
  954. int cpu = sysdev->id;
  955. int ret = 0;
  956. unsigned int cur_freq = 0;
  957. struct cpufreq_policy *cpu_policy;
  958. dprintk("suspending cpu %u\n", cpu);
  959. if (!cpu_online(cpu))
  960. return 0;
  961. /* we may be lax here as interrupts are off. Nonetheless
  962. * we need to grab the correct cpu policy, as to check
  963. * whether we really run on this CPU.
  964. */
  965. cpu_policy = cpufreq_cpu_get(cpu);
  966. if (!cpu_policy)
  967. return -EINVAL;
  968. /* only handle each CPU group once */
  969. if (unlikely(cpu_policy->cpu != cpu)) {
  970. cpufreq_cpu_put(cpu_policy);
  971. return 0;
  972. }
  973. if (cpufreq_driver->suspend) {
  974. ret = cpufreq_driver->suspend(cpu_policy, pmsg);
  975. if (ret) {
  976. printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
  977. "step on CPU %u\n", cpu_policy->cpu);
  978. cpufreq_cpu_put(cpu_policy);
  979. return ret;
  980. }
  981. }
  982. if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
  983. goto out;
  984. if (cpufreq_driver->get)
  985. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  986. if (!cur_freq || !cpu_policy->cur) {
  987. printk(KERN_ERR "cpufreq: suspend failed to assert current "
  988. "frequency is what timing core thinks it is.\n");
  989. goto out;
  990. }
  991. if (unlikely(cur_freq != cpu_policy->cur)) {
  992. struct cpufreq_freqs freqs;
  993. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  994. dprintk("Warning: CPU frequency is %u, "
  995. "cpufreq assumed %u kHz.\n",
  996. cur_freq, cpu_policy->cur);
  997. freqs.cpu = cpu;
  998. freqs.old = cpu_policy->cur;
  999. freqs.new = cur_freq;
  1000. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  1001. CPUFREQ_SUSPENDCHANGE, &freqs);
  1002. adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
  1003. cpu_policy->cur = cur_freq;
  1004. }
  1005. out:
  1006. cpufreq_cpu_put(cpu_policy);
  1007. return 0;
  1008. }
  1009. /**
  1010. * cpufreq_resume - restore proper CPU frequency handling after resume
  1011. *
  1012. * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
  1013. * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
  1014. * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
  1015. * restored.
  1016. */
  1017. static int cpufreq_resume(struct sys_device * sysdev)
  1018. {
  1019. int cpu = sysdev->id;
  1020. int ret = 0;
  1021. struct cpufreq_policy *cpu_policy;
  1022. dprintk("resuming cpu %u\n", cpu);
  1023. if (!cpu_online(cpu))
  1024. return 0;
  1025. /* we may be lax here as interrupts are off. Nonetheless
  1026. * we need to grab the correct cpu policy, as to check
  1027. * whether we really run on this CPU.
  1028. */
  1029. cpu_policy = cpufreq_cpu_get(cpu);
  1030. if (!cpu_policy)
  1031. return -EINVAL;
  1032. /* only handle each CPU group once */
  1033. if (unlikely(cpu_policy->cpu != cpu)) {
  1034. cpufreq_cpu_put(cpu_policy);
  1035. return 0;
  1036. }
  1037. if (cpufreq_driver->resume) {
  1038. ret = cpufreq_driver->resume(cpu_policy);
  1039. if (ret) {
  1040. printk(KERN_ERR "cpufreq: resume failed in ->resume "
  1041. "step on CPU %u\n", cpu_policy->cpu);
  1042. cpufreq_cpu_put(cpu_policy);
  1043. return ret;
  1044. }
  1045. }
  1046. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1047. unsigned int cur_freq = 0;
  1048. if (cpufreq_driver->get)
  1049. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  1050. if (!cur_freq || !cpu_policy->cur) {
  1051. printk(KERN_ERR "cpufreq: resume failed to assert "
  1052. "current frequency is what timing core "
  1053. "thinks it is.\n");
  1054. goto out;
  1055. }
  1056. if (unlikely(cur_freq != cpu_policy->cur)) {
  1057. struct cpufreq_freqs freqs;
  1058. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  1059. dprintk("Warning: CPU frequency"
  1060. "is %u, cpufreq assumed %u kHz.\n",
  1061. cur_freq, cpu_policy->cur);
  1062. freqs.cpu = cpu;
  1063. freqs.old = cpu_policy->cur;
  1064. freqs.new = cur_freq;
  1065. srcu_notifier_call_chain(
  1066. &cpufreq_transition_notifier_list,
  1067. CPUFREQ_RESUMECHANGE, &freqs);
  1068. adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
  1069. cpu_policy->cur = cur_freq;
  1070. }
  1071. }
  1072. out:
  1073. schedule_work(&cpu_policy->update);
  1074. cpufreq_cpu_put(cpu_policy);
  1075. return ret;
  1076. }
  1077. static struct sysdev_driver cpufreq_sysdev_driver = {
  1078. .add = cpufreq_add_dev,
  1079. .remove = cpufreq_remove_dev,
  1080. .suspend = cpufreq_suspend,
  1081. .resume = cpufreq_resume,
  1082. };
  1083. /*********************************************************************
  1084. * NOTIFIER LISTS INTERFACE *
  1085. *********************************************************************/
  1086. /**
  1087. * cpufreq_register_notifier - register a driver with cpufreq
  1088. * @nb: notifier function to register
  1089. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1090. *
  1091. * Add a driver to one of two lists: either a list of drivers that
  1092. * are notified about clock rate changes (once before and once after
  1093. * the transition), or a list of drivers that are notified about
  1094. * changes in cpufreq policy.
  1095. *
  1096. * This function may sleep, and has the same return conditions as
  1097. * blocking_notifier_chain_register.
  1098. */
  1099. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1100. {
  1101. int ret;
  1102. switch (list) {
  1103. case CPUFREQ_TRANSITION_NOTIFIER:
  1104. ret = srcu_notifier_chain_register(
  1105. &cpufreq_transition_notifier_list, nb);
  1106. break;
  1107. case CPUFREQ_POLICY_NOTIFIER:
  1108. ret = blocking_notifier_chain_register(
  1109. &cpufreq_policy_notifier_list, nb);
  1110. break;
  1111. default:
  1112. ret = -EINVAL;
  1113. }
  1114. return ret;
  1115. }
  1116. EXPORT_SYMBOL(cpufreq_register_notifier);
  1117. /**
  1118. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1119. * @nb: notifier block to be unregistered
  1120. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1121. *
  1122. * Remove a driver from the CPU frequency notifier list.
  1123. *
  1124. * This function may sleep, and has the same return conditions as
  1125. * blocking_notifier_chain_unregister.
  1126. */
  1127. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1128. {
  1129. int ret;
  1130. switch (list) {
  1131. case CPUFREQ_TRANSITION_NOTIFIER:
  1132. ret = srcu_notifier_chain_unregister(
  1133. &cpufreq_transition_notifier_list, nb);
  1134. break;
  1135. case CPUFREQ_POLICY_NOTIFIER:
  1136. ret = blocking_notifier_chain_unregister(
  1137. &cpufreq_policy_notifier_list, nb);
  1138. break;
  1139. default:
  1140. ret = -EINVAL;
  1141. }
  1142. return ret;
  1143. }
  1144. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1145. /*********************************************************************
  1146. * GOVERNORS *
  1147. *********************************************************************/
  1148. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1149. unsigned int target_freq,
  1150. unsigned int relation)
  1151. {
  1152. int retval = -EINVAL;
  1153. dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
  1154. target_freq, relation);
  1155. if (cpu_online(policy->cpu) && cpufreq_driver->target)
  1156. retval = cpufreq_driver->target(policy, target_freq, relation);
  1157. return retval;
  1158. }
  1159. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1160. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1161. unsigned int target_freq,
  1162. unsigned int relation)
  1163. {
  1164. int ret;
  1165. policy = cpufreq_cpu_get(policy->cpu);
  1166. if (!policy)
  1167. return -EINVAL;
  1168. if (unlikely(lock_policy_rwsem_write(policy->cpu)))
  1169. return -EINVAL;
  1170. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1171. unlock_policy_rwsem_write(policy->cpu);
  1172. cpufreq_cpu_put(policy);
  1173. return ret;
  1174. }
  1175. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1176. int __cpufreq_driver_getavg(struct cpufreq_policy *policy)
  1177. {
  1178. int ret = 0;
  1179. policy = cpufreq_cpu_get(policy->cpu);
  1180. if (!policy)
  1181. return -EINVAL;
  1182. if (cpu_online(policy->cpu) && cpufreq_driver->getavg)
  1183. ret = cpufreq_driver->getavg(policy->cpu);
  1184. cpufreq_cpu_put(policy);
  1185. return ret;
  1186. }
  1187. EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
  1188. /*
  1189. * when "event" is CPUFREQ_GOV_LIMITS
  1190. */
  1191. static int __cpufreq_governor(struct cpufreq_policy *policy,
  1192. unsigned int event)
  1193. {
  1194. int ret;
  1195. if (!try_module_get(policy->governor->owner))
  1196. return -EINVAL;
  1197. dprintk("__cpufreq_governor for CPU %u, event %u\n",
  1198. policy->cpu, event);
  1199. ret = policy->governor->governor(policy, event);
  1200. /* we keep one module reference alive for
  1201. each CPU governed by this CPU */
  1202. if ((event != CPUFREQ_GOV_START) || ret)
  1203. module_put(policy->governor->owner);
  1204. if ((event == CPUFREQ_GOV_STOP) && !ret)
  1205. module_put(policy->governor->owner);
  1206. return ret;
  1207. }
  1208. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1209. {
  1210. int err;
  1211. if (!governor)
  1212. return -EINVAL;
  1213. mutex_lock(&cpufreq_governor_mutex);
  1214. err = -EBUSY;
  1215. if (__find_governor(governor->name) == NULL) {
  1216. err = 0;
  1217. list_add(&governor->governor_list, &cpufreq_governor_list);
  1218. }
  1219. mutex_unlock(&cpufreq_governor_mutex);
  1220. return err;
  1221. }
  1222. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1223. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1224. {
  1225. if (!governor)
  1226. return;
  1227. mutex_lock(&cpufreq_governor_mutex);
  1228. list_del(&governor->governor_list);
  1229. mutex_unlock(&cpufreq_governor_mutex);
  1230. return;
  1231. }
  1232. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1233. /*********************************************************************
  1234. * POLICY INTERFACE *
  1235. *********************************************************************/
  1236. /**
  1237. * cpufreq_get_policy - get the current cpufreq_policy
  1238. * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
  1239. *
  1240. * Reads the current cpufreq policy.
  1241. */
  1242. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1243. {
  1244. struct cpufreq_policy *cpu_policy;
  1245. if (!policy)
  1246. return -EINVAL;
  1247. cpu_policy = cpufreq_cpu_get(cpu);
  1248. if (!cpu_policy)
  1249. return -EINVAL;
  1250. memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
  1251. cpufreq_cpu_put(cpu_policy);
  1252. return 0;
  1253. }
  1254. EXPORT_SYMBOL(cpufreq_get_policy);
  1255. /*
  1256. * data : current policy.
  1257. * policy : policy to be set.
  1258. */
  1259. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  1260. struct cpufreq_policy *policy)
  1261. {
  1262. int ret = 0;
  1263. cpufreq_debug_disable_ratelimit();
  1264. dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
  1265. policy->min, policy->max);
  1266. memcpy(&policy->cpuinfo, &data->cpuinfo,
  1267. sizeof(struct cpufreq_cpuinfo));
  1268. if (policy->min > data->min && policy->min > policy->max) {
  1269. ret = -EINVAL;
  1270. goto error_out;
  1271. }
  1272. /* verify the cpu speed can be set within this limit */
  1273. ret = cpufreq_driver->verify(policy);
  1274. if (ret)
  1275. goto error_out;
  1276. /* adjust if necessary - all reasons */
  1277. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1278. CPUFREQ_ADJUST, policy);
  1279. /* adjust if necessary - hardware incompatibility*/
  1280. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1281. CPUFREQ_INCOMPATIBLE, policy);
  1282. /* verify the cpu speed can be set within this limit,
  1283. which might be different to the first one */
  1284. ret = cpufreq_driver->verify(policy);
  1285. if (ret)
  1286. goto error_out;
  1287. /* notification of the new policy */
  1288. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1289. CPUFREQ_NOTIFY, policy);
  1290. data->min = policy->min;
  1291. data->max = policy->max;
  1292. dprintk("new min and max freqs are %u - %u kHz\n",
  1293. data->min, data->max);
  1294. if (cpufreq_driver->setpolicy) {
  1295. data->policy = policy->policy;
  1296. dprintk("setting range\n");
  1297. ret = cpufreq_driver->setpolicy(policy);
  1298. } else {
  1299. if (policy->governor != data->governor) {
  1300. /* save old, working values */
  1301. struct cpufreq_governor *old_gov = data->governor;
  1302. dprintk("governor switch\n");
  1303. /* end old governor */
  1304. if (data->governor)
  1305. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  1306. /* start new governor */
  1307. data->governor = policy->governor;
  1308. if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
  1309. /* new governor failed, so re-start old one */
  1310. dprintk("starting governor %s failed\n",
  1311. data->governor->name);
  1312. if (old_gov) {
  1313. data->governor = old_gov;
  1314. __cpufreq_governor(data,
  1315. CPUFREQ_GOV_START);
  1316. }
  1317. ret = -EINVAL;
  1318. goto error_out;
  1319. }
  1320. /* might be a policy change, too, so fall through */
  1321. }
  1322. dprintk("governor: change or update limits\n");
  1323. __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
  1324. }
  1325. error_out:
  1326. cpufreq_debug_enable_ratelimit();
  1327. return ret;
  1328. }
  1329. /**
  1330. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1331. * @cpu: CPU which shall be re-evaluated
  1332. *
  1333. * Usefull for policy notifiers which have different necessities
  1334. * at different times.
  1335. */
  1336. int cpufreq_update_policy(unsigned int cpu)
  1337. {
  1338. struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
  1339. struct cpufreq_policy policy;
  1340. int ret = 0;
  1341. if (!data)
  1342. return -ENODEV;
  1343. if (unlikely(lock_policy_rwsem_write(cpu)))
  1344. return -EINVAL;
  1345. dprintk("updating policy for CPU %u\n", cpu);
  1346. memcpy(&policy, data, sizeof(struct cpufreq_policy));
  1347. policy.min = data->user_policy.min;
  1348. policy.max = data->user_policy.max;
  1349. policy.policy = data->user_policy.policy;
  1350. policy.governor = data->user_policy.governor;
  1351. /* BIOS might change freq behind our back
  1352. -> ask driver for current freq and notify governors about a change */
  1353. if (cpufreq_driver->get) {
  1354. policy.cur = cpufreq_driver->get(cpu);
  1355. if (!data->cur) {
  1356. dprintk("Driver did not initialize current freq");
  1357. data->cur = policy.cur;
  1358. } else {
  1359. if (data->cur != policy.cur)
  1360. cpufreq_out_of_sync(cpu, data->cur,
  1361. policy.cur);
  1362. }
  1363. }
  1364. ret = __cpufreq_set_policy(data, &policy);
  1365. unlock_policy_rwsem_write(cpu);
  1366. cpufreq_cpu_put(data);
  1367. return ret;
  1368. }
  1369. EXPORT_SYMBOL(cpufreq_update_policy);
  1370. static int cpufreq_cpu_callback(struct notifier_block *nfb,
  1371. unsigned long action, void *hcpu)
  1372. {
  1373. unsigned int cpu = (unsigned long)hcpu;
  1374. struct sys_device *sys_dev;
  1375. struct cpufreq_policy *policy;
  1376. sys_dev = get_cpu_sysdev(cpu);
  1377. if (sys_dev) {
  1378. switch (action) {
  1379. case CPU_ONLINE:
  1380. case CPU_ONLINE_FROZEN:
  1381. cpufreq_add_dev(sys_dev);
  1382. break;
  1383. case CPU_DOWN_PREPARE:
  1384. case CPU_DOWN_PREPARE_FROZEN:
  1385. if (unlikely(lock_policy_rwsem_write(cpu)))
  1386. BUG();
  1387. policy = cpufreq_cpu_data[cpu];
  1388. if (policy) {
  1389. __cpufreq_driver_target(policy, policy->min,
  1390. CPUFREQ_RELATION_H);
  1391. }
  1392. __cpufreq_remove_dev(sys_dev);
  1393. break;
  1394. case CPU_DOWN_FAILED:
  1395. case CPU_DOWN_FAILED_FROZEN:
  1396. cpufreq_add_dev(sys_dev);
  1397. break;
  1398. }
  1399. }
  1400. return NOTIFY_OK;
  1401. }
  1402. static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
  1403. {
  1404. .notifier_call = cpufreq_cpu_callback,
  1405. };
  1406. /*********************************************************************
  1407. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1408. *********************************************************************/
  1409. /**
  1410. * cpufreq_register_driver - register a CPU Frequency driver
  1411. * @driver_data: A struct cpufreq_driver containing the values#
  1412. * submitted by the CPU Frequency driver.
  1413. *
  1414. * Registers a CPU Frequency driver to this core code. This code
  1415. * returns zero on success, -EBUSY when another driver got here first
  1416. * (and isn't unregistered in the meantime).
  1417. *
  1418. */
  1419. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  1420. {
  1421. unsigned long flags;
  1422. int ret;
  1423. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1424. ((!driver_data->setpolicy) && (!driver_data->target)))
  1425. return -EINVAL;
  1426. dprintk("trying to register driver %s\n", driver_data->name);
  1427. if (driver_data->setpolicy)
  1428. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  1429. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1430. if (cpufreq_driver) {
  1431. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1432. return -EBUSY;
  1433. }
  1434. cpufreq_driver = driver_data;
  1435. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1436. ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
  1437. if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  1438. int i;
  1439. ret = -ENODEV;
  1440. /* check for at least one working CPU */
  1441. for (i=0; i<NR_CPUS; i++)
  1442. if (cpufreq_cpu_data[i])
  1443. ret = 0;
  1444. /* if all ->init() calls failed, unregister */
  1445. if (ret) {
  1446. dprintk("no CPU initialized for driver %s\n",
  1447. driver_data->name);
  1448. sysdev_driver_unregister(&cpu_sysdev_class,
  1449. &cpufreq_sysdev_driver);
  1450. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1451. cpufreq_driver = NULL;
  1452. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1453. }
  1454. }
  1455. if (!ret) {
  1456. register_hotcpu_notifier(&cpufreq_cpu_notifier);
  1457. dprintk("driver %s up and running\n", driver_data->name);
  1458. cpufreq_debug_enable_ratelimit();
  1459. }
  1460. return (ret);
  1461. }
  1462. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  1463. /**
  1464. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  1465. *
  1466. * Unregister the current CPUFreq driver. Only call this if you have
  1467. * the right to do so, i.e. if you have succeeded in initialising before!
  1468. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  1469. * currently not initialised.
  1470. */
  1471. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  1472. {
  1473. unsigned long flags;
  1474. cpufreq_debug_disable_ratelimit();
  1475. if (!cpufreq_driver || (driver != cpufreq_driver)) {
  1476. cpufreq_debug_enable_ratelimit();
  1477. return -EINVAL;
  1478. }
  1479. dprintk("unregistering driver %s\n", driver->name);
  1480. sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
  1481. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  1482. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1483. cpufreq_driver = NULL;
  1484. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1485. return 0;
  1486. }
  1487. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  1488. static int __init cpufreq_core_init(void)
  1489. {
  1490. int cpu;
  1491. for_each_possible_cpu(cpu) {
  1492. per_cpu(policy_cpu, cpu) = -1;
  1493. init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
  1494. }
  1495. return 0;
  1496. }
  1497. core_initcall(cpufreq_core_init);