cpufreq.c 42 KB

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