arm_arch_timer.c 39 KB

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