cpufreq.c 42 KB

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