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