cpufreq.c 61 KB

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