arch_timer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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/localtimer.h>
  24. #include <asm/arch_timer.h>
  25. #include <asm/sched_clock.h>
  26. static unsigned long arch_timer_rate;
  27. enum ppi_nr {
  28. PHYS_SECURE_PPI,
  29. PHYS_NONSECURE_PPI,
  30. VIRT_PPI,
  31. HYP_PPI,
  32. MAX_TIMER_PPI
  33. };
  34. static int arch_timer_ppi[MAX_TIMER_PPI];
  35. static struct clock_event_device __percpu **arch_timer_evt;
  36. static struct delay_timer arch_delay_timer;
  37. static bool arch_timer_use_virtual = true;
  38. /*
  39. * Architected system timer support.
  40. */
  41. #define ARCH_TIMER_CTRL_ENABLE (1 << 0)
  42. #define ARCH_TIMER_CTRL_IT_MASK (1 << 1)
  43. #define ARCH_TIMER_CTRL_IT_STAT (1 << 2)
  44. #define ARCH_TIMER_REG_CTRL 0
  45. #define ARCH_TIMER_REG_FREQ 1
  46. #define ARCH_TIMER_REG_TVAL 2
  47. #define ARCH_TIMER_PHYS_ACCESS 0
  48. #define ARCH_TIMER_VIRT_ACCESS 1
  49. /*
  50. * These register accessors are marked inline so the compiler can
  51. * nicely work out which register we want, and chuck away the rest of
  52. * the code. At least it does so with a recent GCC (4.6.3).
  53. */
  54. static inline void arch_timer_reg_write(const int access, const int reg, u32 val)
  55. {
  56. if (access == ARCH_TIMER_PHYS_ACCESS) {
  57. switch (reg) {
  58. case ARCH_TIMER_REG_CTRL:
  59. asm volatile("mcr p15, 0, %0, c14, c2, 1" : : "r" (val));
  60. break;
  61. case ARCH_TIMER_REG_TVAL:
  62. asm volatile("mcr p15, 0, %0, c14, c2, 0" : : "r" (val));
  63. break;
  64. }
  65. }
  66. if (access == ARCH_TIMER_VIRT_ACCESS) {
  67. switch (reg) {
  68. case ARCH_TIMER_REG_CTRL:
  69. asm volatile("mcr p15, 0, %0, c14, c3, 1" : : "r" (val));
  70. break;
  71. case ARCH_TIMER_REG_TVAL:
  72. asm volatile("mcr p15, 0, %0, c14, c3, 0" : : "r" (val));
  73. break;
  74. }
  75. }
  76. isb();
  77. }
  78. static inline u32 arch_timer_reg_read(const int access, const int reg)
  79. {
  80. u32 val = 0;
  81. if (access == ARCH_TIMER_PHYS_ACCESS) {
  82. switch (reg) {
  83. case ARCH_TIMER_REG_CTRL:
  84. asm volatile("mrc p15, 0, %0, c14, c2, 1" : "=r" (val));
  85. break;
  86. case ARCH_TIMER_REG_TVAL:
  87. asm volatile("mrc p15, 0, %0, c14, c2, 0" : "=r" (val));
  88. break;
  89. case ARCH_TIMER_REG_FREQ:
  90. asm volatile("mrc p15, 0, %0, c14, c0, 0" : "=r" (val));
  91. break;
  92. }
  93. }
  94. if (access == ARCH_TIMER_VIRT_ACCESS) {
  95. switch (reg) {
  96. case ARCH_TIMER_REG_CTRL:
  97. asm volatile("mrc p15, 0, %0, c14, c3, 1" : "=r" (val));
  98. break;
  99. case ARCH_TIMER_REG_TVAL:
  100. asm volatile("mrc p15, 0, %0, c14, c3, 0" : "=r" (val));
  101. break;
  102. }
  103. }
  104. return val;
  105. }
  106. static inline cycle_t arch_timer_counter_read(const int access)
  107. {
  108. cycle_t cval = 0;
  109. if (access == ARCH_TIMER_PHYS_ACCESS)
  110. asm volatile("mrrc p15, 0, %Q0, %R0, c14" : "=r" (cval));
  111. if (access == ARCH_TIMER_VIRT_ACCESS)
  112. asm volatile("mrrc p15, 1, %Q0, %R0, c14" : "=r" (cval));
  113. return cval;
  114. }
  115. static inline cycle_t arch_counter_get_cntpct(void)
  116. {
  117. return arch_timer_counter_read(ARCH_TIMER_PHYS_ACCESS);
  118. }
  119. static inline cycle_t arch_counter_get_cntvct(void)
  120. {
  121. return arch_timer_counter_read(ARCH_TIMER_VIRT_ACCESS);
  122. }
  123. static irqreturn_t inline timer_handler(const int access,
  124. struct clock_event_device *evt)
  125. {
  126. unsigned long ctrl;
  127. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  128. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  129. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  130. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  131. evt->event_handler(evt);
  132. return IRQ_HANDLED;
  133. }
  134. return IRQ_NONE;
  135. }
  136. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  137. {
  138. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  139. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  140. }
  141. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  142. {
  143. struct clock_event_device *evt = *(struct clock_event_device **)dev_id;
  144. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  145. }
  146. static inline void timer_set_mode(const int access, int mode)
  147. {
  148. unsigned long ctrl;
  149. switch (mode) {
  150. case CLOCK_EVT_MODE_UNUSED:
  151. case CLOCK_EVT_MODE_SHUTDOWN:
  152. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  153. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  154. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. static void arch_timer_set_mode_virt(enum clock_event_mode mode,
  161. struct clock_event_device *clk)
  162. {
  163. timer_set_mode(ARCH_TIMER_VIRT_ACCESS, mode);
  164. }
  165. static void arch_timer_set_mode_phys(enum clock_event_mode mode,
  166. struct clock_event_device *clk)
  167. {
  168. timer_set_mode(ARCH_TIMER_PHYS_ACCESS, mode);
  169. }
  170. static inline void set_next_event(const int access, unsigned long evt)
  171. {
  172. unsigned long ctrl;
  173. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL);
  174. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  175. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  176. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt);
  177. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl);
  178. }
  179. static int arch_timer_set_next_event_virt(unsigned long evt,
  180. struct clock_event_device *unused)
  181. {
  182. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt);
  183. return 0;
  184. }
  185. static int arch_timer_set_next_event_phys(unsigned long evt,
  186. struct clock_event_device *unused)
  187. {
  188. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt);
  189. return 0;
  190. }
  191. static int __cpuinit arch_timer_setup(struct clock_event_device *clk)
  192. {
  193. clk->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP;
  194. clk->name = "arch_sys_timer";
  195. clk->rating = 450;
  196. if (arch_timer_use_virtual) {
  197. clk->irq = arch_timer_ppi[VIRT_PPI];
  198. clk->set_mode = arch_timer_set_mode_virt;
  199. clk->set_next_event = arch_timer_set_next_event_virt;
  200. } else {
  201. clk->irq = arch_timer_ppi[PHYS_SECURE_PPI];
  202. clk->set_mode = arch_timer_set_mode_phys;
  203. clk->set_next_event = arch_timer_set_next_event_phys;
  204. }
  205. clk->set_mode(CLOCK_EVT_MODE_SHUTDOWN, NULL);
  206. clockevents_config_and_register(clk, arch_timer_rate,
  207. 0xf, 0x7fffffff);
  208. *__this_cpu_ptr(arch_timer_evt) = clk;
  209. if (arch_timer_use_virtual)
  210. enable_percpu_irq(arch_timer_ppi[VIRT_PPI], 0);
  211. else {
  212. enable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI], 0);
  213. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  214. enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], 0);
  215. }
  216. return 0;
  217. }
  218. static int arch_timer_available(void)
  219. {
  220. unsigned long freq;
  221. if (arch_timer_rate == 0) {
  222. freq = arch_timer_reg_read(ARCH_TIMER_PHYS_ACCESS,
  223. ARCH_TIMER_REG_FREQ);
  224. /* Check the timer frequency. */
  225. if (freq == 0) {
  226. pr_warn("Architected timer frequency not available\n");
  227. return -EINVAL;
  228. }
  229. arch_timer_rate = freq;
  230. }
  231. pr_info_once("Architected local timer running at %lu.%02luMHz (%s).\n",
  232. arch_timer_rate / 1000000, (arch_timer_rate / 10000) % 100,
  233. arch_timer_use_virtual ? "virt" : "phys");
  234. return 0;
  235. }
  236. static u32 notrace arch_counter_get_cntpct32(void)
  237. {
  238. cycle_t cnt = arch_counter_get_cntpct();
  239. /*
  240. * The sched_clock infrastructure only knows about counters
  241. * with at most 32bits. Forget about the upper 24 bits for the
  242. * time being...
  243. */
  244. return (u32)cnt;
  245. }
  246. static u32 notrace arch_counter_get_cntvct32(void)
  247. {
  248. cycle_t cnt = arch_counter_get_cntvct();
  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 cycle_t arch_counter_read(struct clocksource *cs)
  257. {
  258. /*
  259. * Always use the physical counter for the clocksource.
  260. * CNTHCTL.PL1PCTEN must be set to 1.
  261. */
  262. return arch_counter_get_cntpct();
  263. }
  264. static unsigned long arch_timer_read_current_timer(void)
  265. {
  266. return arch_counter_get_cntpct();
  267. }
  268. static cycle_t arch_counter_read_cc(const struct cyclecounter *cc)
  269. {
  270. /*
  271. * Always use the physical counter for the clocksource.
  272. * CNTHCTL.PL1PCTEN must be set to 1.
  273. */
  274. return arch_counter_get_cntpct();
  275. }
  276. static struct clocksource clocksource_counter = {
  277. .name = "arch_sys_counter",
  278. .rating = 400,
  279. .read = arch_counter_read,
  280. .mask = CLOCKSOURCE_MASK(56),
  281. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  282. };
  283. static struct cyclecounter cyclecounter = {
  284. .read = arch_counter_read_cc,
  285. .mask = CLOCKSOURCE_MASK(56),
  286. };
  287. static struct timecounter timecounter;
  288. struct timecounter *arch_timer_get_timecounter(void)
  289. {
  290. return &timecounter;
  291. }
  292. static void __cpuinit arch_timer_stop(struct clock_event_device *clk)
  293. {
  294. pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
  295. clk->irq, smp_processor_id());
  296. if (arch_timer_use_virtual)
  297. disable_percpu_irq(arch_timer_ppi[VIRT_PPI]);
  298. else {
  299. disable_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI]);
  300. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  301. disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
  302. }
  303. clk->set_mode(CLOCK_EVT_MODE_UNUSED, clk);
  304. }
  305. static struct local_timer_ops arch_timer_ops __cpuinitdata = {
  306. .setup = arch_timer_setup,
  307. .stop = arch_timer_stop,
  308. };
  309. static struct clock_event_device arch_timer_global_evt;
  310. static int __init arch_timer_register(void)
  311. {
  312. int err;
  313. int ppi;
  314. err = arch_timer_available();
  315. if (err)
  316. goto out;
  317. arch_timer_evt = alloc_percpu(struct clock_event_device *);
  318. if (!arch_timer_evt) {
  319. err = -ENOMEM;
  320. goto out;
  321. }
  322. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  323. cyclecounter.mult = clocksource_counter.mult;
  324. cyclecounter.shift = clocksource_counter.shift;
  325. timecounter_init(&timecounter, &cyclecounter,
  326. arch_counter_get_cntpct());
  327. if (arch_timer_use_virtual) {
  328. ppi = arch_timer_ppi[VIRT_PPI];
  329. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  330. "arch_timer", arch_timer_evt);
  331. } else {
  332. ppi = arch_timer_ppi[PHYS_SECURE_PPI];
  333. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  334. "arch_timer", arch_timer_evt);
  335. if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  336. ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
  337. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  338. "arch_timer", arch_timer_evt);
  339. if (err)
  340. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  341. arch_timer_evt);
  342. }
  343. }
  344. if (err) {
  345. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  346. ppi, err);
  347. goto out_free;
  348. }
  349. err = local_timer_register(&arch_timer_ops);
  350. if (err) {
  351. /*
  352. * We couldn't register as a local timer (could be
  353. * because we're on a UP platform, or because some
  354. * other local timer is already present...). Try as a
  355. * global timer instead.
  356. */
  357. arch_timer_global_evt.cpumask = cpumask_of(0);
  358. err = arch_timer_setup(&arch_timer_global_evt);
  359. }
  360. if (err)
  361. goto out_free_irq;
  362. /* Use the architected timer for the delay loop. */
  363. arch_delay_timer.read_current_timer = &arch_timer_read_current_timer;
  364. arch_delay_timer.freq = arch_timer_rate;
  365. register_current_timer_delay(&arch_delay_timer);
  366. return 0;
  367. out_free_irq:
  368. if (arch_timer_use_virtual)
  369. free_percpu_irq(arch_timer_ppi[VIRT_PPI], arch_timer_evt);
  370. else {
  371. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  372. arch_timer_evt);
  373. if (arch_timer_ppi[PHYS_NONSECURE_PPI])
  374. free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
  375. arch_timer_evt);
  376. }
  377. out_free:
  378. free_percpu(arch_timer_evt);
  379. out:
  380. return err;
  381. }
  382. static const struct of_device_id arch_timer_of_match[] __initconst = {
  383. { .compatible = "arm,armv7-timer", },
  384. {},
  385. };
  386. int __init arch_timer_of_register(void)
  387. {
  388. struct device_node *np;
  389. u32 freq;
  390. int i;
  391. np = of_find_matching_node(NULL, arch_timer_of_match);
  392. if (!np) {
  393. pr_err("arch_timer: can't find DT node\n");
  394. return -ENODEV;
  395. }
  396. /* Try to determine the frequency from the device tree or CNTFRQ */
  397. if (!of_property_read_u32(np, "clock-frequency", &freq))
  398. arch_timer_rate = freq;
  399. for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
  400. arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
  401. of_node_put(np);
  402. /*
  403. * If no interrupt provided for virtual timer, we'll have to
  404. * stick to the physical timer. It'd better be accessible...
  405. */
  406. if (!arch_timer_ppi[VIRT_PPI]) {
  407. arch_timer_use_virtual = false;
  408. if (!arch_timer_ppi[PHYS_SECURE_PPI] ||
  409. !arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  410. pr_warn("arch_timer: No interrupt available, giving up\n");
  411. return -EINVAL;
  412. }
  413. }
  414. return arch_timer_register();
  415. }
  416. int __init arch_timer_sched_clock_init(void)
  417. {
  418. u32 (*cnt32)(void);
  419. int err;
  420. err = arch_timer_available();
  421. if (err)
  422. return err;
  423. if (arch_timer_use_virtual)
  424. cnt32 = arch_counter_get_cntvct32;
  425. else
  426. cnt32 = arch_counter_get_cntpct32;
  427. setup_sched_clock(cnt32, 32, arch_timer_rate);
  428. return 0;
  429. }