cpu-boost.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. pr_debug("Migration: CPU%d --> CPU%d\n", (int) arg, (int) dest_cpu);
  171. spin_lock_irqsave(&s->lock, flags);
  172. s->pending = true;
  173. s->src_cpu = (int) arg;
  174. spin_unlock_irqrestore(&s->lock, flags);
  175. /*
  176. * Avoid issuing recursive wakeup call, as sync thread itself could be
  177. * seen as migrating triggering this notification. Note that sync thread
  178. * of a cpu could be running for a short while with its affinity broken
  179. * because of CPU hotplug.
  180. */
  181. if (!atomic_cmpxchg(&s->being_woken, 0, 1)) {
  182. wake_up(&s->sync_wq);
  183. atomic_set(&s->being_woken, 0);
  184. }
  185. return NOTIFY_OK;
  186. }
  187. static struct notifier_block boost_migration_nb = {
  188. .notifier_call = boost_migration_notify,
  189. };
  190. static void do_input_boost(struct work_struct *work)
  191. {
  192. unsigned int i, ret;
  193. struct cpu_sync *i_sync_info;
  194. struct cpufreq_policy policy;
  195. get_online_cpus();
  196. for_each_online_cpu(i) {
  197. i_sync_info = &per_cpu(sync_info, i);
  198. ret = cpufreq_get_policy(&policy, i);
  199. if (ret)
  200. continue;
  201. if (policy.cur >= input_boost_freq)
  202. continue;
  203. cancel_delayed_work_sync(&i_sync_info->input_boost_rem);
  204. i_sync_info->input_boost_min = input_boost_freq;
  205. cpufreq_update_policy(i);
  206. queue_delayed_work_on(i_sync_info->cpu, cpu_boost_wq,
  207. &i_sync_info->input_boost_rem,
  208. msecs_to_jiffies(input_boost_ms));
  209. }
  210. put_online_cpus();
  211. }
  212. static void cpuboost_input_event(struct input_handle *handle,
  213. unsigned int type, unsigned int code, int value)
  214. {
  215. u64 now;
  216. if (!input_boost_freq)
  217. return;
  218. now = ktime_to_us(ktime_get());
  219. if (now - last_input_time < MIN_INPUT_INTERVAL)
  220. return;
  221. if (work_pending(&input_boost_work))
  222. return;
  223. queue_work(cpu_boost_wq, &input_boost_work);
  224. last_input_time = ktime_to_us(ktime_get());
  225. }
  226. static int cpuboost_input_connect(struct input_handler *handler,
  227. struct input_dev *dev, const struct input_device_id *id)
  228. {
  229. struct input_handle *handle;
  230. int error;
  231. handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
  232. if (!handle)
  233. return -ENOMEM;
  234. handle->dev = dev;
  235. handle->handler = handler;
  236. handle->name = "cpufreq";
  237. error = input_register_handle(handle);
  238. if (error)
  239. goto err2;
  240. error = input_open_device(handle);
  241. if (error)
  242. goto err1;
  243. return 0;
  244. err1:
  245. input_unregister_handle(handle);
  246. err2:
  247. kfree(handle);
  248. return error;
  249. }
  250. static void cpuboost_input_disconnect(struct input_handle *handle)
  251. {
  252. input_close_device(handle);
  253. input_unregister_handle(handle);
  254. kfree(handle);
  255. }
  256. static const struct input_device_id cpuboost_ids[] = {
  257. /* multi-touch touchscreen */
  258. {
  259. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  260. INPUT_DEVICE_ID_MATCH_ABSBIT,
  261. .evbit = { BIT_MASK(EV_ABS) },
  262. .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
  263. BIT_MASK(ABS_MT_POSITION_X) |
  264. BIT_MASK(ABS_MT_POSITION_Y) },
  265. },
  266. /* touchpad */
  267. {
  268. .flags = INPUT_DEVICE_ID_MATCH_KEYBIT |
  269. INPUT_DEVICE_ID_MATCH_ABSBIT,
  270. .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
  271. .absbit = { [BIT_WORD(ABS_X)] =
  272. BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) },
  273. },
  274. /* Keypad */
  275. {
  276. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  277. .evbit = { BIT_MASK(EV_KEY) },
  278. },
  279. { },
  280. };
  281. static struct input_handler cpuboost_input_handler = {
  282. .event = cpuboost_input_event,
  283. .connect = cpuboost_input_connect,
  284. .disconnect = cpuboost_input_disconnect,
  285. .name = "cpu-boost",
  286. .id_table = cpuboost_ids,
  287. };
  288. static int cpu_boost_init(void)
  289. {
  290. int cpu, ret;
  291. struct cpu_sync *s;
  292. cpu_boost_wq = alloc_workqueue("cpuboost_wq", WQ_HIGHPRI, 0);
  293. if (!cpu_boost_wq)
  294. return -EFAULT;
  295. INIT_WORK(&input_boost_work, do_input_boost);
  296. for_each_possible_cpu(cpu) {
  297. s = &per_cpu(sync_info, cpu);
  298. s->cpu = cpu;
  299. init_waitqueue_head(&s->sync_wq);
  300. atomic_set(&s->being_woken, 0);
  301. spin_lock_init(&s->lock);
  302. INIT_DELAYED_WORK(&s->boost_rem, do_boost_rem);
  303. INIT_DELAYED_WORK(&s->input_boost_rem, do_input_boost_rem);
  304. s->thread = kthread_run(boost_mig_sync_thread, (void *)cpu,
  305. "boost_sync/%d", cpu);
  306. set_cpus_allowed(s->thread, *cpumask_of(cpu));
  307. }
  308. cpufreq_register_notifier(&boost_adjust_nb, CPUFREQ_POLICY_NOTIFIER);
  309. atomic_notifier_chain_register(&migration_notifier_head,
  310. &boost_migration_nb);
  311. ret = input_register_handler(&cpuboost_input_handler);
  312. return 0;
  313. }
  314. late_initcall(cpu_boost_init);