arch_timer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/delay.h>
  23. #include <asm/arch_timer.h>
  24. #include <asm/sched_clock.h>
  25. static u32 arch_timer_rate;
  26. enum ppi_nr {
  27. PHYS_SECURE_PPI,
  28. PHYS_NONSECURE_PPI,
  29. VIRT_PPI,
  30. HYP_PPI,
  31. MAX_TIMER_PPI
  32. };
  33. static int arch_timer_ppi[MAX_TIMER_PPI];
  34. static struct clock_event_device __percpu *arch_timer_evt;
  35. static struct delay_timer arch_delay_timer;
  36. static bool arch_timer_use_virtual = true;
  37. /*
  38. * Architected system timer support.
  39. */
  40. static irqreturn_t inline timer_handler(const int access,
  41. struct clock_event_device *evt)
  42. {
  43. unsigned long ctrl;
  44. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  45. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  46. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  47. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  48. evt->event_handler(evt);
  49. return IRQ_HANDLED;
  50. }
  51. return IRQ_NONE;
  52. }
  53. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  54. {
  55. struct clock_event_device *evt = dev_id;
  56. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  57. }
  58. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  59. {
  60. struct clock_event_device *evt = dev_id;
  61. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  62. }
  63. static inline void timer_set_mode(const int access, int mode)
  64. {
  65. unsigned long ctrl;
  66. switch (mode) {
  67. case CLOCK_EVT_MODE_UNUSED:
  68. case CLOCK_EVT_MODE_SHUTDOWN:
  69. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  70. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  71. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  72. break;
  73. default:
  74. break;
  75. }
  76. }
  77. static void arch_timer_set_mode_virt(enum clock_event_mode mode,
  78. struct clock_event_device *clk)
  79. {
  80. timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode);
  81. }
  82. static void arch_timer_set_mode_phys(enum clock_event_mode mode,
  83. struct clock_event_device *clk)
  84. {
  85. timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode);
  86. }
  87. static inline void set_next_event(const int access, unsigned long evt)
  88. {
  89. unsigned long ctrl;
  90. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  91. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  92. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  93. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt);
  94. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  95. }
  96. static int arch_timer_set_next_event_virt(unsigned long evt,
  97. struct clock_event_device *unused)
  98. {
  99. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt);
  100. return 0;
  101. }
  102. static int arch_timer_set_next_event_phys(unsigned long evt,
  103. struct clock_event_device *unused)
  104. {
  105. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt);
  106. return 0;
  107. }
  108. static int __cpuinit arch_timer_setup(struct clock_event_device *clk)
  109. {
  110. clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP;
  111. clk->name = "arch_sys_timer";
  112. clk->rating = 450;
  113. if (arch_timer_use_virtual) {
  114. clk->irq = arch_timer_ppi[VIRT_PPI];
  115. clk->set_mode = arch_timer_set_mode_virt;
  116. clk->set_next_event = arch_timer_set_next_event_virt;
  117. } else {
  118. clk->irq = arch_timer_ppi[PHYS_SECURE_PPI];
  119. clk->set_mode = arch_timer_set_mode_phys;
  120. clk->set_next_event = arch_timer_set_next_event_phys;
  121. }
  122. clk->cpumask = cpumask_of(smp_processor_id());
  123. clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, NULL);
  124. clockevents_config_and_register(clk, arch_timer_rate,
  125. 0xf, 0x7fffffff);
  126. if (arch_timer_use_virtual)
  127. enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
  128. else {
  129. enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
  130. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  131. enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
  132. }
  133. return 0;
  134. }
  135. static int arch_timer_available(void)
  136. {
  137. u32 freq;
  138. if (arch_timer_rate == 0) {
  139. freq = arch_timer_get_cntfrq();
  140. /* Check the timer frequency. */
  141. if (freq == 0) {
  142. pr_warn("Architected timer frequency not available\n");
  143. return -EINVAL;
  144. }
  145. arch_timer_rate = freq;
  146. }
  147. pr_info_once("Architected local timer running at %lu.%02luMHz (%s).\n",
  148. (unsigned long)arch_timer_rate / 1000000,
  149. (unsigned long)(arch_timer_rate / 10000) % 100,
  150. arch_timer_use_virtual ? "virt" : "phys");
  151. return 0;
  152. }
  153. /*
  154. * Some external users of arch_timer_read_counter (e.g. sched_clock) may try to
  155. * call it before it has been initialised. Rather than incur a performance
  156. * penalty checking for initialisation, provide a default implementation that
  157. * won't lead to time appearing to jump backwards.
  158. */
  159. static u64 arch_timer_read_zero(void)
  160. {
  161. return 0;
  162. }
  163. u64 (*arch_timer_read_counter)(void) = arch_timer_read_zero;
  164. static u32 arch_timer_read_counter32(void)
  165. {
  166. return arch_timer_read_counter();
  167. }
  168. static cycle_t arch_counter_read(struct clocksource *cs)
  169. {
  170. return arch_timer_read_counter();
  171. }
  172. static unsigned long arch_timer_read_current_timer(void)
  173. {
  174. return arch_timer_read_counter();
  175. }
  176. static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
  177. {
  178. return arch_timer_read_counter();
  179. }
  180. static struct clocksource clocksource_counter = {
  181. .name = "arch_sys_counter",
  182. .rating = 400,
  183. .read = arch_counter_read,
  184. .mask = CLOCKSOURCE_MASK(56),
  185. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  186. };
  187. static struct cyclecounter cyclecounter = {
  188. .read = arch_counter_read_cc,
  189. .mask = CLOCKSOURCE_MASK(56),
  190. };
  191. static struct timecounter timecounter;
  192. struct timecounter *arch_timer_get_timecounter(void)
  193. {
  194. return &timecounter;
  195. }
  196. static void __cpuinit arch_timer_stop(struct clock_event_device *clk)
  197. {
  198. pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
  199. clk->irq, smp_processor_id());
  200. if (arch_timer_use_virtual)
  201. disable_percpu_irq(arch_timer_ppi[VIRT_PPI]);
  202. else {
  203. disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]);
  204. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  205. disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
  206. }
  207. clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  208. }
  209. static int __cpuinit arch_timer_cpu_notify(struct notifier_block *self,
  210. unsigned long action, void *hcpu)
  211. {
  212. struct clock_event_device *evt = this_cpu_ptr(arch_timer_evt);
  213. switch (action & ~CPU_TASKS_FROZEN) {
  214. case CPU_STARTING:
  215. arch_timer_setup(evt);
  216. break;
  217. case CPU_DYING:
  218. arch_timer_stop(evt);
  219. break;
  220. }
  221. return NOTIFY_OK;
  222. }
  223. static struct notifier_block arch_timer_cpu_nb __cpuinitdata = {
  224. .notifier_call = arch_timer_cpu_notify,
  225. };
  226. static int __init arch_timer_register(void)
  227. {
  228. int err;
  229. int ppi;
  230. err = arch_timer_available();
  231. if (err)
  232. goto out;
  233. arch_timer_evt = alloc_percpu(struct clock_event_device);
  234. if (!arch_timer_evt) {
  235. err = -ENOMEM;
  236. goto out;
  237. }
  238. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  239. cyclecounter.mult = clocksource_counter.mult;
  240. cyclecounter.shift = clocksource_counter.shift;
  241. timecounter_init(&timecounter, &cyclecounter,
  242. arch_counter_get_cntpct());
  243. if (arch_timer_use_virtual) {
  244. ppi = arch_timer_ppi[VIRT_PPI];
  245. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  246. "arch_timer", arch_timer_evt);
  247. } else {
  248. ppi = arch_timer_ppi[PHYS_SECURE_PPI];
  249. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  250. "arch_timer", arch_timer_evt);
  251. if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  252. ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
  253. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  254. "arch_timer", arch_timer_evt);
  255. if (err)
  256. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  257. arch_timer_evt);
  258. }
  259. }
  260. if (err) {
  261. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  262. ppi, err);
  263. goto out_free;
  264. }
  265. err = register_cpu_notifier(&arch_timer_cpu_nb);
  266. if (err)
  267. goto out_free_irq;
  268. /* Immediately configure the timer on the boot CPU */
  269. arch_timer_setup(this_cpu_ptr(arch_timer_evt));
  270. /* Use the architected timer for the delay loop. */
  271. arch_delay_timer.read_current_timer = &arch_timer_read_current_timer;
  272. arch_delay_timer.freq = arch_timer_rate;
  273. register_current_timer_delay(&arch_delay_timer);
  274. return 0;
  275. out_free_irq:
  276. if (arch_timer_use_virtual)
  277. free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt);
  278. else {
  279. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  280. arch_timer_evt);
  281. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  282. free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
  283. arch_timer_evt);
  284. }
  285. out_free:
  286. free_percpu(arch_timer_evt);
  287. out:
  288. return err;
  289. }
  290. static const struct of_device_id arch_timer_of_match[] __initconst = {
  291. { .compatible = "arm,armv7-timer", },
  292. {},
  293. };
  294. int __init arch_timer_of_register(void)
  295. {
  296. struct device_node *np;
  297. u32 freq;
  298. int i;
  299. np = of_find_matching_node(NULL, arch_timer_of_match);
  300. if (!np) {
  301. pr_err("arch_timer: can't find DT node\n");
  302. return -ENODEV;
  303. }
  304. /* Try to determine the frequency from the device tree or CNTFRQ */
  305. if (!of_property_read_u32(np, "clock-frequency", &freq))
  306. arch_timer_rate = freq;
  307. for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
  308. arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
  309. of_node_put(np);
  310. /*
  311. * If no interrupt provided for virtual timer, we'll have to
  312. * stick to the physical timer. It'd better be accessible...
  313. */
  314. if (!arch_timer_ppi[VIRT_PPI]) {
  315. arch_timer_use_virtual = false;
  316. if (!arch_timer_ppi[PHYS_SECURE_PPI] ||
  317. !arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  318. pr_warn("arch_timer: No interrupt available, giving up\n");
  319. return -EINVAL;
  320. }
  321. }
  322. if (arch_timer_use_virtual)
  323. arch_timer_read_counter = arch_counter_get_cntvct;
  324. else
  325. arch_timer_read_counter = arch_counter_get_cntpct;
  326. return arch_timer_register();
  327. }
  328. int __init arch_timer_sched_clock_init(void)
  329. {
  330. int err;
  331. err = arch_timer_available();
  332. if (err)
  333. return err;
  334. setup_sched_clock(arch_timer_read_counter32,
  335. 32, arch_timer_rate);
  336. return 0;
  337. }