cpufreq.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/clk.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/completion.h>
  16. #include <linux/kobject.h>
  17. #include <linux/notifier.h>
  18. #include <linux/sysfs.h>
  19. /*********************************************************************
  20. * CPUFREQ INTERFACE *
  21. *********************************************************************/
  22. /*
  23. * Frequency values here are CPU kHz
  24. *
  25. * Maximum transition latency is in nanoseconds - if it's unknown,
  26. * CPUFREQ_ETERNAL shall be used.
  27. */
  28. #define CPUFREQ_ETERNAL (-1)
  29. #define CPUFREQ_NAME_LEN 16
  30. /* Print length for names. Extra 1 space for accomodating '\n' in prints */
  31. #define CPUFREQ_NAME_PLEN (CPUFREQ_NAME_LEN + 1)
  32. struct cpufreq_governor;
  33. struct cpufreq_freqs {
  34. unsigned int cpu; /* cpu nr */
  35. unsigned int old;
  36. unsigned int new;
  37. u8 flags; /* flags of cpufreq_driver, see below. */
  38. };
  39. struct cpufreq_cpuinfo {
  40. unsigned int max_freq;
  41. unsigned int min_freq;
  42. /* in 10^(-9) s = nanoseconds */
  43. unsigned int transition_latency;
  44. };
  45. struct cpufreq_real_policy {
  46. unsigned int min; /* in kHz */
  47. unsigned int max; /* in kHz */
  48. unsigned int policy; /* see above */
  49. struct cpufreq_governor *governor; /* see below */
  50. };
  51. struct cpufreq_policy {
  52. /* CPUs sharing clock, require sw coordination */
  53. cpumask_var_t cpus; /* Online CPUs only */
  54. cpumask_var_t related_cpus; /* Online + Offline CPUs */
  55. unsigned int shared_type; /* ACPI: ANY or ALL affected CPUs
  56. should set cpufreq */
  57. unsigned int cpu; /* cpu nr of CPU managing this policy */
  58. unsigned int last_cpu; /* cpu nr of previous CPU that managed
  59. * this policy */
  60. struct clk *clk;
  61. struct cpufreq_cpuinfo cpuinfo;/* see above */
  62. unsigned int min; /* in kHz */
  63. unsigned int max; /* in kHz */
  64. unsigned int cur; /* in kHz, only needed if cpufreq
  65. * governors are used */
  66. unsigned int suspend_freq; /* freq to set during suspend */
  67. unsigned int policy; /* see above */
  68. struct cpufreq_governor *governor; /* see below */
  69. void *governor_data;
  70. bool governor_enabled; /* governor start/stop flag */
  71. struct work_struct update; /* if update_policy() needs to be
  72. * called, but you're in IRQ context */
  73. struct cpufreq_real_policy user_policy;
  74. struct cpufreq_frequency_table *freq_table;
  75. struct list_head policy_list;
  76. struct kobject kobj;
  77. struct completion kobj_unregister;
  78. /*
  79. * The rules for this semaphore:
  80. * - Any routine that wants to read from the policy structure will
  81. * do a down_read on this semaphore.
  82. * - Any routine that will write to the policy structure and/or may take away
  83. * the policy altogether (eg. CPU hotplug), will hold this lock in write
  84. * mode before doing so.
  85. *
  86. * Additional rules:
  87. * - Lock should not be held across
  88. * __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
  89. */
  90. struct rw_semaphore rwsem;
  91. };
  92. /* Only for ACPI */
  93. #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
  94. #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */
  95. #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */
  96. #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/
  97. #ifdef CONFIG_CPU_FREQ
  98. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
  99. void cpufreq_cpu_put(struct cpufreq_policy *policy);
  100. #else
  101. static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  102. {
  103. return NULL;
  104. }
  105. static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
  106. #endif
  107. static inline bool policy_is_shared(struct cpufreq_policy *policy)
  108. {
  109. return cpumask_weight(policy->cpus) > 1;
  110. }
  111. /* /sys/devices/system/cpu/cpufreq: entry point for global variables */
  112. extern struct kobject *cpufreq_global_kobject;
  113. int cpufreq_get_global_kobject(void);
  114. void cpufreq_put_global_kobject(void);
  115. int cpufreq_sysfs_create_file(const struct attribute *attr);
  116. void cpufreq_sysfs_remove_file(const struct attribute *attr);
  117. #ifdef CONFIG_CPU_FREQ
  118. unsigned int cpufreq_get(unsigned int cpu);
  119. unsigned int cpufreq_quick_get(unsigned int cpu);
  120. unsigned int cpufreq_quick_get_max(unsigned int cpu);
  121. void disable_cpufreq(void);
  122. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
  123. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
  124. int cpufreq_update_policy(unsigned int cpu);
  125. bool have_governor_per_policy(void);
  126. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
  127. #else
  128. static inline unsigned int cpufreq_get(unsigned int cpu)
  129. {
  130. return 0;
  131. }
  132. static inline unsigned int cpufreq_quick_get(unsigned int cpu)
  133. {
  134. return 0;
  135. }
  136. static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
  137. {
  138. return 0;
  139. }
  140. static inline void disable_cpufreq(void) { }
  141. #endif
  142. /*********************************************************************
  143. * CPUFREQ DRIVER INTERFACE *
  144. *********************************************************************/
  145. #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */
  146. #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */
  147. struct freq_attr {
  148. struct attribute attr;
  149. ssize_t (*show)(struct cpufreq_policy *, char *);
  150. ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
  151. };
  152. #define cpufreq_freq_attr_ro(_name) \
  153. static struct freq_attr _name = \
  154. __ATTR(_name, 0444, show_##_name, NULL)
  155. #define cpufreq_freq_attr_ro_perm(_name, _perm) \
  156. static struct freq_attr _name = \
  157. __ATTR(_name, _perm, show_##_name, NULL)
  158. #define cpufreq_freq_attr_rw(_name) \
  159. static struct freq_attr _name = \
  160. __ATTR(_name, 0644, show_##_name, store_##_name)
  161. struct global_attr {
  162. struct attribute attr;
  163. ssize_t (*show)(struct kobject *kobj,
  164. struct attribute *attr, char *buf);
  165. ssize_t (*store)(struct kobject *a, struct attribute *b,
  166. const char *c, size_t count);
  167. };
  168. #define define_one_global_ro(_name) \
  169. static struct global_attr _name = \
  170. __ATTR(_name, 0444, show_##_name, NULL)
  171. #define define_one_global_rw(_name) \
  172. static struct global_attr _name = \
  173. __ATTR(_name, 0644, show_##_name, store_##_name)
  174. struct cpufreq_driver {
  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, /* Deprecated */
  183. unsigned int target_freq,
  184. unsigned int relation);
  185. int (*target_index) (struct cpufreq_policy *policy,
  186. unsigned int index);
  187. /* should be defined, if possible */
  188. unsigned int (*get) (unsigned int cpu);
  189. /* optional */
  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. /* platform specific boost support code */
  196. bool boost_supported;
  197. bool boost_enabled;
  198. int (*set_boost) (int state);
  199. };
  200. /* flags */
  201. #define CPUFREQ_STICKY (1 << 0) /* driver isn't removed even if
  202. all ->init() calls failed */
  203. #define CPUFREQ_CONST_LOOPS (1 << 1) /* loops_per_jiffy or other
  204. kernel "constants" aren't
  205. affected by frequency
  206. transitions */
  207. #define CPUFREQ_PM_NO_WARN (1 << 2) /* don't warn on suspend/resume
  208. speed mismatches */
  209. /*
  210. * This should be set by platforms having multiple clock-domains, i.e.
  211. * supporting multiple policies. With this sysfs directories of governor would
  212. * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
  213. * governor with different tunables for different clusters.
  214. */
  215. #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY (1 << 3)
  216. /*
  217. * Driver will do POSTCHANGE notifications from outside of their ->target()
  218. * routine and so must set cpufreq_driver->flags with this flag, so that core
  219. * can handle them specially.
  220. */
  221. #define CPUFREQ_ASYNC_NOTIFICATION (1 << 4)
  222. /*
  223. * Set by drivers which want cpufreq core to check if CPU is running at a
  224. * frequency present in freq-table exposed by the driver. For these drivers if
  225. * CPU is found running at an out of table freq, we will try to set it to a freq
  226. * from the table. And if that fails, we will stop further boot process by
  227. * issuing a BUG_ON().
  228. */
  229. #define CPUFREQ_NEED_INITIAL_FREQ_CHECK (1 << 5)
  230. int cpufreq_register_driver(struct cpufreq_driver *driver_data);
  231. int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
  232. const char *cpufreq_get_current_driver(void);
  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. static inline void
  249. cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
  250. {
  251. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  252. policy->cpuinfo.max_freq);
  253. }
  254. #ifdef CONFIG_CPU_FREQ
  255. void cpufreq_suspend(void);
  256. void cpufreq_resume(void);
  257. int cpufreq_generic_suspend(struct cpufreq_policy *policy);
  258. #else
  259. static inline void cpufreq_suspend(void) {}
  260. static inline void cpufreq_resume(void) {}
  261. #endif
  262. /*********************************************************************
  263. * CPUFREQ NOTIFIER INTERFACE *
  264. *********************************************************************/
  265. #define CPUFREQ_TRANSITION_NOTIFIER (0)
  266. #define CPUFREQ_POLICY_NOTIFIER (1)
  267. /* Transition notifiers */
  268. #define CPUFREQ_PRECHANGE (0)
  269. #define CPUFREQ_POSTCHANGE (1)
  270. #define CPUFREQ_RESUMECHANGE (8)
  271. #define CPUFREQ_SUSPENDCHANGE (9)
  272. /* Policy Notifiers */
  273. #define CPUFREQ_ADJUST (0)
  274. #define CPUFREQ_INCOMPATIBLE (1)
  275. #define CPUFREQ_NOTIFY (2)
  276. #define CPUFREQ_START (3)
  277. #define CPUFREQ_UPDATE_POLICY_CPU (4)
  278. #define CPUFREQ_CREATE_POLICY (5)
  279. #define CPUFREQ_REMOVE_POLICY (6)
  280. #ifdef CONFIG_CPU_FREQ
  281. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
  282. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
  283. void cpufreq_notify_transition(struct cpufreq_policy *policy,
  284. struct cpufreq_freqs *freqs, unsigned int state);
  285. void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
  286. struct cpufreq_freqs *freqs, int transition_failed);
  287. #else /* CONFIG_CPU_FREQ */
  288. static inline int cpufreq_register_notifier(struct notifier_block *nb,
  289. unsigned int list)
  290. {
  291. return 0;
  292. }
  293. static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
  294. unsigned int list)
  295. {
  296. return 0;
  297. }
  298. #endif /* !CONFIG_CPU_FREQ */
  299. /**
  300. * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
  301. * safe)
  302. * @old: old value
  303. * @div: divisor
  304. * @mult: multiplier
  305. *
  306. *
  307. * new = old * mult / div
  308. */
  309. static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
  310. u_int mult)
  311. {
  312. #if BITS_PER_LONG == 32
  313. u64 result = ((u64) old) * ((u64) mult);
  314. do_div(result, div);
  315. return (unsigned long) result;
  316. #elif BITS_PER_LONG == 64
  317. unsigned long result = old * ((u64) mult);
  318. result /= div;
  319. return result;
  320. #endif
  321. }
  322. /*********************************************************************
  323. * CPUFREQ GOVERNORS *
  324. *********************************************************************/
  325. /*
  326. * If (cpufreq_driver->target) exists, the ->governor decides what frequency
  327. * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
  328. * two generic policies are available:
  329. */
  330. #define CPUFREQ_POLICY_POWERSAVE (1)
  331. #define CPUFREQ_POLICY_PERFORMANCE (2)
  332. /* Governor Events */
  333. #define CPUFREQ_GOV_START 1
  334. #define CPUFREQ_GOV_STOP 2
  335. #define CPUFREQ_GOV_LIMITS 3
  336. #define CPUFREQ_GOV_POLICY_INIT 4
  337. #define CPUFREQ_GOV_POLICY_EXIT 5
  338. struct cpufreq_governor {
  339. char name[CPUFREQ_NAME_LEN];
  340. int initialized;
  341. int (*governor) (struct cpufreq_policy *policy,
  342. unsigned int event);
  343. ssize_t (*show_setspeed) (struct cpufreq_policy *policy,
  344. char *buf);
  345. int (*store_setspeed) (struct cpufreq_policy *policy,
  346. unsigned int freq);
  347. unsigned int max_transition_latency; /* HW must be able to switch to
  348. next freq faster than this value in nano secs or we
  349. will fallback to performance governor */
  350. struct list_head governor_list;
  351. struct module *owner;
  352. };
  353. /* Pass a target to the cpufreq driver */
  354. int cpufreq_driver_target(struct cpufreq_policy *policy,
  355. unsigned int target_freq,
  356. unsigned int relation);
  357. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  358. unsigned int target_freq,
  359. unsigned int relation);
  360. int cpufreq_register_governor(struct cpufreq_governor *governor);
  361. void cpufreq_unregister_governor(struct cpufreq_governor *governor);
  362. /* CPUFREQ DEFAULT GOVERNOR */
  363. /*
  364. * Performance governor is fallback governor if any other gov failed to auto
  365. * load due latency restrictions
  366. */
  367. #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
  368. extern struct cpufreq_governor cpufreq_gov_performance;
  369. #endif
  370. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_PERFORMANCE
  371. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_performance)
  372. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_POWERSAVE)
  373. extern struct cpufreq_governor cpufreq_gov_powersave;
  374. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_powersave)
  375. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_USERSPACE)
  376. extern struct cpufreq_governor cpufreq_gov_userspace;
  377. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_userspace)
  378. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND)
  379. extern struct cpufreq_governor cpufreq_gov_ondemand;
  380. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_ondemand)
  381. #elif defined(CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE)
  382. extern struct cpufreq_governor cpufreq_gov_conservative;
  383. #define CPUFREQ_DEFAULT_GOVERNOR (&cpufreq_gov_conservative)
  384. #endif
  385. /*********************************************************************
  386. * FREQUENCY TABLE HELPERS *
  387. *********************************************************************/
  388. #define CPUFREQ_ENTRY_INVALID ~0
  389. #define CPUFREQ_TABLE_END ~1
  390. #define CPUFREQ_BOOST_FREQ ~2
  391. struct cpufreq_frequency_table {
  392. unsigned int driver_data; /* driver specific data, not used by core */
  393. unsigned int frequency; /* kHz - doesn't need to be in ascending
  394. * order */
  395. };
  396. int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  397. struct cpufreq_frequency_table *table);
  398. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  399. struct cpufreq_frequency_table *table);
  400. int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy);
  401. int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  402. struct cpufreq_frequency_table *table,
  403. unsigned int target_freq,
  404. unsigned int relation,
  405. unsigned int *index);
  406. int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
  407. unsigned int freq);
  408. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
  409. #ifdef CONFIG_CPU_FREQ
  410. int cpufreq_boost_trigger_state(int state);
  411. int cpufreq_boost_supported(void);
  412. int cpufreq_boost_enabled(void);
  413. #else
  414. static inline int cpufreq_boost_trigger_state(int state)
  415. {
  416. return 0;
  417. }
  418. static inline int cpufreq_boost_supported(void)
  419. {
  420. return 0;
  421. }
  422. static inline int cpufreq_boost_enabled(void)
  423. {
  424. return 0;
  425. }
  426. #endif
  427. /* the following funtion is for cpufreq core use only */
  428. struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu);
  429. /* the following are really really optional */
  430. extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
  431. extern struct freq_attr *cpufreq_generic_attr[];
  432. int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
  433. struct cpufreq_frequency_table *table);
  434. unsigned int cpufreq_generic_get(unsigned int cpu);
  435. int cpufreq_generic_init(struct cpufreq_policy *policy,
  436. struct cpufreq_frequency_table *table,
  437. unsigned int transition_latency);
  438. #endif /* _LINUX_CPUFREQ_H */