cpufreq.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. enum cpufreq_table_sorting {
  35. CPUFREQ_TABLE_UNSORTED,
  36. CPUFREQ_TABLE_SORTED_ASCENDING,
  37. CPUFREQ_TABLE_SORTED_DESCENDING
  38. };
  39. struct cpufreq_freqs {
  40. unsigned int cpu; /* cpu nr */
  41. unsigned int old;
  42. unsigned int new;
  43. u8 flags; /* flags of cpufreq_driver, see below. */
  44. };
  45. struct cpufreq_cpuinfo {
  46. unsigned int max_freq;
  47. unsigned int min_freq;
  48. /* in 10^(-9) s = nanoseconds */
  49. unsigned int transition_latency;
  50. };
  51. struct cpufreq_user_policy {
  52. unsigned int min; /* in kHz */
  53. unsigned int max; /* in kHz */
  54. };
  55. struct cpufreq_policy {
  56. /* CPUs sharing clock, require sw coordination */
  57. cpumask_var_t cpus; /* Online CPUs only */
  58. cpumask_var_t related_cpus; /* Online + Offline CPUs */
  59. cpumask_var_t real_cpus; /* Related and present */
  60. unsigned int shared_type; /* ACPI: ANY or ALL affected CPUs
  61. should set cpufreq */
  62. unsigned int cpu; /* cpu managing this policy, must be online */
  63. struct clk *clk;
  64. struct cpufreq_cpuinfo cpuinfo;/* see above */
  65. unsigned int min; /* in kHz */
  66. unsigned int max; /* in kHz */
  67. unsigned int cur; /* in kHz, only needed if cpufreq
  68. * governors are used */
  69. unsigned int restore_freq; /* = policy->cur before transition */
  70. unsigned int suspend_freq; /* freq to set during suspend */
  71. unsigned int policy; /* see above */
  72. unsigned int last_policy; /* policy before unplug */
  73. struct cpufreq_governor *governor; /* see below */
  74. void *governor_data;
  75. char last_governor[CPUFREQ_NAME_LEN]; /* last governor used */
  76. struct work_struct update; /* if update_policy() needs to be
  77. * called, but you're in IRQ context */
  78. struct cpufreq_user_policy user_policy;
  79. struct cpufreq_frequency_table *freq_table;
  80. enum cpufreq_table_sorting freq_table_sorted;
  81. struct list_head policy_list;
  82. struct kobject kobj;
  83. struct completion kobj_unregister;
  84. /*
  85. * The rules for this semaphore:
  86. * - Any routine that wants to read from the policy structure will
  87. * do a down_read on this semaphore.
  88. * - Any routine that will write to the policy structure and/or may take away
  89. * the policy altogether (eg. CPU hotplug), will hold this lock in write
  90. * mode before doing so.
  91. */
  92. struct rw_semaphore rwsem;
  93. /*
  94. * Fast switch flags:
  95. * - fast_switch_possible should be set by the driver if it can
  96. * guarantee that frequency can be changed on any CPU sharing the
  97. * policy and that the change will affect all of the policy CPUs then.
  98. * - fast_switch_enabled is to be set by governors that support fast
  99. * freqnency switching with the help of cpufreq_enable_fast_switch().
  100. */
  101. bool fast_switch_possible;
  102. bool fast_switch_enabled;
  103. /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
  104. unsigned int cached_target_freq;
  105. int cached_resolved_idx;
  106. /* Synchronization for frequency transitions */
  107. bool transition_ongoing; /* Tracks transition status */
  108. spinlock_t transition_lock;
  109. wait_queue_head_t transition_wait;
  110. struct task_struct *transition_task; /* Task which is doing the transition */
  111. /* cpufreq-stats */
  112. struct cpufreq_stats *stats;
  113. /* For cpufreq driver's internal use */
  114. void *driver_data;
  115. };
  116. /* Only for ACPI */
  117. #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
  118. #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */
  119. #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */
  120. #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/
  121. #ifdef CONFIG_CPU_FREQ
  122. struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
  123. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
  124. void cpufreq_cpu_put(struct cpufreq_policy *policy);
  125. #else
  126. static inline struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
  127. {
  128. return NULL;
  129. }
  130. static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  131. {
  132. return NULL;
  133. }
  134. static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
  135. #endif
  136. static inline bool policy_is_shared(struct cpufreq_policy *policy)
  137. {
  138. return cpumask_weight(policy->cpus) > 1;
  139. }
  140. /* /sys/devices/system/cpu/cpufreq: entry point for global variables */
  141. extern struct kobject *cpufreq_global_kobject;
  142. #ifdef CONFIG_CPU_FREQ
  143. unsigned int cpufreq_get(unsigned int cpu);
  144. unsigned int cpufreq_quick_get(unsigned int cpu);
  145. unsigned int cpufreq_quick_get_max(unsigned int cpu);
  146. void disable_cpufreq(void);
  147. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
  148. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
  149. int cpufreq_update_policy(unsigned int cpu);
  150. bool have_governor_per_policy(void);
  151. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
  152. void cpufreq_enable_fast_switch(struct cpufreq_policy *policy);
  153. void cpufreq_disable_fast_switch(struct cpufreq_policy *policy);
  154. #else
  155. static inline unsigned int cpufreq_get(unsigned int cpu)
  156. {
  157. return 0;
  158. }
  159. static inline unsigned int cpufreq_quick_get(unsigned int cpu)
  160. {
  161. return 0;
  162. }
  163. static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
  164. {
  165. return 0;
  166. }
  167. static inline void disable_cpufreq(void) { }
  168. #endif
  169. #ifdef CONFIG_CPU_FREQ_STAT
  170. void cpufreq_stats_create_table(struct cpufreq_policy *policy);
  171. void cpufreq_stats_free_table(struct cpufreq_policy *policy);
  172. void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  173. unsigned int new_freq);
  174. #else
  175. static inline void cpufreq_stats_create_table(struct cpufreq_policy *policy) { }
  176. static inline void cpufreq_stats_free_table(struct cpufreq_policy *policy) { }
  177. static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  178. unsigned int new_freq) { }
  179. #endif /* CONFIG_CPU_FREQ_STAT */
  180. /*********************************************************************
  181. * CPUFREQ DRIVER INTERFACE *
  182. *********************************************************************/
  183. #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */
  184. #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */
  185. #define CPUFREQ_RELATION_C 2 /* closest frequency to target */
  186. struct freq_attr {
  187. struct attribute attr;
  188. ssize_t (*show)(struct cpufreq_policy *, char *);
  189. ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
  190. };
  191. #define cpufreq_freq_attr_ro(_name) \
  192. static struct freq_attr _name = \
  193. __ATTR(_name, 0444, show_##_name, NULL)
  194. #define cpufreq_freq_attr_ro_perm(_name, _perm) \
  195. static struct freq_attr _name = \
  196. __ATTR(_name, _perm, show_##_name, NULL)
  197. #define cpufreq_freq_attr_rw(_name) \
  198. static struct freq_attr _name = \
  199. __ATTR(_name, 0644, show_##_name, store_##_name)
  200. struct global_attr {
  201. struct attribute attr;
  202. ssize_t (*show)(struct kobject *kobj,
  203. struct attribute *attr, char *buf);
  204. ssize_t (*store)(struct kobject *a, struct attribute *b,
  205. const char *c, size_t count);
  206. };
  207. #define define_one_global_ro(_name) \
  208. static struct global_attr _name = \
  209. __ATTR(_name, 0444, show_##_name, NULL)
  210. #define define_one_global_rw(_name) \
  211. static struct global_attr _name = \
  212. __ATTR(_name, 0644, show_##_name, store_##_name)
  213. struct cpufreq_driver {
  214. char name[CPUFREQ_NAME_LEN];
  215. u8 flags;
  216. void *driver_data;
  217. /* needed by all drivers */
  218. int (*init)(struct cpufreq_policy *policy);
  219. int (*verify)(struct cpufreq_policy *policy);
  220. /* define one out of two */
  221. int (*setpolicy)(struct cpufreq_policy *policy);
  222. /*
  223. * On failure, should always restore frequency to policy->restore_freq
  224. * (i.e. old freq).
  225. */
  226. int (*target)(struct cpufreq_policy *policy,
  227. unsigned int target_freq,
  228. unsigned int relation); /* Deprecated */
  229. int (*target_index)(struct cpufreq_policy *policy,
  230. unsigned int index);
  231. unsigned int (*fast_switch)(struct cpufreq_policy *policy,
  232. unsigned int target_freq);
  233. /*
  234. * Caches and returns the lowest driver-supported frequency greater than
  235. * or equal to the target frequency, subject to any driver limitations.
  236. * Does not set the frequency. Only to be implemented for drivers with
  237. * target().
  238. */
  239. unsigned int (*resolve_freq)(struct cpufreq_policy *policy,
  240. unsigned int target_freq);
  241. /*
  242. * Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION
  243. * unset.
  244. *
  245. * get_intermediate should return a stable intermediate frequency
  246. * platform wants to switch to and target_intermediate() should set CPU
  247. * to to that frequency, before jumping to the frequency corresponding
  248. * to 'index'. Core will take care of sending notifications and driver
  249. * doesn't have to handle them in target_intermediate() or
  250. * target_index().
  251. *
  252. * Drivers can return '0' from get_intermediate() in case they don't
  253. * wish to switch to intermediate frequency for some target frequency.
  254. * In that case core will directly call ->target_index().
  255. */
  256. unsigned int (*get_intermediate)(struct cpufreq_policy *policy,
  257. unsigned int index);
  258. int (*target_intermediate)(struct cpufreq_policy *policy,
  259. unsigned int index);
  260. /* should be defined, if possible */
  261. unsigned int (*get)(unsigned int cpu);
  262. /* optional */
  263. int (*bios_limit)(int cpu, unsigned int *limit);
  264. int (*exit)(struct cpufreq_policy *policy);
  265. void (*stop_cpu)(struct cpufreq_policy *policy);
  266. int (*suspend)(struct cpufreq_policy *policy);
  267. int (*resume)(struct cpufreq_policy *policy);
  268. /* Will be called after the driver is fully initialized */
  269. void (*ready)(struct cpufreq_policy *policy);
  270. struct freq_attr **attr;
  271. /* platform specific boost support code */
  272. bool boost_enabled;
  273. int (*set_boost)(int state);
  274. };
  275. /* flags */
  276. #define CPUFREQ_STICKY (1 << 0) /* driver isn't removed even if
  277. all ->init() calls failed */
  278. #define CPUFREQ_CONST_LOOPS (1 << 1) /* loops_per_jiffy or other
  279. kernel "constants" aren't
  280. affected by frequency
  281. transitions */
  282. #define CPUFREQ_PM_NO_WARN (1 << 2) /* don't warn on suspend/resume
  283. speed mismatches */
  284. /*
  285. * This should be set by platforms having multiple clock-domains, i.e.
  286. * supporting multiple policies. With this sysfs directories of governor would
  287. * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
  288. * governor with different tunables for different clusters.
  289. */
  290. #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY (1 << 3)
  291. /*
  292. * Driver will do POSTCHANGE notifications from outside of their ->target()
  293. * routine and so must set cpufreq_driver->flags with this flag, so that core
  294. * can handle them specially.
  295. */
  296. #define CPUFREQ_ASYNC_NOTIFICATION (1 << 4)
  297. /*
  298. * Set by drivers which want cpufreq core to check if CPU is running at a
  299. * frequency present in freq-table exposed by the driver. For these drivers if
  300. * CPU is found running at an out of table freq, we will try to set it to a freq
  301. * from the table. And if that fails, we will stop further boot process by
  302. * issuing a BUG_ON().
  303. */
  304. #define CPUFREQ_NEED_INITIAL_FREQ_CHECK (1 << 5)
  305. int cpufreq_register_driver(struct cpufreq_driver *driver_data);
  306. int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
  307. const char *cpufreq_get_current_driver(void);
  308. void *cpufreq_get_driver_data(void);
  309. static inline void cpufreq_verify_within_limits(struct cpufreq_policy *policy,
  310. unsigned int min, unsigned int max)
  311. {
  312. if (policy->min < min)
  313. policy->min = min;
  314. if (policy->max < min)
  315. policy->max = min;
  316. if (policy->min > max)
  317. policy->min = max;
  318. if (policy->max > max)
  319. policy->max = max;
  320. if (policy->min > policy->max)
  321. policy->min = policy->max;
  322. return;
  323. }
  324. static inline void
  325. cpufreq_verify_within_cpu_limits(struct cpufreq_policy *policy)
  326. {
  327. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  328. policy->cpuinfo.max_freq);
  329. }
  330. #ifdef CONFIG_CPU_FREQ
  331. void cpufreq_suspend(void);
  332. void cpufreq_resume(void);
  333. int cpufreq_generic_suspend(struct cpufreq_policy *policy);
  334. #else
  335. static inline void cpufreq_suspend(void) {}
  336. static inline void cpufreq_resume(void) {}
  337. #endif
  338. /*********************************************************************
  339. * CPUFREQ NOTIFIER INTERFACE *
  340. *********************************************************************/
  341. #define CPUFREQ_TRANSITION_NOTIFIER (0)
  342. #define CPUFREQ_POLICY_NOTIFIER (1)
  343. /* Transition notifiers */
  344. #define CPUFREQ_PRECHANGE (0)
  345. #define CPUFREQ_POSTCHANGE (1)
  346. /* Policy Notifiers */
  347. #define CPUFREQ_ADJUST (0)
  348. #define CPUFREQ_NOTIFY (1)
  349. #define CPUFREQ_START (2)
  350. #define CPUFREQ_CREATE_POLICY (3)
  351. #define CPUFREQ_REMOVE_POLICY (4)
  352. #ifdef CONFIG_CPU_FREQ
  353. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
  354. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
  355. void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
  356. struct cpufreq_freqs *freqs);
  357. void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
  358. struct cpufreq_freqs *freqs, int transition_failed);
  359. #else /* CONFIG_CPU_FREQ */
  360. static inline int cpufreq_register_notifier(struct notifier_block *nb,
  361. unsigned int list)
  362. {
  363. return 0;
  364. }
  365. static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
  366. unsigned int list)
  367. {
  368. return 0;
  369. }
  370. #endif /* !CONFIG_CPU_FREQ */
  371. /**
  372. * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
  373. * safe)
  374. * @old: old value
  375. * @div: divisor
  376. * @mult: multiplier
  377. *
  378. *
  379. * new = old * mult / div
  380. */
  381. static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
  382. u_int mult)
  383. {
  384. #if BITS_PER_LONG == 32
  385. u64 result = ((u64) old) * ((u64) mult);
  386. do_div(result, div);
  387. return (unsigned long) result;
  388. #elif BITS_PER_LONG == 64
  389. unsigned long result = old * ((u64) mult);
  390. result /= div;
  391. return result;
  392. #endif
  393. }
  394. /*********************************************************************
  395. * CPUFREQ GOVERNORS *
  396. *********************************************************************/
  397. /*
  398. * If (cpufreq_driver->target) exists, the ->governor decides what frequency
  399. * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
  400. * two generic policies are available:
  401. */
  402. #define CPUFREQ_POLICY_POWERSAVE (1)
  403. #define CPUFREQ_POLICY_PERFORMANCE (2)
  404. /*
  405. * The polling frequency depends on the capability of the processor. Default
  406. * polling frequency is 1000 times the transition latency of the processor. The
  407. * ondemand governor will work on any processor with transition latency <= 10ms,
  408. * using appropriate sampling rate.
  409. *
  410. * For CPUs with transition latency > 10ms (mostly drivers with CPUFREQ_ETERNAL)
  411. * the ondemand governor will not work. All times here are in us (microseconds).
  412. */
  413. #define MIN_SAMPLING_RATE_RATIO (2)
  414. #define LATENCY_MULTIPLIER (1000)
  415. #define MIN_LATENCY_MULTIPLIER (20)
  416. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  417. struct cpufreq_governor {
  418. char name[CPUFREQ_NAME_LEN];
  419. int (*init)(struct cpufreq_policy *policy);
  420. void (*exit)(struct cpufreq_policy *policy);
  421. int (*start)(struct cpufreq_policy *policy);
  422. void (*stop)(struct cpufreq_policy *policy);
  423. void (*limits)(struct cpufreq_policy *policy);
  424. ssize_t (*show_setspeed) (struct cpufreq_policy *policy,
  425. char *buf);
  426. int (*store_setspeed) (struct cpufreq_policy *policy,
  427. unsigned int freq);
  428. unsigned int max_transition_latency; /* HW must be able to switch to
  429. next freq faster than this value in nano secs or we
  430. will fallback to performance governor */
  431. struct list_head governor_list;
  432. struct module *owner;
  433. };
  434. /* Pass a target to the cpufreq driver */
  435. unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
  436. unsigned int target_freq);
  437. int cpufreq_driver_target(struct cpufreq_policy *policy,
  438. unsigned int target_freq,
  439. unsigned int relation);
  440. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  441. unsigned int target_freq,
  442. unsigned int relation);
  443. unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
  444. unsigned int target_freq);
  445. int cpufreq_register_governor(struct cpufreq_governor *governor);
  446. void cpufreq_unregister_governor(struct cpufreq_governor *governor);
  447. struct cpufreq_governor *cpufreq_default_governor(void);
  448. struct cpufreq_governor *cpufreq_fallback_governor(void);
  449. static inline void cpufreq_policy_apply_limits(struct cpufreq_policy *policy)
  450. {
  451. if (policy->max < policy->cur)
  452. __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
  453. else if (policy->min > policy->cur)
  454. __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
  455. }
  456. /* Governor attribute set */
  457. struct gov_attr_set {
  458. struct kobject kobj;
  459. struct list_head policy_list;
  460. struct mutex update_lock;
  461. int usage_count;
  462. };
  463. /* sysfs ops for cpufreq governors */
  464. extern const struct sysfs_ops governor_sysfs_ops;
  465. void gov_attr_set_init(struct gov_attr_set *attr_set, struct list_head *list_node);
  466. void gov_attr_set_get(struct gov_attr_set *attr_set, struct list_head *list_node);
  467. unsigned int gov_attr_set_put(struct gov_attr_set *attr_set, struct list_head *list_node);
  468. /* Governor sysfs attribute */
  469. struct governor_attr {
  470. struct attribute attr;
  471. ssize_t (*show)(struct gov_attr_set *attr_set, char *buf);
  472. ssize_t (*store)(struct gov_attr_set *attr_set, const char *buf,
  473. size_t count);
  474. };
  475. /*********************************************************************
  476. * FREQUENCY TABLE HELPERS *
  477. *********************************************************************/
  478. /* Special Values of .frequency field */
  479. #define CPUFREQ_ENTRY_INVALID ~0u
  480. #define CPUFREQ_TABLE_END ~1u
  481. /* Special Values of .flags field */
  482. #define CPUFREQ_BOOST_FREQ (1 << 0)
  483. struct cpufreq_frequency_table {
  484. unsigned int flags;
  485. unsigned int driver_data; /* driver specific data, not used by core */
  486. unsigned int frequency; /* kHz - doesn't need to be in ascending
  487. * order */
  488. };
  489. #if defined(CONFIG_CPU_FREQ) && defined(CONFIG_PM_OPP)
  490. int dev_pm_opp_init_cpufreq_table(struct device *dev,
  491. struct cpufreq_frequency_table **table);
  492. void dev_pm_opp_free_cpufreq_table(struct device *dev,
  493. struct cpufreq_frequency_table **table);
  494. #else
  495. static inline int dev_pm_opp_init_cpufreq_table(struct device *dev,
  496. struct cpufreq_frequency_table
  497. **table)
  498. {
  499. return -EINVAL;
  500. }
  501. static inline void dev_pm_opp_free_cpufreq_table(struct device *dev,
  502. struct cpufreq_frequency_table
  503. **table)
  504. {
  505. }
  506. #endif
  507. /*
  508. * cpufreq_for_each_entry - iterate over a cpufreq_frequency_table
  509. * @pos: the cpufreq_frequency_table * to use as a loop cursor.
  510. * @table: the cpufreq_frequency_table * to iterate over.
  511. */
  512. #define cpufreq_for_each_entry(pos, table) \
  513. for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++)
  514. /*
  515. * cpufreq_for_each_valid_entry - iterate over a cpufreq_frequency_table
  516. * excluding CPUFREQ_ENTRY_INVALID frequencies.
  517. * @pos: the cpufreq_frequency_table * to use as a loop cursor.
  518. * @table: the cpufreq_frequency_table * to iterate over.
  519. */
  520. #define cpufreq_for_each_valid_entry(pos, table) \
  521. for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++) \
  522. if (pos->frequency == CPUFREQ_ENTRY_INVALID) \
  523. continue; \
  524. else
  525. int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  526. struct cpufreq_frequency_table *table);
  527. int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  528. struct cpufreq_frequency_table *table);
  529. int cpufreq_generic_frequency_table_verify(struct cpufreq_policy *policy);
  530. int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
  531. unsigned int target_freq,
  532. unsigned int relation);
  533. int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
  534. unsigned int freq);
  535. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
  536. #ifdef CONFIG_CPU_FREQ
  537. int cpufreq_boost_trigger_state(int state);
  538. int cpufreq_boost_enabled(void);
  539. int cpufreq_enable_boost_support(void);
  540. bool policy_has_boost_freq(struct cpufreq_policy *policy);
  541. /* Find lowest freq at or above target in a table in ascending order */
  542. static inline int cpufreq_table_find_index_al(struct cpufreq_policy *policy,
  543. unsigned int target_freq)
  544. {
  545. struct cpufreq_frequency_table *table = policy->freq_table;
  546. struct cpufreq_frequency_table *pos, *best = table - 1;
  547. unsigned int freq;
  548. cpufreq_for_each_valid_entry(pos, table) {
  549. freq = pos->frequency;
  550. if (freq >= target_freq)
  551. return pos - table;
  552. best = pos;
  553. }
  554. return best - table;
  555. }
  556. /* Find lowest freq at or above target in a table in descending order */
  557. static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy,
  558. unsigned int target_freq)
  559. {
  560. struct cpufreq_frequency_table *table = policy->freq_table;
  561. struct cpufreq_frequency_table *pos, *best = table - 1;
  562. unsigned int freq;
  563. cpufreq_for_each_valid_entry(pos, table) {
  564. freq = pos->frequency;
  565. if (freq == target_freq)
  566. return pos - table;
  567. if (freq > target_freq) {
  568. best = pos;
  569. continue;
  570. }
  571. /* No freq found above target_freq */
  572. if (best == table - 1)
  573. return pos - table;
  574. return best - table;
  575. }
  576. return best - table;
  577. }
  578. /* Works only on sorted freq-tables */
  579. static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy,
  580. unsigned int target_freq)
  581. {
  582. target_freq = clamp_val(target_freq, policy->min, policy->max);
  583. if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
  584. return cpufreq_table_find_index_al(policy, target_freq);
  585. else
  586. return cpufreq_table_find_index_dl(policy, target_freq);
  587. }
  588. /* Find highest freq at or below target in a table in ascending order */
  589. static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy,
  590. unsigned int target_freq)
  591. {
  592. struct cpufreq_frequency_table *table = policy->freq_table;
  593. struct cpufreq_frequency_table *pos, *best = table - 1;
  594. unsigned int freq;
  595. cpufreq_for_each_valid_entry(pos, table) {
  596. freq = pos->frequency;
  597. if (freq == target_freq)
  598. return pos - table;
  599. if (freq < target_freq) {
  600. best = pos;
  601. continue;
  602. }
  603. /* No freq found below target_freq */
  604. if (best == table - 1)
  605. return pos - table;
  606. return best - table;
  607. }
  608. return best - table;
  609. }
  610. /* Find highest freq at or below target in a table in descending order */
  611. static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy,
  612. unsigned int target_freq)
  613. {
  614. struct cpufreq_frequency_table *table = policy->freq_table;
  615. struct cpufreq_frequency_table *pos, *best = table - 1;
  616. unsigned int freq;
  617. cpufreq_for_each_valid_entry(pos, table) {
  618. freq = pos->frequency;
  619. if (freq <= target_freq)
  620. return pos - table;
  621. best = pos;
  622. }
  623. return best - table;
  624. }
  625. /* Works only on sorted freq-tables */
  626. static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy,
  627. unsigned int target_freq)
  628. {
  629. target_freq = clamp_val(target_freq, policy->min, policy->max);
  630. if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
  631. return cpufreq_table_find_index_ah(policy, target_freq);
  632. else
  633. return cpufreq_table_find_index_dh(policy, target_freq);
  634. }
  635. /* Find closest freq to target in a table in ascending order */
  636. static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy,
  637. unsigned int target_freq)
  638. {
  639. struct cpufreq_frequency_table *table = policy->freq_table;
  640. struct cpufreq_frequency_table *pos, *best = table - 1;
  641. unsigned int freq;
  642. cpufreq_for_each_valid_entry(pos, table) {
  643. freq = pos->frequency;
  644. if (freq == target_freq)
  645. return pos - table;
  646. if (freq < target_freq) {
  647. best = pos;
  648. continue;
  649. }
  650. /* No freq found below target_freq */
  651. if (best == table - 1)
  652. return pos - table;
  653. /* Choose the closest freq */
  654. if (target_freq - best->frequency > freq - target_freq)
  655. return pos - table;
  656. return best - table;
  657. }
  658. return best - table;
  659. }
  660. /* Find closest freq to target in a table in descending order */
  661. static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy,
  662. unsigned int target_freq)
  663. {
  664. struct cpufreq_frequency_table *table = policy->freq_table;
  665. struct cpufreq_frequency_table *pos, *best = table - 1;
  666. unsigned int freq;
  667. cpufreq_for_each_valid_entry(pos, table) {
  668. freq = pos->frequency;
  669. if (freq == target_freq)
  670. return pos - table;
  671. if (freq > target_freq) {
  672. best = pos;
  673. continue;
  674. }
  675. /* No freq found above target_freq */
  676. if (best == table - 1)
  677. return pos - table;
  678. /* Choose the closest freq */
  679. if (best->frequency - target_freq > target_freq - freq)
  680. return pos - table;
  681. return best - table;
  682. }
  683. return best - table;
  684. }
  685. /* Works only on sorted freq-tables */
  686. static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
  687. unsigned int target_freq)
  688. {
  689. target_freq = clamp_val(target_freq, policy->min, policy->max);
  690. if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
  691. return cpufreq_table_find_index_ac(policy, target_freq);
  692. else
  693. return cpufreq_table_find_index_dc(policy, target_freq);
  694. }
  695. static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  696. unsigned int target_freq,
  697. unsigned int relation)
  698. {
  699. if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED))
  700. return cpufreq_table_index_unsorted(policy, target_freq,
  701. relation);
  702. switch (relation) {
  703. case CPUFREQ_RELATION_L:
  704. return cpufreq_table_find_index_l(policy, target_freq);
  705. case CPUFREQ_RELATION_H:
  706. return cpufreq_table_find_index_h(policy, target_freq);
  707. case CPUFREQ_RELATION_C:
  708. return cpufreq_table_find_index_c(policy, target_freq);
  709. default:
  710. pr_err("%s: Invalid relation: %d\n", __func__, relation);
  711. return -EINVAL;
  712. }
  713. }
  714. #else
  715. static inline int cpufreq_boost_trigger_state(int state)
  716. {
  717. return 0;
  718. }
  719. static inline int cpufreq_boost_enabled(void)
  720. {
  721. return 0;
  722. }
  723. static inline int cpufreq_enable_boost_support(void)
  724. {
  725. return -EINVAL;
  726. }
  727. static inline bool policy_has_boost_freq(struct cpufreq_policy *policy)
  728. {
  729. return false;
  730. }
  731. #endif
  732. /* the following are really really optional */
  733. extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
  734. extern struct freq_attr cpufreq_freq_attr_scaling_boost_freqs;
  735. extern struct freq_attr *cpufreq_generic_attr[];
  736. int cpufreq_table_validate_and_show(struct cpufreq_policy *policy,
  737. struct cpufreq_frequency_table *table);
  738. unsigned int cpufreq_generic_get(unsigned int cpu);
  739. int cpufreq_generic_init(struct cpufreq_policy *policy,
  740. struct cpufreq_frequency_table *table,
  741. unsigned int transition_latency);
  742. #endif /* _LINUX_CPUFREQ_H */