Merge remote-tracking branch 'char-misc/char-misc-next'
[deliverable/linux.git] / drivers / hwtracing / coresight / coresight-etm4x.c
index 1a5e0d14c1dd4d3fc1b876d8ff8fd1dd07c6c61c..4db8d6a4d0cbbe0545e5724b7de75fbae0438c68 100644 (file)
@@ -33,7 +33,6 @@
 #include <linux/uaccess.h>
 #include <linux/perf_event.h>
 #include <linux/pm_runtime.h>
-#include <linux/perf_event.h>
 #include <asm/sections.h>
 #include <asm/local.h>
 
@@ -46,7 +45,9 @@ module_param_named(boot_enable, boot_enable, int, S_IRUGO);
 /* The number of ETMv4 currently registered */
 static int etm4_count;
 static struct etmv4_drvdata *etmdrvdata[NR_CPUS];
-static void etm4_set_default(struct etmv4_config *config);
+static void etm4_set_default_config(struct etmv4_config *config);
+static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
+                                 struct perf_event *event);
 
 static enum cpuhp_state hp_online;
 
@@ -79,22 +80,8 @@ static int etm4_cpu_id(struct coresight_device *csdev)
 static int etm4_trace_id(struct coresight_device *csdev)
 {
        struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
-       unsigned long flags;
-       int trace_id = -1;
-
-       if (!local_read(&drvdata->mode))
-               return drvdata->trcid;
-
-       spin_lock_irqsave(&drvdata->spinlock, flags);
-
-       CS_UNLOCK(drvdata->base);
-       trace_id = readl_relaxed(drvdata->base + TRCTRACEIDR);
-       trace_id &= ETM_TRACEID_MASK;
-       CS_LOCK(drvdata->base);
 
-       spin_unlock_irqrestore(&drvdata->spinlock, flags);
-
-       return trace_id;
+       return drvdata->trcid;
 }
 
 static void etm4_enable_hw(void *info)
@@ -113,8 +100,7 @@ static void etm4_enable_hw(void *info)
        /* wait for TRCSTATR.IDLE to go up */
        if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 1))
                dev_err(drvdata->dev,
-                       "timeout observed when probing at offset %#x\n",
-                       TRCSTATR);
+                       "timeout while waiting for Idle Trace Status\n");
 
        writel_relaxed(config->pe_sel, drvdata->base + TRCPROCSELR);
        writel_relaxed(config->cfg, drvdata->base + TRCCONFIGR);
@@ -180,14 +166,20 @@ static void etm4_enable_hw(void *info)
        writel_relaxed(config->vmid_mask0, drvdata->base + TRCVMIDCCTLR0);
        writel_relaxed(config->vmid_mask1, drvdata->base + TRCVMIDCCTLR1);
 
+       /*
+        * Request to keep the trace unit powered and also
+        * emulation of powerdown
+        */
+       writel_relaxed(readl_relaxed(drvdata->base + TRCPDCR) | TRCPDCR_PU,
+                      drvdata->base + TRCPDCR);
+
        /* Enable the trace unit */
        writel_relaxed(1, drvdata->base + TRCPRGCTLR);
 
        /* wait for TRCSTATR.IDLE to go back down to '0' */
        if (coresight_timeout(drvdata->base, TRCSTATR, TRCSTATR_IDLE_BIT, 0))
                dev_err(drvdata->dev,
-                       "timeout observed when probing at offset %#x\n",
-                       TRCSTATR);
+                       "timeout while waiting for Idle Trace Status\n");
 
        CS_LOCK(drvdata->base);
 
@@ -195,12 +187,16 @@ static void etm4_enable_hw(void *info)
 }
 
 static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
-                                  struct perf_event_attr *attr)
+                                  struct perf_event *event)
 {
+       int ret = 0;
        struct etmv4_config *config = &drvdata->config;
+       struct perf_event_attr *attr = &event->attr;
 
-       if (!attr)
-               return -EINVAL;
+       if (!attr) {
+               ret = -EINVAL;
+               goto out;
+       }
 
        /* Clear configuration from previous run */
        memset(config, 0, sizeof(struct etmv4_config));
@@ -212,14 +208,12 @@ static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
                config->mode = ETM_MODE_EXCL_USER;
 
        /* Always start from the default config */
-       etm4_set_default(config);
+       etm4_set_default_config(config);
 
-       /*
-        * By default the tracers are configured to trace the whole address
-        * range.  Narrow the field only if requested by user space.
-        */
-       if (config->mode)
-               etm4_config_trace_mode(config);
+       /* Configure filters specified on the perf cmd line, if any. */
+       ret = etm4_set_event_filters(drvdata, event);
+       if (ret)
+               goto out;
 
        /* Go from generic option to ETMv4 specifics */
        if (attr->config & BIT(ETM_OPT_CYCACC))
@@ -227,23 +221,30 @@ static int etm4_parse_event_config(struct etmv4_drvdata *drvdata,
        if (attr->config & BIT(ETM_OPT_TS))
                config->cfg |= ETMv4_MODE_TIMESTAMP;
 
-       return 0;
+out:
+       return ret;
 }
 
 static int etm4_enable_perf(struct coresight_device *csdev,
-                           struct perf_event_attr *attr)
+                           struct perf_event *event)
 {
+       int ret = 0;
        struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
 
-       if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
-               return -EINVAL;
+       if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id())) {
+               ret = -EINVAL;
+               goto out;
+       }
 
        /* Configure the tracer based on the session's specifics */
-       etm4_parse_event_config(drvdata, attr);
+       ret = etm4_parse_event_config(drvdata, event);
+       if (ret)
+               goto out;
        /* And enable it */
        etm4_enable_hw(drvdata);
 
-       return 0;
+out:
+       return ret;
 }
 
 static int etm4_enable_sysfs(struct coresight_device *csdev)
@@ -274,7 +275,7 @@ err:
 }
 
 static int etm4_enable(struct coresight_device *csdev,
-                      struct perf_event_attr *attr, u32 mode)
+                      struct perf_event *event, u32 mode)
 {
        int ret;
        u32 val;
@@ -291,7 +292,7 @@ static int etm4_enable(struct coresight_device *csdev,
                ret = etm4_enable_sysfs(csdev);
                break;
        case CS_MODE_PERF:
-               ret = etm4_enable_perf(csdev, attr);
+               ret = etm4_enable_perf(csdev, event);
                break;
        default:
                ret = -EINVAL;
@@ -311,6 +312,11 @@ static void etm4_disable_hw(void *info)
 
        CS_UNLOCK(drvdata->base);
 
+       /* power can be removed from the trace unit now */
+       control = readl_relaxed(drvdata->base + TRCPDCR);
+       control &= ~TRCPDCR_PU;
+       writel_relaxed(control, drvdata->base + TRCPDCR);
+
        control = readl_relaxed(drvdata->base + TRCPRGCTLR);
 
        /* EN, bit[0] Trace unit enable bit */
@@ -326,14 +332,28 @@ static void etm4_disable_hw(void *info)
        dev_dbg(drvdata->dev, "cpu: %d disable smp call done\n", drvdata->cpu);
 }
 
-static int etm4_disable_perf(struct coresight_device *csdev)
+static int etm4_disable_perf(struct coresight_device *csdev,
+                            struct perf_event *event)
 {
+       u32 control;
+       struct etm_filters *filters = event->hw.addr_filters;
        struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
 
        if (WARN_ON_ONCE(drvdata->cpu != smp_processor_id()))
                return -EINVAL;
 
        etm4_disable_hw(drvdata);
+
+       /*
+        * Check if the start/stop logic was active when the unit was stopped.
+        * That way we can re-enable the start/stop logic when the process is
+        * scheduled again.  Configuration of the start/stop logic happens in
+        * function etm4_set_event_filters().
+        */
+       control = readl_relaxed(drvdata->base + TRCVICTLR);
+       /* TRCVICTLR::SSSTATUS, bit[9] */
+       filters->ssstatus = (control & BIT(9));
+
        return 0;
 }
 
@@ -362,7 +382,8 @@ static void etm4_disable_sysfs(struct coresight_device *csdev)
        dev_info(drvdata->dev, "ETM tracing disabled\n");
 }
 
-static void etm4_disable(struct coresight_device *csdev)
+static void etm4_disable(struct coresight_device *csdev,
+                        struct perf_event *event)
 {
        u32 mode;
        struct etmv4_drvdata *drvdata = dev_get_drvdata(csdev->dev.parent);
@@ -381,7 +402,7 @@ static void etm4_disable(struct coresight_device *csdev)
                etm4_disable_sysfs(csdev);
                break;
        case CS_MODE_PERF:
-               etm4_disable_perf(csdev);
+               etm4_disable_perf(csdev, event);
                break;
        }
 
@@ -564,21 +585,8 @@ static void etm4_init_arch_data(void *info)
        CS_LOCK(drvdata->base);
 }
 
-static void etm4_set_default(struct etmv4_config *config)
+static void etm4_set_default_config(struct etmv4_config *config)
 {
-       if (WARN_ON_ONCE(!config))
-               return;
-
-       /*
-        * Make default initialisation trace everything
-        *
-        * Select the "always true" resource selector on the
-        * "Enablign Event" line and configure address range comparator
-        * '0' to trace all the possible address range.  From there
-        * configure the "include/exclude" engine to include address
-        * range comparator '0'.
-        */
-
        /* disable all events tracing */
        config->eventctrl0 = 0x0;
        config->eventctrl1 = 0x0;
@@ -594,6 +602,108 @@ static void etm4_set_default(struct etmv4_config *config)
 
        /* TRCVICTLR::EVENT = 0x01, select the always on logic */
        config->vinst_ctrl |= BIT(0);
+}
+
+static u64 etm4_get_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
+        *
+        * Always stay away from hypervisor mode.
+        */
+       access_type = ETM_EXLEVEL_NS_HYP;
+
+       if (config->mode & ETM_MODE_EXCL_KERN)
+               access_type |= ETM_EXLEVEL_NS_OS;
+
+       if (config->mode & ETM_MODE_EXCL_USER)
+               access_type |= ETM_EXLEVEL_NS_APP;
+
+       /*
+        * EXLEVEL_S, bits[11:8], don't trace anything happening
+        * in secure state.
+        */
+       access_type |= (ETM_EXLEVEL_S_APP       |
+                       ETM_EXLEVEL_S_OS        |
+                       ETM_EXLEVEL_S_HYP);
+
+       return access_type;
+}
+
+static void etm4_set_comparator_filter(struct etmv4_config *config,
+                                      u64 start, u64 stop, int comparator)
+{
+       u64 access_type = etm4_get_access_type(config);
+
+       /* First half of default address comparator */
+       config->addr_val[comparator] = start;
+       config->addr_acc[comparator] = access_type;
+       config->addr_type[comparator] = ETM_ADDR_TYPE_RANGE;
+
+       /* Second half of default address comparator */
+       config->addr_val[comparator + 1] = stop;
+       config->addr_acc[comparator + 1] = access_type;
+       config->addr_type[comparator + 1] = ETM_ADDR_TYPE_RANGE;
+
+       /*
+        * Configure the ViewInst function to include this address range
+        * comparator.
+        *
+        * @comparator is divided by two since it is the index in the
+        * etmv4_config::addr_val array but register TRCVIIECTLR deals with
+        * address range comparator _pairs_.
+        *
+        * Therefore:
+        *      index 0 -> compatator pair 0
+        *      index 2 -> comparator pair 1
+        *      index 4 -> comparator pair 2
+        *      ...
+        *      index 14 -> comparator pair 7
+        */
+       config->viiectlr |= BIT(comparator / 2);
+}
+
+static void etm4_set_start_stop_filter(struct etmv4_config *config,
+                                      u64 address, int comparator,
+                                      enum etm_addr_type type)
+{
+       int shift;
+       u64 access_type = etm4_get_access_type(config);
+
+       /* Configure the comparator */
+       config->addr_val[comparator] = address;
+       config->addr_acc[comparator] = access_type;
+       config->addr_type[comparator] = type;
+
+       /*
+        * Configure ViewInst Start-Stop control register.
+        * Addresses configured to start tracing go from bit 0 to n-1,
+        * while those configured to stop tracing from 16 to 16 + n-1.
+        */
+       shift = (type == ETM_ADDR_TYPE_START ? 0 : 16);
+       config->vissctlr |= BIT(shift + comparator);
+}
+
+static void etm4_set_default_filter(struct etmv4_config *config)
+{
+       u64 start, stop;
+
+       /*
+        * Configure address range comparator '0' to encompass all
+        * possible addresses.
+        */
+       start = 0x0;
+       stop = ~0x0;
+
+       etm4_set_comparator_filter(config, start, stop,
+                                  ETM_DEFAULT_ADDR_COMP);
 
        /*
         * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
@@ -601,43 +711,156 @@ static void etm4_set_default(struct etmv4_config *config)
         */
        config->vinst_ctrl |= BIT(9);
 
+       /* No start-stop filtering for ViewInst */
+       config->vissctlr = 0x0;
+}
+
+static void etm4_set_default(struct etmv4_config *config)
+{
+       if (WARN_ON_ONCE(!config))
+               return;
+
        /*
-        * Configure address range comparator '0' to encompass all
-        * possible addresses.
+        * Make default initialisation trace everything
+        *
+        * Select the "always true" resource selector on the
+        * "Enablign Event" line and configure address range comparator
+        * '0' to trace all the possible address range.  From there
+        * configure the "include/exclude" engine to include address
+        * range comparator '0'.
         */
+       etm4_set_default_config(config);
+       etm4_set_default_filter(config);
+}
 
-       /* First half of default address comparator: start at address 0 */
-       config->addr_val[ETM_DEFAULT_ADDR_COMP] = 0x0;
-       /* trace instruction addresses */
-       config->addr_acc[ETM_DEFAULT_ADDR_COMP] &= ~(BIT(0) | BIT(1));
-       /* EXLEVEL_NS, bits[12:15], only trace application and kernel space */
-       config->addr_acc[ETM_DEFAULT_ADDR_COMP] |= ETM_EXLEVEL_NS_HYP;
-       /* EXLEVEL_S, bits[11:8], don't trace anything in secure state */
-       config->addr_acc[ETM_DEFAULT_ADDR_COMP] |= (ETM_EXLEVEL_S_APP |
-                                                   ETM_EXLEVEL_S_OS |
-                                                   ETM_EXLEVEL_S_HYP);
-       config->addr_type[ETM_DEFAULT_ADDR_COMP] = ETM_ADDR_TYPE_RANGE;
+static int etm4_get_next_comparator(struct etmv4_drvdata *drvdata, u32 type)
+{
+       int nr_comparator, index = 0;
+       struct etmv4_config *config = &drvdata->config;
 
        /*
-        * Second half of default address comparator: go all
-        * the way to the top.
-       */
-       config->addr_val[ETM_DEFAULT_ADDR_COMP + 1] = ~0x0;
-       /* trace instruction addresses */
-       config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] &= ~(BIT(0) | BIT(1));
-       /* Address comparator type must be equal for both halves */
-       config->addr_acc[ETM_DEFAULT_ADDR_COMP + 1] =
-                                       config->addr_acc[ETM_DEFAULT_ADDR_COMP];
-       config->addr_type[ETM_DEFAULT_ADDR_COMP + 1] = ETM_ADDR_TYPE_RANGE;
+        * nr_addr_cmp holds the number of comparator _pair_, so time 2
+        * for the total number of comparators.
+        */
+       nr_comparator = drvdata->nr_addr_cmp * 2;
+
+       /* Go through the tally of comparators looking for a free one. */
+       while (index < nr_comparator) {
+               switch (type) {
+               case ETM_ADDR_TYPE_RANGE:
+                       if (config->addr_type[index] == ETM_ADDR_TYPE_NONE &&
+                           config->addr_type[index + 1] == ETM_ADDR_TYPE_NONE)
+                               return index;
+
+                       /* Address range comparators go in pairs */
+                       index += 2;
+                       break;
+               case ETM_ADDR_TYPE_START:
+               case ETM_ADDR_TYPE_STOP:
+                       if (config->addr_type[index] == ETM_ADDR_TYPE_NONE)
+                               return index;
+
+                       /* Start/stop address can have odd indexes */
+                       index += 1;
+                       break;
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       /* If we are here all the comparators have been used. */
+       return -ENOSPC;
+}
+
+static int etm4_set_event_filters(struct etmv4_drvdata *drvdata,
+                                 struct perf_event *event)
+{
+       int i, comparator, ret = 0;
+       u64 address;
+       struct etmv4_config *config = &drvdata->config;
+       struct etm_filters *filters = event->hw.addr_filters;
+
+       if (!filters)
+               goto default_filter;
+
+       /* Sync events with what Perf got */
+       perf_event_addr_filters_sync(event);
 
        /*
-        * Configure the ViewInst function to filter on address range
-        * comparator '0'.
+        * If there are no filters to deal with simply go ahead with
+        * the default filter, i.e the entire address range.
         */
-       config->viiectlr = BIT(0);
+       if (!filters->nr_filters)
+               goto default_filter;
+
+       for (i = 0; i < filters->nr_filters; i++) {
+               struct etm_filter *filter = &filters->etm_filter[i];
+               enum etm_addr_type type = filter->type;
+
+               /* See if a comparator is free. */
+               comparator = etm4_get_next_comparator(drvdata, type);
+               if (comparator < 0) {
+                       ret = comparator;
+                       goto out;
+               }
+
+               switch (type) {
+               case ETM_ADDR_TYPE_RANGE:
+                       etm4_set_comparator_filter(config,
+                                                  filter->start_addr,
+                                                  filter->stop_addr,
+                                                  comparator);
+                       /*
+                        * TRCVICTLR::SSSTATUS == 1, the start-stop logic is
+                        * in the started state
+                        */
+                       config->vinst_ctrl |= BIT(9);
+
+                       /* No start-stop filtering for ViewInst */
+                       config->vissctlr = 0x0;
+                       break;
+               case ETM_ADDR_TYPE_START:
+               case ETM_ADDR_TYPE_STOP:
+                       /* Get the right start or stop address */
+                       address = (type == ETM_ADDR_TYPE_START ?
+                                  filter->start_addr :
+                                  filter->stop_addr);
+
+                       /* Configure comparator */
+                       etm4_set_start_stop_filter(config, address,
+                                                  comparator, type);
+
+                       /*
+                        * If filters::ssstatus == 1, trace acquisition was
+                        * started but the process was yanked away before the
+                        * the stop address was hit.  As such the start/stop
+                        * logic needs to be re-started so that tracing can
+                        * resume where it left.
+                        *
+                        * The start/stop logic status when a process is
+                        * scheduled out is checked in function
+                        * etm4_disable_perf().
+                        */
+                       if (filters->ssstatus)
+                               config->vinst_ctrl |= BIT(9);
+
+                       /* No include/exclude filtering for ViewInst */
+                       config->viiectlr = 0x0;
+                       break;
+               default:
+                       ret = -EINVAL;
+                       goto out;
+               }
+       }
 
-       /* no start-stop filtering for ViewInst */
-       config->vissctlr = 0x0;
+       goto out;
+
+
+default_filter:
+       etm4_set_default_filter(config);
+
+out:
+       return ret;
 }
 
 void etm4_config_trace_mode(struct etmv4_config *config)
@@ -727,13 +950,9 @@ static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
        struct coresight_platform_data *pdata = NULL;
        struct etmv4_drvdata *drvdata;
        struct resource *res = &adev->res;
-       struct coresight_desc *desc;
+       struct coresight_desc desc = { 0 };
        struct device_node *np = adev->dev.of_node;
 
-       desc = devm_kzalloc(dev, sizeof(*desc), GFP_KERNEL);
-       if (!desc)
-               return -ENOMEM;
-
        drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
        if (!drvdata)
                return -ENOMEM;
@@ -788,13 +1007,13 @@ static int etm4_probe(struct amba_device *adev, const struct amba_id *id)
        etm4_init_trace_id(drvdata);
        etm4_set_default(&drvdata->config);
 
-       desc->type = CORESIGHT_DEV_TYPE_SOURCE;
-       desc->subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
-       desc->ops = &etm4_cs_ops;
-       desc->pdata = pdata;
-       desc->dev = dev;
-       desc->groups = coresight_etmv4_groups;
-       drvdata->csdev = coresight_register(desc);
+       desc.type = CORESIGHT_DEV_TYPE_SOURCE;
+       desc.subtype.source_subtype = CORESIGHT_DEV_SUBTYPE_SOURCE_PROC;
+       desc.ops = &etm4_cs_ops;
+       desc.pdata = pdata;
+       desc.dev = dev;
+       desc.groups = coresight_etmv4_groups;
+       drvdata->csdev = coresight_register(&desc);
        if (IS_ERR(drvdata->csdev)) {
                ret = PTR_ERR(drvdata->csdev);
                goto err_arch_supported;
@@ -826,12 +1045,12 @@ err_arch_supported:
 }
 
 static struct amba_id etm4_ids[] = {
-       {       /* ETM 4.0 - Qualcomm */
-               .id     = 0x0003b95d,
-               .mask   = 0x0003ffff,
+       {       /* ETM 4.0 - Cortex-A53  */
+               .id     = 0x000bb95d,
+               .mask   = 0x000fffff,
                .data   = "ETM 4.0",
        },
-       {       /* ETM 4.0 - Juno board */
+       {       /* ETM 4.0 - Cortex-A57 */
                .id     = 0x000bb95e,
                .mask   = 0x000fffff,
                .data   = "ETM 4.0",
This page took 0.032705 seconds and 5 git commands to generate.