cpufreq.h 10 KB

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