cpufreq.h 14 KB

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