cpufreq.c 42 KB

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