coresight.c 16 KB

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