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