blob: 398e44a9ec45d5b141e22177df429d7cd9c202d9 [file] [log] [blame]
Pratik Patela06ae862014-11-03 11:07:35 -07001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/kernel.h>
Pratik Patela06ae862014-11-03 11:07:35 -070014#include <linux/init.h>
15#include <linux/types.h>
16#include <linux/device.h>
17#include <linux/io.h>
18#include <linux/err.h>
19#include <linux/export.h>
20#include <linux/slab.h>
21#include <linux/mutex.h>
22#include <linux/clk.h>
23#include <linux/coresight.h>
24#include <linux/of_platform.h>
25#include <linux/delay.h>
Mathieu Poirier5da53252016-02-17 17:51:47 -070026#include <linux/pm_runtime.h>
Pratik Patela06ae862014-11-03 11:07:35 -070027
28#include "coresight-priv.h"
29
30static DEFINE_MUTEX(coresight_mutex);
31
Mathieu Poirierb3e94402016-02-17 17:51:45 -070032/**
33 * struct coresight_node - elements of a path, from source to sink
34 * @csdev: Address of an element.
35 * @link: hook to the list.
36 */
37struct coresight_node {
38 struct coresight_device *csdev;
39 struct list_head link;
40};
41
42/*
43 * When operating Coresight drivers from the sysFS interface, only a single
44 * path can exist from a tracer (associated to a CPU) to a sink.
45 */
Mathieu Poiriera685d682016-05-03 11:33:38 -060046static DEFINE_PER_CPU(struct list_head *, tracer_path);
47
48/*
49 * As of this writing only a single STM can be found in CS topologies. Since
50 * there is no way to know if we'll ever see more and what kind of
51 * configuration they will enact, for the time being only define a single path
52 * for STM.
53 */
54static struct list_head *stm_path;
Mathieu Poirierb3e94402016-02-17 17:51:45 -070055
Pratik Patela06ae862014-11-03 11:07:35 -070056static int coresight_id_match(struct device *dev, void *data)
57{
58 int trace_id, i_trace_id;
59 struct coresight_device *csdev, *i_csdev;
60
61 csdev = data;
62 i_csdev = to_coresight_device(dev);
63
64 /*
65 * No need to care about oneself and components that are not
66 * sources or not enabled
67 */
68 if (i_csdev == csdev || !i_csdev->enable ||
69 i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
70 return 0;
71
72 /* Get the source ID for both compoment */
73 trace_id = source_ops(csdev)->trace_id(csdev);
74 i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
75
76 /* All you need is one */
77 if (trace_id == i_trace_id)
78 return 1;
79
80 return 0;
81}
82
83static int coresight_source_is_unique(struct coresight_device *csdev)
84{
85 int trace_id = source_ops(csdev)->trace_id(csdev);
86
87 /* this shouldn't happen */
88 if (trace_id < 0)
89 return 0;
90
91 return !bus_for_each_dev(&coresight_bustype, NULL,
92 csdev, coresight_id_match);
93}
94
Mathieu Poirierb3e94402016-02-17 17:51:45 -070095static int coresight_find_link_inport(struct coresight_device *csdev,
96 struct coresight_device *parent)
Pratik Patela06ae862014-11-03 11:07:35 -070097{
98 int i;
Pratik Patela06ae862014-11-03 11:07:35 -070099 struct coresight_connection *conn;
100
Pratik Patela06ae862014-11-03 11:07:35 -0700101 for (i = 0; i < parent->nr_outport; i++) {
102 conn = &parent->conns[i];
103 if (conn->child_dev == csdev)
104 return conn->child_port;
105 }
106
107 dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
108 dev_name(&parent->dev), dev_name(&csdev->dev));
109
Suzuki K Poulose2887d522018-07-11 13:40:28 -0600110 return -ENODEV;
Pratik Patela06ae862014-11-03 11:07:35 -0700111}
112
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700113static int coresight_find_link_outport(struct coresight_device *csdev,
114 struct coresight_device *child)
Pratik Patela06ae862014-11-03 11:07:35 -0700115{
116 int i;
Pratik Patela06ae862014-11-03 11:07:35 -0700117 struct coresight_connection *conn;
118
Pratik Patela06ae862014-11-03 11:07:35 -0700119 for (i = 0; i < csdev->nr_outport; i++) {
120 conn = &csdev->conns[i];
121 if (conn->child_dev == child)
122 return conn->outport;
123 }
124
125 dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
126 dev_name(&csdev->dev), dev_name(&child->dev));
127
Suzuki K Poulose2887d522018-07-11 13:40:28 -0600128 return -ENODEV;
Pratik Patela06ae862014-11-03 11:07:35 -0700129}
130
Mathieu Poiriere827d452016-02-17 17:51:59 -0700131static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
Pratik Patela06ae862014-11-03 11:07:35 -0700132{
133 int ret;
134
135 if (!csdev->enable) {
136 if (sink_ops(csdev)->enable) {
Mathieu Poiriere827d452016-02-17 17:51:59 -0700137 ret = sink_ops(csdev)->enable(csdev, mode);
Pratik Patela06ae862014-11-03 11:07:35 -0700138 if (ret)
139 return ret;
140 }
141 csdev->enable = true;
142 }
143
144 atomic_inc(csdev->refcnt);
145
146 return 0;
147}
148
149static void coresight_disable_sink(struct coresight_device *csdev)
150{
151 if (atomic_dec_return(csdev->refcnt) == 0) {
152 if (sink_ops(csdev)->disable) {
153 sink_ops(csdev)->disable(csdev);
154 csdev->enable = false;
155 }
156 }
157}
158
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700159static int coresight_enable_link(struct coresight_device *csdev,
160 struct coresight_device *parent,
161 struct coresight_device *child)
Pratik Patela06ae862014-11-03 11:07:35 -0700162{
163 int ret;
164 int link_subtype;
165 int refport, inport, outport;
166
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700167 if (!parent || !child)
168 return -EINVAL;
169
170 inport = coresight_find_link_inport(csdev, parent);
171 outport = coresight_find_link_outport(csdev, child);
Pratik Patela06ae862014-11-03 11:07:35 -0700172 link_subtype = csdev->subtype.link_subtype;
173
174 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
175 refport = inport;
176 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
177 refport = outport;
178 else
179 refport = 0;
180
Suzuki K Poulose2887d522018-07-11 13:40:28 -0600181 if (refport < 0)
182 return refport;
183
Pratik Patela06ae862014-11-03 11:07:35 -0700184 if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
185 if (link_ops(csdev)->enable) {
186 ret = link_ops(csdev)->enable(csdev, inport, outport);
187 if (ret)
188 return ret;
189 }
190 }
191
192 csdev->enable = true;
193
194 return 0;
195}
196
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700197static void coresight_disable_link(struct coresight_device *csdev,
198 struct coresight_device *parent,
199 struct coresight_device *child)
Pratik Patela06ae862014-11-03 11:07:35 -0700200{
201 int i, nr_conns;
202 int link_subtype;
203 int refport, inport, outport;
204
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700205 if (!parent || !child)
206 return;
207
208 inport = coresight_find_link_inport(csdev, parent);
209 outport = coresight_find_link_outport(csdev, child);
Pratik Patela06ae862014-11-03 11:07:35 -0700210 link_subtype = csdev->subtype.link_subtype;
211
212 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
213 refport = inport;
214 nr_conns = csdev->nr_inport;
215 } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
216 refport = outport;
217 nr_conns = csdev->nr_outport;
218 } else {
219 refport = 0;
220 nr_conns = 1;
221 }
222
223 if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
224 if (link_ops(csdev)->disable)
225 link_ops(csdev)->disable(csdev, inport, outport);
226 }
227
228 for (i = 0; i < nr_conns; i++)
229 if (atomic_read(&csdev->refcnt[i]) != 0)
230 return;
231
232 csdev->enable = false;
233}
234
Mathieu Poirier22fd5322016-02-17 17:51:52 -0700235static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
Pratik Patela06ae862014-11-03 11:07:35 -0700236{
237 int ret;
238
239 if (!coresight_source_is_unique(csdev)) {
240 dev_warn(&csdev->dev, "traceID %d not unique\n",
241 source_ops(csdev)->trace_id(csdev));
242 return -EINVAL;
243 }
244
245 if (!csdev->enable) {
246 if (source_ops(csdev)->enable) {
Mathieu Poirier882d5e12016-02-17 17:51:57 -0700247 ret = source_ops(csdev)->enable(csdev, NULL, mode);
Pratik Patela06ae862014-11-03 11:07:35 -0700248 if (ret)
249 return ret;
250 }
251 csdev->enable = true;
252 }
253
254 atomic_inc(csdev->refcnt);
255
256 return 0;
257}
258
259static void coresight_disable_source(struct coresight_device *csdev)
260{
261 if (atomic_dec_return(csdev->refcnt) == 0) {
262 if (source_ops(csdev)->disable) {
Mathieu Poirier68905d72016-08-25 15:19:10 -0600263 source_ops(csdev)->disable(csdev, NULL);
Pratik Patela06ae862014-11-03 11:07:35 -0700264 csdev->enable = false;
265 }
266 }
267}
268
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700269void coresight_disable_path(struct list_head *path)
Pratik Patela06ae862014-11-03 11:07:35 -0700270{
Mathieu Poirierdc2c4ef2016-05-03 11:34:00 -0600271 u32 type;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700272 struct coresight_node *nd;
273 struct coresight_device *csdev, *parent, *child;
274
275 list_for_each_entry(nd, path, link) {
276 csdev = nd->csdev;
Mathieu Poirierdc2c4ef2016-05-03 11:34:00 -0600277 type = csdev->type;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700278
Mathieu Poirierdc2c4ef2016-05-03 11:34:00 -0600279 /*
280 * ETF devices are tricky... They can be a link or a sink,
281 * depending on how they are configured. If an ETF has been
282 * "activated" it will be configured as a sink, otherwise
283 * go ahead with the link configuration.
284 */
285 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
286 type = (csdev == coresight_get_sink(path)) ?
287 CORESIGHT_DEV_TYPE_SINK :
288 CORESIGHT_DEV_TYPE_LINK;
289
290 switch (type) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700291 case CORESIGHT_DEV_TYPE_SINK:
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700292 coresight_disable_sink(csdev);
293 break;
294 case CORESIGHT_DEV_TYPE_SOURCE:
295 /* sources are disabled from either sysFS or Perf */
296 break;
297 case CORESIGHT_DEV_TYPE_LINK:
298 parent = list_prev_entry(nd, link)->csdev;
299 child = list_next_entry(nd, link)->csdev;
300 coresight_disable_link(csdev, parent, child);
301 break;
302 default:
303 break;
304 }
305 }
306}
307
Mathieu Poiriere827d452016-02-17 17:51:59 -0700308int coresight_enable_path(struct list_head *path, u32 mode)
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700309{
310
Pratik Patela06ae862014-11-03 11:07:35 -0700311 int ret = 0;
Mathieu Poirierdc2c4ef2016-05-03 11:34:00 -0600312 u32 type;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700313 struct coresight_node *nd;
314 struct coresight_device *csdev, *parent, *child;
Pratik Patela06ae862014-11-03 11:07:35 -0700315
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700316 list_for_each_entry_reverse(nd, path, link) {
317 csdev = nd->csdev;
Mathieu Poirierdc2c4ef2016-05-03 11:34:00 -0600318 type = csdev->type;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700319
Mathieu Poirierdc2c4ef2016-05-03 11:34:00 -0600320 /*
321 * ETF devices are tricky... They can be a link or a sink,
322 * depending on how they are configured. If an ETF has been
323 * "activated" it will be configured as a sink, otherwise
324 * go ahead with the link configuration.
325 */
326 if (type == CORESIGHT_DEV_TYPE_LINKSINK)
327 type = (csdev == coresight_get_sink(path)) ?
328 CORESIGHT_DEV_TYPE_SINK :
329 CORESIGHT_DEV_TYPE_LINK;
330
331 switch (type) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700332 case CORESIGHT_DEV_TYPE_SINK:
Mathieu Poiriere827d452016-02-17 17:51:59 -0700333 ret = coresight_enable_sink(csdev, mode);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700334 if (ret)
335 goto err;
336 break;
337 case CORESIGHT_DEV_TYPE_SOURCE:
338 /* sources are enabled from either sysFS or Perf */
339 break;
340 case CORESIGHT_DEV_TYPE_LINK:
341 parent = list_prev_entry(nd, link)->csdev;
342 child = list_next_entry(nd, link)->csdev;
343 ret = coresight_enable_link(csdev, parent, child);
344 if (ret)
345 goto err;
346 break;
347 default:
Pratik Patela06ae862014-11-03 11:07:35 -0700348 goto err;
Pratik Patela06ae862014-11-03 11:07:35 -0700349 }
350 }
351
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700352out:
Pratik Patela06ae862014-11-03 11:07:35 -0700353 return ret;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700354err:
355 coresight_disable_path(path);
356 goto out;
Pratik Patela06ae862014-11-03 11:07:35 -0700357}
358
Mathieu Poirierb6404e22016-02-17 17:51:46 -0700359struct coresight_device *coresight_get_sink(struct list_head *path)
360{
361 struct coresight_device *csdev;
362
363 if (!path)
364 return NULL;
365
366 csdev = list_last_entry(path, struct coresight_node, link)->csdev;
367 if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
368 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
369 return NULL;
370
371 return csdev;
372}
373
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700374/**
375 * _coresight_build_path - recursively build a path from a @csdev to a sink.
376 * @csdev: The device to start from.
377 * @path: The list to add devices to.
378 *
379 * The tree of Coresight device is traversed until an activated sink is
380 * found. From there the sink is added to the list along with all the
381 * devices that led to that point - the end result is a list from source
382 * to sink. In that list the source is the first device and the sink the
383 * last one.
384 */
385static int _coresight_build_path(struct coresight_device *csdev,
386 struct list_head *path)
Pratik Patela06ae862014-11-03 11:07:35 -0700387{
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700388 int i;
389 bool found = false;
390 struct coresight_node *node;
Pratik Patela06ae862014-11-03 11:07:35 -0700391
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700392 /* An activated sink has been found. Enqueue the element */
Xia Kaixua0a500e2015-03-30 14:13:38 -0600393 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700394 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated)
395 goto out;
396
397 /* Not a sink - recursively explore each port found on this element */
398 for (i = 0; i < csdev->nr_outport; i++) {
Suzuki K Pouloseec48a1d2016-06-14 11:17:12 -0600399 struct coresight_device *child_dev = csdev->conns[i].child_dev;
400
401 if (child_dev && _coresight_build_path(child_dev, path) == 0) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700402 found = true;
403 break;
Pratik Patela06ae862014-11-03 11:07:35 -0700404 }
405 }
406
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700407 if (!found)
408 return -ENODEV;
Pratik Patela06ae862014-11-03 11:07:35 -0700409
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700410out:
411 /*
412 * A path from this element to a sink has been found. The elements
413 * leading to the sink are already enqueued, all that is left to do
Mathieu Poirier5da53252016-02-17 17:51:47 -0700414 * is tell the PM runtime core we need this element and add a node
415 * for it.
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700416 */
417 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
418 if (!node)
419 return -ENOMEM;
Pratik Patela06ae862014-11-03 11:07:35 -0700420
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700421 node->csdev = csdev;
422 list_add(&node->link, path);
Mathieu Poirier5da53252016-02-17 17:51:47 -0700423 pm_runtime_get_sync(csdev->dev.parent);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700424
425 return 0;
426}
427
428struct list_head *coresight_build_path(struct coresight_device *csdev)
429{
430 struct list_head *path;
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100431 int rc;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700432
433 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
434 if (!path)
Mathieu Poirier8e67cdb2016-09-08 16:50:38 -0600435 return ERR_PTR(-ENOMEM);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700436
437 INIT_LIST_HEAD(path);
438
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100439 rc = _coresight_build_path(csdev, path);
440 if (rc) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700441 kfree(path);
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100442 return ERR_PTR(rc);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700443 }
444
445 return path;
446}
447
448/**
449 * coresight_release_path - release a previously built path.
450 * @path: the path to release.
451 *
452 * Go through all the elements of a path and 1) removed it from the list and
453 * 2) free the memory allocated for each node.
454 */
455void coresight_release_path(struct list_head *path)
456{
Mathieu Poirier5da53252016-02-17 17:51:47 -0700457 struct coresight_device *csdev;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700458 struct coresight_node *nd, *next;
459
460 list_for_each_entry_safe(nd, next, path, link) {
Mathieu Poirier5da53252016-02-17 17:51:47 -0700461 csdev = nd->csdev;
462
463 pm_runtime_put_sync(csdev->dev.parent);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700464 list_del(&nd->link);
465 kfree(nd);
466 }
467
468 kfree(path);
469 path = NULL;
Pratik Patela06ae862014-11-03 11:07:35 -0700470}
471
Mathieu Poiriera685d682016-05-03 11:33:38 -0600472/** coresight_validate_source - make sure a source has the right credentials
473 * @csdev: the device structure for a source.
474 * @function: the function this was called from.
475 *
476 * Assumes the coresight_mutex is held.
477 */
478static int coresight_validate_source(struct coresight_device *csdev,
479 const char *function)
480{
481 u32 type, subtype;
482
483 type = csdev->type;
484 subtype = csdev->subtype.source_subtype;
485
486 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
487 dev_err(&csdev->dev, "wrong device type in %s\n", function);
488 return -EINVAL;
489 }
490
491 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
492 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
493 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
494 return -EINVAL;
495 }
496
497 return 0;
498}
499
Pratik Patela06ae862014-11-03 11:07:35 -0700500int coresight_enable(struct coresight_device *csdev)
501{
Mathieu Poiriera685d682016-05-03 11:33:38 -0600502 int cpu, ret = 0;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700503 struct list_head *path;
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600504 enum coresight_dev_subtype_source subtype;
505
506 subtype = csdev->subtype.source_subtype;
Pratik Patela06ae862014-11-03 11:07:35 -0700507
508 mutex_lock(&coresight_mutex);
Mathieu Poiriera685d682016-05-03 11:33:38 -0600509
510 ret = coresight_validate_source(csdev, __func__);
511 if (ret)
Pratik Patela06ae862014-11-03 11:07:35 -0700512 goto out;
Mathieu Poiriera685d682016-05-03 11:33:38 -0600513
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600514 if (csdev->enable) {
515 /*
516 * There could be multiple applications driving the software
517 * source. So keep the refcount for each such user when the
518 * source is already enabled.
519 */
520 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
521 atomic_inc(csdev->refcnt);
Pratik Patela06ae862014-11-03 11:07:35 -0700522 goto out;
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600523 }
Pratik Patela06ae862014-11-03 11:07:35 -0700524
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700525 path = coresight_build_path(csdev);
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100526 if (IS_ERR(path)) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700527 pr_err("building path(s) failed\n");
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100528 ret = PTR_ERR(path);
Pratik Patela06ae862014-11-03 11:07:35 -0700529 goto out;
530 }
531
Mathieu Poiriere827d452016-02-17 17:51:59 -0700532 ret = coresight_enable_path(path, CS_MODE_SYSFS);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700533 if (ret)
534 goto err_path;
535
Mathieu Poirier22fd5322016-02-17 17:51:52 -0700536 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700537 if (ret)
538 goto err_source;
539
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600540 switch (subtype) {
Mathieu Poiriera685d682016-05-03 11:33:38 -0600541 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
542 /*
543 * When working from sysFS it is important to keep track
544 * of the paths that were created so that they can be
545 * undone in 'coresight_disable()'. Since there can only
546 * be a single session per tracer (when working from sysFS)
547 * a per-cpu variable will do just fine.
548 */
549 cpu = source_ops(csdev)->cpu_id(csdev);
550 per_cpu(tracer_path, cpu) = path;
551 break;
552 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
553 stm_path = path;
554 break;
555 default:
556 /* We can't be here */
557 break;
558 }
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700559
Pratik Patela06ae862014-11-03 11:07:35 -0700560out:
561 mutex_unlock(&coresight_mutex);
562 return ret;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700563
564err_source:
565 coresight_disable_path(path);
566
567err_path:
568 coresight_release_path(path);
569 goto out;
Pratik Patela06ae862014-11-03 11:07:35 -0700570}
571EXPORT_SYMBOL_GPL(coresight_enable);
572
573void coresight_disable(struct coresight_device *csdev)
574{
Mathieu Poiriera685d682016-05-03 11:33:38 -0600575 int cpu, ret;
576 struct list_head *path = NULL;
Pratik Patela06ae862014-11-03 11:07:35 -0700577
578 mutex_lock(&coresight_mutex);
Mathieu Poiriera685d682016-05-03 11:33:38 -0600579
580 ret = coresight_validate_source(csdev, __func__);
581 if (ret)
Pratik Patela06ae862014-11-03 11:07:35 -0700582 goto out;
Mathieu Poiriera685d682016-05-03 11:33:38 -0600583
Pratik Patela06ae862014-11-03 11:07:35 -0700584 if (!csdev->enable)
585 goto out;
586
Mathieu Poiriera685d682016-05-03 11:33:38 -0600587 switch (csdev->subtype.source_subtype) {
588 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
589 cpu = source_ops(csdev)->cpu_id(csdev);
590 path = per_cpu(tracer_path, cpu);
591 per_cpu(tracer_path, cpu) = NULL;
592 break;
593 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
594 path = stm_path;
595 stm_path = NULL;
596 break;
597 default:
598 /* We can't be here */
599 break;
600 }
601
Pratik Patela06ae862014-11-03 11:07:35 -0700602 coresight_disable_source(csdev);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700603 coresight_disable_path(path);
604 coresight_release_path(path);
Pratik Patela06ae862014-11-03 11:07:35 -0700605
606out:
607 mutex_unlock(&coresight_mutex);
608}
609EXPORT_SYMBOL_GPL(coresight_disable);
610
611static ssize_t enable_sink_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
613{
614 struct coresight_device *csdev = to_coresight_device(dev);
615
Li Pengchenge8dc27d2016-05-03 11:33:35 -0600616 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
Pratik Patela06ae862014-11-03 11:07:35 -0700617}
618
619static ssize_t enable_sink_store(struct device *dev,
620 struct device_attribute *attr,
621 const char *buf, size_t size)
622{
623 int ret;
624 unsigned long val;
625 struct coresight_device *csdev = to_coresight_device(dev);
626
627 ret = kstrtoul(buf, 10, &val);
628 if (ret)
629 return ret;
630
631 if (val)
632 csdev->activated = true;
633 else
634 csdev->activated = false;
635
636 return size;
637
638}
639static DEVICE_ATTR_RW(enable_sink);
640
641static ssize_t enable_source_show(struct device *dev,
642 struct device_attribute *attr, char *buf)
643{
644 struct coresight_device *csdev = to_coresight_device(dev);
645
Li Pengchenge8dc27d2016-05-03 11:33:35 -0600646 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
Pratik Patela06ae862014-11-03 11:07:35 -0700647}
648
649static ssize_t enable_source_store(struct device *dev,
650 struct device_attribute *attr,
651 const char *buf, size_t size)
652{
653 int ret = 0;
654 unsigned long val;
655 struct coresight_device *csdev = to_coresight_device(dev);
656
657 ret = kstrtoul(buf, 10, &val);
658 if (ret)
659 return ret;
660
661 if (val) {
662 ret = coresight_enable(csdev);
663 if (ret)
664 return ret;
665 } else {
666 coresight_disable(csdev);
667 }
668
669 return size;
670}
671static DEVICE_ATTR_RW(enable_source);
672
673static struct attribute *coresight_sink_attrs[] = {
674 &dev_attr_enable_sink.attr,
675 NULL,
676};
677ATTRIBUTE_GROUPS(coresight_sink);
678
679static struct attribute *coresight_source_attrs[] = {
680 &dev_attr_enable_source.attr,
681 NULL,
682};
683ATTRIBUTE_GROUPS(coresight_source);
684
685static struct device_type coresight_dev_type[] = {
686 {
687 .name = "none",
688 },
689 {
690 .name = "sink",
691 .groups = coresight_sink_groups,
692 },
693 {
694 .name = "link",
695 },
696 {
697 .name = "linksink",
698 .groups = coresight_sink_groups,
699 },
700 {
701 .name = "source",
702 .groups = coresight_source_groups,
703 },
704};
705
706static void coresight_device_release(struct device *dev)
707{
708 struct coresight_device *csdev = to_coresight_device(dev);
709
Mathieu Poirierfae54152016-02-02 14:13:57 -0700710 kfree(csdev->conns);
711 kfree(csdev->refcnt);
Pratik Patela06ae862014-11-03 11:07:35 -0700712 kfree(csdev);
713}
714
715static int coresight_orphan_match(struct device *dev, void *data)
716{
717 int i;
718 bool still_orphan = false;
719 struct coresight_device *csdev, *i_csdev;
720 struct coresight_connection *conn;
721
722 csdev = data;
723 i_csdev = to_coresight_device(dev);
724
725 /* No need to check oneself */
726 if (csdev == i_csdev)
727 return 0;
728
729 /* Move on to another component if no connection is orphan */
730 if (!i_csdev->orphan)
731 return 0;
732 /*
733 * Circle throuch all the connection of that component. If we find
734 * an orphan connection whose name matches @csdev, link it.
735 */
Kaixu Xiad786a47d2015-01-26 09:22:20 -0700736 for (i = 0; i < i_csdev->nr_outport; i++) {
Pratik Patela06ae862014-11-03 11:07:35 -0700737 conn = &i_csdev->conns[i];
738
739 /* We have found at least one orphan connection */
740 if (conn->child_dev == NULL) {
741 /* Does it match this newly added device? */
Sudeep Hollab8392152016-08-25 15:18:51 -0600742 if (conn->child_name &&
743 !strcmp(dev_name(&csdev->dev), conn->child_name)) {
Pratik Patela06ae862014-11-03 11:07:35 -0700744 conn->child_dev = csdev;
Kaixu Xia22394bc2015-01-26 09:22:19 -0700745 } else {
746 /* This component still has an orphan */
747 still_orphan = true;
748 }
Pratik Patela06ae862014-11-03 11:07:35 -0700749 }
750 }
751
752 i_csdev->orphan = still_orphan;
753
754 /*
755 * Returning '0' ensures that all known component on the
756 * bus will be checked.
757 */
758 return 0;
759}
760
761static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
762{
763 /*
764 * No need to check for a return value as orphan connection(s)
765 * are hooked-up with each newly added component.
766 */
767 bus_for_each_dev(&coresight_bustype, NULL,
Mathieu Poirierff874222016-02-02 14:13:55 -0700768 csdev, coresight_orphan_match);
Pratik Patela06ae862014-11-03 11:07:35 -0700769}
770
771
772static int coresight_name_match(struct device *dev, void *data)
773{
774 char *to_match;
775 struct coresight_device *i_csdev;
776
777 to_match = data;
778 i_csdev = to_coresight_device(dev);
779
Mathieu Poirierfadf3a42015-12-17 08:47:02 -0700780 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
Pratik Patela06ae862014-11-03 11:07:35 -0700781 return 1;
782
783 return 0;
784}
785
786static void coresight_fixup_device_conns(struct coresight_device *csdev)
787{
788 int i;
789 struct device *dev = NULL;
790 struct coresight_connection *conn;
791
792 for (i = 0; i < csdev->nr_outport; i++) {
793 conn = &csdev->conns[i];
794 dev = bus_find_device(&coresight_bustype, NULL,
795 (void *)conn->child_name,
796 coresight_name_match);
797
798 if (dev) {
799 conn->child_dev = to_coresight_device(dev);
Mathieu Poirierf2dfab32016-02-02 14:13:58 -0700800 /* and put reference from 'bus_find_device()' */
801 put_device(dev);
Pratik Patela06ae862014-11-03 11:07:35 -0700802 } else {
803 csdev->orphan = true;
804 conn->child_dev = NULL;
805 }
806 }
807}
808
Mathieu Poirierad725ae2016-02-02 14:13:59 -0700809static int coresight_remove_match(struct device *dev, void *data)
810{
811 int i;
812 struct coresight_device *csdev, *iterator;
813 struct coresight_connection *conn;
814
815 csdev = data;
816 iterator = to_coresight_device(dev);
817
818 /* No need to check oneself */
819 if (csdev == iterator)
820 return 0;
821
822 /*
823 * Circle throuch all the connection of that component. If we find
824 * a connection whose name matches @csdev, remove it.
825 */
826 for (i = 0; i < iterator->nr_outport; i++) {
827 conn = &iterator->conns[i];
828
829 if (conn->child_dev == NULL)
830 continue;
831
832 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
833 iterator->orphan = true;
834 conn->child_dev = NULL;
835 /* No need to continue */
836 break;
837 }
838 }
839
840 /*
841 * Returning '0' ensures that all known component on the
842 * bus will be checked.
843 */
844 return 0;
845}
846
847static void coresight_remove_conns(struct coresight_device *csdev)
848{
849 bus_for_each_dev(&coresight_bustype, NULL,
850 csdev, coresight_remove_match);
851}
852
Pratik Patela06ae862014-11-03 11:07:35 -0700853/**
854 * coresight_timeout - loop until a bit has changed to a specific state.
855 * @addr: base address of the area of interest.
856 * @offset: address of a register, starting from @addr.
857 * @position: the position of the bit of interest.
858 * @value: the value the bit should have.
859 *
860 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
861 * TIMEOUT_US has elapsed, which ever happens first.
862 */
863
864int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
865{
866 int i;
867 u32 val;
868
869 for (i = TIMEOUT_US; i > 0; i--) {
870 val = __raw_readl(addr + offset);
871 /* waiting on the bit to go from 0 to 1 */
872 if (value) {
873 if (val & BIT(position))
874 return 0;
875 /* waiting on the bit to go from 1 to 0 */
876 } else {
877 if (!(val & BIT(position)))
878 return 0;
879 }
880
881 /*
882 * Delay is arbitrary - the specification doesn't say how long
883 * we are expected to wait. Extra check required to make sure
884 * we don't wait needlessly on the last iteration.
885 */
886 if (i - 1)
887 udelay(1);
888 }
889
890 return -EAGAIN;
891}
892
893struct bus_type coresight_bustype = {
894 .name = "coresight",
895};
896
897static int __init coresight_init(void)
898{
899 return bus_register(&coresight_bustype);
900}
901postcore_initcall(coresight_init);
902
903struct coresight_device *coresight_register(struct coresight_desc *desc)
904{
905 int i;
906 int ret;
907 int link_subtype;
908 int nr_refcnts = 1;
909 atomic_t *refcnts = NULL;
910 struct coresight_device *csdev;
Suzuki K Poulose068c0a52016-08-25 15:18:56 -0600911 struct coresight_connection *conns = NULL;
Pratik Patela06ae862014-11-03 11:07:35 -0700912
913 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
914 if (!csdev) {
915 ret = -ENOMEM;
916 goto err_kzalloc_csdev;
917 }
918
919 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
920 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
921 link_subtype = desc->subtype.link_subtype;
922
923 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
924 nr_refcnts = desc->pdata->nr_inport;
925 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
926 nr_refcnts = desc->pdata->nr_outport;
927 }
928
929 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
930 if (!refcnts) {
931 ret = -ENOMEM;
932 goto err_kzalloc_refcnts;
933 }
934
935 csdev->refcnt = refcnts;
936
937 csdev->nr_inport = desc->pdata->nr_inport;
938 csdev->nr_outport = desc->pdata->nr_outport;
Pratik Patela06ae862014-11-03 11:07:35 -0700939
Suzuki K Poulose068c0a52016-08-25 15:18:56 -0600940 /* Initialise connections if there is at least one outport */
941 if (csdev->nr_outport) {
942 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
943 if (!conns) {
944 ret = -ENOMEM;
945 goto err_kzalloc_conns;
946 }
947
948 for (i = 0; i < csdev->nr_outport; i++) {
949 conns[i].outport = desc->pdata->outports[i];
950 conns[i].child_name = desc->pdata->child_names[i];
951 conns[i].child_port = desc->pdata->child_ports[i];
952 }
Pratik Patela06ae862014-11-03 11:07:35 -0700953 }
954
955 csdev->conns = conns;
956
957 csdev->type = desc->type;
958 csdev->subtype = desc->subtype;
959 csdev->ops = desc->ops;
960 csdev->orphan = false;
961
962 csdev->dev.type = &coresight_dev_type[desc->type];
963 csdev->dev.groups = desc->groups;
964 csdev->dev.parent = desc->dev;
965 csdev->dev.release = coresight_device_release;
966 csdev->dev.bus = &coresight_bustype;
967 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
968
969 ret = device_register(&csdev->dev);
970 if (ret)
971 goto err_device_register;
972
973 mutex_lock(&coresight_mutex);
974
975 coresight_fixup_device_conns(csdev);
976 coresight_fixup_orphan_conns(csdev);
977
978 mutex_unlock(&coresight_mutex);
979
980 return csdev;
981
982err_device_register:
983 kfree(conns);
984err_kzalloc_conns:
985 kfree(refcnts);
986err_kzalloc_refcnts:
987 kfree(csdev);
988err_kzalloc_csdev:
989 return ERR_PTR(ret);
990}
991EXPORT_SYMBOL_GPL(coresight_register);
992
993void coresight_unregister(struct coresight_device *csdev)
994{
Mathieu Poirierad725ae2016-02-02 14:13:59 -0700995 /* Remove references of that device in the topology */
996 coresight_remove_conns(csdev);
Pratik Patela06ae862014-11-03 11:07:35 -0700997 device_unregister(&csdev->dev);
Pratik Patela06ae862014-11-03 11:07:35 -0700998}
999EXPORT_SYMBOL_GPL(coresight_unregister);