coresight.c 20 KB

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