cpufreq.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. * The rules for this semaphore:
  76. * - Any routine that wants to read from the policy structure will
  77. * do a down_read on this semaphore.
  78. * - Any routine that will write to the policy structure and/or may take away
  79. * the policy altogether (eg. CPU hotplug), will hold this lock in write
  80. * mode before doing so.
  81. *
  82. * Additional rules:
  83. * - Lock should not be held across
  84. * __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
  85. */
  86. struct rw_semaphore rwsem;
  87. };
  88. /* Only for ACPI */
  89. #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
  90. #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */
  91. #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */
  92. #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/
  93. #ifdef CONFIG_CPU_FREQ
  94. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
  95. void cpufreq_cpu_put(struct cpufreq_policy *policy);
  96. #else
  97. static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  98. {
  99. return NULL;
  100. }
  101. static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
  102. #endif
  103. static inline bool policy_is_shared(struct cpufreq_policy *policy)
  104. {
  105. return cpumask_weight(policy->cpus) > 1;
  106. }
  107. /* /sys/devices/system/cpu/cpufreq: entry point for global variables */
  108. extern struct kobject *cpufreq_global_kobject;
  109. int cpufreq_get_global_kobject(void);
  110. void cpufreq_put_global_kobject(void);
  111. int cpufreq_sysfs_create_file(const struct attribute *attr);
  112. void cpufreq_sysfs_remove_file(const struct attribute *attr);
  113. #ifdef CONFIG_CPU_FREQ
  114. unsigned int cpufreq_get(unsigned int cpu);
  115. unsigned int cpufreq_quick_get(unsigned int cpu);
  116. unsigned int cpufreq_quick_get_max(unsigned int cpu);
  117. void disable_cpufreq(void);
  118. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
  119. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
  120. int cpufreq_update_policy(unsigned int cpu);
  121. bool have_governor_per_policy(void);
  122. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
  123. #else
  124. static inline unsigned int cpufreq_get(unsigned int cpu)
  125. {
  126. return 0;
  127. }
  128. static inline unsigned int cpufreq_quick_get(unsigned int cpu)
  129. {
  130. return 0;
  131. }
  132. static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
  133. {
  134. return 0;
  135. }
  136. static inline void disable_cpufreq(void) { }
  137. #endif
  138. /*********************************************************************
  139. * CPUFREQ DRIVER INTERFACE *
  140. *********************************************************************/
  141. #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */
  142. #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */
  143. struct freq_attr {
  144. struct attribute attr;
  145. ssize_t (*show)(struct cpufreq_policy *, char *);
  146. ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
  147. };
  148. #define cpufreq_freq_attr_ro(_name) \
  149. static struct freq_attr _name = \
  150. __ATTR(_name, 0444, show_##_name, NULL)
  151. #define cpufreq_freq_attr_ro_perm(_name, _perm) \
  152. static struct freq_attr _name = \
  153. __ATTR(_name, _perm, show_##_name, NULL)
  154. #define cpufreq_freq_attr_rw(_name) \
  155. static struct freq_attr _name = \
  156. __ATTR(_name, 0644, show_##_name, store_##_name)
  157. struct global_attr {
  158. struct attribute attr;
  159. ssize_t (*show)(struct kobject *kobj,
  160. struct attribute *attr, char *buf);
  161. ssize_t (*store)(struct kobject *a, struct attribute *b,
  162. const char *c, size_t count);
  163. };
  164. #define define_one_global_ro(_name) \
  165. static struct global_attr _name = \
  166. __ATTR(_name, 0444, show_##_name, NULL)
  167. #define define_one_global_rw(_name) \
  168. static struct global_attr _name = \
  169. __ATTR(_name, 0644, show_##_name, store_##_name)
  170. struct cpufreq_driver {
  171. char name[CPUFREQ_NAME_LEN];
  172. u8 flags;
  173. /* needed by all drivers */
  174. int (*init) (struct cpufreq_policy *policy);
  175. int (*verify) (struct cpufreq_policy *policy);
  176. /* define one out of two */
  177. int (*setpolicy) (struct cpufreq_policy *policy);
  178. int (*target) (struct cpufreq_policy *policy, /* Deprecated */
  179. unsigned int target_freq,
  180. unsigned int relation);
  181. int (*target_index) (struct cpufreq_policy *policy,
  182. unsigned int index);
  183. /* should be defined, if possible */
  184. unsigned int (*get) (unsigned int cpu);
  185. /* optional */
  186. int (*bios_limit) (int cpu, unsigned int *limit);
  187. int (*exit) (struct cpufreq_policy *policy);
  188. int (*suspend) (struct cpufreq_policy *policy);
  189. int (*resume) (struct cpufreq_policy *policy);
  190. struct freq_attr **attr;
  191. };
  192. /* flags */
  193. #define CPUFREQ_STICKY (1 << 0) /* driver isn't removed even if
  194. all ->init() calls failed */
  195. #define CPUFREQ_CONST_LOOPS (1 << 1) /* loops_per_jiffy or other
  196. kernel "constants" aren't
  197. affected by frequency
  198. transitions */
  199. #define CPUFREQ_PM_NO_WARN (1 << 2) /* don't warn on suspend/resume
  200. speed mismatches */
  201. /*
  202. * This should be set by platforms having multiple clock-domains, i.e.
  203. * supporting multiple policies. With this sysfs directories of governor would
  204. * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
  205. * governor with different tunables for different clusters.
  206. */
  207. #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY (1 << 3)
  208. /*
  209. * Driver will do POSTCHANGE notifications from outside of their ->target()
  210. * routine and so must set cpufreq_driver->flags with this flag, so that core
  211. * can handle them specially.
  212. */
  213. #define CPUFREQ_ASYNC_NOTIFICATION (1 << 4)
  214. int cpufreq_register_driver(struct cpufreq_driver *driver_data);
  215. int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
  216. const char *cpufreq_get_current_driver(void);
  217. static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy,
  218. unsigned int min, unsigned int max)
  219. {
  220. if (policy->min < min)
  221. policy->min = min;
  222. if (policy->max < min)
  223. policy->max = min;
  224. if (policy->min > max)
  225. policy->min = max;
  226. if (policy->max > max)
  227. policy->max = max;
  228. if (policy->min > policy->max)
  229. policy->min = policy->max;
  230. return;
  231. }
  232. static inline void
  233. cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
  234. {
  235. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  236. policy->cpuinfo.max_freq);
  237. }
  238. #ifdef CONFIG_CPU_FREQ
  239. void cpufreq_suspend(void);
  240. void cpufreq_resume(void);
  241. #else
  242. static inline void cpufreq_suspend(void) {}
  243. static inline void cpufreq_resume(void) {}
  244. #endif
  245. /*********************************************************************
  246. * CPUFREQ NOTIFIER INTERFACE *
  247. *********************************************************************/
  248. #define CPUFREQ_TRANSITION_NOTIFIER (0)
  249. #define CPUFREQ_POLICY_NOTIFIER (1)
  250. /* Transition notifiers */
  251. #define CPUFREQ_PRECHANGE (0)
  252. #define CPUFREQ_POSTCHANGE (1)
  253. #define CPUFREQ_RESUMECHANGE (8)
  254. #define CPUFREQ_SUSPENDCHANGE (9)
  255. /* Policy Notifiers */
  256. #define CPUFREQ_ADJUST (0)
  257. #define CPUFREQ_INCOMPATIBLE (1)
  258. #define CPUFREQ_NOTIFY (2)
  259. #define CPUFREQ_START (3)
  260. #define CPUFREQ_UPDATE_POLICY_CPU (4)
  261. #ifdef CONFIG_CPU_FREQ
  262. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
  263. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
  264. void cpufreq_notify_transition(struct cpufreq_policy *policy,
  265. struct cpufreq_freqs *freqs, unsigned int state);
  266. #else /* CONFIG_CPU_FREQ */
  267. static inline int cpufreq_register_notifier(struct notifier_block *nb,
  268. unsigned int list)
  269. {
  270. return 0;
  271. }
  272. static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
  273. unsigned int list)
  274. {
  275. return 0;
  276. }
  277. #endif /* !CONFIG_CPU_FREQ */
  278. /**
  279. * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
  280. * safe)
  281. * @old: old value
  282. * @div: divisor
  283. * @mult: multiplier
  284. *
  285. *
  286. * new = old * mult / div
  287. */
  288. static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
  289. u_int mult)
  290. {
  291. #if BITS_PER_LONG == 32
  292. u64 result = ((u64) old) * ((u64) mult);
  293. do_div(result, div);
  294. return (unsigned long) result;
  295. #elif BITS_PER_LONG == 64
  296. unsigned long result = old * ((u64) mult);
  297. result /= div;
  298. return result;
  299. #endif
  300. }
  301. /*********************************************************************
  302. * CPUFREQ GOVERNORS *
  303. *********************************************************************/
  304. /*
  305. * If (cpufreq_driver->target) exists, the ->governor decides what frequency
  306. * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
  307. * two generic policies are available:
  308. */
  309. #define CPUFREQ_POLICY_POWERSAVE (1)
  310. #define CPUFREQ_POLICY_PERFORMANCE (2)
  311. /* Governor Events */
  312. #define CPUFREQ_GOV_START 1
  313. #define CPUFREQ_GOV_STOP 2
  314. #define CPUFREQ_GOV_LIMITS 3
  315. #define CPUFREQ_GOV_POLICY_INIT 4
  316. #define CPUFREQ_GOV_POLICY_EXIT 5
  317. struct cpufreq_governor {
  318. char name[CPUFREQ_NAME_LEN];
  319. int initialized;
  320. int (*governor) (struct cpufreq_policy *policy,
  321. unsigned int event);
  322. ssize_t (*show_setspeed) (struct cpufreq_policy *policy,
  323. char *buf);
  324. int (*store_setspeed) (struct cpufreq_policy *policy,
  325. unsigned int freq);
  326. unsigned int max_transition_latency; /* HW must be able to switch to
  327. next freq faster than this value in nano secs or we
  328. will fallback to performance governor */
  329. struct list_head governor_list;
  330. struct module *owner;
  331. };
  332. /* Pass a target to the cpufreq driver */
  333. int cpufreq_driver_target(struct cpufreq_policy *policy,
  334. unsigned int target_freq,
  335. unsigned int relation);
  336. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  337. unsigned int target_freq,
  338. unsigned int relation);
  339. int cpufreq_register_governor(struct cpufreq_governor *governor);
  340. void cpufreq_unregister_governor(struct cpufreq_governor *governor);
  341. /* CPUFREQ DEFAULT GOVERNOR */
  342. /*
  343. * Performance governor is fallback governor if any other gov failed to auto
  344. * load due latency restrictions
  345. */
  346. #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
  347. extern struct cpufreq_governor cpufreq_gov_performance;
  348. #endif
  349. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
  350. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_performance)
  351. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE)
  352. extern struct cpufreq_governor cpufreq_gov_powersave;
  353. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_powersave)
  354. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
  355. extern struct cpufreq_governor cpufreq_gov_userspace;
  356. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_userspace)
  357. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND)
  358. extern struct cpufreq_governor cpufreq_gov_ondemand;
  359. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_ondemand)
  360. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE)
  361. extern struct cpufreq_governor cpufreq_gov_conservative;
  362. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_conservative)
  363. #endif
  364. /*********************************************************************
  365. * FREQUENCY TABLE HELPERS *
  366. *********************************************************************/
  367. #define CPUFREQ_ENTRY_INVALID ~0
  368. #define CPUFREQ_TABLE_END ~1
  369. struct cpufreq_frequency_table {
  370. unsigned int driver_data; /* driver specific data, not used by core */
  371. unsigned int frequency; /* kHz - doesn't need to be in ascending
  372. * order */
  373. };
  374. int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  375. struct cpufreq_frequency_table *table);
  376. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  377. struct cpufreq_frequency_table *table);
  378. int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy);
  379. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  380. struct cpufreq_frequency_table *table,
  381. unsigned int target_freq,
  382. unsigned int relation,
  383. unsigned int *index);
  384. void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy);
  385. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
  386. /* the following funtion is for cpufreq core use only */
  387. struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
  388. /* the following are really really optional */
  389. extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
  390. extern struct freq_attr *cpufreq_generic_attr[];
  391. void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
  392. unsigned int cpu);
  393. void cpufreq_frequency_table_put_attr(unsigned int cpu);
  394. int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
  395. struct cpufreq_frequency_table *table);
  396. int cpufreq_generic_init(struct cpufreq_policy *policy,
  397. struct cpufreq_frequency_table *table,
  398. unsigned int transition_latency);
  399. static inline int cpufreq_generic_exit(struct cpufreq_policy *policy)
  400. {
  401. cpufreq_frequency_table_put_attr(policy->cpu);
  402. return 0;
  403. }
  404. #endif /* _LINUX_CPUFREQ_H */