coresight.c 23 KB

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