arm_arch_timer.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  1. /*
  2. * linux/drivers/clocksource/arm_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. #define pr_fmt(fmt) "arm_arch_timer: " fmt
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/device.h>
  15. #include <linux/smp.h>
  16. #include <linux/cpu.h>
  17. #include <linux/cpu_pm.h>
  18. #include <linux/clockchips.h>
  19. #include <linux/clocksource.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_address.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <linux/sched/clock.h>
  26. #include <linux/sched_clock.h>
  27. #include <linux/acpi.h>
  28. #include <asm/arch_timer.h>
  29. #include <asm/virt.h>
  30. #include <clocksource/arm_arch_timer.h>
  31. #define CNTTIDR 0x08
  32. #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
  33. #define CNTACR(n) (0x40 + ((n) * 4))
  34. #define CNTACR_RPCT BIT(0)
  35. #define CNTACR_RVCT BIT(1)
  36. #define CNTACR_RFRQ BIT(2)
  37. #define CNTACR_RVOFF BIT(3)
  38. #define CNTACR_RWVT BIT(4)
  39. #define CNTACR_RWPT BIT(5)
  40. #define CNTVCT_LO 0x08
  41. #define CNTVCT_HI 0x0c
  42. #define CNTFRQ 0x10
  43. #define CNTP_TVAL 0x28
  44. #define CNTP_CTL 0x2c
  45. #define CNTV_TVAL 0x38
  46. #define CNTV_CTL 0x3c
  47. #define ARCH_CP15_TIMER BIT(0)
  48. #define ARCH_MEM_TIMER BIT(1)
  49. static unsigned arch_timers_present __initdata;
  50. static void __iomem *arch_counter_base;
  51. struct arch_timer {
  52. void __iomem *base;
  53. struct clock_event_device evt;
  54. };
  55. #define to_arch_timer(e) container_of(e, struct arch_timer, evt)
  56. static u32 arch_timer_rate;
  57. enum ppi_nr {
  58. PHYS_SECURE_PPI,
  59. PHYS_NONSECURE_PPI,
  60. VIRT_PPI,
  61. HYP_PPI,
  62. MAX_TIMER_PPI
  63. };
  64. static int arch_timer_ppi[MAX_TIMER_PPI];
  65. static struct clock_event_device __percpu *arch_timer_evt;
  66. static enum ppi_nr arch_timer_uses_ppi = VIRT_PPI;
  67. static bool arch_timer_c3stop;
  68. static bool arch_timer_mem_use_virtual;
  69. static bool arch_counter_suspend_stop;
  70. static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
  71. static int __init early_evtstrm_cfg(char *buf)
  72. {
  73. return strtobool(buf, &evtstrm_enable);
  74. }
  75. early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
  76. /*
  77. * Architected system timer support.
  78. */
  79. #ifdef CONFIG_FSL_ERRATUM_A008585
  80. /*
  81. * The number of retries is an arbitrary value well beyond the highest number
  82. * of iterations the loop has been observed to take.
  83. */
  84. #define __fsl_a008585_read_reg(reg) ({ \
  85. u64 _old, _new; \
  86. int _retries = 200; \
  87. \
  88. do { \
  89. _old = read_sysreg(reg); \
  90. _new = read_sysreg(reg); \
  91. _retries--; \
  92. } while (unlikely(_old != _new) && _retries); \
  93. \
  94. WARN_ON_ONCE(!_retries); \
  95. _new; \
  96. })
  97. static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
  98. {
  99. return __fsl_a008585_read_reg(cntp_tval_el0);
  100. }
  101. static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
  102. {
  103. return __fsl_a008585_read_reg(cntv_tval_el0);
  104. }
  105. static u64 notrace fsl_a008585_read_cntvct_el0(void)
  106. {
  107. return __fsl_a008585_read_reg(cntvct_el0);
  108. }
  109. #endif
  110. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  111. /*
  112. * Verify whether the value of the second read is larger than the first by
  113. * less than 32 is the only way to confirm the value is correct, so clear the
  114. * lower 5 bits to check whether the difference is greater than 32 or not.
  115. * Theoretically the erratum should not occur more than twice in succession
  116. * when reading the system counter, but it is possible that some interrupts
  117. * may lead to more than twice read errors, triggering the warning, so setting
  118. * the number of retries far beyond the number of iterations the loop has been
  119. * observed to take.
  120. */
  121. #define __hisi_161010101_read_reg(reg) ({ \
  122. u64 _old, _new; \
  123. int _retries = 50; \
  124. \
  125. do { \
  126. _old = read_sysreg(reg); \
  127. _new = read_sysreg(reg); \
  128. _retries--; \
  129. } while (unlikely((_new - _old) >> 5) && _retries); \
  130. \
  131. WARN_ON_ONCE(!_retries); \
  132. _new; \
  133. })
  134. static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
  135. {
  136. return __hisi_161010101_read_reg(cntp_tval_el0);
  137. }
  138. static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
  139. {
  140. return __hisi_161010101_read_reg(cntv_tval_el0);
  141. }
  142. static u64 notrace hisi_161010101_read_cntvct_el0(void)
  143. {
  144. return __hisi_161010101_read_reg(cntvct_el0);
  145. }
  146. #endif
  147. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  148. const struct arch_timer_erratum_workaround *timer_unstable_counter_workaround = NULL;
  149. EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
  150. DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
  151. EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
  152. static const struct arch_timer_erratum_workaround ool_workarounds[] = {
  153. #ifdef CONFIG_FSL_ERRATUM_A008585
  154. {
  155. .id = "fsl,erratum-a008585",
  156. .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
  157. .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
  158. .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
  159. },
  160. #endif
  161. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  162. {
  163. .id = "hisilicon,erratum-161010101",
  164. .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
  165. .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
  166. .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
  167. },
  168. #endif
  169. };
  170. #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
  171. static __always_inline
  172. void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
  173. struct clock_event_device *clk)
  174. {
  175. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  176. struct arch_timer *timer = to_arch_timer(clk);
  177. switch (reg) {
  178. case ARCH_TIMER_REG_CTRL:
  179. writel_relaxed(val, timer->base + CNTP_CTL);
  180. break;
  181. case ARCH_TIMER_REG_TVAL:
  182. writel_relaxed(val, timer->base + CNTP_TVAL);
  183. break;
  184. }
  185. } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
  186. struct arch_timer *timer = to_arch_timer(clk);
  187. switch (reg) {
  188. case ARCH_TIMER_REG_CTRL:
  189. writel_relaxed(val, timer->base + CNTV_CTL);
  190. break;
  191. case ARCH_TIMER_REG_TVAL:
  192. writel_relaxed(val, timer->base + CNTV_TVAL);
  193. break;
  194. }
  195. } else {
  196. arch_timer_reg_write_cp15(access, reg, val);
  197. }
  198. }
  199. static __always_inline
  200. u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
  201. struct clock_event_device *clk)
  202. {
  203. u32 val;
  204. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  205. struct arch_timer *timer = to_arch_timer(clk);
  206. switch (reg) {
  207. case ARCH_TIMER_REG_CTRL:
  208. val = readl_relaxed(timer->base + CNTP_CTL);
  209. break;
  210. case ARCH_TIMER_REG_TVAL:
  211. val = readl_relaxed(timer->base + CNTP_TVAL);
  212. break;
  213. }
  214. } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
  215. struct arch_timer *timer = to_arch_timer(clk);
  216. switch (reg) {
  217. case ARCH_TIMER_REG_CTRL:
  218. val = readl_relaxed(timer->base + CNTV_CTL);
  219. break;
  220. case ARCH_TIMER_REG_TVAL:
  221. val = readl_relaxed(timer->base + CNTV_TVAL);
  222. break;
  223. }
  224. } else {
  225. val = arch_timer_reg_read_cp15(access, reg);
  226. }
  227. return val;
  228. }
  229. static __always_inline irqreturn_t timer_handler(const int access,
  230. struct clock_event_device *evt)
  231. {
  232. unsigned long ctrl;
  233. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
  234. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  235. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  236. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
  237. evt->event_handler(evt);
  238. return IRQ_HANDLED;
  239. }
  240. return IRQ_NONE;
  241. }
  242. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  243. {
  244. struct clock_event_device *evt = dev_id;
  245. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  246. }
  247. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  248. {
  249. struct clock_event_device *evt = dev_id;
  250. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  251. }
  252. static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
  253. {
  254. struct clock_event_device *evt = dev_id;
  255. return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
  256. }
  257. static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
  258. {
  259. struct clock_event_device *evt = dev_id;
  260. return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
  261. }
  262. static __always_inline int timer_shutdown(const int access,
  263. struct clock_event_device *clk)
  264. {
  265. unsigned long ctrl;
  266. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  267. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  268. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  269. return 0;
  270. }
  271. static int arch_timer_shutdown_virt(struct clock_event_device *clk)
  272. {
  273. return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
  274. }
  275. static int arch_timer_shutdown_phys(struct clock_event_device *clk)
  276. {
  277. return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
  278. }
  279. static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
  280. {
  281. return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
  282. }
  283. static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
  284. {
  285. return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
  286. }
  287. static __always_inline void set_next_event(const int access, unsigned long evt,
  288. struct clock_event_device *clk)
  289. {
  290. unsigned long ctrl;
  291. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  292. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  293. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  294. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
  295. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  296. }
  297. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  298. static __always_inline void erratum_set_next_event_generic(const int access,
  299. unsigned long evt, struct clock_event_device *clk)
  300. {
  301. unsigned long ctrl;
  302. u64 cval = evt + arch_counter_get_cntvct();
  303. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  304. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  305. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  306. if (access == ARCH_TIMER_PHYS_ACCESS)
  307. write_sysreg(cval, cntp_cval_el0);
  308. else if (access == ARCH_TIMER_VIRT_ACCESS)
  309. write_sysreg(cval, cntv_cval_el0);
  310. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  311. }
  312. static int erratum_set_next_event_virt(unsigned long evt,
  313. struct clock_event_device *clk)
  314. {
  315. erratum_set_next_event_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  316. return 0;
  317. }
  318. static int erratum_set_next_event_phys(unsigned long evt,
  319. struct clock_event_device *clk)
  320. {
  321. erratum_set_next_event_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  322. return 0;
  323. }
  324. #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
  325. static int arch_timer_set_next_event_virt(unsigned long evt,
  326. struct clock_event_device *clk)
  327. {
  328. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  329. return 0;
  330. }
  331. static int arch_timer_set_next_event_phys(unsigned long evt,
  332. struct clock_event_device *clk)
  333. {
  334. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  335. return 0;
  336. }
  337. static int arch_timer_set_next_event_virt_mem(unsigned long evt,
  338. struct clock_event_device *clk)
  339. {
  340. set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
  341. return 0;
  342. }
  343. static int arch_timer_set_next_event_phys_mem(unsigned long evt,
  344. struct clock_event_device *clk)
  345. {
  346. set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
  347. return 0;
  348. }
  349. static void erratum_workaround_set_sne(struct clock_event_device *clk)
  350. {
  351. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  352. if (!static_branch_unlikely(&arch_timer_read_ool_enabled))
  353. return;
  354. if (arch_timer_uses_ppi == VIRT_PPI)
  355. clk->set_next_event = erratum_set_next_event_virt;
  356. else
  357. clk->set_next_event = erratum_set_next_event_phys;
  358. #endif
  359. }
  360. static void __arch_timer_setup(unsigned type,
  361. struct clock_event_device *clk)
  362. {
  363. clk->features = CLOCK_EVT_FEAT_ONESHOT;
  364. if (type == ARCH_CP15_TIMER) {
  365. if (arch_timer_c3stop)
  366. clk->features |= CLOCK_EVT_FEAT_C3STOP;
  367. clk->name = "arch_sys_timer";
  368. clk->rating = 450;
  369. clk->cpumask = cpumask_of(smp_processor_id());
  370. clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
  371. switch (arch_timer_uses_ppi) {
  372. case VIRT_PPI:
  373. clk->set_state_shutdown = arch_timer_shutdown_virt;
  374. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
  375. clk->set_next_event = arch_timer_set_next_event_virt;
  376. break;
  377. case PHYS_SECURE_PPI:
  378. case PHYS_NONSECURE_PPI:
  379. case HYP_PPI:
  380. clk->set_state_shutdown = arch_timer_shutdown_phys;
  381. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
  382. clk->set_next_event = arch_timer_set_next_event_phys;
  383. break;
  384. default:
  385. BUG();
  386. }
  387. erratum_workaround_set_sne(clk);
  388. } else {
  389. clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
  390. clk->name = "arch_mem_timer";
  391. clk->rating = 400;
  392. clk->cpumask = cpu_all_mask;
  393. if (arch_timer_mem_use_virtual) {
  394. clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
  395. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
  396. clk->set_next_event =
  397. arch_timer_set_next_event_virt_mem;
  398. } else {
  399. clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
  400. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
  401. clk->set_next_event =
  402. arch_timer_set_next_event_phys_mem;
  403. }
  404. }
  405. clk->set_state_shutdown(clk);
  406. clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
  407. }
  408. static void arch_timer_evtstrm_enable(int divider)
  409. {
  410. u32 cntkctl = arch_timer_get_cntkctl();
  411. cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
  412. /* Set the divider and enable virtual event stream */
  413. cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
  414. | ARCH_TIMER_VIRT_EVT_EN;
  415. arch_timer_set_cntkctl(cntkctl);
  416. elf_hwcap |= HWCAP_EVTSTRM;
  417. #ifdef CONFIG_COMPAT
  418. compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
  419. #endif
  420. }
  421. static void arch_timer_configure_evtstream(void)
  422. {
  423. int evt_stream_div, pos;
  424. /* Find the closest power of two to the divisor */
  425. evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
  426. pos = fls(evt_stream_div);
  427. if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
  428. pos--;
  429. /* enable event stream */
  430. arch_timer_evtstrm_enable(min(pos, 15));
  431. }
  432. static void arch_counter_set_user_access(void)
  433. {
  434. u32 cntkctl = arch_timer_get_cntkctl();
  435. /* Disable user access to the timers and the physical counter */
  436. /* Also disable virtual event stream */
  437. cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
  438. | ARCH_TIMER_USR_VT_ACCESS_EN
  439. | ARCH_TIMER_VIRT_EVT_EN
  440. | ARCH_TIMER_USR_PCT_ACCESS_EN);
  441. /* Enable user access to the virtual counter */
  442. cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
  443. arch_timer_set_cntkctl(cntkctl);
  444. }
  445. static bool arch_timer_has_nonsecure_ppi(void)
  446. {
  447. return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
  448. arch_timer_ppi[PHYS_NONSECURE_PPI]);
  449. }
  450. static u32 check_ppi_trigger(int irq)
  451. {
  452. u32 flags = irq_get_trigger_type(irq);
  453. if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
  454. pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
  455. pr_warn("WARNING: Please fix your firmware\n");
  456. flags = IRQF_TRIGGER_LOW;
  457. }
  458. return flags;
  459. }
  460. static int arch_timer_starting_cpu(unsigned int cpu)
  461. {
  462. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  463. u32 flags;
  464. __arch_timer_setup(ARCH_CP15_TIMER, clk);
  465. flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
  466. enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
  467. if (arch_timer_has_nonsecure_ppi()) {
  468. flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
  469. enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
  470. }
  471. arch_counter_set_user_access();
  472. if (evtstrm_enable)
  473. arch_timer_configure_evtstream();
  474. return 0;
  475. }
  476. static void
  477. arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
  478. {
  479. /* Who has more than one independent system counter? */
  480. if (arch_timer_rate)
  481. return;
  482. /*
  483. * Try to determine the frequency from the device tree or CNTFRQ,
  484. * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
  485. */
  486. if (!acpi_disabled ||
  487. of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
  488. if (cntbase)
  489. arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
  490. else
  491. arch_timer_rate = arch_timer_get_cntfrq();
  492. }
  493. /* Check the timer frequency. */
  494. if (arch_timer_rate == 0)
  495. pr_warn("Architected timer frequency not available\n");
  496. }
  497. static void arch_timer_banner(unsigned type)
  498. {
  499. pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
  500. type & ARCH_CP15_TIMER ? "cp15" : "",
  501. type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? " and " : "",
  502. type & ARCH_MEM_TIMER ? "mmio" : "",
  503. (unsigned long)arch_timer_rate / 1000000,
  504. (unsigned long)(arch_timer_rate / 10000) % 100,
  505. type & ARCH_CP15_TIMER ?
  506. (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
  507. "",
  508. type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ? "/" : "",
  509. type & ARCH_MEM_TIMER ?
  510. arch_timer_mem_use_virtual ? "virt" : "phys" :
  511. "");
  512. }
  513. u32 arch_timer_get_rate(void)
  514. {
  515. return arch_timer_rate;
  516. }
  517. static u64 arch_counter_get_cntvct_mem(void)
  518. {
  519. u32 vct_lo, vct_hi, tmp_hi;
  520. do {
  521. vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  522. vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
  523. tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  524. } while (vct_hi != tmp_hi);
  525. return ((u64) vct_hi << 32) | vct_lo;
  526. }
  527. /*
  528. * Default to cp15 based access because arm64 uses this function for
  529. * sched_clock() before DT is probed and the cp15 method is guaranteed
  530. * to exist on arm64. arm doesn't use this before DT is probed so even
  531. * if we don't have the cp15 accessors we won't have a problem.
  532. */
  533. u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
  534. static u64 arch_counter_read(struct clocksource *cs)
  535. {
  536. return arch_timer_read_counter();
  537. }
  538. static u64 arch_counter_read_cc(const struct cyclecounter *cc)
  539. {
  540. return arch_timer_read_counter();
  541. }
  542. static struct clocksource clocksource_counter = {
  543. .name = "arch_sys_counter",
  544. .rating = 400,
  545. .read = arch_counter_read,
  546. .mask = CLOCKSOURCE_MASK(56),
  547. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  548. };
  549. static struct cyclecounter cyclecounter __ro_after_init = {
  550. .read = arch_counter_read_cc,
  551. .mask = CLOCKSOURCE_MASK(56),
  552. };
  553. static struct arch_timer_kvm_info arch_timer_kvm_info;
  554. struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
  555. {
  556. return &arch_timer_kvm_info;
  557. }
  558. static void __init arch_counter_register(unsigned type)
  559. {
  560. u64 start_count;
  561. /* Register the CP15 based counter if we have one */
  562. if (type & ARCH_CP15_TIMER) {
  563. if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
  564. arch_timer_read_counter = arch_counter_get_cntvct;
  565. else
  566. arch_timer_read_counter = arch_counter_get_cntpct;
  567. clocksource_counter.archdata.vdso_direct = true;
  568. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  569. /*
  570. * Don't use the vdso fastpath if errata require using
  571. * the out-of-line counter accessor.
  572. */
  573. if (static_branch_unlikely(&arch_timer_read_ool_enabled))
  574. clocksource_counter.archdata.vdso_direct = false;
  575. #endif
  576. } else {
  577. arch_timer_read_counter = arch_counter_get_cntvct_mem;
  578. }
  579. if (!arch_counter_suspend_stop)
  580. clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
  581. start_count = arch_timer_read_counter();
  582. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  583. cyclecounter.mult = clocksource_counter.mult;
  584. cyclecounter.shift = clocksource_counter.shift;
  585. timecounter_init(&arch_timer_kvm_info.timecounter,
  586. &cyclecounter, start_count);
  587. /* 56 bits minimum, so we assume worst case rollover */
  588. sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
  589. }
  590. static void arch_timer_stop(struct clock_event_device *clk)
  591. {
  592. pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
  593. clk->irq, smp_processor_id());
  594. disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
  595. if (arch_timer_has_nonsecure_ppi())
  596. disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
  597. clk->set_state_shutdown(clk);
  598. }
  599. static int arch_timer_dying_cpu(unsigned int cpu)
  600. {
  601. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  602. arch_timer_stop(clk);
  603. return 0;
  604. }
  605. #ifdef CONFIG_CPU_PM
  606. static unsigned int saved_cntkctl;
  607. static int arch_timer_cpu_pm_notify(struct notifier_block *self,
  608. unsigned long action, void *hcpu)
  609. {
  610. if (action == CPU_PM_ENTER)
  611. saved_cntkctl = arch_timer_get_cntkctl();
  612. else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
  613. arch_timer_set_cntkctl(saved_cntkctl);
  614. return NOTIFY_OK;
  615. }
  616. static struct notifier_block arch_timer_cpu_pm_notifier = {
  617. .notifier_call = arch_timer_cpu_pm_notify,
  618. };
  619. static int __init arch_timer_cpu_pm_init(void)
  620. {
  621. return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
  622. }
  623. static void __init arch_timer_cpu_pm_deinit(void)
  624. {
  625. WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
  626. }
  627. #else
  628. static int __init arch_timer_cpu_pm_init(void)
  629. {
  630. return 0;
  631. }
  632. static void __init arch_timer_cpu_pm_deinit(void)
  633. {
  634. }
  635. #endif
  636. static int __init arch_timer_register(void)
  637. {
  638. int err;
  639. int ppi;
  640. arch_timer_evt = alloc_percpu(struct clock_event_device);
  641. if (!arch_timer_evt) {
  642. err = -ENOMEM;
  643. goto out;
  644. }
  645. ppi = arch_timer_ppi[arch_timer_uses_ppi];
  646. switch (arch_timer_uses_ppi) {
  647. case VIRT_PPI:
  648. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  649. "arch_timer", arch_timer_evt);
  650. break;
  651. case PHYS_SECURE_PPI:
  652. case PHYS_NONSECURE_PPI:
  653. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  654. "arch_timer", arch_timer_evt);
  655. if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
  656. ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
  657. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  658. "arch_timer", arch_timer_evt);
  659. if (err)
  660. free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
  661. arch_timer_evt);
  662. }
  663. break;
  664. case HYP_PPI:
  665. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  666. "arch_timer", arch_timer_evt);
  667. break;
  668. default:
  669. BUG();
  670. }
  671. if (err) {
  672. pr_err("arch_timer: can't register interrupt %d (%d)\n",
  673. ppi, err);
  674. goto out_free;
  675. }
  676. err = arch_timer_cpu_pm_init();
  677. if (err)
  678. goto out_unreg_notify;
  679. /* Register and immediately configure the timer on the boot CPU */
  680. err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
  681. "clockevents/arm/arch_timer:starting",
  682. arch_timer_starting_cpu, arch_timer_dying_cpu);
  683. if (err)
  684. goto out_unreg_cpupm;
  685. return 0;
  686. out_unreg_cpupm:
  687. arch_timer_cpu_pm_deinit();
  688. out_unreg_notify:
  689. free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
  690. if (arch_timer_has_nonsecure_ppi())
  691. free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
  692. arch_timer_evt);
  693. out_free:
  694. free_percpu(arch_timer_evt);
  695. out:
  696. return err;
  697. }
  698. static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
  699. {
  700. int ret;
  701. irq_handler_t func;
  702. struct arch_timer *t;
  703. t = kzalloc(sizeof(*t), GFP_KERNEL);
  704. if (!t)
  705. return -ENOMEM;
  706. t->base = base;
  707. t->evt.irq = irq;
  708. __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
  709. if (arch_timer_mem_use_virtual)
  710. func = arch_timer_handler_virt_mem;
  711. else
  712. func = arch_timer_handler_phys_mem;
  713. ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
  714. if (ret) {
  715. pr_err("arch_timer: Failed to request mem timer irq\n");
  716. kfree(t);
  717. }
  718. return ret;
  719. }
  720. static const struct of_device_id arch_timer_of_match[] __initconst = {
  721. { .compatible = "arm,armv7-timer", },
  722. { .compatible = "arm,armv8-timer", },
  723. {},
  724. };
  725. static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
  726. { .compatible = "arm,armv7-timer-mem", },
  727. {},
  728. };
  729. static bool __init
  730. arch_timer_needs_probing(int type, const struct of_device_id *matches)
  731. {
  732. struct device_node *dn;
  733. bool needs_probing = false;
  734. dn = of_find_matching_node(NULL, matches);
  735. if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
  736. needs_probing = true;
  737. of_node_put(dn);
  738. return needs_probing;
  739. }
  740. static int __init arch_timer_common_init(void)
  741. {
  742. unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
  743. /* Wait until both nodes are probed if we have two timers */
  744. if ((arch_timers_present & mask) != mask) {
  745. if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
  746. return 0;
  747. if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
  748. return 0;
  749. }
  750. arch_timer_banner(arch_timers_present);
  751. arch_counter_register(arch_timers_present);
  752. return arch_timer_arch_init();
  753. }
  754. static int __init arch_timer_init(void)
  755. {
  756. int ret;
  757. /*
  758. * If HYP mode is available, we know that the physical timer
  759. * has been configured to be accessible from PL1. Use it, so
  760. * that a guest can use the virtual timer instead.
  761. *
  762. * If no interrupt provided for virtual timer, we'll have to
  763. * stick to the physical timer. It'd better be accessible...
  764. *
  765. * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
  766. * accesses to CNTP_*_EL1 registers are silently redirected to
  767. * their CNTHP_*_EL2 counterparts, and use a different PPI
  768. * number.
  769. */
  770. if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
  771. bool has_ppi;
  772. if (is_kernel_in_hyp_mode()) {
  773. arch_timer_uses_ppi = HYP_PPI;
  774. has_ppi = !!arch_timer_ppi[HYP_PPI];
  775. } else {
  776. arch_timer_uses_ppi = PHYS_SECURE_PPI;
  777. has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
  778. !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
  779. }
  780. if (!has_ppi) {
  781. pr_warn("arch_timer: No interrupt available, giving up\n");
  782. return -EINVAL;
  783. }
  784. }
  785. ret = arch_timer_register();
  786. if (ret)
  787. return ret;
  788. ret = arch_timer_common_init();
  789. if (ret)
  790. return ret;
  791. arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
  792. return 0;
  793. }
  794. static int __init arch_timer_of_init(struct device_node *np)
  795. {
  796. int i;
  797. if (arch_timers_present & ARCH_CP15_TIMER) {
  798. pr_warn("arch_timer: multiple nodes in dt, skipping\n");
  799. return 0;
  800. }
  801. arch_timers_present |= ARCH_CP15_TIMER;
  802. for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
  803. arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
  804. arch_timer_detect_rate(NULL, np);
  805. arch_timer_c3stop = !of_property_read_bool(np, "always-on");
  806. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  807. for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
  808. if (of_property_read_bool(np, ool_workarounds[i].id)) {
  809. timer_unstable_counter_workaround = &ool_workarounds[i];
  810. static_branch_enable(&arch_timer_read_ool_enabled);
  811. pr_info("arch_timer: Enabling workaround for %s\n",
  812. timer_unstable_counter_workaround->id);
  813. break;
  814. }
  815. }
  816. #endif
  817. /*
  818. * If we cannot rely on firmware initializing the timer registers then
  819. * we should use the physical timers instead.
  820. */
  821. if (IS_ENABLED(CONFIG_ARM) &&
  822. of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
  823. arch_timer_uses_ppi = PHYS_SECURE_PPI;
  824. /* On some systems, the counter stops ticking when in suspend. */
  825. arch_counter_suspend_stop = of_property_read_bool(np,
  826. "arm,no-tick-in-suspend");
  827. return arch_timer_init();
  828. }
  829. CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
  830. CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
  831. static int __init arch_timer_mem_init(struct device_node *np)
  832. {
  833. struct device_node *frame, *best_frame = NULL;
  834. void __iomem *cntctlbase, *base;
  835. unsigned int irq, ret = -EINVAL;
  836. u32 cnttidr;
  837. arch_timers_present |= ARCH_MEM_TIMER;
  838. cntctlbase = of_iomap(np, 0);
  839. if (!cntctlbase) {
  840. pr_err("arch_timer: Can't find CNTCTLBase\n");
  841. return -ENXIO;
  842. }
  843. cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
  844. /*
  845. * Try to find a virtual capable frame. Otherwise fall back to a
  846. * physical capable frame.
  847. */
  848. for_each_available_child_of_node(np, frame) {
  849. int n;
  850. u32 cntacr;
  851. if (of_property_read_u32(frame, "frame-number", &n)) {
  852. pr_err("arch_timer: Missing frame-number\n");
  853. of_node_put(frame);
  854. goto out;
  855. }
  856. /* Try enabling everything, and see what sticks */
  857. cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
  858. CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
  859. writel_relaxed(cntacr, cntctlbase + CNTACR(n));
  860. cntacr = readl_relaxed(cntctlbase + CNTACR(n));
  861. if ((cnttidr & CNTTIDR_VIRT(n)) &&
  862. !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
  863. of_node_put(best_frame);
  864. best_frame = frame;
  865. arch_timer_mem_use_virtual = true;
  866. break;
  867. }
  868. if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
  869. continue;
  870. of_node_put(best_frame);
  871. best_frame = of_node_get(frame);
  872. }
  873. ret= -ENXIO;
  874. base = arch_counter_base = of_io_request_and_map(best_frame, 0,
  875. "arch_mem_timer");
  876. if (IS_ERR(base)) {
  877. pr_err("arch_timer: Can't map frame's registers\n");
  878. goto out;
  879. }
  880. if (arch_timer_mem_use_virtual)
  881. irq = irq_of_parse_and_map(best_frame, 1);
  882. else
  883. irq = irq_of_parse_and_map(best_frame, 0);
  884. ret = -EINVAL;
  885. if (!irq) {
  886. pr_err("arch_timer: Frame missing %s irq",
  887. arch_timer_mem_use_virtual ? "virt" : "phys");
  888. goto out;
  889. }
  890. arch_timer_detect_rate(base, np);
  891. ret = arch_timer_mem_register(base, irq);
  892. if (ret)
  893. goto out;
  894. return arch_timer_common_init();
  895. out:
  896. iounmap(cntctlbase);
  897. of_node_put(best_frame);
  898. return ret;
  899. }
  900. CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
  901. arch_timer_mem_init);
  902. #ifdef CONFIG_ACPI
  903. static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
  904. {
  905. int trigger, polarity;
  906. if (!interrupt)
  907. return 0;
  908. trigger = (flags & ACPI_GTDT_INTERRUPT_MODE) ? ACPI_EDGE_SENSITIVE
  909. : ACPI_LEVEL_SENSITIVE;
  910. polarity = (flags & ACPI_GTDT_INTERRUPT_POLARITY) ? ACPI_ACTIVE_LOW
  911. : ACPI_ACTIVE_HIGH;
  912. return acpi_register_gsi(NULL, interrupt, trigger, polarity);
  913. }
  914. /* Initialize per-processor generic timer */
  915. static int __init arch_timer_acpi_init(struct acpi_table_header *table)
  916. {
  917. struct acpi_table_gtdt *gtdt;
  918. if (arch_timers_present & ARCH_CP15_TIMER) {
  919. pr_warn("arch_timer: already initialized, skipping\n");
  920. return -EINVAL;
  921. }
  922. gtdt = container_of(table, struct acpi_table_gtdt, header);
  923. arch_timers_present |= ARCH_CP15_TIMER;
  924. arch_timer_ppi[PHYS_SECURE_PPI] =
  925. map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
  926. gtdt->secure_el1_flags);
  927. arch_timer_ppi[PHYS_NONSECURE_PPI] =
  928. map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
  929. gtdt->non_secure_el1_flags);
  930. arch_timer_ppi[VIRT_PPI] =
  931. map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
  932. gtdt->virtual_timer_flags);
  933. arch_timer_ppi[HYP_PPI] =
  934. map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
  935. gtdt->non_secure_el2_flags);
  936. /* Get the frequency from CNTFRQ */
  937. arch_timer_detect_rate(NULL, NULL);
  938. /* Always-on capability */
  939. arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
  940. arch_timer_init();
  941. return 0;
  942. }
  943. CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
  944. #endif