cpufreq.c 48 KB

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