cpufreq.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  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. #include <linux/syscore_ops.h>
  31. #include <trace/events/power.h>
  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 DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
  39. #ifdef CONFIG_HOTPLUG_CPU
  40. /* This one keeps track of the previously set governor of a removed CPU */
  41. static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
  42. #endif
  43. static DEFINE_SPINLOCK(cpufreq_driver_lock);
  44. /*
  45. * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
  46. * all cpufreq/hotplug/workqueue/etc related lock issues.
  47. *
  48. * The rules for this semaphore:
  49. * - Any routine that wants to read from the policy structure will
  50. * do a down_read on this semaphore.
  51. * - Any routine that will write to the policy structure and/or may take away
  52. * the policy altogether (eg. CPU hotplug), will hold this lock in write
  53. * mode before doing so.
  54. *
  55. * Additional rules:
  56. * - All holders of the lock should check to make sure that the CPU they
  57. * are concerned with are online after they get the lock.
  58. * - Governor routines that can be called in cpufreq hotplug path should not
  59. * take this sem as top level hotplug notifier handler takes this.
  60. * - Lock should not be held across
  61. * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  62. */
  63. static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
  64. static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
  65. #define lock_policy_rwsem(mode, cpu) \
  66. static int lock_policy_rwsem_##mode \
  67. (int cpu) \
  68. { \
  69. int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
  70. BUG_ON(policy_cpu == -1); \
  71. down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  72. if (unlikely(!cpu_online(cpu))) { \
  73. up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  74. return -1; \
  75. } \
  76. \
  77. return 0; \
  78. }
  79. lock_policy_rwsem(read, cpu);
  80. lock_policy_rwsem(write, cpu);
  81. static void unlock_policy_rwsem_read(int cpu)
  82. {
  83. int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
  84. BUG_ON(policy_cpu == -1);
  85. up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
  86. }
  87. static void unlock_policy_rwsem_write(int cpu)
  88. {
  89. int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
  90. BUG_ON(policy_cpu == -1);
  91. up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
  92. }
  93. /* internal prototypes */
  94. static int __cpufreq_governor(struct cpufreq_policy *policy,
  95. unsigned int event);
  96. static unsigned int __cpufreq_get(unsigned int cpu);
  97. static void handle_update(struct work_struct *work);
  98. /**
  99. * Two notifier lists: the "policy" list is involved in the
  100. * validation process for a new CPU frequency policy; the
  101. * "transition" list for kernel code that needs to handle
  102. * changes to devices when the CPU clock speed changes.
  103. * The mutex locks both lists.
  104. */
  105. static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
  106. static struct srcu_notifier_head cpufreq_transition_notifier_list;
  107. static bool init_cpufreq_transition_notifier_list_called;
  108. static int __init init_cpufreq_transition_notifier_list(void)
  109. {
  110. srcu_init_notifier_head(&cpufreq_transition_notifier_list);
  111. init_cpufreq_transition_notifier_list_called = true;
  112. return 0;
  113. }
  114. pure_initcall(init_cpufreq_transition_notifier_list);
  115. static LIST_HEAD(cpufreq_governor_list);
  116. static DEFINE_MUTEX(cpufreq_governor_mutex);
  117. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  118. {
  119. struct cpufreq_policy *data;
  120. unsigned long flags;
  121. if (cpu >= nr_cpu_ids)
  122. goto err_out;
  123. /* get the cpufreq driver */
  124. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  125. if (!cpufreq_driver)
  126. goto err_out_unlock;
  127. if (!try_module_get(cpufreq_driver->owner))
  128. goto err_out_unlock;
  129. /* get the CPU */
  130. data = per_cpu(cpufreq_cpu_data, cpu);
  131. if (!data)
  132. goto err_out_put_module;
  133. if (!kobject_get(&data->kobj))
  134. goto err_out_put_module;
  135. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  136. return data;
  137. err_out_put_module:
  138. module_put(cpufreq_driver->owner);
  139. err_out_unlock:
  140. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  141. err_out:
  142. return NULL;
  143. }
  144. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  145. void cpufreq_cpu_put(struct cpufreq_policy *data)
  146. {
  147. kobject_put(&data->kobj);
  148. module_put(cpufreq_driver->owner);
  149. }
  150. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  151. /*********************************************************************
  152. * EXTERNALLY AFFECTING FREQUENCY CHANGES *
  153. *********************************************************************/
  154. /**
  155. * adjust_jiffies - adjust the system "loops_per_jiffy"
  156. *
  157. * This function alters the system "loops_per_jiffy" for the clock
  158. * speed change. Note that loops_per_jiffy cannot be updated on SMP
  159. * systems as each CPU might be scaled differently. So, use the arch
  160. * per-CPU loops_per_jiffy value wherever possible.
  161. */
  162. #ifndef CONFIG_SMP
  163. static unsigned long l_p_j_ref;
  164. static unsigned int l_p_j_ref_freq;
  165. static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  166. {
  167. if (ci->flags & CPUFREQ_CONST_LOOPS)
  168. return;
  169. if (!l_p_j_ref_freq) {
  170. l_p_j_ref = loops_per_jiffy;
  171. l_p_j_ref_freq = ci->old;
  172. pr_debug("saving %lu as reference value for loops_per_jiffy; "
  173. "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
  174. }
  175. if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
  176. (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
  177. loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
  178. ci->new);
  179. pr_debug("scaling loops_per_jiffy to %lu "
  180. "for frequency %u kHz\n", loops_per_jiffy, ci->new);
  181. }
  182. }
  183. #else
  184. static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  185. {
  186. return;
  187. }
  188. #endif
  189. /**
  190. * cpufreq_notify_transition - call notifier chain and adjust_jiffies
  191. * on frequency transition.
  192. *
  193. * This function calls the transition notifiers and the "adjust_jiffies"
  194. * function. It is called twice on all CPU frequency changes that have
  195. * external effects.
  196. */
  197. void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
  198. {
  199. struct cpufreq_policy *policy;
  200. BUG_ON(irqs_disabled());
  201. freqs->flags = cpufreq_driver->flags;
  202. pr_debug("notification %u of frequency transition to %u kHz\n",
  203. state, freqs->new);
  204. policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
  205. switch (state) {
  206. case CPUFREQ_PRECHANGE:
  207. /* detect if the driver reported a value as "old frequency"
  208. * which is not equal to what the cpufreq core thinks is
  209. * "old frequency".
  210. */
  211. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  212. if ((policy) && (policy->cpu == freqs->cpu) &&
  213. (policy->cur) && (policy->cur != freqs->old)) {
  214. pr_debug("Warning: CPU frequency is"
  215. " %u, cpufreq assumed %u kHz.\n",
  216. freqs->old, policy->cur);
  217. freqs->old = policy->cur;
  218. }
  219. }
  220. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  221. CPUFREQ_PRECHANGE, freqs);
  222. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  223. break;
  224. case CPUFREQ_POSTCHANGE:
  225. adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
  226. pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
  227. (unsigned long)freqs->cpu);
  228. trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
  229. trace_cpu_frequency(freqs->new, freqs->cpu);
  230. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  231. CPUFREQ_POSTCHANGE, freqs);
  232. if (likely(policy) && likely(policy->cpu == freqs->cpu))
  233. policy->cur = freqs->new;
  234. break;
  235. }
  236. }
  237. EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
  238. /*********************************************************************
  239. * SYSFS INTERFACE *
  240. *********************************************************************/
  241. static struct cpufreq_governor *__find_governor(const char *str_governor)
  242. {
  243. struct cpufreq_governor *t;
  244. list_for_each_entry(t, &cpufreq_governor_list, governor_list)
  245. if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
  246. return t;
  247. return NULL;
  248. }
  249. /**
  250. * cpufreq_parse_governor - parse a governor string
  251. */
  252. static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
  253. struct cpufreq_governor **governor)
  254. {
  255. int err = -EINVAL;
  256. if (!cpufreq_driver)
  257. goto out;
  258. if (cpufreq_driver->setpolicy) {
  259. if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
  260. *policy = CPUFREQ_POLICY_PERFORMANCE;
  261. err = 0;
  262. } else if (!strnicmp(str_governor, "powersave",
  263. CPUFREQ_NAME_LEN)) {
  264. *policy = CPUFREQ_POLICY_POWERSAVE;
  265. err = 0;
  266. }
  267. } else if (cpufreq_driver->target) {
  268. struct cpufreq_governor *t;
  269. mutex_lock(&cpufreq_governor_mutex);
  270. t = __find_governor(str_governor);
  271. if (t == NULL) {
  272. int ret;
  273. mutex_unlock(&cpufreq_governor_mutex);
  274. ret = request_module("cpufreq_%s", str_governor);
  275. mutex_lock(&cpufreq_governor_mutex);
  276. if (ret == 0)
  277. t = __find_governor(str_governor);
  278. }
  279. if (t != NULL) {
  280. *governor = t;
  281. err = 0;
  282. }
  283. mutex_unlock(&cpufreq_governor_mutex);
  284. }
  285. out:
  286. return err;
  287. }
  288. /**
  289. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  290. * print out cpufreq information
  291. *
  292. * Write out information from cpufreq_driver->policy[cpu]; object must be
  293. * "unsigned int".
  294. */
  295. #define show_one(file_name, object) \
  296. static ssize_t show_##file_name \
  297. (struct cpufreq_policy *policy, char *buf) \
  298. { \
  299. return sprintf(buf, "%u\n", policy->object); \
  300. }
  301. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  302. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  303. show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
  304. show_one(scaling_min_freq, min);
  305. show_one(scaling_max_freq, max);
  306. show_one(scaling_cur_freq, cur);
  307. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  308. struct cpufreq_policy *policy);
  309. /**
  310. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  311. */
  312. #define store_one(file_name, object) \
  313. static ssize_t store_##file_name \
  314. (struct cpufreq_policy *policy, const char *buf, size_t count) \
  315. { \
  316. unsigned int ret = -EINVAL; \
  317. struct cpufreq_policy new_policy; \
  318. \
  319. ret = cpufreq_get_policy(&new_policy, policy->cpu); \
  320. if (ret) \
  321. return -EINVAL; \
  322. \
  323. ret = sscanf(buf, "%u", &new_policy.object); \
  324. if (ret != 1) \
  325. return -EINVAL; \
  326. \
  327. ret = __cpufreq_set_policy(policy, &new_policy); \
  328. policy->user_policy.object = policy->object; \
  329. \
  330. return ret ? ret : count; \
  331. }
  332. store_one(scaling_min_freq, min);
  333. store_one(scaling_max_freq, max);
  334. /**
  335. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  336. */
  337. static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
  338. char *buf)
  339. {
  340. unsigned int cur_freq = __cpufreq_get(policy->cpu);
  341. if (!cur_freq)
  342. return sprintf(buf, "<unknown>");
  343. return sprintf(buf, "%u\n", cur_freq);
  344. }
  345. /**
  346. * show_scaling_governor - show the current policy for the specified CPU
  347. */
  348. static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
  349. {
  350. if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
  351. return sprintf(buf, "powersave\n");
  352. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  353. return sprintf(buf, "performance\n");
  354. else if (policy->governor)
  355. return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
  356. policy->governor->name);
  357. return -EINVAL;
  358. }
  359. /**
  360. * store_scaling_governor - store policy for the specified CPU
  361. */
  362. static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
  363. const char *buf, size_t count)
  364. {
  365. unsigned int ret = -EINVAL;
  366. char str_governor[16];
  367. struct cpufreq_policy new_policy;
  368. ret = cpufreq_get_policy(&new_policy, policy->cpu);
  369. if (ret)
  370. return ret;
  371. ret = sscanf(buf, "%15s", str_governor);
  372. if (ret != 1)
  373. return -EINVAL;
  374. if (cpufreq_parse_governor(str_governor, &new_policy.policy,
  375. &new_policy.governor))
  376. return -EINVAL;
  377. /* Do not use cpufreq_set_policy here or the user_policy.max
  378. will be wrongly overridden */
  379. ret = __cpufreq_set_policy(policy, &new_policy);
  380. policy->user_policy.policy = policy->policy;
  381. policy->user_policy.governor = policy->governor;
  382. if (ret)
  383. return ret;
  384. else
  385. return count;
  386. }
  387. /**
  388. * show_scaling_driver - show the cpufreq driver currently loaded
  389. */
  390. static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
  391. {
  392. return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
  393. }
  394. /**
  395. * show_scaling_available_governors - show the available CPUfreq governors
  396. */
  397. static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
  398. char *buf)
  399. {
  400. ssize_t i = 0;
  401. struct cpufreq_governor *t;
  402. if (!cpufreq_driver->target) {
  403. i += sprintf(buf, "performance powersave");
  404. goto out;
  405. }
  406. list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
  407. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
  408. - (CPUFREQ_NAME_LEN + 2)))
  409. goto out;
  410. i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
  411. }
  412. out:
  413. i += sprintf(&buf[i], "\n");
  414. return i;
  415. }
  416. static ssize_t show_cpus(const struct cpumask *mask, char *buf)
  417. {
  418. ssize_t i = 0;
  419. unsigned int cpu;
  420. for_each_cpu(cpu, mask) {
  421. if (i)
  422. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
  423. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
  424. if (i >= (PAGE_SIZE - 5))
  425. break;
  426. }
  427. i += sprintf(&buf[i], "\n");
  428. return i;
  429. }
  430. /**
  431. * show_related_cpus - show the CPUs affected by each transition even if
  432. * hw coordination is in use
  433. */
  434. static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
  435. {
  436. if (cpumask_empty(policy->related_cpus))
  437. return show_cpus(policy->cpus, buf);
  438. return show_cpus(policy->related_cpus, buf);
  439. }
  440. /**
  441. * show_affected_cpus - show the CPUs affected by each transition
  442. */
  443. static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
  444. {
  445. return show_cpus(policy->cpus, buf);
  446. }
  447. static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
  448. const char *buf, size_t count)
  449. {
  450. unsigned int freq = 0;
  451. unsigned int ret;
  452. if (!policy->governor || !policy->governor->store_setspeed)
  453. return -EINVAL;
  454. ret = sscanf(buf, "%u", &freq);
  455. if (ret != 1)
  456. return -EINVAL;
  457. policy->governor->store_setspeed(policy, freq);
  458. return count;
  459. }
  460. static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
  461. {
  462. if (!policy->governor || !policy->governor->show_setspeed)
  463. return sprintf(buf, "<unsupported>\n");
  464. return policy->governor->show_setspeed(policy, buf);
  465. }
  466. /**
  467. * show_scaling_driver - show the current cpufreq HW/BIOS limitation
  468. */
  469. static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
  470. {
  471. unsigned int limit;
  472. int ret;
  473. if (cpufreq_driver->bios_limit) {
  474. ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
  475. if (!ret)
  476. return sprintf(buf, "%u\n", limit);
  477. }
  478. return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
  479. }
  480. cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
  481. cpufreq_freq_attr_ro(cpuinfo_min_freq);
  482. cpufreq_freq_attr_ro(cpuinfo_max_freq);
  483. cpufreq_freq_attr_ro(cpuinfo_transition_latency);
  484. cpufreq_freq_attr_ro(scaling_available_governors);
  485. cpufreq_freq_attr_ro(scaling_driver);
  486. cpufreq_freq_attr_ro(scaling_cur_freq);
  487. cpufreq_freq_attr_ro(bios_limit);
  488. cpufreq_freq_attr_ro(related_cpus);
  489. cpufreq_freq_attr_ro(affected_cpus);
  490. cpufreq_freq_attr_rw(scaling_min_freq);
  491. cpufreq_freq_attr_rw(scaling_max_freq);
  492. cpufreq_freq_attr_rw(scaling_governor);
  493. cpufreq_freq_attr_rw(scaling_setspeed);
  494. static struct attribute *default_attrs[] = {
  495. &cpuinfo_min_freq.attr,
  496. &cpuinfo_max_freq.attr,
  497. &cpuinfo_transition_latency.attr,
  498. &scaling_min_freq.attr,
  499. &scaling_max_freq.attr,
  500. &affected_cpus.attr,
  501. &related_cpus.attr,
  502. &scaling_governor.attr,
  503. &scaling_driver.attr,
  504. &scaling_available_governors.attr,
  505. &scaling_setspeed.attr,
  506. NULL
  507. };
  508. struct kobject *cpufreq_global_kobject;
  509. EXPORT_SYMBOL(cpufreq_global_kobject);
  510. #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
  511. #define to_attr(a) container_of(a, struct freq_attr, attr)
  512. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  513. {
  514. struct cpufreq_policy *policy = to_policy(kobj);
  515. struct freq_attr *fattr = to_attr(attr);
  516. ssize_t ret = -EINVAL;
  517. policy = cpufreq_cpu_get(policy->cpu);
  518. if (!policy)
  519. goto no_policy;
  520. if (lock_policy_rwsem_read(policy->cpu) < 0)
  521. goto fail;
  522. if (fattr->show)
  523. ret = fattr->show(policy, buf);
  524. else
  525. ret = -EIO;
  526. unlock_policy_rwsem_read(policy->cpu);
  527. fail:
  528. cpufreq_cpu_put(policy);
  529. no_policy:
  530. return ret;
  531. }
  532. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  533. const char *buf, size_t count)
  534. {
  535. struct cpufreq_policy *policy = to_policy(kobj);
  536. struct freq_attr *fattr = to_attr(attr);
  537. ssize_t ret = -EINVAL;
  538. policy = cpufreq_cpu_get(policy->cpu);
  539. if (!policy)
  540. goto no_policy;
  541. if (lock_policy_rwsem_write(policy->cpu) < 0)
  542. goto fail;
  543. if (fattr->store)
  544. ret = fattr->store(policy, buf, count);
  545. else
  546. ret = -EIO;
  547. unlock_policy_rwsem_write(policy->cpu);
  548. fail:
  549. cpufreq_cpu_put(policy);
  550. no_policy:
  551. return ret;
  552. }
  553. static void cpufreq_sysfs_release(struct kobject *kobj)
  554. {
  555. struct cpufreq_policy *policy = to_policy(kobj);
  556. pr_debug("last reference is dropped\n");
  557. complete(&policy->kobj_unregister);
  558. }
  559. static const struct sysfs_ops sysfs_ops = {
  560. .show = show,
  561. .store = store,
  562. };
  563. static struct kobj_type ktype_cpufreq = {
  564. .sysfs_ops = &sysfs_ops,
  565. .default_attrs = default_attrs,
  566. .release = cpufreq_sysfs_release,
  567. };
  568. /*
  569. * Returns:
  570. * Negative: Failure
  571. * 0: Success
  572. * Positive: When we have a managed CPU and the sysfs got symlinked
  573. */
  574. static int cpufreq_add_dev_policy(unsigned int cpu,
  575. struct cpufreq_policy *policy,
  576. struct device *dev)
  577. {
  578. int ret = 0;
  579. #ifdef CONFIG_SMP
  580. unsigned long flags;
  581. unsigned int j;
  582. #ifdef CONFIG_HOTPLUG_CPU
  583. struct cpufreq_governor *gov;
  584. gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
  585. if (gov) {
  586. policy->governor = gov;
  587. pr_debug("Restoring governor %s for cpu %d\n",
  588. policy->governor->name, cpu);
  589. }
  590. #endif
  591. for_each_cpu(j, policy->cpus) {
  592. struct cpufreq_policy *managed_policy;
  593. if (cpu == j)
  594. continue;
  595. /* Check for existing affected CPUs.
  596. * They may not be aware of it due to CPU Hotplug.
  597. * cpufreq_cpu_put is called when the device is removed
  598. * in __cpufreq_remove_dev()
  599. */
  600. managed_policy = cpufreq_cpu_get(j);
  601. if (unlikely(managed_policy)) {
  602. /* Set proper policy_cpu */
  603. unlock_policy_rwsem_write(cpu);
  604. per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
  605. if (lock_policy_rwsem_write(cpu) < 0) {
  606. /* Should not go through policy unlock path */
  607. if (cpufreq_driver->exit)
  608. cpufreq_driver->exit(policy);
  609. cpufreq_cpu_put(managed_policy);
  610. return -EBUSY;
  611. }
  612. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  613. cpumask_copy(managed_policy->cpus, policy->cpus);
  614. per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
  615. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  616. pr_debug("CPU already managed, adding link\n");
  617. ret = sysfs_create_link(&dev->kobj,
  618. &managed_policy->kobj,
  619. "cpufreq");
  620. if (ret)
  621. cpufreq_cpu_put(managed_policy);
  622. /*
  623. * Success. We only needed to be added to the mask.
  624. * Call driver->exit() because only the cpu parent of
  625. * the kobj needed to call init().
  626. */
  627. if (cpufreq_driver->exit)
  628. cpufreq_driver->exit(policy);
  629. if (!ret)
  630. return 1;
  631. else
  632. return ret;
  633. }
  634. }
  635. #endif
  636. return ret;
  637. }
  638. /* symlink affected CPUs */
  639. static int cpufreq_add_dev_symlink(unsigned int cpu,
  640. struct cpufreq_policy *policy)
  641. {
  642. unsigned int j;
  643. int ret = 0;
  644. for_each_cpu(j, policy->cpus) {
  645. struct cpufreq_policy *managed_policy;
  646. struct device *cpu_dev;
  647. if (j == cpu)
  648. continue;
  649. if (!cpu_online(j))
  650. continue;
  651. pr_debug("CPU %u already managed, adding link\n", j);
  652. managed_policy = cpufreq_cpu_get(cpu);
  653. cpu_dev = get_cpu_device(j);
  654. ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
  655. "cpufreq");
  656. if (ret) {
  657. cpufreq_cpu_put(managed_policy);
  658. return ret;
  659. }
  660. }
  661. return ret;
  662. }
  663. static int cpufreq_add_dev_interface(unsigned int cpu,
  664. struct cpufreq_policy *policy,
  665. struct device *dev)
  666. {
  667. struct cpufreq_policy new_policy;
  668. struct freq_attr **drv_attr;
  669. unsigned long flags;
  670. int ret = 0;
  671. unsigned int j;
  672. /* prepare interface data */
  673. ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
  674. &dev->kobj, "cpufreq");
  675. if (ret)
  676. return ret;
  677. /* set up files for this cpu device */
  678. drv_attr = cpufreq_driver->attr;
  679. while ((drv_attr) && (*drv_attr)) {
  680. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  681. if (ret)
  682. goto err_out_kobj_put;
  683. drv_attr++;
  684. }
  685. if (cpufreq_driver->get) {
  686. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  687. if (ret)
  688. goto err_out_kobj_put;
  689. }
  690. if (cpufreq_driver->target) {
  691. ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  692. if (ret)
  693. goto err_out_kobj_put;
  694. }
  695. if (cpufreq_driver->bios_limit) {
  696. ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
  697. if (ret)
  698. goto err_out_kobj_put;
  699. }
  700. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  701. for_each_cpu(j, policy->cpus) {
  702. if (!cpu_online(j))
  703. continue;
  704. per_cpu(cpufreq_cpu_data, j) = policy;
  705. per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
  706. }
  707. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  708. ret = cpufreq_add_dev_symlink(cpu, policy);
  709. if (ret)
  710. goto err_out_kobj_put;
  711. memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
  712. /* assure that the starting sequence is run in __cpufreq_set_policy */
  713. policy->governor = NULL;
  714. /* set default policy */
  715. ret = __cpufreq_set_policy(policy, &new_policy);
  716. policy->user_policy.policy = policy->policy;
  717. policy->user_policy.governor = policy->governor;
  718. if (ret) {
  719. pr_debug("setting policy failed\n");
  720. if (cpufreq_driver->exit)
  721. cpufreq_driver->exit(policy);
  722. }
  723. return ret;
  724. err_out_kobj_put:
  725. kobject_put(&policy->kobj);
  726. wait_for_completion(&policy->kobj_unregister);
  727. return ret;
  728. }
  729. /**
  730. * cpufreq_add_dev - add a CPU device
  731. *
  732. * Adds the cpufreq interface for a CPU device.
  733. *
  734. * The Oracle says: try running cpufreq registration/unregistration concurrently
  735. * with with cpu hotplugging and all hell will break loose. Tried to clean this
  736. * mess up, but more thorough testing is needed. - Mathieu
  737. */
  738. static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
  739. {
  740. unsigned int cpu = dev->id;
  741. int ret = 0, found = 0;
  742. struct cpufreq_policy *policy;
  743. unsigned long flags;
  744. unsigned int j;
  745. #ifdef CONFIG_HOTPLUG_CPU
  746. int sibling;
  747. #endif
  748. if (cpu_is_offline(cpu))
  749. return 0;
  750. pr_debug("adding CPU %u\n", cpu);
  751. #ifdef CONFIG_SMP
  752. /* check whether a different CPU already registered this
  753. * CPU because it is in the same boat. */
  754. policy = cpufreq_cpu_get(cpu);
  755. if (unlikely(policy)) {
  756. cpufreq_cpu_put(policy);
  757. return 0;
  758. }
  759. #endif
  760. if (!try_module_get(cpufreq_driver->owner)) {
  761. ret = -EINVAL;
  762. goto module_out;
  763. }
  764. ret = -ENOMEM;
  765. policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
  766. if (!policy)
  767. goto nomem_out;
  768. if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
  769. goto err_free_policy;
  770. if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
  771. goto err_free_cpumask;
  772. policy->cpu = cpu;
  773. cpumask_copy(policy->cpus, cpumask_of(cpu));
  774. /* Initially set CPU itself as the policy_cpu */
  775. per_cpu(cpufreq_policy_cpu, cpu) = cpu;
  776. ret = (lock_policy_rwsem_write(cpu) < 0);
  777. WARN_ON(ret);
  778. init_completion(&policy->kobj_unregister);
  779. INIT_WORK(&policy->update, handle_update);
  780. /* Set governor before ->init, so that driver could check it */
  781. #ifdef CONFIG_HOTPLUG_CPU
  782. for_each_online_cpu(sibling) {
  783. struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
  784. if (cp && cp->governor &&
  785. (cpumask_test_cpu(cpu, cp->related_cpus))) {
  786. policy->governor = cp->governor;
  787. found = 1;
  788. break;
  789. }
  790. }
  791. #endif
  792. if (!found)
  793. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  794. /* call driver. From then on the cpufreq must be able
  795. * to accept all calls to ->verify and ->setpolicy for this CPU
  796. */
  797. ret = cpufreq_driver->init(policy);
  798. if (ret) {
  799. pr_debug("initialization failed\n");
  800. goto err_unlock_policy;
  801. }
  802. policy->user_policy.min = policy->min;
  803. policy->user_policy.max = policy->max;
  804. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  805. CPUFREQ_START, policy);
  806. ret = cpufreq_add_dev_policy(cpu, policy, dev);
  807. if (ret) {
  808. if (ret > 0)
  809. /* This is a managed cpu, symlink created,
  810. exit with 0 */
  811. ret = 0;
  812. goto err_unlock_policy;
  813. }
  814. ret = cpufreq_add_dev_interface(cpu, policy, dev);
  815. if (ret)
  816. goto err_out_unregister;
  817. unlock_policy_rwsem_write(cpu);
  818. kobject_uevent(&policy->kobj, KOBJ_ADD);
  819. module_put(cpufreq_driver->owner);
  820. pr_debug("initialization complete\n");
  821. return 0;
  822. err_out_unregister:
  823. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  824. for_each_cpu(j, policy->cpus)
  825. per_cpu(cpufreq_cpu_data, j) = NULL;
  826. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  827. kobject_put(&policy->kobj);
  828. wait_for_completion(&policy->kobj_unregister);
  829. err_unlock_policy:
  830. unlock_policy_rwsem_write(cpu);
  831. free_cpumask_var(policy->related_cpus);
  832. err_free_cpumask:
  833. free_cpumask_var(policy->cpus);
  834. err_free_policy:
  835. kfree(policy);
  836. nomem_out:
  837. module_put(cpufreq_driver->owner);
  838. module_out:
  839. return ret;
  840. }
  841. /**
  842. * __cpufreq_remove_dev - remove a CPU device
  843. *
  844. * Removes the cpufreq interface for a CPU device.
  845. * Caller should already have policy_rwsem in write mode for this CPU.
  846. * This routine frees the rwsem before returning.
  847. */
  848. static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  849. {
  850. unsigned int cpu = dev->id;
  851. unsigned long flags;
  852. struct cpufreq_policy *data;
  853. struct kobject *kobj;
  854. struct completion *cmp;
  855. #ifdef CONFIG_SMP
  856. struct device *cpu_dev;
  857. unsigned int j;
  858. #endif
  859. pr_debug("unregistering CPU %u\n", cpu);
  860. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  861. data = per_cpu(cpufreq_cpu_data, cpu);
  862. if (!data) {
  863. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  864. unlock_policy_rwsem_write(cpu);
  865. return -EINVAL;
  866. }
  867. per_cpu(cpufreq_cpu_data, cpu) = NULL;
  868. #ifdef CONFIG_SMP
  869. /* if this isn't the CPU which is the parent of the kobj, we
  870. * only need to unlink, put and exit
  871. */
  872. if (unlikely(cpu != data->cpu)) {
  873. pr_debug("removing link\n");
  874. cpumask_clear_cpu(cpu, data->cpus);
  875. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  876. kobj = &dev->kobj;
  877. cpufreq_cpu_put(data);
  878. unlock_policy_rwsem_write(cpu);
  879. sysfs_remove_link(kobj, "cpufreq");
  880. return 0;
  881. }
  882. #endif
  883. #ifdef CONFIG_SMP
  884. #ifdef CONFIG_HOTPLUG_CPU
  885. strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
  886. CPUFREQ_NAME_LEN);
  887. #endif
  888. /* if we have other CPUs still registered, we need to unlink them,
  889. * or else wait_for_completion below will lock up. Clean the
  890. * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
  891. * the sysfs links afterwards.
  892. */
  893. if (unlikely(cpumask_weight(data->cpus) > 1)) {
  894. for_each_cpu(j, data->cpus) {
  895. if (j == cpu)
  896. continue;
  897. per_cpu(cpufreq_cpu_data, j) = NULL;
  898. }
  899. }
  900. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  901. if (unlikely(cpumask_weight(data->cpus) > 1)) {
  902. for_each_cpu(j, data->cpus) {
  903. if (j == cpu)
  904. continue;
  905. pr_debug("removing link for cpu %u\n", j);
  906. #ifdef CONFIG_HOTPLUG_CPU
  907. strncpy(per_cpu(cpufreq_cpu_governor, j),
  908. data->governor->name, CPUFREQ_NAME_LEN);
  909. #endif
  910. cpu_dev = get_cpu_device(j);
  911. kobj = &cpu_dev->kobj;
  912. unlock_policy_rwsem_write(cpu);
  913. sysfs_remove_link(kobj, "cpufreq");
  914. lock_policy_rwsem_write(cpu);
  915. cpufreq_cpu_put(data);
  916. }
  917. }
  918. #else
  919. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  920. #endif
  921. if (cpufreq_driver->target)
  922. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  923. kobj = &data->kobj;
  924. cmp = &data->kobj_unregister;
  925. unlock_policy_rwsem_write(cpu);
  926. kobject_put(kobj);
  927. /* we need to make sure that the underlying kobj is actually
  928. * not referenced anymore by anybody before we proceed with
  929. * unloading.
  930. */
  931. pr_debug("waiting for dropping of refcount\n");
  932. wait_for_completion(cmp);
  933. pr_debug("wait complete\n");
  934. lock_policy_rwsem_write(cpu);
  935. if (cpufreq_driver->exit)
  936. cpufreq_driver->exit(data);
  937. unlock_policy_rwsem_write(cpu);
  938. #ifdef CONFIG_HOTPLUG_CPU
  939. /* when the CPU which is the parent of the kobj is hotplugged
  940. * offline, check for siblings, and create cpufreq sysfs interface
  941. * and symlinks
  942. */
  943. if (unlikely(cpumask_weight(data->cpus) > 1)) {
  944. /* first sibling now owns the new sysfs dir */
  945. cpumask_clear_cpu(cpu, data->cpus);
  946. cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
  947. /* finally remove our own symlink */
  948. lock_policy_rwsem_write(cpu);
  949. __cpufreq_remove_dev(dev, sif);
  950. }
  951. #endif
  952. free_cpumask_var(data->related_cpus);
  953. free_cpumask_var(data->cpus);
  954. kfree(data);
  955. return 0;
  956. }
  957. static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  958. {
  959. unsigned int cpu = dev->id;
  960. int retval;
  961. if (cpu_is_offline(cpu))
  962. return 0;
  963. if (unlikely(lock_policy_rwsem_write(cpu)))
  964. BUG();
  965. retval = __cpufreq_remove_dev(dev, sif);
  966. return retval;
  967. }
  968. static void handle_update(struct work_struct *work)
  969. {
  970. struct cpufreq_policy *policy =
  971. container_of(work, struct cpufreq_policy, update);
  972. unsigned int cpu = policy->cpu;
  973. pr_debug("handle_update for cpu %u called\n", cpu);
  974. cpufreq_update_policy(cpu);
  975. }
  976. /**
  977. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
  978. * @cpu: cpu number
  979. * @old_freq: CPU frequency the kernel thinks the CPU runs at
  980. * @new_freq: CPU frequency the CPU actually runs at
  981. *
  982. * We adjust to current frequency first, and need to clean up later.
  983. * So either call to cpufreq_update_policy() or schedule handle_update()).
  984. */
  985. static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
  986. unsigned int new_freq)
  987. {
  988. struct cpufreq_freqs freqs;
  989. pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
  990. "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
  991. freqs.cpu = cpu;
  992. freqs.old = old_freq;
  993. freqs.new = new_freq;
  994. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  995. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  996. }
  997. /**
  998. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  999. * @cpu: CPU number
  1000. *
  1001. * This is the last known freq, without actually getting it from the driver.
  1002. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  1003. */
  1004. unsigned int cpufreq_quick_get(unsigned int cpu)
  1005. {
  1006. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1007. unsigned int ret_freq = 0;
  1008. if (policy) {
  1009. ret_freq = policy->cur;
  1010. cpufreq_cpu_put(policy);
  1011. }
  1012. return ret_freq;
  1013. }
  1014. EXPORT_SYMBOL(cpufreq_quick_get);
  1015. /**
  1016. * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
  1017. * @cpu: CPU number
  1018. *
  1019. * Just return the max possible frequency for a given CPU.
  1020. */
  1021. unsigned int cpufreq_quick_get_max(unsigned int cpu)
  1022. {
  1023. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1024. unsigned int ret_freq = 0;
  1025. if (policy) {
  1026. ret_freq = policy->max;
  1027. cpufreq_cpu_put(policy);
  1028. }
  1029. return ret_freq;
  1030. }
  1031. EXPORT_SYMBOL(cpufreq_quick_get_max);
  1032. static unsigned int __cpufreq_get(unsigned int cpu)
  1033. {
  1034. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1035. unsigned int ret_freq = 0;
  1036. if (!cpufreq_driver->get)
  1037. return ret_freq;
  1038. ret_freq = cpufreq_driver->get(cpu);
  1039. if (ret_freq && policy->cur &&
  1040. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1041. /* verify no discrepancy between actual and
  1042. saved value exists */
  1043. if (unlikely(ret_freq != policy->cur)) {
  1044. cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
  1045. schedule_work(&policy->update);
  1046. }
  1047. }
  1048. return ret_freq;
  1049. }
  1050. /**
  1051. * cpufreq_get - get the current CPU frequency (in kHz)
  1052. * @cpu: CPU number
  1053. *
  1054. * Get the CPU current (static) CPU frequency
  1055. */
  1056. unsigned int cpufreq_get(unsigned int cpu)
  1057. {
  1058. unsigned int ret_freq = 0;
  1059. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1060. if (!policy)
  1061. goto out;
  1062. if (unlikely(lock_policy_rwsem_read(cpu)))
  1063. goto out_policy;
  1064. ret_freq = __cpufreq_get(cpu);
  1065. unlock_policy_rwsem_read(cpu);
  1066. out_policy:
  1067. cpufreq_cpu_put(policy);
  1068. out:
  1069. return ret_freq;
  1070. }
  1071. EXPORT_SYMBOL(cpufreq_get);
  1072. static struct subsys_interface cpufreq_interface = {
  1073. .name = "cpufreq",
  1074. .subsys = &cpu_subsys,
  1075. .add_dev = cpufreq_add_dev,
  1076. .remove_dev = cpufreq_remove_dev,
  1077. };
  1078. /**
  1079. * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
  1080. *
  1081. * This function is only executed for the boot processor. The other CPUs
  1082. * have been put offline by means of CPU hotplug.
  1083. */
  1084. static int cpufreq_bp_suspend(void)
  1085. {
  1086. int ret = 0;
  1087. int cpu = smp_processor_id();
  1088. struct cpufreq_policy *cpu_policy;
  1089. pr_debug("suspending cpu %u\n", cpu);
  1090. /* If there's no policy for the boot CPU, we have nothing to do. */
  1091. cpu_policy = cpufreq_cpu_get(cpu);
  1092. if (!cpu_policy)
  1093. return 0;
  1094. if (cpufreq_driver->suspend) {
  1095. ret = cpufreq_driver->suspend(cpu_policy);
  1096. if (ret)
  1097. printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
  1098. "step on CPU %u\n", cpu_policy->cpu);
  1099. }
  1100. cpufreq_cpu_put(cpu_policy);
  1101. return ret;
  1102. }
  1103. /**
  1104. * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
  1105. *
  1106. * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
  1107. * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
  1108. * restored. It will verify that the current freq is in sync with
  1109. * what we believe it to be. This is a bit later than when it
  1110. * should be, but nonethteless it's better than calling
  1111. * cpufreq_driver->get() here which might re-enable interrupts...
  1112. *
  1113. * This function is only executed for the boot CPU. The other CPUs have not
  1114. * been turned on yet.
  1115. */
  1116. static void cpufreq_bp_resume(void)
  1117. {
  1118. int ret = 0;
  1119. int cpu = smp_processor_id();
  1120. struct cpufreq_policy *cpu_policy;
  1121. pr_debug("resuming cpu %u\n", cpu);
  1122. /* If there's no policy for the boot CPU, we have nothing to do. */
  1123. cpu_policy = cpufreq_cpu_get(cpu);
  1124. if (!cpu_policy)
  1125. return;
  1126. if (cpufreq_driver->resume) {
  1127. ret = cpufreq_driver->resume(cpu_policy);
  1128. if (ret) {
  1129. printk(KERN_ERR "cpufreq: resume failed in ->resume "
  1130. "step on CPU %u\n", cpu_policy->cpu);
  1131. goto fail;
  1132. }
  1133. }
  1134. schedule_work(&cpu_policy->update);
  1135. fail:
  1136. cpufreq_cpu_put(cpu_policy);
  1137. }
  1138. static struct syscore_ops cpufreq_syscore_ops = {
  1139. .suspend = cpufreq_bp_suspend,
  1140. .resume = cpufreq_bp_resume,
  1141. };
  1142. /*********************************************************************
  1143. * NOTIFIER LISTS INTERFACE *
  1144. *********************************************************************/
  1145. /**
  1146. * cpufreq_register_notifier - register a driver with cpufreq
  1147. * @nb: notifier function to register
  1148. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1149. *
  1150. * Add a driver to one of two lists: either a list of drivers that
  1151. * are notified about clock rate changes (once before and once after
  1152. * the transition), or a list of drivers that are notified about
  1153. * changes in cpufreq policy.
  1154. *
  1155. * This function may sleep, and has the same return conditions as
  1156. * blocking_notifier_chain_register.
  1157. */
  1158. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1159. {
  1160. int ret;
  1161. WARN_ON(!init_cpufreq_transition_notifier_list_called);
  1162. switch (list) {
  1163. case CPUFREQ_TRANSITION_NOTIFIER:
  1164. ret = srcu_notifier_chain_register(
  1165. &cpufreq_transition_notifier_list, nb);
  1166. break;
  1167. case CPUFREQ_POLICY_NOTIFIER:
  1168. ret = blocking_notifier_chain_register(
  1169. &cpufreq_policy_notifier_list, nb);
  1170. break;
  1171. default:
  1172. ret = -EINVAL;
  1173. }
  1174. return ret;
  1175. }
  1176. EXPORT_SYMBOL(cpufreq_register_notifier);
  1177. /**
  1178. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1179. * @nb: notifier block to be unregistered
  1180. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1181. *
  1182. * Remove a driver from the CPU frequency notifier list.
  1183. *
  1184. * This function may sleep, and has the same return conditions as
  1185. * blocking_notifier_chain_unregister.
  1186. */
  1187. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1188. {
  1189. int ret;
  1190. switch (list) {
  1191. case CPUFREQ_TRANSITION_NOTIFIER:
  1192. ret = srcu_notifier_chain_unregister(
  1193. &cpufreq_transition_notifier_list, nb);
  1194. break;
  1195. case CPUFREQ_POLICY_NOTIFIER:
  1196. ret = blocking_notifier_chain_unregister(
  1197. &cpufreq_policy_notifier_list, nb);
  1198. break;
  1199. default:
  1200. ret = -EINVAL;
  1201. }
  1202. return ret;
  1203. }
  1204. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1205. /*********************************************************************
  1206. * GOVERNORS *
  1207. *********************************************************************/
  1208. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1209. unsigned int target_freq,
  1210. unsigned int relation)
  1211. {
  1212. int retval = -EINVAL;
  1213. pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
  1214. target_freq, relation);
  1215. if (cpu_online(policy->cpu) && cpufreq_driver->target)
  1216. retval = cpufreq_driver->target(policy, target_freq, relation);
  1217. return retval;
  1218. }
  1219. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1220. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1221. unsigned int target_freq,
  1222. unsigned int relation)
  1223. {
  1224. int ret = -EINVAL;
  1225. policy = cpufreq_cpu_get(policy->cpu);
  1226. if (!policy)
  1227. goto no_policy;
  1228. if (unlikely(lock_policy_rwsem_write(policy->cpu)))
  1229. goto fail;
  1230. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1231. unlock_policy_rwsem_write(policy->cpu);
  1232. fail:
  1233. cpufreq_cpu_put(policy);
  1234. no_policy:
  1235. return ret;
  1236. }
  1237. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1238. int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
  1239. {
  1240. int ret = 0;
  1241. policy = cpufreq_cpu_get(policy->cpu);
  1242. if (!policy)
  1243. return -EINVAL;
  1244. if (cpu_online(cpu) && cpufreq_driver->getavg)
  1245. ret = cpufreq_driver->getavg(policy, cpu);
  1246. cpufreq_cpu_put(policy);
  1247. return ret;
  1248. }
  1249. EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
  1250. /*
  1251. * when "event" is CPUFREQ_GOV_LIMITS
  1252. */
  1253. static int __cpufreq_governor(struct cpufreq_policy *policy,
  1254. unsigned int event)
  1255. {
  1256. int ret;
  1257. /* Only must be defined when default governor is known to have latency
  1258. restrictions, like e.g. conservative or ondemand.
  1259. That this is the case is already ensured in Kconfig
  1260. */
  1261. #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
  1262. struct cpufreq_governor *gov = &cpufreq_gov_performance;
  1263. #else
  1264. struct cpufreq_governor *gov = NULL;
  1265. #endif
  1266. if (policy->governor->max_transition_latency &&
  1267. policy->cpuinfo.transition_latency >
  1268. policy->governor->max_transition_latency) {
  1269. if (!gov)
  1270. return -EINVAL;
  1271. else {
  1272. printk(KERN_WARNING "%s governor failed, too long"
  1273. " transition latency of HW, fallback"
  1274. " to %s governor\n",
  1275. policy->governor->name,
  1276. gov->name);
  1277. policy->governor = gov;
  1278. }
  1279. }
  1280. if (!try_module_get(policy->governor->owner))
  1281. return -EINVAL;
  1282. pr_debug("__cpufreq_governor for CPU %u, event %u\n",
  1283. policy->cpu, event);
  1284. ret = policy->governor->governor(policy, event);
  1285. /* we keep one module reference alive for
  1286. each CPU governed by this CPU */
  1287. if ((event != CPUFREQ_GOV_START) || ret)
  1288. module_put(policy->governor->owner);
  1289. if ((event == CPUFREQ_GOV_STOP) && !ret)
  1290. module_put(policy->governor->owner);
  1291. return ret;
  1292. }
  1293. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1294. {
  1295. int err;
  1296. if (!governor)
  1297. return -EINVAL;
  1298. mutex_lock(&cpufreq_governor_mutex);
  1299. err = -EBUSY;
  1300. if (__find_governor(governor->name) == NULL) {
  1301. err = 0;
  1302. list_add(&governor->governor_list, &cpufreq_governor_list);
  1303. }
  1304. mutex_unlock(&cpufreq_governor_mutex);
  1305. return err;
  1306. }
  1307. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1308. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1309. {
  1310. #ifdef CONFIG_HOTPLUG_CPU
  1311. int cpu;
  1312. #endif
  1313. if (!governor)
  1314. return;
  1315. #ifdef CONFIG_HOTPLUG_CPU
  1316. for_each_present_cpu(cpu) {
  1317. if (cpu_online(cpu))
  1318. continue;
  1319. if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
  1320. strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
  1321. }
  1322. #endif
  1323. mutex_lock(&cpufreq_governor_mutex);
  1324. list_del(&governor->governor_list);
  1325. mutex_unlock(&cpufreq_governor_mutex);
  1326. return;
  1327. }
  1328. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1329. /*********************************************************************
  1330. * POLICY INTERFACE *
  1331. *********************************************************************/
  1332. /**
  1333. * cpufreq_get_policy - get the current cpufreq_policy
  1334. * @policy: struct cpufreq_policy into which the current cpufreq_policy
  1335. * is written
  1336. *
  1337. * Reads the current cpufreq policy.
  1338. */
  1339. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1340. {
  1341. struct cpufreq_policy *cpu_policy;
  1342. if (!policy)
  1343. return -EINVAL;
  1344. cpu_policy = cpufreq_cpu_get(cpu);
  1345. if (!cpu_policy)
  1346. return -EINVAL;
  1347. memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
  1348. cpufreq_cpu_put(cpu_policy);
  1349. return 0;
  1350. }
  1351. EXPORT_SYMBOL(cpufreq_get_policy);
  1352. /*
  1353. * data : current policy.
  1354. * policy : policy to be set.
  1355. */
  1356. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  1357. struct cpufreq_policy *policy)
  1358. {
  1359. int ret = 0;
  1360. pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
  1361. policy->min, policy->max);
  1362. memcpy(&policy->cpuinfo, &data->cpuinfo,
  1363. sizeof(struct cpufreq_cpuinfo));
  1364. if (policy->min > data->max || policy->max < data->min) {
  1365. ret = -EINVAL;
  1366. goto error_out;
  1367. }
  1368. /* verify the cpu speed can be set within this limit */
  1369. ret = cpufreq_driver->verify(policy);
  1370. if (ret)
  1371. goto error_out;
  1372. /* adjust if necessary - all reasons */
  1373. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1374. CPUFREQ_ADJUST, policy);
  1375. /* adjust if necessary - hardware incompatibility*/
  1376. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1377. CPUFREQ_INCOMPATIBLE, policy);
  1378. /* verify the cpu speed can be set within this limit,
  1379. which might be different to the first one */
  1380. ret = cpufreq_driver->verify(policy);
  1381. if (ret)
  1382. goto error_out;
  1383. /* notification of the new policy */
  1384. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1385. CPUFREQ_NOTIFY, policy);
  1386. data->min = policy->min;
  1387. data->max = policy->max;
  1388. pr_debug("new min and max freqs are %u - %u kHz\n",
  1389. data->min, data->max);
  1390. if (cpufreq_driver->setpolicy) {
  1391. data->policy = policy->policy;
  1392. pr_debug("setting range\n");
  1393. ret = cpufreq_driver->setpolicy(policy);
  1394. } else {
  1395. if (policy->governor != data->governor) {
  1396. /* save old, working values */
  1397. struct cpufreq_governor *old_gov = data->governor;
  1398. pr_debug("governor switch\n");
  1399. /* end old governor */
  1400. if (data->governor)
  1401. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  1402. /* start new governor */
  1403. data->governor = policy->governor;
  1404. if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
  1405. /* new governor failed, so re-start old one */
  1406. pr_debug("starting governor %s failed\n",
  1407. data->governor->name);
  1408. if (old_gov) {
  1409. data->governor = old_gov;
  1410. __cpufreq_governor(data,
  1411. CPUFREQ_GOV_START);
  1412. }
  1413. ret = -EINVAL;
  1414. goto error_out;
  1415. }
  1416. /* might be a policy change, too, so fall through */
  1417. }
  1418. pr_debug("governor: change or update limits\n");
  1419. __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
  1420. }
  1421. error_out:
  1422. return ret;
  1423. }
  1424. /**
  1425. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1426. * @cpu: CPU which shall be re-evaluated
  1427. *
  1428. * Useful for policy notifiers which have different necessities
  1429. * at different times.
  1430. */
  1431. int cpufreq_update_policy(unsigned int cpu)
  1432. {
  1433. struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
  1434. struct cpufreq_policy policy;
  1435. int ret;
  1436. if (!data) {
  1437. ret = -ENODEV;
  1438. goto no_policy;
  1439. }
  1440. if (unlikely(lock_policy_rwsem_write(cpu))) {
  1441. ret = -EINVAL;
  1442. goto fail;
  1443. }
  1444. pr_debug("updating policy for CPU %u\n", cpu);
  1445. memcpy(&policy, data, sizeof(struct cpufreq_policy));
  1446. policy.min = data->user_policy.min;
  1447. policy.max = data->user_policy.max;
  1448. policy.policy = data->user_policy.policy;
  1449. policy.governor = data->user_policy.governor;
  1450. /* BIOS might change freq behind our back
  1451. -> ask driver for current freq and notify governors about a change */
  1452. if (cpufreq_driver->get) {
  1453. policy.cur = cpufreq_driver->get(cpu);
  1454. if (!data->cur) {
  1455. pr_debug("Driver did not initialize current freq");
  1456. data->cur = policy.cur;
  1457. } else {
  1458. if (data->cur != policy.cur)
  1459. cpufreq_out_of_sync(cpu, data->cur,
  1460. policy.cur);
  1461. }
  1462. }
  1463. ret = __cpufreq_set_policy(data, &policy);
  1464. unlock_policy_rwsem_write(cpu);
  1465. fail:
  1466. cpufreq_cpu_put(data);
  1467. no_policy:
  1468. return ret;
  1469. }
  1470. EXPORT_SYMBOL(cpufreq_update_policy);
  1471. static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
  1472. unsigned long action, void *hcpu)
  1473. {
  1474. unsigned int cpu = (unsigned long)hcpu;
  1475. struct device *dev;
  1476. dev = get_cpu_device(cpu);
  1477. if (dev) {
  1478. switch (action) {
  1479. case CPU_ONLINE:
  1480. case CPU_ONLINE_FROZEN:
  1481. cpufreq_add_dev(dev, NULL);
  1482. break;
  1483. case CPU_DOWN_PREPARE:
  1484. case CPU_DOWN_PREPARE_FROZEN:
  1485. if (unlikely(lock_policy_rwsem_write(cpu)))
  1486. BUG();
  1487. __cpufreq_remove_dev(dev, NULL);
  1488. break;
  1489. case CPU_DOWN_FAILED:
  1490. case CPU_DOWN_FAILED_FROZEN:
  1491. cpufreq_add_dev(dev, NULL);
  1492. break;
  1493. }
  1494. }
  1495. return NOTIFY_OK;
  1496. }
  1497. static struct notifier_block __refdata cpufreq_cpu_notifier = {
  1498. .notifier_call = cpufreq_cpu_callback,
  1499. };
  1500. /*********************************************************************
  1501. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1502. *********************************************************************/
  1503. /**
  1504. * cpufreq_register_driver - register a CPU Frequency driver
  1505. * @driver_data: A struct cpufreq_driver containing the values#
  1506. * submitted by the CPU Frequency driver.
  1507. *
  1508. * Registers a CPU Frequency driver to this core code. This code
  1509. * returns zero on success, -EBUSY when another driver got here first
  1510. * (and isn't unregistered in the meantime).
  1511. *
  1512. */
  1513. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  1514. {
  1515. unsigned long flags;
  1516. int ret;
  1517. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1518. ((!driver_data->setpolicy) && (!driver_data->target)))
  1519. return -EINVAL;
  1520. pr_debug("trying to register driver %s\n", driver_data->name);
  1521. if (driver_data->setpolicy)
  1522. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  1523. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1524. if (cpufreq_driver) {
  1525. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1526. return -EBUSY;
  1527. }
  1528. cpufreq_driver = driver_data;
  1529. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1530. ret = subsys_interface_register(&cpufreq_interface);
  1531. if (ret)
  1532. goto err_null_driver;
  1533. if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  1534. int i;
  1535. ret = -ENODEV;
  1536. /* check for at least one working CPU */
  1537. for (i = 0; i < nr_cpu_ids; i++)
  1538. if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
  1539. ret = 0;
  1540. break;
  1541. }
  1542. /* if all ->init() calls failed, unregister */
  1543. if (ret) {
  1544. pr_debug("no CPU initialized for driver %s\n",
  1545. driver_data->name);
  1546. goto err_if_unreg;
  1547. }
  1548. }
  1549. register_hotcpu_notifier(&cpufreq_cpu_notifier);
  1550. pr_debug("driver %s up and running\n", driver_data->name);
  1551. return 0;
  1552. err_if_unreg:
  1553. subsys_interface_unregister(&cpufreq_interface);
  1554. err_null_driver:
  1555. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1556. cpufreq_driver = NULL;
  1557. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1558. return ret;
  1559. }
  1560. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  1561. /**
  1562. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  1563. *
  1564. * Unregister the current CPUFreq driver. Only call this if you have
  1565. * the right to do so, i.e. if you have succeeded in initialising before!
  1566. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  1567. * currently not initialised.
  1568. */
  1569. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  1570. {
  1571. unsigned long flags;
  1572. if (!cpufreq_driver || (driver != cpufreq_driver))
  1573. return -EINVAL;
  1574. pr_debug("unregistering driver %s\n", driver->name);
  1575. subsys_interface_unregister(&cpufreq_interface);
  1576. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  1577. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1578. cpufreq_driver = NULL;
  1579. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1580. return 0;
  1581. }
  1582. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  1583. static int __init cpufreq_core_init(void)
  1584. {
  1585. int cpu;
  1586. for_each_possible_cpu(cpu) {
  1587. per_cpu(cpufreq_policy_cpu, cpu) = -1;
  1588. init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
  1589. }
  1590. cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
  1591. BUG_ON(!cpufreq_global_kobject);
  1592. register_syscore_ops(&cpufreq_syscore_ops);
  1593. return 0;
  1594. }
  1595. core_initcall(cpufreq_core_init);