cpufreq.c 42 KB

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