cpufreq_ondemand.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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 dbs_enable in governor start/stop.
  91. */
  92. static DEFINE_MUTEX(dbs_mutex);
  93. static struct dbs_tuners {
  94. unsigned int sampling_rate;
  95. unsigned int up_threshold;
  96. unsigned int down_differential;
  97. unsigned int ignore_nice;
  98. unsigned int sampling_down_factor;
  99. unsigned int powersave_bias;
  100. unsigned int io_is_busy;
  101. } dbs_tuners_ins = {
  102. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  103. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  104. .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
  105. .ignore_nice = 0,
  106. .powersave_bias = 0,
  107. };
  108. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  109. {
  110. u64 idle_time;
  111. u64 cur_wall_time;
  112. u64 busy_time;
  113. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  114. busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
  115. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
  116. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
  117. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
  118. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
  119. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
  120. idle_time = cur_wall_time - busy_time;
  121. if (wall)
  122. *wall = jiffies_to_usecs(cur_wall_time);
  123. return jiffies_to_usecs(idle_time);
  124. }
  125. static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
  126. {
  127. u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
  128. if (idle_time == -1ULL)
  129. return get_cpu_idle_time_jiffy(cpu, wall);
  130. else
  131. idle_time += get_cpu_iowait_time_us(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_min(struct kobject *kobj,
  206. struct attribute *attr, char *buf)
  207. {
  208. return sprintf(buf, "%u\n", min_sampling_rate);
  209. }
  210. define_one_global_ro(sampling_rate_min);
  211. /* cpufreq_ondemand Governor Tunables */
  212. #define show_one(file_name, object) \
  213. static ssize_t show_##file_name \
  214. (struct kobject *kobj, struct attribute *attr, char *buf) \
  215. { \
  216. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  217. }
  218. show_one(sampling_rate, sampling_rate);
  219. show_one(io_is_busy, io_is_busy);
  220. show_one(up_threshold, up_threshold);
  221. show_one(sampling_down_factor, sampling_down_factor);
  222. show_one(ignore_nice_load, ignore_nice);
  223. show_one(powersave_bias, powersave_bias);
  224. /**
  225. * update_sampling_rate - update sampling rate effective immediately if needed.
  226. * @new_rate: new sampling rate
  227. *
  228. * If new rate is smaller than the old, simply updaing
  229. * dbs_tuners_int.sampling_rate might not be appropriate. For example,
  230. * if the original sampling_rate was 1 second and the requested new sampling
  231. * rate is 10 ms because the user needs immediate reaction from ondemand
  232. * governor, but not sure if higher frequency will be required or not,
  233. * then, the governor may change the sampling rate too late; up to 1 second
  234. * later. Thus, if we are reducing the sampling rate, we need to make the
  235. * new value effective immediately.
  236. */
  237. static void update_sampling_rate(unsigned int new_rate)
  238. {
  239. int cpu;
  240. dbs_tuners_ins.sampling_rate = new_rate
  241. = max(new_rate, min_sampling_rate);
  242. for_each_online_cpu(cpu) {
  243. struct cpufreq_policy *policy;
  244. struct cpu_dbs_info_s *dbs_info;
  245. unsigned long next_sampling, appointed_at;
  246. policy = cpufreq_cpu_get(cpu);
  247. if (!policy)
  248. continue;
  249. dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
  250. cpufreq_cpu_put(policy);
  251. mutex_lock(&dbs_info->timer_mutex);
  252. if (!delayed_work_pending(&dbs_info->work)) {
  253. mutex_unlock(&dbs_info->timer_mutex);
  254. continue;
  255. }
  256. next_sampling = jiffies + usecs_to_jiffies(new_rate);
  257. appointed_at = dbs_info->work.timer.expires;
  258. if (time_before(next_sampling, appointed_at)) {
  259. mutex_unlock(&dbs_info->timer_mutex);
  260. cancel_delayed_work_sync(&dbs_info->work);
  261. mutex_lock(&dbs_info->timer_mutex);
  262. schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work,
  263. usecs_to_jiffies(new_rate));
  264. }
  265. mutex_unlock(&dbs_info->timer_mutex);
  266. }
  267. }
  268. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  269. const char *buf, size_t count)
  270. {
  271. unsigned int input;
  272. int ret;
  273. ret = sscanf(buf, "%u", &input);
  274. if (ret != 1)
  275. return -EINVAL;
  276. update_sampling_rate(input);
  277. return count;
  278. }
  279. static ssize_t store_io_is_busy(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)
  286. return -EINVAL;
  287. dbs_tuners_ins.io_is_busy = !!input;
  288. return count;
  289. }
  290. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  291. const char *buf, size_t count)
  292. {
  293. unsigned int input;
  294. int ret;
  295. ret = sscanf(buf, "%u", &input);
  296. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  297. input < MIN_FREQUENCY_UP_THRESHOLD) {
  298. return -EINVAL;
  299. }
  300. dbs_tuners_ins.up_threshold = input;
  301. return count;
  302. }
  303. static ssize_t store_sampling_down_factor(struct kobject *a,
  304. struct attribute *b, const char *buf, size_t count)
  305. {
  306. unsigned int input, j;
  307. int ret;
  308. ret = sscanf(buf, "%u", &input);
  309. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  310. return -EINVAL;
  311. dbs_tuners_ins.sampling_down_factor = input;
  312. /* Reset down sampling multiplier in case it was active */
  313. for_each_online_cpu(j) {
  314. struct cpu_dbs_info_s *dbs_info;
  315. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  316. dbs_info->rate_mult = 1;
  317. }
  318. return count;
  319. }
  320. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  321. const char *buf, size_t count)
  322. {
  323. unsigned int input;
  324. int ret;
  325. unsigned int j;
  326. ret = sscanf(buf, "%u", &input);
  327. if (ret != 1)
  328. return -EINVAL;
  329. if (input > 1)
  330. input = 1;
  331. if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
  332. return count;
  333. }
  334. dbs_tuners_ins.ignore_nice = input;
  335. /* we need to re-evaluate prev_cpu_idle */
  336. for_each_online_cpu(j) {
  337. struct cpu_dbs_info_s *dbs_info;
  338. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  339. dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  340. &dbs_info->prev_cpu_wall);
  341. if (dbs_tuners_ins.ignore_nice)
  342. dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  343. }
  344. return count;
  345. }
  346. static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
  347. const char *buf, size_t count)
  348. {
  349. unsigned int input;
  350. int ret;
  351. ret = sscanf(buf, "%u", &input);
  352. if (ret != 1)
  353. return -EINVAL;
  354. if (input > 1000)
  355. input = 1000;
  356. dbs_tuners_ins.powersave_bias = input;
  357. ondemand_powersave_bias_init();
  358. return count;
  359. }
  360. define_one_global_rw(sampling_rate);
  361. define_one_global_rw(io_is_busy);
  362. define_one_global_rw(up_threshold);
  363. define_one_global_rw(sampling_down_factor);
  364. define_one_global_rw(ignore_nice_load);
  365. define_one_global_rw(powersave_bias);
  366. static struct attribute *dbs_attributes[] = {
  367. &sampling_rate_min.attr,
  368. &sampling_rate.attr,
  369. &up_threshold.attr,
  370. &sampling_down_factor.attr,
  371. &ignore_nice_load.attr,
  372. &powersave_bias.attr,
  373. &io_is_busy.attr,
  374. NULL
  375. };
  376. static struct attribute_group dbs_attr_group = {
  377. .attrs = dbs_attributes,
  378. .name = "ondemand",
  379. };
  380. /************************** sysfs end ************************/
  381. static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
  382. {
  383. if (dbs_tuners_ins.powersave_bias)
  384. freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
  385. else if (p->cur == p->max)
  386. return;
  387. __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
  388. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  389. }
  390. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  391. {
  392. unsigned int max_load_freq;
  393. struct cpufreq_policy *policy;
  394. unsigned int j;
  395. this_dbs_info->freq_lo = 0;
  396. policy = this_dbs_info->cur_policy;
  397. /*
  398. * Every sampling_rate, we check, if current idle time is less
  399. * than 20% (default), then we try to increase frequency
  400. * Every sampling_rate, we look for a the lowest
  401. * frequency which can sustain the load while keeping idle time over
  402. * 30%. If such a frequency exist, we try to decrease to this frequency.
  403. *
  404. * Any frequency increase takes it to the maximum frequency.
  405. * Frequency reduction happens at minimum steps of
  406. * 5% (default) of current frequency
  407. */
  408. /* Get Absolute Load - in terms of freq */
  409. max_load_freq = 0;
  410. for_each_cpu(j, policy->cpus) {
  411. struct cpu_dbs_info_s *j_dbs_info;
  412. cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
  413. unsigned int idle_time, wall_time, iowait_time;
  414. unsigned int load, load_freq;
  415. int freq_avg;
  416. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  417. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
  418. cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
  419. wall_time = (unsigned int)
  420. (cur_wall_time - j_dbs_info->prev_cpu_wall);
  421. j_dbs_info->prev_cpu_wall = cur_wall_time;
  422. idle_time = (unsigned int)
  423. (cur_idle_time - j_dbs_info->prev_cpu_idle);
  424. j_dbs_info->prev_cpu_idle = cur_idle_time;
  425. iowait_time = (unsigned int)
  426. (cur_iowait_time - j_dbs_info->prev_cpu_iowait);
  427. j_dbs_info->prev_cpu_iowait = cur_iowait_time;
  428. if (dbs_tuners_ins.ignore_nice) {
  429. u64 cur_nice;
  430. unsigned long cur_nice_jiffies;
  431. cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  432. j_dbs_info->prev_cpu_nice;
  433. /*
  434. * Assumption: nice time between sampling periods will
  435. * be less than 2^32 jiffies for 32 bit sys
  436. */
  437. cur_nice_jiffies = (unsigned long)
  438. cputime64_to_jiffies64(cur_nice);
  439. j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  440. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  441. }
  442. /*
  443. * For the purpose of ondemand, waiting for disk IO is an
  444. * indication that you're performance critical, and not that
  445. * the system is actually idle. So subtract the iowait time
  446. * from the cpu idle time.
  447. */
  448. if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
  449. idle_time -= iowait_time;
  450. if (unlikely(!wall_time || wall_time < idle_time))
  451. continue;
  452. load = 100 * (wall_time - idle_time) / wall_time;
  453. freq_avg = __cpufreq_driver_getavg(policy, j);
  454. if (freq_avg <= 0)
  455. freq_avg = policy->cur;
  456. load_freq = load * freq_avg;
  457. if (load_freq > max_load_freq)
  458. max_load_freq = load_freq;
  459. }
  460. /* Check for frequency increase */
  461. if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
  462. /* If switching to max speed, apply sampling_down_factor */
  463. if (policy->cur < policy->max)
  464. this_dbs_info->rate_mult =
  465. dbs_tuners_ins.sampling_down_factor;
  466. dbs_freq_increase(policy, policy->max);
  467. return;
  468. }
  469. /* Check for frequency decrease */
  470. /* if we cannot reduce the frequency anymore, break out early */
  471. if (policy->cur == policy->min)
  472. return;
  473. /*
  474. * The optimal frequency is the frequency that is the lowest that
  475. * can support the current CPU usage without triggering the up
  476. * policy. To be safe, we focus 10 points under the threshold.
  477. */
  478. if (max_load_freq <
  479. (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
  480. policy->cur) {
  481. unsigned int freq_next;
  482. freq_next = max_load_freq /
  483. (dbs_tuners_ins.up_threshold -
  484. dbs_tuners_ins.down_differential);
  485. /* No longer fully busy, reset rate_mult */
  486. this_dbs_info->rate_mult = 1;
  487. if (freq_next < policy->min)
  488. freq_next = policy->min;
  489. if (!dbs_tuners_ins.powersave_bias) {
  490. __cpufreq_driver_target(policy, freq_next,
  491. CPUFREQ_RELATION_L);
  492. } else {
  493. int freq = powersave_bias_target(policy, freq_next,
  494. CPUFREQ_RELATION_L);
  495. __cpufreq_driver_target(policy, freq,
  496. CPUFREQ_RELATION_L);
  497. }
  498. }
  499. }
  500. static void do_dbs_timer(struct work_struct *work)
  501. {
  502. struct cpu_dbs_info_s *dbs_info =
  503. container_of(work, struct cpu_dbs_info_s, work.work);
  504. unsigned int cpu = dbs_info->cpu;
  505. int sample_type = dbs_info->sample_type;
  506. int delay;
  507. mutex_lock(&dbs_info->timer_mutex);
  508. /* Common NORMAL_SAMPLE setup */
  509. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  510. if (!dbs_tuners_ins.powersave_bias ||
  511. sample_type == DBS_NORMAL_SAMPLE) {
  512. dbs_check_cpu(dbs_info);
  513. if (dbs_info->freq_lo) {
  514. /* Setup timer for SUB_SAMPLE */
  515. dbs_info->sample_type = DBS_SUB_SAMPLE;
  516. delay = dbs_info->freq_hi_jiffies;
  517. } else {
  518. /* We want all CPUs to do sampling nearly on
  519. * same jiffy
  520. */
  521. delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
  522. * dbs_info->rate_mult);
  523. if (num_online_cpus() > 1)
  524. delay -= jiffies % delay;
  525. }
  526. } else {
  527. __cpufreq_driver_target(dbs_info->cur_policy,
  528. dbs_info->freq_lo, CPUFREQ_RELATION_H);
  529. delay = dbs_info->freq_lo_jiffies;
  530. }
  531. schedule_delayed_work_on(cpu, &dbs_info->work, delay);
  532. mutex_unlock(&dbs_info->timer_mutex);
  533. }
  534. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
  535. {
  536. /* We want all CPUs to do sampling nearly on same jiffy */
  537. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  538. if (num_online_cpus() > 1)
  539. delay -= jiffies % delay;
  540. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  541. INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
  542. schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
  543. }
  544. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  545. {
  546. cancel_delayed_work_sync(&dbs_info->work);
  547. }
  548. /*
  549. * Not all CPUs want IO time to be accounted as busy; this dependson how
  550. * efficient idling at a higher frequency/voltage is.
  551. * Pavel Machek says this is not so for various generations of AMD and old
  552. * Intel systems.
  553. * Mike Chan (androidlcom) calis this is also not true for ARM.
  554. * Because of this, whitelist specific known (series) of CPUs by default, and
  555. * leave all others up to the user.
  556. */
  557. static int should_io_be_busy(void)
  558. {
  559. #if defined(CONFIG_X86)
  560. /*
  561. * For Intel, Core 2 (model 15) andl later have an efficient idle.
  562. */
  563. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  564. boot_cpu_data.x86 == 6 &&
  565. boot_cpu_data.x86_model >= 15)
  566. return 1;
  567. #endif
  568. return 0;
  569. }
  570. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  571. unsigned int event)
  572. {
  573. unsigned int cpu = policy->cpu;
  574. struct cpu_dbs_info_s *this_dbs_info;
  575. unsigned int j;
  576. int rc;
  577. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  578. switch (event) {
  579. case CPUFREQ_GOV_START:
  580. if ((!cpu_online(cpu)) || (!policy->cur))
  581. return -EINVAL;
  582. mutex_lock(&dbs_mutex);
  583. dbs_enable++;
  584. for_each_cpu(j, policy->cpus) {
  585. struct cpu_dbs_info_s *j_dbs_info;
  586. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  587. j_dbs_info->cur_policy = policy;
  588. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  589. &j_dbs_info->prev_cpu_wall);
  590. if (dbs_tuners_ins.ignore_nice)
  591. j_dbs_info->prev_cpu_nice =
  592. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  593. }
  594. this_dbs_info->cpu = cpu;
  595. this_dbs_info->rate_mult = 1;
  596. ondemand_powersave_bias_init_cpu(cpu);
  597. /*
  598. * Start the timerschedule work, when this governor
  599. * is used for first time
  600. */
  601. if (dbs_enable == 1) {
  602. unsigned int latency;
  603. rc = sysfs_create_group(cpufreq_global_kobject,
  604. &dbs_attr_group);
  605. if (rc) {
  606. mutex_unlock(&dbs_mutex);
  607. return rc;
  608. }
  609. /* policy latency is in nS. Convert it to uS first */
  610. latency = policy->cpuinfo.transition_latency / 1000;
  611. if (latency == 0)
  612. latency = 1;
  613. /* Bring kernel and HW constraints together */
  614. min_sampling_rate = max(min_sampling_rate,
  615. MIN_LATENCY_MULTIPLIER * latency);
  616. dbs_tuners_ins.sampling_rate =
  617. max(min_sampling_rate,
  618. latency * LATENCY_MULTIPLIER);
  619. dbs_tuners_ins.io_is_busy = should_io_be_busy();
  620. }
  621. mutex_unlock(&dbs_mutex);
  622. mutex_init(&this_dbs_info->timer_mutex);
  623. dbs_timer_init(this_dbs_info);
  624. break;
  625. case CPUFREQ_GOV_STOP:
  626. dbs_timer_exit(this_dbs_info);
  627. mutex_lock(&dbs_mutex);
  628. mutex_destroy(&this_dbs_info->timer_mutex);
  629. dbs_enable--;
  630. mutex_unlock(&dbs_mutex);
  631. if (!dbs_enable)
  632. sysfs_remove_group(cpufreq_global_kobject,
  633. &dbs_attr_group);
  634. break;
  635. case CPUFREQ_GOV_LIMITS:
  636. mutex_lock(&this_dbs_info->timer_mutex);
  637. if (policy->max < this_dbs_info->cur_policy->cur)
  638. __cpufreq_driver_target(this_dbs_info->cur_policy,
  639. policy->max, CPUFREQ_RELATION_H);
  640. else if (policy->min > this_dbs_info->cur_policy->cur)
  641. __cpufreq_driver_target(this_dbs_info->cur_policy,
  642. policy->min, CPUFREQ_RELATION_L);
  643. mutex_unlock(&this_dbs_info->timer_mutex);
  644. break;
  645. }
  646. return 0;
  647. }
  648. static int __init cpufreq_gov_dbs_init(void)
  649. {
  650. u64 idle_time;
  651. int cpu = get_cpu();
  652. idle_time = get_cpu_idle_time_us(cpu, NULL);
  653. put_cpu();
  654. if (idle_time != -1ULL) {
  655. /* Idle micro accounting is supported. Use finer thresholds */
  656. dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  657. dbs_tuners_ins.down_differential =
  658. MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  659. /*
  660. * In nohz/micro accounting case we set the minimum frequency
  661. * not depending on HZ, but fixed (very low). The deferred
  662. * timer might skip some samples if idle/sleeping as needed.
  663. */
  664. min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  665. } else {
  666. /* For correct statistics, we need 10 ticks for each measure */
  667. min_sampling_rate =
  668. MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
  669. }
  670. return cpufreq_register_governor(&cpufreq_gov_ondemand);
  671. }
  672. static void __exit cpufreq_gov_dbs_exit(void)
  673. {
  674. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  675. }
  676. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  677. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  678. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  679. "Low Latency Frequency Transition capable processors");
  680. MODULE_LICENSE("GPL");
  681. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  682. fs_initcall(cpufreq_gov_dbs_init);
  683. #else
  684. module_init(cpufreq_gov_dbs_init);
  685. #endif
  686. module_exit(cpufreq_gov_dbs_exit);