cpufreq_ondemand.c 21 KB

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