cpufreq.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/include/linux/cpufreq.h
  4. *
  5. * Copyright (C) 2001 Russell King
  6. * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
  7. */
  8. #ifndef _LINUX_CPUFREQ_H
  9. #define _LINUX_CPUFREQ_H
  10. #include <linux/clk.h>
  11. #include <linux/cpumask.h>
  12. #include <linux/completion.h>
  13. #include <linux/kobject.h>
  14. #include <linux/notifier.h>
  15. #include <linux/pm_qos.h>
  16. #include <linux/spinlock.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 accommodating '\n' in prints */
  30. #define CPUFREQ_NAME_PLEN (CPUFREQ_NAME_LEN + 1)
  31. struct cpufreq_governor;
  32. enum cpufreq_table_sorting {
  33. CPUFREQ_TABLE_UNSORTED,
  34. CPUFREQ_TABLE_SORTED_ASCENDING,
  35. CPUFREQ_TABLE_SORTED_DESCENDING
  36. };
  37. struct cpufreq_cpuinfo {
  38. unsigned int max_freq;
  39. unsigned int min_freq;
  40. /* in 10^(-9) s = nanoseconds */
  41. unsigned int transition_latency;
  42. };
  43. struct cpufreq_policy {
  44. /* CPUs sharing clock, require sw coordination */
  45. cpumask_var_t cpus; /* Online CPUs only */
  46. cpumask_var_t related_cpus; /* Online + Offline CPUs */
  47. cpumask_var_t real_cpus; /* Related and present */
  48. unsigned int shared_type; /* ACPI: ANY or ALL affected CPUs
  49. should set cpufreq */
  50. unsigned int cpu; /* cpu managing this policy, must be online */
  51. struct clk *clk;
  52. struct cpufreq_cpuinfo cpuinfo;/* see above */
  53. unsigned int min; /* in kHz */
  54. unsigned int max; /* in kHz */
  55. unsigned int cur; /* in kHz, only needed if cpufreq
  56. * governors are used */
  57. unsigned int restore_freq; /* = policy->cur before transition */
  58. unsigned int suspend_freq; /* freq to set during suspend */
  59. unsigned int policy; /* see above */
  60. unsigned int last_policy; /* policy before unplug */
  61. struct cpufreq_governor *governor; /* see below */
  62. void *governor_data;
  63. char last_governor[CPUFREQ_NAME_LEN]; /* last governor used */
  64. struct work_struct update; /* if update_policy() needs to be
  65. * called, but you're in IRQ context */
  66. struct freq_constraints constraints;
  67. struct freq_qos_request *min_freq_req;
  68. struct freq_qos_request *max_freq_req;
  69. struct cpufreq_frequency_table *freq_table;
  70. enum cpufreq_table_sorting freq_table_sorted;
  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. struct rw_semaphore rwsem;
  83. /*
  84. * Fast switch flags:
  85. * - fast_switch_possible should be set by the driver if it can
  86. * guarantee that frequency can be changed on any CPU sharing the
  87. * policy and that the change will affect all of the policy CPUs then.
  88. * - fast_switch_enabled is to be set by governors that support fast
  89. * frequency switching with the help of cpufreq_enable_fast_switch().
  90. */
  91. bool fast_switch_possible;
  92. bool fast_switch_enabled;
  93. /*
  94. * Set if the CPUFREQ_GOV_STRICT_TARGET flag is set for the current
  95. * governor.
  96. */
  97. bool strict_target;
  98. /*
  99. * Preferred average time interval between consecutive invocations of
  100. * the driver to set the frequency for this policy. To be set by the
  101. * scaling driver (0, which is the default, means no preference).
  102. */
  103. unsigned int transition_delay_us;
  104. /*
  105. * Remote DVFS flag (Not added to the driver structure as we don't want
  106. * to access another structure from scheduler hotpath).
  107. *
  108. * Should be set if CPUs can do DVFS on behalf of other CPUs from
  109. * different cpufreq policies.
  110. */
  111. bool dvfs_possible_from_any_cpu;
  112. /* Cached frequency lookup from cpufreq_driver_resolve_freq. */
  113. unsigned int cached_target_freq;
  114. unsigned int cached_resolved_idx;
  115. /* Synchronization for frequency transitions */
  116. bool transition_ongoing; /* Tracks transition status */
  117. spinlock_t transition_lock;
  118. wait_queue_head_t transition_wait;
  119. struct task_struct *transition_task; /* Task which is doing the transition */
  120. /* cpufreq-stats */
  121. struct cpufreq_stats *stats;
  122. /* For cpufreq driver's internal use */
  123. void *driver_data;
  124. /* Pointer to the cooling device if used for thermal mitigation */
  125. struct thermal_cooling_device *cdev;
  126. struct notifier_block nb_min;
  127. struct notifier_block nb_max;
  128. };
  129. /*
  130. * Used for passing new cpufreq policy data to the cpufreq driver's ->verify()
  131. * callback for sanitization. That callback is only expected to modify the min
  132. * and max values, if necessary, and specifically it must not update the
  133. * frequency table.
  134. */
  135. struct cpufreq_policy_data {
  136. struct cpufreq_cpuinfo cpuinfo;
  137. struct cpufreq_frequency_table *freq_table;
  138. unsigned int cpu;
  139. unsigned int min; /* in kHz */
  140. unsigned int max; /* in kHz */
  141. };
  142. struct cpufreq_freqs {
  143. struct cpufreq_policy *policy;
  144. unsigned int old;
  145. unsigned int new;
  146. u8 flags; /* flags of cpufreq_driver, see below. */
  147. };
  148. /* Only for ACPI */
  149. #define CPUFREQ_SHARED_TYPE_NONE (0) /* None */
  150. #define CPUFREQ_SHARED_TYPE_HW (1) /* HW does needed coordination */
  151. #define CPUFREQ_SHARED_TYPE_ALL (2) /* All dependent CPUs should set freq */
  152. #define CPUFREQ_SHARED_TYPE_ANY (3) /* Freq can be set from any dependent CPU*/
  153. #ifdef CONFIG_CPU_FREQ
  154. struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu);
  155. struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu);
  156. void cpufreq_cpu_put(struct cpufreq_policy *policy);
  157. #else
  158. static inline struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
  159. {
  160. return NULL;
  161. }
  162. static inline struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
  163. {
  164. return NULL;
  165. }
  166. static inline void cpufreq_cpu_put(struct cpufreq_policy *policy) { }
  167. #endif
  168. static inline bool policy_is_inactive(struct cpufreq_policy *policy)
  169. {
  170. return cpumask_empty(policy->cpus);
  171. }
  172. static inline bool policy_is_shared(struct cpufreq_policy *policy)
  173. {
  174. return cpumask_weight(policy->cpus) > 1;
  175. }
  176. #ifdef CONFIG_CPU_FREQ
  177. unsigned int cpufreq_get(unsigned int cpu);
  178. unsigned int cpufreq_quick_get(unsigned int cpu);
  179. unsigned int cpufreq_quick_get_max(unsigned int cpu);
  180. unsigned int cpufreq_get_hw_max_freq(unsigned int cpu);
  181. void disable_cpufreq(void);
  182. u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy);
  183. struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu);
  184. void cpufreq_cpu_release(struct cpufreq_policy *policy);
  185. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu);
  186. void refresh_frequency_limits(struct cpufreq_policy *policy);
  187. void cpufreq_update_policy(unsigned int cpu);
  188. void cpufreq_update_limits(unsigned int cpu);
  189. bool have_governor_per_policy(void);
  190. bool cpufreq_supports_freq_invariance(void);
  191. struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
  192. void cpufreq_enable_fast_switch(struct cpufreq_policy *policy);
  193. void cpufreq_disable_fast_switch(struct cpufreq_policy *policy);
  194. #else
  195. static inline unsigned int cpufreq_get(unsigned int cpu)
  196. {
  197. return 0;
  198. }
  199. static inline unsigned int cpufreq_quick_get(unsigned int cpu)
  200. {
  201. return 0;
  202. }
  203. static inline unsigned int cpufreq_quick_get_max(unsigned int cpu)
  204. {
  205. return 0;
  206. }
  207. static inline unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
  208. {
  209. return 0;
  210. }
  211. static inline bool cpufreq_supports_freq_invariance(void)
  212. {
  213. return false;
  214. }
  215. static inline void disable_cpufreq(void) { }
  216. #endif
  217. #ifdef CONFIG_CPU_FREQ_STAT
  218. void cpufreq_stats_create_table(struct cpufreq_policy *policy);
  219. void cpufreq_stats_free_table(struct cpufreq_policy *policy);
  220. void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  221. unsigned int new_freq);
  222. #else
  223. static inline void cpufreq_stats_create_table(struct cpufreq_policy *policy) { }
  224. static inline void cpufreq_stats_free_table(struct cpufreq_policy *policy) { }
  225. static inline void cpufreq_stats_record_transition(struct cpufreq_policy *policy,
  226. unsigned int new_freq) { }
  227. #endif /* CONFIG_CPU_FREQ_STAT */
  228. /*********************************************************************
  229. * CPUFREQ DRIVER INTERFACE *
  230. *********************************************************************/
  231. #define CPUFREQ_RELATION_L 0 /* lowest frequency at or above target */
  232. #define CPUFREQ_RELATION_H 1 /* highest frequency below or at target */
  233. #define CPUFREQ_RELATION_C 2 /* closest frequency to target */
  234. struct freq_attr {
  235. struct attribute attr;
  236. ssize_t (*show)(struct cpufreq_policy *, char *);
  237. ssize_t (*store)(struct cpufreq_policy *, const char *, size_t count);
  238. };
  239. #define cpufreq_freq_attr_ro(_name) \
  240. static struct freq_attr _name = \
  241. __ATTR(_name, 0444, show_##_name, NULL)
  242. #define cpufreq_freq_attr_ro_perm(_name, _perm) \
  243. static struct freq_attr _name = \
  244. __ATTR(_name, _perm, show_##_name, NULL)
  245. #define cpufreq_freq_attr_rw(_name) \
  246. static struct freq_attr _name = \
  247. __ATTR(_name, 0644, show_##_name, store_##_name)
  248. #define cpufreq_freq_attr_wo(_name) \
  249. static struct freq_attr _name = \
  250. __ATTR(_name, 0200, NULL, store_##_name)
  251. #define define_one_global_ro(_name) \
  252. static struct kobj_attribute _name = \
  253. __ATTR(_name, 0444, show_##_name, NULL)
  254. #define define_one_global_rw(_name) \
  255. static struct kobj_attribute _name = \
  256. __ATTR(_name, 0644, show_##_name, store_##_name)
  257. struct cpufreq_driver {
  258. char name[CPUFREQ_NAME_LEN];
  259. u16 flags;
  260. void *driver_data;
  261. /* needed by all drivers */
  262. int (*init)(struct cpufreq_policy *policy);
  263. int (*verify)(struct cpufreq_policy_data *policy);
  264. /* define one out of two */
  265. int (*setpolicy)(struct cpufreq_policy *policy);
  266. /*
  267. * On failure, should always restore frequency to policy->restore_freq
  268. * (i.e. old freq).
  269. */
  270. int (*target)(struct cpufreq_policy *policy,
  271. unsigned int target_freq,
  272. unsigned int relation); /* Deprecated */
  273. int (*target_index)(struct cpufreq_policy *policy,
  274. unsigned int index);
  275. unsigned int (*fast_switch)(struct cpufreq_policy *policy,
  276. unsigned int target_freq);
  277. /*
  278. * Caches and returns the lowest driver-supported frequency greater than
  279. * or equal to the target frequency, subject to any driver limitations.
  280. * Does not set the frequency. Only to be implemented for drivers with
  281. * target().
  282. */
  283. unsigned int (*resolve_freq)(struct cpufreq_policy *policy,
  284. unsigned int target_freq);
  285. /*
  286. * Only for drivers with target_index() and CPUFREQ_ASYNC_NOTIFICATION
  287. * unset.
  288. *
  289. * get_intermediate should return a stable intermediate frequency
  290. * platform wants to switch to and target_intermediate() should set CPU
  291. * to that frequency, before jumping to the frequency corresponding
  292. * to 'index'. Core will take care of sending notifications and driver
  293. * doesn't have to handle them in target_intermediate() or
  294. * target_index().
  295. *
  296. * Drivers can return '0' from get_intermediate() in case they don't
  297. * wish to switch to intermediate frequency for some target frequency.
  298. * In that case core will directly call ->target_index().
  299. */
  300. unsigned int (*get_intermediate)(struct cpufreq_policy *policy,
  301. unsigned int index);
  302. int (*target_intermediate)(struct cpufreq_policy *policy,
  303. unsigned int index);
  304. /* should be defined, if possible */
  305. unsigned int (*get)(unsigned int cpu);
  306. /* Called to update policy limits on firmware notifications. */
  307. void (*update_limits)(unsigned int cpu);
  308. /* optional */
  309. int (*bios_limit)(int cpu, unsigned int *limit);
  310. int (*online)(struct cpufreq_policy *policy);
  311. int (*offline)(struct cpufreq_policy *policy);
  312. int (*exit)(struct cpufreq_policy *policy);
  313. void (*stop_cpu)(struct cpufreq_policy *policy);
  314. int (*suspend)(struct cpufreq_policy *policy);
  315. int (*resume)(struct cpufreq_policy *policy);
  316. /* Will be called after the driver is fully initialized */
  317. void (*ready)(struct cpufreq_policy *policy);
  318. struct freq_attr **attr;
  319. /* platform specific boost support code */
  320. bool boost_enabled;
  321. int (*set_boost)(struct cpufreq_policy *policy, int state);
  322. };
  323. /* flags */
  324. /* driver isn't removed even if all ->init() calls failed */
  325. #define CPUFREQ_STICKY BIT(0)
  326. /* loops_per_jiffy or other kernel "constants" aren't affected by frequency transitions */
  327. #define CPUFREQ_CONST_LOOPS BIT(1)
  328. /* don't warn on suspend/resume speed mismatches */
  329. #define CPUFREQ_PM_NO_WARN BIT(2)
  330. /*
  331. * This should be set by platforms having multiple clock-domains, i.e.
  332. * supporting multiple policies. With this sysfs directories of governor would
  333. * be created in cpu/cpu<num>/cpufreq/ directory and so they can use the same
  334. * governor with different tunables for different clusters.
  335. */
  336. #define CPUFREQ_HAVE_GOVERNOR_PER_POLICY BIT(3)
  337. /*
  338. * Driver will do POSTCHANGE notifications from outside of their ->target()
  339. * routine and so must set cpufreq_driver->flags with this flag, so that core
  340. * can handle them specially.
  341. */
  342. #define CPUFREQ_ASYNC_NOTIFICATION BIT(4)
  343. /*
  344. * Set by drivers which want cpufreq core to check if CPU is running at a
  345. * frequency present in freq-table exposed by the driver. For these drivers if
  346. * CPU is found running at an out of table freq, we will try to set it to a freq
  347. * from the table. And if that fails, we will stop further boot process by
  348. * issuing a BUG_ON().
  349. */
  350. #define CPUFREQ_NEED_INITIAL_FREQ_CHECK BIT(5)
  351. /*
  352. * Set by drivers to disallow use of governors with "dynamic_switching" flag
  353. * set.
  354. */
  355. #define CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING BIT(6)
  356. /*
  357. * Set by drivers that want the core to automatically register the cpufreq
  358. * driver as a thermal cooling device.
  359. */
  360. #define CPUFREQ_IS_COOLING_DEV BIT(7)
  361. /*
  362. * Set by drivers that need to update internale upper and lower boundaries along
  363. * with the target frequency and so the core and governors should also invoke
  364. * the diver if the target frequency does not change, but the policy min or max
  365. * may have changed.
  366. */
  367. #define CPUFREQ_NEED_UPDATE_LIMITS BIT(8)
  368. int cpufreq_register_driver(struct cpufreq_driver *driver_data);
  369. int cpufreq_unregister_driver(struct cpufreq_driver *driver_data);
  370. bool cpufreq_driver_test_flags(u16 flags);
  371. const char *cpufreq_get_current_driver(void);
  372. void *cpufreq_get_driver_data(void);
  373. static inline int cpufreq_thermal_control_enabled(struct cpufreq_driver *drv)
  374. {
  375. return IS_ENABLED(CONFIG_CPU_THERMAL) &&
  376. (drv->flags & CPUFREQ_IS_COOLING_DEV);
  377. }
  378. static inline void cpufreq_verify_within_limits(struct cpufreq_policy_data *policy,
  379. unsigned int min,
  380. unsigned int max)
  381. {
  382. if (policy->min < min)
  383. policy->min = min;
  384. if (policy->max < min)
  385. policy->max = min;
  386. if (policy->min > max)
  387. policy->min = max;
  388. if (policy->max > max)
  389. policy->max = max;
  390. if (policy->min > policy->max)
  391. policy->min = policy->max;
  392. return;
  393. }
  394. static inline void
  395. cpufreq_verify_within_cpu_limits(struct cpufreq_policy_data *policy)
  396. {
  397. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  398. policy->cpuinfo.max_freq);
  399. }
  400. #ifdef CONFIG_CPU_FREQ
  401. void cpufreq_suspend(void);
  402. void cpufreq_resume(void);
  403. int cpufreq_generic_suspend(struct cpufreq_policy *policy);
  404. #else
  405. static inline void cpufreq_suspend(void) {}
  406. static inline void cpufreq_resume(void) {}
  407. #endif
  408. /*********************************************************************
  409. * CPUFREQ NOTIFIER INTERFACE *
  410. *********************************************************************/
  411. #define CPUFREQ_TRANSITION_NOTIFIER (0)
  412. #define CPUFREQ_POLICY_NOTIFIER (1)
  413. /* Transition notifiers */
  414. #define CPUFREQ_PRECHANGE (0)
  415. #define CPUFREQ_POSTCHANGE (1)
  416. /* Policy Notifiers */
  417. #define CPUFREQ_CREATE_POLICY (0)
  418. #define CPUFREQ_REMOVE_POLICY (1)
  419. #ifdef CONFIG_CPU_FREQ
  420. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list);
  421. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list);
  422. void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
  423. struct cpufreq_freqs *freqs);
  424. void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
  425. struct cpufreq_freqs *freqs, int transition_failed);
  426. #else /* CONFIG_CPU_FREQ */
  427. static inline int cpufreq_register_notifier(struct notifier_block *nb,
  428. unsigned int list)
  429. {
  430. return 0;
  431. }
  432. static inline int cpufreq_unregister_notifier(struct notifier_block *nb,
  433. unsigned int list)
  434. {
  435. return 0;
  436. }
  437. #endif /* !CONFIG_CPU_FREQ */
  438. /**
  439. * cpufreq_scale - "old * mult / div" calculation for large values (32-bit-arch
  440. * safe)
  441. * @old: old value
  442. * @div: divisor
  443. * @mult: multiplier
  444. *
  445. *
  446. * new = old * mult / div
  447. */
  448. static inline unsigned long cpufreq_scale(unsigned long old, u_int div,
  449. u_int mult)
  450. {
  451. #if BITS_PER_LONG == 32
  452. u64 result = ((u64) old) * ((u64) mult);
  453. do_div(result, div);
  454. return (unsigned long) result;
  455. #elif BITS_PER_LONG == 64
  456. unsigned long result = old * ((u64) mult);
  457. result /= div;
  458. return result;
  459. #endif
  460. }
  461. /*********************************************************************
  462. * CPUFREQ GOVERNORS *
  463. *********************************************************************/
  464. #define CPUFREQ_POLICY_UNKNOWN (0)
  465. /*
  466. * If (cpufreq_driver->target) exists, the ->governor decides what frequency
  467. * within the limits is used. If (cpufreq_driver->setpolicy> exists, these
  468. * two generic policies are available:
  469. */
  470. #define CPUFREQ_POLICY_POWERSAVE (1)
  471. #define CPUFREQ_POLICY_PERFORMANCE (2)
  472. /*
  473. * The polling frequency depends on the capability of the processor. Default
  474. * polling frequency is 1000 times the transition latency of the processor. The
  475. * ondemand governor will work on any processor with transition latency <= 10ms,
  476. * using appropriate sampling rate.
  477. */
  478. #define LATENCY_MULTIPLIER (1000)
  479. struct cpufreq_governor {
  480. char name[CPUFREQ_NAME_LEN];
  481. int (*init)(struct cpufreq_policy *policy);
  482. void (*exit)(struct cpufreq_policy *policy);
  483. int (*start)(struct cpufreq_policy *policy);
  484. void (*stop)(struct cpufreq_policy *policy);
  485. void (*limits)(struct cpufreq_policy *policy);
  486. ssize_t (*show_setspeed) (struct cpufreq_policy *policy,
  487. char *buf);
  488. int (*store_setspeed) (struct cpufreq_policy *policy,
  489. unsigned int freq);
  490. struct list_head governor_list;
  491. struct module *owner;
  492. u8 flags;
  493. };
  494. /* Governor flags */
  495. /* For governors which change frequency dynamically by themselves */
  496. #define CPUFREQ_GOV_DYNAMIC_SWITCHING BIT(0)
  497. /* For governors wanting the target frequency to be set exactly */
  498. #define CPUFREQ_GOV_STRICT_TARGET BIT(1)
  499. /* Pass a target to the cpufreq driver */
  500. unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
  501. unsigned int target_freq);
  502. int cpufreq_driver_target(struct cpufreq_policy *policy,
  503. unsigned int target_freq,
  504. unsigned int relation);
  505. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  506. unsigned int target_freq,
  507. unsigned int relation);
  508. unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
  509. unsigned int target_freq);
  510. unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy);
  511. int cpufreq_register_governor(struct cpufreq_governor *governor);
  512. void cpufreq_unregister_governor(struct cpufreq_governor *governor);
  513. int cpufreq_start_governor(struct cpufreq_policy *policy);
  514. void cpufreq_stop_governor(struct cpufreq_policy *policy);
  515. #define cpufreq_governor_init(__governor) \
  516. static int __init __governor##_init(void) \
  517. { \
  518. return cpufreq_register_governor(&__governor); \
  519. } \
  520. core_initcall(__governor##_init)
  521. #define cpufreq_governor_exit(__governor) \
  522. static void __exit __governor##_exit(void) \
  523. { \
  524. return cpufreq_unregister_governor(&__governor); \
  525. } \
  526. module_exit(__governor##_exit)
  527. struct cpufreq_governor *cpufreq_default_governor(void);
  528. struct cpufreq_governor *cpufreq_fallback_governor(void);
  529. static inline void cpufreq_policy_apply_limits(struct cpufreq_policy *policy)
  530. {
  531. if (policy->max < policy->cur)
  532. __cpufreq_driver_target(policy, policy->max, CPUFREQ_RELATION_H);
  533. else if (policy->min > policy->cur)
  534. __cpufreq_driver_target(policy, policy->min, CPUFREQ_RELATION_L);
  535. }
  536. /* Governor attribute set */
  537. struct gov_attr_set {
  538. struct kobject kobj;
  539. struct list_head policy_list;
  540. struct mutex update_lock;
  541. int usage_count;
  542. };
  543. /* sysfs ops for cpufreq governors */
  544. extern const struct sysfs_ops governor_sysfs_ops;
  545. void gov_attr_set_init(struct gov_attr_set *attr_set, struct list_head *list_node);
  546. void gov_attr_set_get(struct gov_attr_set *attr_set, struct list_head *list_node);
  547. unsigned int gov_attr_set_put(struct gov_attr_set *attr_set, struct list_head *list_node);
  548. /* Governor sysfs attribute */
  549. struct governor_attr {
  550. struct attribute attr;
  551. ssize_t (*show)(struct gov_attr_set *attr_set, char *buf);
  552. ssize_t (*store)(struct gov_attr_set *attr_set, const char *buf,
  553. size_t count);
  554. };
  555. /*********************************************************************
  556. * FREQUENCY TABLE HELPERS *
  557. *********************************************************************/
  558. /* Special Values of .frequency field */
  559. #define CPUFREQ_ENTRY_INVALID ~0u
  560. #define CPUFREQ_TABLE_END ~1u
  561. /* Special Values of .flags field */
  562. #define CPUFREQ_BOOST_FREQ (1 << 0)
  563. struct cpufreq_frequency_table {
  564. unsigned int flags;
  565. unsigned int driver_data; /* driver specific data, not used by core */
  566. unsigned int frequency; /* kHz - doesn't need to be in ascending
  567. * order */
  568. };
  569. #if defined(CONFIG_CPU_FREQ) && defined(CONFIG_PM_OPP)
  570. int dev_pm_opp_init_cpufreq_table(struct device *dev,
  571. struct cpufreq_frequency_table **table);
  572. void dev_pm_opp_free_cpufreq_table(struct device *dev,
  573. struct cpufreq_frequency_table **table);
  574. #else
  575. static inline int dev_pm_opp_init_cpufreq_table(struct device *dev,
  576. struct cpufreq_frequency_table
  577. **table)
  578. {
  579. return -EINVAL;
  580. }
  581. static inline void dev_pm_opp_free_cpufreq_table(struct device *dev,
  582. struct cpufreq_frequency_table
  583. **table)
  584. {
  585. }
  586. #endif
  587. /*
  588. * cpufreq_for_each_entry - iterate over a cpufreq_frequency_table
  589. * @pos: the cpufreq_frequency_table * to use as a loop cursor.
  590. * @table: the cpufreq_frequency_table * to iterate over.
  591. */
  592. #define cpufreq_for_each_entry(pos, table) \
  593. for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++)
  594. /*
  595. * cpufreq_for_each_entry_idx - iterate over a cpufreq_frequency_table
  596. * with index
  597. * @pos: the cpufreq_frequency_table * to use as a loop cursor.
  598. * @table: the cpufreq_frequency_table * to iterate over.
  599. * @idx: the table entry currently being processed
  600. */
  601. #define cpufreq_for_each_entry_idx(pos, table, idx) \
  602. for (pos = table, idx = 0; pos->frequency != CPUFREQ_TABLE_END; \
  603. pos++, idx++)
  604. /*
  605. * cpufreq_for_each_valid_entry - iterate over a cpufreq_frequency_table
  606. * excluding CPUFREQ_ENTRY_INVALID frequencies.
  607. * @pos: the cpufreq_frequency_table * to use as a loop cursor.
  608. * @table: the cpufreq_frequency_table * to iterate over.
  609. */
  610. #define cpufreq_for_each_valid_entry(pos, table) \
  611. for (pos = table; pos->frequency != CPUFREQ_TABLE_END; pos++) \
  612. if (pos->frequency == CPUFREQ_ENTRY_INVALID) \
  613. continue; \
  614. else
  615. /*
  616. * cpufreq_for_each_valid_entry_idx - iterate with index over a cpufreq
  617. * frequency_table excluding CPUFREQ_ENTRY_INVALID frequencies.
  618. * @pos: the cpufreq_frequency_table * to use as a loop cursor.
  619. * @table: the cpufreq_frequency_table * to iterate over.
  620. * @idx: the table entry currently being processed
  621. */
  622. #define cpufreq_for_each_valid_entry_idx(pos, table, idx) \
  623. cpufreq_for_each_entry_idx(pos, table, idx) \
  624. if (pos->frequency == CPUFREQ_ENTRY_INVALID) \
  625. continue; \
  626. else
  627. int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  628. struct cpufreq_frequency_table *table);
  629. int cpufreq_frequency_table_verify(struct cpufreq_policy_data *policy,
  630. struct cpufreq_frequency_table *table);
  631. int cpufreq_generic_frequency_table_verify(struct cpufreq_policy_data *policy);
  632. int cpufreq_table_index_unsorted(struct cpufreq_policy *policy,
  633. unsigned int target_freq,
  634. unsigned int relation);
  635. int cpufreq_frequency_table_get_index(struct cpufreq_policy *policy,
  636. unsigned int freq);
  637. ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf);
  638. #ifdef CONFIG_CPU_FREQ
  639. int cpufreq_boost_trigger_state(int state);
  640. int cpufreq_boost_enabled(void);
  641. int cpufreq_enable_boost_support(void);
  642. bool policy_has_boost_freq(struct cpufreq_policy *policy);
  643. /* Find lowest freq at or above target in a table in ascending order */
  644. static inline int cpufreq_table_find_index_al(struct cpufreq_policy *policy,
  645. unsigned int target_freq)
  646. {
  647. struct cpufreq_frequency_table *table = policy->freq_table;
  648. struct cpufreq_frequency_table *pos;
  649. unsigned int freq;
  650. int idx, best = -1;
  651. cpufreq_for_each_valid_entry_idx(pos, table, idx) {
  652. freq = pos->frequency;
  653. if (freq >= target_freq)
  654. return idx;
  655. best = idx;
  656. }
  657. return best;
  658. }
  659. /* Find lowest freq at or above target in a table in descending order */
  660. static inline int cpufreq_table_find_index_dl(struct cpufreq_policy *policy,
  661. unsigned int target_freq)
  662. {
  663. struct cpufreq_frequency_table *table = policy->freq_table;
  664. struct cpufreq_frequency_table *pos;
  665. unsigned int freq;
  666. int idx, best = -1;
  667. cpufreq_for_each_valid_entry_idx(pos, table, idx) {
  668. freq = pos->frequency;
  669. if (freq == target_freq)
  670. return idx;
  671. if (freq > target_freq) {
  672. best = idx;
  673. continue;
  674. }
  675. /* No freq found above target_freq */
  676. if (best == -1)
  677. return idx;
  678. return best;
  679. }
  680. return best;
  681. }
  682. /* Works only on sorted freq-tables */
  683. static inline int cpufreq_table_find_index_l(struct cpufreq_policy *policy,
  684. unsigned int target_freq)
  685. {
  686. target_freq = clamp_val(target_freq, policy->min, policy->max);
  687. if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
  688. return cpufreq_table_find_index_al(policy, target_freq);
  689. else
  690. return cpufreq_table_find_index_dl(policy, target_freq);
  691. }
  692. /* Find highest freq at or below target in a table in ascending order */
  693. static inline int cpufreq_table_find_index_ah(struct cpufreq_policy *policy,
  694. unsigned int target_freq)
  695. {
  696. struct cpufreq_frequency_table *table = policy->freq_table;
  697. struct cpufreq_frequency_table *pos;
  698. unsigned int freq;
  699. int idx, best = -1;
  700. cpufreq_for_each_valid_entry_idx(pos, table, idx) {
  701. freq = pos->frequency;
  702. if (freq == target_freq)
  703. return idx;
  704. if (freq < target_freq) {
  705. best = idx;
  706. continue;
  707. }
  708. /* No freq found below target_freq */
  709. if (best == -1)
  710. return idx;
  711. return best;
  712. }
  713. return best;
  714. }
  715. /* Find highest freq at or below target in a table in descending order */
  716. static inline int cpufreq_table_find_index_dh(struct cpufreq_policy *policy,
  717. unsigned int target_freq)
  718. {
  719. struct cpufreq_frequency_table *table = policy->freq_table;
  720. struct cpufreq_frequency_table *pos;
  721. unsigned int freq;
  722. int idx, best = -1;
  723. cpufreq_for_each_valid_entry_idx(pos, table, idx) {
  724. freq = pos->frequency;
  725. if (freq <= target_freq)
  726. return idx;
  727. best = idx;
  728. }
  729. return best;
  730. }
  731. /* Works only on sorted freq-tables */
  732. static inline int cpufreq_table_find_index_h(struct cpufreq_policy *policy,
  733. unsigned int target_freq)
  734. {
  735. target_freq = clamp_val(target_freq, policy->min, policy->max);
  736. if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
  737. return cpufreq_table_find_index_ah(policy, target_freq);
  738. else
  739. return cpufreq_table_find_index_dh(policy, target_freq);
  740. }
  741. /* Find closest freq to target in a table in ascending order */
  742. static inline int cpufreq_table_find_index_ac(struct cpufreq_policy *policy,
  743. unsigned int target_freq)
  744. {
  745. struct cpufreq_frequency_table *table = policy->freq_table;
  746. struct cpufreq_frequency_table *pos;
  747. unsigned int freq;
  748. int idx, best = -1;
  749. cpufreq_for_each_valid_entry_idx(pos, table, idx) {
  750. freq = pos->frequency;
  751. if (freq == target_freq)
  752. return idx;
  753. if (freq < target_freq) {
  754. best = idx;
  755. continue;
  756. }
  757. /* No freq found below target_freq */
  758. if (best == -1)
  759. return idx;
  760. /* Choose the closest freq */
  761. if (target_freq - table[best].frequency > freq - target_freq)
  762. return idx;
  763. return best;
  764. }
  765. return best;
  766. }
  767. /* Find closest freq to target in a table in descending order */
  768. static inline int cpufreq_table_find_index_dc(struct cpufreq_policy *policy,
  769. unsigned int target_freq)
  770. {
  771. struct cpufreq_frequency_table *table = policy->freq_table;
  772. struct cpufreq_frequency_table *pos;
  773. unsigned int freq;
  774. int idx, best = -1;
  775. cpufreq_for_each_valid_entry_idx(pos, table, idx) {
  776. freq = pos->frequency;
  777. if (freq == target_freq)
  778. return idx;
  779. if (freq > target_freq) {
  780. best = idx;
  781. continue;
  782. }
  783. /* No freq found above target_freq */
  784. if (best == -1)
  785. return idx;
  786. /* Choose the closest freq */
  787. if (table[best].frequency - target_freq > target_freq - freq)
  788. return idx;
  789. return best;
  790. }
  791. return best;
  792. }
  793. /* Works only on sorted freq-tables */
  794. static inline int cpufreq_table_find_index_c(struct cpufreq_policy *policy,
  795. unsigned int target_freq)
  796. {
  797. target_freq = clamp_val(target_freq, policy->min, policy->max);
  798. if (policy->freq_table_sorted == CPUFREQ_TABLE_SORTED_ASCENDING)
  799. return cpufreq_table_find_index_ac(policy, target_freq);
  800. else
  801. return cpufreq_table_find_index_dc(policy, target_freq);
  802. }
  803. static inline int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  804. unsigned int target_freq,
  805. unsigned int relation)
  806. {
  807. if (unlikely(policy->freq_table_sorted == CPUFREQ_TABLE_UNSORTED))
  808. return cpufreq_table_index_unsorted(policy, target_freq,
  809. relation);
  810. switch (relation) {
  811. case CPUFREQ_RELATION_L:
  812. return cpufreq_table_find_index_l(policy, target_freq);
  813. case CPUFREQ_RELATION_H:
  814. return cpufreq_table_find_index_h(policy, target_freq);
  815. case CPUFREQ_RELATION_C:
  816. return cpufreq_table_find_index_c(policy, target_freq);
  817. default:
  818. WARN_ON_ONCE(1);
  819. return 0;
  820. }
  821. }
  822. static inline int cpufreq_table_count_valid_entries(const struct cpufreq_policy *policy)
  823. {
  824. struct cpufreq_frequency_table *pos;
  825. int count = 0;
  826. if (unlikely(!policy->freq_table))
  827. return 0;
  828. cpufreq_for_each_valid_entry(pos, policy->freq_table)
  829. count++;
  830. return count;
  831. }
  832. #else
  833. static inline int cpufreq_boost_trigger_state(int state)
  834. {
  835. return 0;
  836. }
  837. static inline int cpufreq_boost_enabled(void)
  838. {
  839. return 0;
  840. }
  841. static inline int cpufreq_enable_boost_support(void)
  842. {
  843. return -EINVAL;
  844. }
  845. static inline bool policy_has_boost_freq(struct cpufreq_policy *policy)
  846. {
  847. return false;
  848. }
  849. #endif
  850. extern void arch_freq_prepare_all(void);
  851. extern unsigned int arch_freq_get_on_cpu(int cpu);
  852. #ifndef arch_set_freq_scale
  853. static __always_inline
  854. void arch_set_freq_scale(const struct cpumask *cpus,
  855. unsigned long cur_freq,
  856. unsigned long max_freq)
  857. {
  858. }
  859. #endif
  860. /* the following are really really optional */
  861. extern struct freq_attr cpufreq_freq_attr_scaling_available_freqs;
  862. extern struct freq_attr cpufreq_freq_attr_scaling_boost_freqs;
  863. extern struct freq_attr *cpufreq_generic_attr[];
  864. int cpufreq_table_validate_and_sort(struct cpufreq_policy *policy);
  865. unsigned int cpufreq_generic_get(unsigned int cpu);
  866. void cpufreq_generic_init(struct cpufreq_policy *policy,
  867. struct cpufreq_frequency_table *table,
  868. unsigned int transition_latency);
  869. #endif /* _LINUX_CPUFREQ_H */