cpufreq_ondemand.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/cpufreq.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/kobject.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/percpu-defs.h>
  21. #include <linux/slab.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/tick.h>
  24. #include <linux/types.h>
  25. #include <linux/cpu.h>
  26. #include "cpufreq_governor.h"
  27. /* On-demand governor macros */
  28. #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
  29. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  30. #define DEF_SAMPLING_DOWN_FACTOR (1)
  31. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  32. #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
  33. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  34. #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
  35. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  36. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  37. static DEFINE_PER_CPU(struct od_cpu_dbs_info_s, od_cpu_dbs_info);
  38. static struct od_ops od_ops;
  39. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  40. static struct cpufreq_governor cpufreq_gov_ondemand;
  41. #endif
  42. static void ondemand_powersave_bias_init_cpu(int cpu)
  43. {
  44. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  45. dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
  46. dbs_info->freq_lo = 0;
  47. }
  48. /*
  49. * Not all CPUs want IO time to be accounted as busy; this depends on how
  50. * efficient idling at a higher frequency/voltage is.
  51. * Pavel Machek says this is not so for various generations of AMD and old
  52. * Intel systems.
  53. * Mike Chan (android.com) claims this is also not true for ARM.
  54. * Because of this, whitelist specific known (series) of CPUs by default, and
  55. * leave all others up to the user.
  56. */
  57. static int should_io_be_busy(void)
  58. {
  59. #if defined(CONFIG_X86)
  60. /*
  61. * For Intel, Core 2 (model 15) and later have an efficient idle.
  62. */
  63. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  64. boot_cpu_data.x86 == 6 &&
  65. boot_cpu_data.x86_model >= 15)
  66. return 1;
  67. #endif
  68. return 0;
  69. }
  70. /*
  71. * Find right freq to be set now with powersave_bias on.
  72. * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
  73. * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
  74. */
  75. static unsigned int generic_powersave_bias_target(struct cpufreq_policy *policy,
  76. unsigned int freq_next, unsigned int relation)
  77. {
  78. unsigned int freq_req, freq_reduc, freq_avg;
  79. unsigned int freq_hi, freq_lo;
  80. unsigned int index = 0;
  81. unsigned int jiffies_total, jiffies_hi, jiffies_lo;
  82. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  83. policy->cpu);
  84. struct dbs_data *dbs_data = policy->governor_data;
  85. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  86. if (!dbs_info->freq_table) {
  87. dbs_info->freq_lo = 0;
  88. dbs_info->freq_lo_jiffies = 0;
  89. return freq_next;
  90. }
  91. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  92. relation, &index);
  93. freq_req = dbs_info->freq_table[index].frequency;
  94. freq_reduc = freq_req * od_tuners->powersave_bias / 1000;
  95. freq_avg = freq_req - freq_reduc;
  96. /* Find freq bounds for freq_avg in freq_table */
  97. index = 0;
  98. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  99. CPUFREQ_RELATION_H, &index);
  100. freq_lo = dbs_info->freq_table[index].frequency;
  101. index = 0;
  102. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  103. CPUFREQ_RELATION_L, &index);
  104. freq_hi = dbs_info->freq_table[index].frequency;
  105. /* Find out how long we have to be in hi and lo freqs */
  106. if (freq_hi == freq_lo) {
  107. dbs_info->freq_lo = 0;
  108. dbs_info->freq_lo_jiffies = 0;
  109. return freq_lo;
  110. }
  111. jiffies_total = usecs_to_jiffies(od_tuners->sampling_rate);
  112. jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
  113. jiffies_hi += ((freq_hi - freq_lo) / 2);
  114. jiffies_hi /= (freq_hi - freq_lo);
  115. jiffies_lo = jiffies_total - jiffies_hi;
  116. dbs_info->freq_lo = freq_lo;
  117. dbs_info->freq_lo_jiffies = jiffies_lo;
  118. dbs_info->freq_hi_jiffies = jiffies_hi;
  119. return freq_hi;
  120. }
  121. static void ondemand_powersave_bias_init(void)
  122. {
  123. int i;
  124. for_each_online_cpu(i) {
  125. ondemand_powersave_bias_init_cpu(i);
  126. }
  127. }
  128. static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
  129. {
  130. struct dbs_data *dbs_data = p->governor_data;
  131. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  132. if (od_tuners->powersave_bias)
  133. freq = od_ops.powersave_bias_target(p, freq,
  134. CPUFREQ_RELATION_H);
  135. else if (p->cur == p->max)
  136. return;
  137. __cpufreq_driver_target(p, freq, od_tuners->powersave_bias ?
  138. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  139. }
  140. /*
  141. * Every sampling_rate, we check, if current idle time is less than 20%
  142. * (default), then we try to increase frequency. Every sampling_rate, we look
  143. * for the lowest frequency which can sustain the load while keeping idle time
  144. * over 30%. If such a frequency exist, we try to decrease to this frequency.
  145. *
  146. * Any frequency increase takes it to the maximum frequency. Frequency reduction
  147. * happens at minimum steps of 5% (default) of current frequency
  148. */
  149. static void od_check_cpu(int cpu, unsigned int load_freq)
  150. {
  151. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  152. struct cpufreq_policy *policy = dbs_info->cdbs.cur_policy;
  153. struct dbs_data *dbs_data = policy->governor_data;
  154. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  155. dbs_info->freq_lo = 0;
  156. /* Check for frequency increase */
  157. if (load_freq > od_tuners->up_threshold * policy->cur) {
  158. /* If switching to max speed, apply sampling_down_factor */
  159. if (policy->cur < policy->max)
  160. dbs_info->rate_mult =
  161. od_tuners->sampling_down_factor;
  162. dbs_freq_increase(policy, policy->max);
  163. return;
  164. }
  165. /* Check for frequency decrease */
  166. /* if we cannot reduce the frequency anymore, break out early */
  167. if (policy->cur == policy->min)
  168. return;
  169. /*
  170. * The optimal frequency is the frequency that is the lowest that can
  171. * support the current CPU usage without triggering the up policy. To be
  172. * safe, we focus 10 points under the threshold.
  173. */
  174. if (load_freq < od_tuners->adj_up_threshold
  175. * policy->cur) {
  176. unsigned int freq_next;
  177. freq_next = load_freq / od_tuners->adj_up_threshold;
  178. /* No longer fully busy, reset rate_mult */
  179. dbs_info->rate_mult = 1;
  180. if (freq_next < policy->min)
  181. freq_next = policy->min;
  182. if (!od_tuners->powersave_bias) {
  183. __cpufreq_driver_target(policy, freq_next,
  184. CPUFREQ_RELATION_L);
  185. return;
  186. }
  187. freq_next = od_ops.powersave_bias_target(policy, freq_next,
  188. CPUFREQ_RELATION_L);
  189. __cpufreq_driver_target(policy, freq_next, CPUFREQ_RELATION_L);
  190. }
  191. }
  192. static void od_dbs_timer(struct work_struct *work)
  193. {
  194. struct od_cpu_dbs_info_s *dbs_info =
  195. container_of(work, struct od_cpu_dbs_info_s, cdbs.work.work);
  196. unsigned int cpu = dbs_info->cdbs.cur_policy->cpu;
  197. struct od_cpu_dbs_info_s *core_dbs_info = &per_cpu(od_cpu_dbs_info,
  198. cpu);
  199. struct dbs_data *dbs_data = dbs_info->cdbs.cur_policy->governor_data;
  200. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  201. int delay = 0, sample_type = core_dbs_info->sample_type;
  202. bool modify_all = true;
  203. mutex_lock(&core_dbs_info->cdbs.timer_mutex);
  204. if (!need_load_eval(&core_dbs_info->cdbs, od_tuners->sampling_rate)) {
  205. modify_all = false;
  206. goto max_delay;
  207. }
  208. /* Common NORMAL_SAMPLE setup */
  209. core_dbs_info->sample_type = OD_NORMAL_SAMPLE;
  210. if (sample_type == OD_SUB_SAMPLE) {
  211. delay = core_dbs_info->freq_lo_jiffies;
  212. __cpufreq_driver_target(core_dbs_info->cdbs.cur_policy,
  213. core_dbs_info->freq_lo, CPUFREQ_RELATION_H);
  214. } else {
  215. dbs_check_cpu(dbs_data, cpu);
  216. if (core_dbs_info->freq_lo) {
  217. /* Setup timer for SUB_SAMPLE */
  218. core_dbs_info->sample_type = OD_SUB_SAMPLE;
  219. delay = core_dbs_info->freq_hi_jiffies;
  220. }
  221. }
  222. max_delay:
  223. if (!delay)
  224. delay = delay_for_sampling_rate(od_tuners->sampling_rate
  225. * core_dbs_info->rate_mult);
  226. gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy, delay, modify_all);
  227. mutex_unlock(&core_dbs_info->cdbs.timer_mutex);
  228. }
  229. /************************** sysfs interface ************************/
  230. static struct common_dbs_data od_dbs_cdata;
  231. /**
  232. * update_sampling_rate - update sampling rate effective immediately if needed.
  233. * @new_rate: new sampling rate
  234. *
  235. * If new rate is smaller than the old, simply updating
  236. * dbs_tuners_int.sampling_rate might not be appropriate. For example, if the
  237. * original sampling_rate was 1 second and the requested new sampling rate is 10
  238. * ms because the user needs immediate reaction from ondemand governor, but not
  239. * sure if higher frequency will be required or not, then, the governor may
  240. * change the sampling rate too late; up to 1 second later. Thus, if we are
  241. * reducing the sampling rate, we need to make the new value effective
  242. * immediately.
  243. */
  244. static void update_sampling_rate(struct dbs_data *dbs_data,
  245. unsigned int new_rate)
  246. {
  247. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  248. int cpu;
  249. od_tuners->sampling_rate = new_rate = max(new_rate,
  250. dbs_data->min_sampling_rate);
  251. for_each_online_cpu(cpu) {
  252. struct cpufreq_policy *policy;
  253. struct od_cpu_dbs_info_s *dbs_info;
  254. unsigned long next_sampling, appointed_at;
  255. policy = cpufreq_cpu_get(cpu);
  256. if (!policy)
  257. continue;
  258. if (policy->governor != &cpufreq_gov_ondemand) {
  259. cpufreq_cpu_put(policy);
  260. continue;
  261. }
  262. dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  263. cpufreq_cpu_put(policy);
  264. mutex_lock(&dbs_info->cdbs.timer_mutex);
  265. if (!delayed_work_pending(&dbs_info->cdbs.work)) {
  266. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  267. continue;
  268. }
  269. next_sampling = jiffies + usecs_to_jiffies(new_rate);
  270. appointed_at = dbs_info->cdbs.work.timer.expires;
  271. if (time_before(next_sampling, appointed_at)) {
  272. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  273. cancel_delayed_work_sync(&dbs_info->cdbs.work);
  274. mutex_lock(&dbs_info->cdbs.timer_mutex);
  275. gov_queue_work(dbs_data, dbs_info->cdbs.cur_policy,
  276. usecs_to_jiffies(new_rate), true);
  277. }
  278. mutex_unlock(&dbs_info->cdbs.timer_mutex);
  279. }
  280. }
  281. static ssize_t store_sampling_rate(struct dbs_data *dbs_data, const char *buf,
  282. size_t count)
  283. {
  284. unsigned int input;
  285. int ret;
  286. ret = sscanf(buf, "%u", &input);
  287. if (ret != 1)
  288. return -EINVAL;
  289. update_sampling_rate(dbs_data, input);
  290. return count;
  291. }
  292. static ssize_t store_io_is_busy(struct dbs_data *dbs_data, const char *buf,
  293. size_t count)
  294. {
  295. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  296. unsigned int input;
  297. int ret;
  298. unsigned int j;
  299. ret = sscanf(buf, "%u", &input);
  300. if (ret != 1)
  301. return -EINVAL;
  302. od_tuners->io_is_busy = !!input;
  303. /* we need to re-evaluate prev_cpu_idle */
  304. for_each_online_cpu(j) {
  305. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  306. j);
  307. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  308. &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
  309. }
  310. return count;
  311. }
  312. static ssize_t store_up_threshold(struct dbs_data *dbs_data, const char *buf,
  313. size_t count)
  314. {
  315. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  316. unsigned int input;
  317. int ret;
  318. ret = sscanf(buf, "%u", &input);
  319. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  320. input < MIN_FREQUENCY_UP_THRESHOLD) {
  321. return -EINVAL;
  322. }
  323. /* Calculate the new adj_up_threshold */
  324. od_tuners->adj_up_threshold += input;
  325. od_tuners->adj_up_threshold -= od_tuners->up_threshold;
  326. od_tuners->up_threshold = input;
  327. return count;
  328. }
  329. static ssize_t store_sampling_down_factor(struct dbs_data *dbs_data,
  330. const char *buf, size_t count)
  331. {
  332. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  333. unsigned int input, j;
  334. int ret;
  335. ret = sscanf(buf, "%u", &input);
  336. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  337. return -EINVAL;
  338. od_tuners->sampling_down_factor = input;
  339. /* Reset down sampling multiplier in case it was active */
  340. for_each_online_cpu(j) {
  341. struct od_cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  342. j);
  343. dbs_info->rate_mult = 1;
  344. }
  345. return count;
  346. }
  347. static ssize_t store_ignore_nice(struct dbs_data *dbs_data, const char *buf,
  348. size_t count)
  349. {
  350. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  351. unsigned int input;
  352. int ret;
  353. unsigned int j;
  354. ret = sscanf(buf, "%u", &input);
  355. if (ret != 1)
  356. return -EINVAL;
  357. if (input > 1)
  358. input = 1;
  359. if (input == od_tuners->ignore_nice) { /* nothing to do */
  360. return count;
  361. }
  362. od_tuners->ignore_nice = input;
  363. /* we need to re-evaluate prev_cpu_idle */
  364. for_each_online_cpu(j) {
  365. struct od_cpu_dbs_info_s *dbs_info;
  366. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  367. dbs_info->cdbs.prev_cpu_idle = get_cpu_idle_time(j,
  368. &dbs_info->cdbs.prev_cpu_wall, od_tuners->io_is_busy);
  369. if (od_tuners->ignore_nice)
  370. dbs_info->cdbs.prev_cpu_nice =
  371. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  372. }
  373. return count;
  374. }
  375. static ssize_t store_powersave_bias(struct dbs_data *dbs_data, const char *buf,
  376. size_t count)
  377. {
  378. struct od_dbs_tuners *od_tuners = dbs_data->tuners;
  379. unsigned int input;
  380. int ret;
  381. ret = sscanf(buf, "%u", &input);
  382. if (ret != 1)
  383. return -EINVAL;
  384. if (input > 1000)
  385. input = 1000;
  386. od_tuners->powersave_bias = input;
  387. ondemand_powersave_bias_init();
  388. return count;
  389. }
  390. show_store_one(od, sampling_rate);
  391. show_store_one(od, io_is_busy);
  392. show_store_one(od, up_threshold);
  393. show_store_one(od, sampling_down_factor);
  394. show_store_one(od, ignore_nice);
  395. show_store_one(od, powersave_bias);
  396. declare_show_sampling_rate_min(od);
  397. gov_sys_pol_attr_rw(sampling_rate);
  398. gov_sys_pol_attr_rw(io_is_busy);
  399. gov_sys_pol_attr_rw(up_threshold);
  400. gov_sys_pol_attr_rw(sampling_down_factor);
  401. gov_sys_pol_attr_rw(ignore_nice);
  402. gov_sys_pol_attr_rw(powersave_bias);
  403. gov_sys_pol_attr_ro(sampling_rate_min);
  404. static struct attribute *dbs_attributes_gov_sys[] = {
  405. &sampling_rate_min_gov_sys.attr,
  406. &sampling_rate_gov_sys.attr,
  407. &up_threshold_gov_sys.attr,
  408. &sampling_down_factor_gov_sys.attr,
  409. &ignore_nice_gov_sys.attr,
  410. &powersave_bias_gov_sys.attr,
  411. &io_is_busy_gov_sys.attr,
  412. NULL
  413. };
  414. static struct attribute_group od_attr_group_gov_sys = {
  415. .attrs = dbs_attributes_gov_sys,
  416. .name = "ondemand",
  417. };
  418. static struct attribute *dbs_attributes_gov_pol[] = {
  419. &sampling_rate_min_gov_pol.attr,
  420. &sampling_rate_gov_pol.attr,
  421. &up_threshold_gov_pol.attr,
  422. &sampling_down_factor_gov_pol.attr,
  423. &ignore_nice_gov_pol.attr,
  424. &powersave_bias_gov_pol.attr,
  425. &io_is_busy_gov_pol.attr,
  426. NULL
  427. };
  428. static struct attribute_group od_attr_group_gov_pol = {
  429. .attrs = dbs_attributes_gov_pol,
  430. .name = "ondemand",
  431. };
  432. /************************** sysfs end ************************/
  433. static int od_init(struct dbs_data *dbs_data)
  434. {
  435. struct od_dbs_tuners *tuners;
  436. u64 idle_time;
  437. int cpu;
  438. tuners = kzalloc(sizeof(struct od_dbs_tuners), GFP_KERNEL);
  439. if (!tuners) {
  440. pr_err("%s: kzalloc failed\n", __func__);
  441. return -ENOMEM;
  442. }
  443. cpu = get_cpu();
  444. idle_time = get_cpu_idle_time_us(cpu, NULL);
  445. put_cpu();
  446. if (idle_time != -1ULL) {
  447. /* Idle micro accounting is supported. Use finer thresholds */
  448. tuners->up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  449. tuners->adj_up_threshold = MICRO_FREQUENCY_UP_THRESHOLD -
  450. MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  451. /*
  452. * In nohz/micro accounting case we set the minimum frequency
  453. * not depending on HZ, but fixed (very low). The deferred
  454. * timer might skip some samples if idle/sleeping as needed.
  455. */
  456. dbs_data->min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  457. } else {
  458. tuners->up_threshold = DEF_FREQUENCY_UP_THRESHOLD;
  459. tuners->adj_up_threshold = DEF_FREQUENCY_UP_THRESHOLD -
  460. DEF_FREQUENCY_DOWN_DIFFERENTIAL;
  461. /* For correct statistics, we need 10 ticks for each measure */
  462. dbs_data->min_sampling_rate = MIN_SAMPLING_RATE_RATIO *
  463. jiffies_to_usecs(10);
  464. }
  465. tuners->sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR;
  466. tuners->ignore_nice = 0;
  467. tuners->powersave_bias = 0;
  468. tuners->io_is_busy = should_io_be_busy();
  469. dbs_data->tuners = tuners;
  470. mutex_init(&dbs_data->mutex);
  471. return 0;
  472. }
  473. static void od_exit(struct dbs_data *dbs_data)
  474. {
  475. kfree(dbs_data->tuners);
  476. }
  477. define_get_cpu_dbs_routines(od_cpu_dbs_info);
  478. static struct od_ops od_ops = {
  479. .powersave_bias_init_cpu = ondemand_powersave_bias_init_cpu,
  480. .powersave_bias_target = generic_powersave_bias_target,
  481. .freq_increase = dbs_freq_increase,
  482. };
  483. static struct common_dbs_data od_dbs_cdata = {
  484. .governor = GOV_ONDEMAND,
  485. .attr_group_gov_sys = &od_attr_group_gov_sys,
  486. .attr_group_gov_pol = &od_attr_group_gov_pol,
  487. .get_cpu_cdbs = get_cpu_cdbs,
  488. .get_cpu_dbs_info_s = get_cpu_dbs_info_s,
  489. .gov_dbs_timer = od_dbs_timer,
  490. .gov_check_cpu = od_check_cpu,
  491. .gov_ops = &od_ops,
  492. .init = od_init,
  493. .exit = od_exit,
  494. };
  495. static void od_set_powersave_bias(unsigned int powersave_bias)
  496. {
  497. struct cpufreq_policy *policy;
  498. struct dbs_data *dbs_data;
  499. struct od_dbs_tuners *od_tuners;
  500. unsigned int cpu;
  501. cpumask_t done;
  502. cpumask_clear(&done);
  503. get_online_cpus();
  504. for_each_online_cpu(cpu) {
  505. if (cpumask_test_cpu(cpu, &done))
  506. continue;
  507. policy = per_cpu(od_cpu_dbs_info, cpu).cdbs.cur_policy;
  508. dbs_data = policy->governor_data;
  509. od_tuners = dbs_data->tuners;
  510. od_tuners->powersave_bias = powersave_bias;
  511. cpumask_or(&done, &done, policy->cpus);
  512. }
  513. put_online_cpus();
  514. }
  515. void od_register_powersave_bias_handler(unsigned int (*f)
  516. (struct cpufreq_policy *, unsigned int, unsigned int),
  517. unsigned int powersave_bias)
  518. {
  519. od_ops.powersave_bias_target = f;
  520. od_set_powersave_bias(powersave_bias);
  521. }
  522. EXPORT_SYMBOL_GPL(od_register_powersave_bias_handler);
  523. void od_unregister_powersave_bias_handler(void)
  524. {
  525. od_ops.powersave_bias_target = generic_powersave_bias_target;
  526. od_set_powersave_bias(0);
  527. }
  528. EXPORT_SYMBOL_GPL(od_unregister_powersave_bias_handler);
  529. static int od_cpufreq_governor_dbs(struct cpufreq_policy *policy,
  530. unsigned int event)
  531. {
  532. return cpufreq_governor_dbs(policy, &od_dbs_cdata, event);
  533. }
  534. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  535. static
  536. #endif
  537. struct cpufreq_governor cpufreq_gov_ondemand = {
  538. .name = "ondemand",
  539. .governor = od_cpufreq_governor_dbs,
  540. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  541. .owner = THIS_MODULE,
  542. };
  543. static int __init cpufreq_gov_dbs_init(void)
  544. {
  545. return cpufreq_register_governor(&cpufreq_gov_ondemand);
  546. }
  547. static void __exit cpufreq_gov_dbs_exit(void)
  548. {
  549. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  550. }
  551. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  552. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  553. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  554. "Low Latency Frequency Transition capable processors");
  555. MODULE_LICENSE("GPL");
  556. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  557. fs_initcall(cpufreq_gov_dbs_init);
  558. #else
  559. module_init(cpufreq_gov_dbs_init);
  560. #endif
  561. module_exit(cpufreq_gov_dbs_exit);