cpufreq.c 42 KB

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