mt_cpu_ss.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/sched.h>
  10. #include <linux/init.h>
  11. #include <linux/delay.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/kthread.h>
  15. #include <linux/hrtimer.h>
  16. #include <linux/ktime.h>
  17. #include <linux/jiffies.h>
  18. #include <asm/io.h>
  19. #include <asm/uaccess.h>
  20. #include <linux/seq_file.h>
  21. #include "sync_write.h"
  22. #include "mt_cpufreq.h"
  23. #include "mach/mt_clkmgr.h"
  24. static struct hrtimer mt_cpu_ss_timer;
  25. struct task_struct *mt_cpu_ss_thread = NULL;
  26. static DECLARE_WAIT_QUEUE_HEAD(mt_cpu_ss_timer_waiter);
  27. static int mt_cpu_ss_period_s;
  28. static int mt_cpu_ss_period_ns = 100;
  29. static int mt_cpu_ss_timer_flag;
  30. static bool mt_cpu_ss_debug_mode;
  31. static bool mt_cpu_ss_period_mode;
  32. enum hrtimer_restart mt_cpu_ss_timer_func(struct hrtimer *timer)
  33. {
  34. if (mt_cpu_ss_debug_mode)
  35. pr_debug("[%s]: enter timer function\n", __func__);
  36. mt_cpu_ss_timer_flag = 1;
  37. wake_up_interruptible(&mt_cpu_ss_timer_waiter);
  38. return HRTIMER_NORESTART;
  39. }
  40. int mt_cpu_ss_thread_handler(void *unused)
  41. {
  42. unsigned int flag = 0;
  43. do {
  44. ktime_t ktime = ktime_set(mt_cpu_ss_period_s, mt_cpu_ss_period_ns);
  45. wait_event_interruptible(mt_cpu_ss_timer_waiter, mt_cpu_ss_timer_flag != 0);
  46. mt_cpu_ss_timer_flag = 0;
  47. if (!flag) {
  48. mt_cpufreq_set_cpu_clk_src(0, TOP_CKMUXSEL_CLKSQ);
  49. flag = 1;
  50. } else {
  51. mt_cpufreq_set_cpu_clk_src(0, TOP_CKMUXSEL_ARMPLL);
  52. flag = 0;
  53. }
  54. if (mt_cpu_ss_debug_mode)
  55. pr_debug("[%s]: TOP_CKMUXSEL = 0x%x\n", __func__, __raw_readl(TOP_CKMUXSEL));
  56. hrtimer_start(&mt_cpu_ss_timer, ktime, HRTIMER_MODE_REL);
  57. } while (!kthread_should_stop());
  58. return 0;
  59. }
  60. #ifdef CONFIG_PROC_FS
  61. /*
  62. * PROC
  63. */
  64. static char *_copy_from_user_for_proc(const char __user *buffer, size_t count)
  65. {
  66. char *buf = (char *)__get_free_page(GFP_USER);
  67. if (!buf)
  68. return NULL;
  69. if (count >= PAGE_SIZE)
  70. goto out;
  71. if (copy_from_user(buf, buffer, count))
  72. goto out;
  73. buf[count] = '\0';
  74. return buf;
  75. out:
  76. free_page((unsigned long)buf);
  77. return NULL;
  78. }
  79. /* cpu_ss_debug_mode */
  80. static int cpu_ss_debug_mode_proc_show(struct seq_file *m, void *v)
  81. {
  82. seq_printf(m, "%s", (mt_cpu_ss_debug_mode) ? "enable" : "disable");
  83. return 0;
  84. }
  85. static ssize_t cpu_ss_debug_mode_proc_write(struct file *file, const char __user *buffer,
  86. size_t count, loff_t *pos)
  87. {
  88. char mode[20];
  89. char *buf = _copy_from_user_for_proc(buffer, count);
  90. if (!buf)
  91. return -EINVAL;
  92. if (count >= sizeof(mode)) {
  93. pr_err("[%s]: bad argument!! input length is over buffer size\n", __func__);
  94. return -EINVAL;
  95. }
  96. if (sscanf(buf, "%19s", mode) == 1) {
  97. if (!strcmp(mode, "enable")) {
  98. pr_debug("[%s]: %s cpu speed switch debug mode\n", mode, __func__);
  99. mt_cpu_ss_debug_mode = true;
  100. } else if (!strcmp(mode, "disable")) {
  101. pr_debug("[%s]: %s cpu speed switch debug mode\n", mode, __func__);
  102. mt_cpu_ss_debug_mode = false;
  103. } else
  104. pr_err("[%s]: bad argument!! should be \"enable\" or \"disable\"\n",
  105. __func__);
  106. } else
  107. pr_err("[%s]: bad argument!! should be \"enable\" or \"disable\"\n", __func__);
  108. free_page((unsigned long)buf);
  109. return count;
  110. }
  111. /* cpu_ss_period_mode */
  112. static int cpu_ss_period_mode_proc_show(struct seq_file *m, void *v)
  113. {
  114. seq_printf(m, "%s", (mt_cpu_ss_period_mode) ? "enable" : "disable");
  115. return 0;
  116. }
  117. static ssize_t cpu_ss_period_mode_proc_write(struct file *file, const char __user *buffer,
  118. size_t count, loff_t *pos)
  119. {
  120. char mode[20];
  121. ktime_t ktime = ktime_set(mt_cpu_ss_period_s, mt_cpu_ss_period_ns);
  122. char *buf = _copy_from_user_for_proc(buffer, count);
  123. if (!buf)
  124. return -EINVAL;
  125. if (count >= sizeof(mode)) {
  126. pr_err("[%s]: bad argument!! input length is over buffer size\n", __func__);
  127. return -EINVAL;
  128. }
  129. if (sscanf(buf, "%19s", mode) == 1) {
  130. if (!strcmp(mode, "enable")) {
  131. pr_debug("[%s]: %s cpu speed switch period mode\n", mode, __func__);
  132. mt_cpu_ss_period_mode = true;
  133. mt_cpu_ss_thread =
  134. kthread_run(mt_cpu_ss_thread_handler, 0, "cpu speed switch");
  135. if (IS_ERR(mt_cpu_ss_thread))
  136. pr_err("[%s]: failed to create cpu speed switch thread\n",
  137. __func__);
  138. hrtimer_start(&mt_cpu_ss_timer, ktime, HRTIMER_MODE_REL);
  139. } else if (!strcmp(mode, "disable")) {
  140. pr_debug("[%s]: %s cpu speed switch period mode\n", mode, __func__);
  141. mt_cpu_ss_period_mode = false;
  142. kthread_stop(mt_cpu_ss_thread);
  143. mt_cpufreq_set_cpu_clk_src(0, TOP_CKMUXSEL_ARMPLL);
  144. hrtimer_cancel(&mt_cpu_ss_timer);
  145. } else
  146. pr_err("[%s]: bad argument!! should be \"enable\" or \"disable\"\n",
  147. __func__);
  148. } else
  149. pr_err("[%s]: bad argument!! should be \"enable\" or \"disable\"\n", __func__);
  150. free_page((unsigned long)buf);
  151. return count;
  152. }
  153. /* cpu_ss_period */
  154. static int cpu_ss_period_proc_show(struct seq_file *m, void *v)
  155. {
  156. seq_printf(m, "%d (s) %d (ns)\n", mt_cpu_ss_period_s, mt_cpu_ss_period_ns);
  157. return 0;
  158. }
  159. static ssize_t cpu_ss_period_proc_write(struct file *file, const char __user *buffer, size_t count,
  160. loff_t *pos)
  161. {
  162. int s = 0, ns = 0;
  163. char *buf = _copy_from_user_for_proc(buffer, count);
  164. if (!buf)
  165. return -EINVAL;
  166. if (sscanf(buf, "%d %d", &s, &ns) == 2) {
  167. pr_debug("[%s]: set cpu speed switch period = %d (s), %d (ns)\n", __func__, s, ns);
  168. mt_cpu_ss_period_s = s;
  169. mt_cpu_ss_period_ns = ns;
  170. } else
  171. pr_err("[%s]: bad argument!! should be \"[s]\" or \"[ns]\"\n", __func__);
  172. return count;
  173. }
  174. /* cpu_ss_mode */
  175. static int cpu_ss_mode_proc_show(struct seq_file *m, void *v)
  176. {
  177. char *cpu_ss_mode;
  178. switch (mt_cpufreq_get_cpu_clk_src(0)) {
  179. case TOP_CKMUXSEL_CLKSQ:
  180. cpu_ss_mode = "CLKSQ";
  181. break;
  182. case TOP_CKMUXSEL_ARMPLL:
  183. cpu_ss_mode = "ARMPLL";
  184. break;
  185. case TOP_CKMUXSEL_MAINPLL:
  186. cpu_ss_mode = "MAINPLL";
  187. break;
  188. default:
  189. cpu_ss_mode = "UNKNOWN";
  190. break;
  191. }
  192. seq_printf(m, "CPU clock source is %s\n", cpu_ss_mode);
  193. return 0;
  194. }
  195. static ssize_t cpu_ss_mode_proc_write(struct file *file, const char __user *buffer, size_t count,
  196. loff_t *pos)
  197. {
  198. int mode = 0;
  199. char *buf = _copy_from_user_for_proc(buffer, count);
  200. if (!buf)
  201. return -EINVAL;
  202. if (!kstrtoint(buf, 10, &mode)) {
  203. if (mode) {
  204. pr_debug("[%s]: config cpu speed switch mode = ARMPLL\n", __func__);
  205. mt_cpufreq_set_cpu_clk_src(0, TOP_CKMUXSEL_ARMPLL);
  206. } else {
  207. pr_debug("[%s]: config cpu speed switch mode = CLKSQ\n", __func__);
  208. mt_cpufreq_set_cpu_clk_src(0, TOP_CKMUXSEL_CLKSQ);
  209. }
  210. } else
  211. pr_err("[%s]: bad argument!! should be \"1\" or \"0\"\n", __func__);
  212. return count;
  213. }
  214. #define PROC_FOPS_RW(name) \
  215. static int name ## _proc_open(struct inode *inode, struct file *file) \
  216. { \
  217. return single_open(file, name ## _proc_show, NULL); \
  218. } \
  219. static const struct file_operations name ## _proc_fops = { \
  220. .owner = THIS_MODULE, \
  221. .open = name ## _proc_open, \
  222. .read = seq_read, \
  223. .llseek = seq_lseek, \
  224. .release = single_release, \
  225. .write = name ## _proc_write, \
  226. }
  227. #define PROC_FOPS_RO(name) \
  228. static int name ## _proc_open(struct inode *inode, struct file *file) \
  229. { \
  230. return single_open(file, name ## _proc_show, NULL); \
  231. } \
  232. static const struct file_operations name ## _proc_fops = { \
  233. .owner = THIS_MODULE, \
  234. .open = name ## _proc_open, \
  235. .read = seq_read, \
  236. .llseek = seq_lseek, \
  237. .release = single_release, \
  238. }
  239. #define PROC_ENTRY(name) {__stringify(name), &name ## _proc_fops}
  240. PROC_FOPS_RW(cpu_ss_debug_mode);
  241. PROC_FOPS_RW(cpu_ss_period_mode);
  242. PROC_FOPS_RW(cpu_ss_period);
  243. PROC_FOPS_RW(cpu_ss_mode);
  244. static int _create_procfs(void)
  245. {
  246. struct proc_dir_entry *dir = NULL;
  247. int i;
  248. const struct {
  249. const char *name;
  250. const struct file_operations *fops;
  251. } entries[] = {
  252. PROC_ENTRY(cpu_ss_debug_mode),
  253. PROC_ENTRY(cpu_ss_period_mode),
  254. PROC_ENTRY(cpu_ss_period), PROC_ENTRY(cpu_ss_mode),};
  255. dir = proc_mkdir("cpu_ss", NULL);
  256. if (!dir) {
  257. pr_err("fail to create /proc/cpu_ss @ %s()\n", __func__);
  258. return -ENOMEM;
  259. }
  260. for (i = 0; i < ARRAY_SIZE(entries); i++) {
  261. if (!proc_create
  262. (entries[i].name, S_IRUGO | S_IWUSR | S_IWGRP, dir, entries[i].fops))
  263. pr_err("%s(), create /proc/cpu_ss/%s failed\n", __func__, entries[i].name);
  264. }
  265. return 0;
  266. }
  267. #endif /* CONFIG_PROC_FS */
  268. /*********************************
  269. * cpu speed stress initialization
  270. **********************************/
  271. static int __init mt_cpu_ss_init(void)
  272. {
  273. hrtimer_init(&mt_cpu_ss_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  274. mt_cpu_ss_timer.function = mt_cpu_ss_timer_func;
  275. return _create_procfs();
  276. }
  277. static void __exit mt_cpu_ss_exit(void)
  278. {
  279. }
  280. module_init(mt_cpu_ss_init);
  281. module_exit(mt_cpu_ss_exit);
  282. MODULE_DESCRIPTION("MediaTek CPU Speed Stress driver");
  283. MODULE_LICENSE("GPL");