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 | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 374 | /** |
| 375 | * _coresight_build_path - recursively build a path from a @csdev to a sink. |
| 376 | * @csdev: The device to start from. |
| 377 | * @path: The list to add devices to. |
| 378 | * |
| 379 | * The tree of Coresight device is traversed until an activated sink is |
| 380 | * found. From there the sink is added to the list along with all the |
| 381 | * devices that led to that point - the end result is a list from source |
| 382 | * to sink. In that list the source is the first device and the sink the |
| 383 | * last one. |
| 384 | */ |
| 385 | static int _coresight_build_path(struct coresight_device *csdev, |
| 386 | struct list_head *path) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 387 | { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 388 | int i; |
| 389 | bool found = false; |
| 390 | struct coresight_node *node; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 391 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 392 | /* An activated sink has been found. Enqueue the element */ |
Xia Kaixu | a0a500e | 2015-03-30 14:13:38 -0600 | [diff] [blame] | 393 | if ((csdev->type == CORESIGHT_DEV_TYPE_SINK || |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 394 | csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) && csdev->activated) |
| 395 | goto out; |
| 396 | |
| 397 | /* Not a sink - recursively explore each port found on this element */ |
| 398 | for (i = 0; i < csdev->nr_outport; i++) { |
Suzuki K Poulose | ec48a1d | 2016-06-14 11:17:12 -0600 | [diff] [blame] | 399 | struct coresight_device *child_dev = csdev->conns[i].child_dev; |
| 400 | |
| 401 | if (child_dev && _coresight_build_path(child_dev, path) == 0) { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 402 | found = true; |
| 403 | break; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 407 | if (!found) |
| 408 | return -ENODEV; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 409 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 410 | out: |
| 411 | /* |
| 412 | * A path from this element to a sink has been found. The elements |
| 413 | * leading to the sink are already enqueued, all that is left to do |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 414 | * is tell the PM runtime core we need this element and add a node |
| 415 | * for it. |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 416 | */ |
| 417 | node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL); |
| 418 | if (!node) |
| 419 | return -ENOMEM; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 420 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 421 | node->csdev = csdev; |
| 422 | list_add(&node->link, path); |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 423 | pm_runtime_get_sync(csdev->dev.parent); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | struct list_head *coresight_build_path(struct coresight_device *csdev) |
| 429 | { |
| 430 | struct list_head *path; |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 431 | int rc; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 432 | |
| 433 | path = kzalloc(sizeof(struct list_head), GFP_KERNEL); |
| 434 | if (!path) |
Mathieu Poirier | 8e67cdb | 2016-09-08 16:50:38 -0600 | [diff] [blame] | 435 | return ERR_PTR(-ENOMEM); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 436 | |
| 437 | INIT_LIST_HEAD(path); |
| 438 | |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 439 | rc = _coresight_build_path(csdev, path); |
| 440 | if (rc) { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 441 | kfree(path); |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 442 | return ERR_PTR(rc); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | return path; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * coresight_release_path - release a previously built path. |
| 450 | * @path: the path to release. |
| 451 | * |
| 452 | * Go through all the elements of a path and 1) removed it from the list and |
| 453 | * 2) free the memory allocated for each node. |
| 454 | */ |
| 455 | void coresight_release_path(struct list_head *path) |
| 456 | { |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 457 | struct coresight_device *csdev; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 458 | struct coresight_node *nd, *next; |
| 459 | |
| 460 | list_for_each_entry_safe(nd, next, path, link) { |
Mathieu Poirier | 5da5325 | 2016-02-17 17:51:47 -0700 | [diff] [blame] | 461 | csdev = nd->csdev; |
| 462 | |
| 463 | pm_runtime_put_sync(csdev->dev.parent); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 464 | list_del(&nd->link); |
| 465 | kfree(nd); |
| 466 | } |
| 467 | |
| 468 | kfree(path); |
| 469 | path = NULL; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 470 | } |
| 471 | |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 472 | /** coresight_validate_source - make sure a source has the right credentials |
| 473 | * @csdev: the device structure for a source. |
| 474 | * @function: the function this was called from. |
| 475 | * |
| 476 | * Assumes the coresight_mutex is held. |
| 477 | */ |
| 478 | static int coresight_validate_source(struct coresight_device *csdev, |
| 479 | const char *function) |
| 480 | { |
| 481 | u32 type, subtype; |
| 482 | |
| 483 | type = csdev->type; |
| 484 | subtype = csdev->subtype.source_subtype; |
| 485 | |
| 486 | if (type != CORESIGHT_DEV_TYPE_SOURCE) { |
| 487 | dev_err(&csdev->dev, "wrong device type in %s\n", function); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC && |
| 492 | subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) { |
| 493 | dev_err(&csdev->dev, "wrong device subtype in %s\n", function); |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 500 | int coresight_enable(struct coresight_device *csdev) |
| 501 | { |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 502 | int cpu, ret = 0; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 503 | struct list_head *path; |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 504 | enum coresight_dev_subtype_source subtype; |
| 505 | |
| 506 | subtype = csdev->subtype.source_subtype; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 507 | |
| 508 | mutex_lock(&coresight_mutex); |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 509 | |
| 510 | ret = coresight_validate_source(csdev, __func__); |
| 511 | if (ret) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 512 | goto out; |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 513 | |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 514 | if (csdev->enable) { |
| 515 | /* |
| 516 | * There could be multiple applications driving the software |
| 517 | * source. So keep the refcount for each such user when the |
| 518 | * source is already enabled. |
| 519 | */ |
| 520 | if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) |
| 521 | atomic_inc(csdev->refcnt); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 522 | goto out; |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 523 | } |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 524 | |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 525 | path = coresight_build_path(csdev); |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 526 | if (IS_ERR(path)) { |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 527 | pr_err("building path(s) failed\n"); |
Suzuki K Poulose | 5014e90 | 2016-05-06 15:35:50 +0100 | [diff] [blame] | 528 | ret = PTR_ERR(path); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 529 | goto out; |
| 530 | } |
| 531 | |
Mathieu Poirier | e827d45 | 2016-02-17 17:51:59 -0700 | [diff] [blame] | 532 | ret = coresight_enable_path(path, CS_MODE_SYSFS); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 533 | if (ret) |
| 534 | goto err_path; |
| 535 | |
Mathieu Poirier | 22fd532 | 2016-02-17 17:51:52 -0700 | [diff] [blame] | 536 | ret = coresight_enable_source(csdev, CS_MODE_SYSFS); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 537 | if (ret) |
| 538 | goto err_source; |
| 539 | |
Suzuki K Poulose | 41830b8 | 2017-06-05 14:15:03 -0600 | [diff] [blame] | 540 | switch (subtype) { |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 541 | case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC: |
| 542 | /* |
| 543 | * When working from sysFS it is important to keep track |
| 544 | * of the paths that were created so that they can be |
| 545 | * undone in 'coresight_disable()'. Since there can only |
| 546 | * be a single session per tracer (when working from sysFS) |
| 547 | * a per-cpu variable will do just fine. |
| 548 | */ |
| 549 | cpu = source_ops(csdev)->cpu_id(csdev); |
| 550 | per_cpu(tracer_path, cpu) = path; |
| 551 | break; |
| 552 | case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE: |
| 553 | stm_path = path; |
| 554 | break; |
| 555 | default: |
| 556 | /* We can't be here */ |
| 557 | break; |
| 558 | } |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 559 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 560 | out: |
| 561 | mutex_unlock(&coresight_mutex); |
| 562 | return ret; |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 563 | |
| 564 | err_source: |
| 565 | coresight_disable_path(path); |
| 566 | |
| 567 | err_path: |
| 568 | coresight_release_path(path); |
| 569 | goto out; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 570 | } |
| 571 | EXPORT_SYMBOL_GPL(coresight_enable); |
| 572 | |
| 573 | void coresight_disable(struct coresight_device *csdev) |
| 574 | { |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 575 | int cpu, ret; |
| 576 | struct list_head *path = NULL; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 577 | |
| 578 | mutex_lock(&coresight_mutex); |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 579 | |
| 580 | ret = coresight_validate_source(csdev, __func__); |
| 581 | if (ret) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 582 | goto out; |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 583 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 584 | if (!csdev->enable) |
| 585 | goto out; |
| 586 | |
Mathieu Poirier | a685d68 | 2016-05-03 11:33:38 -0600 | [diff] [blame] | 587 | switch (csdev->subtype.source_subtype) { |
| 588 | case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC: |
| 589 | cpu = source_ops(csdev)->cpu_id(csdev); |
| 590 | path = per_cpu(tracer_path, cpu); |
| 591 | per_cpu(tracer_path, cpu) = NULL; |
| 592 | break; |
| 593 | case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE: |
| 594 | path = stm_path; |
| 595 | stm_path = NULL; |
| 596 | break; |
| 597 | default: |
| 598 | /* We can't be here */ |
| 599 | break; |
| 600 | } |
| 601 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 602 | coresight_disable_source(csdev); |
Mathieu Poirier | b3e9440 | 2016-02-17 17:51:45 -0700 | [diff] [blame] | 603 | coresight_disable_path(path); |
| 604 | coresight_release_path(path); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 605 | |
| 606 | out: |
| 607 | mutex_unlock(&coresight_mutex); |
| 608 | } |
| 609 | EXPORT_SYMBOL_GPL(coresight_disable); |
| 610 | |
| 611 | static ssize_t enable_sink_show(struct device *dev, |
| 612 | struct device_attribute *attr, char *buf) |
| 613 | { |
| 614 | struct coresight_device *csdev = to_coresight_device(dev); |
| 615 | |
Li Pengcheng | e8dc27d | 2016-05-03 11:33:35 -0600 | [diff] [blame] | 616 | return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static ssize_t enable_sink_store(struct device *dev, |
| 620 | struct device_attribute *attr, |
| 621 | const char *buf, size_t size) |
| 622 | { |
| 623 | int ret; |
| 624 | unsigned long val; |
| 625 | struct coresight_device *csdev = to_coresight_device(dev); |
| 626 | |
| 627 | ret = kstrtoul(buf, 10, &val); |
| 628 | if (ret) |
| 629 | return ret; |
| 630 | |
| 631 | if (val) |
| 632 | csdev->activated = true; |
| 633 | else |
| 634 | csdev->activated = false; |
| 635 | |
| 636 | return size; |
| 637 | |
| 638 | } |
| 639 | static DEVICE_ATTR_RW(enable_sink); |
| 640 | |
| 641 | static ssize_t enable_source_show(struct device *dev, |
| 642 | struct device_attribute *attr, char *buf) |
| 643 | { |
| 644 | struct coresight_device *csdev = to_coresight_device(dev); |
| 645 | |
Li Pengcheng | e8dc27d | 2016-05-03 11:33:35 -0600 | [diff] [blame] | 646 | return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | static ssize_t enable_source_store(struct device *dev, |
| 650 | struct device_attribute *attr, |
| 651 | const char *buf, size_t size) |
| 652 | { |
| 653 | int ret = 0; |
| 654 | unsigned long val; |
| 655 | struct coresight_device *csdev = to_coresight_device(dev); |
| 656 | |
| 657 | ret = kstrtoul(buf, 10, &val); |
| 658 | if (ret) |
| 659 | return ret; |
| 660 | |
| 661 | if (val) { |
| 662 | ret = coresight_enable(csdev); |
| 663 | if (ret) |
| 664 | return ret; |
| 665 | } else { |
| 666 | coresight_disable(csdev); |
| 667 | } |
| 668 | |
| 669 | return size; |
| 670 | } |
| 671 | static DEVICE_ATTR_RW(enable_source); |
| 672 | |
| 673 | static struct attribute *coresight_sink_attrs[] = { |
| 674 | &dev_attr_enable_sink.attr, |
| 675 | NULL, |
| 676 | }; |
| 677 | ATTRIBUTE_GROUPS(coresight_sink); |
| 678 | |
| 679 | static struct attribute *coresight_source_attrs[] = { |
| 680 | &dev_attr_enable_source.attr, |
| 681 | NULL, |
| 682 | }; |
| 683 | ATTRIBUTE_GROUPS(coresight_source); |
| 684 | |
| 685 | static struct device_type coresight_dev_type[] = { |
| 686 | { |
| 687 | .name = "none", |
| 688 | }, |
| 689 | { |
| 690 | .name = "sink", |
| 691 | .groups = coresight_sink_groups, |
| 692 | }, |
| 693 | { |
| 694 | .name = "link", |
| 695 | }, |
| 696 | { |
| 697 | .name = "linksink", |
| 698 | .groups = coresight_sink_groups, |
| 699 | }, |
| 700 | { |
| 701 | .name = "source", |
| 702 | .groups = coresight_source_groups, |
| 703 | }, |
| 704 | }; |
| 705 | |
| 706 | static void coresight_device_release(struct device *dev) |
| 707 | { |
| 708 | struct coresight_device *csdev = to_coresight_device(dev); |
| 709 | |
Mathieu Poirier | fae5415 | 2016-02-02 14:13:57 -0700 | [diff] [blame] | 710 | kfree(csdev->conns); |
| 711 | kfree(csdev->refcnt); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 712 | kfree(csdev); |
| 713 | } |
| 714 | |
| 715 | static int coresight_orphan_match(struct device *dev, void *data) |
| 716 | { |
| 717 | int i; |
| 718 | bool still_orphan = false; |
| 719 | struct coresight_device *csdev, *i_csdev; |
| 720 | struct coresight_connection *conn; |
| 721 | |
| 722 | csdev = data; |
| 723 | i_csdev = to_coresight_device(dev); |
| 724 | |
| 725 | /* No need to check oneself */ |
| 726 | if (csdev == i_csdev) |
| 727 | return 0; |
| 728 | |
| 729 | /* Move on to another component if no connection is orphan */ |
| 730 | if (!i_csdev->orphan) |
| 731 | return 0; |
| 732 | /* |
| 733 | * Circle throuch all the connection of that component. If we find |
| 734 | * an orphan connection whose name matches @csdev, link it. |
| 735 | */ |
Kaixu Xia | d786a47d | 2015-01-26 09:22:20 -0700 | [diff] [blame] | 736 | for (i = 0; i < i_csdev->nr_outport; i++) { |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 737 | conn = &i_csdev->conns[i]; |
| 738 | |
| 739 | /* We have found at least one orphan connection */ |
| 740 | if (conn->child_dev == NULL) { |
| 741 | /* Does it match this newly added device? */ |
Sudeep Holla | b839215 | 2016-08-25 15:18:51 -0600 | [diff] [blame] | 742 | if (conn->child_name && |
| 743 | !strcmp(dev_name(&csdev->dev), conn->child_name)) { |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 744 | conn->child_dev = csdev; |
Kaixu Xia | 22394bc | 2015-01-26 09:22:19 -0700 | [diff] [blame] | 745 | } else { |
| 746 | /* This component still has an orphan */ |
| 747 | still_orphan = true; |
| 748 | } |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
| 751 | |
| 752 | i_csdev->orphan = still_orphan; |
| 753 | |
| 754 | /* |
| 755 | * Returning '0' ensures that all known component on the |
| 756 | * bus will be checked. |
| 757 | */ |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | static void coresight_fixup_orphan_conns(struct coresight_device *csdev) |
| 762 | { |
| 763 | /* |
| 764 | * No need to check for a return value as orphan connection(s) |
| 765 | * are hooked-up with each newly added component. |
| 766 | */ |
| 767 | bus_for_each_dev(&coresight_bustype, NULL, |
Mathieu Poirier | ff87422 | 2016-02-02 14:13:55 -0700 | [diff] [blame] | 768 | csdev, coresight_orphan_match); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | |
| 772 | static int coresight_name_match(struct device *dev, void *data) |
| 773 | { |
| 774 | char *to_match; |
| 775 | struct coresight_device *i_csdev; |
| 776 | |
| 777 | to_match = data; |
| 778 | i_csdev = to_coresight_device(dev); |
| 779 | |
Mathieu Poirier | fadf3a4 | 2015-12-17 08:47:02 -0700 | [diff] [blame] | 780 | if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev))) |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 781 | return 1; |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static void coresight_fixup_device_conns(struct coresight_device *csdev) |
| 787 | { |
| 788 | int i; |
| 789 | struct device *dev = NULL; |
| 790 | struct coresight_connection *conn; |
| 791 | |
| 792 | for (i = 0; i < csdev->nr_outport; i++) { |
| 793 | conn = &csdev->conns[i]; |
| 794 | dev = bus_find_device(&coresight_bustype, NULL, |
| 795 | (void *)conn->child_name, |
| 796 | coresight_name_match); |
| 797 | |
| 798 | if (dev) { |
| 799 | conn->child_dev = to_coresight_device(dev); |
Mathieu Poirier | f2dfab3 | 2016-02-02 14:13:58 -0700 | [diff] [blame] | 800 | /* and put reference from 'bus_find_device()' */ |
| 801 | put_device(dev); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 802 | } else { |
| 803 | csdev->orphan = true; |
| 804 | conn->child_dev = NULL; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Mathieu Poirier | ad725ae | 2016-02-02 14:13:59 -0700 | [diff] [blame] | 809 | static int coresight_remove_match(struct device *dev, void *data) |
| 810 | { |
| 811 | int i; |
| 812 | struct coresight_device *csdev, *iterator; |
| 813 | struct coresight_connection *conn; |
| 814 | |
| 815 | csdev = data; |
| 816 | iterator = to_coresight_device(dev); |
| 817 | |
| 818 | /* No need to check oneself */ |
| 819 | if (csdev == iterator) |
| 820 | return 0; |
| 821 | |
| 822 | /* |
| 823 | * Circle throuch all the connection of that component. If we find |
| 824 | * a connection whose name matches @csdev, remove it. |
| 825 | */ |
| 826 | for (i = 0; i < iterator->nr_outport; i++) { |
| 827 | conn = &iterator->conns[i]; |
| 828 | |
| 829 | if (conn->child_dev == NULL) |
| 830 | continue; |
| 831 | |
| 832 | if (!strcmp(dev_name(&csdev->dev), conn->child_name)) { |
| 833 | iterator->orphan = true; |
| 834 | conn->child_dev = NULL; |
| 835 | /* No need to continue */ |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | /* |
| 841 | * Returning '0' ensures that all known component on the |
| 842 | * bus will be checked. |
| 843 | */ |
| 844 | return 0; |
| 845 | } |
| 846 | |
| 847 | static void coresight_remove_conns(struct coresight_device *csdev) |
| 848 | { |
| 849 | bus_for_each_dev(&coresight_bustype, NULL, |
| 850 | csdev, coresight_remove_match); |
| 851 | } |
| 852 | |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 853 | /** |
| 854 | * coresight_timeout - loop until a bit has changed to a specific state. |
| 855 | * @addr: base address of the area of interest. |
| 856 | * @offset: address of a register, starting from @addr. |
| 857 | * @position: the position of the bit of interest. |
| 858 | * @value: the value the bit should have. |
| 859 | * |
| 860 | * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if |
| 861 | * TIMEOUT_US has elapsed, which ever happens first. |
| 862 | */ |
| 863 | |
| 864 | int coresight_timeout(void __iomem *addr, u32 offset, int position, int value) |
| 865 | { |
| 866 | int i; |
| 867 | u32 val; |
| 868 | |
| 869 | for (i = TIMEOUT_US; i > 0; i--) { |
| 870 | val = __raw_readl(addr + offset); |
| 871 | /* waiting on the bit to go from 0 to 1 */ |
| 872 | if (value) { |
| 873 | if (val & BIT(position)) |
| 874 | return 0; |
| 875 | /* waiting on the bit to go from 1 to 0 */ |
| 876 | } else { |
| 877 | if (!(val & BIT(position))) |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Delay is arbitrary - the specification doesn't say how long |
| 883 | * we are expected to wait. Extra check required to make sure |
| 884 | * we don't wait needlessly on the last iteration. |
| 885 | */ |
| 886 | if (i - 1) |
| 887 | udelay(1); |
| 888 | } |
| 889 | |
| 890 | return -EAGAIN; |
| 891 | } |
| 892 | |
| 893 | struct bus_type coresight_bustype = { |
| 894 | .name = "coresight", |
| 895 | }; |
| 896 | |
| 897 | static int __init coresight_init(void) |
| 898 | { |
| 899 | return bus_register(&coresight_bustype); |
| 900 | } |
| 901 | postcore_initcall(coresight_init); |
| 902 | |
| 903 | struct coresight_device *coresight_register(struct coresight_desc *desc) |
| 904 | { |
| 905 | int i; |
| 906 | int ret; |
| 907 | int link_subtype; |
| 908 | int nr_refcnts = 1; |
| 909 | atomic_t *refcnts = NULL; |
| 910 | struct coresight_device *csdev; |
Suzuki K Poulose | 068c0a5 | 2016-08-25 15:18:56 -0600 | [diff] [blame] | 911 | struct coresight_connection *conns = NULL; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 912 | |
| 913 | csdev = kzalloc(sizeof(*csdev), GFP_KERNEL); |
| 914 | if (!csdev) { |
| 915 | ret = -ENOMEM; |
| 916 | goto err_kzalloc_csdev; |
| 917 | } |
| 918 | |
| 919 | if (desc->type == CORESIGHT_DEV_TYPE_LINK || |
| 920 | desc->type == CORESIGHT_DEV_TYPE_LINKSINK) { |
| 921 | link_subtype = desc->subtype.link_subtype; |
| 922 | |
| 923 | if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) |
| 924 | nr_refcnts = desc->pdata->nr_inport; |
| 925 | else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) |
| 926 | nr_refcnts = desc->pdata->nr_outport; |
| 927 | } |
| 928 | |
| 929 | refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL); |
| 930 | if (!refcnts) { |
| 931 | ret = -ENOMEM; |
| 932 | goto err_kzalloc_refcnts; |
| 933 | } |
| 934 | |
| 935 | csdev->refcnt = refcnts; |
| 936 | |
| 937 | csdev->nr_inport = desc->pdata->nr_inport; |
| 938 | csdev->nr_outport = desc->pdata->nr_outport; |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 939 | |
Suzuki K Poulose | 068c0a5 | 2016-08-25 15:18:56 -0600 | [diff] [blame] | 940 | /* Initialise connections if there is at least one outport */ |
| 941 | if (csdev->nr_outport) { |
| 942 | conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL); |
| 943 | if (!conns) { |
| 944 | ret = -ENOMEM; |
| 945 | goto err_kzalloc_conns; |
| 946 | } |
| 947 | |
| 948 | for (i = 0; i < csdev->nr_outport; i++) { |
| 949 | conns[i].outport = desc->pdata->outports[i]; |
| 950 | conns[i].child_name = desc->pdata->child_names[i]; |
| 951 | conns[i].child_port = desc->pdata->child_ports[i]; |
| 952 | } |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | csdev->conns = conns; |
| 956 | |
| 957 | csdev->type = desc->type; |
| 958 | csdev->subtype = desc->subtype; |
| 959 | csdev->ops = desc->ops; |
| 960 | csdev->orphan = false; |
| 961 | |
| 962 | csdev->dev.type = &coresight_dev_type[desc->type]; |
| 963 | csdev->dev.groups = desc->groups; |
| 964 | csdev->dev.parent = desc->dev; |
| 965 | csdev->dev.release = coresight_device_release; |
| 966 | csdev->dev.bus = &coresight_bustype; |
| 967 | dev_set_name(&csdev->dev, "%s", desc->pdata->name); |
| 968 | |
| 969 | ret = device_register(&csdev->dev); |
| 970 | if (ret) |
| 971 | goto err_device_register; |
| 972 | |
| 973 | mutex_lock(&coresight_mutex); |
| 974 | |
| 975 | coresight_fixup_device_conns(csdev); |
| 976 | coresight_fixup_orphan_conns(csdev); |
| 977 | |
| 978 | mutex_unlock(&coresight_mutex); |
| 979 | |
| 980 | return csdev; |
| 981 | |
| 982 | err_device_register: |
| 983 | kfree(conns); |
| 984 | err_kzalloc_conns: |
| 985 | kfree(refcnts); |
| 986 | err_kzalloc_refcnts: |
| 987 | kfree(csdev); |
| 988 | err_kzalloc_csdev: |
| 989 | return ERR_PTR(ret); |
| 990 | } |
| 991 | EXPORT_SYMBOL_GPL(coresight_register); |
| 992 | |
| 993 | void coresight_unregister(struct coresight_device *csdev) |
| 994 | { |
Mathieu Poirier | ad725ae | 2016-02-02 14:13:59 -0700 | [diff] [blame] | 995 | /* Remove references of that device in the topology */ |
| 996 | coresight_remove_conns(csdev); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 997 | device_unregister(&csdev->dev); |
Pratik Patel | a06ae86 | 2014-11-03 11:07:35 -0700 | [diff] [blame] | 998 | } |
| 999 | EXPORT_SYMBOL_GPL(coresight_unregister); |