cpufreq.c 43 KB

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