coresight.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  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);
  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 NULL;
  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. mutex_lock(&coresight_mutex);
  417. ret = coresight_validate_source(csdev, __func__);
  418. if (ret)
  419. goto out;
  420. if (csdev->enable)
  421. goto out;
  422. path = coresight_build_path(csdev);
  423. if (IS_ERR(path)) {
  424. pr_err("building path(s) failed\n");
  425. ret = PTR_ERR(path);
  426. goto out;
  427. }
  428. ret = coresight_enable_path(path, CS_MODE_SYSFS);
  429. if (ret)
  430. goto err_path;
  431. ret = coresight_enable_source(csdev, CS_MODE_SYSFS);
  432. if (ret)
  433. goto err_source;
  434. switch (csdev->subtype.source_subtype) {
  435. case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
  436. /*
  437. * When working from sysFS it is important to keep track
  438. * of the paths that were created so that they can be
  439. * undone in 'coresight_disable()'. Since there can only
  440. * be a single session per tracer (when working from sysFS)
  441. * a per-cpu variable will do just fine.
  442. */
  443. cpu = source_ops(csdev)->cpu_id(csdev);
  444. per_cpu(tracer_path, cpu) = path;
  445. break;
  446. case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
  447. stm_path = path;
  448. break;
  449. default:
  450. /* We can't be here */
  451. break;
  452. }
  453. out:
  454. mutex_unlock(&coresight_mutex);
  455. return ret;
  456. err_source:
  457. coresight_disable_path(path);
  458. err_path:
  459. coresight_release_path(path);
  460. goto out;
  461. }
  462. EXPORT_SYMBOL_GPL(coresight_enable);
  463. void coresight_disable(struct coresight_device *csdev)
  464. {
  465. int cpu, ret;
  466. struct list_head *path = NULL;
  467. mutex_lock(&coresight_mutex);
  468. ret = coresight_validate_source(csdev, __func__);
  469. if (ret)
  470. goto out;
  471. if (!csdev->enable)
  472. goto out;
  473. switch (csdev->subtype.source_subtype) {
  474. case CORESIGHT_DEV_SUBTYPE_SOURCE_PROC:
  475. cpu = source_ops(csdev)->cpu_id(csdev);
  476. path = per_cpu(tracer_path, cpu);
  477. per_cpu(tracer_path, cpu) = NULL;
  478. break;
  479. case CORESIGHT_DEV_SUBTYPE_SOURCE_SOFTWARE:
  480. path = stm_path;
  481. stm_path = NULL;
  482. break;
  483. default:
  484. /* We can't be here */
  485. break;
  486. }
  487. coresight_disable_source(csdev);
  488. coresight_disable_path(path);
  489. coresight_release_path(path);
  490. out:
  491. mutex_unlock(&coresight_mutex);
  492. }
  493. EXPORT_SYMBOL_GPL(coresight_disable);
  494. static ssize_t enable_sink_show(struct device *dev,
  495. struct device_attribute *attr, char *buf)
  496. {
  497. struct coresight_device *csdev = to_coresight_device(dev);
  498. return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->activated);
  499. }
  500. static ssize_t enable_sink_store(struct device *dev,
  501. struct device_attribute *attr,
  502. const char *buf, size_t size)
  503. {
  504. int ret;
  505. unsigned long val;
  506. struct coresight_device *csdev = to_coresight_device(dev);
  507. ret = kstrtoul(buf, 10, &val);
  508. if (ret)
  509. return ret;
  510. if (val)
  511. csdev->activated = true;
  512. else
  513. csdev->activated = false;
  514. return size;
  515. }
  516. static DEVICE_ATTR_RW(enable_sink);
  517. static ssize_t enable_source_show(struct device *dev,
  518. struct device_attribute *attr, char *buf)
  519. {
  520. struct coresight_device *csdev = to_coresight_device(dev);
  521. return scnprintf(buf, PAGE_SIZE, "%u\n", csdev->enable);
  522. }
  523. static ssize_t enable_source_store(struct device *dev,
  524. struct device_attribute *attr,
  525. const char *buf, size_t size)
  526. {
  527. int ret = 0;
  528. unsigned long val;
  529. struct coresight_device *csdev = to_coresight_device(dev);
  530. ret = kstrtoul(buf, 10, &val);
  531. if (ret)
  532. return ret;
  533. if (val) {
  534. ret = coresight_enable(csdev);
  535. if (ret)
  536. return ret;
  537. } else {
  538. coresight_disable(csdev);
  539. }
  540. return size;
  541. }
  542. static DEVICE_ATTR_RW(enable_source);
  543. static struct attribute *coresight_sink_attrs[] = {
  544. &dev_attr_enable_sink.attr,
  545. NULL,
  546. };
  547. ATTRIBUTE_GROUPS(coresight_sink);
  548. static struct attribute *coresight_source_attrs[] = {
  549. &dev_attr_enable_source.attr,
  550. NULL,
  551. };
  552. ATTRIBUTE_GROUPS(coresight_source);
  553. static struct device_type coresight_dev_type[] = {
  554. {
  555. .name = "none",
  556. },
  557. {
  558. .name = "sink",
  559. .groups = coresight_sink_groups,
  560. },
  561. {
  562. .name = "link",
  563. },
  564. {
  565. .name = "linksink",
  566. .groups = coresight_sink_groups,
  567. },
  568. {
  569. .name = "source",
  570. .groups = coresight_source_groups,
  571. },
  572. };
  573. static void coresight_device_release(struct device *dev)
  574. {
  575. struct coresight_device *csdev = to_coresight_device(dev);
  576. kfree(csdev->conns);
  577. kfree(csdev->refcnt);
  578. kfree(csdev);
  579. }
  580. static int coresight_orphan_match(struct device *dev, void *data)
  581. {
  582. int i;
  583. bool still_orphan = false;
  584. struct coresight_device *csdev, *i_csdev;
  585. struct coresight_connection *conn;
  586. csdev = data;
  587. i_csdev = to_coresight_device(dev);
  588. /* No need to check oneself */
  589. if (csdev == i_csdev)
  590. return 0;
  591. /* Move on to another component if no connection is orphan */
  592. if (!i_csdev->orphan)
  593. return 0;
  594. /*
  595. * Circle throuch all the connection of that component. If we find
  596. * an orphan connection whose name matches @csdev, link it.
  597. */
  598. for (i = 0; i < i_csdev->nr_outport; i++) {
  599. conn = &i_csdev->conns[i];
  600. /* We have found at least one orphan connection */
  601. if (conn->child_dev == NULL) {
  602. /* Does it match this newly added device? */
  603. if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
  604. conn->child_dev = csdev;
  605. } else {
  606. /* This component still has an orphan */
  607. still_orphan = true;
  608. }
  609. }
  610. }
  611. i_csdev->orphan = still_orphan;
  612. /*
  613. * Returning '0' ensures that all known component on the
  614. * bus will be checked.
  615. */
  616. return 0;
  617. }
  618. static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
  619. {
  620. /*
  621. * No need to check for a return value as orphan connection(s)
  622. * are hooked-up with each newly added component.
  623. */
  624. bus_for_each_dev(&coresight_bustype, NULL,
  625. csdev, coresight_orphan_match);
  626. }
  627. static int coresight_name_match(struct device *dev, void *data)
  628. {
  629. char *to_match;
  630. struct coresight_device *i_csdev;
  631. to_match = data;
  632. i_csdev = to_coresight_device(dev);
  633. if (to_match && !strcmp(to_match, dev_name(&i_csdev->dev)))
  634. return 1;
  635. return 0;
  636. }
  637. static void coresight_fixup_device_conns(struct coresight_device *csdev)
  638. {
  639. int i;
  640. struct device *dev = NULL;
  641. struct coresight_connection *conn;
  642. for (i = 0; i < csdev->nr_outport; i++) {
  643. conn = &csdev->conns[i];
  644. dev = bus_find_device(&coresight_bustype, NULL,
  645. (void *)conn->child_name,
  646. coresight_name_match);
  647. if (dev) {
  648. conn->child_dev = to_coresight_device(dev);
  649. /* and put reference from 'bus_find_device()' */
  650. put_device(dev);
  651. } else {
  652. csdev->orphan = true;
  653. conn->child_dev = NULL;
  654. }
  655. }
  656. }
  657. static int coresight_remove_match(struct device *dev, void *data)
  658. {
  659. int i;
  660. struct coresight_device *csdev, *iterator;
  661. struct coresight_connection *conn;
  662. csdev = data;
  663. iterator = to_coresight_device(dev);
  664. /* No need to check oneself */
  665. if (csdev == iterator)
  666. return 0;
  667. /*
  668. * Circle throuch all the connection of that component. If we find
  669. * a connection whose name matches @csdev, remove it.
  670. */
  671. for (i = 0; i < iterator->nr_outport; i++) {
  672. conn = &iterator->conns[i];
  673. if (conn->child_dev == NULL)
  674. continue;
  675. if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
  676. iterator->orphan = true;
  677. conn->child_dev = NULL;
  678. /* No need to continue */
  679. break;
  680. }
  681. }
  682. /*
  683. * Returning '0' ensures that all known component on the
  684. * bus will be checked.
  685. */
  686. return 0;
  687. }
  688. static void coresight_remove_conns(struct coresight_device *csdev)
  689. {
  690. bus_for_each_dev(&coresight_bustype, NULL,
  691. csdev, coresight_remove_match);
  692. }
  693. /**
  694. * coresight_timeout - loop until a bit has changed to a specific state.
  695. * @addr: base address of the area of interest.
  696. * @offset: address of a register, starting from @addr.
  697. * @position: the position of the bit of interest.
  698. * @value: the value the bit should have.
  699. *
  700. * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
  701. * TIMEOUT_US has elapsed, which ever happens first.
  702. */
  703. int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
  704. {
  705. int i;
  706. u32 val;
  707. for (i = TIMEOUT_US; i > 0; i--) {
  708. val = __raw_readl(addr + offset);
  709. /* waiting on the bit to go from 0 to 1 */
  710. if (value) {
  711. if (val & BIT(position))
  712. return 0;
  713. /* waiting on the bit to go from 1 to 0 */
  714. } else {
  715. if (!(val & BIT(position)))
  716. return 0;
  717. }
  718. /*
  719. * Delay is arbitrary - the specification doesn't say how long
  720. * we are expected to wait. Extra check required to make sure
  721. * we don't wait needlessly on the last iteration.
  722. */
  723. if (i - 1)
  724. udelay(1);
  725. }
  726. return -EAGAIN;
  727. }
  728. struct bus_type coresight_bustype = {
  729. .name = "coresight",
  730. };
  731. static int __init coresight_init(void)
  732. {
  733. return bus_register(&coresight_bustype);
  734. }
  735. postcore_initcall(coresight_init);
  736. struct coresight_device *coresight_register(struct coresight_desc *desc)
  737. {
  738. int i;
  739. int ret;
  740. int link_subtype;
  741. int nr_refcnts = 1;
  742. atomic_t *refcnts = NULL;
  743. struct coresight_device *csdev;
  744. struct coresight_connection *conns;
  745. csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
  746. if (!csdev) {
  747. ret = -ENOMEM;
  748. goto err_kzalloc_csdev;
  749. }
  750. if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
  751. desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
  752. link_subtype = desc->subtype.link_subtype;
  753. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
  754. nr_refcnts = desc->pdata->nr_inport;
  755. else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
  756. nr_refcnts = desc->pdata->nr_outport;
  757. }
  758. refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
  759. if (!refcnts) {
  760. ret = -ENOMEM;
  761. goto err_kzalloc_refcnts;
  762. }
  763. csdev->refcnt = refcnts;
  764. csdev->nr_inport = desc->pdata->nr_inport;
  765. csdev->nr_outport = desc->pdata->nr_outport;
  766. conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
  767. if (!conns) {
  768. ret = -ENOMEM;
  769. goto err_kzalloc_conns;
  770. }
  771. for (i = 0; i < csdev->nr_outport; i++) {
  772. conns[i].outport = desc->pdata->outports[i];
  773. conns[i].child_name = desc->pdata->child_names[i];
  774. conns[i].child_port = desc->pdata->child_ports[i];
  775. }
  776. csdev->conns = conns;
  777. csdev->type = desc->type;
  778. csdev->subtype = desc->subtype;
  779. csdev->ops = desc->ops;
  780. csdev->orphan = false;
  781. csdev->dev.type = &coresight_dev_type[desc->type];
  782. csdev->dev.groups = desc->groups;
  783. csdev->dev.parent = desc->dev;
  784. csdev->dev.release = coresight_device_release;
  785. csdev->dev.bus = &coresight_bustype;
  786. dev_set_name(&csdev->dev, "%s", desc->pdata->name);
  787. ret = device_register(&csdev->dev);
  788. if (ret)
  789. goto err_device_register;
  790. mutex_lock(&coresight_mutex);
  791. coresight_fixup_device_conns(csdev);
  792. coresight_fixup_orphan_conns(csdev);
  793. mutex_unlock(&coresight_mutex);
  794. return csdev;
  795. err_device_register:
  796. kfree(conns);
  797. err_kzalloc_conns:
  798. kfree(refcnts);
  799. err_kzalloc_refcnts:
  800. kfree(csdev);
  801. err_kzalloc_csdev:
  802. return ERR_PTR(ret);
  803. }
  804. EXPORT_SYMBOL_GPL(coresight_register);
  805. void coresight_unregister(struct coresight_device *csdev)
  806. {
  807. /* Remove references of that device in the topology */
  808. coresight_remove_conns(csdev);
  809. device_unregister(&csdev->dev);
  810. }
  811. EXPORT_SYMBOL_GPL(coresight_unregister);