cpufreq.c 40 KB

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