cpufreq.c 49 KB

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