cpufreq.c 42 KB

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