coresight.c 24 KB

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