cpufreq.c 40 KB

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