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