arm_arch_timer.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. #undef pr_fmt
  32. #define pr_fmt(fmt) "arch_timer: " fmt
  33. #define CNTTIDR 0x08
  34. #define CNTTIDR_VIRT(n) (BIT(1) << ((n) * 4))
  35. #define CNTACR(n) (0x40 + ((n) * 4))
  36. #define CNTACR_RPCT BIT(0)
  37. #define CNTACR_RVCT BIT(1)
  38. #define CNTACR_RFRQ BIT(2)
  39. #define CNTACR_RVOFF BIT(3)
  40. #define CNTACR_RWVT BIT(4)
  41. #define CNTACR_RWPT BIT(5)
  42. #define CNTVCT_LO 0x08
  43. #define CNTVCT_HI 0x0c
  44. #define CNTFRQ 0x10
  45. #define CNTP_TVAL 0x28
  46. #define CNTP_CTL 0x2c
  47. #define CNTV_TVAL 0x38
  48. #define CNTV_CTL 0x3c
  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. static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
  58. static struct clock_event_device __percpu *arch_timer_evt;
  59. static enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
  60. static bool arch_timer_c3stop;
  61. static bool arch_timer_mem_use_virtual;
  62. static bool arch_counter_suspend_stop;
  63. static bool vdso_default = true;
  64. static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
  65. static int __init early_evtstrm_cfg(char *buf)
  66. {
  67. return strtobool(buf, &evtstrm_enable);
  68. }
  69. early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
  70. /*
  71. * Architected system timer support.
  72. */
  73. static __always_inline
  74. void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
  75. struct clock_event_device *clk)
  76. {
  77. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  78. struct arch_timer *timer = to_arch_timer(clk);
  79. switch (reg) {
  80. case ARCH_TIMER_REG_CTRL:
  81. writel_relaxed(val, timer->base + CNTP_CTL);
  82. break;
  83. case ARCH_TIMER_REG_TVAL:
  84. writel_relaxed(val, timer->base + CNTP_TVAL);
  85. break;
  86. }
  87. } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
  88. struct arch_timer *timer = to_arch_timer(clk);
  89. switch (reg) {
  90. case ARCH_TIMER_REG_CTRL:
  91. writel_relaxed(val, timer->base + CNTV_CTL);
  92. break;
  93. case ARCH_TIMER_REG_TVAL:
  94. writel_relaxed(val, timer->base + CNTV_TVAL);
  95. break;
  96. }
  97. } else {
  98. arch_timer_reg_write_cp15(access, reg, val);
  99. }
  100. }
  101. static __always_inline
  102. u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
  103. struct clock_event_device *clk)
  104. {
  105. u32 val;
  106. if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
  107. struct arch_timer *timer = to_arch_timer(clk);
  108. switch (reg) {
  109. case ARCH_TIMER_REG_CTRL:
  110. val = readl_relaxed(timer->base + CNTP_CTL);
  111. break;
  112. case ARCH_TIMER_REG_TVAL:
  113. val = readl_relaxed(timer->base + CNTP_TVAL);
  114. break;
  115. }
  116. } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
  117. struct arch_timer *timer = to_arch_timer(clk);
  118. switch (reg) {
  119. case ARCH_TIMER_REG_CTRL:
  120. val = readl_relaxed(timer->base + CNTV_CTL);
  121. break;
  122. case ARCH_TIMER_REG_TVAL:
  123. val = readl_relaxed(timer->base + CNTV_TVAL);
  124. break;
  125. }
  126. } else {
  127. val = arch_timer_reg_read_cp15(access, reg);
  128. }
  129. return val;
  130. }
  131. /*
  132. * Default to cp15 based access because arm64 uses this function for
  133. * sched_clock() before DT is probed and the cp15 method is guaranteed
  134. * to exist on arm64. arm doesn't use this before DT is probed so even
  135. * if we don't have the cp15 accessors we won't have a problem.
  136. */
  137. u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
  138. static u64 arch_counter_read(struct clocksource *cs)
  139. {
  140. return arch_timer_read_counter();
  141. }
  142. static u64 arch_counter_read_cc(const struct cyclecounter *cc)
  143. {
  144. return arch_timer_read_counter();
  145. }
  146. static struct clocksource clocksource_counter = {
  147. .name = "arch_sys_counter",
  148. .rating = 400,
  149. .read = arch_counter_read,
  150. .mask = CLOCKSOURCE_MASK(56),
  151. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  152. };
  153. static struct cyclecounter cyclecounter __ro_after_init = {
  154. .read = arch_counter_read_cc,
  155. .mask = CLOCKSOURCE_MASK(56),
  156. };
  157. struct ate_acpi_oem_info {
  158. char oem_id[ACPI_OEM_ID_SIZE + 1];
  159. char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
  160. u32 oem_revision;
  161. };
  162. #ifdef CONFIG_FSL_ERRATUM_A008585
  163. /*
  164. * The number of retries is an arbitrary value well beyond the highest number
  165. * of iterations the loop has been observed to take.
  166. */
  167. #define __fsl_a008585_read_reg(reg) ({ \
  168. u64 _old, _new; \
  169. int _retries = 200; \
  170. \
  171. do { \
  172. _old = read_sysreg(reg); \
  173. _new = read_sysreg(reg); \
  174. _retries--; \
  175. } while (unlikely(_old != _new) && _retries); \
  176. \
  177. WARN_ON_ONCE(!_retries); \
  178. _new; \
  179. })
  180. static u32 notrace fsl_a008585_read_cntp_tval_el0(void)
  181. {
  182. return __fsl_a008585_read_reg(cntp_tval_el0);
  183. }
  184. static u32 notrace fsl_a008585_read_cntv_tval_el0(void)
  185. {
  186. return __fsl_a008585_read_reg(cntv_tval_el0);
  187. }
  188. static u64 notrace fsl_a008585_read_cntvct_el0(void)
  189. {
  190. return __fsl_a008585_read_reg(cntvct_el0);
  191. }
  192. #endif
  193. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  194. /*
  195. * Verify whether the value of the second read is larger than the first by
  196. * less than 32 is the only way to confirm the value is correct, so clear the
  197. * lower 5 bits to check whether the difference is greater than 32 or not.
  198. * Theoretically the erratum should not occur more than twice in succession
  199. * when reading the system counter, but it is possible that some interrupts
  200. * may lead to more than twice read errors, triggering the warning, so setting
  201. * the number of retries far beyond the number of iterations the loop has been
  202. * observed to take.
  203. */
  204. #define __hisi_161010101_read_reg(reg) ({ \
  205. u64 _old, _new; \
  206. int _retries = 50; \
  207. \
  208. do { \
  209. _old = read_sysreg(reg); \
  210. _new = read_sysreg(reg); \
  211. _retries--; \
  212. } while (unlikely((_new - _old) >> 5) && _retries); \
  213. \
  214. WARN_ON_ONCE(!_retries); \
  215. _new; \
  216. })
  217. static u32 notrace hisi_161010101_read_cntp_tval_el0(void)
  218. {
  219. return __hisi_161010101_read_reg(cntp_tval_el0);
  220. }
  221. static u32 notrace hisi_161010101_read_cntv_tval_el0(void)
  222. {
  223. return __hisi_161010101_read_reg(cntv_tval_el0);
  224. }
  225. static u64 notrace hisi_161010101_read_cntvct_el0(void)
  226. {
  227. return __hisi_161010101_read_reg(cntvct_el0);
  228. }
  229. static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
  230. /*
  231. * Note that trailing spaces are required to properly match
  232. * the OEM table information.
  233. */
  234. {
  235. .oem_id = "HISI ",
  236. .oem_table_id = "HIP05 ",
  237. .oem_revision = 0,
  238. },
  239. {
  240. .oem_id = "HISI ",
  241. .oem_table_id = "HIP06 ",
  242. .oem_revision = 0,
  243. },
  244. {
  245. .oem_id = "HISI ",
  246. .oem_table_id = "HIP07 ",
  247. .oem_revision = 0,
  248. },
  249. { /* Sentinel indicating the end of the OEM array */ },
  250. };
  251. #endif
  252. #ifdef CONFIG_ARM64_ERRATUM_858921
  253. static u64 notrace arm64_858921_read_cntvct_el0(void)
  254. {
  255. u64 old, new;
  256. old = read_sysreg(cntvct_el0);
  257. new = read_sysreg(cntvct_el0);
  258. return (((old ^ new) >> 32) & 1) ? old : new;
  259. }
  260. #endif
  261. #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
  262. DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
  263. timer_unstable_counter_workaround);
  264. EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
  265. DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
  266. EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
  267. static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
  268. struct clock_event_device *clk)
  269. {
  270. unsigned long ctrl;
  271. u64 cval = evt + arch_counter_get_cntvct();
  272. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  273. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  274. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  275. if (access == ARCH_TIMER_PHYS_ACCESS)
  276. write_sysreg(cval, cntp_cval_el0);
  277. else
  278. write_sysreg(cval, cntv_cval_el0);
  279. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  280. }
  281. static __maybe_unused int erratum_set_next_event_tval_virt(unsigned long evt,
  282. struct clock_event_device *clk)
  283. {
  284. erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  285. return 0;
  286. }
  287. static __maybe_unused int erratum_set_next_event_tval_phys(unsigned long evt,
  288. struct clock_event_device *clk)
  289. {
  290. erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  291. return 0;
  292. }
  293. static const struct arch_timer_erratum_workaround ool_workarounds[] = {
  294. #ifdef CONFIG_FSL_ERRATUM_A008585
  295. {
  296. .match_type = ate_match_dt,
  297. .id = "fsl,erratum-a008585",
  298. .desc = "Freescale erratum a005858",
  299. .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
  300. .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
  301. .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
  302. .set_next_event_phys = erratum_set_next_event_tval_phys,
  303. .set_next_event_virt = erratum_set_next_event_tval_virt,
  304. },
  305. #endif
  306. #ifdef CONFIG_HISILICON_ERRATUM_161010101
  307. {
  308. .match_type = ate_match_dt,
  309. .id = "hisilicon,erratum-161010101",
  310. .desc = "HiSilicon erratum 161010101",
  311. .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
  312. .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
  313. .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
  314. .set_next_event_phys = erratum_set_next_event_tval_phys,
  315. .set_next_event_virt = erratum_set_next_event_tval_virt,
  316. },
  317. {
  318. .match_type = ate_match_acpi_oem_info,
  319. .id = hisi_161010101_oem_info,
  320. .desc = "HiSilicon erratum 161010101",
  321. .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
  322. .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
  323. .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
  324. .set_next_event_phys = erratum_set_next_event_tval_phys,
  325. .set_next_event_virt = erratum_set_next_event_tval_virt,
  326. },
  327. #endif
  328. #ifdef CONFIG_ARM64_ERRATUM_858921
  329. {
  330. .match_type = ate_match_local_cap_id,
  331. .id = (void *)ARM64_WORKAROUND_858921,
  332. .desc = "ARM erratum 858921",
  333. .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
  334. },
  335. #endif
  336. };
  337. typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
  338. const void *);
  339. static
  340. bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
  341. const void *arg)
  342. {
  343. const struct device_node *np = arg;
  344. return of_property_read_bool(np, wa->id);
  345. }
  346. static
  347. bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
  348. const void *arg)
  349. {
  350. return this_cpu_has_cap((uintptr_t)wa->id);
  351. }
  352. static
  353. bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
  354. const void *arg)
  355. {
  356. static const struct ate_acpi_oem_info empty_oem_info = {};
  357. const struct ate_acpi_oem_info *info = wa->id;
  358. const struct acpi_table_header *table = arg;
  359. /* Iterate over the ACPI OEM info array, looking for a match */
  360. while (memcmp(info, &empty_oem_info, sizeof(*info))) {
  361. if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
  362. !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
  363. info->oem_revision == table->oem_revision)
  364. return true;
  365. info++;
  366. }
  367. return false;
  368. }
  369. static const struct arch_timer_erratum_workaround *
  370. arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
  371. ate_match_fn_t match_fn,
  372. void *arg)
  373. {
  374. int i;
  375. for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
  376. if (ool_workarounds[i].match_type != type)
  377. continue;
  378. if (match_fn(&ool_workarounds[i], arg))
  379. return &ool_workarounds[i];
  380. }
  381. return NULL;
  382. }
  383. static
  384. void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
  385. bool local)
  386. {
  387. int i;
  388. if (local) {
  389. __this_cpu_write(timer_unstable_counter_workaround, wa);
  390. } else {
  391. for_each_possible_cpu(i)
  392. per_cpu(timer_unstable_counter_workaround, i) = wa;
  393. }
  394. static_branch_enable(&arch_timer_read_ool_enabled);
  395. /*
  396. * Don't use the vdso fastpath if errata require using the
  397. * out-of-line counter accessor. We may change our mind pretty
  398. * late in the game (with a per-CPU erratum, for example), so
  399. * change both the default value and the vdso itself.
  400. */
  401. if (wa->read_cntvct_el0) {
  402. clocksource_counter.archdata.vdso_direct = false;
  403. vdso_default = false;
  404. }
  405. }
  406. static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
  407. void *arg)
  408. {
  409. const struct arch_timer_erratum_workaround *wa;
  410. ate_match_fn_t match_fn = NULL;
  411. bool local = false;
  412. switch (type) {
  413. case ate_match_dt:
  414. match_fn = arch_timer_check_dt_erratum;
  415. break;
  416. case ate_match_local_cap_id:
  417. match_fn = arch_timer_check_local_cap_erratum;
  418. local = true;
  419. break;
  420. case ate_match_acpi_oem_info:
  421. match_fn = arch_timer_check_acpi_oem_erratum;
  422. break;
  423. default:
  424. WARN_ON(1);
  425. return;
  426. }
  427. wa = arch_timer_iterate_errata(type, match_fn, arg);
  428. if (!wa)
  429. return;
  430. if (needs_unstable_timer_counter_workaround()) {
  431. const struct arch_timer_erratum_workaround *__wa;
  432. __wa = __this_cpu_read(timer_unstable_counter_workaround);
  433. if (__wa && wa != __wa)
  434. pr_warn("Can't enable workaround for %s (clashes with %s\n)",
  435. wa->desc, __wa->desc);
  436. if (__wa)
  437. return;
  438. }
  439. arch_timer_enable_workaround(wa, local);
  440. pr_info("Enabling %s workaround for %s\n",
  441. local ? "local" : "global", wa->desc);
  442. }
  443. #define erratum_handler(fn, r, ...) \
  444. ({ \
  445. bool __val; \
  446. if (needs_unstable_timer_counter_workaround()) { \
  447. const struct arch_timer_erratum_workaround *__wa; \
  448. __wa = __this_cpu_read(timer_unstable_counter_workaround); \
  449. if (__wa && __wa->fn) { \
  450. r = __wa->fn(__VA_ARGS__); \
  451. __val = true; \
  452. } else { \
  453. __val = false; \
  454. } \
  455. } else { \
  456. __val = false; \
  457. } \
  458. __val; \
  459. })
  460. static bool arch_timer_this_cpu_has_cntvct_wa(void)
  461. {
  462. const struct arch_timer_erratum_workaround *wa;
  463. wa = __this_cpu_read(timer_unstable_counter_workaround);
  464. return wa && wa->read_cntvct_el0;
  465. }
  466. #else
  467. #define arch_timer_check_ool_workaround(t,a) do { } while(0)
  468. #define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
  469. #define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
  470. #define erratum_handler(fn, r, ...) ({false;})
  471. #define arch_timer_this_cpu_has_cntvct_wa() ({false;})
  472. #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
  473. static __always_inline irqreturn_t timer_handler(const int access,
  474. struct clock_event_device *evt)
  475. {
  476. unsigned long ctrl;
  477. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
  478. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  479. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  480. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
  481. evt->event_handler(evt);
  482. return IRQ_HANDLED;
  483. }
  484. return IRQ_NONE;
  485. }
  486. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  487. {
  488. struct clock_event_device *evt = dev_id;
  489. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  490. }
  491. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  492. {
  493. struct clock_event_device *evt = dev_id;
  494. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  495. }
  496. static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
  497. {
  498. struct clock_event_device *evt = dev_id;
  499. return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
  500. }
  501. static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
  502. {
  503. struct clock_event_device *evt = dev_id;
  504. return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
  505. }
  506. static __always_inline int timer_shutdown(const int access,
  507. struct clock_event_device *clk)
  508. {
  509. unsigned long ctrl;
  510. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  511. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  512. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  513. return 0;
  514. }
  515. static int arch_timer_shutdown_virt(struct clock_event_device *clk)
  516. {
  517. return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
  518. }
  519. static int arch_timer_shutdown_phys(struct clock_event_device *clk)
  520. {
  521. return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
  522. }
  523. static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
  524. {
  525. return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
  526. }
  527. static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
  528. {
  529. return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
  530. }
  531. static __always_inline void set_next_event(const int access, unsigned long evt,
  532. struct clock_event_device *clk)
  533. {
  534. unsigned long ctrl;
  535. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  536. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  537. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  538. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
  539. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  540. }
  541. static int arch_timer_set_next_event_virt(unsigned long evt,
  542. struct clock_event_device *clk)
  543. {
  544. int ret;
  545. if (erratum_handler(set_next_event_virt, ret, evt, clk))
  546. return ret;
  547. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  548. return 0;
  549. }
  550. static int arch_timer_set_next_event_phys(unsigned long evt,
  551. struct clock_event_device *clk)
  552. {
  553. int ret;
  554. if (erratum_handler(set_next_event_phys, ret, evt, clk))
  555. return ret;
  556. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  557. return 0;
  558. }
  559. static int arch_timer_set_next_event_virt_mem(unsigned long evt,
  560. struct clock_event_device *clk)
  561. {
  562. set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
  563. return 0;
  564. }
  565. static int arch_timer_set_next_event_phys_mem(unsigned long evt,
  566. struct clock_event_device *clk)
  567. {
  568. set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
  569. return 0;
  570. }
  571. static void __arch_timer_setup(unsigned type,
  572. struct clock_event_device *clk)
  573. {
  574. clk->features = CLOCK_EVT_FEAT_ONESHOT;
  575. if (type == ARCH_TIMER_TYPE_CP15) {
  576. if (arch_timer_c3stop)
  577. clk->features |= CLOCK_EVT_FEAT_C3STOP;
  578. clk->name = "arch_sys_timer";
  579. clk->rating = 450;
  580. clk->cpumask = cpumask_of(smp_processor_id());
  581. clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
  582. switch (arch_timer_uses_ppi) {
  583. case ARCH_TIMER_VIRT_PPI:
  584. clk->set_state_shutdown = arch_timer_shutdown_virt;
  585. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
  586. clk->set_next_event = arch_timer_set_next_event_virt;
  587. break;
  588. case ARCH_TIMER_PHYS_SECURE_PPI:
  589. case ARCH_TIMER_PHYS_NONSECURE_PPI:
  590. case ARCH_TIMER_HYP_PPI:
  591. clk->set_state_shutdown = arch_timer_shutdown_phys;
  592. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
  593. clk->set_next_event = arch_timer_set_next_event_phys;
  594. break;
  595. default:
  596. BUG();
  597. }
  598. arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
  599. } else {
  600. clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
  601. clk->name = "arch_mem_timer";
  602. clk->rating = 400;
  603. clk->cpumask = cpu_all_mask;
  604. if (arch_timer_mem_use_virtual) {
  605. clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
  606. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
  607. clk->set_next_event =
  608. arch_timer_set_next_event_virt_mem;
  609. } else {
  610. clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
  611. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
  612. clk->set_next_event =
  613. arch_timer_set_next_event_phys_mem;
  614. }
  615. }
  616. clk->set_state_shutdown(clk);
  617. clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
  618. }
  619. static void arch_timer_evtstrm_enable(int divider)
  620. {
  621. u32 cntkctl = arch_timer_get_cntkctl();
  622. cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
  623. /* Set the divider and enable virtual event stream */
  624. cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
  625. | ARCH_TIMER_VIRT_EVT_EN;
  626. arch_timer_set_cntkctl(cntkctl);
  627. elf_hwcap |= HWCAP_EVTSTRM;
  628. #ifdef CONFIG_COMPAT
  629. compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
  630. #endif
  631. }
  632. static void arch_timer_configure_evtstream(void)
  633. {
  634. int evt_stream_div, pos;
  635. /* Find the closest power of two to the divisor */
  636. evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
  637. pos = fls(evt_stream_div);
  638. if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
  639. pos--;
  640. /* enable event stream */
  641. arch_timer_evtstrm_enable(min(pos, 15));
  642. }
  643. static void arch_counter_set_user_access(void)
  644. {
  645. u32 cntkctl = arch_timer_get_cntkctl();
  646. /* Disable user access to the timers and both counters */
  647. /* Also disable virtual event stream */
  648. cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
  649. | ARCH_TIMER_USR_VT_ACCESS_EN
  650. | ARCH_TIMER_USR_VCT_ACCESS_EN
  651. | ARCH_TIMER_VIRT_EVT_EN
  652. | ARCH_TIMER_USR_PCT_ACCESS_EN);
  653. /*
  654. * Enable user access to the virtual counter if it doesn't
  655. * need to be workaround. The vdso may have been already
  656. * disabled though.
  657. */
  658. if (arch_timer_this_cpu_has_cntvct_wa())
  659. pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
  660. else
  661. cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
  662. arch_timer_set_cntkctl(cntkctl);
  663. }
  664. static bool arch_timer_has_nonsecure_ppi(void)
  665. {
  666. return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
  667. arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  668. }
  669. static u32 check_ppi_trigger(int irq)
  670. {
  671. u32 flags = irq_get_trigger_type(irq);
  672. if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
  673. pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
  674. pr_warn("WARNING: Please fix your firmware\n");
  675. flags = IRQF_TRIGGER_LOW;
  676. }
  677. return flags;
  678. }
  679. static int arch_timer_starting_cpu(unsigned int cpu)
  680. {
  681. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  682. u32 flags;
  683. __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
  684. flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
  685. enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
  686. if (arch_timer_has_nonsecure_ppi()) {
  687. flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  688. enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
  689. flags);
  690. }
  691. arch_counter_set_user_access();
  692. if (evtstrm_enable)
  693. arch_timer_configure_evtstream();
  694. return 0;
  695. }
  696. /*
  697. * For historical reasons, when probing with DT we use whichever (non-zero)
  698. * rate was probed first, and don't verify that others match. If the first node
  699. * probed has a clock-frequency property, this overrides the HW register.
  700. */
  701. static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
  702. {
  703. /* Who has more than one independent system counter? */
  704. if (arch_timer_rate)
  705. return;
  706. if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
  707. arch_timer_rate = rate;
  708. /* Check the timer frequency. */
  709. if (arch_timer_rate == 0)
  710. pr_warn("frequency not available\n");
  711. }
  712. static void arch_timer_banner(unsigned type)
  713. {
  714. pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
  715. type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
  716. type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
  717. " and " : "",
  718. type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
  719. (unsigned long)arch_timer_rate / 1000000,
  720. (unsigned long)(arch_timer_rate / 10000) % 100,
  721. type & ARCH_TIMER_TYPE_CP15 ?
  722. (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
  723. "",
  724. type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
  725. type & ARCH_TIMER_TYPE_MEM ?
  726. arch_timer_mem_use_virtual ? "virt" : "phys" :
  727. "");
  728. }
  729. u32 arch_timer_get_rate(void)
  730. {
  731. return arch_timer_rate;
  732. }
  733. static u64 arch_counter_get_cntvct_mem(void)
  734. {
  735. u32 vct_lo, vct_hi, tmp_hi;
  736. do {
  737. vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  738. vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
  739. tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  740. } while (vct_hi != tmp_hi);
  741. return ((u64) vct_hi << 32) | vct_lo;
  742. }
  743. static struct arch_timer_kvm_info arch_timer_kvm_info;
  744. struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
  745. {
  746. return &arch_timer_kvm_info;
  747. }
  748. static void __init arch_counter_register(unsigned type)
  749. {
  750. u64 start_count;
  751. /* Register the CP15 based counter if we have one */
  752. if (type & ARCH_TIMER_TYPE_CP15) {
  753. if (IS_ENABLED(CONFIG_ARM64) ||
  754. arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
  755. arch_timer_read_counter = arch_counter_get_cntvct;
  756. else
  757. arch_timer_read_counter = arch_counter_get_cntpct;
  758. clocksource_counter.archdata.vdso_direct = vdso_default;
  759. } else {
  760. arch_timer_read_counter = arch_counter_get_cntvct_mem;
  761. }
  762. if (!arch_counter_suspend_stop)
  763. clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
  764. start_count = arch_timer_read_counter();
  765. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  766. cyclecounter.mult = clocksource_counter.mult;
  767. cyclecounter.shift = clocksource_counter.shift;
  768. timecounter_init(&arch_timer_kvm_info.timecounter,
  769. &cyclecounter, start_count);
  770. /* 56 bits minimum, so we assume worst case rollover */
  771. sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
  772. }
  773. static void arch_timer_stop(struct clock_event_device *clk)
  774. {
  775. pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
  776. disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
  777. if (arch_timer_has_nonsecure_ppi())
  778. disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  779. clk->set_state_shutdown(clk);
  780. }
  781. static int arch_timer_dying_cpu(unsigned int cpu)
  782. {
  783. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  784. arch_timer_stop(clk);
  785. return 0;
  786. }
  787. #ifdef CONFIG_CPU_PM
  788. static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
  789. static int arch_timer_cpu_pm_notify(struct notifier_block *self,
  790. unsigned long action, void *hcpu)
  791. {
  792. if (action == CPU_PM_ENTER)
  793. __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
  794. else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
  795. arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
  796. return NOTIFY_OK;
  797. }
  798. static struct notifier_block arch_timer_cpu_pm_notifier = {
  799. .notifier_call = arch_timer_cpu_pm_notify,
  800. };
  801. static int __init arch_timer_cpu_pm_init(void)
  802. {
  803. return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
  804. }
  805. static void __init arch_timer_cpu_pm_deinit(void)
  806. {
  807. WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
  808. }
  809. #else
  810. static int __init arch_timer_cpu_pm_init(void)
  811. {
  812. return 0;
  813. }
  814. static void __init arch_timer_cpu_pm_deinit(void)
  815. {
  816. }
  817. #endif
  818. static int __init arch_timer_register(void)
  819. {
  820. int err;
  821. int ppi;
  822. arch_timer_evt = alloc_percpu(struct clock_event_device);
  823. if (!arch_timer_evt) {
  824. err = -ENOMEM;
  825. goto out;
  826. }
  827. ppi = arch_timer_ppi[arch_timer_uses_ppi];
  828. switch (arch_timer_uses_ppi) {
  829. case ARCH_TIMER_VIRT_PPI:
  830. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  831. "arch_timer", arch_timer_evt);
  832. break;
  833. case ARCH_TIMER_PHYS_SECURE_PPI:
  834. case ARCH_TIMER_PHYS_NONSECURE_PPI:
  835. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  836. "arch_timer", arch_timer_evt);
  837. if (!err && arch_timer_has_nonsecure_ppi()) {
  838. ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
  839. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  840. "arch_timer", arch_timer_evt);
  841. if (err)
  842. free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
  843. arch_timer_evt);
  844. }
  845. break;
  846. case ARCH_TIMER_HYP_PPI:
  847. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  848. "arch_timer", arch_timer_evt);
  849. break;
  850. default:
  851. BUG();
  852. }
  853. if (err) {
  854. pr_err("can't register interrupt %d (%d)\n", ppi, err);
  855. goto out_free;
  856. }
  857. err = arch_timer_cpu_pm_init();
  858. if (err)
  859. goto out_unreg_notify;
  860. /* Register and immediately configure the timer on the boot CPU */
  861. err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
  862. "clockevents/arm/arch_timer:starting",
  863. arch_timer_starting_cpu, arch_timer_dying_cpu);
  864. if (err)
  865. goto out_unreg_cpupm;
  866. return 0;
  867. out_unreg_cpupm:
  868. arch_timer_cpu_pm_deinit();
  869. out_unreg_notify:
  870. free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
  871. if (arch_timer_has_nonsecure_ppi())
  872. free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
  873. arch_timer_evt);
  874. out_free:
  875. free_percpu(arch_timer_evt);
  876. out:
  877. return err;
  878. }
  879. static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
  880. {
  881. int ret;
  882. irq_handler_t func;
  883. struct arch_timer *t;
  884. t = kzalloc(sizeof(*t), GFP_KERNEL);
  885. if (!t)
  886. return -ENOMEM;
  887. t->base = base;
  888. t->evt.irq = irq;
  889. __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
  890. if (arch_timer_mem_use_virtual)
  891. func = arch_timer_handler_virt_mem;
  892. else
  893. func = arch_timer_handler_phys_mem;
  894. ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
  895. if (ret) {
  896. pr_err("Failed to request mem timer irq\n");
  897. kfree(t);
  898. }
  899. return ret;
  900. }
  901. static const struct of_device_id arch_timer_of_match[] __initconst = {
  902. { .compatible = "arm,armv7-timer", },
  903. { .compatible = "arm,armv8-timer", },
  904. {},
  905. };
  906. static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
  907. { .compatible = "arm,armv7-timer-mem", },
  908. {},
  909. };
  910. static bool __init arch_timer_needs_of_probing(void)
  911. {
  912. struct device_node *dn;
  913. bool needs_probing = false;
  914. unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
  915. /* We have two timers, and both device-tree nodes are probed. */
  916. if ((arch_timers_present & mask) == mask)
  917. return false;
  918. /*
  919. * Only one type of timer is probed,
  920. * check if we have another type of timer node in device-tree.
  921. */
  922. if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
  923. dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
  924. else
  925. dn = of_find_matching_node(NULL, arch_timer_of_match);
  926. if (dn && of_device_is_available(dn))
  927. needs_probing = true;
  928. of_node_put(dn);
  929. return needs_probing;
  930. }
  931. static int __init arch_timer_common_init(void)
  932. {
  933. arch_timer_banner(arch_timers_present);
  934. arch_counter_register(arch_timers_present);
  935. return arch_timer_arch_init();
  936. }
  937. /**
  938. * arch_timer_select_ppi() - Select suitable PPI for the current system.
  939. *
  940. * If HYP mode is available, we know that the physical timer
  941. * has been configured to be accessible from PL1. Use it, so
  942. * that a guest can use the virtual timer instead.
  943. *
  944. * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
  945. * accesses to CNTP_*_EL1 registers are silently redirected to
  946. * their CNTHP_*_EL2 counterparts, and use a different PPI
  947. * number.
  948. *
  949. * If no interrupt provided for virtual timer, we'll have to
  950. * stick to the physical timer. It'd better be accessible...
  951. * For arm64 we never use the secure interrupt.
  952. *
  953. * Return: a suitable PPI type for the current system.
  954. */
  955. static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
  956. {
  957. if (is_kernel_in_hyp_mode())
  958. return ARCH_TIMER_HYP_PPI;
  959. if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
  960. return ARCH_TIMER_VIRT_PPI;
  961. if (IS_ENABLED(CONFIG_ARM64))
  962. return ARCH_TIMER_PHYS_NONSECURE_PPI;
  963. return ARCH_TIMER_PHYS_SECURE_PPI;
  964. }
  965. static int __init arch_timer_of_init(struct device_node *np)
  966. {
  967. int i, ret;
  968. u32 rate;
  969. if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
  970. pr_warn("multiple nodes in dt, skipping\n");
  971. return 0;
  972. }
  973. arch_timers_present |= ARCH_TIMER_TYPE_CP15;
  974. for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
  975. arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
  976. arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
  977. rate = arch_timer_get_cntfrq();
  978. arch_timer_of_configure_rate(rate, np);
  979. arch_timer_c3stop = !of_property_read_bool(np, "always-on");
  980. /* Check for globally applicable workarounds */
  981. arch_timer_check_ool_workaround(ate_match_dt, np);
  982. /*
  983. * If we cannot rely on firmware initializing the timer registers then
  984. * we should use the physical timers instead.
  985. */
  986. if (IS_ENABLED(CONFIG_ARM) &&
  987. of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
  988. arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
  989. else
  990. arch_timer_uses_ppi = arch_timer_select_ppi();
  991. if (!arch_timer_ppi[arch_timer_uses_ppi]) {
  992. pr_err("No interrupt available, giving up\n");
  993. return -EINVAL;
  994. }
  995. /* On some systems, the counter stops ticking when in suspend. */
  996. arch_counter_suspend_stop = of_property_read_bool(np,
  997. "arm,no-tick-in-suspend");
  998. ret = arch_timer_register();
  999. if (ret)
  1000. return ret;
  1001. if (arch_timer_needs_of_probing())
  1002. return 0;
  1003. return arch_timer_common_init();
  1004. }
  1005. CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
  1006. CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
  1007. static u32 __init
  1008. arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
  1009. {
  1010. void __iomem *base;
  1011. u32 rate;
  1012. base = ioremap(frame->cntbase, frame->size);
  1013. if (!base) {
  1014. pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
  1015. return 0;
  1016. }
  1017. rate = readl_relaxed(frame + CNTFRQ);
  1018. iounmap(frame);
  1019. return rate;
  1020. }
  1021. static struct arch_timer_mem_frame * __init
  1022. arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
  1023. {
  1024. struct arch_timer_mem_frame *frame, *best_frame = NULL;
  1025. void __iomem *cntctlbase;
  1026. u32 cnttidr;
  1027. int i;
  1028. cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
  1029. if (!cntctlbase) {
  1030. pr_err("Can't map CNTCTLBase @ %pa\n",
  1031. &timer_mem->cntctlbase);
  1032. return NULL;
  1033. }
  1034. cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
  1035. /*
  1036. * Try to find a virtual capable frame. Otherwise fall back to a
  1037. * physical capable frame.
  1038. */
  1039. for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
  1040. u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
  1041. CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
  1042. frame = &timer_mem->frame[i];
  1043. if (!frame->valid)
  1044. continue;
  1045. /* Try enabling everything, and see what sticks */
  1046. writel_relaxed(cntacr, cntctlbase + CNTACR(i));
  1047. cntacr = readl_relaxed(cntctlbase + CNTACR(i));
  1048. if ((cnttidr & CNTTIDR_VIRT(i)) &&
  1049. !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
  1050. best_frame = frame;
  1051. arch_timer_mem_use_virtual = true;
  1052. break;
  1053. }
  1054. if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
  1055. continue;
  1056. best_frame = frame;
  1057. }
  1058. iounmap(cntctlbase);
  1059. if (!best_frame)
  1060. pr_err("Unable to find a suitable frame in timer @ %pa\n",
  1061. &timer_mem->cntctlbase);
  1062. return best_frame;
  1063. }
  1064. static int __init
  1065. arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
  1066. {
  1067. void __iomem *base;
  1068. int ret, irq = 0;
  1069. if (arch_timer_mem_use_virtual)
  1070. irq = frame->virt_irq;
  1071. else
  1072. irq = frame->phys_irq;
  1073. if (!irq) {
  1074. pr_err("Frame missing %s irq.\n",
  1075. arch_timer_mem_use_virtual ? "virt" : "phys");
  1076. return -EINVAL;
  1077. }
  1078. if (!request_mem_region(frame->cntbase, frame->size,
  1079. "arch_mem_timer"))
  1080. return -EBUSY;
  1081. base = ioremap(frame->cntbase, frame->size);
  1082. if (!base) {
  1083. pr_err("Can't map frame's registers\n");
  1084. return -ENXIO;
  1085. }
  1086. ret = arch_timer_mem_register(base, irq);
  1087. if (ret) {
  1088. iounmap(base);
  1089. return ret;
  1090. }
  1091. arch_counter_base = base;
  1092. arch_timers_present |= ARCH_TIMER_TYPE_MEM;
  1093. return 0;
  1094. }
  1095. static int __init arch_timer_mem_of_init(struct device_node *np)
  1096. {
  1097. struct arch_timer_mem *timer_mem;
  1098. struct arch_timer_mem_frame *frame;
  1099. struct device_node *frame_node;
  1100. struct resource res;
  1101. int ret = -EINVAL;
  1102. u32 rate;
  1103. timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
  1104. if (!timer_mem)
  1105. return -ENOMEM;
  1106. if (of_address_to_resource(np, 0, &res))
  1107. goto out;
  1108. timer_mem->cntctlbase = res.start;
  1109. timer_mem->size = resource_size(&res);
  1110. for_each_available_child_of_node(np, frame_node) {
  1111. u32 n;
  1112. struct arch_timer_mem_frame *frame;
  1113. if (of_property_read_u32(frame_node, "frame-number", &n)) {
  1114. pr_err(FW_BUG "Missing frame-number.\n");
  1115. of_node_put(frame_node);
  1116. goto out;
  1117. }
  1118. if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
  1119. pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
  1120. ARCH_TIMER_MEM_MAX_FRAMES - 1);
  1121. of_node_put(frame_node);
  1122. goto out;
  1123. }
  1124. frame = &timer_mem->frame[n];
  1125. if (frame->valid) {
  1126. pr_err(FW_BUG "Duplicated frame-number.\n");
  1127. of_node_put(frame_node);
  1128. goto out;
  1129. }
  1130. if (of_address_to_resource(frame_node, 0, &res)) {
  1131. of_node_put(frame_node);
  1132. goto out;
  1133. }
  1134. frame->cntbase = res.start;
  1135. frame->size = resource_size(&res);
  1136. frame->virt_irq = irq_of_parse_and_map(frame_node,
  1137. ARCH_TIMER_VIRT_SPI);
  1138. frame->phys_irq = irq_of_parse_and_map(frame_node,
  1139. ARCH_TIMER_PHYS_SPI);
  1140. frame->valid = true;
  1141. }
  1142. frame = arch_timer_mem_find_best_frame(timer_mem);
  1143. if (!frame) {
  1144. ret = -EINVAL;
  1145. goto out;
  1146. }
  1147. rate = arch_timer_mem_frame_get_cntfrq(frame);
  1148. arch_timer_of_configure_rate(rate, np);
  1149. ret = arch_timer_mem_frame_register(frame);
  1150. if (!ret && !arch_timer_needs_of_probing())
  1151. ret = arch_timer_common_init();
  1152. out:
  1153. kfree(timer_mem);
  1154. return ret;
  1155. }
  1156. CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
  1157. arch_timer_mem_of_init);
  1158. #ifdef CONFIG_ACPI_GTDT
  1159. static int __init
  1160. arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
  1161. {
  1162. struct arch_timer_mem_frame *frame;
  1163. u32 rate;
  1164. int i;
  1165. for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
  1166. frame = &timer_mem->frame[i];
  1167. if (!frame->valid)
  1168. continue;
  1169. rate = arch_timer_mem_frame_get_cntfrq(frame);
  1170. if (rate == arch_timer_rate)
  1171. continue;
  1172. pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
  1173. &frame->cntbase,
  1174. (unsigned long)rate, (unsigned long)arch_timer_rate);
  1175. return -EINVAL;
  1176. }
  1177. return 0;
  1178. }
  1179. static int __init arch_timer_mem_acpi_init(int platform_timer_count)
  1180. {
  1181. struct arch_timer_mem *timers, *timer;
  1182. struct arch_timer_mem_frame *frame;
  1183. int timer_count, i, ret = 0;
  1184. timers = kcalloc(platform_timer_count, sizeof(*timers),
  1185. GFP_KERNEL);
  1186. if (!timers)
  1187. return -ENOMEM;
  1188. ret = acpi_arch_timer_mem_init(timers, &timer_count);
  1189. if (ret || !timer_count)
  1190. goto out;
  1191. for (i = 0; i < timer_count; i++) {
  1192. ret = arch_timer_mem_verify_cntfrq(&timers[i]);
  1193. if (ret) {
  1194. pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
  1195. goto out;
  1196. }
  1197. }
  1198. /*
  1199. * While unlikely, it's theoretically possible that none of the frames
  1200. * in a timer expose the combination of feature we want.
  1201. */
  1202. for (i = i; i < timer_count; i++) {
  1203. timer = &timers[i];
  1204. frame = arch_timer_mem_find_best_frame(timer);
  1205. if (frame)
  1206. break;
  1207. }
  1208. if (frame)
  1209. ret = arch_timer_mem_frame_register(frame);
  1210. out:
  1211. kfree(timers);
  1212. return ret;
  1213. }
  1214. /* Initialize per-processor generic timer and memory-mapped timer(if present) */
  1215. static int __init arch_timer_acpi_init(struct acpi_table_header *table)
  1216. {
  1217. int ret, platform_timer_count;
  1218. if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
  1219. pr_warn("already initialized, skipping\n");
  1220. return -EINVAL;
  1221. }
  1222. arch_timers_present |= ARCH_TIMER_TYPE_CP15;
  1223. ret = acpi_gtdt_init(table, &platform_timer_count);
  1224. if (ret) {
  1225. pr_err("Failed to init GTDT table.\n");
  1226. return ret;
  1227. }
  1228. arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
  1229. acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
  1230. arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
  1231. acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
  1232. arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
  1233. acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
  1234. arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
  1235. /*
  1236. * When probing via ACPI, we have no mechanism to override the sysreg
  1237. * CNTFRQ value. This *must* be correct.
  1238. */
  1239. arch_timer_rate = arch_timer_get_cntfrq();
  1240. if (!arch_timer_rate) {
  1241. pr_err(FW_BUG "frequency not available.\n");
  1242. return -EINVAL;
  1243. }
  1244. arch_timer_uses_ppi = arch_timer_select_ppi();
  1245. if (!arch_timer_ppi[arch_timer_uses_ppi]) {
  1246. pr_err("No interrupt available, giving up\n");
  1247. return -EINVAL;
  1248. }
  1249. /* Always-on capability */
  1250. arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
  1251. /* Check for globally applicable workarounds */
  1252. arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
  1253. ret = arch_timer_register();
  1254. if (ret)
  1255. return ret;
  1256. if (platform_timer_count &&
  1257. arch_timer_mem_acpi_init(platform_timer_count))
  1258. pr_err("Failed to initialize memory-mapped timer.\n");
  1259. return arch_timer_common_init();
  1260. }
  1261. CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
  1262. #endif