coresight.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/device.h>
  9. #include <linux/io.h>
  10. #include <linux/err.h>
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/mutex.h>
  14. #include <linux/clk.h>
  15. #include <linux/coresight.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/delay.h>
  18. #include <linux/pm_runtime.h>
  19. #include "coresight-priv.h"
  20. static DEFINE_MUTEX(coresight_mutex);
  21. /**
  22. * struct coresight_node - elements of a path, from source to sink
  23. * @csdev: Address of an element.
  24. * @link: hook to the list.
  25. */
  26. struct coresight_node {
  27. struct coresight_device *csdev;
  28. struct list_head link;
  29. };
  30. /*
  31. * When operating Coresight drivers from the sysFS interface, only a single
  32. * path can exist from a tracer (associated to a CPU) to a sink.
  33. */
  34. static DEFINE_PER_CPU(struct list_head *, tracer_path);
  35. /*
  36. * As of this writing only a single STM can be found in CS topologies. Since
  37. * there is no way to know if we'll ever see more and what kind of
  38. * configuration they will enact, for the time being only define a single path
  39. * for STM.
  40. */
  41. static struct list_head *stm_path;
  42. /*
  43. * When losing synchronisation a new barrier packet needs to be inserted at the
  44. * beginning of the data collected in a buffer. That way the decoder knows that
  45. * it needs to look for another sync sequence.
  46. */
  47. const u32 barrier_pkt[4] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
  48. static int coresight_id_match(struct device *dev, void *data)
  49. {
  50. int trace_id, i_trace_id;
  51. struct coresight_device *csdev, *i_csdev;
  52. csdev = data;
  53. i_csdev = to_coresight_device(dev);
  54. /*
  55. * No need to care about oneself and components that are not
  56. * sources or not enabled
  57. */
  58. if (i_csdev == csdev || !i_csdev->enable ||
  59. i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
  60. return 0;
  61. /* Get the source ID for both compoment */
  62. trace_id = source_ops(csdev)->trace_id(csdev);
  63. i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
  64. /* All you need is one */
  65. if (trace_id == i_trace_id)
  66. return 1;
  67. return 0;
  68. }
  69. static int coresight_source_is_unique(struct coresight_device *csdev)
  70. {
  71. int trace_id = source_ops(csdev)->trace_id(csdev);
  72. /* this shouldn't happen */
  73. if (trace_id < 0)
  74. return 0;
  75. return !bus_for_each_dev(&coresight_bustype, NULL,
  76. csdev, coresight_id_match);
  77. }
  78. static int coresight_find_link_inport(struct coresight_device *csdev,
  79. struct coresight_device *parent)
  80. {
  81. int i;
  82. struct coresight_connection *conn;
  83. for (i = 0; i < parent->nr_outport; i++) {
  84. conn = &parent->conns[i];
  85. if (conn->child_dev == csdev)
  86. return conn->child_port;
  87. }
  88. dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
  89. dev_name(&parent->dev), dev_name(&csdev->dev));
  90. return -ENODEV;
  91. }
  92. static int coresight_find_link_outport(struct coresight_device *csdev,
  93. struct coresight_device *child)
  94. {
  95. int i;
  96. struct coresight_connection *conn;
  97. for (i = 0; i < csdev->nr_outport; i++) {
  98. conn = &csdev->conns[i];
  99. if (conn->child_dev == child)
  100. return conn->outport;
  101. }
  102. dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
  103. dev_name(&csdev->dev), dev_name(&child->dev));
  104. return -ENODEV;
  105. }
  106. static int coresight_enable_sink(struct coresight_device *csdev, u32 mode)
  107. {
  108. int ret;
  109. /*
  110. * We need to make sure the "new" session is compatible with the
  111. * existing "mode" of operation.
  112. */
  113. if (sink_ops(csdev)->enable) {
  114. ret = sink_ops(csdev)->enable(csdev, mode);
  115. if (ret)
  116. return ret;
  117. csdev->enable = true;
  118. }
  119. atomic_inc(csdev->refcnt);
  120. return 0;
  121. }
  122. static void coresight_disable_sink(struct coresight_device *csdev)
  123. {
  124. if (atomic_dec_return(csdev->refcnt) == 0) {
  125. if (sink_ops(csdev)->disable) {
  126. sink_ops(csdev)->disable(csdev);
  127. csdev->enable = false;
  128. }
  129. }
  130. }
  131. static int coresight_enable_link(struct coresight_device *csdev,
  132. struct coresight_device *parent,
  133. struct coresight_device *child)
  134. {
  135. int ret;
  136. int link_subtype;
  137. int refport, inport, outport;
  138. if (!parent || !child)
  139. return -EINVAL;
  140. inport = coresight_find_link_inport(csdev, parent);
  141. outport = coresight_find_link_outport(csdev, child);
  142. link_subtype = csdev->subtype.link_subtype;
  143. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
  144. refport = inport;
  145. else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
  146. refport = outport;
  147. else
  148. refport = 0;
  149. if (refport < 0)
  150. return refport;
  151. if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
  152. if (link_ops(csdev)->enable) {
  153. ret = link_ops(csdev)->enable(csdev, inport, outport);
  154. if (ret)
  155. return ret;
  156. }
  157. }
  158. csdev->enable = true;
  159. return 0;
  160. }
  161. static void coresight_disable_link(struct coresight_device *csdev,
  162. struct coresight_device *parent,
  163. struct coresight_device *child)
  164. {
  165. int i, nr_conns;
  166. int link_subtype;
  167. int refport, inport, outport;
  168. if (!parent || !child)
  169. return;
  170. inport = coresight_find_link_inport(csdev, parent);
  171. outport = coresight_find_link_outport(csdev, child);
  172. link_subtype = csdev->subtype.link_subtype;
  173. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
  174. refport = inport;
  175. nr_conns = csdev->nr_inport;
  176. } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
  177. refport = outport;
  178. nr_conns = csdev->nr_outport;
  179. } else {
  180. refport = 0;
  181. nr_conns = 1;
  182. }
  183. if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
  184. if (link_ops(csdev)->disable)
  185. link_ops(csdev)->disable(csdev, inport, outport);
  186. }
  187. for (i = 0; i < nr_conns; i++)
  188. if (atomic_read(&csdev->refcnt[i]) != 0)
  189. return;
  190. csdev->enable = false;
  191. }
  192. static int coresight_enable_source(struct coresight_device *csdev, u32 mode)
  193. {
  194. int ret;
  195. if (!coresight_source_is_unique(csdev)) {
  196. dev_warn(&csdev->dev, "traceID %d not unique\n",
  197. source_ops(csdev)->trace_id(csdev));
  198. return -EINVAL;
  199. }
  200. if (!csdev->enable) {
  201. if (source_ops(csdev)->enable) {
  202. ret = source_ops(csdev)->enable(csdev, NULL, mode);
  203. if (ret)
  204. return ret;
  205. }
  206. csdev->enable = true;
  207. }
  208. atomic_inc(csdev->refcnt);
  209. return 0;
  210. }
  211. /**
  212. * coresight_disable_source - Drop the reference count by 1 and disable
  213. * the device if there are no users left.
  214. *
  215. * @csdev - The coresight device to disable
  216. *
  217. * Returns true if the device has been disabled.
  218. */
  219. static bool coresight_disable_source(struct coresight_device *csdev)
  220. {
  221. if (atomic_dec_return(csdev->refcnt) == 0) {
  222. if (source_ops(csdev)->disable)
  223. source_ops(csdev)->disable(csdev, NULL);
  224. csdev->enable = false;
  225. }
  226. return !csdev->enable;
  227. }
  228. void coresight_disable_path(struct list_head *path)
  229. {
  230. u32 type;
  231. struct coresight_node *nd;
  232. struct coresight_device *csdev, *parent, *child;
  233. list_for_each_entry(nd, path, link) {
  234. csdev = nd->csdev;
  235. type = csdev->type;
  236. /*
  237. * ETF devices are tricky... They can be a link or a sink,
  238. * depending on how they are configured. If an ETF has been
  239. * "activated" it will be configured as a sink, otherwise
  240. * go ahead with the link configuration.
  241. */
  242. if (type == CORESIGHT_DEV_TYPE_LINKSINK)
  243. type = (csdev == coresight_get_sink(path)) ?
  244. CORESIGHT_DEV_TYPE_SINK :
  245. CORESIGHT_DEV_TYPE_LINK;
  246. switch (type) {
  247. case CORESIGHT_DEV_TYPE_SINK:
  248. coresight_disable_sink(csdev);
  249. break;
  250. case CORESIGHT_DEV_TYPE_SOURCE:
  251. /* sources are disabled from either sysFS or Perf */
  252. break;
  253. case CORESIGHT_DEV_TYPE_LINK:
  254. parent = list_prev_entry(nd, link)->csdev;
  255. child = list_next_entry(nd, link)->csdev;
  256. coresight_disable_link(csdev, parent, child);
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. }
  263. int coresight_enable_path(struct list_head *path, u32 mode)
  264. {
  265. int ret = 0;
  266. u32 type;
  267. struct coresight_node *nd;
  268. struct coresight_device *csdev, *parent, *child;
  269. list_for_each_entry_reverse(nd, path, link) {
  270. csdev = nd->csdev;
  271. type = csdev->type;
  272. /*
  273. * ETF devices are tricky... They can be a link or a sink,
  274. * depending on how they are configured. If an ETF has been
  275. * "activated" it will be configured as a sink, otherwise
  276. * go ahead with the link configuration.
  277. */
  278. if (type == CORESIGHT_DEV_TYPE_LINKSINK)
  279. type = (csdev == coresight_get_sink(path)) ?
  280. CORESIGHT_DEV_TYPE_SINK :
  281. CORESIGHT_DEV_TYPE_LINK;
  282. switch (type) {
  283. case CORESIGHT_DEV_TYPE_SINK:
  284. ret = coresight_enable_sink(csdev, mode);
  285. /*
  286. * Sink is the first component turned on. If we
  287. * failed to enable the sink, there are no components
  288. * that need disabling. Disabling the path here
  289. * would mean we could disrupt an existing session.
  290. */
  291. if (ret)
  292. goto out;
  293. break;
  294. case CORESIGHT_DEV_TYPE_SOURCE:
  295. /* sources are enabled from either sysFS or Perf */
  296. break;
  297. case CORESIGHT_DEV_TYPE_LINK:
  298. parent = list_prev_entry(nd, link)->csdev;
  299. child = list_next_entry(nd, link)->csdev;
  300. ret = coresight_enable_link(csdev, parent, child);
  301. if (ret)
  302. goto err;
  303. break;
  304. default:
  305. goto err;
  306. }
  307. }
  308. out:
  309. return ret;
  310. err:
  311. coresight_disable_path(path);
  312. goto out;
  313. }
  314. struct coresight_device *coresight_get_sink(struct list_head *path)
  315. {
  316. struct coresight_device *csdev;
  317. if (!path)
  318. return NULL;
  319. csdev = list_last_entry(path, struct coresight_node, link)->csdev;
  320. if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
  321. csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
  322. return NULL;
  323. return csdev;
  324. }
  325. static int coresight_enabled_sink(struct device *dev, void *data)
  326. {
  327. bool *reset = data;
  328. struct coresight_device *csdev = to_coresight_device(dev);
  329. if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
  330. csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
  331. csdev->activated) {
  332. /*
  333. * Now that we have a handle on the sink for this session,
  334. * disable the sysFS "enable_sink" flag so that possible
  335. * concurrent perf session that wish to use another sink don't
  336. * trip on it. Doing so has no ramification for the current
  337. * session.
  338. */
  339. if (*reset)
  340. csdev->activated = false;
  341. return 1;
  342. }
  343. return 0;
  344. }
  345. /**
  346. * coresight_get_enabled_sink - returns the first enabled sink found on the bus
  347. * @deactivate: Whether the 'enable_sink' flag should be reset
  348. *
  349. * When operated from perf the deactivate parameter should be set to 'true'.
  350. * That way the "enabled_sink" flag of the sink that was selected can be reset,
  351. * allowing for other concurrent perf sessions to choose a different sink.
  352. *
  353. * When operated from sysFS users have full control and as such the deactivate
  354. * parameter should be set to 'false', hence mandating users to explicitly
  355. * clear the flag.
  356. */
  357. struct coresight_device *coresight_get_enabled_sink(bool deactivate)
  358. {
  359. struct device *dev = NULL;
  360. dev = bus_find_device(&coresight_bustype, NULL, &deactivate,
  361. coresight_enabled_sink);
  362. return dev ? to_coresight_device(dev) : NULL;
  363. }
  364. /*
  365. * coresight_grab_device - Power up this device and any of the helper
  366. * devices connected to it for trace operation. Since the helper devices
  367. * don't appear on the trace path, they should be handled along with the
  368. * the master device.
  369. */
  370. static void coresight_grab_device(struct coresight_device *csdev)
  371. {
  372. int i;
  373. for (i = 0; i < csdev->nr_outport; i++) {
  374. struct coresight_device *child = csdev->conns[i].child_dev;
  375. if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
  376. pm_runtime_get_sync(child->dev.parent);
  377. }
  378. pm_runtime_get_sync(csdev->dev.parent);
  379. }
  380. /*
  381. * coresight_drop_device - Release this device and any of the helper
  382. * devices connected to it.
  383. */
  384. static void coresight_drop_device(struct coresight_device *csdev)
  385. {
  386. int i;
  387. pm_runtime_put(csdev->dev.parent);
  388. for (i = 0; i < csdev->nr_outport; i++) {
  389. struct coresight_device *child = csdev->conns[i].child_dev;
  390. if (child && child->type == CORESIGHT_DEV_TYPE_HELPER)
  391. pm_runtime_put(child->dev.parent);
  392. }
  393. }
  394. /**
  395. * _coresight_build_path - recursively build a path from a @csdev to a sink.
  396. * @csdev: The device to start from.
  397. * @path: The list to add devices to.
  398. *
  399. * The tree of Coresight device is traversed until an activated sink is
  400. * found. From there the sink is added to the list along with all the
  401. * devices that led to that point - the end result is a list from source
  402. * to sink. In that list the source is the first device and the sink the
  403. * last one.
  404. */
  405. static int _coresight_build_path(struct coresight_device *csdev,
  406. struct coresight_device *sink,
  407. struct list_head *path)
  408. {
  409. int i;
  410. bool found = false;
  411. struct coresight_node *node;
  412. /* An activated sink has been found. Enqueue the element */
  413. if (csdev == sink)
  414. goto out;
  415. /* Not a sink - recursively explore each port found on this element */
  416. for (i = 0; i < csdev->nr_outport; i++) {
  417. struct coresight_device *child_dev = csdev->conns[i].child_dev;
  418. if (child_dev &&
  419. _coresight_build_path(child_dev, sink, path) == 0) {
  420. found = true;
  421. break;
  422. }
  423. }
  424. if (!found)
  425. return -ENODEV;
  426. out:
  427. /*
  428. * A path from this element to a sink has been found. The elements
  429. * leading to the sink are already enqueued, all that is left to do
  430. * is tell the PM runtime core we need this element and add a node
  431. * for it.
  432. */
  433. node = kzalloc(sizeof(struct coresight_node), GFP_KERNEL);
  434. if (!node)
  435. return -ENOMEM;
  436. coresight_grab_device(csdev);
  437. node->csdev = csdev;
  438. list_add(&node->link, path);
  439. return 0;
  440. }
  441. struct list_head *coresight_build_path(struct coresight_device *source,
  442. struct coresight_device *sink)
  443. {
  444. struct list_head *path;
  445. int rc;
  446. if (!sink)
  447. return ERR_PTR(-EINVAL);
  448. path = kzalloc(sizeof(struct list_head), GFP_KERNEL);
  449. if (!path)
  450. return ERR_PTR(-ENOMEM);
  451. INIT_LIST_HEAD(path);
  452. rc = _coresight_build_path(source, sink, path);
  453. if (rc) {
  454. kfree(path);
  455. return ERR_PTR(rc);
  456. }
  457. return path;
  458. }
  459. /**
  460. * coresight_release_path - release a previously built path.
  461. * @path: the path to release.
  462. *
  463. * Go through all the elements of a path and 1) removed it from the list and
  464. * 2) free the memory allocated for each node.
  465. */
  466. void coresight_release_path(struct list_head *path)
  467. {
  468. struct coresight_device *csdev;
  469. struct coresight_node *nd, *next;
  470. list_for_each_entry_safe(nd, next, path, link) {
  471. csdev = nd->csdev;
  472. coresight_drop_device(csdev);
  473. list_del(&nd->link);
  474. kfree(nd);
  475. }
  476. kfree(path);
  477. path = NULL;
  478. }
  479. /** coresight_validate_source - make sure a source has the right credentials
  480. * @csdev: the device structure for a source.
  481. * @function: the function this was called from.
  482. *
  483. * Assumes the coresight_mutex is held.
  484. */
  485. static int coresight_validate_source(struct coresight_device *csdev,
  486. const char *function)
  487. {
  488. u32 type, subtype;
  489. type = csdev->type;
  490. subtype = csdev->subtype.source_subtype;
  491. if (type != CORESIGHT_DEV_TYPE_SOURCE) {
  492. dev_err(&csdev->dev, "wrong device type in %s\n", function);
  493. return -EINVAL;
  494. }
  495. if (subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_PROC &&
  496. subtype != CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE) {
  497. dev_err(&csdev->dev, "wrong device subtype in %s\n", function);
  498. return -EINVAL;
  499. }
  500. return 0;
  501. }
  502. int coresight_enable(struct coresight_device *csdev)
  503. {
  504. int cpu, ret = 0;
  505. struct coresight_device *sink;
  506. struct list_head *path;
  507. enum coresight_dev_subtype_source subtype;
  508. subtype = csdev->subtype.source_subtype;
  509. mutex_lock(&coresight_mutex);
  510. ret = coresight_validate_source(csdev, __func__);
  511. if (ret)
  512. goto out;
  513. if (csdev->enable) {
  514. /*
  515. * There could be multiple applications driving the software
  516. * source. So keep the refcount for each such user when the
  517. * source is already enabled.
  518. */
  519. if (subtype == CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE)
  520. atomic_inc(csdev->refcnt);
  521. goto out;
  522. }
  523. /*
  524. * Search for a valid sink for this session but don't reset the
  525. * "enable_sink" flag in sysFS. Users get to do that explicitly.
  526. */
  527. sink = coresight_get_enabled_sink(false);
  528. if (!sink) {
  529. ret = -EINVAL;
  530. goto out;
  531. }
  532. path = coresight_build_path(csdev, sink);
  533. if (IS_ERR(path)) {
  534. pr_err("building path(s) failed\n");
  535. ret = PTR_ERR(path);
  536. goto out;
  537. }
  538. ret = coresight_enable_path(path, CS_MODE_SYSFS);
  539. if (ret)
  540. goto err_path;
  541. ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
  542. if (ret)
  543. goto err_source;
  544. switch (subtype) {
  545. case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
  546. /*
  547. * When working from sysFS it is important to keep track
  548. * of the paths that were created so that they can be
  549. * undone in 'coresight_disable()'. Since there can only
  550. * be a single session per tracer (when working from sysFS)
  551. * a per-cpu variable will do just fine.
  552. */
  553. cpu = source_ops(csdev)->cpu_id(csdev);
  554. per_cpu(tracer_path, cpu) = path;
  555. break;
  556. case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
  557. stm_path = path;
  558. break;
  559. default:
  560. /* We can't be here */
  561. break;
  562. }
  563. out:
  564. mutex_unlock(&coresight_mutex);
  565. return ret;
  566. err_source:
  567. coresight_disable_path(path);
  568. err_path:
  569. coresight_release_path(path);
  570. goto out;
  571. }
  572. EXPORT_SYMBOL_GPL(coresight_enable);
  573. void coresight_disable(struct coresight_device *csdev)
  574. {
  575. int cpu, ret;
  576. struct list_head *path = NULL;
  577. mutex_lock(&coresight_mutex);
  578. ret = coresight_validate_source(csdev, __func__);
  579. if (ret)
  580. goto out;
  581. if (!csdev->enable || !coresight_disable_source(csdev))
  582. goto out;
  583. switch (csdev->subtype.source_subtype) {
  584. case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
  585. cpu = source_ops(csdev)->cpu_id(csdev);
  586. path = per_cpu(tracer_path, cpu);
  587. per_cpu(tracer_path, cpu) = NULL;
  588. break;
  589. case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
  590. path = stm_path;
  591. stm_path = NULL;
  592. break;
  593. default:
  594. /* We can't be here */
  595. break;
  596. }
  597. coresight_disable_path(path);
  598. coresight_release_path(path);
  599. out:
  600. mutex_unlock(&coresight_mutex);
  601. }
  602. EXPORT_SYMBOL_GPL(coresight_disable);
  603. static ssize_t enable_sink_show(struct device *dev,
  604. struct device_attribute *attr, char *buf)
  605. {
  606. struct coresight_device *csdev = to_coresight_device(dev);
  607. return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
  608. }
  609. static ssize_t enable_sink_store(struct device *dev,
  610. struct device_attribute *attr,
  611. const char *buf, size_t size)
  612. {
  613. int ret;
  614. unsigned long val;
  615. struct coresight_device *csdev = to_coresight_device(dev);
  616. ret = kstrtoul(buf, 10, &val);
  617. if (ret)
  618. return ret;
  619. if (val)
  620. csdev->activated = true;
  621. else
  622. csdev->activated = false;
  623. return size;
  624. }
  625. static DEVICE_ATTR_RW(enable_sink);
  626. static ssize_t enable_source_show(struct device *dev,
  627. struct device_attribute *attr, char *buf)
  628. {
  629. struct coresight_device *csdev = to_coresight_device(dev);
  630. return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
  631. }
  632. static ssize_t enable_source_store(struct device *dev,
  633. struct device_attribute *attr,
  634. const char *buf, size_t size)
  635. {
  636. int ret = 0;
  637. unsigned long val;
  638. struct coresight_device *csdev = to_coresight_device(dev);
  639. ret = kstrtoul(buf, 10, &val);
  640. if (ret)
  641. return ret;
  642. if (val) {
  643. ret = coresight_enable(csdev);
  644. if (ret)
  645. return ret;
  646. } else {
  647. coresight_disable(csdev);
  648. }
  649. return size;
  650. }
  651. static DEVICE_ATTR_RW(enable_source);
  652. static struct attribute *coresight_sink_attrs[] = {
  653. &dev_attr_enable_sink.attr,
  654. NULL,
  655. };
  656. ATTRIBUTE_GROUPS(coresight_sink);
  657. static struct attribute *coresight_source_attrs[] = {
  658. &dev_attr_enable_source.attr,
  659. NULL,
  660. };
  661. ATTRIBUTE_GROUPS(coresight_source);
  662. static struct device_type coresight_dev_type[] = {
  663. {
  664. .name = "none",
  665. },
  666. {
  667. .name = "sink",
  668. .groups = coresight_sink_groups,
  669. },
  670. {
  671. .name = "link",
  672. },
  673. {
  674. .name = "linksink",
  675. .groups = coresight_sink_groups,
  676. },
  677. {
  678. .name = "source",
  679. .groups = coresight_source_groups,
  680. },
  681. {
  682. .name = "helper",
  683. },
  684. };
  685. static void coresight_device_release(struct device *dev)
  686. {
  687. struct coresight_device *csdev = to_coresight_device(dev);
  688. kfree(csdev->conns);
  689. kfree(csdev->refcnt);
  690. kfree(csdev);
  691. }
  692. static int coresight_orphan_match(struct device *dev, void *data)
  693. {
  694. int i;
  695. bool still_orphan = false;
  696. struct coresight_device *csdev, *i_csdev;
  697. struct coresight_connection *conn;
  698. csdev = data;
  699. i_csdev = to_coresight_device(dev);
  700. /* No need to check oneself */
  701. if (csdev == i_csdev)
  702. return 0;
  703. /* Move on to another component if no connection is orphan */
  704. if (!i_csdev->orphan)
  705. return 0;
  706. /*
  707. * Circle throuch all the connection of that component. If we find
  708. * an orphan connection whose name matches @csdev, link it.
  709. */
  710. for (i = 0; i < i_csdev->nr_outport; i++) {
  711. conn = &i_csdev->conns[i];
  712. /* We have found at least one orphan connection */
  713. if (conn->child_dev == NULL) {
  714. /* Does it match this newly added device? */
  715. if (conn->child_name &&
  716. !strcmp(dev_name(&csdev->dev), conn->child_name)) {
  717. conn->child_dev = csdev;
  718. } else {
  719. /* This component still has an orphan */
  720. still_orphan = true;
  721. }
  722. }
  723. }
  724. i_csdev->orphan = still_orphan;
  725. /*
  726. * Returning '0' ensures that all known component on the
  727. * bus will be checked.
  728. */
  729. return 0;
  730. }
  731. static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
  732. {
  733. /*
  734. * No need to check for a return value as orphan connection(s)
  735. * are hooked-up with each newly added component.
  736. */
  737. bus_for_each_dev(&coresight_bustype, NULL,
  738. csdev, coresight_orphan_match);
  739. }
  740. static void coresight_fixup_device_conns(struct coresight_device *csdev)
  741. {
  742. int i;
  743. for (i = 0; i < csdev->nr_outport; i++) {
  744. struct coresight_connection *conn = &csdev->conns[i];
  745. struct device *dev = NULL;
  746. if (conn->child_name)
  747. dev = bus_find_device_by_name(&coresight_bustype, NULL,
  748. conn->child_name);
  749. if (dev) {
  750. conn->child_dev = to_coresight_device(dev);
  751. /* and put reference from 'bus_find_device()' */
  752. put_device(dev);
  753. } else {
  754. csdev->orphan = true;
  755. conn->child_dev = NULL;
  756. }
  757. }
  758. }
  759. static int coresight_remove_match(struct device *dev, void *data)
  760. {
  761. int i;
  762. struct coresight_device *csdev, *iterator;
  763. struct coresight_connection *conn;
  764. csdev = data;
  765. iterator = to_coresight_device(dev);
  766. /* No need to check oneself */
  767. if (csdev == iterator)
  768. return 0;
  769. /*
  770. * Circle throuch all the connection of that component. If we find
  771. * a connection whose name matches @csdev, remove it.
  772. */
  773. for (i = 0; i < iterator->nr_outport; i++) {
  774. conn = &iterator->conns[i];
  775. if (conn->child_dev == NULL)
  776. continue;
  777. if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
  778. iterator->orphan = true;
  779. conn->child_dev = NULL;
  780. /* No need to continue */
  781. break;
  782. }
  783. }
  784. /*
  785. * Returning '0' ensures that all known component on the
  786. * bus will be checked.
  787. */
  788. return 0;
  789. }
  790. static void coresight_remove_conns(struct coresight_device *csdev)
  791. {
  792. bus_for_each_dev(&coresight_bustype, NULL,
  793. csdev, coresight_remove_match);
  794. }
  795. /**
  796. * coresight_timeout - loop until a bit has changed to a specific state.
  797. * @addr: base address of the area of interest.
  798. * @offset: address of a register, starting from @addr.
  799. * @position: the position of the bit of interest.
  800. * @value: the value the bit should have.
  801. *
  802. * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
  803. * TIMEOUT_US has elapsed, which ever happens first.
  804. */
  805. int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
  806. {
  807. int i;
  808. u32 val;
  809. for (i = TIMEOUT_US; i > 0; i--) {
  810. val = __raw_readl(addr + offset);
  811. /* waiting on the bit to go from 0 to 1 */
  812. if (value) {
  813. if (val & BIT(position))
  814. return 0;
  815. /* waiting on the bit to go from 1 to 0 */
  816. } else {
  817. if (!(val & BIT(position)))
  818. return 0;
  819. }
  820. /*
  821. * Delay is arbitrary - the specification doesn't say how long
  822. * we are expected to wait. Extra check required to make sure
  823. * we don't wait needlessly on the last iteration.
  824. */
  825. if (i - 1)
  826. udelay(1);
  827. }
  828. return -EAGAIN;
  829. }
  830. struct bus_type coresight_bustype = {
  831. .name = "coresight",
  832. };
  833. static int __init coresight_init(void)
  834. {
  835. return bus_register(&coresight_bustype);
  836. }
  837. postcore_initcall(coresight_init);
  838. struct coresight_device *coresight_register(struct coresight_desc *desc)
  839. {
  840. int i;
  841. int ret;
  842. int link_subtype;
  843. int nr_refcnts = 1;
  844. atomic_t *refcnts = NULL;
  845. struct coresight_device *csdev;
  846. struct coresight_connection *conns = NULL;
  847. csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
  848. if (!csdev) {
  849. ret = -ENOMEM;
  850. goto err_kzalloc_csdev;
  851. }
  852. if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
  853. desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
  854. link_subtype = desc->subtype.link_subtype;
  855. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
  856. nr_refcnts = desc->pdata->nr_inport;
  857. else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
  858. nr_refcnts = desc->pdata->nr_outport;
  859. }
  860. refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
  861. if (!refcnts) {
  862. ret = -ENOMEM;
  863. goto err_kzalloc_refcnts;
  864. }
  865. csdev->refcnt = refcnts;
  866. csdev->nr_inport = desc->pdata->nr_inport;
  867. csdev->nr_outport = desc->pdata->nr_outport;
  868. /* Initialise connections if there is at least one outport */
  869. if (csdev->nr_outport) {
  870. conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
  871. if (!conns) {
  872. ret = -ENOMEM;
  873. goto err_kzalloc_conns;
  874. }
  875. for (i = 0; i < csdev->nr_outport; i++) {
  876. conns[i].outport = desc->pdata->outports[i];
  877. conns[i].child_name = desc->pdata->child_names[i];
  878. conns[i].child_port = desc->pdata->child_ports[i];
  879. }
  880. }
  881. csdev->conns = conns;
  882. csdev->type = desc->type;
  883. csdev->subtype = desc->subtype;
  884. csdev->ops = desc->ops;
  885. csdev->orphan = false;
  886. csdev->dev.type = &coresight_dev_type[desc->type];
  887. csdev->dev.groups = desc->groups;
  888. csdev->dev.parent = desc->dev;
  889. csdev->dev.release = coresight_device_release;
  890. csdev->dev.bus = &coresight_bustype;
  891. dev_set_name(&csdev->dev, "%s", desc->pdata->name);
  892. ret = device_register(&csdev->dev);
  893. if (ret) {
  894. put_device(&csdev->dev);
  895. goto err_kzalloc_csdev;
  896. }
  897. mutex_lock(&coresight_mutex);
  898. coresight_fixup_device_conns(csdev);
  899. coresight_fixup_orphan_conns(csdev);
  900. mutex_unlock(&coresight_mutex);
  901. return csdev;
  902. err_kzalloc_conns:
  903. kfree(refcnts);
  904. err_kzalloc_refcnts:
  905. kfree(csdev);
  906. err_kzalloc_csdev:
  907. return ERR_PTR(ret);
  908. }
  909. EXPORT_SYMBOL_GPL(coresight_register);
  910. void coresight_unregister(struct coresight_device *csdev)
  911. {
  912. /* Remove references of that device in the topology */
  913. coresight_remove_conns(csdev);
  914. device_unregister(&csdev->dev);
  915. }
  916. EXPORT_SYMBOL_GPL(coresight_unregister);