arm_arch_timer.c 24 KB

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