cpufreq_ondemand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * drivers/cpufreq/cpufreq_ondemand.c
  3. *
  4. * Copyright (C) 2001 Russell King
  5. * (C) 2003 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>.
  6. * Jun Nakajima <jun.nakajima@intel.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/cpu.h>
  14. #include <linux/percpu-defs.h>
  15. #include <linux/slab.h>
  16. #include <linux/tick.h>
  17. #include "cpufreq_governor.h"
  18. /* On-demand governor macros */
  19. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  20. #define DEF_SAMPLING_DOWN_FACTOR (1)
  21. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  22. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  23. #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
  24. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  25. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  26. static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
  27. static struct od_ops od_ops;
  28. static unsigned int default_powersave_bias;
  29. static void ondemand_powersave_bias_init_cpu(int cpu)
  30. {
  31. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  32. dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
  33. dbs_info->freq_lo = 0;
  34. }
  35. /*
  36. * Not all CPUs want IO time to be accounted as busy; this depends on how
  37. * efficient idling at a higher frequency/voltage is.
  38. * Pavel Machek says this is not so for various generations of AMD and old
  39. * Intel systems.
  40. * Mike Chan (android.com) claims this is also not true for ARM.
  41. * Because of this, whitelist specific known (series) of CPUs by default, and
  42. * leave all others up to the user.
  43. */
  44. static int should_io_be_busy(void)
  45. {
  46. #if defined(CONFIG_X86)
  47. /*
  48. * For Intel, Core 2 (model 15) and later have an efficient idle.
  49. */
  50. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  51. boot_cpu_data.x86 == 6 &&
  52. boot_cpu_data.x86_model >= 15)
  53. return 1;
  54. #endif
  55. return 0;
  56. }
  57. /*
  58. * Find right freq to be set now with powersave_bias on.
  59. * Returns the freq_hi to be used right now and will set freq_hi_delay_us,
  60. * freq_lo, and freq_lo_delay_us in percpu area for averaging freqs.
  61. */
  62. static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
  63. unsigned int freq_next, unsigned int relation)
  64. {
  65. unsigned int freq_req, freq_reduc, freq_avg;
  66. unsigned int freq_hi, freq_lo;
  67. unsigned int index = 0;
  68. unsigned int delay_hi_us;
  69. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  70. policy->cpu);
  71. struct policy_dbs_info *policy_dbs = policy->governor_data;
  72. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  73. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  74. if (!dbs_info->freq_table) {
  75. dbs_info->freq_lo = 0;
  76. dbs_info->freq_lo_delay_us = 0;
  77. return freq_next;
  78. }
  79. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  80. relation, &index);
  81. freq_req = dbs_info->freq_table[index].frequency;
  82. freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
  83. freq_avg = freq_req - freq_reduc;
  84. /* Find freq bounds for freq_avg in freq_table */
  85. index = 0;
  86. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  87. CPUFREQ_RELATION_H, &index);
  88. freq_lo = dbs_info->freq_table[index].frequency;
  89. index = 0;
  90. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  91. CPUFREQ_RELATION_L, &index);
  92. freq_hi = dbs_info->freq_table[index].frequency;
  93. /* Find out how long we have to be in hi and lo freqs */
  94. if (freq_hi == freq_lo) {
  95. dbs_info->freq_lo = 0;
  96. dbs_info->freq_lo_delay_us = 0;
  97. return freq_lo;
  98. }
  99. delay_hi_us = (freq_avg - freq_lo) * dbs_data->sampling_rate;
  100. delay_hi_us += (freq_hi - freq_lo) / 2;
  101. delay_hi_us /= freq_hi - freq_lo;
  102. dbs_info->freq_hi_delay_us = delay_hi_us;
  103. dbs_info->freq_lo = freq_lo;
  104. dbs_info->freq_lo_delay_us = dbs_data->sampling_rate - delay_hi_us;
  105. return freq_hi;
  106. }
  107. static void ondemand_powersave_bias_init(void)
  108. {
  109. int i;
  110. for_each_online_cpu(i) {
  111. ondemand_powersave_bias_init_cpu(i);
  112. }
  113. }
  114. static void dbs_freq_increase(struct cpufreq_policy *policy, unsigned int freq)
  115. {
  116. struct policy_dbs_info *policy_dbs = policy->governor_data;
  117. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  118. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  119. if (od_tuners->powersave_bias)
  120. freq = od_ops.powersave_bias_target(policy, freq,
  121. CPUFREQ_RELATION_H);
  122. else if (policy->cur == policy->max)
  123. return;
  124. __cpufreq_driver_target(policy, freq, od_tuners->powersave_bias ?
  125. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  126. }
  127. /*
  128. * Every sampling_rate, we check, if current idle time is less than 20%
  129. * (default), then we try to increase frequency. Else, we adjust the frequency
  130. * proportional to load.
  131. */
  132. static void od_update(struct cpufreq_policy *policy)
  133. {
  134. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
  135. struct policy_dbs_info *policy_dbs = dbs_info->cdbs.policy_dbs;
  136. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  137. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  138. unsigned int load = dbs_update(policy);
  139. dbs_info->freq_lo = 0;
  140. /* Check for frequency increase */
  141. if (load > dbs_data->up_threshold) {
  142. /* If switching to max speed, apply sampling_down_factor */
  143. if (policy->cur < policy->max)
  144. policy_dbs->rate_mult = dbs_data->sampling_down_factor;
  145. dbs_freq_increase(policy, policy->max);
  146. } else {
  147. /* Calculate the next frequency proportional to load */
  148. unsigned int freq_next, min_f, max_f;
  149. min_f = policy->cpuinfo.min_freq;
  150. max_f = policy->cpuinfo.max_freq;
  151. freq_next = min_f + load * (max_f - min_f) / 100;
  152. /* No longer fully busy, reset rate_mult */
  153. policy_dbs->rate_mult = 1;
  154. if (od_tuners->powersave_bias)
  155. freq_next = od_ops.powersave_bias_target(policy,
  156. freq_next,
  157. CPUFREQ_RELATION_L);
  158. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_C);
  159. }
  160. }
  161. static unsigned int od_dbs_timer(struct cpufreq_policy *policy)
  162. {
  163. struct policy_dbs_info *policy_dbs = policy->governor_data;
  164. struct dbs_data *dbs_data = policy_dbs->dbs_data;
  165. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
  166. int sample_type = dbs_info->sample_type;
  167. /* Common NORMAL_SAMPLE setup */
  168. dbs_info->sample_type = OD_NORMAL_SAMPLE;
  169. /*
  170. * OD_SUB_SAMPLE doesn't make sense if sample_delay_ns is 0, so ignore
  171. * it then.
  172. */
  173. if (sample_type == OD_SUB_SAMPLE && policy_dbs->sample_delay_ns > 0) {
  174. __cpufreq_driver_target(policy, dbs_info->freq_lo,
  175. CPUFREQ_RELATION_H);
  176. return dbs_info->freq_lo_delay_us;
  177. }
  178. od_update(policy);
  179. if (dbs_info->freq_lo) {
  180. /* Setup timer for SUB_SAMPLE */
  181. dbs_info->sample_type = OD_SUB_SAMPLE;
  182. return dbs_info->freq_hi_delay_us;
  183. }
  184. return dbs_data->sampling_rate * policy_dbs->rate_mult;
  185. }
  186. /************************** sysfs interface ************************/
  187. static struct dbs_governor od_dbs_gov;
  188. static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
  189. size_t count)
  190. {
  191. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  192. unsigned int input;
  193. int ret;
  194. unsigned int j;
  195. ret = sscanf(buf, "%u", &input);
  196. if (ret != 1)
  197. return -EINVAL;
  198. od_tuners->io_is_busy = !!input;
  199. /* we need to re-evaluate prev_cpu_idle */
  200. for_each_online_cpu(j) {
  201. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  202. j);
  203. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  204. &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
  205. }
  206. return count;
  207. }
  208. static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
  209. size_t count)
  210. {
  211. unsigned int input;
  212. int ret;
  213. ret = sscanf(buf, "%u", &input);
  214. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  215. input < MIN_FREQUENCY_UP_THRESHOLD) {
  216. return -EINVAL;
  217. }
  218. dbs_data->up_threshold = input;
  219. return count;
  220. }
  221. static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
  222. const char *buf, size_t count)
  223. {
  224. struct policy_dbs_info *policy_dbs;
  225. unsigned int input;
  226. int ret;
  227. ret = sscanf(buf, "%u", &input);
  228. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  229. return -EINVAL;
  230. dbs_data->sampling_down_factor = input;
  231. /* Reset down sampling multiplier in case it was active */
  232. list_for_each_entry(policy_dbs, &dbs_data->policy_dbs_list, list) {
  233. /*
  234. * Doing this without locking might lead to using different
  235. * rate_mult values in od_update() and od_dbs_timer().
  236. */
  237. mutex_lock(&policy_dbs->timer_mutex);
  238. policy_dbs->rate_mult = 1;
  239. mutex_unlock(&policy_dbs->timer_mutex);
  240. }
  241. return count;
  242. }
  243. static ssize_t store_ignore_nice_load(struct dbs_data *dbs_data,
  244. const char *buf, size_t count)
  245. {
  246. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  247. unsigned int input;
  248. int ret;
  249. unsigned int j;
  250. ret = sscanf(buf, "%u", &input);
  251. if (ret != 1)
  252. return -EINVAL;
  253. if (input > 1)
  254. input = 1;
  255. if (input == dbs_data->ignore_nice_load) { /* nothing to do */
  256. return count;
  257. }
  258. dbs_data->ignore_nice_load = input;
  259. /* we need to re-evaluate prev_cpu_idle */
  260. for_each_online_cpu(j) {
  261. struct od_cpu_dbs_info_s *dbs_info;
  262. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  263. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  264. &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
  265. if (dbs_data->ignore_nice_load)
  266. dbs_info->cdbs.prev_cpu_nice =
  267. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  268. }
  269. return count;
  270. }
  271. static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
  272. size_t count)
  273. {
  274. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  275. unsigned int input;
  276. int ret;
  277. ret = sscanf(buf, "%u", &input);
  278. if (ret != 1)
  279. return -EINVAL;
  280. if (input > 1000)
  281. input = 1000;
  282. od_tuners->powersave_bias = input;
  283. ondemand_powersave_bias_init();
  284. return count;
  285. }
  286. gov_show_one_common(sampling_rate);
  287. gov_show_one_common(up_threshold);
  288. gov_show_one_common(sampling_down_factor);
  289. gov_show_one_common(ignore_nice_load);
  290. gov_show_one_common(min_sampling_rate);
  291. gov_show_one(od, io_is_busy);
  292. gov_show_one(od, powersave_bias);
  293. gov_attr_rw(sampling_rate);
  294. gov_attr_rw(io_is_busy);
  295. gov_attr_rw(up_threshold);
  296. gov_attr_rw(sampling_down_factor);
  297. gov_attr_rw(ignore_nice_load);
  298. gov_attr_rw(powersave_bias);
  299. gov_attr_ro(min_sampling_rate);
  300. static struct attribute *od_attributes[] = {
  301. &min_sampling_rate.attr,
  302. &sampling_rate.attr,
  303. &up_threshold.attr,
  304. &sampling_down_factor.attr,
  305. &ignore_nice_load.attr,
  306. &powersave_bias.attr,
  307. &io_is_busy.attr,
  308. NULL
  309. };
  310. /************************** sysfs end ************************/
  311. static int od_init(struct dbs_data *dbs_data, bool notify)
  312. {
  313. struct od_dbs_tuners *tuners;
  314. u64 idle_time;
  315. int cpu;
  316. tuners = kzalloc(sizeof(*tuners), GFP_KERNEL);
  317. if (!tuners) {
  318. pr_err("%s: kzalloc failed\n", __func__);
  319. return -ENOMEM;
  320. }
  321. cpu = get_cpu();
  322. idle_time = get_cpu_idle_time_us(cpu, NULL);
  323. put_cpu();
  324. if (idle_time != -1ULL) {
  325. /* Idle micro accounting is supported. Use finer thresholds */
  326. dbs_data->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  327. /*
  328. * In nohz/micro accounting case we set the minimum frequency
  329. * not depending on HZ, but fixed (very low). The deferred
  330. * timer might skip some samples if idle/sleeping as needed.
  331. */
  332. dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  333. } else {
  334. dbs_data->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  335. /* For correct statistics, we need 10 ticks for each measure */
  336. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  337. jiffies_to_usecs(10);
  338. }
  339. dbs_data->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  340. dbs_data->ignore_nice_load = 0;
  341. tuners->powersave_bias = default_powersave_bias;
  342. tuners->io_is_busy = should_io_be_busy();
  343. dbs_data->tuners = tuners;
  344. return 0;
  345. }
  346. static void od_exit(struct dbs_data *dbs_data, bool notify)
  347. {
  348. kfree(dbs_data->tuners);
  349. }
  350. define_get_cpu_dbs_routines(od_cpu_dbs_info);
  351. static struct od_ops od_ops = {
  352. .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
  353. .powersave_bias_target = generic_powersave_bias_target,
  354. };
  355. static struct dbs_governor od_dbs_gov = {
  356. .gov = {
  357. .name = "ondemand",
  358. .governor = cpufreq_governor_dbs,
  359. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  360. .owner = THIS_MODULE,
  361. },
  362. .governor = GOV_ONDEMAND,
  363. .kobj_type = { .default_attrs = od_attributes },
  364. .get_cpu_cdbs = get_cpu_cdbs,
  365. .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
  366. .gov_dbs_timer = od_dbs_timer,
  367. .gov_ops = &od_ops,
  368. .init = od_init,
  369. .exit = od_exit,
  370. };
  371. #define CPU_FREQ_GOV_ONDEMAND (&od_dbs_gov.gov)
  372. static void od_set_powersave_bias(unsigned int powersave_bias)
  373. {
  374. struct cpufreq_policy *policy;
  375. struct dbs_data *dbs_data;
  376. struct od_dbs_tuners *od_tuners;
  377. unsigned int cpu;
  378. cpumask_t done;
  379. default_powersave_bias = powersave_bias;
  380. cpumask_clear(&done);
  381. get_online_cpus();
  382. for_each_online_cpu(cpu) {
  383. struct policy_dbs_info *policy_dbs;
  384. if (cpumask_test_cpu(cpu, &done))
  385. continue;
  386. policy_dbs = per_cpu(od_cpu_dbs_info, cpu).cdbs.policy_dbs;
  387. if (!policy_dbs)
  388. continue;
  389. policy = policy_dbs->policy;
  390. cpumask_or(&done, &done, policy->cpus);
  391. if (policy->governor != CPU_FREQ_GOV_ONDEMAND)
  392. continue;
  393. dbs_data = policy_dbs->dbs_data;
  394. od_tuners = dbs_data->tuners;
  395. od_tuners->powersave_bias = default_powersave_bias;
  396. }
  397. put_online_cpus();
  398. }
  399. void od_register_powersave_bias_handler(unsigned int (*f)
  400. (struct cpufreq_policy *, unsigned int, unsigned int),
  401. unsigned int powersave_bias)
  402. {
  403. od_ops.powersave_bias_target = f;
  404. od_set_powersave_bias(powersave_bias);
  405. }
  406. EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
  407. void od_unregister_powersave_bias_handler(void)
  408. {
  409. od_ops.powersave_bias_target = generic_powersave_bias_target;
  410. od_set_powersave_bias(0);
  411. }
  412. EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
  413. static int __init cpufreq_gov_dbs_init(void)
  414. {
  415. return cpufreq_register_governor(CPU_FREQ_GOV_ONDEMAND);
  416. }
  417. static void __exit cpufreq_gov_dbs_exit(void)
  418. {
  419. cpufreq_unregister_governor(CPU_FREQ_GOV_ONDEMAND);
  420. }
  421. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  422. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  423. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  424. "Low Latency Frequency Transition capable processors");
  425. MODULE_LICENSE("GPL");
  426. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  427. struct cpufreq_governor *cpufreq_default_governor(void)
  428. {
  429. return CPU_FREQ_GOV_ONDEMAND;
  430. }
  431. fs_initcall(cpufreq_gov_dbs_init);
  432. #else
  433. module_init(cpufreq_gov_dbs_init);
  434. #endif
  435. module_exit(cpufreq_gov_dbs_exit);