cpufreq_ondemand.c 14 KB

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