clocksource: arm_arch_timer: split MMIO timer probing.
[sfrench/cifs-2.6.git] / drivers / clocksource / arm_arch_timer.c
index 7a8a4117f123d6d861e6e1a9751b1a93defd19ae..d87c4035a5056d41372ff6613ce0b89ebd5543f8 100644 (file)
@@ -33,6 +33,9 @@
 
 #include <clocksource/arm_arch_timer.h>
 
+#undef pr_fmt
+#define pr_fmt(fmt) "arch_timer: " fmt
+
 #define CNTTIDR                0x08
 #define CNTTIDR_VIRT(n)        (BIT(1) << ((n) * 4))
 
@@ -52,8 +55,6 @@
 #define CNTV_TVAL      0x38
 #define CNTV_CTL       0x3c
 
-#define ARCH_CP15_TIMER        BIT(0)
-#define ARCH_MEM_TIMER BIT(1)
 static unsigned arch_timers_present __initdata;
 
 static void __iomem *arch_counter_base;
@@ -66,23 +67,15 @@ struct arch_timer {
 #define to_arch_timer(e) container_of(e, struct arch_timer, evt)
 
 static u32 arch_timer_rate;
-
-enum ppi_nr {
-       PHYS_SECURE_PPI,
-       PHYS_NONSECURE_PPI,
-       VIRT_PPI,
-       HYP_PPI,
-       MAX_TIMER_PPI
-};
-
-static int arch_timer_ppi[MAX_TIMER_PPI];
+static int arch_timer_ppi[ARCH_TIMER_MAX_TIMER_PPI];
 
 static struct clock_event_device __percpu *arch_timer_evt;
 
-static enum ppi_nr arch_timer_uses_ppi = VIRT_PPI;
+static enum arch_timer_ppi_nr arch_timer_uses_ppi = ARCH_TIMER_VIRT_PPI;
 static bool arch_timer_c3stop;
 static bool arch_timer_mem_use_virtual;
 static bool arch_counter_suspend_stop;
+static bool vdso_default = true;
 
 static bool evtstrm_enable = IS_ENABLED(CONFIG_ARM_ARCH_TIMER_EVTSTREAM);
 
@@ -96,6 +89,105 @@ early_param("clocksource.arm_arch_timer.evtstrm", early_evtstrm_cfg);
  * Architected system timer support.
  */
 
+static __always_inline
+void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
+                         struct clock_event_device *clk)
+{
+       if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
+               struct arch_timer *timer = to_arch_timer(clk);
+               switch (reg) {
+               case ARCH_TIMER_REG_CTRL:
+                       writel_relaxed(val, timer->base + CNTP_CTL);
+                       break;
+               case ARCH_TIMER_REG_TVAL:
+                       writel_relaxed(val, timer->base + CNTP_TVAL);
+                       break;
+               }
+       } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
+               struct arch_timer *timer = to_arch_timer(clk);
+               switch (reg) {
+               case ARCH_TIMER_REG_CTRL:
+                       writel_relaxed(val, timer->base + CNTV_CTL);
+                       break;
+               case ARCH_TIMER_REG_TVAL:
+                       writel_relaxed(val, timer->base + CNTV_TVAL);
+                       break;
+               }
+       } else {
+               arch_timer_reg_write_cp15(access, reg, val);
+       }
+}
+
+static __always_inline
+u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
+                       struct clock_event_device *clk)
+{
+       u32 val;
+
+       if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
+               struct arch_timer *timer = to_arch_timer(clk);
+               switch (reg) {
+               case ARCH_TIMER_REG_CTRL:
+                       val = readl_relaxed(timer->base + CNTP_CTL);
+                       break;
+               case ARCH_TIMER_REG_TVAL:
+                       val = readl_relaxed(timer->base + CNTP_TVAL);
+                       break;
+               }
+       } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
+               struct arch_timer *timer = to_arch_timer(clk);
+               switch (reg) {
+               case ARCH_TIMER_REG_CTRL:
+                       val = readl_relaxed(timer->base + CNTV_CTL);
+                       break;
+               case ARCH_TIMER_REG_TVAL:
+                       val = readl_relaxed(timer->base + CNTV_TVAL);
+                       break;
+               }
+       } else {
+               val = arch_timer_reg_read_cp15(access, reg);
+       }
+
+       return val;
+}
+
+/*
+ * Default to cp15 based access because arm64 uses this function for
+ * sched_clock() before DT is probed and the cp15 method is guaranteed
+ * to exist on arm64. arm doesn't use this before DT is probed so even
+ * if we don't have the cp15 accessors we won't have a problem.
+ */
+u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
+
+static u64 arch_counter_read(struct clocksource *cs)
+{
+       return arch_timer_read_counter();
+}
+
+static u64 arch_counter_read_cc(const struct cyclecounter *cc)
+{
+       return arch_timer_read_counter();
+}
+
+static struct clocksource clocksource_counter = {
+       .name   = "arch_sys_counter",
+       .rating = 400,
+       .read   = arch_counter_read,
+       .mask   = CLOCKSOURCE_MASK(56),
+       .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
+};
+
+static struct cyclecounter cyclecounter __ro_after_init = {
+       .read   = arch_counter_read_cc,
+       .mask   = CLOCKSOURCE_MASK(56),
+};
+
+struct ate_acpi_oem_info {
+       char oem_id[ACPI_OEM_ID_SIZE + 1];
+       char oem_table_id[ACPI_OEM_TABLE_ID_SIZE + 1];
+       u32 oem_revision;
+};
+
 #ifdef CONFIG_FSL_ERRATUM_A008585
 /*
  * The number of retries is an arbitrary value well beyond the highest number
@@ -170,97 +262,289 @@ static u64 notrace hisi_161010101_read_cntvct_el0(void)
 {
        return __hisi_161010101_read_reg(cntvct_el0);
 }
+
+static struct ate_acpi_oem_info hisi_161010101_oem_info[] = {
+       /*
+        * Note that trailing spaces are required to properly match
+        * the OEM table information.
+        */
+       {
+               .oem_id         = "HISI  ",
+               .oem_table_id   = "HIP05   ",
+               .oem_revision   = 0,
+       },
+       {
+               .oem_id         = "HISI  ",
+               .oem_table_id   = "HIP06   ",
+               .oem_revision   = 0,
+       },
+       {
+               .oem_id         = "HISI  ",
+               .oem_table_id   = "HIP07   ",
+               .oem_revision   = 0,
+       },
+       { /* Sentinel indicating the end of the OEM array */ },
+};
+#endif
+
+#ifdef CONFIG_ARM64_ERRATUM_858921
+static u64 notrace arm64_858921_read_cntvct_el0(void)
+{
+       u64 old, new;
+
+       old = read_sysreg(cntvct_el0);
+       new = read_sysreg(cntvct_el0);
+       return (((old ^ new) >> 32) & 1) ? old : new;
+}
 #endif
 
 #ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
-const struct arch_timer_erratum_workaround *timer_unstable_counter_workaround = NULL;
+DEFINE_PER_CPU(const struct arch_timer_erratum_workaround *,
+              timer_unstable_counter_workaround);
 EXPORT_SYMBOL_GPL(timer_unstable_counter_workaround);
 
 DEFINE_STATIC_KEY_FALSE(arch_timer_read_ool_enabled);
 EXPORT_SYMBOL_GPL(arch_timer_read_ool_enabled);
 
+static void erratum_set_next_event_tval_generic(const int access, unsigned long evt,
+                                               struct clock_event_device *clk)
+{
+       unsigned long ctrl;
+       u64 cval = evt + arch_counter_get_cntvct();
+
+       ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
+       ctrl |= ARCH_TIMER_CTRL_ENABLE;
+       ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
+
+       if (access == ARCH_TIMER_PHYS_ACCESS)
+               write_sysreg(cval, cntp_cval_el0);
+       else
+               write_sysreg(cval, cntv_cval_el0);
+
+       arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
+}
+
+static int erratum_set_next_event_tval_virt(unsigned long evt,
+                                           struct clock_event_device *clk)
+{
+       erratum_set_next_event_tval_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
+       return 0;
+}
+
+static int erratum_set_next_event_tval_phys(unsigned long evt,
+                                           struct clock_event_device *clk)
+{
+       erratum_set_next_event_tval_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
+       return 0;
+}
+
 static const struct arch_timer_erratum_workaround ool_workarounds[] = {
 #ifdef CONFIG_FSL_ERRATUM_A008585
        {
+               .match_type = ate_match_dt,
                .id = "fsl,erratum-a008585",
+               .desc = "Freescale erratum a005858",
                .read_cntp_tval_el0 = fsl_a008585_read_cntp_tval_el0,
                .read_cntv_tval_el0 = fsl_a008585_read_cntv_tval_el0,
                .read_cntvct_el0 = fsl_a008585_read_cntvct_el0,
+               .set_next_event_phys = erratum_set_next_event_tval_phys,
+               .set_next_event_virt = erratum_set_next_event_tval_virt,
        },
 #endif
 #ifdef CONFIG_HISILICON_ERRATUM_161010101
        {
+               .match_type = ate_match_dt,
                .id = "hisilicon,erratum-161010101",
+               .desc = "HiSilicon erratum 161010101",
                .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
                .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
                .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
+               .set_next_event_phys = erratum_set_next_event_tval_phys,
+               .set_next_event_virt = erratum_set_next_event_tval_virt,
+       },
+       {
+               .match_type = ate_match_acpi_oem_info,
+               .id = hisi_161010101_oem_info,
+               .desc = "HiSilicon erratum 161010101",
+               .read_cntp_tval_el0 = hisi_161010101_read_cntp_tval_el0,
+               .read_cntv_tval_el0 = hisi_161010101_read_cntv_tval_el0,
+               .read_cntvct_el0 = hisi_161010101_read_cntvct_el0,
+               .set_next_event_phys = erratum_set_next_event_tval_phys,
+               .set_next_event_virt = erratum_set_next_event_tval_virt,
+       },
+#endif
+#ifdef CONFIG_ARM64_ERRATUM_858921
+       {
+               .match_type = ate_match_local_cap_id,
+               .id = (void *)ARM64_WORKAROUND_858921,
+               .desc = "ARM erratum 858921",
+               .read_cntvct_el0 = arm64_858921_read_cntvct_el0,
        },
 #endif
 };
-#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
 
-static __always_inline
-void arch_timer_reg_write(int access, enum arch_timer_reg reg, u32 val,
-                         struct clock_event_device *clk)
+typedef bool (*ate_match_fn_t)(const struct arch_timer_erratum_workaround *,
+                              const void *);
+
+static
+bool arch_timer_check_dt_erratum(const struct arch_timer_erratum_workaround *wa,
+                                const void *arg)
 {
-       if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
-               struct arch_timer *timer = to_arch_timer(clk);
-               switch (reg) {
-               case ARCH_TIMER_REG_CTRL:
-                       writel_relaxed(val, timer->base + CNTP_CTL);
-                       break;
-               case ARCH_TIMER_REG_TVAL:
-                       writel_relaxed(val, timer->base + CNTP_TVAL);
-                       break;
-               }
-       } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
-               struct arch_timer *timer = to_arch_timer(clk);
-               switch (reg) {
-               case ARCH_TIMER_REG_CTRL:
-                       writel_relaxed(val, timer->base + CNTV_CTL);
-                       break;
-               case ARCH_TIMER_REG_TVAL:
-                       writel_relaxed(val, timer->base + CNTV_TVAL);
-                       break;
-               }
-       } else {
-               arch_timer_reg_write_cp15(access, reg, val);
+       const struct device_node *np = arg;
+
+       return of_property_read_bool(np, wa->id);
+}
+
+static
+bool arch_timer_check_local_cap_erratum(const struct arch_timer_erratum_workaround *wa,
+                                       const void *arg)
+{
+       return this_cpu_has_cap((uintptr_t)wa->id);
+}
+
+
+static
+bool arch_timer_check_acpi_oem_erratum(const struct arch_timer_erratum_workaround *wa,
+                                      const void *arg)
+{
+       static const struct ate_acpi_oem_info empty_oem_info = {};
+       const struct ate_acpi_oem_info *info = wa->id;
+       const struct acpi_table_header *table = arg;
+
+       /* Iterate over the ACPI OEM info array, looking for a match */
+       while (memcmp(info, &empty_oem_info, sizeof(*info))) {
+               if (!memcmp(info->oem_id, table->oem_id, ACPI_OEM_ID_SIZE) &&
+                   !memcmp(info->oem_table_id, table->oem_table_id, ACPI_OEM_TABLE_ID_SIZE) &&
+                   info->oem_revision == table->oem_revision)
+                       return true;
+
+               info++;
        }
+
+       return false;
 }
 
-static __always_inline
-u32 arch_timer_reg_read(int access, enum arch_timer_reg reg,
-                       struct clock_event_device *clk)
+static const struct arch_timer_erratum_workaround *
+arch_timer_iterate_errata(enum arch_timer_erratum_match_type type,
+                         ate_match_fn_t match_fn,
+                         void *arg)
 {
-       u32 val;
+       int i;
 
-       if (access == ARCH_TIMER_MEM_PHYS_ACCESS) {
-               struct arch_timer *timer = to_arch_timer(clk);
-               switch (reg) {
-               case ARCH_TIMER_REG_CTRL:
-                       val = readl_relaxed(timer->base + CNTP_CTL);
-                       break;
-               case ARCH_TIMER_REG_TVAL:
-                       val = readl_relaxed(timer->base + CNTP_TVAL);
-                       break;
-               }
-       } else if (access == ARCH_TIMER_MEM_VIRT_ACCESS) {
-               struct arch_timer *timer = to_arch_timer(clk);
-               switch (reg) {
-               case ARCH_TIMER_REG_CTRL:
-                       val = readl_relaxed(timer->base + CNTV_CTL);
-                       break;
-               case ARCH_TIMER_REG_TVAL:
-                       val = readl_relaxed(timer->base + CNTV_TVAL);
-                       break;
-               }
+       for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
+               if (ool_workarounds[i].match_type != type)
+                       continue;
+
+               if (match_fn(&ool_workarounds[i], arg))
+                       return &ool_workarounds[i];
+       }
+
+       return NULL;
+}
+
+static
+void arch_timer_enable_workaround(const struct arch_timer_erratum_workaround *wa,
+                                 bool local)
+{
+       int i;
+
+       if (local) {
+               __this_cpu_write(timer_unstable_counter_workaround, wa);
        } else {
-               val = arch_timer_reg_read_cp15(access, reg);
+               for_each_possible_cpu(i)
+                       per_cpu(timer_unstable_counter_workaround, i) = wa;
        }
 
-       return val;
+       static_branch_enable(&arch_timer_read_ool_enabled);
+
+       /*
+        * Don't use the vdso fastpath if errata require using the
+        * out-of-line counter accessor. We may change our mind pretty
+        * late in the game (with a per-CPU erratum, for example), so
+        * change both the default value and the vdso itself.
+        */
+       if (wa->read_cntvct_el0) {
+               clocksource_counter.archdata.vdso_direct = false;
+               vdso_default = false;
+       }
+}
+
+static void arch_timer_check_ool_workaround(enum arch_timer_erratum_match_type type,
+                                           void *arg)
+{
+       const struct arch_timer_erratum_workaround *wa;
+       ate_match_fn_t match_fn = NULL;
+       bool local = false;
+
+       switch (type) {
+       case ate_match_dt:
+               match_fn = arch_timer_check_dt_erratum;
+               break;
+       case ate_match_local_cap_id:
+               match_fn = arch_timer_check_local_cap_erratum;
+               local = true;
+               break;
+       case ate_match_acpi_oem_info:
+               match_fn = arch_timer_check_acpi_oem_erratum;
+               break;
+       default:
+               WARN_ON(1);
+               return;
+       }
+
+       wa = arch_timer_iterate_errata(type, match_fn, arg);
+       if (!wa)
+               return;
+
+       if (needs_unstable_timer_counter_workaround()) {
+               const struct arch_timer_erratum_workaround *__wa;
+               __wa = __this_cpu_read(timer_unstable_counter_workaround);
+               if (__wa && wa != __wa)
+                       pr_warn("Can't enable workaround for %s (clashes with %s\n)",
+                               wa->desc, __wa->desc);
+
+               if (__wa)
+                       return;
+       }
+
+       arch_timer_enable_workaround(wa, local);
+       pr_info("Enabling %s workaround for %s\n",
+               local ? "local" : "global", wa->desc);
 }
 
+#define erratum_handler(fn, r, ...)                                    \
+({                                                                     \
+       bool __val;                                                     \
+       if (needs_unstable_timer_counter_workaround()) {                \
+               const struct arch_timer_erratum_workaround *__wa;       \
+               __wa = __this_cpu_read(timer_unstable_counter_workaround); \
+               if (__wa && __wa->fn) {                                 \
+                       r = __wa->fn(__VA_ARGS__);                      \
+                       __val = true;                                   \
+               } else {                                                \
+                       __val = false;                                  \
+               }                                                       \
+       } else {                                                        \
+               __val = false;                                          \
+       }                                                               \
+       __val;                                                          \
+})
+
+static bool arch_timer_this_cpu_has_cntvct_wa(void)
+{
+       const struct arch_timer_erratum_workaround *wa;
+
+       wa = __this_cpu_read(timer_unstable_counter_workaround);
+       return wa && wa->read_cntvct_el0;
+}
+#else
+#define arch_timer_check_ool_workaround(t,a)           do { } while(0)
+#define erratum_set_next_event_tval_virt(...)          ({BUG(); 0;})
+#define erratum_set_next_event_tval_phys(...)          ({BUG(); 0;})
+#define erratum_handler(fn, r, ...)                    ({false;})
+#define arch_timer_this_cpu_has_cntvct_wa()            ({false;})
+#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
+
 static __always_inline irqreturn_t timer_handler(const int access,
                                        struct clock_event_device *evt)
 {
@@ -348,43 +632,14 @@ static __always_inline void set_next_event(const int access, unsigned long evt,
        arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
 }
 
-#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
-static __always_inline void erratum_set_next_event_generic(const int access,
-               unsigned long evt, struct clock_event_device *clk)
-{
-       unsigned long ctrl;
-       u64 cval = evt + arch_counter_get_cntvct();
-
-       ctrl = arch_timer_reg_read(access, ARCH_TIMER_REG_CTRL, clk);
-       ctrl |= ARCH_TIMER_CTRL_ENABLE;
-       ctrl &= ~ARCH_TIMER_CTRL_IT_MASK;
-
-       if (access == ARCH_TIMER_PHYS_ACCESS)
-               write_sysreg(cval, cntp_cval_el0);
-       else if (access == ARCH_TIMER_VIRT_ACCESS)
-               write_sysreg(cval, cntv_cval_el0);
-
-       arch_timer_reg_write(access, ARCH_TIMER_REG_CTRL, ctrl, clk);
-}
-
-static int erratum_set_next_event_virt(unsigned long evt,
-                                          struct clock_event_device *clk)
-{
-       erratum_set_next_event_generic(ARCH_TIMER_VIRT_ACCESS, evt, clk);
-       return 0;
-}
-
-static int erratum_set_next_event_phys(unsigned long evt,
-                                          struct clock_event_device *clk)
-{
-       erratum_set_next_event_generic(ARCH_TIMER_PHYS_ACCESS, evt, clk);
-       return 0;
-}
-#endif /* CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND */
-
 static int arch_timer_set_next_event_virt(unsigned long evt,
                                          struct clock_event_device *clk)
 {
+       int ret;
+
+       if (erratum_handler(set_next_event_virt, ret, evt, clk))
+               return ret;
+
        set_next_event(ARCH_TIMER_VIRT_ACCESS, evt, clk);
        return 0;
 }
@@ -392,6 +647,11 @@ static int arch_timer_set_next_event_virt(unsigned long evt,
 static int arch_timer_set_next_event_phys(unsigned long evt,
                                          struct clock_event_device *clk)
 {
+       int ret;
+
+       if (erratum_handler(set_next_event_phys, ret, evt, clk))
+               return ret;
+
        set_next_event(ARCH_TIMER_PHYS_ACCESS, evt, clk);
        return 0;
 }
@@ -410,25 +670,12 @@ static int arch_timer_set_next_event_phys_mem(unsigned long evt,
        return 0;
 }
 
-static void erratum_workaround_set_sne(struct clock_event_device *clk)
-{
-#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
-       if (!static_branch_unlikely(&arch_timer_read_ool_enabled))
-               return;
-
-       if (arch_timer_uses_ppi == VIRT_PPI)
-               clk->set_next_event = erratum_set_next_event_virt;
-       else
-               clk->set_next_event = erratum_set_next_event_phys;
-#endif
-}
-
 static void __arch_timer_setup(unsigned type,
                               struct clock_event_device *clk)
 {
        clk->features = CLOCK_EVT_FEAT_ONESHOT;
 
-       if (type == ARCH_CP15_TIMER) {
+       if (type == ARCH_TIMER_TYPE_CP15) {
                if (arch_timer_c3stop)
                        clk->features |= CLOCK_EVT_FEAT_C3STOP;
                clk->name = "arch_sys_timer";
@@ -436,14 +683,14 @@ static void __arch_timer_setup(unsigned type,
                clk->cpumask = cpumask_of(smp_processor_id());
                clk->irq = arch_timer_ppi[arch_timer_uses_ppi];
                switch (arch_timer_uses_ppi) {
-               case VIRT_PPI:
+               case ARCH_TIMER_VIRT_PPI:
                        clk->set_state_shutdown = arch_timer_shutdown_virt;
                        clk->set_state_oneshot_stopped = arch_timer_shutdown_virt;
                        clk->set_next_event = arch_timer_set_next_event_virt;
                        break;
-               case PHYS_SECURE_PPI:
-               case PHYS_NONSECURE_PPI:
-               case HYP_PPI:
+               case ARCH_TIMER_PHYS_SECURE_PPI:
+               case ARCH_TIMER_PHYS_NONSECURE_PPI:
+               case ARCH_TIMER_HYP_PPI:
                        clk->set_state_shutdown = arch_timer_shutdown_phys;
                        clk->set_state_oneshot_stopped = arch_timer_shutdown_phys;
                        clk->set_next_event = arch_timer_set_next_event_phys;
@@ -452,7 +699,7 @@ static void __arch_timer_setup(unsigned type,
                        BUG();
                }
 
-               erratum_workaround_set_sne(clk);
+               arch_timer_check_ool_workaround(ate_match_local_cap_id, NULL);
        } else {
                clk->features |= CLOCK_EVT_FEAT_DYNIRQ;
                clk->name = "arch_mem_timer";
@@ -508,23 +755,31 @@ static void arch_counter_set_user_access(void)
 {
        u32 cntkctl = arch_timer_get_cntkctl();
 
-       /* Disable user access to the timers and the physical counter */
+       /* Disable user access to the timers and both counters */
        /* Also disable virtual event stream */
        cntkctl &= ~(ARCH_TIMER_USR_PT_ACCESS_EN
                        | ARCH_TIMER_USR_VT_ACCESS_EN
+                       | ARCH_TIMER_USR_VCT_ACCESS_EN
                        | ARCH_TIMER_VIRT_EVT_EN
                        | ARCH_TIMER_USR_PCT_ACCESS_EN);
 
-       /* Enable user access to the virtual counter */
-       cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
+       /*
+        * Enable user access to the virtual counter if it doesn't
+        * need to be workaround. The vdso may have been already
+        * disabled though.
+        */
+       if (arch_timer_this_cpu_has_cntvct_wa())
+               pr_info("CPU%d: Trapping CNTVCT access\n", smp_processor_id());
+       else
+               cntkctl |= ARCH_TIMER_USR_VCT_ACCESS_EN;
 
        arch_timer_set_cntkctl(cntkctl);
 }
 
 static bool arch_timer_has_nonsecure_ppi(void)
 {
-       return (arch_timer_uses_ppi == PHYS_SECURE_PPI &&
-               arch_timer_ppi[PHYS_NONSECURE_PPI]);
+       return (arch_timer_uses_ppi == ARCH_TIMER_PHYS_SECURE_PPI &&
+               arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
 }
 
 static u32 check_ppi_trigger(int irq)
@@ -545,14 +800,15 @@ static int arch_timer_starting_cpu(unsigned int cpu)
        struct clock_event_device *clk = this_cpu_ptr(arch_timer_evt);
        u32 flags;
 
-       __arch_timer_setup(ARCH_CP15_TIMER, clk);
+       __arch_timer_setup(ARCH_TIMER_TYPE_CP15, clk);
 
        flags = check_ppi_trigger(arch_timer_ppi[arch_timer_uses_ppi]);
        enable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], flags);
 
        if (arch_timer_has_nonsecure_ppi()) {
-               flags = check_ppi_trigger(arch_timer_ppi[PHYS_NONSECURE_PPI]);
-               enable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI], flags);
+               flags = check_ppi_trigger(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
+               enable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
+                                 flags);
        }
 
        arch_counter_set_user_access();
@@ -562,43 +818,39 @@ static int arch_timer_starting_cpu(unsigned int cpu)
        return 0;
 }
 
-static void
-arch_timer_detect_rate(void __iomem *cntbase, struct device_node *np)
+/*
+ * For historical reasons, when probing with DT we use whichever (non-zero)
+ * rate was probed first, and don't verify that others match. If the first node
+ * probed has a clock-frequency property, this overrides the HW register.
+ */
+static void arch_timer_of_configure_rate(u32 rate, struct device_node *np)
 {
        /* Who has more than one independent system counter? */
        if (arch_timer_rate)
                return;
 
-       /*
-        * Try to determine the frequency from the device tree or CNTFRQ,
-        * if ACPI is enabled, get the frequency from CNTFRQ ONLY.
-        */
-       if (!acpi_disabled ||
-           of_property_read_u32(np, "clock-frequency", &arch_timer_rate)) {
-               if (cntbase)
-                       arch_timer_rate = readl_relaxed(cntbase + CNTFRQ);
-               else
-                       arch_timer_rate = arch_timer_get_cntfrq();
-       }
+       if (of_property_read_u32(np, "clock-frequency", &arch_timer_rate))
+               arch_timer_rate = rate;
 
        /* Check the timer frequency. */
        if (arch_timer_rate == 0)
-               pr_warn("Architected timer frequency not available\n");
+               pr_warn("frequency not available\n");
 }
 
 static void arch_timer_banner(unsigned type)
 {
-       pr_info("Architected %s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
-                    type & ARCH_CP15_TIMER ? "cp15" : "",
-                    type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ?  " and " : "",
-                    type & ARCH_MEM_TIMER ? "mmio" : "",
-                    (unsigned long)arch_timer_rate / 1000000,
-                    (unsigned long)(arch_timer_rate / 10000) % 100,
-                    type & ARCH_CP15_TIMER ?
-                    (arch_timer_uses_ppi == VIRT_PPI) ? "virt" : "phys" :
+       pr_info("%s%s%s timer(s) running at %lu.%02luMHz (%s%s%s).\n",
+               type & ARCH_TIMER_TYPE_CP15 ? "cp15" : "",
+               type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ?
+                       " and " : "",
+               type & ARCH_TIMER_TYPE_MEM ? "mmio" : "",
+               (unsigned long)arch_timer_rate / 1000000,
+               (unsigned long)(arch_timer_rate / 10000) % 100,
+               type & ARCH_TIMER_TYPE_CP15 ?
+                       (arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI) ? "virt" : "phys" :
                        "",
-                    type == (ARCH_CP15_TIMER | ARCH_MEM_TIMER) ?  "/" : "",
-                    type & ARCH_MEM_TIMER ?
+               type == (ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM) ? "/" : "",
+               type & ARCH_TIMER_TYPE_MEM ?
                        arch_timer_mem_use_virtual ? "virt" : "phys" :
                        "");
 }
@@ -621,37 +873,6 @@ static u64 arch_counter_get_cntvct_mem(void)
        return ((u64) vct_hi << 32) | vct_lo;
 }
 
-/*
- * Default to cp15 based access because arm64 uses this function for
- * sched_clock() before DT is probed and the cp15 method is guaranteed
- * to exist on arm64. arm doesn't use this before DT is probed so even
- * if we don't have the cp15 accessors we won't have a problem.
- */
-u64 (*arch_timer_read_counter)(void) = arch_counter_get_cntvct;
-
-static u64 arch_counter_read(struct clocksource *cs)
-{
-       return arch_timer_read_counter();
-}
-
-static u64 arch_counter_read_cc(const struct cyclecounter *cc)
-{
-       return arch_timer_read_counter();
-}
-
-static struct clocksource clocksource_counter = {
-       .name   = "arch_sys_counter",
-       .rating = 400,
-       .read   = arch_counter_read,
-       .mask   = CLOCKSOURCE_MASK(56),
-       .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
-};
-
-static struct cyclecounter cyclecounter __ro_after_init = {
-       .read   = arch_counter_read_cc,
-       .mask   = CLOCKSOURCE_MASK(56),
-};
-
 static struct arch_timer_kvm_info arch_timer_kvm_info;
 
 struct arch_timer_kvm_info *arch_timer_get_kvm_info(void)
@@ -664,22 +885,14 @@ static void __init arch_counter_register(unsigned type)
        u64 start_count;
 
        /* Register the CP15 based counter if we have one */
-       if (type & ARCH_CP15_TIMER) {
-               if (IS_ENABLED(CONFIG_ARM64) || arch_timer_uses_ppi == VIRT_PPI)
+       if (type & ARCH_TIMER_TYPE_CP15) {
+               if (IS_ENABLED(CONFIG_ARM64) ||
+                   arch_timer_uses_ppi == ARCH_TIMER_VIRT_PPI)
                        arch_timer_read_counter = arch_counter_get_cntvct;
                else
                        arch_timer_read_counter = arch_counter_get_cntpct;
 
-               clocksource_counter.archdata.vdso_direct = true;
-
-#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
-               /*
-                * Don't use the vdso fastpath if errata require using
-                * the out-of-line counter accessor.
-                */
-               if (static_branch_unlikely(&arch_timer_read_ool_enabled))
-                       clocksource_counter.archdata.vdso_direct = false;
-#endif
+               clocksource_counter.archdata.vdso_direct = vdso_default;
        } else {
                arch_timer_read_counter = arch_counter_get_cntvct_mem;
        }
@@ -699,12 +912,11 @@ static void __init arch_counter_register(unsigned type)
 
 static void arch_timer_stop(struct clock_event_device *clk)
 {
-       pr_debug("arch_timer_teardown disable IRQ%d cpu #%d\n",
-                clk->irq, smp_processor_id());
+       pr_debug("disable IRQ%d cpu #%d\n", clk->irq, smp_processor_id());
 
        disable_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi]);
        if (arch_timer_has_nonsecure_ppi())
-               disable_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI]);
+               disable_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI]);
 
        clk->set_state_shutdown(clk);
 }
@@ -718,14 +930,14 @@ static int arch_timer_dying_cpu(unsigned int cpu)
 }
 
 #ifdef CONFIG_CPU_PM
-static unsigned int saved_cntkctl;
+static DEFINE_PER_CPU(unsigned long, saved_cntkctl);
 static int arch_timer_cpu_pm_notify(struct notifier_block *self,
                                    unsigned long action, void *hcpu)
 {
        if (action == CPU_PM_ENTER)
-               saved_cntkctl = arch_timer_get_cntkctl();
+               __this_cpu_write(saved_cntkctl, arch_timer_get_cntkctl());
        else if (action == CPU_PM_ENTER_FAILED || action == CPU_PM_EXIT)
-               arch_timer_set_cntkctl(saved_cntkctl);
+               arch_timer_set_cntkctl(__this_cpu_read(saved_cntkctl));
        return NOTIFY_OK;
 }
 
@@ -767,24 +979,24 @@ static int __init arch_timer_register(void)
 
        ppi = arch_timer_ppi[arch_timer_uses_ppi];
        switch (arch_timer_uses_ppi) {
-       case VIRT_PPI:
+       case ARCH_TIMER_VIRT_PPI:
                err = request_percpu_irq(ppi, arch_timer_handler_virt,
                                         "arch_timer", arch_timer_evt);
                break;
-       case PHYS_SECURE_PPI:
-       case PHYS_NONSECURE_PPI:
+       case ARCH_TIMER_PHYS_SECURE_PPI:
+       case ARCH_TIMER_PHYS_NONSECURE_PPI:
                err = request_percpu_irq(ppi, arch_timer_handler_phys,
                                         "arch_timer", arch_timer_evt);
-               if (!err && arch_timer_ppi[PHYS_NONSECURE_PPI]) {
-                       ppi = arch_timer_ppi[PHYS_NONSECURE_PPI];
+               if (!err && arch_timer_has_nonsecure_ppi()) {
+                       ppi = arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI];
                        err = request_percpu_irq(ppi, arch_timer_handler_phys,
                                                 "arch_timer", arch_timer_evt);
                        if (err)
-                               free_percpu_irq(arch_timer_ppi[PHYS_SECURE_PPI],
+                               free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI],
                                                arch_timer_evt);
                }
                break;
-       case HYP_PPI:
+       case ARCH_TIMER_HYP_PPI:
                err = request_percpu_irq(ppi, arch_timer_handler_phys,
                                         "arch_timer", arch_timer_evt);
                break;
@@ -793,8 +1005,7 @@ static int __init arch_timer_register(void)
        }
 
        if (err) {
-               pr_err("arch_timer: can't register interrupt %d (%d)\n",
-                      ppi, err);
+               pr_err("can't register interrupt %d (%d)\n", ppi, err);
                goto out_free;
        }
 
@@ -817,7 +1028,7 @@ out_unreg_cpupm:
 out_unreg_notify:
        free_percpu_irq(arch_timer_ppi[arch_timer_uses_ppi], arch_timer_evt);
        if (arch_timer_has_nonsecure_ppi())
-               free_percpu_irq(arch_timer_ppi[PHYS_NONSECURE_PPI],
+               free_percpu_irq(arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI],
                                arch_timer_evt);
 
 out_free:
@@ -838,7 +1049,7 @@ static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
 
        t->base = base;
        t->evt.irq = irq;
-       __arch_timer_setup(ARCH_MEM_TIMER, &t->evt);
+       __arch_timer_setup(ARCH_TIMER_TYPE_MEM, &t->evt);
 
        if (arch_timer_mem_use_virtual)
                func = arch_timer_handler_virt_mem;
@@ -847,7 +1058,7 @@ static int __init arch_timer_mem_register(void __iomem *base, unsigned int irq)
 
        ret = request_irq(irq, func, IRQF_TIMER, "arch_mem_timer", &t->evt);
        if (ret) {
-               pr_err("arch_timer: Failed to request mem timer irq\n");
+               pr_err("Failed to request mem timer irq\n");
                kfree(t);
        }
 
@@ -865,15 +1076,28 @@ static const struct of_device_id arch_timer_mem_of_match[] __initconst = {
        {},
 };
 
-static bool __init
-arch_timer_needs_probing(int type, const struct of_device_id *matches)
+static bool __init arch_timer_needs_of_probing(void)
 {
        struct device_node *dn;
        bool needs_probing = false;
+       unsigned int mask = ARCH_TIMER_TYPE_CP15 | ARCH_TIMER_TYPE_MEM;
+
+       /* We have two timers, and both device-tree nodes are probed. */
+       if ((arch_timers_present & mask) == mask)
+               return false;
+
+       /*
+        * Only one type of timer is probed,
+        * check if we have another type of timer node in device-tree.
+        */
+       if (arch_timers_present & ARCH_TIMER_TYPE_CP15)
+               dn = of_find_matching_node(NULL, arch_timer_mem_of_match);
+       else
+               dn = of_find_matching_node(NULL, arch_timer_of_match);
 
-       dn = of_find_matching_node(NULL, matches);
-       if (dn && of_device_is_available(dn) && !(arch_timers_present & type))
+       if (dn && of_device_is_available(dn))
                needs_probing = true;
+
        of_node_put(dn);
 
        return needs_probing;
@@ -881,96 +1105,66 @@ arch_timer_needs_probing(int type, const struct of_device_id *matches)
 
 static int __init arch_timer_common_init(void)
 {
-       unsigned mask = ARCH_CP15_TIMER | ARCH_MEM_TIMER;
-
-       /* Wait until both nodes are probed if we have two timers */
-       if ((arch_timers_present & mask) != mask) {
-               if (arch_timer_needs_probing(ARCH_MEM_TIMER, arch_timer_mem_of_match))
-                       return 0;
-               if (arch_timer_needs_probing(ARCH_CP15_TIMER, arch_timer_of_match))
-                       return 0;
-       }
-
        arch_timer_banner(arch_timers_present);
        arch_counter_register(arch_timers_present);
        return arch_timer_arch_init();
 }
 
-static int __init arch_timer_init(void)
+/**
+ * arch_timer_select_ppi() - Select suitable PPI for the current system.
+ *
+ * If HYP mode is available, we know that the physical timer
+ * has been configured to be accessible from PL1. Use it, so
+ * that a guest can use the virtual timer instead.
+ *
+ * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
+ * accesses to CNTP_*_EL1 registers are silently redirected to
+ * their CNTHP_*_EL2 counterparts, and use a different PPI
+ * number.
+ *
+ * If no interrupt provided for virtual timer, we'll have to
+ * stick to the physical timer. It'd better be accessible...
+ * For arm64 we never use the secure interrupt.
+ *
+ * Return: a suitable PPI type for the current system.
+ */
+static enum arch_timer_ppi_nr __init arch_timer_select_ppi(void)
 {
-       int ret;
-       /*
-        * If HYP mode is available, we know that the physical timer
-        * has been configured to be accessible from PL1. Use it, so
-        * that a guest can use the virtual timer instead.
-        *
-        * If no interrupt provided for virtual timer, we'll have to
-        * stick to the physical timer. It'd better be accessible...
-        *
-        * On ARMv8.1 with VH extensions, the kernel runs in HYP. VHE
-        * accesses to CNTP_*_EL1 registers are silently redirected to
-        * their CNTHP_*_EL2 counterparts, and use a different PPI
-        * number.
-        */
-       if (is_hyp_mode_available() || !arch_timer_ppi[VIRT_PPI]) {
-               bool has_ppi;
+       if (is_kernel_in_hyp_mode())
+               return ARCH_TIMER_HYP_PPI;
 
-               if (is_kernel_in_hyp_mode()) {
-                       arch_timer_uses_ppi = HYP_PPI;
-                       has_ppi = !!arch_timer_ppi[HYP_PPI];
-               } else {
-                       arch_timer_uses_ppi = PHYS_SECURE_PPI;
-                       has_ppi = (!!arch_timer_ppi[PHYS_SECURE_PPI] ||
-                                  !!arch_timer_ppi[PHYS_NONSECURE_PPI]);
-               }
+       if (!is_hyp_mode_available() && arch_timer_ppi[ARCH_TIMER_VIRT_PPI])
+               return ARCH_TIMER_VIRT_PPI;
 
-               if (!has_ppi) {
-                       pr_warn("arch_timer: No interrupt available, giving up\n");
-                       return -EINVAL;
-               }
-       }
+       if (IS_ENABLED(CONFIG_ARM64))
+               return ARCH_TIMER_PHYS_NONSECURE_PPI;
 
-       ret = arch_timer_register();
-       if (ret)
-               return ret;
-
-       ret = arch_timer_common_init();
-       if (ret)
-               return ret;
-
-       arch_timer_kvm_info.virtual_irq = arch_timer_ppi[VIRT_PPI];
-       
-       return 0;
+       return ARCH_TIMER_PHYS_SECURE_PPI;
 }
 
 static int __init arch_timer_of_init(struct device_node *np)
 {
-       int i;
+       int i, ret;
+       u32 rate;
 
-       if (arch_timers_present & ARCH_CP15_TIMER) {
-               pr_warn("arch_timer: multiple nodes in dt, skipping\n");
+       if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
+               pr_warn("multiple nodes in dt, skipping\n");
                return 0;
        }
 
-       arch_timers_present |= ARCH_CP15_TIMER;
-       for (i = PHYS_SECURE_PPI; i < MAX_TIMER_PPI; i++)
+       arch_timers_present |= ARCH_TIMER_TYPE_CP15;
+       for (i = ARCH_TIMER_PHYS_SECURE_PPI; i < ARCH_TIMER_MAX_TIMER_PPI; i++)
                arch_timer_ppi[i] = irq_of_parse_and_map(np, i);
 
-       arch_timer_detect_rate(NULL, np);
+       arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
+
+       rate = arch_timer_get_cntfrq();
+       arch_timer_of_configure_rate(rate, np);
 
        arch_timer_c3stop = !of_property_read_bool(np, "always-on");
 
-#ifdef CONFIG_ARM_ARCH_TIMER_OOL_WORKAROUND
-       for (i = 0; i < ARRAY_SIZE(ool_workarounds); i++) {
-               if (of_property_read_bool(np, ool_workarounds[i].id)) {
-                       timer_unstable_counter_workaround = &ool_workarounds[i];
-                       static_branch_enable(&arch_timer_read_ool_enabled);
-                       pr_info("arch_timer: Enabling workaround for %s\n",
-                               timer_unstable_counter_workaround->id);
-                       break;
-               }
-       }
-#endif
+       /* Check for globally applicable workarounds */
+       arch_timer_check_ool_workaround(ate_match_dt, np);
 
        /*
         * If we cannot rely on firmware initializing the timer registers then
@@ -978,29 +1172,63 @@ static int __init arch_timer_of_init(struct device_node *np)
         */
        if (IS_ENABLED(CONFIG_ARM) &&
            of_property_read_bool(np, "arm,cpu-registers-not-fw-configured"))
-               arch_timer_uses_ppi = PHYS_SECURE_PPI;
+               arch_timer_uses_ppi = ARCH_TIMER_PHYS_SECURE_PPI;
+       else
+               arch_timer_uses_ppi = arch_timer_select_ppi();
+
+       if (!arch_timer_ppi[arch_timer_uses_ppi]) {
+               pr_err("No interrupt available, giving up\n");
+               return -EINVAL;
+       }
 
        /* On some systems, the counter stops ticking when in suspend. */
        arch_counter_suspend_stop = of_property_read_bool(np,
                                                         "arm,no-tick-in-suspend");
 
-       return arch_timer_init();
+       ret = arch_timer_register();
+       if (ret)
+               return ret;
+
+       if (arch_timer_needs_of_probing())
+               return 0;
+
+       return arch_timer_common_init();
 }
 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer, "arm,armv7-timer", arch_timer_of_init);
 CLOCKSOURCE_OF_DECLARE(armv8_arch_timer, "arm,armv8-timer", arch_timer_of_init);
 
-static int __init arch_timer_mem_init(struct device_node *np)
+static u32 __init
+arch_timer_mem_frame_get_cntfrq(struct arch_timer_mem_frame *frame)
 {
-       struct device_node *frame, *best_frame = NULL;
-       void __iomem *cntctlbase, *base;
-       unsigned int irq, ret = -EINVAL;
+       void __iomem *base;
+       u32 rate;
+
+       base = ioremap(frame->cntbase, frame->size);
+       if (!base) {
+               pr_err("Unable to map frame @ %pa\n", &frame->cntbase);
+               return 0;
+       }
+
+       rate = readl_relaxed(frame + CNTFRQ);
+
+       iounmap(frame);
+
+       return rate;
+}
+
+static struct arch_timer_mem_frame * __init
+arch_timer_mem_find_best_frame(struct arch_timer_mem *timer_mem)
+{
+       struct arch_timer_mem_frame *frame, *best_frame = NULL;
+       void __iomem *cntctlbase;
        u32 cnttidr;
+       int i;
 
-       arch_timers_present |= ARCH_MEM_TIMER;
-       cntctlbase = of_iomap(np, 0);
+       cntctlbase = ioremap(timer_mem->cntctlbase, timer_mem->size);
        if (!cntctlbase) {
-               pr_err("arch_timer: Can't find CNTCTLBase\n");
-               return -ENXIO;
+               pr_err("Can't map CNTCTLBase @ %pa\n",
+                       &timer_mem->cntctlbase);
+               return NULL;
        }
 
        cnttidr = readl_relaxed(cntctlbase + CNTTIDR);
@@ -1009,25 +1237,20 @@ static int __init arch_timer_mem_init(struct device_node *np)
         * Try to find a virtual capable frame. Otherwise fall back to a
         * physical capable frame.
         */
-       for_each_available_child_of_node(np, frame) {
-               int n;
-               u32 cntacr;
+       for (i = 0; i < ARCH_TIMER_MEM_MAX_FRAMES; i++) {
+               u32 cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
+                            CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
 
-               if (of_property_read_u32(frame, "frame-number", &n)) {
-                       pr_err("arch_timer: Missing frame-number\n");
-                       of_node_put(frame);
-                       goto out;
-               }
+               frame = &timer_mem->frame[i];
+               if (!frame->valid)
+                       continue;
 
                /* Try enabling everything, and see what sticks */
-               cntacr = CNTACR_RFRQ | CNTACR_RWPT | CNTACR_RPCT |
-                        CNTACR_RWVT | CNTACR_RVOFF | CNTACR_RVCT;
-               writel_relaxed(cntacr, cntctlbase + CNTACR(n));
-               cntacr = readl_relaxed(cntctlbase + CNTACR(n));
+               writel_relaxed(cntacr, cntctlbase + CNTACR(i));
+               cntacr = readl_relaxed(cntctlbase + CNTACR(i));
 
-               if ((cnttidr & CNTTIDR_VIRT(n)) &&
+               if ((cnttidr & CNTTIDR_VIRT(i)) &&
                    !(~cntacr & (CNTACR_RWVT | CNTACR_RVCT))) {
-                       of_node_put(best_frame);
                        best_frame = frame;
                        arch_timer_mem_use_virtual = true;
                        break;
@@ -1036,43 +1259,131 @@ static int __init arch_timer_mem_init(struct device_node *np)
                if (~cntacr & (CNTACR_RWPT | CNTACR_RPCT))
                        continue;
 
-               of_node_put(best_frame);
-               best_frame = of_node_get(frame);
+               best_frame = frame;
        }
 
-       ret= -ENXIO;
-       base = arch_counter_base = of_io_request_and_map(best_frame, 0,
-                                                        "arch_mem_timer");
-       if (IS_ERR(base)) {
-               pr_err("arch_timer: Can't map frame's registers\n");
-               goto out;
-       }
+       iounmap(cntctlbase);
+
+       if (!best_frame)
+               pr_err("Unable to find a suitable frame in timer @ %pa\n",
+                       &timer_mem->cntctlbase);
+
+       return frame;
+}
+
+static int __init
+arch_timer_mem_frame_register(struct arch_timer_mem_frame *frame)
+{
+       void __iomem *base;
+       int ret, irq = 0;
 
        if (arch_timer_mem_use_virtual)
-               irq = irq_of_parse_and_map(best_frame, 1);
+               irq = frame->virt_irq;
        else
-               irq = irq_of_parse_and_map(best_frame, 0);
+               irq = frame->phys_irq;
 
-       ret = -EINVAL;
        if (!irq) {
-               pr_err("arch_timer: Frame missing %s irq",
+               pr_err("Frame missing %s irq.\n",
                       arch_timer_mem_use_virtual ? "virt" : "phys");
-               goto out;
+               return -EINVAL;
+       }
+
+       if (!request_mem_region(frame->cntbase, frame->size,
+                               "arch_mem_timer"))
+               return -EBUSY;
+
+       base = ioremap(frame->cntbase, frame->size);
+       if (!base) {
+               pr_err("Can't map frame's registers\n");
+               return -ENXIO;
        }
 
-       arch_timer_detect_rate(base, np);
        ret = arch_timer_mem_register(base, irq);
-       if (ret)
+       if (ret) {
+               iounmap(base);
+               return ret;
+       }
+
+       arch_counter_base = base;
+       arch_timers_present |= ARCH_TIMER_TYPE_MEM;
+
+       return 0;
+}
+
+static int __init arch_timer_mem_of_init(struct device_node *np)
+{
+       struct arch_timer_mem *timer_mem;
+       struct arch_timer_mem_frame *frame;
+       struct device_node *frame_node;
+       struct resource res;
+       int ret = -EINVAL;
+       u32 rate;
+
+       timer_mem = kzalloc(sizeof(*timer_mem), GFP_KERNEL);
+       if (!timer_mem)
+               return -ENOMEM;
+
+       if (of_address_to_resource(np, 0, &res))
                goto out;
+       timer_mem->cntctlbase = res.start;
+       timer_mem->size = resource_size(&res);
 
-       return arch_timer_common_init();
+       for_each_available_child_of_node(np, frame_node) {
+               u32 n;
+               struct arch_timer_mem_frame *frame;
+
+               if (of_property_read_u32(frame_node, "frame-number", &n)) {
+                       pr_err(FW_BUG "Missing frame-number.\n");
+                       of_node_put(frame_node);
+                       goto out;
+               }
+               if (n >= ARCH_TIMER_MEM_MAX_FRAMES) {
+                       pr_err(FW_BUG "Wrong frame-number, only 0-%u are permitted.\n",
+                              ARCH_TIMER_MEM_MAX_FRAMES - 1);
+                       of_node_put(frame_node);
+                       goto out;
+               }
+               frame = &timer_mem->frame[n];
+
+               if (frame->valid) {
+                       pr_err(FW_BUG "Duplicated frame-number.\n");
+                       of_node_put(frame_node);
+                       goto out;
+               }
+
+               if (of_address_to_resource(frame_node, 0, &res)) {
+                       of_node_put(frame_node);
+                       goto out;
+               }
+               frame->cntbase = res.start;
+               frame->size = resource_size(&res);
+
+               frame->virt_irq = irq_of_parse_and_map(frame_node,
+                                                      ARCH_TIMER_VIRT_SPI);
+               frame->phys_irq = irq_of_parse_and_map(frame_node,
+                                                      ARCH_TIMER_PHYS_SPI);
+
+               frame->valid = true;
+       }
+
+       frame = arch_timer_mem_find_best_frame(timer_mem);
+       if (!frame) {
+               ret = -EINVAL;
+               goto out;
+       }
+
+       rate = arch_timer_mem_frame_get_cntfrq(frame);
+       arch_timer_of_configure_rate(rate, np);
+
+       ret = arch_timer_mem_frame_register(frame);
+       if (!ret && !arch_timer_needs_of_probing())
+               ret = arch_timer_common_init();
 out:
-       iounmap(cntctlbase);
-       of_node_put(best_frame);
+       kfree(timer_mem);
        return ret;
 }
 CLOCKSOURCE_OF_DECLARE(armv7_arch_timer_mem, "arm,armv7-timer-mem",
-                      arch_timer_mem_init);
+                      arch_timer_mem_of_init);
 
 #ifdef CONFIG_ACPI
 static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
@@ -1094,41 +1405,63 @@ static int __init map_generic_timer_interrupt(u32 interrupt, u32 flags)
 /* Initialize per-processor generic timer */
 static int __init arch_timer_acpi_init(struct acpi_table_header *table)
 {
+       int ret;
        struct acpi_table_gtdt *gtdt;
 
-       if (arch_timers_present & ARCH_CP15_TIMER) {
-               pr_warn("arch_timer: already initialized, skipping\n");
+       if (arch_timers_present & ARCH_TIMER_TYPE_CP15) {
+               pr_warn("already initialized, skipping\n");
                return -EINVAL;
        }
 
        gtdt = container_of(table, struct acpi_table_gtdt, header);
 
-       arch_timers_present |= ARCH_CP15_TIMER;
+       arch_timers_present |= ARCH_TIMER_TYPE_CP15;
 
-       arch_timer_ppi[PHYS_SECURE_PPI] =
+       arch_timer_ppi[ARCH_TIMER_PHYS_SECURE_PPI] =
                map_generic_timer_interrupt(gtdt->secure_el1_interrupt,
                gtdt->secure_el1_flags);
 
-       arch_timer_ppi[PHYS_NONSECURE_PPI] =
+       arch_timer_ppi[ARCH_TIMER_PHYS_NONSECURE_PPI] =
                map_generic_timer_interrupt(gtdt->non_secure_el1_interrupt,
                gtdt->non_secure_el1_flags);
 
-       arch_timer_ppi[VIRT_PPI] =
+       arch_timer_ppi[ARCH_TIMER_VIRT_PPI] =
                map_generic_timer_interrupt(gtdt->virtual_timer_interrupt,
                gtdt->virtual_timer_flags);
 
-       arch_timer_ppi[HYP_PPI] =
+       arch_timer_ppi[ARCH_TIMER_HYP_PPI] =
                map_generic_timer_interrupt(gtdt->non_secure_el2_interrupt,
                gtdt->non_secure_el2_flags);
 
-       /* Get the frequency from CNTFRQ */
-       arch_timer_detect_rate(NULL, NULL);
+       arch_timer_kvm_info.virtual_irq = arch_timer_ppi[ARCH_TIMER_VIRT_PPI];
+
+       /*
+        * When probing via ACPI, we have no mechanism to override the sysreg
+        * CNTFRQ value. This *must* be correct.
+        */
+       arch_timer_rate = arch_timer_get_cntfrq();
+       if (!arch_timer_rate) {
+               pr_err(FW_BUG "frequency not available.\n");
+               return -EINVAL;
+       }
+
+       arch_timer_uses_ppi = arch_timer_select_ppi();
+       if (!arch_timer_ppi[arch_timer_uses_ppi]) {
+               pr_err("No interrupt available, giving up\n");
+               return -EINVAL;
+       }
 
        /* Always-on capability */
        arch_timer_c3stop = !(gtdt->non_secure_el1_flags & ACPI_GTDT_ALWAYS_ON);
 
-       arch_timer_init();
-       return 0;
+       /* Check for globally applicable workarounds */
+       arch_timer_check_ool_workaround(ate_match_acpi_oem_info, table);
+
+       ret = arch_timer_register();
+       if (ret)
+               return ret;
+
+       return arch_timer_common_init();
 }
 CLOCKSOURCE_ACPI_DECLARE(arch_timer, ACPI_SIG_GTDT, arch_timer_acpi_init);
 #endif