cpufreq.c 43 KB

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