1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2012, The Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/acpi.h> 7 #include <linux/types.h> 8 #include <linux/err.h> 9 #include <linux/slab.h> 10 #include <linux/clk.h> 11 #include <linux/of.h> 12 #include <linux/of_address.h> 13 #include <linux/of_graph.h> 14 #include <linux/of_platform.h> 15 #include <linux/platform_device.h> 16 #include <linux/amba/bus.h> 17 #include <linux/coresight.h> 18 #include <linux/cpumask.h> 19 #include <asm/smp_plat.h> 20 21 #include "coresight-priv.h" 22 /* 23 * coresight_alloc_conns: Allocate connections record for each output 24 * port from the device. 25 */ 26 static int coresight_alloc_conns(struct device *dev, 27 struct coresight_platform_data *pdata) 28 { 29 if (pdata->nr_outport) { 30 pdata->conns = devm_kcalloc(dev, pdata->nr_outport, 31 sizeof(*pdata->conns), GFP_KERNEL); 32 if (!pdata->conns) 33 return -ENOMEM; 34 } 35 36 return 0; 37 } 38 39 static struct device * 40 coresight_find_device_by_fwnode(struct fwnode_handle *fwnode) 41 { 42 struct device *dev = NULL; 43 44 /* 45 * If we have a non-configurable replicator, it will be found on the 46 * platform bus. 47 */ 48 dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode); 49 if (dev) 50 return dev; 51 52 /* 53 * We have a configurable component - circle through the AMBA bus 54 * looking for the device that matches the endpoint node. 55 */ 56 return bus_find_device_by_fwnode(&amba_bustype, fwnode); 57 } 58 59 /* 60 * Find a registered coresight device from a device fwnode. 61 * The node info is associated with the AMBA parent, but the 62 * csdev keeps a copy so iterate round the coresight bus to 63 * find the device. 64 */ 65 struct coresight_device * 66 coresight_find_csdev_by_fwnode(struct fwnode_handle *r_fwnode) 67 { 68 struct device *dev; 69 struct coresight_device *csdev = NULL; 70 71 dev = bus_find_device_by_fwnode(&coresight_bustype, r_fwnode); 72 if (dev) { 73 csdev = to_coresight_device(dev); 74 put_device(dev); 75 } 76 return csdev; 77 } 78 79 #ifdef CONFIG_OF 80 static inline bool of_coresight_legacy_ep_is_input(struct device_node *ep) 81 { 82 return of_property_read_bool(ep, "slave-mode"); 83 } 84 85 static void of_coresight_get_ports_legacy(const struct device_node *node, 86 int *nr_inport, int *nr_outport) 87 { 88 struct device_node *ep = NULL; 89 struct of_endpoint endpoint; 90 int in = 0, out = 0; 91 92 do { 93 ep = of_graph_get_next_endpoint(node, ep); 94 if (!ep) 95 break; 96 97 if (of_graph_parse_endpoint(ep, &endpoint)) 98 continue; 99 100 if (of_coresight_legacy_ep_is_input(ep)) { 101 in = (endpoint.port + 1 > in) ? 102 endpoint.port + 1 : in; 103 } else { 104 out = (endpoint.port + 1) > out ? 105 endpoint.port + 1 : out; 106 } 107 108 } while (ep); 109 110 *nr_inport = in; 111 *nr_outport = out; 112 } 113 114 static struct device_node *of_coresight_get_port_parent(struct device_node *ep) 115 { 116 struct device_node *parent = of_graph_get_port_parent(ep); 117 118 /* 119 * Skip one-level up to the real device node, if we 120 * are using the new bindings. 121 */ 122 if (of_node_name_eq(parent, "in-ports") || 123 of_node_name_eq(parent, "out-ports")) 124 parent = of_get_next_parent(parent); 125 126 return parent; 127 } 128 129 static inline struct device_node * 130 of_coresight_get_input_ports_node(const struct device_node *node) 131 { 132 return of_get_child_by_name(node, "in-ports"); 133 } 134 135 static inline struct device_node * 136 of_coresight_get_output_ports_node(const struct device_node *node) 137 { 138 return of_get_child_by_name(node, "out-ports"); 139 } 140 141 static inline int 142 of_coresight_count_ports(struct device_node *port_parent) 143 { 144 int i = 0; 145 struct device_node *ep = NULL; 146 struct of_endpoint endpoint; 147 148 while ((ep = of_graph_get_next_endpoint(port_parent, ep))) { 149 /* Defer error handling to parsing */ 150 if (of_graph_parse_endpoint(ep, &endpoint)) 151 continue; 152 if (endpoint.port + 1 > i) 153 i = endpoint.port + 1; 154 } 155 156 return i; 157 } 158 159 static void of_coresight_get_ports(const struct device_node *node, 160 int *nr_inport, int *nr_outport) 161 { 162 struct device_node *input_ports = NULL, *output_ports = NULL; 163 164 input_ports = of_coresight_get_input_ports_node(node); 165 output_ports = of_coresight_get_output_ports_node(node); 166 167 if (input_ports || output_ports) { 168 if (input_ports) { 169 *nr_inport = of_coresight_count_ports(input_ports); 170 of_node_put(input_ports); 171 } 172 if (output_ports) { 173 *nr_outport = of_coresight_count_ports(output_ports); 174 of_node_put(output_ports); 175 } 176 } else { 177 /* Fall back to legacy DT bindings parsing */ 178 of_coresight_get_ports_legacy(node, nr_inport, nr_outport); 179 } 180 } 181 182 static int of_coresight_get_cpu(struct device *dev) 183 { 184 int cpu; 185 struct device_node *dn; 186 187 if (!dev->of_node) 188 return -ENODEV; 189 190 dn = of_parse_phandle(dev->of_node, "cpu", 0); 191 if (!dn) 192 return -ENODEV; 193 194 cpu = of_cpu_node_to_id(dn); 195 of_node_put(dn); 196 197 return cpu; 198 } 199 200 /* 201 * of_coresight_parse_endpoint : Parse the given output endpoint @ep 202 * and fill the connection information in @conn 203 * 204 * Parses the local port, remote device name and the remote port. 205 * 206 * Returns : 207 * 0 - If the parsing completed without any fatal errors. 208 * -Errno - Fatal error, abort the scanning. 209 */ 210 static int of_coresight_parse_endpoint(struct device *dev, 211 struct device_node *ep, 212 struct coresight_platform_data *pdata) 213 { 214 int ret = 0; 215 struct of_endpoint endpoint, rendpoint; 216 struct device_node *rparent = NULL; 217 struct device_node *rep = NULL; 218 struct device *rdev = NULL; 219 struct fwnode_handle *rdev_fwnode; 220 struct coresight_connection *conn; 221 222 do { 223 /* Parse the local port details */ 224 if (of_graph_parse_endpoint(ep, &endpoint)) 225 break; 226 /* 227 * Get a handle on the remote endpoint and the device it is 228 * attached to. 229 */ 230 rep = of_graph_get_remote_endpoint(ep); 231 if (!rep) 232 break; 233 rparent = of_coresight_get_port_parent(rep); 234 if (!rparent) 235 break; 236 if (of_graph_parse_endpoint(rep, &rendpoint)) 237 break; 238 239 rdev_fwnode = of_fwnode_handle(rparent); 240 /* If the remote device is not available, defer probing */ 241 rdev = coresight_find_device_by_fwnode(rdev_fwnode); 242 if (!rdev) { 243 ret = -EPROBE_DEFER; 244 break; 245 } 246 247 conn = &pdata->conns[endpoint.port]; 248 if (conn->child_fwnode) { 249 dev_warn(dev, "Duplicate output port %d\n", 250 endpoint.port); 251 ret = -EINVAL; 252 break; 253 } 254 conn->outport = endpoint.port; 255 /* 256 * Hold the refcount to the target device. This could be 257 * released via: 258 * 1) coresight_release_platform_data() if the probe fails or 259 * this device is unregistered. 260 * 2) While removing the target device via 261 * coresight_remove_match() 262 */ 263 conn->child_fwnode = fwnode_handle_get(rdev_fwnode); 264 conn->child_port = rendpoint.port; 265 /* Connection record updated */ 266 } while (0); 267 268 of_node_put(rparent); 269 of_node_put(rep); 270 put_device(rdev); 271 272 return ret; 273 } 274 275 static int of_get_coresight_platform_data(struct device *dev, 276 struct coresight_platform_data *pdata) 277 { 278 int ret = 0; 279 struct device_node *ep = NULL; 280 const struct device_node *parent = NULL; 281 bool legacy_binding = false; 282 struct device_node *node = dev->of_node; 283 284 /* Get the number of input and output port for this component */ 285 of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport); 286 287 /* If there are no output connections, we are done */ 288 if (!pdata->nr_outport) 289 return 0; 290 291 ret = coresight_alloc_conns(dev, pdata); 292 if (ret) 293 return ret; 294 295 parent = of_coresight_get_output_ports_node(node); 296 /* 297 * If the DT uses obsoleted bindings, the ports are listed 298 * under the device and we need to filter out the input 299 * ports. 300 */ 301 if (!parent) { 302 legacy_binding = true; 303 parent = node; 304 dev_warn_once(dev, "Uses obsolete Coresight DT bindings\n"); 305 } 306 307 /* Iterate through each output port to discover topology */ 308 while ((ep = of_graph_get_next_endpoint(parent, ep))) { 309 /* 310 * Legacy binding mixes input/output ports under the 311 * same parent. So, skip the input ports if we are dealing 312 * with legacy binding, as they processed with their 313 * connected output ports. 314 */ 315 if (legacy_binding && of_coresight_legacy_ep_is_input(ep)) 316 continue; 317 318 ret = of_coresight_parse_endpoint(dev, ep, pdata); 319 if (ret) 320 return ret; 321 } 322 323 return 0; 324 } 325 #else 326 static inline int 327 of_get_coresight_platform_data(struct device *dev, 328 struct coresight_platform_data *pdata) 329 { 330 return -ENOENT; 331 } 332 333 static inline int of_coresight_get_cpu(struct device *dev) 334 { 335 return -ENODEV; 336 } 337 #endif 338 339 #ifdef CONFIG_ACPI 340 341 #include <acpi/actypes.h> 342 #include <acpi/processor.h> 343 344 /* ACPI Graph _DSD UUID : "ab02a46b-74c7-45a2-bd68-f7d344ef2153" */ 345 static const guid_t acpi_graph_uuid = GUID_INIT(0xab02a46b, 0x74c7, 0x45a2, 346 0xbd, 0x68, 0xf7, 0xd3, 347 0x44, 0xef, 0x21, 0x53); 348 /* Coresight ACPI Graph UUID : "3ecbc8b6-1d0e-4fb3-8107-e627f805c6cd" */ 349 static const guid_t coresight_graph_uuid = GUID_INIT(0x3ecbc8b6, 0x1d0e, 0x4fb3, 350 0x81, 0x07, 0xe6, 0x27, 351 0xf8, 0x05, 0xc6, 0xcd); 352 #define ACPI_CORESIGHT_LINK_SLAVE 0 353 #define ACPI_CORESIGHT_LINK_MASTER 1 354 355 static inline bool is_acpi_guid(const union acpi_object *obj) 356 { 357 return (obj->type == ACPI_TYPE_BUFFER) && (obj->buffer.length == 16); 358 } 359 360 /* 361 * acpi_guid_matches - Checks if the given object is a GUID object and 362 * that it matches the supplied the GUID. 363 */ 364 static inline bool acpi_guid_matches(const union acpi_object *obj, 365 const guid_t *guid) 366 { 367 return is_acpi_guid(obj) && 368 guid_equal((guid_t *)obj->buffer.pointer, guid); 369 } 370 371 static inline bool is_acpi_dsd_graph_guid(const union acpi_object *obj) 372 { 373 return acpi_guid_matches(obj, &acpi_graph_uuid); 374 } 375 376 static inline bool is_acpi_coresight_graph_guid(const union acpi_object *obj) 377 { 378 return acpi_guid_matches(obj, &coresight_graph_uuid); 379 } 380 381 static inline bool is_acpi_coresight_graph(const union acpi_object *obj) 382 { 383 const union acpi_object *graphid, *guid, *links; 384 385 if (obj->type != ACPI_TYPE_PACKAGE || 386 obj->package.count < 3) 387 return false; 388 389 graphid = &obj->package.elements[0]; 390 guid = &obj->package.elements[1]; 391 links = &obj->package.elements[2]; 392 393 if (graphid->type != ACPI_TYPE_INTEGER || 394 links->type != ACPI_TYPE_INTEGER) 395 return false; 396 397 return is_acpi_coresight_graph_guid(guid); 398 } 399 400 /* 401 * acpi_validate_dsd_graph - Make sure the given _DSD graph conforms 402 * to the ACPI _DSD Graph specification. 403 * 404 * ACPI Devices Graph property has the following format: 405 * { 406 * Revision - Integer, must be 0 407 * NumberOfGraphs - Integer, N indicating the following list. 408 * Graph[1], 409 * ... 410 * Graph[N] 411 * } 412 * 413 * And each Graph entry has the following format: 414 * { 415 * GraphID - Integer, identifying a graph the device belongs to. 416 * UUID - UUID identifying the specification that governs 417 * this graph. (e.g, see is_acpi_coresight_graph()) 418 * NumberOfLinks - Number "N" of connections on this node of the graph. 419 * Links[1] 420 * ... 421 * Links[N] 422 * } 423 * 424 * Where each "Links" entry has the following format: 425 * 426 * { 427 * SourcePortAddress - Integer 428 * DestinationPortAddress - Integer 429 * DestinationDeviceName - Reference to another device 430 * ( --- CoreSight specific extensions below ---) 431 * DirectionOfFlow - Integer 1 for output(master) 432 * 0 for input(slave) 433 * } 434 * 435 * e.g: 436 * For a Funnel device 437 * 438 * Device(MFUN) { 439 * ... 440 * 441 * Name (_DSD, Package() { 442 * // DSD Package contains tuples of { Proeprty_Type_UUID, Package() } 443 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), //Std. Property UUID 444 * Package() { 445 * Package(2) { "property-name", <property-value> } 446 * }, 447 * 448 * ToUUID("ab02a46b-74c7-45a2-bd68-f7d344ef2153"), // ACPI Graph UUID 449 * Package() { 450 * 0, // Revision 451 * 1, // NumberOfGraphs. 452 * Package() { // Graph[0] Package 453 * 1, // GraphID 454 * // Coresight Graph UUID 455 * ToUUID("3ecbc8b6-1d0e-4fb3-8107-e627f805c6cd"), 456 * 3, // NumberOfLinks aka ports 457 * // Link[0]: Output_0 -> Replicator:Input_0 458 * Package () { 0, 0, \_SB_.RPL0, 1 }, 459 * // Link[1]: Input_0 <- Cluster0_Funnel0:Output_0 460 * Package () { 0, 0, \_SB_.CLU0.FUN0, 0 }, 461 * // Link[2]: Input_1 <- Cluster1_Funnel0:Output_0 462 * Package () { 1, 0, \_SB_.CLU1.FUN0, 0 }, 463 * } // End of Graph[0] Package 464 * 465 * }, // End of ACPI Graph Property 466 * }) 467 */ 468 static inline bool acpi_validate_dsd_graph(const union acpi_object *graph) 469 { 470 int i, n; 471 const union acpi_object *rev, *nr_graphs; 472 473 /* The graph must contain at least the Revision and Number of Graphs */ 474 if (graph->package.count < 2) 475 return false; 476 477 rev = &graph->package.elements[0]; 478 nr_graphs = &graph->package.elements[1]; 479 480 if (rev->type != ACPI_TYPE_INTEGER || 481 nr_graphs->type != ACPI_TYPE_INTEGER) 482 return false; 483 484 /* We only support revision 0 */ 485 if (rev->integer.value != 0) 486 return false; 487 488 n = nr_graphs->integer.value; 489 /* CoreSight devices are only part of a single Graph */ 490 if (n != 1) 491 return false; 492 493 /* Make sure the ACPI graph package has right number of elements */ 494 if (graph->package.count != (n + 2)) 495 return false; 496 497 /* 498 * Each entry must be a graph package with at least 3 members : 499 * { GraphID, UUID, NumberOfLinks(n), Links[.],... } 500 */ 501 for (i = 2; i < n + 2; i++) { 502 const union acpi_object *obj = &graph->package.elements[i]; 503 504 if (obj->type != ACPI_TYPE_PACKAGE || 505 obj->package.count < 3) 506 return false; 507 } 508 509 return true; 510 } 511 512 /* acpi_get_dsd_graph - Find the _DSD Graph property for the given device. */ 513 static const union acpi_object * 514 acpi_get_dsd_graph(struct acpi_device *adev) 515 { 516 int i; 517 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 518 acpi_status status; 519 const union acpi_object *dsd; 520 521 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, 522 &buf, ACPI_TYPE_PACKAGE); 523 if (ACPI_FAILURE(status)) 524 return NULL; 525 526 dsd = buf.pointer; 527 528 /* 529 * _DSD property consists tuples { Prop_UUID, Package() } 530 * Iterate through all the packages and find the Graph. 531 */ 532 for (i = 0; i + 1 < dsd->package.count; i += 2) { 533 const union acpi_object *guid, *package; 534 535 guid = &dsd->package.elements[i]; 536 package = &dsd->package.elements[i + 1]; 537 538 /* All _DSD elements must have a UUID and a Package */ 539 if (!is_acpi_guid(guid) || package->type != ACPI_TYPE_PACKAGE) 540 break; 541 /* Skip the non-Graph _DSD packages */ 542 if (!is_acpi_dsd_graph_guid(guid)) 543 continue; 544 if (acpi_validate_dsd_graph(package)) 545 return package; 546 /* Invalid graph format, continue */ 547 dev_warn(&adev->dev, "Invalid Graph _DSD property\n"); 548 } 549 550 return NULL; 551 } 552 553 static inline bool 554 acpi_validate_coresight_graph(const union acpi_object *cs_graph) 555 { 556 int nlinks; 557 558 nlinks = cs_graph->package.elements[2].integer.value; 559 /* 560 * Graph must have the following fields : 561 * { GraphID, GraphUUID, NumberOfLinks, Links... } 562 */ 563 if (cs_graph->package.count != (nlinks + 3)) 564 return false; 565 /* The links are validated in acpi_coresight_parse_link() */ 566 return true; 567 } 568 569 /* 570 * acpi_get_coresight_graph - Parse the device _DSD tables and find 571 * the Graph property matching the CoreSight Graphs. 572 * 573 * Returns the pointer to the CoreSight Graph Package when found. Otherwise 574 * returns NULL. 575 */ 576 static const union acpi_object * 577 acpi_get_coresight_graph(struct acpi_device *adev) 578 { 579 const union acpi_object *graph_list, *graph; 580 int i, nr_graphs; 581 582 graph_list = acpi_get_dsd_graph(adev); 583 if (!graph_list) 584 return graph_list; 585 586 nr_graphs = graph_list->package.elements[1].integer.value; 587 588 for (i = 2; i < nr_graphs + 2; i++) { 589 graph = &graph_list->package.elements[i]; 590 if (!is_acpi_coresight_graph(graph)) 591 continue; 592 if (acpi_validate_coresight_graph(graph)) 593 return graph; 594 /* Invalid graph format */ 595 break; 596 } 597 598 return NULL; 599 } 600 601 /* 602 * acpi_coresight_parse_link - Parse the given Graph connection 603 * of the device and populate the coresight_connection for an output 604 * connection. 605 * 606 * CoreSight Graph specification mandates that the direction of the data 607 * flow must be specified in the link. i.e, 608 * 609 * SourcePortAddress, // Integer 610 * DestinationPortAddress, // Integer 611 * DestinationDeviceName, // Reference to another device 612 * DirectionOfFlow, // 1 for output(master), 0 for input(slave) 613 * 614 * Returns the direction of the data flow [ Input(slave) or Output(master) ] 615 * upon success. 616 * Returns an negative error number otherwise. 617 */ 618 static int acpi_coresight_parse_link(struct acpi_device *adev, 619 const union acpi_object *link, 620 struct coresight_connection *conn) 621 { 622 int rc, dir; 623 const union acpi_object *fields; 624 struct acpi_device *r_adev; 625 struct device *rdev; 626 627 if (link->type != ACPI_TYPE_PACKAGE || 628 link->package.count != 4) 629 return -EINVAL; 630 631 fields = link->package.elements; 632 633 if (fields[0].type != ACPI_TYPE_INTEGER || 634 fields[1].type != ACPI_TYPE_INTEGER || 635 fields[2].type != ACPI_TYPE_LOCAL_REFERENCE || 636 fields[3].type != ACPI_TYPE_INTEGER) 637 return -EINVAL; 638 639 rc = acpi_bus_get_device(fields[2].reference.handle, &r_adev); 640 if (rc) 641 return rc; 642 643 dir = fields[3].integer.value; 644 if (dir == ACPI_CORESIGHT_LINK_MASTER) { 645 conn->outport = fields[0].integer.value; 646 conn->child_port = fields[1].integer.value; 647 rdev = coresight_find_device_by_fwnode(&r_adev->fwnode); 648 if (!rdev) 649 return -EPROBE_DEFER; 650 /* 651 * Hold the refcount to the target device. This could be 652 * released via: 653 * 1) coresight_release_platform_data() if the probe fails or 654 * this device is unregistered. 655 * 2) While removing the target device via 656 * coresight_remove_match(). 657 */ 658 conn->child_fwnode = fwnode_handle_get(&r_adev->fwnode); 659 } else if (dir == ACPI_CORESIGHT_LINK_SLAVE) { 660 /* 661 * We are only interested in the port number 662 * for the input ports at this component. 663 * Store the port number in child_port. 664 */ 665 conn->child_port = fields[0].integer.value; 666 } else { 667 /* Invalid direction */ 668 return -EINVAL; 669 } 670 671 return dir; 672 } 673 674 /* 675 * acpi_coresight_parse_graph - Parse the _DSD CoreSight graph 676 * connection information and populate the supplied coresight_platform_data 677 * instance. 678 */ 679 static int acpi_coresight_parse_graph(struct acpi_device *adev, 680 struct coresight_platform_data *pdata) 681 { 682 int rc, i, nlinks; 683 const union acpi_object *graph; 684 struct coresight_connection *conns, *ptr; 685 686 pdata->nr_inport = pdata->nr_outport = 0; 687 graph = acpi_get_coresight_graph(adev); 688 if (!graph) 689 return -ENOENT; 690 691 nlinks = graph->package.elements[2].integer.value; 692 if (!nlinks) 693 return 0; 694 695 /* 696 * To avoid scanning the table twice (once for finding the number of 697 * output links and then later for parsing the output links), 698 * cache the links information in one go and then later copy 699 * it to the pdata. 700 */ 701 conns = devm_kcalloc(&adev->dev, nlinks, sizeof(*conns), GFP_KERNEL); 702 if (!conns) 703 return -ENOMEM; 704 ptr = conns; 705 for (i = 0; i < nlinks; i++) { 706 const union acpi_object *link = &graph->package.elements[3 + i]; 707 int dir; 708 709 dir = acpi_coresight_parse_link(adev, link, ptr); 710 if (dir < 0) 711 return dir; 712 713 if (dir == ACPI_CORESIGHT_LINK_MASTER) { 714 if (ptr->outport > pdata->nr_outport) 715 pdata->nr_outport = ptr->outport; 716 ptr++; 717 } else { 718 WARN_ON(pdata->nr_inport == ptr->child_port); 719 /* 720 * We do not track input port connections for a device. 721 * However we need the highest port number described, 722 * which can be recorded now and reuse this connection 723 * record for an output connection. Hence, do not move 724 * the ptr for input connections 725 */ 726 if (ptr->child_port > pdata->nr_inport) 727 pdata->nr_inport = ptr->child_port; 728 } 729 } 730 731 rc = coresight_alloc_conns(&adev->dev, pdata); 732 if (rc) 733 return rc; 734 735 /* Copy the connection information to the final location */ 736 for (i = 0; conns + i < ptr; i++) { 737 int port = conns[i].outport; 738 739 /* Duplicate output port */ 740 WARN_ON(pdata->conns[port].child_fwnode); 741 pdata->conns[port] = conns[i]; 742 } 743 744 devm_kfree(&adev->dev, conns); 745 return 0; 746 } 747 748 /* 749 * acpi_handle_to_logical_cpuid - Map a given acpi_handle to the 750 * logical CPU id of the corresponding CPU device. 751 * 752 * Returns the logical CPU id when found. Otherwise returns >= nr_cpus_id. 753 */ 754 static int 755 acpi_handle_to_logical_cpuid(acpi_handle handle) 756 { 757 int i; 758 struct acpi_processor *pr; 759 760 for_each_possible_cpu(i) { 761 pr = per_cpu(processors, i); 762 if (pr && pr->handle == handle) 763 break; 764 } 765 766 return i; 767 } 768 769 /* 770 * acpi_coresigh_get_cpu - Find the logical CPU id of the CPU associated 771 * with this coresight device. With ACPI bindings, the CoreSight components 772 * are listed as child device of the associated CPU. 773 * 774 * Returns the logical CPU id when found. Otherwise returns 0. 775 */ 776 static int acpi_coresight_get_cpu(struct device *dev) 777 { 778 int cpu; 779 acpi_handle cpu_handle; 780 acpi_status status; 781 struct acpi_device *adev = ACPI_COMPANION(dev); 782 783 if (!adev) 784 return -ENODEV; 785 status = acpi_get_parent(adev->handle, &cpu_handle); 786 if (ACPI_FAILURE(status)) 787 return -ENODEV; 788 789 cpu = acpi_handle_to_logical_cpuid(cpu_handle); 790 if (cpu >= nr_cpu_ids) 791 return -ENODEV; 792 return cpu; 793 } 794 795 static int 796 acpi_get_coresight_platform_data(struct device *dev, 797 struct coresight_platform_data *pdata) 798 { 799 struct acpi_device *adev; 800 801 adev = ACPI_COMPANION(dev); 802 if (!adev) 803 return -EINVAL; 804 805 return acpi_coresight_parse_graph(adev, pdata); 806 } 807 808 #else 809 810 static inline int 811 acpi_get_coresight_platform_data(struct device *dev, 812 struct coresight_platform_data *pdata) 813 { 814 return -ENOENT; 815 } 816 817 static inline int acpi_coresight_get_cpu(struct device *dev) 818 { 819 return -ENODEV; 820 } 821 #endif 822 823 int coresight_get_cpu(struct device *dev) 824 { 825 if (is_of_node(dev->fwnode)) 826 return of_coresight_get_cpu(dev); 827 else if (is_acpi_device_node(dev->fwnode)) 828 return acpi_coresight_get_cpu(dev); 829 return 0; 830 } 831 EXPORT_SYMBOL_GPL(coresight_get_cpu); 832 833 struct coresight_platform_data * 834 coresight_get_platform_data(struct device *dev) 835 { 836 int ret = -ENOENT; 837 struct coresight_platform_data *pdata = NULL; 838 struct fwnode_handle *fwnode = dev_fwnode(dev); 839 840 if (IS_ERR_OR_NULL(fwnode)) 841 goto error; 842 843 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 844 if (!pdata) { 845 ret = -ENOMEM; 846 goto error; 847 } 848 849 if (is_of_node(fwnode)) 850 ret = of_get_coresight_platform_data(dev, pdata); 851 else if (is_acpi_device_node(fwnode)) 852 ret = acpi_get_coresight_platform_data(dev, pdata); 853 854 if (!ret) 855 return pdata; 856 error: 857 if (!IS_ERR_OR_NULL(pdata)) 858 /* Cleanup the connection information */ 859 coresight_release_platform_data(NULL, pdata); 860 return ERR_PTR(ret); 861 } 862 EXPORT_SYMBOL_GPL(coresight_get_platform_data); 863