arm_arch_timer.c 32 KB

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