cpufreq.c 43 KB

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