cpufreq.c 40 KB

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