cpufreq.c 41 KB

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