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