Linux Audio

Check our new training course

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