dynamic_boost.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * drivers/dynamic_boost/dynamic_boost.c
  3. *
  4. * Copyright (C) 2010 MediaTek <www.MediaTek.com>
  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. *
  16. *
  17. */
  18. #include <linux/cpu.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/cpufreq.h>
  21. #include <linux/delay.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/mutex.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/rwsem.h>
  27. #include <linux/sched.h>
  28. #include <linux/sched/rt.h>
  29. #include <linux/sysfs.h>
  30. #include <linux/kthread.h>
  31. #include <linux/slab.h>
  32. #include <linux/spinlock.h>
  33. #include <mt_hotplug_strategy.h>
  34. #include <mt_hotplug_strategy_internal.h>
  35. #include "mt_cpufreq.h"
  36. #include <linux/input.h>
  37. #include <linux/workqueue.h>
  38. #include "dynamic_boost.h"
  39. struct boost_state {
  40. int active;
  41. struct delayed_work work;
  42. };
  43. struct dynamic_boost {
  44. spinlock_t boost_lock;
  45. int last_req_mode;
  46. wait_queue_head_t wq;
  47. struct task_struct *thread;
  48. struct boost_state state[PRIO_DEFAULT];
  49. atomic_t event;
  50. };
  51. static struct dynamic_boost dboost;
  52. struct dboost_input_handle {
  53. struct input_handle handle;
  54. int duration;
  55. int prio_mode;
  56. };
  57. #define MAX_CORES_NUMBER nr_cpu_ids
  58. #define MAX_FREQUENCY 1
  59. #define MAX_DURATION 10000
  60. static ssize_t dynamic_boost_show(struct device *dev, struct device_attribute *attr, char *buf);
  61. static ssize_t dynamic_boost_store(struct device *dev, struct device_attribute *attr, const char *buf,
  62. size_t n);
  63. static struct device_attribute dynamic_boost_attr = __ATTR(dynamic_boost, 0750,
  64. dynamic_boost_show, dynamic_boost_store);
  65. static void dboost_disable_work(struct work_struct *work)
  66. {
  67. unsigned long flags;
  68. struct boost_state *state = container_of(work, struct boost_state, work.work);
  69. spin_lock_irqsave(&dboost.boost_lock, flags);
  70. if (state->active > 0)
  71. state->active--;
  72. spin_unlock_irqrestore(&dboost.boost_lock, flags);
  73. atomic_inc(&dboost.event);
  74. wake_up(&dboost.wq);
  75. }
  76. /*
  77. * Set up performance boost mode with requested duration
  78. * @duration: How long the user want this mode to keep. Specify with ms.
  79. * @mode: Request mode.
  80. */
  81. int set_dynamic_boost(int duration, int prio_mode)
  82. {
  83. unsigned long flags;
  84. struct boost_state *state;
  85. if (duration > MAX_DURATION ||
  86. prio_mode < 0 || prio_mode > PRIO_DEFAULT)
  87. return -EINVAL;
  88. if (prio_mode == PRIO_DEFAULT || !duration)
  89. return 0;
  90. spin_lock_irqsave(&dboost.boost_lock, flags);
  91. state = &dboost.state[prio_mode];
  92. if (duration == ON)
  93. state->active++;
  94. else if (duration == OFF)
  95. state->active--;
  96. else if (!mod_delayed_work(system_wq, &state->work, msecs_to_jiffies(duration)))
  97. state->active++;
  98. spin_unlock_irqrestore(&dboost.boost_lock, flags);
  99. atomic_inc(&dboost.event);
  100. wake_up(&dboost.wq);
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(set_dynamic_boost);
  104. static int dboost_dvfs_hotplug_thread(void *ptr)
  105. {
  106. int max_freq, cores_to_set_b, cores_to_set_l;
  107. unsigned long flags;
  108. set_user_nice(current, -10);
  109. while (!kthread_should_stop()) {
  110. int i, set_mode = PRIO_DEFAULT;
  111. spin_lock_irqsave(&dboost.boost_lock, flags);
  112. for (i = PRIO_DEFAULT - 1; i >= 0; i--) {
  113. if (dboost.state[i].active) {
  114. set_mode = i;
  115. break;
  116. }
  117. }
  118. spin_unlock_irqrestore(&dboost.boost_lock, flags);
  119. switch (set_mode) {
  120. case PRIO_MAX_CORES_MAX_FREQ:
  121. cores_to_set_b = num_possible_big_cpus();
  122. cores_to_set_l = num_possible_little_cpus();
  123. max_freq = MAX_FREQUENCY;
  124. break;
  125. case PRIO_MAX_CORES:
  126. cores_to_set_b = num_possible_big_cpus();
  127. cores_to_set_l = num_possible_little_cpus();
  128. max_freq = 0;
  129. break;
  130. case PRIO_FOUR_BIGS_MAX_FREQ:
  131. cores_to_set_b = 4;
  132. cores_to_set_l = 0;
  133. max_freq = MAX_FREQUENCY;
  134. break;
  135. case PRIO_FOUR_BIGS:
  136. cores_to_set_b = 4;
  137. cores_to_set_l = 0;
  138. max_freq = 0;
  139. break;
  140. case PRIO_TWO_BIGS_TWO_LITTLES_MAX_FREQ:
  141. cores_to_set_b = 2;
  142. cores_to_set_l = 2;
  143. max_freq = MAX_FREQUENCY;
  144. break;
  145. case PRIO_TWO_BIGS_TWO_LITTLES:
  146. cores_to_set_b = 2;
  147. cores_to_set_l = 2;
  148. max_freq = 0;
  149. break;
  150. case PRIO_FOUR_LITTLES_MAX_FREQ:
  151. cores_to_set_b = 0;
  152. cores_to_set_l = 4;
  153. max_freq = MAX_FREQUENCY;
  154. break;
  155. case PRIO_FOUR_LITTLES:
  156. cores_to_set_b = 0;
  157. cores_to_set_l = 4;
  158. max_freq = 0;
  159. break;
  160. case PRIO_TWO_BIGS_MAX_FREQ:
  161. cores_to_set_b = 2;
  162. cores_to_set_l = 0;
  163. max_freq = MAX_FREQUENCY;
  164. break;
  165. case PRIO_TWO_BIGS:
  166. cores_to_set_b = 2;
  167. cores_to_set_l = 0;
  168. max_freq = 0;
  169. break;
  170. case PRIO_ONE_BIG_ONE_LITTLE_MAX_FREQ:
  171. cores_to_set_b = 1;
  172. cores_to_set_l = 1;
  173. max_freq = MAX_FREQUENCY;
  174. break;
  175. case PRIO_ONE_BIG_ONE_LITTLE:
  176. cores_to_set_b = 1;
  177. cores_to_set_l = 1;
  178. max_freq = 0;
  179. break;
  180. case PRIO_ONE_BIG_MAX_FREQ:
  181. cores_to_set_b = 1;
  182. cores_to_set_l = 0;
  183. max_freq = MAX_FREQUENCY;
  184. break;
  185. case PRIO_ONE_BIG:
  186. cores_to_set_b = 1;
  187. cores_to_set_l = 0;
  188. max_freq = 0;
  189. break;
  190. case PRIO_TWO_LITTLES_MAX_FREQ:
  191. cores_to_set_b = 0;
  192. cores_to_set_l = 2;
  193. max_freq = MAX_FREQUENCY;
  194. break;
  195. case PRIO_TWO_LITTLES:
  196. cores_to_set_b = 0;
  197. cores_to_set_l = 2;
  198. max_freq = 0;
  199. break;
  200. case PRIO_RESET:
  201. for (i = PRIO_DEFAULT - 1; i >= 0; i--)
  202. dboost.state[i].active = 0;
  203. default:
  204. cores_to_set_b = 0;
  205. cores_to_set_l = 0;
  206. max_freq = 0;
  207. break;
  208. }
  209. interactive_boost_cpu(max_freq);
  210. if (!cores_to_set_l)
  211. cores_to_set_l = 1;
  212. hps_set_cpu_num_base(BASE_PERF_SERV, cores_to_set_l, cores_to_set_b);
  213. /* printk("dynamic boost: Mode=%d cpu_min_num_big=%d cpu_min_num_little=%d\n",
  214. set_mode, cores_to_set_b, cores_to_set_l);*/
  215. dboost.last_req_mode = set_mode;
  216. while (!atomic_read(&dboost.event))
  217. wait_event(dboost.wq, atomic_read(&dboost.event));
  218. atomic_dec(&dboost.event);
  219. }
  220. return 0;
  221. }
  222. static ssize_t dynamic_boost_show(struct device *dev, struct device_attribute *attr, char *buf)
  223. {
  224. int i;
  225. i = snprintf(buf, PAGE_SIZE, "Mode: %d\n", dboost.last_req_mode);
  226. return i;
  227. }
  228. static ssize_t dynamic_boost_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t n)
  229. {
  230. int now_req_duration = 0;
  231. int now_req_mode = 0;
  232. if ((n == 0) || (buf == NULL))
  233. return -EINVAL;
  234. if (sscanf(buf, "%d %d", &now_req_duration, &now_req_mode) != 2)
  235. return -EINVAL;
  236. if (now_req_mode < 0)
  237. return -EINVAL;
  238. set_dynamic_boost(now_req_duration, now_req_mode);
  239. return n;
  240. }
  241. static int dynamic_boost_probe(struct platform_device *dev)
  242. {
  243. int ret_device_file = 0;
  244. ret_device_file = device_create_file(&(dev->dev), &dynamic_boost_attr);
  245. return ret_device_file;
  246. }
  247. struct platform_device dynamic_boost_device = {
  248. .name = "dynamic_boost",
  249. .id = -1,
  250. };
  251. int dboost_suspend(struct device *dev)
  252. {
  253. unsigned long flags;
  254. int i;
  255. /* cancel all boost jobs if system suspend is requested */
  256. for (i = 0; i < ARRAY_SIZE(dboost.state); ++i) {
  257. cancel_delayed_work_sync(&dboost.state[i].work);
  258. spin_lock_irqsave(&dboost.boost_lock, flags);
  259. dboost.state[i].active = 0;
  260. spin_unlock_irqrestore(&dboost.boost_lock, flags);
  261. }
  262. atomic_inc(&dboost.event);
  263. wake_up(&dboost.wq);
  264. return 0;
  265. }
  266. int dboost_resume(struct device *dev)
  267. {
  268. return 0;
  269. }
  270. static struct platform_driver dynamic_boost_driver = {
  271. .probe = dynamic_boost_probe,
  272. .driver = {
  273. .name = "dynamic_boost",
  274. .pm = &(const struct dev_pm_ops){
  275. .suspend = dboost_suspend,
  276. .resume = dboost_resume,
  277. },
  278. },
  279. };
  280. #ifdef CONFIG_TOUCH_BOOST
  281. /******************************************************************************
  282. * Handle touch boost *
  283. ******************************************************************************/
  284. static void dboost_input_event(struct input_handle *handle, unsigned int type,
  285. unsigned int code, int value)
  286. {
  287. struct dboost_input_handle *in = container_of(handle, struct dboost_input_handle, handle);
  288. if ((type == EV_KEY && code == BTN_TOUCH) ||
  289. (type == EV_ABS && code == ABS_MT_TRACKING_ID && value != 0))
  290. set_dynamic_boost(in->duration, in->prio_mode);
  291. }
  292. static int dboost_input_connect(struct input_handler *handler,
  293. struct input_dev *dev, const struct input_device_id *id)
  294. {
  295. struct dboost_input_handle *in;
  296. int error;
  297. in = kzalloc(sizeof(struct dboost_input_handle), GFP_KERNEL);
  298. if (!in)
  299. return -ENOMEM;
  300. in->handle.dev = dev;
  301. in->handle.handler = handler;
  302. in->handle.name = "dynamic_boost";
  303. /* TODO: the following parameters should be configured through platform data */
  304. in->prio_mode = PRIO_TWO_BIGS_TWO_LITTLES_MAX_FREQ;
  305. in->duration = 150;
  306. error = input_register_handle(&in->handle);
  307. if (error)
  308. goto err2;
  309. error = input_open_device(&in->handle);
  310. if (error)
  311. goto err1;
  312. return 0;
  313. err1:
  314. input_unregister_handle(&in->handle);
  315. err2:
  316. kfree(&in->handle);
  317. return error;
  318. }
  319. static void dboost_input_disconnect(struct input_handle *handle)
  320. {
  321. struct dboost_input_handle *in = container_of(handle, struct dboost_input_handle, handle);
  322. input_close_device(handle);
  323. input_unregister_handle(handle);
  324. kfree(in);
  325. }
  326. static const struct input_device_id dboost_ids[] = {
  327. { .driver_info = 1 },
  328. { },
  329. };
  330. static struct input_handler dboost_input_handler = {
  331. .event = dboost_input_event,
  332. .connect = dboost_input_connect,
  333. .disconnect = dboost_input_disconnect,
  334. .name = "touch_boost_ond",
  335. .id_table = dboost_ids,
  336. };
  337. #endif /* CONFIG_TOUCH_BOOST */
  338. static int __init dynamic_boost_init(void)
  339. {
  340. int ret = 0, i;
  341. ret = platform_device_register(&dynamic_boost_device);
  342. if (ret)
  343. return ret;
  344. ret = platform_driver_register(&dynamic_boost_driver);
  345. if (ret)
  346. return ret;
  347. spin_lock_init(&dboost.boost_lock);
  348. dboost.last_req_mode = PRIO_DEFAULT;
  349. atomic_set(&dboost.event, 0);
  350. for (i = 0; i < ARRAY_SIZE(dboost.state); ++i) {
  351. INIT_DELAYED_WORK(&dboost.state[i].work, dboost_disable_work);
  352. #if 0 /* TODO: should 2 littles default always on? */
  353. if (i == PRIO_TWO_LITTLES)
  354. dboost.state[i].active = 1;
  355. else
  356. #endif
  357. dboost.state[i].active = 0;
  358. }
  359. init_waitqueue_head(&dboost.wq);
  360. dboost.thread = kthread_run(dboost_dvfs_hotplug_thread, &dboost, "dynamic_boost");
  361. if (IS_ERR(dboost.thread))
  362. return -EINVAL;
  363. #ifdef CONFIG_TOUCH_BOOST
  364. ret = input_register_handler(&dboost_input_handler);
  365. if (ret)
  366. return ret;
  367. #endif
  368. return 0;
  369. }
  370. late_initcall(dynamic_boost_init);
  371. static void __exit dynamic_boost_exit(void)
  372. {
  373. #ifdef CONFIG_TOUCH_BOOST
  374. input_unregister_handler(&dboost_input_handler);
  375. #endif
  376. kthread_stop(dboost.thread);
  377. }
  378. module_exit(dynamic_boost_exit);
  379. MODULE_AUTHOR("MediaTek Inc.");
  380. MODULE_DESCRIPTION("'dynamic boost' - provide performance boost");
  381. MODULE_LICENSE("GPL");