cpufreq.h 12 KB

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