cpufreq_ondemand.c 21 KB

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