cpufreq.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  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. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/notifier.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/device.h>
  26. #include <linux/slab.h>
  27. #include <linux/cpu.h>
  28. #include <linux/completion.h>
  29. #include <linux/mutex.h>
  30. #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
  31. "cpufreq-core", msg)
  32. /**
  33. * The "cpufreq driver" - the arch- or hardware-dependent low
  34. * level driver of CPUFreq support, and its spinlock. This lock
  35. * also protects the cpufreq_cpu_data array.
  36. */
  37. static struct cpufreq_driver *cpufreq_driver;
  38. static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
  39. #ifdef CONFIG_HOTPLUG_CPU
  40. /* This one keeps track of the previously set governor of a removed CPU */
  41. static struct cpufreq_governor *cpufreq_cpu_governor[NR_CPUS];
  42. #endif
  43. static DEFINE_SPINLOCK(cpufreq_driver_lock);
  44. /*
  45. * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
  46. * all cpufreq/hotplug/workqueue/etc related lock issues.
  47. *
  48. * The rules for this semaphore:
  49. * - Any routine that wants to read from the policy structure will
  50. * do a down_read on this semaphore.
  51. * - Any routine that will write to the policy structure and/or may take away
  52. * the policy altogether (eg. CPU hotplug), will hold this lock in write
  53. * mode before doing so.
  54. *
  55. * Additional rules:
  56. * - All holders of the lock should check to make sure that the CPU they
  57. * are concerned with are online after they get the lock.
  58. * - Governor routines that can be called in cpufreq hotplug path should not
  59. * take this sem as top level hotplug notifier handler takes this.
  60. */
  61. static DEFINE_PER_CPU(int, policy_cpu);
  62. static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
  63. #define lock_policy_rwsem(mode, cpu) \
  64. int lock_policy_rwsem_##mode \
  65. (int cpu) \
  66. { \
  67. int policy_cpu = per_cpu(policy_cpu, cpu); \
  68. BUG_ON(policy_cpu == -1); \
  69. down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  70. if (unlikely(!cpu_online(cpu))) { \
  71. up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
  72. return -1; \
  73. } \
  74. \
  75. return 0; \
  76. }
  77. lock_policy_rwsem(read, cpu);
  78. EXPORT_SYMBOL_GPL(lock_policy_rwsem_read);
  79. lock_policy_rwsem(write, cpu);
  80. EXPORT_SYMBOL_GPL(lock_policy_rwsem_write);
  81. void unlock_policy_rwsem_read(int cpu)
  82. {
  83. int policy_cpu = per_cpu(policy_cpu, cpu);
  84. BUG_ON(policy_cpu == -1);
  85. up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
  86. }
  87. EXPORT_SYMBOL_GPL(unlock_policy_rwsem_read);
  88. void unlock_policy_rwsem_write(int cpu)
  89. {
  90. int policy_cpu = per_cpu(policy_cpu, cpu);
  91. BUG_ON(policy_cpu == -1);
  92. up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
  93. }
  94. EXPORT_SYMBOL_GPL(unlock_policy_rwsem_write);
  95. /* internal prototypes */
  96. static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
  97. static unsigned int __cpufreq_get(unsigned int cpu);
  98. static void handle_update(struct work_struct *work);
  99. /**
  100. * Two notifier lists: the "policy" list is involved in the
  101. * validation process for a new CPU frequency policy; the
  102. * "transition" list for kernel code that needs to handle
  103. * changes to devices when the CPU clock speed changes.
  104. * The mutex locks both lists.
  105. */
  106. static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
  107. static struct srcu_notifier_head cpufreq_transition_notifier_list;
  108. static int __init init_cpufreq_transition_notifier_list(void)
  109. {
  110. srcu_init_notifier_head(&cpufreq_transition_notifier_list);
  111. return 0;
  112. }
  113. pure_initcall(init_cpufreq_transition_notifier_list);
  114. static LIST_HEAD(cpufreq_governor_list);
  115. static DEFINE_MUTEX (cpufreq_governor_mutex);
  116. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  117. {
  118. struct cpufreq_policy *data;
  119. unsigned long flags;
  120. if (cpu >= NR_CPUS)
  121. goto err_out;
  122. /* get the cpufreq driver */
  123. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  124. if (!cpufreq_driver)
  125. goto err_out_unlock;
  126. if (!try_module_get(cpufreq_driver->owner))
  127. goto err_out_unlock;
  128. /* get the CPU */
  129. data = cpufreq_cpu_data[cpu];
  130. if (!data)
  131. goto err_out_put_module;
  132. if (!kobject_get(&data->kobj))
  133. goto err_out_put_module;
  134. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  135. return data;
  136. err_out_put_module:
  137. module_put(cpufreq_driver->owner);
  138. err_out_unlock:
  139. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  140. err_out:
  141. return NULL;
  142. }
  143. EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
  144. void cpufreq_cpu_put(struct cpufreq_policy *data)
  145. {
  146. kobject_put(&data->kobj);
  147. module_put(cpufreq_driver->owner);
  148. }
  149. EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
  150. /*********************************************************************
  151. * UNIFIED DEBUG HELPERS *
  152. *********************************************************************/
  153. #ifdef CONFIG_CPU_FREQ_DEBUG
  154. /* what part(s) of the CPUfreq subsystem are debugged? */
  155. static unsigned int debug;
  156. /* is the debug output ratelimit'ed using printk_ratelimit? User can
  157. * set or modify this value.
  158. */
  159. static unsigned int debug_ratelimit = 1;
  160. /* is the printk_ratelimit'ing enabled? It's enabled after a successful
  161. * loading of a cpufreq driver, temporarily disabled when a new policy
  162. * is set, and disabled upon cpufreq driver removal
  163. */
  164. static unsigned int disable_ratelimit = 1;
  165. static DEFINE_SPINLOCK(disable_ratelimit_lock);
  166. static void cpufreq_debug_enable_ratelimit(void)
  167. {
  168. unsigned long flags;
  169. spin_lock_irqsave(&disable_ratelimit_lock, flags);
  170. if (disable_ratelimit)
  171. disable_ratelimit--;
  172. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  173. }
  174. static void cpufreq_debug_disable_ratelimit(void)
  175. {
  176. unsigned long flags;
  177. spin_lock_irqsave(&disable_ratelimit_lock, flags);
  178. disable_ratelimit++;
  179. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  180. }
  181. void cpufreq_debug_printk(unsigned int type, const char *prefix,
  182. const char *fmt, ...)
  183. {
  184. char s[256];
  185. va_list args;
  186. unsigned int len;
  187. unsigned long flags;
  188. WARN_ON(!prefix);
  189. if (type & debug) {
  190. spin_lock_irqsave(&disable_ratelimit_lock, flags);
  191. if (!disable_ratelimit && debug_ratelimit
  192. && !printk_ratelimit()) {
  193. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  194. return;
  195. }
  196. spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
  197. len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
  198. va_start(args, fmt);
  199. len += vsnprintf(&s[len], (256 - len), fmt, args);
  200. va_end(args);
  201. printk(s);
  202. WARN_ON(len < 5);
  203. }
  204. }
  205. EXPORT_SYMBOL(cpufreq_debug_printk);
  206. module_param(debug, uint, 0644);
  207. MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
  208. " 2 to debug drivers, and 4 to debug governors.");
  209. module_param(debug_ratelimit, uint, 0644);
  210. MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
  211. " set to 0 to disable ratelimiting.");
  212. #else /* !CONFIG_CPU_FREQ_DEBUG */
  213. static inline void cpufreq_debug_enable_ratelimit(void) { return; }
  214. static inline void cpufreq_debug_disable_ratelimit(void) { return; }
  215. #endif /* CONFIG_CPU_FREQ_DEBUG */
  216. /*********************************************************************
  217. * EXTERNALLY AFFECTING FREQUENCY CHANGES *
  218. *********************************************************************/
  219. /**
  220. * adjust_jiffies - adjust the system "loops_per_jiffy"
  221. *
  222. * This function alters the system "loops_per_jiffy" for the clock
  223. * speed change. Note that loops_per_jiffy cannot be updated on SMP
  224. * systems as each CPU might be scaled differently. So, use the arch
  225. * per-CPU loops_per_jiffy value wherever possible.
  226. */
  227. #ifndef CONFIG_SMP
  228. static unsigned long l_p_j_ref;
  229. static unsigned int l_p_j_ref_freq;
  230. static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  231. {
  232. if (ci->flags & CPUFREQ_CONST_LOOPS)
  233. return;
  234. if (!l_p_j_ref_freq) {
  235. l_p_j_ref = loops_per_jiffy;
  236. l_p_j_ref_freq = ci->old;
  237. dprintk("saving %lu as reference value for loops_per_jiffy;"
  238. "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
  239. }
  240. if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
  241. (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
  242. (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
  243. loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
  244. ci->new);
  245. dprintk("scaling loops_per_jiffy to %lu"
  246. "for frequency %u kHz\n", loops_per_jiffy, ci->new);
  247. }
  248. }
  249. #else
  250. static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
  251. {
  252. return;
  253. }
  254. #endif
  255. /**
  256. * cpufreq_notify_transition - call notifier chain and adjust_jiffies
  257. * on frequency transition.
  258. *
  259. * This function calls the transition notifiers and the "adjust_jiffies"
  260. * function. It is called twice on all CPU frequency changes that have
  261. * external effects.
  262. */
  263. void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
  264. {
  265. struct cpufreq_policy *policy;
  266. BUG_ON(irqs_disabled());
  267. freqs->flags = cpufreq_driver->flags;
  268. dprintk("notification %u of frequency transition to %u kHz\n",
  269. state, freqs->new);
  270. policy = cpufreq_cpu_data[freqs->cpu];
  271. switch (state) {
  272. case CPUFREQ_PRECHANGE:
  273. /* detect if the driver reported a value as "old frequency"
  274. * which is not equal to what the cpufreq core thinks is
  275. * "old frequency".
  276. */
  277. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  278. if ((policy) && (policy->cpu == freqs->cpu) &&
  279. (policy->cur) && (policy->cur != freqs->old)) {
  280. dprintk("Warning: CPU frequency is"
  281. " %u, cpufreq assumed %u kHz.\n",
  282. freqs->old, policy->cur);
  283. freqs->old = policy->cur;
  284. }
  285. }
  286. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  287. CPUFREQ_PRECHANGE, freqs);
  288. adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
  289. break;
  290. case CPUFREQ_POSTCHANGE:
  291. adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
  292. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  293. CPUFREQ_POSTCHANGE, freqs);
  294. if (likely(policy) && likely(policy->cpu == freqs->cpu))
  295. policy->cur = freqs->new;
  296. break;
  297. }
  298. }
  299. EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
  300. /*********************************************************************
  301. * SYSFS INTERFACE *
  302. *********************************************************************/
  303. static struct cpufreq_governor *__find_governor(const char *str_governor)
  304. {
  305. struct cpufreq_governor *t;
  306. list_for_each_entry(t, &cpufreq_governor_list, governor_list)
  307. if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
  308. return t;
  309. return NULL;
  310. }
  311. /**
  312. * cpufreq_parse_governor - parse a governor string
  313. */
  314. static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
  315. struct cpufreq_governor **governor)
  316. {
  317. int err = -EINVAL;
  318. if (!cpufreq_driver)
  319. goto out;
  320. if (cpufreq_driver->setpolicy) {
  321. if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
  322. *policy = CPUFREQ_POLICY_PERFORMANCE;
  323. err = 0;
  324. } else if (!strnicmp(str_governor, "powersave",
  325. CPUFREQ_NAME_LEN)) {
  326. *policy = CPUFREQ_POLICY_POWERSAVE;
  327. err = 0;
  328. }
  329. } else if (cpufreq_driver->target) {
  330. struct cpufreq_governor *t;
  331. mutex_lock(&cpufreq_governor_mutex);
  332. t = __find_governor(str_governor);
  333. if (t == NULL) {
  334. char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
  335. str_governor);
  336. if (name) {
  337. int ret;
  338. mutex_unlock(&cpufreq_governor_mutex);
  339. ret = request_module(name);
  340. mutex_lock(&cpufreq_governor_mutex);
  341. if (ret == 0)
  342. t = __find_governor(str_governor);
  343. }
  344. kfree(name);
  345. }
  346. if (t != NULL) {
  347. *governor = t;
  348. err = 0;
  349. }
  350. mutex_unlock(&cpufreq_governor_mutex);
  351. }
  352. out:
  353. return err;
  354. }
  355. /* drivers/base/cpu.c */
  356. extern struct sysdev_class cpu_sysdev_class;
  357. /**
  358. * cpufreq_per_cpu_attr_read() / show_##file_name() -
  359. * print out cpufreq information
  360. *
  361. * Write out information from cpufreq_driver->policy[cpu]; object must be
  362. * "unsigned int".
  363. */
  364. #define show_one(file_name, object) \
  365. static ssize_t show_##file_name \
  366. (struct cpufreq_policy * policy, char *buf) \
  367. { \
  368. return sprintf (buf, "%u\n", policy->object); \
  369. }
  370. show_one(cpuinfo_min_freq, cpuinfo.min_freq);
  371. show_one(cpuinfo_max_freq, cpuinfo.max_freq);
  372. show_one(scaling_min_freq, min);
  373. show_one(scaling_max_freq, max);
  374. show_one(scaling_cur_freq, cur);
  375. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  376. struct cpufreq_policy *policy);
  377. /**
  378. * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
  379. */
  380. #define store_one(file_name, object) \
  381. static ssize_t store_##file_name \
  382. (struct cpufreq_policy * policy, const char *buf, size_t count) \
  383. { \
  384. unsigned int ret = -EINVAL; \
  385. struct cpufreq_policy new_policy; \
  386. \
  387. ret = cpufreq_get_policy(&new_policy, policy->cpu); \
  388. if (ret) \
  389. return -EINVAL; \
  390. \
  391. ret = sscanf (buf, "%u", &new_policy.object); \
  392. if (ret != 1) \
  393. return -EINVAL; \
  394. \
  395. ret = __cpufreq_set_policy(policy, &new_policy); \
  396. policy->user_policy.object = policy->object; \
  397. \
  398. return ret ? ret : count; \
  399. }
  400. store_one(scaling_min_freq,min);
  401. store_one(scaling_max_freq,max);
  402. /**
  403. * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
  404. */
  405. static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy,
  406. char *buf)
  407. {
  408. unsigned int cur_freq = __cpufreq_get(policy->cpu);
  409. if (!cur_freq)
  410. return sprintf(buf, "<unknown>");
  411. return sprintf(buf, "%u\n", cur_freq);
  412. }
  413. /**
  414. * show_scaling_governor - show the current policy for the specified CPU
  415. */
  416. static ssize_t show_scaling_governor (struct cpufreq_policy * policy,
  417. char *buf)
  418. {
  419. if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
  420. return sprintf(buf, "powersave\n");
  421. else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
  422. return sprintf(buf, "performance\n");
  423. else if (policy->governor)
  424. return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
  425. return -EINVAL;
  426. }
  427. /**
  428. * store_scaling_governor - store policy for the specified CPU
  429. */
  430. static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
  431. const char *buf, size_t count)
  432. {
  433. unsigned int ret = -EINVAL;
  434. char str_governor[16];
  435. struct cpufreq_policy new_policy;
  436. ret = cpufreq_get_policy(&new_policy, policy->cpu);
  437. if (ret)
  438. return ret;
  439. ret = sscanf (buf, "%15s", str_governor);
  440. if (ret != 1)
  441. return -EINVAL;
  442. if (cpufreq_parse_governor(str_governor, &new_policy.policy,
  443. &new_policy.governor))
  444. return -EINVAL;
  445. /* Do not use cpufreq_set_policy here or the user_policy.max
  446. will be wrongly overridden */
  447. ret = __cpufreq_set_policy(policy, &new_policy);
  448. policy->user_policy.policy = policy->policy;
  449. policy->user_policy.governor = policy->governor;
  450. if (ret)
  451. return ret;
  452. else
  453. return count;
  454. }
  455. /**
  456. * show_scaling_driver - show the cpufreq driver currently loaded
  457. */
  458. static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
  459. {
  460. return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
  461. }
  462. /**
  463. * show_scaling_available_governors - show the available CPUfreq governors
  464. */
  465. static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy,
  466. char *buf)
  467. {
  468. ssize_t i = 0;
  469. struct cpufreq_governor *t;
  470. if (!cpufreq_driver->target) {
  471. i += sprintf(buf, "performance powersave");
  472. goto out;
  473. }
  474. list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
  475. if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
  476. goto out;
  477. i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
  478. }
  479. out:
  480. i += sprintf(&buf[i], "\n");
  481. return i;
  482. }
  483. /**
  484. * show_affected_cpus - show the CPUs affected by each transition
  485. */
  486. static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
  487. {
  488. ssize_t i = 0;
  489. unsigned int cpu;
  490. for_each_cpu_mask(cpu, policy->cpus) {
  491. if (i)
  492. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
  493. i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
  494. if (i >= (PAGE_SIZE - 5))
  495. break;
  496. }
  497. i += sprintf(&buf[i], "\n");
  498. return i;
  499. }
  500. #define define_one_ro(_name) \
  501. static struct freq_attr _name = \
  502. __ATTR(_name, 0444, show_##_name, NULL)
  503. #define define_one_ro0400(_name) \
  504. static struct freq_attr _name = \
  505. __ATTR(_name, 0400, show_##_name, NULL)
  506. #define define_one_rw(_name) \
  507. static struct freq_attr _name = \
  508. __ATTR(_name, 0644, show_##_name, store_##_name)
  509. define_one_ro0400(cpuinfo_cur_freq);
  510. define_one_ro(cpuinfo_min_freq);
  511. define_one_ro(cpuinfo_max_freq);
  512. define_one_ro(scaling_available_governors);
  513. define_one_ro(scaling_driver);
  514. define_one_ro(scaling_cur_freq);
  515. define_one_ro(affected_cpus);
  516. define_one_rw(scaling_min_freq);
  517. define_one_rw(scaling_max_freq);
  518. define_one_rw(scaling_governor);
  519. static struct attribute * default_attrs[] = {
  520. &cpuinfo_min_freq.attr,
  521. &cpuinfo_max_freq.attr,
  522. &scaling_min_freq.attr,
  523. &scaling_max_freq.attr,
  524. &affected_cpus.attr,
  525. &scaling_governor.attr,
  526. &scaling_driver.attr,
  527. &scaling_available_governors.attr,
  528. NULL
  529. };
  530. #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
  531. #define to_attr(a) container_of(a,struct freq_attr,attr)
  532. static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
  533. {
  534. struct cpufreq_policy * policy = to_policy(kobj);
  535. struct freq_attr * fattr = to_attr(attr);
  536. ssize_t ret;
  537. policy = cpufreq_cpu_get(policy->cpu);
  538. if (!policy)
  539. return -EINVAL;
  540. if (lock_policy_rwsem_read(policy->cpu) < 0)
  541. return -EINVAL;
  542. if (fattr->show)
  543. ret = fattr->show(policy, buf);
  544. else
  545. ret = -EIO;
  546. unlock_policy_rwsem_read(policy->cpu);
  547. cpufreq_cpu_put(policy);
  548. return ret;
  549. }
  550. static ssize_t store(struct kobject * kobj, struct attribute * attr,
  551. const char * buf, size_t count)
  552. {
  553. struct cpufreq_policy * policy = to_policy(kobj);
  554. struct freq_attr * fattr = to_attr(attr);
  555. ssize_t ret;
  556. policy = cpufreq_cpu_get(policy->cpu);
  557. if (!policy)
  558. return -EINVAL;
  559. if (lock_policy_rwsem_write(policy->cpu) < 0)
  560. return -EINVAL;
  561. if (fattr->store)
  562. ret = fattr->store(policy, buf, count);
  563. else
  564. ret = -EIO;
  565. unlock_policy_rwsem_write(policy->cpu);
  566. cpufreq_cpu_put(policy);
  567. return ret;
  568. }
  569. static void cpufreq_sysfs_release(struct kobject * kobj)
  570. {
  571. struct cpufreq_policy * policy = to_policy(kobj);
  572. dprintk("last reference is dropped\n");
  573. complete(&policy->kobj_unregister);
  574. }
  575. static struct sysfs_ops sysfs_ops = {
  576. .show = show,
  577. .store = store,
  578. };
  579. static struct kobj_type ktype_cpufreq = {
  580. .sysfs_ops = &sysfs_ops,
  581. .default_attrs = default_attrs,
  582. .release = cpufreq_sysfs_release,
  583. };
  584. /**
  585. * cpufreq_add_dev - add a CPU device
  586. *
  587. * Adds the cpufreq interface for a CPU device.
  588. */
  589. static int cpufreq_add_dev (struct sys_device * sys_dev)
  590. {
  591. unsigned int cpu = sys_dev->id;
  592. int ret = 0;
  593. struct cpufreq_policy new_policy;
  594. struct cpufreq_policy *policy;
  595. struct freq_attr **drv_attr;
  596. struct sys_device *cpu_sys_dev;
  597. unsigned long flags;
  598. unsigned int j;
  599. #ifdef CONFIG_SMP
  600. struct cpufreq_policy *managed_policy;
  601. #endif
  602. if (cpu_is_offline(cpu))
  603. return 0;
  604. cpufreq_debug_disable_ratelimit();
  605. dprintk("adding CPU %u\n", cpu);
  606. #ifdef CONFIG_SMP
  607. /* check whether a different CPU already registered this
  608. * CPU because it is in the same boat. */
  609. policy = cpufreq_cpu_get(cpu);
  610. if (unlikely(policy)) {
  611. cpufreq_cpu_put(policy);
  612. cpufreq_debug_enable_ratelimit();
  613. return 0;
  614. }
  615. #endif
  616. if (!try_module_get(cpufreq_driver->owner)) {
  617. ret = -EINVAL;
  618. goto module_out;
  619. }
  620. policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
  621. if (!policy) {
  622. ret = -ENOMEM;
  623. goto nomem_out;
  624. }
  625. policy->cpu = cpu;
  626. policy->cpus = cpumask_of_cpu(cpu);
  627. /* Initially set CPU itself as the policy_cpu */
  628. per_cpu(policy_cpu, cpu) = cpu;
  629. lock_policy_rwsem_write(cpu);
  630. init_completion(&policy->kobj_unregister);
  631. INIT_WORK(&policy->update, handle_update);
  632. /* Set governor before ->init, so that driver could check it */
  633. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  634. /* call driver. From then on the cpufreq must be able
  635. * to accept all calls to ->verify and ->setpolicy for this CPU
  636. */
  637. ret = cpufreq_driver->init(policy);
  638. if (ret) {
  639. dprintk("initialization failed\n");
  640. unlock_policy_rwsem_write(cpu);
  641. goto err_out;
  642. }
  643. policy->user_policy.min = policy->cpuinfo.min_freq;
  644. policy->user_policy.max = policy->cpuinfo.max_freq;
  645. #ifdef CONFIG_SMP
  646. #ifdef CONFIG_HOTPLUG_CPU
  647. if (cpufreq_cpu_governor[cpu]){
  648. policy->governor = cpufreq_cpu_governor[cpu];
  649. dprintk("Restoring governor %s for cpu %d\n",
  650. policy->governor->name, cpu);
  651. }
  652. #endif
  653. for_each_cpu_mask(j, policy->cpus) {
  654. if (cpu == j)
  655. continue;
  656. /* check for existing affected CPUs. They may not be aware
  657. * of it due to CPU Hotplug.
  658. */
  659. managed_policy = cpufreq_cpu_get(j);
  660. if (unlikely(managed_policy)) {
  661. /* Set proper policy_cpu */
  662. unlock_policy_rwsem_write(cpu);
  663. per_cpu(policy_cpu, cpu) = managed_policy->cpu;
  664. if (lock_policy_rwsem_write(cpu) < 0)
  665. goto err_out_driver_exit;
  666. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  667. managed_policy->cpus = policy->cpus;
  668. cpufreq_cpu_data[cpu] = managed_policy;
  669. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  670. dprintk("CPU already managed, adding link\n");
  671. ret = sysfs_create_link(&sys_dev->kobj,
  672. &managed_policy->kobj,
  673. "cpufreq");
  674. if (ret) {
  675. unlock_policy_rwsem_write(cpu);
  676. goto err_out_driver_exit;
  677. }
  678. cpufreq_debug_enable_ratelimit();
  679. ret = 0;
  680. unlock_policy_rwsem_write(cpu);
  681. goto err_out_driver_exit; /* call driver->exit() */
  682. }
  683. }
  684. #endif
  685. memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
  686. /* prepare interface data */
  687. policy->kobj.parent = &sys_dev->kobj;
  688. policy->kobj.ktype = &ktype_cpufreq;
  689. strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
  690. ret = kobject_register(&policy->kobj);
  691. if (ret) {
  692. unlock_policy_rwsem_write(cpu);
  693. goto err_out_driver_exit;
  694. }
  695. /* set up files for this cpu device */
  696. drv_attr = cpufreq_driver->attr;
  697. while ((drv_attr) && (*drv_attr)) {
  698. ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  699. if (ret)
  700. goto err_out_driver_exit;
  701. drv_attr++;
  702. }
  703. if (cpufreq_driver->get){
  704. ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  705. if (ret)
  706. goto err_out_driver_exit;
  707. }
  708. if (cpufreq_driver->target){
  709. ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  710. if (ret)
  711. goto err_out_driver_exit;
  712. }
  713. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  714. for_each_cpu_mask(j, policy->cpus) {
  715. cpufreq_cpu_data[j] = policy;
  716. per_cpu(policy_cpu, j) = policy->cpu;
  717. }
  718. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  719. /* symlink affected CPUs */
  720. for_each_cpu_mask(j, policy->cpus) {
  721. if (j == cpu)
  722. continue;
  723. if (!cpu_online(j))
  724. continue;
  725. dprintk("CPU %u already managed, adding link\n", j);
  726. cpufreq_cpu_get(cpu);
  727. cpu_sys_dev = get_cpu_sysdev(j);
  728. ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
  729. "cpufreq");
  730. if (ret) {
  731. unlock_policy_rwsem_write(cpu);
  732. goto err_out_unregister;
  733. }
  734. }
  735. policy->governor = NULL; /* to assure that the starting sequence is
  736. * run in cpufreq_set_policy */
  737. /* set default policy */
  738. ret = __cpufreq_set_policy(policy, &new_policy);
  739. policy->user_policy.policy = policy->policy;
  740. policy->user_policy.governor = policy->governor;
  741. unlock_policy_rwsem_write(cpu);
  742. if (ret) {
  743. dprintk("setting policy failed\n");
  744. goto err_out_unregister;
  745. }
  746. module_put(cpufreq_driver->owner);
  747. dprintk("initialization complete\n");
  748. cpufreq_debug_enable_ratelimit();
  749. return 0;
  750. err_out_unregister:
  751. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  752. for_each_cpu_mask(j, policy->cpus)
  753. cpufreq_cpu_data[j] = NULL;
  754. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  755. kobject_unregister(&policy->kobj);
  756. wait_for_completion(&policy->kobj_unregister);
  757. err_out_driver_exit:
  758. if (cpufreq_driver->exit)
  759. cpufreq_driver->exit(policy);
  760. err_out:
  761. kfree(policy);
  762. nomem_out:
  763. module_put(cpufreq_driver->owner);
  764. module_out:
  765. cpufreq_debug_enable_ratelimit();
  766. return ret;
  767. }
  768. /**
  769. * __cpufreq_remove_dev - remove a CPU device
  770. *
  771. * Removes the cpufreq interface for a CPU device.
  772. * Caller should already have policy_rwsem in write mode for this CPU.
  773. * This routine frees the rwsem before returning.
  774. */
  775. static int __cpufreq_remove_dev (struct sys_device * sys_dev)
  776. {
  777. unsigned int cpu = sys_dev->id;
  778. unsigned long flags;
  779. struct cpufreq_policy *data;
  780. #ifdef CONFIG_SMP
  781. struct sys_device *cpu_sys_dev;
  782. unsigned int j;
  783. #endif
  784. cpufreq_debug_disable_ratelimit();
  785. dprintk("unregistering CPU %u\n", cpu);
  786. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  787. data = cpufreq_cpu_data[cpu];
  788. if (!data) {
  789. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  790. cpufreq_debug_enable_ratelimit();
  791. unlock_policy_rwsem_write(cpu);
  792. return -EINVAL;
  793. }
  794. cpufreq_cpu_data[cpu] = NULL;
  795. #ifdef CONFIG_SMP
  796. /* if this isn't the CPU which is the parent of the kobj, we
  797. * only need to unlink, put and exit
  798. */
  799. if (unlikely(cpu != data->cpu)) {
  800. dprintk("removing link\n");
  801. cpu_clear(cpu, data->cpus);
  802. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  803. sysfs_remove_link(&sys_dev->kobj, "cpufreq");
  804. cpufreq_cpu_put(data);
  805. cpufreq_debug_enable_ratelimit();
  806. unlock_policy_rwsem_write(cpu);
  807. return 0;
  808. }
  809. #endif
  810. if (!kobject_get(&data->kobj)) {
  811. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  812. cpufreq_debug_enable_ratelimit();
  813. unlock_policy_rwsem_write(cpu);
  814. return -EFAULT;
  815. }
  816. #ifdef CONFIG_SMP
  817. #ifdef CONFIG_HOTPLUG_CPU
  818. cpufreq_cpu_governor[cpu] = data->governor;
  819. #endif
  820. /* if we have other CPUs still registered, we need to unlink them,
  821. * or else wait_for_completion below will lock up. Clean the
  822. * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
  823. * links afterwards.
  824. */
  825. if (unlikely(cpus_weight(data->cpus) > 1)) {
  826. for_each_cpu_mask(j, data->cpus) {
  827. if (j == cpu)
  828. continue;
  829. cpufreq_cpu_data[j] = NULL;
  830. }
  831. }
  832. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  833. if (unlikely(cpus_weight(data->cpus) > 1)) {
  834. for_each_cpu_mask(j, data->cpus) {
  835. if (j == cpu)
  836. continue;
  837. dprintk("removing link for cpu %u\n", j);
  838. #ifdef CONFIG_HOTPLUG_CPU
  839. cpufreq_cpu_governor[j] = data->governor;
  840. #endif
  841. cpu_sys_dev = get_cpu_sysdev(j);
  842. sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
  843. cpufreq_cpu_put(data);
  844. }
  845. }
  846. #else
  847. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  848. #endif
  849. if (cpufreq_driver->target)
  850. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  851. unlock_policy_rwsem_write(cpu);
  852. kobject_unregister(&data->kobj);
  853. kobject_put(&data->kobj);
  854. /* we need to make sure that the underlying kobj is actually
  855. * not referenced anymore by anybody before we proceed with
  856. * unloading.
  857. */
  858. dprintk("waiting for dropping of refcount\n");
  859. wait_for_completion(&data->kobj_unregister);
  860. dprintk("wait complete\n");
  861. if (cpufreq_driver->exit)
  862. cpufreq_driver->exit(data);
  863. kfree(data);
  864. cpufreq_debug_enable_ratelimit();
  865. return 0;
  866. }
  867. static int cpufreq_remove_dev (struct sys_device * sys_dev)
  868. {
  869. unsigned int cpu = sys_dev->id;
  870. int retval;
  871. if (cpu_is_offline(cpu))
  872. return 0;
  873. if (unlikely(lock_policy_rwsem_write(cpu)))
  874. BUG();
  875. retval = __cpufreq_remove_dev(sys_dev);
  876. return retval;
  877. }
  878. static void handle_update(struct work_struct *work)
  879. {
  880. struct cpufreq_policy *policy =
  881. container_of(work, struct cpufreq_policy, update);
  882. unsigned int cpu = policy->cpu;
  883. dprintk("handle_update for cpu %u called\n", cpu);
  884. cpufreq_update_policy(cpu);
  885. }
  886. /**
  887. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
  888. * @cpu: cpu number
  889. * @old_freq: CPU frequency the kernel thinks the CPU runs at
  890. * @new_freq: CPU frequency the CPU actually runs at
  891. *
  892. * We adjust to current frequency first, and need to clean up later. So either call
  893. * to cpufreq_update_policy() or schedule handle_update()).
  894. */
  895. static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
  896. unsigned int new_freq)
  897. {
  898. struct cpufreq_freqs freqs;
  899. dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
  900. "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
  901. freqs.cpu = cpu;
  902. freqs.old = old_freq;
  903. freqs.new = new_freq;
  904. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  905. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  906. }
  907. /**
  908. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  909. * @cpu: CPU number
  910. *
  911. * This is the last known freq, without actually getting it from the driver.
  912. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  913. */
  914. unsigned int cpufreq_quick_get(unsigned int cpu)
  915. {
  916. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  917. unsigned int ret_freq = 0;
  918. if (policy) {
  919. if (unlikely(lock_policy_rwsem_read(cpu)))
  920. return ret_freq;
  921. ret_freq = policy->cur;
  922. unlock_policy_rwsem_read(cpu);
  923. cpufreq_cpu_put(policy);
  924. }
  925. return (ret_freq);
  926. }
  927. EXPORT_SYMBOL(cpufreq_quick_get);
  928. static unsigned int __cpufreq_get(unsigned int cpu)
  929. {
  930. struct cpufreq_policy *policy = cpufreq_cpu_data[cpu];
  931. unsigned int ret_freq = 0;
  932. if (!cpufreq_driver->get)
  933. return (ret_freq);
  934. ret_freq = cpufreq_driver->get(cpu);
  935. if (ret_freq && policy->cur &&
  936. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  937. /* verify no discrepancy between actual and
  938. saved value exists */
  939. if (unlikely(ret_freq != policy->cur)) {
  940. cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
  941. schedule_work(&policy->update);
  942. }
  943. }
  944. return (ret_freq);
  945. }
  946. /**
  947. * cpufreq_get - get the current CPU frequency (in kHz)
  948. * @cpu: CPU number
  949. *
  950. * Get the CPU current (static) CPU frequency
  951. */
  952. unsigned int cpufreq_get(unsigned int cpu)
  953. {
  954. unsigned int ret_freq = 0;
  955. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  956. if (!policy)
  957. goto out;
  958. if (unlikely(lock_policy_rwsem_read(cpu)))
  959. goto out_policy;
  960. ret_freq = __cpufreq_get(cpu);
  961. unlock_policy_rwsem_read(cpu);
  962. out_policy:
  963. cpufreq_cpu_put(policy);
  964. out:
  965. return (ret_freq);
  966. }
  967. EXPORT_SYMBOL(cpufreq_get);
  968. /**
  969. * cpufreq_suspend - let the low level driver prepare for suspend
  970. */
  971. static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
  972. {
  973. int cpu = sysdev->id;
  974. int ret = 0;
  975. unsigned int cur_freq = 0;
  976. struct cpufreq_policy *cpu_policy;
  977. dprintk("suspending cpu %u\n", cpu);
  978. if (!cpu_online(cpu))
  979. return 0;
  980. /* we may be lax here as interrupts are off. Nonetheless
  981. * we need to grab the correct cpu policy, as to check
  982. * whether we really run on this CPU.
  983. */
  984. cpu_policy = cpufreq_cpu_get(cpu);
  985. if (!cpu_policy)
  986. return -EINVAL;
  987. /* only handle each CPU group once */
  988. if (unlikely(cpu_policy->cpu != cpu)) {
  989. cpufreq_cpu_put(cpu_policy);
  990. return 0;
  991. }
  992. if (cpufreq_driver->suspend) {
  993. ret = cpufreq_driver->suspend(cpu_policy, pmsg);
  994. if (ret) {
  995. printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
  996. "step on CPU %u\n", cpu_policy->cpu);
  997. cpufreq_cpu_put(cpu_policy);
  998. return ret;
  999. }
  1000. }
  1001. if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
  1002. goto out;
  1003. if (cpufreq_driver->get)
  1004. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  1005. if (!cur_freq || !cpu_policy->cur) {
  1006. printk(KERN_ERR "cpufreq: suspend failed to assert current "
  1007. "frequency is what timing core thinks it is.\n");
  1008. goto out;
  1009. }
  1010. if (unlikely(cur_freq != cpu_policy->cur)) {
  1011. struct cpufreq_freqs freqs;
  1012. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  1013. dprintk("Warning: CPU frequency is %u, "
  1014. "cpufreq assumed %u kHz.\n",
  1015. cur_freq, cpu_policy->cur);
  1016. freqs.cpu = cpu;
  1017. freqs.old = cpu_policy->cur;
  1018. freqs.new = cur_freq;
  1019. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  1020. CPUFREQ_SUSPENDCHANGE, &freqs);
  1021. adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
  1022. cpu_policy->cur = cur_freq;
  1023. }
  1024. out:
  1025. cpufreq_cpu_put(cpu_policy);
  1026. return 0;
  1027. }
  1028. /**
  1029. * cpufreq_resume - restore proper CPU frequency handling after resume
  1030. *
  1031. * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
  1032. * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
  1033. * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
  1034. * restored.
  1035. */
  1036. static int cpufreq_resume(struct sys_device * sysdev)
  1037. {
  1038. int cpu = sysdev->id;
  1039. int ret = 0;
  1040. struct cpufreq_policy *cpu_policy;
  1041. dprintk("resuming cpu %u\n", cpu);
  1042. if (!cpu_online(cpu))
  1043. return 0;
  1044. /* we may be lax here as interrupts are off. Nonetheless
  1045. * we need to grab the correct cpu policy, as to check
  1046. * whether we really run on this CPU.
  1047. */
  1048. cpu_policy = cpufreq_cpu_get(cpu);
  1049. if (!cpu_policy)
  1050. return -EINVAL;
  1051. /* only handle each CPU group once */
  1052. if (unlikely(cpu_policy->cpu != cpu)) {
  1053. cpufreq_cpu_put(cpu_policy);
  1054. return 0;
  1055. }
  1056. if (cpufreq_driver->resume) {
  1057. ret = cpufreq_driver->resume(cpu_policy);
  1058. if (ret) {
  1059. printk(KERN_ERR "cpufreq: resume failed in ->resume "
  1060. "step on CPU %u\n", cpu_policy->cpu);
  1061. cpufreq_cpu_put(cpu_policy);
  1062. return ret;
  1063. }
  1064. }
  1065. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1066. unsigned int cur_freq = 0;
  1067. if (cpufreq_driver->get)
  1068. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  1069. if (!cur_freq || !cpu_policy->cur) {
  1070. printk(KERN_ERR "cpufreq: resume failed to assert "
  1071. "current frequency is what timing core "
  1072. "thinks it is.\n");
  1073. goto out;
  1074. }
  1075. if (unlikely(cur_freq != cpu_policy->cur)) {
  1076. struct cpufreq_freqs freqs;
  1077. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  1078. dprintk("Warning: CPU frequency"
  1079. "is %u, cpufreq assumed %u kHz.\n",
  1080. cur_freq, cpu_policy->cur);
  1081. freqs.cpu = cpu;
  1082. freqs.old = cpu_policy->cur;
  1083. freqs.new = cur_freq;
  1084. srcu_notifier_call_chain(
  1085. &cpufreq_transition_notifier_list,
  1086. CPUFREQ_RESUMECHANGE, &freqs);
  1087. adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
  1088. cpu_policy->cur = cur_freq;
  1089. }
  1090. }
  1091. out:
  1092. schedule_work(&cpu_policy->update);
  1093. cpufreq_cpu_put(cpu_policy);
  1094. return ret;
  1095. }
  1096. static struct sysdev_driver cpufreq_sysdev_driver = {
  1097. .add = cpufreq_add_dev,
  1098. .remove = cpufreq_remove_dev,
  1099. .suspend = cpufreq_suspend,
  1100. .resume = cpufreq_resume,
  1101. };
  1102. /*********************************************************************
  1103. * NOTIFIER LISTS INTERFACE *
  1104. *********************************************************************/
  1105. /**
  1106. * cpufreq_register_notifier - register a driver with cpufreq
  1107. * @nb: notifier function to register
  1108. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1109. *
  1110. * Add a driver to one of two lists: either a list of drivers that
  1111. * are notified about clock rate changes (once before and once after
  1112. * the transition), or a list of drivers that are notified about
  1113. * changes in cpufreq policy.
  1114. *
  1115. * This function may sleep, and has the same return conditions as
  1116. * blocking_notifier_chain_register.
  1117. */
  1118. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1119. {
  1120. int ret;
  1121. switch (list) {
  1122. case CPUFREQ_TRANSITION_NOTIFIER:
  1123. ret = srcu_notifier_chain_register(
  1124. &cpufreq_transition_notifier_list, nb);
  1125. break;
  1126. case CPUFREQ_POLICY_NOTIFIER:
  1127. ret = blocking_notifier_chain_register(
  1128. &cpufreq_policy_notifier_list, nb);
  1129. break;
  1130. default:
  1131. ret = -EINVAL;
  1132. }
  1133. return ret;
  1134. }
  1135. EXPORT_SYMBOL(cpufreq_register_notifier);
  1136. /**
  1137. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1138. * @nb: notifier block to be unregistered
  1139. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1140. *
  1141. * Remove a driver from the CPU frequency notifier list.
  1142. *
  1143. * This function may sleep, and has the same return conditions as
  1144. * blocking_notifier_chain_unregister.
  1145. */
  1146. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1147. {
  1148. int ret;
  1149. switch (list) {
  1150. case CPUFREQ_TRANSITION_NOTIFIER:
  1151. ret = srcu_notifier_chain_unregister(
  1152. &cpufreq_transition_notifier_list, nb);
  1153. break;
  1154. case CPUFREQ_POLICY_NOTIFIER:
  1155. ret = blocking_notifier_chain_unregister(
  1156. &cpufreq_policy_notifier_list, nb);
  1157. break;
  1158. default:
  1159. ret = -EINVAL;
  1160. }
  1161. return ret;
  1162. }
  1163. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1164. /*********************************************************************
  1165. * GOVERNORS *
  1166. *********************************************************************/
  1167. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1168. unsigned int target_freq,
  1169. unsigned int relation)
  1170. {
  1171. int retval = -EINVAL;
  1172. dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
  1173. target_freq, relation);
  1174. if (cpu_online(policy->cpu) && cpufreq_driver->target)
  1175. retval = cpufreq_driver->target(policy, target_freq, relation);
  1176. return retval;
  1177. }
  1178. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1179. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1180. unsigned int target_freq,
  1181. unsigned int relation)
  1182. {
  1183. int ret;
  1184. policy = cpufreq_cpu_get(policy->cpu);
  1185. if (!policy)
  1186. return -EINVAL;
  1187. if (unlikely(lock_policy_rwsem_write(policy->cpu)))
  1188. return -EINVAL;
  1189. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1190. unlock_policy_rwsem_write(policy->cpu);
  1191. cpufreq_cpu_put(policy);
  1192. return ret;
  1193. }
  1194. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1195. int __cpufreq_driver_getavg(struct cpufreq_policy *policy)
  1196. {
  1197. int ret = 0;
  1198. policy = cpufreq_cpu_get(policy->cpu);
  1199. if (!policy)
  1200. return -EINVAL;
  1201. if (cpu_online(policy->cpu) && cpufreq_driver->getavg)
  1202. ret = cpufreq_driver->getavg(policy->cpu);
  1203. cpufreq_cpu_put(policy);
  1204. return ret;
  1205. }
  1206. EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
  1207. /*
  1208. * when "event" is CPUFREQ_GOV_LIMITS
  1209. */
  1210. static int __cpufreq_governor(struct cpufreq_policy *policy,
  1211. unsigned int event)
  1212. {
  1213. int ret;
  1214. struct cpufreq_governor *gov = CPUFREQ_PERFORMANCE_GOVERNOR;
  1215. if (policy->governor->max_transition_latency &&
  1216. policy->cpuinfo.transition_latency >
  1217. policy->governor->max_transition_latency) {
  1218. printk(KERN_WARNING "%s governor failed, too long"
  1219. " transition latency of HW, fallback"
  1220. " to %s governor\n",
  1221. policy->governor->name,
  1222. gov->name);
  1223. policy->governor = gov;
  1224. }
  1225. if (!try_module_get(policy->governor->owner))
  1226. return -EINVAL;
  1227. dprintk("__cpufreq_governor for CPU %u, event %u\n",
  1228. policy->cpu, event);
  1229. ret = policy->governor->governor(policy, event);
  1230. /* we keep one module reference alive for
  1231. each CPU governed by this CPU */
  1232. if ((event != CPUFREQ_GOV_START) || ret)
  1233. module_put(policy->governor->owner);
  1234. if ((event == CPUFREQ_GOV_STOP) && !ret)
  1235. module_put(policy->governor->owner);
  1236. return ret;
  1237. }
  1238. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1239. {
  1240. int err;
  1241. if (!governor)
  1242. return -EINVAL;
  1243. mutex_lock(&cpufreq_governor_mutex);
  1244. err = -EBUSY;
  1245. if (__find_governor(governor->name) == NULL) {
  1246. err = 0;
  1247. list_add(&governor->governor_list, &cpufreq_governor_list);
  1248. }
  1249. mutex_unlock(&cpufreq_governor_mutex);
  1250. return err;
  1251. }
  1252. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1253. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1254. {
  1255. if (!governor)
  1256. return;
  1257. mutex_lock(&cpufreq_governor_mutex);
  1258. list_del(&governor->governor_list);
  1259. mutex_unlock(&cpufreq_governor_mutex);
  1260. return;
  1261. }
  1262. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1263. /*********************************************************************
  1264. * POLICY INTERFACE *
  1265. *********************************************************************/
  1266. /**
  1267. * cpufreq_get_policy - get the current cpufreq_policy
  1268. * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
  1269. *
  1270. * Reads the current cpufreq policy.
  1271. */
  1272. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1273. {
  1274. struct cpufreq_policy *cpu_policy;
  1275. if (!policy)
  1276. return -EINVAL;
  1277. cpu_policy = cpufreq_cpu_get(cpu);
  1278. if (!cpu_policy)
  1279. return -EINVAL;
  1280. memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
  1281. cpufreq_cpu_put(cpu_policy);
  1282. return 0;
  1283. }
  1284. EXPORT_SYMBOL(cpufreq_get_policy);
  1285. /*
  1286. * data : current policy.
  1287. * policy : policy to be set.
  1288. */
  1289. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  1290. struct cpufreq_policy *policy)
  1291. {
  1292. int ret = 0;
  1293. cpufreq_debug_disable_ratelimit();
  1294. dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
  1295. policy->min, policy->max);
  1296. memcpy(&policy->cpuinfo, &data->cpuinfo,
  1297. sizeof(struct cpufreq_cpuinfo));
  1298. if (policy->min > data->min && policy->min > policy->max) {
  1299. ret = -EINVAL;
  1300. goto error_out;
  1301. }
  1302. /* verify the cpu speed can be set within this limit */
  1303. ret = cpufreq_driver->verify(policy);
  1304. if (ret)
  1305. goto error_out;
  1306. /* adjust if necessary - all reasons */
  1307. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1308. CPUFREQ_ADJUST, policy);
  1309. /* adjust if necessary - hardware incompatibility*/
  1310. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1311. CPUFREQ_INCOMPATIBLE, policy);
  1312. /* verify the cpu speed can be set within this limit,
  1313. which might be different to the first one */
  1314. ret = cpufreq_driver->verify(policy);
  1315. if (ret)
  1316. goto error_out;
  1317. /* notification of the new policy */
  1318. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1319. CPUFREQ_NOTIFY, policy);
  1320. data->min = policy->min;
  1321. data->max = policy->max;
  1322. dprintk("new min and max freqs are %u - %u kHz\n",
  1323. data->min, data->max);
  1324. if (cpufreq_driver->setpolicy) {
  1325. data->policy = policy->policy;
  1326. dprintk("setting range\n");
  1327. ret = cpufreq_driver->setpolicy(policy);
  1328. } else {
  1329. if (policy->governor != data->governor) {
  1330. /* save old, working values */
  1331. struct cpufreq_governor *old_gov = data->governor;
  1332. dprintk("governor switch\n");
  1333. /* end old governor */
  1334. if (data->governor)
  1335. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  1336. /* start new governor */
  1337. data->governor = policy->governor;
  1338. if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
  1339. /* new governor failed, so re-start old one */
  1340. dprintk("starting governor %s failed\n",
  1341. data->governor->name);
  1342. if (old_gov) {
  1343. data->governor = old_gov;
  1344. __cpufreq_governor(data,
  1345. CPUFREQ_GOV_START);
  1346. }
  1347. ret = -EINVAL;
  1348. goto error_out;
  1349. }
  1350. /* might be a policy change, too, so fall through */
  1351. }
  1352. dprintk("governor: change or update limits\n");
  1353. __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
  1354. }
  1355. error_out:
  1356. cpufreq_debug_enable_ratelimit();
  1357. return ret;
  1358. }
  1359. /**
  1360. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1361. * @cpu: CPU which shall be re-evaluated
  1362. *
  1363. * Usefull for policy notifiers which have different necessities
  1364. * at different times.
  1365. */
  1366. int cpufreq_update_policy(unsigned int cpu)
  1367. {
  1368. struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
  1369. struct cpufreq_policy policy;
  1370. int ret = 0;
  1371. if (!data)
  1372. return -ENODEV;
  1373. if (unlikely(lock_policy_rwsem_write(cpu)))
  1374. return -EINVAL;
  1375. dprintk("updating policy for CPU %u\n", cpu);
  1376. memcpy(&policy, data, sizeof(struct cpufreq_policy));
  1377. policy.min = data->user_policy.min;
  1378. policy.max = data->user_policy.max;
  1379. policy.policy = data->user_policy.policy;
  1380. policy.governor = data->user_policy.governor;
  1381. /* BIOS might change freq behind our back
  1382. -> ask driver for current freq and notify governors about a change */
  1383. if (cpufreq_driver->get) {
  1384. policy.cur = cpufreq_driver->get(cpu);
  1385. if (!data->cur) {
  1386. dprintk("Driver did not initialize current freq");
  1387. data->cur = policy.cur;
  1388. } else {
  1389. if (data->cur != policy.cur)
  1390. cpufreq_out_of_sync(cpu, data->cur,
  1391. policy.cur);
  1392. }
  1393. }
  1394. ret = __cpufreq_set_policy(data, &policy);
  1395. unlock_policy_rwsem_write(cpu);
  1396. cpufreq_cpu_put(data);
  1397. return ret;
  1398. }
  1399. EXPORT_SYMBOL(cpufreq_update_policy);
  1400. static int cpufreq_cpu_callback(struct notifier_block *nfb,
  1401. unsigned long action, void *hcpu)
  1402. {
  1403. unsigned int cpu = (unsigned long)hcpu;
  1404. struct sys_device *sys_dev;
  1405. sys_dev = get_cpu_sysdev(cpu);
  1406. if (sys_dev) {
  1407. switch (action) {
  1408. case CPU_ONLINE:
  1409. case CPU_ONLINE_FROZEN:
  1410. cpufreq_add_dev(sys_dev);
  1411. break;
  1412. case CPU_DOWN_PREPARE:
  1413. case CPU_DOWN_PREPARE_FROZEN:
  1414. if (unlikely(lock_policy_rwsem_write(cpu)))
  1415. BUG();
  1416. __cpufreq_remove_dev(sys_dev);
  1417. break;
  1418. case CPU_DOWN_FAILED:
  1419. case CPU_DOWN_FAILED_FROZEN:
  1420. cpufreq_add_dev(sys_dev);
  1421. break;
  1422. }
  1423. }
  1424. return NOTIFY_OK;
  1425. }
  1426. static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
  1427. {
  1428. .notifier_call = cpufreq_cpu_callback,
  1429. };
  1430. /*********************************************************************
  1431. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1432. *********************************************************************/
  1433. /**
  1434. * cpufreq_register_driver - register a CPU Frequency driver
  1435. * @driver_data: A struct cpufreq_driver containing the values#
  1436. * submitted by the CPU Frequency driver.
  1437. *
  1438. * Registers a CPU Frequency driver to this core code. This code
  1439. * returns zero on success, -EBUSY when another driver got here first
  1440. * (and isn't unregistered in the meantime).
  1441. *
  1442. */
  1443. int cpufreq_register_driver(struct cpufreq_driver *driver_data)
  1444. {
  1445. unsigned long flags;
  1446. int ret;
  1447. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1448. ((!driver_data->setpolicy) && (!driver_data->target)))
  1449. return -EINVAL;
  1450. dprintk("trying to register driver %s\n", driver_data->name);
  1451. if (driver_data->setpolicy)
  1452. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  1453. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1454. if (cpufreq_driver) {
  1455. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1456. return -EBUSY;
  1457. }
  1458. cpufreq_driver = driver_data;
  1459. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1460. ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
  1461. if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  1462. int i;
  1463. ret = -ENODEV;
  1464. /* check for at least one working CPU */
  1465. for (i=0; i<NR_CPUS; i++)
  1466. if (cpufreq_cpu_data[i])
  1467. ret = 0;
  1468. /* if all ->init() calls failed, unregister */
  1469. if (ret) {
  1470. dprintk("no CPU initialized for driver %s\n",
  1471. driver_data->name);
  1472. sysdev_driver_unregister(&cpu_sysdev_class,
  1473. &cpufreq_sysdev_driver);
  1474. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1475. cpufreq_driver = NULL;
  1476. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1477. }
  1478. }
  1479. if (!ret) {
  1480. register_hotcpu_notifier(&cpufreq_cpu_notifier);
  1481. dprintk("driver %s up and running\n", driver_data->name);
  1482. cpufreq_debug_enable_ratelimit();
  1483. }
  1484. return (ret);
  1485. }
  1486. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  1487. /**
  1488. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  1489. *
  1490. * Unregister the current CPUFreq driver. Only call this if you have
  1491. * the right to do so, i.e. if you have succeeded in initialising before!
  1492. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  1493. * currently not initialised.
  1494. */
  1495. int cpufreq_unregister_driver(struct cpufreq_driver *driver)
  1496. {
  1497. unsigned long flags;
  1498. cpufreq_debug_disable_ratelimit();
  1499. if (!cpufreq_driver || (driver != cpufreq_driver)) {
  1500. cpufreq_debug_enable_ratelimit();
  1501. return -EINVAL;
  1502. }
  1503. dprintk("unregistering driver %s\n", driver->name);
  1504. sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
  1505. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  1506. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1507. cpufreq_driver = NULL;
  1508. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1509. return 0;
  1510. }
  1511. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  1512. static int __init cpufreq_core_init(void)
  1513. {
  1514. int cpu;
  1515. for_each_possible_cpu(cpu) {
  1516. per_cpu(policy_cpu, cpu) = -1;
  1517. init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
  1518. }
  1519. return 0;
  1520. }
  1521. core_initcall(cpufreq_core_init);