Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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> |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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> |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 27 | |
| 28 | #include "coresight-priv.h" |
| 29 | |
| 30 | static DEFINE_MUTEX(coresight_mutex); |
| 31 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 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 | */ |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 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; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 55 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 95 | static int coresight_find_link_inport(struct coresight_device *csdev, |
| 96 | struct coresight_device *parent) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 97 | { |
| 98 | int i; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 99 | struct coresight_connection *conn; |
| 100 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | |
Suzuki K Poulose | 2887d52 | 2018-07-11 13:40:28 -0600 | [diff] [blame] | 110 | return -ENODEV; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 113 | static int coresight_find_link_outport(struct coresight_device *csdev, |
| 114 | struct coresight_device *child) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 115 | { |
| 116 | int i; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 117 | struct coresight_connection *conn; |
| 118 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | |
Suzuki K Poulose | 2887d52 | 2018-07-11 13:40:28 -0600 | [diff] [blame] | 128 | return -ENODEV; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Mathieu Poirier | e827d45 | 2016-02-17 17:51:59 -0700 | [diff] [blame] | 131 | static int coresight_enable_sink(struct coresight_device *csdev, u32 mode) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 132 | { |
| 133 | int ret; |
| 134 | |
| 135 | if (!csdev->enable) { |
| 136 | if (sink_ops(csdev)->enable) { |
Mathieu Poirier | e827d45 | 2016-02-17 17:51:59 -0700 | [diff] [blame] | 137 | ret = sink_ops(csdev)->enable(csdev, mode); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 159 | static int coresight_enable_link(struct coresight_device *csdev, |
| 160 | struct coresight_device *parent, |
| 161 | struct coresight_device *child) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 162 | { |
| 163 | int ret; |
| 164 | int link_subtype; |
| 165 | int refport, inport, outport; |
| 166 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 167 | if (!parent || !child) |
| 168 | return -EINVAL; |
| 169 | |
| 170 | inport = coresight_find_link_inport(csdev, parent); |
| 171 | outport = coresight_find_link_outport(csdev, child); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 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 | |
Suzuki K Poulose | 2887d52 | 2018-07-11 13:40:28 -0600 | [diff] [blame] | 181 | if (refport < 0) |
| 182 | return refport; |
| 183 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 184 | 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 Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 197 | static void coresight_disable_link(struct coresight_device *csdev, |
| 198 | struct coresight_device *parent, |
| 199 | struct coresight_device *child) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 200 | { |
| 201 | int i, nr_conns; |
| 202 | int link_subtype; |
| 203 | int refport, inport, outport; |
| 204 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 205 | if (!parent || !child) |
| 206 | return; |
| 207 | |
| 208 | inport = coresight_find_link_inport(csdev, parent); |
| 209 | outport = coresight_find_link_outport(csdev, child); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 210 | 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 Poirier | 22fd532 | 2016-02-17 17:51:52 -0700 | [diff] [blame] | 235 | static int coresight_enable_source(struct coresight_device *csdev, u32 mode) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 236 | { |
| 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 Poirier | 882d5e1 | 2016-02-17 17:51:57 -0700 | [diff] [blame] | 247 | ret = source_ops(csdev)->enable(csdev, NULL, mode); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 248 | if (ret) |
| 249 | return ret; |
| 250 | } |
| 251 | csdev->enable = true; |
| 252 | } |
| 253 | |
| 254 | atomic_inc(csdev->refcnt); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static void coresight_disable_source(struct coresight_device *csdev) |
| 260 | { |
| 261 | if (atomic_dec_return(csdev->refcnt) == 0) { |
| 262 | if (source_ops(csdev)->disable) { |
Mathieu Poirier | 68905d7 | 2016-08-25 15:19:10 -0600 | [diff] [blame] | 263 | source_ops(csdev)->disable(csdev, NULL); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 264 | csdev->enable = false; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 269 | void coresight_disable_path(struct list_head *path) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 270 | { |
Mathieu Poirier | dc2c4ef | 2016-05-03 11:34:00 -0600 | [diff] [blame] | 271 | u32 type; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 272 | 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 Poirier | dc2c4ef | 2016-05-03 11:34:00 -0600 | [diff] [blame] | 277 | type = csdev->type; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 278 | |
Mathieu Poirier | dc2c4ef | 2016-05-03 11:34:00 -0600 | [diff] [blame] | 279 | /* |
| 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 Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 291 | case CORESIGHT_DEV_TYPE_SINK: |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 292 | 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 Poirier | e827d45 | 2016-02-17 17:51:59 -0700 | [diff] [blame] | 308 | int coresight_enable_path(struct list_head *path, u32 mode) |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 309 | { |
| 310 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 311 | int ret = 0; |
Mathieu Poirier | dc2c4ef | 2016-05-03 11:34:00 -0600 | [diff] [blame] | 312 | u32 type; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 313 | struct coresight_node *nd; |
| 314 | struct coresight_device *csdev, *parent, *child; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 315 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 316 | list_for_each_entry_reverse(nd, path, link) { |
| 317 | csdev = nd->csdev; |
Mathieu Poirier | dc2c4ef | 2016-05-03 11:34:00 -0600 | [diff] [blame] | 318 | type = csdev->type; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 319 | |
Mathieu Poirier | dc2c4ef | 2016-05-03 11:34:00 -0600 | [diff] [blame] | 320 | /* |
| 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 Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 332 | case CORESIGHT_DEV_TYPE_SINK: |
Mathieu Poirier | e827d45 | 2016-02-17 17:51:59 -0700 | [diff] [blame] | 333 | ret = coresight_enable_sink(csdev, mode); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 334 | 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 Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 348 | goto err; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 352 | out: |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 353 | return ret; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 354 | err: |
| 355 | coresight_disable_path(path); |
| 356 | goto out; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Mathieu Poirier | b6404e2 | 2016-02-17 17:51:46 -0700 | [diff] [blame] | 359 | struct 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 Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 374 | static 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 | */ |
| 410 | struct 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 Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 420 | /** |
| 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 | */ |
| 431 | static int _coresight_build_path(struct coresight_device *csdev, |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 432 | struct coresight_device *sink, |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 433 | struct list_head *path) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 434 | { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 435 | int i; |
| 436 | bool found = false; |
| 437 | struct coresight_node *node; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 438 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 439 | /* An activated sink has been found. Enqueue the element */ |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 440 | if (csdev == sink) |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 441 | 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 Poulose | ec48a1d | 2016-06-14 11:17:12 -0600 | [diff] [blame] | 445 | struct coresight_device *child_dev = csdev->conns[i].child_dev; |
| 446 | |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 447 | if (child_dev && |
| 448 | _coresight_build_path(child_dev, sink, path) == 0) { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 449 | found = true; |
| 450 | break; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 454 | if (!found) |
| 455 | return -ENODEV; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 456 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 457 | out: |
| 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 Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 461 | * is tell the PM runtime core we need this element and add a node |
| 462 | * for it. |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 463 | */ |
| 464 | node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL); |
| 465 | if (!node) |
| 466 | return -ENOMEM; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 467 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 468 | node->csdev = csdev; |
| 469 | list_add(&node->link, path); |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 470 | pm_runtime_get_sync(csdev->dev.parent); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 475 | struct list_head *coresight_build_path(struct coresight_device *source, |
| 476 | struct coresight_device *sink) |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 477 | { |
| 478 | struct list_head *path; |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 479 | int rc; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 480 | |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 481 | if (!sink) |
| 482 | return ERR_PTR(-EINVAL); |
| 483 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 484 | path = kzalloc(sizeof(struct list_head), GFP_KERNEL); |
| 485 | if (!path) |
Mathieu Poirier | 8e67cdb | 2016-09-08 16:50:38 -0600 | [diff] [blame] | 486 | return ERR_PTR(-ENOMEM); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 487 | |
| 488 | INIT_LIST_HEAD(path); |
| 489 | |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 490 | rc = _coresight_build_path(source, sink, path); |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 491 | if (rc) { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 492 | kfree(path); |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 493 | return ERR_PTR(rc); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 494 | } |
| 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 | */ |
| 506 | void coresight_release_path(struct list_head *path) |
| 507 | { |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 508 | struct coresight_device *csdev; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 509 | struct coresight_node *nd, *next; |
| 510 | |
| 511 | list_for_each_entry_safe(nd, next, path, link) { |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 512 | csdev = nd->csdev; |
| 513 | |
| 514 | pm_runtime_put_sync(csdev->dev.parent); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 515 | list_del(&nd->link); |
| 516 | kfree(nd); |
| 517 | } |
| 518 | |
| 519 | kfree(path); |
| 520 | path = NULL; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 523 | /** 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 | */ |
| 529 | static 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 Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 551 | int coresight_enable(struct coresight_device *csdev) |
| 552 | { |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 553 | int cpu, ret = 0; |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 554 | struct coresight_device *sink; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 555 | struct list_head *path; |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 556 | enum coresight_dev_subtype_source subtype; |
| 557 | |
| 558 | subtype = csdev->subtype.source_subtype; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 559 | |
| 560 | mutex_lock(&coresight_mutex); |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 561 | |
| 562 | ret = coresight_validate_source(csdev, __func__); |
| 563 | if (ret) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 564 | goto out; |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 565 | |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 566 | 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 Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 574 | goto out; |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 575 | } |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 576 | |
Mathieu Poirier | ebb45d7 | 2016-11-29 09:47:14 -0700 | [diff] [blame] | 577 | /* |
| 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 Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 588 | if (IS_ERR(path)) { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 589 | pr_err("building path(s) failed\n"); |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 590 | ret = PTR_ERR(path); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 591 | goto out; |
| 592 | } |
| 593 | |
Mathieu Poirier | e827d45 | 2016-02-17 17:51:59 -0700 | [diff] [blame] | 594 | ret = coresight_enable_path(path, CS_MODE_SYSFS); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 595 | if (ret) |
| 596 | goto err_path; |
| 597 | |
Mathieu Poirier | 22fd532 | 2016-02-17 17:51:52 -0700 | [diff] [blame] | 598 | ret = coresight_enable_source(csdev, CS_MODE_SYSFS); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 599 | if (ret) |
| 600 | goto err_source; |
| 601 | |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 602 | switch (subtype) { |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 603 | 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 Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 621 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 622 | out: |
| 623 | mutex_unlock(&coresight_mutex); |
| 624 | return ret; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 625 | |
| 626 | err_source: |
| 627 | coresight_disable_path(path); |
| 628 | |
| 629 | err_path: |
| 630 | coresight_release_path(path); |
| 631 | goto out; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 632 | } |
| 633 | EXPORT_SYMBOL_GPL(coresight_enable); |
| 634 | |
| 635 | void coresight_disable(struct coresight_device *csdev) |
| 636 | { |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 637 | int cpu, ret; |
| 638 | struct list_head *path = NULL; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 639 | |
| 640 | mutex_lock(&coresight_mutex); |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 641 | |
| 642 | ret = coresight_validate_source(csdev, __func__); |
| 643 | if (ret) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 644 | goto out; |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 645 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 646 | if (!csdev->enable) |
| 647 | goto out; |
| 648 | |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 649 | 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 Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 664 | coresight_disable_source(csdev); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 665 | coresight_disable_path(path); |
| 666 | coresight_release_path(path); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 667 | |
| 668 | out: |
| 669 | mutex_unlock(&coresight_mutex); |
| 670 | } |
| 671 | EXPORT_SYMBOL_GPL(coresight_disable); |
| 672 | |
| 673 | static 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 Pengcheng | e8dc27d | 2016-05-03 11:33:35 -0600 | [diff] [blame] | 678 | return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static 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 | } |
| 701 | static DEVICE_ATTR_RW(enable_sink); |
| 702 | |
| 703 | static 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 Pengcheng | e8dc27d | 2016-05-03 11:33:35 -0600 | [diff] [blame] | 708 | return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | static 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 | } |
| 733 | static DEVICE_ATTR_RW(enable_source); |
| 734 | |
| 735 | static struct attribute *coresight_sink_attrs[] = { |
| 736 | &dev_attr_enable_sink.attr, |
| 737 | NULL, |
| 738 | }; |
| 739 | ATTRIBUTE_GROUPS(coresight_sink); |
| 740 | |
| 741 | static struct attribute *coresight_source_attrs[] = { |
| 742 | &dev_attr_enable_source.attr, |
| 743 | NULL, |
| 744 | }; |
| 745 | ATTRIBUTE_GROUPS(coresight_source); |
| 746 | |
| 747 | static 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 | |
| 768 | static void coresight_device_release(struct device *dev) |
| 769 | { |
| 770 | struct coresight_device *csdev = to_coresight_device(dev); |
| 771 | |
Mathieu Poirier | fae5415 | 2016-02-02 14:13:57 -0700 | [diff] [blame] | 772 | kfree(csdev->conns); |
| 773 | kfree(csdev->refcnt); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 774 | kfree(csdev); |
| 775 | } |
| 776 | |
| 777 | static 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 Xia | d786a47d | 2015-01-26 09:22:20 -0700 | [diff] [blame] | 798 | for (i = 0; i < i_csdev->nr_outport; i++) { |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 799 | 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 Holla | b839215 | 2016-08-25 15:18:51 -0600 | [diff] [blame] | 804 | if (conn->child_name && |
| 805 | !strcmp(dev_name(&csdev->dev), conn->child_name)) { |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 806 | conn->child_dev = csdev; |
Kaixu Xia | 22394bc | 2015-01-26 09:22:19 -0700 | [diff] [blame] | 807 | } else { |
| 808 | /* This component still has an orphan */ |
| 809 | still_orphan = true; |
| 810 | } |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 811 | } |
| 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 | |
| 823 | static 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 Poirier | ff87422 | 2016-02-02 14:13:55 -0700 | [diff] [blame] | 830 | csdev, coresight_orphan_match); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | |
| 834 | static 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 Poirier | fadf3a4 | 2015-12-17 08:47:02 -0700 | [diff] [blame] | 842 | if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev))) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 843 | return 1; |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static 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 Poirier | f2dfab3 | 2016-02-02 14:13:58 -0700 | [diff] [blame] | 862 | /* and put reference from 'bus_find_device()' */ |
| 863 | put_device(dev); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 864 | } else { |
| 865 | csdev->orphan = true; |
| 866 | conn->child_dev = NULL; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
Mathieu Poirier | ad725ae | 2016-02-02 14:13:59 -0700 | [diff] [blame] | 871 | static 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 | |
| 909 | static 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 Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 915 | /** |
| 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 | |
| 926 | int 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 | |
| 955 | struct bus_type coresight_bustype = { |
| 956 | .name = "coresight", |
| 957 | }; |
| 958 | |
| 959 | static int __init coresight_init(void) |
| 960 | { |
| 961 | return bus_register(&coresight_bustype); |
| 962 | } |
| 963 | postcore_initcall(coresight_init); |
| 964 | |
| 965 | struct 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 Poulose | 068c0a5 | 2016-08-25 15:18:56 -0600 | [diff] [blame] | 973 | struct coresight_connection *conns = NULL; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 974 | |
| 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 Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 1001 | |
Suzuki K Poulose | 068c0a5 | 2016-08-25 15:18:56 -0600 | [diff] [blame] | 1002 | /* 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 Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 1015 | } |
| 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 | |
| 1044 | err_device_register: |
| 1045 | kfree(conns); |
| 1046 | err_kzalloc_conns: |
| 1047 | kfree(refcnts); |
| 1048 | err_kzalloc_refcnts: |
| 1049 | kfree(csdev); |
| 1050 | err_kzalloc_csdev: |
| 1051 | return ERR_PTR(ret); |
| 1052 | } |
| 1053 | EXPORT_SYMBOL_GPL(coresight_register); |
| 1054 | |
| 1055 | void coresight_unregister(struct coresight_device *csdev) |
| 1056 | { |
Mathieu Poirier | ad725ae | 2016-02-02 14:13:59 -0700 | [diff] [blame] | 1057 | /* Remove references of that device in the topology */ |
| 1058 | coresight_remove_conns(csdev); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 1059 | device_unregister(&csdev->dev); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 1060 | } |
| 1061 | EXPORT_SYMBOL_GPL(coresight_unregister); |