cpufreq_ondemand.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/cpu.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/mutex.h>
  20. #include <linux/hrtimer.h>
  21. #include <linux/tick.h>
  22. #include <linux/ktime.h>
  23. #include <linux/sched.h>
  24. /*
  25. * dbs is used in this file as a shortform for demandbased switching
  26. * It helps to keep variable names smaller, simpler
  27. */
  28. #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
  29. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  30. #define DEF_SAMPLING_DOWN_FACTOR (1)
  31. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  32. #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
  33. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  34. #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
  35. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  36. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  37. /*
  38. * The polling frequency of this governor depends on the capability of
  39. * the processor. Default polling frequency is 1000 times the transition
  40. * latency of the processor. The governor will work on any processor with
  41. * transition latency <= 10mS, using appropriate sampling
  42. * rate.
  43. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  44. * this governor will not work.
  45. * All times here are in uS.
  46. */
  47. #define MIN_SAMPLING_RATE_RATIO (2)
  48. static unsigned int min_sampling_rate;
  49. #define LATENCY_MULTIPLIER (1000)
  50. #define MIN_LATENCY_MULTIPLIER (100)
  51. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  52. static void do_dbs_timer(struct work_struct *work);
  53. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  54. unsigned int event);
  55. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  56. static
  57. #endif
  58. struct cpufreq_governor cpufreq_gov_ondemand = {
  59. .name = "ondemand",
  60. .governor = cpufreq_governor_dbs,
  61. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  62. .owner = THIS_MODULE,
  63. };
  64. /* Sampling types */
  65. enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
  66. struct cpu_dbs_info_s {
  67. cputime64_t prev_cpu_idle;
  68. cputime64_t prev_cpu_iowait;
  69. cputime64_t prev_cpu_wall;
  70. cputime64_t prev_cpu_nice;
  71. struct cpufreq_policy *cur_policy;
  72. struct delayed_work work;
  73. struct cpufreq_frequency_table *freq_table;
  74. unsigned int freq_lo;
  75. unsigned int freq_lo_jiffies;
  76. unsigned int freq_hi_jiffies;
  77. unsigned int rate_mult;
  78. int cpu;
  79. unsigned int sample_type:1;
  80. /*
  81. * percpu mutex that serializes governor limit change with
  82. * do_dbs_timer invocation. We do not want do_dbs_timer to run
  83. * when user is changing the governor or limits.
  84. */
  85. struct mutex timer_mutex;
  86. };
  87. static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info);
  88. static unsigned int dbs_enable; /* number of CPUs using this policy */
  89. /*
  90. * dbs_mutex protects data in dbs_tuners_ins from concurrent changes on
  91. * different CPUs. It protects dbs_enable in governor start/stop.
  92. */
  93. static DEFINE_MUTEX(dbs_mutex);
  94. static struct dbs_tuners {
  95. unsigned int sampling_rate;
  96. unsigned int up_threshold;
  97. unsigned int down_differential;
  98. unsigned int ignore_nice;
  99. unsigned int sampling_down_factor;
  100. unsigned int powersave_bias;
  101. unsigned int io_is_busy;
  102. } dbs_tuners_ins = {
  103. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  104. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  105. .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
  106. .ignore_nice = 0,
  107. .powersave_bias = 0,
  108. };
  109. static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
  110. cputime64_t *wall)
  111. {
  112. cputime64_t idle_time;
  113. cputime64_t cur_wall_time;
  114. cputime64_t busy_time;
  115. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  116. busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
  117. kstat_cpu(cpu).cpustat.system);
  118. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
  119. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
  120. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
  121. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
  122. idle_time = cputime64_sub(cur_wall_time, busy_time);
  123. if (wall)
  124. *wall = (cputime64_t)jiffies_to_usecs(cur_wall_time);
  125. return (cputime64_t)jiffies_to_usecs(idle_time);
  126. }
  127. static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
  128. {
  129. u64 idle_time = get_cpu_idle_time_us(cpu, wall);
  130. if (idle_time == -1ULL)
  131. return get_cpu_idle_time_jiffy(cpu, wall);
  132. return idle_time;
  133. }
  134. static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
  135. {
  136. u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
  137. if (iowait_time == -1ULL)
  138. return 0;
  139. return iowait_time;
  140. }
  141. /*
  142. * Find right freq to be set now with powersave_bias on.
  143. * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
  144. * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
  145. */
  146. static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
  147. unsigned int freq_next,
  148. unsigned int relation)
  149. {
  150. unsigned int freq_req, freq_reduc, freq_avg;
  151. unsigned int freq_hi, freq_lo;
  152. unsigned int index = 0;
  153. unsigned int jiffies_total, jiffies_hi, jiffies_lo;
  154. struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  155. policy->cpu);
  156. if (!dbs_info->freq_table) {
  157. dbs_info->freq_lo = 0;
  158. dbs_info->freq_lo_jiffies = 0;
  159. return freq_next;
  160. }
  161. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  162. relation, &index);
  163. freq_req = dbs_info->freq_table[index].frequency;
  164. freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
  165. freq_avg = freq_req - freq_reduc;
  166. /* Find freq bounds for freq_avg in freq_table */
  167. index = 0;
  168. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  169. CPUFREQ_RELATION_H, &index);
  170. freq_lo = dbs_info->freq_table[index].frequency;
  171. index = 0;
  172. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  173. CPUFREQ_RELATION_L, &index);
  174. freq_hi = dbs_info->freq_table[index].frequency;
  175. /* Find out how long we have to be in hi and lo freqs */
  176. if (freq_hi == freq_lo) {
  177. dbs_info->freq_lo = 0;
  178. dbs_info->freq_lo_jiffies = 0;
  179. return freq_lo;
  180. }
  181. jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  182. jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
  183. jiffies_hi += ((freq_hi - freq_lo) / 2);
  184. jiffies_hi /= (freq_hi - freq_lo);
  185. jiffies_lo = jiffies_total - jiffies_hi;
  186. dbs_info->freq_lo = freq_lo;
  187. dbs_info->freq_lo_jiffies = jiffies_lo;
  188. dbs_info->freq_hi_jiffies = jiffies_hi;
  189. return freq_hi;
  190. }
  191. static void ondemand_powersave_bias_init_cpu(int cpu)
  192. {
  193. struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  194. dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
  195. dbs_info->freq_lo = 0;
  196. }
  197. static void ondemand_powersave_bias_init(void)
  198. {
  199. int i;
  200. for_each_online_cpu(i) {
  201. ondemand_powersave_bias_init_cpu(i);
  202. }
  203. }
  204. /************************** sysfs interface ************************/
  205. static ssize_t show_sampling_rate_max(struct kobject *kobj,
  206. struct attribute *attr, char *buf)
  207. {
  208. printk_once(KERN_INFO "CPUFREQ: ondemand sampling_rate_max "
  209. "sysfs file is deprecated - used by: %s\n", current->comm);
  210. return sprintf(buf, "%u\n", -1U);
  211. }
  212. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  213. struct attribute *attr, char *buf)
  214. {
  215. return sprintf(buf, "%u\n", min_sampling_rate);
  216. }
  217. define_one_global_ro(sampling_rate_max);
  218. define_one_global_ro(sampling_rate_min);
  219. /* cpufreq_ondemand Governor Tunables */
  220. #define show_one(file_name, object) \
  221. static ssize_t show_##file_name \
  222. (struct kobject *kobj, struct attribute *attr, char *buf) \
  223. { \
  224. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  225. }
  226. show_one(sampling_rate, sampling_rate);
  227. show_one(io_is_busy, io_is_busy);
  228. show_one(up_threshold, up_threshold);
  229. show_one(sampling_down_factor, sampling_down_factor);
  230. show_one(ignore_nice_load, ignore_nice);
  231. show_one(powersave_bias, powersave_bias);
  232. /*** delete after deprecation time ***/
  233. #define DEPRECATION_MSG(file_name) \
  234. printk_once(KERN_INFO "CPUFREQ: Per core ondemand sysfs " \
  235. "interface is deprecated - " #file_name "\n");
  236. #define show_one_old(file_name) \
  237. static ssize_t show_##file_name##_old \
  238. (struct cpufreq_policy *unused, char *buf) \
  239. { \
  240. printk_once(KERN_INFO "CPUFREQ: Per core ondemand sysfs " \
  241. "interface is deprecated - " #file_name "\n"); \
  242. return show_##file_name(NULL, NULL, buf); \
  243. }
  244. show_one_old(sampling_rate);
  245. show_one_old(up_threshold);
  246. show_one_old(ignore_nice_load);
  247. show_one_old(powersave_bias);
  248. show_one_old(sampling_rate_min);
  249. show_one_old(sampling_rate_max);
  250. cpufreq_freq_attr_ro_old(sampling_rate_min);
  251. cpufreq_freq_attr_ro_old(sampling_rate_max);
  252. /*** delete after deprecation time ***/
  253. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  254. const char *buf, size_t count)
  255. {
  256. unsigned int input;
  257. int ret;
  258. ret = sscanf(buf, "%u", &input);
  259. if (ret != 1)
  260. return -EINVAL;
  261. mutex_lock(&dbs_mutex);
  262. dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
  263. mutex_unlock(&dbs_mutex);
  264. return count;
  265. }
  266. static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
  267. const char *buf, size_t count)
  268. {
  269. unsigned int input;
  270. int ret;
  271. ret = sscanf(buf, "%u", &input);
  272. if (ret != 1)
  273. return -EINVAL;
  274. mutex_lock(&dbs_mutex);
  275. dbs_tuners_ins.io_is_busy = !!input;
  276. mutex_unlock(&dbs_mutex);
  277. return count;
  278. }
  279. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  280. const char *buf, size_t count)
  281. {
  282. unsigned int input;
  283. int ret;
  284. ret = sscanf(buf, "%u", &input);
  285. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  286. input < MIN_FREQUENCY_UP_THRESHOLD) {
  287. return -EINVAL;
  288. }
  289. mutex_lock(&dbs_mutex);
  290. dbs_tuners_ins.up_threshold = input;
  291. mutex_unlock(&dbs_mutex);
  292. return count;
  293. }
  294. static ssize_t store_sampling_down_factor(struct kobject *a,
  295. struct attribute *b, const char *buf, size_t count)
  296. {
  297. unsigned int input, j;
  298. int ret;
  299. ret = sscanf(buf, "%u", &input);
  300. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  301. return -EINVAL;
  302. mutex_lock(&dbs_mutex);
  303. dbs_tuners_ins.sampling_down_factor = input;
  304. /* Reset down sampling multiplier in case it was active */
  305. for_each_online_cpu(j) {
  306. struct cpu_dbs_info_s *dbs_info;
  307. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  308. dbs_info->rate_mult = 1;
  309. }
  310. mutex_unlock(&dbs_mutex);
  311. return count;
  312. }
  313. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  314. const char *buf, size_t count)
  315. {
  316. unsigned int input;
  317. int ret;
  318. unsigned int j;
  319. ret = sscanf(buf, "%u", &input);
  320. if (ret != 1)
  321. return -EINVAL;
  322. if (input > 1)
  323. input = 1;
  324. mutex_lock(&dbs_mutex);
  325. if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
  326. mutex_unlock(&dbs_mutex);
  327. return count;
  328. }
  329. dbs_tuners_ins.ignore_nice = input;
  330. /* we need to re-evaluate prev_cpu_idle */
  331. for_each_online_cpu(j) {
  332. struct cpu_dbs_info_s *dbs_info;
  333. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  334. dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  335. &dbs_info->prev_cpu_wall);
  336. if (dbs_tuners_ins.ignore_nice)
  337. dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
  338. }
  339. mutex_unlock(&dbs_mutex);
  340. return count;
  341. }
  342. static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
  343. const char *buf, size_t count)
  344. {
  345. unsigned int input;
  346. int ret;
  347. ret = sscanf(buf, "%u", &input);
  348. if (ret != 1)
  349. return -EINVAL;
  350. if (input > 1000)
  351. input = 1000;
  352. mutex_lock(&dbs_mutex);
  353. dbs_tuners_ins.powersave_bias = input;
  354. ondemand_powersave_bias_init();
  355. mutex_unlock(&dbs_mutex);
  356. return count;
  357. }
  358. define_one_global_rw(sampling_rate);
  359. define_one_global_rw(io_is_busy);
  360. define_one_global_rw(up_threshold);
  361. define_one_global_rw(sampling_down_factor);
  362. define_one_global_rw(ignore_nice_load);
  363. define_one_global_rw(powersave_bias);
  364. static struct attribute *dbs_attributes[] = {
  365. &sampling_rate_max.attr,
  366. &sampling_rate_min.attr,
  367. &sampling_rate.attr,
  368. &up_threshold.attr,
  369. &sampling_down_factor.attr,
  370. &ignore_nice_load.attr,
  371. &powersave_bias.attr,
  372. &io_is_busy.attr,
  373. NULL
  374. };
  375. static struct attribute_group dbs_attr_group = {
  376. .attrs = dbs_attributes,
  377. .name = "ondemand",
  378. };
  379. /*** delete after deprecation time ***/
  380. #define write_one_old(file_name) \
  381. static ssize_t store_##file_name##_old \
  382. (struct cpufreq_policy *unused, const char *buf, size_t count) \
  383. { \
  384. printk_once(KERN_INFO "CPUFREQ: Per core ondemand sysfs " \
  385. "interface is deprecated - " #file_name "\n"); \
  386. return store_##file_name(NULL, NULL, buf, count); \
  387. }
  388. write_one_old(sampling_rate);
  389. write_one_old(up_threshold);
  390. write_one_old(ignore_nice_load);
  391. write_one_old(powersave_bias);
  392. cpufreq_freq_attr_rw_old(sampling_rate);
  393. cpufreq_freq_attr_rw_old(up_threshold);
  394. cpufreq_freq_attr_rw_old(ignore_nice_load);
  395. cpufreq_freq_attr_rw_old(powersave_bias);
  396. static struct attribute *dbs_attributes_old[] = {
  397. &sampling_rate_max_old.attr,
  398. &sampling_rate_min_old.attr,
  399. &sampling_rate_old.attr,
  400. &up_threshold_old.attr,
  401. &ignore_nice_load_old.attr,
  402. &powersave_bias_old.attr,
  403. NULL
  404. };
  405. static struct attribute_group dbs_attr_group_old = {
  406. .attrs = dbs_attributes_old,
  407. .name = "ondemand",
  408. };
  409. /*** delete after deprecation time ***/
  410. /************************** sysfs end ************************/
  411. static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
  412. {
  413. if (dbs_tuners_ins.powersave_bias)
  414. freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
  415. else if (p->cur == p->max)
  416. return;
  417. __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
  418. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  419. }
  420. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  421. {
  422. unsigned int max_load_freq;
  423. struct cpufreq_policy *policy;
  424. unsigned int j;
  425. this_dbs_info->freq_lo = 0;
  426. policy = this_dbs_info->cur_policy;
  427. /*
  428. * Every sampling_rate, we check, if current idle time is less
  429. * than 20% (default), then we try to increase frequency
  430. * Every sampling_rate, we look for a the lowest
  431. * frequency which can sustain the load while keeping idle time over
  432. * 30%. If such a frequency exist, we try to decrease to this frequency.
  433. *
  434. * Any frequency increase takes it to the maximum frequency.
  435. * Frequency reduction happens at minimum steps of
  436. * 5% (default) of current frequency
  437. */
  438. /* Get Absolute Load - in terms of freq */
  439. max_load_freq = 0;
  440. for_each_cpu(j, policy->cpus) {
  441. struct cpu_dbs_info_s *j_dbs_info;
  442. cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
  443. unsigned int idle_time, wall_time, iowait_time;
  444. unsigned int load, load_freq;
  445. int freq_avg;
  446. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  447. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
  448. cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
  449. wall_time = (unsigned int) cputime64_sub(cur_wall_time,
  450. j_dbs_info->prev_cpu_wall);
  451. j_dbs_info->prev_cpu_wall = cur_wall_time;
  452. idle_time = (unsigned int) cputime64_sub(cur_idle_time,
  453. j_dbs_info->prev_cpu_idle);
  454. j_dbs_info->prev_cpu_idle = cur_idle_time;
  455. iowait_time = (unsigned int) cputime64_sub(cur_iowait_time,
  456. j_dbs_info->prev_cpu_iowait);
  457. j_dbs_info->prev_cpu_iowait = cur_iowait_time;
  458. if (dbs_tuners_ins.ignore_nice) {
  459. cputime64_t cur_nice;
  460. unsigned long cur_nice_jiffies;
  461. cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
  462. j_dbs_info->prev_cpu_nice);
  463. /*
  464. * Assumption: nice time between sampling periods will
  465. * be less than 2^32 jiffies for 32 bit sys
  466. */
  467. cur_nice_jiffies = (unsigned long)
  468. cputime64_to_jiffies64(cur_nice);
  469. j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
  470. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  471. }
  472. /*
  473. * For the purpose of ondemand, waiting for disk IO is an
  474. * indication that you're performance critical, and not that
  475. * the system is actually idle. So subtract the iowait time
  476. * from the cpu idle time.
  477. */
  478. if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
  479. idle_time -= iowait_time;
  480. if (unlikely(!wall_time || wall_time < idle_time))
  481. continue;
  482. load = 100 * (wall_time - idle_time) / wall_time;
  483. freq_avg = __cpufreq_driver_getavg(policy, j);
  484. if (freq_avg <= 0)
  485. freq_avg = policy->cur;
  486. load_freq = load * freq_avg;
  487. if (load_freq > max_load_freq)
  488. max_load_freq = load_freq;
  489. }
  490. /* Check for frequency increase */
  491. if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
  492. /* If switching to max speed, apply sampling_down_factor */
  493. if (policy->cur < policy->max)
  494. this_dbs_info->rate_mult =
  495. dbs_tuners_ins.sampling_down_factor;
  496. dbs_freq_increase(policy, policy->max);
  497. return;
  498. }
  499. /* Check for frequency decrease */
  500. /* if we cannot reduce the frequency anymore, break out early */
  501. if (policy->cur == policy->min)
  502. return;
  503. /*
  504. * The optimal frequency is the frequency that is the lowest that
  505. * can support the current CPU usage without triggering the up
  506. * policy. To be safe, we focus 10 points under the threshold.
  507. */
  508. if (max_load_freq <
  509. (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
  510. policy->cur) {
  511. unsigned int freq_next;
  512. freq_next = max_load_freq /
  513. (dbs_tuners_ins.up_threshold -
  514. dbs_tuners_ins.down_differential);
  515. /* No longer fully busy, reset rate_mult */
  516. this_dbs_info->rate_mult = 1;
  517. if (freq_next < policy->min)
  518. freq_next = policy->min;
  519. if (!dbs_tuners_ins.powersave_bias) {
  520. __cpufreq_driver_target(policy, freq_next,
  521. CPUFREQ_RELATION_L);
  522. } else {
  523. int freq = powersave_bias_target(policy, freq_next,
  524. CPUFREQ_RELATION_L);
  525. __cpufreq_driver_target(policy, freq,
  526. CPUFREQ_RELATION_L);
  527. }
  528. }
  529. }
  530. static void do_dbs_timer(struct work_struct *work)
  531. {
  532. struct cpu_dbs_info_s *dbs_info =
  533. container_of(work, struct cpu_dbs_info_s, work.work);
  534. unsigned int cpu = dbs_info->cpu;
  535. int sample_type = dbs_info->sample_type;
  536. /* We want all CPUs to do sampling nearly on same jiffy */
  537. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
  538. * dbs_info->rate_mult);
  539. if (num_online_cpus() > 1)
  540. delay -= jiffies % delay;
  541. mutex_lock(&dbs_info->timer_mutex);
  542. /* Common NORMAL_SAMPLE setup */
  543. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  544. if (!dbs_tuners_ins.powersave_bias ||
  545. sample_type == DBS_NORMAL_SAMPLE) {
  546. dbs_check_cpu(dbs_info);
  547. if (dbs_info->freq_lo) {
  548. /* Setup timer for SUB_SAMPLE */
  549. dbs_info->sample_type = DBS_SUB_SAMPLE;
  550. delay = dbs_info->freq_hi_jiffies;
  551. }
  552. } else {
  553. __cpufreq_driver_target(dbs_info->cur_policy,
  554. dbs_info->freq_lo, CPUFREQ_RELATION_H);
  555. }
  556. schedule_delayed_work_on(cpu, &dbs_info->work, delay);
  557. mutex_unlock(&dbs_info->timer_mutex);
  558. }
  559. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
  560. {
  561. /* We want all CPUs to do sampling nearly on same jiffy */
  562. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  563. if (num_online_cpus() > 1)
  564. delay -= jiffies % delay;
  565. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  566. INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
  567. schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
  568. }
  569. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  570. {
  571. cancel_delayed_work_sync(&dbs_info->work);
  572. }
  573. /*
  574. * Not all CPUs want IO time to be accounted as busy; this dependson how
  575. * efficient idling at a higher frequency/voltage is.
  576. * Pavel Machek says this is not so for various generations of AMD and old
  577. * Intel systems.
  578. * Mike Chan (androidlcom) calis this is also not true for ARM.
  579. * Because of this, whitelist specific known (series) of CPUs by default, and
  580. * leave all others up to the user.
  581. */
  582. static int should_io_be_busy(void)
  583. {
  584. #if defined(CONFIG_X86)
  585. /*
  586. * For Intel, Core 2 (model 15) andl later have an efficient idle.
  587. */
  588. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  589. boot_cpu_data.x86 == 6 &&
  590. boot_cpu_data.x86_model >= 15)
  591. return 1;
  592. #endif
  593. return 0;
  594. }
  595. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  596. unsigned int event)
  597. {
  598. unsigned int cpu = policy->cpu;
  599. struct cpu_dbs_info_s *this_dbs_info;
  600. unsigned int j;
  601. int rc;
  602. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  603. switch (event) {
  604. case CPUFREQ_GOV_START:
  605. if ((!cpu_online(cpu)) || (!policy->cur))
  606. return -EINVAL;
  607. mutex_lock(&dbs_mutex);
  608. rc = sysfs_create_group(&policy->kobj, &dbs_attr_group_old);
  609. if (rc) {
  610. mutex_unlock(&dbs_mutex);
  611. return rc;
  612. }
  613. dbs_enable++;
  614. for_each_cpu(j, policy->cpus) {
  615. struct cpu_dbs_info_s *j_dbs_info;
  616. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  617. j_dbs_info->cur_policy = policy;
  618. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  619. &j_dbs_info->prev_cpu_wall);
  620. if (dbs_tuners_ins.ignore_nice) {
  621. j_dbs_info->prev_cpu_nice =
  622. kstat_cpu(j).cpustat.nice;
  623. }
  624. }
  625. this_dbs_info->cpu = cpu;
  626. this_dbs_info->rate_mult = 1;
  627. ondemand_powersave_bias_init_cpu(cpu);
  628. /*
  629. * Start the timerschedule work, when this governor
  630. * is used for first time
  631. */
  632. if (dbs_enable == 1) {
  633. unsigned int latency;
  634. rc = sysfs_create_group(cpufreq_global_kobject,
  635. &dbs_attr_group);
  636. if (rc) {
  637. mutex_unlock(&dbs_mutex);
  638. return rc;
  639. }
  640. /* policy latency is in nS. Convert it to uS first */
  641. latency = policy->cpuinfo.transition_latency / 1000;
  642. if (latency == 0)
  643. latency = 1;
  644. /* Bring kernel and HW constraints together */
  645. min_sampling_rate = max(min_sampling_rate,
  646. MIN_LATENCY_MULTIPLIER * latency);
  647. dbs_tuners_ins.sampling_rate =
  648. max(min_sampling_rate,
  649. latency * LATENCY_MULTIPLIER);
  650. dbs_tuners_ins.io_is_busy = should_io_be_busy();
  651. }
  652. mutex_unlock(&dbs_mutex);
  653. mutex_init(&this_dbs_info->timer_mutex);
  654. dbs_timer_init(this_dbs_info);
  655. break;
  656. case CPUFREQ_GOV_STOP:
  657. dbs_timer_exit(this_dbs_info);
  658. mutex_lock(&dbs_mutex);
  659. sysfs_remove_group(&policy->kobj, &dbs_attr_group_old);
  660. mutex_destroy(&this_dbs_info->timer_mutex);
  661. dbs_enable--;
  662. mutex_unlock(&dbs_mutex);
  663. if (!dbs_enable)
  664. sysfs_remove_group(cpufreq_global_kobject,
  665. &dbs_attr_group);
  666. break;
  667. case CPUFREQ_GOV_LIMITS:
  668. mutex_lock(&this_dbs_info->timer_mutex);
  669. if (policy->max < this_dbs_info->cur_policy->cur)
  670. __cpufreq_driver_target(this_dbs_info->cur_policy,
  671. policy->max, CPUFREQ_RELATION_H);
  672. else if (policy->min > this_dbs_info->cur_policy->cur)
  673. __cpufreq_driver_target(this_dbs_info->cur_policy,
  674. policy->min, CPUFREQ_RELATION_L);
  675. mutex_unlock(&this_dbs_info->timer_mutex);
  676. break;
  677. }
  678. return 0;
  679. }
  680. static int __init cpufreq_gov_dbs_init(void)
  681. {
  682. cputime64_t wall;
  683. u64 idle_time;
  684. int cpu = get_cpu();
  685. idle_time = get_cpu_idle_time_us(cpu, &wall);
  686. put_cpu();
  687. if (idle_time != -1ULL) {
  688. /* Idle micro accounting is supported. Use finer thresholds */
  689. dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  690. dbs_tuners_ins.down_differential =
  691. MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  692. /*
  693. * In no_hz/micro accounting case we set the minimum frequency
  694. * not depending on HZ, but fixed (very low). The deferred
  695. * timer might skip some samples if idle/sleeping as needed.
  696. */
  697. min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  698. } else {
  699. /* For correct statistics, we need 10 ticks for each measure */
  700. min_sampling_rate =
  701. MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
  702. }
  703. return cpufreq_register_governor(&cpufreq_gov_ondemand);
  704. }
  705. static void __exit cpufreq_gov_dbs_exit(void)
  706. {
  707. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  708. }
  709. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  710. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  711. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  712. "Low Latency Frequency Transition capable processors");
  713. MODULE_LICENSE("GPL");
  714. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  715. fs_initcall(cpufreq_gov_dbs_init);
  716. #else
  717. module_init(cpufreq_gov_dbs_init);
  718. #endif
  719. module_exit(cpufreq_gov_dbs_exit);