cpufreq.c 43 KB

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