coresight.c 24 KB

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