commandProcessing.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * linux/arch/arm/kernel/arch_timer.c
  3. *
  4. * Copyright (C) 2011 ARM Ltd.
  5. * All Rights Reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/delay.h>
  14. #include <linux/device.h>
  15. #include <linux/smp.h>
  16. #include <linux/cpu.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/io.h>
  22. #include <asm/cputype.h>
  23. #include <asm/delay.h>
  24. #include <asm/localtimer.h>
  25. #include <asm/arch_timer.h>
  26. #include <asm/system_info.h>
  27. #include <asm/sched_clock.h>
  28. static unsigned long arch_timer_rate;
  29. enum ppi_nr {
  30. PHYS_SECURE_PPI,
  31. PHYS_NONSECURE_PPI,
  32. VIRT_PPI,
  33. HYP_PPI,
  34. MAX_TIMER_PPI
  35. };
  36. static int arch_timer_ppi[MAX_TIMER_PPI];
  37. static struct clock_event_device __percpu **arch_timer_evt;
  38. static struct delay_timer arch_delay_timer;
  39. static bool arch_timer_use_virtual = true;
  40. /*
  41. * Architected system timer support.
  42. */
  43. #define ARCH_TIMER_CTRL_ENABLE (1 << 0)
  44. #define ARCH_TIMER_CTRL_IT_MASK (1 << 1)
  45. #define ARCH_TIMER_CTRL_IT_STAT (1 << 2)
  46. #define ARCH_TIMER_REG_CTRL 0
  47. #define ARCH_TIMER_REG_FREQ 1
  48. #define ARCH_TIMER_REG_TVAL 2
  49. #define ARCH_TIMER_PHYS_ACCESS 0
  50. #define ARCH_TIMER_VIRT_ACCESS 1
  51. /*
  52. * These register accessors are marked inline so the compiler can
  53. * nicely work out which register we want, and chuck away the rest of
  54. * the code. At least it does so with a recent GCC (4.6.3).
  55. */
  56. static inline void arch_timer_reg_write(const int access, const int reg, u32 val)
  57. {
  58. if (access == ARCH_TIMER_PHYS_ACCESS) {
  59. switch (reg) {
  60. case ARCH_TIMER_REG_CTRL:
  61. asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val));
  62. break;
  63. case ARCH_TIMER_REG_TVAL:
  64. asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val));
  65. break;
  66. }
  67. }
  68. if (access == ARCH_TIMER_VIRT_ACCESS) {
  69. switch (reg) {
  70. case ARCH_TIMER_REG_CTRL:
  71. asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" (val));
  72. break;
  73. case ARCH_TIMER_REG_TVAL:
  74. asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" (val));
  75. break;
  76. }
  77. }
  78. isb();
  79. }
  80. static inline u32 arch_timer_reg_read(const int access, const int reg)
  81. {
  82. u32 val = 0;
  83. if (access == ARCH_TIMER_PHYS_ACCESS) {
  84. switch (reg) {
  85. case ARCH_TIMER_REG_CTRL:
  86. asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
  87. break;
  88. case ARCH_TIMER_REG_TVAL:
  89. asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val));
  90. break;
  91. case ARCH_TIMER_REG_FREQ:
  92. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val));
  93. break;
  94. }
  95. }
  96. if (access == ARCH_TIMER_VIRT_ACCESS) {
  97. switch (reg) {
  98. case ARCH_TIMER_REG_CTRL:
  99. asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val));
  100. break;
  101. case ARCH_TIMER_REG_TVAL:
  102. asm volatile("mrc p15, 0, %0, c14, c3, 0" : "=r" (val));
  103. break;
  104. }
  105. }
  106. return val;
  107. }
  108. static inline cycle_t arch_timer_counter_read(const int access)
  109. {
  110. cycle_t cval = 0;
  111. if (access == ARCH_TIMER_PHYS_ACCESS)
  112. asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
  113. if (access == ARCH_TIMER_VIRT_ACCESS)
  114. asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval));
  115. return cval;
  116. }
  117. static inline cycle_t arch_counter_get_cntpct(void)
  118. {
  119. return arch_timer_counter_read(ARCH_TIMER_PHYS_ACCESS);
  120. }
  121. static inline cycle_t arch_counter_get_cntvct(void)
  122. {
  123. return arch_timer_counter_read(ARCH_TIMER_VIRT_ACCESS);
  124. }
  125. static irqreturn_t inline timer_handler(const int access,
  126. struct clock_event_device *evt)
  127. {
  128. unsigned long ctrl;
  129. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  130. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  131. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  132. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  133. evt->event_handler(evt);
  134. return IRQ_HANDLED;
  135. }
  136. return IRQ_NONE;
  137. }
  138. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  139. {
  140. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  141. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  142. }
  143. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  144. {
  145. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  146. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  147. }
  148. static inline void timer_set_mode(const int access, int mode)
  149. {
  150. unsigned long ctrl;
  151. switch (mode) {
  152. case CLOCK_EVT_MODE_UNUSED:
  153. case CLOCK_EVT_MODE_SHUTDOWN:
  154. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  155. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  156. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. static void arch_timer_set_mode_virt(enum clock_event_mode mode,
  163. struct clock_event_device *clk)
  164. {
  165. timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode);
  166. }
  167. static void arch_timer_set_mode_phys(enum clock_event_mode mode,
  168. struct clock_event_device *clk)
  169. {
  170. timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode);
  171. }
  172. static inline void set_next_event(const int access, unsigned long evt)
  173. {
  174. unsigned long ctrl;
  175. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  176. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  177. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  178. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt);
  179. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  180. }
  181. static int arch_timer_set_next_event_virt(unsigned long evt,
  182. struct clock_event_device *unused)
  183. {
  184. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt);
  185. return 0;
  186. }
  187. static int arch_timer_set_next_event_phys(unsigned long evt,
  188. struct clock_event_device *unused)
  189. {
  190. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt);
  191. return 0;
  192. }
  193. static int __cpuinit arch_timer_setup(struct clock_event_device *clk)
  194. {
  195. clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP;
  196. clk->name = "arch_sys_timer";
  197. clk->rating = 450;
  198. if (arch_timer_use_virtual) {
  199. clk->irq = arch_timer_ppi[VIRT_PPI];
  200. clk->set_mode = arch_timer_set_mode_virt;
  201. clk->set_next_event = arch_timer_set_next_event_virt;
  202. } else {
  203. clk->irq = arch_timer_ppi[PHYS_SECURE_PPI];
  204. clk->set_mode = arch_timer_set_mode_phys;
  205. clk->set_next_event = arch_timer_set_next_event_phys;
  206. }
  207. clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, NULL);
  208. clockevents_config_and_register(clk, arch_timer_rate,
  209. 0xf, 0x7fffffff);
  210. *__this_cpu_ptr(arch_timer_evt) = clk;
  211. if (arch_timer_use_virtual)
  212. enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
  213. else {
  214. enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
  215. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  216. enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
  217. }
  218. return 0;
  219. }
  220. /* Is the optional system timer available? */
  221. static int local_timer_is_architected(void)
  222. {
  223. return (cpu_architecture() >= CPU_ARCH_ARMv7) &&
  224. ((read_cpuid_ext(CPUID_EXT_PFR1) >> 16) & 0xf) == 1;
  225. }
  226. static int arch_timer_available(void)
  227. {
  228. unsigned long freq;
  229. if (!local_timer_is_architected())
  230. return -ENXIO;
  231. if (arch_timer_rate == 0) {
  232. freq = arch_timer_reg_read(ARCH_TIMER_PHYS_ACCESS,
  233. ARCH_TIMER_REG_FREQ);
  234. /* Check the timer frequency. */
  235. if (freq == 0) {
  236. pr_warn("Architected timer frequency not available\n");
  237. return -EINVAL;
  238. }
  239. arch_timer_rate = freq;
  240. }
  241. pr_info_once("Architected local timer running at %lu.%02luMHz (%s).\n",
  242. arch_timer_rate / 1000000, (arch_timer_rate / 10000) % 100,
  243. arch_timer_use_virtual ? "virt" : "phys");
  244. return 0;
  245. }
  246. static u32 notrace arch_counter_get_cntpct32(void)
  247. {
  248. cycle_t cnt = arch_counter_get_cntpct();
  249. /*
  250. * The sched_clock infrastructure only knows about counters
  251. * with at most 32bits. Forget about the upper 24 bits for the
  252. * time being...
  253. */
  254. return (u32)cnt;
  255. }
  256. static u32 notrace arch_counter_get_cntvct32(void)
  257. {
  258. cycle_t cnt = arch_counter_get_cntvct();
  259. /*
  260. * The sched_clock infrastructure only knows about counters
  261. * with at most 32bits. Forget about the upper 24 bits for the
  262. * time being...
  263. */
  264. return (u32)cnt;
  265. }
  266. static cycle_t arch_counter_read(struct clocksource *cs)
  267. {
  268. /*
  269. * Always use the physical counter for the clocksource.
  270. * CNTHCTL.PL1PCTEN must be set to 1.
  271. */
  272. return arch_counter_get_cntpct();
  273. }
  274. static unsigned long arch_timer_read_current_timer(void)
  275. {
  276. return arch_counter_get_cntpct();
  277. }
  278. static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
  279. {
  280. /*
  281. * Always use the physical counter for the clocksource.
  282. * CNTHCTL.PL1PCTEN must be set to 1.
  283. */
  284. return arch_counter_get_cntpct();
  285. }
  286. static struct clocksource clocksource_counter = {
  287. .name = "arch_sys_counter",
  288. .rating = 400,
  289. .read = arch_counter_read,
  290. .mask = CLOCKSOURCE_MASK(56),
  291. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  292. };
  293. static struct cyclecounter cyclecounter = {