linux/drivers/hwtracing/coresight/coresight-cti.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2018 Linaro Limited, All rights reserved.
   4 * Author: Mike Leach <mike.leach@linaro.org>
   5 */
   6
   7#include <linux/amba/bus.h>
   8#include <linux/atomic.h>
   9#include <linux/bits.h>
  10#include <linux/coresight.h>
  11#include <linux/cpu_pm.h>
  12#include <linux/cpuhotplug.h>
  13#include <linux/device.h>
  14#include <linux/io.h>
  15#include <linux/kernel.h>
  16#include <linux/list.h>
  17#include <linux/mutex.h>
  18#include <linux/pm_runtime.h>
  19#include <linux/property.h>
  20#include <linux/spinlock.h>
  21
  22#include "coresight-priv.h"
  23#include "coresight-cti.h"
  24
  25/**
  26 * CTI devices can be associated with a PE, or be connected to CoreSight
  27 * hardware. We have a list of all CTIs irrespective of CPU bound or
  28 * otherwise.
  29 *
  30 * We assume that the non-CPU CTIs are always powered as we do with sinks etc.
  31 *
  32 * We leave the client to figure out if all the CTIs are interconnected with
  33 * the same CTM, in general this is the case but does not always have to be.
  34 */
  35
  36/* net of CTI devices connected via CTM */
  37static LIST_HEAD(ect_net);
  38
  39/* protect the list */
  40static DEFINE_MUTEX(ect_mutex);
  41
  42#define csdev_to_cti_drvdata(csdev)     \
  43        dev_get_drvdata(csdev->dev.parent)
  44
  45/* power management handling */
  46static int nr_cti_cpu;
  47
  48/* quick lookup list for CPU bound CTIs when power handling */
  49static struct cti_drvdata *cti_cpu_drvdata[NR_CPUS];
  50
  51/*
  52 * CTI naming. CTI bound to cores will have the name cti_cpu<N> where
  53 * N is the CPU ID. System CTIs will have the name cti_sys<I> where I
  54 * is an index allocated by order of discovery.
  55 *
  56 * CTI device name list - for CTI not bound to cores.
  57 */
  58DEFINE_CORESIGHT_DEVLIST(cti_sys_devs, "cti_sys");
  59
  60/* write set of regs to hardware - call with spinlock claimed */
  61void cti_write_all_hw_regs(struct cti_drvdata *drvdata)
  62{
  63        struct cti_config *config = &drvdata->config;
  64        int i;
  65
  66        CS_UNLOCK(drvdata->base);
  67
  68        /* disable CTI before writing registers */
  69        writel_relaxed(0, drvdata->base + CTICONTROL);
  70
  71        /* write the CTI trigger registers */
  72        for (i = 0; i < config->nr_trig_max; i++) {
  73                writel_relaxed(config->ctiinen[i], drvdata->base + CTIINEN(i));
  74                writel_relaxed(config->ctiouten[i],
  75                               drvdata->base + CTIOUTEN(i));
  76        }
  77
  78        /* other regs */
  79        writel_relaxed(config->ctigate, drvdata->base + CTIGATE);
  80        writel_relaxed(config->asicctl, drvdata->base + ASICCTL);
  81        writel_relaxed(config->ctiappset, drvdata->base + CTIAPPSET);
  82
  83        /* re-enable CTI */
  84        writel_relaxed(1, drvdata->base + CTICONTROL);
  85
  86        CS_LOCK(drvdata->base);
  87}
  88
  89static void cti_enable_hw_smp_call(void *info)
  90{
  91        struct cti_drvdata *drvdata = info;
  92
  93        cti_write_all_hw_regs(drvdata);
  94}
  95
  96/* write regs to hardware and enable */
  97static int cti_enable_hw(struct cti_drvdata *drvdata)
  98{
  99        struct cti_config *config = &drvdata->config;
 100        struct device *dev = &drvdata->csdev->dev;
 101        int rc = 0;
 102
 103        pm_runtime_get_sync(dev->parent);
 104        spin_lock(&drvdata->spinlock);
 105
 106        /* no need to do anything if enabled or unpowered*/
 107        if (config->hw_enabled || !config->hw_powered)
 108                goto cti_state_unchanged;
 109
 110        /* claim the device */
 111        rc = coresight_claim_device(drvdata->base);
 112        if (rc)
 113                goto cti_err_not_enabled;
 114
 115        if (drvdata->ctidev.cpu >= 0) {
 116                rc = smp_call_function_single(drvdata->ctidev.cpu,
 117                                              cti_enable_hw_smp_call,
 118                                              drvdata, 1);
 119                if (rc)
 120                        goto cti_err_not_enabled;
 121        } else {
 122                cti_write_all_hw_regs(drvdata);
 123        }
 124
 125        config->hw_enabled = true;
 126        atomic_inc(&drvdata->config.enable_req_count);
 127        spin_unlock(&drvdata->spinlock);
 128        return rc;
 129
 130cti_state_unchanged:
 131        atomic_inc(&drvdata->config.enable_req_count);
 132
 133        /* cannot enable due to error */
 134cti_err_not_enabled:
 135        spin_unlock(&drvdata->spinlock);
 136        pm_runtime_put(dev->parent);
 137        return rc;
 138}
 139
 140/* re-enable CTI on CPU when using CPU hotplug */
 141static void cti_cpuhp_enable_hw(struct cti_drvdata *drvdata)
 142{
 143        struct cti_config *config = &drvdata->config;
 144        struct device *dev = &drvdata->csdev->dev;
 145
 146        pm_runtime_get_sync(dev->parent);
 147        spin_lock(&drvdata->spinlock);
 148        config->hw_powered = true;
 149
 150        /* no need to do anything if no enable request */
 151        if (!atomic_read(&drvdata->config.enable_req_count))
 152                goto cti_hp_not_enabled;
 153
 154        /* try to claim the device */
 155        if (coresight_claim_device(drvdata->base))
 156                goto cti_hp_not_enabled;
 157
 158        cti_write_all_hw_regs(drvdata);
 159        config->hw_enabled = true;
 160        spin_unlock(&drvdata->spinlock);
 161        return;
 162
 163        /* did not re-enable due to no claim / no request */
 164cti_hp_not_enabled:
 165        spin_unlock(&drvdata->spinlock);
 166        pm_runtime_put(dev->parent);
 167}
 168
 169/* disable hardware */
 170static int cti_disable_hw(struct cti_drvdata *drvdata)
 171{
 172        struct cti_config *config = &drvdata->config;
 173        struct device *dev = &drvdata->csdev->dev;
 174
 175        spin_lock(&drvdata->spinlock);
 176
 177        /* check refcount - disable on 0 */
 178        if (atomic_dec_return(&drvdata->config.enable_req_count) > 0)
 179                goto cti_not_disabled;
 180
 181        /* no need to do anything if disabled or cpu unpowered */
 182        if (!config->hw_enabled || !config->hw_powered)
 183                goto cti_not_disabled;
 184
 185        CS_UNLOCK(drvdata->base);
 186
 187        /* disable CTI */
 188        writel_relaxed(0, drvdata->base + CTICONTROL);
 189        config->hw_enabled = false;
 190
 191        coresight_disclaim_device_unlocked(drvdata->base);
 192        CS_LOCK(drvdata->base);
 193        spin_unlock(&drvdata->spinlock);
 194        pm_runtime_put(dev);
 195        return 0;
 196
 197        /* not disabled this call */
 198cti_not_disabled:
 199        spin_unlock(&drvdata->spinlock);
 200        return 0;
 201}
 202
 203void cti_write_single_reg(struct cti_drvdata *drvdata, int offset, u32 value)
 204{
 205        CS_UNLOCK(drvdata->base);
 206        writel_relaxed(value, drvdata->base + offset);
 207        CS_LOCK(drvdata->base);
 208}
 209
 210void cti_write_intack(struct device *dev, u32 ackval)
 211{
 212        struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
 213        struct cti_config *config = &drvdata->config;
 214
 215        spin_lock(&drvdata->spinlock);
 216        /* write if enabled */
 217        if (cti_active(config))
 218                cti_write_single_reg(drvdata, CTIINTACK, ackval);
 219        spin_unlock(&drvdata->spinlock);
 220}
 221
 222/*
 223 * Look at the HW DEVID register for some of the HW settings.
 224 * DEVID[15:8] - max number of in / out triggers.
 225 */
 226#define CTI_DEVID_MAXTRIGS(devid_val) ((int) BMVAL(devid_val, 8, 15))
 227
 228/* DEVID[19:16] - number of CTM channels */
 229#define CTI_DEVID_CTMCHANNELS(devid_val) ((int) BMVAL(devid_val, 16, 19))
 230
 231static void cti_set_default_config(struct device *dev,
 232                                   struct cti_drvdata *drvdata)
 233{
 234        struct cti_config *config = &drvdata->config;
 235        u32 devid;
 236
 237        devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
 238        config->nr_trig_max = CTI_DEVID_MAXTRIGS(devid);
 239
 240        /*
 241         * no current hardware should exceed this, but protect the driver
 242         * in case of fault / out of spec hw
 243         */
 244        if (config->nr_trig_max > CTIINOUTEN_MAX) {
 245                dev_warn_once(dev,
 246                        "Limiting HW MaxTrig value(%d) to driver max(%d)\n",
 247                        config->nr_trig_max, CTIINOUTEN_MAX);
 248                config->nr_trig_max = CTIINOUTEN_MAX;
 249        }
 250
 251        config->nr_ctm_channels = CTI_DEVID_CTMCHANNELS(devid);
 252
 253        /* Most regs default to 0 as zalloc'ed except...*/
 254        config->trig_filter_enable = true;
 255        config->ctigate = GENMASK(config->nr_ctm_channels - 1, 0);
 256        atomic_set(&config->enable_req_count, 0);
 257}
 258
 259/*
 260 * Add a connection entry to the list of connections for this
 261 * CTI device.
 262 */
 263int cti_add_connection_entry(struct device *dev, struct cti_drvdata *drvdata,
 264                             struct cti_trig_con *tc,
 265                             struct coresight_device *csdev,
 266                             const char *assoc_dev_name)
 267{
 268        struct cti_device *cti_dev = &drvdata->ctidev;
 269
 270        tc->con_dev = csdev;
 271        /*
 272         * Prefer actual associated CS device dev name to supplied value -
 273         * which is likely to be node name / other conn name.
 274         */
 275        if (csdev)
 276                tc->con_dev_name = dev_name(&csdev->dev);
 277        else if (assoc_dev_name != NULL) {
 278                tc->con_dev_name = devm_kstrdup(dev,
 279                                                assoc_dev_name, GFP_KERNEL);
 280                if (!tc->con_dev_name)
 281                        return -ENOMEM;
 282        }
 283        list_add_tail(&tc->node, &cti_dev->trig_cons);
 284        cti_dev->nr_trig_con++;
 285
 286        /* add connection usage bit info to overall info */
 287        drvdata->config.trig_in_use |= tc->con_in->used_mask;
 288        drvdata->config.trig_out_use |= tc->con_out->used_mask;
 289
 290        return 0;
 291}
 292
 293/* create a trigger connection with appropriately sized signal groups */
 294struct cti_trig_con *cti_allocate_trig_con(struct device *dev, int in_sigs,
 295                                           int out_sigs)
 296{
 297        struct cti_trig_con *tc = NULL;
 298        struct cti_trig_grp *in = NULL, *out = NULL;
 299
 300        tc = devm_kzalloc(dev, sizeof(struct cti_trig_con), GFP_KERNEL);
 301        if (!tc)
 302                return tc;
 303
 304        in = devm_kzalloc(dev,
 305                          offsetof(struct cti_trig_grp, sig_types[in_sigs]),
 306                          GFP_KERNEL);
 307        if (!in)
 308                return NULL;
 309
 310        out = devm_kzalloc(dev,
 311                           offsetof(struct cti_trig_grp, sig_types[out_sigs]),
 312                           GFP_KERNEL);
 313        if (!out)
 314                return NULL;
 315
 316        tc->con_in = in;
 317        tc->con_out = out;
 318        tc->con_in->nr_sigs = in_sigs;
 319        tc->con_out->nr_sigs = out_sigs;
 320        return tc;
 321}
 322
 323/*
 324 * Add a default connection if nothing else is specified.
 325 * single connection based on max in/out info, no assoc device
 326 */
 327int cti_add_default_connection(struct device *dev, struct cti_drvdata *drvdata)
 328{
 329        int ret = 0;
 330        int n_trigs = drvdata->config.nr_trig_max;
 331        u32 n_trig_mask = GENMASK(n_trigs - 1, 0);
 332        struct cti_trig_con *tc = NULL;
 333
 334        /*
 335         * Assume max trigs for in and out,
 336         * all used, default sig types allocated
 337         */
 338        tc = cti_allocate_trig_con(dev, n_trigs, n_trigs);
 339        if (!tc)
 340                return -ENOMEM;
 341
 342        tc->con_in->used_mask = n_trig_mask;
 343        tc->con_out->used_mask = n_trig_mask;
 344        ret = cti_add_connection_entry(dev, drvdata, tc, NULL, "default");
 345        return ret;
 346}
 347
 348/** cti channel api **/
 349/* attach/detach channel from trigger - write through if enabled. */
 350int cti_channel_trig_op(struct device *dev, enum cti_chan_op op,
 351                        enum cti_trig_dir direction, u32 channel_idx,
 352                        u32 trigger_idx)
 353{
 354        struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
 355        struct cti_config *config = &drvdata->config;
 356        u32 trig_bitmask;
 357        u32 chan_bitmask;
 358        u32 reg_value;
 359        int reg_offset;
 360
 361        /* ensure indexes in range */
 362        if ((channel_idx >= config->nr_ctm_channels) ||
 363           (trigger_idx >= config->nr_trig_max))
 364                return -EINVAL;
 365
 366        trig_bitmask = BIT(trigger_idx);
 367
 368        /* ensure registered triggers and not out filtered */
 369        if (direction == CTI_TRIG_IN)   {
 370                if (!(trig_bitmask & config->trig_in_use))
 371                        return -EINVAL;
 372        } else {
 373                if (!(trig_bitmask & config->trig_out_use))
 374                        return -EINVAL;
 375
 376                if ((config->trig_filter_enable) &&
 377                    (config->trig_out_filter & trig_bitmask))
 378                        return -EINVAL;
 379        }
 380
 381        /* update the local register values */
 382        chan_bitmask = BIT(channel_idx);
 383        reg_offset = (direction == CTI_TRIG_IN ? CTIINEN(trigger_idx) :
 384                      CTIOUTEN(trigger_idx));
 385
 386        spin_lock(&drvdata->spinlock);
 387
 388        /* read - modify write - the trigger / channel enable value */
 389        reg_value = direction == CTI_TRIG_IN ? config->ctiinen[trigger_idx] :
 390                     config->ctiouten[trigger_idx];
 391        if (op == CTI_CHAN_ATTACH)
 392                reg_value |= chan_bitmask;
 393        else
 394                reg_value &= ~chan_bitmask;
 395
 396        /* write local copy */
 397        if (direction == CTI_TRIG_IN)
 398                config->ctiinen[trigger_idx] = reg_value;
 399        else
 400                config->ctiouten[trigger_idx] = reg_value;
 401
 402        /* write through if enabled */
 403        if (cti_active(config))
 404                cti_write_single_reg(drvdata, reg_offset, reg_value);
 405        spin_unlock(&drvdata->spinlock);
 406        return 0;
 407}
 408
 409int cti_channel_gate_op(struct device *dev, enum cti_chan_gate_op op,
 410                        u32 channel_idx)
 411{
 412        struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
 413        struct cti_config *config = &drvdata->config;
 414        u32 chan_bitmask;
 415        u32 reg_value;
 416        int err = 0;
 417
 418        if (channel_idx >= config->nr_ctm_channels)
 419                return -EINVAL;
 420
 421        chan_bitmask = BIT(channel_idx);
 422
 423        spin_lock(&drvdata->spinlock);
 424        reg_value = config->ctigate;
 425        switch (op) {
 426        case CTI_GATE_CHAN_ENABLE:
 427                reg_value |= chan_bitmask;
 428                break;
 429
 430        case CTI_GATE_CHAN_DISABLE:
 431                reg_value &= ~chan_bitmask;
 432                break;
 433
 434        default:
 435                err = -EINVAL;
 436                break;
 437        }
 438        if (err == 0) {
 439                config->ctigate = reg_value;
 440                if (cti_active(config))
 441                        cti_write_single_reg(drvdata, CTIGATE, reg_value);
 442        }
 443        spin_unlock(&drvdata->spinlock);
 444        return err;
 445}
 446
 447int cti_channel_setop(struct device *dev, enum cti_chan_set_op op,
 448                      u32 channel_idx)
 449{
 450        struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
 451        struct cti_config *config = &drvdata->config;
 452        u32 chan_bitmask;
 453        u32 reg_value;
 454        u32 reg_offset;
 455        int err = 0;
 456
 457        if (channel_idx >= config->nr_ctm_channels)
 458                return -EINVAL;
 459
 460        chan_bitmask = BIT(channel_idx);
 461
 462        spin_lock(&drvdata->spinlock);
 463        reg_value = config->ctiappset;
 464        switch (op) {
 465        case CTI_CHAN_SET:
 466                config->ctiappset |= chan_bitmask;
 467                reg_value  = config->ctiappset;
 468                reg_offset = CTIAPPSET;
 469                break;
 470
 471        case CTI_CHAN_CLR:
 472                config->ctiappset &= ~chan_bitmask;
 473                reg_value = chan_bitmask;
 474                reg_offset = CTIAPPCLEAR;
 475                break;
 476
 477        case CTI_CHAN_PULSE:
 478                config->ctiappset &= ~chan_bitmask;
 479                reg_value = chan_bitmask;
 480                reg_offset = CTIAPPPULSE;
 481                break;
 482
 483        default:
 484                err = -EINVAL;
 485                break;
 486        }
 487
 488        if ((err == 0) && cti_active(config))
 489                cti_write_single_reg(drvdata, reg_offset, reg_value);
 490        spin_unlock(&drvdata->spinlock);
 491
 492        return err;
 493}
 494
 495static bool cti_add_sysfs_link(struct cti_drvdata *drvdata,
 496                               struct cti_trig_con *tc)
 497{
 498        struct coresight_sysfs_link link_info;
 499        int link_err = 0;
 500
 501        link_info.orig = drvdata->csdev;
 502        link_info.orig_name = tc->con_dev_name;
 503        link_info.target = tc->con_dev;
 504        link_info.target_name = dev_name(&drvdata->csdev->dev);
 505
 506        link_err = coresight_add_sysfs_link(&link_info);
 507        if (link_err)
 508                dev_warn(&drvdata->csdev->dev,
 509                         "Failed to set CTI sysfs link %s<=>%s\n",
 510                         link_info.orig_name, link_info.target_name);
 511        return !link_err;
 512}
 513
 514static void cti_remove_sysfs_link(struct cti_trig_con *tc)
 515{
 516        struct coresight_sysfs_link link_info;
 517
 518        link_info.orig_name = tc->con_dev_name;
 519        link_info.target = tc->con_dev;
 520        coresight_remove_sysfs_link(&link_info);
 521}
 522
 523/*
 524 * Look for a matching connection device name in the list of connections.
 525 * If found then swap in the csdev name, set trig con association pointer
 526 * and return found.
 527 */
 528static bool
 529cti_match_fixup_csdev(struct cti_device *ctidev, const char *node_name,
 530                      struct coresight_device *csdev)
 531{
 532        struct cti_trig_con *tc;
 533        struct cti_drvdata *drvdata = container_of(ctidev, struct cti_drvdata,
 534                                                   ctidev);
 535
 536        list_for_each_entry(tc, &ctidev->trig_cons, node) {
 537                if (tc->con_dev_name) {
 538                        if (!strcmp(node_name, tc->con_dev_name)) {
 539                                /* match: so swap in csdev name & dev */
 540                                tc->con_dev_name = dev_name(&csdev->dev);
 541                                tc->con_dev = csdev;
 542                                /* try to set sysfs link */
 543                                if (cti_add_sysfs_link(drvdata, tc))
 544                                        return true;
 545                                /* link failed - remove CTI reference */
 546                                tc->con_dev = NULL;
 547                                break;
 548                        }
 549                }
 550        }
 551        return false;
 552}
 553
 554/*
 555 * Search the cti list to add an associated CTI into the supplied CS device
 556 * This will set the association if CTI declared before the CS device.
 557 * (called from coresight_register() with coresight_mutex locked).
 558 */
 559void cti_add_assoc_to_csdev(struct coresight_device *csdev)
 560{
 561        struct cti_drvdata *ect_item;
 562        struct cti_device *ctidev;
 563        const char *node_name = NULL;
 564
 565        /* protect the list */
 566        mutex_lock(&ect_mutex);
 567
 568        /* exit if current is an ECT device.*/
 569        if ((csdev->type == CORESIGHT_DEV_TYPE_ECT) || list_empty(&ect_net))
 570                goto cti_add_done;
 571
 572        /* if we didn't find the csdev previously we used the fwnode name */
 573        node_name = cti_plat_get_node_name(dev_fwnode(csdev->dev.parent));
 574        if (!node_name)
 575                goto cti_add_done;
 576
 577        /* for each CTI in list... */
 578        list_for_each_entry(ect_item, &ect_net, node) {
 579                ctidev = &ect_item->ctidev;
 580                if (cti_match_fixup_csdev(ctidev, node_name, csdev)) {
 581                        /*
 582                         * if we found a matching csdev then update the ECT
 583                         * association pointer for the device with this CTI.
 584                         */
 585                        csdev->ect_dev = ect_item->csdev;
 586                        break;
 587                }
 588        }
 589cti_add_done:
 590        mutex_unlock(&ect_mutex);
 591}
 592EXPORT_SYMBOL_GPL(cti_add_assoc_to_csdev);
 593
 594/*
 595 * Removing the associated devices is easier.
 596 * A CTI will not have a value for csdev->ect_dev.
 597 */
 598void cti_remove_assoc_from_csdev(struct coresight_device *csdev)
 599{
 600        struct cti_drvdata *ctidrv;
 601        struct cti_trig_con *tc;
 602        struct cti_device *ctidev;
 603
 604        mutex_lock(&ect_mutex);
 605        if (csdev->ect_dev) {
 606                ctidrv = csdev_to_cti_drvdata(csdev->ect_dev);
 607                ctidev = &ctidrv->ctidev;
 608                list_for_each_entry(tc, &ctidev->trig_cons, node) {
 609                        if (tc->con_dev == csdev->ect_dev) {
 610                                cti_remove_sysfs_link(tc);
 611                                tc->con_dev = NULL;
 612                                break;
 613                        }
 614                }
 615                csdev->ect_dev = NULL;
 616        }
 617        mutex_unlock(&ect_mutex);
 618}
 619EXPORT_SYMBOL_GPL(cti_remove_assoc_from_csdev);
 620
 621/*
 622 * Update the cross references where the associated device was found
 623 * while we were building the connection info. This will occur if the
 624 * assoc device was registered before the CTI.
 625 */
 626static void cti_update_conn_xrefs(struct cti_drvdata *drvdata)
 627{
 628        struct cti_trig_con *tc;
 629        struct cti_device *ctidev = &drvdata->ctidev;
 630
 631        list_for_each_entry(tc, &ctidev->trig_cons, node) {
 632                if (tc->con_dev) {
 633                        /* if we can set the sysfs link */
 634                        if (cti_add_sysfs_link(drvdata, tc))
 635                                /* set the CTI/csdev association */
 636                                coresight_set_assoc_ectdev_mutex(tc->con_dev,
 637                                                         drvdata->csdev);
 638                        else
 639                                /* otherwise remove reference from CTI */
 640                                tc->con_dev = NULL;
 641                }
 642        }
 643}
 644
 645static void cti_remove_conn_xrefs(struct cti_drvdata *drvdata)
 646{
 647        struct cti_trig_con *tc;
 648        struct cti_device *ctidev = &drvdata->ctidev;
 649
 650        list_for_each_entry(tc, &ctidev->trig_cons, node) {
 651                if (tc->con_dev) {
 652                        coresight_set_assoc_ectdev_mutex(tc->con_dev,
 653                                                         NULL);
 654                        cti_remove_sysfs_link(tc);
 655                        tc->con_dev = NULL;
 656                }
 657        }
 658}
 659
 660/** cti PM callbacks **/
 661static int cti_cpu_pm_notify(struct notifier_block *nb, unsigned long cmd,
 662                             void *v)
 663{
 664        struct cti_drvdata *drvdata;
 665        unsigned int cpu = smp_processor_id();
 666        int notify_res = NOTIFY_OK;
 667
 668        if (!cti_cpu_drvdata[cpu])
 669                return NOTIFY_OK;
 670
 671        drvdata = cti_cpu_drvdata[cpu];
 672
 673        if (WARN_ON_ONCE(drvdata->ctidev.cpu != cpu))
 674                return NOTIFY_BAD;
 675
 676        spin_lock(&drvdata->spinlock);
 677
 678        switch (cmd) {
 679        case CPU_PM_ENTER:
 680                /* CTI regs all static - we have a copy & nothing to save */
 681                drvdata->config.hw_powered = false;
 682                if (drvdata->config.hw_enabled)
 683                        coresight_disclaim_device(drvdata->base);
 684                break;
 685
 686        case CPU_PM_ENTER_FAILED:
 687                drvdata->config.hw_powered = true;
 688                if (drvdata->config.hw_enabled) {
 689                        if (coresight_claim_device(drvdata->base))
 690                                drvdata->config.hw_enabled = false;
 691                }
 692                break;
 693
 694        case CPU_PM_EXIT:
 695                /* write hardware registers to re-enable. */
 696                drvdata->config.hw_powered = true;
 697                drvdata->config.hw_enabled = false;
 698
 699                /* check enable reference count to enable HW */
 700                if (atomic_read(&drvdata->config.enable_req_count)) {
 701                        /* check we can claim the device as we re-power */
 702                        if (coresight_claim_device(drvdata->base))
 703                                goto cti_notify_exit;
 704
 705                        drvdata->config.hw_enabled = true;
 706                        cti_write_all_hw_regs(drvdata);
 707                }
 708                break;
 709
 710        default:
 711                notify_res = NOTIFY_DONE;
 712                break;
 713        }
 714
 715cti_notify_exit:
 716        spin_unlock(&drvdata->spinlock);
 717        return notify_res;
 718}
 719
 720static struct notifier_block cti_cpu_pm_nb = {
 721        .notifier_call = cti_cpu_pm_notify,
 722};
 723
 724/* CPU HP handlers */
 725static int cti_starting_cpu(unsigned int cpu)
 726{
 727        struct cti_drvdata *drvdata = cti_cpu_drvdata[cpu];
 728
 729        if (!drvdata)
 730                return 0;
 731
 732        cti_cpuhp_enable_hw(drvdata);
 733        return 0;
 734}
 735
 736static int cti_dying_cpu(unsigned int cpu)
 737{
 738        struct cti_drvdata *drvdata = cti_cpu_drvdata[cpu];
 739
 740        if (!drvdata)
 741                return 0;
 742
 743        spin_lock(&drvdata->spinlock);
 744        drvdata->config.hw_powered = false;
 745        coresight_disclaim_device(drvdata->base);
 746        spin_unlock(&drvdata->spinlock);
 747        return 0;
 748}
 749
 750static int cti_pm_setup(struct cti_drvdata *drvdata)
 751{
 752        int ret;
 753
 754        if (drvdata->ctidev.cpu == -1)
 755                return 0;
 756
 757        if (nr_cti_cpu)
 758                goto done;
 759
 760        cpus_read_lock();
 761        ret = cpuhp_setup_state_nocalls_cpuslocked(
 762                        CPUHP_AP_ARM_CORESIGHT_CTI_STARTING,
 763                        "arm/coresight_cti:starting",
 764                        cti_starting_cpu, cti_dying_cpu);
 765        if (ret) {
 766                cpus_read_unlock();
 767                return ret;
 768        }
 769
 770        ret = cpu_pm_register_notifier(&cti_cpu_pm_nb);
 771        cpus_read_unlock();
 772        if (ret) {
 773                cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_CTI_STARTING);
 774                return ret;
 775        }
 776
 777done:
 778        nr_cti_cpu++;
 779        cti_cpu_drvdata[drvdata->ctidev.cpu] = drvdata;
 780
 781        return 0;
 782}
 783
 784/* release PM registrations */
 785static void cti_pm_release(struct cti_drvdata *drvdata)
 786{
 787        if (drvdata->ctidev.cpu == -1)
 788                return;
 789
 790        cti_cpu_drvdata[drvdata->ctidev.cpu] = NULL;
 791        if (--nr_cti_cpu == 0) {
 792                cpu_pm_unregister_notifier(&cti_cpu_pm_nb);
 793                cpuhp_remove_state_nocalls(CPUHP_AP_ARM_CORESIGHT_CTI_STARTING);
 794        }
 795}
 796
 797/** cti ect operations **/
 798int cti_enable(struct coresight_device *csdev)
 799{
 800        struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
 801
 802        return cti_enable_hw(drvdata);
 803}
 804
 805int cti_disable(struct coresight_device *csdev)
 806{
 807        struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
 808
 809        return cti_disable_hw(drvdata);
 810}
 811
 812static const struct coresight_ops_ect cti_ops_ect = {
 813        .enable = cti_enable,
 814        .disable = cti_disable,
 815};
 816
 817static const struct coresight_ops cti_ops = {
 818        .ect_ops = &cti_ops_ect,
 819};
 820
 821/*
 822 * Free up CTI specific resources
 823 * called by dev->release, need to call down to underlying csdev release.
 824 */
 825static void cti_device_release(struct device *dev)
 826{
 827        struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
 828        struct cti_drvdata *ect_item, *ect_tmp;
 829
 830        mutex_lock(&ect_mutex);
 831        cti_remove_conn_xrefs(drvdata);
 832        cti_pm_release(drvdata);
 833
 834        /* remove from the list */
 835        list_for_each_entry_safe(ect_item, ect_tmp, &ect_net, node) {
 836                if (ect_item == drvdata) {
 837                        list_del(&ect_item->node);
 838                        break;
 839                }
 840        }
 841        mutex_unlock(&ect_mutex);
 842
 843        if (drvdata->csdev_release)
 844                drvdata->csdev_release(dev);
 845}
 846
 847static int cti_probe(struct amba_device *adev, const struct amba_id *id)
 848{
 849        int ret = 0;
 850        void __iomem *base;
 851        struct device *dev = &adev->dev;
 852        struct cti_drvdata *drvdata = NULL;
 853        struct coresight_desc cti_desc;
 854        struct coresight_platform_data *pdata = NULL;
 855        struct resource *res = &adev->res;
 856
 857        /* driver data*/
 858        drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
 859        if (!drvdata)
 860                return -ENOMEM;
 861
 862        /* Validity for the resource is already checked by the AMBA core */
 863        base = devm_ioremap_resource(dev, res);
 864        if (IS_ERR(base))
 865                return PTR_ERR(base);
 866
 867        drvdata->base = base;
 868
 869        dev_set_drvdata(dev, drvdata);
 870
 871        /* default CTI device info  */
 872        drvdata->ctidev.cpu = -1;
 873        drvdata->ctidev.nr_trig_con = 0;
 874        drvdata->ctidev.ctm_id = 0;
 875        INIT_LIST_HEAD(&drvdata->ctidev.trig_cons);
 876
 877        spin_lock_init(&drvdata->spinlock);
 878
 879        /* initialise CTI driver config values */
 880        cti_set_default_config(dev, drvdata);
 881
 882        pdata = coresight_cti_get_platform_data(dev);
 883        if (IS_ERR(pdata)) {
 884                dev_err(dev, "coresight_cti_get_platform_data err\n");
 885                return  PTR_ERR(pdata);
 886        }
 887
 888        /* default to powered - could change on PM notifications */
 889        drvdata->config.hw_powered = true;
 890
 891        /* set up device name - will depend if cpu bound or otherwise */
 892        if (drvdata->ctidev.cpu >= 0)
 893                cti_desc.name = devm_kasprintf(dev, GFP_KERNEL, "cti_cpu%d",
 894                                               drvdata->ctidev.cpu);
 895        else
 896                cti_desc.name = coresight_alloc_device_name(&cti_sys_devs, dev);
 897        if (!cti_desc.name)
 898                return -ENOMEM;
 899
 900        /* setup CPU power management handling for CPU bound CTI devices. */
 901        ret = cti_pm_setup(drvdata);
 902        if (ret)
 903                return ret;
 904
 905        /* create dynamic attributes for connections */
 906        ret = cti_create_cons_sysfs(dev, drvdata);
 907        if (ret) {
 908                dev_err(dev, "%s: create dynamic sysfs entries failed\n",
 909                        cti_desc.name);
 910                goto pm_release;
 911        }
 912
 913        /* set up coresight component description */
 914        cti_desc.pdata = pdata;
 915        cti_desc.type = CORESIGHT_DEV_TYPE_ECT;
 916        cti_desc.subtype.ect_subtype = CORESIGHT_DEV_SUBTYPE_ECT_CTI;
 917        cti_desc.ops = &cti_ops;
 918        cti_desc.groups = drvdata->ctidev.con_groups;
 919        cti_desc.dev = dev;
 920        drvdata->csdev = coresight_register(&cti_desc);
 921        if (IS_ERR(drvdata->csdev)) {
 922                ret = PTR_ERR(drvdata->csdev);
 923                goto pm_release;
 924        }
 925
 926        /* add to list of CTI devices */
 927        mutex_lock(&ect_mutex);
 928        list_add(&drvdata->node, &ect_net);
 929        /* set any cross references */
 930        cti_update_conn_xrefs(drvdata);
 931        mutex_unlock(&ect_mutex);
 932
 933        /* set up release chain */
 934        drvdata->csdev_release = drvdata->csdev->dev.release;
 935        drvdata->csdev->dev.release = cti_device_release;
 936
 937        /* all done - dec pm refcount */
 938        pm_runtime_put(&adev->dev);
 939        dev_info(&drvdata->csdev->dev, "CTI initialized\n");
 940        return 0;
 941
 942pm_release:
 943        cti_pm_release(drvdata);
 944        return ret;
 945}
 946
 947static struct amba_cs_uci_id uci_id_cti[] = {
 948        {
 949                /*  CTI UCI data */
 950                .devarch        = 0x47701a14, /* CTI v2 */
 951                .devarch_mask   = 0xfff0ffff,
 952                .devtype        = 0x00000014, /* maj(0x4-debug) min(0x1-ECT) */
 953        }
 954};
 955
 956static const struct amba_id cti_ids[] = {
 957        CS_AMBA_ID(0x000bb906), /* Coresight CTI (SoC 400), C-A72, C-A57 */
 958        CS_AMBA_ID(0x000bb922), /* CTI - C-A8 */
 959        CS_AMBA_ID(0x000bb9a8), /* CTI - C-A53 */
 960        CS_AMBA_ID(0x000bb9aa), /* CTI - C-A73 */
 961        CS_AMBA_UCI_ID(0x000bb9da, uci_id_cti), /* CTI - C-A35 */
 962        CS_AMBA_UCI_ID(0x000bb9ed, uci_id_cti), /* Coresight CTI (SoC 600) */
 963        { 0, 0},
 964};
 965
 966static struct amba_driver cti_driver = {
 967        .drv = {
 968                .name   = "coresight-cti",
 969                .owner = THIS_MODULE,
 970                .suppress_bind_attrs = true,
 971        },
 972        .probe          = cti_probe,
 973        .id_table       = cti_ids,
 974};
 975builtin_amba_driver(cti_driver);
 976