arm_arch_timer.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. /*
  395. * Use the locked version, as we're called from the CPU
  396. * hotplug framework. Otherwise, we end-up in deadlock-land.
  397. */
  398. static_branch_enable_cpuslocked(&arch_timer_read_ool_enabled);
  399. /*
  400. * Don't use the vdso fastpath if errata require using the
  401. * out-of-line counter accessor. We may change our mind pretty
  402. * late in the game (with a per-CPU erratum, for example), so
  403. * change both the default value and the vdso itself.
  404. */
  405. if (wa->read_cntvct_el0) {
  406. clocksource_counter.archdata.vdso_direct = false;
  407. vdso_default = false;
  408. }
  409. }
  410. static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
  411. void *arg)
  412. {
  413. const struct arch_timer_erratum_workaround *wa;
  414. ate_match_fn_t match_fn = NULL;
  415. bool local = false;
  416. switch (type) {
  417. case ate_match_dt:
  418. match_fn = arch_timer_check_dt_erratum;
  419. break;
  420. case ate_match_local_cap_id:
  421. match_fn = arch_timer_check_local_cap_erratum;
  422. local = true;
  423. break;
  424. case ate_match_acpi_oem_info:
  425. match_fn = arch_timer_check_acpi_oem_erratum;
  426. break;
  427. default:
  428. WARN_ON(1);
  429. return;
  430. }
  431. wa = arch_timer_iterate_errata(type, match_fn, arg);
  432. if (!wa)
  433. return;
  434. if (needs_unstable_timer_counter_workaround()) {
  435. const struct arch_timer_erratum_workaround *__wa;
  436. __wa = __this_cpu_read(timer_unstable_counter_workaround);
  437. if (__wa && wa != __wa)
  438. pr_warn("Can't enable workaround for %s (clashes with %s\n)",
  439. wa->desc, __wa->desc);
  440. if (__wa)
  441. return;
  442. }
  443. arch_timer_enable_workaround(wa, local);
  444. pr_info("Enabling %s workaround for %s\n",
  445. local ? "local" : "global", wa->desc);
  446. }
  447. #define erratum_handler(fn, r, ...) \
  448. ({ \
  449. bool __val; \
  450. if (needs_unstable_timer_counter_workaround()) { \
  451. const struct arch_timer_erratum_workaround *__wa; \
  452. __wa = __this_cpu_read(timer_unstable_counter_workaround); \
  453. if (__wa && __wa->fn) { \
  454. r = __wa->fn(__VA_ARGS__); \
  455. __val = true; \
  456. } else { \
  457. __val = false; \
  458. } \
  459. } else { \
  460. __val = false; \
  461. } \
  462. __val; \
  463. })
  464. static bool arch_timer_this_cpu_has_cntvct_wa(void)
  465. {
  466. const struct arch_timer_erratum_workaround *wa;
  467. wa = __this_cpu_read(timer_unstable_counter_workaround);
  468. return wa && wa->read_cntvct_el0;
  469. }
  470. #else
  471. #define arch_timer_check_ool_workaround(t,a) do { } while(0)
  472. #define erratum_set_next_event_tval_virt(...) ({BUG(); 0;})
  473. #define erratum_set_next_event_tval_phys(...) ({BUG(); 0;})
  474. #define erratum_handler(fn, r, ...) ({false;})
  475. #define arch_timer_this_cpu_has_cntvct_wa() ({false;})
  476. #endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
  477. static __always_inline irqreturn_t timer_handler(const int access,
  478. struct clock_event_device *evt)
  479. {
  480. unsigned long ctrl;
  481. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, evt);
  482. if (ctrl & ARCH_TIMER_CTRL_IT_STAT) {
  483. ctrl |= ARCH_TIMER_CTRL_IT_MASK;
  484. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, evt);
  485. evt->event_handler(evt);
  486. return IRQ_HANDLED;
  487. }
  488. return IRQ_NONE;
  489. }
  490. static irqreturn_t arch_timer_handler_virt(int irq, void *dev_id)
  491. {
  492. struct clock_event_device *evt = dev_id;
  493. return timer_handler(ARCH_TIMER_VIRT_ACCESS, evt);
  494. }
  495. static irqreturn_t arch_timer_handler_phys(int irq, void *dev_id)
  496. {
  497. struct clock_event_device *evt = dev_id;
  498. return timer_handler(ARCH_TIMER_PHYS_ACCESS, evt);
  499. }
  500. static irqreturn_t arch_timer_handler_phys_mem(int irq, void *dev_id)
  501. {
  502. struct clock_event_device *evt = dev_id;
  503. return timer_handler(ARCH_TIMER_MEM_PHYS_ACCESS, evt);
  504. }
  505. static irqreturn_t arch_timer_handler_virt_mem(int irq, void *dev_id)
  506. {
  507. struct clock_event_device *evt = dev_id;
  508. return timer_handler(ARCH_TIMER_MEM_VIRT_ACCESS, evt);
  509. }
  510. static __always_inline int timer_shutdown(const int access,
  511. struct clock_event_device *clk)
  512. {
  513. unsigned long ctrl;
  514. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  515. ctrl &= ~ARCH_TIMER_CTRL_ENABLE;
  516. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  517. return 0;
  518. }
  519. static int arch_timer_shutdown_virt(struct clock_event_device *clk)
  520. {
  521. return timer_shutdown(ARCH_TIMER_VIRT_ACCESS, clk);
  522. }
  523. static int arch_timer_shutdown_phys(struct clock_event_device *clk)
  524. {
  525. return timer_shutdown(ARCH_TIMER_PHYS_ACCESS, clk);
  526. }
  527. static int arch_timer_shutdown_virt_mem(struct clock_event_device *clk)
  528. {
  529. return timer_shutdown(ARCH_TIMER_MEM_VIRT_ACCESS, clk);
  530. }
  531. static int arch_timer_shutdown_phys_mem(struct clock_event_device *clk)
  532. {
  533. return timer_shutdown(ARCH_TIMER_MEM_PHYS_ACCESS, clk);
  534. }
  535. static __always_inline void set_next_event(const int access, unsigned long evt,
  536. struct clock_event_device *clk)
  537. {
  538. unsigned long ctrl;
  539. ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
  540. ctrl |= ARCH_TIMER_CTRL_ENABLE;
  541. ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
  542. arch_timer_reg_write(access, ARCH_TIMER_REG_TVAL, evt, clk);
  543. arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
  544. }
  545. static int arch_timer_set_next_event_virt(unsigned long evt,
  546. struct clock_event_device *clk)
  547. {
  548. int ret;
  549. if (erratum_handler(set_next_event_virt, ret, evt, clk))
  550. return ret;
  551. set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
  552. return 0;
  553. }
  554. static int arch_timer_set_next_event_phys(unsigned long evt,
  555. struct clock_event_device *clk)
  556. {
  557. int ret;
  558. if (erratum_handler(set_next_event_phys, ret, evt, clk))
  559. return ret;
  560. set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
  561. return 0;
  562. }
  563. static int arch_timer_set_next_event_virt_mem(unsigned long evt,
  564. struct clock_event_device *clk)
  565. {
  566. set_next_event(ARCH_TIMER_MEM_VIRT_ACCESS, evt, clk);
  567. return 0;
  568. }
  569. static int arch_timer_set_next_event_phys_mem(unsigned long evt,
  570. struct clock_event_device *clk)
  571. {
  572. set_next_event(ARCH_TIMER_MEM_PHYS_ACCESS, evt, clk);
  573. return 0;
  574. }
  575. static void __arch_timer_setup(unsigned type,
  576. struct clock_event_device *clk)
  577. {
  578. clk->features = CLOCK_EVT_FEAT_ONESHOT;
  579. if (type == ARCH_TIMER_TYPE_CP15) {
  580. if (arch_timer_c3stop)
  581. clk->features |= CLOCK_EVT_FEAT_C3STOP;
  582. clk->name = "arch_sys_timer";
  583. clk->rating = 450;
  584. clk->cpumask = cpumask_of(smp_processor_id());
  585. clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
  586. switch (arch_timer_uses_ppi) {
  587. case ARCH_TIMER_VIRT_PPI:
  588. clk->set_state_shutdown = arch_timer_shutdown_virt;
  589. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
  590. clk->set_next_event = arch_timer_set_next_event_virt;
  591. break;
  592. case ARCH_TIMER_PHYS_SECURE_PPI:
  593. case ARCH_TIMER_PHYS_NONSECURE_PPI:
  594. case ARCH_TIMER_HYP_PPI:
  595. clk->set_state_shutdown = arch_timer_shutdown_phys;
  596. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
  597. clk->set_next_event = arch_timer_set_next_event_phys;
  598. break;
  599. default:
  600. BUG();
  601. }
  602. arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
  603. } else {
  604. clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
  605. clk->name = "arch_mem_timer";
  606. clk->rating = 400;
  607. clk->cpumask = cpu_all_mask;
  608. if (arch_timer_mem_use_virtual) {
  609. clk->set_state_shutdown = arch_timer_shutdown_virt_mem;
  610. clk->set_state_oneshot_stopped = arch_timer_shutdown_virt_mem;
  611. clk->set_next_event =
  612. arch_timer_set_next_event_virt_mem;
  613. } else {
  614. clk->set_state_shutdown = arch_timer_shutdown_phys_mem;
  615. clk->set_state_oneshot_stopped = arch_timer_shutdown_phys_mem;
  616. clk->set_next_event =
  617. arch_timer_set_next_event_phys_mem;
  618. }
  619. }
  620. clk->set_state_shutdown(clk);
  621. clockevents_config_and_register(clk, arch_timer_rate, 0xf, 0x7fffffff);
  622. }
  623. static void arch_timer_evtstrm_enable(int divider)
  624. {
  625. u32 cntkctl = arch_timer_get_cntkctl();
  626. cntkctl &= ~ARCH_TIMER_EVT_TRIGGER_MASK;
  627. /* Set the divider and enable virtual event stream */
  628. cntkctl |= (divider << ARCH_TIMER_EVT_TRIGGER_SHIFT)
  629. | ARCH_TIMER_VIRT_EVT_EN;
  630. arch_timer_set_cntkctl(cntkctl);
  631. elf_hwcap |= HWCAP_EVTSTRM;
  632. #ifdef CONFIG_COMPAT
  633. compat_elf_hwcap |= COMPAT_HWCAP_EVTSTRM;
  634. #endif
  635. }
  636. static void arch_timer_configure_evtstream(void)
  637. {
  638. int evt_stream_div, pos;
  639. /* Find the closest power of two to the divisor */
  640. evt_stream_div = arch_timer_rate / ARCH_TIMER_EVT_STREAM_FREQ;
  641. pos = fls(evt_stream_div);
  642. if (pos > 1 && !(evt_stream_div & (1 << (pos - 2))))
  643. pos--;
  644. /* enable event stream */
  645. arch_timer_evtstrm_enable(min(pos, 15));
  646. }
  647. static void arch_counter_set_user_access(void)
  648. {
  649. u32 cntkctl = arch_timer_get_cntkctl();
  650. /* Disable user access to the timers and both counters */
  651. /* Also disable virtual event stream */
  652. cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
  653. | ARCH_TIMER_USR_VT_ACCESS_EN
  654. | ARCH_TIMER_USR_VCT_ACCESS_EN
  655. | ARCH_TIMER_VIRT_EVT_EN
  656. | ARCH_TIMER_USR_PCT_ACCESS_EN);
  657. /*
  658. * Enable user access to the virtual counter if it doesn't
  659. * need to be workaround. The vdso may have been already
  660. * disabled though.
  661. */
  662. if (arch_timer_this_cpu_has_cntvct_wa())
  663. pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
  664. else
  665. cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
  666. arch_timer_set_cntkctl(cntkctl);
  667. }
  668. static bool arch_timer_has_nonsecure_ppi(void)
  669. {
  670. return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
  671. arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  672. }
  673. static u32 check_ppi_trigger(int irq)
  674. {
  675. u32 flags = irq_get_trigger_type(irq);
  676. if (flags != IRQF_TRIGGER_HIGH && flags != IRQF_TRIGGER_LOW) {
  677. pr_warn("WARNING: Invalid trigger for IRQ%d, assuming level low\n", irq);
  678. pr_warn("WARNING: Please fix your firmware\n");
  679. flags = IRQF_TRIGGER_LOW;
  680. }
  681. return flags;
  682. }
  683. static int arch_timer_starting_cpu(unsigned int cpu)
  684. {
  685. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  686. u32 flags;
  687. __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
  688. flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
  689. enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
  690. if (arch_timer_has_nonsecure_ppi()) {
  691. flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  692. enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
  693. flags);
  694. }
  695. arch_counter_set_user_access();
  696. if (evtstrm_enable)
  697. arch_timer_configure_evtstream();
  698. return 0;
  699. }
  700. /*
  701. * For historical reasons, when probing with DT we use whichever (non-zero)
  702. * rate was probed first, and don't verify that others match. If the first node
  703. * probed has a clock-frequency property, this overrides the HW register.
  704. */
  705. static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
  706. {
  707. /* Who has more than one independent system counter? */
  708. if (arch_timer_rate)
  709. return;
  710. if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
  711. arch_timer_rate = rate;
  712. /* Check the timer frequency. */
  713. if (arch_timer_rate == 0)
  714. pr_warn("frequency not available\n");
  715. }
  716. static void arch_timer_banner(unsigned type)
  717. {
  718. pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
  719. type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
  720. type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
  721. " and " : "",
  722. type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
  723. (unsigned long)arch_timer_rate / 1000000,
  724. (unsigned long)(arch_timer_rate / 10000) % 100,
  725. type & ARCH_TIMER_TYPE_CP15 ?
  726. (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
  727. "",
  728. type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
  729. type & ARCH_TIMER_TYPE_MEM ?
  730. arch_timer_mem_use_virtual ? "virt" : "phys" :
  731. "");
  732. }
  733. u32 arch_timer_get_rate(void)
  734. {
  735. return arch_timer_rate;
  736. }
  737. static u64 arch_counter_get_cntvct_mem(void)
  738. {
  739. u32 vct_lo, vct_hi, tmp_hi;
  740. do {
  741. vct_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  742. vct_lo = readl_relaxed(arch_counter_base + CNTVCT_LO);
  743. tmp_hi = readl_relaxed(arch_counter_base + CNTVCT_HI);
  744. } while (vct_hi != tmp_hi);
  745. return ((u64) vct_hi << 32) | vct_lo;
  746. }
  747. static struct arch_timer_kvm_info arch_timer_kvm_info;
  748. struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
  749. {
  750. return &arch_timer_kvm_info;
  751. }
  752. static void __init arch_counter_register(unsigned type)
  753. {
  754. u64 start_count;
  755. /* Register the CP15 based counter if we have one */
  756. if (type & ARCH_TIMER_TYPE_CP15) {
  757. if (IS_ENABLED(CONFIG_ARM64) ||
  758. arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
  759. arch_timer_read_counter = arch_counter_get_cntvct;
  760. else
  761. arch_timer_read_counter = arch_counter_get_cntpct;
  762. clocksource_counter.archdata.vdso_direct = vdso_default;
  763. } else {
  764. arch_timer_read_counter = arch_counter_get_cntvct_mem;
  765. }
  766. if (!arch_counter_suspend_stop)
  767. clocksource_counter.flags |= CLOCK_SOURCE_SUSPEND_NONSTOP;
  768. start_count = arch_timer_read_counter();
  769. clocksource_register_hz(&clocksource_counter, arch_timer_rate);
  770. cyclecounter.mult = clocksource_counter.mult;
  771. cyclecounter.shift = clocksource_counter.shift;
  772. timecounter_init(&arch_timer_kvm_info.timecounter,
  773. &cyclecounter, start_count);
  774. /* 56 bits minimum, so we assume worst case rollover */
  775. sched_clock_register(arch_timer_read_counter, 56, arch_timer_rate);
  776. }
  777. static void arch_timer_stop(struct clock_event_device *clk)
  778. {
  779. pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
  780. disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
  781. if (arch_timer_has_nonsecure_ppi())
  782. disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
  783. clk->set_state_shutdown(clk);
  784. }
  785. static int arch_timer_dying_cpu(unsigned int cpu)
  786. {
  787. struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
  788. arch_timer_stop(clk);
  789. return 0;
  790. }
  791. #ifdef CONFIG_CPU_PM
  792. static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
  793. static int arch_timer_cpu_pm_notify(struct notifier_block *self,
  794. unsigned long action, void *hcpu)
  795. {
  796. if (action == CPU_PM_ENTER)
  797. __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
  798. else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
  799. arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
  800. return NOTIFY_OK;
  801. }
  802. static struct notifier_block arch_timer_cpu_pm_notifier = {
  803. .notifier_call = arch_timer_cpu_pm_notify,
  804. };
  805. static int __init arch_timer_cpu_pm_init(void)
  806. {
  807. return cpu_pm_register_notifier(&arch_timer_cpu_pm_notifier);
  808. }
  809. static void __init arch_timer_cpu_pm_deinit(void)
  810. {
  811. WARN_ON(cpu_pm_unregister_notifier(&arch_timer_cpu_pm_notifier));
  812. }
  813. #else
  814. static int __init arch_timer_cpu_pm_init(void)
  815. {
  816. return 0;
  817. }
  818. static void __init arch_timer_cpu_pm_deinit(void)
  819. {
  820. }
  821. #endif
  822. static int __init arch_timer_register(void)
  823. {
  824. int err;
  825. int ppi;
  826. arch_timer_evt = alloc_percpu(struct clock_event_device);
  827. if (!arch_timer_evt) {
  828. err = -ENOMEM;
  829. goto out;
  830. }
  831. ppi = arch_timer_ppi[arch_timer_uses_ppi];
  832. switch (arch_timer_uses_ppi) {
  833. case ARCH_TIMER_VIRT_PPI:
  834. err = request_percpu_irq(ppi, arch_timer_handler_virt,
  835. "arch_timer", arch_timer_evt);
  836. break;
  837. case ARCH_TIMER_PHYS_SECURE_PPI:
  838. case ARCH_TIMER_PHYS_NONSECURE_PPI:
  839. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  840. "arch_timer", arch_timer_evt);
  841. if (!err && arch_timer_has_nonsecure_ppi()) {
  842. ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
  843. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  844. "arch_timer", arch_timer_evt);
  845. if (err)
  846. free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
  847. arch_timer_evt);
  848. }
  849. break;
  850. case ARCH_TIMER_HYP_PPI:
  851. err = request_percpu_irq(ppi, arch_timer_handler_phys,
  852. "arch_timer", arch_timer_evt);
  853. break;
  854. default:
  855. BUG();
  856. }
  857. if (err) {
  858. pr_err("can't register interrupt %d (%d)\n", ppi, err);
  859. goto out_free;
  860. }
  861. err = arch_timer_cpu_pm_init();
  862. if (err)
  863. goto out_unreg_notify;
  864. /* Register and immediately configure the timer on the boot CPU */
  865. err = cpuhp_setup_state(CPUHP_AP_ARM_ARCH_TIMER_STARTING,
  866. "clockevents/arm/arch_timer:starting",
  867. arch_timer_starting_cpu, arch_timer_dying_cpu);
  868. if (err)
  869. goto out_unreg_cpupm;
  870. return 0;
  871. out_unreg_cpupm:
  872. arch_timer_cpu_pm_deinit();
  873. out_unreg_notify:
  874. free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
  875. if (arch_timer_has_nonsecure_ppi())
  876. free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
  877. arch_timer_evt);
  878. out_free:
  879. free_percpu(arch_timer_evt);
  880. out:
  881. return err;
  882. }
  883. static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
  884. {
  885. int ret;
  886. irq_handler_t func;
  887. struct arch_timer *t;
  888. t = kzalloc(sizeof(*t), GFP_KERNEL);
  889. if (!t)
  890. return -ENOMEM;
  891. t->base = base;
  892. t->evt.irq = irq;
  893. __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
  894. if (arch_timer_mem_use_virtual)
  895. func = arch_timer_handler_virt_mem;
  896. else
  897. func = arch_timer_handler_phys_mem;
  898. ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
  899. if (ret) {
  900. pr_err("Failed to request mem timer irq\n");
  901. kfree(t);
  902. }
  903. return ret;
  904. }
  905. static const struct of_device_id arch_timer_of_match[] __initconst = {
  906. { .compatible = "arm,armv7-timer", },
  907. { .compatible = "arm,armv8-timer", },
  908. {},
  909. };
  910. static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
  911. { .compatible = "arm,armv7-timer-mem", },
  912. {},
  913. };
  914. static bool __init arch_timer_needs_of_probing(void)
  915. {
  916. struct device_node *dn;
  917. bool needs_probing = false;
  918. unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
  919. /* We have two timers, and both device-tree nodes are probed. */
  920. if ((arch_timers_present & mask) == mask)
  921. return false;
  922. /*
  923. * Only one type of timer is probed,
  924. * check if we have another type of timer node in device-tree.
  925. */
  926. if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
  927. dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
  928. else
  929. dn = of_find_matching_node(NULL, arch_timer_of_match);
  930. if (dn && of_device_is_available(dn))
  931. needs_probing = true;
  932. of_node_put(dn);
  933. return needs_probing;
  934. }
  935. static int __init arch_timer_common_init(void)
  936. {
  937. arch_timer_banner(arch_timers_present);
  938. arch_counter_register(arch_timers_present);
  939. return arch_timer_arch_init();
  940. }
  941. /**
  942. * arch_timer_select_ppi() - Select suitable PPI for the current system.
  943. *
  944. * If HYP mode is available, we know that the physical timer
  945. * has been configured to be accessible from PL1. Use it, so
  946. * that a guest can use the virtual timer instead.
  947. *
  948. * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
  949. * accesses to CNTP_*_EL1 registers are silently redirected to
  950. * their CNTHP_*_EL2 counterparts, and use a different PPI
  951. * number.
  952. *
  953. * If no interrupt provided for virtual timer, we'll have to
  954. * stick to the physical timer. It'd better be accessible...
  955. * For arm64 we never use the secure interrupt.
  956. *
  957. * Return: a suitable PPI type for the current system.
  958. */
  959. static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
  960. {
  961. if (is_kernel_in_hyp_mode())
  962. return ARCH_TIMER_HYP_PPI;
  963. if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
  964. return ARCH_TIMER_VIRT_PPI;
  965. if (IS_ENABLED(CONFIG_ARM64))
  966. return ARCH_TIMER_PHYS_NONSECURE_PPI;
  967. return ARCH_TIMER_PHYS_SECURE_PPI;
  968. }
  969. static int __init arch_timer_of_init(struct device_node *np)
  970. {
  971. int i, ret;
  972. u32 rate;
  973. if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
  974. pr_warn("multiple nodes in dt, skipping\n");
  975. return 0;
  976. }
  977. arch_timers_present |= ARCH_TIMER_TYPE_CP15;
  978. for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
  979. arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
  980. arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
  981. rate = arch_timer_get_cntfrq();
  982. arch_timer_of_configure_rate(rate, np);
  983. arch_timer_c3stop = !of_property_read_bool(np, "always-on");
  984. /* Check for globally applicable workarounds */
  985. arch_timer_check_ool_workaround(ate_match_dt, np);
  986. /*
  987. * If we cannot rely on firmware initializing the timer registers then
  988. * we should use the physical timers instead.
  989. */
  990. if (IS_ENABLED(CONFIG_ARM) &&
  991. of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
  992. arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
  993. else
  994. arch_timer_uses_ppi = arch_timer_select_ppi();
  995. if (!arch_timer_ppi[arch_timer_uses_ppi]) {
  996. pr_err("No interrupt available, giving up\n");
  997. return -EINVAL;
  998. }
  999. /* On some systems, the counter stops ticking when in suspend. */
  1000. arch_counter_suspend_stop = of_property_read_bool(np,
  1001. "arm,no-tick-in-suspend");
  1002. ret = arch_timer_register();
  1003. if (ret)
  1004. return ret;
  1005. if (arch_timer_needs_of_probing())
  1006. return 0;
  1007. return arch_timer_common_init();
  1008. }
  1009. TIMER_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
  1010. TIMER_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
  1011. static u32 __init
  1012. arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
  1013. {
  1014. void __iomem *base;
  1015. u32 rate;
  1016. base = ioremap(frame->cntbase, frame->size);
  1017. if (!base) {
  1018. pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
  1019. return 0;
  1020. }
  1021. rate = readl_relaxed(base + CNTFRQ);
  1022. iounmap(base);
  1023. return rate;
  1024. }
  1025. static struct arch_timer_mem_frame * __init
  1026. arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
  1027. {
  1028. struct arch_timer_mem_frame *frame, *best_frame = NULL;
  1029. void __iomem *cntctlbase;
  1030. u32 cnttidr;
  1031. int i;
  1032. cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
  1033. if (!cntctlbase) {
  1034. pr_err("Can't map CNTCTLBase @ %pa\n",
  1035. &timer_mem->cntctlbase);
  1036. return NULL;
  1037. }
  1038. cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
  1039. /*
  1040. * Try to find a virtual capable frame. Otherwise fall back to a
  1041. * physical capable frame.
  1042. */
  1043. for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
  1044. u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
  1045. CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
  1046. frame = &timer_mem->frame[i];
  1047. if (!frame->valid)
  1048. continue;
  1049. /* Try enabling everything, and see what sticks */
  1050. writel_relaxed(cntacr, cntctlbase + CNTACR(i));
  1051. cntacr = readl_relaxed(cntctlbase + CNTACR(i));
  1052. if ((cnttidr & CNTTIDR_VIRT(i)) &&
  1053. !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
  1054. best_frame = frame;
  1055. arch_timer_mem_use_virtual = true;
  1056. break;
  1057. }
  1058. if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
  1059. continue;
  1060. best_frame = frame;
  1061. }
  1062. iounmap(cntctlbase);
  1063. if (!best_frame)
  1064. pr_err("Unable to find a suitable frame in timer @ %pa\n",
  1065. &timer_mem->cntctlbase);
  1066. return best_frame;
  1067. }
  1068. static int __init
  1069. arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
  1070. {
  1071. void __iomem *base;
  1072. int ret, irq = 0;
  1073. if (arch_timer_mem_use_virtual)
  1074. irq = frame->virt_irq;
  1075. else
  1076. irq = frame->phys_irq;
  1077. if (!irq) {
  1078. pr_err("Frame missing %s irq.\n",
  1079. arch_timer_mem_use_virtual ? "virt" : "phys");
  1080. return -EINVAL;
  1081. }
  1082. if (!request_mem_region(frame->cntbase, frame->size,
  1083. "arch_mem_timer"))
  1084. return -EBUSY;
  1085. base = ioremap(frame->cntbase, frame->size);
  1086. if (!base) {
  1087. pr_err("Can't map frame's registers\n");
  1088. return -ENXIO;
  1089. }
  1090. ret = arch_timer_mem_register(base, irq);
  1091. if (ret) {
  1092. iounmap(base);
  1093. return ret;
  1094. }
  1095. arch_counter_base = base;
  1096. arch_timers_present |= ARCH_TIMER_TYPE_MEM;
  1097. return 0;
  1098. }
  1099. static int __init arch_timer_mem_of_init(struct device_node *np)
  1100. {
  1101. struct arch_timer_mem *timer_mem;
  1102. struct arch_timer_mem_frame *frame;
  1103. struct device_node *frame_node;
  1104. struct resource res;
  1105. int ret = -EINVAL;
  1106. u32 rate;
  1107. timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
  1108. if (!timer_mem)
  1109. return -ENOMEM;
  1110. if (of_address_to_resource(np, 0, &res))
  1111. goto out;
  1112. timer_mem->cntctlbase = res.start;
  1113. timer_mem->size = resource_size(&res);
  1114. for_each_available_child_of_node(np, frame_node) {
  1115. u32 n;
  1116. struct arch_timer_mem_frame *frame;
  1117. if (of_property_read_u32(frame_node, "frame-number", &n)) {
  1118. pr_err(FW_BUG "Missing frame-number.\n");
  1119. of_node_put(frame_node);
  1120. goto out;
  1121. }
  1122. if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
  1123. pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
  1124. ARCH_TIMER_MEM_MAX_FRAMES - 1);
  1125. of_node_put(frame_node);
  1126. goto out;
  1127. }
  1128. frame = &timer_mem->frame[n];
  1129. if (frame->valid) {
  1130. pr_err(FW_BUG "Duplicated frame-number.\n");
  1131. of_node_put(frame_node);
  1132. goto out;
  1133. }
  1134. if (of_address_to_resource(frame_node, 0, &res)) {
  1135. of_node_put(frame_node);
  1136. goto out;
  1137. }
  1138. frame->cntbase = res.start;
  1139. frame->size = resource_size(&res);
  1140. frame->virt_irq = irq_of_parse_and_map(frame_node,
  1141. ARCH_TIMER_VIRT_SPI);
  1142. frame->phys_irq = irq_of_parse_and_map(frame_node,
  1143. ARCH_TIMER_PHYS_SPI);
  1144. frame->valid = true;
  1145. }
  1146. frame = arch_timer_mem_find_best_frame(timer_mem);
  1147. if (!frame) {
  1148. ret = -EINVAL;
  1149. goto out;
  1150. }
  1151. rate = arch_timer_mem_frame_get_cntfrq(frame);
  1152. arch_timer_of_configure_rate(rate, np);
  1153. ret = arch_timer_mem_frame_register(frame);
  1154. if (!ret && !arch_timer_needs_of_probing())
  1155. ret = arch_timer_common_init();
  1156. out:
  1157. kfree(timer_mem);
  1158. return ret;
  1159. }
  1160. TIMER_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
  1161. arch_timer_mem_of_init);
  1162. #ifdef CONFIG_ACPI_GTDT
  1163. static int __init
  1164. arch_timer_mem_verify_cntfrq(struct arch_timer_mem *timer_mem)
  1165. {
  1166. struct arch_timer_mem_frame *frame;
  1167. u32 rate;
  1168. int i;
  1169. for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
  1170. frame = &timer_mem->frame[i];
  1171. if (!frame->valid)
  1172. continue;
  1173. rate = arch_timer_mem_frame_get_cntfrq(frame);
  1174. if (rate == arch_timer_rate)
  1175. continue;
  1176. pr_err(FW_BUG "CNTFRQ mismatch: frame @ %pa: (0x%08lx), CPU: (0x%08lx)\n",
  1177. &frame->cntbase,
  1178. (unsigned long)rate, (unsigned long)arch_timer_rate);
  1179. return -EINVAL;
  1180. }
  1181. return 0;
  1182. }
  1183. static int __init arch_timer_mem_acpi_init(int platform_timer_count)
  1184. {
  1185. struct arch_timer_mem *timers, *timer;
  1186. struct arch_timer_mem_frame *frame;
  1187. int timer_count, i, ret = 0;
  1188. timers = kcalloc(platform_timer_count, sizeof(*timers),
  1189. GFP_KERNEL);
  1190. if (!timers)
  1191. return -ENOMEM;
  1192. ret = acpi_arch_timer_mem_init(timers, &timer_count);
  1193. if (ret || !timer_count)
  1194. goto out;
  1195. for (i = 0; i < timer_count; i++) {
  1196. ret = arch_timer_mem_verify_cntfrq(&timers[i]);
  1197. if (ret) {
  1198. pr_err("Disabling MMIO timers due to CNTFRQ mismatch\n");
  1199. goto out;
  1200. }
  1201. }
  1202. /*
  1203. * While unlikely, it's theoretically possible that none of the frames
  1204. * in a timer expose the combination of feature we want.
  1205. */
  1206. for (i = 0; i < timer_count; i++) {
  1207. timer = &timers[i];
  1208. frame = arch_timer_mem_find_best_frame(timer);
  1209. if (frame)
  1210. break;
  1211. }
  1212. if (frame)
  1213. ret = arch_timer_mem_frame_register(frame);
  1214. out:
  1215. kfree(timers);
  1216. return ret;
  1217. }
  1218. /* Initialize per-processor generic timer and memory-mapped timer(if present) */
  1219. static int __init arch_timer_acpi_init(struct acpi_table_header *table)
  1220. {
  1221. int ret, platform_timer_count;
  1222. if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
  1223. pr_warn("already initialized, skipping\n");
  1224. return -EINVAL;
  1225. }
  1226. arch_timers_present |= ARCH_TIMER_TYPE_CP15;
  1227. ret = acpi_gtdt_init(table, &platform_timer_count);
  1228. if (ret) {
  1229. pr_err("Failed to init GTDT table.\n");
  1230. return ret;
  1231. }
  1232. arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
  1233. acpi_gtdt_map_ppi(ARCH_TIMER_PHYS_NONSECURE_PPI);
  1234. arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
  1235. acpi_gtdt_map_ppi(ARCH_TIMER_VIRT_PPI);
  1236. arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
  1237. acpi_gtdt_map_ppi(ARCH_TIMER_HYP_PPI);
  1238. arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
  1239. /*
  1240. * When probing via ACPI, we have no mechanism to override the sysreg
  1241. * CNTFRQ value. This *must* be correct.
  1242. */
  1243. arch_timer_rate = arch_timer_get_cntfrq();
  1244. if (!arch_timer_rate) {
  1245. pr_err(FW_BUG "frequency not available.\n");
  1246. return -EINVAL;
  1247. }
  1248. arch_timer_uses_ppi = arch_timer_select_ppi();
  1249. if (!arch_timer_ppi[arch_timer_uses_ppi]) {
  1250. pr_err("No interrupt available, giving up\n");
  1251. return -EINVAL;
  1252. }
  1253. /* Always-on capability */
  1254. arch_timer_c3stop = acpi_gtdt_c3stop(arch_timer_uses_ppi);
  1255. /* Check for globally applicable workarounds */
  1256. arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
  1257. ret = arch_timer_register();
  1258. if (ret)
  1259. return ret;
  1260. if (platform_timer_count &&
  1261. arch_timer_mem_acpi_init(platform_timer_count))
  1262. pr_err("Failed to initialize memory-mapped timer.\n");
  1263. return arch_timer_common_init();
  1264. }
  1265. TIMER_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
  1266. #endif