cpufreq_ondemand.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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. * (c) 2013, 2015 The Linux Foundation. All rights reserved.
  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/kthread.h>
  25. #include <linux/sched.h>
  26. #include <linux/input.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/slab.h>
  29. /*
  30. * dbs is used in this file as a shortform for demandbased switching
  31. * It helps to keep variable names smaller, simpler
  32. */
  33. #define DEF_FREQUENCY_DOWN_DIFFERENTIAL (10)
  34. #define DEF_FREQUENCY_UP_THRESHOLD (80)
  35. #define DEF_SAMPLING_DOWN_FACTOR (1)
  36. #define MAX_SAMPLING_DOWN_FACTOR (100000)
  37. #define MICRO_FREQUENCY_DOWN_DIFFERENTIAL (3)
  38. #define MICRO_FREQUENCY_UP_THRESHOLD (95)
  39. #define MICRO_FREQUENCY_MIN_SAMPLE_RATE (10000)
  40. #define MIN_FREQUENCY_UP_THRESHOLD (11)
  41. #define MAX_FREQUENCY_UP_THRESHOLD (100)
  42. #define MIN_FREQUENCY_DOWN_DIFFERENTIAL (1)
  43. #define STEP_UP
  44. #ifdef STEP_UP
  45. #define DEF_FREQ_STEP (20)
  46. #define DEF_STEP_UP_EARLY_HISPEED (787200)
  47. #define DEF_STEP_UP_INTERIM_HISPEED (998400)
  48. #define DEF_SAMPLING_EARLY_HISPEED_FACTOR (1)
  49. #define DEF_SAMPLING_INTERIM_HISPEED_FACTOR (1)
  50. #endif
  51. /*
  52. * The polling frequency of this governor depends on the capability of
  53. * the processor. Default polling frequency is 1000 times the transition
  54. * latency of the processor. The governor will work on any processor with
  55. * transition latency <= 10mS, using appropriate sampling
  56. * rate.
  57. * For CPUs with transition latency > 10mS (mostly drivers with CPUFREQ_ETERNAL)
  58. * this governor will not work.
  59. * All times here are in uS.
  60. */
  61. #define MIN_SAMPLING_RATE_RATIO (2)
  62. static unsigned int min_sampling_rate;
  63. #define LATENCY_MULTIPLIER (1000)
  64. #define MIN_LATENCY_MULTIPLIER (100)
  65. #define TRANSITION_LATENCY_LIMIT (10 * 1000 * 1000)
  66. #define POWERSAVE_BIAS_MAXLEVEL (1000)
  67. #define POWERSAVE_BIAS_MINLEVEL (-1000)
  68. static void do_dbs_timer(struct work_struct *work);
  69. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  70. unsigned int event);
  71. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  72. static
  73. #endif
  74. struct cpufreq_governor cpufreq_gov_ondemand = {
  75. .name = "ondemand",
  76. .governor = cpufreq_governor_dbs,
  77. .max_transition_latency = TRANSITION_LATENCY_LIMIT,
  78. .owner = THIS_MODULE,
  79. };
  80. /* Sampling types */
  81. enum {DBS_NORMAL_SAMPLE, DBS_SUB_SAMPLE};
  82. struct cpu_dbs_info_s {
  83. cputime64_t prev_cpu_idle;
  84. cputime64_t prev_cpu_iowait;
  85. cputime64_t prev_cpu_wall;
  86. cputime64_t prev_cpu_nice;
  87. struct cpufreq_policy *cur_policy;
  88. struct delayed_work work;
  89. struct cpufreq_frequency_table *freq_table;
  90. unsigned int freq_lo;
  91. unsigned int freq_lo_jiffies;
  92. unsigned int freq_hi_jiffies;
  93. unsigned int rate_mult;
  94. unsigned int prev_load;
  95. unsigned int max_load;
  96. int cpu;
  97. unsigned int sample_type:1;
  98. #ifdef STEP_UP
  99. unsigned int freq_stay_count;
  100. #endif
  101. /*
  102. * percpu mutex that serializes governor limit change with
  103. * do_dbs_timer invocation. We do not want do_dbs_timer to run
  104. * when user is changing the governor or limits.
  105. */
  106. struct mutex timer_mutex;
  107. struct task_struct *sync_thread;
  108. wait_queue_head_t sync_wq;
  109. atomic_t src_sync_cpu;
  110. atomic_t being_woken;
  111. atomic_t sync_enabled;
  112. };
  113. static DEFINE_PER_CPU(struct cpu_dbs_info_s, od_cpu_dbs_info);
  114. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info);
  115. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info);
  116. static unsigned int dbs_enable; /* number of CPUs using this policy */
  117. /*
  118. * dbs_mutex protects dbs_enable and dbs_info during start/stop.
  119. */
  120. static DEFINE_MUTEX(dbs_mutex);
  121. static struct workqueue_struct *dbs_wq;
  122. struct dbs_work_struct {
  123. struct work_struct work;
  124. unsigned int cpu;
  125. };
  126. static DEFINE_PER_CPU(struct dbs_work_struct, dbs_refresh_work);
  127. static struct dbs_tuners {
  128. unsigned int sampling_rate;
  129. unsigned int up_threshold;
  130. unsigned int up_threshold_multi_core;
  131. unsigned int down_differential;
  132. unsigned int down_differential_multi_core;
  133. unsigned int optimal_freq;
  134. unsigned int up_threshold_any_cpu_load;
  135. unsigned int sync_freq;
  136. unsigned int ignore_nice;
  137. unsigned int sampling_down_factor;
  138. int powersave_bias;
  139. unsigned int io_is_busy;
  140. unsigned int input_boost;
  141. #ifdef STEP_UP
  142. unsigned int freq_step;
  143. unsigned int step_up_early_hispeed;
  144. unsigned int step_up_interim_hispeed;
  145. unsigned int sampling_early_factor;
  146. unsigned int sampling_interim_factor;
  147. #endif
  148. } dbs_tuners_ins = {
  149. .up_threshold_multi_core = DEF_FREQUENCY_UP_THRESHOLD,
  150. .up_threshold = DEF_FREQUENCY_UP_THRESHOLD,
  151. .sampling_down_factor = DEF_SAMPLING_DOWN_FACTOR,
  152. .down_differential = DEF_FREQUENCY_DOWN_DIFFERENTIAL,
  153. .down_differential_multi_core = MICRO_FREQUENCY_DOWN_DIFFERENTIAL,
  154. .up_threshold_any_cpu_load = DEF_FREQUENCY_UP_THRESHOLD,
  155. .ignore_nice = 0,
  156. .powersave_bias = 0,
  157. .sync_freq = 0,
  158. .optimal_freq = 0,
  159. .input_boost = 0,
  160. #ifdef STEP_UP
  161. .freq_step = DEF_FREQ_STEP,
  162. .step_up_early_hispeed = DEF_STEP_UP_EARLY_HISPEED,
  163. .step_up_interim_hispeed = DEF_STEP_UP_INTERIM_HISPEED,
  164. .sampling_early_factor = DEF_SAMPLING_EARLY_HISPEED_FACTOR,
  165. .sampling_interim_factor = DEF_SAMPLING_INTERIM_HISPEED_FACTOR,
  166. #endif
  167. };
  168. static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
  169. {
  170. u64 idle_time;
  171. u64 cur_wall_time;
  172. u64 busy_time;
  173. cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
  174. busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
  175. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
  176. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
  177. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
  178. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
  179. busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
  180. idle_time = cur_wall_time - busy_time;
  181. if (wall)
  182. *wall = jiffies_to_usecs(cur_wall_time);
  183. return jiffies_to_usecs(idle_time);
  184. }
  185. static inline cputime64_t get_cpu_idle_time(unsigned int cpu, cputime64_t *wall)
  186. {
  187. u64 idle_time = get_cpu_idle_time_us(cpu, NULL);
  188. if (idle_time == -1ULL)
  189. return get_cpu_idle_time_jiffy(cpu, wall);
  190. else
  191. idle_time += get_cpu_iowait_time_us(cpu, wall);
  192. return idle_time;
  193. }
  194. static inline cputime64_t get_cpu_iowait_time(unsigned int cpu, cputime64_t *wall)
  195. {
  196. u64 iowait_time = get_cpu_iowait_time_us(cpu, wall);
  197. if (iowait_time == -1ULL)
  198. return 0;
  199. return iowait_time;
  200. }
  201. /*
  202. * Find right freq to be set now with powersave_bias on.
  203. * Returns the freq_hi to be used right now and will set freq_hi_jiffies,
  204. * freq_lo, and freq_lo_jiffies in percpu area for averaging freqs.
  205. */
  206. static unsigned int powersave_bias_target(struct cpufreq_policy *policy,
  207. unsigned int freq_next,
  208. unsigned int relation)
  209. {
  210. unsigned int freq_req, freq_avg;
  211. unsigned int freq_hi, freq_lo;
  212. unsigned int index = 0;
  213. unsigned int jiffies_total, jiffies_hi, jiffies_lo;
  214. int freq_reduc;
  215. struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info,
  216. policy->cpu);
  217. if (!dbs_info->freq_table) {
  218. dbs_info->freq_lo = 0;
  219. dbs_info->freq_lo_jiffies = 0;
  220. return freq_next;
  221. }
  222. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_next,
  223. relation, &index);
  224. freq_req = dbs_info->freq_table[index].frequency;
  225. freq_reduc = freq_req * dbs_tuners_ins.powersave_bias / 1000;
  226. freq_avg = freq_req - freq_reduc;
  227. /* Find freq bounds for freq_avg in freq_table */
  228. index = 0;
  229. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  230. CPUFREQ_RELATION_H, &index);
  231. freq_lo = dbs_info->freq_table[index].frequency;
  232. index = 0;
  233. cpufreq_frequency_table_target(policy, dbs_info->freq_table, freq_avg,
  234. CPUFREQ_RELATION_L, &index);
  235. freq_hi = dbs_info->freq_table[index].frequency;
  236. /* Find out how long we have to be in hi and lo freqs */
  237. if (freq_hi == freq_lo) {
  238. dbs_info->freq_lo = 0;
  239. dbs_info->freq_lo_jiffies = 0;
  240. return freq_lo;
  241. }
  242. jiffies_total = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  243. jiffies_hi = (freq_avg - freq_lo) * jiffies_total;
  244. jiffies_hi += ((freq_hi - freq_lo) / 2);
  245. jiffies_hi /= (freq_hi - freq_lo);
  246. jiffies_lo = jiffies_total - jiffies_hi;
  247. dbs_info->freq_lo = freq_lo;
  248. dbs_info->freq_lo_jiffies = jiffies_lo;
  249. dbs_info->freq_hi_jiffies = jiffies_hi;
  250. return freq_hi;
  251. }
  252. static int ondemand_powersave_bias_setspeed(struct cpufreq_policy *policy,
  253. struct cpufreq_policy *altpolicy,
  254. int level)
  255. {
  256. if (level == POWERSAVE_BIAS_MAXLEVEL) {
  257. /* maximum powersave; set to lowest frequency */
  258. __cpufreq_driver_target(policy,
  259. (altpolicy) ? altpolicy->min : policy->min,
  260. CPUFREQ_RELATION_L);
  261. return 1;
  262. } else if (level == POWERSAVE_BIAS_MINLEVEL) {
  263. /* minimum powersave; set to highest frequency */
  264. __cpufreq_driver_target(policy,
  265. (altpolicy) ? altpolicy->max : policy->max,
  266. CPUFREQ_RELATION_H);
  267. return 1;
  268. }
  269. return 0;
  270. }
  271. static void ondemand_powersave_bias_init_cpu(int cpu)
  272. {
  273. struct cpu_dbs_info_s *dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  274. dbs_info->freq_table = cpufreq_frequency_get_table(cpu);
  275. dbs_info->freq_lo = 0;
  276. }
  277. static void ondemand_powersave_bias_init(void)
  278. {
  279. int i;
  280. for_each_online_cpu(i) {
  281. ondemand_powersave_bias_init_cpu(i);
  282. }
  283. }
  284. /************************** sysfs interface ************************/
  285. static ssize_t show_sampling_rate_min(struct kobject *kobj,
  286. struct attribute *attr, char *buf)
  287. {
  288. return sprintf(buf, "%u\n", min_sampling_rate);
  289. }
  290. define_one_global_ro(sampling_rate_min);
  291. /* cpufreq_ondemand Governor Tunables */
  292. #define show_one(file_name, object) \
  293. static ssize_t show_##file_name \
  294. (struct kobject *kobj, struct attribute *attr, char *buf) \
  295. { \
  296. return sprintf(buf, "%u\n", dbs_tuners_ins.object); \
  297. }
  298. show_one(sampling_rate, sampling_rate);
  299. show_one(io_is_busy, io_is_busy);
  300. show_one(up_threshold, up_threshold);
  301. show_one(up_threshold_multi_core, up_threshold_multi_core);
  302. show_one(down_differential, down_differential);
  303. show_one(sampling_down_factor, sampling_down_factor);
  304. show_one(ignore_nice_load, ignore_nice);
  305. show_one(down_differential_multi_core, down_differential_multi_core);
  306. show_one(optimal_freq, optimal_freq);
  307. show_one(up_threshold_any_cpu_load, up_threshold_any_cpu_load);
  308. show_one(sync_freq, sync_freq);
  309. show_one(input_boost, input_boost);
  310. #ifdef STEP_UP
  311. show_one(freq_step, freq_step);
  312. show_one(step_up_early_hispeed, step_up_early_hispeed);
  313. show_one(step_up_interim_hispeed, step_up_interim_hispeed);
  314. show_one(sampling_early_factor, sampling_early_factor);
  315. show_one(sampling_interim_factor, sampling_interim_factor);
  316. #endif
  317. static ssize_t show_powersave_bias
  318. (struct kobject *kobj, struct attribute *attr, char *buf)
  319. {
  320. return snprintf(buf, PAGE_SIZE, "%d\n", dbs_tuners_ins.powersave_bias);
  321. }
  322. /**
  323. * update_sampling_rate - update sampling rate effective immediately if needed.
  324. * @new_rate: new sampling rate
  325. *
  326. * If new rate is smaller than the old, simply updaing
  327. * dbs_tuners_int.sampling_rate might not be appropriate. For example,
  328. * if the original sampling_rate was 1 second and the requested new sampling
  329. * rate is 10 ms because the user needs immediate reaction from ondemand
  330. * governor, but not sure if higher frequency will be required or not,
  331. * then, the governor may change the sampling rate too late; up to 1 second
  332. * later. Thus, if we are reducing the sampling rate, we need to make the
  333. * new value effective immediately.
  334. */
  335. static void update_sampling_rate(unsigned int new_rate)
  336. {
  337. int cpu;
  338. dbs_tuners_ins.sampling_rate = new_rate
  339. = max(new_rate, min_sampling_rate);
  340. get_online_cpus();
  341. mutex_lock(&dbs_mutex);
  342. for_each_online_cpu(cpu) {
  343. struct cpufreq_policy *policy;
  344. struct cpu_dbs_info_s *dbs_info;
  345. unsigned long next_sampling, appointed_at;
  346. policy = cpufreq_cpu_get(cpu);
  347. if (!policy)
  348. continue;
  349. dbs_info = &per_cpu(od_cpu_dbs_info, policy->cpu);
  350. cpufreq_cpu_put(policy);
  351. mutex_lock(&dbs_info->timer_mutex);
  352. if (!delayed_work_pending(&dbs_info->work)) {
  353. mutex_unlock(&dbs_info->timer_mutex);
  354. continue;
  355. }
  356. next_sampling = jiffies + usecs_to_jiffies(new_rate);
  357. appointed_at = dbs_info->work.timer.expires;
  358. if (time_before(next_sampling, appointed_at)) {
  359. mutex_unlock(&dbs_info->timer_mutex);
  360. cancel_delayed_work_sync(&dbs_info->work);
  361. mutex_lock(&dbs_info->timer_mutex);
  362. queue_delayed_work_on(dbs_info->cpu, dbs_wq,
  363. &dbs_info->work, usecs_to_jiffies(new_rate));
  364. }
  365. mutex_unlock(&dbs_info->timer_mutex);
  366. }
  367. mutex_unlock(&dbs_mutex);
  368. put_online_cpus();
  369. }
  370. static ssize_t store_sampling_rate(struct kobject *a, struct attribute *b,
  371. const char *buf, size_t count)
  372. {
  373. unsigned int input;
  374. int ret;
  375. ret = sscanf(buf, "%u", &input);
  376. if (ret != 1)
  377. return -EINVAL;
  378. update_sampling_rate(input);
  379. return count;
  380. }
  381. static ssize_t store_input_boost(struct kobject *a, struct attribute *b,
  382. const char *buf, size_t count)
  383. {
  384. unsigned int input;
  385. int ret;
  386. ret = sscanf(buf, "%u", &input);
  387. if (ret != 1)
  388. return -EINVAL;
  389. dbs_tuners_ins.input_boost = input;
  390. return count;
  391. }
  392. static ssize_t store_sync_freq(struct kobject *a, struct attribute *b,
  393. const char *buf, size_t count)
  394. {
  395. unsigned int input;
  396. int ret;
  397. ret = sscanf(buf, "%u", &input);
  398. if (ret != 1)
  399. return -EINVAL;
  400. dbs_tuners_ins.sync_freq = input;
  401. return count;
  402. }
  403. static ssize_t store_io_is_busy(struct kobject *a, struct attribute *b,
  404. const char *buf, size_t count)
  405. {
  406. unsigned int input;
  407. int ret;
  408. ret = sscanf(buf, "%u", &input);
  409. if (ret != 1)
  410. return -EINVAL;
  411. dbs_tuners_ins.io_is_busy = !!input;
  412. return count;
  413. }
  414. static ssize_t store_down_differential_multi_core(struct kobject *a,
  415. struct attribute *b, const char *buf, size_t count)
  416. {
  417. unsigned int input;
  418. int ret;
  419. ret = sscanf(buf, "%u", &input);
  420. if (ret != 1)
  421. return -EINVAL;
  422. dbs_tuners_ins.down_differential_multi_core = input;
  423. return count;
  424. }
  425. static ssize_t store_optimal_freq(struct kobject *a, struct attribute *b,
  426. const char *buf, size_t count)
  427. {
  428. unsigned int input;
  429. int ret;
  430. ret = sscanf(buf, "%u", &input);
  431. if (ret != 1)
  432. return -EINVAL;
  433. dbs_tuners_ins.optimal_freq = input;
  434. return count;
  435. }
  436. static ssize_t store_up_threshold(struct kobject *a, struct attribute *b,
  437. const char *buf, size_t count)
  438. {
  439. unsigned int input;
  440. int ret;
  441. ret = sscanf(buf, "%u", &input);
  442. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  443. input < MIN_FREQUENCY_UP_THRESHOLD) {
  444. return -EINVAL;
  445. }
  446. dbs_tuners_ins.up_threshold = input;
  447. return count;
  448. }
  449. static ssize_t store_up_threshold_multi_core(struct kobject *a,
  450. struct attribute *b, const char *buf, size_t count)
  451. {
  452. unsigned int input;
  453. int ret;
  454. ret = sscanf(buf, "%u", &input);
  455. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  456. input < MIN_FREQUENCY_UP_THRESHOLD) {
  457. return -EINVAL;
  458. }
  459. dbs_tuners_ins.up_threshold_multi_core = input;
  460. return count;
  461. }
  462. static ssize_t store_up_threshold_any_cpu_load(struct kobject *a,
  463. struct attribute *b, const char *buf, size_t count)
  464. {
  465. unsigned int input;
  466. int ret;
  467. ret = sscanf(buf, "%u", &input);
  468. if (ret != 1 || input > MAX_FREQUENCY_UP_THRESHOLD ||
  469. input < MIN_FREQUENCY_UP_THRESHOLD) {
  470. return -EINVAL;
  471. }
  472. dbs_tuners_ins.up_threshold_any_cpu_load = input;
  473. return count;
  474. }
  475. static ssize_t store_down_differential(struct kobject *a, struct attribute *b,
  476. const char *buf, size_t count)
  477. {
  478. unsigned int input;
  479. int ret;
  480. ret = sscanf(buf, "%u", &input);
  481. if (ret != 1 || input >= dbs_tuners_ins.up_threshold ||
  482. input < MIN_FREQUENCY_DOWN_DIFFERENTIAL) {
  483. return -EINVAL;
  484. }
  485. dbs_tuners_ins.down_differential = input;
  486. return count;
  487. }
  488. static ssize_t store_sampling_down_factor(struct kobject *a,
  489. struct attribute *b, const char *buf, size_t count)
  490. {
  491. unsigned int input, j;
  492. int ret;
  493. ret = sscanf(buf, "%u", &input);
  494. if (ret != 1 || input > MAX_SAMPLING_DOWN_FACTOR || input < 1)
  495. return -EINVAL;
  496. dbs_tuners_ins.sampling_down_factor = input;
  497. /* Reset down sampling multiplier in case it was active */
  498. for_each_online_cpu(j) {
  499. struct cpu_dbs_info_s *dbs_info;
  500. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  501. dbs_info->rate_mult = 1;
  502. }
  503. return count;
  504. }
  505. static ssize_t store_ignore_nice_load(struct kobject *a, struct attribute *b,
  506. const char *buf, size_t count)
  507. {
  508. unsigned int input;
  509. int ret;
  510. unsigned int j;
  511. ret = sscanf(buf, "%u", &input);
  512. if (ret != 1)
  513. return -EINVAL;
  514. if (input > 1)
  515. input = 1;
  516. if (input == dbs_tuners_ins.ignore_nice) { /* nothing to do */
  517. return count;
  518. }
  519. dbs_tuners_ins.ignore_nice = input;
  520. /* we need to re-evaluate prev_cpu_idle */
  521. for_each_online_cpu(j) {
  522. struct cpu_dbs_info_s *dbs_info;
  523. dbs_info = &per_cpu(od_cpu_dbs_info, j);
  524. dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  525. &dbs_info->prev_cpu_wall);
  526. if (dbs_tuners_ins.ignore_nice)
  527. dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  528. }
  529. return count;
  530. }
  531. static ssize_t store_powersave_bias(struct kobject *a, struct attribute *b,
  532. const char *buf, size_t count)
  533. {
  534. int input = 0;
  535. int bypass = 0;
  536. int ret, cpu, reenable_timer, j;
  537. struct cpu_dbs_info_s *dbs_info;
  538. struct cpumask cpus_timer_done;
  539. cpumask_clear(&cpus_timer_done);
  540. ret = sscanf(buf, "%d", &input);
  541. if (ret != 1)
  542. return -EINVAL;
  543. if (input >= POWERSAVE_BIAS_MAXLEVEL) {
  544. input = POWERSAVE_BIAS_MAXLEVEL;
  545. bypass = 1;
  546. } else if (input <= POWERSAVE_BIAS_MINLEVEL) {
  547. input = POWERSAVE_BIAS_MINLEVEL;
  548. bypass = 1;
  549. }
  550. if (input == dbs_tuners_ins.powersave_bias) {
  551. /* no change */
  552. return count;
  553. }
  554. reenable_timer = ((dbs_tuners_ins.powersave_bias ==
  555. POWERSAVE_BIAS_MAXLEVEL) ||
  556. (dbs_tuners_ins.powersave_bias ==
  557. POWERSAVE_BIAS_MINLEVEL));
  558. dbs_tuners_ins.powersave_bias = input;
  559. get_online_cpus();
  560. mutex_lock(&dbs_mutex);
  561. if (!bypass) {
  562. if (reenable_timer) {
  563. /* reinstate dbs timer */
  564. for_each_online_cpu(cpu) {
  565. if (lock_policy_rwsem_write(cpu) < 0)
  566. continue;
  567. dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  568. for_each_cpu(j, &cpus_timer_done) {
  569. if (!dbs_info->cur_policy) {
  570. pr_err("Dbs policy is NULL\n");
  571. goto skip_this_cpu;
  572. }
  573. if (cpumask_test_cpu(j, dbs_info->
  574. cur_policy->cpus))
  575. goto skip_this_cpu;
  576. }
  577. cpumask_set_cpu(cpu, &cpus_timer_done);
  578. if (dbs_info->cur_policy) {
  579. dbs_timer_exit(dbs_info);
  580. /* restart dbs timer */
  581. mutex_lock(&dbs_info->timer_mutex);
  582. dbs_timer_init(dbs_info);
  583. /* Enable frequency synchronization
  584. * of CPUs */
  585. mutex_unlock(&dbs_info->timer_mutex);
  586. atomic_set(&dbs_info->sync_enabled, 1);
  587. }
  588. skip_this_cpu:
  589. unlock_policy_rwsem_write(cpu);
  590. }
  591. }
  592. ondemand_powersave_bias_init();
  593. } else {
  594. /* running at maximum or minimum frequencies; cancel
  595. dbs timer as periodic load sampling is not necessary */
  596. for_each_online_cpu(cpu) {
  597. if (lock_policy_rwsem_write(cpu) < 0)
  598. continue;
  599. dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  600. for_each_cpu(j, &cpus_timer_done) {
  601. if (!dbs_info->cur_policy) {
  602. pr_err("Dbs policy is NULL\n");
  603. goto skip_this_cpu_bypass;
  604. }
  605. if (cpumask_test_cpu(j, dbs_info->
  606. cur_policy->cpus))
  607. goto skip_this_cpu_bypass;
  608. }
  609. cpumask_set_cpu(cpu, &cpus_timer_done);
  610. if (dbs_info->cur_policy) {
  611. /* cpu using ondemand, cancel dbs timer */
  612. dbs_timer_exit(dbs_info);
  613. /* Disable frequency synchronization of
  614. * CPUs to avoid re-queueing of work from
  615. * sync_thread */
  616. atomic_set(&dbs_info->sync_enabled, 0);
  617. mutex_lock(&dbs_info->timer_mutex);
  618. ondemand_powersave_bias_setspeed(
  619. dbs_info->cur_policy,
  620. NULL,
  621. input);
  622. mutex_unlock(&dbs_info->timer_mutex);
  623. }
  624. skip_this_cpu_bypass:
  625. unlock_policy_rwsem_write(cpu);
  626. }
  627. }
  628. mutex_unlock(&dbs_mutex);
  629. put_online_cpus();
  630. return count;
  631. }
  632. #ifdef STEP_UP
  633. static ssize_t store_freq_step(struct kobject *a,
  634. struct attribute *b, const char *buf, size_t count)
  635. {
  636. unsigned int input;
  637. int ret;
  638. ret = sscanf(buf, "%u", &input);
  639. if (ret != 1 || input > 100 ||
  640. input < 0) {
  641. return -EINVAL;
  642. }
  643. dbs_tuners_ins.freq_step = input;
  644. return count;
  645. }
  646. static ssize_t store_step_up_early_hispeed(struct kobject *a,
  647. struct attribute *b, const char *buf, size_t count)
  648. {
  649. unsigned int input;
  650. int ret;
  651. ret = sscanf(buf, "%u", &input);
  652. if (ret != 1 || input > 2265600 ||
  653. input < 0) {
  654. return -EINVAL;
  655. }
  656. dbs_tuners_ins.step_up_early_hispeed = input;
  657. return count;
  658. }
  659. static ssize_t store_step_up_interim_hispeed(struct kobject *a,
  660. struct attribute *b, const char *buf, size_t count)
  661. {
  662. unsigned int input;
  663. int ret;
  664. ret = sscanf(buf, "%u", &input);
  665. if (ret != 1 || input > 2265600 ||
  666. input < 0) {
  667. return -EINVAL;
  668. }
  669. dbs_tuners_ins.step_up_interim_hispeed = input;
  670. return count;
  671. }
  672. static ssize_t store_sampling_early_factor(struct kobject *a,
  673. struct attribute *b, const char *buf, size_t count)
  674. {
  675. unsigned int input;
  676. int ret;
  677. ret = sscanf(buf, "%u", &input);
  678. if (ret != 1 || input < 1)
  679. return -EINVAL;
  680. dbs_tuners_ins.sampling_early_factor = input;
  681. return count;
  682. }
  683. static ssize_t store_sampling_interim_factor(struct kobject *a,
  684. struct attribute *b, const char *buf, size_t count)
  685. {
  686. unsigned int input;
  687. int ret;
  688. ret = sscanf(buf, "%u", &input);
  689. if (ret != 1 || input < 1)
  690. return -EINVAL;
  691. dbs_tuners_ins.sampling_interim_factor = input;
  692. return count;
  693. }
  694. #endif
  695. define_one_global_rw(sampling_rate);
  696. define_one_global_rw(io_is_busy);
  697. define_one_global_rw(up_threshold);
  698. define_one_global_rw(down_differential);
  699. define_one_global_rw(sampling_down_factor);
  700. define_one_global_rw(ignore_nice_load);
  701. define_one_global_rw(powersave_bias);
  702. define_one_global_rw(up_threshold_multi_core);
  703. define_one_global_rw(down_differential_multi_core);
  704. define_one_global_rw(optimal_freq);
  705. define_one_global_rw(up_threshold_any_cpu_load);
  706. define_one_global_rw(sync_freq);
  707. define_one_global_rw(input_boost);
  708. #ifdef STEP_UP
  709. define_one_global_rw(freq_step);
  710. define_one_global_rw(step_up_early_hispeed);
  711. define_one_global_rw(step_up_interim_hispeed);
  712. define_one_global_rw(sampling_early_factor);
  713. define_one_global_rw(sampling_interim_factor);
  714. #endif
  715. static struct attribute *dbs_attributes[] = {
  716. &sampling_rate_min.attr,
  717. &sampling_rate.attr,
  718. &up_threshold.attr,
  719. &down_differential.attr,
  720. &sampling_down_factor.attr,
  721. &ignore_nice_load.attr,
  722. &powersave_bias.attr,
  723. &io_is_busy.attr,
  724. &up_threshold_multi_core.attr,
  725. &down_differential_multi_core.attr,
  726. &optimal_freq.attr,
  727. &up_threshold_any_cpu_load.attr,
  728. &sync_freq.attr,
  729. &input_boost.attr,
  730. #ifdef STEP_UP
  731. &freq_step.attr,
  732. &step_up_early_hispeed.attr,
  733. &step_up_interim_hispeed.attr,
  734. &sampling_early_factor.attr,
  735. &sampling_interim_factor.attr,
  736. #endif
  737. NULL
  738. };
  739. static struct attribute_group dbs_attr_group = {
  740. .attrs = dbs_attributes,
  741. .name = "ondemand",
  742. };
  743. /************************** sysfs end ************************/
  744. static void dbs_freq_increase(struct cpufreq_policy *p, unsigned int freq)
  745. {
  746. if (dbs_tuners_ins.powersave_bias)
  747. freq = powersave_bias_target(p, freq, CPUFREQ_RELATION_H);
  748. else if (p->cur == p->max)
  749. return;
  750. __cpufreq_driver_target(p, freq, dbs_tuners_ins.powersave_bias ?
  751. CPUFREQ_RELATION_L : CPUFREQ_RELATION_H);
  752. }
  753. static void dbs_check_cpu(struct cpu_dbs_info_s *this_dbs_info)
  754. {
  755. /* Extrapolated load of this CPU */
  756. unsigned int load_at_max_freq = 0;
  757. unsigned int max_load_freq;
  758. /* Current load across this CPU */
  759. unsigned int cur_load = 0;
  760. unsigned int max_load_other_cpu = 0;
  761. struct cpufreq_policy *policy;
  762. unsigned int j;
  763. this_dbs_info->freq_lo = 0;
  764. policy = this_dbs_info->cur_policy;
  765. if(policy == NULL)
  766. return;
  767. /*
  768. * Every sampling_rate, we check, if current idle time is less
  769. * than 20% (default), then we try to increase frequency
  770. * Every sampling_rate, we look for a the lowest
  771. * frequency which can sustain the load while keeping idle time over
  772. * 30%. If such a frequency exist, we try to decrease to this frequency.
  773. *
  774. * Any frequency increase takes it to the maximum frequency.
  775. * Frequency reduction happens at minimum steps of
  776. * 5% (default) of current frequency
  777. */
  778. /* Get Absolute Load - in terms of freq */
  779. max_load_freq = 0;
  780. for_each_cpu(j, policy->cpus) {
  781. struct cpu_dbs_info_s *j_dbs_info;
  782. cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
  783. unsigned int idle_time, wall_time, iowait_time;
  784. unsigned int load_freq;
  785. int freq_avg;
  786. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  787. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
  788. cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
  789. wall_time = (unsigned int)
  790. (cur_wall_time - j_dbs_info->prev_cpu_wall);
  791. j_dbs_info->prev_cpu_wall = cur_wall_time;
  792. idle_time = (unsigned int)
  793. (cur_idle_time - j_dbs_info->prev_cpu_idle);
  794. j_dbs_info->prev_cpu_idle = cur_idle_time;
  795. iowait_time = (unsigned int)
  796. (cur_iowait_time - j_dbs_info->prev_cpu_iowait);
  797. j_dbs_info->prev_cpu_iowait = cur_iowait_time;
  798. if (dbs_tuners_ins.ignore_nice) {
  799. u64 cur_nice;
  800. unsigned long cur_nice_jiffies;
  801. cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  802. j_dbs_info->prev_cpu_nice;
  803. /*
  804. * Assumption: nice time between sampling periods will
  805. * be less than 2^32 jiffies for 32 bit sys
  806. */
  807. cur_nice_jiffies = (unsigned long)
  808. cputime64_to_jiffies64(cur_nice);
  809. j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  810. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  811. }
  812. /*
  813. * For the purpose of ondemand, waiting for disk IO is an
  814. * indication that you're performance critical, and not that
  815. * the system is actually idle. So subtract the iowait time
  816. * from the cpu idle time.
  817. */
  818. if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
  819. idle_time -= iowait_time;
  820. if (unlikely(!wall_time || wall_time < idle_time))
  821. continue;
  822. cur_load = 100 * (wall_time - idle_time) / wall_time;
  823. j_dbs_info->max_load = max(cur_load, j_dbs_info->prev_load);
  824. j_dbs_info->prev_load = cur_load;
  825. freq_avg = __cpufreq_driver_getavg(policy, j);
  826. if (policy == NULL)
  827. return;
  828. if (freq_avg <= 0)
  829. freq_avg = policy->cur;
  830. load_freq = cur_load * freq_avg;
  831. if (load_freq > max_load_freq)
  832. max_load_freq = load_freq;
  833. }
  834. for_each_online_cpu(j) {
  835. struct cpu_dbs_info_s *j_dbs_info;
  836. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  837. if (j == policy->cpu)
  838. continue;
  839. if (max_load_other_cpu < j_dbs_info->max_load)
  840. max_load_other_cpu = j_dbs_info->max_load;
  841. /*
  842. * The other cpu could be running at higher frequency
  843. * but may not have completed it's sampling_down_factor.
  844. * For that case consider other cpu is loaded so that
  845. * frequency imbalance does not occur.
  846. */
  847. if ((j_dbs_info->cur_policy != NULL)
  848. && (j_dbs_info->cur_policy->cur ==
  849. j_dbs_info->cur_policy->max)) {
  850. if (policy->cur >= dbs_tuners_ins.optimal_freq)
  851. max_load_other_cpu =
  852. dbs_tuners_ins.up_threshold_any_cpu_load;
  853. }
  854. }
  855. /* calculate the scaled load across CPU */
  856. load_at_max_freq = (cur_load * policy->cur)/policy->cpuinfo.max_freq;
  857. cpufreq_notify_utilization(policy, load_at_max_freq);
  858. /* Check for frequency increase */
  859. if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
  860. #ifdef STEP_UP
  861. int target;
  862. int inc;
  863. if (policy->cur < dbs_tuners_ins.step_up_early_hispeed) {
  864. target = dbs_tuners_ins.step_up_early_hispeed;
  865. } else if (policy->cur < dbs_tuners_ins.step_up_interim_hispeed) {
  866. if(policy->cur == dbs_tuners_ins.step_up_early_hispeed) {
  867. if(this_dbs_info->freq_stay_count <
  868. dbs_tuners_ins.sampling_early_factor) {
  869. this_dbs_info->freq_stay_count++;
  870. return;
  871. }
  872. }
  873. this_dbs_info->freq_stay_count = 1;
  874. inc = (policy->max * dbs_tuners_ins.freq_step) / 100;
  875. target = min(dbs_tuners_ins.step_up_interim_hispeed,
  876. policy->cur + inc);
  877. } else {
  878. if(policy->cur == dbs_tuners_ins.step_up_interim_hispeed) {
  879. if(this_dbs_info->freq_stay_count <
  880. dbs_tuners_ins.sampling_interim_factor) {
  881. this_dbs_info->freq_stay_count++;
  882. return;
  883. }
  884. }
  885. this_dbs_info->freq_stay_count = 1;
  886. target = policy->max;
  887. //int inc = (policy->max * dbs_tuners_ins.freq_step) / 100;
  888. //target = min(policy->max, policy->cur + inc);
  889. }
  890. pr_debug("%s: cpu=%d, cur=%d, target=%d\n",
  891. __func__, policy->cpu, policy->cur, target);
  892. /* If switching to max speed, apply sampling_down_factor */
  893. if (target == policy->max)
  894. this_dbs_info->rate_mult =
  895. dbs_tuners_ins.sampling_down_factor;
  896. dbs_freq_increase(policy, target);
  897. #else
  898. /* If switching to max speed, apply sampling_down_factor */
  899. if (policy->cur < policy->max)
  900. this_dbs_info->rate_mult =
  901. dbs_tuners_ins.sampling_down_factor;
  902. dbs_freq_increase(policy, policy->max);
  903. #endif
  904. return;
  905. }
  906. if (num_online_cpus() > 1) {
  907. if (max_load_other_cpu >
  908. dbs_tuners_ins.up_threshold_any_cpu_load) {
  909. if (policy->cur < dbs_tuners_ins.sync_freq)
  910. dbs_freq_increase(policy,
  911. dbs_tuners_ins.sync_freq);
  912. return;
  913. }
  914. if (max_load_freq > dbs_tuners_ins.up_threshold_multi_core *
  915. policy->cur) {
  916. if (policy->cur < dbs_tuners_ins.optimal_freq)
  917. dbs_freq_increase(policy,
  918. dbs_tuners_ins.optimal_freq);
  919. return;
  920. }
  921. }
  922. /* Check for frequency decrease */
  923. /* if we cannot reduce the frequency anymore, break out early */
  924. if (policy->cur == policy->min)
  925. return;
  926. /*
  927. * The optimal frequency is the frequency that is the lowest that
  928. * can support the current CPU usage without triggering the up
  929. * policy. To be safe, we focus 10 points under the threshold.
  930. */
  931. if (max_load_freq <
  932. (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
  933. policy->cur) {
  934. unsigned int freq_next;
  935. freq_next = max_load_freq /
  936. (dbs_tuners_ins.up_threshold -
  937. dbs_tuners_ins.down_differential);
  938. /* No longer fully busy, reset rate_mult */
  939. this_dbs_info->rate_mult = 1;
  940. #ifdef STEP_UP
  941. this_dbs_info->freq_stay_count = 1;
  942. #endif
  943. if (freq_next < policy->min)
  944. freq_next = policy->min;
  945. if (num_online_cpus() > 1) {
  946. if (max_load_other_cpu >
  947. (dbs_tuners_ins.up_threshold_multi_core -
  948. dbs_tuners_ins.down_differential) &&
  949. freq_next < dbs_tuners_ins.sync_freq)
  950. freq_next = dbs_tuners_ins.sync_freq;
  951. if (max_load_freq >
  952. ((dbs_tuners_ins.up_threshold_multi_core -
  953. dbs_tuners_ins.down_differential_multi_core) *
  954. policy->cur) &&
  955. freq_next < dbs_tuners_ins.optimal_freq)
  956. freq_next = dbs_tuners_ins.optimal_freq;
  957. }
  958. if (!dbs_tuners_ins.powersave_bias) {
  959. __cpufreq_driver_target(policy, freq_next,
  960. CPUFREQ_RELATION_L);
  961. } else {
  962. int freq = powersave_bias_target(policy, freq_next,
  963. CPUFREQ_RELATION_L);
  964. __cpufreq_driver_target(policy, freq,
  965. CPUFREQ_RELATION_L);
  966. }
  967. }
  968. }
  969. static void do_dbs_timer(struct work_struct *work)
  970. {
  971. struct cpu_dbs_info_s *dbs_info =
  972. container_of(work, struct cpu_dbs_info_s, work.work);
  973. unsigned int cpu = dbs_info->cpu;
  974. int sample_type = dbs_info->sample_type;
  975. int delay;
  976. mutex_lock(&dbs_info->timer_mutex);
  977. /* Common NORMAL_SAMPLE setup */
  978. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  979. if (!dbs_tuners_ins.powersave_bias ||
  980. sample_type == DBS_NORMAL_SAMPLE) {
  981. dbs_check_cpu(dbs_info);
  982. if (dbs_info->freq_lo) {
  983. /* Setup timer for SUB_SAMPLE */
  984. dbs_info->sample_type = DBS_SUB_SAMPLE;
  985. delay = dbs_info->freq_hi_jiffies;
  986. } else {
  987. delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
  988. * dbs_info->rate_mult);
  989. }
  990. } else {
  991. __cpufreq_driver_target(dbs_info->cur_policy,
  992. dbs_info->freq_lo, CPUFREQ_RELATION_H);
  993. delay = dbs_info->freq_lo_jiffies;
  994. }
  995. queue_delayed_work_on(cpu, dbs_wq, &dbs_info->work, delay);
  996. mutex_unlock(&dbs_info->timer_mutex);
  997. }
  998. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
  999. {
  1000. /* We want all CPUs to do sampling nearly on same jiffy */
  1001. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  1002. if (num_online_cpus() > 1)
  1003. delay -= jiffies % delay;
  1004. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  1005. INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
  1006. queue_delayed_work_on(dbs_info->cpu, dbs_wq, &dbs_info->work, delay);
  1007. }
  1008. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  1009. {
  1010. cancel_delayed_work_sync(&dbs_info->work);
  1011. }
  1012. /*
  1013. * Not all CPUs want IO time to be accounted as busy; this dependson how
  1014. * efficient idling at a higher frequency/voltage is.
  1015. * Pavel Machek says this is not so for various generations of AMD and old
  1016. * Intel systems.
  1017. * Mike Chan (androidlcom) calis this is also not true for ARM.
  1018. * Because of this, whitelist specific known (series) of CPUs by default, and
  1019. * leave all others up to the user.
  1020. */
  1021. static int should_io_be_busy(void)
  1022. {
  1023. #if defined(CONFIG_X86)
  1024. /*
  1025. * For Intel, Core 2 (model 15) andl later have an efficient idle.
  1026. */
  1027. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1028. boot_cpu_data.x86 == 6 &&
  1029. boot_cpu_data.x86_model >= 15)
  1030. return 1;
  1031. #endif
  1032. return 0;
  1033. }
  1034. static void dbs_refresh_callback(struct work_struct *work)
  1035. {
  1036. struct cpufreq_policy *policy;
  1037. struct cpu_dbs_info_s *this_dbs_info;
  1038. struct dbs_work_struct *dbs_work;
  1039. unsigned int cpu;
  1040. unsigned int target_freq;
  1041. dbs_work = container_of(work, struct dbs_work_struct, work);
  1042. cpu = dbs_work->cpu;
  1043. get_online_cpus();
  1044. if (lock_policy_rwsem_write(cpu) < 0)
  1045. goto bail_acq_sema_failed;
  1046. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  1047. policy = this_dbs_info->cur_policy;
  1048. if (!policy) {
  1049. /* CPU not using ondemand governor */
  1050. goto bail_incorrect_governor;
  1051. }
  1052. if (dbs_tuners_ins.input_boost)
  1053. target_freq = dbs_tuners_ins.input_boost;
  1054. else
  1055. target_freq = policy->max;
  1056. if (policy->cur < target_freq) {
  1057. /*
  1058. * Arch specific cpufreq driver may fail.
  1059. * Don't update governor frequency upon failure.
  1060. */
  1061. if (__cpufreq_driver_target(policy, target_freq,
  1062. CPUFREQ_RELATION_L) >= 0)
  1063. policy->cur = target_freq;
  1064. this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
  1065. &this_dbs_info->prev_cpu_wall);
  1066. }
  1067. bail_incorrect_governor:
  1068. unlock_policy_rwsem_write(cpu);
  1069. bail_acq_sema_failed:
  1070. put_online_cpus();
  1071. return;
  1072. }
  1073. static int dbs_migration_notify(struct notifier_block *nb,
  1074. unsigned long target_cpu, void *arg)
  1075. {
  1076. struct cpu_dbs_info_s *target_dbs_info =
  1077. &per_cpu(od_cpu_dbs_info, target_cpu);
  1078. atomic_set(&target_dbs_info->src_sync_cpu, (int)arg);
  1079. /*
  1080. * Avoid issuing recursive wakeup call, as sync thread itself could be
  1081. * seen as migrating triggering this notification. Note that sync thread
  1082. * of a cpu could be running for a short while with its affinity broken
  1083. * because of CPU hotplug.
  1084. */
  1085. if (!atomic_cmpxchg(&target_dbs_info->being_woken, 0, 1)) {
  1086. wake_up(&target_dbs_info->sync_wq);
  1087. atomic_set(&target_dbs_info->being_woken, 0);
  1088. }
  1089. return NOTIFY_OK;
  1090. }
  1091. static struct notifier_block dbs_migration_nb = {
  1092. .notifier_call = dbs_migration_notify,
  1093. };
  1094. static int sync_pending(struct cpu_dbs_info_s *this_dbs_info)
  1095. {
  1096. return atomic_read(&this_dbs_info->src_sync_cpu) >= 0;
  1097. }
  1098. static int dbs_sync_thread(void *data)
  1099. {
  1100. int src_cpu, cpu = (int)data;
  1101. unsigned int src_freq, src_max_load;
  1102. struct cpu_dbs_info_s *this_dbs_info, *src_dbs_info;
  1103. struct cpufreq_policy *policy;
  1104. int delay;
  1105. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  1106. while (1) {
  1107. wait_event(this_dbs_info->sync_wq,
  1108. sync_pending(this_dbs_info) ||
  1109. kthread_should_stop());
  1110. if (kthread_should_stop())
  1111. break;
  1112. get_online_cpus();
  1113. src_cpu = atomic_read(&this_dbs_info->src_sync_cpu);
  1114. src_dbs_info = &per_cpu(od_cpu_dbs_info, src_cpu);
  1115. if (src_dbs_info != NULL &&
  1116. src_dbs_info->cur_policy != NULL) {
  1117. src_freq = src_dbs_info->cur_policy->cur;
  1118. src_max_load = src_dbs_info->max_load;
  1119. } else {
  1120. src_freq = dbs_tuners_ins.sync_freq;
  1121. src_max_load = 0;
  1122. }
  1123. if (lock_policy_rwsem_write(cpu) < 0)
  1124. goto bail_acq_sema_failed;
  1125. if (!atomic_read(&this_dbs_info->sync_enabled)) {
  1126. atomic_set(&this_dbs_info->src_sync_cpu, -1);
  1127. put_online_cpus();
  1128. unlock_policy_rwsem_write(cpu);
  1129. continue;
  1130. }
  1131. policy = this_dbs_info->cur_policy;
  1132. if (!policy) {
  1133. /* CPU not using ondemand governor */
  1134. goto bail_incorrect_governor;
  1135. }
  1136. delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  1137. if (policy->cur < src_freq) {
  1138. /* cancel the next ondemand sample */
  1139. cancel_delayed_work_sync(&this_dbs_info->work);
  1140. /*
  1141. * Arch specific cpufreq driver may fail.
  1142. * Don't update governor frequency upon failure.
  1143. */
  1144. if (__cpufreq_driver_target(policy, src_freq,
  1145. CPUFREQ_RELATION_L) >= 0) {
  1146. policy->cur = src_freq;
  1147. if (src_max_load > this_dbs_info->max_load) {
  1148. this_dbs_info->max_load = src_max_load;
  1149. this_dbs_info->prev_load = src_max_load;
  1150. }
  1151. }
  1152. /* reschedule the next ondemand sample */
  1153. mutex_lock(&this_dbs_info->timer_mutex);
  1154. queue_delayed_work_on(cpu, dbs_wq,
  1155. &this_dbs_info->work, delay);
  1156. mutex_unlock(&this_dbs_info->timer_mutex);
  1157. }
  1158. bail_incorrect_governor:
  1159. unlock_policy_rwsem_write(cpu);
  1160. bail_acq_sema_failed:
  1161. put_online_cpus();
  1162. atomic_set(&this_dbs_info->src_sync_cpu, -1);
  1163. }
  1164. return 0;
  1165. }
  1166. #ifndef CONFIG_SEC_DVFS
  1167. static void dbs_input_event(struct input_handle *handle, unsigned int type,
  1168. unsigned int code, int value)
  1169. {
  1170. int i;
  1171. if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
  1172. (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
  1173. /* nothing to do */
  1174. return;
  1175. }
  1176. for_each_online_cpu(i)
  1177. queue_work_on(i, dbs_wq, &per_cpu(dbs_refresh_work, i).work);
  1178. }
  1179. static int dbs_input_connect(struct input_handler *handler,
  1180. struct input_dev *dev, const struct input_device_id *id)
  1181. {
  1182. struct input_handle *handle;
  1183. int error;
  1184. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  1185. if (!handle)
  1186. return -ENOMEM;
  1187. handle->dev = dev;
  1188. handle->handler = handler;
  1189. handle->name = "cpufreq";
  1190. error = input_register_handle(handle);
  1191. if (error)
  1192. goto err2;
  1193. error = input_open_device(handle);
  1194. if (error)
  1195. goto err1;
  1196. return 0;
  1197. err1:
  1198. input_unregister_handle(handle);
  1199. err2:
  1200. kfree(handle);
  1201. return error;
  1202. }
  1203. static void dbs_input_disconnect(struct input_handle *handle)
  1204. {
  1205. input_close_device(handle);
  1206. input_unregister_handle(handle);
  1207. kfree(handle);
  1208. }
  1209. static const struct input_device_id dbs_ids[] = {
  1210. /* multi-touch touchscreen */
  1211. {
  1212. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  1213. INPUT_DEVICE_ID_MATCH_ABSBIT,
  1214. .evbit = { BIT_MASK(EV_ABS) },
  1215. .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
  1216. BIT_MASK(ABS_MT_POSITION_X) |
  1217. BIT_MASK(ABS_MT_POSITION_Y) },
  1218. },
  1219. /* touchpad */
  1220. {
  1221. .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
  1222. INPUT_DEVICE_ID_MATCH_ABSBIT,
  1223. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  1224. .absbit = { [BIT_WORD(ABS_X)] =
  1225. BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  1226. },
  1227. /* Keypad */
  1228. {
  1229. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1230. .evbit = { BIT_MASK(EV_KEY) },
  1231. },
  1232. { },
  1233. };
  1234. static struct input_handler dbs_input_handler = {
  1235. .event = dbs_input_event,
  1236. .connect = dbs_input_connect,
  1237. .disconnect = dbs_input_disconnect,
  1238. .name = "cpufreq_ond",
  1239. .id_table = dbs_ids,
  1240. };
  1241. #endif
  1242. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  1243. unsigned int event)
  1244. {
  1245. unsigned int cpu = policy->cpu;
  1246. struct cpu_dbs_info_s *this_dbs_info;
  1247. unsigned int j;
  1248. int rc;
  1249. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  1250. switch (event) {
  1251. case CPUFREQ_GOV_START:
  1252. if ((!cpu_online(cpu)) || (!policy->cur))
  1253. return -EINVAL;
  1254. mutex_lock(&dbs_mutex);
  1255. dbs_enable++;
  1256. for_each_cpu(j, policy->cpus) {
  1257. struct cpu_dbs_info_s *j_dbs_info;
  1258. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  1259. j_dbs_info->cur_policy = policy;
  1260. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  1261. &j_dbs_info->prev_cpu_wall);
  1262. if (dbs_tuners_ins.ignore_nice)
  1263. j_dbs_info->prev_cpu_nice =
  1264. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  1265. set_cpus_allowed(j_dbs_info->sync_thread,
  1266. *cpumask_of(j));
  1267. if (!dbs_tuners_ins.powersave_bias)
  1268. atomic_set(&j_dbs_info->sync_enabled, 1);
  1269. }
  1270. this_dbs_info->cpu = cpu;
  1271. this_dbs_info->rate_mult = 1;
  1272. #ifdef STEP_UP
  1273. this_dbs_info->freq_stay_count = 1;
  1274. #endif
  1275. ondemand_powersave_bias_init_cpu(cpu);
  1276. /*
  1277. * Start the timerschedule work, when this governor
  1278. * is used for first time
  1279. */
  1280. if (dbs_enable == 1) {
  1281. unsigned int latency;
  1282. rc = sysfs_create_group(cpufreq_global_kobject,
  1283. &dbs_attr_group);
  1284. if (rc) {
  1285. mutex_unlock(&dbs_mutex);
  1286. return rc;
  1287. }
  1288. /* policy latency is in nS. Convert it to uS first */
  1289. latency = policy->cpuinfo.transition_latency / 1000;
  1290. if (latency == 0)
  1291. latency = 1;
  1292. /* Bring kernel and HW constraints together */
  1293. min_sampling_rate = max(min_sampling_rate,
  1294. MIN_LATENCY_MULTIPLIER * latency);
  1295. dbs_tuners_ins.sampling_rate =
  1296. max(min_sampling_rate,
  1297. latency * LATENCY_MULTIPLIER);
  1298. dbs_tuners_ins.io_is_busy = should_io_be_busy();
  1299. if (dbs_tuners_ins.optimal_freq == 0)
  1300. dbs_tuners_ins.optimal_freq = policy->min;
  1301. if (dbs_tuners_ins.sync_freq == 0)
  1302. dbs_tuners_ins.sync_freq = policy->min;
  1303. atomic_notifier_chain_register(&migration_notifier_head,
  1304. &dbs_migration_nb);
  1305. }
  1306. #ifndef CONFIG_SEC_DVFS
  1307. if (!cpu)
  1308. rc = input_register_handler(&dbs_input_handler);
  1309. #endif
  1310. mutex_unlock(&dbs_mutex);
  1311. if (!ondemand_powersave_bias_setspeed(
  1312. this_dbs_info->cur_policy,
  1313. NULL,
  1314. dbs_tuners_ins.powersave_bias))
  1315. dbs_timer_init(this_dbs_info);
  1316. break;
  1317. case CPUFREQ_GOV_STOP:
  1318. mutex_lock(&dbs_mutex);
  1319. dbs_timer_exit(this_dbs_info);
  1320. dbs_enable--;
  1321. for_each_cpu(j, policy->cpus) {
  1322. struct cpu_dbs_info_s *j_dbs_info;
  1323. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  1324. atomic_set(&j_dbs_info->sync_enabled, 0);
  1325. }
  1326. /* If device is being removed, policy is no longer
  1327. * valid. */
  1328. this_dbs_info->cur_policy = NULL;
  1329. #ifndef CONFIG_SEC_DVFS
  1330. if (!cpu)
  1331. input_unregister_handler(&dbs_input_handler);
  1332. #endif
  1333. if (!dbs_enable) {
  1334. sysfs_remove_group(cpufreq_global_kobject,
  1335. &dbs_attr_group);
  1336. atomic_notifier_chain_unregister(
  1337. &migration_notifier_head,
  1338. &dbs_migration_nb);
  1339. }
  1340. mutex_unlock(&dbs_mutex);
  1341. break;
  1342. case CPUFREQ_GOV_LIMITS:
  1343. mutex_lock(&this_dbs_info->timer_mutex);
  1344. if (this_dbs_info->cur_policy == NULL) {
  1345. pr_debug("Unable to limit cpu freq due to cur_policy == NULL\n");
  1346. mutex_unlock(&this_dbs_info->timer_mutex);
  1347. return -EPERM;
  1348. }
  1349. if (policy->max < this_dbs_info->cur_policy->cur)
  1350. __cpufreq_driver_target(this_dbs_info->cur_policy,
  1351. policy->max, CPUFREQ_RELATION_H);
  1352. else if (policy->min > this_dbs_info->cur_policy->cur)
  1353. __cpufreq_driver_target(this_dbs_info->cur_policy,
  1354. policy->min, CPUFREQ_RELATION_L);
  1355. else if (dbs_tuners_ins.powersave_bias != 0)
  1356. ondemand_powersave_bias_setspeed(
  1357. this_dbs_info->cur_policy,
  1358. policy,
  1359. dbs_tuners_ins.powersave_bias);
  1360. mutex_unlock(&this_dbs_info->timer_mutex);
  1361. break;
  1362. }
  1363. return 0;
  1364. }
  1365. static int __init cpufreq_gov_dbs_init(void)
  1366. {
  1367. u64 idle_time;
  1368. unsigned int i;
  1369. int cpu = get_cpu();
  1370. idle_time = get_cpu_idle_time_us(cpu, NULL);
  1371. put_cpu();
  1372. if (idle_time != -1ULL) {
  1373. /* Idle micro accounting is supported. Use finer thresholds */
  1374. dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  1375. dbs_tuners_ins.down_differential =
  1376. MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  1377. /*
  1378. * In nohz/micro accounting case we set the minimum frequency
  1379. * not depending on HZ, but fixed (very low). The deferred
  1380. * timer might skip some samples if idle/sleeping as needed.
  1381. */
  1382. min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  1383. } else {
  1384. /* For correct statistics, we need 10 ticks for each measure */
  1385. min_sampling_rate =
  1386. MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
  1387. }
  1388. dbs_wq = alloc_workqueue("ondemand_dbs_wq", WQ_HIGHPRI, 0);
  1389. if (!dbs_wq) {
  1390. printk(KERN_ERR "Failed to create ondemand_dbs_wq workqueue\n");
  1391. return -EFAULT;
  1392. }
  1393. for_each_possible_cpu(i) {
  1394. struct cpu_dbs_info_s *this_dbs_info =
  1395. &per_cpu(od_cpu_dbs_info, i);
  1396. struct dbs_work_struct *dbs_work =
  1397. &per_cpu(dbs_refresh_work, i);
  1398. mutex_init(&this_dbs_info->timer_mutex);
  1399. INIT_WORK(&dbs_work->work, dbs_refresh_callback);
  1400. dbs_work->cpu = i;
  1401. atomic_set(&this_dbs_info->src_sync_cpu, -1);
  1402. atomic_set(&this_dbs_info->being_woken, 0);
  1403. init_waitqueue_head(&this_dbs_info->sync_wq);
  1404. this_dbs_info->sync_thread = kthread_run(dbs_sync_thread,
  1405. (void *)i,
  1406. "dbs_sync/%d", i);
  1407. }
  1408. return cpufreq_register_governor(&cpufreq_gov_ondemand);
  1409. }
  1410. static void __exit cpufreq_gov_dbs_exit(void)
  1411. {
  1412. unsigned int i;
  1413. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  1414. for_each_possible_cpu(i) {
  1415. struct cpu_dbs_info_s *this_dbs_info =
  1416. &per_cpu(od_cpu_dbs_info, i);
  1417. mutex_destroy(&this_dbs_info->timer_mutex);
  1418. kthread_stop(this_dbs_info->sync_thread);
  1419. }
  1420. destroy_workqueue(dbs_wq);
  1421. }
  1422. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  1423. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  1424. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  1425. "Low Latency Frequency Transition capable processors");
  1426. MODULE_LICENSE("GPL");
  1427. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  1428. fs_initcall(cpufreq_gov_dbs_init);
  1429. #else
  1430. module_init(cpufreq_gov_dbs_init);
  1431. #endif
  1432. module_exit(cpufreq_gov_dbs_exit);