viulian

cpufreq_smartass.c

Aug 26th, 2011
3,376
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * drivers/cpufreq/cpufreq_smartass.c
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Author: Erasmux
  16. *
  17. * Based on the interactive governor By Mike Chan (mike@android.com)
  18. * which was adaptated to 2.6.29 kernel by Nadlabak (pavel@doshaska.net)
  19. *
  20. * requires to add
  21. * EXPORT_SYMBOL_GPL(nr_running);
  22. * at the end of kernel/sched.c
  23. *
  24. */
  25.  
  26. #include <linux/cpu.h>
  27. #include <linux/cpumask.h>
  28. #include <linux/cpufreq.h>
  29. #include <linux/sched.h>
  30. #include <linux/tick.h>
  31. #include <linux/timer.h>
  32. #include <linux/workqueue.h>
  33. #include <linux/moduleparam.h>
  34. #include <asm/cputime.h>
  35. #include <linux/earlysuspend.h>
  36.  
  37. #define AX_MODULE_NAME "smartass"
  38. #define AX_MODULE_VER "v001"
  39.  
  40. #define DEVICE_NAME "X10"
  41. #define OFS_KALLSYMS_LOOKUP_NAME 0xc009684c // kallsyms_lookup_name
  42.  
  43. // for get proc address
  44. typedef unsigned long (*kallsyms_lookup_name_type)(const char *name);
  45. static kallsyms_lookup_name_type kallsyms_lookup_name_ax;
  46.  
  47. typedef long (*nr_running_type) (void);
  48. static nr_running_type nr_running_ax;
  49.  
  50. typedef void (*default_idle_type) (void);
  51. static default_idle_type default_idle_ax;
  52.  
  53. static void (*pm_idle_old)(void);
  54. static atomic_t active_count = ATOMIC_INIT(0);
  55.  
  56. struct smartass_info_s {
  57. struct cpufreq_policy *cur_policy;
  58. struct timer_list timer;
  59. u64 time_in_idle;
  60. u64 idle_exit_time;
  61. u64 freq_change_time;
  62. u64 freq_change_time_in_idle;
  63. int cur_cpu_load;
  64. unsigned int force_ramp_up;
  65. unsigned int enable;
  66. int max_speed;
  67. int min_speed;
  68. int idling;
  69. int suspended;
  70. };
  71.  
  72. static DEFINE_PER_CPU(struct smartass_info_s, smartass_info);
  73.  
  74. /* Workqueues handle frequency scaling */
  75. static struct workqueue_struct *up_wq;
  76. static struct workqueue_struct *down_wq;
  77. static struct work_struct freq_scale_work;
  78.  
  79. static cpumask_t work_cpumask;
  80. static unsigned int suspended;
  81.  
  82. enum {
  83. SMARTASS_DEBUG_JUMPS=1,
  84. SMARTASS_DEBUG_LOAD=2,
  85. SMARTASS_DEBUG_IDLE=4
  86. };
  87.  
  88. /*
  89. * Combination of the above debug flags.
  90. */
  91. static unsigned long debug_mask = 7;
  92.  
  93. /*
  94. * The minimum amount of time to spend at a frequency before we can ramp up.
  95. */
  96. #define DEFAULT_UP_RATE_US 24000;
  97. static unsigned long up_rate_us;
  98.  
  99. /*
  100. * The minimum amount of time to spend at a frequency before we can ramp down.
  101. */
  102. #define DEFAULT_DOWN_RATE_US 49000;
  103. static unsigned long down_rate_us;
  104.  
  105. /*
  106. * When ramping up frequency with no idle cycles jump to at least this frequency.
  107. * Zero disables. Set a very high value to jump to policy max freqeuncy.
  108. */
  109. #define DEFAULT_UP_MIN_FREQ 0
  110. static unsigned int up_min_freq;
  111.  
  112. /*
  113. * When sleep_max_freq>0 the frequency when suspended will be capped
  114. * by this frequency. Also will wake up at max frequency of policy
  115. * to minimize wakeup issues.
  116. * Set sleep_max_freq=0 to disable this behavior.
  117. */
  118. #define DEFAULT_SLEEP_MAX_FREQ 245760
  119. static unsigned int sleep_max_freq;
  120.  
  121. /*
  122. * The frequency to set when waking up from sleep.
  123. * When sleep_max_freq=0 this will have no effect.
  124. */
  125. #define DEFAULT_SLEEP_WAKEUP_FREQ 998400
  126. static unsigned int sleep_wakeup_freq;
  127.  
  128. /*
  129. * When awake_min_freq>0 the frequency when not suspended will not
  130. * go below this frequency.
  131. * Set awake_min_freq=0 to disable this behavior.
  132. */
  133. #define DEFAULT_AWAKE_MIN_FREQ 0
  134. static unsigned int awake_min_freq;
  135.  
  136. /*
  137. * Sampling rate, I highly recommend to leave it at 2.
  138. */
  139. #define DEFAULT_SAMPLE_RATE_JIFFIES 2
  140. static unsigned int sample_rate_jiffies;
  141.  
  142. /*
  143. * Freqeuncy delta when ramping up.
  144. * zero disables and causes to always jump straight to max frequency.
  145. */
  146. #define DEFAULT_RAMP_UP_STEP 220000
  147. static unsigned int ramp_up_step;
  148.  
  149. /*
  150. * Freqeuncy delta when ramping down.
  151. * zero disables and will calculate ramp down according to load heuristic.
  152. */
  153. #define DEFAULT_RAMP_DOWN_STEP 160000
  154. static unsigned int ramp_down_step;
  155.  
  156. /*
  157. * CPU freq will be increased if measured load > max_cpu_load;
  158. */
  159. #define DEFAULT_MAX_CPU_LOAD 75
  160. static unsigned long max_cpu_load;
  161.  
  162. /*
  163. * CPU freq will be decreased if measured load < min_cpu_load;
  164. */
  165. #define DEFAULT_MIN_CPU_LOAD 25
  166. static unsigned long min_cpu_load;
  167.  
  168. /*
  169. * When screen if off behave like conservative governor;
  170. */
  171. #define DEFAULT_SLEEP_RATE_US (usecs_to_jiffies(500000))
  172. static unsigned long sleep_rate_jiffies;
  173.  
  174. static int cpufreq_governor_smartass(struct cpufreq_policy *policy,
  175. unsigned int event);
  176.  
  177. #ifndef CONFIG_CPU_FREQ_DEFAULT_GOV_SMARTASS
  178. static
  179. #endif
  180. struct cpufreq_governor cpufreq_gov_smartass = {
  181. .name = "smartass",
  182. .governor = cpufreq_governor_smartass,
  183. .max_transition_latency = 9000000,
  184. .owner = THIS_MODULE,
  185. };
  186.  
  187. static void smartass_update_min_max(struct smartass_info_s *this_smartass, struct cpufreq_policy *policy, int suspend) {
  188. if (suspend) {
  189. this_smartass->min_speed = policy->min;
  190. this_smartass->max_speed = // sleep_max_freq; but make sure it obeys the policy min/max
  191. policy->max > sleep_max_freq ? (sleep_max_freq > policy->min ? sleep_max_freq : policy->min) : policy->max;
  192. } else {
  193. this_smartass->min_speed = // awake_min_freq; but make sure it obeys the policy min/max
  194. policy->min < awake_min_freq ? (awake_min_freq < policy->max ? awake_min_freq : policy->max) : policy->min;
  195. this_smartass->max_speed = policy->max;
  196. }
  197. }
  198.  
  199. inline static unsigned int validate_freq(struct smartass_info_s *this_smartass, int freq) {
  200. if (freq > this_smartass->max_speed)
  201. return this_smartass->max_speed;
  202. if (freq < this_smartass->min_speed)
  203. return this_smartass->min_speed;
  204. return freq;
  205. }
  206.  
  207. static void reset_timer(unsigned long cpu, struct smartass_info_s *this_smartass, unsigned int timer_rate_jiffies) {
  208. this_smartass->time_in_idle = get_cpu_idle_time_us(cpu, &this_smartass->idle_exit_time);
  209. mod_timer(&this_smartass->timer, jiffies + timer_rate_jiffies);
  210. }
  211.  
  212. static void cpufreq_smartass_timer(unsigned long data)
  213. {
  214. u64 delta_idle;
  215. u64 delta_time;
  216. int cpu_load;
  217. u64 update_time;
  218. u64 now_idle;
  219. unsigned int timer_rate_jiffies = sample_rate_jiffies;
  220.  
  221. struct smartass_info_s *this_smartass = &per_cpu(smartass_info, data);
  222. struct cpufreq_policy *policy = this_smartass->cur_policy;
  223.  
  224. now_idle = get_cpu_idle_time_us(data, &update_time);
  225.  
  226. if (this_smartass->idle_exit_time == 0 || update_time == this_smartass->idle_exit_time)
  227. return;
  228.  
  229. //be more relaxed when screen if off
  230. if(this_smartass->suspended && policy->cur == policy->min)
  231. timer_rate_jiffies = sleep_rate_jiffies;
  232.  
  233. delta_idle = cputime64_sub(now_idle, this_smartass->time_in_idle);
  234. delta_time = cputime64_sub(update_time, this_smartass->idle_exit_time);
  235.  
  236. if (debug_mask & SMARTASS_DEBUG_IDLE)
  237. printk(KERN_INFO "smartassT: t=%llu i=%llu\n",
  238. delta_time, delta_idle);
  239.  
  240. // If timer ran less than 1ms after short-term sample started, retry.
  241. if (delta_time < 1000) {
  242. if (!timer_pending(&this_smartass->timer))
  243. reset_timer(data,this_smartass, timer_rate_jiffies);
  244. return;
  245. }
  246.  
  247. if (delta_idle > delta_time)
  248. cpu_load = 0;
  249. else
  250. cpu_load = 100 * (unsigned int)(delta_time - delta_idle) / (unsigned int)delta_time;
  251.  
  252. if (debug_mask & SMARTASS_DEBUG_LOAD)
  253. printk(KERN_INFO "smartassT @ %d: load %d (delta_time %llu)\n",policy->cur,cpu_load,delta_time);
  254.  
  255. this_smartass->cur_cpu_load = cpu_load;
  256.  
  257. // Scale up if load is above max or if there where no idle cycles since coming out of idle,
  258. // or when we are above our max speed for a very long time (should only happend if entering sleep
  259. // at high loads)
  260. if ((cpu_load > max_cpu_load || delta_idle == 0) &&
  261. !(policy->cur > this_smartass->max_speed &&
  262. cputime64_sub(update_time, this_smartass->freq_change_time) > 100*down_rate_us)) {
  263.  
  264. if (policy->cur > this_smartass->max_speed) {
  265. reset_timer(data,this_smartass, timer_rate_jiffies);
  266. }
  267.  
  268. if (policy->cur == policy->max)
  269. return;
  270.  
  271. if (nr_running_ax() < 1)
  272. return;
  273.  
  274. if (cputime64_sub(update_time, this_smartass->freq_change_time) < up_rate_us)
  275. return;
  276.  
  277.  
  278. this_smartass->force_ramp_up = 1;
  279. cpumask_set_cpu(data, &work_cpumask);
  280. queue_work(up_wq, &freq_scale_work);
  281. return;
  282. }
  283.  
  284. /*
  285. * There is a window where if the cpu utlization can go from low to high
  286. * between the timer expiring, delta_idle will be > 0 and the cpu will
  287. * be 100% busy, preventing idle from running, and this timer from
  288. * firing. So setup another timer to fire to check cpu utlization.
  289. * Do not setup the timer if there is no scheduled work or if at max speed.
  290. */
  291. if (policy->cur < this_smartass->max_speed && !timer_pending(&this_smartass->timer) && nr_running_ax() > 0)
  292. {
  293. /*
  294. * If already at min: if that CPU is idle, don't set timer.
  295. * Else cancel the timer if that CPU goes idle. We don't
  296. * need to re-evaluate speed until the next idle exit.
  297. */
  298. if (policy->cur == policy->min)
  299. {
  300. if (this_smartass->idling) {
  301. return;
  302. }
  303. }
  304.  
  305. reset_timer(data,this_smartass, timer_rate_jiffies);
  306. }
  307.  
  308. if (policy->cur == policy->min)
  309. return;
  310.  
  311. /*
  312. * Do not scale down unless we have been at this frequency for the
  313. * minimum sample time.
  314. */
  315. if (cputime64_sub(update_time, this_smartass->freq_change_time) < down_rate_us)
  316. return;
  317.  
  318. /*
  319. * Scale down only when there is something to scale down.
  320. */
  321. if (cpu_load < min_cpu_load)
  322. {
  323. cpumask_set_cpu(data, &work_cpumask);
  324. queue_work(down_wq, &freq_scale_work);
  325. }
  326. }
  327.  
  328. static void execute_pm_idle_old(void)
  329. {
  330. if(pm_idle_old)
  331. pm_idle_old();
  332. else
  333. default_idle_ax();
  334. }
  335.  
  336. static void cpufreq_idle(void)
  337. {
  338. struct smartass_info_s *this_smartass = &per_cpu(smartass_info, smp_processor_id());
  339. struct cpufreq_policy *policy = this_smartass->cur_policy;
  340. unsigned int timer_rate_jiffies = sample_rate_jiffies;
  341.  
  342. if (!this_smartass->enable) {
  343. execute_pm_idle_old();
  344. return;
  345. }
  346.  
  347. this_smartass->idling = 1;
  348.  
  349. //make additional calc
  350. if (!this_smartass->suspended && policy->cur == this_smartass->min_speed && timer_pending(&this_smartass->timer))
  351. del_timer(&this_smartass->timer);
  352.  
  353. execute_pm_idle_old();
  354.  
  355. this_smartass->idling = 0;
  356.  
  357. if (!timer_pending(&this_smartass->timer))
  358. {
  359. //be more relaxed when screen if off
  360. if(this_smartass->suspended && policy->cur == policy->min)
  361. timer_rate_jiffies = sleep_rate_jiffies;
  362.  
  363. reset_timer(smp_processor_id(), this_smartass, timer_rate_jiffies );
  364. }
  365. }
  366.  
  367. /* We use the same work function to sale up and down */
  368. static void cpufreq_smartass_freq_change_time_work(struct work_struct *work)
  369. {
  370. unsigned int cpu;
  371. int new_freq;
  372. unsigned int force_ramp_up;
  373. int cpu_load;
  374. struct smartass_info_s *this_smartass;
  375. struct cpufreq_policy *policy;
  376. unsigned int relation = CPUFREQ_RELATION_L;
  377. cpumask_t tmp_mask = work_cpumask;
  378. for_each_cpu(cpu, tmp_mask) {
  379. this_smartass = &per_cpu(smartass_info, cpu);
  380. policy = this_smartass->cur_policy;
  381. cpu_load = this_smartass->cur_cpu_load;
  382. force_ramp_up = this_smartass->force_ramp_up && nr_running_ax() > 1;
  383. this_smartass->force_ramp_up = 0;
  384.  
  385. if (force_ramp_up || cpu_load > max_cpu_load) {
  386. if (force_ramp_up && up_min_freq) {
  387. new_freq = up_min_freq;
  388. relation = CPUFREQ_RELATION_L;
  389. } else if (ramp_up_step) {
  390. new_freq = policy->cur + ramp_up_step;
  391. relation = CPUFREQ_RELATION_H;
  392. } else {
  393. new_freq = this_smartass->max_speed;
  394. relation = CPUFREQ_RELATION_H;
  395. }
  396. }
  397. else if (cpu_load < min_cpu_load) {
  398. if (ramp_down_step)
  399. new_freq = policy->cur - ramp_down_step;
  400. else {
  401. cpu_load += 100 - max_cpu_load; // dummy load.
  402. new_freq = policy->cur * cpu_load / 100;
  403. }
  404. relation = CPUFREQ_RELATION_L;
  405. }
  406. else new_freq = policy->cur;
  407.  
  408. new_freq = validate_freq(this_smartass,new_freq);
  409.  
  410. if (new_freq != policy->cur) {
  411. if (debug_mask & SMARTASS_DEBUG_JUMPS)
  412. printk(KERN_INFO "SmartassQ: jumping from %d to %d\n",policy->cur,new_freq);
  413.  
  414. __cpufreq_driver_target(policy, new_freq, relation);
  415.  
  416. this_smartass->freq_change_time_in_idle =
  417. get_cpu_idle_time_us(cpu,&this_smartass->freq_change_time);
  418. }
  419.  
  420. cpumask_clear_cpu(cpu, &work_cpumask);
  421. }
  422. }
  423.  
  424. static ssize_t show_debug_mask(struct cpufreq_policy *policy, char *buf)
  425. {
  426. return sprintf(buf, "%lu\n", debug_mask);
  427. }
  428.  
  429. static ssize_t store_debug_mask(struct cpufreq_policy *policy, const char *buf, size_t count)
  430. {
  431. ssize_t res;
  432. unsigned long input;
  433. res = strict_strtoul(buf, 0, &input);
  434. if (res >= 0)
  435. debug_mask = input;
  436. return res;
  437. }
  438.  
  439. static struct freq_attr debug_mask_attr = __ATTR(debug_mask, 0644,
  440. show_debug_mask, store_debug_mask);
  441.  
  442. static ssize_t show_up_rate_us(struct cpufreq_policy *policy, char *buf)
  443. {
  444. return sprintf(buf, "%lu\n", up_rate_us);
  445. }
  446.  
  447. static ssize_t store_up_rate_us(struct cpufreq_policy *policy, const char *buf, size_t count)
  448. {
  449. ssize_t res;
  450. unsigned long input;
  451. res = strict_strtoul(buf, 0, &input);
  452. if (res >= 0 && input >= 0 && input <= 100000000)
  453. up_rate_us = input;
  454. return res;
  455. }
  456.  
  457. static struct freq_attr up_rate_us_attr = __ATTR(up_rate_us, 0644,
  458. show_up_rate_us, store_up_rate_us);
  459.  
  460. static ssize_t show_down_rate_us(struct cpufreq_policy *policy, char *buf)
  461. {
  462. return sprintf(buf, "%lu\n", down_rate_us);
  463. }
  464.  
  465. static ssize_t store_down_rate_us(struct cpufreq_policy *policy, const char *buf, size_t count)
  466. {
  467. ssize_t res;
  468. unsigned long input;
  469. res = strict_strtoul(buf, 0, &input);
  470. if (res >= 0 && input >= 0 && input <= 100000000)
  471. down_rate_us = input;
  472. return res;
  473. }
  474.  
  475. static struct freq_attr down_rate_us_attr = __ATTR(down_rate_us, 0644,
  476. show_down_rate_us, store_down_rate_us);
  477.  
  478. static ssize_t show_up_min_freq(struct cpufreq_policy *policy, char *buf)
  479. {
  480. return sprintf(buf, "%u\n", up_min_freq);
  481. }
  482.  
  483. static ssize_t store_up_min_freq(struct cpufreq_policy *policy, const char *buf, size_t count)
  484. {
  485. ssize_t res;
  486. unsigned long input;
  487. res = strict_strtoul(buf, 0, &input);
  488. if (res >= 0 && input >= 0)
  489. up_min_freq = input;
  490. return res;
  491. }
  492.  
  493. static struct freq_attr up_min_freq_attr = __ATTR(up_min_freq, 0644,
  494. show_up_min_freq, store_up_min_freq);
  495.  
  496. static ssize_t show_sleep_max_freq(struct cpufreq_policy *policy, char *buf)
  497. {
  498. return sprintf(buf, "%u\n", sleep_max_freq);
  499. }
  500.  
  501. static ssize_t store_sleep_max_freq(struct cpufreq_policy *policy, const char *buf, size_t count)
  502. {
  503. ssize_t res;
  504. unsigned long input;
  505. res = strict_strtoul(buf, 0, &input);
  506. if (res >= 0 && input >= 0)
  507. sleep_max_freq = input;
  508. return res;
  509. }
  510.  
  511. static struct freq_attr sleep_max_freq_attr = __ATTR(sleep_max_freq, 0644,
  512. show_sleep_max_freq, store_sleep_max_freq);
  513.  
  514. static ssize_t show_sleep_wakeup_freq(struct cpufreq_policy *policy, char *buf)
  515. {
  516. return sprintf(buf, "%u\n", sleep_wakeup_freq);
  517. }
  518.  
  519. static ssize_t store_sleep_wakeup_freq(struct cpufreq_policy *policy, const char *buf, size_t count)
  520. {
  521. ssize_t res;
  522. unsigned long input;
  523. res = strict_strtoul(buf, 0, &input);
  524. if (res >= 0 && input >= 0)
  525. sleep_wakeup_freq = input;
  526. return res;
  527. }
  528.  
  529. static struct freq_attr sleep_wakeup_freq_attr = __ATTR(sleep_wakeup_freq, 0644,
  530. show_sleep_wakeup_freq, store_sleep_wakeup_freq);
  531.  
  532. static ssize_t show_awake_min_freq(struct cpufreq_policy *policy, char *buf)
  533. {
  534. return sprintf(buf, "%u\n", awake_min_freq);
  535. }
  536.  
  537. static ssize_t store_awake_min_freq(struct cpufreq_policy *policy, const char *buf, size_t count)
  538. {
  539. ssize_t res;
  540. unsigned long input;
  541. res = strict_strtoul(buf, 0, &input);
  542. if (res >= 0 && input >= 0)
  543. awake_min_freq = input;
  544. return res;
  545. }
  546.  
  547. static struct freq_attr awake_min_freq_attr = __ATTR(awake_min_freq, 0644,
  548. show_awake_min_freq, store_awake_min_freq);
  549.  
  550. static ssize_t show_sample_rate_jiffies(struct cpufreq_policy *policy, char *buf)
  551. {
  552. return sprintf(buf, "%u\n", sample_rate_jiffies);
  553. }
  554.  
  555. static ssize_t store_sample_rate_jiffies(struct cpufreq_policy *policy, const char *buf, size_t count)
  556. {
  557. ssize_t res;
  558. unsigned long input;
  559. res = strict_strtoul(buf, 0, &input);
  560. if (res >= 0 && input > 0 && input <= 1000)
  561. sample_rate_jiffies = input;
  562. return res;
  563. }
  564.  
  565. static struct freq_attr sample_rate_jiffies_attr = __ATTR(sample_rate_jiffies, 0644,
  566. show_sample_rate_jiffies, store_sample_rate_jiffies);
  567.  
  568. static ssize_t show_ramp_up_step(struct cpufreq_policy *policy, char *buf)
  569. {
  570. return sprintf(buf, "%u\n", ramp_up_step);
  571. }
  572.  
  573. static ssize_t store_ramp_up_step(struct cpufreq_policy *policy, const char *buf, size_t count)
  574. {
  575. ssize_t res;
  576. unsigned long input;
  577. res = strict_strtoul(buf, 0, &input);
  578. if (res >= 0 && input >= 0)
  579. ramp_up_step = input;
  580. return res;
  581. }
  582.  
  583. static struct freq_attr ramp_up_step_attr = __ATTR(ramp_up_step, 0644,
  584. show_ramp_up_step, store_ramp_up_step);
  585.  
  586. static ssize_t show_ramp_down_step(struct cpufreq_policy *policy, char *buf)
  587. {
  588. return sprintf(buf, "%u\n", ramp_down_step);
  589. }
  590.  
  591. static ssize_t store_ramp_down_step(struct cpufreq_policy *policy, const char *buf, size_t count)
  592. {
  593. ssize_t res;
  594. unsigned long input;
  595. res = strict_strtoul(buf, 0, &input);
  596. if (res >= 0 && input >= 0)
  597. ramp_down_step = input;
  598. return res;
  599. }
  600.  
  601. static struct freq_attr ramp_down_step_attr = __ATTR(ramp_down_step, 0644,
  602. show_ramp_down_step, store_ramp_down_step);
  603.  
  604. static ssize_t show_max_cpu_load(struct cpufreq_policy *policy, char *buf)
  605. {
  606. return sprintf(buf, "%lu\n", max_cpu_load);
  607. }
  608.  
  609. static ssize_t store_max_cpu_load(struct cpufreq_policy *policy, const char *buf, size_t count)
  610. {
  611. ssize_t res;
  612. unsigned long input;
  613. res = strict_strtoul(buf, 0, &input);
  614. if (res >= 0 && input > 0 && input <= 100)
  615. max_cpu_load = input;
  616. return res;
  617. }
  618.  
  619. static struct freq_attr max_cpu_load_attr = __ATTR(max_cpu_load, 0644,
  620. show_max_cpu_load, store_max_cpu_load);
  621.  
  622. static ssize_t show_min_cpu_load(struct cpufreq_policy *policy, char *buf)
  623. {
  624. return sprintf(buf, "%lu\n", min_cpu_load);
  625. }
  626.  
  627. static ssize_t store_min_cpu_load(struct cpufreq_policy *policy, const char *buf, size_t count)
  628. {
  629. ssize_t res;
  630. unsigned long input;
  631. res = strict_strtoul(buf, 0, &input);
  632. if (res >= 0 && input > 0 && input < 100)
  633. min_cpu_load = input;
  634. return res;
  635. }
  636.  
  637. static struct freq_attr min_cpu_load_attr = __ATTR(min_cpu_load, 0644,
  638. show_min_cpu_load, store_min_cpu_load);
  639.  
  640. static ssize_t show_sleep_rate_us(struct cpufreq_policy *policy, char *buf)
  641. {
  642. return sprintf(buf, "%lu\n", (long)jiffies_to_usecs(sleep_rate_jiffies));
  643. }
  644.  
  645. static ssize_t store_sleep_rate_us(struct cpufreq_policy *policy, const char *buf, size_t count)
  646. {
  647. ssize_t res;
  648. unsigned long input;
  649. res = strict_strtoul(buf, 0, &input);
  650. if (res >= 0 && input >= 50000 && input <= 1000000)
  651. sleep_rate_jiffies = usecs_to_jiffies(input);
  652. return res;
  653. }
  654.  
  655. static struct freq_attr sleep_rate_us_attr = __ATTR(sleep_rate_us, 0644,
  656. show_sleep_rate_us, store_sleep_rate_us);
  657.  
  658. static struct attribute * smartass_attributes[] = {
  659. &debug_mask_attr.attr,
  660. &up_rate_us_attr.attr,
  661. &down_rate_us_attr.attr,
  662. &up_min_freq_attr.attr,
  663. &sleep_max_freq_attr.attr,
  664. &sleep_wakeup_freq_attr.attr,
  665. &awake_min_freq_attr.attr,
  666. &sample_rate_jiffies_attr.attr,
  667. &ramp_up_step_attr.attr,
  668. &ramp_down_step_attr.attr,
  669. &max_cpu_load_attr.attr,
  670. &min_cpu_load_attr.attr,
  671. &sleep_rate_us_attr.attr,
  672. NULL,
  673. };
  674.  
  675. static struct attribute_group smartass_attr_group = {
  676. .attrs = smartass_attributes,
  677. .name = "smartass",
  678. };
  679.  
  680. static int cpufreq_governor_smartass(struct cpufreq_policy *new_policy,
  681. unsigned int event)
  682. {
  683. unsigned int cpu = new_policy->cpu;
  684. int rc;
  685. struct smartass_info_s *this_smartass = &per_cpu(smartass_info, cpu);
  686.  
  687. switch (event) {
  688. case CPUFREQ_GOV_START:
  689. if ((!cpu_online(cpu)) || (!new_policy->cur))
  690. return -EINVAL;
  691.  
  692. /*
  693. * Do not register the idle hook and create sysfs
  694. * entries if we have already done so.
  695. */
  696. if (atomic_inc_return(&active_count) <= 1) {
  697. rc = sysfs_create_group(&new_policy->kobj, &smartass_attr_group);
  698. if (rc)
  699. return rc;
  700. pm_idle_old = pm_idle;
  701. pm_idle = cpufreq_idle;
  702. }
  703.  
  704. this_smartass->cur_policy = new_policy;
  705. this_smartass->enable = 1;
  706.  
  707. // notice no break here!
  708.  
  709. case CPUFREQ_GOV_LIMITS:
  710. smartass_update_min_max(this_smartass,new_policy,suspended);
  711. if (this_smartass->cur_policy->cur != this_smartass->max_speed) {
  712. if (debug_mask & SMARTASS_DEBUG_JUMPS)
  713. printk(KERN_INFO "SmartassI: initializing to %d\n",this_smartass->max_speed);
  714. __cpufreq_driver_target(new_policy, this_smartass->max_speed, CPUFREQ_RELATION_H);
  715. }
  716. break;
  717.  
  718. case CPUFREQ_GOV_STOP:
  719. del_timer(&this_smartass->timer);
  720. this_smartass->enable = 0;
  721.  
  722. if (atomic_dec_return(&active_count) > 1)
  723. return 0;
  724.  
  725. sysfs_remove_group(&new_policy->kobj,
  726. &smartass_attr_group);
  727.  
  728. pm_idle = pm_idle_old;
  729. break;
  730. }
  731.  
  732. return 0;
  733. }
  734.  
  735. static void smartass_suspend(int cpu, int suspend)
  736. {
  737. struct smartass_info_s *this_smartass = &per_cpu(smartass_info, smp_processor_id());
  738. struct cpufreq_policy *policy = this_smartass->cur_policy;
  739. unsigned int new_freq;
  740.  
  741. if (!this_smartass->enable || sleep_max_freq==0) // disable behavior for sleep_max_freq==0
  742. return;
  743.  
  744. smartass_update_min_max(this_smartass,policy,suspend);
  745.  
  746. this_smartass->suspended = suspend;
  747.  
  748. if (!suspend) { // resume at max speed:
  749. new_freq = validate_freq(this_smartass,sleep_wakeup_freq);
  750.  
  751. if (debug_mask & SMARTASS_DEBUG_JUMPS)
  752. printk(KERN_INFO "SmartassS: awaking at %d\n",new_freq);
  753.  
  754. __cpufreq_driver_target(policy, new_freq,
  755. CPUFREQ_RELATION_L);
  756.  
  757. if (policy->cur < this_smartass->max_speed && !timer_pending(&this_smartass->timer))
  758. reset_timer(smp_processor_id(),this_smartass, sample_rate_jiffies);
  759. } else {
  760. // to avoid wakeup issues with quick sleep/wakeup don't change actual frequency when entering sleep
  761. // to allow some time to settle down.
  762. // we reset the timer, if eventually, even at full load the timer will lower the freqeuncy.
  763. reset_timer(smp_processor_id(),this_smartass, sample_rate_jiffies);
  764.  
  765. this_smartass->freq_change_time_in_idle =
  766. get_cpu_idle_time_us(cpu,&this_smartass->freq_change_time);
  767.  
  768. if (debug_mask & SMARTASS_DEBUG_JUMPS)
  769. printk(KERN_INFO "SmartassS: suspending at %d\n",policy->cur);
  770. }
  771. }
  772.  
  773. static void smartass_early_suspend(struct early_suspend *handler) {
  774. int i;
  775. for_each_online_cpu(i)
  776. smartass_suspend(i,1);
  777. }
  778.  
  779. static void smartass_late_resume(struct early_suspend *handler) {
  780. int i;
  781. for_each_online_cpu(i)
  782. smartass_suspend(i,0);
  783. }
  784.  
  785. static struct early_suspend smartass_power_suspend = {
  786. .suspend = smartass_early_suspend,
  787. .resume = smartass_late_resume,
  788. .level = EARLY_SUSPEND_LEVEL_DISABLE_FB + 1,
  789. };
  790.  
  791. static int __init cpufreq_smartass_init(void)
  792. {
  793. unsigned int i;
  794. struct smartass_info_s *this_smartass;
  795. debug_mask = 0;
  796. up_rate_us = DEFAULT_UP_RATE_US;
  797. down_rate_us = DEFAULT_DOWN_RATE_US;
  798. up_min_freq = DEFAULT_UP_MIN_FREQ;
  799. sleep_max_freq = DEFAULT_SLEEP_MAX_FREQ;
  800. sleep_wakeup_freq = DEFAULT_SLEEP_WAKEUP_FREQ;
  801. awake_min_freq = DEFAULT_AWAKE_MIN_FREQ;
  802. sample_rate_jiffies = DEFAULT_SAMPLE_RATE_JIFFIES;
  803. ramp_up_step = DEFAULT_RAMP_UP_STEP;
  804. ramp_down_step = DEFAULT_RAMP_DOWN_STEP;
  805. max_cpu_load = DEFAULT_MAX_CPU_LOAD;
  806. min_cpu_load = DEFAULT_MIN_CPU_LOAD;
  807. sleep_rate_jiffies = DEFAULT_SLEEP_RATE_US;
  808.  
  809. suspended = 0;
  810.  
  811. //first check if we can handle required function
  812. // our 'GetProcAddress' :D
  813. kallsyms_lookup_name_ax = (void*) OFS_KALLSYMS_LOOKUP_NAME;
  814. nr_running_ax = (void*) kallsyms_lookup_name_ax("nr_running");
  815. default_idle_ax = (void*) kallsyms_lookup_name_ax("default_idle");
  816.  
  817. if(!nr_running_ax || !default_idle_ax)
  818. return -1;
  819.  
  820.  
  821. /* Initalize per-cpu data: */
  822. for_each_possible_cpu(i) {
  823. this_smartass = &per_cpu(smartass_info, i);
  824. this_smartass->enable = 0;
  825. this_smartass->cur_policy = 0;
  826. this_smartass->force_ramp_up = 0;
  827. this_smartass->max_speed = DEFAULT_SLEEP_WAKEUP_FREQ;
  828. this_smartass->min_speed = DEFAULT_AWAKE_MIN_FREQ;
  829. this_smartass->time_in_idle = 0;
  830. this_smartass->idle_exit_time = 0;
  831. this_smartass->freq_change_time = 0;
  832. this_smartass->freq_change_time_in_idle = 0;
  833. this_smartass->cur_cpu_load = 0;
  834. this_smartass->idling = 0;
  835. this_smartass->suspended = 0;
  836.  
  837. // intialize timer:
  838. init_timer_deferrable(&this_smartass->timer);
  839. this_smartass->timer.function = cpufreq_smartass_timer;
  840. this_smartass->timer.data = i;
  841. }
  842.  
  843. /* Scale up is high priority */
  844. up_wq = create_rt_workqueue("ksmartass_up");
  845. down_wq = create_workqueue("ksmartass_down");
  846.  
  847. INIT_WORK(&freq_scale_work, cpufreq_smartass_freq_change_time_work);
  848.  
  849. register_early_suspend(&smartass_power_suspend);
  850.  
  851. printk(KERN_INFO AX_MODULE_NAME ": module " AX_MODULE_VER " loaded\n");
  852.  
  853. return cpufreq_register_governor(&cpufreq_gov_smartass);
  854. }
  855.  
  856. #ifdef CONFIG_CPU_FREQ_DEFAULT_GOV_SMARTASS
  857. pure_initcall(cpufreq_smartass_init);
  858. #else
  859. module_init(cpufreq_smartass_init);
  860. #endif
  861.  
  862. static void __exit cpufreq_smartass_exit(void)
  863. {
  864. struct smartass_info_s *this_smartass = &per_cpu(smartass_info, 0);
  865. this_smartass->enable = 0;
  866.  
  867. cpufreq_unregister_governor(&cpufreq_gov_smartass);
  868. unregister_early_suspend(&smartass_power_suspend);
  869. destroy_workqueue(up_wq);
  870. destroy_workqueue(down_wq);
  871. printk(KERN_INFO AX_MODULE_NAME ": module " AX_MODULE_VER " unloaded\n");
  872. }
  873.  
  874. module_exit(cpufreq_smartass_exit);
  875.  
  876. MODULE_AUTHOR ("Erasmux");
  877. MODULE_AUTHOR ("AnDyX - fixes for lot of things.");
  878. MODULE_AUTHOR ("viulian - added support for X10");
  879. MODULE_DESCRIPTION ("A smartass cpufreq governor.");
  880. MODULE_LICENSE ("GPL");
RAW Paste Data