coresight.c 24 KB

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