1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2012, The Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/build_bug.h> 7 #include <linux/kernel.h> 8 #include <linux/init.h> 9 #include <linux/types.h> 10 #include <linux/device.h> 11 #include <linux/io.h> 12 #include <linux/err.h> 13 #include <linux/export.h> 14 #include <linux/slab.h> 15 #include <linux/stringhash.h> 16 #include <linux/mutex.h> 17 #include <linux/clk.h> 18 #include <linux/coresight.h> 19 #include <linux/property.h> 20 #include <linux/delay.h> 21 #include <linux/pm_runtime.h> 22 #include <linux/panic_notifier.h> 23 24 #include "coresight-etm-perf.h" 25 #include "coresight-priv.h" 26 #include "coresight-syscfg.h" 27 #include "coresight-trace-id.h" 28 29 /* 30 * Mutex used to lock all sysfs enable and disable actions and loading and 31 * unloading devices by the Coresight core. 32 */ 33 DEFINE_MUTEX(coresight_mutex); 34 static DEFINE_PER_CPU(struct coresight_device *, csdev_sink); 35 36 /** 37 * struct coresight_node - elements of a path, from source to sink 38 * @csdev: Address of an element. 39 * @link: hook to the list. 40 */ 41 struct coresight_node { 42 struct coresight_device *csdev; 43 struct list_head link; 44 }; 45 46 /* 47 * When losing synchronisation a new barrier packet needs to be inserted at the 48 * beginning of the data collected in a buffer. That way the decoder knows that 49 * it needs to look for another sync sequence. 50 */ 51 const u32 coresight_barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff}; 52 EXPORT_SYMBOL_GPL(coresight_barrier_pkt); 53 54 static const struct cti_assoc_op *cti_assoc_ops; 55 56 void coresight_set_cti_ops(const struct cti_assoc_op *cti_op) 57 { 58 cti_assoc_ops = cti_op; 59 } 60 EXPORT_SYMBOL_GPL(coresight_set_cti_ops); 61 62 void coresight_remove_cti_ops(void) 63 { 64 cti_assoc_ops = NULL; 65 } 66 EXPORT_SYMBOL_GPL(coresight_remove_cti_ops); 67 68 void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev) 69 { 70 per_cpu(csdev_sink, cpu) = csdev; 71 } 72 EXPORT_SYMBOL_GPL(coresight_set_percpu_sink); 73 74 struct coresight_device *coresight_get_percpu_sink(int cpu) 75 { 76 return per_cpu(csdev_sink, cpu); 77 } 78 EXPORT_SYMBOL_GPL(coresight_get_percpu_sink); 79 80 static struct coresight_device *coresight_get_source(struct coresight_path *path) 81 { 82 struct coresight_device *csdev; 83 84 if (!path) 85 return NULL; 86 87 csdev = list_first_entry(&path->path_list, struct coresight_node, link)->csdev; 88 if (!coresight_is_device_source(csdev)) 89 return NULL; 90 91 return csdev; 92 } 93 94 /** 95 * coresight_blocks_source - checks whether the connection matches the source 96 * of path if connection is bound to specific source. 97 * @src: The source device of the trace path 98 * @conn: The connection of one outport 99 * 100 * Return false if the connection doesn't have a source binded or source of the 101 * path matches the source binds to connection. 102 */ 103 static bool coresight_blocks_source(struct coresight_device *src, 104 struct coresight_connection *conn) 105 { 106 return conn->filter_src_fwnode && (conn->filter_src_dev != src); 107 } 108 109 static struct coresight_connection * 110 coresight_find_out_connection(struct coresight_device *csdev, 111 struct coresight_device *out_dev, 112 struct coresight_device *trace_src) 113 { 114 int i; 115 struct coresight_connection *conn; 116 117 for (i = 0; i < csdev->pdata->nr_outconns; i++) { 118 conn = csdev->pdata->out_conns[i]; 119 if (coresight_blocks_source(trace_src, conn)) 120 continue; 121 if (conn->dest_dev == out_dev) 122 return conn; 123 } 124 125 dev_err(&csdev->dev, 126 "couldn't find output connection, csdev: %s, out_dev: %s\n", 127 dev_name(&csdev->dev), dev_name(&out_dev->dev)); 128 129 return ERR_PTR(-ENODEV); 130 } 131 132 static inline u32 coresight_read_claim_tags(struct coresight_device *csdev) 133 { 134 return csdev_access_relaxed_read32(&csdev->access, CORESIGHT_CLAIMCLR); 135 } 136 137 static inline bool coresight_is_claimed_self_hosted(struct coresight_device *csdev) 138 { 139 return coresight_read_claim_tags(csdev) == CORESIGHT_CLAIM_SELF_HOSTED; 140 } 141 142 static inline bool coresight_is_claimed_any(struct coresight_device *csdev) 143 { 144 return coresight_read_claim_tags(csdev) != 0; 145 } 146 147 static inline void coresight_set_claim_tags(struct coresight_device *csdev) 148 { 149 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED, 150 CORESIGHT_CLAIMSET); 151 isb(); 152 } 153 154 static inline void coresight_clear_claim_tags(struct coresight_device *csdev) 155 { 156 csdev_access_relaxed_write32(&csdev->access, CORESIGHT_CLAIM_SELF_HOSTED, 157 CORESIGHT_CLAIMCLR); 158 isb(); 159 } 160 161 /* 162 * coresight_claim_device_unlocked : Claim the device for self-hosted usage 163 * to prevent an external tool from touching this device. As per PSCI 164 * standards, section "Preserving the execution context" => "Debug and Trace 165 * save and Restore", DBGCLAIM[1] is reserved for Self-hosted debug/trace and 166 * DBGCLAIM[0] is reserved for external tools. 167 * 168 * Called with CS_UNLOCKed for the component. 169 * Returns : 0 on success 170 */ 171 int coresight_claim_device_unlocked(struct coresight_device *csdev) 172 { 173 if (WARN_ON(!csdev)) 174 return -EINVAL; 175 176 if (coresight_is_claimed_any(csdev)) 177 return -EBUSY; 178 179 coresight_set_claim_tags(csdev); 180 if (coresight_is_claimed_self_hosted(csdev)) 181 return 0; 182 /* There was a race setting the tags, clean up and fail */ 183 coresight_clear_claim_tags(csdev); 184 return -EBUSY; 185 } 186 EXPORT_SYMBOL_GPL(coresight_claim_device_unlocked); 187 188 int coresight_claim_device(struct coresight_device *csdev) 189 { 190 int rc; 191 192 if (WARN_ON(!csdev)) 193 return -EINVAL; 194 195 CS_UNLOCK(csdev->access.base); 196 rc = coresight_claim_device_unlocked(csdev); 197 CS_LOCK(csdev->access.base); 198 199 return rc; 200 } 201 EXPORT_SYMBOL_GPL(coresight_claim_device); 202 203 /* 204 * coresight_disclaim_device_unlocked : Clear the claim tags for the device. 205 * Called with CS_UNLOCKed for the component. 206 */ 207 void coresight_disclaim_device_unlocked(struct coresight_device *csdev) 208 { 209 210 if (WARN_ON(!csdev)) 211 return; 212 213 if (coresight_is_claimed_self_hosted(csdev)) 214 coresight_clear_claim_tags(csdev); 215 else 216 /* 217 * The external agent may have not honoured our claim 218 * and has manipulated it. Or something else has seriously 219 * gone wrong in our driver. 220 */ 221 WARN_ON_ONCE(1); 222 } 223 EXPORT_SYMBOL_GPL(coresight_disclaim_device_unlocked); 224 225 void coresight_disclaim_device(struct coresight_device *csdev) 226 { 227 if (WARN_ON(!csdev)) 228 return; 229 230 CS_UNLOCK(csdev->access.base); 231 coresight_disclaim_device_unlocked(csdev); 232 CS_LOCK(csdev->access.base); 233 } 234 EXPORT_SYMBOL_GPL(coresight_disclaim_device); 235 236 /* 237 * Add a helper as an output device. This function takes the @coresight_mutex 238 * because it's assumed that it's called from the helper device, outside of the 239 * core code where the mutex would already be held. Don't add new calls to this 240 * from inside the core code, instead try to add the new helper to the DT and 241 * ACPI where it will be picked up and linked automatically. 242 */ 243 void coresight_add_helper(struct coresight_device *csdev, 244 struct coresight_device *helper) 245 { 246 int i; 247 struct coresight_connection conn = {}; 248 struct coresight_connection *new_conn; 249 250 mutex_lock(&coresight_mutex); 251 conn.dest_fwnode = fwnode_handle_get(dev_fwnode(&helper->dev)); 252 conn.dest_dev = helper; 253 conn.dest_port = conn.src_port = -1; 254 conn.src_dev = csdev; 255 256 /* 257 * Check for duplicates because this is called every time a helper 258 * device is re-loaded. Existing connections will get re-linked 259 * automatically. 260 */ 261 for (i = 0; i < csdev->pdata->nr_outconns; ++i) 262 if (csdev->pdata->out_conns[i]->dest_fwnode == conn.dest_fwnode) 263 goto unlock; 264 265 new_conn = coresight_add_out_conn(csdev->dev.parent, csdev->pdata, 266 &conn); 267 if (!IS_ERR(new_conn)) 268 coresight_add_in_conn(new_conn); 269 270 unlock: 271 mutex_unlock(&coresight_mutex); 272 } 273 EXPORT_SYMBOL_GPL(coresight_add_helper); 274 275 static int coresight_enable_sink(struct coresight_device *csdev, 276 enum cs_mode mode, void *data) 277 { 278 return sink_ops(csdev)->enable(csdev, mode, data); 279 } 280 281 static void coresight_disable_sink(struct coresight_device *csdev) 282 { 283 sink_ops(csdev)->disable(csdev); 284 } 285 286 static int coresight_enable_link(struct coresight_device *csdev, 287 struct coresight_device *parent, 288 struct coresight_device *child, 289 struct coresight_device *source) 290 { 291 int link_subtype; 292 struct coresight_connection *inconn, *outconn; 293 294 if (!parent || !child) 295 return -EINVAL; 296 297 inconn = coresight_find_out_connection(parent, csdev, source); 298 outconn = coresight_find_out_connection(csdev, child, source); 299 link_subtype = csdev->subtype.link_subtype; 300 301 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG && IS_ERR(inconn)) 302 return PTR_ERR(inconn); 303 if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT && IS_ERR(outconn)) 304 return PTR_ERR(outconn); 305 306 return link_ops(csdev)->enable(csdev, inconn, outconn); 307 } 308 309 static void coresight_disable_link(struct coresight_device *csdev, 310 struct coresight_device *parent, 311 struct coresight_device *child, 312 struct coresight_device *source) 313 { 314 struct coresight_connection *inconn, *outconn; 315 316 if (!parent || !child) 317 return; 318 319 inconn = coresight_find_out_connection(parent, csdev, source); 320 outconn = coresight_find_out_connection(csdev, child, source); 321 322 link_ops(csdev)->disable(csdev, inconn, outconn); 323 } 324 325 static bool coresight_is_helper(struct coresight_device *csdev) 326 { 327 return csdev->type == CORESIGHT_DEV_TYPE_HELPER; 328 } 329 330 static int coresight_enable_helper(struct coresight_device *csdev, 331 enum cs_mode mode, void *data) 332 { 333 return helper_ops(csdev)->enable(csdev, mode, data); 334 } 335 336 static void coresight_disable_helper(struct coresight_device *csdev, void *data) 337 { 338 helper_ops(csdev)->disable(csdev, data); 339 } 340 341 static void coresight_disable_helpers(struct coresight_device *csdev, void *data) 342 { 343 int i; 344 struct coresight_device *helper; 345 346 for (i = 0; i < csdev->pdata->nr_outconns; ++i) { 347 helper = csdev->pdata->out_conns[i]->dest_dev; 348 if (helper && coresight_is_helper(helper)) 349 coresight_disable_helper(helper, data); 350 } 351 } 352 353 /* 354 * Helper function to call source_ops(csdev)->disable and also disable the 355 * helpers. 356 * 357 * There is an imbalance between coresight_enable_path() and 358 * coresight_disable_path(). Enabling also enables the source's helpers as part 359 * of the path, but disabling always skips the first item in the path (which is 360 * the source), so sources and their helpers don't get disabled as part of that 361 * function and we need the extra step here. 362 */ 363 void coresight_disable_source(struct coresight_device *csdev, void *data) 364 { 365 source_ops(csdev)->disable(csdev, data); 366 coresight_disable_helpers(csdev, NULL); 367 } 368 EXPORT_SYMBOL_GPL(coresight_disable_source); 369 370 /* 371 * coresight_disable_path_from : Disable components in the given path beyond 372 * @nd in the list. If @nd is NULL, all the components, except the SOURCE are 373 * disabled. 374 */ 375 static void coresight_disable_path_from(struct coresight_path *path, 376 struct coresight_node *nd) 377 { 378 u32 type; 379 struct coresight_device *csdev, *parent, *child; 380 381 if (!nd) 382 nd = list_first_entry(&path->path_list, struct coresight_node, link); 383 384 list_for_each_entry_continue(nd, &path->path_list, link) { 385 csdev = nd->csdev; 386 type = csdev->type; 387 388 /* 389 * ETF devices are tricky... They can be a link or a sink, 390 * depending on how they are configured. If an ETF has been 391 * selected as a sink it will be configured as a sink, otherwise 392 * go ahead with the link configuration. 393 */ 394 if (type == CORESIGHT_DEV_TYPE_LINKSINK) 395 type = (csdev == coresight_get_sink(path)) ? 396 CORESIGHT_DEV_TYPE_SINK : 397 CORESIGHT_DEV_TYPE_LINK; 398 399 switch (type) { 400 case CORESIGHT_DEV_TYPE_SINK: 401 coresight_disable_sink(csdev); 402 break; 403 case CORESIGHT_DEV_TYPE_SOURCE: 404 /* 405 * We skip the first node in the path assuming that it 406 * is the source. So we don't expect a source device in 407 * the middle of a path. 408 */ 409 WARN_ON(1); 410 break; 411 case CORESIGHT_DEV_TYPE_LINK: 412 parent = list_prev_entry(nd, link)->csdev; 413 child = list_next_entry(nd, link)->csdev; 414 coresight_disable_link(csdev, parent, child, 415 coresight_get_source(path)); 416 break; 417 default: 418 break; 419 } 420 421 /* Disable all helpers adjacent along the path last */ 422 coresight_disable_helpers(csdev, path); 423 } 424 } 425 426 void coresight_disable_path(struct coresight_path *path) 427 { 428 coresight_disable_path_from(path, NULL); 429 } 430 EXPORT_SYMBOL_GPL(coresight_disable_path); 431 432 static int coresight_enable_helpers(struct coresight_device *csdev, 433 enum cs_mode mode, void *data) 434 { 435 int i, ret = 0; 436 struct coresight_device *helper; 437 438 for (i = 0; i < csdev->pdata->nr_outconns; ++i) { 439 helper = csdev->pdata->out_conns[i]->dest_dev; 440 if (!helper || !coresight_is_helper(helper)) 441 continue; 442 443 ret = coresight_enable_helper(helper, mode, data); 444 if (ret) 445 return ret; 446 } 447 448 return 0; 449 } 450 451 int coresight_enable_path(struct coresight_path *path, enum cs_mode mode, 452 void *sink_data) 453 { 454 int ret = 0; 455 u32 type; 456 struct coresight_node *nd; 457 struct coresight_device *csdev, *parent, *child; 458 struct coresight_device *source; 459 460 source = coresight_get_source(path); 461 list_for_each_entry_reverse(nd, &path->path_list, link) { 462 csdev = nd->csdev; 463 type = csdev->type; 464 465 /* Enable all helpers adjacent to the path first */ 466 ret = coresight_enable_helpers(csdev, mode, path); 467 if (ret) 468 goto err; 469 /* 470 * ETF devices are tricky... They can be a link or a sink, 471 * depending on how they are configured. If an ETF has been 472 * selected as a sink it will be configured as a sink, otherwise 473 * go ahead with the link configuration. 474 */ 475 if (type == CORESIGHT_DEV_TYPE_LINKSINK) 476 type = (csdev == coresight_get_sink(path)) ? 477 CORESIGHT_DEV_TYPE_SINK : 478 CORESIGHT_DEV_TYPE_LINK; 479 480 switch (type) { 481 case CORESIGHT_DEV_TYPE_SINK: 482 ret = coresight_enable_sink(csdev, mode, sink_data); 483 /* 484 * Sink is the first component turned on. If we 485 * failed to enable the sink, there are no components 486 * that need disabling. Disabling the path here 487 * would mean we could disrupt an existing session. 488 */ 489 if (ret) 490 goto out; 491 break; 492 case CORESIGHT_DEV_TYPE_SOURCE: 493 /* sources are enabled from either sysFS or Perf */ 494 break; 495 case CORESIGHT_DEV_TYPE_LINK: 496 parent = list_prev_entry(nd, link)->csdev; 497 child = list_next_entry(nd, link)->csdev; 498 ret = coresight_enable_link(csdev, parent, child, source); 499 if (ret) 500 goto err; 501 break; 502 default: 503 goto err; 504 } 505 } 506 507 out: 508 return ret; 509 err: 510 coresight_disable_path_from(path, nd); 511 goto out; 512 } 513 514 struct coresight_device *coresight_get_sink(struct coresight_path *path) 515 { 516 struct coresight_device *csdev; 517 518 if (!path) 519 return NULL; 520 521 csdev = list_last_entry(&path->path_list, struct coresight_node, link)->csdev; 522 if (csdev->type != CORESIGHT_DEV_TYPE_SINK && 523 csdev->type != CORESIGHT_DEV_TYPE_LINKSINK) 524 return NULL; 525 526 return csdev; 527 } 528 EXPORT_SYMBOL_GPL(coresight_get_sink); 529 530 u32 coresight_get_sink_id(struct coresight_device *csdev) 531 { 532 if (!csdev->ea) 533 return 0; 534 535 /* 536 * See function etm_perf_add_symlink_sink() to know where 537 * this comes from. 538 */ 539 return (u32) (unsigned long) csdev->ea->var; 540 } 541 542 static int coresight_sink_by_id(struct device *dev, const void *data) 543 { 544 struct coresight_device *csdev = to_coresight_device(dev); 545 546 if (csdev->type == CORESIGHT_DEV_TYPE_SINK || 547 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) { 548 if (coresight_get_sink_id(csdev) == *(u32 *)data) 549 return 1; 550 } 551 552 return 0; 553 } 554 555 /** 556 * coresight_get_sink_by_id - returns the sink that matches the id 557 * @id: Id of the sink to match 558 * 559 * The name of a sink is unique, whether it is found on the AMBA bus or 560 * otherwise. As such the hash of that name can easily be used to identify 561 * a sink. 562 */ 563 struct coresight_device *coresight_get_sink_by_id(u32 id) 564 { 565 struct device *dev = NULL; 566 567 dev = bus_find_device(&coresight_bustype, NULL, &id, 568 coresight_sink_by_id); 569 570 return dev ? to_coresight_device(dev) : NULL; 571 } 572 573 /** 574 * coresight_get_ref- Helper function to increase reference count to module 575 * and device. 576 * 577 * @csdev: The coresight device to get a reference on. 578 * 579 * Return true in successful case and power up the device. 580 * Return false when failed to get reference of module. 581 */ 582 static inline bool coresight_get_ref(struct coresight_device *csdev) 583 { 584 struct device *dev = csdev->dev.parent; 585 586 /* Make sure the driver can't be removed */ 587 if (!try_module_get(dev->driver->owner)) 588 return false; 589 /* Make sure the device can't go away */ 590 get_device(dev); 591 pm_runtime_get_sync(dev); 592 return true; 593 } 594 595 /** 596 * coresight_put_ref- Helper function to decrease reference count to module 597 * and device. Power off the device. 598 * 599 * @csdev: The coresight device to decrement a reference from. 600 */ 601 static inline void coresight_put_ref(struct coresight_device *csdev) 602 { 603 struct device *dev = csdev->dev.parent; 604 605 pm_runtime_put(dev); 606 put_device(dev); 607 module_put(dev->driver->owner); 608 } 609 610 /* 611 * coresight_grab_device - Power up this device and any of the helper 612 * devices connected to it for trace operation. Since the helper devices 613 * don't appear on the trace path, they should be handled along with the 614 * master device. 615 */ 616 static int coresight_grab_device(struct coresight_device *csdev) 617 { 618 int i; 619 620 for (i = 0; i < csdev->pdata->nr_outconns; i++) { 621 struct coresight_device *child; 622 623 child = csdev->pdata->out_conns[i]->dest_dev; 624 if (child && coresight_is_helper(child)) 625 if (!coresight_get_ref(child)) 626 goto err; 627 } 628 if (coresight_get_ref(csdev)) 629 return 0; 630 err: 631 for (i--; i >= 0; i--) { 632 struct coresight_device *child; 633 634 child = csdev->pdata->out_conns[i]->dest_dev; 635 if (child && coresight_is_helper(child)) 636 coresight_put_ref(child); 637 } 638 return -ENODEV; 639 } 640 641 /* 642 * coresight_drop_device - Release this device and any of the helper 643 * devices connected to it. 644 */ 645 static void coresight_drop_device(struct coresight_device *csdev) 646 { 647 int i; 648 649 coresight_put_ref(csdev); 650 for (i = 0; i < csdev->pdata->nr_outconns; i++) { 651 struct coresight_device *child; 652 653 child = csdev->pdata->out_conns[i]->dest_dev; 654 if (child && coresight_is_helper(child)) 655 coresight_put_ref(child); 656 } 657 } 658 659 /* 660 * coresight device will read their existing or alloc a trace ID, if their trace_id 661 * callback is set. 662 * 663 * Return 0 if the trace_id callback is not set. 664 * Return the result of the trace_id callback if it is set. The return value 665 * will be the trace_id if successful, and an error number if it fails. 666 */ 667 static int coresight_get_trace_id(struct coresight_device *csdev, 668 enum cs_mode mode, 669 struct coresight_device *sink) 670 { 671 if (coresight_ops(csdev)->trace_id) 672 return coresight_ops(csdev)->trace_id(csdev, mode, sink); 673 674 return 0; 675 } 676 677 /* 678 * Call this after creating the path and before enabling it. This leaves 679 * the trace ID set on the path, or it remains 0 if it couldn't be assigned. 680 */ 681 void coresight_path_assign_trace_id(struct coresight_path *path, 682 enum cs_mode mode) 683 { 684 struct coresight_device *sink = coresight_get_sink(path); 685 struct coresight_node *nd; 686 int trace_id; 687 688 list_for_each_entry(nd, &path->path_list, link) { 689 /* Assign a trace ID to the path for the first device that wants to do it */ 690 trace_id = coresight_get_trace_id(nd->csdev, mode, sink); 691 692 /* 693 * 0 in this context is that it didn't want to assign so keep searching. 694 * Non 0 is either success or fail. 695 */ 696 if (trace_id != 0) { 697 path->trace_id = trace_id; 698 return; 699 } 700 } 701 } 702 703 /** 704 * _coresight_build_path - recursively build a path from a @csdev to a sink. 705 * @csdev: The device to start from. 706 * @source: The trace source device of the path. 707 * @sink: The final sink we want in this path. 708 * @path: The list to add devices to. 709 * 710 * The tree of Coresight device is traversed until @sink is found. 711 * From there the sink is added to the list along with all the devices that led 712 * to that point - the end result is a list from source to sink. In that list 713 * the source is the first device and the sink the last one. 714 */ 715 static int _coresight_build_path(struct coresight_device *csdev, 716 struct coresight_device *source, 717 struct coresight_device *sink, 718 struct coresight_path *path) 719 { 720 int i, ret; 721 bool found = false; 722 struct coresight_node *node; 723 724 /* The sink has been found. Enqueue the element */ 725 if (csdev == sink) 726 goto out; 727 728 if (coresight_is_percpu_source(csdev) && coresight_is_percpu_sink(sink) && 729 sink == per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev))) { 730 if (_coresight_build_path(sink, source, sink, path) == 0) { 731 found = true; 732 goto out; 733 } 734 } 735 736 /* Not a sink - recursively explore each port found on this element */ 737 for (i = 0; i < csdev->pdata->nr_outconns; i++) { 738 struct coresight_device *child_dev; 739 740 child_dev = csdev->pdata->out_conns[i]->dest_dev; 741 742 if (coresight_blocks_source(source, csdev->pdata->out_conns[i])) 743 continue; 744 745 if (child_dev && 746 _coresight_build_path(child_dev, source, sink, path) == 0) { 747 found = true; 748 break; 749 } 750 } 751 752 if (!found) 753 return -ENODEV; 754 755 out: 756 /* 757 * A path from this element to a sink has been found. The elements 758 * leading to the sink are already enqueued, all that is left to do 759 * is tell the PM runtime core we need this element and add a node 760 * for it. 761 */ 762 ret = coresight_grab_device(csdev); 763 if (ret) 764 return ret; 765 766 node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL); 767 if (!node) 768 return -ENOMEM; 769 770 node->csdev = csdev; 771 list_add(&node->link, &path->path_list); 772 773 return 0; 774 } 775 776 struct coresight_path *coresight_build_path(struct coresight_device *source, 777 struct coresight_device *sink) 778 { 779 struct coresight_path *path; 780 int rc; 781 782 if (!sink) 783 return ERR_PTR(-EINVAL); 784 785 path = kzalloc(sizeof(struct coresight_path), GFP_KERNEL); 786 if (!path) 787 return ERR_PTR(-ENOMEM); 788 789 INIT_LIST_HEAD(&path->path_list); 790 791 rc = _coresight_build_path(source, source, sink, path); 792 if (rc) { 793 kfree(path); 794 return ERR_PTR(rc); 795 } 796 797 return path; 798 } 799 800 /** 801 * coresight_release_path - release a previously built path. 802 * @path: the path to release. 803 * 804 * Go through all the elements of a path and 1) removed it from the list and 805 * 2) free the memory allocated for each node. 806 */ 807 void coresight_release_path(struct coresight_path *path) 808 { 809 struct coresight_device *csdev; 810 struct coresight_node *nd, *next; 811 812 list_for_each_entry_safe(nd, next, &path->path_list, link) { 813 csdev = nd->csdev; 814 815 coresight_drop_device(csdev); 816 list_del(&nd->link); 817 kfree(nd); 818 } 819 820 kfree(path); 821 } 822 823 /* return true if the device is a suitable type for a default sink */ 824 static inline bool coresight_is_def_sink_type(struct coresight_device *csdev) 825 { 826 /* sink & correct subtype */ 827 if (((csdev->type == CORESIGHT_DEV_TYPE_SINK) || 828 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) && 829 (csdev->subtype.sink_subtype >= CORESIGHT_DEV_SUBTYPE_SINK_BUFFER)) 830 return true; 831 return false; 832 } 833 834 /** 835 * coresight_select_best_sink - return the best sink for use as default from 836 * the two provided. 837 * 838 * @sink: current best sink. 839 * @depth: search depth where current sink was found. 840 * @new_sink: new sink for comparison with current sink. 841 * @new_depth: search depth where new sink was found. 842 * 843 * Sinks prioritised according to coresight_dev_subtype_sink, with only 844 * subtypes CORESIGHT_DEV_SUBTYPE_SINK_BUFFER or higher being used. 845 * 846 * Where two sinks of equal priority are found, the sink closest to the 847 * source is used (smallest search depth). 848 * 849 * return @new_sink & update @depth if better than @sink, else return @sink. 850 */ 851 static struct coresight_device * 852 coresight_select_best_sink(struct coresight_device *sink, int *depth, 853 struct coresight_device *new_sink, int new_depth) 854 { 855 bool update = false; 856 857 if (!sink) { 858 /* first found at this level */ 859 update = true; 860 } else if (new_sink->subtype.sink_subtype > 861 sink->subtype.sink_subtype) { 862 /* found better sink */ 863 update = true; 864 } else if ((new_sink->subtype.sink_subtype == 865 sink->subtype.sink_subtype) && 866 (*depth > new_depth)) { 867 /* found same but closer sink */ 868 update = true; 869 } 870 871 if (update) 872 *depth = new_depth; 873 return update ? new_sink : sink; 874 } 875 876 /** 877 * coresight_find_sink - recursive function to walk trace connections from 878 * source to find a suitable default sink. 879 * 880 * @csdev: source / current device to check. 881 * @depth: [in] search depth of calling dev, [out] depth of found sink. 882 * 883 * This will walk the connection path from a source (ETM) till a suitable 884 * sink is encountered and return that sink to the original caller. 885 * 886 * If current device is a plain sink return that & depth, otherwise recursively 887 * call child connections looking for a sink. Select best possible using 888 * coresight_select_best_sink. 889 * 890 * return best sink found, or NULL if not found at this node or child nodes. 891 */ 892 static struct coresight_device * 893 coresight_find_sink(struct coresight_device *csdev, int *depth) 894 { 895 int i, curr_depth = *depth + 1, found_depth = 0; 896 struct coresight_device *found_sink = NULL; 897 898 if (coresight_is_def_sink_type(csdev)) { 899 found_depth = curr_depth; 900 found_sink = csdev; 901 if (csdev->type == CORESIGHT_DEV_TYPE_SINK) 902 goto return_def_sink; 903 /* look past LINKSINK for something better */ 904 } 905 906 /* 907 * Not a sink we want - or possible child sink may be better. 908 * recursively explore each port found on this element. 909 */ 910 for (i = 0; i < csdev->pdata->nr_outconns; i++) { 911 struct coresight_device *child_dev, *sink = NULL; 912 int child_depth = curr_depth; 913 914 child_dev = csdev->pdata->out_conns[i]->dest_dev; 915 if (child_dev) 916 sink = coresight_find_sink(child_dev, &child_depth); 917 918 if (sink) 919 found_sink = coresight_select_best_sink(found_sink, 920 &found_depth, 921 sink, 922 child_depth); 923 } 924 925 return_def_sink: 926 /* return found sink and depth */ 927 if (found_sink) 928 *depth = found_depth; 929 return found_sink; 930 } 931 932 /** 933 * coresight_find_default_sink: Find a sink suitable for use as a 934 * default sink. 935 * 936 * @csdev: starting source to find a connected sink. 937 * 938 * Walks connections graph looking for a suitable sink to enable for the 939 * supplied source. Uses CoreSight device subtypes and distance from source 940 * to select the best sink. 941 * 942 * If a sink is found, then the default sink for this device is set and 943 * will be automatically used in future. 944 * 945 * Used in cases where the CoreSight user (perf / sysfs) has not selected a 946 * sink. 947 */ 948 struct coresight_device * 949 coresight_find_default_sink(struct coresight_device *csdev) 950 { 951 int depth = 0; 952 953 /* look for a default sink if we have not found for this device */ 954 if (!csdev->def_sink) { 955 if (coresight_is_percpu_source(csdev)) 956 csdev->def_sink = per_cpu(csdev_sink, source_ops(csdev)->cpu_id(csdev)); 957 if (!csdev->def_sink) 958 csdev->def_sink = coresight_find_sink(csdev, &depth); 959 } 960 return csdev->def_sink; 961 } 962 963 static int coresight_remove_sink_ref(struct device *dev, void *data) 964 { 965 struct coresight_device *sink = data; 966 struct coresight_device *source = to_coresight_device(dev); 967 968 if (source->def_sink == sink) 969 source->def_sink = NULL; 970 return 0; 971 } 972 973 /** 974 * coresight_clear_default_sink: Remove all default sink references to the 975 * supplied sink. 976 * 977 * If supplied device is a sink, then check all the bus devices and clear 978 * out all the references to this sink from the coresight_device def_sink 979 * parameter. 980 * 981 * @csdev: coresight sink - remove references to this from all sources. 982 */ 983 static void coresight_clear_default_sink(struct coresight_device *csdev) 984 { 985 if ((csdev->type == CORESIGHT_DEV_TYPE_SINK) || 986 (csdev->type == CORESIGHT_DEV_TYPE_LINKSINK)) { 987 bus_for_each_dev(&coresight_bustype, NULL, csdev, 988 coresight_remove_sink_ref); 989 } 990 } 991 992 static void coresight_device_release(struct device *dev) 993 { 994 struct coresight_device *csdev = to_coresight_device(dev); 995 996 fwnode_handle_put(csdev->dev.fwnode); 997 free_percpu(csdev->perf_sink_id_map.cpu_map); 998 kfree(csdev); 999 } 1000 1001 static int coresight_orphan_match(struct device *dev, void *data) 1002 { 1003 int i, ret = 0; 1004 bool still_orphan = false; 1005 struct coresight_device *dst_csdev = data; 1006 struct coresight_device *src_csdev = to_coresight_device(dev); 1007 struct coresight_connection *conn; 1008 bool fixup_self = (src_csdev == dst_csdev); 1009 1010 /* Move on to another component if no connection is orphan */ 1011 if (!src_csdev->orphan) 1012 return 0; 1013 /* 1014 * Circle through all the connections of that component. If we find 1015 * an orphan connection whose name matches @dst_csdev, link it. 1016 */ 1017 for (i = 0; i < src_csdev->pdata->nr_outconns; i++) { 1018 conn = src_csdev->pdata->out_conns[i]; 1019 1020 /* Fix filter source device before skip the port */ 1021 if (conn->filter_src_fwnode && !conn->filter_src_dev) { 1022 if (dst_csdev && 1023 (conn->filter_src_fwnode == dst_csdev->dev.fwnode) && 1024 !WARN_ON_ONCE(!coresight_is_device_source(dst_csdev))) 1025 conn->filter_src_dev = dst_csdev; 1026 else 1027 still_orphan = true; 1028 } 1029 1030 /* Skip the port if it's already connected. */ 1031 if (conn->dest_dev) 1032 continue; 1033 1034 /* 1035 * If we are at the "new" device, which triggered this search, 1036 * we must find the remote device from the fwnode in the 1037 * connection. 1038 */ 1039 if (fixup_self) 1040 dst_csdev = coresight_find_csdev_by_fwnode( 1041 conn->dest_fwnode); 1042 1043 /* Does it match this newly added device? */ 1044 if (dst_csdev && conn->dest_fwnode == dst_csdev->dev.fwnode) { 1045 ret = coresight_make_links(src_csdev, conn, dst_csdev); 1046 if (ret) 1047 return ret; 1048 1049 /* 1050 * Install the device connection. This also indicates that 1051 * the links are operational on both ends. 1052 */ 1053 conn->dest_dev = dst_csdev; 1054 conn->src_dev = src_csdev; 1055 1056 ret = coresight_add_in_conn(conn); 1057 if (ret) 1058 return ret; 1059 } else { 1060 /* This component still has an orphan */ 1061 still_orphan = true; 1062 } 1063 } 1064 1065 src_csdev->orphan = still_orphan; 1066 1067 /* 1068 * Returning '0' in case we didn't encounter any error, 1069 * ensures that all known component on the bus will be checked. 1070 */ 1071 return 0; 1072 } 1073 1074 static int coresight_fixup_orphan_conns(struct coresight_device *csdev) 1075 { 1076 return bus_for_each_dev(&coresight_bustype, NULL, 1077 csdev, coresight_orphan_match); 1078 } 1079 1080 static int coresight_clear_filter_source(struct device *dev, void *data) 1081 { 1082 int i; 1083 struct coresight_device *source = data; 1084 struct coresight_device *csdev = to_coresight_device(dev); 1085 1086 for (i = 0; i < csdev->pdata->nr_outconns; ++i) { 1087 if (csdev->pdata->out_conns[i]->filter_src_dev == source) 1088 csdev->pdata->out_conns[i]->filter_src_dev = NULL; 1089 } 1090 return 0; 1091 } 1092 1093 /* coresight_remove_conns - Remove other device's references to this device */ 1094 static void coresight_remove_conns(struct coresight_device *csdev) 1095 { 1096 int i, j; 1097 struct coresight_connection *conn; 1098 1099 if (coresight_is_device_source(csdev)) 1100 bus_for_each_dev(&coresight_bustype, NULL, csdev, 1101 coresight_clear_filter_source); 1102 1103 /* 1104 * Remove the input connection references from the destination device 1105 * for each output connection. 1106 */ 1107 for (i = 0; i < csdev->pdata->nr_outconns; i++) { 1108 conn = csdev->pdata->out_conns[i]; 1109 if (conn->filter_src_fwnode) { 1110 conn->filter_src_dev = NULL; 1111 fwnode_handle_put(conn->filter_src_fwnode); 1112 } 1113 1114 if (!conn->dest_dev) 1115 continue; 1116 1117 for (j = 0; j < conn->dest_dev->pdata->nr_inconns; ++j) 1118 if (conn->dest_dev->pdata->in_conns[j] == conn) { 1119 conn->dest_dev->pdata->in_conns[j] = NULL; 1120 break; 1121 } 1122 } 1123 1124 /* 1125 * For all input connections, remove references to this device. 1126 * Connection objects are shared so modifying this device's input 1127 * connections affects the other device's output connection. 1128 */ 1129 for (i = 0; i < csdev->pdata->nr_inconns; ++i) { 1130 conn = csdev->pdata->in_conns[i]; 1131 /* Input conns array is sparse */ 1132 if (!conn) 1133 continue; 1134 1135 conn->src_dev->orphan = true; 1136 coresight_remove_links(conn->src_dev, conn); 1137 conn->dest_dev = NULL; 1138 } 1139 } 1140 1141 /** 1142 * coresight_timeout_action - loop until a bit has changed to a specific register 1143 * state, with a callback after every trial. 1144 * @csa: coresight device access for the device 1145 * @offset: Offset of the register from the base of the device. 1146 * @position: the position of the bit of interest. 1147 * @value: the value the bit should have. 1148 * @cb: Call back after each trial. 1149 * 1150 * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if 1151 * TIMEOUT_US has elapsed, which ever happens first. 1152 */ 1153 int coresight_timeout_action(struct csdev_access *csa, u32 offset, 1154 int position, int value, 1155 coresight_timeout_cb_t cb) 1156 { 1157 int i; 1158 u32 val; 1159 1160 for (i = TIMEOUT_US; i > 0; i--) { 1161 val = csdev_access_read32(csa, offset); 1162 /* waiting on the bit to go from 0 to 1 */ 1163 if (value) { 1164 if (val & BIT(position)) 1165 return 0; 1166 /* waiting on the bit to go from 1 to 0 */ 1167 } else { 1168 if (!(val & BIT(position))) 1169 return 0; 1170 } 1171 if (cb) 1172 cb(csa, offset, position, value); 1173 /* 1174 * Delay is arbitrary - the specification doesn't say how long 1175 * we are expected to wait. Extra check required to make sure 1176 * we don't wait needlessly on the last iteration. 1177 */ 1178 if (i - 1) 1179 udelay(1); 1180 } 1181 1182 return -EAGAIN; 1183 } 1184 EXPORT_SYMBOL_GPL(coresight_timeout_action); 1185 1186 int coresight_timeout(struct csdev_access *csa, u32 offset, 1187 int position, int value) 1188 { 1189 return coresight_timeout_action(csa, offset, position, value, NULL); 1190 } 1191 EXPORT_SYMBOL_GPL(coresight_timeout); 1192 1193 u32 coresight_relaxed_read32(struct coresight_device *csdev, u32 offset) 1194 { 1195 return csdev_access_relaxed_read32(&csdev->access, offset); 1196 } 1197 1198 u32 coresight_read32(struct coresight_device *csdev, u32 offset) 1199 { 1200 return csdev_access_read32(&csdev->access, offset); 1201 } 1202 1203 void coresight_relaxed_write32(struct coresight_device *csdev, 1204 u32 val, u32 offset) 1205 { 1206 csdev_access_relaxed_write32(&csdev->access, val, offset); 1207 } 1208 1209 void coresight_write32(struct coresight_device *csdev, u32 val, u32 offset) 1210 { 1211 csdev_access_write32(&csdev->access, val, offset); 1212 } 1213 1214 u64 coresight_relaxed_read64(struct coresight_device *csdev, u32 offset) 1215 { 1216 return csdev_access_relaxed_read64(&csdev->access, offset); 1217 } 1218 1219 u64 coresight_read64(struct coresight_device *csdev, u32 offset) 1220 { 1221 return csdev_access_read64(&csdev->access, offset); 1222 } 1223 1224 void coresight_relaxed_write64(struct coresight_device *csdev, 1225 u64 val, u32 offset) 1226 { 1227 csdev_access_relaxed_write64(&csdev->access, val, offset); 1228 } 1229 1230 void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset) 1231 { 1232 csdev_access_write64(&csdev->access, val, offset); 1233 } 1234 1235 /* 1236 * coresight_release_platform_data: Release references to the devices connected 1237 * to the output port of this device. 1238 */ 1239 void coresight_release_platform_data(struct coresight_device *csdev, 1240 struct device *dev, 1241 struct coresight_platform_data *pdata) 1242 { 1243 int i; 1244 struct coresight_connection **conns = pdata->out_conns; 1245 1246 for (i = 0; i < pdata->nr_outconns; i++) { 1247 /* If we have made the links, remove them now */ 1248 if (csdev && conns[i]->dest_dev) 1249 coresight_remove_links(csdev, conns[i]); 1250 /* 1251 * Drop the refcount and clear the handle as this device 1252 * is going away 1253 */ 1254 fwnode_handle_put(conns[i]->dest_fwnode); 1255 conns[i]->dest_fwnode = NULL; 1256 devm_kfree(dev, conns[i]); 1257 } 1258 devm_kfree(dev, pdata->out_conns); 1259 devm_kfree(dev, pdata->in_conns); 1260 devm_kfree(dev, pdata); 1261 if (csdev) 1262 coresight_remove_conns_sysfs_group(csdev); 1263 } 1264 1265 struct coresight_device *coresight_register(struct coresight_desc *desc) 1266 { 1267 int ret; 1268 struct coresight_device *csdev; 1269 bool registered = false; 1270 1271 csdev = kzalloc(sizeof(*csdev), GFP_KERNEL); 1272 if (!csdev) { 1273 ret = -ENOMEM; 1274 goto err_out; 1275 } 1276 1277 csdev->pdata = desc->pdata; 1278 1279 csdev->type = desc->type; 1280 csdev->subtype = desc->subtype; 1281 csdev->ops = desc->ops; 1282 csdev->access = desc->access; 1283 csdev->orphan = true; 1284 1285 csdev->dev.type = &coresight_dev_type[desc->type]; 1286 csdev->dev.groups = desc->groups; 1287 csdev->dev.parent = desc->dev; 1288 csdev->dev.release = coresight_device_release; 1289 csdev->dev.bus = &coresight_bustype; 1290 /* 1291 * Hold the reference to our parent device. This will be 1292 * dropped only in coresight_device_release(). 1293 */ 1294 csdev->dev.fwnode = fwnode_handle_get(dev_fwnode(desc->dev)); 1295 dev_set_name(&csdev->dev, "%s", desc->name); 1296 1297 if (csdev->type == CORESIGHT_DEV_TYPE_SINK || 1298 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) { 1299 raw_spin_lock_init(&csdev->perf_sink_id_map.lock); 1300 csdev->perf_sink_id_map.cpu_map = alloc_percpu(atomic_t); 1301 if (!csdev->perf_sink_id_map.cpu_map) { 1302 kfree(csdev); 1303 ret = -ENOMEM; 1304 goto err_out; 1305 } 1306 } 1307 /* 1308 * Make sure the device registration and the connection fixup 1309 * are synchronised, so that we don't see uninitialised devices 1310 * on the coresight bus while trying to resolve the connections. 1311 */ 1312 mutex_lock(&coresight_mutex); 1313 1314 ret = device_register(&csdev->dev); 1315 if (ret) { 1316 put_device(&csdev->dev); 1317 /* 1318 * All resources are free'd explicitly via 1319 * coresight_device_release(), triggered from put_device(). 1320 */ 1321 goto out_unlock; 1322 } 1323 1324 if (csdev->type == CORESIGHT_DEV_TYPE_SINK || 1325 csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) { 1326 ret = etm_perf_add_symlink_sink(csdev); 1327 1328 if (ret) { 1329 device_unregister(&csdev->dev); 1330 /* 1331 * As with the above, all resources are free'd 1332 * explicitly via coresight_device_release() triggered 1333 * from put_device(), which is in turn called from 1334 * function device_unregister(). 1335 */ 1336 goto out_unlock; 1337 } 1338 } 1339 /* Device is now registered */ 1340 registered = true; 1341 1342 ret = coresight_create_conns_sysfs_group(csdev); 1343 if (!ret) 1344 ret = coresight_fixup_orphan_conns(csdev); 1345 1346 out_unlock: 1347 mutex_unlock(&coresight_mutex); 1348 /* Success */ 1349 if (!ret) { 1350 if (cti_assoc_ops && cti_assoc_ops->add) 1351 cti_assoc_ops->add(csdev); 1352 return csdev; 1353 } 1354 1355 /* Unregister the device if needed */ 1356 if (registered) { 1357 coresight_unregister(csdev); 1358 return ERR_PTR(ret); 1359 } 1360 1361 err_out: 1362 /* Cleanup the connection information */ 1363 coresight_release_platform_data(NULL, desc->dev, desc->pdata); 1364 return ERR_PTR(ret); 1365 } 1366 EXPORT_SYMBOL_GPL(coresight_register); 1367 1368 void coresight_unregister(struct coresight_device *csdev) 1369 { 1370 etm_perf_del_symlink_sink(csdev); 1371 /* Remove references of that device in the topology */ 1372 if (cti_assoc_ops && cti_assoc_ops->remove) 1373 cti_assoc_ops->remove(csdev); 1374 coresight_remove_conns(csdev); 1375 coresight_clear_default_sink(csdev); 1376 coresight_release_platform_data(csdev, csdev->dev.parent, csdev->pdata); 1377 device_unregister(&csdev->dev); 1378 } 1379 EXPORT_SYMBOL_GPL(coresight_unregister); 1380 1381 1382 /* 1383 * coresight_search_device_idx - Search the fwnode handle of a device 1384 * in the given dev_idx list. Must be called with the coresight_mutex held. 1385 * 1386 * Returns the index of the entry, when found. Otherwise, -ENOENT. 1387 */ 1388 static inline int coresight_search_device_idx(struct coresight_dev_list *dict, 1389 struct fwnode_handle *fwnode) 1390 { 1391 int i; 1392 1393 for (i = 0; i < dict->nr_idx; i++) 1394 if (dict->fwnode_list[i] == fwnode) 1395 return i; 1396 return -ENOENT; 1397 } 1398 1399 static bool coresight_compare_type(enum coresight_dev_type type_a, 1400 union coresight_dev_subtype subtype_a, 1401 enum coresight_dev_type type_b, 1402 union coresight_dev_subtype subtype_b) 1403 { 1404 if (type_a != type_b) 1405 return false; 1406 1407 switch (type_a) { 1408 case CORESIGHT_DEV_TYPE_SINK: 1409 return subtype_a.sink_subtype == subtype_b.sink_subtype; 1410 case CORESIGHT_DEV_TYPE_LINK: 1411 return subtype_a.link_subtype == subtype_b.link_subtype; 1412 case CORESIGHT_DEV_TYPE_LINKSINK: 1413 return subtype_a.link_subtype == subtype_b.link_subtype && 1414 subtype_a.sink_subtype == subtype_b.sink_subtype; 1415 case CORESIGHT_DEV_TYPE_SOURCE: 1416 return subtype_a.source_subtype == subtype_b.source_subtype; 1417 case CORESIGHT_DEV_TYPE_HELPER: 1418 return subtype_a.helper_subtype == subtype_b.helper_subtype; 1419 default: 1420 return false; 1421 } 1422 } 1423 1424 struct coresight_device * 1425 coresight_find_input_type(struct coresight_platform_data *pdata, 1426 enum coresight_dev_type type, 1427 union coresight_dev_subtype subtype) 1428 { 1429 int i; 1430 struct coresight_connection *conn; 1431 1432 for (i = 0; i < pdata->nr_inconns; ++i) { 1433 conn = pdata->in_conns[i]; 1434 if (conn && 1435 coresight_compare_type(type, subtype, conn->src_dev->type, 1436 conn->src_dev->subtype)) 1437 return conn->src_dev; 1438 } 1439 return NULL; 1440 } 1441 EXPORT_SYMBOL_GPL(coresight_find_input_type); 1442 1443 struct coresight_device * 1444 coresight_find_output_type(struct coresight_platform_data *pdata, 1445 enum coresight_dev_type type, 1446 union coresight_dev_subtype subtype) 1447 { 1448 int i; 1449 struct coresight_connection *conn; 1450 1451 for (i = 0; i < pdata->nr_outconns; ++i) { 1452 conn = pdata->out_conns[i]; 1453 if (conn->dest_dev && 1454 coresight_compare_type(type, subtype, conn->dest_dev->type, 1455 conn->dest_dev->subtype)) 1456 return conn->dest_dev; 1457 } 1458 return NULL; 1459 } 1460 EXPORT_SYMBOL_GPL(coresight_find_output_type); 1461 1462 bool coresight_loses_context_with_cpu(struct device *dev) 1463 { 1464 return fwnode_property_present(dev_fwnode(dev), 1465 "arm,coresight-loses-context-with-cpu"); 1466 } 1467 EXPORT_SYMBOL_GPL(coresight_loses_context_with_cpu); 1468 1469 /* 1470 * coresight_alloc_device_name - Get an index for a given device in the 1471 * device index list specific to a driver. An index is allocated for a 1472 * device and is tracked with the fwnode_handle to prevent allocating 1473 * duplicate indices for the same device (e.g, if we defer probing of 1474 * a device due to dependencies), in case the index is requested again. 1475 */ 1476 char *coresight_alloc_device_name(struct coresight_dev_list *dict, 1477 struct device *dev) 1478 { 1479 int idx; 1480 char *name = NULL; 1481 struct fwnode_handle **list; 1482 1483 mutex_lock(&coresight_mutex); 1484 1485 idx = coresight_search_device_idx(dict, dev_fwnode(dev)); 1486 if (idx < 0) { 1487 /* Make space for the new entry */ 1488 idx = dict->nr_idx; 1489 list = krealloc_array(dict->fwnode_list, 1490 idx + 1, sizeof(*dict->fwnode_list), 1491 GFP_KERNEL); 1492 if (ZERO_OR_NULL_PTR(list)) { 1493 idx = -ENOMEM; 1494 goto done; 1495 } 1496 1497 list[idx] = dev_fwnode(dev); 1498 dict->fwnode_list = list; 1499 dict->nr_idx = idx + 1; 1500 } 1501 1502 name = devm_kasprintf(dev, GFP_KERNEL, "%s%d", dict->pfx, idx); 1503 done: 1504 mutex_unlock(&coresight_mutex); 1505 return name; 1506 } 1507 EXPORT_SYMBOL_GPL(coresight_alloc_device_name); 1508 1509 const struct bus_type coresight_bustype = { 1510 .name = "coresight", 1511 }; 1512 1513 static int coresight_panic_sync(struct device *dev, void *data) 1514 { 1515 int mode; 1516 struct coresight_device *csdev; 1517 1518 /* Run through panic sync handlers for all enabled devices */ 1519 csdev = container_of(dev, struct coresight_device, dev); 1520 mode = coresight_get_mode(csdev); 1521 1522 if ((mode == CS_MODE_SYSFS) || (mode == CS_MODE_PERF)) { 1523 if (panic_ops(csdev)) 1524 panic_ops(csdev)->sync(csdev); 1525 } 1526 1527 return 0; 1528 } 1529 1530 static int coresight_panic_cb(struct notifier_block *self, 1531 unsigned long v, void *p) 1532 { 1533 bus_for_each_dev(&coresight_bustype, NULL, NULL, 1534 coresight_panic_sync); 1535 1536 return 0; 1537 } 1538 1539 static struct notifier_block coresight_notifier = { 1540 .notifier_call = coresight_panic_cb, 1541 }; 1542 1543 static int __init coresight_init(void) 1544 { 1545 int ret; 1546 1547 ret = bus_register(&coresight_bustype); 1548 if (ret) 1549 return ret; 1550 1551 ret = etm_perf_init(); 1552 if (ret) 1553 goto exit_bus_unregister; 1554 1555 /* Register function to be called for panic */ 1556 ret = atomic_notifier_chain_register(&panic_notifier_list, 1557 &coresight_notifier); 1558 if (ret) 1559 goto exit_perf; 1560 1561 /* initialise the coresight syscfg API */ 1562 ret = cscfg_init(); 1563 if (!ret) 1564 return 0; 1565 1566 atomic_notifier_chain_unregister(&panic_notifier_list, 1567 &coresight_notifier); 1568 exit_perf: 1569 etm_perf_exit(); 1570 exit_bus_unregister: 1571 bus_unregister(&coresight_bustype); 1572 return ret; 1573 } 1574 1575 static void __exit coresight_exit(void) 1576 { 1577 cscfg_exit(); 1578 atomic_notifier_chain_unregister(&panic_notifier_list, 1579 &coresight_notifier); 1580 etm_perf_exit(); 1581 bus_unregister(&coresight_bustype); 1582 } 1583 1584 module_init(coresight_init); 1585 module_exit(coresight_exit); 1586 1587 int coresight_init_driver(const char *drv, struct amba_driver *amba_drv, 1588 struct platform_driver *pdev_drv) 1589 { 1590 int ret; 1591 1592 ret = amba_driver_register(amba_drv); 1593 if (ret) { 1594 pr_err("%s: error registering AMBA driver\n", drv); 1595 return ret; 1596 } 1597 1598 ret = platform_driver_register(pdev_drv); 1599 if (!ret) 1600 return 0; 1601 1602 pr_err("%s: error registering platform driver\n", drv); 1603 amba_driver_unregister(amba_drv); 1604 return ret; 1605 } 1606 EXPORT_SYMBOL_GPL(coresight_init_driver); 1607 1608 void coresight_remove_driver(struct amba_driver *amba_drv, 1609 struct platform_driver *pdev_drv) 1610 { 1611 amba_driver_unregister(amba_drv); 1612 platform_driver_unregister(pdev_drv); 1613 } 1614 EXPORT_SYMBOL_GPL(coresight_remove_driver); 1615 1616 int coresight_etm_get_trace_id(struct coresight_device *csdev, enum cs_mode mode, 1617 struct coresight_device *sink) 1618 { 1619 int cpu, trace_id; 1620 1621 if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE || !source_ops(csdev)->cpu_id) 1622 return -EINVAL; 1623 1624 cpu = source_ops(csdev)->cpu_id(csdev); 1625 switch (mode) { 1626 case CS_MODE_SYSFS: 1627 trace_id = coresight_trace_id_get_cpu_id(cpu); 1628 break; 1629 case CS_MODE_PERF: 1630 if (WARN_ON(!sink)) 1631 return -EINVAL; 1632 1633 trace_id = coresight_trace_id_get_cpu_id_map(cpu, &sink->perf_sink_id_map); 1634 break; 1635 default: 1636 trace_id = -EINVAL; 1637 break; 1638 } 1639 1640 if (!IS_VALID_CS_TRACE_ID(trace_id)) 1641 dev_err(&csdev->dev, 1642 "Failed to allocate trace ID on CPU%d\n", cpu); 1643 1644 return trace_id; 1645 } 1646 EXPORT_SYMBOL_GPL(coresight_etm_get_trace_id); 1647 1648 MODULE_LICENSE("GPL v2"); 1649 MODULE_AUTHOR("Pratik Patel <pratikp@codeaurora.org>"); 1650 MODULE_AUTHOR("Mathieu Poirier <mathieu.poirier@linaro.org>"); 1651 MODULE_DESCRIPTION("Arm CoreSight tracer driver"); 1652