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"
23 * coresight_alloc_conns: Allocate connections record for each output
24 * port from the device.
26 static int coresight_alloc_conns(struct device *dev,
27 struct coresight_platform_data *pdata)
29 if (pdata->nr_outport) {
30 pdata->conns = devm_kzalloc(dev, pdata->nr_outport *
31 sizeof(*pdata->conns),
40 int coresight_device_fwnode_match(struct device *dev, const void *fwnode)
42 return dev_fwnode(dev) == fwnode;
45 static struct device *
46 coresight_find_device_by_fwnode(struct fwnode_handle *fwnode)
48 struct device *dev = NULL;
51 * If we have a non-configurable replicator, it will be found on the
54 dev = bus_find_device(&platform_bus_type, NULL,
55 fwnode, coresight_device_fwnode_match);
60 * We have a configurable component - circle through the AMBA bus
61 * looking for the device that matches the endpoint node.
63 return bus_find_device(&amba_bustype, NULL,
64 fwnode, coresight_device_fwnode_match);
68 static inline bool of_coresight_legacy_ep_is_input(struct device_node *ep)
70 return of_property_read_bool(ep, "slave-mode");
73 static void of_coresight_get_ports_legacy(const struct device_node *node,
74 int *nr_inport, int *nr_outport)
76 struct device_node *ep = NULL;
80 ep = of_graph_get_next_endpoint(node, ep);
84 if (of_coresight_legacy_ep_is_input(ep))
95 static struct device_node *of_coresight_get_port_parent(struct device_node *ep)
97 struct device_node *parent = of_graph_get_port_parent(ep);
100 * Skip one-level up to the real device node, if we
101 * are using the new bindings.
103 if (of_node_name_eq(parent, "in-ports") ||
104 of_node_name_eq(parent, "out-ports"))
105 parent = of_get_next_parent(parent);
110 static inline struct device_node *
111 of_coresight_get_input_ports_node(const struct device_node *node)
113 return of_get_child_by_name(node, "in-ports");
116 static inline struct device_node *
117 of_coresight_get_output_ports_node(const struct device_node *node)
119 return of_get_child_by_name(node, "out-ports");
123 of_coresight_count_ports(struct device_node *port_parent)
126 struct device_node *ep = NULL;
128 while ((ep = of_graph_get_next_endpoint(port_parent, ep)))
133 static void of_coresight_get_ports(const struct device_node *node,
134 int *nr_inport, int *nr_outport)
136 struct device_node *input_ports = NULL, *output_ports = NULL;
138 input_ports = of_coresight_get_input_ports_node(node);
139 output_ports = of_coresight_get_output_ports_node(node);
141 if (input_ports || output_ports) {
143 *nr_inport = of_coresight_count_ports(input_ports);
144 of_node_put(input_ports);
147 *nr_outport = of_coresight_count_ports(output_ports);
148 of_node_put(output_ports);
151 /* Fall back to legacy DT bindings parsing */
152 of_coresight_get_ports_legacy(node, nr_inport, nr_outport);
156 static int of_coresight_get_cpu(struct device *dev)
159 struct device_node *dn;
164 dn = of_parse_phandle(dev->of_node, "cpu", 0);
168 cpu = of_cpu_node_to_id(dn);
175 * of_coresight_parse_endpoint : Parse the given output endpoint @ep
176 * and fill the connection information in @conn
178 * Parses the local port, remote device name and the remote port.
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.
186 static int of_coresight_parse_endpoint(struct device *dev,
187 struct device_node *ep,
188 struct coresight_connection *conn)
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;
198 /* Parse the local port details */
199 if (of_graph_parse_endpoint(ep, &endpoint))
202 * Get a handle on the remote endpoint and the device it is
205 rep = of_graph_get_remote_endpoint(ep);
208 rparent = of_coresight_get_port_parent(rep);
211 if (of_graph_parse_endpoint(rep, &rendpoint))
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);
222 conn->outport = endpoint.port;
224 * Hold the refcount to the target device. This could be
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()
231 conn->child_fwnode = fwnode_handle_get(rdev_fwnode);
232 conn->child_port = rendpoint.port;
233 /* Connection record updated */
237 of_node_put(rparent);
244 static int of_get_coresight_platform_data(struct device *dev,
245 struct coresight_platform_data *pdata)
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;
254 /* Get the number of input and output port for this component */
255 of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
257 /* If there are no output connections, we are done */
258 if (!pdata->nr_outport)
261 ret = coresight_alloc_conns(dev, pdata);
265 parent = of_coresight_get_output_ports_node(node);
267 * If the DT uses obsoleted bindings, the ports are listed
268 * under the device and we need to filter out the input
272 legacy_binding = true;
274 dev_warn_once(dev, "Uses obsolete Coresight DT bindings\n");
279 /* Iterate through each output port to discover topology */
280 while ((ep = of_graph_get_next_endpoint(parent, ep))) {
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.
287 if (legacy_binding && of_coresight_legacy_ep_is_input(ep))
290 ret = of_coresight_parse_endpoint(dev, ep, conn);
293 conn++; /* Fall through */
305 of_get_coresight_platform_data(struct device *dev,
306 struct coresight_platform_data *pdata)
311 static inline int of_coresight_get_cpu(struct device *dev)
319 #include <acpi/actypes.h>
320 #include <acpi/processor.h>
322 /* ACPI Graph _DSD UUID : "ab02a46b-74c7-45a2-bd68-f7d344ef2153" */
323 static const guid_t acpi_graph_uuid = GUID_INIT(0xab02a46b, 0x74c7, 0x45a2,
324 0xbd, 0x68, 0xf7, 0xd3,
325 0x44, 0xef, 0x21, 0x53);
326 /* Coresight ACPI Graph UUID : "3ecbc8b6-1d0e-4fb3-8107-e627f805c6cd" */
327 static const guid_t coresight_graph_uuid = GUID_INIT(0x3ecbc8b6, 0x1d0e, 0x4fb3,
328 0x81, 0x07, 0xe6, 0x27,
329 0xf8, 0x05, 0xc6, 0xcd);
330 #define ACPI_CORESIGHT_LINK_SLAVE 0
331 #define ACPI_CORESIGHT_LINK_MASTER 1
333 static inline bool is_acpi_guid(const union acpi_object *obj)
335 return (obj->type == ACPI_TYPE_BUFFER) && (obj->buffer.length == 16);
339 * acpi_guid_matches - Checks if the given object is a GUID object and
340 * that it matches the supplied the GUID.
342 static inline bool acpi_guid_matches(const union acpi_object *obj,
345 return is_acpi_guid(obj) &&
346 guid_equal((guid_t *)obj->buffer.pointer, guid);
349 static inline bool is_acpi_dsd_graph_guid(const union acpi_object *obj)
351 return acpi_guid_matches(obj, &acpi_graph_uuid);
354 static inline bool is_acpi_coresight_graph_guid(const union acpi_object *obj)
356 return acpi_guid_matches(obj, &coresight_graph_uuid);
359 static inline bool is_acpi_coresight_graph(const union acpi_object *obj)
361 const union acpi_object *graphid, *guid, *links;
363 if (obj->type != ACPI_TYPE_PACKAGE ||
364 obj->package.count < 3)
367 graphid = &obj->package.elements[0];
368 guid = &obj->package.elements[1];
369 links = &obj->package.elements[2];
371 if (graphid->type != ACPI_TYPE_INTEGER ||
372 links->type != ACPI_TYPE_INTEGER)
375 return is_acpi_coresight_graph_guid(guid);
379 * acpi_validate_dsd_graph - Make sure the given _DSD graph conforms
380 * to the ACPI _DSD Graph specification.
382 * ACPI Devices Graph property has the following format:
384 * Revision - Integer, must be 0
385 * NumberOfGraphs - Integer, N indicating the following list.
391 * And each Graph entry has the following format:
393 * GraphID - Integer, identifying a graph the device belongs to.
394 * UUID - UUID identifying the specification that governs
395 * this graph. (e.g, see is_acpi_coresight_graph())
396 * NumberOfLinks - Number "N" of connections on this node of the graph.
402 * Where each "Links" entry has the following format:
405 * SourcePortAddress - Integer
406 * DestinationPortAddress - Integer
407 * DestinationDeviceName - Reference to another device
408 * ( --- CoreSight specific extensions below ---)
409 * DirectionOfFlow - Integer 1 for output(master)
414 * For a Funnel device
419 * Name (_DSD, Package() {
420 * // DSD Package contains tuples of { Proeprty_Type_UUID, Package() }
421 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), //Std. Property UUID
423 * Package(2) { "property-name", <property-value> }
426 * ToUUID("ab02a46b-74c7-45a2-bd68-f7d344ef2153"), // ACPI Graph UUID
429 * 1, // NumberOfGraphs.
430 * Package() { // Graph[0] Package
432 * // Coresight Graph UUID
433 * ToUUID("3ecbc8b6-1d0e-4fb3-8107-e627f805c6cd"),
434 * 3, // NumberOfLinks aka ports
435 * // Link[0]: Output_0 -> Replicator:Input_0
436 * Package () { 0, 0, \_SB_.RPL0, 1 },
437 * // Link[1]: Input_0 <- Cluster0_Funnel0:Output_0
438 * Package () { 0, 0, \_SB_.CLU0.FUN0, 0 },
439 * // Link[2]: Input_1 <- Cluster1_Funnel0:Output_0
440 * Package () { 1, 0, \_SB_.CLU1.FUN0, 0 },
441 * } // End of Graph[0] Package
443 * }, // End of ACPI Graph Property
446 static inline bool acpi_validate_dsd_graph(const union acpi_object *graph)
449 const union acpi_object *rev, *nr_graphs;
451 /* The graph must contain at least the Revision and Number of Graphs */
452 if (graph->package.count < 2)
455 rev = &graph->package.elements[0];
456 nr_graphs = &graph->package.elements[1];
458 if (rev->type != ACPI_TYPE_INTEGER ||
459 nr_graphs->type != ACPI_TYPE_INTEGER)
462 /* We only support revision 0 */
463 if (rev->integer.value != 0)
466 n = nr_graphs->integer.value;
467 /* CoreSight devices are only part of a single Graph */
471 /* Make sure the ACPI graph package has right number of elements */
472 if (graph->package.count != (n + 2))
476 * Each entry must be a graph package with at least 3 members :
477 * { GraphID, UUID, NumberOfLinks(n), Links[.],... }
479 for (i = 2; i < n + 2; i++) {
480 const union acpi_object *obj = &graph->package.elements[i];
482 if (obj->type != ACPI_TYPE_PACKAGE ||
483 obj->package.count < 3)
490 /* acpi_get_dsd_graph - Find the _DSD Graph property for the given device. */
491 const union acpi_object *
492 acpi_get_dsd_graph(struct acpi_device *adev)
495 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
497 const union acpi_object *dsd;
499 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL,
500 &buf, ACPI_TYPE_PACKAGE);
501 if (ACPI_FAILURE(status))
507 * _DSD property consists tuples { Prop_UUID, Package() }
508 * Iterate through all the packages and find the Graph.
510 for (i = 0; i + 1 < dsd->package.count; i += 2) {
511 const union acpi_object *guid, *package;
513 guid = &dsd->package.elements[i];
514 package = &dsd->package.elements[i + 1];
516 /* All _DSD elements must have a UUID and a Package */
517 if (!is_acpi_guid(guid) || package->type != ACPI_TYPE_PACKAGE)
519 /* Skip the non-Graph _DSD packages */
520 if (!is_acpi_dsd_graph_guid(guid))
522 if (acpi_validate_dsd_graph(package))
524 /* Invalid graph format, continue */
525 dev_warn(&adev->dev, "Invalid Graph _DSD property\n");
532 acpi_validate_coresight_graph(const union acpi_object *cs_graph)
536 nlinks = cs_graph->package.elements[2].integer.value;
538 * Graph must have the following fields :
539 * { GraphID, GraphUUID, NumberOfLinks, Links... }
541 if (cs_graph->package.count != (nlinks + 3))
543 /* The links are validated in acpi_coresight_parse_link() */
548 * acpi_get_coresight_graph - Parse the device _DSD tables and find
549 * the Graph property matching the CoreSight Graphs.
551 * Returns the pointer to the CoreSight Graph Package when found. Otherwise
554 const union acpi_object *
555 acpi_get_coresight_graph(struct acpi_device *adev)
557 const union acpi_object *graph_list, *graph;
560 graph_list = acpi_get_dsd_graph(adev);
564 nr_graphs = graph_list->package.elements[1].integer.value;
566 for (i = 2; i < nr_graphs + 2; i++) {
567 graph = &graph_list->package.elements[i];
568 if (!is_acpi_coresight_graph(graph))
570 if (acpi_validate_coresight_graph(graph))
572 /* Invalid graph format */
580 * acpi_coresight_parse_link - Parse the given Graph connection
581 * of the device and populate the coresight_connection for an output
584 * CoreSight Graph specification mandates that the direction of the data
585 * flow must be specified in the link. i.e,
587 * SourcePortAddress, // Integer
588 * DestinationPortAddress, // Integer
589 * DestinationDeviceName, // Reference to another device
590 * DirectionOfFlow, // 1 for output(master), 0 for input(slave)
592 * Returns the direction of the data flow [ Input(slave) or Output(master) ]
594 * Returns an negative error number otherwise.
596 static int acpi_coresight_parse_link(struct acpi_device *adev,
597 const union acpi_object *link,
598 struct coresight_connection *conn)
601 const union acpi_object *fields;
602 struct acpi_device *r_adev;
605 if (link->type != ACPI_TYPE_PACKAGE ||
606 link->package.count != 4)
609 fields = link->package.elements;
611 if (fields[0].type != ACPI_TYPE_INTEGER ||
612 fields[1].type != ACPI_TYPE_INTEGER ||
613 fields[2].type != ACPI_TYPE_LOCAL_REFERENCE ||
614 fields[3].type != ACPI_TYPE_INTEGER)
617 rc = acpi_bus_get_device(fields[2].reference.handle, &r_adev);
621 dir = fields[3].integer.value;
622 if (dir == ACPI_CORESIGHT_LINK_MASTER) {
623 conn->outport = fields[0].integer.value;
624 conn->child_port = fields[1].integer.value;
625 rdev = coresight_find_device_by_fwnode(&r_adev->fwnode);
627 return -EPROBE_DEFER;
629 * Hold the refcount to the target device. This could be
631 * 1) coresight_release_platform_data() if the probe fails or
632 * this device is unregistered.
633 * 2) While removing the target device via
634 * coresight_remove_match().
636 conn->child_fwnode = fwnode_handle_get(&r_adev->fwnode);
643 * acpi_coresight_parse_graph - Parse the _DSD CoreSight graph
644 * connection information and populate the supplied coresight_platform_data
647 static int acpi_coresight_parse_graph(struct acpi_device *adev,
648 struct coresight_platform_data *pdata)
651 const union acpi_object *graph;
652 struct coresight_connection *conns, *ptr;
654 pdata->nr_inport = pdata->nr_outport = 0;
655 graph = acpi_get_coresight_graph(adev);
659 nlinks = graph->package.elements[2].integer.value;
664 * To avoid scanning the table twice (once for finding the number of
665 * output links and then later for parsing the output links),
666 * cache the links information in one go and then later copy
669 conns = devm_kcalloc(&adev->dev, nlinks, sizeof(*conns), GFP_KERNEL);
673 for (i = 0; i < nlinks; i++) {
674 const union acpi_object *link = &graph->package.elements[3 + i];
677 dir = acpi_coresight_parse_link(adev, link, ptr);
681 if (dir == ACPI_CORESIGHT_LINK_MASTER) {
689 rc = coresight_alloc_conns(&adev->dev, pdata);
693 /* Copy the connection information to the final location */
694 for (i = 0; i < pdata->nr_outport; i++)
695 pdata->conns[i] = conns[i];
697 devm_kfree(&adev->dev, conns);
702 * acpi_handle_to_logical_cpuid - Map a given acpi_handle to the
703 * logical CPU id of the corresponding CPU device.
705 * Returns the logical CPU id when found. Otherwise returns >= nr_cpus_id.
708 acpi_handle_to_logical_cpuid(acpi_handle handle)
711 struct acpi_processor *pr;
713 for_each_possible_cpu(i) {
714 pr = per_cpu(processors, i);
715 if (pr && pr->handle == handle)
723 * acpi_coresigh_get_cpu - Find the logical CPU id of the CPU associated
724 * with this coresight device. With ACPI bindings, the CoreSight components
725 * are listed as child device of the associated CPU.
727 * Returns the logical CPU id when found. Otherwise returns 0.
729 static int acpi_coresight_get_cpu(struct device *dev)
732 acpi_handle cpu_handle;
734 struct acpi_device *adev = ACPI_COMPANION(dev);
738 status = acpi_get_parent(adev->handle, &cpu_handle);
739 if (ACPI_FAILURE(status))
742 cpu = acpi_handle_to_logical_cpuid(cpu_handle);
743 if (cpu >= nr_cpu_ids)
749 acpi_get_coresight_platform_data(struct device *dev,
750 struct coresight_platform_data *pdata)
752 struct acpi_device *adev;
754 adev = ACPI_COMPANION(dev);
758 return acpi_coresight_parse_graph(adev, pdata);
764 acpi_get_coresight_platform_data(struct device *dev,
765 struct coresight_platform_data *pdata)
770 static inline int acpi_coresight_get_cpu(struct device *dev)
776 int coresight_get_cpu(struct device *dev)
778 if (is_of_node(dev->fwnode))
779 return of_coresight_get_cpu(dev);
780 else if (is_acpi_device_node(dev->fwnode))
781 return acpi_coresight_get_cpu(dev);
784 EXPORT_SYMBOL_GPL(coresight_get_cpu);
786 struct coresight_platform_data *
787 coresight_get_platform_data(struct device *dev)
790 struct coresight_platform_data *pdata = NULL;
791 struct fwnode_handle *fwnode = dev_fwnode(dev);
793 if (IS_ERR_OR_NULL(fwnode))
796 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
802 if (is_of_node(fwnode))
803 ret = of_get_coresight_platform_data(dev, pdata);
804 else if (is_acpi_device_node(fwnode))
805 ret = acpi_get_coresight_platform_data(dev, pdata);
810 if (!IS_ERR_OR_NULL(pdata))
811 /* Cleanup the connection information */
812 coresight_release_platform_data(pdata);
815 EXPORT_SYMBOL_GPL(coresight_get_platform_data);