coresight.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/device.h>
  16. #include <linux/io.h>
  17. #include <linux/err.h>
  18. #include <linux/export.h>
  19. #include <linux/slab.h>
  20. #include <linux/mutex.h>
  21. #include <linux/clk.h>
  22. #include <linux/coresight.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/delay.h>
  25. #include <linux/pm_runtime.h>
  26. #include "coresight-priv.h"
  27. static DEFINE_MUTEX(coresight_mutex);
  28. /**
  29. * struct coresight_node - elements of a path, from source to sink
  30. * @csdev: Address of an element.
  31. * @link: hook to the list.
  32. */
  33. struct coresight_node {
  34. struct coresight_device *csdev;
  35. struct list_head link;
  36. };
  37. /*
  38. * When operating Coresight drivers from the sysFS interface, only a single
  39. * path can exist from a tracer (associated to a CPU) to a sink.
  40. */
  41. static DEFINE_PER_CPU(struct list_head *, tracer_path);
  42. /*
  43. * As of this writing only a single STM can be found in CS topologies. Since
  44. * there is no way to know if we'll ever see more and what kind of
  45. * configuration they will enact, for the time being only define a single path
  46. * for STM.
  47. */
  48. static struct list_head *stm_path;
  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 int coresight_name_match(struct device *dev, void *data)
  699. {
  700. char *to_match;
  701. struct coresight_device *i_csdev;
  702. to_match = data;
  703. i_csdev = to_coresight_device(dev);
  704. if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
  705. return 1;
  706. return 0;
  707. }
  708. static void coresight_fixup_device_conns(struct coresight_device *csdev)
  709. {
  710. int i;
  711. struct device *dev = NULL;
  712. struct coresight_connection *conn;
  713. for (i = 0; i < csdev->nr_outport; i++) {
  714. conn = &csdev->conns[i];
  715. dev = bus_find_device(&coresight_bustype, NULL,
  716. (void *)conn->child_name,
  717. coresight_name_match);
  718. if (dev) {
  719. conn->child_dev = to_coresight_device(dev);
  720. /* and put reference from 'bus_find_device()' */
  721. put_device(dev);
  722. } else {
  723. csdev->orphan = true;
  724. conn->child_dev = NULL;
  725. }
  726. }
  727. }
  728. static int coresight_remove_match(struct device *dev, void *data)
  729. {
  730. int i;
  731. struct coresight_device *csdev, *iterator;
  732. struct coresight_connection *conn;
  733. csdev = data;
  734. iterator = to_coresight_device(dev);
  735. /* No need to check oneself */
  736. if (csdev == iterator)
  737. return 0;
  738. /*
  739. * Circle throuch all the connection of that component. If we find
  740. * a connection whose name matches @csdev, remove it.
  741. */
  742. for (i = 0; i < iterator->nr_outport; i++) {
  743. conn = &iterator->conns[i];
  744. if (conn->child_dev == NULL)
  745. continue;
  746. if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
  747. iterator->orphan = true;
  748. conn->child_dev = NULL;
  749. /* No need to continue */
  750. break;
  751. }
  752. }
  753. /*
  754. * Returning '0' ensures that all known component on the
  755. * bus will be checked.
  756. */
  757. return 0;
  758. }
  759. static void coresight_remove_conns(struct coresight_device *csdev)
  760. {
  761. bus_for_each_dev(&coresight_bustype, NULL,
  762. csdev, coresight_remove_match);
  763. }
  764. /**
  765. * coresight_timeout - loop until a bit has changed to a specific state.
  766. * @addr: base address of the area of interest.
  767. * @offset: address of a register, starting from @addr.
  768. * @position: the position of the bit of interest.
  769. * @value: the value the bit should have.
  770. *
  771. * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
  772. * TIMEOUT_US has elapsed, which ever happens first.
  773. */
  774. int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
  775. {
  776. int i;
  777. u32 val;
  778. for (i = TIMEOUT_US; i > 0; i--) {
  779. val = __raw_readl(addr + offset);
  780. /* waiting on the bit to go from 0 to 1 */
  781. if (value) {
  782. if (val & BIT(position))
  783. return 0;
  784. /* waiting on the bit to go from 1 to 0 */
  785. } else {
  786. if (!(val & BIT(position)))
  787. return 0;
  788. }
  789. /*
  790. * Delay is arbitrary - the specification doesn't say how long
  791. * we are expected to wait. Extra check required to make sure
  792. * we don't wait needlessly on the last iteration.
  793. */
  794. if (i - 1)
  795. udelay(1);
  796. }
  797. return -EAGAIN;
  798. }
  799. struct bus_type coresight_bustype = {
  800. .name = "coresight",
  801. };
  802. static int __init coresight_init(void)
  803. {
  804. return bus_register(&coresight_bustype);
  805. }
  806. postcore_initcall(coresight_init);
  807. struct coresight_device *coresight_register(struct coresight_desc *desc)
  808. {
  809. int i;
  810. int ret;
  811. int link_subtype;
  812. int nr_refcnts = 1;
  813. atomic_t *refcnts = NULL;
  814. struct coresight_device *csdev;
  815. struct coresight_connection *conns = NULL;
  816. csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
  817. if (!csdev) {
  818. ret = -ENOMEM;
  819. goto err_kzalloc_csdev;
  820. }
  821. if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
  822. desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
  823. link_subtype = desc->subtype.link_subtype;
  824. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
  825. nr_refcnts = desc->pdata->nr_inport;
  826. else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
  827. nr_refcnts = desc->pdata->nr_outport;
  828. }
  829. refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
  830. if (!refcnts) {
  831. ret = -ENOMEM;
  832. goto err_kzalloc_refcnts;
  833. }
  834. csdev->refcnt = refcnts;
  835. csdev->nr_inport = desc->pdata->nr_inport;
  836. csdev->nr_outport = desc->pdata->nr_outport;
  837. /* Initialise connections if there is at least one outport */
  838. if (csdev->nr_outport) {
  839. conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
  840. if (!conns) {
  841. ret = -ENOMEM;
  842. goto err_kzalloc_conns;
  843. }
  844. for (i = 0; i < csdev->nr_outport; i++) {
  845. conns[i].outport = desc->pdata->outports[i];
  846. conns[i].child_name = desc->pdata->child_names[i];
  847. conns[i].child_port = desc->pdata->child_ports[i];
  848. }
  849. }
  850. csdev->conns = conns;
  851. csdev->type = desc->type;
  852. csdev->subtype = desc->subtype;
  853. csdev->ops = desc->ops;
  854. csdev->orphan = false;
  855. csdev->dev.type = &coresight_dev_type[desc->type];
  856. csdev->dev.groups = desc->groups;
  857. csdev->dev.parent = desc->dev;
  858. csdev->dev.release = coresight_device_release;
  859. csdev->dev.bus = &coresight_bustype;
  860. dev_set_name(&csdev->dev, "%s", desc->pdata->name);
  861. ret = device_register(&csdev->dev);
  862. if (ret)
  863. goto err_device_register;
  864. mutex_lock(&coresight_mutex);
  865. coresight_fixup_device_conns(csdev);
  866. coresight_fixup_orphan_conns(csdev);
  867. mutex_unlock(&coresight_mutex);
  868. return csdev;
  869. err_device_register:
  870. kfree(conns);
  871. err_kzalloc_conns:
  872. kfree(refcnts);
  873. err_kzalloc_refcnts:
  874. kfree(csdev);
  875. err_kzalloc_csdev:
  876. return ERR_PTR(ret);
  877. }
  878. EXPORT_SYMBOL_GPL(coresight_register);
  879. void coresight_unregister(struct coresight_device *csdev)
  880. {
  881. /* Remove references of that device in the topology */
  882. coresight_remove_conns(csdev);
  883. device_unregister(&csdev->dev);
  884. }
  885. EXPORT_SYMBOL_GPL(coresight_unregister);