cpufreq.c 53 KB

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