cpu-boost.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "cpu-boost: " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/notifier.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/cpu.h>
  19. #include <linux/sched.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/kthread.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/slab.h>
  24. #include <linux/input.h>
  25. #include <linux/time.h>
  26. struct cpu_sync {
  27. struct task_struct *thread;
  28. wait_queue_head_t sync_wq;
  29. struct delayed_work boost_rem;
  30. struct delayed_work input_boost_rem;
  31. int cpu;
  32. spinlock_t lock;
  33. bool pending;
  34. atomic_t being_woken;
  35. int src_cpu;
  36. unsigned int boost_min;
  37. unsigned int input_boost_min;
  38. };
  39. static DEFINE_PER_CPU(struct cpu_sync, sync_info);
  40. static struct workqueue_struct *cpu_boost_wq;
  41. static struct work_struct input_boost_work;
  42. static unsigned int boost_ms;
  43. module_param(boost_ms, uint, 0644);
  44. static unsigned int sync_threshold;
  45. module_param(sync_threshold, uint, 0644);
  46. static unsigned int input_boost_freq;
  47. module_param(input_boost_freq, uint, 0644);
  48. static unsigned int input_boost_ms = 40;
  49. module_param(input_boost_ms, uint, 0644);
  50. static u64 last_input_time;
  51. #define MIN_INPUT_INTERVAL (150 * USEC_PER_MSEC)
  52. /*
  53. * The CPUFREQ_ADJUST notifier is used to override the current policy min to
  54. * make sure policy min >= boost_min. The cpufreq framework then does the job
  55. * of enforcing the new policy.
  56. *
  57. * The sync kthread needs to run on the CPU in question to avoid deadlocks in
  58. * the wake up code. Achieve this by binding the thread to the respective
  59. * CPU. But a CPU going offline unbinds threads from that CPU. So, set it up
  60. * again each time the CPU comes back up. We can use CPUFREQ_START to figure
  61. * out a CPU is coming online instead of registering for hotplug notifiers.
  62. */
  63. static int boost_adjust_notify(struct notifier_block *nb, unsigned long val, void *data)
  64. {
  65. struct cpufreq_policy *policy = data;
  66. unsigned int cpu = policy->cpu;
  67. struct cpu_sync *s = &per_cpu(sync_info, cpu);
  68. unsigned int b_min = s->boost_min;
  69. unsigned int ib_min = s->input_boost_min;
  70. unsigned int min;
  71. switch (val) {
  72. case CPUFREQ_ADJUST:
  73. if (!b_min && !ib_min)
  74. break;
  75. min = max(b_min, ib_min);
  76. pr_debug("CPU%u policy min before boost: %u kHz\n",
  77. cpu, policy->min);
  78. pr_debug("CPU%u boost min: %u kHz\n", cpu, min);
  79. cpufreq_verify_within_limits(policy, min, UINT_MAX);
  80. pr_debug("CPU%u policy min after boost: %u kHz\n",
  81. cpu, policy->min);
  82. break;
  83. case CPUFREQ_START:
  84. set_cpus_allowed(s->thread, *cpumask_of(cpu));
  85. break;
  86. }
  87. return NOTIFY_OK;
  88. }
  89. static struct notifier_block boost_adjust_nb = {
  90. .notifier_call = boost_adjust_notify,
  91. };
  92. static void do_boost_rem(struct work_struct *work)
  93. {
  94. struct cpu_sync *s = container_of(work, struct cpu_sync,
  95. boost_rem.work);
  96. pr_debug("Removing boost for CPU%d\n", s->cpu);
  97. s->boost_min = 0;
  98. /* Force policy re-evaluation to trigger adjust notifier. */
  99. cpufreq_update_policy(s->cpu);
  100. }
  101. static void do_input_boost_rem(struct work_struct *work)
  102. {
  103. struct cpu_sync *s = container_of(work, struct cpu_sync,
  104. input_boost_rem.work);
  105. pr_debug("Removing input boost for CPU%d\n", s->cpu);
  106. s->input_boost_min = 0;
  107. /* Force policy re-evaluation to trigger adjust notifier. */
  108. cpufreq_update_policy(s->cpu);
  109. }
  110. static int boost_mig_sync_thread(void *data)
  111. {
  112. int dest_cpu = (int) data;
  113. int src_cpu, ret;
  114. struct cpu_sync *s = &per_cpu(sync_info, dest_cpu);
  115. struct cpufreq_policy dest_policy;
  116. struct cpufreq_policy src_policy;
  117. unsigned long flags;
  118. while(1) {
  119. wait_event_interruptible(s->sync_wq, s->pending ||
  120. kthread_should_stop());
  121. if (kthread_should_stop())
  122. break;
  123. spin_lock_irqsave(&s->lock, flags);
  124. s->pending = false;
  125. src_cpu = s->src_cpu;
  126. spin_unlock_irqrestore(&s->lock, flags);
  127. ret = cpufreq_get_policy(&src_policy, src_cpu);
  128. if (ret)
  129. continue;
  130. ret = cpufreq_get_policy(&dest_policy, dest_cpu);
  131. if (ret)
  132. continue;
  133. if (dest_policy.cur >= src_policy.cur ) {
  134. pr_debug("No sync. CPU%d@%dKHz >= CPU%d@%dKHz\n",
  135. dest_cpu, dest_policy.cur, src_cpu, src_policy.cur);
  136. continue;
  137. }
  138. if (sync_threshold && (dest_policy.cur >= sync_threshold))
  139. continue;
  140. cancel_delayed_work_sync(&s->boost_rem);
  141. if (sync_threshold) {
  142. if (src_policy.cur >= sync_threshold)
  143. s->boost_min = sync_threshold;
  144. else
  145. s->boost_min = src_policy.cur;
  146. } else {
  147. s->boost_min = src_policy.cur;
  148. }
  149. /* Force policy re-evaluation to trigger adjust notifier. */
  150. get_online_cpus();
  151. if (cpu_online(dest_cpu)) {
  152. cpufreq_update_policy(dest_cpu);
  153. queue_delayed_work_on(dest_cpu, cpu_boost_wq,
  154. &s->boost_rem, msecs_to_jiffies(boost_ms));
  155. } else {
  156. s->boost_min = 0;
  157. pr_debug("Resetting boost_min to 0\n");
  158. }
  159. put_online_cpus();
  160. }
  161. return 0;
  162. }
  163. static int boost_migration_notify(struct notifier_block *nb,
  164. unsigned long dest_cpu, void *arg)
  165. {
  166. unsigned long flags;
  167. struct cpu_sync *s = &per_cpu(sync_info, dest_cpu);
  168. if (!boost_ms)
  169. return NOTIFY_OK;
  170. /* Avoid deadlock in try_to_wake_up() */
  171. if (s->thread == current)
  172. return NOTIFY_OK;
  173. pr_debug("Migration: CPU%d --> CPU%d\n", (int) arg, (int) dest_cpu);
  174. spin_lock_irqsave(&s->lock, flags);
  175. s->pending = true;
  176. s->src_cpu = (int) arg;
  177. spin_unlock_irqrestore(&s->lock, flags);
  178. /*
  179. * Avoid issuing recursive wakeup call, as sync thread itself could be
  180. * seen as migrating triggering this notification. Note that sync thread
  181. * of a cpu could be running for a short while with its affinity broken
  182. * because of CPU hotplug.
  183. */
  184. if (!atomic_cmpxchg(&s->being_woken, 0, 1)) {
  185. wake_up(&s->sync_wq);
  186. atomic_set(&s->being_woken, 0);
  187. }
  188. return NOTIFY_OK;
  189. }
  190. static struct notifier_block boost_migration_nb = {
  191. .notifier_call = boost_migration_notify,
  192. };
  193. static void do_input_boost(struct work_struct *work)
  194. {
  195. unsigned int i, ret;
  196. struct cpu_sync *i_sync_info;
  197. struct cpufreq_policy policy;
  198. get_online_cpus();
  199. for_each_online_cpu(i) {
  200. i_sync_info = &per_cpu(sync_info, i);
  201. ret = cpufreq_get_policy(&policy, i);
  202. if (ret)
  203. continue;
  204. if (policy.cur >= input_boost_freq)
  205. continue;
  206. cancel_delayed_work_sync(&i_sync_info->input_boost_rem);
  207. i_sync_info->input_boost_min = input_boost_freq;
  208. cpufreq_update_policy(i);
  209. queue_delayed_work_on(i_sync_info->cpu, cpu_boost_wq,
  210. &i_sync_info->input_boost_rem,
  211. msecs_to_jiffies(input_boost_ms));
  212. }
  213. put_online_cpus();
  214. }
  215. static void cpuboost_input_event(struct input_handle *handle,
  216. unsigned int type, unsigned int code, int value)
  217. {
  218. u64 now;
  219. if (!input_boost_freq)
  220. return;
  221. now = ktime_to_us(ktime_get());
  222. if (now - last_input_time < MIN_INPUT_INTERVAL)
  223. return;
  224. if (work_pending(&input_boost_work))
  225. return;
  226. queue_work(cpu_boost_wq, &input_boost_work);
  227. last_input_time = ktime_to_us(ktime_get());
  228. }
  229. static int cpuboost_input_connect(struct input_handler *handler,
  230. struct input_dev *dev, const struct input_device_id *id)
  231. {
  232. struct input_handle *handle;
  233. int error;
  234. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  235. if (!handle)
  236. return -ENOMEM;
  237. handle->dev = dev;
  238. handle->handler = handler;
  239. handle->name = "cpufreq";
  240. error = input_register_handle(handle);
  241. if (error)
  242. goto err2;
  243. error = input_open_device(handle);
  244. if (error)
  245. goto err1;
  246. return 0;
  247. err1:
  248. input_unregister_handle(handle);
  249. err2:
  250. kfree(handle);
  251. return error;
  252. }
  253. static void cpuboost_input_disconnect(struct input_handle *handle)
  254. {
  255. input_close_device(handle);
  256. input_unregister_handle(handle);
  257. kfree(handle);
  258. }
  259. static const struct input_device_id cpuboost_ids[] = {
  260. /* multi-touch touchscreen */
  261. {
  262. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  263. INPUT_DEVICE_ID_MATCH_ABSBIT,
  264. .evbit = { BIT_MASK(EV_ABS) },
  265. .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
  266. BIT_MASK(ABS_MT_POSITION_X) |
  267. BIT_MASK(ABS_MT_POSITION_Y) },
  268. },
  269. /* touchpad */
  270. {
  271. .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
  272. INPUT_DEVICE_ID_MATCH_ABSBIT,
  273. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  274. .absbit = { [BIT_WORD(ABS_X)] =
  275. BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  276. },
  277. /* Keypad */
  278. {
  279. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  280. .evbit = { BIT_MASK(EV_KEY) },
  281. },
  282. { },
  283. };
  284. static struct input_handler cpuboost_input_handler = {
  285. .event = cpuboost_input_event,
  286. .connect = cpuboost_input_connect,
  287. .disconnect = cpuboost_input_disconnect,
  288. .name = "cpu-boost",
  289. .id_table = cpuboost_ids,
  290. };
  291. static int cpu_boost_init(void)
  292. {
  293. int cpu, ret;
  294. struct cpu_sync *s;
  295. cpu_boost_wq = alloc_workqueue("cpuboost_wq", WQ_HIGHPRI, 0);
  296. if (!cpu_boost_wq)
  297. return -EFAULT;
  298. INIT_WORK(&input_boost_work, do_input_boost);
  299. for_each_possible_cpu(cpu) {
  300. s = &per_cpu(sync_info, cpu);
  301. s->cpu = cpu;
  302. init_waitqueue_head(&s->sync_wq);
  303. atomic_set(&s->being_woken, 0);
  304. spin_lock_init(&s->lock);
  305. INIT_DELAYED_WORK(&s->boost_rem, do_boost_rem);
  306. INIT_DELAYED_WORK(&s->input_boost_rem, do_input_boost_rem);
  307. s->thread = kthread_run(boost_mig_sync_thread, (void *)cpu,
  308. "boost_sync/%d", cpu);
  309. set_cpus_allowed(s->thread, *cpumask_of(cpu));
  310. }
  311. cpufreq_register_notifier(&boost_adjust_nb, CPUFREQ_POLICY_NOTIFIER);
  312. atomic_notifier_chain_register(&migration_notifier_head,
  313. &boost_migration_nb);
  314. ret = input_register_handler(&cpuboost_input_handler);
  315. return 0;
  316. }
  317. late_initcall(cpu_boost_init);