cpufreq.h 12 KB

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