cpufreq.c 41 KB

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