cpufreq_ondemand.c 19 KB

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