arm_arch_timer.c 27 KB

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