arch_timer.c 13 KB

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