cpufreq.c 46 KB

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