cpufreq_conservative.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * drivers/cpufreq/cpufreq_conservative.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. * (C) 2009 Alexander Clouter <alex@digriz.org.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/cpufreq.h>
  17. #include <linux/cpu.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/mutex.h>
  21. #include <linux/hrtimer.h>
  22. #include <linux/tick.h>
  23. #include <linux/ktime.h>
  24. #include <linux/sched.h>
  25. /*
  26. * dbs is used in this file as a shortform for demandbased switching
  27. * It helps to keep variable names smaller, simpler
  28. */
  29. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  30. #define DEF_FREQUENCY_DOWN_THRESHOLD (20)
  31. /*
  32. * The polling frequency of this governor depends on the capability of
  33. * the processor. Default polling frequency is 1000 times the transition
  34. * latency of the processor. The governor will work on any processor with
  35. * transition latency <= 10mS, using appropriate sampling
  36. * rate.
  37. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  38. * this governor will not work.
  39. * All times here are in uS.
  40. */
  41. #define MIN_SAMPLING_RATE_RATIO (2)
  42. static unsigned int min_sampling_rate;
  43. #define LATENCY_MULTIPLIER (1000)
  44. #define MIN_LATENCY_MULTIPLIER (100)
  45. #define DEF_SAMPLING_DOWN_FACTOR (1)
  46. #define MAX_SAMPLING_DOWN_FACTOR (10)
  47. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  48. static void do_dbs_timer(struct work_struct *work);
  49. struct cpu_dbs_info_s {
  50. cputime64_t prev_cpu_idle;
  51. cputime64_t prev_cpu_wall;
  52. cputime64_t prev_cpu_nice;
  53. struct cpufreq_policy *cur_policy;
  54. struct delayed_work work;
  55. unsigned int down_skip;
  56. unsigned int requested_freq;
  57. int cpu;
  58. unsigned int enable:1;
  59. /*
  60. * percpu mutex that serializes governor limit change with
  61. * do_dbs_timer invocation. We do not want do_dbs_timer to run
  62. * when user is changing the governor or limits.
  63. */
  64. struct mutex timer_mutex;
  65. };
  66. static DEFINE_PER_CPU(struct cpu_dbs_info_s, cs_cpu_dbs_info);
  67. static unsigned int dbs_enable; /* number of CPUs using this policy */
  68. /*
  69. * dbs_mutex protects data in dbs_tuners_ins from concurrent changes on
  70. * different CPUs. It protects dbs_enable in governor start/stop.
  71. */
  72. static DEFINE_MUTEX(dbs_mutex);
  73. static struct dbs_tuners {
  74. unsigned int sampling_rate;
  75. unsigned int sampling_down_factor;
  76. unsigned int up_threshold;
  77. unsigned int down_threshold;
  78. unsigned int ignore_nice;
  79. unsigned int freq_step;
  80. } dbs_tuners_ins = {
  81. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  82. .down_threshold = DEF_FREQUENCY_DOWN_THRESHOLD,
  83. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  84. .ignore_nice = 0,
  85. .freq_step = 5,
  86. };
  87. static inline cputime64_t get_cpu_idle_time_jiffy(unsigned int cpu,
  88. cputime64_t *wall)
  89. {
  90. cputime64_t idle_time;
  91. cputime64_t cur_wall_time;
  92. cputime64_t busy_time;
  93. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  94. busy_time = cputime64_add(kstat_cpu(cpu).cpustat.user,
  95. kstat_cpu(cpu).cpustat.system);
  96. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.irq);
  97. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.softirq);
  98. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.steal);
  99. busy_time = cputime64_add(busy_time, kstat_cpu(cpu).cpustat.nice);
  100. idle_time = cputime64_sub(cur_wall_time, busy_time);
  101. if (wall)
  102. *wall = (cputime64_t)jiffies_to_usecs(cur_wall_time);
  103. return (cputime64_t)jiffies_to_usecs(idle_time);;
  104. }
  105. static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
  106. {
  107. u64 idle_time = get_cpu_idle_time_us(cpu, wall);
  108. if (idle_time == -1ULL)
  109. return get_cpu_idle_time_jiffy(cpu, wall);
  110. return idle_time;
  111. }
  112. /* keep track of frequency transitions */
  113. static int
  114. dbs_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
  115. void *data)
  116. {
  117. struct cpufreq_freqs *freq = data;
  118. struct cpu_dbs_info_s *this_dbs_info = &per_cpu(cs_cpu_dbs_info,
  119. freq->cpu);
  120. struct cpufreq_policy *policy;
  121. if (!this_dbs_info->enable)
  122. return 0;
  123. policy = this_dbs_info->cur_policy;
  124. /*
  125. * we only care if our internally tracked freq moves outside
  126. * the 'valid' ranges of freqency available to us otherwise
  127. * we do not change it
  128. */
  129. if (this_dbs_info->requested_freq > policy->max
  130. || this_dbs_info->requested_freq < policy->min)
  131. this_dbs_info->requested_freq = freq->new;
  132. return 0;
  133. }
  134. static struct notifier_block dbs_cpufreq_notifier_block = {
  135. .notifier_call = dbs_cpufreq_notifier
  136. };
  137. /************************** sysfs interface ************************/
  138. static ssize_t show_sampling_rate_max(struct kobject *kobj,
  139. struct attribute *attr, char *buf)
  140. {
  141. printk_once(KERN_INFO "CPUFREQ: conservative sampling_rate_max "
  142. "sysfs file is deprecated - used by: %s\n", current->comm);
  143. return sprintf(buf, "%u\n", -1U);
  144. }
  145. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  146. struct attribute *attr, char *buf)
  147. {
  148. return sprintf(buf, "%u\n", min_sampling_rate);
  149. }
  150. define_one_global_ro(sampling_rate_max);
  151. define_one_global_ro(sampling_rate_min);
  152. /* cpufreq_conservative Governor Tunables */
  153. #define show_one(file_name, object) \
  154. static ssize_t show_##file_name \
  155. (struct kobject *kobj, struct attribute *attr, char *buf) \
  156. { \
  157. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  158. }
  159. show_one(sampling_rate, sampling_rate);
  160. show_one(sampling_down_factor, sampling_down_factor);
  161. show_one(up_threshold, up_threshold);
  162. show_one(down_threshold, down_threshold);
  163. show_one(ignore_nice_load, ignore_nice);
  164. show_one(freq_step, freq_step);
  165. /*** delete after deprecation time ***/
  166. #define DEPRECATION_MSG(file_name) \
  167. printk_once(KERN_INFO "CPUFREQ: Per core conservative sysfs " \
  168. "interface is deprecated - " #file_name "\n");
  169. #define show_one_old(file_name) \
  170. static ssize_t show_##file_name##_old \
  171. (struct cpufreq_policy *unused, char *buf) \
  172. { \
  173. printk_once(KERN_INFO "CPUFREQ: Per core conservative sysfs " \
  174. "interface is deprecated - " #file_name "\n"); \
  175. return show_##file_name(NULL, NULL, buf); \
  176. }
  177. show_one_old(sampling_rate);
  178. show_one_old(sampling_down_factor);
  179. show_one_old(up_threshold);
  180. show_one_old(down_threshold);
  181. show_one_old(ignore_nice_load);
  182. show_one_old(freq_step);
  183. show_one_old(sampling_rate_min);
  184. show_one_old(sampling_rate_max);
  185. cpufreq_freq_attr_ro_old(sampling_rate_min);
  186. cpufreq_freq_attr_ro_old(sampling_rate_max);
  187. /*** delete after deprecation time ***/
  188. static ssize_t store_sampling_down_factor(struct kobject *a,
  189. struct attribute *b,
  190. const char *buf, size_t count)
  191. {
  192. unsigned int input;
  193. int ret;
  194. ret = sscanf(buf, "%u", &input);
  195. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  196. return -EINVAL;
  197. mutex_lock(&dbs_mutex);
  198. dbs_tuners_ins.sampling_down_factor = input;
  199. mutex_unlock(&dbs_mutex);
  200. return count;
  201. }
  202. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  203. const char *buf, size_t count)
  204. {
  205. unsigned int input;
  206. int ret;
  207. ret = sscanf(buf, "%u", &input);
  208. if (ret != 1)
  209. return -EINVAL;
  210. mutex_lock(&dbs_mutex);
  211. dbs_tuners_ins.sampling_rate = max(input, min_sampling_rate);
  212. mutex_unlock(&dbs_mutex);
  213. return count;
  214. }
  215. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  216. const char *buf, size_t count)
  217. {
  218. unsigned int input;
  219. int ret;
  220. ret = sscanf(buf, "%u", &input);
  221. mutex_lock(&dbs_mutex);
  222. if (ret != 1 || input > 100 ||
  223. input <= dbs_tuners_ins.down_threshold) {
  224. mutex_unlock(&dbs_mutex);
  225. return -EINVAL;
  226. }
  227. dbs_tuners_ins.up_threshold = input;
  228. mutex_unlock(&dbs_mutex);
  229. return count;
  230. }
  231. static ssize_t store_down_threshold(struct kobject *a, struct attribute *b,
  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. /* cannot be lower than 11 otherwise freq will not fall */
  239. if (ret != 1 || input < 11 || input > 100 ||
  240. input >= dbs_tuners_ins.up_threshold) {
  241. mutex_unlock(&dbs_mutex);
  242. return -EINVAL;
  243. }
  244. dbs_tuners_ins.down_threshold = input;
  245. mutex_unlock(&dbs_mutex);
  246. return count;
  247. }
  248. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  249. const char *buf, size_t count)
  250. {
  251. unsigned int input;
  252. int ret;
  253. unsigned int j;
  254. ret = sscanf(buf, "%u", &input);
  255. if (ret != 1)
  256. return -EINVAL;
  257. if (input > 1)
  258. input = 1;
  259. mutex_lock(&dbs_mutex);
  260. if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
  261. mutex_unlock(&dbs_mutex);
  262. return count;
  263. }
  264. dbs_tuners_ins.ignore_nice = input;
  265. /* we need to re-evaluate prev_cpu_idle */
  266. for_each_online_cpu(j) {
  267. struct cpu_dbs_info_s *dbs_info;
  268. dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  269. dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  270. &dbs_info->prev_cpu_wall);
  271. if (dbs_tuners_ins.ignore_nice)
  272. dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
  273. }
  274. mutex_unlock(&dbs_mutex);
  275. return count;
  276. }
  277. static ssize_t store_freq_step(struct kobject *a, struct attribute *b,
  278. const char *buf, size_t count)
  279. {
  280. unsigned int input;
  281. int ret;
  282. ret = sscanf(buf, "%u", &input);
  283. if (ret != 1)
  284. return -EINVAL;
  285. if (input > 100)
  286. input = 100;
  287. /* no need to test here if freq_step is zero as the user might actually
  288. * want this, they would be crazy though :) */
  289. mutex_lock(&dbs_mutex);
  290. dbs_tuners_ins.freq_step = input;
  291. mutex_unlock(&dbs_mutex);
  292. return count;
  293. }
  294. define_one_global_rw(sampling_rate);
  295. define_one_global_rw(sampling_down_factor);
  296. define_one_global_rw(up_threshold);
  297. define_one_global_rw(down_threshold);
  298. define_one_global_rw(ignore_nice_load);
  299. define_one_global_rw(freq_step);
  300. static struct attribute *dbs_attributes[] = {
  301. &sampling_rate_max.attr,
  302. &sampling_rate_min.attr,
  303. &sampling_rate.attr,
  304. &sampling_down_factor.attr,
  305. &up_threshold.attr,
  306. &down_threshold.attr,
  307. &ignore_nice_load.attr,
  308. &freq_step.attr,
  309. NULL
  310. };
  311. static struct attribute_group dbs_attr_group = {
  312. .attrs = dbs_attributes,
  313. .name = "conservative",
  314. };
  315. /*** delete after deprecation time ***/
  316. #define write_one_old(file_name) \
  317. static ssize_t store_##file_name##_old \
  318. (struct cpufreq_policy *unused, const char *buf, size_t count) \
  319. { \
  320. printk_once(KERN_INFO "CPUFREQ: Per core conservative sysfs " \
  321. "interface is deprecated - " #file_name "\n"); \
  322. return store_##file_name(NULL, NULL, buf, count); \
  323. }
  324. write_one_old(sampling_rate);
  325. write_one_old(sampling_down_factor);
  326. write_one_old(up_threshold);
  327. write_one_old(down_threshold);
  328. write_one_old(ignore_nice_load);
  329. write_one_old(freq_step);
  330. cpufreq_freq_attr_rw_old(sampling_rate);
  331. cpufreq_freq_attr_rw_old(sampling_down_factor);
  332. cpufreq_freq_attr_rw_old(up_threshold);
  333. cpufreq_freq_attr_rw_old(down_threshold);
  334. cpufreq_freq_attr_rw_old(ignore_nice_load);
  335. cpufreq_freq_attr_rw_old(freq_step);
  336. static struct attribute *dbs_attributes_old[] = {
  337. &sampling_rate_max_old.attr,
  338. &sampling_rate_min_old.attr,
  339. &sampling_rate_old.attr,
  340. &sampling_down_factor_old.attr,
  341. &up_threshold_old.attr,
  342. &down_threshold_old.attr,
  343. &ignore_nice_load_old.attr,
  344. &freq_step_old.attr,
  345. NULL
  346. };
  347. static struct attribute_group dbs_attr_group_old = {
  348. .attrs = dbs_attributes_old,
  349. .name = "conservative",
  350. };
  351. /*** delete after deprecation time ***/
  352. /************************** sysfs end ************************/
  353. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  354. {
  355. unsigned int load = 0;
  356. unsigned int max_load = 0;
  357. unsigned int freq_target;
  358. struct cpufreq_policy *policy;
  359. unsigned int j;
  360. policy = this_dbs_info->cur_policy;
  361. /*
  362. * Every sampling_rate, we check, if current idle time is less
  363. * than 20% (default), then we try to increase frequency
  364. * Every sampling_rate*sampling_down_factor, we check, if current
  365. * idle time is more than 80%, then we try to decrease frequency
  366. *
  367. * Any frequency increase takes it to the maximum frequency.
  368. * Frequency reduction happens at minimum steps of
  369. * 5% (default) of maximum frequency
  370. */
  371. /* Get Absolute Load */
  372. for_each_cpu(j, policy->cpus) {
  373. struct cpu_dbs_info_s *j_dbs_info;
  374. cputime64_t cur_wall_time, cur_idle_time;
  375. unsigned int idle_time, wall_time;
  376. j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  377. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
  378. wall_time = (unsigned int) cputime64_sub(cur_wall_time,
  379. j_dbs_info->prev_cpu_wall);
  380. j_dbs_info->prev_cpu_wall = cur_wall_time;
  381. idle_time = (unsigned int) cputime64_sub(cur_idle_time,
  382. j_dbs_info->prev_cpu_idle);
  383. j_dbs_info->prev_cpu_idle = cur_idle_time;
  384. if (dbs_tuners_ins.ignore_nice) {
  385. cputime64_t cur_nice;
  386. unsigned long cur_nice_jiffies;
  387. cur_nice = cputime64_sub(kstat_cpu(j).cpustat.nice,
  388. j_dbs_info->prev_cpu_nice);
  389. /*
  390. * Assumption: nice time between sampling periods will
  391. * be less than 2^32 jiffies for 32 bit sys
  392. */
  393. cur_nice_jiffies = (unsigned long)
  394. cputime64_to_jiffies64(cur_nice);
  395. j_dbs_info->prev_cpu_nice = kstat_cpu(j).cpustat.nice;
  396. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  397. }
  398. if (unlikely(!wall_time || wall_time < idle_time))
  399. continue;
  400. load = 100 * (wall_time - idle_time) / wall_time;
  401. if (load > max_load)
  402. max_load = load;
  403. }
  404. /*
  405. * break out if we 'cannot' reduce the speed as the user might
  406. * want freq_step to be zero
  407. */
  408. if (dbs_tuners_ins.freq_step == 0)
  409. return;
  410. /* Check for frequency increase */
  411. if (max_load > dbs_tuners_ins.up_threshold) {
  412. this_dbs_info->down_skip = 0;
  413. /* if we are already at full speed then break out early */
  414. if (this_dbs_info->requested_freq == policy->max)
  415. return;
  416. freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  417. /* max freq cannot be less than 100. But who knows.... */
  418. if (unlikely(freq_target == 0))
  419. freq_target = 5;
  420. this_dbs_info->requested_freq += freq_target;
  421. if (this_dbs_info->requested_freq > policy->max)
  422. this_dbs_info->requested_freq = policy->max;
  423. __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  424. CPUFREQ_RELATION_H);
  425. return;
  426. }
  427. /*
  428. * The optimal frequency is the frequency that is the lowest that
  429. * can support the current CPU usage without triggering the up
  430. * policy. To be safe, we focus 10 points under the threshold.
  431. */
  432. if (max_load < (dbs_tuners_ins.down_threshold - 10)) {
  433. freq_target = (dbs_tuners_ins.freq_step * policy->max) / 100;
  434. this_dbs_info->requested_freq -= freq_target;
  435. if (this_dbs_info->requested_freq < policy->min)
  436. this_dbs_info->requested_freq = policy->min;
  437. /*
  438. * if we cannot reduce the frequency anymore, break out early
  439. */
  440. if (policy->cur == policy->min)
  441. return;
  442. __cpufreq_driver_target(policy, this_dbs_info->requested_freq,
  443. CPUFREQ_RELATION_H);
  444. return;
  445. }
  446. }
  447. static void do_dbs_timer(struct work_struct *work)
  448. {
  449. struct cpu_dbs_info_s *dbs_info =
  450. container_of(work, struct cpu_dbs_info_s, work.work);
  451. unsigned int cpu = dbs_info->cpu;
  452. /* We want all CPUs to do sampling nearly on same jiffy */
  453. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  454. delay -= jiffies % delay;
  455. mutex_lock(&dbs_info->timer_mutex);
  456. dbs_check_cpu(dbs_info);
  457. schedule_delayed_work_on(cpu, &dbs_info->work, delay);
  458. mutex_unlock(&dbs_info->timer_mutex);
  459. }
  460. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
  461. {
  462. /* We want all CPUs to do sampling nearly on same jiffy */
  463. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  464. delay -= jiffies % delay;
  465. dbs_info->enable = 1;
  466. INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
  467. schedule_delayed_work_on(dbs_info->cpu, &dbs_info->work, delay);
  468. }
  469. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  470. {
  471. dbs_info->enable = 0;
  472. cancel_delayed_work_sync(&dbs_info->work);
  473. }
  474. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  475. unsigned int event)
  476. {
  477. unsigned int cpu = policy->cpu;
  478. struct cpu_dbs_info_s *this_dbs_info;
  479. unsigned int j;
  480. int rc;
  481. this_dbs_info = &per_cpu(cs_cpu_dbs_info, cpu);
  482. switch (event) {
  483. case CPUFREQ_GOV_START:
  484. if ((!cpu_online(cpu)) || (!policy->cur))
  485. return -EINVAL;
  486. mutex_lock(&dbs_mutex);
  487. rc = sysfs_create_group(&policy->kobj, &dbs_attr_group_old);
  488. if (rc) {
  489. mutex_unlock(&dbs_mutex);
  490. return rc;
  491. }
  492. for_each_cpu(j, policy->cpus) {
  493. struct cpu_dbs_info_s *j_dbs_info;
  494. j_dbs_info = &per_cpu(cs_cpu_dbs_info, j);
  495. j_dbs_info->cur_policy = policy;
  496. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  497. &j_dbs_info->prev_cpu_wall);
  498. if (dbs_tuners_ins.ignore_nice) {
  499. j_dbs_info->prev_cpu_nice =
  500. kstat_cpu(j).cpustat.nice;
  501. }
  502. }
  503. this_dbs_info->down_skip = 0;
  504. this_dbs_info->requested_freq = policy->cur;
  505. mutex_init(&this_dbs_info->timer_mutex);
  506. dbs_enable++;
  507. /*
  508. * Start the timerschedule work, when this governor
  509. * is used for first time
  510. */
  511. if (dbs_enable == 1) {
  512. unsigned int latency;
  513. /* policy latency is in nS. Convert it to uS first */
  514. latency = policy->cpuinfo.transition_latency / 1000;
  515. if (latency == 0)
  516. latency = 1;
  517. rc = sysfs_create_group(cpufreq_global_kobject,
  518. &dbs_attr_group);
  519. if (rc) {
  520. mutex_unlock(&dbs_mutex);
  521. return rc;
  522. }
  523. /*
  524. * conservative does not implement micro like ondemand
  525. * governor, thus we are bound to jiffes/HZ
  526. */
  527. min_sampling_rate =
  528. MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
  529. /* Bring kernel and HW constraints together */
  530. min_sampling_rate = max(min_sampling_rate,
  531. MIN_LATENCY_MULTIPLIER * latency);
  532. dbs_tuners_ins.sampling_rate =
  533. max(min_sampling_rate,
  534. latency * LATENCY_MULTIPLIER);
  535. cpufreq_register_notifier(
  536. &dbs_cpufreq_notifier_block,
  537. CPUFREQ_TRANSITION_NOTIFIER);
  538. }
  539. mutex_unlock(&dbs_mutex);
  540. dbs_timer_init(this_dbs_info);
  541. break;
  542. case CPUFREQ_GOV_STOP:
  543. dbs_timer_exit(this_dbs_info);
  544. mutex_lock(&dbs_mutex);
  545. sysfs_remove_group(&policy->kobj, &dbs_attr_group_old);
  546. dbs_enable--;
  547. mutex_destroy(&this_dbs_info->timer_mutex);
  548. /*
  549. * Stop the timerschedule work, when this governor
  550. * is used for first time
  551. */
  552. if (dbs_enable == 0)
  553. cpufreq_unregister_notifier(
  554. &dbs_cpufreq_notifier_block,
  555. CPUFREQ_TRANSITION_NOTIFIER);
  556. mutex_unlock(&dbs_mutex);
  557. if (!dbs_enable)
  558. sysfs_remove_group(cpufreq_global_kobject,
  559. &dbs_attr_group);
  560. break;
  561. case CPUFREQ_GOV_LIMITS:
  562. mutex_lock(&this_dbs_info->timer_mutex);
  563. if (policy->max < this_dbs_info->cur_policy->cur)
  564. __cpufreq_driver_target(
  565. this_dbs_info->cur_policy,
  566. policy->max, CPUFREQ_RELATION_H);
  567. else if (policy->min > this_dbs_info->cur_policy->cur)
  568. __cpufreq_driver_target(
  569. this_dbs_info->cur_policy,
  570. policy->min, CPUFREQ_RELATION_L);
  571. mutex_unlock(&this_dbs_info->timer_mutex);
  572. break;
  573. }
  574. return 0;
  575. }
  576. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  577. static
  578. #endif
  579. struct cpufreq_governor cpufreq_gov_conservative = {
  580. .name = "conservative",
  581. .governor = cpufreq_governor_dbs,
  582. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  583. .owner = THIS_MODULE,
  584. };
  585. static int __init cpufreq_gov_dbs_init(void)
  586. {
  587. return cpufreq_register_governor(&cpufreq_gov_conservative);
  588. }
  589. static void __exit cpufreq_gov_dbs_exit(void)
  590. {
  591. cpufreq_unregister_governor(&cpufreq_gov_conservative);
  592. }
  593. MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
  594. MODULE_DESCRIPTION("'cpufreq_conservative' - A dynamic cpufreq governor for "
  595. "Low Latency Frequency Transition capable processors "
  596. "optimised for use in a battery environment");
  597. MODULE_LICENSE("GPL");
  598. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_CONSERVATIVE
  599. fs_initcall(cpufreq_gov_dbs_init);
  600. #else
  601. module_init(cpufreq_gov_dbs_init);
  602. #endif
  603. module_exit(cpufreq_gov_dbs_exit);