1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, The Linux Foundation. All rights reserved.
6 #include <linux/types.h>
8 #include <linux/slab.h>
11 #include <linux/of_address.h>
12 #include <linux/of_graph.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/amba/bus.h>
16 #include <linux/coresight.h>
17 #include <linux/cpumask.h>
18 #include <asm/smp_plat.h>
21 static int of_dev_node_match(struct device *dev, void *data)
23 return dev->of_node == data;
26 static struct device *
27 of_coresight_get_endpoint_device(struct device_node *endpoint)
29 struct device *dev = NULL;
32 * If we have a non-configurable replicator, it will be found on the
35 dev = bus_find_device(&platform_bus_type, NULL,
36 endpoint, of_dev_node_match);
41 * We have a configurable component - circle through the AMBA bus
42 * looking for the device that matches the endpoint node.
44 return bus_find_device(&amba_bustype, NULL,
45 endpoint, of_dev_node_match);
48 static inline bool of_coresight_legacy_ep_is_input(struct device_node *ep)
50 return of_property_read_bool(ep, "slave-mode");
53 static void of_coresight_get_ports_legacy(const struct device_node *node,
54 int *nr_inport, int *nr_outport)
56 struct device_node *ep = NULL;
60 ep = of_graph_get_next_endpoint(node, ep);
64 if (of_coresight_legacy_ep_is_input(ep))
75 static struct device_node *of_coresight_get_port_parent(struct device_node *ep)
77 struct device_node *parent = of_graph_get_port_parent(ep);
80 * Skip one-level up to the real device node, if we
81 * are using the new bindings.
83 if (of_node_name_eq(parent, "in-ports") ||
84 of_node_name_eq(parent, "out-ports"))
85 parent = of_get_next_parent(parent);
90 static inline struct device_node *
91 of_coresight_get_input_ports_node(const struct device_node *node)
93 return of_get_child_by_name(node, "in-ports");
96 static inline struct device_node *
97 of_coresight_get_output_ports_node(const struct device_node *node)
99 return of_get_child_by_name(node, "out-ports");
103 of_coresight_count_ports(struct device_node *port_parent)
106 struct device_node *ep = NULL;
108 while ((ep = of_graph_get_next_endpoint(port_parent, ep)))
113 static void of_coresight_get_ports(const struct device_node *node,
114 int *nr_inport, int *nr_outport)
116 struct device_node *input_ports = NULL, *output_ports = NULL;
118 input_ports = of_coresight_get_input_ports_node(node);
119 output_ports = of_coresight_get_output_ports_node(node);
121 if (input_ports || output_ports) {
123 *nr_inport = of_coresight_count_ports(input_ports);
124 of_node_put(input_ports);
127 *nr_outport = of_coresight_count_ports(output_ports);
128 of_node_put(output_ports);
131 /* Fall back to legacy DT bindings parsing */
132 of_coresight_get_ports_legacy(node, nr_inport, nr_outport);
136 static int of_coresight_alloc_memory(struct device *dev,
137 struct coresight_platform_data *pdata)
139 if (pdata->nr_outport) {
140 pdata->conns = devm_kzalloc(dev, pdata->nr_outport *
141 sizeof(*pdata->conns),
150 int of_coresight_get_cpu(const struct device_node *node)
153 struct device_node *dn;
155 dn = of_parse_phandle(node, "cpu", 0);
156 /* Affinity defaults to CPU0 */
159 cpu = of_cpu_node_to_id(dn);
162 /* Affinity to CPU0 if no cpu nodes are found */
163 return (cpu < 0) ? 0 : cpu;
165 EXPORT_SYMBOL_GPL(of_coresight_get_cpu);
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;
190 /* Parse the local port details */
191 if (of_graph_parse_endpoint(ep, &endpoint))
194 * Get a handle on the remote endpoint and the device it is
197 rep = of_graph_get_remote_endpoint(ep);
200 rparent = of_coresight_get_port_parent(rep);
203 if (of_graph_parse_endpoint(rep, &rendpoint))
206 /* If the remote device is not available, defer probing */
207 rdev = of_coresight_get_endpoint_device(rparent);
213 conn->outport = endpoint.port;
214 conn->child_name = devm_kstrdup(dev,
217 conn->child_port = rendpoint.port;
218 /* Connection record updated */
222 of_node_put(rparent);
229 struct coresight_platform_data *
230 of_get_coresight_platform_data(struct device *dev,
231 const struct device_node *node)
234 struct coresight_platform_data *pdata;
235 struct coresight_connection *conn;
236 struct device_node *ep = NULL;
237 const struct device_node *parent = NULL;
238 bool legacy_binding = false;
240 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
242 return ERR_PTR(-ENOMEM);
244 /* Use device name as sysfs handle */
245 pdata->name = dev_name(dev);
246 pdata->cpu = of_coresight_get_cpu(node);
248 /* Get the number of input and output port for this component */
249 of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
251 /* If there are no output connections, we are done */
252 if (!pdata->nr_outport)
255 ret = of_coresight_alloc_memory(dev, pdata);
259 parent = of_coresight_get_output_ports_node(node);
261 * If the DT uses obsoleted bindings, the ports are listed
262 * under the device and we need to filter out the input
266 legacy_binding = true;
268 dev_warn_once(dev, "Uses obsolete Coresight DT bindings\n");
273 /* Iterate through each output port to discover topology */
274 while ((ep = of_graph_get_next_endpoint(parent, ep))) {
276 * Legacy binding mixes input/output ports under the
277 * same parent. So, skip the input ports if we are dealing
278 * with legacy binding, as they processed with their
279 * connected output ports.
281 if (legacy_binding && of_coresight_legacy_ep_is_input(ep))
284 ret = of_coresight_parse_endpoint(dev, ep, conn);
287 conn++; /* Fall through */
297 EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);