coresight.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. list_for_each_entry(cd, path, path_link) {
  197. if (cd == list_first_entry(path, struct coresight_device,
  198. path_link)) {
  199. ret = coresight_enable_sink(cd);
  200. } else if (list_is_last(&cd->path_link, path)) {
  201. /*
  202. * Don't enable the source just yet - this needs to
  203. * happen at the very end when all links and sink
  204. * along the path have been configured properly.
  205. */
  206. ;
  207. } else {
  208. ret = coresight_enable_link(cd);
  209. }
  210. if (ret)
  211. goto err;
  212. }
  213. return 0;
  214. err:
  215. list_for_each_entry_continue_reverse(cd, path, path_link) {
  216. if (cd == list_first_entry(path, struct coresight_device,
  217. path_link)) {
  218. coresight_disable_sink(cd);
  219. } else if (list_is_last(&cd->path_link, path)) {
  220. ;
  221. } else {
  222. coresight_disable_link(cd);
  223. }
  224. }
  225. return ret;
  226. }
  227. static int coresight_disable_path(struct list_head *path)
  228. {
  229. struct coresight_device *cd;
  230. list_for_each_entry_reverse(cd, path, path_link) {
  231. if (cd == list_first_entry(path, struct coresight_device,
  232. path_link)) {
  233. coresight_disable_sink(cd);
  234. } else if (list_is_last(&cd->path_link, path)) {
  235. /*
  236. * The source has already been stopped, no need
  237. * to do it again here.
  238. */
  239. ;
  240. } else {
  241. coresight_disable_link(cd);
  242. }
  243. }
  244. return 0;
  245. }
  246. static int coresight_build_paths(struct coresight_device *csdev,
  247. struct list_head *path,
  248. bool enable)
  249. {
  250. int i, ret = -EINVAL;
  251. struct coresight_connection *conn;
  252. list_add(&csdev->path_link, path);
  253. if ((csdev->type == CORESIGHT_DEV_TYPE_SINK ||
  254. csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) &&
  255. csdev->activated) {
  256. if (enable)
  257. ret = coresight_enable_path(path);
  258. else
  259. ret = coresight_disable_path(path);
  260. } else {
  261. for (i = 0; i < csdev->nr_outport; i++) {
  262. conn = &csdev->conns[i];
  263. if (coresight_build_paths(conn->child_dev,
  264. path, enable) == 0)
  265. ret = 0;
  266. }
  267. }
  268. if (list_first_entry(path, struct coresight_device, path_link) != csdev)
  269. dev_err(&csdev->dev, "wrong device in %s\n", __func__);
  270. list_del(&csdev->path_link);
  271. return ret;
  272. }
  273. int coresight_enable(struct coresight_device *csdev)
  274. {
  275. int ret = 0;
  276. LIST_HEAD(path);
  277. mutex_lock(&coresight_mutex);
  278. if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
  279. ret = -EINVAL;
  280. dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
  281. goto out;
  282. }
  283. if (csdev->enable)
  284. goto out;
  285. if (coresight_build_paths(csdev, &path, true)) {
  286. dev_err(&csdev->dev, "building path(s) failed\n");
  287. goto out;
  288. }
  289. if (coresight_enable_source(csdev))
  290. dev_err(&csdev->dev, "source enable failed\n");
  291. out:
  292. mutex_unlock(&coresight_mutex);
  293. return ret;
  294. }
  295. EXPORT_SYMBOL_GPL(coresight_enable);
  296. void coresight_disable(struct coresight_device *csdev)
  297. {
  298. LIST_HEAD(path);
  299. mutex_lock(&coresight_mutex);
  300. if (csdev->type != CORESIGHT_DEV_TYPE_SOURCE) {
  301. dev_err(&csdev->dev, "wrong device type in %s\n", __func__);
  302. goto out;
  303. }
  304. if (!csdev->enable)
  305. goto out;
  306. coresight_disable_source(csdev);
  307. if (coresight_build_paths(csdev, &path, false))
  308. dev_err(&csdev->dev, "releasing path(s) failed\n");
  309. out:
  310. mutex_unlock(&coresight_mutex);
  311. }
  312. EXPORT_SYMBOL_GPL(coresight_disable);
  313. static ssize_t enable_sink_show(struct device *dev,
  314. struct device_attribute *attr, char *buf)
  315. {
  316. struct coresight_device *csdev = to_coresight_device(dev);
  317. return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->activated);
  318. }
  319. static ssize_t enable_sink_store(struct device *dev,
  320. struct device_attribute *attr,
  321. const char *buf, size_t size)
  322. {
  323. int ret;
  324. unsigned long val;
  325. struct coresight_device *csdev = to_coresight_device(dev);
  326. ret = kstrtoul(buf, 10, &val);
  327. if (ret)
  328. return ret;
  329. if (val)
  330. csdev->activated = true;
  331. else
  332. csdev->activated = false;
  333. return size;
  334. }
  335. static DEVICE_ATTR_RW(enable_sink);
  336. static ssize_t enable_source_show(struct device *dev,
  337. struct device_attribute *attr, char *buf)
  338. {
  339. struct coresight_device *csdev = to_coresight_device(dev);
  340. return scnprintf(buf, PAGE_SIZE, "%u\n", (unsigned)csdev->enable);
  341. }
  342. static ssize_t enable_source_store(struct device *dev,
  343. struct device_attribute *attr,
  344. const char *buf, size_t size)
  345. {
  346. int ret = 0;
  347. unsigned long val;
  348. struct coresight_device *csdev = to_coresight_device(dev);
  349. ret = kstrtoul(buf, 10, &val);
  350. if (ret)
  351. return ret;
  352. if (val) {
  353. ret = coresight_enable(csdev);
  354. if (ret)
  355. return ret;
  356. } else {
  357. coresight_disable(csdev);
  358. }
  359. return size;
  360. }
  361. static DEVICE_ATTR_RW(enable_source);
  362. static struct attribute *coresight_sink_attrs[] = {
  363. &dev_attr_enable_sink.attr,
  364. NULL,
  365. };
  366. ATTRIBUTE_GROUPS(coresight_sink);
  367. static struct attribute *coresight_source_attrs[] = {
  368. &dev_attr_enable_source.attr,
  369. NULL,
  370. };
  371. ATTRIBUTE_GROUPS(coresight_source);
  372. static struct device_type coresight_dev_type[] = {
  373. {
  374. .name = "none",
  375. },
  376. {
  377. .name = "sink",
  378. .groups = coresight_sink_groups,
  379. },
  380. {
  381. .name = "link",
  382. },
  383. {
  384. .name = "linksink",
  385. .groups = coresight_sink_groups,
  386. },
  387. {
  388. .name = "source",
  389. .groups = coresight_source_groups,
  390. },
  391. };
  392. static void coresight_device_release(struct device *dev)
  393. {
  394. struct coresight_device *csdev = to_coresight_device(dev);
  395. kfree(csdev);
  396. }
  397. static int coresight_orphan_match(struct device *dev, void *data)
  398. {
  399. int i;
  400. bool still_orphan = false;
  401. struct coresight_device *csdev, *i_csdev;
  402. struct coresight_connection *conn;
  403. csdev = data;
  404. i_csdev = to_coresight_device(dev);
  405. /* No need to check oneself */
  406. if (csdev == i_csdev)
  407. return 0;
  408. /* Move on to another component if no connection is orphan */
  409. if (!i_csdev->orphan)
  410. return 0;
  411. /*
  412. * Circle throuch all the connection of that component. If we find
  413. * an orphan connection whose name matches @csdev, link it.
  414. */
  415. for (i = 0; i < i_csdev->nr_outport; i++) {
  416. conn = &i_csdev->conns[i];
  417. /* We have found at least one orphan connection */
  418. if (conn->child_dev == NULL) {
  419. /* Does it match this newly added device? */
  420. if (!strcmp(dev_name(&csdev->dev), conn->child_name)) {
  421. conn->child_dev = csdev;
  422. } else {
  423. /* This component still has an orphan */
  424. still_orphan = true;
  425. }
  426. }
  427. }
  428. i_csdev->orphan = still_orphan;
  429. /*
  430. * Returning '0' ensures that all known component on the
  431. * bus will be checked.
  432. */
  433. return 0;
  434. }
  435. static void coresight_fixup_orphan_conns(struct coresight_device *csdev)
  436. {
  437. /*
  438. * No need to check for a return value as orphan connection(s)
  439. * are hooked-up with each newly added component.
  440. */
  441. bus_for_each_dev(&coresight_bustype, NULL,
  442. csdev, coresight_orphan_match);
  443. }
  444. static int coresight_name_match(struct device *dev, void *data)
  445. {
  446. char *to_match;
  447. struct coresight_device *i_csdev;
  448. to_match = data;
  449. i_csdev = to_coresight_device(dev);
  450. if (!strcmp(to_match, dev_name(&i_csdev->dev)))
  451. return 1;
  452. return 0;
  453. }
  454. static void coresight_fixup_device_conns(struct coresight_device *csdev)
  455. {
  456. int i;
  457. struct device *dev = NULL;
  458. struct coresight_connection *conn;
  459. for (i = 0; i < csdev->nr_outport; i++) {
  460. conn = &csdev->conns[i];
  461. dev = bus_find_device(&coresight_bustype, NULL,
  462. (void *)conn->child_name,
  463. coresight_name_match);
  464. if (dev) {
  465. conn->child_dev = to_coresight_device(dev);
  466. } else {
  467. csdev->orphan = true;
  468. conn->child_dev = NULL;
  469. }
  470. }
  471. }
  472. /**
  473. * coresight_timeout - loop until a bit has changed to a specific state.
  474. * @addr: base address of the area of interest.
  475. * @offset: address of a register, starting from @addr.
  476. * @position: the position of the bit of interest.
  477. * @value: the value the bit should have.
  478. *
  479. * Return: 0 as soon as the bit has taken the desired state or -EAGAIN if
  480. * TIMEOUT_US has elapsed, which ever happens first.
  481. */
  482. int coresight_timeout(void __iomem *addr, u32 offset, int position, int value)
  483. {
  484. int i;
  485. u32 val;
  486. for (i = TIMEOUT_US; i > 0; i--) {
  487. val = __raw_readl(addr + offset);
  488. /* waiting on the bit to go from 0 to 1 */
  489. if (value) {
  490. if (val & BIT(position))
  491. return 0;
  492. /* waiting on the bit to go from 1 to 0 */
  493. } else {
  494. if (!(val & BIT(position)))
  495. return 0;
  496. }
  497. /*
  498. * Delay is arbitrary - the specification doesn't say how long
  499. * we are expected to wait. Extra check required to make sure
  500. * we don't wait needlessly on the last iteration.
  501. */
  502. if (i - 1)
  503. udelay(1);
  504. }
  505. return -EAGAIN;
  506. }
  507. struct bus_type coresight_bustype = {
  508. .name = "coresight",
  509. };
  510. static int __init coresight_init(void)
  511. {
  512. return bus_register(&coresight_bustype);
  513. }
  514. postcore_initcall(coresight_init);
  515. struct coresight_device *coresight_register(struct coresight_desc *desc)
  516. {
  517. int i;
  518. int ret;
  519. int link_subtype;
  520. int nr_refcnts = 1;
  521. atomic_t *refcnts = NULL;
  522. struct coresight_device *csdev;
  523. struct coresight_connection *conns;
  524. csdev = kzalloc(sizeof(*csdev), GFP_KERNEL);
  525. if (!csdev) {
  526. ret = -ENOMEM;
  527. goto err_kzalloc_csdev;
  528. }
  529. if (desc->type == CORESIGHT_DEV_TYPE_LINK ||
  530. desc->type == CORESIGHT_DEV_TYPE_LINKSINK) {
  531. link_subtype = desc->subtype.link_subtype;
  532. if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG)
  533. nr_refcnts = desc->pdata->nr_inport;
  534. else if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_SPLIT)
  535. nr_refcnts = desc->pdata->nr_outport;
  536. }
  537. refcnts = kcalloc(nr_refcnts, sizeof(*refcnts), GFP_KERNEL);
  538. if (!refcnts) {
  539. ret = -ENOMEM;
  540. goto err_kzalloc_refcnts;
  541. }
  542. csdev->refcnt = refcnts;
  543. csdev->nr_inport = desc->pdata->nr_inport;
  544. csdev->nr_outport = desc->pdata->nr_outport;
  545. conns = kcalloc(csdev->nr_outport, sizeof(*conns), GFP_KERNEL);
  546. if (!conns) {
  547. ret = -ENOMEM;
  548. goto err_kzalloc_conns;
  549. }
  550. for (i = 0; i < csdev->nr_outport; i++) {
  551. conns[i].outport = desc->pdata->outports[i];
  552. conns[i].child_name = desc->pdata->child_names[i];
  553. conns[i].child_port = desc->pdata->child_ports[i];
  554. }
  555. csdev->conns = conns;
  556. csdev->type = desc->type;
  557. csdev->subtype = desc->subtype;
  558. csdev->ops = desc->ops;
  559. csdev->orphan = false;
  560. csdev->dev.type = &coresight_dev_type[desc->type];
  561. csdev->dev.groups = desc->groups;
  562. csdev->dev.parent = desc->dev;
  563. csdev->dev.release = coresight_device_release;
  564. csdev->dev.bus = &coresight_bustype;
  565. dev_set_name(&csdev->dev, "%s", desc->pdata->name);
  566. ret = device_register(&csdev->dev);
  567. if (ret)
  568. goto err_device_register;
  569. mutex_lock(&coresight_mutex);
  570. coresight_fixup_device_conns(csdev);
  571. coresight_fixup_orphan_conns(csdev);
  572. mutex_unlock(&coresight_mutex);
  573. return csdev;
  574. err_device_register:
  575. kfree(conns);
  576. err_kzalloc_conns:
  577. kfree(refcnts);
  578. err_kzalloc_refcnts:
  579. kfree(csdev);
  580. err_kzalloc_csdev:
  581. return ERR_PTR(ret);
  582. }
  583. EXPORT_SYMBOL_GPL(coresight_register);
  584. void coresight_unregister(struct coresight_device *csdev)
  585. {
  586. mutex_lock(&coresight_mutex);
  587. kfree(csdev->conns);
  588. device_unregister(&csdev->dev);
  589. mutex_unlock(&coresight_mutex);
  590. }
  591. EXPORT_SYMBOL_GPL(coresight_unregister);
  592. MODULE_LICENSE("GPL v2");