cpufreq.c 39 KB

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