cpufreq.c 42 KB

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