blob: cd41f401e8b2ede6a9dd7deca99efffc05c78659 [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 Poirierebb45d72016-11-29 09:47:14 -0700374static int coresight_enabled_sink(struct device *dev, void *data)
375{
376 bool *reset = data;
377 struct coresight_device *csdev = to_coresight_device(dev);
378
379 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
380 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
381 csdev->activated) {
382 /*
383 * Now that we have a handle on the sink for this session,
384 * disable the sysFS "enable_sink" flag so that possible
385 * concurrent perf session that wish to use another sink don't
386 * trip on it. Doing so has no ramification for the current
387 * session.
388 */
389 if (*reset)
390 csdev->activated = false;
391
392 return 1;
393 }
394
395 return 0;
396}
397
398/**
399 * coresight_get_enabled_sink - returns the first enabled sink found on the bus
400 * @deactivate: Whether the 'enable_sink' flag should be reset
401 *
402 * When operated from perf the deactivate parameter should be set to 'true'.
403 * That way the "enabled_sink" flag of the sink that was selected can be reset,
404 * allowing for other concurrent perf sessions to choose a different sink.
405 *
406 * When operated from sysFS users have full control and as such the deactivate
407 * parameter should be set to 'false', hence mandating users to explicitly
408 * clear the flag.
409 */
410struct coresight_device *coresight_get_enabled_sink(bool deactivate)
411{
412 struct device *dev = NULL;
413
414 dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
415 coresight_enabled_sink);
416
417 return dev ? to_coresight_device(dev) : NULL;
418}
419
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700420/**
421 * _coresight_build_path - recursively build a path from a @csdev to a sink.
422 * @csdev: The device to start from.
423 * @path: The list to add devices to.
424 *
425 * The tree of Coresight device is traversed until an activated sink is
426 * found. From there the sink is added to the list along with all the
427 * devices that led to that point - the end result is a list from source
428 * to sink. In that list the source is the first device and the sink the
429 * last one.
430 */
431static int _coresight_build_path(struct coresight_device *csdev,
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700432 struct coresight_device *sink,
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700433 struct list_head *path)
Pratik Patela06ae862014-11-03 11:07:35 -0700434{
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700435 int i;
436 bool found = false;
437 struct coresight_node *node;
Pratik Patela06ae862014-11-03 11:07:35 -0700438
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700439 /* An activated sink has been found. Enqueue the element */
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700440 if (csdev == sink)
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700441 goto out;
442
443 /* Not a sink - recursively explore each port found on this element */
444 for (i = 0; i < csdev->nr_outport; i++) {
Suzuki K Pouloseec48a1d2016-06-14 11:17:12 -0600445 struct coresight_device *child_dev = csdev->conns[i].child_dev;
446
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700447 if (child_dev &&
448 _coresight_build_path(child_dev, sink, path) == 0) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700449 found = true;
450 break;
Pratik Patela06ae862014-11-03 11:07:35 -0700451 }
452 }
453
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700454 if (!found)
455 return -ENODEV;
Pratik Patela06ae862014-11-03 11:07:35 -0700456
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700457out:
458 /*
459 * A path from this element to a sink has been found. The elements
460 * leading to the sink are already enqueued, all that is left to do
Mathieu Poirier5da53252016-02-17 17:51:47 -0700461 * is tell the PM runtime core we need this element and add a node
462 * for it.
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700463 */
464 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
465 if (!node)
466 return -ENOMEM;
Pratik Patela06ae862014-11-03 11:07:35 -0700467
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700468 node->csdev = csdev;
469 list_add(&node->link, path);
Mathieu Poirier5da53252016-02-17 17:51:47 -0700470 pm_runtime_get_sync(csdev->dev.parent);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700471
472 return 0;
473}
474
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700475struct list_head *coresight_build_path(struct coresight_device *source,
476 struct coresight_device *sink)
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700477{
478 struct list_head *path;
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100479 int rc;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700480
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700481 if (!sink)
482 return ERR_PTR(-EINVAL);
483
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700484 path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
485 if (!path)
Mathieu Poirier8e67cdb2016-09-08 16:50:38 -0600486 return ERR_PTR(-ENOMEM);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700487
488 INIT_LIST_HEAD(path);
489
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700490 rc = _coresight_build_path(source, sink, path);
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100491 if (rc) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700492 kfree(path);
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100493 return ERR_PTR(rc);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700494 }
495
496 return path;
497}
498
499/**
500 * coresight_release_path - release a previously built path.
501 * @path: the path to release.
502 *
503 * Go through all the elements of a path and 1) removed it from the list and
504 * 2) free the memory allocated for each node.
505 */
506void coresight_release_path(struct list_head *path)
507{
Mathieu Poirier5da53252016-02-17 17:51:47 -0700508 struct coresight_device *csdev;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700509 struct coresight_node *nd, *next;
510
511 list_for_each_entry_safe(nd, next, path, link) {
Mathieu Poirier5da53252016-02-17 17:51:47 -0700512 csdev = nd->csdev;
513
514 pm_runtime_put_sync(csdev->dev.parent);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700515 list_del(&nd->link);
516 kfree(nd);
517 }
518
519 kfree(path);
520 path = NULL;
Pratik Patela06ae862014-11-03 11:07:35 -0700521}
522
Mathieu Poiriera685d682016-05-03 11:33:38 -0600523/** coresight_validate_source - make sure a source has the right credentials
524 * @csdev: the device structure for a source.
525 * @function: the function this was called from.
526 *
527 * Assumes the coresight_mutex is held.
528 */
529static int coresight_validate_source(struct coresight_device *csdev,
530 const char *function)
531{
532 u32 type, subtype;
533
534 type = csdev->type;
535 subtype = csdev->subtype.source_subtype;
536
537 if (type != CORESIGHT_DEV_TYPE_SOURCE) {
538 dev_err(&csdev->dev, "wrong device type in %s\n", function);
539 return -EINVAL;
540 }
541
542 if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
543 subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
544 dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
545 return -EINVAL;
546 }
547
548 return 0;
549}
550
Pratik Patela06ae862014-11-03 11:07:35 -0700551int coresight_enable(struct coresight_device *csdev)
552{
Mathieu Poiriera685d682016-05-03 11:33:38 -0600553 int cpu, ret = 0;
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700554 struct coresight_device *sink;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700555 struct list_head *path;
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600556 enum coresight_dev_subtype_source subtype;
557
558 subtype = csdev->subtype.source_subtype;
Pratik Patela06ae862014-11-03 11:07:35 -0700559
560 mutex_lock(&coresight_mutex);
Mathieu Poiriera685d682016-05-03 11:33:38 -0600561
562 ret = coresight_validate_source(csdev, __func__);
563 if (ret)
Pratik Patela06ae862014-11-03 11:07:35 -0700564 goto out;
Mathieu Poiriera685d682016-05-03 11:33:38 -0600565
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600566 if (csdev->enable) {
567 /*
568 * There could be multiple applications driving the software
569 * source. So keep the refcount for each such user when the
570 * source is already enabled.
571 */
572 if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
573 atomic_inc(csdev->refcnt);
Pratik Patela06ae862014-11-03 11:07:35 -0700574 goto out;
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600575 }
Pratik Patela06ae862014-11-03 11:07:35 -0700576
Mathieu Poirierebb45d72016-11-29 09:47:14 -0700577 /*
578 * Search for a valid sink for this session but don't reset the
579 * "enable_sink" flag in sysFS. Users get to do that explicitly.
580 */
581 sink = coresight_get_enabled_sink(false);
582 if (!sink) {
583 ret = -EINVAL;
584 goto out;
585 }
586
587 path = coresight_build_path(csdev, sink);
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100588 if (IS_ERR(path)) {
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700589 pr_err("building path(s) failed\n");
Suzuki K Poulose5014e902016-05-06 15:35:50 +0100590 ret = PTR_ERR(path);
Pratik Patela06ae862014-11-03 11:07:35 -0700591 goto out;
592 }
593
Mathieu Poiriere827d452016-02-17 17:51:59 -0700594 ret = coresight_enable_path(path, CS_MODE_SYSFS);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700595 if (ret)
596 goto err_path;
597
Mathieu Poirier22fd5322016-02-17 17:51:52 -0700598 ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700599 if (ret)
600 goto err_source;
601
Suzuki K Poulose41830b82017-06-05 14:15:03 -0600602 switch (subtype) {
Mathieu Poiriera685d682016-05-03 11:33:38 -0600603 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
604 /*
605 * When working from sysFS it is important to keep track
606 * of the paths that were created so that they can be
607 * undone in 'coresight_disable()'. Since there can only
608 * be a single session per tracer (when working from sysFS)
609 * a per-cpu variable will do just fine.
610 */
611 cpu = source_ops(csdev)->cpu_id(csdev);
612 per_cpu(tracer_path, cpu) = path;
613 break;
614 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
615 stm_path = path;
616 break;
617 default:
618 /* We can't be here */
619 break;
620 }
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700621
Pratik Patela06ae862014-11-03 11:07:35 -0700622out:
623 mutex_unlock(&coresight_mutex);
624 return ret;
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700625
626err_source:
627 coresight_disable_path(path);
628
629err_path:
630 coresight_release_path(path);
631 goto out;
Pratik Patela06ae862014-11-03 11:07:35 -0700632}
633EXPORT_SYMBOL_GPL(coresight_enable);
634
635void coresight_disable(struct coresight_device *csdev)
636{
Mathieu Poiriera685d682016-05-03 11:33:38 -0600637 int cpu, ret;
638 struct list_head *path = NULL;
Pratik Patela06ae862014-11-03 11:07:35 -0700639
640 mutex_lock(&coresight_mutex);
Mathieu Poiriera685d682016-05-03 11:33:38 -0600641
642 ret = coresight_validate_source(csdev, __func__);
643 if (ret)
Pratik Patela06ae862014-11-03 11:07:35 -0700644 goto out;
Mathieu Poiriera685d682016-05-03 11:33:38 -0600645
Pratik Patela06ae862014-11-03 11:07:35 -0700646 if (!csdev->enable)
647 goto out;
648
Mathieu Poiriera685d682016-05-03 11:33:38 -0600649 switch (csdev->subtype.source_subtype) {
650 case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
651 cpu = source_ops(csdev)->cpu_id(csdev);
652 path = per_cpu(tracer_path, cpu);
653 per_cpu(tracer_path, cpu) = NULL;
654 break;
655 case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
656 path = stm_path;
657 stm_path = NULL;
658 break;
659 default:
660 /* We can't be here */
661 break;
662 }
663
Pratik Patela06ae862014-11-03 11:07:35 -0700664 coresight_disable_source(csdev);
Mathieu Poirierb3e94402016-02-17 17:51:45 -0700665 coresight_disable_path(path);
666 coresight_release_path(path);
Pratik Patela06ae862014-11-03 11:07:35 -0700667
668out:
669 mutex_unlock(&coresight_mutex);
670}
671EXPORT_SYMBOL_GPL(coresight_disable);
672
673static ssize_t enable_sink_show(struct device *dev,
674 struct device_attribute *attr, char *buf)
675{
676 struct coresight_device *csdev = to_coresight_device(dev);
677
Li Pengchenge8dc27d2016-05-03 11:33:35 -0600678 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
Pratik Patela06ae862014-11-03 11:07:35 -0700679}
680
681static ssize_t enable_sink_store(struct device *dev,
682 struct device_attribute *attr,
683 const char *buf, size_t size)
684{
685 int ret;
686 unsigned long val;
687 struct coresight_device *csdev = to_coresight_device(dev);
688
689 ret = kstrtoul(buf, 10, &val);
690 if (ret)
691 return ret;
692
693 if (val)
694 csdev->activated = true;
695 else
696 csdev->activated = false;
697
698 return size;
699
700}
701static DEVICE_ATTR_RW(enable_sink);
702
703static ssize_t enable_source_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
705{
706 struct coresight_device *csdev = to_coresight_device(dev);
707
Li Pengchenge8dc27d2016-05-03 11:33:35 -0600708 return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
Pratik Patela06ae862014-11-03 11:07:35 -0700709}
710
711static ssize_t enable_source_store(struct device *dev,
712 struct device_attribute *attr,
713 const char *buf, size_t size)
714{
715 int ret = 0;
716 unsigned long val;
717 struct coresight_device *csdev = to_coresight_device(dev);
718
719 ret = kstrtoul(buf, 10, &val);
720 if (ret)
721 return ret;
722
723 if (val) {
724 ret = coresight_enable(csdev);
725 if (ret)
726 return ret;
727 } else {
728 coresight_disable(csdev);
729 }
730
731 return size;
732}
733static DEVICE_ATTR_RW(enable_source);
734
735static struct attribute *coresight_sink_attrs[] = {
736 &dev_attr_enable_sink.attr,
737 NULL,
738};
739ATTRIBUTE_GROUPS(coresight_sink);
740
741static struct attribute *coresight_source_attrs[] = {
742 &dev_attr_enable_source.attr,
743 NULL,
744};
745ATTRIBUTE_GROUPS(coresight_source);
746
747static struct device_type coresight_dev_type[] = {
748 {
749 .name = "none",
750 },
751 {
752 .name = "sink",
753 .groups = coresight_sink_groups,
754 },
755 {
756 .name = "link",
757 },
758 {
759 .name = "linksink",
760 .groups = coresight_sink_groups,
761 },
762 {
763 .name = "source",
764 .groups = coresight_source_groups,
765 },
766};
767
768static void coresight_device_release(struct device *dev)
769{
770 struct coresight_device *csdev = to_coresight_device(dev);
771
Mathieu Poirierfae54152016-02-02 14:13:57 -0700772 kfree(csdev->conns);
773 kfree(csdev->refcnt);
Pratik Patela06ae862014-11-03 11:07:35 -0700774 kfree(csdev);
775}
776
777static int coresight_orphan_match(struct device *dev, void *data)
778{
779 int i;
780 bool still_orphan = false;
781 struct coresight_device *csdev, *i_csdev;
782 struct coresight_connection *conn;
783
784 csdev = data;
785 i_csdev = to_coresight_device(dev);
786
787 /* No need to check oneself */
788 if (csdev == i_csdev)
789 return 0;
790
791 /* Move on to another component if no connection is orphan */
792 if (!i_csdev->orphan)
793 return 0;
794 /*
795 * Circle throuch all the connection of that component. If we find
796 * an orphan connection whose name matches @csdev, link it.
797 */
Kaixu Xiad786a47d2015-01-26 09:22:20 -0700798 for (i = 0; i < i_csdev->nr_outport; i++) {
Pratik Patela06ae862014-11-03 11:07:35 -0700799 conn = &i_csdev->conns[i];
800
801 /* We have found at least one orphan connection */
802 if (conn->child_dev == NULL) {
803 /* Does it match this newly added device? */
Sudeep Hollab8392152016-08-25 15:18:51 -0600804 if (conn->child_name &&
805 !strcmp(dev_name(&csdev->dev), conn->child_name)) {
Pratik Patela06ae862014-11-03 11:07:35 -0700806 conn->child_dev = csdev;
Kaixu Xia22394bc2015-01-26 09:22:19 -0700807 } else {
808 /* This component still has an orphan */
809 still_orphan = true;
810 }
Pratik Patela06ae862014-11-03 11:07:35 -0700811 }
812 }
813
814 i_csdev->orphan = still_orphan;
815
816 /*
817 * Returning '0' ensures that all known component on the
818 * bus will be checked.
819 */
820 return 0;
821}
822
823static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
824{
825 /*
826 * No need to check for a return value as orphan connection(s)
827 * are hooked-up with each newly added component.
828 */
829 bus_for_each_dev(&coresight_bustype, NULL,
Mathieu Poirierff874222016-02-02 14:13:55 -0700830 csdev, coresight_orphan_match);
Pratik Patela06ae862014-11-03 11:07:35 -0700831}
832
833
834static int coresight_name_match(struct device *dev, void *data)
835{
836 char *to_match;
837 struct coresight_device *i_csdev;
838
839 to_match = data;
840 i_csdev = to_coresight_device(dev);
841
Mathieu Poirierfadf3a42015-12-17 08:47:02 -0700842 if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
Pratik Patela06ae862014-11-03 11:07:35 -0700843 return 1;
844
845 return 0;
846}
847
848static void coresight_fixup_device_conns(struct coresight_device *csdev)
849{
850 int i;
851 struct device *dev = NULL;
852 struct coresight_connection *conn;
853
854 for (i = 0; i < csdev->nr_outport; i++) {
855 conn = &csdev->conns[i];
856 dev = bus_find_device(&coresight_bustype, NULL,
857 (void *)conn->child_name,
858 coresight_name_match);
859
860 if (dev) {
861 conn->child_dev = to_coresight_device(dev);
Mathieu Poirierf2dfab32016-02-02 14:13:58 -0700862 /* and put reference from 'bus_find_device()' */
863 put_device(dev);
Pratik Patela06ae862014-11-03 11:07:35 -0700864 } else {
865 csdev->orphan = true;
866 conn->child_dev = NULL;
867 }
868 }
869}
870
Mathieu Poirierad725ae2016-02-02 14:13:59 -0700871static int coresight_remove_match(struct device *dev, void *data)
872{
873 int i;
874 struct coresight_device *csdev, *iterator;
875 struct coresight_connection *conn;
876
877 csdev = data;
878 iterator = to_coresight_device(dev);
879
880 /* No need to check oneself */
881 if (csdev == iterator)
882 return 0;
883
884 /*
885 * Circle throuch all the connection of that component. If we find
886 * a connection whose name matches @csdev, remove it.
887 */
888 for (i = 0; i < iterator->nr_outport; i++) {
889 conn = &iterator->conns[i];
890
891 if (conn->child_dev == NULL)
892 continue;
893
894 if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
895 iterator->orphan = true;
896 conn->child_dev = NULL;
897 /* No need to continue */
898 break;
899 }
900 }
901
902 /*
903 * Returning '0' ensures that all known component on the
904 * bus will be checked.
905 */
906 return 0;
907}
908
909static void coresight_remove_conns(struct coresight_device *csdev)
910{
911 bus_for_each_dev(&coresight_bustype, NULL,
912 csdev, coresight_remove_match);
913}
914
Pratik Patela06ae862014-11-03 11:07:35 -0700915/**
916 * coresight_timeout - loop until a bit has changed to a specific state.
917 * @addr: base address of the area of interest.
918 * @offset: address of a register, starting from @addr.
919 * @position: the position of the bit of interest.
920 * @value: the value the bit should have.
921 *
922 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
923 * TIMEOUT_US has elapsed, which ever happens first.
924 */
925
926int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
927{
928 int i;
929 u32 val;
930
931 for (i = TIMEOUT_US; i > 0; i--) {
932 val = __raw_readl(addr + offset);
933 /* waiting on the bit to go from 0 to 1 */
934 if (value) {
935 if (val & BIT(position))
936 return 0;
937 /* waiting on the bit to go from 1 to 0 */
938 } else {
939 if (!(val & BIT(position)))
940 return 0;
941 }
942
943 /*
944 * Delay is arbitrary - the specification doesn't say how long
945 * we are expected to wait. Extra check required to make sure
946 * we don't wait needlessly on the last iteration.
947 */
948 if (i - 1)
949 udelay(1);
950 }
951
952 return -EAGAIN;
953}
954
955struct bus_type coresight_bustype = {
956 .name = "coresight",
957};
958
959static int __init coresight_init(void)
960{
961 return bus_register(&coresight_bustype);
962}
963postcore_initcall(coresight_init);
964
965struct coresight_device *coresight_register(struct coresight_desc *desc)
966{
967 int i;
968 int ret;
969 int link_subtype;
970 int nr_refcnts = 1;
971 atomic_t *refcnts = NULL;
972 struct coresight_device *csdev;
Suzuki K Poulose068c0a52016-08-25 15:18:56 -0600973 struct coresight_connection *conns = NULL;
Pratik Patela06ae862014-11-03 11:07:35 -0700974
975 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
976 if (!csdev) {
977 ret = -ENOMEM;
978 goto err_kzalloc_csdev;
979 }
980
981 if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
982 desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
983 link_subtype = desc->subtype.link_subtype;
984
985 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
986 nr_refcnts = desc->pdata->nr_inport;
987 else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
988 nr_refcnts = desc->pdata->nr_outport;
989 }
990
991 refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
992 if (!refcnts) {
993 ret = -ENOMEM;
994 goto err_kzalloc_refcnts;
995 }
996
997 csdev->refcnt = refcnts;
998
999 csdev->nr_inport = desc->pdata->nr_inport;
1000 csdev->nr_outport = desc->pdata->nr_outport;
Pratik Patela06ae862014-11-03 11:07:35 -07001001
Suzuki K Poulose068c0a52016-08-25 15:18:56 -06001002 /* Initialise connections if there is at least one outport */
1003 if (csdev->nr_outport) {
1004 conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
1005 if (!conns) {
1006 ret = -ENOMEM;
1007 goto err_kzalloc_conns;
1008 }
1009
1010 for (i = 0; i < csdev->nr_outport; i++) {
1011 conns[i].outport = desc->pdata->outports[i];
1012 conns[i].child_name = desc->pdata->child_names[i];
1013 conns[i].child_port = desc->pdata->child_ports[i];
1014 }
Pratik Patela06ae862014-11-03 11:07:35 -07001015 }
1016
1017 csdev->conns = conns;
1018
1019 csdev->type = desc->type;
1020 csdev->subtype = desc->subtype;
1021 csdev->ops = desc->ops;
1022 csdev->orphan = false;
1023
1024 csdev->dev.type = &coresight_dev_type[desc->type];
1025 csdev->dev.groups = desc->groups;
1026 csdev->dev.parent = desc->dev;
1027 csdev->dev.release = coresight_device_release;
1028 csdev->dev.bus = &coresight_bustype;
1029 dev_set_name(&csdev->dev, "%s", desc->pdata->name);
1030
1031 ret = device_register(&csdev->dev);
1032 if (ret)
1033 goto err_device_register;
1034
1035 mutex_lock(&coresight_mutex);
1036
1037 coresight_fixup_device_conns(csdev);
1038 coresight_fixup_orphan_conns(csdev);
1039
1040 mutex_unlock(&coresight_mutex);
1041
1042 return csdev;
1043
1044err_device_register:
1045 kfree(conns);
1046err_kzalloc_conns:
1047 kfree(refcnts);
1048err_kzalloc_refcnts:
1049 kfree(csdev);
1050err_kzalloc_csdev:
1051 return ERR_PTR(ret);
1052}
1053EXPORT_SYMBOL_GPL(coresight_register);
1054
1055void coresight_unregister(struct coresight_device *csdev)
1056{
Mathieu Poirierad725ae2016-02-02 14:13:59 -07001057 /* Remove references of that device in the topology */
1058 coresight_remove_conns(csdev);
Pratik Patela06ae862014-11-03 11:07:35 -07001059 device_unregister(&csdev->dev);
Pratik Patela06ae862014-11-03 11:07:35 -07001060}
1061EXPORT_SYMBOL_GPL(coresight_unregister);