cpufreq.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  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. ret = sysfs_create_link(&sys_dev->kobj,
  601. &managed_policy->kobj,
  602. "cpufreq");
  603. if (ret) {
  604. mutex_unlock(&policy->lock);
  605. goto err_out_driver_exit;
  606. }
  607. cpufreq_debug_enable_ratelimit();
  608. mutex_unlock(&policy->lock);
  609. ret = 0;
  610. goto err_out_driver_exit; /* call driver->exit() */
  611. }
  612. }
  613. #endif
  614. memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
  615. /* prepare interface data */
  616. policy->kobj.parent = &sys_dev->kobj;
  617. policy->kobj.ktype = &ktype_cpufreq;
  618. strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
  619. ret = kobject_register(&policy->kobj);
  620. if (ret) {
  621. mutex_unlock(&policy->lock);
  622. goto err_out_driver_exit;
  623. }
  624. /* set up files for this cpu device */
  625. drv_attr = cpufreq_driver->attr;
  626. while ((drv_attr) && (*drv_attr)) {
  627. sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  628. drv_attr++;
  629. }
  630. if (cpufreq_driver->get)
  631. sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  632. if (cpufreq_driver->target)
  633. sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  634. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  635. for_each_cpu_mask(j, policy->cpus)
  636. cpufreq_cpu_data[j] = policy;
  637. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  638. /* symlink affected CPUs */
  639. for_each_cpu_mask(j, policy->cpus) {
  640. if (j == cpu)
  641. continue;
  642. if (!cpu_online(j))
  643. continue;
  644. dprintk("CPU %u already managed, adding link\n", j);
  645. cpufreq_cpu_get(cpu);
  646. cpu_sys_dev = get_cpu_sysdev(j);
  647. ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
  648. "cpufreq");
  649. if (ret) {
  650. mutex_unlock(&policy->lock);
  651. goto err_out_unregister;
  652. }
  653. }
  654. policy->governor = NULL; /* to assure that the starting sequence is
  655. * run in cpufreq_set_policy */
  656. mutex_unlock(&policy->lock);
  657. /* set default policy */
  658. ret = cpufreq_set_policy(&new_policy);
  659. if (ret) {
  660. dprintk("setting policy failed\n");
  661. goto err_out_unregister;
  662. }
  663. module_put(cpufreq_driver->owner);
  664. dprintk("initialization complete\n");
  665. cpufreq_debug_enable_ratelimit();
  666. return 0;
  667. err_out_unregister:
  668. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  669. for_each_cpu_mask(j, policy->cpus)
  670. cpufreq_cpu_data[j] = NULL;
  671. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  672. kobject_unregister(&policy->kobj);
  673. wait_for_completion(&policy->kobj_unregister);
  674. err_out_driver_exit:
  675. if (cpufreq_driver->exit)
  676. cpufreq_driver->exit(policy);
  677. err_out:
  678. kfree(policy);
  679. nomem_out:
  680. module_put(cpufreq_driver->owner);
  681. module_out:
  682. cpufreq_debug_enable_ratelimit();
  683. return ret;
  684. }
  685. /**
  686. * cpufreq_remove_dev - remove a CPU device
  687. *
  688. * Removes the cpufreq interface for a CPU device.
  689. */
  690. static int cpufreq_remove_dev (struct sys_device * sys_dev)
  691. {
  692. unsigned int cpu = sys_dev->id;
  693. unsigned long flags;
  694. struct cpufreq_policy *data;
  695. #ifdef CONFIG_SMP
  696. struct sys_device *cpu_sys_dev;
  697. unsigned int j;
  698. #endif
  699. cpufreq_debug_disable_ratelimit();
  700. dprintk("unregistering CPU %u\n", cpu);
  701. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  702. data = cpufreq_cpu_data[cpu];
  703. if (!data) {
  704. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  705. cpufreq_debug_enable_ratelimit();
  706. return -EINVAL;
  707. }
  708. cpufreq_cpu_data[cpu] = NULL;
  709. #ifdef CONFIG_SMP
  710. /* if this isn't the CPU which is the parent of the kobj, we
  711. * only need to unlink, put and exit
  712. */
  713. if (unlikely(cpu != data->cpu)) {
  714. dprintk("removing link\n");
  715. cpu_clear(cpu, data->cpus);
  716. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  717. sysfs_remove_link(&sys_dev->kobj, "cpufreq");
  718. cpufreq_cpu_put(data);
  719. cpufreq_debug_enable_ratelimit();
  720. return 0;
  721. }
  722. #endif
  723. if (!kobject_get(&data->kobj)) {
  724. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  725. cpufreq_debug_enable_ratelimit();
  726. return -EFAULT;
  727. }
  728. #ifdef CONFIG_SMP
  729. /* if we have other CPUs still registered, we need to unlink them,
  730. * or else wait_for_completion below will lock up. Clean the
  731. * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
  732. * links afterwards.
  733. */
  734. if (unlikely(cpus_weight(data->cpus) > 1)) {
  735. for_each_cpu_mask(j, data->cpus) {
  736. if (j == cpu)
  737. continue;
  738. cpufreq_cpu_data[j] = NULL;
  739. }
  740. }
  741. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  742. if (unlikely(cpus_weight(data->cpus) > 1)) {
  743. for_each_cpu_mask(j, data->cpus) {
  744. if (j == cpu)
  745. continue;
  746. dprintk("removing link for cpu %u\n", j);
  747. cpu_sys_dev = get_cpu_sysdev(j);
  748. sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
  749. cpufreq_cpu_put(data);
  750. }
  751. }
  752. #else
  753. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  754. #endif
  755. mutex_lock(&data->lock);
  756. if (cpufreq_driver->target)
  757. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  758. mutex_unlock(&data->lock);
  759. kobject_unregister(&data->kobj);
  760. kobject_put(&data->kobj);
  761. /* we need to make sure that the underlying kobj is actually
  762. * not referenced anymore by anybody before we proceed with
  763. * unloading.
  764. */
  765. dprintk("waiting for dropping of refcount\n");
  766. wait_for_completion(&data->kobj_unregister);
  767. dprintk("wait complete\n");
  768. if (cpufreq_driver->exit)
  769. cpufreq_driver->exit(data);
  770. kfree(data);
  771. cpufreq_debug_enable_ratelimit();
  772. return 0;
  773. }
  774. static void handle_update(struct work_struct *work)
  775. {
  776. struct cpufreq_policy *policy =
  777. container_of(work, struct cpufreq_policy, update);
  778. unsigned int cpu = policy->cpu;
  779. dprintk("handle_update for cpu %u called\n", cpu);
  780. cpufreq_update_policy(cpu);
  781. }
  782. /**
  783. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
  784. * @cpu: cpu number
  785. * @old_freq: CPU frequency the kernel thinks the CPU runs at
  786. * @new_freq: CPU frequency the CPU actually runs at
  787. *
  788. * We adjust to current frequency first, and need to clean up later. So either call
  789. * to cpufreq_update_policy() or schedule handle_update()).
  790. */
  791. static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
  792. unsigned int new_freq)
  793. {
  794. struct cpufreq_freqs freqs;
  795. dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
  796. "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
  797. freqs.cpu = cpu;
  798. freqs.old = old_freq;
  799. freqs.new = new_freq;
  800. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  801. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  802. }
  803. /**
  804. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  805. * @cpu: CPU number
  806. *
  807. * This is the last known freq, without actually getting it from the driver.
  808. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  809. */
  810. unsigned int cpufreq_quick_get(unsigned int cpu)
  811. {
  812. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  813. unsigned int ret_freq = 0;
  814. if (policy) {
  815. mutex_lock(&policy->lock);
  816. ret_freq = policy->cur;
  817. mutex_unlock(&policy->lock);
  818. cpufreq_cpu_put(policy);
  819. }
  820. return (ret_freq);
  821. }
  822. EXPORT_SYMBOL(cpufreq_quick_get);
  823. /**
  824. * cpufreq_get - get the current CPU frequency (in kHz)
  825. * @cpu: CPU number
  826. *
  827. * Get the CPU current (static) CPU frequency
  828. */
  829. unsigned int cpufreq_get(unsigned int cpu)
  830. {
  831. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  832. unsigned int ret_freq = 0;
  833. if (!policy)
  834. return 0;
  835. if (!cpufreq_driver->get)
  836. goto out;
  837. mutex_lock(&policy->lock);
  838. ret_freq = cpufreq_driver->get(cpu);
  839. if (ret_freq && policy->cur &&
  840. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  841. /* verify no discrepancy between actual and
  842. saved value exists */
  843. if (unlikely(ret_freq != policy->cur)) {
  844. cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
  845. schedule_work(&policy->update);
  846. }
  847. }
  848. mutex_unlock(&policy->lock);
  849. out:
  850. cpufreq_cpu_put(policy);
  851. return (ret_freq);
  852. }
  853. EXPORT_SYMBOL(cpufreq_get);
  854. /**
  855. * cpufreq_suspend - let the low level driver prepare for suspend
  856. */
  857. static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
  858. {
  859. int cpu = sysdev->id;
  860. int ret = 0;
  861. unsigned int cur_freq = 0;
  862. struct cpufreq_policy *cpu_policy;
  863. dprintk("suspending cpu %u\n", cpu);
  864. if (!cpu_online(cpu))
  865. return 0;
  866. /* we may be lax here as interrupts are off. Nonetheless
  867. * we need to grab the correct cpu policy, as to check
  868. * whether we really run on this CPU.
  869. */
  870. cpu_policy = cpufreq_cpu_get(cpu);
  871. if (!cpu_policy)
  872. return -EINVAL;
  873. /* only handle each CPU group once */
  874. if (unlikely(cpu_policy->cpu != cpu)) {
  875. cpufreq_cpu_put(cpu_policy);
  876. return 0;
  877. }
  878. if (cpufreq_driver->suspend) {
  879. ret = cpufreq_driver->suspend(cpu_policy, pmsg);
  880. if (ret) {
  881. printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
  882. "step on CPU %u\n", cpu_policy->cpu);
  883. cpufreq_cpu_put(cpu_policy);
  884. return ret;
  885. }
  886. }
  887. if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
  888. goto out;
  889. if (cpufreq_driver->get)
  890. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  891. if (!cur_freq || !cpu_policy->cur) {
  892. printk(KERN_ERR "cpufreq: suspend failed to assert current "
  893. "frequency is what timing core thinks it is.\n");
  894. goto out;
  895. }
  896. if (unlikely(cur_freq != cpu_policy->cur)) {
  897. struct cpufreq_freqs freqs;
  898. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  899. dprintk("Warning: CPU frequency is %u, "
  900. "cpufreq assumed %u kHz.\n",
  901. cur_freq, cpu_policy->cur);
  902. freqs.cpu = cpu;
  903. freqs.old = cpu_policy->cur;
  904. freqs.new = cur_freq;
  905. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  906. CPUFREQ_SUSPENDCHANGE, &freqs);
  907. adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
  908. cpu_policy->cur = cur_freq;
  909. }
  910. out:
  911. cpufreq_cpu_put(cpu_policy);
  912. return 0;
  913. }
  914. /**
  915. * cpufreq_resume - restore proper CPU frequency handling after resume
  916. *
  917. * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
  918. * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
  919. * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
  920. * restored.
  921. */
  922. static int cpufreq_resume(struct sys_device * sysdev)
  923. {
  924. int cpu = sysdev->id;
  925. int ret = 0;
  926. struct cpufreq_policy *cpu_policy;
  927. dprintk("resuming cpu %u\n", cpu);
  928. if (!cpu_online(cpu))
  929. return 0;
  930. /* we may be lax here as interrupts are off. Nonetheless
  931. * we need to grab the correct cpu policy, as to check
  932. * whether we really run on this CPU.
  933. */
  934. cpu_policy = cpufreq_cpu_get(cpu);
  935. if (!cpu_policy)
  936. return -EINVAL;
  937. /* only handle each CPU group once */
  938. if (unlikely(cpu_policy->cpu != cpu)) {
  939. cpufreq_cpu_put(cpu_policy);
  940. return 0;
  941. }
  942. if (cpufreq_driver->resume) {
  943. ret = cpufreq_driver->resume(cpu_policy);
  944. if (ret) {
  945. printk(KERN_ERR "cpufreq: resume failed in ->resume "
  946. "step on CPU %u\n", cpu_policy->cpu);
  947. cpufreq_cpu_put(cpu_policy);
  948. return ret;
  949. }
  950. }
  951. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  952. unsigned int cur_freq = 0;
  953. if (cpufreq_driver->get)
  954. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  955. if (!cur_freq || !cpu_policy->cur) {
  956. printk(KERN_ERR "cpufreq: resume failed to assert "
  957. "current frequency is what timing core "
  958. "thinks it is.\n");
  959. goto out;
  960. }
  961. if (unlikely(cur_freq != cpu_policy->cur)) {
  962. struct cpufreq_freqs freqs;
  963. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  964. dprintk("Warning: CPU frequency"
  965. "is %u, cpufreq assumed %u kHz.\n",
  966. cur_freq, cpu_policy->cur);
  967. freqs.cpu = cpu;
  968. freqs.old = cpu_policy->cur;
  969. freqs.new = cur_freq;
  970. srcu_notifier_call_chain(
  971. &cpufreq_transition_notifier_list,
  972. CPUFREQ_RESUMECHANGE, &freqs);
  973. adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
  974. cpu_policy->cur = cur_freq;
  975. }
  976. }
  977. out:
  978. schedule_work(&cpu_policy->update);
  979. cpufreq_cpu_put(cpu_policy);
  980. return ret;
  981. }
  982. static struct sysdev_driver cpufreq_sysdev_driver = {
  983. .add = cpufreq_add_dev,
  984. .remove = cpufreq_remove_dev,
  985. .suspend = cpufreq_suspend,
  986. .resume = cpufreq_resume,
  987. };
  988. /*********************************************************************
  989. * NOTIFIER LISTS INTERFACE *
  990. *********************************************************************/
  991. /**
  992. * cpufreq_register_notifier - register a driver with cpufreq
  993. * @nb: notifier function to register
  994. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  995. *
  996. * Add a driver to one of two lists: either a list of drivers that
  997. * are notified about clock rate changes (once before and once after
  998. * the transition), or a list of drivers that are notified about
  999. * changes in cpufreq policy.
  1000. *
  1001. * This function may sleep, and has the same return conditions as
  1002. * blocking_notifier_chain_register.
  1003. */
  1004. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1005. {
  1006. int ret;
  1007. switch (list) {
  1008. case CPUFREQ_TRANSITION_NOTIFIER:
  1009. ret = srcu_notifier_chain_register(
  1010. &cpufreq_transition_notifier_list, nb);
  1011. break;
  1012. case CPUFREQ_POLICY_NOTIFIER:
  1013. ret = blocking_notifier_chain_register(
  1014. &cpufreq_policy_notifier_list, nb);
  1015. break;
  1016. default:
  1017. ret = -EINVAL;
  1018. }
  1019. return ret;
  1020. }
  1021. EXPORT_SYMBOL(cpufreq_register_notifier);
  1022. /**
  1023. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1024. * @nb: notifier block to be unregistered
  1025. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1026. *
  1027. * Remove a driver from the CPU frequency notifier list.
  1028. *
  1029. * This function may sleep, and has the same return conditions as
  1030. * blocking_notifier_chain_unregister.
  1031. */
  1032. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1033. {
  1034. int ret;
  1035. switch (list) {
  1036. case CPUFREQ_TRANSITION_NOTIFIER:
  1037. ret = srcu_notifier_chain_unregister(
  1038. &cpufreq_transition_notifier_list, nb);
  1039. break;
  1040. case CPUFREQ_POLICY_NOTIFIER:
  1041. ret = blocking_notifier_chain_unregister(
  1042. &cpufreq_policy_notifier_list, nb);
  1043. break;
  1044. default:
  1045. ret = -EINVAL;
  1046. }
  1047. return ret;
  1048. }
  1049. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1050. /*********************************************************************
  1051. * GOVERNORS *
  1052. *********************************************************************/
  1053. /* Must be called with lock_cpu_hotplug held */
  1054. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1055. unsigned int target_freq,
  1056. unsigned int relation)
  1057. {
  1058. int retval = -EINVAL;
  1059. dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
  1060. target_freq, relation);
  1061. if (cpu_online(policy->cpu) && cpufreq_driver->target)
  1062. retval = cpufreq_driver->target(policy, target_freq, relation);
  1063. return retval;
  1064. }
  1065. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1066. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1067. unsigned int target_freq,
  1068. unsigned int relation)
  1069. {
  1070. int ret;
  1071. policy = cpufreq_cpu_get(policy->cpu);
  1072. if (!policy)
  1073. return -EINVAL;
  1074. lock_cpu_hotplug();
  1075. mutex_lock(&policy->lock);
  1076. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1077. mutex_unlock(&policy->lock);
  1078. unlock_cpu_hotplug();
  1079. cpufreq_cpu_put(policy);
  1080. return ret;
  1081. }
  1082. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1083. int cpufreq_driver_getavg(struct cpufreq_policy *policy)
  1084. {
  1085. int ret = 0;
  1086. policy = cpufreq_cpu_get(policy->cpu);
  1087. if (!policy)
  1088. return -EINVAL;
  1089. mutex_lock(&policy->lock);
  1090. if (cpu_online(policy->cpu) && cpufreq_driver->getavg)
  1091. ret = cpufreq_driver->getavg(policy->cpu);
  1092. mutex_unlock(&policy->lock);
  1093. cpufreq_cpu_put(policy);
  1094. return ret;
  1095. }
  1096. EXPORT_SYMBOL_GPL(cpufreq_driver_getavg);
  1097. /*
  1098. * Locking: Must be called with the lock_cpu_hotplug() lock held
  1099. * when "event" is CPUFREQ_GOV_LIMITS
  1100. */
  1101. static int __cpufreq_governor(struct cpufreq_policy *policy,
  1102. unsigned int event)
  1103. {
  1104. int ret;
  1105. if (!try_module_get(policy->governor->owner))
  1106. return -EINVAL;
  1107. dprintk("__cpufreq_governor for CPU %u, event %u\n",
  1108. policy->cpu, event);
  1109. ret = policy->governor->governor(policy, event);
  1110. /* we keep one module reference alive for
  1111. each CPU governed by this CPU */
  1112. if ((event != CPUFREQ_GOV_START) || ret)
  1113. module_put(policy->governor->owner);
  1114. if ((event == CPUFREQ_GOV_STOP) && !ret)
  1115. module_put(policy->governor->owner);
  1116. return ret;
  1117. }
  1118. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1119. {
  1120. int err;
  1121. if (!governor)
  1122. return -EINVAL;
  1123. mutex_lock(&cpufreq_governor_mutex);
  1124. err = -EBUSY;
  1125. if (__find_governor(governor->name) == NULL) {
  1126. err = 0;
  1127. list_add(&governor->governor_list, &cpufreq_governor_list);
  1128. }
  1129. mutex_unlock(&cpufreq_governor_mutex);
  1130. return err;
  1131. }
  1132. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1133. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1134. {
  1135. if (!governor)
  1136. return;
  1137. mutex_lock(&cpufreq_governor_mutex);
  1138. list_del(&governor->governor_list);
  1139. mutex_unlock(&cpufreq_governor_mutex);
  1140. return;
  1141. }
  1142. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1143. /*********************************************************************
  1144. * POLICY INTERFACE *
  1145. *********************************************************************/
  1146. /**
  1147. * cpufreq_get_policy - get the current cpufreq_policy
  1148. * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
  1149. *
  1150. * Reads the current cpufreq policy.
  1151. */
  1152. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1153. {
  1154. struct cpufreq_policy *cpu_policy;
  1155. if (!policy)
  1156. return -EINVAL;
  1157. cpu_policy = cpufreq_cpu_get(cpu);
  1158. if (!cpu_policy)
  1159. return -EINVAL;
  1160. mutex_lock(&cpu_policy->lock);
  1161. memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
  1162. mutex_unlock(&cpu_policy->lock);
  1163. cpufreq_cpu_put(cpu_policy);
  1164. return 0;
  1165. }
  1166. EXPORT_SYMBOL(cpufreq_get_policy);
  1167. /*
  1168. * data : current policy.
  1169. * policy : policy to be set.
  1170. * Locking: Must be called with the lock_cpu_hotplug() lock held
  1171. */
  1172. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  1173. struct cpufreq_policy *policy)
  1174. {
  1175. int ret = 0;
  1176. cpufreq_debug_disable_ratelimit();
  1177. dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
  1178. policy->min, policy->max);
  1179. memcpy(&policy->cpuinfo, &data->cpuinfo,
  1180. sizeof(struct cpufreq_cpuinfo));
  1181. if (policy->min > data->min && policy->min > policy->max) {
  1182. ret = -EINVAL;
  1183. goto error_out;
  1184. }
  1185. /* verify the cpu speed can be set within this limit */
  1186. ret = cpufreq_driver->verify(policy);
  1187. if (ret)
  1188. goto error_out;
  1189. /* adjust if necessary - all reasons */
  1190. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1191. CPUFREQ_ADJUST, policy);
  1192. /* adjust if necessary - hardware incompatibility*/
  1193. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1194. CPUFREQ_INCOMPATIBLE, policy);
  1195. /* verify the cpu speed can be set within this limit,
  1196. which might be different to the first one */
  1197. ret = cpufreq_driver->verify(policy);
  1198. if (ret)
  1199. goto error_out;
  1200. /* notification of the new policy */
  1201. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1202. CPUFREQ_NOTIFY, policy);
  1203. data->min = policy->min;
  1204. data->max = policy->max;
  1205. dprintk("new min and max freqs are %u - %u kHz\n",
  1206. data->min, data->max);
  1207. if (cpufreq_driver->setpolicy) {
  1208. data->policy = policy->policy;
  1209. dprintk("setting range\n");
  1210. ret = cpufreq_driver->setpolicy(policy);
  1211. } else {
  1212. if (policy->governor != data->governor) {
  1213. /* save old, working values */
  1214. struct cpufreq_governor *old_gov = data->governor;
  1215. dprintk("governor switch\n");
  1216. /* end old governor */
  1217. if (data->governor)
  1218. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  1219. /* start new governor */
  1220. data->governor = policy->governor;
  1221. if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
  1222. /* new governor failed, so re-start old one */
  1223. dprintk("starting governor %s failed\n",
  1224. data->governor->name);
  1225. if (old_gov) {
  1226. data->governor = old_gov;
  1227. __cpufreq_governor(data,
  1228. CPUFREQ_GOV_START);
  1229. }
  1230. ret = -EINVAL;
  1231. goto error_out;
  1232. }
  1233. /* might be a policy change, too, so fall through */
  1234. }
  1235. dprintk("governor: change or update limits\n");
  1236. __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
  1237. }
  1238. error_out:
  1239. cpufreq_debug_enable_ratelimit();
  1240. return ret;
  1241. }
  1242. /**
  1243. * cpufreq_set_policy - set a new CPUFreq policy
  1244. * @policy: policy to be set.
  1245. *
  1246. * Sets a new CPU frequency and voltage scaling policy.
  1247. */
  1248. int cpufreq_set_policy(struct cpufreq_policy *policy)
  1249. {
  1250. int ret = 0;
  1251. struct cpufreq_policy *data;
  1252. if (!policy)
  1253. return -EINVAL;
  1254. data = cpufreq_cpu_get(policy->cpu);
  1255. if (!data)
  1256. return -EINVAL;
  1257. lock_cpu_hotplug();
  1258. /* lock this CPU */
  1259. mutex_lock(&data->lock);
  1260. ret = __cpufreq_set_policy(data, policy);
  1261. data->user_policy.min = data->min;
  1262. data->user_policy.max = data->max;
  1263. data->user_policy.policy = data->policy;
  1264. data->user_policy.governor = data->governor;
  1265. mutex_unlock(&data->lock);
  1266. unlock_cpu_hotplug();
  1267. cpufreq_cpu_put(data);
  1268. return ret;
  1269. }
  1270. EXPORT_SYMBOL(cpufreq_set_policy);
  1271. /**
  1272. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1273. * @cpu: CPU which shall be re-evaluated
  1274. *
  1275. * Usefull for policy notifiers which have different necessities
  1276. * at different times.
  1277. */
  1278. int cpufreq_update_policy(unsigned int cpu)
  1279. {
  1280. struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
  1281. struct cpufreq_policy policy;
  1282. int ret = 0;
  1283. if (!data)
  1284. return -ENODEV;
  1285. lock_cpu_hotplug();
  1286. mutex_lock(&data->lock);
  1287. dprintk("updating policy for CPU %u\n", cpu);
  1288. memcpy(&policy, data, sizeof(struct cpufreq_policy));
  1289. policy.min = data->user_policy.min;
  1290. policy.max = data->user_policy.max;
  1291. policy.policy = data->user_policy.policy;
  1292. policy.governor = data->user_policy.governor;
  1293. /* BIOS might change freq behind our back
  1294. -> ask driver for current freq and notify governors about a change */
  1295. if (cpufreq_driver->get) {
  1296. policy.cur = cpufreq_driver->get(cpu);
  1297. if (!data->cur) {
  1298. dprintk("Driver did not initialize current freq");
  1299. data->cur = policy.cur;
  1300. } else {
  1301. if (data->cur != policy.cur)
  1302. cpufreq_out_of_sync(cpu, data->cur,
  1303. policy.cur);
  1304. }
  1305. }
  1306. ret = __cpufreq_set_policy(data, &policy);
  1307. mutex_unlock(&data->lock);
  1308. unlock_cpu_hotplug();
  1309. cpufreq_cpu_put(data);
  1310. return ret;
  1311. }
  1312. EXPORT_SYMBOL(cpufreq_update_policy);
  1313. static int cpufreq_cpu_callback(struct notifier_block *nfb,
  1314. unsigned long action, void *hcpu)
  1315. {
  1316. unsigned int cpu = (unsigned long)hcpu;
  1317. struct cpufreq_policy *policy;
  1318. struct sys_device *sys_dev;
  1319. sys_dev = get_cpu_sysdev(cpu);
  1320. if (sys_dev) {
  1321. switch (action) {
  1322. case CPU_ONLINE:
  1323. cpufreq_add_dev(sys_dev);
  1324. break;
  1325. case CPU_DOWN_PREPARE:
  1326. /*
  1327. * We attempt to put this cpu in lowest frequency
  1328. * possible before going down. This will permit
  1329. * hardware-managed P-State to switch other related
  1330. * threads to min or higher speeds if possible.
  1331. */
  1332. policy = cpufreq_cpu_data[cpu];
  1333. if (policy) {
  1334. cpufreq_driver_target(policy, policy->min,
  1335. CPUFREQ_RELATION_H);
  1336. }
  1337. break;
  1338. case CPU_DEAD:
  1339. cpufreq_remove_dev(sys_dev);
  1340. break;
  1341. }
  1342. }
  1343. return NOTIFY_OK;
  1344. }
  1345. static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
  1346. {
  1347. .notifier_call = cpufreq_cpu_callback,
  1348. };
  1349. /*********************************************************************
  1350. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1351. *********************************************************************/
  1352. /**
  1353. * cpufreq_register_driver - register a CPU Frequency driver
  1354. * @driver_data: A struct cpufreq_driver containing the values#
  1355. * submitted by the CPU Frequency driver.
  1356. *
  1357. * Registers a CPU Frequency driver to this core code. This code
  1358. * returns zero on success, -EBUSY when another driver got here first
  1359. * (and isn't unregistered in the meantime).
  1360. *
  1361. */
  1362. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  1363. {
  1364. unsigned long flags;
  1365. int ret;
  1366. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1367. ((!driver_data->setpolicy) && (!driver_data->target)))
  1368. return -EINVAL;
  1369. dprintk("trying to register driver %s\n", driver_data->name);
  1370. if (driver_data->setpolicy)
  1371. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  1372. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1373. if (cpufreq_driver) {
  1374. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1375. return -EBUSY;
  1376. }
  1377. cpufreq_driver = driver_data;
  1378. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1379. ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
  1380. if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  1381. int i;
  1382. ret = -ENODEV;
  1383. /* check for at least one working CPU */
  1384. for (i=0; i<NR_CPUS; i++)
  1385. if (cpufreq_cpu_data[i])
  1386. ret = 0;
  1387. /* if all ->init() calls failed, unregister */
  1388. if (ret) {
  1389. dprintk("no CPU initialized for driver %s\n",
  1390. driver_data->name);
  1391. sysdev_driver_unregister(&cpu_sysdev_class,
  1392. &cpufreq_sysdev_driver);
  1393. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1394. cpufreq_driver = NULL;
  1395. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1396. }
  1397. }
  1398. if (!ret) {
  1399. register_hotcpu_notifier(&cpufreq_cpu_notifier);
  1400. dprintk("driver %s up and running\n", driver_data->name);
  1401. cpufreq_debug_enable_ratelimit();
  1402. }
  1403. return (ret);
  1404. }
  1405. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  1406. /**
  1407. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  1408. *
  1409. * Unregister the current CPUFreq driver. Only call this if you have
  1410. * the right to do so, i.e. if you have succeeded in initialising before!
  1411. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  1412. * currently not initialised.
  1413. */
  1414. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  1415. {
  1416. unsigned long flags;
  1417. cpufreq_debug_disable_ratelimit();
  1418. if (!cpufreq_driver || (driver != cpufreq_driver)) {
  1419. cpufreq_debug_enable_ratelimit();
  1420. return -EINVAL;
  1421. }
  1422. dprintk("unregistering driver %s\n", driver->name);
  1423. sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
  1424. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  1425. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1426. cpufreq_driver = NULL;
  1427. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1428. return 0;
  1429. }
  1430. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);