coresight.c 25 KB

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