cpufreq_ondemand.c 21 KB

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