]>
Commit | Line | Data |
---|---|---|
a06ae860 PP |
1 | /* 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> | |
a06ae860 PP |
14 | #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> | |
5da5325f | 26 | #include <linux/pm_runtime.h> |
a06ae860 PP |
27 | |
28 | #include "coresight-priv.h" | |
29 | ||
30 | static DEFINE_MUTEX(coresight_mutex); | |
31 | ||
b3e94405 MP |
32 | /** |
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 | */ | |
37 | struct 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 | */ | |
a685d683 MP |
46 | static 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 | */ | |
54 | static struct list_head *stm_path; | |
b3e94405 | 55 | |
a06ae860 PP |
56 | static 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 | ||
83 | static 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 | ||
b3e94405 MP |
95 | static int coresight_find_link_inport(struct coresight_device *csdev, |
96 | struct coresight_device *parent) | |
a06ae860 PP |
97 | { |
98 | int i; | |
a06ae860 PP |
99 | struct coresight_connection *conn; |
100 | ||
a06ae860 PP |
101 | 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 | ||
110 | return 0; | |
111 | } | |
112 | ||
b3e94405 MP |
113 | static int coresight_find_link_outport(struct coresight_device *csdev, |
114 | struct coresight_device *child) | |
a06ae860 PP |
115 | { |
116 | int i; | |
a06ae860 PP |
117 | struct coresight_connection *conn; |
118 | ||
a06ae860 PP |
119 | 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 | ||
128 | return 0; | |
129 | } | |
130 | ||
e827d455 | 131 | static int coresight_enable_sink(struct coresight_device *csdev, u32 mode) |
a06ae860 PP |
132 | { |
133 | int ret; | |
134 | ||
135 | if (!csdev->enable) { | |
136 | if (sink_ops(csdev)->enable) { | |
e827d455 | 137 | ret = sink_ops(csdev)->enable(csdev, mode); |
a06ae860 PP |
138 | if (ret) |
139 | return ret; | |
140 | } | |
141 | csdev->enable = true; | |
142 | } | |
143 | ||
144 | atomic_inc(csdev->refcnt); | |
145 | ||
146 | return 0; | |
147 | } | |
148 | ||
149 | static 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 | ||
b3e94405 MP |
159 | static int coresight_enable_link(struct coresight_device *csdev, |
160 | struct coresight_device *parent, | |
161 | struct coresight_device *child) | |
a06ae860 PP |
162 | { |
163 | int ret; | |
164 | int link_subtype; | |
165 | int refport, inport, outport; | |
166 | ||
b3e94405 MP |
167 | if (!parent || !child) |
168 | return -EINVAL; | |
169 | ||
170 | inport = coresight_find_link_inport(csdev, parent); | |
171 | outport = coresight_find_link_outport(csdev, child); | |
a06ae860 PP |
172 | 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 | ||
181 | if (atomic_inc_return(&csdev->refcnt[refport]) == 1) { | |
182 | if (link_ops(csdev)->enable) { | |
183 | ret = link_ops(csdev)->enable(csdev, inport, outport); | |
184 | if (ret) | |
185 | return ret; | |
186 | } | |
187 | } | |
188 | ||
189 | csdev->enable = true; | |
190 | ||
191 | return 0; | |
192 | } | |
193 | ||
b3e94405 MP |
194 | static void coresight_disable_link(struct coresight_device *csdev, |
195 | struct coresight_device *parent, | |
196 | struct coresight_device *child) | |
a06ae860 PP |
197 | { |
198 | int i, nr_conns; | |
199 | int link_subtype; | |
200 | int refport, inport, outport; | |
201 | ||
b3e94405 MP |
202 | if (!parent || !child) |
203 | return; | |
204 | ||
205 | inport = coresight_find_link_inport(csdev, parent); | |
206 | outport = coresight_find_link_outport(csdev, child); | |
a06ae860 PP |
207 | link_subtype = csdev->subtype.link_subtype; |
208 | ||
209 | if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) { | |
210 | refport = inport; | |
211 | nr_conns = csdev->nr_inport; | |
212 | } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) { | |
213 | refport = outport; | |
214 | nr_conns = csdev->nr_outport; | |
215 | } else { | |
216 | refport = 0; | |
217 | nr_conns = 1; | |
218 | } | |
219 | ||
220 | if (atomic_dec_return(&csdev->refcnt[refport]) == 0) { | |
221 | if (link_ops(csdev)->disable) | |
222 | link_ops(csdev)->disable(csdev, inport, outport); | |
223 | } | |
224 | ||
225 | for (i = 0; i < nr_conns; i++) | |
226 | if (atomic_read(&csdev->refcnt[i]) != 0) | |
227 | return; | |
228 | ||
229 | csdev->enable = false; | |
230 | } | |
231 | ||
22fd532e | 232 | static int coresight_enable_source(struct coresight_device *csdev, u32 mode) |
a06ae860 PP |
233 | { |
234 | int ret; | |
235 | ||
236 | if (!coresight_source_is_unique(csdev)) { | |
237 | dev_warn(&csdev->dev, "traceID %d not unique\n", | |
238 | source_ops(csdev)->trace_id(csdev)); | |
239 | return -EINVAL; | |
240 | } | |
241 | ||
242 | if (!csdev->enable) { | |
243 | if (source_ops(csdev)->enable) { | |
882d5e11 | 244 | ret = source_ops(csdev)->enable(csdev, NULL, mode); |
a06ae860 PP |
245 | if (ret) |
246 | return ret; | |
247 | } | |
248 | csdev->enable = true; | |
249 | } | |
250 | ||
251 | atomic_inc(csdev->refcnt); | |
252 | ||
253 | return 0; | |
254 | } | |
255 | ||
256 | static void coresight_disable_source(struct coresight_device *csdev) | |
257 | { | |
258 | if (atomic_dec_return(csdev->refcnt) == 0) { | |
259 | if (source_ops(csdev)->disable) { | |
260 | source_ops(csdev)->disable(csdev); | |
261 | csdev->enable = false; | |
262 | } | |
263 | } | |
264 | } | |
265 | ||
b3e94405 | 266 | void coresight_disable_path(struct list_head *path) |
a06ae860 | 267 | { |
b3e94405 MP |
268 | struct coresight_node *nd; |
269 | struct coresight_device *csdev, *parent, *child; | |
270 | ||
271 | list_for_each_entry(nd, path, link) { | |
272 | csdev = nd->csdev; | |
273 | ||
274 | switch (csdev->type) { | |
275 | case CORESIGHT_DEV_TYPE_SINK: | |
276 | case CORESIGHT_DEV_TYPE_LINKSINK: | |
277 | coresight_disable_sink(csdev); | |
278 | break; | |
279 | case CORESIGHT_DEV_TYPE_SOURCE: | |
280 | /* sources are disabled from either sysFS or Perf */ | |
281 | break; | |
282 | case CORESIGHT_DEV_TYPE_LINK: | |
283 | parent = list_prev_entry(nd, link)->csdev; | |
284 | child = list_next_entry(nd, link)->csdev; | |
285 | coresight_disable_link(csdev, parent, child); | |
286 | break; | |
287 | default: | |
288 | break; | |
a06ae860 | 289 | } |
a06ae860 | 290 | } |
b3e94405 | 291 | } |
a06ae860 | 292 | |
e827d455 | 293 | int coresight_enable_path(struct list_head *path, u32 mode) |
b3e94405 MP |
294 | { |
295 | ||
296 | int ret = 0; | |
297 | struct coresight_node *nd; | |
298 | struct coresight_device *csdev, *parent, *child; | |
299 | ||
300 | list_for_each_entry_reverse(nd, path, link) { | |
301 | csdev = nd->csdev; | |
302 | ||
303 | switch (csdev->type) { | |
304 | case CORESIGHT_DEV_TYPE_SINK: | |
305 | case CORESIGHT_DEV_TYPE_LINKSINK: | |
e827d455 | 306 | ret = coresight_enable_sink(csdev, mode); |
b3e94405 MP |
307 | if (ret) |
308 | goto err; | |
309 | break; | |
310 | case CORESIGHT_DEV_TYPE_SOURCE: | |
311 | /* sources are enabled from either sysFS or Perf */ | |
312 | break; | |
313 | case CORESIGHT_DEV_TYPE_LINK: | |
314 | parent = list_prev_entry(nd, link)->csdev; | |
315 | child = list_next_entry(nd, link)->csdev; | |
316 | ret = coresight_enable_link(csdev, parent, child); | |
317 | if (ret) | |
318 | goto err; | |
319 | break; | |
320 | default: | |
321 | goto err; | |
a06ae860 PP |
322 | } |
323 | } | |
324 | ||
b3e94405 | 325 | out: |
a06ae860 | 326 | return ret; |
b3e94405 MP |
327 | err: |
328 | coresight_disable_path(path); | |
329 | goto out; | |
a06ae860 PP |
330 | } |
331 | ||
b6404e21 MP |
332 | struct coresight_device *coresight_get_sink(struct list_head *path) |
333 | { | |
334 | struct coresight_device *csdev; | |
335 | ||
336 | if (!path) | |
337 | return NULL; | |
338 | ||
339 | csdev = list_last_entry(path, struct coresight_node, link)->csdev; | |
340 | if (csdev->type != CORESIGHT_DEV_TYPE_SINK && | |
341 | csdev->type != CORESIGHT_DEV_TYPE_LINKSINK) | |
342 | return NULL; | |
343 | ||
344 | return csdev; | |
345 | } | |
346 | ||
b3e94405 MP |
347 | /** |
348 | * _coresight_build_path - recursively build a path from a @csdev to a sink. | |
349 | * @csdev: The device to start from. | |
350 | * @path: The list to add devices to. | |
351 | * | |
352 | * The tree of Coresight device is traversed until an activated sink is | |
353 | * found. From there the sink is added to the list along with all the | |
354 | * devices that led to that point - the end result is a list from source | |
355 | * to sink. In that list the source is the first device and the sink the | |
356 | * last one. | |
357 | */ | |
358 | static int _coresight_build_path(struct coresight_device *csdev, | |
359 | struct list_head *path) | |
a06ae860 | 360 | { |
b3e94405 MP |
361 | int i; |
362 | bool found = false; | |
363 | struct coresight_node *node; | |
364 | struct coresight_connection *conn; | |
365 | ||
366 | /* An activated sink has been found. Enqueue the element */ | |
367 | if ((csdev->type == CORESIGHT_DEV_TYPE_SINK || | |
368 | csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated) | |
369 | goto out; | |
370 | ||
371 | /* Not a sink - recursively explore each port found on this element */ | |
372 | for (i = 0; i < csdev->nr_outport; i++) { | |
373 | conn = &csdev->conns[i]; | |
374 | if (_coresight_build_path(conn->child_dev, path) == 0) { | |
375 | found = true; | |
376 | break; | |
a06ae860 PP |
377 | } |
378 | } | |
379 | ||
b3e94405 MP |
380 | if (!found) |
381 | return -ENODEV; | |
382 | ||
383 | out: | |
384 | /* | |
385 | * A path from this element to a sink has been found. The elements | |
386 | * leading to the sink are already enqueued, all that is left to do | |
5da5325f MP |
387 | * is tell the PM runtime core we need this element and add a node |
388 | * for it. | |
b3e94405 MP |
389 | */ |
390 | node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL); | |
391 | if (!node) | |
392 | return -ENOMEM; | |
393 | ||
394 | node->csdev = csdev; | |
395 | list_add(&node->link, path); | |
5da5325f | 396 | pm_runtime_get_sync(csdev->dev.parent); |
b3e94405 | 397 | |
a06ae860 PP |
398 | return 0; |
399 | } | |
400 | ||
b3e94405 | 401 | struct list_head *coresight_build_path(struct coresight_device *csdev) |
a06ae860 | 402 | { |
b3e94405 | 403 | struct list_head *path; |
a06ae860 | 404 | |
b3e94405 MP |
405 | path = kzalloc(sizeof(struct list_head), GFP_KERNEL); |
406 | if (!path) | |
407 | return NULL; | |
a06ae860 | 408 | |
b3e94405 MP |
409 | INIT_LIST_HEAD(path); |
410 | ||
411 | if (_coresight_build_path(csdev, path)) { | |
412 | kfree(path); | |
413 | path = NULL; | |
a06ae860 PP |
414 | } |
415 | ||
b3e94405 MP |
416 | return path; |
417 | } | |
a06ae860 | 418 | |
b3e94405 MP |
419 | /** |
420 | * coresight_release_path - release a previously built path. | |
421 | * @path: the path to release. | |
422 | * | |
423 | * Go through all the elements of a path and 1) removed it from the list and | |
424 | * 2) free the memory allocated for each node. | |
425 | */ | |
426 | void coresight_release_path(struct list_head *path) | |
427 | { | |
5da5325f | 428 | struct coresight_device *csdev; |
b3e94405 | 429 | struct coresight_node *nd, *next; |
a06ae860 | 430 | |
b3e94405 | 431 | list_for_each_entry_safe(nd, next, path, link) { |
5da5325f MP |
432 | csdev = nd->csdev; |
433 | ||
434 | pm_runtime_put_sync(csdev->dev.parent); | |
b3e94405 MP |
435 | list_del(&nd->link); |
436 | kfree(nd); | |
437 | } | |
438 | ||
439 | kfree(path); | |
440 | path = NULL; | |
a06ae860 PP |
441 | } |
442 | ||
a685d683 MP |
443 | /** coresight_validate_source - make sure a source has the right credentials |
444 | * @csdev: the device structure for a source. | |
445 | * @function: the function this was called from. | |
446 | * | |
447 | * Assumes the coresight_mutex is held. | |
448 | */ | |
449 | static int coresight_validate_source(struct coresight_device *csdev, | |
450 | const char *function) | |
451 | { | |
452 | u32 type, subtype; | |
453 | ||
454 | type = csdev->type; | |
455 | subtype = csdev->subtype.source_subtype; | |
456 | ||
457 | if (type != CORESIGHT_DEV_TYPE_SOURCE) { | |
458 | dev_err(&csdev->dev, "wrong device type in %s\n", function); | |
459 | return -EINVAL; | |
460 | } | |
461 | ||
462 | if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC && | |
463 | subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) { | |
464 | dev_err(&csdev->dev, "wrong device subtype in %s\n", function); | |
465 | return -EINVAL; | |
466 | } | |
467 | ||
468 | return 0; | |
469 | } | |
470 | ||
a06ae860 PP |
471 | int coresight_enable(struct coresight_device *csdev) |
472 | { | |
a685d683 | 473 | int cpu, ret = 0; |
b3e94405 | 474 | struct list_head *path; |
a06ae860 PP |
475 | |
476 | mutex_lock(&coresight_mutex); | |
a685d683 MP |
477 | |
478 | ret = coresight_validate_source(csdev, __func__); | |
479 | if (ret) | |
a06ae860 | 480 | goto out; |
a685d683 | 481 | |
a06ae860 PP |
482 | if (csdev->enable) |
483 | goto out; | |
484 | ||
b3e94405 MP |
485 | path = coresight_build_path(csdev); |
486 | if (!path) { | |
487 | pr_err("building path(s) failed\n"); | |
a06ae860 PP |
488 | goto out; |
489 | } | |
490 | ||
e827d455 | 491 | ret = coresight_enable_path(path, CS_MODE_SYSFS); |
b3e94405 MP |
492 | if (ret) |
493 | goto err_path; | |
494 | ||
22fd532e | 495 | ret = coresight_enable_source(csdev, CS_MODE_SYSFS); |
b3e94405 MP |
496 | if (ret) |
497 | goto err_source; | |
498 | ||
a685d683 MP |
499 | switch (csdev->subtype.source_subtype) { |
500 | case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC: | |
501 | /* | |
502 | * When working from sysFS it is important to keep track | |
503 | * of the paths that were created so that they can be | |
504 | * undone in 'coresight_disable()'. Since there can only | |
505 | * be a single session per tracer (when working from sysFS) | |
506 | * a per-cpu variable will do just fine. | |
507 | */ | |
508 | cpu = source_ops(csdev)->cpu_id(csdev); | |
509 | per_cpu(tracer_path, cpu) = path; | |
510 | break; | |
511 | case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE: | |
512 | stm_path = path; | |
513 | break; | |
514 | default: | |
515 | /* We can't be here */ | |
516 | break; | |
517 | } | |
b3e94405 | 518 | |
a06ae860 PP |
519 | out: |
520 | mutex_unlock(&coresight_mutex); | |
521 | return ret; | |
b3e94405 MP |
522 | |
523 | err_source: | |
524 | coresight_disable_path(path); | |
525 | ||
526 | err_path: | |
527 | coresight_release_path(path); | |
528 | goto out; | |
a06ae860 PP |
529 | } |
530 | EXPORT_SYMBOL_GPL(coresight_enable); | |
531 | ||
532 | void coresight_disable(struct coresight_device *csdev) | |
533 | { | |
a685d683 MP |
534 | int cpu, ret; |
535 | struct list_head *path = NULL; | |
a06ae860 PP |
536 | |
537 | mutex_lock(&coresight_mutex); | |
a685d683 MP |
538 | |
539 | ret = coresight_validate_source(csdev, __func__); | |
540 | if (ret) | |
a06ae860 | 541 | goto out; |
a685d683 | 542 | |
a06ae860 PP |
543 | if (!csdev->enable) |
544 | goto out; | |
545 | ||
a685d683 MP |
546 | switch (csdev->subtype.source_subtype) { |
547 | case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC: | |
548 | cpu = source_ops(csdev)->cpu_id(csdev); | |
549 | path = per_cpu(tracer_path, cpu); | |
550 | per_cpu(tracer_path, cpu) = NULL; | |
551 | break; | |
552 | case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE: | |
553 | path = stm_path; | |
554 | stm_path = NULL; | |
555 | break; | |
556 | default: | |
557 | /* We can't be here */ | |
558 | break; | |
559 | } | |
560 | ||
a06ae860 | 561 | coresight_disable_source(csdev); |
b3e94405 MP |
562 | coresight_disable_path(path); |
563 | coresight_release_path(path); | |
a06ae860 PP |
564 | |
565 | out: | |
566 | mutex_unlock(&coresight_mutex); | |
567 | } | |
568 | EXPORT_SYMBOL_GPL(coresight_disable); | |
569 | ||
570 | static ssize_t enable_sink_show(struct device *dev, | |
571 | struct device_attribute *attr, char *buf) | |
572 | { | |
573 | struct coresight_device *csdev = to_coresight_device(dev); | |
574 | ||
e8dc27d0 | 575 | return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated); |
a06ae860 PP |
576 | } |
577 | ||
578 | static ssize_t enable_sink_store(struct device *dev, | |
579 | struct device_attribute *attr, | |
580 | const char *buf, size_t size) | |
581 | { | |
582 | int ret; | |
583 | unsigned long val; | |
584 | struct coresight_device *csdev = to_coresight_device(dev); | |
585 | ||
586 | ret = kstrtoul(buf, 10, &val); | |
587 | if (ret) | |
588 | return ret; | |
589 | ||
590 | if (val) | |
591 | csdev->activated = true; | |
592 | else | |
593 | csdev->activated = false; | |
594 | ||
595 | return size; | |
596 | ||
597 | } | |
598 | static DEVICE_ATTR_RW(enable_sink); | |
599 | ||
600 | static ssize_t enable_source_show(struct device *dev, | |
601 | struct device_attribute *attr, char *buf) | |
602 | { | |
603 | struct coresight_device *csdev = to_coresight_device(dev); | |
604 | ||
e8dc27d0 | 605 | return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable); |
a06ae860 PP |
606 | } |
607 | ||
608 | static ssize_t enable_source_store(struct device *dev, | |
609 | struct device_attribute *attr, | |
610 | const char *buf, size_t size) | |
611 | { | |
612 | int ret = 0; | |
613 | unsigned long val; | |
614 | struct coresight_device *csdev = to_coresight_device(dev); | |
615 | ||
616 | ret = kstrtoul(buf, 10, &val); | |
617 | if (ret) | |
618 | return ret; | |
619 | ||
620 | if (val) { | |
621 | ret = coresight_enable(csdev); | |
622 | if (ret) | |
623 | return ret; | |
624 | } else { | |
625 | coresight_disable(csdev); | |
626 | } | |
627 | ||
628 | return size; | |
629 | } | |
630 | static DEVICE_ATTR_RW(enable_source); | |
631 | ||
632 | static struct attribute *coresight_sink_attrs[] = { | |
633 | &dev_attr_enable_sink.attr, | |
634 | NULL, | |
635 | }; | |
636 | ATTRIBUTE_GROUPS(coresight_sink); | |
637 | ||
638 | static struct attribute *coresight_source_attrs[] = { | |
639 | &dev_attr_enable_source.attr, | |
640 | NULL, | |
641 | }; | |
642 | ATTRIBUTE_GROUPS(coresight_source); | |
643 | ||
644 | static struct device_type coresight_dev_type[] = { | |
645 | { | |
646 | .name = "none", | |
647 | }, | |
648 | { | |
649 | .name = "sink", | |
650 | .groups = coresight_sink_groups, | |
651 | }, | |
652 | { | |
653 | .name = "link", | |
654 | }, | |
655 | { | |
656 | .name = "linksink", | |
657 | .groups = coresight_sink_groups, | |
658 | }, | |
659 | { | |
660 | .name = "source", | |
661 | .groups = coresight_source_groups, | |
662 | }, | |
663 | }; | |
664 | ||
665 | static void coresight_device_release(struct device *dev) | |
666 | { | |
667 | struct coresight_device *csdev = to_coresight_device(dev); | |
668 | ||
fae54158 MP |
669 | kfree(csdev->conns); |
670 | kfree(csdev->refcnt); | |
a06ae860 PP |
671 | kfree(csdev); |
672 | } | |
673 | ||
674 | static int coresight_orphan_match(struct device *dev, void *data) | |
675 | { | |
676 | int i; | |
677 | bool still_orphan = false; | |
678 | struct coresight_device *csdev, *i_csdev; | |
679 | struct coresight_connection *conn; | |
680 | ||
681 | csdev = data; | |
682 | i_csdev = to_coresight_device(dev); | |
683 | ||
684 | /* No need to check oneself */ | |
685 | if (csdev == i_csdev) | |
686 | return 0; | |
687 | ||
688 | /* Move on to another component if no connection is orphan */ | |
689 | if (!i_csdev->orphan) | |
690 | return 0; | |
691 | /* | |
692 | * Circle throuch all the connection of that component. If we find | |
693 | * an orphan connection whose name matches @csdev, link it. | |
694 | */ | |
d786a47d | 695 | for (i = 0; i < i_csdev->nr_outport; i++) { |
a06ae860 PP |
696 | conn = &i_csdev->conns[i]; |
697 | ||
698 | /* We have found at least one orphan connection */ | |
699 | if (conn->child_dev == NULL) { | |
700 | /* Does it match this newly added device? */ | |
22394bc5 | 701 | if (!strcmp(dev_name(&csdev->dev), conn->child_name)) { |
a06ae860 | 702 | conn->child_dev = csdev; |
22394bc5 KX |
703 | } else { |
704 | /* This component still has an orphan */ | |
705 | still_orphan = true; | |
706 | } | |
a06ae860 PP |
707 | } |
708 | } | |
709 | ||
710 | i_csdev->orphan = still_orphan; | |
711 | ||
712 | /* | |
713 | * Returning '0' ensures that all known component on the | |
714 | * bus will be checked. | |
715 | */ | |
716 | return 0; | |
717 | } | |
718 | ||
719 | static void coresight_fixup_orphan_conns(struct coresight_device *csdev) | |
720 | { | |
721 | /* | |
722 | * No need to check for a return value as orphan connection(s) | |
723 | * are hooked-up with each newly added component. | |
724 | */ | |
725 | bus_for_each_dev(&coresight_bustype, NULL, | |
ff874227 | 726 | csdev, coresight_orphan_match); |
a06ae860 PP |
727 | } |
728 | ||
729 | ||
730 | static int coresight_name_match(struct device *dev, void *data) | |
731 | { | |
732 | char *to_match; | |
733 | struct coresight_device *i_csdev; | |
734 | ||
735 | to_match = data; | |
736 | i_csdev = to_coresight_device(dev); | |
737 | ||
fadf3a44 | 738 | if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev))) |
a06ae860 PP |
739 | return 1; |
740 | ||
741 | return 0; | |
742 | } | |
743 | ||
744 | static void coresight_fixup_device_conns(struct coresight_device *csdev) | |
745 | { | |
746 | int i; | |
747 | struct device *dev = NULL; | |
748 | struct coresight_connection *conn; | |
749 | ||
750 | for (i = 0; i < csdev->nr_outport; i++) { | |
751 | conn = &csdev->conns[i]; | |
752 | dev = bus_find_device(&coresight_bustype, NULL, | |
753 | (void *)conn->child_name, | |
754 | coresight_name_match); | |
755 | ||
756 | if (dev) { | |
757 | conn->child_dev = to_coresight_device(dev); | |
f2dfab35 MP |
758 | /* and put reference from 'bus_find_device()' */ |
759 | put_device(dev); | |
a06ae860 PP |
760 | } else { |
761 | csdev->orphan = true; | |
762 | conn->child_dev = NULL; | |
763 | } | |
764 | } | |
765 | } | |
766 | ||
ad725aee MP |
767 | static int coresight_remove_match(struct device *dev, void *data) |
768 | { | |
769 | int i; | |
770 | struct coresight_device *csdev, *iterator; | |
771 | struct coresight_connection *conn; | |
772 | ||
773 | csdev = data; | |
774 | iterator = to_coresight_device(dev); | |
775 | ||
776 | /* No need to check oneself */ | |
777 | if (csdev == iterator) | |
778 | return 0; | |
779 | ||
780 | /* | |
781 | * Circle throuch all the connection of that component. If we find | |
782 | * a connection whose name matches @csdev, remove it. | |
783 | */ | |
784 | for (i = 0; i < iterator->nr_outport; i++) { | |
785 | conn = &iterator->conns[i]; | |
786 | ||
787 | if (conn->child_dev == NULL) | |
788 | continue; | |
789 | ||
790 | if (!strcmp(dev_name(&csdev->dev), conn->child_name)) { | |
791 | iterator->orphan = true; | |
792 | conn->child_dev = NULL; | |
793 | /* No need to continue */ | |
794 | break; | |
795 | } | |
796 | } | |
797 | ||
798 | /* | |
799 | * Returning '0' ensures that all known component on the | |
800 | * bus will be checked. | |
801 | */ | |
802 | return 0; | |
803 | } | |
804 | ||
805 | static void coresight_remove_conns(struct coresight_device *csdev) | |
806 | { | |
807 | bus_for_each_dev(&coresight_bustype, NULL, | |
808 | csdev, coresight_remove_match); | |
809 | } | |
810 | ||
a06ae860 PP |
811 | /** |
812 | * coresight_timeout - loop until a bit has changed to a specific state. | |
813 | * @addr: base address of the area of interest. | |
814 | * @offset: address of a register, starting from @addr. | |
815 | * @position: the position of the bit of interest. | |
816 | * @value: the value the bit should have. | |
817 | * | |
818 | * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if | |
819 | * TIMEOUT_US has elapsed, which ever happens first. | |
820 | */ | |
821 | ||
822 | int coresight_timeout(void __iomem *addr, u32 offset, int position, int value) | |
823 | { | |
824 | int i; | |
825 | u32 val; | |
826 | ||
827 | for (i = TIMEOUT_US; i > 0; i--) { | |
828 | val = __raw_readl(addr + offset); | |
829 | /* waiting on the bit to go from 0 to 1 */ | |
830 | if (value) { | |
831 | if (val & BIT(position)) | |
832 | return 0; | |
833 | /* waiting on the bit to go from 1 to 0 */ | |
834 | } else { | |
835 | if (!(val & BIT(position))) | |
836 | return 0; | |
837 | } | |
838 | ||
839 | /* | |
840 | * Delay is arbitrary - the specification doesn't say how long | |
841 | * we are expected to wait. Extra check required to make sure | |
842 | * we don't wait needlessly on the last iteration. | |
843 | */ | |
844 | if (i - 1) | |
845 | udelay(1); | |
846 | } | |
847 | ||
848 | return -EAGAIN; | |
849 | } | |
850 | ||
851 | struct bus_type coresight_bustype = { | |
852 | .name = "coresight", | |
853 | }; | |
854 | ||
855 | static int __init coresight_init(void) | |
856 | { | |
857 | return bus_register(&coresight_bustype); | |
858 | } | |
859 | postcore_initcall(coresight_init); | |
860 | ||
861 | struct coresight_device *coresight_register(struct coresight_desc *desc) | |
862 | { | |
863 | int i; | |
864 | int ret; | |
865 | int link_subtype; | |
866 | int nr_refcnts = 1; | |
867 | atomic_t *refcnts = NULL; | |
868 | struct coresight_device *csdev; | |
869 | struct coresight_connection *conns; | |
870 | ||
871 | csdev = kzalloc(sizeof(*csdev), GFP_KERNEL); | |
872 | if (!csdev) { | |
873 | ret = -ENOMEM; | |
874 | goto err_kzalloc_csdev; | |
875 | } | |
876 | ||
877 | if (desc->type == CORESIGHT_DEV_TYPE_LINK || | |
878 | desc->type == CORESIGHT_DEV_TYPE_LINKSINK) { | |
879 | link_subtype = desc->subtype.link_subtype; | |
880 | ||
881 | if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) | |
882 | nr_refcnts = desc->pdata->nr_inport; | |
883 | else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) | |
884 | nr_refcnts = desc->pdata->nr_outport; | |
885 | } | |
886 | ||
887 | refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL); | |
888 | if (!refcnts) { | |
889 | ret = -ENOMEM; | |
890 | goto err_kzalloc_refcnts; | |
891 | } | |
892 | ||
893 | csdev->refcnt = refcnts; | |
894 | ||
895 | csdev->nr_inport = desc->pdata->nr_inport; | |
896 | csdev->nr_outport = desc->pdata->nr_outport; | |
897 | conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL); | |
898 | if (!conns) { | |
899 | ret = -ENOMEM; | |
900 | goto err_kzalloc_conns; | |
901 | } | |
902 | ||
903 | for (i = 0; i < csdev->nr_outport; i++) { | |
904 | conns[i].outport = desc->pdata->outports[i]; | |
905 | conns[i].child_name = desc->pdata->child_names[i]; | |
906 | conns[i].child_port = desc->pdata->child_ports[i]; | |
907 | } | |
908 | ||
909 | csdev->conns = conns; | |
910 | ||
911 | csdev->type = desc->type; | |
912 | csdev->subtype = desc->subtype; | |
913 | csdev->ops = desc->ops; | |
914 | csdev->orphan = false; | |
915 | ||
916 | csdev->dev.type = &coresight_dev_type[desc->type]; | |
917 | csdev->dev.groups = desc->groups; | |
918 | csdev->dev.parent = desc->dev; | |
919 | csdev->dev.release = coresight_device_release; | |
920 | csdev->dev.bus = &coresight_bustype; | |
921 | dev_set_name(&csdev->dev, "%s", desc->pdata->name); | |
922 | ||
923 | ret = device_register(&csdev->dev); | |
924 | if (ret) | |
925 | goto err_device_register; | |
926 | ||
927 | mutex_lock(&coresight_mutex); | |
928 | ||
929 | coresight_fixup_device_conns(csdev); | |
930 | coresight_fixup_orphan_conns(csdev); | |
931 | ||
932 | mutex_unlock(&coresight_mutex); | |
933 | ||
934 | return csdev; | |
935 | ||
936 | err_device_register: | |
937 | kfree(conns); | |
938 | err_kzalloc_conns: | |
939 | kfree(refcnts); | |
940 | err_kzalloc_refcnts: | |
941 | kfree(csdev); | |
942 | err_kzalloc_csdev: | |
943 | return ERR_PTR(ret); | |
944 | } | |
945 | EXPORT_SYMBOL_GPL(coresight_register); | |
946 | ||
947 | void coresight_unregister(struct coresight_device *csdev) | |
948 | { | |
ad725aee MP |
949 | /* Remove references of that device in the topology */ |
950 | coresight_remove_conns(csdev); | |
a06ae860 | 951 | device_unregister(&csdev->dev); |
a06ae860 PP |
952 | } |
953 | EXPORT_SYMBOL_GPL(coresight_unregister); |