cpufreq_ondemand.c 23 KB

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