cpufreq.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * linux/include/linux/cpufreq.h
  3. *
  4. * Copyright (C) 2001 Russell King
  5. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  6. *
  7. *
  8. * $Id: cpufreq.h,v 1.36 2003/01/20 17:31:48 db Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #ifndef _LINUX_CPUFREQ_H
  15. #define _LINUX_CPUFREQ_H
  16. #include <linux/mutex.h>
  17. #include <linux/notifier.h>
  18. #include <linux/threads.h>
  19. #include <linux/device.h>
  20. #include <linux/kobject.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/completion.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/cpumask.h>
  25. #include <asm/div64.h>
  26. #define CPUFREQ_NAME_LEN 16
  27. /*********************************************************************
  28. * CPUFREQ NOTIFIER INTERFACE *
  29. *********************************************************************/
  30. #define CPUFREQ_TRANSITION_NOTIFIER (0)
  31. #define CPUFREQ_POLICY_NOTIFIER (1)
  32. #ifdef CONFIG_CPU_FREQ
  33. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
  34. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
  35. #else /* CONFIG_CPU_FREQ */
  36. static inline int cpufreq_register_notifier(struct notifier_block *nb,
  37. unsigned int list)
  38. {
  39. return 0;
  40. }
  41. static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
  42. unsigned int list)
  43. {
  44. return 0;
  45. }
  46. #endif /* CONFIG_CPU_FREQ */
  47. /* if (cpufreq_driver->target) exists, the ->governor decides what frequency
  48. * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
  49. * two generic policies are available:
  50. */
  51. #define CPUFREQ_POLICY_POWERSAVE (1)
  52. #define CPUFREQ_POLICY_PERFORMANCE (2)
  53. /* Frequency values here are CPU kHz so that hardware which doesn't run
  54. * with some frequencies can complain without having to guess what per
  55. * cent / per mille means.
  56. * Maximum transition latency is in nanoseconds - if it's unknown,
  57. * CPUFREQ_ETERNAL shall be used.
  58. */
  59. struct cpufreq_governor;
  60. #define CPUFREQ_ETERNAL (-1)
  61. struct cpufreq_cpuinfo {
  62. unsigned int max_freq;
  63. unsigned int min_freq;
  64. unsigned int transition_latency; /* in 10^(-9) s = nanoseconds */
  65. };
  66. struct cpufreq_real_policy {
  67. unsigned int min; /* in kHz */
  68. unsigned int max; /* in kHz */
  69. unsigned int policy; /* see above */
  70. struct cpufreq_governor *governor; /* see below */
  71. };
  72. struct cpufreq_policy {
  73. cpumask_t cpus; /* affected CPUs */
  74. unsigned int shared_type; /* ANY or ALL affected CPUs
  75. should set cpufreq */
  76. unsigned int cpu; /* cpu nr of registered CPU */
  77. struct cpufreq_cpuinfo cpuinfo;/* see above */
  78. unsigned int min; /* in kHz */
  79. unsigned int max; /* in kHz */
  80. unsigned int cur; /* in kHz, only needed if cpufreq
  81. * governors are used */
  82. unsigned int policy; /* see above */
  83. struct cpufreq_governor *governor; /* see below */
  84. struct work_struct update; /* if update_policy() needs to be
  85. * called, but you're in IRQ context */
  86. struct cpufreq_real_policy user_policy;
  87. struct kobject kobj;
  88. struct completion kobj_unregister;
  89. };
  90. #define CPUFREQ_ADJUST (0)
  91. #define CPUFREQ_INCOMPATIBLE (1)
  92. #define CPUFREQ_NOTIFY (2)
  93. #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
  94. #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */
  95. #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */
  96. #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/
  97. /******************** cpufreq transition notifiers *******************/
  98. #define CPUFREQ_PRECHANGE (0)
  99. #define CPUFREQ_POSTCHANGE (1)
  100. #define CPUFREQ_RESUMECHANGE (8)
  101. #define CPUFREQ_SUSPENDCHANGE (9)
  102. struct cpufreq_freqs {
  103. unsigned int cpu; /* cpu nr */
  104. unsigned int old;
  105. unsigned int new;
  106. u8 flags; /* flags of cpufreq_driver, see below. */
  107. };
  108. /**
  109. * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch safe)
  110. * @old: old value
  111. * @div: divisor
  112. * @mult: multiplier
  113. *
  114. *
  115. * new = old * mult / div
  116. */
  117. static inline unsigned long cpufreq_scale(unsigned long old, u_int div, u_int mult)
  118. {
  119. #if BITS_PER_LONG == 32
  120. u64 result = ((u64) old) * ((u64) mult);
  121. do_div(result, div);
  122. return (unsigned long) result;
  123. #elif BITS_PER_LONG == 64
  124. unsigned long result = old * ((u64) mult);
  125. result /= div;
  126. return result;
  127. #endif
  128. };
  129. /*********************************************************************
  130. * CPUFREQ GOVERNORS *
  131. *********************************************************************/
  132. #define CPUFREQ_GOV_START 1
  133. #define CPUFREQ_GOV_STOP 2
  134. #define CPUFREQ_GOV_LIMITS 3
  135. struct cpufreq_governor {
  136. char name[CPUFREQ_NAME_LEN];
  137. int (*governor) (struct cpufreq_policy *policy,
  138. unsigned int event);
  139. unsigned int max_transition_latency; /* HW must be able to switch to
  140. next freq faster than this value in nano secs or we
  141. will fallback to performance governor */
  142. struct list_head governor_list;
  143. struct module *owner;
  144. };
  145. /* pass a target to the cpufreq driver
  146. */
  147. extern int cpufreq_driver_target(struct cpufreq_policy *policy,
  148. unsigned int target_freq,
  149. unsigned int relation);
  150. extern int __cpufreq_driver_target(struct cpufreq_policy *policy,
  151. unsigned int target_freq,
  152. unsigned int relation);
  153. extern int __cpufreq_driver_getavg(struct cpufreq_policy *policy);
  154. int cpufreq_register_governor(struct cpufreq_governor *governor);
  155. void cpufreq_unregister_governor(struct cpufreq_governor *governor);
  156. int lock_policy_rwsem_read(int cpu);
  157. int lock_policy_rwsem_write(int cpu);
  158. void unlock_policy_rwsem_read(int cpu);
  159. void unlock_policy_rwsem_write(int cpu);
  160. /*********************************************************************
  161. * CPUFREQ DRIVER INTERFACE *
  162. *********************************************************************/
  163. #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */
  164. #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */
  165. struct freq_attr;
  166. struct cpufreq_driver {
  167. struct module *owner;
  168. char name[CPUFREQ_NAME_LEN];
  169. u8 flags;
  170. /* needed by all drivers */
  171. int (*init) (struct cpufreq_policy *policy);
  172. int (*verify) (struct cpufreq_policy *policy);
  173. /* define one out of two */
  174. int (*setpolicy) (struct cpufreq_policy *policy);
  175. int (*target) (struct cpufreq_policy *policy,
  176. unsigned int target_freq,
  177. unsigned int relation);
  178. /* should be defined, if possible */
  179. unsigned int (*get) (unsigned int cpu);
  180. /* optional */
  181. unsigned int (*getavg) (unsigned int cpu);
  182. int (*exit) (struct cpufreq_policy *policy);
  183. int (*suspend) (struct cpufreq_policy *policy, pm_message_t pmsg);
  184. int (*resume) (struct cpufreq_policy *policy);
  185. struct freq_attr **attr;
  186. };
  187. /* flags */
  188. #define CPUFREQ_STICKY 0x01 /* the driver isn't removed even if
  189. * all ->init() calls failed */
  190. #define CPUFREQ_CONST_LOOPS 0x02 /* loops_per_jiffy or other kernel
  191. * "constants" aren't affected by
  192. * frequency transitions */
  193. #define CPUFREQ_PM_NO_WARN 0x04 /* don't warn on suspend/resume speed
  194. * mismatches */
  195. int cpufreq_register_driver(struct cpufreq_driver *driver_data);
  196. int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
  197. void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state);
  198. static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy, unsigned int min, unsigned int max)
  199. {
  200. if (policy->min < min)
  201. policy->min = min;
  202. if (policy->max < min)
  203. policy->max = min;
  204. if (policy->min > max)
  205. policy->min = max;
  206. if (policy->max > max)
  207. policy->max = max;
  208. if (policy->min > policy->max)
  209. policy->min = policy->max;
  210. return;
  211. }
  212. struct freq_attr {
  213. struct attribute attr;
  214. ssize_t (*show)(struct cpufreq_policy *, char *);
  215. ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
  216. };
  217. /*********************************************************************
  218. * CPUFREQ 2.6. INTERFACE *
  219. *********************************************************************/
  220. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
  221. int cpufreq_update_policy(unsigned int cpu);
  222. /* query the current CPU frequency (in kHz). If zero, cpufreq couldn't detect it */
  223. unsigned int cpufreq_get(unsigned int cpu);
  224. /* query the last known CPU freq (in kHz). If zero, cpufreq couldn't detect it */
  225. #ifdef CONFIG_CPU_FREQ
  226. unsigned int cpufreq_quick_get(unsigned int cpu);
  227. #else
  228. static inline unsigned int cpufreq_quick_get(unsigned int cpu)
  229. {
  230. return 0;
  231. }
  232. #endif
  233. /*********************************************************************
  234. * CPUFREQ DEFAULT GOVERNOR *
  235. *********************************************************************/
  236. /*
  237. Performance governor is fallback governor if any other gov failed to
  238. auto load due latency restrictions
  239. */
  240. #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
  241. extern struct cpufreq_governor cpufreq_gov_performance;
  242. #endif
  243. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
  244. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_performance)
  245. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
  246. extern struct cpufreq_governor cpufreq_gov_userspace;
  247. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_userspace)
  248. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND)
  249. extern struct cpufreq_governor cpufreq_gov_ondemand;
  250. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_ondemand)
  251. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE)
  252. extern struct cpufreq_governor cpufreq_gov_conservative;
  253. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_conservative)
  254. #endif
  255. /*********************************************************************
  256. * FREQUENCY TABLE HELPERS *
  257. *********************************************************************/
  258. #define CPUFREQ_ENTRY_INVALID ~0
  259. #define CPUFREQ_TABLE_END ~1
  260. struct cpufreq_frequency_table {
  261. unsigned int index; /* any */
  262. unsigned int frequency; /* kHz - doesn't need to be in ascending
  263. * order */
  264. };
  265. int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  266. struct cpufreq_frequency_table *table);
  267. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  268. struct cpufreq_frequency_table *table);
  269. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  270. struct cpufreq_frequency_table *table,
  271. unsigned int target_freq,
  272. unsigned int relation,
  273. unsigned int *index);
  274. /* the following 3 funtions are for cpufreq core use only */
  275. struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
  276. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
  277. void cpufreq_cpu_put (struct cpufreq_policy *data);
  278. /* the following are really really optional */
  279. extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
  280. void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
  281. unsigned int cpu);
  282. void cpufreq_frequency_table_put_attr(unsigned int cpu);
  283. /*********************************************************************
  284. * UNIFIED DEBUG HELPERS *
  285. *********************************************************************/
  286. #define CPUFREQ_DEBUG_CORE 1
  287. #define CPUFREQ_DEBUG_DRIVER 2
  288. #define CPUFREQ_DEBUG_GOVERNOR 4
  289. #ifdef CONFIG_CPU_FREQ_DEBUG
  290. extern void cpufreq_debug_printk(unsigned int type, const char *prefix,
  291. const char *fmt, ...);
  292. #else
  293. #define cpufreq_debug_printk(msg...) do { } while(0)
  294. #endif /* CONFIG_CPU_FREQ_DEBUG */
  295. #endif /* _LINUX_CPUFREQ_H */