cpufreq.c 39 KB

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