cpufreq_ondemand.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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. /*
  766. * Every sampling_rate, we check, if current idle time is less
  767. * than 20% (default), then we try to increase frequency
  768. * Every sampling_rate, we look for a the lowest
  769. * frequency which can sustain the load while keeping idle time over
  770. * 30%. If such a frequency exist, we try to decrease to this frequency.
  771. *
  772. * Any frequency increase takes it to the maximum frequency.
  773. * Frequency reduction happens at minimum steps of
  774. * 5% (default) of current frequency
  775. */
  776. /* Get Absolute Load - in terms of freq */
  777. max_load_freq = 0;
  778. for_each_cpu(j, policy->cpus) {
  779. struct cpu_dbs_info_s *j_dbs_info;
  780. cputime64_t cur_wall_time, cur_idle_time, cur_iowait_time;
  781. unsigned int idle_time, wall_time, iowait_time;
  782. unsigned int load_freq;
  783. int freq_avg;
  784. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  785. cur_idle_time = get_cpu_idle_time(j, &cur_wall_time);
  786. cur_iowait_time = get_cpu_iowait_time(j, &cur_wall_time);
  787. wall_time = (unsigned int)
  788. (cur_wall_time - j_dbs_info->prev_cpu_wall);
  789. j_dbs_info->prev_cpu_wall = cur_wall_time;
  790. idle_time = (unsigned int)
  791. (cur_idle_time - j_dbs_info->prev_cpu_idle);
  792. j_dbs_info->prev_cpu_idle = cur_idle_time;
  793. iowait_time = (unsigned int)
  794. (cur_iowait_time - j_dbs_info->prev_cpu_iowait);
  795. j_dbs_info->prev_cpu_iowait = cur_iowait_time;
  796. if (dbs_tuners_ins.ignore_nice) {
  797. u64 cur_nice;
  798. unsigned long cur_nice_jiffies;
  799. cur_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE] -
  800. j_dbs_info->prev_cpu_nice;
  801. /*
  802. * Assumption: nice time between sampling periods will
  803. * be less than 2^32 jiffies for 32 bit sys
  804. */
  805. cur_nice_jiffies = (unsigned long)
  806. cputime64_to_jiffies64(cur_nice);
  807. j_dbs_info->prev_cpu_nice = kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  808. idle_time += jiffies_to_usecs(cur_nice_jiffies);
  809. }
  810. /*
  811. * For the purpose of ondemand, waiting for disk IO is an
  812. * indication that you're performance critical, and not that
  813. * the system is actually idle. So subtract the iowait time
  814. * from the cpu idle time.
  815. */
  816. if (dbs_tuners_ins.io_is_busy && idle_time >= iowait_time)
  817. idle_time -= iowait_time;
  818. if (unlikely(!wall_time || wall_time < idle_time))
  819. continue;
  820. cur_load = 100 * (wall_time - idle_time) / wall_time;
  821. j_dbs_info->max_load = max(cur_load, j_dbs_info->prev_load);
  822. j_dbs_info->prev_load = cur_load;
  823. freq_avg = __cpufreq_driver_getavg(policy, j);
  824. if (freq_avg <= 0)
  825. freq_avg = policy->cur;
  826. load_freq = cur_load * freq_avg;
  827. if (load_freq > max_load_freq)
  828. max_load_freq = load_freq;
  829. }
  830. for_each_online_cpu(j) {
  831. struct cpu_dbs_info_s *j_dbs_info;
  832. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  833. if (j == policy->cpu)
  834. continue;
  835. if (max_load_other_cpu < j_dbs_info->max_load)
  836. max_load_other_cpu = j_dbs_info->max_load;
  837. /*
  838. * The other cpu could be running at higher frequency
  839. * but may not have completed it's sampling_down_factor.
  840. * For that case consider other cpu is loaded so that
  841. * frequency imbalance does not occur.
  842. */
  843. if ((j_dbs_info->cur_policy != NULL)
  844. && (j_dbs_info->cur_policy->cur ==
  845. j_dbs_info->cur_policy->max)) {
  846. if (policy->cur >= dbs_tuners_ins.optimal_freq)
  847. max_load_other_cpu =
  848. dbs_tuners_ins.up_threshold_any_cpu_load;
  849. }
  850. }
  851. /* calculate the scaled load across CPU */
  852. load_at_max_freq = (cur_load * policy->cur)/policy->cpuinfo.max_freq;
  853. cpufreq_notify_utilization(policy, load_at_max_freq);
  854. /* Check for frequency increase */
  855. if (max_load_freq > dbs_tuners_ins.up_threshold * policy->cur) {
  856. #ifdef STEP_UP
  857. int target;
  858. int inc;
  859. if (policy->cur < dbs_tuners_ins.step_up_early_hispeed) {
  860. target = dbs_tuners_ins.step_up_early_hispeed;
  861. } else if (policy->cur < dbs_tuners_ins.step_up_interim_hispeed) {
  862. if(policy->cur == dbs_tuners_ins.step_up_early_hispeed) {
  863. if(this_dbs_info->freq_stay_count <
  864. dbs_tuners_ins.sampling_early_factor) {
  865. this_dbs_info->freq_stay_count++;
  866. return;
  867. }
  868. }
  869. this_dbs_info->freq_stay_count = 1;
  870. inc = (policy->max * dbs_tuners_ins.freq_step) / 100;
  871. target = min(dbs_tuners_ins.step_up_interim_hispeed,
  872. policy->cur + inc);
  873. } else {
  874. if(policy->cur == dbs_tuners_ins.step_up_interim_hispeed) {
  875. if(this_dbs_info->freq_stay_count <
  876. dbs_tuners_ins.sampling_interim_factor) {
  877. this_dbs_info->freq_stay_count++;
  878. return;
  879. }
  880. }
  881. this_dbs_info->freq_stay_count = 1;
  882. target = policy->max;
  883. //int inc = (policy->max * dbs_tuners_ins.freq_step) / 100;
  884. //target = min(policy->max, policy->cur + inc);
  885. }
  886. pr_debug("%s: cpu=%d, cur=%d, target=%d\n",
  887. __func__, policy->cpu, policy->cur, target);
  888. /* If switching to max speed, apply sampling_down_factor */
  889. if (target == policy->max)
  890. this_dbs_info->rate_mult =
  891. dbs_tuners_ins.sampling_down_factor;
  892. dbs_freq_increase(policy, target);
  893. #else
  894. /* If switching to max speed, apply sampling_down_factor */
  895. if (policy->cur < policy->max)
  896. this_dbs_info->rate_mult =
  897. dbs_tuners_ins.sampling_down_factor;
  898. dbs_freq_increase(policy, policy->max);
  899. #endif
  900. return;
  901. }
  902. if (num_online_cpus() > 1) {
  903. if (max_load_other_cpu >
  904. dbs_tuners_ins.up_threshold_any_cpu_load) {
  905. if (policy->cur < dbs_tuners_ins.sync_freq)
  906. dbs_freq_increase(policy,
  907. dbs_tuners_ins.sync_freq);
  908. return;
  909. }
  910. if (max_load_freq > dbs_tuners_ins.up_threshold_multi_core *
  911. policy->cur) {
  912. if (policy->cur < dbs_tuners_ins.optimal_freq)
  913. dbs_freq_increase(policy,
  914. dbs_tuners_ins.optimal_freq);
  915. return;
  916. }
  917. }
  918. /* Check for frequency decrease */
  919. /* if we cannot reduce the frequency anymore, break out early */
  920. if (policy->cur == policy->min)
  921. return;
  922. /*
  923. * The optimal frequency is the frequency that is the lowest that
  924. * can support the current CPU usage without triggering the up
  925. * policy. To be safe, we focus 10 points under the threshold.
  926. */
  927. if (max_load_freq <
  928. (dbs_tuners_ins.up_threshold - dbs_tuners_ins.down_differential) *
  929. policy->cur) {
  930. unsigned int freq_next;
  931. freq_next = max_load_freq /
  932. (dbs_tuners_ins.up_threshold -
  933. dbs_tuners_ins.down_differential);
  934. /* No longer fully busy, reset rate_mult */
  935. this_dbs_info->rate_mult = 1;
  936. #ifdef STEP_UP
  937. this_dbs_info->freq_stay_count = 1;
  938. #endif
  939. if (freq_next < policy->min)
  940. freq_next = policy->min;
  941. if (num_online_cpus() > 1) {
  942. if (max_load_other_cpu >
  943. (dbs_tuners_ins.up_threshold_multi_core -
  944. dbs_tuners_ins.down_differential) &&
  945. freq_next < dbs_tuners_ins.sync_freq)
  946. freq_next = dbs_tuners_ins.sync_freq;
  947. if (max_load_freq >
  948. ((dbs_tuners_ins.up_threshold_multi_core -
  949. dbs_tuners_ins.down_differential_multi_core) *
  950. policy->cur) &&
  951. freq_next < dbs_tuners_ins.optimal_freq)
  952. freq_next = dbs_tuners_ins.optimal_freq;
  953. }
  954. if (!dbs_tuners_ins.powersave_bias) {
  955. __cpufreq_driver_target(policy, freq_next,
  956. CPUFREQ_RELATION_L);
  957. } else {
  958. int freq = powersave_bias_target(policy, freq_next,
  959. CPUFREQ_RELATION_L);
  960. __cpufreq_driver_target(policy, freq,
  961. CPUFREQ_RELATION_L);
  962. }
  963. }
  964. }
  965. static void do_dbs_timer(struct work_struct *work)
  966. {
  967. struct cpu_dbs_info_s *dbs_info =
  968. container_of(work, struct cpu_dbs_info_s, work.work);
  969. unsigned int cpu = dbs_info->cpu;
  970. int sample_type = dbs_info->sample_type;
  971. int delay;
  972. mutex_lock(&dbs_info->timer_mutex);
  973. /* Common NORMAL_SAMPLE setup */
  974. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  975. if (!dbs_tuners_ins.powersave_bias ||
  976. sample_type == DBS_NORMAL_SAMPLE) {
  977. dbs_check_cpu(dbs_info);
  978. if (dbs_info->freq_lo) {
  979. /* Setup timer for SUB_SAMPLE */
  980. dbs_info->sample_type = DBS_SUB_SAMPLE;
  981. delay = dbs_info->freq_hi_jiffies;
  982. } else {
  983. /* We want all CPUs to do sampling nearly on
  984. * same jiffy
  985. */
  986. delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate
  987. * dbs_info->rate_mult);
  988. if (num_online_cpus() > 1)
  989. delay -= jiffies % delay;
  990. }
  991. } else {
  992. __cpufreq_driver_target(dbs_info->cur_policy,
  993. dbs_info->freq_lo, CPUFREQ_RELATION_H);
  994. delay = dbs_info->freq_lo_jiffies;
  995. }
  996. queue_delayed_work_on(cpu, dbs_wq, &dbs_info->work, delay);
  997. mutex_unlock(&dbs_info->timer_mutex);
  998. }
  999. static inline void dbs_timer_init(struct cpu_dbs_info_s *dbs_info)
  1000. {
  1001. /* We want all CPUs to do sampling nearly on same jiffy */
  1002. int delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  1003. if (num_online_cpus() > 1)
  1004. delay -= jiffies % delay;
  1005. dbs_info->sample_type = DBS_NORMAL_SAMPLE;
  1006. INIT_DELAYED_WORK_DEFERRABLE(&dbs_info->work, do_dbs_timer);
  1007. queue_delayed_work_on(dbs_info->cpu, dbs_wq, &dbs_info->work, delay);
  1008. }
  1009. static inline void dbs_timer_exit(struct cpu_dbs_info_s *dbs_info)
  1010. {
  1011. cancel_delayed_work_sync(&dbs_info->work);
  1012. }
  1013. /*
  1014. * Not all CPUs want IO time to be accounted as busy; this dependson how
  1015. * efficient idling at a higher frequency/voltage is.
  1016. * Pavel Machek says this is not so for various generations of AMD and old
  1017. * Intel systems.
  1018. * Mike Chan (androidlcom) calis this is also not true for ARM.
  1019. * Because of this, whitelist specific known (series) of CPUs by default, and
  1020. * leave all others up to the user.
  1021. */
  1022. static int should_io_be_busy(void)
  1023. {
  1024. #if defined(CONFIG_X86)
  1025. /*
  1026. * For Intel, Core 2 (model 15) andl later have an efficient idle.
  1027. */
  1028. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  1029. boot_cpu_data.x86 == 6 &&
  1030. boot_cpu_data.x86_model >= 15)
  1031. return 1;
  1032. #endif
  1033. return 0;
  1034. }
  1035. static void dbs_refresh_callback(struct work_struct *work)
  1036. {
  1037. struct cpufreq_policy *policy;
  1038. struct cpu_dbs_info_s *this_dbs_info;
  1039. struct dbs_work_struct *dbs_work;
  1040. unsigned int cpu;
  1041. unsigned int target_freq;
  1042. dbs_work = container_of(work, struct dbs_work_struct, work);
  1043. cpu = dbs_work->cpu;
  1044. get_online_cpus();
  1045. if (lock_policy_rwsem_write(cpu) < 0)
  1046. goto bail_acq_sema_failed;
  1047. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  1048. policy = this_dbs_info->cur_policy;
  1049. if (!policy) {
  1050. /* CPU not using ondemand governor */
  1051. goto bail_incorrect_governor;
  1052. }
  1053. if (dbs_tuners_ins.input_boost)
  1054. target_freq = dbs_tuners_ins.input_boost;
  1055. else
  1056. target_freq = policy->max;
  1057. if (policy->cur < target_freq) {
  1058. /*
  1059. * Arch specific cpufreq driver may fail.
  1060. * Don't update governor frequency upon failure.
  1061. */
  1062. if (__cpufreq_driver_target(policy, target_freq,
  1063. CPUFREQ_RELATION_L) >= 0)
  1064. policy->cur = target_freq;
  1065. this_dbs_info->prev_cpu_idle = get_cpu_idle_time(cpu,
  1066. &this_dbs_info->prev_cpu_wall);
  1067. }
  1068. bail_incorrect_governor:
  1069. unlock_policy_rwsem_write(cpu);
  1070. bail_acq_sema_failed:
  1071. put_online_cpus();
  1072. return;
  1073. }
  1074. static int dbs_migration_notify(struct notifier_block *nb,
  1075. unsigned long target_cpu, void *arg)
  1076. {
  1077. struct cpu_dbs_info_s *target_dbs_info =
  1078. &per_cpu(od_cpu_dbs_info, target_cpu);
  1079. atomic_set(&target_dbs_info->src_sync_cpu, (int)arg);
  1080. /*
  1081. * Avoid issuing recursive wakeup call, as sync thread itself could be
  1082. * seen as migrating triggering this notification. Note that sync thread
  1083. * of a cpu could be running for a short while with its affinity broken
  1084. * because of CPU hotplug.
  1085. */
  1086. if (!atomic_cmpxchg(&target_dbs_info->being_woken, 0, 1)) {
  1087. wake_up(&target_dbs_info->sync_wq);
  1088. atomic_set(&target_dbs_info->being_woken, 0);
  1089. }
  1090. return NOTIFY_OK;
  1091. }
  1092. static struct notifier_block dbs_migration_nb = {
  1093. .notifier_call = dbs_migration_notify,
  1094. };
  1095. static int sync_pending(struct cpu_dbs_info_s *this_dbs_info)
  1096. {
  1097. return atomic_read(&this_dbs_info->src_sync_cpu) >= 0;
  1098. }
  1099. static int dbs_sync_thread(void *data)
  1100. {
  1101. int src_cpu, cpu = (int)data;
  1102. unsigned int src_freq, src_max_load;
  1103. struct cpu_dbs_info_s *this_dbs_info, *src_dbs_info;
  1104. struct cpufreq_policy *policy;
  1105. int delay;
  1106. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  1107. while (1) {
  1108. wait_event(this_dbs_info->sync_wq,
  1109. sync_pending(this_dbs_info) ||
  1110. kthread_should_stop());
  1111. if (kthread_should_stop())
  1112. break;
  1113. get_online_cpus();
  1114. src_cpu = atomic_read(&this_dbs_info->src_sync_cpu);
  1115. src_dbs_info = &per_cpu(od_cpu_dbs_info, src_cpu);
  1116. if (src_dbs_info != NULL &&
  1117. src_dbs_info->cur_policy != NULL) {
  1118. src_freq = src_dbs_info->cur_policy->cur;
  1119. src_max_load = src_dbs_info->max_load;
  1120. } else {
  1121. src_freq = dbs_tuners_ins.sync_freq;
  1122. src_max_load = 0;
  1123. }
  1124. if (lock_policy_rwsem_write(cpu) < 0)
  1125. goto bail_acq_sema_failed;
  1126. if (!atomic_read(&this_dbs_info->sync_enabled)) {
  1127. atomic_set(&this_dbs_info->src_sync_cpu, -1);
  1128. put_online_cpus();
  1129. unlock_policy_rwsem_write(cpu);
  1130. continue;
  1131. }
  1132. policy = this_dbs_info->cur_policy;
  1133. if (!policy) {
  1134. /* CPU not using ondemand governor */
  1135. goto bail_incorrect_governor;
  1136. }
  1137. delay = usecs_to_jiffies(dbs_tuners_ins.sampling_rate);
  1138. if (policy->cur < src_freq) {
  1139. /* cancel the next ondemand sample */
  1140. cancel_delayed_work_sync(&this_dbs_info->work);
  1141. /*
  1142. * Arch specific cpufreq driver may fail.
  1143. * Don't update governor frequency upon failure.
  1144. */
  1145. if (__cpufreq_driver_target(policy, src_freq,
  1146. CPUFREQ_RELATION_L) >= 0) {
  1147. policy->cur = src_freq;
  1148. if (src_max_load > this_dbs_info->max_load) {
  1149. this_dbs_info->max_load = src_max_load;
  1150. this_dbs_info->prev_load = src_max_load;
  1151. }
  1152. }
  1153. /* reschedule the next ondemand sample */
  1154. mutex_lock(&this_dbs_info->timer_mutex);
  1155. queue_delayed_work_on(cpu, dbs_wq,
  1156. &this_dbs_info->work, delay);
  1157. mutex_unlock(&this_dbs_info->timer_mutex);
  1158. }
  1159. bail_incorrect_governor:
  1160. unlock_policy_rwsem_write(cpu);
  1161. bail_acq_sema_failed:
  1162. put_online_cpus();
  1163. atomic_set(&this_dbs_info->src_sync_cpu, -1);
  1164. }
  1165. return 0;
  1166. }
  1167. #ifndef CONFIG_SEC_DVFS
  1168. static void dbs_input_event(struct input_handle *handle, unsigned int type,
  1169. unsigned int code, int value)
  1170. {
  1171. int i;
  1172. if ((dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MAXLEVEL) ||
  1173. (dbs_tuners_ins.powersave_bias == POWERSAVE_BIAS_MINLEVEL)) {
  1174. /* nothing to do */
  1175. return;
  1176. }
  1177. for_each_online_cpu(i)
  1178. queue_work_on(i, dbs_wq, &per_cpu(dbs_refresh_work, i).work);
  1179. }
  1180. static int dbs_input_connect(struct input_handler *handler,
  1181. struct input_dev *dev, const struct input_device_id *id)
  1182. {
  1183. struct input_handle *handle;
  1184. int error;
  1185. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  1186. if (!handle)
  1187. return -ENOMEM;
  1188. handle->dev = dev;
  1189. handle->handler = handler;
  1190. handle->name = "cpufreq";
  1191. error = input_register_handle(handle);
  1192. if (error)
  1193. goto err2;
  1194. error = input_open_device(handle);
  1195. if (error)
  1196. goto err1;
  1197. return 0;
  1198. err1:
  1199. input_unregister_handle(handle);
  1200. err2:
  1201. kfree(handle);
  1202. return error;
  1203. }
  1204. static void dbs_input_disconnect(struct input_handle *handle)
  1205. {
  1206. input_close_device(handle);
  1207. input_unregister_handle(handle);
  1208. kfree(handle);
  1209. }
  1210. static const struct input_device_id dbs_ids[] = {
  1211. /* multi-touch touchscreen */
  1212. {
  1213. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  1214. INPUT_DEVICE_ID_MATCH_ABSBIT,
  1215. .evbit = { BIT_MASK(EV_ABS) },
  1216. .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
  1217. BIT_MASK(ABS_MT_POSITION_X) |
  1218. BIT_MASK(ABS_MT_POSITION_Y) },
  1219. },
  1220. /* touchpad */
  1221. {
  1222. .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
  1223. INPUT_DEVICE_ID_MATCH_ABSBIT,
  1224. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  1225. .absbit = { [BIT_WORD(ABS_X)] =
  1226. BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  1227. },
  1228. /* Keypad */
  1229. {
  1230. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  1231. .evbit = { BIT_MASK(EV_KEY) },
  1232. },
  1233. { },
  1234. };
  1235. static struct input_handler dbs_input_handler = {
  1236. .event = dbs_input_event,
  1237. .connect = dbs_input_connect,
  1238. .disconnect = dbs_input_disconnect,
  1239. .name = "cpufreq_ond",
  1240. .id_table = dbs_ids,
  1241. };
  1242. #endif
  1243. static int cpufreq_governor_dbs(struct cpufreq_policy *policy,
  1244. unsigned int event)
  1245. {
  1246. unsigned int cpu = policy->cpu;
  1247. struct cpu_dbs_info_s *this_dbs_info;
  1248. unsigned int j;
  1249. int rc;
  1250. this_dbs_info = &per_cpu(od_cpu_dbs_info, cpu);
  1251. switch (event) {
  1252. case CPUFREQ_GOV_START:
  1253. if ((!cpu_online(cpu)) || (!policy->cur))
  1254. return -EINVAL;
  1255. mutex_lock(&dbs_mutex);
  1256. dbs_enable++;
  1257. for_each_cpu(j, policy->cpus) {
  1258. struct cpu_dbs_info_s *j_dbs_info;
  1259. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  1260. j_dbs_info->cur_policy = policy;
  1261. j_dbs_info->prev_cpu_idle = get_cpu_idle_time(j,
  1262. &j_dbs_info->prev_cpu_wall);
  1263. if (dbs_tuners_ins.ignore_nice)
  1264. j_dbs_info->prev_cpu_nice =
  1265. kcpustat_cpu(j).cpustat[CPUTIME_NICE];
  1266. set_cpus_allowed(j_dbs_info->sync_thread,
  1267. *cpumask_of(j));
  1268. if (!dbs_tuners_ins.powersave_bias)
  1269. atomic_set(&j_dbs_info->sync_enabled, 1);
  1270. }
  1271. this_dbs_info->cpu = cpu;
  1272. this_dbs_info->rate_mult = 1;
  1273. #ifdef STEP_UP
  1274. this_dbs_info->freq_stay_count = 1;
  1275. #endif
  1276. ondemand_powersave_bias_init_cpu(cpu);
  1277. /*
  1278. * Start the timerschedule work, when this governor
  1279. * is used for first time
  1280. */
  1281. if (dbs_enable == 1) {
  1282. unsigned int latency;
  1283. rc = sysfs_create_group(cpufreq_global_kobject,
  1284. &dbs_attr_group);
  1285. if (rc) {
  1286. mutex_unlock(&dbs_mutex);
  1287. return rc;
  1288. }
  1289. /* policy latency is in nS. Convert it to uS first */
  1290. latency = policy->cpuinfo.transition_latency / 1000;
  1291. if (latency == 0)
  1292. latency = 1;
  1293. /* Bring kernel and HW constraints together */
  1294. min_sampling_rate = max(min_sampling_rate,
  1295. MIN_LATENCY_MULTIPLIER * latency);
  1296. dbs_tuners_ins.sampling_rate =
  1297. max(min_sampling_rate,
  1298. latency * LATENCY_MULTIPLIER);
  1299. dbs_tuners_ins.io_is_busy = should_io_be_busy();
  1300. if (dbs_tuners_ins.optimal_freq == 0)
  1301. dbs_tuners_ins.optimal_freq = policy->min;
  1302. if (dbs_tuners_ins.sync_freq == 0)
  1303. dbs_tuners_ins.sync_freq = policy->min;
  1304. atomic_notifier_chain_register(&migration_notifier_head,
  1305. &dbs_migration_nb);
  1306. }
  1307. #ifndef CONFIG_SEC_DVFS
  1308. if (!cpu)
  1309. rc = input_register_handler(&dbs_input_handler);
  1310. #endif
  1311. mutex_unlock(&dbs_mutex);
  1312. if (!ondemand_powersave_bias_setspeed(
  1313. this_dbs_info->cur_policy,
  1314. NULL,
  1315. dbs_tuners_ins.powersave_bias))
  1316. dbs_timer_init(this_dbs_info);
  1317. break;
  1318. case CPUFREQ_GOV_STOP:
  1319. mutex_lock(&dbs_mutex);
  1320. dbs_timer_exit(this_dbs_info);
  1321. dbs_enable--;
  1322. for_each_cpu(j, policy->cpus) {
  1323. struct cpu_dbs_info_s *j_dbs_info;
  1324. j_dbs_info = &per_cpu(od_cpu_dbs_info, j);
  1325. atomic_set(&j_dbs_info->sync_enabled, 0);
  1326. }
  1327. /* If device is being removed, policy is no longer
  1328. * valid. */
  1329. this_dbs_info->cur_policy = NULL;
  1330. #ifndef CONFIG_SEC_DVFS
  1331. if (!cpu)
  1332. input_unregister_handler(&dbs_input_handler);
  1333. #endif
  1334. if (!dbs_enable) {
  1335. sysfs_remove_group(cpufreq_global_kobject,
  1336. &dbs_attr_group);
  1337. atomic_notifier_chain_unregister(
  1338. &migration_notifier_head,
  1339. &dbs_migration_nb);
  1340. }
  1341. mutex_unlock(&dbs_mutex);
  1342. break;
  1343. case CPUFREQ_GOV_LIMITS:
  1344. mutex_lock(&this_dbs_info->timer_mutex);
  1345. if (this_dbs_info->cur_policy == NULL) {
  1346. pr_debug("Unable to limit cpu freq due to cur_policy == NULL\n");
  1347. mutex_unlock(&this_dbs_info->timer_mutex);
  1348. return -EPERM;
  1349. }
  1350. if (policy->max < this_dbs_info->cur_policy->cur)
  1351. __cpufreq_driver_target(this_dbs_info->cur_policy,
  1352. policy->max, CPUFREQ_RELATION_H);
  1353. else if (policy->min > this_dbs_info->cur_policy->cur)
  1354. __cpufreq_driver_target(this_dbs_info->cur_policy,
  1355. policy->min, CPUFREQ_RELATION_L);
  1356. else if (dbs_tuners_ins.powersave_bias != 0)
  1357. ondemand_powersave_bias_setspeed(
  1358. this_dbs_info->cur_policy,
  1359. policy,
  1360. dbs_tuners_ins.powersave_bias);
  1361. mutex_unlock(&this_dbs_info->timer_mutex);
  1362. break;
  1363. }
  1364. return 0;
  1365. }
  1366. static int __init cpufreq_gov_dbs_init(void)
  1367. {
  1368. u64 idle_time;
  1369. unsigned int i;
  1370. int cpu = get_cpu();
  1371. idle_time = get_cpu_idle_time_us(cpu, NULL);
  1372. put_cpu();
  1373. if (idle_time != -1ULL) {
  1374. /* Idle micro accounting is supported. Use finer thresholds */
  1375. dbs_tuners_ins.up_threshold = MICRO_FREQUENCY_UP_THRESHOLD;
  1376. dbs_tuners_ins.down_differential =
  1377. MICRO_FREQUENCY_DOWN_DIFFERENTIAL;
  1378. /*
  1379. * In nohz/micro accounting case we set the minimum frequency
  1380. * not depending on HZ, but fixed (very low). The deferred
  1381. * timer might skip some samples if idle/sleeping as needed.
  1382. */
  1383. min_sampling_rate = MICRO_FREQUENCY_MIN_SAMPLE_RATE;
  1384. } else {
  1385. /* For correct statistics, we need 10 ticks for each measure */
  1386. min_sampling_rate =
  1387. MIN_SAMPLING_RATE_RATIO * jiffies_to_usecs(10);
  1388. }
  1389. dbs_wq = alloc_workqueue("ondemand_dbs_wq", WQ_HIGHPRI, 0);
  1390. if (!dbs_wq) {
  1391. printk(KERN_ERR "Failed to create ondemand_dbs_wq workqueue\n");
  1392. return -EFAULT;
  1393. }
  1394. for_each_possible_cpu(i) {
  1395. struct cpu_dbs_info_s *this_dbs_info =
  1396. &per_cpu(od_cpu_dbs_info, i);
  1397. struct dbs_work_struct *dbs_work =
  1398. &per_cpu(dbs_refresh_work, i);
  1399. mutex_init(&this_dbs_info->timer_mutex);
  1400. INIT_WORK(&dbs_work->work, dbs_refresh_callback);
  1401. dbs_work->cpu = i;
  1402. atomic_set(&this_dbs_info->src_sync_cpu, -1);
  1403. atomic_set(&this_dbs_info->being_woken, 0);
  1404. init_waitqueue_head(&this_dbs_info->sync_wq);
  1405. this_dbs_info->sync_thread = kthread_run(dbs_sync_thread,
  1406. (void *)i,
  1407. "dbs_sync/%d", i);
  1408. }
  1409. return cpufreq_register_governor(&cpufreq_gov_ondemand);
  1410. }
  1411. static void __exit cpufreq_gov_dbs_exit(void)
  1412. {
  1413. unsigned int i;
  1414. cpufreq_unregister_governor(&cpufreq_gov_ondemand);
  1415. for_each_possible_cpu(i) {
  1416. struct cpu_dbs_info_s *this_dbs_info =
  1417. &per_cpu(od_cpu_dbs_info, i);
  1418. mutex_destroy(&this_dbs_info->timer_mutex);
  1419. kthread_stop(this_dbs_info->sync_thread);
  1420. }
  1421. destroy_workqueue(dbs_wq);
  1422. }
  1423. MODULE_AUTHOR("Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>");
  1424. MODULE_AUTHOR("Alexey Starikovskiy <alexey.y.starikovskiy@intel.com>");
  1425. MODULE_DESCRIPTION("'cpufreq_ondemand' - A dynamic cpufreq governor for "
  1426. "Low Latency Frequency Transition capable processors");
  1427. MODULE_LICENSE("GPL");
  1428. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_ONDEMAND
  1429. fs_initcall(cpufreq_gov_dbs_init);
  1430. #else
  1431. module_init(cpufreq_gov_dbs_init);
  1432. #endif
  1433. module_exit(cpufreq_gov_dbs_exit);