0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 ===================
0004 System Trace Module
0005 ===================
0006
0007 System Trace Module (STM) is a device described in MIPI STP specs as
0008 STP trace stream generator. STP (System Trace Protocol) is a trace
0009 protocol multiplexing data from multiple trace sources, each one of
0010 which is assigned a unique pair of master and channel. While some of
0011 these masters and channels are statically allocated to certain
0012 hardware trace sources, others are available to software. Software
0013 trace sources are usually free to pick for themselves any
0014 master/channel combination from this pool.
0015
0016 On the receiving end of this STP stream (the decoder side), trace
0017 sources can only be identified by master/channel combination, so in
0018 order for the decoder to be able to make sense of the trace that
0019 involves multiple trace sources, it needs to be able to map those
0020 master/channel pairs to the trace sources that it understands.
0021
0022 For instance, it is helpful to know that syslog messages come on
0023 master 7 channel 15, while arbitrary user applications can use masters
0024 48 to 63 and channels 0 to 127.
0025
0026 To solve this mapping problem, stm class provides a policy management
0027 mechanism via configfs, that allows defining rules that map string
0028 identifiers to ranges of masters and channels. If these rules (policy)
0029 are consistent with what decoder expects, it will be able to properly
0030 process the trace data.
0031
0032 This policy is a tree structure containing rules (policy_node) that
0033 have a name (string identifier) and a range of masters and channels
0034 associated with it, located in "stp-policy" subsystem directory in
0035 configfs. The topmost directory's name (the policy) is formatted as
0036 the STM device name to which this policy applies and an arbitrary
0037 string identifier separated by a stop. From the example above, a rule
0038 may look like this::
0039
0040 $ ls /config/stp-policy/dummy_stm.my-policy/user
0041 channels masters
0042 $ cat /config/stp-policy/dummy_stm.my-policy/user/masters
0043 48 63
0044 $ cat /config/stp-policy/dummy_stm.my-policy/user/channels
0045 0 127
0046
0047 which means that the master allocation pool for this rule consists of
0048 masters 48 through 63 and channel allocation pool has channels 0
0049 through 127 in it. Now, any producer (trace source) identifying itself
0050 with "user" identification string will be allocated a master and
0051 channel from within these ranges.
0052
0053 These rules can be nested, for example, one can define a rule "dummy"
0054 under "user" directory from the example above and this new rule will
0055 be used for trace sources with the id string of "user/dummy".
0056
0057 Trace sources have to open the stm class device's node and write their
0058 trace data into its file descriptor.
0059
0060 In order to find an appropriate policy node for a given trace source,
0061 several mechanisms can be used. First, a trace source can explicitly
0062 identify itself by calling an STP_POLICY_ID_SET ioctl on the character
0063 device's file descriptor, providing their id string, before they write
0064 any data there. Secondly, if they chose not to perform the explicit
0065 identification (because you may not want to patch existing software
0066 to do this), they can just start writing the data, at which point the
0067 stm core will try to find a policy node with the name matching the
0068 task's name (e.g., "syslogd") and if one exists, it will be used.
0069 Thirdly, if the task name can't be found among the policy nodes, the
0070 catch-all entry "default" will be used, if it exists. This entry also
0071 needs to be created and configured by the system administrator or
0072 whatever tools are taking care of the policy configuration. Finally,
0073 if all the above steps failed, the write() to an stm file descriptor
0074 will return a error (EINVAL).
0075
0076 Previously, if no policy nodes were found for a trace source, the stm
0077 class would silently fall back to allocating the first available
0078 contiguous range of master/channels from the beginning of the device's
0079 master/channel range. The new requirement for a policy node to exist
0080 will help programmers and sysadmins identify gaps in configuration
0081 and have better control over the un-identified sources.
0082
0083 Some STM devices may allow direct mapping of the channel mmio regions
0084 to userspace for zero-copy writing. One mappable page (in terms of
0085 mmu) will usually contain multiple channels' mmios, so the user will
0086 need to allocate that many channels to themselves (via the
0087 aforementioned ioctl() call) to be able to do this. That is, if your
0088 stm device's channel mmio region is 64 bytes and hardware page size is
0089 4096 bytes, after a successful STP_POLICY_ID_SET ioctl() call with
0090 width==64, you should be able to mmap() one page on this file
0091 descriptor and obtain direct access to an mmio region for 64 channels.
0092
0093 Examples of STM devices are Intel(R) Trace Hub [1] and Coresight STM
0094 [2].
0095
0096 stm_source
0097 ==========
0098
0099 For kernel-based trace sources, there is "stm_source" device
0100 class. Devices of this class can be connected and disconnected to/from
0101 stm devices at runtime via a sysfs attribute called "stm_source_link"
0102 by writing the name of the desired stm device there, for example::
0103
0104 $ echo dummy_stm.0 > /sys/class/stm_source/console/stm_source_link
0105
0106 For examples on how to use stm_source interface in the kernel, refer
0107 to stm_console, stm_heartbeat or stm_ftrace drivers.
0108
0109 Each stm_source device will need to assume a master and a range of
0110 channels, depending on how many channels it requires. These are
0111 allocated for the device according to the policy configuration. If
0112 there's a node in the root of the policy directory that matches the
0113 stm_source device's name (for example, "console"), this node will be
0114 used to allocate master and channel numbers. If there's no such policy
0115 node, the stm core will use the catch-all entry "default", if one
0116 exists. If neither policy nodes exist, the write() to stm_source_link
0117 will return an error.
0118
0119 stm_console
0120 ===========
0121
0122 One implementation of this interface also used in the example above is
0123 the "stm_console" driver, which basically provides a one-way console
0124 for kernel messages over an stm device.
0125
0126 To configure the master/channel pair that will be assigned to this
0127 console in the STP stream, create a "console" policy entry (see the
0128 beginning of this text on how to do that). When initialized, it will
0129 consume one channel.
0130
0131 stm_ftrace
0132 ==========
0133
0134 This is another "stm_source" device, once the stm_ftrace has been
0135 linked with an stm device, and if "function" tracer is enabled,
0136 function address and parent function address which Ftrace subsystem
0137 would store into ring buffer will be exported via the stm device at
0138 the same time.
0139
0140 Currently only Ftrace "function" tracer is supported.
0141
0142 * [1] https://software.intel.com/sites/default/files/managed/d3/3c/intel-th-developer-manual.pdf
0143 * [2] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0444b/index.html