cpufreq.c 43 KB

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