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