cpufreq_conservative.c 20 KB

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