cpufreq.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268
  1. /*
  2. * linux/drivers/cpufreq/cpufreq.c
  3. *
  4. * Copyright (C) 2001 Russell King
  5. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  6. * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
  7. *
  8. * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
  9. * Added handling for CPU hotplug
  10. * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
  11. * Fix handling for CPU hotplug -- affected CPUs
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/cpu.h>
  19. #include <linux/cpufreq.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/module.h>
  25. #include <linux/mutex.h>
  26. #include <linux/slab.h>
  27. #include <linux/suspend.h>
  28. #include <linux/syscore_ops.h>
  29. #include <linux/tick.h>
  30. #include <trace/events/power.h>
  31. /**
  32. * The "cpufreq driver" - the arch- or hardware-dependent low
  33. * level driver of CPUFreq support, and its spinlock. This lock
  34. * also protects the cpufreq_cpu_data array.
  35. */
  36. static struct cpufreq_driver *cpufreq_driver;
  37. static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
  38. static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
  39. static DEFINE_RWLOCK(cpufreq_driver_lock);
  40. static DEFINE_MUTEX(cpufreq_governor_lock);
  41. static LIST_HEAD(cpufreq_policy_list);
  42. #ifdef CONFIG_HOTPLUG_CPU
  43. /* This one keeps track of the previously set governor of a removed CPU */
  44. static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
  45. #endif
  46. /* Flag to suspend/resume CPUFreq governors */
  47. static bool cpufreq_suspended;
  48. static inline bool has_target(void)
  49. {
  50. return cpufreq_driver->target_index || cpufreq_driver->target;
  51. }
  52. /*
  53. * rwsem to guarantee that cpufreq driver module doesn't unload during critical
  54. * sections
  55. */
  56. static DECLARE_RWSEM(cpufreq_rwsem);
  57. /* internal prototypes */
  58. static int __cpufreq_governor(struct cpufreq_policy *policy,
  59. unsigned int event);
  60. static unsigned int __cpufreq_get(unsigned int cpu);
  61. static void handle_update(struct work_struct *work);
  62. /**
  63. * Two notifier lists: the "policy" list is involved in the
  64. * validation process for a new CPU frequency policy; the
  65. * "transition" list for kernel code that needs to handle
  66. * changes to devices when the CPU clock speed changes.
  67. * The mutex locks both lists.
  68. */
  69. static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
  70. static struct srcu_notifier_head cpufreq_transition_notifier_list;
  71. static bool init_cpufreq_transition_notifier_list_called;
  72. static int __init init_cpufreq_transition_notifier_list(void)
  73. {
  74. srcu_init_notifier_head(&cpufreq_transition_notifier_list);
  75. init_cpufreq_transition_notifier_list_called = true;
  76. return 0;
  77. }
  78. pure_initcall(init_cpufreq_transition_notifier_list);
  79. static int off __read_mostly;
  80. static int cpufreq_disabled(void)
  81. {
  82. return off;
  83. }
  84. void disable_cpufreq(void)
  85. {
  86. off = 1;
  87. }
  88. static LIST_HEAD(cpufreq_governor_list);
  89. static DEFINE_MUTEX(cpufreq_governor_mutex);
  90. bool have_governor_per_policy(void)
  91. {
  92. return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
  93. }
  94. EXPORT_SYMBOL_GPL(have_governor_per_policy);
  95. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
  96. {
  97. if (have_governor_per_policy())
  98. return &policy->kobj;
  99. else
  100. return cpufreq_global_kobject;
  101. }
  102. EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
  103. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  104. {
  105. u64 idle_time;
  106. u64 cur_wall_time;
  107. u64 busy_time;
  108. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  109. busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
  110. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
  111. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
  112. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
  113. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
  114. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
  115. idle_time = cur_wall_time - busy_time;
  116. if (wall)
  117. *wall = cputime_to_usecs(cur_wall_time);
  118. return cputime_to_usecs(idle_time);
  119. }
  120. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
  121. {
  122. u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
  123. if (idle_time == -1ULL)
  124. return get_cpu_idle_time_jiffy(cpu, wall);
  125. else if (!io_busy)
  126. idle_time += get_cpu_iowait_time_us(cpu, wall);
  127. return idle_time;
  128. }
  129. EXPORT_SYMBOL_GPL(get_cpu_idle_time);
  130. /*
  131. * This is a generic cpufreq init() routine which can be used by cpufreq
  132. * drivers of SMP systems. It will do following:
  133. * - validate & show freq table passed
  134. * - set policies transition latency
  135. * - policy->cpus with all possible CPUs
  136. */
  137. int cpufreq_generic_init(struct cpufreq_policy *policy,
  138. struct cpufreq_frequency_table *table,
  139. unsigned int transition_latency)
  140. {
  141. int ret;
  142. ret = cpufreq_table_validate_and_show(policy, table);
  143. if (ret) {
  144. pr_err("%s: invalid frequency table: %d\n", __func__, ret);
  145. return ret;
  146. }
  147. policy->cpuinfo.transition_latency = transition_latency;
  148. /*
  149. * The driver only supports the SMP configuartion where all processors
  150. * share the clock and voltage and clock.
  151. */
  152. cpumask_setall(policy->cpus);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL_GPL(cpufreq_generic_init);
  156. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  157. {
  158. struct cpufreq_policy *policy = NULL;
  159. unsigned long flags;
  160. if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
  161. return NULL;
  162. if (!down_read_trylock(&cpufreq_rwsem))
  163. return NULL;
  164. /* get the cpufreq driver */
  165. read_lock_irqsave(&cpufreq_driver_lock, flags);
  166. if (cpufreq_driver) {
  167. /* get the CPU */
  168. policy = per_cpu(cpufreq_cpu_data, cpu);
  169. if (policy)
  170. kobject_get(&policy->kobj);
  171. }
  172. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  173. if (!policy)
  174. up_read(&cpufreq_rwsem);
  175. return policy;
  176. }
  177. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  178. void cpufreq_cpu_put(struct cpufreq_policy *policy)
  179. {
  180. if (cpufreq_disabled())
  181. return;
  182. kobject_put(&policy->kobj);
  183. up_read(&cpufreq_rwsem);
  184. }
  185. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  186. /*********************************************************************
  187. * EXTERNALLY AFFECTING FREQUENCY CHANGES *
  188. *********************************************************************/
  189. /**
  190. * adjust_jiffies - adjust the system "loops_per_jiffy"
  191. *
  192. * This function alters the system "loops_per_jiffy" for the clock
  193. * speed change. Note that loops_per_jiffy cannot be updated on SMP
  194. * systems as each CPU might be scaled differently. So, use the arch
  195. * per-CPU loops_per_jiffy value wherever possible.
  196. */
  197. #ifndef CONFIG_SMP
  198. static unsigned long l_p_j_ref;
  199. static unsigned int l_p_j_ref_freq;
  200. static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  201. {
  202. if (ci->flags & CPUFREQ_CONST_LOOPS)
  203. return;
  204. if (!l_p_j_ref_freq) {
  205. l_p_j_ref = loops_per_jiffy;
  206. l_p_j_ref_freq = ci->old;
  207. pr_debug("saving %lu as reference value for loops_per_jiffy; "
  208. "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
  209. }
  210. if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
  211. (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
  212. loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
  213. ci->new);
  214. pr_debug("scaling loops_per_jiffy to %lu "
  215. "for frequency %u kHz\n", loops_per_jiffy, ci->new);
  216. }
  217. }
  218. #else
  219. static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  220. {
  221. return;
  222. }
  223. #endif
  224. static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
  225. struct cpufreq_freqs *freqs, unsigned int state)
  226. {
  227. BUG_ON(irqs_disabled());
  228. if (cpufreq_disabled())
  229. return;
  230. freqs->flags = cpufreq_driver->flags;
  231. pr_debug("notification %u of frequency transition to %u kHz\n",
  232. state, freqs->new);
  233. switch (state) {
  234. case CPUFREQ_PRECHANGE:
  235. /* detect if the driver reported a value as "old frequency"
  236. * which is not equal to what the cpufreq core thinks is
  237. * "old frequency".
  238. */
  239. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  240. if ((policy) && (policy->cpu == freqs->cpu) &&
  241. (policy->cur) && (policy->cur != freqs->old)) {
  242. pr_debug("Warning: CPU frequency is"
  243. " %u, cpufreq assumed %u kHz.\n",
  244. freqs->old, policy->cur);
  245. freqs->old = policy->cur;
  246. }
  247. }
  248. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  249. CPUFREQ_PRECHANGE, freqs);
  250. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  251. break;
  252. case CPUFREQ_POSTCHANGE:
  253. adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
  254. pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
  255. (unsigned long)freqs->cpu);
  256. trace_cpu_frequency(freqs->new, freqs->cpu);
  257. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  258. CPUFREQ_POSTCHANGE, freqs);
  259. if (likely(policy) && likely(policy->cpu == freqs->cpu))
  260. policy->cur = freqs->new;
  261. break;
  262. }
  263. }
  264. /**
  265. * cpufreq_notify_transition - call notifier chain and adjust_jiffies
  266. * on frequency transition.
  267. *
  268. * This function calls the transition notifiers and the "adjust_jiffies"
  269. * function. It is called twice on all CPU frequency changes that have
  270. * external effects.
  271. */
  272. void cpufreq_notify_transition(struct cpufreq_policy *policy,
  273. struct cpufreq_freqs *freqs, unsigned int state)
  274. {
  275. for_each_cpu(freqs->cpu, policy->cpus)
  276. __cpufreq_notify_transition(policy, freqs, state);
  277. }
  278. EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
  279. /*********************************************************************
  280. * SYSFS INTERFACE *
  281. *********************************************************************/
  282. static struct cpufreq_governor *__find_governor(const char *str_governor)
  283. {
  284. struct cpufreq_governor *t;
  285. list_for_each_entry(t, &cpufreq_governor_list, governor_list)
  286. if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
  287. return t;
  288. return NULL;
  289. }
  290. /**
  291. * cpufreq_parse_governor - parse a governor string
  292. */
  293. static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
  294. struct cpufreq_governor **governor)
  295. {
  296. int err = -EINVAL;
  297. if (!cpufreq_driver)
  298. goto out;
  299. if (cpufreq_driver->setpolicy) {
  300. if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
  301. *policy = CPUFREQ_POLICY_PERFORMANCE;
  302. err = 0;
  303. } else if (!strnicmp(str_governor, "powersave",
  304. CPUFREQ_NAME_LEN)) {
  305. *policy = CPUFREQ_POLICY_POWERSAVE;
  306. err = 0;
  307. }
  308. } else if (has_target()) {
  309. struct cpufreq_governor *t;
  310. mutex_lock(&cpufreq_governor_mutex);
  311. t = __find_governor(str_governor);
  312. if (t == NULL) {
  313. int ret;
  314. mutex_unlock(&cpufreq_governor_mutex);
  315. ret = request_module("cpufreq_%s", str_governor);
  316. mutex_lock(&cpufreq_governor_mutex);
  317. if (ret == 0)
  318. t = __find_governor(str_governor);
  319. }
  320. if (t != NULL) {
  321. *governor = t;
  322. err = 0;
  323. }
  324. mutex_unlock(&cpufreq_governor_mutex);
  325. }
  326. out:
  327. return err;
  328. }
  329. /**
  330. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  331. * print out cpufreq information
  332. *
  333. * Write out information from cpufreq_driver->policy[cpu]; object must be
  334. * "unsigned int".
  335. */
  336. #define show_one(file_name, object) \
  337. static ssize_t show_##file_name \
  338. (struct cpufreq_policy *policy, char *buf) \
  339. { \
  340. return sprintf(buf, "%u\n", policy->object); \
  341. }
  342. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  343. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  344. show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
  345. show_one(scaling_min_freq, min);
  346. show_one(scaling_max_freq, max);
  347. show_one(scaling_cur_freq, cur);
  348. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  349. struct cpufreq_policy *new_policy);
  350. /**
  351. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  352. */
  353. #define store_one(file_name, object) \
  354. static ssize_t store_##file_name \
  355. (struct cpufreq_policy *policy, const char *buf, size_t count) \
  356. { \
  357. int ret; \
  358. struct cpufreq_policy new_policy; \
  359. \
  360. ret = cpufreq_get_policy(&new_policy, policy->cpu); \
  361. if (ret) \
  362. return -EINVAL; \
  363. \
  364. ret = sscanf(buf, "%u", &new_policy.object); \
  365. if (ret != 1) \
  366. return -EINVAL; \
  367. \
  368. ret = cpufreq_set_policy(policy, &new_policy); \
  369. policy->user_policy.object = policy->object; \
  370. \
  371. return ret ? ret : count; \
  372. }
  373. store_one(scaling_min_freq, min);
  374. store_one(scaling_max_freq, max);
  375. /**
  376. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  377. */
  378. static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
  379. char *buf)
  380. {
  381. unsigned int cur_freq = __cpufreq_get(policy->cpu);
  382. if (!cur_freq)
  383. return sprintf(buf, "<unknown>");
  384. return sprintf(buf, "%u\n", cur_freq);
  385. }
  386. /**
  387. * show_scaling_governor - show the current policy for the specified CPU
  388. */
  389. static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
  390. {
  391. if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
  392. return sprintf(buf, "powersave\n");
  393. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  394. return sprintf(buf, "performance\n");
  395. else if (policy->governor)
  396. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
  397. policy->governor->name);
  398. return -EINVAL;
  399. }
  400. /**
  401. * store_scaling_governor - store policy for the specified CPU
  402. */
  403. static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
  404. const char *buf, size_t count)
  405. {
  406. int ret;
  407. char str_governor[16];
  408. struct cpufreq_policy new_policy;
  409. ret = cpufreq_get_policy(&new_policy, policy->cpu);
  410. if (ret)
  411. return ret;
  412. ret = sscanf(buf, "%15s", str_governor);
  413. if (ret != 1)
  414. return -EINVAL;
  415. if (cpufreq_parse_governor(str_governor, &new_policy.policy,
  416. &new_policy.governor))
  417. return -EINVAL;
  418. ret = cpufreq_set_policy(policy, &new_policy);
  419. policy->user_policy.policy = policy->policy;
  420. policy->user_policy.governor = policy->governor;
  421. if (ret)
  422. return ret;
  423. else
  424. return count;
  425. }
  426. /**
  427. * show_scaling_driver - show the cpufreq driver currently loaded
  428. */
  429. static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
  430. {
  431. return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
  432. }
  433. /**
  434. * show_scaling_available_governors - show the available CPUfreq governors
  435. */
  436. static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
  437. char *buf)
  438. {
  439. ssize_t i = 0;
  440. struct cpufreq_governor *t;
  441. if (!has_target()) {
  442. i += sprintf(buf, "performance powersave");
  443. goto out;
  444. }
  445. list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
  446. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
  447. - (CPUFREQ_NAME_LEN + 2)))
  448. goto out;
  449. i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
  450. }
  451. out:
  452. i += sprintf(&buf[i], "\n");
  453. return i;
  454. }
  455. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
  456. {
  457. ssize_t i = 0;
  458. unsigned int cpu;
  459. for_each_cpu(cpu, mask) {
  460. if (i)
  461. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
  462. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
  463. if (i >= (PAGE_SIZE - 5))
  464. break;
  465. }
  466. i += sprintf(&buf[i], "\n");
  467. return i;
  468. }
  469. EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
  470. /**
  471. * show_related_cpus - show the CPUs affected by each transition even if
  472. * hw coordination is in use
  473. */
  474. static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
  475. {
  476. return cpufreq_show_cpus(policy->related_cpus, buf);
  477. }
  478. /**
  479. * show_affected_cpus - show the CPUs affected by each transition
  480. */
  481. static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
  482. {
  483. return cpufreq_show_cpus(policy->cpus, buf);
  484. }
  485. static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
  486. const char *buf, size_t count)
  487. {
  488. unsigned int freq = 0;
  489. unsigned int ret;
  490. if (!policy->governor || !policy->governor->store_setspeed)
  491. return -EINVAL;
  492. ret = sscanf(buf, "%u", &freq);
  493. if (ret != 1)
  494. return -EINVAL;
  495. policy->governor->store_setspeed(policy, freq);
  496. return count;
  497. }
  498. static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
  499. {
  500. if (!policy->governor || !policy->governor->show_setspeed)
  501. return sprintf(buf, "<unsupported>\n");
  502. return policy->governor->show_setspeed(policy, buf);
  503. }
  504. /**
  505. * show_bios_limit - show the current cpufreq HW/BIOS limitation
  506. */
  507. static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
  508. {
  509. unsigned int limit;
  510. int ret;
  511. if (cpufreq_driver->bios_limit) {
  512. ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
  513. if (!ret)
  514. return sprintf(buf, "%u\n", limit);
  515. }
  516. return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
  517. }
  518. cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
  519. cpufreq_freq_attr_ro(cpuinfo_min_freq);
  520. cpufreq_freq_attr_ro(cpuinfo_max_freq);
  521. cpufreq_freq_attr_ro(cpuinfo_transition_latency);
  522. cpufreq_freq_attr_ro(scaling_available_governors);
  523. cpufreq_freq_attr_ro(scaling_driver);
  524. cpufreq_freq_attr_ro(scaling_cur_freq);
  525. cpufreq_freq_attr_ro(bios_limit);
  526. cpufreq_freq_attr_ro(related_cpus);
  527. cpufreq_freq_attr_ro(affected_cpus);
  528. cpufreq_freq_attr_rw(scaling_min_freq);
  529. cpufreq_freq_attr_rw(scaling_max_freq);
  530. cpufreq_freq_attr_rw(scaling_governor);
  531. cpufreq_freq_attr_rw(scaling_setspeed);
  532. static struct attribute *default_attrs[] = {
  533. &cpuinfo_min_freq.attr,
  534. &cpuinfo_max_freq.attr,
  535. &cpuinfo_transition_latency.attr,
  536. &scaling_min_freq.attr,
  537. &scaling_max_freq.attr,
  538. &affected_cpus.attr,
  539. &related_cpus.attr,
  540. &scaling_governor.attr,
  541. &scaling_driver.attr,
  542. &scaling_available_governors.attr,
  543. &scaling_setspeed.attr,
  544. NULL
  545. };
  546. #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
  547. #define to_attr(a) container_of(a, struct freq_attr, attr)
  548. static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
  549. {
  550. struct cpufreq_policy *policy = to_policy(kobj);
  551. struct freq_attr *fattr = to_attr(attr);
  552. ssize_t ret;
  553. if (!down_read_trylock(&cpufreq_rwsem))
  554. return -EINVAL;
  555. down_read(&policy->rwsem);
  556. if (fattr->show)
  557. ret = fattr->show(policy, buf);
  558. else
  559. ret = -EIO;
  560. up_read(&policy->rwsem);
  561. up_read(&cpufreq_rwsem);
  562. return ret;
  563. }
  564. static ssize_t store(struct kobject *kobj, struct attribute *attr,
  565. const char *buf, size_t count)
  566. {
  567. struct cpufreq_policy *policy = to_policy(kobj);
  568. struct freq_attr *fattr = to_attr(attr);
  569. ssize_t ret = -EINVAL;
  570. get_online_cpus();
  571. if (!cpu_online(policy->cpu))
  572. goto unlock;
  573. if (!down_read_trylock(&cpufreq_rwsem))
  574. goto unlock;
  575. down_write(&policy->rwsem);
  576. if (fattr->store)
  577. ret = fattr->store(policy, buf, count);
  578. else
  579. ret = -EIO;
  580. up_write(&policy->rwsem);
  581. up_read(&cpufreq_rwsem);
  582. unlock:
  583. put_online_cpus();
  584. return ret;
  585. }
  586. static void cpufreq_sysfs_release(struct kobject *kobj)
  587. {
  588. struct cpufreq_policy *policy = to_policy(kobj);
  589. pr_debug("last reference is dropped\n");
  590. complete(&policy->kobj_unregister);
  591. }
  592. static const struct sysfs_ops sysfs_ops = {
  593. .show = show,
  594. .store = store,
  595. };
  596. static struct kobj_type ktype_cpufreq = {
  597. .sysfs_ops = &sysfs_ops,
  598. .default_attrs = default_attrs,
  599. .release = cpufreq_sysfs_release,
  600. };
  601. struct kobject *cpufreq_global_kobject;
  602. EXPORT_SYMBOL(cpufreq_global_kobject);
  603. static int cpufreq_global_kobject_usage;
  604. int cpufreq_get_global_kobject(void)
  605. {
  606. if (!cpufreq_global_kobject_usage++)
  607. return kobject_add(cpufreq_global_kobject,
  608. &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
  609. return 0;
  610. }
  611. EXPORT_SYMBOL(cpufreq_get_global_kobject);
  612. void cpufreq_put_global_kobject(void)
  613. {
  614. if (!--cpufreq_global_kobject_usage)
  615. kobject_del(cpufreq_global_kobject);
  616. }
  617. EXPORT_SYMBOL(cpufreq_put_global_kobject);
  618. int cpufreq_sysfs_create_file(const struct attribute *attr)
  619. {
  620. int ret = cpufreq_get_global_kobject();
  621. if (!ret) {
  622. ret = sysfs_create_file(cpufreq_global_kobject, attr);
  623. if (ret)
  624. cpufreq_put_global_kobject();
  625. }
  626. return ret;
  627. }
  628. EXPORT_SYMBOL(cpufreq_sysfs_create_file);
  629. void cpufreq_sysfs_remove_file(const struct attribute *attr)
  630. {
  631. sysfs_remove_file(cpufreq_global_kobject, attr);
  632. cpufreq_put_global_kobject();
  633. }
  634. EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
  635. /* symlink affected CPUs */
  636. static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
  637. {
  638. unsigned int j;
  639. int ret = 0;
  640. for_each_cpu(j, policy->cpus) {
  641. struct device *cpu_dev;
  642. if (j == policy->cpu)
  643. continue;
  644. pr_debug("Adding link for CPU: %u\n", j);
  645. cpu_dev = get_cpu_device(j);
  646. ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
  647. "cpufreq");
  648. if (ret)
  649. break;
  650. }
  651. return ret;
  652. }
  653. static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
  654. struct device *dev)
  655. {
  656. struct freq_attr **drv_attr;
  657. int ret = 0;
  658. /* prepare interface data */
  659. ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
  660. &dev->kobj, "cpufreq");
  661. if (ret)
  662. return ret;
  663. /* set up files for this cpu device */
  664. drv_attr = cpufreq_driver->attr;
  665. while ((drv_attr) && (*drv_attr)) {
  666. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  667. if (ret)
  668. goto err_out_kobj_put;
  669. drv_attr++;
  670. }
  671. if (cpufreq_driver->get) {
  672. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  673. if (ret)
  674. goto err_out_kobj_put;
  675. }
  676. if (has_target()) {
  677. ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  678. if (ret)
  679. goto err_out_kobj_put;
  680. }
  681. if (cpufreq_driver->bios_limit) {
  682. ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
  683. if (ret)
  684. goto err_out_kobj_put;
  685. }
  686. ret = cpufreq_add_dev_symlink(policy);
  687. if (ret)
  688. goto err_out_kobj_put;
  689. return ret;
  690. err_out_kobj_put:
  691. kobject_put(&policy->kobj);
  692. wait_for_completion(&policy->kobj_unregister);
  693. return ret;
  694. }
  695. static void cpufreq_init_policy(struct cpufreq_policy *policy)
  696. {
  697. struct cpufreq_policy new_policy;
  698. int ret = 0;
  699. memcpy(&new_policy, policy, sizeof(*policy));
  700. /* assure that the starting sequence is run in cpufreq_set_policy */
  701. policy->governor = NULL;
  702. /* set default policy */
  703. ret = cpufreq_set_policy(policy, &new_policy);
  704. policy->user_policy.policy = policy->policy;
  705. policy->user_policy.governor = policy->governor;
  706. if (ret) {
  707. pr_debug("setting policy failed\n");
  708. if (cpufreq_driver->exit)
  709. cpufreq_driver->exit(policy);
  710. }
  711. }
  712. #ifdef CONFIG_HOTPLUG_CPU
  713. static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
  714. unsigned int cpu, struct device *dev,
  715. bool frozen)
  716. {
  717. int ret = 0;
  718. unsigned long flags;
  719. if (has_target()) {
  720. ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
  721. if (ret) {
  722. pr_err("%s: Failed to stop governor\n", __func__);
  723. return ret;
  724. }
  725. }
  726. down_write(&policy->rwsem);
  727. write_lock_irqsave(&cpufreq_driver_lock, flags);
  728. cpumask_set_cpu(cpu, policy->cpus);
  729. per_cpu(cpufreq_cpu_data, cpu) = policy;
  730. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  731. up_write(&policy->rwsem);
  732. if (has_target()) {
  733. if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
  734. (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
  735. pr_err("%s: Failed to start governor\n", __func__);
  736. return ret;
  737. }
  738. }
  739. /* Don't touch sysfs links during light-weight init */
  740. if (!frozen)
  741. ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
  742. return ret;
  743. }
  744. #endif
  745. static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
  746. {
  747. struct cpufreq_policy *policy;
  748. unsigned long flags;
  749. read_lock_irqsave(&cpufreq_driver_lock, flags);
  750. policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
  751. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  752. return policy;
  753. }
  754. static struct cpufreq_policy *cpufreq_policy_alloc(void)
  755. {
  756. struct cpufreq_policy *policy;
  757. policy = kzalloc(sizeof(*policy), GFP_KERNEL);
  758. if (!policy)
  759. return NULL;
  760. if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
  761. goto err_free_policy;
  762. if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
  763. goto err_free_cpumask;
  764. INIT_LIST_HEAD(&policy->policy_list);
  765. init_rwsem(&policy->rwsem);
  766. return policy;
  767. err_free_cpumask:
  768. free_cpumask_var(policy->cpus);
  769. err_free_policy:
  770. kfree(policy);
  771. return NULL;
  772. }
  773. static void cpufreq_policy_free(struct cpufreq_policy *policy)
  774. {
  775. free_cpumask_var(policy->related_cpus);
  776. free_cpumask_var(policy->cpus);
  777. kfree(policy);
  778. }
  779. static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
  780. {
  781. if (WARN_ON(cpu == policy->cpu))
  782. return;
  783. down_write(&policy->rwsem);
  784. policy->last_cpu = policy->cpu;
  785. policy->cpu = cpu;
  786. up_write(&policy->rwsem);
  787. cpufreq_frequency_table_update_policy_cpu(policy);
  788. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  789. CPUFREQ_UPDATE_POLICY_CPU, policy);
  790. }
  791. static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
  792. bool frozen)
  793. {
  794. unsigned int j, cpu = dev->id;
  795. int ret = -ENOMEM;
  796. struct cpufreq_policy *policy;
  797. unsigned long flags;
  798. #ifdef CONFIG_HOTPLUG_CPU
  799. struct cpufreq_policy *tpolicy;
  800. struct cpufreq_governor *gov;
  801. #endif
  802. if (cpu_is_offline(cpu))
  803. return 0;
  804. pr_debug("adding CPU %u\n", cpu);
  805. #ifdef CONFIG_SMP
  806. /* check whether a different CPU already registered this
  807. * CPU because it is in the same boat. */
  808. policy = cpufreq_cpu_get(cpu);
  809. if (unlikely(policy)) {
  810. cpufreq_cpu_put(policy);
  811. return 0;
  812. }
  813. #endif
  814. if (!down_read_trylock(&cpufreq_rwsem))
  815. return 0;
  816. #ifdef CONFIG_HOTPLUG_CPU
  817. /* Check if this cpu was hot-unplugged earlier and has siblings */
  818. read_lock_irqsave(&cpufreq_driver_lock, flags);
  819. list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
  820. if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
  821. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  822. ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
  823. up_read(&cpufreq_rwsem);
  824. return ret;
  825. }
  826. }
  827. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  828. #endif
  829. if (frozen)
  830. /* Restore the saved policy when doing light-weight init */
  831. policy = cpufreq_policy_restore(cpu);
  832. else
  833. policy = cpufreq_policy_alloc();
  834. if (!policy)
  835. goto nomem_out;
  836. /*
  837. * In the resume path, since we restore a saved policy, the assignment
  838. * to policy->cpu is like an update of the existing policy, rather than
  839. * the creation of a brand new one. So we need to perform this update
  840. * by invoking update_policy_cpu().
  841. */
  842. if (frozen && cpu != policy->cpu)
  843. update_policy_cpu(policy, cpu);
  844. else
  845. policy->cpu = cpu;
  846. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  847. cpumask_copy(policy->cpus, cpumask_of(cpu));
  848. init_completion(&policy->kobj_unregister);
  849. INIT_WORK(&policy->update, handle_update);
  850. /* call driver. From then on the cpufreq must be able
  851. * to accept all calls to ->verify and ->setpolicy for this CPU
  852. */
  853. ret = cpufreq_driver->init(policy);
  854. if (ret) {
  855. pr_debug("initialization failed\n");
  856. goto err_set_policy_cpu;
  857. }
  858. if (cpufreq_driver->get) {
  859. policy->cur = cpufreq_driver->get(policy->cpu);
  860. if (!policy->cur) {
  861. pr_err("%s: ->get() failed\n", __func__);
  862. goto err_get_freq;
  863. }
  864. }
  865. /* related cpus should atleast have policy->cpus */
  866. cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
  867. /*
  868. * affected cpus must always be the one, which are online. We aren't
  869. * managing offline cpus here.
  870. */
  871. cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
  872. policy->user_policy.min = policy->min;
  873. policy->user_policy.max = policy->max;
  874. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  875. CPUFREQ_START, policy);
  876. #ifdef CONFIG_HOTPLUG_CPU
  877. gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
  878. if (gov) {
  879. policy->governor = gov;
  880. pr_debug("Restoring governor %s for cpu %d\n",
  881. policy->governor->name, cpu);
  882. }
  883. #endif
  884. write_lock_irqsave(&cpufreq_driver_lock, flags);
  885. for_each_cpu(j, policy->cpus)
  886. per_cpu(cpufreq_cpu_data, j) = policy;
  887. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  888. if (!frozen) {
  889. ret = cpufreq_add_dev_interface(policy, dev);
  890. if (ret)
  891. goto err_out_unregister;
  892. }
  893. write_lock_irqsave(&cpufreq_driver_lock, flags);
  894. list_add(&policy->policy_list, &cpufreq_policy_list);
  895. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  896. cpufreq_init_policy(policy);
  897. kobject_uevent(&policy->kobj, KOBJ_ADD);
  898. up_read(&cpufreq_rwsem);
  899. pr_debug("initialization complete\n");
  900. return 0;
  901. err_out_unregister:
  902. write_lock_irqsave(&cpufreq_driver_lock, flags);
  903. for_each_cpu(j, policy->cpus)
  904. per_cpu(cpufreq_cpu_data, j) = NULL;
  905. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  906. err_get_freq:
  907. if (cpufreq_driver->exit)
  908. cpufreq_driver->exit(policy);
  909. err_set_policy_cpu:
  910. cpufreq_policy_free(policy);
  911. nomem_out:
  912. up_read(&cpufreq_rwsem);
  913. return ret;
  914. }
  915. /**
  916. * cpufreq_add_dev - add a CPU device
  917. *
  918. * Adds the cpufreq interface for a CPU device.
  919. *
  920. * The Oracle says: try running cpufreq registration/unregistration concurrently
  921. * with with cpu hotplugging and all hell will break loose. Tried to clean this
  922. * mess up, but more thorough testing is needed. - Mathieu
  923. */
  924. static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
  925. {
  926. return __cpufreq_add_dev(dev, sif, false);
  927. }
  928. static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
  929. unsigned int old_cpu, bool frozen)
  930. {
  931. struct device *cpu_dev;
  932. int ret;
  933. /* first sibling now owns the new sysfs dir */
  934. cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
  935. /* Don't touch sysfs files during light-weight tear-down */
  936. if (frozen)
  937. return cpu_dev->id;
  938. sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
  939. ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
  940. if (ret) {
  941. pr_err("%s: Failed to move kobj: %d", __func__, ret);
  942. down_write(&policy->rwsem);
  943. cpumask_set_cpu(old_cpu, policy->cpus);
  944. up_write(&policy->rwsem);
  945. ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
  946. "cpufreq");
  947. return -EINVAL;
  948. }
  949. return cpu_dev->id;
  950. }
  951. static int __cpufreq_remove_dev_prepare(struct device *dev,
  952. struct subsys_interface *sif,
  953. bool frozen)
  954. {
  955. unsigned int cpu = dev->id, cpus;
  956. int new_cpu, ret;
  957. unsigned long flags;
  958. struct cpufreq_policy *policy;
  959. pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
  960. write_lock_irqsave(&cpufreq_driver_lock, flags);
  961. policy = per_cpu(cpufreq_cpu_data, cpu);
  962. /* Save the policy somewhere when doing a light-weight tear-down */
  963. if (frozen)
  964. per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
  965. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  966. if (!policy) {
  967. pr_debug("%s: No cpu_data found\n", __func__);
  968. return -EINVAL;
  969. }
  970. if (has_target()) {
  971. ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
  972. if (ret) {
  973. pr_err("%s: Failed to stop governor\n", __func__);
  974. return ret;
  975. }
  976. }
  977. #ifdef CONFIG_HOTPLUG_CPU
  978. if (!cpufreq_driver->setpolicy)
  979. strncpy(per_cpu(cpufreq_cpu_governor, cpu),
  980. policy->governor->name, CPUFREQ_NAME_LEN);
  981. #endif
  982. down_read(&policy->rwsem);
  983. cpus = cpumask_weight(policy->cpus);
  984. up_read(&policy->rwsem);
  985. if (cpu != policy->cpu) {
  986. if (!frozen)
  987. sysfs_remove_link(&dev->kobj, "cpufreq");
  988. } else if (cpus > 1) {
  989. new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
  990. if (new_cpu >= 0) {
  991. update_policy_cpu(policy, new_cpu);
  992. if (!frozen) {
  993. pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
  994. __func__, new_cpu, cpu);
  995. }
  996. }
  997. }
  998. return 0;
  999. }
  1000. static int __cpufreq_remove_dev_finish(struct device *dev,
  1001. struct subsys_interface *sif,
  1002. bool frozen)
  1003. {
  1004. unsigned int cpu = dev->id, cpus;
  1005. int ret;
  1006. unsigned long flags;
  1007. struct cpufreq_policy *policy;
  1008. struct kobject *kobj;
  1009. struct completion *cmp;
  1010. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1011. policy = per_cpu(cpufreq_cpu_data, cpu);
  1012. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1013. if (!policy) {
  1014. pr_debug("%s: No cpu_data found\n", __func__);
  1015. return -EINVAL;
  1016. }
  1017. down_write(&policy->rwsem);
  1018. cpus = cpumask_weight(policy->cpus);
  1019. if (cpus > 1)
  1020. cpumask_clear_cpu(cpu, policy->cpus);
  1021. up_write(&policy->rwsem);
  1022. /* If cpu is last user of policy, free policy */
  1023. if (cpus == 1) {
  1024. if (has_target()) {
  1025. ret = __cpufreq_governor(policy,
  1026. CPUFREQ_GOV_POLICY_EXIT);
  1027. if (ret) {
  1028. pr_err("%s: Failed to exit governor\n",
  1029. __func__);
  1030. return ret;
  1031. }
  1032. }
  1033. if (!frozen) {
  1034. down_read(&policy->rwsem);
  1035. kobj = &policy->kobj;
  1036. cmp = &policy->kobj_unregister;
  1037. up_read(&policy->rwsem);
  1038. kobject_put(kobj);
  1039. /*
  1040. * We need to make sure that the underlying kobj is
  1041. * actually not referenced anymore by anybody before we
  1042. * proceed with unloading.
  1043. */
  1044. pr_debug("waiting for dropping of refcount\n");
  1045. wait_for_completion(cmp);
  1046. pr_debug("wait complete\n");
  1047. }
  1048. /*
  1049. * Perform the ->exit() even during light-weight tear-down,
  1050. * since this is a core component, and is essential for the
  1051. * subsequent light-weight ->init() to succeed.
  1052. */
  1053. if (cpufreq_driver->exit)
  1054. cpufreq_driver->exit(policy);
  1055. /* Remove policy from list of active policies */
  1056. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1057. list_del(&policy->policy_list);
  1058. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1059. if (!frozen)
  1060. cpufreq_policy_free(policy);
  1061. } else {
  1062. if (has_target()) {
  1063. if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
  1064. (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
  1065. pr_err("%s: Failed to start governor\n",
  1066. __func__);
  1067. return ret;
  1068. }
  1069. }
  1070. }
  1071. per_cpu(cpufreq_cpu_data, cpu) = NULL;
  1072. return 0;
  1073. }
  1074. /**
  1075. * cpufreq_remove_dev - remove a CPU device
  1076. *
  1077. * Removes the cpufreq interface for a CPU device.
  1078. */
  1079. static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
  1080. {
  1081. unsigned int cpu = dev->id;
  1082. int ret;
  1083. if (cpu_is_offline(cpu))
  1084. return 0;
  1085. ret = __cpufreq_remove_dev_prepare(dev, sif, false);
  1086. if (!ret)
  1087. ret = __cpufreq_remove_dev_finish(dev, sif, false);
  1088. return ret;
  1089. }
  1090. static void handle_update(struct work_struct *work)
  1091. {
  1092. struct cpufreq_policy *policy =
  1093. container_of(work, struct cpufreq_policy, update);
  1094. unsigned int cpu = policy->cpu;
  1095. pr_debug("handle_update for cpu %u called\n", cpu);
  1096. cpufreq_update_policy(cpu);
  1097. }
  1098. /**
  1099. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
  1100. * in deep trouble.
  1101. * @cpu: cpu number
  1102. * @old_freq: CPU frequency the kernel thinks the CPU runs at
  1103. * @new_freq: CPU frequency the CPU actually runs at
  1104. *
  1105. * We adjust to current frequency first, and need to clean up later.
  1106. * So either call to cpufreq_update_policy() or schedule handle_update()).
  1107. */
  1108. static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
  1109. unsigned int new_freq)
  1110. {
  1111. struct cpufreq_policy *policy;
  1112. struct cpufreq_freqs freqs;
  1113. unsigned long flags;
  1114. pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
  1115. "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
  1116. freqs.old = old_freq;
  1117. freqs.new = new_freq;
  1118. read_lock_irqsave(&cpufreq_driver_lock, flags);
  1119. policy = per_cpu(cpufreq_cpu_data, cpu);
  1120. read_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1121. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  1122. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  1123. }
  1124. /**
  1125. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  1126. * @cpu: CPU number
  1127. *
  1128. * This is the last known freq, without actually getting it from the driver.
  1129. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  1130. */
  1131. unsigned int cpufreq_quick_get(unsigned int cpu)
  1132. {
  1133. struct cpufreq_policy *policy;
  1134. unsigned int ret_freq = 0;
  1135. if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
  1136. return cpufreq_driver->get(cpu);
  1137. policy = cpufreq_cpu_get(cpu);
  1138. if (policy) {
  1139. ret_freq = policy->cur;
  1140. cpufreq_cpu_put(policy);
  1141. }
  1142. return ret_freq;
  1143. }
  1144. EXPORT_SYMBOL(cpufreq_quick_get);
  1145. /**
  1146. * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
  1147. * @cpu: CPU number
  1148. *
  1149. * Just return the max possible frequency for a given CPU.
  1150. */
  1151. unsigned int cpufreq_quick_get_max(unsigned int cpu)
  1152. {
  1153. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1154. unsigned int ret_freq = 0;
  1155. if (policy) {
  1156. ret_freq = policy->max;
  1157. cpufreq_cpu_put(policy);
  1158. }
  1159. return ret_freq;
  1160. }
  1161. EXPORT_SYMBOL(cpufreq_quick_get_max);
  1162. static unsigned int __cpufreq_get(unsigned int cpu)
  1163. {
  1164. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1165. unsigned int ret_freq = 0;
  1166. if (!cpufreq_driver->get)
  1167. return ret_freq;
  1168. ret_freq = cpufreq_driver->get(cpu);
  1169. if (ret_freq && policy->cur &&
  1170. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1171. /* verify no discrepancy between actual and
  1172. saved value exists */
  1173. if (unlikely(ret_freq != policy->cur)) {
  1174. cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
  1175. schedule_work(&policy->update);
  1176. }
  1177. }
  1178. return ret_freq;
  1179. }
  1180. /**
  1181. * cpufreq_get - get the current CPU frequency (in kHz)
  1182. * @cpu: CPU number
  1183. *
  1184. * Get the CPU current (static) CPU frequency
  1185. */
  1186. unsigned int cpufreq_get(unsigned int cpu)
  1187. {
  1188. struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
  1189. unsigned int ret_freq = 0;
  1190. if (cpufreq_disabled() || !cpufreq_driver)
  1191. return -ENOENT;
  1192. BUG_ON(!policy);
  1193. if (!down_read_trylock(&cpufreq_rwsem))
  1194. return 0;
  1195. down_read(&policy->rwsem);
  1196. ret_freq = __cpufreq_get(cpu);
  1197. up_read(&policy->rwsem);
  1198. up_read(&cpufreq_rwsem);
  1199. return ret_freq;
  1200. }
  1201. EXPORT_SYMBOL(cpufreq_get);
  1202. static struct subsys_interface cpufreq_interface = {
  1203. .name = "cpufreq",
  1204. .subsys = &cpu_subsys,
  1205. .add_dev = cpufreq_add_dev,
  1206. .remove_dev = cpufreq_remove_dev,
  1207. };
  1208. void cpufreq_suspend(void)
  1209. {
  1210. struct cpufreq_policy *policy;
  1211. if (!has_target())
  1212. return;
  1213. pr_debug("%s: Suspending Governors\n", __func__);
  1214. list_for_each_entry(policy, &cpufreq_policy_list, policy_list)
  1215. if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
  1216. pr_err("%s: Failed to stop governor for policy: %p\n",
  1217. __func__, policy);
  1218. cpufreq_suspended = true;
  1219. }
  1220. void cpufreq_resume(void)
  1221. {
  1222. struct cpufreq_policy *policy;
  1223. if (!has_target())
  1224. return;
  1225. pr_debug("%s: Resuming Governors\n", __func__);
  1226. cpufreq_suspended = false;
  1227. list_for_each_entry(policy, &cpufreq_policy_list, policy_list)
  1228. if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
  1229. || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
  1230. pr_err("%s: Failed to start governor for policy: %p\n",
  1231. __func__, policy);
  1232. }
  1233. /**
  1234. * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
  1235. *
  1236. * This function is only executed for the boot processor. The other CPUs
  1237. * have been put offline by means of CPU hotplug.
  1238. */
  1239. static int cpufreq_bp_suspend(void)
  1240. {
  1241. int ret = 0;
  1242. int cpu = smp_processor_id();
  1243. struct cpufreq_policy *policy;
  1244. pr_debug("suspending cpu %u\n", cpu);
  1245. /* If there's no policy for the boot CPU, we have nothing to do. */
  1246. policy = cpufreq_cpu_get(cpu);
  1247. if (!policy)
  1248. return 0;
  1249. if (cpufreq_driver->suspend) {
  1250. ret = cpufreq_driver->suspend(policy);
  1251. if (ret)
  1252. printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
  1253. "step on CPU %u\n", policy->cpu);
  1254. }
  1255. cpufreq_cpu_put(policy);
  1256. return ret;
  1257. }
  1258. /**
  1259. * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
  1260. *
  1261. * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
  1262. * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
  1263. * restored. It will verify that the current freq is in sync with
  1264. * what we believe it to be. This is a bit later than when it
  1265. * should be, but nonethteless it's better than calling
  1266. * cpufreq_driver->get() here which might re-enable interrupts...
  1267. *
  1268. * This function is only executed for the boot CPU. The other CPUs have not
  1269. * been turned on yet.
  1270. */
  1271. static void cpufreq_bp_resume(void)
  1272. {
  1273. int ret = 0;
  1274. int cpu = smp_processor_id();
  1275. struct cpufreq_policy *policy;
  1276. pr_debug("resuming cpu %u\n", cpu);
  1277. /* If there's no policy for the boot CPU, we have nothing to do. */
  1278. policy = cpufreq_cpu_get(cpu);
  1279. if (!policy)
  1280. return;
  1281. if (cpufreq_driver->resume) {
  1282. ret = cpufreq_driver->resume(policy);
  1283. if (ret) {
  1284. printk(KERN_ERR "cpufreq: resume failed in ->resume "
  1285. "step on CPU %u\n", policy->cpu);
  1286. goto fail;
  1287. }
  1288. }
  1289. schedule_work(&policy->update);
  1290. fail:
  1291. cpufreq_cpu_put(policy);
  1292. }
  1293. static struct syscore_ops cpufreq_syscore_ops = {
  1294. .suspend = cpufreq_bp_suspend,
  1295. .resume = cpufreq_bp_resume,
  1296. };
  1297. /**
  1298. * cpufreq_get_current_driver - return current driver's name
  1299. *
  1300. * Return the name string of the currently loaded cpufreq driver
  1301. * or NULL, if none.
  1302. */
  1303. const char *cpufreq_get_current_driver(void)
  1304. {
  1305. if (cpufreq_driver)
  1306. return cpufreq_driver->name;
  1307. return NULL;
  1308. }
  1309. EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
  1310. /*********************************************************************
  1311. * NOTIFIER LISTS INTERFACE *
  1312. *********************************************************************/
  1313. /**
  1314. * cpufreq_register_notifier - register a driver with cpufreq
  1315. * @nb: notifier function to register
  1316. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1317. *
  1318. * Add a driver to one of two lists: either a list of drivers that
  1319. * are notified about clock rate changes (once before and once after
  1320. * the transition), or a list of drivers that are notified about
  1321. * changes in cpufreq policy.
  1322. *
  1323. * This function may sleep, and has the same return conditions as
  1324. * blocking_notifier_chain_register.
  1325. */
  1326. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1327. {
  1328. int ret;
  1329. if (cpufreq_disabled())
  1330. return -EINVAL;
  1331. WARN_ON(!init_cpufreq_transition_notifier_list_called);
  1332. switch (list) {
  1333. case CPUFREQ_TRANSITION_NOTIFIER:
  1334. ret = srcu_notifier_chain_register(
  1335. &cpufreq_transition_notifier_list, nb);
  1336. break;
  1337. case CPUFREQ_POLICY_NOTIFIER:
  1338. ret = blocking_notifier_chain_register(
  1339. &cpufreq_policy_notifier_list, nb);
  1340. break;
  1341. default:
  1342. ret = -EINVAL;
  1343. }
  1344. return ret;
  1345. }
  1346. EXPORT_SYMBOL(cpufreq_register_notifier);
  1347. /**
  1348. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1349. * @nb: notifier block to be unregistered
  1350. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1351. *
  1352. * Remove a driver from the CPU frequency notifier list.
  1353. *
  1354. * This function may sleep, and has the same return conditions as
  1355. * blocking_notifier_chain_unregister.
  1356. */
  1357. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1358. {
  1359. int ret;
  1360. if (cpufreq_disabled())
  1361. return -EINVAL;
  1362. switch (list) {
  1363. case CPUFREQ_TRANSITION_NOTIFIER:
  1364. ret = srcu_notifier_chain_unregister(
  1365. &cpufreq_transition_notifier_list, nb);
  1366. break;
  1367. case CPUFREQ_POLICY_NOTIFIER:
  1368. ret = blocking_notifier_chain_unregister(
  1369. &cpufreq_policy_notifier_list, nb);
  1370. break;
  1371. default:
  1372. ret = -EINVAL;
  1373. }
  1374. return ret;
  1375. }
  1376. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1377. /*********************************************************************
  1378. * GOVERNORS *
  1379. *********************************************************************/
  1380. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1381. unsigned int target_freq,
  1382. unsigned int relation)
  1383. {
  1384. int retval = -EINVAL;
  1385. unsigned int old_target_freq = target_freq;
  1386. if (cpufreq_disabled())
  1387. return -ENODEV;
  1388. /* Make sure that target_freq is within supported range */
  1389. if (target_freq > policy->max)
  1390. target_freq = policy->max;
  1391. if (target_freq < policy->min)
  1392. target_freq = policy->min;
  1393. pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
  1394. policy->cpu, target_freq, relation, old_target_freq);
  1395. /*
  1396. * This might look like a redundant call as we are checking it again
  1397. * after finding index. But it is left intentionally for cases where
  1398. * exactly same freq is called again and so we can save on few function
  1399. * calls.
  1400. */
  1401. if (target_freq == policy->cur)
  1402. return 0;
  1403. if (cpufreq_driver->target)
  1404. retval = cpufreq_driver->target(policy, target_freq, relation);
  1405. else if (cpufreq_driver->target_index) {
  1406. struct cpufreq_frequency_table *freq_table;
  1407. struct cpufreq_freqs freqs;
  1408. bool notify;
  1409. int index;
  1410. freq_table = cpufreq_frequency_get_table(policy->cpu);
  1411. if (unlikely(!freq_table)) {
  1412. pr_err("%s: Unable to find freq_table\n", __func__);
  1413. goto out;
  1414. }
  1415. retval = cpufreq_frequency_table_target(policy, freq_table,
  1416. target_freq, relation, &index);
  1417. if (unlikely(retval)) {
  1418. pr_err("%s: Unable to find matching freq\n", __func__);
  1419. goto out;
  1420. }
  1421. if (freq_table[index].frequency == policy->cur) {
  1422. retval = 0;
  1423. goto out;
  1424. }
  1425. notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
  1426. if (notify) {
  1427. freqs.old = policy->cur;
  1428. freqs.new = freq_table[index].frequency;
  1429. freqs.flags = 0;
  1430. pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
  1431. __func__, policy->cpu, freqs.old,
  1432. freqs.new);
  1433. cpufreq_notify_transition(policy, &freqs,
  1434. CPUFREQ_PRECHANGE);
  1435. }
  1436. retval = cpufreq_driver->target_index(policy, index);
  1437. if (retval)
  1438. pr_err("%s: Failed to change cpu frequency: %d\n",
  1439. __func__, retval);
  1440. if (notify) {
  1441. /*
  1442. * Notify with old freq in case we failed to change
  1443. * frequency
  1444. */
  1445. if (retval)
  1446. freqs.new = freqs.old;
  1447. cpufreq_notify_transition(policy, &freqs,
  1448. CPUFREQ_POSTCHANGE);
  1449. }
  1450. }
  1451. out:
  1452. return retval;
  1453. }
  1454. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1455. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1456. unsigned int target_freq,
  1457. unsigned int relation)
  1458. {
  1459. int ret = -EINVAL;
  1460. down_write(&policy->rwsem);
  1461. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1462. up_write(&policy->rwsem);
  1463. return ret;
  1464. }
  1465. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1466. /*
  1467. * when "event" is CPUFREQ_GOV_LIMITS
  1468. */
  1469. static int __cpufreq_governor(struct cpufreq_policy *policy,
  1470. unsigned int event)
  1471. {
  1472. int ret;
  1473. /* Only must be defined when default governor is known to have latency
  1474. restrictions, like e.g. conservative or ondemand.
  1475. That this is the case is already ensured in Kconfig
  1476. */
  1477. #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
  1478. struct cpufreq_governor *gov = &cpufreq_gov_performance;
  1479. #else
  1480. struct cpufreq_governor *gov = NULL;
  1481. #endif
  1482. /* Don't start any governor operations if we are entering suspend */
  1483. if (cpufreq_suspended)
  1484. return 0;
  1485. if (policy->governor->max_transition_latency &&
  1486. policy->cpuinfo.transition_latency >
  1487. policy->governor->max_transition_latency) {
  1488. if (!gov)
  1489. return -EINVAL;
  1490. else {
  1491. printk(KERN_WARNING "%s governor failed, too long"
  1492. " transition latency of HW, fallback"
  1493. " to %s governor\n",
  1494. policy->governor->name,
  1495. gov->name);
  1496. policy->governor = gov;
  1497. }
  1498. }
  1499. if (event == CPUFREQ_GOV_POLICY_INIT)
  1500. if (!try_module_get(policy->governor->owner))
  1501. return -EINVAL;
  1502. pr_debug("__cpufreq_governor for CPU %u, event %u\n",
  1503. policy->cpu, event);
  1504. mutex_lock(&cpufreq_governor_lock);
  1505. if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
  1506. || (!policy->governor_enabled
  1507. && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
  1508. mutex_unlock(&cpufreq_governor_lock);
  1509. return -EBUSY;
  1510. }
  1511. if (event == CPUFREQ_GOV_STOP)
  1512. policy->governor_enabled = false;
  1513. else if (event == CPUFREQ_GOV_START)
  1514. policy->governor_enabled = true;
  1515. mutex_unlock(&cpufreq_governor_lock);
  1516. ret = policy->governor->governor(policy, event);
  1517. if (!ret) {
  1518. if (event == CPUFREQ_GOV_POLICY_INIT)
  1519. policy->governor->initialized++;
  1520. else if (event == CPUFREQ_GOV_POLICY_EXIT)
  1521. policy->governor->initialized--;
  1522. } else {
  1523. /* Restore original values */
  1524. mutex_lock(&cpufreq_governor_lock);
  1525. if (event == CPUFREQ_GOV_STOP)
  1526. policy->governor_enabled = true;
  1527. else if (event == CPUFREQ_GOV_START)
  1528. policy->governor_enabled = false;
  1529. mutex_unlock(&cpufreq_governor_lock);
  1530. }
  1531. if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
  1532. ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
  1533. module_put(policy->governor->owner);
  1534. return ret;
  1535. }
  1536. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1537. {
  1538. int err;
  1539. if (!governor)
  1540. return -EINVAL;
  1541. if (cpufreq_disabled())
  1542. return -ENODEV;
  1543. mutex_lock(&cpufreq_governor_mutex);
  1544. governor->initialized = 0;
  1545. err = -EBUSY;
  1546. if (__find_governor(governor->name) == NULL) {
  1547. err = 0;
  1548. list_add(&governor->governor_list, &cpufreq_governor_list);
  1549. }
  1550. mutex_unlock(&cpufreq_governor_mutex);
  1551. return err;
  1552. }
  1553. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1554. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1555. {
  1556. #ifdef CONFIG_HOTPLUG_CPU
  1557. int cpu;
  1558. #endif
  1559. if (!governor)
  1560. return;
  1561. if (cpufreq_disabled())
  1562. return;
  1563. #ifdef CONFIG_HOTPLUG_CPU
  1564. for_each_present_cpu(cpu) {
  1565. if (cpu_online(cpu))
  1566. continue;
  1567. if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
  1568. strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
  1569. }
  1570. #endif
  1571. mutex_lock(&cpufreq_governor_mutex);
  1572. list_del(&governor->governor_list);
  1573. mutex_unlock(&cpufreq_governor_mutex);
  1574. return;
  1575. }
  1576. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1577. /*********************************************************************
  1578. * POLICY INTERFACE *
  1579. *********************************************************************/
  1580. /**
  1581. * cpufreq_get_policy - get the current cpufreq_policy
  1582. * @policy: struct cpufreq_policy into which the current cpufreq_policy
  1583. * is written
  1584. *
  1585. * Reads the current cpufreq policy.
  1586. */
  1587. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1588. {
  1589. struct cpufreq_policy *cpu_policy;
  1590. if (!policy)
  1591. return -EINVAL;
  1592. cpu_policy = cpufreq_cpu_get(cpu);
  1593. if (!cpu_policy)
  1594. return -EINVAL;
  1595. memcpy(policy, cpu_policy, sizeof(*policy));
  1596. cpufreq_cpu_put(cpu_policy);
  1597. return 0;
  1598. }
  1599. EXPORT_SYMBOL(cpufreq_get_policy);
  1600. /*
  1601. * policy : current policy.
  1602. * new_policy: policy to be set.
  1603. */
  1604. static int cpufreq_set_policy(struct cpufreq_policy *policy,
  1605. struct cpufreq_policy *new_policy)
  1606. {
  1607. int ret = 0, failed = 1;
  1608. pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
  1609. new_policy->min, new_policy->max);
  1610. memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
  1611. if (new_policy->min > policy->max || new_policy->max < policy->min) {
  1612. ret = -EINVAL;
  1613. goto error_out;
  1614. }
  1615. /* verify the cpu speed can be set within this limit */
  1616. ret = cpufreq_driver->verify(new_policy);
  1617. if (ret)
  1618. goto error_out;
  1619. /* adjust if necessary - all reasons */
  1620. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1621. CPUFREQ_ADJUST, new_policy);
  1622. /* adjust if necessary - hardware incompatibility*/
  1623. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1624. CPUFREQ_INCOMPATIBLE, new_policy);
  1625. /*
  1626. * verify the cpu speed can be set within this limit, which might be
  1627. * different to the first one
  1628. */
  1629. ret = cpufreq_driver->verify(new_policy);
  1630. if (ret)
  1631. goto error_out;
  1632. /* notification of the new policy */
  1633. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1634. CPUFREQ_NOTIFY, new_policy);
  1635. policy->min = new_policy->min;
  1636. policy->max = new_policy->max;
  1637. pr_debug("new min and max freqs are %u - %u kHz\n",
  1638. policy->min, policy->max);
  1639. if (cpufreq_driver->setpolicy) {
  1640. policy->policy = new_policy->policy;
  1641. pr_debug("setting range\n");
  1642. ret = cpufreq_driver->setpolicy(new_policy);
  1643. } else {
  1644. if (new_policy->governor != policy->governor) {
  1645. /* save old, working values */
  1646. struct cpufreq_governor *old_gov = policy->governor;
  1647. pr_debug("governor switch\n");
  1648. /* end old governor */
  1649. if (policy->governor) {
  1650. __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
  1651. up_write(&policy->rwsem);
  1652. __cpufreq_governor(policy,
  1653. CPUFREQ_GOV_POLICY_EXIT);
  1654. down_write(&policy->rwsem);
  1655. }
  1656. /* start new governor */
  1657. policy->governor = new_policy->governor;
  1658. if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
  1659. if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
  1660. failed = 0;
  1661. } else {
  1662. up_write(&policy->rwsem);
  1663. __cpufreq_governor(policy,
  1664. CPUFREQ_GOV_POLICY_EXIT);
  1665. down_write(&policy->rwsem);
  1666. }
  1667. }
  1668. if (failed) {
  1669. /* new governor failed, so re-start old one */
  1670. pr_debug("starting governor %s failed\n",
  1671. policy->governor->name);
  1672. if (old_gov) {
  1673. policy->governor = old_gov;
  1674. __cpufreq_governor(policy,
  1675. CPUFREQ_GOV_POLICY_INIT);
  1676. __cpufreq_governor(policy,
  1677. CPUFREQ_GOV_START);
  1678. }
  1679. ret = -EINVAL;
  1680. goto error_out;
  1681. }
  1682. /* might be a policy change, too, so fall through */
  1683. }
  1684. pr_debug("governor: change or update limits\n");
  1685. ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
  1686. }
  1687. error_out:
  1688. return ret;
  1689. }
  1690. /**
  1691. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1692. * @cpu: CPU which shall be re-evaluated
  1693. *
  1694. * Useful for policy notifiers which have different necessities
  1695. * at different times.
  1696. */
  1697. int cpufreq_update_policy(unsigned int cpu)
  1698. {
  1699. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  1700. struct cpufreq_policy new_policy;
  1701. int ret;
  1702. if (!policy) {
  1703. ret = -ENODEV;
  1704. goto no_policy;
  1705. }
  1706. down_write(&policy->rwsem);
  1707. pr_debug("updating policy for CPU %u\n", cpu);
  1708. memcpy(&new_policy, policy, sizeof(*policy));
  1709. new_policy.min = policy->user_policy.min;
  1710. new_policy.max = policy->user_policy.max;
  1711. new_policy.policy = policy->user_policy.policy;
  1712. new_policy.governor = policy->user_policy.governor;
  1713. /*
  1714. * BIOS might change freq behind our back
  1715. * -> ask driver for current freq and notify governors about a change
  1716. */
  1717. if (cpufreq_driver->get) {
  1718. new_policy.cur = cpufreq_driver->get(cpu);
  1719. if (!policy->cur) {
  1720. pr_debug("Driver did not initialize current freq");
  1721. policy->cur = new_policy.cur;
  1722. } else {
  1723. if (policy->cur != new_policy.cur && has_target())
  1724. cpufreq_out_of_sync(cpu, policy->cur,
  1725. new_policy.cur);
  1726. }
  1727. }
  1728. ret = cpufreq_set_policy(policy, &new_policy);
  1729. up_write(&policy->rwsem);
  1730. cpufreq_cpu_put(policy);
  1731. no_policy:
  1732. return ret;
  1733. }
  1734. EXPORT_SYMBOL(cpufreq_update_policy);
  1735. static int cpufreq_cpu_callback(struct notifier_block *nfb,
  1736. unsigned long action, void *hcpu)
  1737. {
  1738. unsigned int cpu = (unsigned long)hcpu;
  1739. struct device *dev;
  1740. bool frozen = false;
  1741. dev = get_cpu_device(cpu);
  1742. if (dev) {
  1743. switch (action & ~CPU_TASKS_FROZEN) {
  1744. case CPU_ONLINE:
  1745. __cpufreq_add_dev(dev, NULL, frozen);
  1746. cpufreq_update_policy(cpu);
  1747. break;
  1748. case CPU_DOWN_PREPARE:
  1749. __cpufreq_remove_dev_prepare(dev, NULL, frozen);
  1750. break;
  1751. case CPU_POST_DEAD:
  1752. __cpufreq_remove_dev_finish(dev, NULL, frozen);
  1753. break;
  1754. case CPU_DOWN_FAILED:
  1755. __cpufreq_add_dev(dev, NULL, frozen);
  1756. break;
  1757. }
  1758. }
  1759. return NOTIFY_OK;
  1760. }
  1761. static struct notifier_block __refdata cpufreq_cpu_notifier = {
  1762. .notifier_call = cpufreq_cpu_callback,
  1763. };
  1764. /*********************************************************************
  1765. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1766. *********************************************************************/
  1767. /**
  1768. * cpufreq_register_driver - register a CPU Frequency driver
  1769. * @driver_data: A struct cpufreq_driver containing the values#
  1770. * submitted by the CPU Frequency driver.
  1771. *
  1772. * Registers a CPU Frequency driver to this core code. This code
  1773. * returns zero on success, -EBUSY when another driver got here first
  1774. * (and isn't unregistered in the meantime).
  1775. *
  1776. */
  1777. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  1778. {
  1779. unsigned long flags;
  1780. int ret;
  1781. if (cpufreq_disabled())
  1782. return -ENODEV;
  1783. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1784. !(driver_data->setpolicy || driver_data->target_index ||
  1785. driver_data->target))
  1786. return -EINVAL;
  1787. pr_debug("trying to register driver %s\n", driver_data->name);
  1788. if (driver_data->setpolicy)
  1789. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  1790. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1791. if (cpufreq_driver) {
  1792. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1793. return -EEXIST;
  1794. }
  1795. cpufreq_driver = driver_data;
  1796. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1797. ret = subsys_interface_register(&cpufreq_interface);
  1798. if (ret)
  1799. goto err_null_driver;
  1800. if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  1801. int i;
  1802. ret = -ENODEV;
  1803. /* check for at least one working CPU */
  1804. for (i = 0; i < nr_cpu_ids; i++)
  1805. if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
  1806. ret = 0;
  1807. break;
  1808. }
  1809. /* if all ->init() calls failed, unregister */
  1810. if (ret) {
  1811. pr_debug("no CPU initialized for driver %s\n",
  1812. driver_data->name);
  1813. goto err_if_unreg;
  1814. }
  1815. }
  1816. register_hotcpu_notifier(&cpufreq_cpu_notifier);
  1817. pr_debug("driver %s up and running\n", driver_data->name);
  1818. return 0;
  1819. err_if_unreg:
  1820. subsys_interface_unregister(&cpufreq_interface);
  1821. err_null_driver:
  1822. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1823. cpufreq_driver = NULL;
  1824. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1825. return ret;
  1826. }
  1827. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  1828. /**
  1829. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  1830. *
  1831. * Unregister the current CPUFreq driver. Only call this if you have
  1832. * the right to do so, i.e. if you have succeeded in initialising before!
  1833. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  1834. * currently not initialised.
  1835. */
  1836. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  1837. {
  1838. unsigned long flags;
  1839. if (!cpufreq_driver || (driver != cpufreq_driver))
  1840. return -EINVAL;
  1841. pr_debug("unregistering driver %s\n", driver->name);
  1842. subsys_interface_unregister(&cpufreq_interface);
  1843. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  1844. down_write(&cpufreq_rwsem);
  1845. write_lock_irqsave(&cpufreq_driver_lock, flags);
  1846. cpufreq_driver = NULL;
  1847. write_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1848. up_write(&cpufreq_rwsem);
  1849. return 0;
  1850. }
  1851. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  1852. static int __init cpufreq_core_init(void)
  1853. {
  1854. if (cpufreq_disabled())
  1855. return -ENODEV;
  1856. cpufreq_global_kobject = kobject_create();
  1857. BUG_ON(!cpufreq_global_kobject);
  1858. register_syscore_ops(&cpufreq_syscore_ops);
  1859. return 0;
  1860. }
  1861. core_initcall(cpufreq_core_init);