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 static struct device
*
41 coresight_find_device_by_fwnode(struct fwnode_handle
*fwnode
)
43 struct device
*dev
= NULL
;
46 * If we have a non-configurable replicator, it will be found on the
49 dev
= bus_find_device_by_fwnode(&platform_bus_type
, fwnode
);
54 * We have a configurable component - circle through the AMBA bus
55 * looking for the device that matches the endpoint node.
57 return bus_find_device_by_fwnode(&amba_bustype
, fwnode
);
61 static inline bool of_coresight_legacy_ep_is_input(struct device_node
*ep
)
63 return of_property_read_bool(ep
, "slave-mode");
66 static void of_coresight_get_ports_legacy(const struct device_node
*node
,
67 int *nr_inport
, int *nr_outport
)
69 struct device_node
*ep
= NULL
;
73 ep
= of_graph_get_next_endpoint(node
, ep
);
77 if (of_coresight_legacy_ep_is_input(ep
))
88 static struct device_node
*of_coresight_get_port_parent(struct device_node
*ep
)
90 struct device_node
*parent
= of_graph_get_port_parent(ep
);
93 * Skip one-level up to the real device node, if we
94 * are using the new bindings.
96 if (of_node_name_eq(parent
, "in-ports") ||
97 of_node_name_eq(parent
, "out-ports"))
98 parent
= of_get_next_parent(parent
);
103 static inline struct device_node
*
104 of_coresight_get_input_ports_node(const struct device_node
*node
)
106 return of_get_child_by_name(node
, "in-ports");
109 static inline struct device_node
*
110 of_coresight_get_output_ports_node(const struct device_node
*node
)
112 return of_get_child_by_name(node
, "out-ports");
116 of_coresight_count_ports(struct device_node
*port_parent
)
119 struct device_node
*ep
= NULL
;
121 while ((ep
= of_graph_get_next_endpoint(port_parent
, ep
)))
126 static void of_coresight_get_ports(const struct device_node
*node
,
127 int *nr_inport
, int *nr_outport
)
129 struct device_node
*input_ports
= NULL
, *output_ports
= NULL
;
131 input_ports
= of_coresight_get_input_ports_node(node
);
132 output_ports
= of_coresight_get_output_ports_node(node
);
134 if (input_ports
|| output_ports
) {
136 *nr_inport
= of_coresight_count_ports(input_ports
);
137 of_node_put(input_ports
);
140 *nr_outport
= of_coresight_count_ports(output_ports
);
141 of_node_put(output_ports
);
144 /* Fall back to legacy DT bindings parsing */
145 of_coresight_get_ports_legacy(node
, nr_inport
, nr_outport
);
149 static int of_coresight_get_cpu(struct device
*dev
)
152 struct device_node
*dn
;
157 dn
= of_parse_phandle(dev
->of_node
, "cpu", 0);
161 cpu
= of_cpu_node_to_id(dn
);
168 * of_coresight_parse_endpoint : Parse the given output endpoint @ep
169 * and fill the connection information in @conn
171 * Parses the local port, remote device name and the remote port.
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.
179 static int of_coresight_parse_endpoint(struct device
*dev
,
180 struct device_node
*ep
,
181 struct coresight_connection
*conn
)
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
;
191 /* Parse the local port details */
192 if (of_graph_parse_endpoint(ep
, &endpoint
))
195 * Get a handle on the remote endpoint and the device it is
198 rep
= of_graph_get_remote_endpoint(ep
);
201 rparent
= of_coresight_get_port_parent(rep
);
204 if (of_graph_parse_endpoint(rep
, &rendpoint
))
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
);
215 conn
->outport
= endpoint
.port
;
217 * Hold the refcount to the target device. This could be
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()
224 conn
->child_fwnode
= fwnode_handle_get(rdev_fwnode
);
225 conn
->child_port
= rendpoint
.port
;
226 /* Connection record updated */
230 of_node_put(rparent
);
237 static int of_get_coresight_platform_data(struct device
*dev
,
238 struct coresight_platform_data
*pdata
)
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
;
247 /* Get the number of input and output port for this component */
248 of_coresight_get_ports(node
, &pdata
->nr_inport
, &pdata
->nr_outport
);
250 /* If there are no output connections, we are done */
251 if (!pdata
->nr_outport
)
254 ret
= coresight_alloc_conns(dev
, pdata
);
258 parent
= of_coresight_get_output_ports_node(node
);
260 * If the DT uses obsoleted bindings, the ports are listed
261 * under the device and we need to filter out the input
265 legacy_binding
= true;
267 dev_warn_once(dev
, "Uses obsolete Coresight DT bindings\n");
272 /* Iterate through each output port to discover topology */
273 while ((ep
= of_graph_get_next_endpoint(parent
, ep
))) {
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.
280 if (legacy_binding
&& of_coresight_legacy_ep_is_input(ep
))
283 ret
= of_coresight_parse_endpoint(dev
, ep
, conn
);
286 conn
++; /* Fall through */
298 of_get_coresight_platform_data(struct device
*dev
,
299 struct coresight_platform_data
*pdata
)
304 static inline int of_coresight_get_cpu(struct device
*dev
)
312 #include <acpi/actypes.h>
313 #include <acpi/processor.h>
315 /* ACPI Graph _DSD UUID : "ab02a46b-74c7-45a2-bd68-f7d344ef2153" */
316 static 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" */
320 static 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
326 static inline bool is_acpi_guid(const union acpi_object
*obj
)
328 return (obj
->type
== ACPI_TYPE_BUFFER
) && (obj
->buffer
.length
== 16);
332 * acpi_guid_matches - Checks if the given object is a GUID object and
333 * that it matches the supplied the GUID.
335 static inline bool acpi_guid_matches(const union acpi_object
*obj
,
338 return is_acpi_guid(obj
) &&
339 guid_equal((guid_t
*)obj
->buffer
.pointer
, guid
);
342 static inline bool is_acpi_dsd_graph_guid(const union acpi_object
*obj
)
344 return acpi_guid_matches(obj
, &acpi_graph_uuid
);
347 static inline bool is_acpi_coresight_graph_guid(const union acpi_object
*obj
)
349 return acpi_guid_matches(obj
, &coresight_graph_uuid
);
352 static inline bool is_acpi_coresight_graph(const union acpi_object
*obj
)
354 const union acpi_object
*graphid
, *guid
, *links
;
356 if (obj
->type
!= ACPI_TYPE_PACKAGE
||
357 obj
->package
.count
< 3)
360 graphid
= &obj
->package
.elements
[0];
361 guid
= &obj
->package
.elements
[1];
362 links
= &obj
->package
.elements
[2];
364 if (graphid
->type
!= ACPI_TYPE_INTEGER
||
365 links
->type
!= ACPI_TYPE_INTEGER
)
368 return is_acpi_coresight_graph_guid(guid
);
372 * acpi_validate_dsd_graph - Make sure the given _DSD graph conforms
373 * to the ACPI _DSD Graph specification.
375 * ACPI Devices Graph property has the following format:
377 * Revision - Integer, must be 0
378 * NumberOfGraphs - Integer, N indicating the following list.
384 * And each Graph entry has the following format:
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.
395 * Where each "Links" entry has the following format:
398 * SourcePortAddress - Integer
399 * DestinationPortAddress - Integer
400 * DestinationDeviceName - Reference to another device
401 * ( --- CoreSight specific extensions below ---)
402 * DirectionOfFlow - Integer 1 for output(master)
407 * For a Funnel device
412 * Name (_DSD, Package() {
413 * // DSD Package contains tuples of { Proeprty_Type_UUID, Package() }
414 * ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"), //Std. Property UUID
416 * Package(2) { "property-name", <property-value> }
419 * ToUUID("ab02a46b-74c7-45a2-bd68-f7d344ef2153"), // ACPI Graph UUID
422 * 1, // NumberOfGraphs.
423 * Package() { // Graph[0] Package
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
436 * }, // End of ACPI Graph Property
439 static inline bool acpi_validate_dsd_graph(const union acpi_object
*graph
)
442 const union acpi_object
*rev
, *nr_graphs
;
444 /* The graph must contain at least the Revision and Number of Graphs */
445 if (graph
->package
.count
< 2)
448 rev
= &graph
->package
.elements
[0];
449 nr_graphs
= &graph
->package
.elements
[1];
451 if (rev
->type
!= ACPI_TYPE_INTEGER
||
452 nr_graphs
->type
!= ACPI_TYPE_INTEGER
)
455 /* We only support revision 0 */
456 if (rev
->integer
.value
!= 0)
459 n
= nr_graphs
->integer
.value
;
460 /* CoreSight devices are only part of a single Graph */
464 /* Make sure the ACPI graph package has right number of elements */
465 if (graph
->package
.count
!= (n
+ 2))
469 * Each entry must be a graph package with at least 3 members :
470 * { GraphID, UUID, NumberOfLinks(n), Links[.],... }
472 for (i
= 2; i
< n
+ 2; i
++) {
473 const union acpi_object
*obj
= &graph
->package
.elements
[i
];
475 if (obj
->type
!= ACPI_TYPE_PACKAGE
||
476 obj
->package
.count
< 3)
483 /* acpi_get_dsd_graph - Find the _DSD Graph property for the given device. */
484 const union acpi_object
*
485 acpi_get_dsd_graph(struct acpi_device
*adev
)
488 struct acpi_buffer buf
= { ACPI_ALLOCATE_BUFFER
};
490 const union acpi_object
*dsd
;
492 status
= acpi_evaluate_object_typed(adev
->handle
, "_DSD", NULL
,
493 &buf
, ACPI_TYPE_PACKAGE
);
494 if (ACPI_FAILURE(status
))
500 * _DSD property consists tuples { Prop_UUID, Package() }
501 * Iterate through all the packages and find the Graph.
503 for (i
= 0; i
+ 1 < dsd
->package
.count
; i
+= 2) {
504 const union acpi_object
*guid
, *package
;
506 guid
= &dsd
->package
.elements
[i
];
507 package
= &dsd
->package
.elements
[i
+ 1];
509 /* All _DSD elements must have a UUID and a Package */
510 if (!is_acpi_guid(guid
) || package
->type
!= ACPI_TYPE_PACKAGE
)
512 /* Skip the non-Graph _DSD packages */
513 if (!is_acpi_dsd_graph_guid(guid
))
515 if (acpi_validate_dsd_graph(package
))
517 /* Invalid graph format, continue */
518 dev_warn(&adev
->dev
, "Invalid Graph _DSD property\n");
525 acpi_validate_coresight_graph(const union acpi_object
*cs_graph
)
529 nlinks
= cs_graph
->package
.elements
[2].integer
.value
;
531 * Graph must have the following fields :
532 * { GraphID, GraphUUID, NumberOfLinks, Links... }
534 if (cs_graph
->package
.count
!= (nlinks
+ 3))
536 /* The links are validated in acpi_coresight_parse_link() */
541 * acpi_get_coresight_graph - Parse the device _DSD tables and find
542 * the Graph property matching the CoreSight Graphs.
544 * Returns the pointer to the CoreSight Graph Package when found. Otherwise
547 const union acpi_object
*
548 acpi_get_coresight_graph(struct acpi_device
*adev
)
550 const union acpi_object
*graph_list
, *graph
;
553 graph_list
= acpi_get_dsd_graph(adev
);
557 nr_graphs
= graph_list
->package
.elements
[1].integer
.value
;
559 for (i
= 2; i
< nr_graphs
+ 2; i
++) {
560 graph
= &graph_list
->package
.elements
[i
];
561 if (!is_acpi_coresight_graph(graph
))
563 if (acpi_validate_coresight_graph(graph
))
565 /* Invalid graph format */
573 * acpi_coresight_parse_link - Parse the given Graph connection
574 * of the device and populate the coresight_connection for an output
577 * CoreSight Graph specification mandates that the direction of the data
578 * flow must be specified in the link. i.e,
580 * SourcePortAddress, // Integer
581 * DestinationPortAddress, // Integer
582 * DestinationDeviceName, // Reference to another device
583 * DirectionOfFlow, // 1 for output(master), 0 for input(slave)
585 * Returns the direction of the data flow [ Input(slave) or Output(master) ]
587 * Returns an negative error number otherwise.
589 static int acpi_coresight_parse_link(struct acpi_device
*adev
,
590 const union acpi_object
*link
,
591 struct coresight_connection
*conn
)
594 const union acpi_object
*fields
;
595 struct acpi_device
*r_adev
;
598 if (link
->type
!= ACPI_TYPE_PACKAGE
||
599 link
->package
.count
!= 4)
602 fields
= link
->package
.elements
;
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
)
610 rc
= acpi_bus_get_device(fields
[2].reference
.handle
, &r_adev
);
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
);
620 return -EPROBE_DEFER
;
622 * Hold the refcount to the target device. This could be
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().
629 conn
->child_fwnode
= fwnode_handle_get(&r_adev
->fwnode
);
636 * acpi_coresight_parse_graph - Parse the _DSD CoreSight graph
637 * connection information and populate the supplied coresight_platform_data
640 static int acpi_coresight_parse_graph(struct acpi_device
*adev
,
641 struct coresight_platform_data
*pdata
)
644 const union acpi_object
*graph
;
645 struct coresight_connection
*conns
, *ptr
;
647 pdata
->nr_inport
= pdata
->nr_outport
= 0;
648 graph
= acpi_get_coresight_graph(adev
);
652 nlinks
= graph
->package
.elements
[2].integer
.value
;
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
662 conns
= devm_kcalloc(&adev
->dev
, nlinks
, sizeof(*conns
), GFP_KERNEL
);
666 for (i
= 0; i
< nlinks
; i
++) {
667 const union acpi_object
*link
= &graph
->package
.elements
[3 + i
];
670 dir
= acpi_coresight_parse_link(adev
, link
, ptr
);
674 if (dir
== ACPI_CORESIGHT_LINK_MASTER
) {
682 rc
= coresight_alloc_conns(&adev
->dev
, pdata
);
686 /* Copy the connection information to the final location */
687 for (i
= 0; i
< pdata
->nr_outport
; i
++)
688 pdata
->conns
[i
] = conns
[i
];
690 devm_kfree(&adev
->dev
, conns
);
695 * acpi_handle_to_logical_cpuid - Map a given acpi_handle to the
696 * logical CPU id of the corresponding CPU device.
698 * Returns the logical CPU id when found. Otherwise returns >= nr_cpus_id.
701 acpi_handle_to_logical_cpuid(acpi_handle handle
)
704 struct acpi_processor
*pr
;
706 for_each_possible_cpu(i
) {
707 pr
= per_cpu(processors
, i
);
708 if (pr
&& pr
->handle
== handle
)
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.
720 * Returns the logical CPU id when found. Otherwise returns 0.
722 static int acpi_coresight_get_cpu(struct device
*dev
)
725 acpi_handle cpu_handle
;
727 struct acpi_device
*adev
= ACPI_COMPANION(dev
);
731 status
= acpi_get_parent(adev
->handle
, &cpu_handle
);
732 if (ACPI_FAILURE(status
))
735 cpu
= acpi_handle_to_logical_cpuid(cpu_handle
);
736 if (cpu
>= nr_cpu_ids
)
742 acpi_get_coresight_platform_data(struct device
*dev
,
743 struct coresight_platform_data
*pdata
)
745 struct acpi_device
*adev
;
747 adev
= ACPI_COMPANION(dev
);
751 return acpi_coresight_parse_graph(adev
, pdata
);
757 acpi_get_coresight_platform_data(struct device
*dev
,
758 struct coresight_platform_data
*pdata
)
763 static inline int acpi_coresight_get_cpu(struct device
*dev
)
769 int coresight_get_cpu(struct device
*dev
)
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
);
777 EXPORT_SYMBOL_GPL(coresight_get_cpu
);
779 struct coresight_platform_data
*
780 coresight_get_platform_data(struct device
*dev
)
783 struct coresight_platform_data
*pdata
= NULL
;
784 struct fwnode_handle
*fwnode
= dev_fwnode(dev
);
786 if (IS_ERR_OR_NULL(fwnode
))
789 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
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
);
803 if (!IS_ERR_OR_NULL(pdata
))
804 /* Cleanup the connection information */
805 coresight_release_platform_data(pdata
);
808 EXPORT_SYMBOL_GPL(coresight_get_platform_data
);