cpufreq.c 39 KB

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