cpufreq.h 16 KB

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