xref: /linux/drivers/hwtracing/coresight/coresight-cti-core.c (revision cb4eb6771c0f8fd1c52a8f6fdec7762fb087380a)
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 /* write set of regs to hardware - call with spinlock claimed */
cti_write_all_hw_regs(struct cti_drvdata * drvdata)46 void cti_write_all_hw_regs(struct cti_drvdata *drvdata)
47 {
48 	struct cti_config *config = &drvdata->config;
49 	int i;
50 
51 	CS_UNLOCK(drvdata->base);
52 
53 	/* disable CTI before writing registers */
54 	writel_relaxed(0, drvdata->base + CTICONTROL);
55 
56 	/* write the CTI trigger registers */
57 	for (i = 0; i < config->nr_trig_max; i++) {
58 		writel_relaxed(config->ctiinen[i], drvdata->base + CTIINEN(i));
59 		writel_relaxed(config->ctiouten[i],
60 			       drvdata->base + CTIOUTEN(i));
61 	}
62 
63 	/* other regs */
64 	writel_relaxed(config->ctigate, drvdata->base + CTIGATE);
65 	if (config->asicctl_impl)
66 		writel_relaxed(config->asicctl, drvdata->base + ASICCTL);
67 	writel_relaxed(config->ctiappset, drvdata->base + CTIAPPSET);
68 
69 	/* re-enable CTI */
70 	writel_relaxed(1, drvdata->base + CTICONTROL);
71 
72 	CS_LOCK(drvdata->base);
73 }
74 
75 /* write regs to hardware and enable */
cti_enable_hw(struct cti_drvdata * drvdata)76 static int cti_enable_hw(struct cti_drvdata *drvdata)
77 {
78 	struct cti_config *config = &drvdata->config;
79 	int rc;
80 
81 	guard(raw_spinlock_irqsave)(&drvdata->spinlock);
82 
83 	/* no need to do anything if enabled */
84 	if (cti_is_active(config))
85 		goto cti_state_unchanged;
86 
87 	/* claim the device */
88 	rc = coresight_claim_device(drvdata->csdev);
89 	if (rc)
90 		return rc;
91 
92 	cti_write_all_hw_regs(drvdata);
93 
94 cti_state_unchanged:
95 	drvdata->config.enable_req_count++;
96 	return 0;
97 }
98 
99 /* disable hardware */
cti_disable_hw(struct cti_drvdata * drvdata)100 static int cti_disable_hw(struct cti_drvdata *drvdata)
101 {
102 	struct cti_config *config = &drvdata->config;
103 	struct coresight_device *csdev = drvdata->csdev;
104 
105 	guard(raw_spinlock_irqsave)(&drvdata->spinlock);
106 
107 	/* don't allow negative refcounts, return an error */
108 	if (!cti_is_active(config))
109 		return -EINVAL;
110 
111 	/* check refcount - disable on 0 */
112 	if (--drvdata->config.enable_req_count > 0)
113 		return 0;
114 
115 	CS_UNLOCK(drvdata->base);
116 
117 	/* disable CTI */
118 	writel_relaxed(0, drvdata->base + CTICONTROL);
119 
120 	coresight_disclaim_device_unlocked(csdev);
121 	CS_LOCK(drvdata->base);
122 	return 0;
123 }
124 
cti_read_single_reg(struct cti_drvdata * drvdata,int offset)125 u32 cti_read_single_reg(struct cti_drvdata *drvdata, int offset)
126 {
127 	int val;
128 
129 	CS_UNLOCK(drvdata->base);
130 	val = readl_relaxed(drvdata->base + offset);
131 	CS_LOCK(drvdata->base);
132 
133 	return val;
134 }
135 
cti_write_single_reg(struct cti_drvdata * drvdata,int offset,u32 value)136 void cti_write_single_reg(struct cti_drvdata *drvdata, int offset, u32 value)
137 {
138 	CS_UNLOCK(drvdata->base);
139 	writel_relaxed(value, drvdata->base + offset);
140 	CS_LOCK(drvdata->base);
141 }
142 
cti_write_intack(struct device * dev,u32 ackval)143 void cti_write_intack(struct device *dev, u32 ackval)
144 {
145 	struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
146 	struct cti_config *config = &drvdata->config;
147 
148 	guard(raw_spinlock_irqsave)(&drvdata->spinlock);
149 
150 	/* write if enabled */
151 	if (cti_is_active(config))
152 		cti_write_single_reg(drvdata, CTIINTACK, ackval);
153 }
154 
155 /*
156  * Look at the HW DEVID register for some of the HW settings.
157  * DEVID[15:8] - max number of in / out triggers.
158  */
159 #define CTI_DEVID_MAXTRIGS(devid_val) ((int) BMVAL(devid_val, 8, 15))
160 
161 /* DEVID[19:16] - number of CTM channels */
162 #define CTI_DEVID_CTMCHANNELS(devid_val) ((int) BMVAL(devid_val, 16, 19))
163 
cti_set_default_config(struct device * dev,struct cti_drvdata * drvdata)164 static void cti_set_default_config(struct device *dev,
165 				   struct cti_drvdata *drvdata)
166 {
167 	struct cti_config *config = &drvdata->config;
168 	u32 devid;
169 
170 	devid = readl_relaxed(drvdata->base + CORESIGHT_DEVID);
171 	config->nr_trig_max = CTI_DEVID_MAXTRIGS(devid);
172 
173 	/*
174 	 * no current hardware should exceed this, but protect the driver
175 	 * in case of fault / out of spec hw
176 	 */
177 	if (config->nr_trig_max > CTIINOUTEN_MAX) {
178 		dev_warn_once(dev,
179 			"Limiting HW MaxTrig value(%d) to driver max(%d)\n",
180 			config->nr_trig_max, CTIINOUTEN_MAX);
181 		config->nr_trig_max = CTIINOUTEN_MAX;
182 	}
183 
184 	config->nr_ctm_channels = CTI_DEVID_CTMCHANNELS(devid);
185 
186 	/* Most regs default to 0 as zalloc'ed except...*/
187 	config->trig_filter_enable = true;
188 	config->ctigate = GENMASK(config->nr_ctm_channels - 1, 0);
189 	config->enable_req_count = 0;
190 
191 	config->asicctl_impl = !!FIELD_GET(GENMASK(4, 0), devid);
192 }
193 
194 /*
195  * Add a connection entry to the list of connections for this
196  * CTI device.
197  */
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)198 int cti_add_connection_entry(struct device *dev, struct cti_drvdata *drvdata,
199 			     struct cti_trig_con *tc,
200 			     struct coresight_device *csdev,
201 			     const char *assoc_dev_name)
202 {
203 	struct cti_device *cti_dev = &drvdata->ctidev;
204 
205 	tc->con_dev = csdev;
206 	/*
207 	 * Prefer actual associated CS device dev name to supplied value -
208 	 * which is likely to be node name / other conn name.
209 	 */
210 	if (csdev)
211 		tc->con_dev_name = dev_name(&csdev->dev);
212 	else if (assoc_dev_name != NULL) {
213 		tc->con_dev_name = devm_kstrdup(dev,
214 						assoc_dev_name, GFP_KERNEL);
215 		if (!tc->con_dev_name)
216 			return -ENOMEM;
217 	}
218 	list_add_tail(&tc->node, &cti_dev->trig_cons);
219 	cti_dev->nr_trig_con++;
220 
221 	/* add connection usage bit info to overall info */
222 	drvdata->config.trig_in_use |= tc->con_in->used_mask;
223 	drvdata->config.trig_out_use |= tc->con_out->used_mask;
224 
225 	return 0;
226 }
227 
228 /* create a trigger connection with appropriately sized signal groups */
cti_allocate_trig_con(struct device * dev,int in_sigs,int out_sigs)229 struct cti_trig_con *cti_allocate_trig_con(struct device *dev, int in_sigs,
230 					   int out_sigs)
231 {
232 	struct cti_trig_con *tc = NULL;
233 	struct cti_trig_grp *in = NULL, *out = NULL;
234 
235 	tc = devm_kzalloc(dev, sizeof(struct cti_trig_con), GFP_KERNEL);
236 	if (!tc)
237 		return tc;
238 
239 	in = devm_kzalloc(dev,
240 			  offsetof(struct cti_trig_grp, sig_types[in_sigs]),
241 			  GFP_KERNEL);
242 	if (!in)
243 		return NULL;
244 
245 	out = devm_kzalloc(dev,
246 			   offsetof(struct cti_trig_grp, sig_types[out_sigs]),
247 			   GFP_KERNEL);
248 	if (!out)
249 		return NULL;
250 
251 	tc->con_in = in;
252 	tc->con_out = out;
253 	tc->con_in->nr_sigs = in_sigs;
254 	tc->con_out->nr_sigs = out_sigs;
255 	return tc;
256 }
257 
258 /*
259  * Add a default connection if nothing else is specified.
260  * single connection based on max in/out info, no assoc device
261  */
cti_add_default_connection(struct device * dev,struct cti_drvdata * drvdata)262 int cti_add_default_connection(struct device *dev, struct cti_drvdata *drvdata)
263 {
264 	int ret = 0;
265 	int n_trigs = drvdata->config.nr_trig_max;
266 	u32 n_trig_mask = GENMASK(n_trigs - 1, 0);
267 	struct cti_trig_con *tc = NULL;
268 
269 	/*
270 	 * Assume max trigs for in and out,
271 	 * all used, default sig types allocated
272 	 */
273 	tc = cti_allocate_trig_con(dev, n_trigs, n_trigs);
274 	if (!tc)
275 		return -ENOMEM;
276 
277 	tc->con_in->used_mask = n_trig_mask;
278 	tc->con_out->used_mask = n_trig_mask;
279 	ret = cti_add_connection_entry(dev, drvdata, tc, NULL, "default");
280 	return ret;
281 }
282 
283 /** cti channel api **/
284 /* 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)285 int cti_channel_trig_op(struct device *dev, enum cti_chan_op op,
286 			enum cti_trig_dir direction, u32 channel_idx,
287 			u32 trigger_idx)
288 {
289 	struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
290 	struct cti_config *config = &drvdata->config;
291 	u32 trig_bitmask;
292 	u32 chan_bitmask;
293 	u32 reg_value;
294 	int reg_offset;
295 
296 	/* ensure indexes in range */
297 	if ((channel_idx >= config->nr_ctm_channels) ||
298 	   (trigger_idx >= config->nr_trig_max))
299 		return -EINVAL;
300 
301 	trig_bitmask = BIT(trigger_idx);
302 
303 	/* ensure registered triggers and not out filtered */
304 	if (direction == CTI_TRIG_IN)	{
305 		if (!(trig_bitmask & config->trig_in_use))
306 			return -EINVAL;
307 	} else {
308 		if (!(trig_bitmask & config->trig_out_use))
309 			return -EINVAL;
310 
311 		if ((config->trig_filter_enable) &&
312 		    (config->trig_out_filter & trig_bitmask))
313 			return -EINVAL;
314 	}
315 
316 	/* update the local register values */
317 	chan_bitmask = BIT(channel_idx);
318 	reg_offset = (direction == CTI_TRIG_IN ? CTIINEN(trigger_idx) :
319 		      CTIOUTEN(trigger_idx));
320 
321 	guard(raw_spinlock_irqsave)(&drvdata->spinlock);
322 
323 	/* read - modify write - the trigger / channel enable value */
324 	reg_value = direction == CTI_TRIG_IN ? config->ctiinen[trigger_idx] :
325 		     config->ctiouten[trigger_idx];
326 	if (op == CTI_CHAN_ATTACH)
327 		reg_value |= chan_bitmask;
328 	else
329 		reg_value &= ~chan_bitmask;
330 
331 	/* write local copy */
332 	if (direction == CTI_TRIG_IN)
333 		config->ctiinen[trigger_idx] = reg_value;
334 	else
335 		config->ctiouten[trigger_idx] = reg_value;
336 
337 	/* write through if enabled */
338 	if (cti_is_active(config))
339 		cti_write_single_reg(drvdata, reg_offset, reg_value);
340 
341 	return 0;
342 }
343 
cti_channel_gate_op(struct device * dev,enum cti_chan_gate_op op,u32 channel_idx)344 int cti_channel_gate_op(struct device *dev, enum cti_chan_gate_op op,
345 			u32 channel_idx)
346 {
347 	struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
348 	struct cti_config *config = &drvdata->config;
349 	u32 chan_bitmask;
350 	u32 reg_value;
351 	int err = 0;
352 
353 	if (channel_idx >= config->nr_ctm_channels)
354 		return -EINVAL;
355 
356 	chan_bitmask = BIT(channel_idx);
357 
358 	guard(raw_spinlock_irqsave)(&drvdata->spinlock);
359 
360 	reg_value = config->ctigate;
361 	switch (op) {
362 	case CTI_GATE_CHAN_ENABLE:
363 		reg_value |= chan_bitmask;
364 		break;
365 
366 	case CTI_GATE_CHAN_DISABLE:
367 		reg_value &= ~chan_bitmask;
368 		break;
369 
370 	default:
371 		err = -EINVAL;
372 		break;
373 	}
374 	if (err == 0) {
375 		config->ctigate = reg_value;
376 		if (cti_is_active(config))
377 			cti_write_single_reg(drvdata, CTIGATE, reg_value);
378 	}
379 
380 	return err;
381 }
382 
cti_channel_setop(struct device * dev,enum cti_chan_set_op op,u32 channel_idx)383 int cti_channel_setop(struct device *dev, enum cti_chan_set_op op,
384 		      u32 channel_idx)
385 {
386 	struct cti_drvdata *drvdata = dev_get_drvdata(dev->parent);
387 	struct cti_config *config = &drvdata->config;
388 	u32 chan_bitmask;
389 	u32 reg_value;
390 	u32 reg_offset;
391 	int err = 0;
392 
393 	if (channel_idx >= config->nr_ctm_channels)
394 		return -EINVAL;
395 
396 	chan_bitmask = BIT(channel_idx);
397 
398 	guard(raw_spinlock_irqsave)(&drvdata->spinlock);
399 
400 	reg_value = config->ctiappset;
401 	switch (op) {
402 	case CTI_CHAN_SET:
403 		config->ctiappset |= chan_bitmask;
404 		reg_value  = config->ctiappset;
405 		reg_offset = CTIAPPSET;
406 		break;
407 
408 	case CTI_CHAN_CLR:
409 		config->ctiappset &= ~chan_bitmask;
410 		reg_value = chan_bitmask;
411 		reg_offset = CTIAPPCLEAR;
412 		break;
413 
414 	case CTI_CHAN_PULSE:
415 		config->ctiappset &= ~chan_bitmask;
416 		reg_value = chan_bitmask;
417 		reg_offset = CTIAPPPULSE;
418 		break;
419 
420 	default:
421 		err = -EINVAL;
422 		break;
423 	}
424 
425 	if ((err == 0) && cti_is_active(config))
426 		cti_write_single_reg(drvdata, reg_offset, reg_value);
427 
428 	return err;
429 }
430 
cti_add_sysfs_link(struct cti_drvdata * drvdata,struct cti_trig_con * tc)431 static bool cti_add_sysfs_link(struct cti_drvdata *drvdata,
432 			       struct cti_trig_con *tc)
433 {
434 	struct coresight_sysfs_link link_info;
435 	int link_err = 0;
436 
437 	link_info.orig = drvdata->csdev;
438 	link_info.orig_name = tc->con_dev_name;
439 	link_info.target = tc->con_dev;
440 	link_info.target_name = dev_name(&drvdata->csdev->dev);
441 
442 	link_err = coresight_add_sysfs_link(&link_info);
443 	if (link_err)
444 		dev_warn(&drvdata->csdev->dev,
445 			 "Failed to set CTI sysfs link %s<=>%s\n",
446 			 link_info.orig_name, link_info.target_name);
447 	return !link_err;
448 }
449 
cti_remove_sysfs_link(struct cti_drvdata * drvdata,struct cti_trig_con * tc)450 static void cti_remove_sysfs_link(struct cti_drvdata *drvdata,
451 				  struct cti_trig_con *tc)
452 {
453 	struct coresight_sysfs_link link_info;
454 
455 	link_info.orig = drvdata->csdev;
456 	link_info.orig_name = tc->con_dev_name;
457 	link_info.target = tc->con_dev;
458 	link_info.target_name = dev_name(&drvdata->csdev->dev);
459 	coresight_remove_sysfs_link(&link_info);
460 }
461 
462 /*
463  * Look for a matching connection device name in the list of connections.
464  * If found then swap in the csdev name, set trig con association pointer
465  * and return found.
466  */
467 static bool
cti_match_fixup_csdev(struct cti_device * ctidev,const char * node_name,struct coresight_device * csdev)468 cti_match_fixup_csdev(struct cti_device *ctidev, const char *node_name,
469 		      struct coresight_device *csdev)
470 {
471 	struct cti_trig_con *tc;
472 	struct cti_drvdata *drvdata = container_of(ctidev, struct cti_drvdata,
473 						   ctidev);
474 
475 	list_for_each_entry(tc, &ctidev->trig_cons, node) {
476 		if (tc->con_dev_name) {
477 			if (!strcmp(node_name, tc->con_dev_name)) {
478 				/* match: so swap in csdev name & dev */
479 				tc->con_dev_name = dev_name(&csdev->dev);
480 				tc->con_dev = csdev;
481 				/* try to set sysfs link */
482 				if (cti_add_sysfs_link(drvdata, tc))
483 					return true;
484 				/* link failed - remove CTI reference */
485 				tc->con_dev = NULL;
486 				break;
487 			}
488 		}
489 	}
490 	return false;
491 }
492 
493 /*
494  * Search the cti list to add an associated CTI into the supplied CS device
495  * This will set the association if CTI declared before the CS device.
496  * (called from coresight_register() without coresight_mutex locked).
497  */
cti_add_assoc_to_csdev(struct coresight_device * csdev)498 static void cti_add_assoc_to_csdev(struct coresight_device *csdev)
499 {
500 	struct cti_drvdata *ect_item;
501 	struct cti_device *ctidev;
502 	const char *node_name = NULL;
503 
504 	/* protect the list */
505 	mutex_lock(&ect_mutex);
506 
507 	/* exit if current is an ECT device.*/
508 	if ((csdev->type == CORESIGHT_DEV_TYPE_HELPER &&
509 	     csdev->subtype.helper_subtype ==
510 		     CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI) ||
511 	    list_empty(&ect_net))
512 		goto cti_add_done;
513 
514 	/* if we didn't find the csdev previously we used the fwnode name */
515 	node_name = cti_plat_get_node_name(dev_fwnode(csdev->dev.parent));
516 	if (!node_name)
517 		goto cti_add_done;
518 
519 	/* for each CTI in list... */
520 	list_for_each_entry(ect_item, &ect_net, node) {
521 		ctidev = &ect_item->ctidev;
522 		if (cti_match_fixup_csdev(ctidev, node_name, csdev)) {
523 			/*
524 			 * if we found a matching csdev then update the ECT
525 			 * association pointer for the device with this CTI.
526 			 */
527 			coresight_add_helper(csdev, ect_item->csdev);
528 			break;
529 		}
530 	}
531 cti_add_done:
532 	mutex_unlock(&ect_mutex);
533 }
534 
535 /*
536  * Removing the associated devices is easier.
537  */
cti_remove_assoc_from_csdev(struct coresight_device * csdev)538 static void cti_remove_assoc_from_csdev(struct coresight_device *csdev)
539 {
540 	struct cti_drvdata *ctidrv;
541 	struct cti_trig_con *tc;
542 	struct cti_device *ctidev;
543 	union coresight_dev_subtype cti_subtype = {
544 		.helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI
545 	};
546 	struct coresight_device *cti_csdev = coresight_find_output_type(
547 		csdev->pdata, CORESIGHT_DEV_TYPE_HELPER, cti_subtype);
548 
549 	if (!cti_csdev)
550 		return;
551 
552 	mutex_lock(&ect_mutex);
553 	ctidrv = csdev_to_cti_drvdata(cti_csdev);
554 	ctidev = &ctidrv->ctidev;
555 	list_for_each_entry(tc, &ctidev->trig_cons, node) {
556 		if (tc->con_dev == csdev) {
557 			cti_remove_sysfs_link(ctidrv, tc);
558 			tc->con_dev = NULL;
559 			break;
560 		}
561 	}
562 	mutex_unlock(&ect_mutex);
563 }
564 
565 /*
566  * Operations to add and remove associated CTI.
567  * Register to coresight core driver as call back function.
568  */
569 static struct cti_assoc_op cti_assoc_ops = {
570 	.add = cti_add_assoc_to_csdev,
571 	.remove = cti_remove_assoc_from_csdev
572 };
573 
574 /*
575  * Update the cross references where the associated device was found
576  * while we were building the connection info. This will occur if the
577  * assoc device was registered before the CTI.
578  */
cti_update_conn_xrefs(struct cti_drvdata * drvdata)579 static void cti_update_conn_xrefs(struct cti_drvdata *drvdata)
580 {
581 	struct cti_trig_con *tc;
582 	struct cti_device *ctidev = &drvdata->ctidev;
583 
584 	list_for_each_entry(tc, &ctidev->trig_cons, node) {
585 		if (tc->con_dev) {
586 			/* if we can set the sysfs link */
587 			if (cti_add_sysfs_link(drvdata, tc))
588 				/* set the CTI/csdev association */
589 				coresight_add_helper(tc->con_dev,
590 						     drvdata->csdev);
591 			else
592 				/* otherwise remove reference from CTI */
593 				tc->con_dev = NULL;
594 		}
595 	}
596 }
597 
cti_remove_conn_xrefs(struct cti_drvdata * drvdata)598 static void cti_remove_conn_xrefs(struct cti_drvdata *drvdata)
599 {
600 	struct cti_trig_con *tc;
601 	struct cti_device *ctidev = &drvdata->ctidev;
602 
603 	list_for_each_entry(tc, &ctidev->trig_cons, node) {
604 		if (tc->con_dev) {
605 			cti_remove_sysfs_link(drvdata, tc);
606 			tc->con_dev = NULL;
607 		}
608 	}
609 }
610 
611 /** cti ect operations **/
cti_enable(struct coresight_device * csdev,enum cs_mode mode,struct coresight_path * path)612 int cti_enable(struct coresight_device *csdev, enum cs_mode mode,
613 	       struct coresight_path *path)
614 {
615 	struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
616 
617 	return cti_enable_hw(drvdata);
618 }
619 
cti_disable(struct coresight_device * csdev,struct coresight_path * path)620 int cti_disable(struct coresight_device *csdev, struct coresight_path *path)
621 {
622 	struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
623 
624 	return cti_disable_hw(drvdata);
625 }
626 
627 static const struct coresight_ops_helper cti_ops_ect = {
628 	.enable = cti_enable,
629 	.disable = cti_disable,
630 };
631 
632 static const struct coresight_ops cti_ops = {
633 	.helper_ops = &cti_ops_ect,
634 };
635 
cti_remove(struct amba_device * adev)636 static void cti_remove(struct amba_device *adev)
637 {
638 	struct cti_drvdata *drvdata = dev_get_drvdata(&adev->dev);
639 	struct cti_drvdata *ect_item, *ect_tmp;
640 
641 	mutex_lock(&ect_mutex);
642 	cti_remove_conn_xrefs(drvdata);
643 
644 	/* remove from the list */
645 	list_for_each_entry_safe(ect_item, ect_tmp, &ect_net, node) {
646 		if (ect_item == drvdata) {
647 			list_del(&ect_item->node);
648 			break;
649 		}
650 	}
651 	mutex_unlock(&ect_mutex);
652 
653 	coresight_unregister(drvdata->csdev);
654 }
655 
cti_probe(struct amba_device * adev,const struct amba_id * id)656 static int cti_probe(struct amba_device *adev, const struct amba_id *id)
657 {
658 	int ret = 0;
659 	void __iomem *base;
660 	struct device *dev = &adev->dev;
661 	struct cti_drvdata *drvdata = NULL;
662 	struct coresight_desc cti_desc;
663 	struct coresight_platform_data *pdata = NULL;
664 	struct resource *res = &adev->res;
665 
666 	/* driver data*/
667 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
668 	if (!drvdata)
669 		return -ENOMEM;
670 
671 	/* Validity for the resource is already checked by the AMBA core */
672 	base = devm_ioremap_resource(dev, res);
673 	if (IS_ERR(base))
674 		return PTR_ERR(base);
675 
676 	drvdata->base = base;
677 	cti_desc.access = CSDEV_ACCESS_IOMEM(base);
678 
679 	dev_set_drvdata(dev, drvdata);
680 
681 	/* default CTI device info  */
682 	drvdata->ctidev.cpu = -1;
683 	drvdata->ctidev.nr_trig_con = 0;
684 	drvdata->ctidev.ctm_id = 0;
685 	INIT_LIST_HEAD(&drvdata->ctidev.trig_cons);
686 
687 	raw_spin_lock_init(&drvdata->spinlock);
688 
689 	/* initialise CTI driver config values */
690 	cti_set_default_config(dev, drvdata);
691 
692 	pdata = coresight_cti_get_platform_data(dev);
693 	if (IS_ERR(pdata)) {
694 		dev_err(dev, "coresight_cti_get_platform_data err\n");
695 		return  PTR_ERR(pdata);
696 	}
697 
698 	/*
699 	 * Set up device name - will depend if cpu bound or otherwise.
700 	 *
701 	 * CTI bound to cores will have the name cti_cpu<N> where N is th
702 	 * eCPU ID. System CTIs will have the name cti_sys<I> where I is an
703 	 * index allocated by order of discovery.
704 	 */
705 	if (drvdata->ctidev.cpu >= 0)
706 		cti_desc.name = devm_kasprintf(dev, GFP_KERNEL, "cti_cpu%d",
707 					       drvdata->ctidev.cpu);
708 	else
709 		cti_desc.name = coresight_alloc_device_name("cti_sys", dev);
710 	if (!cti_desc.name)
711 		return -ENOMEM;
712 
713 	/* create dynamic attributes for connections */
714 	ret = cti_create_cons_sysfs(dev, drvdata);
715 	if (ret) {
716 		dev_err(dev, "%s: create dynamic sysfs entries failed\n",
717 			cti_desc.name);
718 		return ret;
719 	}
720 
721 	/* set up coresight component description */
722 	cti_desc.pdata = pdata;
723 	cti_desc.type = CORESIGHT_DEV_TYPE_HELPER;
724 	cti_desc.subtype.helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI;
725 	cti_desc.ops = &cti_ops;
726 	cti_desc.groups = drvdata->ctidev.con_groups;
727 	cti_desc.dev = dev;
728 
729 	coresight_clear_self_claim_tag(&cti_desc.access);
730 	drvdata->csdev = coresight_register(&cti_desc);
731 	if (IS_ERR(drvdata->csdev))
732 		return PTR_ERR(drvdata->csdev);
733 
734 	/* add to list of CTI devices */
735 	mutex_lock(&ect_mutex);
736 	list_add(&drvdata->node, &ect_net);
737 	/* set any cross references */
738 	cti_update_conn_xrefs(drvdata);
739 	mutex_unlock(&ect_mutex);
740 
741 	/* all done - dec pm refcount */
742 	pm_runtime_put(&adev->dev);
743 	dev_info(&drvdata->csdev->dev, "CTI initialized\n");
744 	return 0;
745 }
746 
747 static struct amba_cs_uci_id uci_id_cti[] = {
748 	{
749 		/*  CTI UCI data */
750 		.devarch	= 0x47701a14, /* CTI v2 */
751 		.devarch_mask	= 0xfff0ffff,
752 		.devtype	= 0x00000014, /* maj(0x4-debug) min(0x1-ECT) */
753 	}
754 };
755 
756 static const struct amba_id cti_ids[] = {
757 	CS_AMBA_ID(0x000bb906), /* Coresight CTI (SoC 400), C-A72, C-A57 */
758 	CS_AMBA_ID(0x000bb922), /* CTI - C-A8 */
759 	CS_AMBA_ID(0x000bb9a8), /* CTI - C-A53 */
760 	CS_AMBA_ID(0x000bb9aa), /* CTI - C-A73 */
761 	CS_AMBA_UCI_ID(0x000bb9da, uci_id_cti), /* CTI - C-A35 */
762 	CS_AMBA_UCI_ID(0x000bb9ed, uci_id_cti), /* Coresight CTI (SoC 600) */
763 	{ 0, 0, NULL },
764 };
765 
766 MODULE_DEVICE_TABLE(amba, cti_ids);
767 
768 static struct amba_driver cti_driver = {
769 	.drv = {
770 		.name	= "coresight-cti",
771 		.suppress_bind_attrs = true,
772 	},
773 	.probe		= cti_probe,
774 	.remove		= cti_remove,
775 	.id_table	= cti_ids,
776 };
777 
cti_init(void)778 static int __init cti_init(void)
779 {
780 	int ret;
781 
782 	ret = amba_driver_register(&cti_driver);
783 	if (ret)
784 		pr_info("Error registering cti driver\n");
785 	coresight_set_cti_ops(&cti_assoc_ops);
786 	return ret;
787 }
788 
cti_exit(void)789 static void __exit cti_exit(void)
790 {
791 	coresight_remove_cti_ops();
792 	amba_driver_unregister(&cti_driver);
793 }
794 
795 module_init(cti_init);
796 module_exit(cti_exit);
797 
798 MODULE_AUTHOR("Mike Leach <mike.leach@linaro.org>");
799 MODULE_DESCRIPTION("Arm CoreSight CTI Driver");
800 MODULE_LICENSE("GPL v2");
801