cpufreq_ondemand.c 19 KB

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