stm.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ===================
  3. System Trace Module
  4. ===================
  5. System Trace Module (STM) is a device described in MIPI STP specs as
  6. STP trace stream generator. STP (System Trace Protocol) is a trace
  7. protocol multiplexing data from multiple trace sources, each one of
  8. which is assigned a unique pair of master and channel. While some of
  9. these masters and channels are statically allocated to certain
  10. hardware trace sources, others are available to software. Software
  11. trace sources are usually free to pick for themselves any
  12. master/channel combination from this pool.
  13. On the receiving end of this STP stream (the decoder side), trace
  14. sources can only be identified by master/channel combination, so in
  15. order for the decoder to be able to make sense of the trace that
  16. involves multiple trace sources, it needs to be able to map those
  17. master/channel pairs to the trace sources that it understands.
  18. For instance, it is helpful to know that syslog messages come on
  19. master 7 channel 15, while arbitrary user applications can use masters
  20. 48 to 63 and channels 0 to 127.
  21. To solve this mapping problem, stm class provides a policy management
  22. mechanism via configfs, that allows defining rules that map string
  23. identifiers to ranges of masters and channels. If these rules (policy)
  24. are consistent with what decoder expects, it will be able to properly
  25. process the trace data.
  26. This policy is a tree structure containing rules (policy_node) that
  27. have a name (string identifier) and a range of masters and channels
  28. associated with it, located in "stp-policy" subsystem directory in
  29. configfs. The topmost directory's name (the policy) is formatted as
  30. the STM device name to which this policy applies and an arbitrary
  31. string identifier separated by a stop. From the example above, a rule
  32. may look like this::
  33. $ ls /config/stp-policy/dummy_stm.my-policy/user
  34. channels masters
  35. $ cat /config/stp-policy/dummy_stm.my-policy/user/masters
  36. 48 63
  37. $ cat /config/stp-policy/dummy_stm.my-policy/user/channels
  38. 0 127
  39. which means that the master allocation pool for this rule consists of
  40. masters 48 through 63 and channel allocation pool has channels 0
  41. through 127 in it. Now, any producer (trace source) identifying itself
  42. with "user" identification string will be allocated a master and
  43. channel from within these ranges.
  44. These rules can be nested, for example, one can define a rule "dummy"
  45. under "user" directory from the example above and this new rule will
  46. be used for trace sources with the id string of "user/dummy".
  47. Trace sources have to open the stm class device's node and write their
  48. trace data into its file descriptor.
  49. In order to find an appropriate policy node for a given trace source,
  50. several mechanisms can be used. First, a trace source can explicitly
  51. identify itself by calling an STP_POLICY_ID_SET ioctl on the character
  52. device's file descriptor, providing their id string, before they write
  53. any data there. Secondly, if they chose not to perform the explicit
  54. identification (because you may not want to patch existing software
  55. to do this), they can just start writing the data, at which point the
  56. stm core will try to find a policy node with the name matching the
  57. task's name (e.g., "syslogd") and if one exists, it will be used.
  58. Thirdly, if the task name can't be found among the policy nodes, the
  59. catch-all entry "default" will be used, if it exists. This entry also
  60. needs to be created and configured by the system administrator or
  61. whatever tools are taking care of the policy configuration. Finally,
  62. if all the above steps failed, the write() to an stm file descriptor
  63. will return a error (EINVAL).
  64. Previously, if no policy nodes were found for a trace source, the stm
  65. class would silently fall back to allocating the first available
  66. contiguous range of master/channels from the beginning of the device's
  67. master/channel range. The new requirement for a policy node to exist
  68. will help programmers and sysadmins identify gaps in configuration
  69. and have better control over the un-identified sources.
  70. Some STM devices may allow direct mapping of the channel mmio regions
  71. to userspace for zero-copy writing. One mappable page (in terms of
  72. mmu) will usually contain multiple channels' mmios, so the user will
  73. need to allocate that many channels to themselves (via the
  74. aforementioned ioctl() call) to be able to do this. That is, if your
  75. stm device's channel mmio region is 64 bytes and hardware page size is
  76. 4096 bytes, after a successful STP_POLICY_ID_SET ioctl() call with
  77. width==64, you should be able to mmap() one page on this file
  78. descriptor and obtain direct access to an mmio region for 64 channels.
  79. Examples of STM devices are Intel(R) Trace Hub [1] and Coresight STM
  80. [2].
  81. stm_source
  82. ==========
  83. For kernel-based trace sources, there is "stm_source" device
  84. class. Devices of this class can be connected and disconnected to/from
  85. stm devices at runtime via a sysfs attribute called "stm_source_link"
  86. by writing the name of the desired stm device there, for example::
  87. $ echo dummy_stm.0 > /sys/class/stm_source/console/stm_source_link
  88. For examples on how to use stm_source interface in the kernel, refer
  89. to stm_console, stm_heartbeat or stm_ftrace drivers.
  90. Each stm_source device will need to assume a master and a range of
  91. channels, depending on how many channels it requires. These are
  92. allocated for the device according to the policy configuration. If
  93. there's a node in the root of the policy directory that matches the
  94. stm_source device's name (for example, "console"), this node will be
  95. used to allocate master and channel numbers. If there's no such policy
  96. node, the stm core will use the catch-all entry "default", if one
  97. exists. If neither policy nodes exist, the write() to stm_source_link
  98. will return an error.
  99. stm_console
  100. ===========
  101. One implementation of this interface also used in the example above is
  102. the "stm_console" driver, which basically provides a one-way console
  103. for kernel messages over an stm device.
  104. To configure the master/channel pair that will be assigned to this
  105. console in the STP stream, create a "console" policy entry (see the
  106. beginning of this text on how to do that). When initialized, it will
  107. consume one channel.
  108. stm_ftrace
  109. ==========
  110. This is another "stm_source" device, once the stm_ftrace has been
  111. linked with an stm device, and if "function" tracer is enabled,
  112. function address and parent function address which Ftrace subsystem
  113. would store into ring buffer will be exported via the stm device at
  114. the same time.
  115. Currently only Ftrace "function" tracer is supported.
  116. * [1] https://software.intel.com/sites/default/files/managed/d3/3c/intel-th-developer-manual.pdf
  117. * [2] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0444b/index.html