cpufreq.c 42 KB

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