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 * coresight_alloc_conns: Allocate connections record for each output
22 * port from the device.
24 static int coresight_alloc_conns(struct device *dev,
25 struct coresight_platform_data *pdata)
27 if (pdata->nr_outport) {
28 pdata->conns = devm_kzalloc(dev, pdata->nr_outport *
29 sizeof(*pdata->conns),
39 static int of_dev_node_match(struct device *dev, void *data)
41 return dev->of_node == data;
44 static struct device *
45 of_coresight_get_endpoint_device(struct device_node *endpoint)
47 struct device *dev = NULL;
50 * If we have a non-configurable replicator, it will be found on the
53 dev = bus_find_device(&platform_bus_type, NULL,
54 endpoint, of_dev_node_match);
59 * We have a configurable component - circle through the AMBA bus
60 * looking for the device that matches the endpoint node.
62 return bus_find_device(&amba_bustype, NULL,
63 endpoint, of_dev_node_match);
66 static inline bool of_coresight_legacy_ep_is_input(struct device_node *ep)
68 return of_property_read_bool(ep, "slave-mode");
71 static void of_coresight_get_ports_legacy(const struct device_node *node,
72 int *nr_inport, int *nr_outport)
74 struct device_node *ep = NULL;
78 ep = of_graph_get_next_endpoint(node, ep);
82 if (of_coresight_legacy_ep_is_input(ep))
93 static struct device_node *of_coresight_get_port_parent(struct device_node *ep)
95 struct device_node *parent = of_graph_get_port_parent(ep);
98 * Skip one-level up to the real device node, if we
99 * are using the new bindings.
101 if (of_node_name_eq(parent, "in-ports") ||
102 of_node_name_eq(parent, "out-ports"))
103 parent = of_get_next_parent(parent);
108 static inline struct device_node *
109 of_coresight_get_input_ports_node(const struct device_node *node)
111 return of_get_child_by_name(node, "in-ports");
114 static inline struct device_node *
115 of_coresight_get_output_ports_node(const struct device_node *node)
117 return of_get_child_by_name(node, "out-ports");
121 of_coresight_count_ports(struct device_node *port_parent)
124 struct device_node *ep = NULL;
126 while ((ep = of_graph_get_next_endpoint(port_parent, ep)))
131 static void of_coresight_get_ports(const struct device_node *node,
132 int *nr_inport, int *nr_outport)
134 struct device_node *input_ports = NULL, *output_ports = NULL;
136 input_ports = of_coresight_get_input_ports_node(node);
137 output_ports = of_coresight_get_output_ports_node(node);
139 if (input_ports || output_ports) {
141 *nr_inport = of_coresight_count_ports(input_ports);
142 of_node_put(input_ports);
145 *nr_outport = of_coresight_count_ports(output_ports);
146 of_node_put(output_ports);
149 /* Fall back to legacy DT bindings parsing */
150 of_coresight_get_ports_legacy(node, nr_inport, nr_outport);
154 int of_coresight_get_cpu(const struct device_node *node)
157 struct device_node *dn;
159 dn = of_parse_phandle(node, "cpu", 0);
160 /* Affinity defaults to CPU0 */
163 cpu = of_cpu_node_to_id(dn);
166 /* Affinity to CPU0 if no cpu nodes are found */
167 return (cpu < 0) ? 0 : cpu;
169 EXPORT_SYMBOL_GPL(of_coresight_get_cpu);
172 * of_coresight_parse_endpoint : Parse the given output endpoint @ep
173 * and fill the connection information in @conn
175 * Parses the local port, remote device name and the remote port.
178 * 1 - If the parsing is successful and a connection record
179 * was created for an output connection.
180 * 0 - If the parsing completed without any fatal errors.
181 * -Errno - Fatal error, abort the scanning.
183 static int of_coresight_parse_endpoint(struct device *dev,
184 struct device_node *ep,
185 struct coresight_connection *conn)
188 struct of_endpoint endpoint, rendpoint;
189 struct device_node *rparent = NULL;
190 struct device_node *rep = NULL;
191 struct device *rdev = NULL;
194 /* Parse the local port details */
195 if (of_graph_parse_endpoint(ep, &endpoint))
198 * Get a handle on the remote endpoint and the device it is
201 rep = of_graph_get_remote_endpoint(ep);
204 rparent = of_coresight_get_port_parent(rep);
207 if (of_graph_parse_endpoint(rep, &rendpoint))
210 /* If the remote device is not available, defer probing */
211 rdev = of_coresight_get_endpoint_device(rparent);
217 conn->outport = endpoint.port;
218 conn->child_name = devm_kstrdup(dev,
221 conn->child_port = rendpoint.port;
222 /* Connection record updated */
226 of_node_put(rparent);
233 struct coresight_platform_data *
234 of_get_coresight_platform_data(struct device *dev,
235 const struct device_node *node)
238 struct coresight_platform_data *pdata;
239 struct coresight_connection *conn;
240 struct device_node *ep = NULL;
241 const struct device_node *parent = NULL;
242 bool legacy_binding = false;
244 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
246 return ERR_PTR(-ENOMEM);
248 /* Use device name as sysfs handle */
249 pdata->name = dev_name(dev);
250 pdata->cpu = of_coresight_get_cpu(node);
252 /* Get the number of input and output port for this component */
253 of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
255 /* If there are no output connections, we are done */
256 if (!pdata->nr_outport)
259 ret = coresight_alloc_conns(dev, pdata);
263 parent = of_coresight_get_output_ports_node(node);
265 * If the DT uses obsoleted bindings, the ports are listed
266 * under the device and we need to filter out the input
270 legacy_binding = true;
272 dev_warn_once(dev, "Uses obsolete Coresight DT bindings\n");
277 /* Iterate through each output port to discover topology */
278 while ((ep = of_graph_get_next_endpoint(parent, ep))) {
280 * Legacy binding mixes input/output ports under the
281 * same parent. So, skip the input ports if we are dealing
282 * with legacy binding, as they processed with their
283 * connected output ports.
285 if (legacy_binding && of_coresight_legacy_ep_is_input(ep))
288 ret = of_coresight_parse_endpoint(dev, ep, conn);
291 conn++; /* Fall through */
301 EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);