cpufreq_ondemand.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 MIN_FREQUENCY_UP_THRESHOLD (11)
  33. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  34. /*
  35. * The polling frequency of this governor depends on the capability of
  36. * the processor. Default polling frequency is 1000 times the transition
  37. * latency of the processor. The governor will work on any processor with
  38. * transition latency <= 10mS, using appropriate sampling
  39. * rate.
  40. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  41. * this governor will not work.
  42. * All times here are in uS.
  43. */
  44. static unsigned int def_sampling_rate;
  45. #define MIN_SAMPLING_RATE_RATIO (2)
  46. /* for correct statistics, we need at least 10 ticks between each measure */
  47. #define MIN_STAT_SAMPLING_RATE \
  48. (MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10))
  49. #define MIN_SAMPLING_RATE \
  50. (def_sampling_rate / MIN_SAMPLING_RATE_RATIO)
  51. #define MAX_SAMPLING_RATE (500 * def_sampling_rate)
  52. #define DEF_SAMPLING_RATE_LATENCY_MULTIPLIER (1000)
  53. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  54. static void do_dbs_timer(struct work_struct *work);
  55. /* Sampling types */
  56. enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
  57. struct cpu_dbs_info_s {
  58. cputime64_t prev_cpu_idle;
  59. cputime64_t prev_cpu_wall;
  60. cputime64_t prev_cpu_nice;
  61. struct cpufreq_policy *cur_policy;
  62. struct delayed_work work;
  63. struct cpufreq_frequency_table *freq_table;
  64. unsigned int freq_lo;
  65. unsigned int freq_lo_jiffies;
  66. unsigned int freq_hi_jiffies;
  67. int cpu;
  68. unsigned int enable:1,
  69. sample_type:1;
  70. };
  71. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cpu_dbs_info);
  72. static unsigned int dbs_enable; /* number of CPUs using this policy */
  73. /*
  74. * DEADLOCK ALERT! There is a ordering requirement between cpu_hotplug
  75. * lock and dbs_mutex. cpu_hotplug lock should always be held before
  76. * dbs_mutex. If any function that can potentially take cpu_hotplug lock
  77. * (like __cpufreq_driver_target()) is being called with dbs_mutex taken, then
  78. * cpu_hotplug lock should be taken before that. Note that cpu_hotplug lock
  79. * is recursive for the same process. -Venki
  80. */
  81. static DEFINE_MUTEX(dbs_mutex);
  82. static struct workqueue_struct *kondemand_wq;
  83. static struct dbs_tuners {
  84. unsigned int sampling_rate;
  85. unsigned int up_threshold;
  86. unsigned int down_differential;
  87. unsigned int ignore_nice;
  88. unsigned int powersave_bias;
  89. } dbs_tuners_ins = {
  90. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  91. .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
  92. .ignore_nice = 0,
  93. .powersave_bias = 0,
  94. };
  95. static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
  96. cputime64_t *wall)
  97. {
  98. cputime64_t idle_time;
  99. cputime64_t cur_wall_time;
  100. cputime64_t busy_time;
  101. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  102. busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
  103. kstat_cpu(cpu).cpustat.system);
  104. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
  105. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
  106. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
  107. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
  108. idle_time = cputime64_sub(cur_wall_time, busy_time);
  109. if (wall)
  110. *wall = cur_wall_time;
  111. return idle_time;
  112. }
  113. static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
  114. {
  115. u64 idle_time = get_cpu_idle_time_us(cpu, wall);
  116. if (idle_time == -1ULL)
  117. return get_cpu_idle_time_jiffy(cpu, wall);
  118. return idle_time;
  119. }
  120. /*
  121. * Find right freq to be set now with powersave_bias on.
  122. * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
  123. * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
  124. */
  125. static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
  126. unsigned int freq_next,
  127. unsigned int relation)
  128. {
  129. unsigned int freq_req, freq_reduc, freq_avg;
  130. unsigned int freq_hi, freq_lo;
  131. unsigned int index = 0;
  132. unsigned int jiffies_total, jiffies_hi, jiffies_lo;
  133. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, policy->cpu);
  134. if (!dbs_info->freq_table) {
  135. dbs_info->freq_lo = 0;
  136. dbs_info->freq_lo_jiffies = 0;
  137. return freq_next;
  138. }
  139. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  140. relation, &index);
  141. freq_req = dbs_info->freq_table[index].frequency;
  142. freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
  143. freq_avg = freq_req - freq_reduc;
  144. /* Find freq bounds for freq_avg in freq_table */
  145. index = 0;
  146. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  147. CPUFREQ_RELATION_H, &index);
  148. freq_lo = dbs_info->freq_table[index].frequency;
  149. index = 0;
  150. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  151. CPUFREQ_RELATION_L, &index);
  152. freq_hi = dbs_info->freq_table[index].frequency;
  153. /* Find out how long we have to be in hi and lo freqs */
  154. if (freq_hi == freq_lo) {
  155. dbs_info->freq_lo = 0;
  156. dbs_info->freq_lo_jiffies = 0;
  157. return freq_lo;
  158. }
  159. jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  160. jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
  161. jiffies_hi += ((freq_hi - freq_lo) / 2);
  162. jiffies_hi /= (freq_hi - freq_lo);
  163. jiffies_lo = jiffies_total - jiffies_hi;
  164. dbs_info->freq_lo = freq_lo;
  165. dbs_info->freq_lo_jiffies = jiffies_lo;
  166. dbs_info->freq_hi_jiffies = jiffies_hi;
  167. return freq_hi;
  168. }
  169. static void ondemand_powersave_bias_init(void)
  170. {
  171. int i;
  172. for_each_online_cpu(i) {
  173. struct cpu_dbs_info_s *dbs_info = &per_cpu(cpu_dbs_info, i);
  174. dbs_info->freq_table = cpufreq_frequency_get_table(i);
  175. dbs_info->freq_lo = 0;
  176. }
  177. }
  178. /************************** sysfs interface ************************/
  179. static ssize_t show_sampling_rate_max(struct cpufreq_policy *policy, char *buf)
  180. {
  181. static int print_once;
  182. if (!print_once) {
  183. printk(KERN_INFO "CPUFREQ: ondemand sampling_rate_max "
  184. "sysfs file is deprecated - used by: %s\n",
  185. current->comm);
  186. print_once = 1;
  187. }
  188. return sprintf(buf, "%u\n", MAX_SAMPLING_RATE);
  189. }
  190. static ssize_t show_sampling_rate_min(struct cpufreq_policy *policy, char *buf)
  191. {
  192. static int print_once;
  193. if (!print_once) {
  194. printk(KERN_INFO "CPUFREQ: ondemand sampling_rate_min "
  195. "sysfs file is deprecated - used by: %s\n",
  196. current->comm);
  197. print_once = 1;
  198. }
  199. return sprintf(buf, "%u\n", MIN_SAMPLING_RATE);
  200. }
  201. #define define_one_ro(_name) \
  202. static struct freq_attr _name = \
  203. __ATTR(_name, 0444, show_##_name, NULL)
  204. define_one_ro(sampling_rate_max);
  205. define_one_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 cpufreq_policy *unused, 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. static ssize_t store_sampling_rate(struct cpufreq_policy *unused,
  218. const char *buf, size_t count)
  219. {
  220. unsigned int input;
  221. int ret;
  222. ret = sscanf(buf, "%u", &input);
  223. mutex_lock(&dbs_mutex);
  224. if (ret != 1 || input > MAX_SAMPLING_RATE
  225. || input < MIN_SAMPLING_RATE) {
  226. mutex_unlock(&dbs_mutex);
  227. return -EINVAL;
  228. }
  229. dbs_tuners_ins.sampling_rate = input;
  230. mutex_unlock(&dbs_mutex);
  231. return count;
  232. }
  233. static ssize_t store_up_threshold(struct cpufreq_policy *unused,
  234. const char *buf, size_t count)
  235. {
  236. unsigned int input;
  237. int ret;
  238. ret = sscanf(buf, "%u", &input);
  239. mutex_lock(&dbs_mutex);
  240. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  241. input < MIN_FREQUENCY_UP_THRESHOLD) {
  242. mutex_unlock(&dbs_mutex);
  243. return -EINVAL;
  244. }
  245. dbs_tuners_ins.up_threshold = input;
  246. mutex_unlock(&dbs_mutex);
  247. return count;
  248. }
  249. static ssize_t store_ignore_nice_load(struct cpufreq_policy *policy,
  250. const char *buf, size_t count)
  251. {
  252. unsigned int input;
  253. int ret;
  254. unsigned int j;
  255. ret = sscanf(buf, "%u", &input);
  256. if (ret != 1)
  257. return -EINVAL;
  258. if (input > 1)
  259. input = 1;
  260. mutex_lock(&dbs_mutex);
  261. if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
  262. mutex_unlock(&dbs_mutex);
  263. return count;
  264. }
  265. dbs_tuners_ins.ignore_nice = input;
  266. /* we need to re-evaluate prev_cpu_idle */
  267. for_each_online_cpu(j) {
  268. struct cpu_dbs_info_s *dbs_info;
  269. dbs_info = &per_cpu(cpu_dbs_info, j);
  270. dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  271. &dbs_info->prev_cpu_wall);
  272. if (dbs_tuners_ins.ignore_nice)
  273. dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
  274. }
  275. mutex_unlock(&dbs_mutex);
  276. return count;
  277. }
  278. static ssize_t store_powersave_bias(struct cpufreq_policy *unused,
  279. const char *buf, size_t count)
  280. {
  281. unsigned int input;
  282. int ret;
  283. ret = sscanf(buf, "%u", &input);
  284. if (ret != 1)
  285. return -EINVAL;
  286. if (input > 1000)
  287. input = 1000;
  288. mutex_lock(&dbs_mutex);
  289. dbs_tuners_ins.powersave_bias = input;
  290. ondemand_powersave_bias_init();
  291. mutex_unlock(&dbs_mutex);
  292. return count;
  293. }
  294. #define define_one_rw(_name) \
  295. static struct freq_attr _name = \
  296. __ATTR(_name, 0644, show_##_name, store_##_name)
  297. define_one_rw(sampling_rate);
  298. define_one_rw(up_threshold);
  299. define_one_rw(ignore_nice_load);
  300. define_one_rw(powersave_bias);
  301. static struct attribute *dbs_attributes[] = {
  302. &sampling_rate_max.attr,
  303. &sampling_rate_min.attr,
  304. &sampling_rate.attr,
  305. &up_threshold.attr,
  306. &ignore_nice_load.attr,
  307. &powersave_bias.attr,
  308. NULL
  309. };
  310. static struct attribute_group dbs_attr_group = {
  311. .attrs = dbs_attributes,
  312. .name = "ondemand",
  313. };
  314. /************************** sysfs end ************************/
  315. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  316. {
  317. unsigned int max_load_freq;
  318. struct cpufreq_policy *policy;
  319. unsigned int j;
  320. if (!this_dbs_info->enable)
  321. return;
  322. this_dbs_info->freq_lo = 0;
  323. policy = this_dbs_info->cur_policy;
  324. /*
  325. * Every sampling_rate, we check, if current idle time is less
  326. * than 20% (default), then we try to increase frequency
  327. * Every sampling_rate, we look for a the lowest
  328. * frequency which can sustain the load while keeping idle time over
  329. * 30%. If such a frequency exist, we try to decrease to this frequency.
  330. *
  331. * Any frequency increase takes it to the maximum frequency.
  332. * Frequency reduction happens at minimum steps of
  333. * 5% (default) of current frequency
  334. */
  335. /* Get Absolute Load - in terms of freq */
  336. max_load_freq = 0;
  337. for_each_cpu(j, policy->cpus) {
  338. struct cpu_dbs_info_s *j_dbs_info;
  339. cputime64_t cur_wall_time, cur_idle_time;
  340. unsigned int idle_time, wall_time;
  341. unsigned int load, load_freq;
  342. int freq_avg;
  343. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  344. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
  345. wall_time = (unsigned int) cputime64_sub(cur_wall_time,
  346. j_dbs_info->prev_cpu_wall);
  347. j_dbs_info->prev_cpu_wall = cur_wall_time;
  348. idle_time = (unsigned int) cputime64_sub(cur_idle_time,
  349. j_dbs_info->prev_cpu_idle);
  350. j_dbs_info->prev_cpu_idle = cur_idle_time;
  351. if (dbs_tuners_ins.ignore_nice) {
  352. cputime64_t cur_nice;
  353. unsigned long cur_nice_jiffies;
  354. cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
  355. j_dbs_info->prev_cpu_nice);
  356. /*
  357. * Assumption: nice time between sampling periods will
  358. * be less than 2^32 jiffies for 32 bit sys
  359. */
  360. cur_nice_jiffies = (unsigned long)
  361. cputime64_to_jiffies64(cur_nice);
  362. j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
  363. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  364. }
  365. if (unlikely(!wall_time || wall_time < idle_time))
  366. continue;
  367. load = 100 * (wall_time - idle_time) / wall_time;
  368. freq_avg = __cpufreq_driver_getavg(policy, j);
  369. if (freq_avg <= 0)
  370. freq_avg = policy->cur;
  371. load_freq = load * freq_avg;
  372. if (load_freq > max_load_freq)
  373. max_load_freq = load_freq;
  374. }
  375. /* Check for frequency increase */
  376. if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
  377. /* if we are already at full speed then break out early */
  378. if (!dbs_tuners_ins.powersave_bias) {
  379. if (policy->cur == policy->max)
  380. return;
  381. __cpufreq_driver_target(policy, policy->max,
  382. CPUFREQ_RELATION_H);
  383. } else {
  384. int freq = powersave_bias_target(policy, policy->max,
  385. CPUFREQ_RELATION_H);
  386. __cpufreq_driver_target(policy, freq,
  387. CPUFREQ_RELATION_L);
  388. }
  389. return;
  390. }
  391. /* Check for frequency decrease */
  392. /* if we cannot reduce the frequency anymore, break out early */
  393. if (policy->cur == policy->min)
  394. return;
  395. /*
  396. * The optimal frequency is the frequency that is the lowest that
  397. * can support the current CPU usage without triggering the up
  398. * policy. To be safe, we focus 10 points under the threshold.
  399. */
  400. if (max_load_freq <
  401. (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
  402. policy->cur) {
  403. unsigned int freq_next;
  404. freq_next = max_load_freq /
  405. (dbs_tuners_ins.up_threshold -
  406. dbs_tuners_ins.down_differential);
  407. if (!dbs_tuners_ins.powersave_bias) {
  408. __cpufreq_driver_target(policy, freq_next,
  409. CPUFREQ_RELATION_L);
  410. } else {
  411. int freq = powersave_bias_target(policy, freq_next,
  412. CPUFREQ_RELATION_L);
  413. __cpufreq_driver_target(policy, freq,
  414. CPUFREQ_RELATION_L);
  415. }
  416. }
  417. }
  418. static void do_dbs_timer(struct work_struct *work)
  419. {
  420. struct cpu_dbs_info_s *dbs_info =
  421. container_of(work, struct cpu_dbs_info_s, work.work);
  422. unsigned int cpu = dbs_info->cpu;
  423. int sample_type = dbs_info->sample_type;
  424. /* We want all CPUs to do sampling nearly on same jiffy */
  425. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  426. delay -= jiffies % delay;
  427. if (lock_policy_rwsem_write(cpu) < 0)
  428. return;
  429. if (!dbs_info->enable) {
  430. unlock_policy_rwsem_write(cpu);
  431. return;
  432. }
  433. /* Common NORMAL_SAMPLE setup */
  434. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  435. if (!dbs_tuners_ins.powersave_bias ||
  436. sample_type == DBS_NORMAL_SAMPLE) {
  437. dbs_check_cpu(dbs_info);
  438. if (dbs_info->freq_lo) {
  439. /* Setup timer for SUB_SAMPLE */
  440. dbs_info->sample_type = DBS_SUB_SAMPLE;
  441. delay = dbs_info->freq_hi_jiffies;
  442. }
  443. } else {
  444. __cpufreq_driver_target(dbs_info->cur_policy,
  445. dbs_info->freq_lo, CPUFREQ_RELATION_H);
  446. }
  447. queue_delayed_work_on(cpu, kondemand_wq, &dbs_info->work, delay);
  448. unlock_policy_rwsem_write(cpu);
  449. }
  450. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
  451. {
  452. /* We want all CPUs to do sampling nearly on same jiffy */
  453. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  454. delay -= jiffies % delay;
  455. dbs_info->enable = 1;
  456. ondemand_powersave_bias_init();
  457. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  458. INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
  459. queue_delayed_work_on(dbs_info->cpu, kondemand_wq, &dbs_info->work,
  460. delay);
  461. }
  462. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  463. {
  464. dbs_info->enable = 0;
  465. cancel_delayed_work(&dbs_info->work);
  466. }
  467. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  468. unsigned int event)
  469. {
  470. unsigned int cpu = policy->cpu;
  471. struct cpu_dbs_info_s *this_dbs_info;
  472. unsigned int j;
  473. int rc;
  474. this_dbs_info = &per_cpu(cpu_dbs_info, cpu);
  475. switch (event) {
  476. case CPUFREQ_GOV_START:
  477. if ((!cpu_online(cpu)) || (!policy->cur))
  478. return -EINVAL;
  479. if (this_dbs_info->enable) /* Already enabled */
  480. break;
  481. mutex_lock(&dbs_mutex);
  482. dbs_enable++;
  483. rc = sysfs_create_group(&policy->kobj, &dbs_attr_group);
  484. if (rc) {
  485. dbs_enable--;
  486. mutex_unlock(&dbs_mutex);
  487. return rc;
  488. }
  489. for_each_cpu(j, policy->cpus) {
  490. struct cpu_dbs_info_s *j_dbs_info;
  491. j_dbs_info = &per_cpu(cpu_dbs_info, j);
  492. j_dbs_info->cur_policy = policy;
  493. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  494. &j_dbs_info->prev_cpu_wall);
  495. if (dbs_tuners_ins.ignore_nice) {
  496. j_dbs_info->prev_cpu_nice =
  497. kstat_cpu(j).cpustat.nice;
  498. }
  499. }
  500. this_dbs_info->cpu = cpu;
  501. /*
  502. * Start the timerschedule work, when this governor
  503. * is used for first time
  504. */
  505. if (dbs_enable == 1) {
  506. unsigned int latency;
  507. /* policy latency is in nS. Convert it to uS first */
  508. latency = policy->cpuinfo.transition_latency / 1000;
  509. if (latency == 0)
  510. latency = 1;
  511. def_sampling_rate = latency *
  512. DEF_SAMPLING_RATE_LATENCY_MULTIPLIER;
  513. if (def_sampling_rate < MIN_STAT_SAMPLING_RATE)
  514. def_sampling_rate = MIN_STAT_SAMPLING_RATE;
  515. dbs_tuners_ins.sampling_rate = def_sampling_rate;
  516. }
  517. dbs_timer_init(this_dbs_info);
  518. mutex_unlock(&dbs_mutex);
  519. break;
  520. case CPUFREQ_GOV_STOP:
  521. mutex_lock(&dbs_mutex);
  522. dbs_timer_exit(this_dbs_info);
  523. sysfs_remove_group(&policy->kobj, &dbs_attr_group);
  524. dbs_enable--;
  525. mutex_unlock(&dbs_mutex);
  526. break;
  527. case CPUFREQ_GOV_LIMITS:
  528. mutex_lock(&dbs_mutex);
  529. if (policy->max < this_dbs_info->cur_policy->cur)
  530. __cpufreq_driver_target(this_dbs_info->cur_policy,
  531. policy->max, CPUFREQ_RELATION_H);
  532. else if (policy->min > this_dbs_info->cur_policy->cur)
  533. __cpufreq_driver_target(this_dbs_info->cur_policy,
  534. policy->min, CPUFREQ_RELATION_L);
  535. mutex_unlock(&dbs_mutex);
  536. break;
  537. }
  538. return 0;
  539. }
  540. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  541. static
  542. #endif
  543. struct cpufreq_governor cpufreq_gov_ondemand = {
  544. .name = "ondemand",
  545. .governor = cpufreq_governor_dbs,
  546. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  547. .owner = THIS_MODULE,
  548. };
  549. static int __init cpufreq_gov_dbs_init(void)
  550. {
  551. int err;
  552. cputime64_t wall;
  553. u64 idle_time;
  554. int cpu = get_cpu();
  555. idle_time = get_cpu_idle_time_us(cpu, &wall);
  556. put_cpu();
  557. if (idle_time != -1ULL) {
  558. /* Idle micro accounting is supported. Use finer thresholds */
  559. dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  560. dbs_tuners_ins.down_differential =
  561. MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  562. }
  563. kondemand_wq = create_workqueue("kondemand");
  564. if (!kondemand_wq) {
  565. printk(KERN_ERR "Creation of kondemand failed\n");
  566. return -EFAULT;
  567. }
  568. err = cpufreq_register_governor(&cpufreq_gov_ondemand);
  569. if (err)
  570. destroy_workqueue(kondemand_wq);
  571. return err;
  572. }
  573. static void __exit cpufreq_gov_dbs_exit(void)
  574. {
  575. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  576. destroy_workqueue(kondemand_wq);
  577. }
  578. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  579. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  580. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  581. "Low Latency Frequency Transition capable processors");
  582. MODULE_LICENSE("GPL");
  583. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  584. fs_initcall(cpufreq_gov_dbs_init);
  585. #else
  586. module_init(cpufreq_gov_dbs_init);
  587. #endif
  588. module_exit(cpufreq_gov_dbs_exit);