cpufreq.c 39 KB

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