cpufreq.c 53 KB

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