coresight: etm4x: Refactor probing routine
[linux-2.6-microblaze.git] / drivers / hwtracing / coresight / coresight-etm4x-core.c
index f095ab9..c3e458a 100644 (file)
@@ -59,6 +59,11 @@ static u64 etm4_get_access_type(struct etmv4_config *config);
 
 static enum cpuhp_state hp_online;
 
+struct etm4_init_arg {
+       struct etmv4_drvdata    *drvdata;
+       struct csdev_access     *csa;
+};
+
 /*
  * Check if TRCSSPCICRn(i) is implemented for a given instance.
  *
@@ -148,18 +153,6 @@ static void etm4_cs_unlock(struct etmv4_drvdata *drvdata,
                CS_UNLOCK(csa->base);
 }
 
-static bool etm4_arch_supported(u8 arch)
-{
-       /* Mask out the minor version number */
-       switch (arch & 0xf0) {
-       case ETM_ARCH_V4:
-               break;
-       default:
-               return false;
-       }
-       return true;
-}
-
 static int etm4_cpu_id(struct coresight_device *csdev)
 {
        struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
@@ -291,6 +284,15 @@ static int etm4_enable_hw(struct etmv4_drvdata *drvdata)
        /* Disable the trace unit before programming trace registers */
        etm4x_relaxed_write32(csa, 0, TRCPRGCTLR);
 
+       /*
+        * If we use system instructions, we need to synchronize the
+        * write to the TRCPRGCTLR, before accessing the TRCSTATR.
+        * See ARM IHI0064F, section
+        * "4.3.7 Synchronization of register updates"
+        */
+       if (!csa->io_mem)
+               isb();
+
        /* wait for TRCSTATR.IDLE to go up */
        if (coresight_timeout(csa, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
                dev_err(etm_dev,
@@ -369,6 +371,10 @@ static int etm4_enable_hw(struct etmv4_drvdata *drvdata)
        /* Enable the trace unit */
        etm4x_relaxed_write32(csa, 1, TRCPRGCTLR);
 
+       /* Synchronize the register updates for sysreg access */
+       if (!csa->io_mem)
+               isb();
+
        /* wait for TRCSTATR.IDLE to go back down to '0' */
        if (coresight_timeout(csa, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
                dev_err(etm_dev,
@@ -776,19 +782,104 @@ static const struct coresight_ops etm4_cs_ops = {
        .source_ops     = &etm4_source_ops,
 };
 
+static inline bool cpu_supports_sysreg_trace(void)
+{
+       u64 dfr0 = read_sysreg_s(SYS_ID_AA64DFR0_EL1);
+
+       return ((dfr0 >> ID_AA64DFR0_TRACEVER_SHIFT) & 0xfUL) > 0;
+}
+
+static bool etm4_init_sysreg_access(struct etmv4_drvdata *drvdata,
+                                   struct csdev_access *csa)
+{
+       u32 devarch;
+
+       if (!cpu_supports_sysreg_trace())
+               return false;
+
+       /*
+        * ETMs implementing sysreg access must implement TRCDEVARCH.
+        */
+       devarch = read_etm4x_sysreg_const_offset(TRCDEVARCH);
+       if ((devarch & ETM_DEVARCH_ID_MASK) != ETM_DEVARCH_ETMv4x_ARCH)
+               return false;
+       *csa = (struct csdev_access) {
+               .io_mem = false,
+               .read   = etm4x_sysreg_read,
+               .write  = etm4x_sysreg_write,
+       };
+
+       drvdata->arch = etm_devarch_to_arch(devarch);
+       return true;
+}
+
+static bool etm4_init_iomem_access(struct etmv4_drvdata *drvdata,
+                                  struct csdev_access *csa)
+{
+       u32 devarch = readl_relaxed(drvdata->base + TRCDEVARCH);
+       u32 idr1 = readl_relaxed(drvdata->base + TRCIDR1);
+
+       /*
+        * All ETMs must implement TRCDEVARCH to indicate that
+        * the component is an ETMv4. To support any broken
+        * implementations we fall back to TRCIDR1 check, which
+        * is not really reliable.
+        */
+       if ((devarch & ETM_DEVARCH_ID_MASK) == ETM_DEVARCH_ETMv4x_ARCH) {
+               drvdata->arch = etm_devarch_to_arch(devarch);
+       } else {
+               pr_warn("CPU%d: ETM4x incompatible TRCDEVARCH: %x, falling back to TRCIDR1\n",
+                       smp_processor_id(), devarch);
+
+               if (ETM_TRCIDR1_ARCH_MAJOR(idr1) != ETM_TRCIDR1_ARCH_ETMv4)
+                       return false;
+               drvdata->arch = etm_trcidr_to_arch(idr1);
+       }
+
+       *csa = CSDEV_ACCESS_IOMEM(drvdata->base);
+       return true;
+}
+
+static bool etm4_init_csdev_access(struct etmv4_drvdata *drvdata,
+                                  struct csdev_access *csa)
+{
+       /*
+        * Always choose the memory mapped io, if there is
+        * a memory map to prevent sysreg access on broken
+        * systems.
+        */
+       if (drvdata->base)
+               return etm4_init_iomem_access(drvdata, csa);
+
+       if (etm4_init_sysreg_access(drvdata, csa))
+               return true;
+
+       return false;
+}
+
 static void etm4_init_arch_data(void *info)
 {
        u32 etmidr0;
-       u32 etmidr1;
        u32 etmidr2;
        u32 etmidr3;
        u32 etmidr4;
        u32 etmidr5;
-       struct etmv4_drvdata *drvdata = info;
-       struct csdev_access tmp_csa = CSDEV_ACCESS_IOMEM(drvdata->base);
-       struct csdev_access *csa = &tmp_csa;
+       struct etm4_init_arg *init_arg = info;
+       struct etmv4_drvdata *drvdata;
+       struct csdev_access *csa;
        int i;
 
+       drvdata = init_arg->drvdata;
+       csa = init_arg->csa;
+
+       /*
+        * If we are unable to detect the access mechanism,
+        * or unable to detect the trace unit type, fail
+        * early.
+        */
+       if (!etm4_init_csdev_access(drvdata, csa))
+               return;
+
        /* Make sure all registers are accessible */
        etm4_os_unlock_csa(drvdata, csa);
        etm4_cs_unlock(drvdata, csa);
@@ -833,15 +924,6 @@ static void etm4_init_arch_data(void *info)
        /* TSSIZE, bits[28:24] Global timestamp size field */
        drvdata->ts_size = BMVAL(etmidr0, 24, 28);
 
-       /* base architecture of trace unit */
-       etmidr1 = etm4x_relaxed_read32(csa, TRCIDR1);
-       /*
-        * TRCARCHMIN, bits[7:4] architecture the minor version number
-        * TRCARCHMAJ, bits[11:8] architecture major versin number
-        */
-       drvdata->arch = BMVAL(etmidr1, 4, 11);
-       drvdata->config.arch = drvdata->arch;
-
        /* maximum size of resources */
        etmidr2 = etm4x_relaxed_read32(csa, TRCIDR2);
        /* CIDSIZE, bits[9:5] Indicates the Context ID size */
@@ -856,6 +938,7 @@ static void etm4_init_arch_data(void *info)
        drvdata->ccitmin = BMVAL(etmidr3, 0, 11);
        /* EXLEVEL_S, bits[19:16] Secure state instruction tracing */
        drvdata->s_ex_level = BMVAL(etmidr3, 16, 19);
+       drvdata->config.s_ex_level = drvdata->s_ex_level;
        /* EXLEVEL_NS, bits[23:20] Non-secure state instruction tracing */
        drvdata->ns_ex_level = BMVAL(etmidr3, 20, 23);
 
@@ -917,7 +1000,7 @@ static void etm4_init_arch_data(void *info)
         * Otherwise for values 0x1 and above the number is N + 1 as per v4.2.
         */
        drvdata->nr_resource = BMVAL(etmidr4, 16, 19);
-       if ((drvdata->arch < ETM4X_ARCH_4V3) || (drvdata->nr_resource > 0))
+       if ((drvdata->arch < ETM_ARCH_V4_3) || (drvdata->nr_resource > 0))
                drvdata->nr_resource += 1;
        /*
         * NUMSSCC, bits[23:20] the number of single-shot
@@ -959,20 +1042,16 @@ static void etm4_init_arch_data(void *info)
        etm4_cs_lock(drvdata, csa);
 }
 
+static inline u32 etm4_get_victlr_access_type(struct etmv4_config *config)
+{
+       return etm4_get_access_type(config) << TRCVICTLR_EXLEVEL_SHIFT;
+}
+
 /* Set ELx trace filter access in the TRCVICTLR register */
 static void etm4_set_victlr_access(struct etmv4_config *config)
 {
-       u64 access_type;
-
-       config->vinst_ctrl &= ~(ETM_EXLEVEL_S_VICTLR_MASK | ETM_EXLEVEL_NS_VICTLR_MASK);
-
-       /*
-        * TRCVICTLR::EXLEVEL_NS:EXLEVELS: Set kernel / user filtering
-        * bits in vinst_ctrl, same bit pattern as TRCACATRn values returned by
-        * etm4_get_access_type() but with a relative shift in this register.
-        */
-       access_type = etm4_get_access_type(config) << ETM_EXLEVEL_LSHIFT_TRCVICTLR;
-       config->vinst_ctrl |= (u32)access_type;
+       config->vinst_ctrl &= ~TRCVICTLR_EXLEVEL_MASK;
+       config->vinst_ctrl |= etm4_get_victlr_access_type(config);
 }
 
 static void etm4_set_default_config(struct etmv4_config *config)
@@ -1002,12 +1081,9 @@ static u64 etm4_get_ns_access_type(struct etmv4_config *config)
        u64 access_type = 0;
 
        /*
-        * EXLEVEL_NS, bits[15:12]
-        * The Exception levels are:
-        *   Bit[12] Exception level 0 - Application
-        *   Bit[13] Exception level 1 - OS
-        *   Bit[14] Exception level 2 - Hypervisor
-        *   Bit[15] Never implemented
+        * EXLEVEL_NS, for NonSecure Exception levels.
+        * The mask here is a generic value and must be
+        * shifted to the corresponding field for the registers
         */
        if (!is_kernel_in_hyp_mode()) {
                /* Stay away from hypervisor mode for non-VHE */
@@ -1024,27 +1100,26 @@ static u64 etm4_get_ns_access_type(struct etmv4_config *config)
        return access_type;
 }
 
+/*
+ * Construct the exception level masks for a given config.
+ * This must be shifted to the corresponding register field
+ * for usage.
+ */
 static u64 etm4_get_access_type(struct etmv4_config *config)
 {
-       u64 access_type = etm4_get_ns_access_type(config);
-       u64 s_hyp = (config->arch & 0x0f) >= 0x4 ? ETM_EXLEVEL_S_HYP : 0;
-
-       /*
-        * EXLEVEL_S, bits[11:8], don't trace anything happening
-        * in secure state.
-        */
-       access_type |= (ETM_EXLEVEL_S_APP       |
-                       ETM_EXLEVEL_S_OS        |
-                       s_hyp                   |
-                       ETM_EXLEVEL_S_MON);
+       /* All Secure exception levels are excluded from the trace */
+       return etm4_get_ns_access_type(config) | (u64)config->s_ex_level;
+}
 
-       return access_type;
+static u64 etm4_get_comparator_access_type(struct etmv4_config *config)
+{
+       return etm4_get_access_type(config) << TRCACATR_EXLEVEL_SHIFT;
 }
 
 static void etm4_set_comparator_filter(struct etmv4_config *config,
                                       u64 start, u64 stop, int comparator)
 {
-       u64 access_type = etm4_get_access_type(config);
+       u64 access_type = etm4_get_comparator_access_type(config);
 
        /* First half of default address comparator */
        config->addr_val[comparator] = start;
@@ -1079,7 +1154,7 @@ static void etm4_set_start_stop_filter(struct etmv4_config *config,
                                       enum etm_addr_type type)
 {
        int shift;
-       u64 access_type = etm4_get_access_type(config);
+       u64 access_type = etm4_get_comparator_access_type(config);
 
        /* Configure the comparator */
        config->addr_val[comparator] = address;
@@ -1633,15 +1708,13 @@ static void etm4_pm_clear(void)
        }
 }
 
-static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
+static int etm4_probe(struct device *dev, void __iomem *base, u32 etm_pid)
 {
        int ret;
-       void __iomem *base;
-       struct device *dev = &adev->dev;
        struct coresight_platform_data *pdata = NULL;
        struct etmv4_drvdata *drvdata;
-       struct resource *res = &adev->res;
        struct coresight_desc desc = { 0 };
+       struct etm4_init_arg init_arg = { 0 };
 
        drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
        if (!drvdata)
@@ -1663,13 +1736,7 @@ static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
        if (fwnode_property_present(dev_fwnode(dev), "qcom,skip-power-up"))
                drvdata->skip_power_up = true;
 
-       /* Validity for the resource is already checked by the AMBA core */
-       base = devm_ioremap_resource(dev, res);
-       if (IS_ERR(base))
-               return PTR_ERR(base);
-
        drvdata->base = base;
-       desc.access = CSDEV_ACCESS_IOMEM(base);
 
        spin_lock_init(&drvdata->spinlock);
 
@@ -1681,11 +1748,14 @@ static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
        if (!desc.name)
                return -ENOMEM;
 
+       init_arg.drvdata = drvdata;
+       init_arg.csa = &desc.access;
+
        if (smp_call_function_single(drvdata->cpu,
-                               etm4_init_arch_data,  drvdata, 1))
+                               etm4_init_arch_data,  &init_arg, 1))
                dev_err(dev, "ETM arch init failed\n");
 
-       if (etm4_arch_supported(drvdata->arch) == false)
+       if (!drvdata->arch)
                return -EINVAL;
 
        etm4_init_trace_id(drvdata);
@@ -1695,7 +1765,7 @@ static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
        if (IS_ERR(pdata))
                return PTR_ERR(pdata);
 
-       adev->dev.platform_data = pdata;
+       dev->platform_data = pdata;
 
        desc.type = CORESIGHT_DEV_TYPE_SOURCE;
        desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
@@ -1715,20 +1785,39 @@ static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
 
        etmdrvdata[drvdata->cpu] = drvdata;
 
-       pm_runtime_put(&adev->dev);
        dev_info(&drvdata->csdev->dev, "CPU%d: ETM v%d.%d initialized\n",
-                drvdata->cpu, drvdata->arch >> 4, drvdata->arch & 0xf);
+                drvdata->cpu, ETM_ARCH_MAJOR_VERSION(drvdata->arch),
+                ETM_ARCH_MINOR_VERSION(drvdata->arch));
 
        if (boot_enable) {
                coresight_enable(drvdata->csdev);
                drvdata->boot_enable = true;
        }
 
-       etm4_check_arch_features(drvdata, id->id);
+       etm4_check_arch_features(drvdata, etm_pid);
 
        return 0;
 }
 
+static int etm4_probe_amba(struct amba_device *adev, const struct amba_id *id)
+{
+       void __iomem *base;
+       struct device *dev = &adev->dev;
+       struct resource *res = &adev->res;
+       int ret;
+
+       /* Validity for the resource is already checked by the AMBA core */
+       base = devm_ioremap_resource(dev, res);
+       if (IS_ERR(base))
+               return PTR_ERR(base);
+
+       ret = etm4_probe(dev, base, id->id);
+       if (!ret)
+               pm_runtime_put(&adev->dev);
+
+       return ret;
+}
+
 static struct amba_cs_uci_id uci_id_etm4[] = {
        {
                /*  ETMv4 UCI data */
@@ -1745,15 +1834,12 @@ static void clear_etmdrvdata(void *info)
        etmdrvdata[cpu] = NULL;
 }
 
-static int etm4_remove(struct amba_device *adev)
+static int __exit etm4_remove_dev(struct etmv4_drvdata *drvdata)
 {
-       struct etmv4_drvdata *drvdata = dev_get_drvdata(&adev->dev);
-
        etm_perf_symlink(drvdata->csdev, false);
-
        /*
-        * Taking hotplug lock here to avoid racing between etm4_remove and
-        * CPU hotplug call backs.
+        * Taking hotplug lock here to avoid racing between etm4_remove_dev()
+        * and CPU hotplug call backs.
         */
        cpus_read_lock();
        /*
@@ -1772,6 +1858,15 @@ static int etm4_remove(struct amba_device *adev)
        return 0;
 }
 
+static int __exit etm4_remove_amba(struct amba_device *adev)
+{
+       struct etmv4_drvdata *drvdata = dev_get_drvdata(&adev->dev);
+
+       if (drvdata)
+               return etm4_remove_dev(drvdata);
+       return 0;
+}
+
 static const struct amba_id etm4_ids[] = {
        CS_AMBA_ID(0x000bb95d),                 /* Cortex-A53 */
        CS_AMBA_ID(0x000bb95e),                 /* Cortex-A57 */
@@ -1795,14 +1890,14 @@ static const struct amba_id etm4_ids[] = {
 
 MODULE_DEVICE_TABLE(amba, etm4_ids);
 
-static struct amba_driver etm4x_driver = {
+static struct amba_driver etm4x_amba_driver = {
        .drv = {
                .name   = "coresight-etm4x",
                .owner  = THIS_MODULE,
                .suppress_bind_attrs = true,
        },
-       .probe          = etm4_probe,
-       .remove         = etm4_remove,
+       .probe          = etm4_probe_amba,
+       .remove         = etm4_remove_amba,
        .id_table       = etm4_ids,
 };
 
@@ -1816,7 +1911,7 @@ static int __init etm4x_init(void)
        if (ret)
                return ret;
 
-       ret = amba_driver_register(&etm4x_driver);
+       ret = amba_driver_register(&etm4x_amba_driver);
        if (ret) {
                pr_err("Error registering etm4x driver\n");
                etm4_pm_clear();
@@ -1827,7 +1922,7 @@ static int __init etm4x_init(void)
 
 static void __exit etm4x_exit(void)
 {
-       amba_driver_unregister(&etm4x_driver);
+       amba_driver_unregister(&etm4x_amba_driver);
        etm4_pm_clear();
 }