cpufreq_ondemand.c 19 KB

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