1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
6 #include <linux/acpi.h>
7 #include <linux/types.h>
9 #include <linux/slab.h>
10 #include <linux/clk.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>
21 #include "coresight-priv.h"
24 * Add an entry to the connection list and assign @conn's contents to it.
26 * If the output port is already assigned on this device, return -EINVAL
28 struct coresight_connection *
29 coresight_add_out_conn(struct device *dev,
30 struct coresight_platform_data *pdata,
31 const struct coresight_connection *new_conn)
34 struct coresight_connection *conn;
37 * Warn on any existing duplicate output port.
39 for (i = 0; i < pdata->nr_outconns; ++i) {
40 conn = &pdata->out_conns[i];
41 /* Output == -1 means ignore the port for example for helpers */
42 if (conn->src_port != -1 &&
43 conn->src_port == new_conn->src_port) {
44 dev_warn(dev, "Duplicate output port %d\n",
46 return ERR_PTR(-EINVAL);
52 devm_krealloc_array(dev, pdata->out_conns, pdata->nr_outconns,
53 sizeof(*pdata->out_conns), GFP_KERNEL);
54 if (!pdata->out_conns)
55 return ERR_PTR(-ENOMEM);
57 pdata->out_conns[pdata->nr_outconns - 1] = *new_conn;
58 return &pdata->out_conns[pdata->nr_outconns - 1];
60 EXPORT_SYMBOL_GPL(coresight_add_out_conn);
62 static struct device *
63 coresight_find_device_by_fwnode(struct fwnode_handle *fwnode)
65 struct device *dev = NULL;
68 * If we have a non-configurable replicator, it will be found on the
71 dev = bus_find_device_by_fwnode(&platform_bus_type, fwnode);
76 * We have a configurable component - circle through the AMBA bus
77 * looking for the device that matches the endpoint node.
79 return bus_find_device_by_fwnode(&amba_bustype, fwnode);
83 * Find a registered coresight device from a device fwnode.
84 * The node info is associated with the AMBA parent, but the
85 * csdev keeps a copy so iterate round the coresight bus to
88 struct coresight_device *
89 coresight_find_csdev_by_fwnode(struct fwnode_handle *r_fwnode)
92 struct coresight_device *csdev = NULL;
94 dev = bus_find_device_by_fwnode(&coresight_bustype, r_fwnode);
96 csdev = to_coresight_device(dev);
101 EXPORT_SYMBOL_GPL(coresight_find_csdev_by_fwnode);
104 static inline bool of_coresight_legacy_ep_is_input(struct device_node *ep)
106 return of_property_read_bool(ep, "slave-mode");
109 static void of_coresight_get_ports_legacy(const struct device_node *node,
110 int *nr_inconns, int *nr_outconns)
112 struct device_node *ep = NULL;
113 struct of_endpoint endpoint;
117 * Avoid warnings in of_graph_get_next_endpoint()
118 * if the device doesn't have any graph connections
120 if (!of_graph_is_present(node))
123 ep = of_graph_get_next_endpoint(node, ep);
127 if (of_graph_parse_endpoint(ep, &endpoint))
130 if (of_coresight_legacy_ep_is_input(ep)) {
131 in = (endpoint.port + 1 > in) ?
132 endpoint.port + 1 : in;
134 out = (endpoint.port + 1) > out ?
135 endpoint.port + 1 : out;
144 static struct device_node *of_coresight_get_port_parent(struct device_node *ep)
146 struct device_node *parent = of_graph_get_port_parent(ep);
149 * Skip one-level up to the real device node, if we
150 * are using the new bindings.
152 if (of_node_name_eq(parent, "in-ports") ||
153 of_node_name_eq(parent, "out-ports"))
154 parent = of_get_next_parent(parent);
159 static inline struct device_node *
160 of_coresight_get_input_ports_node(const struct device_node *node)
162 return of_get_child_by_name(node, "in-ports");
165 static inline struct device_node *
166 of_coresight_get_output_ports_node(const struct device_node *node)
168 return of_get_child_by_name(node, "out-ports");
172 of_coresight_count_ports(struct device_node *port_parent)
175 struct device_node *ep = NULL;
176 struct of_endpoint endpoint;
178 while ((ep = of_graph_get_next_endpoint(port_parent, ep))) {
179 /* Defer error handling to parsing */
180 if (of_graph_parse_endpoint(ep, &endpoint))
182 if (endpoint.port + 1 > i)
183 i = endpoint.port + 1;
189 static void of_coresight_get_ports(const struct device_node *node,
190 int *nr_inconns, int *nr_outconns)
192 struct device_node *input_ports = NULL, *output_ports = NULL;
194 input_ports = of_coresight_get_input_ports_node(node);
195 output_ports = of_coresight_get_output_ports_node(node);
197 if (input_ports || output_ports) {
199 *nr_inconns = of_coresight_count_ports(input_ports);
200 of_node_put(input_ports);
203 *nr_outconns = of_coresight_count_ports(output_ports);
204 of_node_put(output_ports);
207 /* Fall back to legacy DT bindings parsing */
208 of_coresight_get_ports_legacy(node, nr_inconns, nr_outconns);
212 static int of_coresight_get_cpu(struct device *dev)
215 struct device_node *dn;
220 dn = of_parse_phandle(dev->of_node, "cpu", 0);
224 cpu = of_cpu_node_to_id(dn);
231 * of_coresight_parse_endpoint : Parse the given output endpoint @ep
232 * and fill the connection information in @conn
234 * Parses the local port, remote device name and the remote port.
237 * 0 - If the parsing completed without any fatal errors.
238 * -Errno - Fatal error, abort the scanning.
240 static int of_coresight_parse_endpoint(struct device *dev,
241 struct device_node *ep,
242 struct coresight_platform_data *pdata)
245 struct of_endpoint endpoint, rendpoint;
246 struct device_node *rparent = NULL;
247 struct device_node *rep = NULL;
248 struct device *rdev = NULL;
249 struct fwnode_handle *rdev_fwnode;
250 struct coresight_connection conn = {};
251 struct coresight_connection *new_conn;
254 /* Parse the local port details */
255 if (of_graph_parse_endpoint(ep, &endpoint))
258 * Get a handle on the remote endpoint and the device it is
261 rep = of_graph_get_remote_endpoint(ep);
264 rparent = of_coresight_get_port_parent(rep);
267 if (of_graph_parse_endpoint(rep, &rendpoint))
270 rdev_fwnode = of_fwnode_handle(rparent);
271 /* If the remote device is not available, defer probing */
272 rdev = coresight_find_device_by_fwnode(rdev_fwnode);
278 conn.src_port = endpoint.port;
280 * Hold the refcount to the target device. This could be
282 * 1) coresight_release_platform_data() if the probe fails or
283 * this device is unregistered.
284 * 2) While removing the target device via
285 * coresight_remove_match()
287 conn.dest_fwnode = fwnode_handle_get(rdev_fwnode);
288 conn.dest_port = rendpoint.port;
290 new_conn = coresight_add_out_conn(dev, pdata, &conn);
291 if (IS_ERR_VALUE(new_conn)) {
292 fwnode_handle_put(conn.dest_fwnode);
293 return PTR_ERR(new_conn);
295 /* Connection record updated */
298 of_node_put(rparent);
305 static int of_get_coresight_platform_data(struct device *dev,
306 struct coresight_platform_data *pdata)
309 struct device_node *ep = NULL;
310 const struct device_node *parent = NULL;
311 bool legacy_binding = false;
312 struct device_node *node = dev->of_node;
314 /* Get the number of input and output port for this component */
315 of_coresight_get_ports(node, &pdata->high_inport, &pdata->high_outport);
317 /* If there are no output connections, we are done */
318 if (!pdata->high_outport)
321 parent = of_coresight_get_output_ports_node(node);
323 * If the DT uses obsoleted bindings, the ports are listed
324 * under the device and we need to filter out the input
328 legacy_binding = true;
330 dev_warn_once(dev, "Uses obsolete Coresight DT bindings\n");
333 /* Iterate through each output port to discover topology */
334 while ((ep = of_graph_get_next_endpoint(parent, ep))) {
336 * Legacy binding mixes input/output ports under the
337 * same parent. So, skip the input ports if we are dealing
338 * with legacy binding, as they processed with their
339 * connected output ports.
341 if (legacy_binding && of_coresight_legacy_ep_is_input(ep))
344 ret = of_coresight_parse_endpoint(dev, ep, pdata);
353 of_get_coresight_platform_data(struct device *dev,
354 struct coresight_platform_data *pdata)
359 static inline int of_coresight_get_cpu(struct device *dev)
367 #include <acpi/actypes.h>
368 #include <acpi/processor.h>
370 /* ACPI Graph _DSD UUID : "ab02a46b-74c7-45a2-bd68-f7d344ef2153" */
371 static const guid_t acpi_graph_uuid = GUID_INIT(0xab02a46b, 0x74c7, 0x45a2,
372 0xbd, 0x68, 0xf7, 0xd3,
373 0x44, 0xef, 0x21, 0x53);
374 /* Coresight ACPI Graph UUID : "3ecbc8b6-1d0e-4fb3-8107-e627f805c6cd" */
375 static const guid_t coresight_graph_uuid = GUID_INIT(0x3ecbc8b6, 0x1d0e, 0x4fb3,
376 0x81, 0x07, 0xe6, 0x27,
377 0xf8, 0x05, 0xc6, 0xcd);
378 #define ACPI_CORESIGHT_LINK_SLAVE 0
379 #define ACPI_CORESIGHT_LINK_MASTER 1
381 static inline bool is_acpi_guid(const union acpi_object *obj)
383 return (obj->type == ACPI_TYPE_BUFFER) && (obj->buffer.length == 16);
387 * acpi_guid_matches - Checks if the given object is a GUID object and
388 * that it matches the supplied the GUID.
390 static inline bool acpi_guid_matches(const union acpi_object *obj,
393 return is_acpi_guid(obj) &&
394 guid_equal((guid_t *)obj->buffer.pointer, guid);
397 static inline bool is_acpi_dsd_graph_guid(const union acpi_object *obj)
399 return acpi_guid_matches(obj, &acpi_graph_uuid);
402 static inline bool is_acpi_coresight_graph_guid(const union acpi_object *obj)
404 return acpi_guid_matches(obj, &coresight_graph_uuid);
407 static inline bool is_acpi_coresight_graph(const union acpi_object *obj)
409 const union acpi_object *graphid, *guid, *links;
411 if (obj->type != ACPI_TYPE_PACKAGE ||
412 obj->package.count < 3)
415 graphid = &obj->package.elements[0];
416 guid = &obj->package.elements[1];
417 links = &obj->package.elements[2];
419 if (graphid->type != ACPI_TYPE_INTEGER ||
420 links->type != ACPI_TYPE_INTEGER)
423 return is_acpi_coresight_graph_guid(guid);
427 * acpi_validate_dsd_graph - Make sure the given _DSD graph conforms
428 * to the ACPI _DSD Graph specification.
430 * ACPI Devices Graph property has the following format:
432 * Revision - Integer, must be 0
433 * NumberOfGraphs - Integer, N indicating the following list.
439 * And each Graph entry has the following format:
441 * GraphID - Integer, identifying a graph the device belongs to.
442 * UUID - UUID identifying the specification that governs
443 * this graph. (e.g, see is_acpi_coresight_graph())
444 * NumberOfLinks - Number "N" of connections on this node of the graph.
450 * Where each "Links" entry has the following format:
453 * SourcePortAddress - Integer
454 * DestinationPortAddress - Integer
455 * DestinationDeviceName - Reference to another device
456 * ( --- CoreSight specific extensions below ---)
457 * DirectionOfFlow - Integer 1 for output(master)
462 * For a Funnel device
467 * Name (_DSD, Package() {
468 * // DSD Package contains tuples of { Proeprty_Type_UUID, Package() }
469 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), //Std. Property UUID
471 * Package(2) { "property-name", <property-value> }
474 * ToUUID("ab02a46b-74c7-45a2-bd68-f7d344ef2153"), // ACPI Graph UUID
477 * 1, // NumberOfGraphs.
478 * Package() { // Graph[0] Package
480 * // Coresight Graph UUID
481 * ToUUID("3ecbc8b6-1d0e-4fb3-8107-e627f805c6cd"),
482 * 3, // NumberOfLinks aka ports
483 * // Link[0]: Output_0 -> Replicator:Input_0
484 * Package () { 0, 0, \_SB_.RPL0, 1 },
485 * // Link[1]: Input_0 <- Cluster0_Funnel0:Output_0
486 * Package () { 0, 0, \_SB_.CLU0.FUN0, 0 },
487 * // Link[2]: Input_1 <- Cluster1_Funnel0:Output_0
488 * Package () { 1, 0, \_SB_.CLU1.FUN0, 0 },
489 * } // End of Graph[0] Package
491 * }, // End of ACPI Graph Property
494 static inline bool acpi_validate_dsd_graph(const union acpi_object *graph)
497 const union acpi_object *rev, *nr_graphs;
499 /* The graph must contain at least the Revision and Number of Graphs */
500 if (graph->package.count < 2)
503 rev = &graph->package.elements[0];
504 nr_graphs = &graph->package.elements[1];
506 if (rev->type != ACPI_TYPE_INTEGER ||
507 nr_graphs->type != ACPI_TYPE_INTEGER)
510 /* We only support revision 0 */
511 if (rev->integer.value != 0)
514 n = nr_graphs->integer.value;
515 /* CoreSight devices are only part of a single Graph */
519 /* Make sure the ACPI graph package has right number of elements */
520 if (graph->package.count != (n + 2))
524 * Each entry must be a graph package with at least 3 members :
525 * { GraphID, UUID, NumberOfLinks(n), Links[.],... }
527 for (i = 2; i < n + 2; i++) {
528 const union acpi_object *obj = &graph->package.elements[i];
530 if (obj->type != ACPI_TYPE_PACKAGE ||
531 obj->package.count < 3)
538 /* acpi_get_dsd_graph - Find the _DSD Graph property for the given device. */
539 static const union acpi_object *
540 acpi_get_dsd_graph(struct acpi_device *adev)
543 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
545 const union acpi_object *dsd;
547 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL,
548 &buf, ACPI_TYPE_PACKAGE);
549 if (ACPI_FAILURE(status))
555 * _DSD property consists tuples { Prop_UUID, Package() }
556 * Iterate through all the packages and find the Graph.
558 for (i = 0; i + 1 < dsd->package.count; i += 2) {
559 const union acpi_object *guid, *package;
561 guid = &dsd->package.elements[i];
562 package = &dsd->package.elements[i + 1];
564 /* All _DSD elements must have a UUID and a Package */
565 if (!is_acpi_guid(guid) || package->type != ACPI_TYPE_PACKAGE)
567 /* Skip the non-Graph _DSD packages */
568 if (!is_acpi_dsd_graph_guid(guid))
570 if (acpi_validate_dsd_graph(package))
572 /* Invalid graph format, continue */
573 dev_warn(&adev->dev, "Invalid Graph _DSD property\n");
580 acpi_validate_coresight_graph(const union acpi_object *cs_graph)
584 nlinks = cs_graph->package.elements[2].integer.value;
586 * Graph must have the following fields :
587 * { GraphID, GraphUUID, NumberOfLinks, Links... }
589 if (cs_graph->package.count != (nlinks + 3))
591 /* The links are validated in acpi_coresight_parse_link() */
596 * acpi_get_coresight_graph - Parse the device _DSD tables and find
597 * the Graph property matching the CoreSight Graphs.
599 * Returns the pointer to the CoreSight Graph Package when found. Otherwise
602 static const union acpi_object *
603 acpi_get_coresight_graph(struct acpi_device *adev)
605 const union acpi_object *graph_list, *graph;
608 graph_list = acpi_get_dsd_graph(adev);
612 nr_graphs = graph_list->package.elements[1].integer.value;
614 for (i = 2; i < nr_graphs + 2; i++) {
615 graph = &graph_list->package.elements[i];
616 if (!is_acpi_coresight_graph(graph))
618 if (acpi_validate_coresight_graph(graph))
620 /* Invalid graph format */
628 * acpi_coresight_parse_link - Parse the given Graph connection
629 * of the device and populate the coresight_connection for an output
632 * CoreSight Graph specification mandates that the direction of the data
633 * flow must be specified in the link. i.e,
635 * SourcePortAddress, // Integer
636 * DestinationPortAddress, // Integer
637 * DestinationDeviceName, // Reference to another device
638 * DirectionOfFlow, // 1 for output(master), 0 for input(slave)
640 * Returns the direction of the data flow [ Input(slave) or Output(master) ]
642 * Returns an negative error number otherwise.
644 static int acpi_coresight_parse_link(struct acpi_device *adev,
645 const union acpi_object *link,
646 struct coresight_connection *conn)
649 const union acpi_object *fields;
650 struct acpi_device *r_adev;
653 if (link->type != ACPI_TYPE_PACKAGE ||
654 link->package.count != 4)
657 fields = link->package.elements;
659 if (fields[0].type != ACPI_TYPE_INTEGER ||
660 fields[1].type != ACPI_TYPE_INTEGER ||
661 fields[2].type != ACPI_TYPE_LOCAL_REFERENCE ||
662 fields[3].type != ACPI_TYPE_INTEGER)
665 r_adev = acpi_fetch_acpi_dev(fields[2].reference.handle);
669 dir = fields[3].integer.value;
670 if (dir == ACPI_CORESIGHT_LINK_MASTER) {
671 conn->src_port = fields[0].integer.value;
672 conn->dest_port = fields[1].integer.value;
673 rdev = coresight_find_device_by_fwnode(&r_adev->fwnode);
675 return -EPROBE_DEFER;
677 * Hold the refcount to the target device. This could be
679 * 1) coresight_release_platform_data() if the probe fails or
680 * this device is unregistered.
681 * 2) While removing the target device via
682 * coresight_remove_match().
684 conn->dest_fwnode = fwnode_handle_get(&r_adev->fwnode);
685 } else if (dir == ACPI_CORESIGHT_LINK_SLAVE) {
687 * We are only interested in the port number
688 * for the input ports at this component.
689 * Store the port number in child_port.
691 conn->dest_port = fields[0].integer.value;
693 /* Invalid direction */
701 * acpi_coresight_parse_graph - Parse the _DSD CoreSight graph
702 * connection information and populate the supplied coresight_platform_data
705 static int acpi_coresight_parse_graph(struct device *dev,
706 struct acpi_device *adev,
707 struct coresight_platform_data *pdata)
710 const union acpi_object *graph;
711 struct coresight_connection conn, zero_conn = {};
712 struct coresight_connection *new_conn;
714 pdata->nr_inconns = pdata->nr_outconns = 0;
715 graph = acpi_get_coresight_graph(adev);
719 nlinks = graph->package.elements[2].integer.value;
723 for (i = 0; i < nlinks; i++) {
724 const union acpi_object *link = &graph->package.elements[3 + i];
728 dir = acpi_coresight_parse_link(adev, link, &conn);
732 if (dir == ACPI_CORESIGHT_LINK_MASTER) {
733 if (conn.src_port >= pdata->high_outport)
734 pdata->high_outport = conn.src_port + 1;
735 new_conn = coresight_add_out_conn(dev, pdata, &conn);
736 if (IS_ERR(new_conn))
737 return PTR_ERR(new_conn);
739 WARN_ON(pdata->high_inport == conn.dest_port + 1);
741 * We do not track input port connections for a device.
742 * However we need the highest port number described,
743 * which can be recorded now and reuse this connection
744 * record for an output connection. Hence, do not move
745 * the ptr for input connections
747 if (conn.dest_port >= pdata->high_inport)
748 pdata->high_inport = conn.dest_port + 1;
756 * acpi_handle_to_logical_cpuid - Map a given acpi_handle to the
757 * logical CPU id of the corresponding CPU device.
759 * Returns the logical CPU id when found. Otherwise returns >= nr_cpus_id.
762 acpi_handle_to_logical_cpuid(acpi_handle handle)
765 struct acpi_processor *pr;
767 for_each_possible_cpu(i) {
768 pr = per_cpu(processors, i);
769 if (pr && pr->handle == handle)
777 * acpi_coresigh_get_cpu - Find the logical CPU id of the CPU associated
778 * with this coresight device. With ACPI bindings, the CoreSight components
779 * are listed as child device of the associated CPU.
781 * Returns the logical CPU id when found. Otherwise returns 0.
783 static int acpi_coresight_get_cpu(struct device *dev)
786 acpi_handle cpu_handle;
788 struct acpi_device *adev = ACPI_COMPANION(dev);
792 status = acpi_get_parent(adev->handle, &cpu_handle);
793 if (ACPI_FAILURE(status))
796 cpu = acpi_handle_to_logical_cpuid(cpu_handle);
797 if (cpu >= nr_cpu_ids)
803 acpi_get_coresight_platform_data(struct device *dev,
804 struct coresight_platform_data *pdata)
806 struct acpi_device *adev;
808 adev = ACPI_COMPANION(dev);
812 return acpi_coresight_parse_graph(dev, adev, pdata);
818 acpi_get_coresight_platform_data(struct device *dev,
819 struct coresight_platform_data *pdata)
824 static inline int acpi_coresight_get_cpu(struct device *dev)
830 int coresight_get_cpu(struct device *dev)
832 if (is_of_node(dev->fwnode))
833 return of_coresight_get_cpu(dev);
834 else if (is_acpi_device_node(dev->fwnode))
835 return acpi_coresight_get_cpu(dev);
838 EXPORT_SYMBOL_GPL(coresight_get_cpu);
840 struct coresight_platform_data *
841 coresight_get_platform_data(struct device *dev)
844 struct coresight_platform_data *pdata = NULL;
845 struct fwnode_handle *fwnode = dev_fwnode(dev);
847 if (IS_ERR_OR_NULL(fwnode))
850 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
856 if (is_of_node(fwnode))
857 ret = of_get_coresight_platform_data(dev, pdata);
858 else if (is_acpi_device_node(fwnode))
859 ret = acpi_get_coresight_platform_data(dev, pdata);
864 if (!IS_ERR_OR_NULL(pdata))
865 /* Cleanup the connection information */
866 coresight_release_platform_data(NULL, pdata);
869 EXPORT_SYMBOL_GPL(coresight_get_platform_data);