coresight.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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/module.h>
  14. #include <linux/init.h>
  15. #include <linux/types.h>
  16. #include <linux/device.h>
  17. #include <linux/io.h>
  18. #include <linux/err.h>
  19. #include <linux/export.h>
  20. #include <linux/slab.h>
  21. #include <linux/mutex.h>
  22. #include <linux/clk.h>
  23. #include <linux/coresight.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/delay.h>
  26. #include "coresight-priv.h"
  27. static DEFINE_MUTEX(coresight_mutex);
  28. static int coresight_id_match(struct device *dev, void *data)
  29. {
  30. int trace_id, i_trace_id;
  31. struct coresight_device *csdev, *i_csdev;
  32. csdev = data;
  33. i_csdev = to_coresight_device(dev);
  34. /*
  35. * No need to care about oneself and components that are not
  36. * sources or not enabled
  37. */
  38. if (i_csdev == csdev || !i_csdev->enable ||
  39. i_csdev->type != CORESIGHT_DEV_TYPE_SOURCE)
  40. return 0;
  41. /* Get the source ID for both compoment */
  42. trace_id = source_ops(csdev)->trace_id(csdev);
  43. i_trace_id = source_ops(i_csdev)->trace_id(i_csdev);
  44. /* All you need is one */
  45. if (trace_id == i_trace_id)
  46. return 1;
  47. return 0;
  48. }
  49. static int coresight_source_is_unique(struct coresight_device *csdev)
  50. {
  51. int trace_id = source_ops(csdev)->trace_id(csdev);
  52. /* this shouldn't happen */
  53. if (trace_id < 0)
  54. return 0;
  55. return !bus_for_each_dev(&coresight_bustype, NULL,
  56. csdev, coresight_id_match);
  57. }
  58. static int coresight_find_link_inport(struct coresight_device *csdev)
  59. {
  60. int i;
  61. struct coresight_device *parent;
  62. struct coresight_connection *conn;
  63. parent = container_of(csdev->path_link.next,
  64. struct coresight_device, path_link);
  65. for (i = 0; i < parent->nr_outport; i++) {
  66. conn = &parent->conns[i];
  67. if (conn->child_dev == csdev)
  68. return conn->child_port;
  69. }
  70. dev_err(&csdev->dev, "couldn't find inport, parent: %s, child: %s\n",
  71. dev_name(&parent->dev), dev_name(&csdev->dev));
  72. return 0;
  73. }
  74. static int coresight_find_link_outport(struct coresight_device *csdev)
  75. {
  76. int i;
  77. struct coresight_device *child;
  78. struct coresight_connection *conn;
  79. child = container_of(csdev->path_link.prev,
  80. struct coresight_device, path_link);
  81. for (i = 0; i < csdev->nr_outport; i++) {
  82. conn = &csdev->conns[i];
  83. if (conn->child_dev == child)
  84. return conn->outport;
  85. }
  86. dev_err(&csdev->dev, "couldn't find outport, parent: %s, child: %s\n",
  87. dev_name(&csdev->dev), dev_name(&child->dev));
  88. return 0;
  89. }
  90. static int coresight_enable_sink(struct coresight_device *csdev)
  91. {
  92. int ret;
  93. if (!csdev->enable) {
  94. if (sink_ops(csdev)->enable) {
  95. ret = sink_ops(csdev)->enable(csdev);
  96. if (ret)
  97. return ret;
  98. }
  99. csdev->enable = true;
  100. }
  101. atomic_inc(csdev->refcnt);
  102. return 0;
  103. }
  104. static void coresight_disable_sink(struct coresight_device *csdev)
  105. {
  106. if (atomic_dec_return(csdev->refcnt) == 0) {
  107. if (sink_ops(csdev)->disable) {
  108. sink_ops(csdev)->disable(csdev);
  109. csdev->enable = false;
  110. }
  111. }
  112. }
  113. static int coresight_enable_link(struct coresight_device *csdev)
  114. {
  115. int ret;
  116. int link_subtype;
  117. int refport, inport, outport;
  118. inport = coresight_find_link_inport(csdev);
  119. outport = coresight_find_link_outport(csdev);
  120. link_subtype = csdev->subtype.link_subtype;
  121. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
  122. refport = inport;
  123. else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
  124. refport = outport;
  125. else
  126. refport = 0;
  127. if (atomic_inc_return(&csdev->refcnt[refport]) == 1) {
  128. if (link_ops(csdev)->enable) {
  129. ret = link_ops(csdev)->enable(csdev, inport, outport);
  130. if (ret)
  131. return ret;
  132. }
  133. }
  134. csdev->enable = true;
  135. return 0;
  136. }
  137. static void coresight_disable_link(struct coresight_device *csdev)
  138. {
  139. int i, nr_conns;
  140. int link_subtype;
  141. int refport, inport, outport;
  142. inport = coresight_find_link_inport(csdev);
  143. outport = coresight_find_link_outport(csdev);
  144. link_subtype = csdev->subtype.link_subtype;
  145. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
  146. refport = inport;
  147. nr_conns = csdev->nr_inport;
  148. } else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT) {
  149. refport = outport;
  150. nr_conns = csdev->nr_outport;
  151. } else {
  152. refport = 0;
  153. nr_conns = 1;
  154. }
  155. if (atomic_dec_return(&csdev->refcnt[refport]) == 0) {
  156. if (link_ops(csdev)->disable)
  157. link_ops(csdev)->disable(csdev, inport, outport);
  158. }
  159. for (i = 0; i < nr_conns; i++)
  160. if (atomic_read(&csdev->refcnt[i]) != 0)
  161. return;
  162. csdev->enable = false;
  163. }
  164. static int coresight_enable_source(struct coresight_device *csdev)
  165. {
  166. int ret;
  167. if (!coresight_source_is_unique(csdev)) {
  168. dev_warn(&csdev->dev, "traceID %d not unique\n",
  169. source_ops(csdev)->trace_id(csdev));
  170. return -EINVAL;
  171. }
  172. if (!csdev->enable) {
  173. if (source_ops(csdev)->enable) {
  174. ret = source_ops(csdev)->enable(csdev);
  175. if (ret)
  176. return ret;
  177. }
  178. csdev->enable = true;
  179. }
  180. atomic_inc(csdev->refcnt);
  181. return 0;
  182. }
  183. static void coresight_disable_source(struct coresight_device *csdev)
  184. {
  185. if (atomic_dec_return(csdev->refcnt) == 0) {
  186. if (source_ops(csdev)->disable) {
  187. source_ops(csdev)->disable(csdev);
  188. csdev->enable = false;
  189. }
  190. }
  191. }
  192. static int coresight_enable_path(struct list_head *path)
  193. {
  194. int ret = 0;
  195. struct coresight_device *cd;
  196. /*
  197. * At this point we have a full @path, from source to sink. The
  198. * sink is the first entry and the source the last one. Go through
  199. * all the components and enable them one by one.
  200. */
  201. list_for_each_entry(cd, path, path_link) {
  202. if (cd == list_first_entry(path, struct coresight_device,
  203. path_link)) {
  204. ret = coresight_enable_sink(cd);
  205. } else if (list_is_last(&cd->path_link, path)) {
  206. /*
  207. * Don't enable the source just yet - this needs to
  208. * happen at the very end when all links and sink
  209. * along the path have been configured properly.
  210. */
  211. ;
  212. } else {
  213. ret = coresight_enable_link(cd);
  214. }
  215. if (ret)
  216. goto err;
  217. }
  218. return 0;
  219. err:
  220. list_for_each_entry_continue_reverse(cd, path, path_link) {
  221. if (cd == list_first_entry(path, struct coresight_device,
  222. path_link)) {
  223. coresight_disable_sink(cd);
  224. } else if (list_is_last(&cd->path_link, path)) {
  225. ;
  226. } else {
  227. coresight_disable_link(cd);
  228. }
  229. }
  230. return ret;
  231. }
  232. static int coresight_disable_path(struct list_head *path)
  233. {
  234. struct coresight_device *cd;
  235. list_for_each_entry_reverse(cd, path, path_link) {
  236. if (cd == list_first_entry(path, struct coresight_device,
  237. path_link)) {
  238. coresight_disable_sink(cd);
  239. } else if (list_is_last(&cd->path_link, path)) {
  240. /*
  241. * The source has already been stopped, no need
  242. * to do it again here.
  243. */
  244. ;
  245. } else {
  246. coresight_disable_link(cd);
  247. }
  248. }
  249. return 0;
  250. }
  251. static int coresight_build_paths(struct coresight_device *csdev,
  252. struct list_head *path,
  253. bool enable)
  254. {
  255. int i, ret = -EINVAL;
  256. struct coresight_connection *conn;
  257. list_add(&csdev->path_link, path);
  258. if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
  259. csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
  260. csdev->activated) {
  261. if (enable)
  262. ret = coresight_enable_path(path);
  263. else
  264. ret = coresight_disable_path(path);
  265. } else {
  266. for (i = 0; i < csdev->nr_outport; i++) {
  267. conn = &csdev->conns[i];
  268. if (coresight_build_paths(conn->child_dev,
  269. path, enable) == 0)
  270. ret = 0;
  271. }
  272. }
  273. if (list_first_entry(path, struct coresight_device, path_link) != csdev)
  274. dev_err(&csdev->dev, "wrong device in %s\n", __func__);
  275. list_del(&csdev->path_link);
  276. return ret;
  277. }
  278. int coresight_enable(struct coresight_device *csdev)
  279. {
  280. int ret = 0;
  281. LIST_HEAD(path);
  282. mutex_lock(&coresight_mutex);
  283. if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
  284. ret = -EINVAL;
  285. dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
  286. goto out;
  287. }
  288. if (csdev->enable)
  289. goto out;
  290. if (coresight_build_paths(csdev, &path, true)) {
  291. dev_err(&csdev->dev, "building path(s) failed\n");
  292. goto out;
  293. }
  294. if (coresight_enable_source(csdev))
  295. dev_err(&csdev->dev, "source enable failed\n");
  296. out:
  297. mutex_unlock(&coresight_mutex);
  298. return ret;
  299. }
  300. EXPORT_SYMBOL_GPL(coresight_enable);
  301. void coresight_disable(struct coresight_device *csdev)
  302. {
  303. LIST_HEAD(path);
  304. mutex_lock(&coresight_mutex);
  305. if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
  306. dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
  307. goto out;
  308. }
  309. if (!csdev->enable)
  310. goto out;
  311. coresight_disable_source(csdev);
  312. if (coresight_build_paths(csdev, &path, false))
  313. dev_err(&csdev->dev, "releasing path(s) failed\n");
  314. out:
  315. mutex_unlock(&coresight_mutex);
  316. }
  317. EXPORT_SYMBOL_GPL(coresight_disable);
  318. static ssize_t enable_sink_show(struct device *dev,
  319. struct device_attribute *attr, char *buf)
  320. {
  321. struct coresight_device *csdev = to_coresight_device(dev);
  322. return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
  323. }
  324. static ssize_t enable_sink_store(struct device *dev,
  325. struct device_attribute *attr,
  326. const char *buf, size_t size)
  327. {
  328. int ret;
  329. unsigned long val;
  330. struct coresight_device *csdev = to_coresight_device(dev);
  331. ret = kstrtoul(buf, 10, &val);
  332. if (ret)
  333. return ret;
  334. if (val)
  335. csdev->activated = true;
  336. else
  337. csdev->activated = false;
  338. return size;
  339. }
  340. static DEVICE_ATTR_RW(enable_sink);
  341. static ssize_t enable_source_show(struct device *dev,
  342. struct device_attribute *attr, char *buf)
  343. {
  344. struct coresight_device *csdev = to_coresight_device(dev);
  345. return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
  346. }
  347. static ssize_t enable_source_store(struct device *dev,
  348. struct device_attribute *attr,
  349. const char *buf, size_t size)
  350. {
  351. int ret = 0;
  352. unsigned long val;
  353. struct coresight_device *csdev = to_coresight_device(dev);
  354. ret = kstrtoul(buf, 10, &val);
  355. if (ret)
  356. return ret;
  357. if (val) {
  358. ret = coresight_enable(csdev);
  359. if (ret)
  360. return ret;
  361. } else {
  362. coresight_disable(csdev);
  363. }
  364. return size;
  365. }
  366. static DEVICE_ATTR_RW(enable_source);
  367. static struct attribute *coresight_sink_attrs[] = {
  368. &dev_attr_enable_sink.attr,
  369. NULL,
  370. };
  371. ATTRIBUTE_GROUPS(coresight_sink);
  372. static struct attribute *coresight_source_attrs[] = {
  373. &dev_attr_enable_source.attr,
  374. NULL,
  375. };
  376. ATTRIBUTE_GROUPS(coresight_source);
  377. static struct device_type coresight_dev_type[] = {
  378. {
  379. .name = "none",
  380. },
  381. {
  382. .name = "sink",
  383. .groups = coresight_sink_groups,
  384. },
  385. {
  386. .name = "link",
  387. },
  388. {
  389. .name = "linksink",
  390. .groups = coresight_sink_groups,
  391. },
  392. {
  393. .name = "source",
  394. .groups = coresight_source_groups,
  395. },
  396. };
  397. static void coresight_device_release(struct device *dev)
  398. {
  399. struct coresight_device *csdev = to_coresight_device(dev);
  400. kfree(csdev);
  401. }
  402. static int coresight_orphan_match(struct device *dev, void *data)
  403. {
  404. int i;
  405. bool still_orphan = false;
  406. struct coresight_device *csdev, *i_csdev;
  407. struct coresight_connection *conn;
  408. csdev = data;
  409. i_csdev = to_coresight_device(dev);
  410. /* No need to check oneself */
  411. if (csdev == i_csdev)
  412. return 0;
  413. /* Move on to another component if no connection is orphan */
  414. if (!i_csdev->orphan)
  415. return 0;
  416. /*
  417. * Circle throuch all the connection of that component. If we find
  418. * an orphan connection whose name matches @csdev, link it.
  419. */
  420. for (i = 0; i < i_csdev->nr_outport; i++) {
  421. conn = &i_csdev->conns[i];
  422. /* We have found at least one orphan connection */
  423. if (conn->child_dev == NULL) {
  424. /* Does it match this newly added device? */
  425. if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
  426. conn->child_dev = csdev;
  427. } else {
  428. /* This component still has an orphan */
  429. still_orphan = true;
  430. }
  431. }
  432. }
  433. i_csdev->orphan = still_orphan;
  434. /*
  435. * Returning '0' ensures that all known component on the
  436. * bus will be checked.
  437. */
  438. return 0;
  439. }
  440. static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
  441. {
  442. /*
  443. * No need to check for a return value as orphan connection(s)
  444. * are hooked-up with each newly added component.
  445. */
  446. bus_for_each_dev(&coresight_bustype, NULL,
  447. csdev, coresight_orphan_match);
  448. }
  449. static int coresight_name_match(struct device *dev, void *data)
  450. {
  451. char *to_match;
  452. struct coresight_device *i_csdev;
  453. to_match = data;
  454. i_csdev = to_coresight_device(dev);
  455. if (!strcmp(to_match, dev_name(&i_csdev->dev)))
  456. return 1;
  457. return 0;
  458. }
  459. static void coresight_fixup_device_conns(struct coresight_device *csdev)
  460. {
  461. int i;
  462. struct device *dev = NULL;
  463. struct coresight_connection *conn;
  464. for (i = 0; i < csdev->nr_outport; i++) {
  465. conn = &csdev->conns[i];
  466. dev = bus_find_device(&coresight_bustype, NULL,
  467. (void *)conn->child_name,
  468. coresight_name_match);
  469. if (dev) {
  470. conn->child_dev = to_coresight_device(dev);
  471. } else {
  472. csdev->orphan = true;
  473. conn->child_dev = NULL;
  474. }
  475. }
  476. }
  477. /**
  478. * coresight_timeout - loop until a bit has changed to a specific state.
  479. * @addr: base address of the area of interest.
  480. * @offset: address of a register, starting from @addr.
  481. * @position: the position of the bit of interest.
  482. * @value: the value the bit should have.
  483. *
  484. * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
  485. * TIMEOUT_US has elapsed, which ever happens first.
  486. */
  487. int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
  488. {
  489. int i;
  490. u32 val;
  491. for (i = TIMEOUT_US; i > 0; i--) {
  492. val = __raw_readl(addr + offset);
  493. /* waiting on the bit to go from 0 to 1 */
  494. if (value) {
  495. if (val & BIT(position))
  496. return 0;
  497. /* waiting on the bit to go from 1 to 0 */
  498. } else {
  499. if (!(val & BIT(position)))
  500. return 0;
  501. }
  502. /*
  503. * Delay is arbitrary - the specification doesn't say how long
  504. * we are expected to wait. Extra check required to make sure
  505. * we don't wait needlessly on the last iteration.
  506. */
  507. if (i - 1)
  508. udelay(1);
  509. }
  510. return -EAGAIN;
  511. }
  512. struct bus_type coresight_bustype = {
  513. .name = "coresight",
  514. };
  515. static int __init coresight_init(void)
  516. {
  517. return bus_register(&coresight_bustype);
  518. }
  519. postcore_initcall(coresight_init);
  520. struct coresight_device *coresight_register(struct coresight_desc *desc)
  521. {
  522. int i;
  523. int ret;
  524. int link_subtype;
  525. int nr_refcnts = 1;
  526. atomic_t *refcnts = NULL;
  527. struct coresight_device *csdev;
  528. struct coresight_connection *conns;
  529. csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
  530. if (!csdev) {
  531. ret = -ENOMEM;
  532. goto err_kzalloc_csdev;
  533. }
  534. if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
  535. desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
  536. link_subtype = desc->subtype.link_subtype;
  537. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
  538. nr_refcnts = desc->pdata->nr_inport;
  539. else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
  540. nr_refcnts = desc->pdata->nr_outport;
  541. }
  542. refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
  543. if (!refcnts) {
  544. ret = -ENOMEM;
  545. goto err_kzalloc_refcnts;
  546. }
  547. csdev->refcnt = refcnts;
  548. csdev->nr_inport = desc->pdata->nr_inport;
  549. csdev->nr_outport = desc->pdata->nr_outport;
  550. conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
  551. if (!conns) {
  552. ret = -ENOMEM;
  553. goto err_kzalloc_conns;
  554. }
  555. for (i = 0; i < csdev->nr_outport; i++) {
  556. conns[i].outport = desc->pdata->outports[i];
  557. conns[i].child_name = desc->pdata->child_names[i];
  558. conns[i].child_port = desc->pdata->child_ports[i];
  559. }
  560. csdev->conns = conns;
  561. csdev->type = desc->type;
  562. csdev->subtype = desc->subtype;
  563. csdev->ops = desc->ops;
  564. csdev->orphan = false;
  565. csdev->dev.type = &coresight_dev_type[desc->type];
  566. csdev->dev.groups = desc->groups;
  567. csdev->dev.parent = desc->dev;
  568. csdev->dev.release = coresight_device_release;
  569. csdev->dev.bus = &coresight_bustype;
  570. dev_set_name(&csdev->dev, "%s", desc->pdata->name);
  571. ret = device_register(&csdev->dev);
  572. if (ret)
  573. goto err_device_register;
  574. mutex_lock(&coresight_mutex);
  575. coresight_fixup_device_conns(csdev);
  576. coresight_fixup_orphan_conns(csdev);
  577. mutex_unlock(&coresight_mutex);
  578. return csdev;
  579. err_device_register:
  580. kfree(conns);
  581. err_kzalloc_conns:
  582. kfree(refcnts);
  583. err_kzalloc_refcnts:
  584. kfree(csdev);
  585. err_kzalloc_csdev:
  586. return ERR_PTR(ret);
  587. }
  588. EXPORT_SYMBOL_GPL(coresight_register);
  589. void coresight_unregister(struct coresight_device *csdev)
  590. {
  591. mutex_lock(&coresight_mutex);
  592. kfree(csdev->conns);
  593. device_unregister(&csdev->dev);
  594. mutex_unlock(&coresight_mutex);
  595. }
  596. EXPORT_SYMBOL_GPL(coresight_unregister);
  597. MODULE_LICENSE("GPL v2");