cpufreq.h 13 KB

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