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