cpufreq_ondemand.c 19 KB

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