of_coresight.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/types.h>
  6. #include <linux/err.h>
  7. #include <linux/slab.h>
  8. #include <linux/clk.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/of_graph.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/amba/bus.h>
  15. #include <linux/coresight.h>
  16. #include <linux/cpumask.h>
  17. #include <asm/smp_plat.h>
  18. static int of_dev_node_match(struct device *dev, void *data)
  19. {
  20. return dev->of_node == data;
  21. }
  22. static struct device *
  23. of_coresight_get_endpoint_device(struct device_node *endpoint)
  24. {
  25. struct device *dev = NULL;
  26. /*
  27. * If we have a non-configurable replicator, it will be found on the
  28. * platform bus.
  29. */
  30. dev = bus_find_device(&platform_bus_type, NULL,
  31. endpoint, of_dev_node_match);
  32. if (dev)
  33. return dev;
  34. /*
  35. * We have a configurable component - circle through the AMBA bus
  36. * looking for the device that matches the endpoint node.
  37. */
  38. return bus_find_device(&amba_bustype, NULL,
  39. endpoint, of_dev_node_match);
  40. }
  41. static inline bool of_coresight_legacy_ep_is_input(struct device_node *ep)
  42. {
  43. return of_property_read_bool(ep, "slave-mode");
  44. }
  45. static void of_coresight_get_ports_legacy(const struct device_node *node,
  46. int *nr_inport, int *nr_outport)
  47. {
  48. struct device_node *ep = NULL;
  49. int in = 0, out = 0;
  50. do {
  51. ep = of_graph_get_next_endpoint(node, ep);
  52. if (!ep)
  53. break;
  54. if (of_coresight_legacy_ep_is_input(ep))
  55. in++;
  56. else
  57. out++;
  58. } while (ep);
  59. *nr_inport = in;
  60. *nr_outport = out;
  61. }
  62. static struct device_node *of_coresight_get_port_parent(struct device_node *ep)
  63. {
  64. struct device_node *parent = of_graph_get_port_parent(ep);
  65. /*
  66. * Skip one-level up to the real device node, if we
  67. * are using the new bindings.
  68. */
  69. if (!of_node_cmp(parent->name, "in-ports") ||
  70. !of_node_cmp(parent->name, "out-ports"))
  71. parent = of_get_next_parent(parent);
  72. return parent;
  73. }
  74. static inline struct device_node *
  75. of_coresight_get_input_ports_node(const struct device_node *node)
  76. {
  77. return of_get_child_by_name(node, "in-ports");
  78. }
  79. static inline struct device_node *
  80. of_coresight_get_output_ports_node(const struct device_node *node)
  81. {
  82. return of_get_child_by_name(node, "out-ports");
  83. }
  84. static inline int
  85. of_coresight_count_ports(struct device_node *port_parent)
  86. {
  87. int i = 0;
  88. struct device_node *ep = NULL;
  89. while ((ep = of_graph_get_next_endpoint(port_parent, ep)))
  90. i++;
  91. return i;
  92. }
  93. static void of_coresight_get_ports(const struct device_node *node,
  94. int *nr_inport, int *nr_outport)
  95. {
  96. struct device_node *input_ports = NULL, *output_ports = NULL;
  97. input_ports = of_coresight_get_input_ports_node(node);
  98. output_ports = of_coresight_get_output_ports_node(node);
  99. if (input_ports || output_ports) {
  100. if (input_ports) {
  101. *nr_inport = of_coresight_count_ports(input_ports);
  102. of_node_put(input_ports);
  103. }
  104. if (output_ports) {
  105. *nr_outport = of_coresight_count_ports(output_ports);
  106. of_node_put(output_ports);
  107. }
  108. } else {
  109. /* Fall back to legacy DT bindings parsing */
  110. of_coresight_get_ports_legacy(node, nr_inport, nr_outport);
  111. }
  112. }
  113. static int of_coresight_alloc_memory(struct device *dev,
  114. struct coresight_platform_data *pdata)
  115. {
  116. if (pdata->nr_outport) {
  117. pdata->conns = devm_kzalloc(dev, pdata->nr_outport *
  118. sizeof(*pdata->conns),
  119. GFP_KERNEL);
  120. if (!pdata->conns)
  121. return -ENOMEM;
  122. }
  123. return 0;
  124. }
  125. int of_coresight_get_cpu(const struct device_node *node)
  126. {
  127. int cpu;
  128. struct device_node *dn;
  129. dn = of_parse_phandle(node, "cpu", 0);
  130. /* Affinity defaults to CPU0 */
  131. if (!dn)
  132. return 0;
  133. cpu = of_cpu_node_to_id(dn);
  134. of_node_put(dn);
  135. /* Affinity to CPU0 if no cpu nodes are found */
  136. return (cpu < 0) ? 0 : cpu;
  137. }
  138. EXPORT_SYMBOL_GPL(of_coresight_get_cpu);
  139. /*
  140. * of_coresight_parse_endpoint : Parse the given output endpoint @ep
  141. * and fill the connection information in @conn
  142. *
  143. * Parses the local port, remote device name and the remote port.
  144. *
  145. * Returns :
  146. * 1 - If the parsing is successful and a connection record
  147. * was created for an output connection.
  148. * 0 - If the parsing completed without any fatal errors.
  149. * -Errno - Fatal error, abort the scanning.
  150. */
  151. static int of_coresight_parse_endpoint(struct device *dev,
  152. struct device_node *ep,
  153. struct coresight_connection *conn)
  154. {
  155. int ret = 0;
  156. struct of_endpoint endpoint, rendpoint;
  157. struct device_node *rparent = NULL;
  158. struct device_node *rep = NULL;
  159. struct device *rdev = NULL;
  160. do {
  161. /* Parse the local port details */
  162. if (of_graph_parse_endpoint(ep, &endpoint))
  163. break;
  164. /*
  165. * Get a handle on the remote endpoint and the device it is
  166. * attached to.
  167. */
  168. rep = of_graph_get_remote_endpoint(ep);
  169. if (!rep)
  170. break;
  171. rparent = of_coresight_get_port_parent(rep);
  172. if (!rparent)
  173. break;
  174. if (of_graph_parse_endpoint(rep, &rendpoint))
  175. break;
  176. /* If the remote device is not available, defer probing */
  177. rdev = of_coresight_get_endpoint_device(rparent);
  178. if (!rdev) {
  179. ret = -EPROBE_DEFER;
  180. break;
  181. }
  182. conn->outport = endpoint.port;
  183. conn->child_name = devm_kstrdup(dev,
  184. dev_name(rdev),
  185. GFP_KERNEL);
  186. conn->child_port = rendpoint.port;
  187. /* Connection record updated */
  188. ret = 1;
  189. } while (0);
  190. of_node_put(rparent);
  191. of_node_put(rep);
  192. put_device(rdev);
  193. return ret;
  194. }
  195. struct coresight_platform_data *
  196. of_get_coresight_platform_data(struct device *dev,
  197. const struct device_node *node)
  198. {
  199. int ret = 0;
  200. struct coresight_platform_data *pdata;
  201. struct coresight_connection *conn;
  202. struct device_node *ep = NULL;
  203. const struct device_node *parent = NULL;
  204. bool legacy_binding = false;
  205. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  206. if (!pdata)
  207. return ERR_PTR(-ENOMEM);
  208. /* Use device name as sysfs handle */
  209. pdata->name = dev_name(dev);
  210. pdata->cpu = of_coresight_get_cpu(node);
  211. /* Get the number of input and output port for this component */
  212. of_coresight_get_ports(node, &pdata->nr_inport, &pdata->nr_outport);
  213. /* If there are no output connections, we are done */
  214. if (!pdata->nr_outport)
  215. return pdata;
  216. ret = of_coresight_alloc_memory(dev, pdata);
  217. if (ret)
  218. return ERR_PTR(ret);
  219. parent = of_coresight_get_output_ports_node(node);
  220. /*
  221. * If the DT uses obsoleted bindings, the ports are listed
  222. * under the device and we need to filter out the input
  223. * ports.
  224. */
  225. if (!parent) {
  226. legacy_binding = true;
  227. parent = node;
  228. dev_warn_once(dev, "Uses obsolete Coresight DT bindings\n");
  229. }
  230. conn = pdata->conns;
  231. /* Iterate through each output port to discover topology */
  232. while ((ep = of_graph_get_next_endpoint(parent, ep))) {
  233. /*
  234. * Legacy binding mixes input/output ports under the
  235. * same parent. So, skip the input ports if we are dealing
  236. * with legacy binding, as they processed with their
  237. * connected output ports.
  238. */
  239. if (legacy_binding && of_coresight_legacy_ep_is_input(ep))
  240. continue;
  241. ret = of_coresight_parse_endpoint(dev, ep, conn);
  242. switch (ret) {
  243. case 1:
  244. conn++; /* Fall through */
  245. case 0:
  246. break;
  247. default:
  248. return ERR_PTR(ret);
  249. }
  250. }
  251. return pdata;
  252. }
  253. EXPORT_SYMBOL_GPL(of_get_coresight_platform_data);