cpufreq_ondemand.c 22 KB

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