cpufreq_ondemand.c 14 KB

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