cpufreq.h 14 KB

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