cpufreq.c 42 KB

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