cpufreq_ondemand.c 19 KB

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