Skip to contentSkip to conversation

Introduce Intel Trace Hub support

PATCH Superseded Alexander Shishkin 5 participants 34 messages Jul 6, 2015 – Aug 5, 2015

Subsystems (7)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH v3 1/11 stm class: Introduce an abstraction for System Trace Module devices

In reply to Alexander Shishkin

A System Trace Module (STM) is a device exporting data in System Trace Protocol (STP) format as defined by MIPI STP standards. Examples of such devices are Intel Trace Hub and Coresight STM.

This abstraction provides a unified interface for software trace sources to send their data over an STM device to a debug host. In order to do that, such a trace source needs to be assigned a pair of master/channel identifiers that all the data from this source will be tagged with. The STP decoder on the debug host side will use these master/channel tags to distinguish different trace streams from one another inside one STP stream.

This abstraction provides a configfs-based policy management mechanism for dynamic allocation of these master/channel pairs based on trace source-supplied string identifier. It has the flexibility of being defined at runtime and at the same time (provided that the policy definition is aligned with the decoding end) consistency.

For userspace trace sources, this abstraction provides write()-based and mmap()-based (if the underlying stm device allows this) output mechanism.

For kernel-side trace sources, we provide "stm_source" device class that can be connected to an stm device at run time.

---
 Documentation/ABI/testing/configfs-stp-policy    |   48 +
 Documentation/ABI/testing/sysfs-class-stm        |   14 +
 Documentation/ABI/testing/sysfs-class-stm_source |   11 +
 Documentation/ioctl/ioctl-number.txt             |    3 +
 Documentation/trace/stm.txt                      |   80 ++
 drivers/Kconfig                                  |    2 +
 drivers/Makefile                                 |    1 +
 drivers/hwtracing/stm/Kconfig                    |    8 +
 drivers/hwtracing/stm/Makefile                   |    3 +
 drivers/hwtracing/stm/core.c                     | 1029 ++++++++++++++++++++++
 drivers/hwtracing/stm/policy.c                   |  529 +++++++++++
 drivers/hwtracing/stm/stm.h                      |   87 ++
 include/linux/stm.h                              |  126 +++
 include/uapi/linux/stm.h                         |   50 ++
 14 files changed, 1991 insertions(+)
 create mode 100644 Documentation/ABI/testing/configfs-stp-policy
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm_source
 create mode 100644 Documentation/trace/stm.txt
 create mode 100644 drivers/hwtracing/stm/Kconfig
 create mode 100644 drivers/hwtracing/stm/Makefile
 create mode 100644 drivers/hwtracing/stm/core.c
 create mode 100644 drivers/hwtracing/stm/policy.c
 create mode 100644 drivers/hwtracing/stm/stm.h
 create mode 100644 include/linux/stm.h
 create mode 100644 include/uapi/linux/stm.h
Documentation/ABI/testing/configfs-stp-policy new file +48 -0
new file mode 100644
index 0000000000..421ce6825c
@@ -0,0 +1,48 @@
+What:		/config/stp-policy
+Date:		June 2015
+KernelVersion:	4.3
+Description:
+		This group contains policies mandating Master/Channel allocation
+		for software sources wishing to send trace data over an STM
+		device.
+
+What:		/config/stp-policy/<device>.<policy>
+Date:		June 2015
+KernelVersion:	4.3
+Description:
+		This group is the root of a policy; its name is a concatenation
+		of an stm device name to which this policy applies and an
+		arbitrary string. If <device> part doesn't match an existing
+		stm device, mkdir will fail with ENODEV; if that device already
+		has a policy assigned to it, mkdir will fail with EBUSY.
+
+What:		/config/stp-policy/<device>.<policy>/device
+Date:		June 2015
+KernelVersion:	4.3
+Description:
+		STM device to which this policy applies, read only. Same as the
+		<device> component of its parent directory.
+
+What:		/config/stp-policy/<device>.<policy>/<node>
+Date:		June 2015
+KernelVersion:	4.3
+Description:
+		Policy node is a string identifier that software clients will
+		use to request a master/channel to be allocated and assigned to
+		them.
+
+What:		/config/stp-policy/<device>.<policy>/<node>/masters
+Date:		June 2015
+KernelVersion:	4.3
+Description:
+		Range of masters from which to allocate for users of this node.
+		Write two numbers: the first master and the last master number.
+
+What:		/config/stp-policy/<device>.<policy>/<node>/channels
+Date:		June 2015
+KernelVersion:	4.3
+Description:
+		Range of channels from which to allocate for users of this node.
+		Write two numbers: the first channel and the last channel
+		number.
+
Documentation/ABI/testing/sysfs-class-stm new file +14 -0
new file mode 100644
index 0000000000..c9aa4f3fc9
@@ -0,0 +1,14 @@
+What:		/sys/class/stm/<stm>/masters
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:
+		Shows first and last available to software master numbers on
+		this STM device.
+
+What:		/sys/class/stm/<stm>/channels
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:
+		Shows the number of channels per master on this STM device.
Documentation/ABI/testing/sysfs-class-stm_source new file +11 -0
new file mode 100644
index 0000000000..57b8dd39bb
@@ -0,0 +1,11 @@
+What:		/sys/class/stm_source/<stm_source>/stm_source_link
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:
+		stm_source device linkage to stm device, where its tracing data
+		is directed. Reads return an existing connection or "<none>" if
+		this stm_source is not connected to any stm device yet.
+		Write an existing (registered) stm device's name here to
+		connect that device. If a device is already connected to this
+		stm_source, it will first be disconnected.
Documentation/ioctl/ioctl-number.txt +3 -0
index 611c52267d..7fa29b4ece 100644
@@ -81,6 +81,9 @@ Code  Seq#(hex)	Include File		Comments
 0x22	all	scsi/sg.h
 '#'	00-3F	IEEE 1394 Subsystem	Block for the entire subsystem
 '$'	00-0F	linux/perf_counter.h, linux/perf_event.h
+'%'	00-0F	include/uapi/linux/stm.h
+					System Trace Module subsystem
+					<mailto:[email protected]>
 '&'	00-07	drivers/firewire/nosy-user.h
 '1'	00-1F	<linux/timepps.h>	PPS kit from Ulrich Windl
 					<ftp://ftp.de.kernel.org/pub/linux/daemons/ntp/PPS/>
Documentation/trace/stm.txt new file +80 -0

80 lines not on this page · raw message

drivers/Kconfig +2 -0
index 6e973b8e3a..96efe4522c 100644
@@ -184,4 +184,6 @@ source "drivers/android/Kconfig"
 
 source "drivers/nvdimm/Kconfig"
 
+source "drivers/hwtracing/stm/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index b64b49f6e0..6647bc923f 100644
@@ -164,4 +164,5 @@ obj-$(CONFIG_MCB)		+= mcb/
 obj-$(CONFIG_RAS)		+= ras/
 obj-$(CONFIG_THUNDERBOLT)	+= thunderbolt/
 obj-$(CONFIG_CORESIGHT)		+= hwtracing/coresight/
+obj-$(CONFIG_STM)		+= hwtracing/stm/
 obj-$(CONFIG_ANDROID)		+= android/
drivers/hwtracing/stm/Kconfig new file +8 -0
new file mode 100644
index 0000000000..90ed327461
@@ -0,0 +1,8 @@
+config STM
+	tristate "System Trace Module devices"
+	help
+	  A System Trace Module (STM) is a device exporting data in System
+	  Trace Protocol (STP) format as defined by MIPI STP standards.
+	  Examples of such devices are Intel Trace Hub and Coresight STM.
+
+	  Say Y here to enable System Trace Module device support.
drivers/hwtracing/stm/Makefile new file +3 -0
new file mode 100644
index 0000000000..adec701649
@@ -0,0 +1,3 @@
+obj-$(CONFIG_STM)	+= stm_core.o
+
+stm_core-y		:= core.o policy.o
drivers/hwtracing/stm/core.c new file +1029 -0

1,029 lines not on this page · raw message

drivers/hwtracing/stm/policy.c new file +529 -0

529 lines not on this page · raw message

drivers/hwtracing/stm/stm.h new file +87 -0

87 lines not on this page · raw message

include/linux/stm.h new file +126 -0

126 lines not on this page · raw message

include/uapi/linux/stm.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.1.4

Alexander Shishkin

author maintainer
#0 lore raw

v3 cover Introduce Intel Trace Hub support

Hi Greg and everybody,

Here's a patchset adding support for Intel Trace Hub (TH) [1] to the kernel. Full description follows the changelog. I took some time discussing matters with Mathieu wrt our common needs from the STM driver framework and also to fix things that I found in the process, so the changelog is a bit hefty. A lot of Intel Trace Hub driver fixes were contributed by Laurent Fert.

Changes since v2:
* STM framework changes:
- Addressed Greg KH's comments (struct device usage, attributes, etc etc),
- Started using STM device name in the policy name to make interface more logical,
- Tried to improve initcall ordering to accomodate coresight modules (we might need more work on that when the time comes),
- Fixed output assignment on unlinking,
- Refcounting and locking fixes in stm vs stm_source code,
- Added missing cleanup_srcu_struct() in exit call,
- Changed stm_data::ioctl() to a more specific set_options() callback, which is what the underlying driver actually wants,
- Allocated our ioctl commands "by the book" throught ioctl-number.txt,
- Made stm_data::link() callback return an error code in case the driver doesn't like something; handle this condition accordingly;
* STM console module got renamed to "stm_console";
* Intel TH: fixed resource printing;
* Intel TH/GTH:
- Added an output attribute to control maintenance packet
frequency: smcfreq,
- Undo master assignment on master disabling (Laurent),
- Disable global destination overriding (Laurent),
- Clear force capture done bit on trace disabling (Laurent),
- Fixed handling of masters above 256;
* Intel TH/MSU:
- Reworked block iteration in multiblock mode (Laurent),
- Use pgprot_noncached() for user mappings,
- Fixed window's page offset based on previous window's offset and size (Laurent),
- Fixed user_count when no buffer is allocated (Laurent),
- Reset hw headers in multiblock mode (Laurent);
* Intel TH/STH:
- Handle failure to register stm device.

Changes since v1:

* fixed Kconfig whitespaces and removed defaults per comments from Paul Bolle,
* addressed comments from Joe Perches.

Changes since v0:

* filter-branch'ed everything under drivers/hwtracing where Mathieu is also moving coresight drivers,
* added some more comments to the stm class code to address Mathieu's questions,
* fixed CONFIG_STM=m case, which failed to build in the previous version as pointed out by Paul Bolle,
* fixed a rather embarrassing memory leak in the error path of stm_char_policy_set_ioctl(),
* added ioctl() callback to struct stm_data so that downstream stm drivers can serve their own commands on top of the same file descriptor, as suggested by Mathieu.

Intel Trace Hub (TH) is a set of hardware blocks that produce, switch and output trace data from multiple hardware and software sources over several types of trace output ports encoded in System Trace Protocol (MIPI STPv2) and is intended to perform full system debugging.

The first part of this patchset introduces an abstraction for STM devices, or the devices that combine trace data from multiple trace sources into a STP trace stream. It provides a generic interface for software writers to send their trace data and also solves the problem of mapping STP master/channel asignments to trace sources between the system under tracing and the STP decoder on the debug host end. This part is also useful for the Coresight STM driver as it uses a similar interface and has the same channel mapping problem.

The remainder of the patchset is Intel TH support. The core part is platform independant and is called into by glue layers such as the pci driver that follows. The core provides a bus, on which Intel TH subdevices are allocated. Each subdevice has its own driver. Subdevices are divided into 3 types: sources, outputs and a switch (there's usually just one). The latter is the central component in this scheme of things and is required for other drivers to load and function. Other drivers, however, are optional. Sources are producers of trace data. Outputs are means of exporting the resulting STP trace to the debug host via trace ports or system memory.

All configuration is done via sysfs and doesn't require any specific userspace beyond a shell.

Currently supported Intel TH subdevices are Software Trace Hub (STH, source), Memory Storage Unit (MSU, output), Parallel Tracing Interface unit (PTI, output), Global Trace Hub (GTH, switch). Support for other subdevices can be added later.

Also this patchset adds MAINTAINERS entries for stm class and Intel TH code.

[1] https://software.intel.com/sites/default/files/managed/d3/3c/intel-th-developer-manual.pdf

Alexander Shishkin (11):
stm class: Introduce an abstraction for System Trace Module devices
MAINTAINERS: add an entry for System Trace Module device class stm class: dummy_stm: Add dummy driver for testing stm class stm class: stm_console: Add kernel-console-over-stm driver
intel_th: Add driver infrastructure for Intel Trace Hub devices
intel_th: Add pci glue layer for Intel Trace Hub
intel_th: Add Global Trace Hub driver
intel_th: Add Software Trace Hub driver
intel_th: Add Memory Storage Unit driver
intel_th: Add PTI output driver
MAINTAINERS: add an entry for Intel(R) Trace Hub

 Documentation/ABI/testing/configfs-stp-policy      |   48 +
 .../ABI/testing/sysfs-bus-intel_th-devices-gth     |   49 +
 .../ABI/testing/sysfs-bus-intel_th-devices-msc     |   33 +
 .../ABI/testing/sysfs-bus-intel_th-devices-pti     |   24 +
 .../ABI/testing/sysfs-bus-intel_th-output-devices  |   13 +
 Documentation/ABI/testing/sysfs-class-stm          |   14 +
 Documentation/ABI/testing/sysfs-class-stm_source   |   11 +
 Documentation/ioctl/ioctl-number.txt               |    3 +
 Documentation/trace/intel_th.txt                   |   99 ++
 Documentation/trace/stm.txt                        |   80 ++
 MAINTAINERS                                        |   14 +
 drivers/Kconfig                                    |    4 +
 drivers/Makefile                                   |    2 +
 drivers/hwtracing/intel_th/Kconfig                 |   72 +
 drivers/hwtracing/intel_th/Makefile                |   18 +
 drivers/hwtracing/intel_th/core.c                  |  692 +++++++++
 drivers/hwtracing/intel_th/debug.c                 |   36 +
 drivers/hwtracing/intel_th/debug.h                 |   34 +
 drivers/hwtracing/intel_th/gth.c                   |  706 +++++++++
 drivers/hwtracing/intel_th/gth.h                   |   66 +
 drivers/hwtracing/intel_th/intel_th.h              |  244 ++++
 drivers/hwtracing/intel_th/msu.c                   | 1509 ++++++++++++++++++++
 drivers/hwtracing/intel_th/msu.h                   |  116 ++
 drivers/hwtracing/intel_th/pci.c                   |   86 ++
 drivers/hwtracing/intel_th/pti.c                   |  252 ++++
 drivers/hwtracing/intel_th/pti.h                   |   29 +
 drivers/hwtracing/intel_th/sth.c                   |  259 ++++
 drivers/hwtracing/intel_th/sth.h                   |   42 +
 drivers/hwtracing/stm/Kconfig                      |   25 +
 drivers/hwtracing/stm/Makefile                     |    9 +
 drivers/hwtracing/stm/console.c                    |   80 ++
 drivers/hwtracing/stm/core.c                       | 1029 +++++++++++++
 drivers/hwtracing/stm/dummy_stm.c                  |   66 +
 drivers/hwtracing/stm/policy.c                     |  529 +++++++
 drivers/hwtracing/stm/stm.h                        |   87 ++
 include/linux/stm.h                                |  126 ++
 include/uapi/linux/stm.h                           |   50 +
 37 files changed, 6556 insertions(+)
 create mode 100644 Documentation/ABI/testing/configfs-stp-policy
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-devices-gth
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-devices-msc
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-devices-pti
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-output-devices
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm_source
 create mode 100644 Documentation/trace/intel_th.txt
 create mode 100644 Documentation/trace/stm.txt
 create mode 100644 drivers/hwtracing/intel_th/Kconfig
 create mode 100644 drivers/hwtracing/intel_th/Makefile
 create mode 100644 drivers/hwtracing/intel_th/core.c
 create mode 100644 drivers/hwtracing/intel_th/debug.c
 create mode 100644 drivers/hwtracing/intel_th/debug.h
 create mode 100644 drivers/hwtracing/intel_th/gth.c
 create mode 100644 drivers/hwtracing/intel_th/gth.h
 create mode 100644 drivers/hwtracing/intel_th/intel_th.h
 create mode 100644 drivers/hwtracing/intel_th/msu.c
 create mode 100644 drivers/hwtracing/intel_th/msu.h
 create mode 100644 drivers/hwtracing/intel_th/pci.c
 create mode 100644 drivers/hwtracing/intel_th/pti.c
 create mode 100644 drivers/hwtracing/intel_th/pti.h
 create mode 100644 drivers/hwtracing/intel_th/sth.c
 create mode 100644 drivers/hwtracing/intel_th/sth.h
 create mode 100644 drivers/hwtracing/stm/Kconfig
 create mode 100644 drivers/hwtracing/stm/Makefile
 create mode 100644 drivers/hwtracing/stm/console.c
 create mode 100644 drivers/hwtracing/stm/core.c
 create mode 100644 drivers/hwtracing/stm/dummy_stm.c
 create mode 100644 drivers/hwtracing/stm/policy.c
 create mode 100644 drivers/hwtracing/stm/stm.h
 create mode 100644 include/linux/stm.h
 create mode 100644 include/uapi/linux/stm.h
Signature · 1 line
2.1.4
author maintainer
#18 lore raw

PATCH v3 3/11 stm class: dummy_stm: Add dummy driver for testing stm class

This is a simple module that pretends to be an stm device and discards all the data that comes in. Useful for testing stm class and its users.

---
 drivers/hwtracing/stm/Kconfig     |  8 +++++
 drivers/hwtracing/stm/Makefile    |  2 ++
 drivers/hwtracing/stm/dummy_stm.c | 66 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 drivers/hwtracing/stm/dummy_stm.c
drivers/hwtracing/stm/Kconfig +8 -0
index 90ed327461..4fdb8f46d2 100644
@@ -6,3 +6,11 @@ config STM
 	  Examples of such devices are Intel Trace Hub and Coresight STM.
 
 	  Say Y here to enable System Trace Module device support.
+
+config STM_DUMMY
+	tristate "Dummy STM driver"
+	help
+	  This is a simple dummy device that pretends to be an stm device
+	  and discards your data. Use for stm class testing.
+
+	  If you don't know what this is, say N.
drivers/hwtracing/stm/Makefile +2 -0
index adec701649..1b2725fd04 100644
@@ -1,3 +1,5 @@
 obj-$(CONFIG_STM)	+= stm_core.o
 
 stm_core-y		:= core.o policy.o
+
+obj-$(CONFIG_STM_DUMMY)	+= dummy_stm.o
drivers/hwtracing/stm/dummy_stm.c new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#17 lore raw

PATCH v3 2/11 MAINTAINERS: add an entry for System Trace Module device class

In reply to Alexander Shishkin

Add myself as a maintainer for the stm class framework.

---
 MAINTAINERS | 8 ++++++++
 1 file changed, 8 insertions(+)
MAINTAINERS +8 -0
index 8133cefb6b..40c2b7dd4a 100644
@@ -8893,6 +8893,14 @@ S:	Maintained
 F:	include/linux/mmc/dw_mmc.h
 F:	drivers/mmc/host/dw_mmc*
 
+SYSTEM TRACE MODULE CLASS
+M:	Alexander Shishkin <[email protected]>
+S:	Maintained
+F:	Documentation/trace/stm.txt
+F:	drivers/hwtracing/stm/
+F:	include/linux/stm.h
+F:	include/uapi/linux/stm.h
+
 THUNDERBOLT DRIVER
 M:	Andreas Noever <[email protected]>
 S:	Maintained
Signature · 1 line
2.1.4
author maintainer
#20 lore raw

PATCH v3 4/11 stm class: stm_console: Add kernel-console-over-stm driver

In reply to Alexander Shishkin

This is a simple stm_source class device driver (kernelspace stm trace source) that registers a console and sends kernel messages over STM devices.

---
 drivers/hwtracing/stm/Kconfig   |  9 +++++
 drivers/hwtracing/stm/Makefile  |  4 +++
 drivers/hwtracing/stm/console.c | 80 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+)
 create mode 100644 drivers/hwtracing/stm/console.c
drivers/hwtracing/stm/Kconfig +9 -0
index 4fdb8f46d2..583c222218 100644
@@ -14,3 +14,12 @@ config STM_DUMMY
 	  and discards your data. Use for stm class testing.
 
 	  If you don't know what this is, say N.
+
+config STM_SOURCE_CONSOLE
+	tristate "Kernel console over STM devices"
+	help
+	  This is a kernel space trace source that sends kernel log
+	  messages to trace hosts over STM devices.
+
+	  If you want to send kernel console messages over STM devices,
+	  say Y.
drivers/hwtracing/stm/Makefile +4 -0
index 1b2725fd04..f9312c38dd 100644
@@ -3,3 +3,7 @@ obj-$(CONFIG_STM)	+= stm_core.o
 stm_core-y		:= core.o policy.o
 
 obj-$(CONFIG_STM_DUMMY)	+= dummy_stm.o
+
+obj-$(CONFIG_STM_SOURCE_CONSOLE)	+= stm_console.o
+
+stm_console-y		:= console.o
drivers/hwtracing/stm/console.c new file +80 -0

80 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#22 lore raw

PATCH v3 5/11 intel_th: Add driver infrastructure for Intel Trace Hub devices

In reply to Alexander Shishkin

Intel Trace Hub (TH) is a set of hardware blocks (subdevices) that produce, switch and output trace data from multiple hardware and software sources over several types of trace output ports encoded in System Trace Protocol (MIPI STPv2) and is intended to perform full system debugging.

For these subdevices, we create a bus, where they can be discovered and configured by userspace software.

This patch creates this bus infrastructure, three types of devices (source, output, switch), resource allocation, some callback mechanisms to facilitate communication between the subdevices' drivers and some common sysfs attributes.

---
 .../ABI/testing/sysfs-bus-intel_th-output-devices  |  13 +
 Documentation/trace/intel_th.txt                   |  99 +++
 drivers/Kconfig                                    |   2 +
 drivers/Makefile                                   |   1 +
 drivers/hwtracing/intel_th/Kconfig                 |  24 +
 drivers/hwtracing/intel_th/Makefile                |   3 +
 drivers/hwtracing/intel_th/core.c                  | 692 +++++++++++++++++++++
 drivers/hwtracing/intel_th/debug.c                 |  36 ++
 drivers/hwtracing/intel_th/debug.h                 |  34 +
 drivers/hwtracing/intel_th/intel_th.h              | 244 ++++++++
 10 files changed, 1148 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-output-devices
 create mode 100644 Documentation/trace/intel_th.txt
 create mode 100644 drivers/hwtracing/intel_th/Kconfig
 create mode 100644 drivers/hwtracing/intel_th/Makefile
 create mode 100644 drivers/hwtracing/intel_th/core.c
 create mode 100644 drivers/hwtracing/intel_th/debug.c
 create mode 100644 drivers/hwtracing/intel_th/debug.h
 create mode 100644 drivers/hwtracing/intel_th/intel_th.h
Documentation/ABI/testing/sysfs-bus-intel_th-output-devices new file +13 -0
new file mode 100644
index 0000000000..4d48a94518
@@ -0,0 +1,13 @@
+What:		/sys/bus/intel_th/devices/<intel_th_id>-<device><id>/active
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Writes of 1 or 0 enable or disable trace output to this
+		output device. Reads return current status.
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-msc<msc-id>/port
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RO) Port number, corresponding to this output device on the
+		switch (GTH).
Documentation/trace/intel_th.txt new file +99 -0

99 lines not on this page · raw message

drivers/Kconfig +2 -0
index 96efe4522c..62987da274 100644
@@ -186,4 +186,6 @@ source "drivers/nvdimm/Kconfig"
 
 source "drivers/hwtracing/stm/Kconfig"
 
+source "drivers/hwtracing/intel_th/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index 6647bc923f..d304cb1e2b 100644
@@ -164,5 +164,6 @@ obj-$(CONFIG_MCB)		+= mcb/
 obj-$(CONFIG_RAS)		+= ras/
 obj-$(CONFIG_THUNDERBOLT)	+= thunderbolt/
 obj-$(CONFIG_CORESIGHT)		+= hwtracing/coresight/
+obj-y				+= hwtracing/intel_th/
 obj-$(CONFIG_STM)		+= hwtracing/stm/
 obj-$(CONFIG_ANDROID)		+= android/
drivers/hwtracing/intel_th/Kconfig new file +24 -0
new file mode 100644
index 0000000000..04e32cca4b
@@ -0,0 +1,24 @@
+config INTEL_TH
+	tristate "Intel Trace Hub controller"
+	help
+	  Intel Trace Hub (TH) is a set of hardware blocks (subdevices) that
+	  produce, switch and output trace data from multiple hardware and
+	  software sources over several types of trace output ports encoded
+	  in System Trace Protocol (MIPI STPv2) and is intended to perform
+	  full system debugging.
+
+	  This option enables intel_th bus and common code used by TH
+	  subdevices to interact with each other and hardware and for
+	  platform glue layers to drive Intel TH devices.
+
+	  Say Y here to enable Intel Trace Hub controller support.
+
+if INTEL_TH
+
+config INTEL_TH_DEBUG
+	bool "Intel Trace Hub debugging"
+	depends on DEBUG_FS
+	help
+	  Say Y here to enable debugging.
+
+endif
drivers/hwtracing/intel_th/Makefile new file +3 -0
new file mode 100644
index 0000000000..dfd7906462
@@ -0,0 +1,3 @@
+obj-$(CONFIG_INTEL_TH)		+= intel_th.o
+intel_th-y			:= core.o
+intel_th-$(CONFIG_INTEL_TH_DEBUG) += debug.o
drivers/hwtracing/intel_th/core.c new file +692 -0

692 lines not on this page · raw message

drivers/hwtracing/intel_th/debug.c new file +36 -0
new file mode 100644
index 0000000000..8c48d16b5d
@@ -0,0 +1,36 @@
+/*
+ * Intel Trace Hub driver debugging
+ *
+ * Copyright (C) 2014-2015 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#include <linux/types.h>
+#include <linux/device.h>
+#include <linux/debugfs.h>
+
+#include "intel_th.h"
+#include "debug.h"
+
+struct dentry *intel_th_dbg;
+
+void intel_th_debug_init(void)
+{
+	intel_th_dbg = debugfs_create_dir("intel_th", NULL);
+	if (IS_ERR(intel_th_dbg))
+		intel_th_dbg = NULL;
+}
+
+void intel_th_debug_done(void)
+{
+	debugfs_remove(intel_th_dbg);
+	intel_th_dbg = NULL;
+}
drivers/hwtracing/intel_th/debug.h new file +34 -0
new file mode 100644
index 0000000000..4495864310
@@ -0,0 +1,34 @@
+/*
+ * Intel Trace Hub driver debugging
+ *
+ * Copyright (C) 2014-2015 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef __INTEL_TH_DEBUG_H__
+#define __INTEL_TH_DEBUG_H__
+
+#ifdef CONFIG_INTEL_TH_DEBUG
+extern struct dentry *intel_th_dbg;
+
+void intel_th_debug_init(void);
+void intel_th_debug_done(void);
+#else
+static inline void intel_th_debug_init(void)
+{
+}
+
+static inline void intel_th_debug_done(void)
+{
+}
+#endif
+
+#endif /* __INTEL_TH_DEBUG_H__ */
drivers/hwtracing/intel_th/intel_th.h new file +244 -0

244 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#23 lore raw

PATCH v3 6/11 intel_th: Add pci glue layer for Intel Trace Hub

In reply to Alexander Shishkin

This patch adds basic support for PCI-based Intel TH devices. It requests 2 bars (configuration registers for the subdevices and STH channel MMIO region) and calls into Intel TH core code to create the bus with subdevices etc.

---
 drivers/hwtracing/intel_th/Kconfig  |  9 ++++
 drivers/hwtracing/intel_th/Makefile |  3 ++
 drivers/hwtracing/intel_th/pci.c    | 86 +++++++++++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100644 drivers/hwtracing/intel_th/pci.c
drivers/hwtracing/intel_th/Kconfig +9 -0
index 04e32cca4b..63b6973315 100644
@@ -15,6 +15,15 @@ config INTEL_TH
 
 if INTEL_TH
 
+config INTEL_TH_PCI
+	tristate "Intel Trace Hub PCI controller"
+	depends on PCI
+	help
+	  Intel Trace Hub may exist as a PCI device. This option enables
+	  support glue layer for PCI-based Intel TH.
+
+	  Say Y here to enable PCI Intel TH support.
+
 config INTEL_TH_DEBUG
 	bool "Intel Trace Hub debugging"
 	depends on DEBUG_FS
drivers/hwtracing/intel_th/Makefile +3 -0
index dfd7906462..5c13ed4121 100644
@@ -1,3 +1,6 @@
 obj-$(CONFIG_INTEL_TH)		+= intel_th.o
 intel_th-y			:= core.o
 intel_th-$(CONFIG_INTEL_TH_DEBUG) += debug.o
+
+obj-$(CONFIG_INTEL_TH_PCI)	+= intel_th_pci.o
+intel_th_pci-y			:= pci.o
drivers/hwtracing/intel_th/pci.c new file +86 -0

86 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#25 lore raw

PATCH v3 8/11 intel_th: Add Software Trace Hub driver

In reply to Alexander Shishkin

Software Trace Hub (STH) is a trace source device in the Intel TH architecture, it generates data that then goes through the switch into one or several output ports.

STH collects data from software sources using the stm device class abstraction.

---
 drivers/hwtracing/intel_th/Kconfig  |  10 ++
 drivers/hwtracing/intel_th/Makefile |   3 +
 drivers/hwtracing/intel_th/sth.c    | 259 ++++++++++++++++++++++++++++++++++++
 drivers/hwtracing/intel_th/sth.h    |  42 ++++++
 4 files changed, 314 insertions(+)
 create mode 100644 drivers/hwtracing/intel_th/sth.c
 create mode 100644 drivers/hwtracing/intel_th/sth.h
drivers/hwtracing/intel_th/Kconfig +10 -0
index 981ac3c958..cd981f8380 100644
@@ -34,6 +34,16 @@ config INTEL_TH_GTH
 
 	  Say Y here to enable GTH subdevice of Intel Trace Hub.
 
+config INTEL_TH_STH
+	tristate "Intel Trace Hub Software Trace Hub support"
+	depends on STM
+	help
+	  Software Trace Hub (STH) enables trace data from software
+	  trace sources to be sent out via Intel Trace Hub. It
+	  uses stm class device to interface with its sources.
+
+	  Say Y here to enable STH subdevice of Intel Trace Hub.
+
 config INTEL_TH_DEBUG
 	bool "Intel Trace Hub debugging"
 	depends on DEBUG_FS
drivers/hwtracing/intel_th/Makefile +3 -0
index 8608c972a6..8c45e9fa6f 100644
@@ -7,3 +7,6 @@ intel_th_pci-y			:= pci.o
 
 obj-$(CONFIG_INTEL_TH_GTH)	+= intel_th_gth.o
 intel_th_gth-y			:= gth.o
+
+obj-$(CONFIG_INTEL_TH_STH)	+= intel_th_sth.o
+intel_th_sth-y			:= sth.o
drivers/hwtracing/intel_th/sth.c new file +259 -0

259 lines not on this page · raw message

drivers/hwtracing/intel_th/sth.h new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#27 lore raw

PATCH v3 10/11 intel_th: Add PTI output driver

In reply to Alexander Shishkin

Parallel Trace Interface (PTI) unit is a trace output device that sends data over a PTI port.

The driver provides interfaces to configure bus width, bus clock divider and mode. Tracing is enabled via output device's "active" attribute.

---
 .../ABI/testing/sysfs-bus-intel_th-devices-pti     |  24 ++
 drivers/hwtracing/intel_th/Kconfig                 |   9 +
 drivers/hwtracing/intel_th/Makefile                |   3 +
 drivers/hwtracing/intel_th/pti.c                   | 252 +++++++++++++++++++++
 drivers/hwtracing/intel_th/pti.h                   |  29 +++
 5 files changed, 317 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-devices-pti
 create mode 100644 drivers/hwtracing/intel_th/pti.c
 create mode 100644 drivers/hwtracing/intel_th/pti.h
Documentation/ABI/testing/sysfs-bus-intel_th-devices-pti new file +24 -0
new file mode 100644
index 0000000000..df0b24fd02
@@ -0,0 +1,24 @@
+What:		/sys/bus/intel_th/devices/<intel_th_id>-pti/mode
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Configure PTI output width. Currently supported values
+		are 4, 8, 12, 16.
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-pti/freerunning_clock
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) 0: PTI trace clock acts as a strobe which only toggles
+		when there is trace data to send. 1: PTI trace clock is a
+		free-running clock.
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-pti/clock_divider
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Configure PTI port clock divider:
+		 - 0: Intel TH clock rate,
+		 - 1: 1/2 Intel TH clock rate,
+		 - 2: 1/4 Intel TH clock rate,
+		 - 3: 1/8 Intel TH clock rate.
drivers/hwtracing/intel_th/Kconfig +9 -0
index e3de779ff5..9c9a82287c 100644
@@ -54,6 +54,15 @@ config INTEL_TH_MSU
 
 	  Say Y here to enable MSU output device for Intel TH.
 
+config INTEL_TH_PTI
+	tristate "Intel Trace Hub PTI output"
+	help
+	  Parallel Trace Interface unit (PTI) is a trace output device
+	  of Intel TH architecture that facilitates STP trace output via
+	  a PTI port.
+
+	  Say Y to enable PTI output of Intel TH data.
+
 config INTEL_TH_DEBUG
 	bool "Intel Trace Hub debugging"
 	depends on DEBUG_FS
drivers/hwtracing/intel_th/Makefile +3 -0
index 4636757df2..81d42fe918 100644
@@ -13,3 +13,6 @@ intel_th_sth-y			:= sth.o
 
 obj-$(CONFIG_INTEL_TH_MSU)	+= intel_th_msu.o
 intel_th_msu-y			:= msu.o
+
+obj-$(CONFIG_INTEL_TH_PTI)	+= intel_th_pti.o
+intel_th_pti-y			:= pti.o
drivers/hwtracing/intel_th/pti.c new file +252 -0

252 lines not on this page · raw message

drivers/hwtracing/intel_th/pti.h new file +29 -0
new file mode 100644
index 0000000000..3084064731
@@ -0,0 +1,29 @@
+/*
+ * Intel Trace Hub PTI output data structures
+ *
+ * Copyright (C) 2014-2015 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef __INTEL_TH_STH_H__
+#define __INTEL_TH_STH_H__
+
+enum {
+	REG_PTI_CTL	= 0x1c00,
+};
+
+#define PTI_EN		BIT(0)
+#define PTI_FCEN	BIT(1)
+#define PTI_MODE	0xf0
+#define PTI_CLKDIV	0x000f0000
+#define PTI_PATGENMODE	0x00f00000
+
+#endif /* __INTEL_TH_STH_H__ */
Signature · 1 line
2.1.4
author maintainer
#26 lore raw

PATCH v3 9/11 intel_th: Add Memory Storage Unit driver

In reply to Alexander Shishkin

Memory Storage Unit (MSU) is a trace output device that collects trace data to system memory. It consists of 2 independent Memory Storage Controllers (MSCs).

This driver provides userspace interfaces to configure in-memory tracing parameters, such as contiguous (high-order allocation) buffer or multiblock (scatter list) buffer mode, wrapping (data overwrite) and number and sizes of windows in multiblock mode. Userspace can read the buffers via mmap()ing or read()ing of the corresponding device node.

---
 .../ABI/testing/sysfs-bus-intel_th-devices-msc     |   33 +
 drivers/hwtracing/intel_th/Kconfig                 |   10 +
 drivers/hwtracing/intel_th/Makefile                |    3 +
 drivers/hwtracing/intel_th/msu.c                   | 1509 ++++++++++++++++++++
 drivers/hwtracing/intel_th/msu.h                   |  116 ++
 5 files changed, 1671 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-devices-msc
 create mode 100644 drivers/hwtracing/intel_th/msu.c
 create mode 100644 drivers/hwtracing/intel_th/msu.h
Documentation/ABI/testing/sysfs-bus-intel_th-devices-msc new file +33 -0
new file mode 100644
index 0000000000..b940c5d91c
@@ -0,0 +1,33 @@
+What:		/sys/bus/intel_th/devices/<intel_th_id>-msc<msc-id>/wrap
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Configure MSC buffer wrapping. 1 == wrapping enabled.
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-msc<msc-id>/mode
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Configure MSC operating mode:
+		  - "single", for contiguous buffer mode (high-order alloc);
+		  - "multi", for multiblock mode;
+		  - "ExI", for DCI handler mode;
+		  - "debug", for debug mode.
+		If operating mode changes, existing buffer is deallocated,
+		provided there are no active users and tracing is not enabled,
+		otherwise the write will fail.
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-msc<msc-id>/nr_pages
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Configure MSC buffer size for "single" or "multi" modes.
+		In single mode, this is a single number of pages, has to be
+		power of 2. In multiblock mode, this is a comma-separated list
+		of numbers of pages for each window to be allocated. Number of
+		windows is not limited.
+		Writing to this file deallocates existing buffer (provided
+		there are no active users and tracing is not enabled) and then
+		allocates a new one.
+
+
drivers/hwtracing/intel_th/Kconfig +10 -0
index cd981f8380..e3de779ff5 100644
@@ -44,6 +44,16 @@ config INTEL_TH_STH
 
 	  Say Y here to enable STH subdevice of Intel Trace Hub.
 
+config INTEL_TH_MSU
+	tristate "Intel Trace Hub Memory Storage Unit"
+	help
+	  Memory Storage Unit (MSU) trace output device enables
+	  storing STP traces to system memory. It supports single
+	  and multiblock modes of operation and provides read()
+	  and mmap() access to the collected data.
+
+	  Say Y here to enable MSU output device for Intel TH.
+
 config INTEL_TH_DEBUG
 	bool "Intel Trace Hub debugging"
 	depends on DEBUG_FS
drivers/hwtracing/intel_th/Makefile +3 -0
index 8c45e9fa6f..4636757df2 100644
@@ -10,3 +10,6 @@ intel_th_gth-y			:= gth.o
 
 obj-$(CONFIG_INTEL_TH_STH)	+= intel_th_sth.o
 intel_th_sth-y			:= sth.o
+
+obj-$(CONFIG_INTEL_TH_MSU)	+= intel_th_msu.o
+intel_th_msu-y			:= msu.o
drivers/hwtracing/intel_th/msu.c new file +1509 -0

1,509 lines not on this page · raw message

drivers/hwtracing/intel_th/msu.h new file +116 -0

116 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#24 lore raw

PATCH v3 7/11 intel_th: Add Global Trace Hub driver

In reply to Alexander Shishkin

Global Trace Hub (GTH) is the central component of Intel TH architecture; it carries out switching between the trace sources and trace outputs, can enable/disable tracing, perform STP encoding, internal buffering, control backpressure from outputs to sources and so on.

This property is also reflected in the software model; GTH (switch) driver is required for the other subdevices to probe, because it matches trace output devices against its output ports and configures them accordingly.

It also implements an interface for output ports to request trace enabling or disabling and a few other useful things.

For userspace, it provides an attribute group "masters", which allows configuration of per-master trace output destinations for up to master 255 and "256+" meaning "masters 256 and above". It also provides an attribute group to discover and configure some of the parameters of its output ports, called "outputs". Via these the user can set up data retention policy for an individual output port or check if it is in reset state.

---
 .../ABI/testing/sysfs-bus-intel_th-devices-gth     |  49 ++
 drivers/hwtracing/intel_th/Kconfig                 |  10 +
 drivers/hwtracing/intel_th/Makefile                |   3 +
 drivers/hwtracing/intel_th/gth.c                   | 706 +++++++++++++++++++++
 drivers/hwtracing/intel_th/gth.h                   |  66 ++
 5 files changed, 834 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-intel_th-devices-gth
 create mode 100644 drivers/hwtracing/intel_th/gth.c
 create mode 100644 drivers/hwtracing/intel_th/gth.h
Documentation/ABI/testing/sysfs-bus-intel_th-devices-gth new file +49 -0
new file mode 100644
index 0000000000..22d0843849
@@ -0,0 +1,49 @@
+What:		/sys/bus/intel_th/devices/<intel_th_id>-gth/masters/*
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Configure output ports for STP masters. Writing -1
+		disables a master; any
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-gth/outputs/[0-7]_port
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RO) Output port type:
+		  0: not present,
+		  1: MSU (Memory Storage Unit)
+		  2: CTP (Common Trace Port)
+		  4: PTI (MIPI PTI).
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-gth/outputs/[0-7]_drop
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Data retention policy setting: keep (0) or drop (1)
+		incoming data while output port is in reset.
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-gth/outputs/[0-7]_null
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) STP NULL packet generation: enabled (1) or disabled (0).
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-gth/outputs/[0-7]_flush
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) Force flush data from byte packing buffer for the output
+		port.
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-gth/outputs/[0-7]_reset
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RO) Output port is in reset (1).
+
+What:		/sys/bus/intel_th/devices/<intel_th_id>-gth/outputs/[0-7]_smcfreq
+Date:		June 2015
+KernelVersion:	4.3
+Contact:	Alexander Shishkin <[email protected]>
+Description:	(RW) STP sync packet frequency for the port. Specifies the
+		number of clocks between mainenance packets.
drivers/hwtracing/intel_th/Kconfig +10 -0
index 63b6973315..981ac3c958 100644
@@ -24,6 +24,16 @@ config INTEL_TH_PCI
 
 	  Say Y here to enable PCI Intel TH support.
 
+config INTEL_TH_GTH
+	tristate "Intel Trace Hub Global Trace Hub"
+	help
+	  Global Trace Hub (GTH) is the central component of the
+	  Intel TH infrastructure and acts as a switch for source
+	  and output devices. This driver is required for other
+	  Intel TH subdevices to initialize.
+
+	  Say Y here to enable GTH subdevice of Intel Trace Hub.
+
 config INTEL_TH_DEBUG
 	bool "Intel Trace Hub debugging"
 	depends on DEBUG_FS
drivers/hwtracing/intel_th/Makefile +3 -0
index 5c13ed4121..8608c972a6 100644
@@ -4,3 +4,6 @@ intel_th-$(CONFIG_INTEL_TH_DEBUG) += debug.o
 
 obj-$(CONFIG_INTEL_TH_PCI)	+= intel_th_pci.o
 intel_th_pci-y			:= pci.o
+
+obj-$(CONFIG_INTEL_TH_GTH)	+= intel_th_gth.o
+intel_th_gth-y			:= gth.o
drivers/hwtracing/intel_th/gth.c new file +706 -0

706 lines not on this page · raw message

drivers/hwtracing/intel_th/gth.h new file +66 -0

66 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#28 lore raw

PATCH v3 11/11 MAINTAINERS: add an entry for Intel(R) Trace Hub

In reply to Alexander Shishkin

Add myself as a maintainer for the Intel(R) Trace Hub framework and drivers.

---
 MAINTAINERS | 6 ++++++
 1 file changed, 6 insertions(+)
MAINTAINERS +6 -0
index 40c2b7dd4a..0053680c4b 100644
@@ -5423,6 +5423,12 @@ F:	Documentation/networking/README.ipw2100
 F:	Documentation/networking/README.ipw2200
 F:	drivers/net/wireless/ipw2x00/
 
+INTEL(R) TRACE HUB
+M:	Alexander Shishkin <[email protected]>
+S:	Supported
+F:	Documentation/trace/intel_th.txt
+F:	drivers/hwtracing/intel_th/
+
 INTEL(R) TRUSTED EXECUTION TECHNOLOGY (TXT)
 M:	Richard L Maliszewski <[email protected]>
 M:	Gang Wei <[email protected]>
Signature · 1 line
2.1.4
#2 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Alexander Shishkin
Quoted 1,844 lines · Alexander Shishkin

A System Trace Module (STM) is a device exporting data in System Trace Protocol (STP) format as defined by MIPI STP standards. Examples of such devices are Intel Trace Hub and Coresight STM.

This abstraction provides a unified interface for software trace sources to send their data over an STM device to a debug host. In order to do that, such a trace source needs to be assigned a pair of master/channel identifiers that all the data from this source will be tagged with. The STP decoder on the debug host side will use these master/channel tags to distinguish different trace streams from one another inside one STP stream.

This abstraction provides a configfs-based policy management mechanism for dynamic allocation of these master/channel pairs based on trace source-supplied string identifier. It has the flexibility of being defined at runtime and at the same time (provided that the policy definition is aligned with the decoding end) consistency.

For userspace trace sources, this abstraction provides write()-based and mmap()-based (if the underlying stm device allows this) output mechanism.

For kernel-side trace sources, we provide "stm_source" device class that can be connected to an stm device at run time.

---
 Documentation/ABI/testing/configfs-stp-policy    |   48 +
 Documentation/ABI/testing/sysfs-class-stm        |   14 +
 Documentation/ABI/testing/sysfs-class-stm_source |   11 +
 Documentation/ioctl/ioctl-number.txt             |    3 +
 Documentation/trace/stm.txt                      |   80 ++
 drivers/Kconfig                                  |    2 +
 drivers/Makefile                                 |    1 +
 drivers/hwtracing/stm/Kconfig                    |    8 +
 drivers/hwtracing/stm/Makefile                   |    3 +
 drivers/hwtracing/stm/core.c                     | 1029 ++++++++++++++++++++++
 drivers/hwtracing/stm/policy.c                   |  529 +++++++++++
 drivers/hwtracing/stm/stm.h                      |   87 ++
 include/linux/stm.h                              |  126 +++
 include/uapi/linux/stm.h                         |   50 ++
 14 files changed, 1991 insertions(+)
 create mode 100644 Documentation/ABI/testing/configfs-stp-policy
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm_source
 create mode 100644 Documentation/trace/stm.txt
 create mode 100644 drivers/hwtracing/stm/Kconfig
 create mode 100644 drivers/hwtracing/stm/Makefile
 create mode 100644 drivers/hwtracing/stm/core.c
 create mode 100644 drivers/hwtracing/stm/policy.c
 create mode 100644 drivers/hwtracing/stm/stm.h
 create mode 100644 include/linux/stm.h
 create mode 100644 include/uapi/linux/stm.h
Documentation/ABI/testing/configfs-stp-policy new file +48 -0
new file mode 100644
index 0000000000..421ce6825c
@@ -0,0 +1,48 @@
+What:          /config/stp-policy
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               This group contains policies mandating Master/Channel allocation
+               for software sources wishing to send trace data over an STM
+               device.
+
+What:          /config/stp-policy/<device>.<policy>
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               This group is the root of a policy; its name is a concatenation
+               of an stm device name to which this policy applies and an
+               arbitrary string. If <device> part doesn't match an existing
+               stm device, mkdir will fail with ENODEV; if that device already
+               has a policy assigned to it, mkdir will fail with EBUSY.
+
+What:          /config/stp-policy/<device>.<policy>/device
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               STM device to which this policy applies, read only. Same as the
+               <device> component of its parent directory.
+
+What:          /config/stp-policy/<device>.<policy>/<node>
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Policy node is a string identifier that software clients will
+               use to request a master/channel to be allocated and assigned to
+               them.
+
+What:          /config/stp-policy/<device>.<policy>/<node>/masters
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Range of masters from which to allocate for users of this node.
+               Write two numbers: the first master and the last master number.
+
+What:          /config/stp-policy/<device>.<policy>/<node>/channels
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Range of channels from which to allocate for users of this node.
+               Write two numbers: the first channel and the last channel
+               number.
+
Documentation/ABI/testing/sysfs-class-stm new file +14 -0
new file mode 100644
index 0000000000..c9aa4f3fc9
@@ -0,0 +1,14 @@
+What:          /sys/class/stm/<stm>/masters
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <[email protected]>
+Description:
+               Shows first and last available to software master numbers on
+               this STM device.
+
+What:          /sys/class/stm/<stm>/channels
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <[email protected]>
+Description:
+               Shows the number of channels per master on this STM device.
Documentation/ABI/testing/sysfs-class-stm_source new file +11 -0
new file mode 100644
index 0000000000..57b8dd39bb
@@ -0,0 +1,11 @@
+What:          /sys/class/stm_source/<stm_source>/stm_source_link
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <[email protected]>
+Description:
+               stm_source device linkage to stm device, where its tracing data
+               is directed. Reads return an existing connection or "<none>" if
+               this stm_source is not connected to any stm device yet.
+               Write an existing (registered) stm device's name here to
+               connect that device. If a device is already connected to this
+               stm_source, it will first be disconnected.
Documentation/ioctl/ioctl-number.txt +3 -0
index 611c52267d..7fa29b4ece 100644
@@ -81,6 +81,9 @@ Code  Seq#(hex)       Include File            Comments
 0x22   all     scsi/sg.h
 '#'    00-3F   IEEE 1394 Subsystem     Block for the entire subsystem
 '$'    00-0F   linux/perf_counter.h, linux/perf_event.h
+'%'    00-0F   include/uapi/linux/stm.h
+                                       System Trace Module subsystem
+                                       <mailto:[email protected]>
 '&'    00-07   drivers/firewire/nosy-user.h
 '1'    00-1F   <linux/timepps.h>       PPS kit from Ulrich Windl
                                        <ftp://ftp.de.kernel.org/pub/linux/daemons/ntp/PPS/>
Documentation/trace/stm.txt new file +80 -0
new file mode 100644
index 0000000000..e140f2e8d7
@@ -0,0 +1,80 @@
+System Trace Module
+===================
+
+System Trace Module (STM) is a device described in MIPI STP specs as
+STP trace stream generator. STP (System Trace Protocol) is a trace
+protocol multiplexing data from multiple trace sources, each one of
+which is assigned a unique pair of master and channel. While some of
+these masters and channels are statically allocated to certain
+hardware trace sources, others are available to software. Software
+trace sources are usually free to pick for themselves any
+master/channel combination from this pool.
+
+On the receiving end of this STP stream (the decoder side), trace
+sources can only be identified by master/channel combination, so in
+order for the decoder to be able to make sense of the trace that
+involves multiple trace sources, it needs to be able to map those
+master/channel pairs to the trace sources that it understands.
+
+For instance, it is helpful to know that syslog messages come on
+master 7 channel 15, while arbitrary user applications can use masters
+48 to 63 and channels 0 to 127.
+
+To solve this mapping problem, stm class provides a policy management
+mechanism via configfs, that allows defining rules that map string
+identifiers to ranges of masters and channels. If these rules (policy)
+are consistent with what decoder expects, it will be able to properly
+process the trace data.
+
+This policy is a tree structure containing rules (policy_node) that
+have a name (string identifier) and a range of masters and channels
+associated with it, located in "stp-policy" subsystem directory in
+configfs. The topmost directory's name (the policy) is formatted as
+the STM device name to which this policy applies and and arbitrary
+string identifier separated by a stop. From the examle above, a rule
+may look like this:
+
+$ ls /config/stp-policy/dummy_stm.my-policy/user
+channels masters
+$ cat /config/stp-policy/dummy_stm.my-policy/user/masters
+48 63
+$ cat /config/stp-policy/dummy_stm.my-policy/user/channels
+0 127
+
+which means that the master allocation pool for this rule consists of
+masters 48 through 63 and channel allocation pool has channels 0
+through 127 in it. Now, any producer (trace source) identifying itself
+with "user" identification string will be allocated a master and
+channel from within these ranges.
+
+These rules can be nested, for example, one can define a rule "dummy"
+under "user" directory from the example above and this new rule will
+be used for trace sources with the id string of "user/dummy".
+
+Trace sources have to open the stm class device's node and write their
+trace data into its file descriptor. In order to identify themselves
+to the policy, they need to do a STP_POLICY_ID_SET ioctl on this file
+descriptor providing their id string. Otherwise, they will be
+automatically allocated a master/channel pair upon first write to this
+file descriptor according to the "default" rule of the policy, if such
+exists.
+
+Some STM devices may allow direct mapping of the channel mmio regions
+to userspace for zero-copy writing. One mappable page (in terms of
+mmu) will usually contain multiple channels' mmios, so the user will
+need to allocate that many channels to themselves (via the
+aforementioned ioctl() call) to be able to do this. That is, if your
+stm device's channel mmio region is 64 bytes and hardware page size is
+4096 bytes, after a successful STP_POLICY_ID_SET ioctl() call with
+width==64, you should be able to mmap() one page on this file
+descriptor and obtain direct access to an mmio region for 64 channels.
+
+For kernel-based trace sources, there is "stm_source" device
+class. Devices of this class can be connected and disconnected to/from
+stm devices at runtime via a sysfs attribute.
+
+Examples of STM devices are Intel Trace Hub [1] and Coresight STM
+[2].
+
+[1] https://software.intel.com/sites/default/files/managed/d3/3c/intel-th-developer-manual.pdf
+[2] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0444b/index.html
drivers/Kconfig +2 -0
index 6e973b8e3a..96efe4522c 100644
@@ -184,4 +184,6 @@ source "drivers/android/Kconfig"
 
 source "drivers/nvdimm/Kconfig"
 
+source "drivers/hwtracing/stm/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index b64b49f6e0..6647bc923f 100644
@@ -164,4 +164,5 @@ obj-$(CONFIG_MCB)           += mcb/
 obj-$(CONFIG_RAS)              += ras/
 obj-$(CONFIG_THUNDERBOLT)      += thunderbolt/
 obj-$(CONFIG_CORESIGHT)                += hwtracing/coresight/
+obj-$(CONFIG_STM)              += hwtracing/stm/
 obj-$(CONFIG_ANDROID)          += android/
drivers/hwtracing/stm/Kconfig new file +8 -0
new file mode 100644
index 0000000000..90ed327461
@@ -0,0 +1,8 @@
+config STM
+       tristate "System Trace Module devices"
+       help
+         A System Trace Module (STM) is a device exporting data in System
+         Trace Protocol (STP) format as defined by MIPI STP standards.
+         Examples of such devices are Intel Trace Hub and Coresight STM.
+
+         Say Y here to enable System Trace Module device support.
drivers/hwtracing/stm/Makefile new file +3 -0
new file mode 100644
index 0000000000..adec701649
@@ -0,0 +1,3 @@
+obj-$(CONFIG_STM)      += stm_core.o
+
+stm_core-y             := core.o policy.o
drivers/hwtracing/stm/core.c new file +1029 -0
new file mode 100644
index 0000000000..b79c42c625
@@ -0,0 +1,1029 @@
+/*
+ * System Trace Module (STM) infrastructure
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#include <linux/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/compat.h>
+#include <linux/kdev_t.h>
+#include <linux/srcu.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+#include <linux/fs.h>
+#include <linux/mm.h>
+#include "stm.h"
+
+#include <uapi/linux/stm.h>
+
+static unsigned int stm_core_up;
+
+/*
+ * The SRCU here makes sure that STM device doesn't disappear from under a
+ * stm_source_write() caller, which may want to have as little overhead as
+ * possible.
+ */
+static struct srcu_struct stm_source_srcu;
+
+static ssize_t masters_show(struct device *dev,
+                           struct device_attribute *attr,
+                           char *buf)
+{
+       struct stm_device *stm = to_stm_device(dev);
+       int ret;
+
+       ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
+
+       return ret;
+}
+
+static DEVICE_ATTR_RO(masters);
+
+static ssize_t channels_show(struct device *dev,
+                            struct device_attribute *attr,
+                            char *buf)
+{
+       struct stm_device *stm = to_stm_device(dev);
+       int ret;
+
+       ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
+
+       return ret;
+}
+
+static DEVICE_ATTR_RO(channels);
+
+static struct attribute *stm_attrs[] = {
+       &dev_attr_masters.attr,
+       &dev_attr_channels.attr,
+       NULL,
+};
+
+ATTRIBUTE_GROUPS(stm);
+
+static struct class stm_class = {
+       .name           = "stm",
+       .dev_groups     = stm_groups,
+};
+
+static int stm_dev_match(struct device *dev, const void *data)
+{
+       const char *name = data;
+
+       return sysfs_streq(name, dev_name(dev));
+}
+
+/**
+ * stm_find_device() - find stm device by name
+ * @buf:       character buffer containing the name
+ *
+ * This is called when either policy gets assigned to an stm device or an
+ * stm_source device gets linked to an stm device.
+ *
+ * This grabs device's reference (get_device()) and module reference, both
+ * of which the calling path needs to make sure to drop with stm_put_device().
+ *
+ * Return:     stm device pointer or null if lookup failed.
+ */
+struct stm_device *stm_find_device(const char *buf)
+{
+       struct stm_device *stm;
+       struct device *dev;
+
+       if (!stm_core_up)
+               return NULL;
+
+       dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
+       if (!dev)
+               return NULL;
+
+       stm = to_stm_device(dev);
+       if (!try_module_get(stm->owner)) {
+               put_device(dev);
+               return NULL;
+       }
+
+       return stm;
+}
+
+/**
+ * stm_put_device() - drop references on the stm device
+ * @stm:       stm device, previously acquired by stm_find_device()
+ *
+ * This drops the module reference and device reference taken by
+ * stm_find_device().
+ */
+void stm_put_device(struct stm_device *stm)
+{
+       module_put(stm->owner);
+       put_device(&stm->dev);
+}
+
+/*
+ * Internally we only care about software-writable masters here, that is the
+ * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
+ * original master numbers to be visible externally, since they are the ones
+ * that will appear in the STP stream. Thus, the internal bookkeeping uses
+ * $master - stm_data->sw_start to reference master descriptors and such.
+ */
+
+#define __stm_master(_s, _m)                           \
+       ((_s)->masters[(_m) - (_s)->data->sw_start])
+
+static inline struct stp_master *
+stm_master(struct stm_device *stm, unsigned int idx)
+{
+       if (idx < stm->data->sw_start || idx > stm->data->sw_end)
+               return NULL;
+
+       return __stm_master(stm, idx);
+}
+
+static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
+{
+       struct stp_master *master;
+       size_t size;
+
+       size = ALIGN(stm->data->sw_nchannels, 8) / 8;
+       size += sizeof(struct stp_master);
+       master = kzalloc(size, GFP_ATOMIC);
+       if (!master)
+               return -ENOMEM;
+
+       master->nr_free = stm->data->sw_nchannels;
+       __stm_master(stm, idx) = master;
+
+       return 0;
+}
+
+static void stp_master_free(struct stm_device *stm, unsigned int idx)
+{
+       struct stp_master *master = stm_master(stm, idx);
+
+       if (!master)
+               return;
+
+       __stm_master(stm, idx) = NULL;
+       kfree(master);
+}
+
+static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
+{
+       struct stp_master *master = stm_master(stm, output->master);
+
+       if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
+               return;
+
+       bitmap_allocate_region(&master->chan_map[0], output->channel,
+                              ilog2(output->nr_chans));
+
+       master->nr_free -= output->nr_chans;
+}
+
+static void
+stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
+{
+       struct stp_master *master = stm_master(stm, output->master);
+
+       bitmap_release_region(&master->chan_map[0], output->channel,
+                             ilog2(output->nr_chans));
+
+       output->nr_chans = 0;
+       master->nr_free += output->nr_chans;
+}
+
+/*
+ * This is like bitmap_find_free_region(), except it can ignore @start bits
+ * at the beginning.
+ */
+static int find_free_channels(unsigned long *bitmap, unsigned int start,
+                             unsigned int end, unsigned int width)
+{
+       unsigned int pos;
+       int i;
+
+       for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
+               pos = find_next_zero_bit(bitmap, end + 1, pos);
+               if (pos + width > end + 1)
+                       break;
+
+               if (pos & (width - 1))
+                       continue;
+
+               for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
+                       ;
+               if (i == width)
+                       return pos;
+       }
+
+       return -1;
+}
+
+static unsigned int
+stm_find_master_chan(struct stm_device *stm, unsigned int width,
+                    unsigned int *mstart, unsigned int mend,
+                    unsigned int *cstart, unsigned int cend)
+{
+       struct stp_master *master;
+       unsigned int midx;
+       int pos, err;
+
+       for (midx = *mstart; midx <= mend; midx++) {
+               if (!stm_master(stm, midx)) {
+                       err = stp_master_alloc(stm, midx);
+                       if (err)
+                               return err;
+               }
+
+               master = stm_master(stm, midx);
+
+               if (!master->nr_free)
+                       continue;
+
+               pos = find_free_channels(master->chan_map, *cstart, cend,
+                                        width);
+               if (pos < 0)
+                       continue;
+
+               *mstart = midx;
+               *cstart = pos;
+               return 0;
+       }
+
+       return -ENOSPC;
+}
+
+static int stm_output_assign(struct stm_device *stm, unsigned int width,
+                            struct stp_policy_node *policy_node,
+                            struct stm_output *output)
+{
+       unsigned int midx, cidx, mend, cend;
+       int ret = -EINVAL;
+
+       if (width > stm->data->sw_nchannels)
+               return -EINVAL;
+
+       if (policy_node) {
+               stp_policy_node_get_ranges(policy_node,
+                                          &midx, &mend, &cidx, &cend);
+       } else {
+               midx = stm->data->sw_start;
+               cidx = 0;
+               mend = stm->data->sw_end;
+               cend = stm->data->sw_nchannels - 1;
+       }
+
+       spin_lock(&stm->mc_lock);
+       /* output is already assigned -- shouldn't happen */
+       if (WARN_ON_ONCE(output->nr_chans))
+               goto unlock;
+
+       ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
+       if (ret)
+               goto unlock;
+
+       output->master = midx;
+       output->channel = cidx;
+       output->nr_chans = width;
+       stm_output_claim(stm, output);
+       dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
+
+       ret = 0;
+unlock:
+       spin_unlock(&stm->mc_lock);
+
+       return ret;
+}
+
+static void stm_output_free(struct stm_device *stm, struct stm_output *output)
+{
+       spin_lock(&stm->mc_lock);
+       if (output->nr_chans)
+               stm_output_disclaim(stm, output);
+       spin_unlock(&stm->mc_lock);
+}
+
+static int major_match(struct device *dev, const void *data)
+{
+       unsigned int major = *(unsigned int *)data;
+
+       return MAJOR(dev->devt) == major;
+}
+
+static int stm_char_open(struct inode *inode, struct file *file)
+{
+       struct stm_file *stmf;
+       struct device *dev;
+       unsigned int major = imajor(inode);
+       int err = -ENODEV;
+
+       dev = class_find_device(&stm_class, NULL, &major, major_match);
+       if (!dev)
+               return -ENODEV;
+
+       stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
+       if (!stmf)
+               return -ENOMEM;
+
+       stmf->stm = to_stm_device(dev);
+
+       if (!try_module_get(stmf->stm->owner))
+               goto err_free;
+
+       file->private_data = stmf;
+
+       return nonseekable_open(inode, file);
+
+err_free:
+       kfree(stmf);
+
+       return err;
+}
+
+static int stm_char_release(struct inode *inode, struct file *file)
+{
+       struct stm_file *stmf = file->private_data;
+
+       stm_output_free(stmf->stm, &stmf->output);
+       stm_put_device(stmf->stm);
+       kfree(stmf);
+
+       return 0;
+}
+
+static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
+{
+       struct stm_device *stm = stmf->stm;
+       int ret;
+
+       stmf->policy_node = stp_policy_node_lookup(stm, id);
+
+       ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
+
+       if (stmf->policy_node)
+               stp_policy_node_put(stmf->policy_node);
+
+       return ret;
+}
+
+static void stm_write(struct stm_data *data, unsigned int master,
+                     unsigned int channel, const char *buf, size_t count)
+{
+       unsigned int flags = STP_PACKET_TIMESTAMPED;
+       const unsigned char *p = buf, nil = 0;
+       size_t pos;
+       ssize_t sz;
+
+       for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
+               sz = min_t(unsigned int, count - pos, 8);
+               sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
+                                 sz, p);
+               flags = 0;
+       }
+
+       data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
+}
+
+static ssize_t stm_char_write(struct file *file, const char __user *buf,
+                             size_t count, loff_t *ppos)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_device *stm = stmf->stm;
+       char *kbuf;
+       int err;
+
+       /*
+        * if no m/c have been assigned to this writer up to this
+        * point, use "default" policy entry
+        */
+       if (!stmf->output.nr_chans) {
+               err = stm_file_assign(stmf, "default", 1);
+               /*
+                * EBUSY means that somebody else just assigned this
+                * output, which is just fine for write()
+                */
+               if (err && err != -EBUSY)
+                       return err;
+       }
+
+       kbuf = kmalloc(count + 1, GFP_KERNEL);
+       if (!kbuf)
+               return -ENOMEM;
+
+       err = copy_from_user(kbuf, buf, count);
+       if (err) {
+               kfree(kbuf);
+               return -EFAULT;
+       }
+
+       stm_write(stm->data, stmf->output.master, stmf->output.channel, kbuf,
+                 count);
+
+       kfree(kbuf);
+
+       return count;
+}
+
+static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_device *stm = stmf->stm;
+       unsigned long size, phys;
+
+       if (!stm->data->mmio_addr)
+               return -EOPNOTSUPP;
+
+       if (vma->vm_pgoff)
+               return -EINVAL;
+
+       size = vma->vm_end - vma->vm_start;
+
+       if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
+               return -EINVAL;
+
+       phys = stm->data->mmio_addr(stm->data, stmf->output.master,
+                                   stmf->output.channel,
+                                   stmf->output.nr_chans);
+
+       if (!phys)
+               return -EINVAL;
+
+       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+       vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+       vm_iomap_memory(vma, phys, size);
+
+       return 0;
+}
+
+static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
+{
+       struct stm_device *stm = stmf->stm;
+       struct stp_policy_id *id;
+       int ret = -EINVAL;
+       u32 size;
+
+       if (stmf->output.nr_chans)
+               return -EBUSY;
+
+       if (copy_from_user(&size, arg, sizeof(size)))
+               return -EFAULT;
+
+       if (size >= PATH_MAX + sizeof(*id))
+               return -EINVAL;
+
+       /*
+        * size + 1 to make sure the .id string at the bottom is terminated,
+        * which is also why memdup_user() is not useful here
+        */
+       id = kzalloc(size + 1, GFP_KERNEL);
+       if (!id)
+               return -ENOMEM;
+
+       if (copy_from_user(id, arg, size)) {
+               ret = -EFAULT;
+               goto err_free;
+       }
+
+       if (id->__reserved_0 || id->__reserved_1)
+               goto err_free;
+
+       if (id->width < 1 ||
+           id->width > PAGE_SIZE / stm->data->sw_mmiosz)
+               goto err_free;
+
+       ret = stm_file_assign(stmf, id->id, id->width);
+       if (ret)
+               goto err_free;
+
+       ret = 0;
+
+       if (stm->data->link)
+               ret = stm->data->link(stm->data, stmf->output.master,
+                                     stmf->output.channel);
+
+       if (ret) {
+               stm_output_free(stmf->stm, &stmf->output);
+               stm_put_device(stmf->stm);
+       }
+
+err_free:
+       kfree(id);
+
+       return ret;
+}
+
+static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
+{
+       struct stp_policy_id id = {
+               .size           = sizeof(id),
+               .master         = stmf->output.master,
+               .channel        = stmf->output.channel,
+               .width          = stmf->output.nr_chans,
+               .__reserved_0   = 0,
+               .__reserved_1   = 0,
+       };
+
+       return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
+}
+
+static long
+stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_data *stm_data = stmf->stm->data;
+       int err = -ENOTTY;
+       u64 options;
+
+       switch (cmd) {
+       case STP_POLICY_ID_SET:
+               err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
+               if (err)
+                       return err;
+
+               return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
+
+       case STP_POLICY_ID_GET:
+               return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
+
+       case STP_SET_OPTIONS:
+               if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
+                       return -EFAULT;
+
+               if (stm_data->set_options)
+                       err = stm_data->set_options(stm_data,
+                                                   stmf->output.master,
+                                                   stmf->output.channel,
+                                                   stmf->output.nr_chans,
+                                                   options);
+
+               break;
+       default:
+               break;
+       }
+
+       return err;
+}
+
+#ifdef CONFIG_COMPAT
+static long
+stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
+}
+#else
+#define stm_char_compat_ioctl  NULL
+#endif
+
+static const struct file_operations stm_fops = {
+       .open           = stm_char_open,
+       .release        = stm_char_release,
+       .write          = stm_char_write,
+       .mmap           = stm_char_mmap,
+       .unlocked_ioctl = stm_char_ioctl,
+       .compat_ioctl   = stm_char_compat_ioctl,
+       .llseek         = no_llseek,
+};
+
+static void stm_device_release(struct device *dev)
+{
+       struct stm_device *stm = to_stm_device(dev);
+
+       kfree(stm);
+}
+
+int stm_register_device(struct device *parent, struct stm_data *stm_data,
+                       struct module *owner)
+{
+       struct stm_device *stm;
+       unsigned int nmasters;
+       int err = -ENOMEM;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
+
+       if (!stm_data->packet || !stm_data->sw_nchannels)
+               return -EINVAL;
+
+       nmasters = stm_data->sw_end - stm_data->sw_start;
+       stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
+       if (!stm)
+               return -ENOMEM;
+
+       stm->major = register_chrdev(0, stm_data->name, &stm_fops);
+       if (stm->major < 0)
+               goto err_free;
+
+       device_initialize(&stm->dev);
+       stm->dev.devt = MKDEV(stm->major, 0);
+       stm->dev.class = &stm_class;
+       stm->dev.parent = parent;
+       stm->dev.release = stm_device_release;
+
+       err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
+       if (err)
+               goto err_device;
+
+       err = device_add(&stm->dev);
+       if (err)
+               goto err_device;
+
+       spin_lock_init(&stm->link_lock);
+       INIT_LIST_HEAD(&stm->link_list);
+
+       spin_lock_init(&stm->mc_lock);
+       mutex_init(&stm->policy_mutex);
+       stm->sw_nmasters = nmasters;
+       stm->owner = owner;
+       stm->data = stm_data;
+       stm_data->stm = stm;
+
+       return 0;
+
+err_device:
+       put_device(&stm->dev);
+err_free:
+       kfree(stm);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(stm_register_device);
+
+static void __stm_source_link_drop(struct stm_source_device *src,
+                                  struct stm_device *stm);
+
+void stm_unregister_device(struct stm_data *stm_data)
+{
+       struct stm_device *stm = stm_data->stm;
+       struct stm_source_device *src, *iter;
+       int i;
+
+       spin_lock(&stm->link_lock);
+       list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
+               __stm_source_link_drop(src, stm);
+       }
+       spin_unlock(&stm->link_lock);
+
+       synchronize_srcu(&stm_source_srcu);
+
+       unregister_chrdev(stm->major, stm_data->name);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy)
+               stp_policy_unbind(stm->policy);
+       mutex_unlock(&stm->policy_mutex);
+
+       for (i = 0; i < stm->sw_nmasters; i++)
+               stp_master_free(stm, i);
+
+       device_unregister(&stm->dev);
+       stm_data->stm = NULL;
+}
+EXPORT_SYMBOL_GPL(stm_unregister_device);
+
+/**
+ * stm_source_link_add() - connect an stm_source device to an stm device
+ * @src:       stm_source device
+ * @stm:       stm device
+ *
+ * This function establishes a link from stm_source to an stm device so that
+ * the former can send out trace data to the latter.
+ *
+ * Return:     0 on success, -errno otherwise.
+ */
+static int stm_source_link_add(struct stm_source_device *src,
+                              struct stm_device *stm)
+{
+       char *id;
+       int err;
+
+       spin_lock(&stm->link_lock);
+       spin_lock(&src->link_lock);
+
+       /* src->link is dereferenced under stm_source_srcu but not the list */
+       rcu_assign_pointer(src->link, stm);
+       list_add_tail(&src->link_entry, &stm->link_list);
+
+       spin_unlock(&src->link_lock);
+       spin_unlock(&stm->link_lock);
+
+       id = kstrdup(src->data->name, GFP_KERNEL);
+       if (id) {
+               src->policy_node =
+                       stp_policy_node_lookup(stm, id);
+
+               kfree(id);
+       }
+
+       err = stm_output_assign(stm, src->data->nr_chans,
+                               src->policy_node, &src->output);
+
+       if (src->policy_node)
+               stp_policy_node_put(src->policy_node);
+
+       if (err)
+               goto fail_detach;
+
+       /* this is to notify the STM device that a new link has been made */
+       if (stm->data->link)
+               err = stm->data->link(stm->data, src->output.master,
+                                     src->output.channel);
+
+       if (err)
+               goto fail_free_output;
+
+       /* this is to let the source carry out all necessary preparations */
+       if (src->data->link)
+               src->data->link(src->data);
+
+       return 0;
+
+fail_free_output:
+       stm_output_free(stm, &src->output);
+       stm_put_device(stm);
+
+fail_detach:
+       spin_lock(&stm->link_lock);
+       spin_lock(&src->link_lock);
+
+       rcu_assign_pointer(src->link, NULL);
+       list_del_init(&src->link_entry);
+
+       spin_unlock(&src->link_lock);
+       spin_unlock(&stm->link_lock);
+
+       return err;
+}
+
+/**
+ * __stm_source_link_drop() - detach stm_source from an stm device
+ * @src:       stm_source device
+ * @stm:       stm device
+ *
+ * If @stm is @src::link, disconnect them from one another and put the
+ * reference on the @stm device.
+ *
+ * Caller must hold stm::link_lock.
+ */
+static void __stm_source_link_drop(struct stm_source_device *src,
+                                  struct stm_device *stm)
+{
+       spin_lock(&src->link_lock);
+       if (WARN_ON_ONCE(src->link != stm)) {
+               spin_unlock(&src->link_lock);
+               return;
+       }
+
+       stm_output_free(src->link, &src->output);
+       /* caller must hold stm::link_lock */
+       list_del_init(&src->link_entry);
+       /* matches stm_find_device() from stm_source_link_store() */
+       stm_put_device(src->link);
+       rcu_assign_pointer(src->link, NULL);
+
+       spin_unlock(&src->link_lock);
+}
+
+/**
+ * stm_source_link_drop() - detach stm_source from its stm device
+ * @src:       stm_source device
+ *
+ * Unlinking means disconnecting from source's STM device; after this
+ * writes will be unsuccessful until it is linked to a new STM device.
+ *
+ * This will happen on "stm_source_link" sysfs attribute write to undo
+ * the existing link (if any), or on linked STM device's de-registration.
+ */
+static void stm_source_link_drop(struct stm_source_device *src)
+{
+       struct stm_device *stm;
+       int idx;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+
+       if (stm) {
+               if (src->data->unlink)
+                       src->data->unlink(src->data);
+
+               spin_lock(&stm->link_lock);
+               __stm_source_link_drop(src, stm);
+               spin_unlock(&stm->link_lock);
+       }
+
+       srcu_read_unlock(&stm_source_srcu, idx);
+}
+
+static ssize_t stm_source_link_show(struct device *dev,
+                                   struct device_attribute *attr,
+                                   char *buf)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+       struct stm_device *stm;
+       int idx, ret;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+       ret = sprintf(buf, "%s\n",
+                     stm ? dev_name(&stm->dev) : "<none>");
+       srcu_read_unlock(&stm_source_srcu, idx);
+
+       return ret;
+}
+
+static ssize_t stm_source_link_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+       struct stm_device *link;
+       int err;
+
+       stm_source_link_drop(src);
+
+       link = stm_find_device(buf);
+       if (!link)
+               return -EINVAL;
+
+       err = stm_source_link_add(src, link);
+       if (err)
+               stm_put_device(link);
+
+       return err ? : count;
+}
+
+static DEVICE_ATTR_RW(stm_source_link);
+
+static struct attribute *stm_source_attrs[] = {
+       &dev_attr_stm_source_link.attr,
+       NULL,
+};
+
+ATTRIBUTE_GROUPS(stm_source);
+
+static struct class stm_source_class = {
+       .name           = "stm_source",
+       .dev_groups     = stm_source_groups,
+};
+
+static void stm_source_device_release(struct device *dev)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+
+       kfree(src);
+}
+
+/**
+ * stm_source_register_device() - register an stm_source device
+ * @parent:    parent device
+ * @data:      device description structure
+ *
+ * This will create a device of stm_source class that can write
+ * data to an stm device once linked.
+ *
+ * Return:     0 on success, -errno otherwise.
+ */
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data)
+{
+       struct stm_source_device *src;
+       int err;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
+
+       src = kzalloc(sizeof(*src), GFP_KERNEL);
+       if (!src)
+               return -ENOMEM;
+
+       device_initialize(&src->dev);
+       src->dev.class = &stm_source_class;
+       src->dev.parent = parent;
+       src->dev.release = stm_source_device_release;
+
+       err = kobject_set_name(&src->dev.kobj, "%s", data->name);
+       if (err)
+               goto err;
+
+       err = device_add(&src->dev);
+       if (err)
+               goto err;
+
+       spin_lock_init(&src->link_lock);
+       INIT_LIST_HEAD(&src->link_entry);
+       src->data = data;
+       data->src = src;
+
+       return 0;
+
+err:
+       put_device(&src->dev);
+       kfree(src);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(stm_source_register_device);
+
+/**
+ * stm_source_unregister_device() - unregister an stm_source device
+ * @data:      device description that was used to register the device
+ *
+ * This will remove a previously created stm_source device from the system.
+ */
+void stm_source_unregister_device(struct stm_source_data *data)
+{
+       struct stm_source_device *src = data->src;
+
+       stm_source_link_drop(src);
+
+       device_destroy(&stm_source_class, src->dev.devt);
+}
+EXPORT_SYMBOL_GPL(stm_source_unregister_device);
+
+int stm_source_write(struct stm_source_data *data, unsigned int chan,
+                    const char *buf, size_t count)
+{
+       struct stm_source_device *src = data->src;
+       struct stm_device *stm;
+       int idx;
+
+       if (!src->output.nr_chans)
+               return -ENODEV;
+
+       if (chan >= src->output.nr_chans)
+               return -EINVAL;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+       if (stm)
+               stm_write(stm->data, src->output.master,
+                         src->output.channel + chan,
+                         buf, count);
+       else
+               count = -ENODEV;
+
+       srcu_read_unlock(&stm_source_srcu, idx);
+
+       return count;
+}
+EXPORT_SYMBOL_GPL(stm_source_write);
+
+static int __init stm_core_init(void)
+{
+       int err;
+
+       err = class_register(&stm_class);
+       if (err)
+               return err;
+
+       err = class_register(&stm_source_class);
+       if (err)
+               goto err_stm;
+
+       err = stp_configfs_init();
+       if (err)
+               goto err_src;
+
+       init_srcu_struct(&stm_source_srcu);
+
+       stm_core_up++;
+
+       return 0;
+
+err_src:
+       class_unregister(&stm_source_class);
+err_stm:
+       class_unregister(&stm_class);
+
+       return err;
+}
+
+module_init(stm_core_init);
+
+static void __exit stm_core_exit(void)
+{
+       cleanup_srcu_struct(&stm_source_srcu);
+       class_unregister(&stm_source_class);
+       class_unregister(&stm_class);
+       stp_configfs_exit();
+}
+
+module_exit(stm_core_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("System Trace Module device class");
+MODULE_AUTHOR("Alexander Shishkin <[email protected]>");
drivers/hwtracing/stm/policy.c new file +513 -0
new file mode 100644
index 0000000000..6498a9dbb7
@@ -0,0 +1,529 @@
+/*
+ * System Trace Module (STM) master/channel allocation policy management
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * A master/channel allocation policy allows mapping string identifiers to
+ * master and channel ranges, where allocation can be done.
+ */
+
+#define pr_fmt(fmt)    KBUILD_MODNAME ": " fmt
+
+#include <linux/types.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/configfs.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+#include "stm.h"
+
+/*
+ * STP Master/Channel allocation policy configfs layout.
+ */
+
+struct stp_policy {
+       struct config_group     group;
+       struct stm_device       *stm;
+};
+
+struct stp_policy_node {
+       struct config_group     group;
+       struct stp_policy       *policy;
+       unsigned int            first_master;
+       unsigned int            last_master;
+       unsigned int            first_channel;
+       unsigned int            last_channel;
+};
+
+static struct configfs_subsystem stp_policy_subsys;
+
+void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
+                               unsigned int *mstart, unsigned int *mend,
+                               unsigned int *cstart, unsigned int *cend)
+{
+       *mstart = policy_node->first_master;
+       *mend   = policy_node->last_master;
+       *cstart = policy_node->first_channel;
+       *cend   = policy_node->last_channel;
+}
+
+static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
+{
+       return policy_node->group.cg_item.ci_name ? : "<none>";
+}
+
+static inline struct stp_policy *to_stp_policy(struct config_item *item)
+{
+       return item ?
+               container_of(to_config_group(item), struct stp_policy, group) :
+               NULL;
+}
+
+static inline struct stp_policy_node *
+to_stp_policy_node(struct config_item *item)
+{
+       return item ?
+               container_of(to_config_group(item), struct stp_policy_node,
+                            group) :
+               NULL;
+}
+
+static ssize_t stp_policy_node_masters_show(struct stp_policy_node *policy_node,
+                                           char *page)
+{
+       ssize_t count;
+
+       count = sprintf(page, "%u %u\n", policy_node->first_master,
+                       policy_node->last_master);
+
+       return count;
+}
+
+static ssize_t
+stp_policy_node_masters_store(struct stp_policy_node *policy_node,
+                             const char *page, size_t count)
+{
+       unsigned int first, last;
+       struct stm_device *stm;
+       char *p = (char *)page;
+       ssize_t ret = -ENODEV;
+
+       if (sscanf(p, "%u %u", &first, &last) != 2)
+               return -EINVAL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+       stm = policy_node->policy->stm;
+       if (!stm)
+               goto unlock;
+
+       /* must be within [sw_start..sw_end], which is an inclusive range */
+       if (first > INT_MAX || last > INT_MAX || first > last ||
+           first < stm->data->sw_start ||
+           last > stm->data->sw_end) {
+               ret = -ERANGE;
+               goto unlock;
+       }
+
+       ret = count;
+       policy_node->first_master = first;
+       policy_node->last_master = last;
+
+unlock:
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return ret;
+}
+
+static ssize_t
+stp_policy_node_channels_show(struct stp_policy_node *policy_node, char *page)
+{
+       ssize_t count;
+
+       count = sprintf(page, "%u %u\n", policy_node->first_channel,
+                       policy_node->last_channel);
+
+       return count;
+}
+
+static ssize_t
+stp_policy_node_channels_store(struct stp_policy_node *policy_node,
+                              const char *page, size_t count)
+{
+       unsigned int first, last;
+       struct stm_device *stm;
+       char *p = (char *)page;
+       ssize_t ret = -ENODEV;
+
+       if (sscanf(p, "%u %u", &first, &last) != 2)
+               return -EINVAL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+       stm = policy_node->policy->stm;
+       if (!stm)
+               goto unlock;
+
+       if (first > INT_MAX || last > INT_MAX || first > last ||
+           last >= stm->data->sw_nchannels) {
+               ret = -ERANGE;
+               goto unlock;
+       }
+
+       ret = count;
+       policy_node->first_channel = first;
+       policy_node->last_channel = last;
+
+unlock:
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return ret;
+}
+
+static void stp_policy_node_release(struct config_item *item)
+{
+       kfree(to_stp_policy_node(item));
+}
+
+struct stp_policy_node_attribute {
+       struct configfs_attribute       attr;
+       ssize_t (*show)(struct stp_policy_node *, char *);
+       ssize_t (*store)(struct stp_policy_node *, const char *, size_t);
+};
+
+static ssize_t stp_policy_node_attr_show(struct config_item *item,
+                                        struct configfs_attribute *attr,
+                                        char *page)
+{
+       struct stp_policy_node *policy_node = to_stp_policy_node(item);
+       struct stp_policy_node_attribute *pn_attr =
+               container_of(attr, struct stp_policy_node_attribute, attr);
+       ssize_t count = 0;
+
+       if (pn_attr->show)
+               count = pn_attr->show(policy_node, page);
+
+       return count;
+}
+
+static ssize_t stp_policy_node_attr_store(struct config_item *item,
+                                         struct configfs_attribute *attr,
+                                         const char *page, size_t len)
+{
+       struct stp_policy_node *policy_node = to_stp_policy_node(item);
+       struct stp_policy_node_attribute *pn_attr =
+               container_of(attr, struct stp_policy_node_attribute, attr);
+       ssize_t count = -EINVAL;
+
+       if (pn_attr->store)
+               count = pn_attr->store(policy_node, page, len);
+
+       return count;
+}
+
+static struct configfs_item_operations stp_policy_node_item_ops = {
+       .release                = stp_policy_node_release,
+       .show_attribute         = stp_policy_node_attr_show,
+       .store_attribute        = stp_policy_node_attr_store,
+};
+
+static struct stp_policy_node_attribute stp_policy_node_attr_range = {
+       .attr   = {
+               .ca_owner = THIS_MODULE,
+               .ca_name = "masters",
+               .ca_mode = S_IRUGO | S_IWUSR,
+       },
+       .show   = stp_policy_node_masters_show,
+       .store  = stp_policy_node_masters_store,
+};
+
+static struct stp_policy_node_attribute stp_policy_node_attr_channels = {
+       .attr   = {
+               .ca_owner = THIS_MODULE,
+               .ca_name = "channels",
+               .ca_mode = S_IRUGO | S_IWUSR,
+       },
+       .show   = stp_policy_node_channels_show,
+       .store  = stp_policy_node_channels_store,
+};
+
+static struct configfs_attribute *stp_policy_node_attrs[] = {
+       &stp_policy_node_attr_range.attr,
+       &stp_policy_node_attr_channels.attr,
+       NULL,
+};
+
+static struct config_item_type stp_policy_type;
+static struct config_item_type stp_policy_node_type;
+
+static struct config_group *
+stp_policy_node_make(struct config_group *group, const char *name)
+{
+       struct stp_policy_node *policy_node, *parent_node;
+       struct stp_policy *policy;
+
+       if (group->cg_item.ci_type == &stp_policy_type) {
+               policy = container_of(group, struct stp_policy, group);
+       } else {
+               parent_node = container_of(group, struct stp_policy_node,
+                                          group);
+               policy = parent_node->policy;
+       }
+
+       if (!policy->stm)
+               return ERR_PTR(-ENODEV);
+
+       policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
+       if (!policy_node)
+               return ERR_PTR(-ENOMEM);
+
+       config_group_init_type_name(&policy_node->group, name,
+                                   &stp_policy_node_type);
+
+       policy_node->policy = policy;
+
+       /* default values for the attributes */
+       policy_node->first_master = policy->stm->data->sw_start;
+       policy_node->last_master = policy->stm->data->sw_end;
+       policy_node->first_channel = 0;
+       policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
+
+       return &policy_node->group;
+}
+
+static void
+stp_policy_node_drop(struct config_group *group, struct config_item *item)
+{
+       config_item_put(item);
+}
+
+static struct configfs_group_operations stp_policy_node_group_ops = {
+       .make_group     = stp_policy_node_make,
+       .drop_item      = stp_policy_node_drop,
+};
+
+static struct config_item_type stp_policy_node_type = {
+       .ct_item_ops    = &stp_policy_node_item_ops,
+       .ct_group_ops   = &stp_policy_node_group_ops,
+       .ct_attrs       = stp_policy_node_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+/*
+ * Root group: policies.
+ */
+static struct configfs_attribute stp_policy_attr_device = {
+       .ca_owner = THIS_MODULE,
+       .ca_name = "device",
+       .ca_mode = S_IRUGO,
+};
+
+static struct configfs_attribute *stp_policy_attrs[] = {
+       &stp_policy_attr_device,
+       NULL,
+};
+
+static ssize_t stp_policy_attr_show(struct config_item *item,
+                                   struct configfs_attribute *attr,
+                                   char *page)
+{
+       struct stp_policy *policy = to_stp_policy(item);
+       ssize_t count;
+
+       count = sprintf(page, "%s\n",
+                       (policy && policy->stm) ?
+                       policy->stm->data->name :
+                       "<none>");
+
+       return count;
+}
+
+void stp_policy_unbind(struct stp_policy *policy)
+{
+       struct stm_device *stm = policy->stm;
+
+       if (WARN_ON_ONCE(!policy->stm))
+               return;
+
+       mutex_lock(&stm->policy_mutex);
+       stm->policy = NULL;
+       mutex_unlock(&stm->policy_mutex);
+
+       policy->stm = NULL;
+
+       stm_put_device(stm);
+}
+
+static void stp_policy_release(struct config_item *item)
+{
+       struct stp_policy *policy = to_stp_policy(item);
+
+       stp_policy_unbind(policy);
+       kfree(policy);
+}
+
+static struct configfs_item_operations stp_policy_item_ops = {
+       .release                = stp_policy_release,
+       .show_attribute         = stp_policy_attr_show,
+};
+
+static struct configfs_group_operations stp_policy_group_ops = {
+       .make_group     = stp_policy_node_make,
+};
+
+static struct config_item_type stp_policy_type = {
+       .ct_item_ops    = &stp_policy_item_ops,
+       .ct_group_ops   = &stp_policy_group_ops,
+       .ct_attrs       = stp_policy_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct config_group *
+stp_policies_make(struct config_group *group, const char *name)
+{
+       struct config_group *ret;
+       struct stm_device *stm;
+       char *devname, *p;
+
+       devname = kasprintf(GFP_KERNEL, "%s", name);
+       if (!devname)
+               return ERR_PTR(-ENOMEM);
+
+       /*
+        * node must look like <device_name>.<policy_name>, where
+        * <device_name> is the name of an existing stm device and
+        * <policy_name> is an arbitrary string
+        */
+       p = strchr(devname, '.');
+       if (!p) {
+               kfree(devname);
+               return ERR_PTR(-EINVAL);
+       }
+
+       *p++ = '\0';
+
+       stm = stm_find_device(devname);
+       kfree(devname);
+
+       if (!stm)
+               return ERR_PTR(-ENODEV);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy) {
+               ret = ERR_PTR(-EBUSY);
+               goto unlock_policy;
+       }
+
+       stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
+       if (!stm->policy) {
+               ret = ERR_PTR(-ENOMEM);
+               goto unlock_policy;
+       }
+
+       config_group_init_type_name(&stm->policy->group, name,
+                                   &stp_policy_type);
+       stm->policy->stm = stm;
+
+       ret = &stm->policy->group;
+
+unlock_policy:
+       mutex_unlock(&stm->policy_mutex);
+
+       if (IS_ERR(ret))
+               stm_put_device(stm);
+
+       return ret;
+}
+
+static struct configfs_group_operations stp_policies_group_ops = {
+       .make_group     = stp_policies_make,
+};
+
+static struct config_item_type stp_policies_type = {
+       .ct_group_ops   = &stp_policies_group_ops,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct configfs_subsystem stp_policy_subsys = {
+       .su_group = {
+               .cg_item = {
+                       .ci_namebuf     = "stp-policy",
+                       .ci_type        = &stp_policies_type,
+               },
+       },
+};
+
+/*
+ * Lock the policy mutex from the outside
+ */
+static struct stp_policy_node *
+__stp_policy_node_lookup(struct stp_policy *policy, char *s)
+{
+       struct stp_policy_node *policy_node, *ret;
+       struct list_head *head = &policy->group.cg_children;
+       struct config_item *item;
+       char *start, *end = s;
+
+       if (list_empty(head))
+               return NULL;
+
+       /* return the first entry if everything else fails */
+       item = list_entry(head->next, struct config_item, ci_entry);
+       ret = to_stp_policy_node(item);
+
+next:
+       for (;;) {
+               start = strsep(&end, "/");
+               if (!start)
+                       break;
+
+               if (!*start)
+                       continue;
+
+               list_for_each_entry(item, head, ci_entry) {
+                       policy_node = to_stp_policy_node(item);
+
+                       if (!strcmp(start,
+                                   policy_node->group.cg_item.ci_name)) {
+                               ret = policy_node;
+
+                               if (!end)
+                                       goto out;
+
+                               head = &policy_node->group.cg_children;
+                               goto next;
+                       }
+               }
+               break;
+       }
+
+out:
+       return ret;
+}
+
+
+struct stp_policy_node *
+stp_policy_node_lookup(struct stm_device *stm, char *s)
+{
+       struct stp_policy_node *policy_node = NULL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy)
+               policy_node = __stp_policy_node_lookup(stm->policy, s);
+       mutex_unlock(&stm->policy_mutex);
+
+       if (policy_node)
+               config_item_get(&policy_node->group.cg_item);
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return policy_node;
+}
+
+void stp_policy_node_put(struct stp_policy_node *policy_node)
+{
+       config_item_put(&policy_node->group.cg_item);
+}

drivers/hwtracing/stm/policy.c line 523

Quoted 10 lines
drivers/hwtracing/stm/policy.c new file +10 -0
+
+int __init stp_configfs_init(void)
+{
+       int err;
+
+       config_group_init(&stp_policy_subsys.su_group);
+       mutex_init(&stp_policy_subsys.su_mutex);
+       err = configfs_register_subsystem(&stp_policy_subsys);
+
+       return err;

One small suggestion, is it better like this:
return configfs_register_subsystem(&stp_policy_subsys); As such, we don't need the local variable "err" any more.

Quoted 295 lines
drivers/hwtracing/stm/policy.c new file +6 -0
+}
+
+void __exit stp_configfs_exit(void)
+{
+       configfs_unregister_subsystem(&stp_policy_subsys);
+}
drivers/hwtracing/stm/stm.h new file +87 -0
new file mode 100644
index 0000000000..cf33bf976a
@@ -0,0 +1,87 @@
+/*
+ * System Trace Module (STM) infrastructure
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#ifndef _STM_STM_H_
+#define _STM_STM_H_
+
+struct stp_policy;
+struct stp_policy_node;
+
+struct stp_policy_node *
+stp_policy_node_lookup(struct stm_device *stm, char *s);
+void stp_policy_node_put(struct stp_policy_node *policy_node);
+void stp_policy_unbind(struct stp_policy *policy);
+
+void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
+                               unsigned int *mstart, unsigned int *mend,
+                               unsigned int *cstart, unsigned int *cend);
+int stp_configfs_init(void);
+void stp_configfs_exit(void);
+
+struct stp_master {
+       unsigned int    nr_free;
+       unsigned long   chan_map[0];
+};
+
+struct stm_device {
+       struct device           dev;
+       struct module           *owner;
+       struct stp_policy       *policy;
+       struct mutex            policy_mutex;
+       int                     major;
+       unsigned int            sw_nmasters;
+       struct stm_data         *data;
+       spinlock_t              link_lock;
+       struct list_head        link_list;
+       /* master allocation */
+       spinlock_t              mc_lock;
+       struct stp_master       *masters[0];
+};
+
+#define to_stm_device(_d)                              \
+       container_of((_d), struct stm_device, dev)
+
+struct stm_output {
+       unsigned int            master;
+       unsigned int            channel;
+       unsigned int            nr_chans;
+};
+
+struct stm_file {
+       struct stm_device       *stm;
+       struct stp_policy_node  *policy_node;
+       struct stm_output       output;
+};
+
+struct stm_device *stm_find_device(const char *name);
+void stm_put_device(struct stm_device *stm);
+
+struct stm_source_device {
+       struct device           dev;
+       struct stm_source_data  *data;
+       spinlock_t              link_lock;
+       struct stm_device       *link;
+       struct list_head        link_entry;
+       /* one output per stm_source device */
+       struct stp_policy_node  *policy_node;
+       struct stm_output       output;
+};
+
+#define to_stm_source_device(_d)                               \
+       container_of((_d), struct stm_source_device, dev)
+
+#endif /* _STM_STM_H_ */
include/linux/stm.h new file +126 -0
new file mode 100644
index 0000000000..9d0083d364
@@ -0,0 +1,126 @@
+/*
+ * System Trace Module (STM) infrastructure apis
+ * Copyright (C) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef _STM_H_
+#define _STM_H_
+
+#include <linux/device.h>
+
+/**
+ * enum stp_packet_type - STP packets that an STM driver sends
+ */
+enum stp_packet_type {
+       STP_PACKET_DATA = 0,
+       STP_PACKET_FLAG,
+       STP_PACKET_USER,
+       STP_PACKET_MERR,
+       STP_PACKET_GERR,
+       STP_PACKET_TRIG,
+       STP_PACKET_XSYNC,
+};
+
+/**
+ * enum stp_packet_flags - STP packet modifiers
+ */
+enum stp_packet_flags {
+       STP_PACKET_MARKED       = 0x1,
+       STP_PACKET_TIMESTAMPED  = 0x2,
+};
+
+struct stp_policy;
+
+struct stm_device;
+
+/**
+ * struct stm_data - STM device description and callbacks
+ * @name:              device name
+ * @stm:               internal structure, only used by stm class code
+ * @sw_start:          first STP master available to software
+ * @sw_end:            last STP master available to software
+ * @sw_nchannels:      number of STP channels per master
+ * @sw_mmiosz:         size of one channel's IO space, for mmap, optional
+ * @packet:            callback that sends an STP packet
+ * @mmio_addr:         mmap callback, optional
+ * @link:              called when a new stm_source gets linked to us, optional
+ * @unlink:            likewise for unlinking, again optional
+ * @set_options:       set device-specific options on a channel
+ *
+ * Fill out this structure before calling stm_register_device() to create
+ * an STM device and stm_unregister_device() to destroy it. It will also be
+ * passed back to @packet(), @mmio_addr(), @link(), @unlink() and @set_options()
+ * callbacks.
+ *
+ * Normally, an STM device will have a range of masters available to software
+ * and the rest being statically assigned to various hardware trace sources.
+ * The former is defined by the the range [@sw_start..@sw_end] of the device
+ * description. That is, the lowest master that can be allocated to software
+ * writers is @sw_start and data from this writer will appear is @sw_start
+ * master in the STP stream.
+ */
+struct stm_data {
+       const char              *name;
+       struct stm_device       *stm;
+       unsigned int            sw_start;
+       unsigned int            sw_end;
+       unsigned int            sw_nchannels;
+       unsigned int            sw_mmiosz;
+       ssize_t                 (*packet)(struct stm_data *, unsigned int,
+                                         unsigned int, unsigned int,
+                                         unsigned int, unsigned int,
+                                         const unsigned char *);
+       phys_addr_t             (*mmio_addr)(struct stm_data *, unsigned int,
+                                            unsigned int, unsigned int);
+       int                     (*link)(struct stm_data *, unsigned int,
+                                       unsigned int);
+       void                    (*unlink)(struct stm_data *, unsigned int,
+                                         unsigned int);
+       long                    (*set_options)(struct stm_data *, unsigned int,
+                                              unsigned int, unsigned int,
+                                              unsigned long);
+};
+
+int stm_register_device(struct device *parent, struct stm_data *stm_data,
+                       struct module *owner);
+void stm_unregister_device(struct stm_data *stm_data);
+
+struct stm_source_device;
+
+/**
+ * struct stm_source_data - STM source device description and callbacks
+ * @name:      device name, will be used for policy lookup
+ * @src:       internal structure, only used by stm class code
+ * @nr_chans:  number of channels to allocate
+ * @link:      called when this source gets linked to an STM device
+ * @unlink:    called when this source is about to get unlinked from its STM
+ *
+ * Fill in this structure before calling stm_source_register_device() to
+ * register a source device. Also pass it to unregister and write calls.
+ */
+struct stm_source_data {
+       const char              *name;
+       struct stm_source_device *src;
+       unsigned int            percpu;
+       unsigned int            nr_chans;
+       int                     (*link)(struct stm_source_data *data);
+       void                    (*unlink)(struct stm_source_data *data);
+};
+
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data);
+void stm_source_unregister_device(struct stm_source_data *data);
+
+int stm_source_write(struct stm_source_data *data, unsigned int chan,
+                    const char *buf, size_t count);
+
+#endif /* _STM_H_ */
include/uapi/linux/stm.h new file +50 -1
new file mode 100644
index 0000000000..626a8d3f63
@@ -0,0 +1,50 @@
+/*
+ * System Trace Module (STM) userspace interfaces
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#ifndef _UAPI_LINUX_STM_H
+#define _UAPI_LINUX_STM_H
+
+#include <linux/types.h>
+
+/**
+ * struct stp_policy_id - identification for the STP policy
+ * @size:      size of the structure including real id[] length
+ * @master:    assigned master
+ * @channel:   first assigned channel
+ * @width:     number of requested channels
+ * @id:                identification string
+ *
+ * User must calculate the total size of the structure and put it into
+ * @size field, fill out the @id and desired @width. In return, kernel
+ * fills out @master, @channel and @width.
+ */
+struct stp_policy_id {
+       __u32           size;
+       __u16           master;
+       __u16           channel;
+       __u16           width;
+       /* padding */
+       __u16           __reserved_0;
+       __u32           __reserved_1;
+       char            id[0];
+};
+
+#define STP_POLICY_ID_SET      _IOWR('%', 0, struct stp_policy_id)
+#define STP_POLICY_ID_GET      _IOR('%', 1, struct stp_policy_id)
+#define STP_SET_OPTIONS                _IOW('%', 2, __u64)
+
+#endif /* _UAPI_LINUX_STM_H */
--

2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author maintainer
#3 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

Quoted 5 lines · Chunyan Zhang
Quoted 5 lines
drivers/hwtracing/stm/policy.c new file +5 -0
+int __init stp_configfs_init(void)
+{
+       int err;
+
+       config_group_init(&stp_policy_subsys.su_group);
Quoted 8 lines

drivers/hwtracing/stm/policy.c line 523

Quoted 4 lines
drivers/hwtracing/stm/policy.c new file +4 -0
+       mutex_init(&stp_policy_subsys.su_mutex);
+       err = configfs_register_subsystem(&stp_policy_subsys);
+
+       return err;

One small suggestion, is it better like this:
return configfs_register_subsystem(&stp_policy_subsys); As such, we don't need the local variable "err" any more.

You are right, err doesn't serve any purpose here.

Thanks,
--
Alex

#30 lore raw
Quoted 9 lines · Alexander Shishkin
Quoted 1 line · Alexander Shishkin

Hi Greg and everybody,

Seems like a polite nudge might be in order. :)

Cheers,
--
Alex

Greg and all,

I have reviewed the portion of Alex's work that pertains to the generic STM interface - in its current form it is generic enough to cater to both Intel and ARM STM blocks. A few modifications will likely be required when upstreaming of the coresight-stm driver but base on our (Alex and I) previous integration exercise, those should be minor.

Thanks,
Mathieu

#4 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Alexander Shishkin
Quoted 1,193 lines · Alexander Shishkin

A System Trace Module (STM) is a device exporting data in System Trace Protocol (STP) format as defined by MIPI STP standards. Examples of such devices are Intel Trace Hub and Coresight STM.

This abstraction provides a unified interface for software trace sources to send their data over an STM device to a debug host. In order to do that, such a trace source needs to be assigned a pair of master/channel identifiers that all the data from this source will be tagged with. The STP decoder on the debug host side will use these master/channel tags to distinguish different trace streams from one another inside one STP stream.

This abstraction provides a configfs-based policy management mechanism for dynamic allocation of these master/channel pairs based on trace source-supplied string identifier. It has the flexibility of being defined at runtime and at the same time (provided that the policy definition is aligned with the decoding end) consistency.

For userspace trace sources, this abstraction provides write()-based and mmap()-based (if the underlying stm device allows this) output mechanism.

For kernel-side trace sources, we provide "stm_source" device class that can be connected to an stm device at run time.

---
 Documentation/ABI/testing/configfs-stp-policy    |   48 +
 Documentation/ABI/testing/sysfs-class-stm        |   14 +
 Documentation/ABI/testing/sysfs-class-stm_source |   11 +
 Documentation/ioctl/ioctl-number.txt             |    3 +
 Documentation/trace/stm.txt                      |   80 ++
 drivers/Kconfig                                  |    2 +
 drivers/Makefile                                 |    1 +
 drivers/hwtracing/stm/Kconfig                    |    8 +
 drivers/hwtracing/stm/Makefile                   |    3 +
 drivers/hwtracing/stm/core.c                     | 1029 ++++++++++++++++++++++
 drivers/hwtracing/stm/policy.c                   |  529 +++++++++++
 drivers/hwtracing/stm/stm.h                      |   87 ++
 include/linux/stm.h                              |  126 +++
 include/uapi/linux/stm.h                         |   50 ++
 14 files changed, 1991 insertions(+)
 create mode 100644 Documentation/ABI/testing/configfs-stp-policy
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm_source
 create mode 100644 Documentation/trace/stm.txt
 create mode 100644 drivers/hwtracing/stm/Kconfig
 create mode 100644 drivers/hwtracing/stm/Makefile
 create mode 100644 drivers/hwtracing/stm/core.c
 create mode 100644 drivers/hwtracing/stm/policy.c
 create mode 100644 drivers/hwtracing/stm/stm.h
 create mode 100644 include/linux/stm.h
 create mode 100644 include/uapi/linux/stm.h
Documentation/ABI/testing/configfs-stp-policy new file +48 -0
new file mode 100644
index 0000000000..421ce6825c
@@ -0,0 +1,48 @@
+What:          /config/stp-policy
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               This group contains policies mandating Master/Channel allocation
+               for software sources wishing to send trace data over an STM
+               device.
+
+What:          /config/stp-policy/<device>.<policy>
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               This group is the root of a policy; its name is a concatenation
+               of an stm device name to which this policy applies and an
+               arbitrary string. If <device> part doesn't match an existing
+               stm device, mkdir will fail with ENODEV; if that device already
+               has a policy assigned to it, mkdir will fail with EBUSY.
+
+What:          /config/stp-policy/<device>.<policy>/device
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               STM device to which this policy applies, read only. Same as the
+               <device> component of its parent directory.
+
+What:          /config/stp-policy/<device>.<policy>/<node>
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Policy node is a string identifier that software clients will
+               use to request a master/channel to be allocated and assigned to
+               them.
+
+What:          /config/stp-policy/<device>.<policy>/<node>/masters
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Range of masters from which to allocate for users of this node.
+               Write two numbers: the first master and the last master number.
+
+What:          /config/stp-policy/<device>.<policy>/<node>/channels
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Range of channels from which to allocate for users of this node.
+               Write two numbers: the first channel and the last channel
+               number.
+
Documentation/ABI/testing/sysfs-class-stm new file +14 -0
new file mode 100644
index 0000000000..c9aa4f3fc9
@@ -0,0 +1,14 @@
+What:          /sys/class/stm/<stm>/masters
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
+Description:
+               Shows first and last available to software master numbers on
+               this STM device.
+
+What:          /sys/class/stm/<stm>/channels
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
+Description:
+               Shows the number of channels per master on this STM device.
Documentation/ABI/testing/sysfs-class-stm_source new file +11 -0
new file mode 100644
index 0000000000..57b8dd39bb
@@ -0,0 +1,11 @@
+What:          /sys/class/stm_source/<stm_source>/stm_source_link
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
+Description:
+               stm_source device linkage to stm device, where its tracing data
+               is directed. Reads return an existing connection or "<none>" if
+               this stm_source is not connected to any stm device yet.
+               Write an existing (registered) stm device's name here to
+               connect that device. If a device is already connected to this
+               stm_source, it will first be disconnected.
Documentation/ioctl/ioctl-number.txt +3 -0
index 611c52267d..7fa29b4ece 100644
@@ -81,6 +81,9 @@ Code  Seq#(hex)       Include File            Comments
 0x22   all     scsi/sg.h
 '#'    00-3F   IEEE 1394 Subsystem     Block for the entire subsystem
 '$'    00-0F   linux/perf_counter.h, linux/perf_event.h
+'%'    00-0F   include/uapi/linux/stm.h
+                                       System Trace Module subsystem
+                                       <mailto:alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
 '&'    00-07   drivers/firewire/nosy-user.h
 '1'    00-1F   <linux/timepps.h>       PPS kit from Ulrich Windl
                                        <ftp://ftp.de.kernel.org/pub/linux/daemons/ntp/PPS/>
Documentation/trace/stm.txt new file +80 -0
new file mode 100644
index 0000000000..e140f2e8d7
@@ -0,0 +1,80 @@
+System Trace Module
+===================
+
+System Trace Module (STM) is a device described in MIPI STP specs as
+STP trace stream generator. STP (System Trace Protocol) is a trace
+protocol multiplexing data from multiple trace sources, each one of
+which is assigned a unique pair of master and channel. While some of
+these masters and channels are statically allocated to certain
+hardware trace sources, others are available to software. Software
+trace sources are usually free to pick for themselves any
+master/channel combination from this pool.
+
+On the receiving end of this STP stream (the decoder side), trace
+sources can only be identified by master/channel combination, so in
+order for the decoder to be able to make sense of the trace that
+involves multiple trace sources, it needs to be able to map those
+master/channel pairs to the trace sources that it understands.
+
+For instance, it is helpful to know that syslog messages come on
+master 7 channel 15, while arbitrary user applications can use masters
+48 to 63 and channels 0 to 127.
+
+To solve this mapping problem, stm class provides a policy management
+mechanism via configfs, that allows defining rules that map string
+identifiers to ranges of masters and channels. If these rules (policy)
+are consistent with what decoder expects, it will be able to properly
+process the trace data.
+
+This policy is a tree structure containing rules (policy_node) that
+have a name (string identifier) and a range of masters and channels
+associated with it, located in "stp-policy" subsystem directory in
+configfs. The topmost directory's name (the policy) is formatted as
+the STM device name to which this policy applies and and arbitrary
+string identifier separated by a stop. From the examle above, a rule
+may look like this:
+
+$ ls /config/stp-policy/dummy_stm.my-policy/user
+channels masters
+$ cat /config/stp-policy/dummy_stm.my-policy/user/masters
+48 63
+$ cat /config/stp-policy/dummy_stm.my-policy/user/channels
+0 127
+
+which means that the master allocation pool for this rule consists of
+masters 48 through 63 and channel allocation pool has channels 0
+through 127 in it. Now, any producer (trace source) identifying itself
+with "user" identification string will be allocated a master and
+channel from within these ranges.
+
+These rules can be nested, for example, one can define a rule "dummy"
+under "user" directory from the example above and this new rule will
+be used for trace sources with the id string of "user/dummy".
+
+Trace sources have to open the stm class device's node and write their
+trace data into its file descriptor. In order to identify themselves
+to the policy, they need to do a STP_POLICY_ID_SET ioctl on this file
+descriptor providing their id string. Otherwise, they will be
+automatically allocated a master/channel pair upon first write to this
+file descriptor according to the "default" rule of the policy, if such
+exists.
+
+Some STM devices may allow direct mapping of the channel mmio regions
+to userspace for zero-copy writing. One mappable page (in terms of
+mmu) will usually contain multiple channels' mmios, so the user will
+need to allocate that many channels to themselves (via the
+aforementioned ioctl() call) to be able to do this. That is, if your
+stm device's channel mmio region is 64 bytes and hardware page size is
+4096 bytes, after a successful STP_POLICY_ID_SET ioctl() call with
+width==64, you should be able to mmap() one page on this file
+descriptor and obtain direct access to an mmio region for 64 channels.
+
+For kernel-based trace sources, there is "stm_source" device
+class. Devices of this class can be connected and disconnected to/from
+stm devices at runtime via a sysfs attribute.
+
+Examples of STM devices are Intel Trace Hub [1] and Coresight STM
+[2].
+
+[1] https://software.intel.com/sites/default/files/managed/d3/3c/intel-th-developer-manual.pdf
+[2] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0444b/index.html
drivers/Kconfig +2 -0
index 6e973b8e3a..96efe4522c 100644
@@ -184,4 +184,6 @@ source "drivers/android/Kconfig"
 
 source "drivers/nvdimm/Kconfig"
 
+source "drivers/hwtracing/stm/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index b64b49f6e0..6647bc923f 100644
@@ -164,4 +164,5 @@ obj-$(CONFIG_MCB)           += mcb/
 obj-$(CONFIG_RAS)              += ras/
 obj-$(CONFIG_THUNDERBOLT)      += thunderbolt/
 obj-$(CONFIG_CORESIGHT)                += hwtracing/coresight/
+obj-$(CONFIG_STM)              += hwtracing/stm/
 obj-$(CONFIG_ANDROID)          += android/
drivers/hwtracing/stm/Kconfig new file +8 -0
new file mode 100644
index 0000000000..90ed327461
@@ -0,0 +1,8 @@
+config STM
+       tristate "System Trace Module devices"
+       help
+         A System Trace Module (STM) is a device exporting data in System
+         Trace Protocol (STP) format as defined by MIPI STP standards.
+         Examples of such devices are Intel Trace Hub and Coresight STM.
+
+         Say Y here to enable System Trace Module device support.
drivers/hwtracing/stm/Makefile new file +3 -0
new file mode 100644
index 0000000000..adec701649
@@ -0,0 +1,3 @@
+obj-$(CONFIG_STM)      += stm_core.o
+
+stm_core-y             := core.o policy.o
drivers/hwtracing/stm/core.c new file +897 -0
new file mode 100644
index 0000000000..b79c42c625
@@ -0,0 +1,1029 @@
+/*
+ * System Trace Module (STM) infrastructure
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#include <linux/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/compat.h>
+#include <linux/kdev_t.h>
+#include <linux/srcu.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+#include <linux/fs.h>
+#include <linux/mm.h>
+#include "stm.h"
+
+#include <uapi/linux/stm.h>
+
+static unsigned int stm_core_up;
+
+/*
+ * The SRCU here makes sure that STM device doesn't disappear from under a
+ * stm_source_write() caller, which may want to have as little overhead as
+ * possible.
+ */
+static struct srcu_struct stm_source_srcu;
+
+static ssize_t masters_show(struct device *dev,
+                           struct device_attribute *attr,
+                           char *buf)
+{
+       struct stm_device *stm = to_stm_device(dev);
+       int ret;
+
+       ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
+
+       return ret;
+}
+
+static DEVICE_ATTR_RO(masters);
+
+static ssize_t channels_show(struct device *dev,
+                            struct device_attribute *attr,
+                            char *buf)
+{
+       struct stm_device *stm = to_stm_device(dev);
+       int ret;
+
+       ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
+
+       return ret;
+}
+
+static DEVICE_ATTR_RO(channels);
+
+static struct attribute *stm_attrs[] = {
+       &dev_attr_masters.attr,
+       &dev_attr_channels.attr,
+       NULL,
+};
+
+ATTRIBUTE_GROUPS(stm);
+
+static struct class stm_class = {
+       .name           = "stm",
+       .dev_groups     = stm_groups,
+};
+
+static int stm_dev_match(struct device *dev, const void *data)
+{
+       const char *name = data;
+
+       return sysfs_streq(name, dev_name(dev));
+}
+
+/**
+ * stm_find_device() - find stm device by name
+ * @buf:       character buffer containing the name
+ *
+ * This is called when either policy gets assigned to an stm device or an
+ * stm_source device gets linked to an stm device.
+ *
+ * This grabs device's reference (get_device()) and module reference, both
+ * of which the calling path needs to make sure to drop with stm_put_device().
+ *
+ * Return:     stm device pointer or null if lookup failed.
+ */
+struct stm_device *stm_find_device(const char *buf)
+{
+       struct stm_device *stm;
+       struct device *dev;
+
+       if (!stm_core_up)
+               return NULL;
+
+       dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
+       if (!dev)
+               return NULL;
+
+       stm = to_stm_device(dev);
+       if (!try_module_get(stm->owner)) {
+               put_device(dev);
+               return NULL;
+       }
+
+       return stm;
+}
+
+/**
+ * stm_put_device() - drop references on the stm device
+ * @stm:       stm device, previously acquired by stm_find_device()
+ *
+ * This drops the module reference and device reference taken by
+ * stm_find_device().
+ */
+void stm_put_device(struct stm_device *stm)
+{
+       module_put(stm->owner);
+       put_device(&stm->dev);
+}
+
+/*
+ * Internally we only care about software-writable masters here, that is the
+ * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
+ * original master numbers to be visible externally, since they are the ones
+ * that will appear in the STP stream. Thus, the internal bookkeeping uses
+ * $master - stm_data->sw_start to reference master descriptors and such.
+ */
+
+#define __stm_master(_s, _m)                           \
+       ((_s)->masters[(_m) - (_s)->data->sw_start])
+
+static inline struct stp_master *
+stm_master(struct stm_device *stm, unsigned int idx)
+{
+       if (idx < stm->data->sw_start || idx > stm->data->sw_end)
+               return NULL;
+
+       return __stm_master(stm, idx);
+}
+
+static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
+{
+       struct stp_master *master;
+       size_t size;
+
+       size = ALIGN(stm->data->sw_nchannels, 8) / 8;
+       size += sizeof(struct stp_master);
+       master = kzalloc(size, GFP_ATOMIC);
+       if (!master)
+               return -ENOMEM;
+
+       master->nr_free = stm->data->sw_nchannels;
+       __stm_master(stm, idx) = master;
+
+       return 0;
+}
+
+static void stp_master_free(struct stm_device *stm, unsigned int idx)
+{
+       struct stp_master *master = stm_master(stm, idx);
+
+       if (!master)
+               return;
+
+       __stm_master(stm, idx) = NULL;
+       kfree(master);
+}
+
+static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
+{
+       struct stp_master *master = stm_master(stm, output->master);
+
+       if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
+               return;
+
+       bitmap_allocate_region(&master->chan_map[0], output->channel,
+                              ilog2(output->nr_chans));
+
+       master->nr_free -= output->nr_chans;
+}
+
+static void
+stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
+{
+       struct stp_master *master = stm_master(stm, output->master);
+
+       bitmap_release_region(&master->chan_map[0], output->channel,
+                             ilog2(output->nr_chans));
+
+       output->nr_chans = 0;
+       master->nr_free += output->nr_chans;
+}
+
+/*
+ * This is like bitmap_find_free_region(), except it can ignore @start bits
+ * at the beginning.
+ */
+static int find_free_channels(unsigned long *bitmap, unsigned int start,
+                             unsigned int end, unsigned int width)
+{
+       unsigned int pos;
+       int i;
+
+       for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
+               pos = find_next_zero_bit(bitmap, end + 1, pos);
+               if (pos + width > end + 1)
+                       break;
+
+               if (pos & (width - 1))
+                       continue;
+
+               for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
+                       ;
+               if (i == width)
+                       return pos;
+       }
+
+       return -1;
+}
+
+static unsigned int
+stm_find_master_chan(struct stm_device *stm, unsigned int width,
+                    unsigned int *mstart, unsigned int mend,
+                    unsigned int *cstart, unsigned int cend)
+{
+       struct stp_master *master;
+       unsigned int midx;
+       int pos, err;
+
+       for (midx = *mstart; midx <= mend; midx++) {
+               if (!stm_master(stm, midx)) {
+                       err = stp_master_alloc(stm, midx);
+                       if (err)
+                               return err;
+               }
+
+               master = stm_master(stm, midx);
+
+               if (!master->nr_free)
+                       continue;
+
+               pos = find_free_channels(master->chan_map, *cstart, cend,
+                                        width);
+               if (pos < 0)
+                       continue;
+
+               *mstart = midx;
+               *cstart = pos;
+               return 0;
+       }
+
+       return -ENOSPC;
+}
+
+static int stm_output_assign(struct stm_device *stm, unsigned int width,
+                            struct stp_policy_node *policy_node,
+                            struct stm_output *output)
+{
+       unsigned int midx, cidx, mend, cend;
+       int ret = -EINVAL;
+
+       if (width > stm->data->sw_nchannels)
+               return -EINVAL;
+
+       if (policy_node) {
+               stp_policy_node_get_ranges(policy_node,
+                                          &midx, &mend, &cidx, &cend);
+       } else {
+               midx = stm->data->sw_start;
+               cidx = 0;
+               mend = stm->data->sw_end;
+               cend = stm->data->sw_nchannels - 1;
+       }
+
+       spin_lock(&stm->mc_lock);
+       /* output is already assigned -- shouldn't happen */
+       if (WARN_ON_ONCE(output->nr_chans))
+               goto unlock;
+
+       ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
+       if (ret)
+               goto unlock;
+
+       output->master = midx;
+       output->channel = cidx;
+       output->nr_chans = width;
+       stm_output_claim(stm, output);
+       dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
+
+       ret = 0;
+unlock:
+       spin_unlock(&stm->mc_lock);
+
+       return ret;
+}
+
+static void stm_output_free(struct stm_device *stm, struct stm_output *output)
+{
+       spin_lock(&stm->mc_lock);
+       if (output->nr_chans)
+               stm_output_disclaim(stm, output);
+       spin_unlock(&stm->mc_lock);
+}
+
+static int major_match(struct device *dev, const void *data)
+{
+       unsigned int major = *(unsigned int *)data;
+
+       return MAJOR(dev->devt) == major;
+}
+
+static int stm_char_open(struct inode *inode, struct file *file)
+{
+       struct stm_file *stmf;
+       struct device *dev;
+       unsigned int major = imajor(inode);
+       int err = -ENODEV;
+
+       dev = class_find_device(&stm_class, NULL, &major, major_match);
+       if (!dev)
+               return -ENODEV;
+
+       stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
+       if (!stmf)
+               return -ENOMEM;
+
+       stmf->stm = to_stm_device(dev);
+
+       if (!try_module_get(stmf->stm->owner))
+               goto err_free;
+
+       file->private_data = stmf;
+
+       return nonseekable_open(inode, file);
+
+err_free:
+       kfree(stmf);
+
+       return err;
+}
+
+static int stm_char_release(struct inode *inode, struct file *file)
+{
+       struct stm_file *stmf = file->private_data;
+
+       stm_output_free(stmf->stm, &stmf->output);
+       stm_put_device(stmf->stm);
+       kfree(stmf);
+
+       return 0;
+}
+
+static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
+{
+       struct stm_device *stm = stmf->stm;
+       int ret;
+
+       stmf->policy_node = stp_policy_node_lookup(stm, id);
+
+       ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
+
+       if (stmf->policy_node)
+               stp_policy_node_put(stmf->policy_node);
+
+       return ret;
+}
+
+static void stm_write(struct stm_data *data, unsigned int master,
+                     unsigned int channel, const char *buf, size_t count)
+{
+       unsigned int flags = STP_PACKET_TIMESTAMPED;
+       const unsigned char *p = buf, nil = 0;
+       size_t pos;
+       ssize_t sz;
+
+       for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
+               sz = min_t(unsigned int, count - pos, 8);
+               sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
+                                 sz, p);
+               flags = 0;
+       }
+
+       data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
+}
+
+static ssize_t stm_char_write(struct file *file, const char __user *buf,
+                             size_t count, loff_t *ppos)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_device *stm = stmf->stm;
+       char *kbuf;
+       int err;
+
+       /*
+        * if no m/c have been assigned to this writer up to this
+        * point, use "default" policy entry
+        */
+       if (!stmf->output.nr_chans) {
+               err = stm_file_assign(stmf, "default", 1);
+               /*
+                * EBUSY means that somebody else just assigned this
+                * output, which is just fine for write()
+                */
+               if (err && err != -EBUSY)
+                       return err;
+       }
+
+       kbuf = kmalloc(count + 1, GFP_KERNEL);
+       if (!kbuf)
+               return -ENOMEM;
+
+       err = copy_from_user(kbuf, buf, count);
+       if (err) {
+               kfree(kbuf);
+               return -EFAULT;
+       }
+
+       stm_write(stm->data, stmf->output.master, stmf->output.channel, kbuf,
+                 count);
+
+       kfree(kbuf);
+
+       return count;
+}
+
+static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_device *stm = stmf->stm;
+       unsigned long size, phys;
+
+       if (!stm->data->mmio_addr)
+               return -EOPNOTSUPP;
+
+       if (vma->vm_pgoff)
+               return -EINVAL;
+
+       size = vma->vm_end - vma->vm_start;
+
+       if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
+               return -EINVAL;
+
+       phys = stm->data->mmio_addr(stm->data, stmf->output.master,
+                                   stmf->output.channel,
+                                   stmf->output.nr_chans);
+
+       if (!phys)
+               return -EINVAL;
+
+       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+       vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+       vm_iomap_memory(vma, phys, size);
+
+       return 0;
+}
+
+static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
+{
+       struct stm_device *stm = stmf->stm;
+       struct stp_policy_id *id;
+       int ret = -EINVAL;
+       u32 size;
+
+       if (stmf->output.nr_chans)
+               return -EBUSY;
+
+       if (copy_from_user(&size, arg, sizeof(size)))
+               return -EFAULT;
+
+       if (size >= PATH_MAX + sizeof(*id))
+               return -EINVAL;
+
+       /*
+        * size + 1 to make sure the .id string at the bottom is terminated,
+        * which is also why memdup_user() is not useful here
+        */
+       id = kzalloc(size + 1, GFP_KERNEL);
+       if (!id)
+               return -ENOMEM;
+
+       if (copy_from_user(id, arg, size)) {
+               ret = -EFAULT;
+               goto err_free;
+       }
+
+       if (id->__reserved_0 || id->__reserved_1)
+               goto err_free;
+
+       if (id->width < 1 ||
+           id->width > PAGE_SIZE / stm->data->sw_mmiosz)
+               goto err_free;
+
+       ret = stm_file_assign(stmf, id->id, id->width);
+       if (ret)
+               goto err_free;
+
+       ret = 0;
+
+       if (stm->data->link)
+               ret = stm->data->link(stm->data, stmf->output.master,
+                                     stmf->output.channel);
+
+       if (ret) {
+               stm_output_free(stmf->stm, &stmf->output);
+               stm_put_device(stmf->stm);
+       }
+
+err_free:
+       kfree(id);
+
+       return ret;
+}
+
+static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
+{
+       struct stp_policy_id id = {
+               .size           = sizeof(id),
+               .master         = stmf->output.master,
+               .channel        = stmf->output.channel,
+               .width          = stmf->output.nr_chans,
+               .__reserved_0   = 0,
+               .__reserved_1   = 0,
+       };
+
+       return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
+}
+
+static long
+stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_data *stm_data = stmf->stm->data;
+       int err = -ENOTTY;
+       u64 options;
+
+       switch (cmd) {
+       case STP_POLICY_ID_SET:
+               err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
+               if (err)
+                       return err;
+
+               return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
+
+       case STP_POLICY_ID_GET:
+               return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
+
+       case STP_SET_OPTIONS:
+               if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
+                       return -EFAULT;
+
+               if (stm_data->set_options)
+                       err = stm_data->set_options(stm_data,
+                                                   stmf->output.master,
+                                                   stmf->output.channel,
+                                                   stmf->output.nr_chans,
+                                                   options);
+
+               break;
+       default:
+               break;
+       }
+
+       return err;
+}
+
+#ifdef CONFIG_COMPAT
+static long
+stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
+}
+#else
+#define stm_char_compat_ioctl  NULL
+#endif
+
+static const struct file_operations stm_fops = {
+       .open           = stm_char_open,
+       .release        = stm_char_release,
+       .write          = stm_char_write,
+       .mmap           = stm_char_mmap,
+       .unlocked_ioctl = stm_char_ioctl,
+       .compat_ioctl   = stm_char_compat_ioctl,
+       .llseek         = no_llseek,
+};
+
+static void stm_device_release(struct device *dev)
+{
+       struct stm_device *stm = to_stm_device(dev);
+
+       kfree(stm);
+}
+
+int stm_register_device(struct device *parent, struct stm_data *stm_data,
+                       struct module *owner)
+{
+       struct stm_device *stm;
+       unsigned int nmasters;
+       int err = -ENOMEM;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
+
+       if (!stm_data->packet || !stm_data->sw_nchannels)
+               return -EINVAL;
+
+       nmasters = stm_data->sw_end - stm_data->sw_start;
+       stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
+       if (!stm)
+               return -ENOMEM;
+
+       stm->major = register_chrdev(0, stm_data->name, &stm_fops);
+       if (stm->major < 0)
+               goto err_free;
+
+       device_initialize(&stm->dev);
+       stm->dev.devt = MKDEV(stm->major, 0);
+       stm->dev.class = &stm_class;
+       stm->dev.parent = parent;
+       stm->dev.release = stm_device_release;
+
+       err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
+       if (err)
+               goto err_device;
+
+       err = device_add(&stm->dev);
+       if (err)
+               goto err_device;
+
+       spin_lock_init(&stm->link_lock);
+       INIT_LIST_HEAD(&stm->link_list);
+
+       spin_lock_init(&stm->mc_lock);
+       mutex_init(&stm->policy_mutex);
+       stm->sw_nmasters = nmasters;
+       stm->owner = owner;
+       stm->data = stm_data;
+       stm_data->stm = stm;
+
+       return 0;
+
+err_device:
+       put_device(&stm->dev);
+err_free:
+       kfree(stm);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(stm_register_device);
+
+static void __stm_source_link_drop(struct stm_source_device *src,
+                                  struct stm_device *stm);
+
+void stm_unregister_device(struct stm_data *stm_data)
+{
+       struct stm_device *stm = stm_data->stm;
+       struct stm_source_device *src, *iter;
+       int i;
+
+       spin_lock(&stm->link_lock);
+       list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
+               __stm_source_link_drop(src, stm);
+       }
+       spin_unlock(&stm->link_lock);
+
+       synchronize_srcu(&stm_source_srcu);
+
+       unregister_chrdev(stm->major, stm_data->name);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy)
+               stp_policy_unbind(stm->policy);
+       mutex_unlock(&stm->policy_mutex);
+
+       for (i = 0; i < stm->sw_nmasters; i++)
+               stp_master_free(stm, i);
+
+       device_unregister(&stm->dev);
+       stm_data->stm = NULL;
+}
+EXPORT_SYMBOL_GPL(stm_unregister_device);
+
+/**
+ * stm_source_link_add() - connect an stm_source device to an stm device
+ * @src:       stm_source device
+ * @stm:       stm device
+ *
+ * This function establishes a link from stm_source to an stm device so that
+ * the former can send out trace data to the latter.
+ *
+ * Return:     0 on success, -errno otherwise.
+ */
+static int stm_source_link_add(struct stm_source_device *src,
+                              struct stm_device *stm)
+{
+       char *id;
+       int err;
+
+       spin_lock(&stm->link_lock);
+       spin_lock(&src->link_lock);
+
+       /* src->link is dereferenced under stm_source_srcu but not the list */
+       rcu_assign_pointer(src->link, stm);
+       list_add_tail(&src->link_entry, &stm->link_list);
+
+       spin_unlock(&src->link_lock);
+       spin_unlock(&stm->link_lock);
+
+       id = kstrdup(src->data->name, GFP_KERNEL);
+       if (id) {
+               src->policy_node =
+                       stp_policy_node_lookup(stm, id);
+
+               kfree(id);
+       }
+
+       err = stm_output_assign(stm, src->data->nr_chans,
+                               src->policy_node, &src->output);
+
+       if (src->policy_node)
+               stp_policy_node_put(src->policy_node);
+
+       if (err)
+               goto fail_detach;
+
+       /* this is to notify the STM device that a new link has been made */
+       if (stm->data->link)
+               err = stm->data->link(stm->data, src->output.master,
+                                     src->output.channel);
+
+       if (err)
+               goto fail_free_output;
+
+       /* this is to let the source carry out all necessary preparations */
+       if (src->data->link)
+               src->data->link(src->data);
+
+       return 0;
+
+fail_free_output:
+       stm_output_free(stm, &src->output);
+       stm_put_device(stm);
+
+fail_detach:
+       spin_lock(&stm->link_lock);
+       spin_lock(&src->link_lock);
+
+       rcu_assign_pointer(src->link, NULL);
+       list_del_init(&src->link_entry);
+
+       spin_unlock(&src->link_lock);
+       spin_unlock(&stm->link_lock);
+
+       return err;
+}
+
+/**
+ * __stm_source_link_drop() - detach stm_source from an stm device
+ * @src:       stm_source device
+ * @stm:       stm device
+ *
+ * If @stm is @src::link, disconnect them from one another and put the
+ * reference on the @stm device.
+ *
+ * Caller must hold stm::link_lock.
+ */
+static void __stm_source_link_drop(struct stm_source_device *src,
+                                  struct stm_device *stm)
+{
+       spin_lock(&src->link_lock);
+       if (WARN_ON_ONCE(src->link != stm)) {
+               spin_unlock(&src->link_lock);
+               return;
+       }
+
+       stm_output_free(src->link, &src->output);
+       /* caller must hold stm::link_lock */
+       list_del_init(&src->link_entry);
+       /* matches stm_find_device() from stm_source_link_store() */
+       stm_put_device(src->link);
+       rcu_assign_pointer(src->link, NULL);
+
+       spin_unlock(&src->link_lock);
+}
+
+/**
+ * stm_source_link_drop() - detach stm_source from its stm device
+ * @src:       stm_source device
+ *
+ * Unlinking means disconnecting from source's STM device; after this
+ * writes will be unsuccessful until it is linked to a new STM device.
+ *
+ * This will happen on "stm_source_link" sysfs attribute write to undo
+ * the existing link (if any), or on linked STM device's de-registration.
+ */
+static void stm_source_link_drop(struct stm_source_device *src)
+{
+       struct stm_device *stm;
+       int idx;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+
+       if (stm) {
+               if (src->data->unlink)
+                       src->data->unlink(src->data);
+
+               spin_lock(&stm->link_lock);
+               __stm_source_link_drop(src, stm);
+               spin_unlock(&stm->link_lock);
+       }
+
+       srcu_read_unlock(&stm_source_srcu, idx);
+}
+
+static ssize_t stm_source_link_show(struct device *dev,
+                                   struct device_attribute *attr,
+                                   char *buf)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+       struct stm_device *stm;
+       int idx, ret;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+       ret = sprintf(buf, "%s\n",
+                     stm ? dev_name(&stm->dev) : "<none>");
+       srcu_read_unlock(&stm_source_srcu, idx);
+
+       return ret;
+}
+
+static ssize_t stm_source_link_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+       struct stm_device *link;
+       int err;
+
+       stm_source_link_drop(src);
+
+       link = stm_find_device(buf);
+       if (!link)
+               return -EINVAL;
+
+       err = stm_source_link_add(src, link);
+       if (err)
+               stm_put_device(link);
+
+       return err ? : count;
+}
+
+static DEVICE_ATTR_RW(stm_source_link);
+
+static struct attribute *stm_source_attrs[] = {
+       &dev_attr_stm_source_link.attr,
+       NULL,
+};
+
+ATTRIBUTE_GROUPS(stm_source);
+
+static struct class stm_source_class = {
+       .name           = "stm_source",
+       .dev_groups     = stm_source_groups,
+};
+
+static void stm_source_device_release(struct device *dev)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+
+       kfree(src);
+}
+
+/**
+ * stm_source_register_device() - register an stm_source device
+ * @parent:    parent device
+ * @data:      device description structure
+ *
+ * This will create a device of stm_source class that can write
+ * data to an stm device once linked.
+ *
+ * Return:     0 on success, -errno otherwise.

drivers/hwtracing/stm/core.c line 907

Quoted 10 lines
drivers/hwtracing/stm/core.c new file +10 -0
+ */
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data)
+{
+       struct stm_source_device *src;
+       int err;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
+

I tried to update Coresight-stm driver[1] based on your this version patch, but the Coresight-stm driver probe() failed.
the reason was:
In the end of Coresight stm_probe(), we called this function, but "stm_core_up" was zero then, so the error returned value "-EPROBE_DEFER" was received.

In fact, "stm_core_up" would increase itself until "stm_core_init" be called - it's the root of this problem, I'll explain this where the function "stm_core_init" defined.

And redoing Coresight stm_probe() will incur a WARN_ON() like below:

[    1.075746] coresight-stm 10006000.stm: stm_register_device failed
[    1.082118] ------------[ cut here ]------------
[    1.086819] WARNING: CPU: 1 PID: 1 at drivers/clk/clk.c:657
clk_core_disable+0x138/0x13c()
[    1.095353] Modules linked in:
[    1.098487] CPU: 1 PID: 1 Comm: swapper/0 Tainted: G S
4.2.0-rc1+ #107
[    1.106398] Hardware name: Spreadtrum SC9836 Openphone Board (DT)
[    1.112678] Call trace:
[    1.115194] [<ffffffc00008a5b4>] dump_backtrace+0x0/0x138
[    1.120761] [<ffffffc00008a708>] show_stack+0x1c/0x28
[    1.125972] [<ffffffc0003320e0>] dump_stack+0x84/0xc8
[    1.131179] [<ffffffc00009b580>] warn_slowpath_common+0xa4/0xdc
[    1.137285] [<ffffffc00009b700>] warn_slowpath_null+0x34/0x44
[    1.143213] [<ffffffc000321eb4>] clk_core_disable+0x134/0x13c
[    1.149140] [<ffffffc000322154>] clk_disable+0x30/0x48
[    1.154439] [<ffffffc0002b5e58>] amba_put_disable_pclk.isra.6+0x20/0x40
[    1.161264] [<ffffffc0002b6134>] amba_probe+0x16c/0x19c
[    1.166653] [<ffffffc0002e664c>] driver_probe_device+0x1a4/0x2b4
[    1.172849] [<ffffffc0002e6804>] __driver_attach+0xa8/0xb0
[    1.178506] [<ffffffc0002e45fc>] bus_for_each_dev+0x68/0xac
[    1.184253] [<ffffffc0002e60b4>] driver_attach+0x28/0x34
[    1.189731] [<ffffffc0002e5cd0>] bus_add_driver+0x1cc/0x234
[    1.195479] [<ffffffc0002e73d8>] driver_register+0x64/0x124
[    1.201226] [<ffffffc0002b58cc>] amba_driver_register+0x64/0x74
[    1.207333] [<ffffffc00045591c>] stm_driver_init+0x10/0x1c
[    1.212990] [<ffffffc0000828e0>] do_one_initcall+0x90/0x1a8
[    1.218739] [<ffffffc000431adc>] kernel_init_freeable+0x150/0x1f4
[    1.225026] [<ffffffc00032fbe4>] kernel_init+0x1c/0xe0
[    1.230321] ---[ end trace 487f4b7838ebf86a ]---
[    1.235102] ------------[ cut here ]------------
Quoted 98 lines
drivers/hwtracing/stm/core.c new file +98 -0
+       src = kzalloc(sizeof(*src), GFP_KERNEL);
+       if (!src)
+               return -ENOMEM;
+
+       device_initialize(&src->dev);
+       src->dev.class = &stm_source_class;
+       src->dev.parent = parent;
+       src->dev.release = stm_source_device_release;
+
+       err = kobject_set_name(&src->dev.kobj, "%s", data->name);
+       if (err)
+               goto err;
+
+       err = device_add(&src->dev);
+       if (err)
+               goto err;
+
+       spin_lock_init(&src->link_lock);
+       INIT_LIST_HEAD(&src->link_entry);
+       src->data = data;
+       data->src = src;
+
+       return 0;
+
+err:
+       put_device(&src->dev);
+       kfree(src);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(stm_source_register_device);
+
+/**
+ * stm_source_unregister_device() - unregister an stm_source device
+ * @data:      device description that was used to register the device
+ *
+ * This will remove a previously created stm_source device from the system.
+ */
+void stm_source_unregister_device(struct stm_source_data *data)
+{
+       struct stm_source_device *src = data->src;
+
+       stm_source_link_drop(src);
+
+       device_destroy(&stm_source_class, src->dev.devt);
+}
+EXPORT_SYMBOL_GPL(stm_source_unregister_device);
+
+int stm_source_write(struct stm_source_data *data, unsigned int chan,
+                    const char *buf, size_t count)
+{
+       struct stm_source_device *src = data->src;
+       struct stm_device *stm;
+       int idx;
+
+       if (!src->output.nr_chans)
+               return -ENODEV;
+
+       if (chan >= src->output.nr_chans)
+               return -EINVAL;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+       if (stm)
+               stm_write(stm->data, src->output.master,
+                         src->output.channel + chan,
+                         buf, count);
+       else
+               count = -ENODEV;
+
+       srcu_read_unlock(&stm_source_srcu, idx);
+
+       return count;
+}
+EXPORT_SYMBOL_GPL(stm_source_write);
+
+static int __init stm_core_init(void)
+{
+       int err;
+
+       err = class_register(&stm_class);
+       if (err)
+               return err;
+
+       err = class_register(&stm_source_class);
+       if (err)
+               goto err_stm;
+
+       err = stp_configfs_init();
+       if (err)
+               goto err_src;
+
+       init_srcu_struct(&stm_source_srcu);
+
+       stm_core_up++;
+
+       return 0;

drivers/hwtracing/stm/core.c line 1015

Quoted 10 lines
drivers/hwtracing/stm/core.c new file +10 -0
+
+err_src:
+       class_unregister(&stm_source_class);
+err_stm:
+       class_unregister(&stm_class);
+
+       return err;
+}
+
+module_init(stm_core_init);

Since you are using module_init() instead of postcore_initcall() which was in the last version patch, as such, this function would be executed after Coresight "stm_probe" finished.

So, we think there a few optional solutions:
1) Remove the "stm_register_device" out from Coresight "stm_probe", but we have to save another global variable:

    struct device *stm_dev;

in the process of Coresight "stm_probe".

2) Change module_init() to other XYX_init() which would run prior to "amba_probe()" (i.e. the caller of Coresight stm_probe), this may be a better one.

3) stm_core_init() could be turned into a library call where initialisation of the internals is done when first called.

Looking forward to your reply/comments.

Thanks,
Chunyan

[1] https://lkml.org/lkml/2015/2/4/729

Quoted 838 lines
drivers/hwtracing/stm/core.c new file +14 -0
+
+static void __exit stm_core_exit(void)
+{
+       cleanup_srcu_struct(&stm_source_srcu);
+       class_unregister(&stm_source_class);
+       class_unregister(&stm_class);
+       stp_configfs_exit();
+}
+
+module_exit(stm_core_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("System Trace Module device class");
+MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>");
drivers/hwtracing/stm/policy.c new file +529 -0
new file mode 100644
index 0000000000..6498a9dbb7
@@ -0,0 +1,529 @@
+/*
+ * System Trace Module (STM) master/channel allocation policy management
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * A master/channel allocation policy allows mapping string identifiers to
+ * master and channel ranges, where allocation can be done.
+ */
+
+#define pr_fmt(fmt)    KBUILD_MODNAME ": " fmt
+
+#include <linux/types.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/configfs.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+#include "stm.h"
+
+/*
+ * STP Master/Channel allocation policy configfs layout.
+ */
+
+struct stp_policy {
+       struct config_group     group;
+       struct stm_device       *stm;
+};
+
+struct stp_policy_node {
+       struct config_group     group;
+       struct stp_policy       *policy;
+       unsigned int            first_master;
+       unsigned int            last_master;
+       unsigned int            first_channel;
+       unsigned int            last_channel;
+};
+
+static struct configfs_subsystem stp_policy_subsys;
+
+void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
+                               unsigned int *mstart, unsigned int *mend,
+                               unsigned int *cstart, unsigned int *cend)
+{
+       *mstart = policy_node->first_master;
+       *mend   = policy_node->last_master;
+       *cstart = policy_node->first_channel;
+       *cend   = policy_node->last_channel;
+}
+
+static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
+{
+       return policy_node->group.cg_item.ci_name ? : "<none>";
+}
+
+static inline struct stp_policy *to_stp_policy(struct config_item *item)
+{
+       return item ?
+               container_of(to_config_group(item), struct stp_policy, group) :
+               NULL;
+}
+
+static inline struct stp_policy_node *
+to_stp_policy_node(struct config_item *item)
+{
+       return item ?
+               container_of(to_config_group(item), struct stp_policy_node,
+                            group) :
+               NULL;
+}
+
+static ssize_t stp_policy_node_masters_show(struct stp_policy_node *policy_node,
+                                           char *page)
+{
+       ssize_t count;
+
+       count = sprintf(page, "%u %u\n", policy_node->first_master,
+                       policy_node->last_master);
+
+       return count;
+}
+
+static ssize_t
+stp_policy_node_masters_store(struct stp_policy_node *policy_node,
+                             const char *page, size_t count)
+{
+       unsigned int first, last;
+       struct stm_device *stm;
+       char *p = (char *)page;
+       ssize_t ret = -ENODEV;
+
+       if (sscanf(p, "%u %u", &first, &last) != 2)
+               return -EINVAL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+       stm = policy_node->policy->stm;
+       if (!stm)
+               goto unlock;
+
+       /* must be within [sw_start..sw_end], which is an inclusive range */
+       if (first > INT_MAX || last > INT_MAX || first > last ||
+           first < stm->data->sw_start ||
+           last > stm->data->sw_end) {
+               ret = -ERANGE;
+               goto unlock;
+       }
+
+       ret = count;
+       policy_node->first_master = first;
+       policy_node->last_master = last;
+
+unlock:
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return ret;
+}
+
+static ssize_t
+stp_policy_node_channels_show(struct stp_policy_node *policy_node, char *page)
+{
+       ssize_t count;
+
+       count = sprintf(page, "%u %u\n", policy_node->first_channel,
+                       policy_node->last_channel);
+
+       return count;
+}
+
+static ssize_t
+stp_policy_node_channels_store(struct stp_policy_node *policy_node,
+                              const char *page, size_t count)
+{
+       unsigned int first, last;
+       struct stm_device *stm;
+       char *p = (char *)page;
+       ssize_t ret = -ENODEV;
+
+       if (sscanf(p, "%u %u", &first, &last) != 2)
+               return -EINVAL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+       stm = policy_node->policy->stm;
+       if (!stm)
+               goto unlock;
+
+       if (first > INT_MAX || last > INT_MAX || first > last ||
+           last >= stm->data->sw_nchannels) {
+               ret = -ERANGE;
+               goto unlock;
+       }
+
+       ret = count;
+       policy_node->first_channel = first;
+       policy_node->last_channel = last;
+
+unlock:
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return ret;
+}
+
+static void stp_policy_node_release(struct config_item *item)
+{
+       kfree(to_stp_policy_node(item));
+}
+
+struct stp_policy_node_attribute {
+       struct configfs_attribute       attr;
+       ssize_t (*show)(struct stp_policy_node *, char *);
+       ssize_t (*store)(struct stp_policy_node *, const char *, size_t);
+};
+
+static ssize_t stp_policy_node_attr_show(struct config_item *item,
+                                        struct configfs_attribute *attr,
+                                        char *page)
+{
+       struct stp_policy_node *policy_node = to_stp_policy_node(item);
+       struct stp_policy_node_attribute *pn_attr =
+               container_of(attr, struct stp_policy_node_attribute, attr);
+       ssize_t count = 0;
+
+       if (pn_attr->show)
+               count = pn_attr->show(policy_node, page);
+
+       return count;
+}
+
+static ssize_t stp_policy_node_attr_store(struct config_item *item,
+                                         struct configfs_attribute *attr,
+                                         const char *page, size_t len)
+{
+       struct stp_policy_node *policy_node = to_stp_policy_node(item);
+       struct stp_policy_node_attribute *pn_attr =
+               container_of(attr, struct stp_policy_node_attribute, attr);
+       ssize_t count = -EINVAL;
+
+       if (pn_attr->store)
+               count = pn_attr->store(policy_node, page, len);
+
+       return count;
+}
+
+static struct configfs_item_operations stp_policy_node_item_ops = {
+       .release                = stp_policy_node_release,
+       .show_attribute         = stp_policy_node_attr_show,
+       .store_attribute        = stp_policy_node_attr_store,
+};
+
+static struct stp_policy_node_attribute stp_policy_node_attr_range = {
+       .attr   = {
+               .ca_owner = THIS_MODULE,
+               .ca_name = "masters",
+               .ca_mode = S_IRUGO | S_IWUSR,
+       },
+       .show   = stp_policy_node_masters_show,
+       .store  = stp_policy_node_masters_store,
+};
+
+static struct stp_policy_node_attribute stp_policy_node_attr_channels = {
+       .attr   = {
+               .ca_owner = THIS_MODULE,
+               .ca_name = "channels",
+               .ca_mode = S_IRUGO | S_IWUSR,
+       },
+       .show   = stp_policy_node_channels_show,
+       .store  = stp_policy_node_channels_store,
+};
+
+static struct configfs_attribute *stp_policy_node_attrs[] = {
+       &stp_policy_node_attr_range.attr,
+       &stp_policy_node_attr_channels.attr,
+       NULL,
+};
+
+static struct config_item_type stp_policy_type;
+static struct config_item_type stp_policy_node_type;
+
+static struct config_group *
+stp_policy_node_make(struct config_group *group, const char *name)
+{
+       struct stp_policy_node *policy_node, *parent_node;
+       struct stp_policy *policy;
+
+       if (group->cg_item.ci_type == &stp_policy_type) {
+               policy = container_of(group, struct stp_policy, group);
+       } else {
+               parent_node = container_of(group, struct stp_policy_node,
+                                          group);
+               policy = parent_node->policy;
+       }
+
+       if (!policy->stm)
+               return ERR_PTR(-ENODEV);
+
+       policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
+       if (!policy_node)
+               return ERR_PTR(-ENOMEM);
+
+       config_group_init_type_name(&policy_node->group, name,
+                                   &stp_policy_node_type);
+
+       policy_node->policy = policy;
+
+       /* default values for the attributes */
+       policy_node->first_master = policy->stm->data->sw_start;
+       policy_node->last_master = policy->stm->data->sw_end;
+       policy_node->first_channel = 0;
+       policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
+
+       return &policy_node->group;
+}
+
+static void
+stp_policy_node_drop(struct config_group *group, struct config_item *item)
+{
+       config_item_put(item);
+}
+
+static struct configfs_group_operations stp_policy_node_group_ops = {
+       .make_group     = stp_policy_node_make,
+       .drop_item      = stp_policy_node_drop,
+};
+
+static struct config_item_type stp_policy_node_type = {
+       .ct_item_ops    = &stp_policy_node_item_ops,
+       .ct_group_ops   = &stp_policy_node_group_ops,
+       .ct_attrs       = stp_policy_node_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+/*
+ * Root group: policies.
+ */
+static struct configfs_attribute stp_policy_attr_device = {
+       .ca_owner = THIS_MODULE,
+       .ca_name = "device",
+       .ca_mode = S_IRUGO,
+};
+
+static struct configfs_attribute *stp_policy_attrs[] = {
+       &stp_policy_attr_device,
+       NULL,
+};
+
+static ssize_t stp_policy_attr_show(struct config_item *item,
+                                   struct configfs_attribute *attr,
+                                   char *page)
+{
+       struct stp_policy *policy = to_stp_policy(item);
+       ssize_t count;
+
+       count = sprintf(page, "%s\n",
+                       (policy && policy->stm) ?
+                       policy->stm->data->name :
+                       "<none>");
+
+       return count;
+}
+
+void stp_policy_unbind(struct stp_policy *policy)
+{
+       struct stm_device *stm = policy->stm;
+
+       if (WARN_ON_ONCE(!policy->stm))
+               return;
+
+       mutex_lock(&stm->policy_mutex);
+       stm->policy = NULL;
+       mutex_unlock(&stm->policy_mutex);
+
+       policy->stm = NULL;
+
+       stm_put_device(stm);
+}
+
+static void stp_policy_release(struct config_item *item)
+{
+       struct stp_policy *policy = to_stp_policy(item);
+
+       stp_policy_unbind(policy);
+       kfree(policy);
+}
+
+static struct configfs_item_operations stp_policy_item_ops = {
+       .release                = stp_policy_release,
+       .show_attribute         = stp_policy_attr_show,
+};
+
+static struct configfs_group_operations stp_policy_group_ops = {
+       .make_group     = stp_policy_node_make,
+};
+
+static struct config_item_type stp_policy_type = {
+       .ct_item_ops    = &stp_policy_item_ops,
+       .ct_group_ops   = &stp_policy_group_ops,
+       .ct_attrs       = stp_policy_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct config_group *
+stp_policies_make(struct config_group *group, const char *name)
+{
+       struct config_group *ret;
+       struct stm_device *stm;
+       char *devname, *p;
+
+       devname = kasprintf(GFP_KERNEL, "%s", name);
+       if (!devname)
+               return ERR_PTR(-ENOMEM);
+
+       /*
+        * node must look like <device_name>.<policy_name>, where
+        * <device_name> is the name of an existing stm device and
+        * <policy_name> is an arbitrary string
+        */
+       p = strchr(devname, '.');
+       if (!p) {
+               kfree(devname);
+               return ERR_PTR(-EINVAL);
+       }
+
+       *p++ = '\0';
+
+       stm = stm_find_device(devname);
+       kfree(devname);
+
+       if (!stm)
+               return ERR_PTR(-ENODEV);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy) {
+               ret = ERR_PTR(-EBUSY);
+               goto unlock_policy;
+       }
+
+       stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
+       if (!stm->policy) {
+               ret = ERR_PTR(-ENOMEM);
+               goto unlock_policy;
+       }
+
+       config_group_init_type_name(&stm->policy->group, name,
+                                   &stp_policy_type);
+       stm->policy->stm = stm;
+
+       ret = &stm->policy->group;
+
+unlock_policy:
+       mutex_unlock(&stm->policy_mutex);
+
+       if (IS_ERR(ret))
+               stm_put_device(stm);
+
+       return ret;
+}
+
+static struct configfs_group_operations stp_policies_group_ops = {
+       .make_group     = stp_policies_make,
+};
+
+static struct config_item_type stp_policies_type = {
+       .ct_group_ops   = &stp_policies_group_ops,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct configfs_subsystem stp_policy_subsys = {
+       .su_group = {
+               .cg_item = {
+                       .ci_namebuf     = "stp-policy",
+                       .ci_type        = &stp_policies_type,
+               },
+       },
+};
+
+/*
+ * Lock the policy mutex from the outside
+ */
+static struct stp_policy_node *
+__stp_policy_node_lookup(struct stp_policy *policy, char *s)
+{
+       struct stp_policy_node *policy_node, *ret;
+       struct list_head *head = &policy->group.cg_children;
+       struct config_item *item;
+       char *start, *end = s;
+
+       if (list_empty(head))
+               return NULL;
+
+       /* return the first entry if everything else fails */
+       item = list_entry(head->next, struct config_item, ci_entry);
+       ret = to_stp_policy_node(item);
+
+next:
+       for (;;) {
+               start = strsep(&end, "/");
+               if (!start)
+                       break;
+
+               if (!*start)
+                       continue;
+
+               list_for_each_entry(item, head, ci_entry) {
+                       policy_node = to_stp_policy_node(item);
+
+                       if (!strcmp(start,
+                                   policy_node->group.cg_item.ci_name)) {
+                               ret = policy_node;
+
+                               if (!end)
+                                       goto out;
+
+                               head = &policy_node->group.cg_children;
+                               goto next;
+                       }
+               }
+               break;
+       }
+
+out:
+       return ret;
+}
+
+
+struct stp_policy_node *
+stp_policy_node_lookup(struct stm_device *stm, char *s)
+{
+       struct stp_policy_node *policy_node = NULL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy)
+               policy_node = __stp_policy_node_lookup(stm->policy, s);
+       mutex_unlock(&stm->policy_mutex);
+
+       if (policy_node)
+               config_item_get(&policy_node->group.cg_item);
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return policy_node;
+}
+
+void stp_policy_node_put(struct stp_policy_node *policy_node)
+{
+       config_item_put(&policy_node->group.cg_item);
+}
+
+int __init stp_configfs_init(void)
+{
+       int err;
+
+       config_group_init(&stp_policy_subsys.su_group);
+       mutex_init(&stp_policy_subsys.su_mutex);
+       err = configfs_register_subsystem(&stp_policy_subsys);
+
+       return err;
+}
+
+void __exit stp_configfs_exit(void)
+{
+       configfs_unregister_subsystem(&stp_policy_subsys);
+}
drivers/hwtracing/stm/stm.h new file +87 -0
new file mode 100644
index 0000000000..cf33bf976a
@@ -0,0 +1,87 @@
+/*
+ * System Trace Module (STM) infrastructure
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#ifndef _STM_STM_H_
+#define _STM_STM_H_
+
+struct stp_policy;
+struct stp_policy_node;
+
+struct stp_policy_node *
+stp_policy_node_lookup(struct stm_device *stm, char *s);
+void stp_policy_node_put(struct stp_policy_node *policy_node);
+void stp_policy_unbind(struct stp_policy *policy);
+
+void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
+                               unsigned int *mstart, unsigned int *mend,
+                               unsigned int *cstart, unsigned int *cend);
+int stp_configfs_init(void);
+void stp_configfs_exit(void);
+
+struct stp_master {
+       unsigned int    nr_free;
+       unsigned long   chan_map[0];
+};
+
+struct stm_device {
+       struct device           dev;
+       struct module           *owner;
+       struct stp_policy       *policy;
+       struct mutex            policy_mutex;
+       int                     major;
+       unsigned int            sw_nmasters;
+       struct stm_data         *data;
+       spinlock_t              link_lock;
+       struct list_head        link_list;
+       /* master allocation */
+       spinlock_t              mc_lock;
+       struct stp_master       *masters[0];
+};
+
+#define to_stm_device(_d)                              \
+       container_of((_d), struct stm_device, dev)
+
+struct stm_output {
+       unsigned int            master;
+       unsigned int            channel;
+       unsigned int            nr_chans;
+};
+
+struct stm_file {
+       struct stm_device       *stm;
+       struct stp_policy_node  *policy_node;
+       struct stm_output       output;
+};
+
+struct stm_device *stm_find_device(const char *name);
+void stm_put_device(struct stm_device *stm);
+
+struct stm_source_device {
+       struct device           dev;
+       struct stm_source_data  *data;
+       spinlock_t              link_lock;
+       struct stm_device       *link;
+       struct list_head        link_entry;
+       /* one output per stm_source device */
+       struct stp_policy_node  *policy_node;
+       struct stm_output       output;
+};
+
+#define to_stm_source_device(_d)                               \
+       container_of((_d), struct stm_source_device, dev)
+
+#endif /* _STM_STM_H_ */
include/linux/stm.h new file +126 -0
new file mode 100644
index 0000000000..9d0083d364
@@ -0,0 +1,126 @@
+/*
+ * System Trace Module (STM) infrastructure apis
+ * Copyright (C) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef _STM_H_
+#define _STM_H_
+
+#include <linux/device.h>
+
+/**
+ * enum stp_packet_type - STP packets that an STM driver sends
+ */
+enum stp_packet_type {
+       STP_PACKET_DATA = 0,
+       STP_PACKET_FLAG,
+       STP_PACKET_USER,
+       STP_PACKET_MERR,
+       STP_PACKET_GERR,
+       STP_PACKET_TRIG,
+       STP_PACKET_XSYNC,
+};
+
+/**
+ * enum stp_packet_flags - STP packet modifiers
+ */
+enum stp_packet_flags {
+       STP_PACKET_MARKED       = 0x1,
+       STP_PACKET_TIMESTAMPED  = 0x2,
+};
+
+struct stp_policy;
+
+struct stm_device;
+
+/**
+ * struct stm_data - STM device description and callbacks
+ * @name:              device name
+ * @stm:               internal structure, only used by stm class code
+ * @sw_start:          first STP master available to software
+ * @sw_end:            last STP master available to software
+ * @sw_nchannels:      number of STP channels per master
+ * @sw_mmiosz:         size of one channel's IO space, for mmap, optional
+ * @packet:            callback that sends an STP packet
+ * @mmio_addr:         mmap callback, optional
+ * @link:              called when a new stm_source gets linked to us, optional
+ * @unlink:            likewise for unlinking, again optional
+ * @set_options:       set device-specific options on a channel
+ *
+ * Fill out this structure before calling stm_register_device() to create
+ * an STM device and stm_unregister_device() to destroy it. It will also be
+ * passed back to @packet(), @mmio_addr(), @link(), @unlink() and @set_options()
+ * callbacks.
+ *
+ * Normally, an STM device will have a range of masters available to software
+ * and the rest being statically assigned to various hardware trace sources.
+ * The former is defined by the the range [@sw_start..@sw_end] of the device
+ * description. That is, the lowest master that can be allocated to software
+ * writers is @sw_start and data from this writer will appear is @sw_start
+ * master in the STP stream.
+ */
+struct stm_data {
+       const char              *name;
+       struct stm_device       *stm;
+       unsigned int            sw_start;
+       unsigned int            sw_end;
+       unsigned int            sw_nchannels;
+       unsigned int            sw_mmiosz;
+       ssize_t                 (*packet)(struct stm_data *, unsigned int,
+                                         unsigned int, unsigned int,
+                                         unsigned int, unsigned int,
+                                         const unsigned char *);
+       phys_addr_t             (*mmio_addr)(struct stm_data *, unsigned int,
+                                            unsigned int, unsigned int);
+       int                     (*link)(struct stm_data *, unsigned int,
+                                       unsigned int);
+       void                    (*unlink)(struct stm_data *, unsigned int,
+                                         unsigned int);
+       long                    (*set_options)(struct stm_data *, unsigned int,
+                                              unsigned int, unsigned int,
+                                              unsigned long);
+};
+
+int stm_register_device(struct device *parent, struct stm_data *stm_data,
+                       struct module *owner);
+void stm_unregister_device(struct stm_data *stm_data);
+
+struct stm_source_device;
+
+/**
+ * struct stm_source_data - STM source device description and callbacks
+ * @name:      device name, will be used for policy lookup
+ * @src:       internal structure, only used by stm class code
+ * @nr_chans:  number of channels to allocate
+ * @link:      called when this source gets linked to an STM device
+ * @unlink:    called when this source is about to get unlinked from its STM
+ *
+ * Fill in this structure before calling stm_source_register_device() to
+ * register a source device. Also pass it to unregister and write calls.
+ */
+struct stm_source_data {
+       const char              *name;
+       struct stm_source_device *src;
+       unsigned int            percpu;
+       unsigned int            nr_chans;
+       int                     (*link)(struct stm_source_data *data);
+       void                    (*unlink)(struct stm_source_data *data);
+};
+
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data);
+void stm_source_unregister_device(struct stm_source_data *data);
+
+int stm_source_write(struct stm_source_data *data, unsigned int chan,
+                    const char *buf, size_t count);
+
+#endif /* _STM_H_ */
include/uapi/linux/stm.h new file +50 -1
new file mode 100644
index 0000000000..626a8d3f63
@@ -0,0 +1,50 @@
+/*
+ * System Trace Module (STM) userspace interfaces
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#ifndef _UAPI_LINUX_STM_H
+#define _UAPI_LINUX_STM_H
+
+#include <linux/types.h>
+
+/**
+ * struct stp_policy_id - identification for the STP policy
+ * @size:      size of the structure including real id[] length
+ * @master:    assigned master
+ * @channel:   first assigned channel
+ * @width:     number of requested channels
+ * @id:                identification string
+ *
+ * User must calculate the total size of the structure and put it into
+ * @size field, fill out the @id and desired @width. In return, kernel
+ * fills out @master, @channel and @width.
+ */
+struct stp_policy_id {
+       __u32           size;
+       __u16           master;
+       __u16           channel;
+       __u16           width;
+       /* padding */
+       __u16           __reserved_0;
+       __u32           __reserved_1;
+       char            id[0];
+};
+
+#define STP_POLICY_ID_SET      _IOWR('%', 0, struct stp_policy_id)
+#define STP_POLICY_ID_GET      _IOR('%', 1, struct stp_policy_id)
+#define STP_SET_OPTIONS                _IOW('%', 2, __u64)
+
+#endif /* _UAPI_LINUX_STM_H */
--

2.1.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author maintainer
#5 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

Quoted 18 lines · Chunyan Zhang
Quoted 18 lines
drivers/hwtracing/stm/core.c new file +18 -0
+/**
+ * stm_source_register_device() - register an stm_source device
+ * @parent:    parent device
+ * @data:      device description structure
+ *
+ * This will create a device of stm_source class that can write
+ * data to an stm device once linked.
+ *
+ * Return:     0 on success, -errno otherwise.
+ */
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data)
+{
+       struct stm_source_device *src;
+       int err;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
Quoted 8 lines
Quoted 1 line

+

I tried to update Coresight-stm driver[1] based on your this version patch, but the Coresight-stm driver probe() failed.
the reason was:
In the end of Coresight stm_probe(), we called this function, but "stm_core_up" was zero then, so the error returned value "-EPROBE_DEFER" was received.

Yes, that is the intended behavior if stm core is not initialized yet.

Quoted 3 lines

In fact, "stm_core_up" would increase itself until "stm_core_init" be called - it's the root of this problem, I'll explain this where the function "stm_core_init" defined.

I'm sorry, I didn't understand this, can you rephrase?

Quoted 17 lines

And redoing Coresight stm_probe() will incur a WARN_ON() like below:

[    1.075746] coresight-stm 10006000.stm: stm_register_device failed
[    1.082118] ------------[ cut here ]------------
[    1.086819] WARNING: CPU: 1 PID: 1 at drivers/clk/clk.c:657
clk_core_disable+0x138/0x13c()
[    1.095353] Modules linked in:
[    1.098487] CPU: 1 PID: 1 Comm: swapper/0 Tainted: G S
4.2.0-rc1+ #107
[    1.106398] Hardware name: Spreadtrum SC9836 Openphone Board (DT)
[    1.112678] Call trace:
[    1.115194] [<ffffffc00008a5b4>] dump_backtrace+0x0/0x138
[    1.120761] [<ffffffc00008a708>] show_stack+0x1c/0x28
[    1.125972] [<ffffffc0003320e0>] dump_stack+0x84/0xc8
[    1.131179] [<ffffffc00009b580>] warn_slowpath_common+0xa4/0xdc
[    1.137285] [<ffffffc00009b700>] warn_slowpath_null+0x34/0x44
[    1.143213] [<ffffffc000321eb4>] clk_core_disable+0x134/0x13c

Well, like I said in the offline thread, this has to do with cleaning up in the error path of stm_probe(). What happens if stm_probe() fails for any other reason? I'm guessing the same warning.

Quoted 27 lines
Quoted 27 lines
drivers/hwtracing/stm/core.c new file +27 -0
+static int __init stm_core_init(void)
+{
+       int err;
+
+       err = class_register(&stm_class);
+       if (err)
+               return err;
+
+       err = class_register(&stm_source_class);
+       if (err)
+               goto err_stm;
+
+       err = stp_configfs_init();
+       if (err)
+               goto err_src;
+
+       init_srcu_struct(&stm_source_srcu);
+
+       stm_core_up++;
+
+       return 0;
+
+err_src:
+       class_unregister(&stm_source_class);
+err_stm:
+       class_unregister(&stm_class);
+
Quoted 8 lines

drivers/hwtracing/stm/core.c line 1015

Quoted 4 lines
drivers/hwtracing/stm/core.c new file +4 -0
+       return err;
+}
+
+module_init(stm_core_init);

Since you are using module_init() instead of postcore_initcall() which was in the last version patch, as such, this function would be executed after Coresight "stm_probe" finished.

Yes, iirc on arm the initcall order somehow forced postcore stm_core_init() before configfs, which it relies on, causing a crash. Now I see that somebody hacked configfs to start at core_initcall (f5b697700c8) instead.

There has to be a way to defer stm_probe(), although a quick look at amba code suggests it's not implemented.

Quoted 7 lines

So, we think there a few optional solutions:
1) Remove the "stm_register_device" out from Coresight "stm_probe", but we have to save another global variable:

    struct device *stm_dev;

in the process of Coresight "stm_probe".

Sorry, didn't understand this one.

Except for I can say that having a global variable like that is a bad idea, but that's not relevant to the problem at hand.

Quoted 3 lines

2) Change module_init() to other XYX_init() which would run prior to "amba_probe()" (i.e. the caller of Coresight stm_probe), this may be a better one.

I'm really not a big fan of the initcall games, to be honest, it will always be a problem on some architecture or other. Having said that, if stm_core_init() runs at postcore_initcall level, does that solve your problem?

Quoted 2 lines

3) stm_core_init() could be turned into a library call where initialisation of the internals is done when first called.

Well, it's not that simple: stm is used by both stm and stm_source devices, in this case we'll need to make sure that the first call to either of the {stm,stm_source}_register_device() results in the actual initialization of the stm core. I think it's a cleaner solution than the initcall games, though.

Regards,
--
Alex

#6 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Alexander Shishkin
Quoted 2 lines · Alexander Shishkin

There has to be a way to defer stm_probe(), although a quick look at amba code suggests it's not implemented.

What makes you say this? Probe deferral is implemented in the driver core rather than individual buses, the buses don't need to know anything about it.

author maintainer
#7 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

Quoted 8 lines · Mark Brown
Quoted 2 lines · Alexander Shishkin

There has to be a way to defer stm_probe(), although a quick look at amba code suggests it's not implemented.

What makes you say this? Probe deferral is implemented in the driver core rather than individual buses, the buses don't need to know anything about it.

I stand corrected, it indeed is.

So returning EPROBE_DEFER from stm_probe() should Just Work (provided stm_probe() handles its error paths correctly).

Regards,
--
Alex

#11 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Alexander Shishkin
Quoted 29 lines · Alexander Shishkin
Quoted 18 lines · Chunyan Zhang
Quoted 18 lines
drivers/hwtracing/stm/core.c new file +18 -0
+/**
+ * stm_source_register_device() - register an stm_source device
+ * @parent:    parent device
+ * @data:      device description structure
+ *
+ * This will create a device of stm_source class that can write
+ * data to an stm device once linked.
+ *
+ * Return:     0 on success, -errno otherwise.
+ */
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data)
+{
+       struct stm_source_device *src;
+       int err;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
Quoted 8 lines
Quoted 1 line

+

I tried to update Coresight-stm driver[1] based on your this version patch, but the Coresight-stm driver probe() failed.
the reason was:
In the end of Coresight stm_probe(), we called this function, but "stm_core_up" was zero then, so the error returned value "-EPROBE_DEFER" was received.

Quoted 7 lines

Yes, that is the intended behavior if stm core is not initialized yet.

Quoted 3 lines

In fact, "stm_core_up" would increase itself until "stm_core_init" be called - it's the root of this problem, I'll explain this where the function "stm_core_init" defined.

I'm sorry, I didn't understand this, can you rephrase?

Sure, I mean, the root reason of this problem is here ( i.e. "stm_core_up" was zero then):

     if (!stm_core_up)
         return -EPROBE_DEFER;

Why it was zero?
Because the function (i.e. stm_core_init() ) in which "stm_core_up" would be added one hasn't been executed at this moment. It would be executed on module_init stage for you this version of patch.

Quoted 19 lines
Quoted 18 lines

And redoing Coresight stm_probe() will incur a WARN_ON() like below:

[    1.075746] coresight-stm 10006000.stm: stm_register_device failed
[    1.082118] ------------[ cut here ]------------
[    1.086819] WARNING: CPU: 1 PID: 1 at drivers/clk/clk.c:657
clk_core_disable+0x138/0x13c()
[    1.095353] Modules linked in:
[    1.098487] CPU: 1 PID: 1 Comm: swapper/0 Tainted: G S
4.2.0-rc1+ #107
[    1.106398] Hardware name: Spreadtrum SC9836 Openphone Board (DT)
[    1.112678] Call trace:
[    1.115194] [<ffffffc00008a5b4>] dump_backtrace+0x0/0x138
[    1.120761] [<ffffffc00008a708>] show_stack+0x1c/0x28
[    1.125972] [<ffffffc0003320e0>] dump_stack+0x84/0xc8
[    1.131179] [<ffffffc00009b580>] warn_slowpath_common+0xa4/0xdc
[    1.137285] [<ffffffc00009b700>] warn_slowpath_null+0x34/0x44
[    1.143213] [<ffffffc000321eb4>] clk_core_disable+0x134/0x13c
Quoted 3 lines

Well, like I said in the offline thread, this has to do with cleaning up in the error path of stm_probe(). What happens if stm_probe() fails for any other reason? I'm guessing the same warning.

The reason of this warning is:
After stm_probe() failed, clk_core_disable() would be called from amba_put_disable_pclk(), then WARN_ON() happened:

         if (WARN_ON(core->enable_count == 0))
                 return;

I'm guessing the reason why "core->enable_count" was 0 at this moment is:
I don't know who created a thread to process the amba_pm_runtime_suspend(), in which clk_core_disable() was already called, "core->enable_count" was, of course, cleared to zero then. And this thread run before amba_put_disable_pclk(pcdev) which is just the one called from amba_probe() after
"->probe"(i.e. stm_probe in this case) returning a non-zero value.

In a word, if clk_core_disable() is called again after "core->enable_count" has already been cleared to zero in the first round of clk_core_disable()'s.

As such, the WARN_ON occurred.

Quoted 49 lines
Quoted 28 lines
Quoted 28 lines
drivers/hwtracing/stm/core.c new file +27 -0
+static int __init stm_core_init(void)
+{
+       int err;
+
+       err = class_register(&stm_class);
+       if (err)
+               return err;
+
+       err = class_register(&stm_source_class);
+       if (err)
+               goto err_stm;
+
+       err = stp_configfs_init();
+       if (err)
+               goto err_src;
+
+       init_srcu_struct(&stm_source_srcu);
+
+       stm_core_up++;
+
+       return 0;
+
+err_src:
+       class_unregister(&stm_source_class);
+err_stm:
+       class_unregister(&stm_class);
+
Quoted 8 lines

drivers/hwtracing/stm/core.c line 1015

Quoted 4 lines
drivers/hwtracing/stm/core.c new file +4 -0
+       return err;
+}
+
+module_init(stm_core_init);

Since you are using module_init() instead of postcore_initcall() which was in the last version patch, as such, this function would be executed after Coresight "stm_probe" finished.

Yes, iirc on arm the initcall order somehow forced postcore stm_core_init() before configfs, which it relies on, causing a crash. Now I see that somebody hacked configfs to start at core_initcall (f5b697700c8) instead.

There has to be a way to defer stm_probe(), although a quick look at amba code suggests it's not implemented.

Quoted 4 lines

So, we think there a few optional solutions:
1) Remove the "stm_register_device" out from Coresight "stm_probe", but we have to save another global variable:

Quoted 8 lines
Quoted 3 lines
    struct device *stm_dev;

in the process of Coresight "stm_probe".

Sorry, didn't understand this one.

Except for I can say that having a global variable like that is a bad idea, but that's not relevant to the problem at hand.

Agree, it's not a good policy :)

Quoted 9 lines
Quoted 4 lines

2) Change module_init() to other XYX_init() which would run prior to "amba_probe()" (i.e. the caller of Coresight stm_probe), this may be a better one.

I'm really not a big fan of the initcall games, to be honest, it will always be a problem on some architecture or other. Having said that, if stm_core_init() runs at postcore_initcall level, does that solve your problem?

Yes of course, what you were using in your last version of patch was just postcore_initcall(), and it works.

Quoted 9 lines
Quoted 3 lines

3) stm_core_init() could be turned into a library call where initialisation of the internals is done when first called.

Well, it's not that simple: stm is used by both stm and stm_source devices, in this case we'll need to make sure that the first call to either of the {stm,stm_source}_register_device() results in the actual initialization of the stm core. I think it's a cleaner solution than the initcall games, though.

Ok, I see.

Thanks,
Chunyan

Quoted 4 lines

Regards,
--
Alex

#8 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Alexander Shishkin
Quoted 7 lines · Alexander Shishkin
Quoted 5 lines · Mark Brown
Quoted 2 lines · Alexander Shishkin

There has to be a way to defer stm_probe(), although a quick look at amba code suggests it's not implemented.

Quoted 8 lines
Quoted 3 lines

What makes you say this? Probe deferral is implemented in the driver core rather than individual buses, the buses don't need to know anything about it.

I stand corrected, it indeed is.

So returning EPROBE_DEFER from stm_probe() should Just Work (provided stm_probe() handles its error paths correctly).

If let stm_probe() implement probe deferral, it has to have a global variable for the later calling of "stm_register_device", because the first parameter of "stm_register_device" is " struct device * " which comes from amba_probe(), after finished amba_probe(), we may not get this structure by other means.

This was a similar policy that we both thought was not good :)

Quoted 4 lines

Regards,
--
Alex

author maintainer
#9 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

Quoted 2 lines · Chunyan Zhang

If let stm_probe() implement probe deferral, it has to have a global variable for the later calling of "stm_register_device", because the

No, it doesn't. Please read about probe deferral.

Regards,
--
Alex

#10 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

Quoted 6 lines · Alexander Shishkin
Quoted 2 lines · Chunyan Zhang

If let stm_probe() implement probe deferral, it has to have a global variable for the later calling of "stm_register_device", because the

No, it doesn't. Please read about probe deferral.

Could you please read my another email sent out today, I explained more details in that email.

Thanks,
Chunyan

Quoted 4 lines

Regards,
--
Alex

author maintainer
#12 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Chunyan Zhang
Quoted 9 lines · Chunyan Zhang

Sure, I mean, the root reason of this problem is here ( i.e. "stm_core_up" was zero then):

     if (!stm_core_up)
         return -EPROBE_DEFER;

Why it was zero?
Because the function (i.e. stm_core_init() ) in which "stm_core_up" would be added one hasn't been executed at this moment. It would be executed on module_init stage for you this version of patch.

Again, this is the indented behavior.

Quoted 6 lines

The reason of this warning is:
After stm_probe() failed, clk_core_disable() would be called from amba_put_disable_pclk(), then WARN_ON() happened:

         if (WARN_ON(core->enable_count == 0))
                 return;
Quoted 7 lines

I'm guessing the reason why "core->enable_count" was 0 at this moment is:
I don't know who created a thread to process the amba_pm_runtime_suspend(), in which clk_core_disable() was already called, "core->enable_count" was, of course, cleared to zero then. And this thread run before amba_put_disable_pclk(pcdev) which is just the one called from amba_probe() after
"->probe"(i.e. stm_probe in this case) returning a non-zero value.

No, this is guesswork. In amba_probe(), clocks are enabled for the drv->probe() and then disabled afterwards and that's where the refcount ends up unbalanced, the probe is the culprit.

I can debug your driver for you but you'll at least need to put the code up somewhere so I can see it.

Regards,
--
Alex

#13 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

Quoted 28 lines · Alexander Shishkin
Quoted 9 lines · Chunyan Zhang

Sure, I mean, the root reason of this problem is here ( i.e. "stm_core_up" was zero then):

     if (!stm_core_up)
         return -EPROBE_DEFER;

Why it was zero?
Because the function (i.e. stm_core_init() ) in which "stm_core_up" would be added one hasn't been executed at this moment. It would be executed on module_init stage for you this version of patch.

Again, this is the indented behavior.

Quoted 6 lines

The reason of this warning is:
After stm_probe() failed, clk_core_disable() would be called from amba_put_disable_pclk(), then WARN_ON() happened:

         if (WARN_ON(core->enable_count == 0))
                 return;
Quoted 7 lines

I'm guessing the reason why "core->enable_count" was 0 at this moment is:
I don't know who created a thread to process the amba_pm_runtime_suspend(), in which clk_core_disable() was already called, "core->enable_count" was, of course, cleared to zero then. And this thread run before amba_put_disable_pclk(pcdev) which is just the one called from amba_probe() after
"->probe"(i.e. stm_probe in this case) returning a non-zero value.

Quoted 6 lines

No, this is guesswork. In amba_probe(), clocks are enabled for the drv->probe() and then disabled afterwards and that's where the refcount ends up unbalanced, the probe is the culprit.

I can debug your driver for you but you'll at least need to put the code up somewhere so I can see it.

The code has already been submitted like I said in the earlier emails, you may refer [1].

Thanks,
Chunyan

[1] https://lkml.org/lkml/2015/2/4/729

Quoted 5 lines

Regards,
--
Alex

#14 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

Quoted 37 lines · Chunyan Zhang
Quoted 28 lines · Alexander Shishkin
Quoted 9 lines · Chunyan Zhang

Sure, I mean, the root reason of this problem is here ( i.e. "stm_core_up" was zero then):

     if (!stm_core_up)
         return -EPROBE_DEFER;

Why it was zero?
Because the function (i.e. stm_core_init() ) in which "stm_core_up" would be added one hasn't been executed at this moment. It would be executed on module_init stage for you this version of patch.

Again, this is the indented behavior.

Quoted 6 lines

The reason of this warning is:
After stm_probe() failed, clk_core_disable() would be called from amba_put_disable_pclk(), then WARN_ON() happened:

         if (WARN_ON(core->enable_count == 0))
                 return;
Quoted 7 lines

I'm guessing the reason why "core->enable_count" was 0 at this moment is:
I don't know who created a thread to process the amba_pm_runtime_suspend(), in which clk_core_disable() was already called, "core->enable_count" was, of course, cleared to zero then. And this thread run before amba_put_disable_pclk(pcdev) which is just the one called from amba_probe() after
"->probe"(i.e. stm_probe in this case) returning a non-zero value.

Quoted 6 lines

No, this is guesswork. In amba_probe(), clocks are enabled for the drv->probe() and then disabled afterwards and that's where the refcount ends up unbalanced, the probe is the culprit.

I can debug your driver for you but you'll at least need to put the code up somewhere so I can see it.

Quoted 7 lines

The code has already been submitted like I said in the earlier emails, you may refer [1].

Thanks,
Chunyan

[1] https://lkml.org/lkml/2015/2/4/729

Er, sorry, it's too old, I'll send out an updated version soon.

Thanks for your patience,
Chunyan

Quoted 6 lines
Quoted 6 lines

Regards,
--
Alex

author maintainer
#15 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Chunyan Zhang
Quoted 7 lines · Chunyan Zhang

The code has already been submitted like I said in the earlier emails, you may refer [1].

Thanks,
Chunyan

[1] https://lkml.org/lkml/2015/2/4/729

This code does the following (pasting from that patch):

in stm_probe():

drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);

... something irrelevant ...

ret = clk_prepare_enable(drvdata->clk);

... something irrelevant ...

clk_disable_unprepare(drvdata->clk);

... something irrelevant ...

drvdata->clk = adev->pclk;

How is this supposed to even work?

Regards,
--
Alex

author maintainer
#33 lore raw
In reply to Alexander Shishkin
Quoted 9 lines · Alexander Shishkin
Quoted 5 lines · Alexander Shishkin
Quoted 1 line · Alexander Shishkin

Hi Greg and everybody,

Seems like a polite nudge might be in order. :)

Greg.

Ping.

#31 lore raw
In reply to Mathieu Poirier
Quoted 12 lines · Mathieu Poirier
Quoted 9 lines · Alexander Shishkin
Quoted 1 line · Alexander Shishkin

Hi Greg and everybody,

Seems like a polite nudge might be in order. :)

Cheers,
--
Alex

Quoted 8 lines

Greg and all,

I have reviewed the portion of Alex's work that pertains to the generic STM interface - in its current form it is generic enough to cater to both Intel and ARM STM blocks. A few modifications will likely be required when upstreaming of the coresight-stm driver but base on our (Alex and I) previous integration exercise, those should be minor.

So is that a "reviewed-by:" mark for some of these patches from you? I'd really like that to feel better about applying these...

thanks,

greg k-h

Reviewed-by on v3 1/11
#16 lore raw

Re: [PATCH v3 01/11] stm class: Introduce an abstraction for System Trace Module devices

In reply to Alexander Shishkin
Quoted 2,142 lines · Alexander Shishkin

A System Trace Module (STM) is a device exporting data in System Trace Protocol (STP) format as defined by MIPI STP standards. Examples of such devices are Intel Trace Hub and Coresight STM.

This abstraction provides a unified interface for software trace sources to send their data over an STM device to a debug host. In order to do that, such a trace source needs to be assigned a pair of master/channel identifiers that all the data from this source will be tagged with. The STP decoder on the debug host side will use these master/channel tags to distinguish different trace streams from one another inside one STP stream.

This abstraction provides a configfs-based policy management mechanism for dynamic allocation of these master/channel pairs based on trace source-supplied string identifier. It has the flexibility of being defined at runtime and at the same time (provided that the policy definition is aligned with the decoding end) consistency.

For userspace trace sources, this abstraction provides write()-based and mmap()-based (if the underlying stm device allows this) output mechanism.

For kernel-side trace sources, we provide "stm_source" device class that can be connected to an stm device at run time.

---
 Documentation/ABI/testing/configfs-stp-policy    |   48 +
 Documentation/ABI/testing/sysfs-class-stm        |   14 +
 Documentation/ABI/testing/sysfs-class-stm_source |   11 +
 Documentation/ioctl/ioctl-number.txt             |    3 +
 Documentation/trace/stm.txt                      |   80 ++
 drivers/Kconfig                                  |    2 +
 drivers/Makefile                                 |    1 +
 drivers/hwtracing/stm/Kconfig                    |    8 +
 drivers/hwtracing/stm/Makefile                   |    3 +
 drivers/hwtracing/stm/core.c                     | 1029 ++++++++++++++++++++++
 drivers/hwtracing/stm/policy.c                   |  529 +++++++++++
 drivers/hwtracing/stm/stm.h                      |   87 ++
 include/linux/stm.h                              |  126 +++
 include/uapi/linux/stm.h                         |   50 ++
 14 files changed, 1991 insertions(+)
 create mode 100644 Documentation/ABI/testing/configfs-stp-policy
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm
 create mode 100644 Documentation/ABI/testing/sysfs-class-stm_source
 create mode 100644 Documentation/trace/stm.txt
 create mode 100644 drivers/hwtracing/stm/Kconfig
 create mode 100644 drivers/hwtracing/stm/Makefile
 create mode 100644 drivers/hwtracing/stm/core.c
 create mode 100644 drivers/hwtracing/stm/policy.c
 create mode 100644 drivers/hwtracing/stm/stm.h
 create mode 100644 include/linux/stm.h
 create mode 100644 include/uapi/linux/stm.h
Documentation/ABI/testing/configfs-stp-policy new file +48 -0
new file mode 100644
index 0000000000..421ce6825c
@@ -0,0 +1,48 @@
+What:          /config/stp-policy
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               This group contains policies mandating Master/Channel allocation
+               for software sources wishing to send trace data over an STM
+               device.
+
+What:          /config/stp-policy/<device>.<policy>
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               This group is the root of a policy; its name is a concatenation
+               of an stm device name to which this policy applies and an
+               arbitrary string. If <device> part doesn't match an existing
+               stm device, mkdir will fail with ENODEV; if that device already
+               has a policy assigned to it, mkdir will fail with EBUSY.
+
+What:          /config/stp-policy/<device>.<policy>/device
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               STM device to which this policy applies, read only. Same as the
+               <device> component of its parent directory.
+
+What:          /config/stp-policy/<device>.<policy>/<node>
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Policy node is a string identifier that software clients will
+               use to request a master/channel to be allocated and assigned to
+               them.
+
+What:          /config/stp-policy/<device>.<policy>/<node>/masters
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Range of masters from which to allocate for users of this node.
+               Write two numbers: the first master and the last master number.
+
+What:          /config/stp-policy/<device>.<policy>/<node>/channels
+Date:          June 2015
+KernelVersion: 4.3
+Description:
+               Range of channels from which to allocate for users of this node.
+               Write two numbers: the first channel and the last channel
+               number.
+
Documentation/ABI/testing/sysfs-class-stm new file +14 -0
new file mode 100644
index 0000000000..c9aa4f3fc9
@@ -0,0 +1,14 @@
+What:          /sys/class/stm/<stm>/masters
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
+Description:
+               Shows first and last available to software master numbers on
+               this STM device.
+
+What:          /sys/class/stm/<stm>/channels
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
+Description:
+               Shows the number of channels per master on this STM device.
Documentation/ABI/testing/sysfs-class-stm_source new file +11 -0
new file mode 100644
index 0000000000..57b8dd39bb
@@ -0,0 +1,11 @@
+What:          /sys/class/stm_source/<stm_source>/stm_source_link
+Date:          June 2015
+KernelVersion: 4.3
+Contact:       Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
+Description:
+               stm_source device linkage to stm device, where its tracing data
+               is directed. Reads return an existing connection or "<none>" if
+               this stm_source is not connected to any stm device yet.
+               Write an existing (registered) stm device's name here to
+               connect that device. If a device is already connected to this
+               stm_source, it will first be disconnected.
Documentation/ioctl/ioctl-number.txt +3 -0
index 611c52267d..7fa29b4ece 100644
@@ -81,6 +81,9 @@ Code  Seq#(hex)       Include File            Comments
 0x22   all     scsi/sg.h
 '#'    00-3F   IEEE 1394 Subsystem     Block for the entire subsystem
 '$'    00-0F   linux/perf_counter.h, linux/perf_event.h
+'%'    00-0F   include/uapi/linux/stm.h
+                                       System Trace Module subsystem
+                                       <mailto:alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>
 '&'    00-07   drivers/firewire/nosy-user.h
 '1'    00-1F   <linux/timepps.h>       PPS kit from Ulrich Windl
                                        <ftp://ftp.de.kernel.org/pub/linux/daemons/ntp/PPS/>
Documentation/trace/stm.txt new file +80 -0
new file mode 100644
index 0000000000..e140f2e8d7
@@ -0,0 +1,80 @@
+System Trace Module
+===================
+
+System Trace Module (STM) is a device described in MIPI STP specs as
+STP trace stream generator. STP (System Trace Protocol) is a trace
+protocol multiplexing data from multiple trace sources, each one of
+which is assigned a unique pair of master and channel. While some of
+these masters and channels are statically allocated to certain
+hardware trace sources, others are available to software. Software
+trace sources are usually free to pick for themselves any
+master/channel combination from this pool.
+
+On the receiving end of this STP stream (the decoder side), trace
+sources can only be identified by master/channel combination, so in
+order for the decoder to be able to make sense of the trace that
+involves multiple trace sources, it needs to be able to map those
+master/channel pairs to the trace sources that it understands.
+
+For instance, it is helpful to know that syslog messages come on
+master 7 channel 15, while arbitrary user applications can use masters
+48 to 63 and channels 0 to 127.
+
+To solve this mapping problem, stm class provides a policy management
+mechanism via configfs, that allows defining rules that map string
+identifiers to ranges of masters and channels. If these rules (policy)
+are consistent with what decoder expects, it will be able to properly
+process the trace data.
+
+This policy is a tree structure containing rules (policy_node) that
+have a name (string identifier) and a range of masters and channels
+associated with it, located in "stp-policy" subsystem directory in
+configfs. The topmost directory's name (the policy) is formatted as
+the STM device name to which this policy applies and and arbitrary
+string identifier separated by a stop. From the examle above, a rule
+may look like this:
+
+$ ls /config/stp-policy/dummy_stm.my-policy/user
+channels masters
+$ cat /config/stp-policy/dummy_stm.my-policy/user/masters
+48 63
+$ cat /config/stp-policy/dummy_stm.my-policy/user/channels
+0 127
+
+which means that the master allocation pool for this rule consists of
+masters 48 through 63 and channel allocation pool has channels 0
+through 127 in it. Now, any producer (trace source) identifying itself
+with "user" identification string will be allocated a master and
+channel from within these ranges.
+
+These rules can be nested, for example, one can define a rule "dummy"
+under "user" directory from the example above and this new rule will
+be used for trace sources with the id string of "user/dummy".
+
+Trace sources have to open the stm class device's node and write their
+trace data into its file descriptor. In order to identify themselves
+to the policy, they need to do a STP_POLICY_ID_SET ioctl on this file
+descriptor providing their id string. Otherwise, they will be
+automatically allocated a master/channel pair upon first write to this
+file descriptor according to the "default" rule of the policy, if such
+exists.
+
+Some STM devices may allow direct mapping of the channel mmio regions
+to userspace for zero-copy writing. One mappable page (in terms of
+mmu) will usually contain multiple channels' mmios, so the user will
+need to allocate that many channels to themselves (via the
+aforementioned ioctl() call) to be able to do this. That is, if your
+stm device's channel mmio region is 64 bytes and hardware page size is
+4096 bytes, after a successful STP_POLICY_ID_SET ioctl() call with
+width==64, you should be able to mmap() one page on this file
+descriptor and obtain direct access to an mmio region for 64 channels.
+
+For kernel-based trace sources, there is "stm_source" device
+class. Devices of this class can be connected and disconnected to/from
+stm devices at runtime via a sysfs attribute.
+
+Examples of STM devices are Intel Trace Hub [1] and Coresight STM
+[2].
+
+[1] https://software.intel.com/sites/default/files/managed/d3/3c/intel-th-developer-manual.pdf
+[2] http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0444b/index.html
drivers/Kconfig +2 -0
index 6e973b8e3a..96efe4522c 100644
@@ -184,4 +184,6 @@ source "drivers/android/Kconfig"
 
 source "drivers/nvdimm/Kconfig"
 
+source "drivers/hwtracing/stm/Kconfig"
+
 endmenu
drivers/Makefile +1 -0
index b64b49f6e0..6647bc923f 100644
@@ -164,4 +164,5 @@ obj-$(CONFIG_MCB)           += mcb/
 obj-$(CONFIG_RAS)              += ras/
 obj-$(CONFIG_THUNDERBOLT)      += thunderbolt/
 obj-$(CONFIG_CORESIGHT)                += hwtracing/coresight/
+obj-$(CONFIG_STM)              += hwtracing/stm/
 obj-$(CONFIG_ANDROID)          += android/
drivers/hwtracing/stm/Kconfig new file +8 -0
new file mode 100644
index 0000000000..90ed327461
@@ -0,0 +1,8 @@
+config STM
+       tristate "System Trace Module devices"
+       help
+         A System Trace Module (STM) is a device exporting data in System
+         Trace Protocol (STP) format as defined by MIPI STP standards.
+         Examples of such devices are Intel Trace Hub and Coresight STM.
+
+         Say Y here to enable System Trace Module device support.
drivers/hwtracing/stm/Makefile new file +3 -0
new file mode 100644
index 0000000000..adec701649
@@ -0,0 +1,3 @@
+obj-$(CONFIG_STM)      += stm_core.o
+
+stm_core-y             := core.o policy.o
drivers/hwtracing/stm/core.c new file +1029 -0
new file mode 100644
index 0000000000..b79c42c625
@@ -0,0 +1,1029 @@
+/*
+ * System Trace Module (STM) infrastructure
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#include <linux/uaccess.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/compat.h>
+#include <linux/kdev_t.h>
+#include <linux/srcu.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+#include <linux/fs.h>
+#include <linux/mm.h>
+#include "stm.h"
+
+#include <uapi/linux/stm.h>
+
+static unsigned int stm_core_up;
+
+/*
+ * The SRCU here makes sure that STM device doesn't disappear from under a
+ * stm_source_write() caller, which may want to have as little overhead as
+ * possible.
+ */
+static struct srcu_struct stm_source_srcu;
+
+static ssize_t masters_show(struct device *dev,
+                           struct device_attribute *attr,
+                           char *buf)
+{
+       struct stm_device *stm = to_stm_device(dev);
+       int ret;
+
+       ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
+
+       return ret;
+}
+
+static DEVICE_ATTR_RO(masters);
+
+static ssize_t channels_show(struct device *dev,
+                            struct device_attribute *attr,
+                            char *buf)
+{
+       struct stm_device *stm = to_stm_device(dev);
+       int ret;
+
+       ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
+
+       return ret;
+}
+
+static DEVICE_ATTR_RO(channels);
+
+static struct attribute *stm_attrs[] = {
+       &dev_attr_masters.attr,
+       &dev_attr_channels.attr,
+       NULL,
+};
+
+ATTRIBUTE_GROUPS(stm);
+
+static struct class stm_class = {
+       .name           = "stm",
+       .dev_groups     = stm_groups,
+};
+
+static int stm_dev_match(struct device *dev, const void *data)
+{
+       const char *name = data;
+
+       return sysfs_streq(name, dev_name(dev));
+}
+
+/**
+ * stm_find_device() - find stm device by name
+ * @buf:       character buffer containing the name
+ *
+ * This is called when either policy gets assigned to an stm device or an
+ * stm_source device gets linked to an stm device.
+ *
+ * This grabs device's reference (get_device()) and module reference, both
+ * of which the calling path needs to make sure to drop with stm_put_device().
+ *
+ * Return:     stm device pointer or null if lookup failed.
+ */
+struct stm_device *stm_find_device(const char *buf)
+{
+       struct stm_device *stm;
+       struct device *dev;
+
+       if (!stm_core_up)
+               return NULL;
+
+       dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
+       if (!dev)
+               return NULL;
+
+       stm = to_stm_device(dev);
+       if (!try_module_get(stm->owner)) {
+               put_device(dev);
+               return NULL;
+       }
+
+       return stm;
+}
+
+/**
+ * stm_put_device() - drop references on the stm device
+ * @stm:       stm device, previously acquired by stm_find_device()
+ *
+ * This drops the module reference and device reference taken by
+ * stm_find_device().
+ */
+void stm_put_device(struct stm_device *stm)
+{
+       module_put(stm->owner);
+       put_device(&stm->dev);
+}
+
+/*
+ * Internally we only care about software-writable masters here, that is the
+ * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
+ * original master numbers to be visible externally, since they are the ones
+ * that will appear in the STP stream. Thus, the internal bookkeeping uses
+ * $master - stm_data->sw_start to reference master descriptors and such.
+ */
+
+#define __stm_master(_s, _m)                           \
+       ((_s)->masters[(_m) - (_s)->data->sw_start])
+
+static inline struct stp_master *
+stm_master(struct stm_device *stm, unsigned int idx)
+{
+       if (idx < stm->data->sw_start || idx > stm->data->sw_end)
+               return NULL;
+
+       return __stm_master(stm, idx);
+}
+
+static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
+{
+       struct stp_master *master;
+       size_t size;
+
+       size = ALIGN(stm->data->sw_nchannels, 8) / 8;
+       size += sizeof(struct stp_master);
+       master = kzalloc(size, GFP_ATOMIC);
+       if (!master)
+               return -ENOMEM;
+
+       master->nr_free = stm->data->sw_nchannels;
+       __stm_master(stm, idx) = master;
+
+       return 0;
+}
+
+static void stp_master_free(struct stm_device *stm, unsigned int idx)
+{
+       struct stp_master *master = stm_master(stm, idx);
+
+       if (!master)
+               return;
+
+       __stm_master(stm, idx) = NULL;
+       kfree(master);
+}
+
+static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
+{
+       struct stp_master *master = stm_master(stm, output->master);
+
+       if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
+               return;
+
+       bitmap_allocate_region(&master->chan_map[0], output->channel,
+                              ilog2(output->nr_chans));
+
+       master->nr_free -= output->nr_chans;
+}
+
+static void
+stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
+{
+       struct stp_master *master = stm_master(stm, output->master);
+
+       bitmap_release_region(&master->chan_map[0], output->channel,
+                             ilog2(output->nr_chans));
+
+       output->nr_chans = 0;
+       master->nr_free += output->nr_chans;
+}
+
+/*
+ * This is like bitmap_find_free_region(), except it can ignore @start bits
+ * at the beginning.
+ */
+static int find_free_channels(unsigned long *bitmap, unsigned int start,
+                             unsigned int end, unsigned int width)
+{
+       unsigned int pos;
+       int i;
+
+       for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
+               pos = find_next_zero_bit(bitmap, end + 1, pos);
+               if (pos + width > end + 1)
+                       break;
+
+               if (pos & (width - 1))
+                       continue;
+
+               for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
+                       ;
+               if (i == width)
+                       return pos;
+       }
+
+       return -1;
+}
+
+static unsigned int
+stm_find_master_chan(struct stm_device *stm, unsigned int width,
+                    unsigned int *mstart, unsigned int mend,
+                    unsigned int *cstart, unsigned int cend)
+{
+       struct stp_master *master;
+       unsigned int midx;
+       int pos, err;
+
+       for (midx = *mstart; midx <= mend; midx++) {
+               if (!stm_master(stm, midx)) {
+                       err = stp_master_alloc(stm, midx);
+                       if (err)
+                               return err;
+               }
+
+               master = stm_master(stm, midx);
+
+               if (!master->nr_free)
+                       continue;
+
+               pos = find_free_channels(master->chan_map, *cstart, cend,
+                                        width);
+               if (pos < 0)
+                       continue;
+
+               *mstart = midx;
+               *cstart = pos;
+               return 0;
+       }
+
+       return -ENOSPC;
+}
+
+static int stm_output_assign(struct stm_device *stm, unsigned int width,
+                            struct stp_policy_node *policy_node,
+                            struct stm_output *output)
+{
+       unsigned int midx, cidx, mend, cend;
+       int ret = -EINVAL;
+
+       if (width > stm->data->sw_nchannels)
+               return -EINVAL;
+
+       if (policy_node) {
+               stp_policy_node_get_ranges(policy_node,
+                                          &midx, &mend, &cidx, &cend);
+       } else {
+               midx = stm->data->sw_start;
+               cidx = 0;
+               mend = stm->data->sw_end;
+               cend = stm->data->sw_nchannels - 1;
+       }
+
+       spin_lock(&stm->mc_lock);
+       /* output is already assigned -- shouldn't happen */
+       if (WARN_ON_ONCE(output->nr_chans))
+               goto unlock;
+
+       ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
+       if (ret)
+               goto unlock;
+
+       output->master = midx;
+       output->channel = cidx;
+       output->nr_chans = width;
+       stm_output_claim(stm, output);
+       dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
+
+       ret = 0;
+unlock:
+       spin_unlock(&stm->mc_lock);
+
+       return ret;
+}
+
+static void stm_output_free(struct stm_device *stm, struct stm_output *output)
+{
+       spin_lock(&stm->mc_lock);
+       if (output->nr_chans)
+               stm_output_disclaim(stm, output);
+       spin_unlock(&stm->mc_lock);
+}
+
+static int major_match(struct device *dev, const void *data)
+{
+       unsigned int major = *(unsigned int *)data;
+
+       return MAJOR(dev->devt) == major;
+}
+
+static int stm_char_open(struct inode *inode, struct file *file)
+{
+       struct stm_file *stmf;
+       struct device *dev;
+       unsigned int major = imajor(inode);
+       int err = -ENODEV;
+
+       dev = class_find_device(&stm_class, NULL, &major, major_match);
+       if (!dev)
+               return -ENODEV;
+
+       stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
+       if (!stmf)
+               return -ENOMEM;
+
+       stmf->stm = to_stm_device(dev);
+
+       if (!try_module_get(stmf->stm->owner))
+               goto err_free;
+
+       file->private_data = stmf;
+
+       return nonseekable_open(inode, file);
+
+err_free:
+       kfree(stmf);
+
+       return err;
+}
+
+static int stm_char_release(struct inode *inode, struct file *file)
+{
+       struct stm_file *stmf = file->private_data;
+
+       stm_output_free(stmf->stm, &stmf->output);
+       stm_put_device(stmf->stm);
+       kfree(stmf);
+
+       return 0;
+}
+
+static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
+{
+       struct stm_device *stm = stmf->stm;
+       int ret;
+
+       stmf->policy_node = stp_policy_node_lookup(stm, id);
+
+       ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
+
+       if (stmf->policy_node)
+               stp_policy_node_put(stmf->policy_node);
+
+       return ret;
+}
+
+static void stm_write(struct stm_data *data, unsigned int master,
+                     unsigned int channel, const char *buf, size_t count)
+{
+       unsigned int flags = STP_PACKET_TIMESTAMPED;
+       const unsigned char *p = buf, nil = 0;
+       size_t pos;
+       ssize_t sz;
+
+       for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
+               sz = min_t(unsigned int, count - pos, 8);
+               sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
+                                 sz, p);
+               flags = 0;
+       }
+
+       data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
+}
+
+static ssize_t stm_char_write(struct file *file, const char __user *buf,
+                             size_t count, loff_t *ppos)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_device *stm = stmf->stm;
+       char *kbuf;
+       int err;
+
+       /*
+        * if no m/c have been assigned to this writer up to this
+        * point, use "default" policy entry
+        */
+       if (!stmf->output.nr_chans) {
+               err = stm_file_assign(stmf, "default", 1);
+               /*
+                * EBUSY means that somebody else just assigned this
+                * output, which is just fine for write()
+                */
+               if (err && err != -EBUSY)
+                       return err;
+       }
+
+       kbuf = kmalloc(count + 1, GFP_KERNEL);
+       if (!kbuf)
+               return -ENOMEM;
+
+       err = copy_from_user(kbuf, buf, count);
+       if (err) {
+               kfree(kbuf);
+               return -EFAULT;
+       }
+
+       stm_write(stm->data, stmf->output.master, stmf->output.channel, kbuf,
+                 count);
+
+       kfree(kbuf);
+
+       return count;
+}
+
+static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_device *stm = stmf->stm;
+       unsigned long size, phys;
+
+       if (!stm->data->mmio_addr)
+               return -EOPNOTSUPP;
+
+       if (vma->vm_pgoff)
+               return -EINVAL;
+
+       size = vma->vm_end - vma->vm_start;
+
+       if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
+               return -EINVAL;
+
+       phys = stm->data->mmio_addr(stm->data, stmf->output.master,
+                                   stmf->output.channel,
+                                   stmf->output.nr_chans);
+
+       if (!phys)
+               return -EINVAL;
+
+       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+       vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
+       vm_iomap_memory(vma, phys, size);
+
+       return 0;
+}
+
+static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
+{
+       struct stm_device *stm = stmf->stm;
+       struct stp_policy_id *id;
+       int ret = -EINVAL;
+       u32 size;
+
+       if (stmf->output.nr_chans)
+               return -EBUSY;
+
+       if (copy_from_user(&size, arg, sizeof(size)))
+               return -EFAULT;
+
+       if (size >= PATH_MAX + sizeof(*id))
+               return -EINVAL;
+
+       /*
+        * size + 1 to make sure the .id string at the bottom is terminated,
+        * which is also why memdup_user() is not useful here
+        */
+       id = kzalloc(size + 1, GFP_KERNEL);
+       if (!id)
+               return -ENOMEM;
+
+       if (copy_from_user(id, arg, size)) {
+               ret = -EFAULT;
+               goto err_free;
+       }
+
+       if (id->__reserved_0 || id->__reserved_1)
+               goto err_free;
+
+       if (id->width < 1 ||
+           id->width > PAGE_SIZE / stm->data->sw_mmiosz)
+               goto err_free;
+
+       ret = stm_file_assign(stmf, id->id, id->width);
+       if (ret)
+               goto err_free;
+
+       ret = 0;
+
+       if (stm->data->link)
+               ret = stm->data->link(stm->data, stmf->output.master,
+                                     stmf->output.channel);
+
+       if (ret) {
+               stm_output_free(stmf->stm, &stmf->output);
+               stm_put_device(stmf->stm);
+       }
+
+err_free:
+       kfree(id);
+
+       return ret;
+}
+
+static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
+{
+       struct stp_policy_id id = {
+               .size           = sizeof(id),
+               .master         = stmf->output.master,
+               .channel        = stmf->output.channel,
+               .width          = stmf->output.nr_chans,
+               .__reserved_0   = 0,
+               .__reserved_1   = 0,
+       };
+
+       return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
+}
+
+static long
+stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       struct stm_file *stmf = file->private_data;
+       struct stm_data *stm_data = stmf->stm->data;
+       int err = -ENOTTY;
+       u64 options;
+
+       switch (cmd) {
+       case STP_POLICY_ID_SET:
+               err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
+               if (err)
+                       return err;
+
+               return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
+
+       case STP_POLICY_ID_GET:
+               return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
+
+       case STP_SET_OPTIONS:
+               if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
+                       return -EFAULT;
+
+               if (stm_data->set_options)
+                       err = stm_data->set_options(stm_data,
+                                                   stmf->output.master,
+                                                   stmf->output.channel,
+                                                   stmf->output.nr_chans,
+                                                   options);
+
+               break;
+       default:
+               break;
+       }
+
+       return err;
+}
+
+#ifdef CONFIG_COMPAT
+static long
+stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+       return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
+}
+#else
+#define stm_char_compat_ioctl  NULL
+#endif
+
+static const struct file_operations stm_fops = {
+       .open           = stm_char_open,
+       .release        = stm_char_release,
+       .write          = stm_char_write,
+       .mmap           = stm_char_mmap,
+       .unlocked_ioctl = stm_char_ioctl,
+       .compat_ioctl   = stm_char_compat_ioctl,
+       .llseek         = no_llseek,
+};
+
+static void stm_device_release(struct device *dev)
+{
+       struct stm_device *stm = to_stm_device(dev);
+
+       kfree(stm);
+}
+
+int stm_register_device(struct device *parent, struct stm_data *stm_data,
+                       struct module *owner)
+{
+       struct stm_device *stm;
+       unsigned int nmasters;
+       int err = -ENOMEM;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
+
+       if (!stm_data->packet || !stm_data->sw_nchannels)
+               return -EINVAL;
+
+       nmasters = stm_data->sw_end - stm_data->sw_start;
+       stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
+       if (!stm)
+               return -ENOMEM;
+
+       stm->major = register_chrdev(0, stm_data->name, &stm_fops);
+       if (stm->major < 0)
+               goto err_free;
+
+       device_initialize(&stm->dev);
+       stm->dev.devt = MKDEV(stm->major, 0);
+       stm->dev.class = &stm_class;
+       stm->dev.parent = parent;
+       stm->dev.release = stm_device_release;
+
+       err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
+       if (err)
+               goto err_device;
+
+       err = device_add(&stm->dev);
+       if (err)
+               goto err_device;
+
+       spin_lock_init(&stm->link_lock);
+       INIT_LIST_HEAD(&stm->link_list);
+
+       spin_lock_init(&stm->mc_lock);
+       mutex_init(&stm->policy_mutex);
+       stm->sw_nmasters = nmasters;
+       stm->owner = owner;
+       stm->data = stm_data;
+       stm_data->stm = stm;
+
+       return 0;
+
+err_device:
+       put_device(&stm->dev);
+err_free:
+       kfree(stm);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(stm_register_device);
+
+static void __stm_source_link_drop(struct stm_source_device *src,
+                                  struct stm_device *stm);
+
+void stm_unregister_device(struct stm_data *stm_data)
+{
+       struct stm_device *stm = stm_data->stm;
+       struct stm_source_device *src, *iter;
+       int i;
+
+       spin_lock(&stm->link_lock);
+       list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
+               __stm_source_link_drop(src, stm);
+       }
+       spin_unlock(&stm->link_lock);
+
+       synchronize_srcu(&stm_source_srcu);
+
+       unregister_chrdev(stm->major, stm_data->name);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy)
+               stp_policy_unbind(stm->policy);
+       mutex_unlock(&stm->policy_mutex);
+
+       for (i = 0; i < stm->sw_nmasters; i++)
+               stp_master_free(stm, i);
+
+       device_unregister(&stm->dev);
+       stm_data->stm = NULL;
+}
+EXPORT_SYMBOL_GPL(stm_unregister_device);
+
+/**
+ * stm_source_link_add() - connect an stm_source device to an stm device
+ * @src:       stm_source device
+ * @stm:       stm device
+ *
+ * This function establishes a link from stm_source to an stm device so that
+ * the former can send out trace data to the latter.
+ *
+ * Return:     0 on success, -errno otherwise.
+ */
+static int stm_source_link_add(struct stm_source_device *src,
+                              struct stm_device *stm)
+{
+       char *id;
+       int err;
+
+       spin_lock(&stm->link_lock);
+       spin_lock(&src->link_lock);
+
+       /* src->link is dereferenced under stm_source_srcu but not the list */
+       rcu_assign_pointer(src->link, stm);
+       list_add_tail(&src->link_entry, &stm->link_list);
+
+       spin_unlock(&src->link_lock);
+       spin_unlock(&stm->link_lock);
+
+       id = kstrdup(src->data->name, GFP_KERNEL);
+       if (id) {
+               src->policy_node =
+                       stp_policy_node_lookup(stm, id);
+
+               kfree(id);
+       }
+
+       err = stm_output_assign(stm, src->data->nr_chans,
+                               src->policy_node, &src->output);
+
+       if (src->policy_node)
+               stp_policy_node_put(src->policy_node);
+
+       if (err)
+               goto fail_detach;
+
+       /* this is to notify the STM device that a new link has been made */
+       if (stm->data->link)
+               err = stm->data->link(stm->data, src->output.master,
+                                     src->output.channel);
+
+       if (err)
+               goto fail_free_output;
+
+       /* this is to let the source carry out all necessary preparations */
+       if (src->data->link)
+               src->data->link(src->data);
+
+       return 0;
+
+fail_free_output:
+       stm_output_free(stm, &src->output);
+       stm_put_device(stm);
+
+fail_detach:
+       spin_lock(&stm->link_lock);
+       spin_lock(&src->link_lock);
+
+       rcu_assign_pointer(src->link, NULL);
+       list_del_init(&src->link_entry);
+
+       spin_unlock(&src->link_lock);
+       spin_unlock(&stm->link_lock);
+
+       return err;
+}
+
+/**
+ * __stm_source_link_drop() - detach stm_source from an stm device
+ * @src:       stm_source device
+ * @stm:       stm device
+ *
+ * If @stm is @src::link, disconnect them from one another and put the
+ * reference on the @stm device.
+ *
+ * Caller must hold stm::link_lock.
+ */
+static void __stm_source_link_drop(struct stm_source_device *src,
+                                  struct stm_device *stm)
+{
+       spin_lock(&src->link_lock);
+       if (WARN_ON_ONCE(src->link != stm)) {
+               spin_unlock(&src->link_lock);
+               return;
+       }
+
+       stm_output_free(src->link, &src->output);
+       /* caller must hold stm::link_lock */
+       list_del_init(&src->link_entry);
+       /* matches stm_find_device() from stm_source_link_store() */
+       stm_put_device(src->link);
+       rcu_assign_pointer(src->link, NULL);
+
+       spin_unlock(&src->link_lock);
+}
+
+/**
+ * stm_source_link_drop() - detach stm_source from its stm device
+ * @src:       stm_source device
+ *
+ * Unlinking means disconnecting from source's STM device; after this
+ * writes will be unsuccessful until it is linked to a new STM device.
+ *
+ * This will happen on "stm_source_link" sysfs attribute write to undo
+ * the existing link (if any), or on linked STM device's de-registration.
+ */
+static void stm_source_link_drop(struct stm_source_device *src)
+{
+       struct stm_device *stm;
+       int idx;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+
+       if (stm) {
+               if (src->data->unlink)
+                       src->data->unlink(src->data);
+
+               spin_lock(&stm->link_lock);
+               __stm_source_link_drop(src, stm);
+               spin_unlock(&stm->link_lock);
+       }
+
+       srcu_read_unlock(&stm_source_srcu, idx);
+}
+
+static ssize_t stm_source_link_show(struct device *dev,
+                                   struct device_attribute *attr,
+                                   char *buf)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+       struct stm_device *stm;
+       int idx, ret;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+       ret = sprintf(buf, "%s\n",
+                     stm ? dev_name(&stm->dev) : "<none>");
+       srcu_read_unlock(&stm_source_srcu, idx);
+
+       return ret;
+}
+
+static ssize_t stm_source_link_store(struct device *dev,
+                                    struct device_attribute *attr,
+                                    const char *buf, size_t count)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+       struct stm_device *link;
+       int err;
+
+       stm_source_link_drop(src);
+
+       link = stm_find_device(buf);
+       if (!link)
+               return -EINVAL;
+
+       err = stm_source_link_add(src, link);
+       if (err)
+               stm_put_device(link);
+
+       return err ? : count;
+}
+
+static DEVICE_ATTR_RW(stm_source_link);
+
+static struct attribute *stm_source_attrs[] = {
+       &dev_attr_stm_source_link.attr,
+       NULL,
+};
+
+ATTRIBUTE_GROUPS(stm_source);
+
+static struct class stm_source_class = {
+       .name           = "stm_source",
+       .dev_groups     = stm_source_groups,
+};
+
+static void stm_source_device_release(struct device *dev)
+{
+       struct stm_source_device *src = to_stm_source_device(dev);
+
+       kfree(src);
+}
+
+/**
+ * stm_source_register_device() - register an stm_source device
+ * @parent:    parent device
+ * @data:      device description structure
+ *
+ * This will create a device of stm_source class that can write
+ * data to an stm device once linked.
+ *
+ * Return:     0 on success, -errno otherwise.
+ */
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data)
+{
+       struct stm_source_device *src;
+       int err;
+
+       if (!stm_core_up)
+               return -EPROBE_DEFER;
+
+       src = kzalloc(sizeof(*src), GFP_KERNEL);
+       if (!src)
+               return -ENOMEM;
+
+       device_initialize(&src->dev);
+       src->dev.class = &stm_source_class;
+       src->dev.parent = parent;
+       src->dev.release = stm_source_device_release;
+
+       err = kobject_set_name(&src->dev.kobj, "%s", data->name);
+       if (err)
+               goto err;
+
+       err = device_add(&src->dev);
+       if (err)
+               goto err;
+
+       spin_lock_init(&src->link_lock);
+       INIT_LIST_HEAD(&src->link_entry);
+       src->data = data;
+       data->src = src;
+
+       return 0;
+
+err:
+       put_device(&src->dev);
+       kfree(src);
+
+       return err;
+}
+EXPORT_SYMBOL_GPL(stm_source_register_device);
+
+/**
+ * stm_source_unregister_device() - unregister an stm_source device
+ * @data:      device description that was used to register the device
+ *
+ * This will remove a previously created stm_source device from the system.
+ */
+void stm_source_unregister_device(struct stm_source_data *data)
+{
+       struct stm_source_device *src = data->src;
+
+       stm_source_link_drop(src);
+
+       device_destroy(&stm_source_class, src->dev.devt);
+}
+EXPORT_SYMBOL_GPL(stm_source_unregister_device);
+
+int stm_source_write(struct stm_source_data *data, unsigned int chan,
+                    const char *buf, size_t count)
+{
+       struct stm_source_device *src = data->src;
+       struct stm_device *stm;
+       int idx;
+
+       if (!src->output.nr_chans)
+               return -ENODEV;
+
+       if (chan >= src->output.nr_chans)
+               return -EINVAL;
+
+       idx = srcu_read_lock(&stm_source_srcu);
+
+       stm = srcu_dereference(src->link, &stm_source_srcu);
+       if (stm)
+               stm_write(stm->data, src->output.master,
+                         src->output.channel + chan,
+                         buf, count);
+       else
+               count = -ENODEV;
+
+       srcu_read_unlock(&stm_source_srcu, idx);
+
+       return count;
+}
+EXPORT_SYMBOL_GPL(stm_source_write);
+
+static int __init stm_core_init(void)
+{
+       int err;
+
+       err = class_register(&stm_class);
+       if (err)
+               return err;
+
+       err = class_register(&stm_source_class);
+       if (err)
+               goto err_stm;
+
+       err = stp_configfs_init();
+       if (err)
+               goto err_src;
+
+       init_srcu_struct(&stm_source_srcu);
+
+       stm_core_up++;
+
+       return 0;
+
+err_src:
+       class_unregister(&stm_source_class);
+err_stm:
+       class_unregister(&stm_class);
+
+       return err;
+}
+
+module_init(stm_core_init);
+
+static void __exit stm_core_exit(void)
+{
+       cleanup_srcu_struct(&stm_source_srcu);
+       class_unregister(&stm_source_class);
+       class_unregister(&stm_class);
+       stp_configfs_exit();
+}
+
+module_exit(stm_core_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("System Trace Module device class");
+MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin-VuQAYsv1563Yd54FQh9/[email protected]>");
drivers/hwtracing/stm/policy.c new file +529 -0
new file mode 100644
index 0000000000..6498a9dbb7
@@ -0,0 +1,529 @@
+/*
+ * System Trace Module (STM) master/channel allocation policy management
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * A master/channel allocation policy allows mapping string identifiers to
+ * master and channel ranges, where allocation can be done.
+ */
+
+#define pr_fmt(fmt)    KBUILD_MODNAME ": " fmt
+
+#include <linux/types.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/configfs.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+#include "stm.h"
+
+/*
+ * STP Master/Channel allocation policy configfs layout.
+ */
+
+struct stp_policy {
+       struct config_group     group;
+       struct stm_device       *stm;
+};
+
+struct stp_policy_node {
+       struct config_group     group;
+       struct stp_policy       *policy;
+       unsigned int            first_master;
+       unsigned int            last_master;
+       unsigned int            first_channel;
+       unsigned int            last_channel;
+};
+
+static struct configfs_subsystem stp_policy_subsys;
+
+void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
+                               unsigned int *mstart, unsigned int *mend,
+                               unsigned int *cstart, unsigned int *cend)
+{
+       *mstart = policy_node->first_master;
+       *mend   = policy_node->last_master;
+       *cstart = policy_node->first_channel;
+       *cend   = policy_node->last_channel;
+}
+
+static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
+{
+       return policy_node->group.cg_item.ci_name ? : "<none>";
+}
+
+static inline struct stp_policy *to_stp_policy(struct config_item *item)
+{
+       return item ?
+               container_of(to_config_group(item), struct stp_policy, group) :
+               NULL;
+}
+
+static inline struct stp_policy_node *
+to_stp_policy_node(struct config_item *item)
+{
+       return item ?
+               container_of(to_config_group(item), struct stp_policy_node,
+                            group) :
+               NULL;
+}
+
+static ssize_t stp_policy_node_masters_show(struct stp_policy_node *policy_node,
+                                           char *page)
+{
+       ssize_t count;
+
+       count = sprintf(page, "%u %u\n", policy_node->first_master,
+                       policy_node->last_master);
+
+       return count;
+}
+
+static ssize_t
+stp_policy_node_masters_store(struct stp_policy_node *policy_node,
+                             const char *page, size_t count)
+{
+       unsigned int first, last;
+       struct stm_device *stm;
+       char *p = (char *)page;
+       ssize_t ret = -ENODEV;
+
+       if (sscanf(p, "%u %u", &first, &last) != 2)
+               return -EINVAL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+       stm = policy_node->policy->stm;
+       if (!stm)
+               goto unlock;
+
+       /* must be within [sw_start..sw_end], which is an inclusive range */
+       if (first > INT_MAX || last > INT_MAX || first > last ||
+           first < stm->data->sw_start ||
+           last > stm->data->sw_end) {
+               ret = -ERANGE;
+               goto unlock;
+       }
+
+       ret = count;
+       policy_node->first_master = first;
+       policy_node->last_master = last;
+
+unlock:
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return ret;
+}
+
+static ssize_t
+stp_policy_node_channels_show(struct stp_policy_node *policy_node, char *page)
+{
+       ssize_t count;
+
+       count = sprintf(page, "%u %u\n", policy_node->first_channel,
+                       policy_node->last_channel);
+
+       return count;
+}
+
+static ssize_t
+stp_policy_node_channels_store(struct stp_policy_node *policy_node,
+                              const char *page, size_t count)
+{
+       unsigned int first, last;
+       struct stm_device *stm;
+       char *p = (char *)page;
+       ssize_t ret = -ENODEV;
+
+       if (sscanf(p, "%u %u", &first, &last) != 2)
+               return -EINVAL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+       stm = policy_node->policy->stm;
+       if (!stm)
+               goto unlock;
+
+       if (first > INT_MAX || last > INT_MAX || first > last ||
+           last >= stm->data->sw_nchannels) {
+               ret = -ERANGE;
+               goto unlock;
+       }
+
+       ret = count;
+       policy_node->first_channel = first;
+       policy_node->last_channel = last;
+
+unlock:
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return ret;
+}
+
+static void stp_policy_node_release(struct config_item *item)
+{
+       kfree(to_stp_policy_node(item));
+}
+
+struct stp_policy_node_attribute {
+       struct configfs_attribute       attr;
+       ssize_t (*show)(struct stp_policy_node *, char *);
+       ssize_t (*store)(struct stp_policy_node *, const char *, size_t);
+};
+
+static ssize_t stp_policy_node_attr_show(struct config_item *item,
+                                        struct configfs_attribute *attr,
+                                        char *page)
+{
+       struct stp_policy_node *policy_node = to_stp_policy_node(item);
+       struct stp_policy_node_attribute *pn_attr =
+               container_of(attr, struct stp_policy_node_attribute, attr);
+       ssize_t count = 0;
+
+       if (pn_attr->show)
+               count = pn_attr->show(policy_node, page);
+
+       return count;
+}
+
+static ssize_t stp_policy_node_attr_store(struct config_item *item,
+                                         struct configfs_attribute *attr,
+                                         const char *page, size_t len)
+{
+       struct stp_policy_node *policy_node = to_stp_policy_node(item);
+       struct stp_policy_node_attribute *pn_attr =
+               container_of(attr, struct stp_policy_node_attribute, attr);
+       ssize_t count = -EINVAL;
+
+       if (pn_attr->store)
+               count = pn_attr->store(policy_node, page, len);
+
+       return count;
+}
+
+static struct configfs_item_operations stp_policy_node_item_ops = {
+       .release                = stp_policy_node_release,
+       .show_attribute         = stp_policy_node_attr_show,
+       .store_attribute        = stp_policy_node_attr_store,
+};
+
+static struct stp_policy_node_attribute stp_policy_node_attr_range = {
+       .attr   = {
+               .ca_owner = THIS_MODULE,
+               .ca_name = "masters",
+               .ca_mode = S_IRUGO | S_IWUSR,
+       },
+       .show   = stp_policy_node_masters_show,
+       .store  = stp_policy_node_masters_store,
+};
+
+static struct stp_policy_node_attribute stp_policy_node_attr_channels = {
+       .attr   = {
+               .ca_owner = THIS_MODULE,
+               .ca_name = "channels",
+               .ca_mode = S_IRUGO | S_IWUSR,
+       },
+       .show   = stp_policy_node_channels_show,
+       .store  = stp_policy_node_channels_store,
+};
+
+static struct configfs_attribute *stp_policy_node_attrs[] = {
+       &stp_policy_node_attr_range.attr,
+       &stp_policy_node_attr_channels.attr,
+       NULL,
+};
+
+static struct config_item_type stp_policy_type;
+static struct config_item_type stp_policy_node_type;
+
+static struct config_group *
+stp_policy_node_make(struct config_group *group, const char *name)
+{
+       struct stp_policy_node *policy_node, *parent_node;
+       struct stp_policy *policy;
+
+       if (group->cg_item.ci_type == &stp_policy_type) {
+               policy = container_of(group, struct stp_policy, group);
+       } else {
+               parent_node = container_of(group, struct stp_policy_node,
+                                          group);
+               policy = parent_node->policy;
+       }
+
+       if (!policy->stm)
+               return ERR_PTR(-ENODEV);
+
+       policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
+       if (!policy_node)
+               return ERR_PTR(-ENOMEM);
+
+       config_group_init_type_name(&policy_node->group, name,
+                                   &stp_policy_node_type);
+
+       policy_node->policy = policy;
+
+       /* default values for the attributes */
+       policy_node->first_master = policy->stm->data->sw_start;
+       policy_node->last_master = policy->stm->data->sw_end;
+       policy_node->first_channel = 0;
+       policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
+
+       return &policy_node->group;
+}
+
+static void
+stp_policy_node_drop(struct config_group *group, struct config_item *item)
+{
+       config_item_put(item);
+}
+
+static struct configfs_group_operations stp_policy_node_group_ops = {
+       .make_group     = stp_policy_node_make,
+       .drop_item      = stp_policy_node_drop,
+};
+
+static struct config_item_type stp_policy_node_type = {
+       .ct_item_ops    = &stp_policy_node_item_ops,
+       .ct_group_ops   = &stp_policy_node_group_ops,
+       .ct_attrs       = stp_policy_node_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+/*
+ * Root group: policies.
+ */
+static struct configfs_attribute stp_policy_attr_device = {
+       .ca_owner = THIS_MODULE,
+       .ca_name = "device",
+       .ca_mode = S_IRUGO,
+};
+
+static struct configfs_attribute *stp_policy_attrs[] = {
+       &stp_policy_attr_device,
+       NULL,
+};
+
+static ssize_t stp_policy_attr_show(struct config_item *item,
+                                   struct configfs_attribute *attr,
+                                   char *page)
+{
+       struct stp_policy *policy = to_stp_policy(item);
+       ssize_t count;
+
+       count = sprintf(page, "%s\n",
+                       (policy && policy->stm) ?
+                       policy->stm->data->name :
+                       "<none>");
+
+       return count;
+}
+
+void stp_policy_unbind(struct stp_policy *policy)
+{
+       struct stm_device *stm = policy->stm;
+
+       if (WARN_ON_ONCE(!policy->stm))
+               return;
+
+       mutex_lock(&stm->policy_mutex);
+       stm->policy = NULL;
+       mutex_unlock(&stm->policy_mutex);
+
+       policy->stm = NULL;
+
+       stm_put_device(stm);
+}
+
+static void stp_policy_release(struct config_item *item)
+{
+       struct stp_policy *policy = to_stp_policy(item);
+
+       stp_policy_unbind(policy);
+       kfree(policy);
+}
+
+static struct configfs_item_operations stp_policy_item_ops = {
+       .release                = stp_policy_release,
+       .show_attribute         = stp_policy_attr_show,
+};
+
+static struct configfs_group_operations stp_policy_group_ops = {
+       .make_group     = stp_policy_node_make,
+};
+
+static struct config_item_type stp_policy_type = {
+       .ct_item_ops    = &stp_policy_item_ops,
+       .ct_group_ops   = &stp_policy_group_ops,
+       .ct_attrs       = stp_policy_attrs,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct config_group *
+stp_policies_make(struct config_group *group, const char *name)
+{
+       struct config_group *ret;
+       struct stm_device *stm;
+       char *devname, *p;
+
+       devname = kasprintf(GFP_KERNEL, "%s", name);
+       if (!devname)
+               return ERR_PTR(-ENOMEM);
+
+       /*
+        * node must look like <device_name>.<policy_name>, where
+        * <device_name> is the name of an existing stm device and
+        * <policy_name> is an arbitrary string
+        */
+       p = strchr(devname, '.');
+       if (!p) {
+               kfree(devname);
+               return ERR_PTR(-EINVAL);
+       }
+
+       *p++ = '\0';
+
+       stm = stm_find_device(devname);
+       kfree(devname);
+
+       if (!stm)
+               return ERR_PTR(-ENODEV);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy) {
+               ret = ERR_PTR(-EBUSY);
+               goto unlock_policy;
+       }
+
+       stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
+       if (!stm->policy) {
+               ret = ERR_PTR(-ENOMEM);
+               goto unlock_policy;
+       }
+
+       config_group_init_type_name(&stm->policy->group, name,
+                                   &stp_policy_type);
+       stm->policy->stm = stm;
+
+       ret = &stm->policy->group;
+
+unlock_policy:
+       mutex_unlock(&stm->policy_mutex);
+
+       if (IS_ERR(ret))
+               stm_put_device(stm);
+
+       return ret;
+}
+
+static struct configfs_group_operations stp_policies_group_ops = {
+       .make_group     = stp_policies_make,
+};
+
+static struct config_item_type stp_policies_type = {
+       .ct_group_ops   = &stp_policies_group_ops,
+       .ct_owner       = THIS_MODULE,
+};
+
+static struct configfs_subsystem stp_policy_subsys = {
+       .su_group = {
+               .cg_item = {
+                       .ci_namebuf     = "stp-policy",
+                       .ci_type        = &stp_policies_type,
+               },
+       },
+};
+
+/*
+ * Lock the policy mutex from the outside
+ */
+static struct stp_policy_node *
+__stp_policy_node_lookup(struct stp_policy *policy, char *s)
+{
+       struct stp_policy_node *policy_node, *ret;
+       struct list_head *head = &policy->group.cg_children;
+       struct config_item *item;
+       char *start, *end = s;
+
+       if (list_empty(head))
+               return NULL;
+
+       /* return the first entry if everything else fails */
+       item = list_entry(head->next, struct config_item, ci_entry);
+       ret = to_stp_policy_node(item);
+
+next:
+       for (;;) {
+               start = strsep(&end, "/");
+               if (!start)
+                       break;
+
+               if (!*start)
+                       continue;
+
+               list_for_each_entry(item, head, ci_entry) {
+                       policy_node = to_stp_policy_node(item);
+
+                       if (!strcmp(start,
+                                   policy_node->group.cg_item.ci_name)) {
+                               ret = policy_node;
+
+                               if (!end)
+                                       goto out;
+
+                               head = &policy_node->group.cg_children;
+                               goto next;
+                       }
+               }
+               break;
+       }
+
+out:
+       return ret;
+}
+
+
+struct stp_policy_node *
+stp_policy_node_lookup(struct stm_device *stm, char *s)
+{
+       struct stp_policy_node *policy_node = NULL;
+
+       mutex_lock(&stp_policy_subsys.su_mutex);
+
+       mutex_lock(&stm->policy_mutex);
+       if (stm->policy)
+               policy_node = __stp_policy_node_lookup(stm->policy, s);
+       mutex_unlock(&stm->policy_mutex);
+
+       if (policy_node)
+               config_item_get(&policy_node->group.cg_item);
+       mutex_unlock(&stp_policy_subsys.su_mutex);
+
+       return policy_node;
+}
+
+void stp_policy_node_put(struct stp_policy_node *policy_node)
+{
+       config_item_put(&policy_node->group.cg_item);
+}
+
+int __init stp_configfs_init(void)
+{
+       int err;
+
+       config_group_init(&stp_policy_subsys.su_group);
+       mutex_init(&stp_policy_subsys.su_mutex);
+       err = configfs_register_subsystem(&stp_policy_subsys);
+
+       return err;
+}
+
+void __exit stp_configfs_exit(void)
+{
+       configfs_unregister_subsystem(&stp_policy_subsys);
+}
drivers/hwtracing/stm/stm.h new file +87 -0
new file mode 100644
index 0000000000..cf33bf976a
@@ -0,0 +1,87 @@
+/*
+ * System Trace Module (STM) infrastructure
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#ifndef _STM_STM_H_
+#define _STM_STM_H_
+
+struct stp_policy;
+struct stp_policy_node;
+
+struct stp_policy_node *
+stp_policy_node_lookup(struct stm_device *stm, char *s);
+void stp_policy_node_put(struct stp_policy_node *policy_node);
+void stp_policy_unbind(struct stp_policy *policy);
+
+void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
+                               unsigned int *mstart, unsigned int *mend,
+                               unsigned int *cstart, unsigned int *cend);
+int stp_configfs_init(void);
+void stp_configfs_exit(void);
+
+struct stp_master {
+       unsigned int    nr_free;
+       unsigned long   chan_map[0];
+};
+
+struct stm_device {
+       struct device           dev;
+       struct module           *owner;
+       struct stp_policy       *policy;
+       struct mutex            policy_mutex;
+       int                     major;
+       unsigned int            sw_nmasters;
+       struct stm_data         *data;
+       spinlock_t              link_lock;
+       struct list_head        link_list;
+       /* master allocation */
+       spinlock_t              mc_lock;
+       struct stp_master       *masters[0];
+};
+
+#define to_stm_device(_d)                              \
+       container_of((_d), struct stm_device, dev)
+
+struct stm_output {
+       unsigned int            master;
+       unsigned int            channel;
+       unsigned int            nr_chans;
+};
+
+struct stm_file {
+       struct stm_device       *stm;
+       struct stp_policy_node  *policy_node;
+       struct stm_output       output;
+};
+
+struct stm_device *stm_find_device(const char *name);
+void stm_put_device(struct stm_device *stm);
+
+struct stm_source_device {
+       struct device           dev;
+       struct stm_source_data  *data;
+       spinlock_t              link_lock;
+       struct stm_device       *link;
+       struct list_head        link_entry;
+       /* one output per stm_source device */
+       struct stp_policy_node  *policy_node;
+       struct stm_output       output;
+};
+
+#define to_stm_source_device(_d)                               \
+       container_of((_d), struct stm_source_device, dev)
+
+#endif /* _STM_STM_H_ */
include/linux/stm.h new file +126 -0
new file mode 100644
index 0000000000..9d0083d364
@@ -0,0 +1,126 @@
+/*
+ * System Trace Module (STM) infrastructure apis
+ * Copyright (C) 2014 Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ */
+
+#ifndef _STM_H_
+#define _STM_H_
+
+#include <linux/device.h>
+
+/**
+ * enum stp_packet_type - STP packets that an STM driver sends
+ */
+enum stp_packet_type {
+       STP_PACKET_DATA = 0,
+       STP_PACKET_FLAG,
+       STP_PACKET_USER,
+       STP_PACKET_MERR,
+       STP_PACKET_GERR,
+       STP_PACKET_TRIG,
+       STP_PACKET_XSYNC,
+};
+
+/**
+ * enum stp_packet_flags - STP packet modifiers
+ */
+enum stp_packet_flags {
+       STP_PACKET_MARKED       = 0x1,
+       STP_PACKET_TIMESTAMPED  = 0x2,
+};
+
+struct stp_policy;
+
+struct stm_device;
+
+/**
+ * struct stm_data - STM device description and callbacks
+ * @name:              device name
+ * @stm:               internal structure, only used by stm class code
+ * @sw_start:          first STP master available to software
+ * @sw_end:            last STP master available to software
+ * @sw_nchannels:      number of STP channels per master
+ * @sw_mmiosz:         size of one channel's IO space, for mmap, optional
+ * @packet:            callback that sends an STP packet
+ * @mmio_addr:         mmap callback, optional
+ * @link:              called when a new stm_source gets linked to us, optional
+ * @unlink:            likewise for unlinking, again optional
+ * @set_options:       set device-specific options on a channel
+ *
+ * Fill out this structure before calling stm_register_device() to create
+ * an STM device and stm_unregister_device() to destroy it. It will also be
+ * passed back to @packet(), @mmio_addr(), @link(), @unlink() and @set_options()
+ * callbacks.
+ *
+ * Normally, an STM device will have a range of masters available to software
+ * and the rest being statically assigned to various hardware trace sources.
+ * The former is defined by the the range [@sw_start..@sw_end] of the device
+ * description. That is, the lowest master that can be allocated to software
+ * writers is @sw_start and data from this writer will appear is @sw_start
+ * master in the STP stream.
+ */
+struct stm_data {
+       const char              *name;
+       struct stm_device       *stm;
+       unsigned int            sw_start;
+       unsigned int            sw_end;
+       unsigned int            sw_nchannels;
+       unsigned int            sw_mmiosz;
+       ssize_t                 (*packet)(struct stm_data *, unsigned int,
+                                         unsigned int, unsigned int,
+                                         unsigned int, unsigned int,
+                                         const unsigned char *);
+       phys_addr_t             (*mmio_addr)(struct stm_data *, unsigned int,
+                                            unsigned int, unsigned int);
+       int                     (*link)(struct stm_data *, unsigned int,
+                                       unsigned int);
+       void                    (*unlink)(struct stm_data *, unsigned int,
+                                         unsigned int);
+       long                    (*set_options)(struct stm_data *, unsigned int,
+                                              unsigned int, unsigned int,
+                                              unsigned long);
+};
+
+int stm_register_device(struct device *parent, struct stm_data *stm_data,
+                       struct module *owner);
+void stm_unregister_device(struct stm_data *stm_data);
+
+struct stm_source_device;
+
+/**
+ * struct stm_source_data - STM source device description and callbacks
+ * @name:      device name, will be used for policy lookup
+ * @src:       internal structure, only used by stm class code
+ * @nr_chans:  number of channels to allocate
+ * @link:      called when this source gets linked to an STM device
+ * @unlink:    called when this source is about to get unlinked from its STM
+ *
+ * Fill in this structure before calling stm_source_register_device() to
+ * register a source device. Also pass it to unregister and write calls.
+ */
+struct stm_source_data {
+       const char              *name;
+       struct stm_source_device *src;
+       unsigned int            percpu;
+       unsigned int            nr_chans;
+       int                     (*link)(struct stm_source_data *data);
+       void                    (*unlink)(struct stm_source_data *data);
+};
+
+int stm_source_register_device(struct device *parent,
+                              struct stm_source_data *data);
+void stm_source_unregister_device(struct stm_source_data *data);
+
+int stm_source_write(struct stm_source_data *data, unsigned int chan,
+                    const char *buf, size_t count);
+
+#endif /* _STM_H_ */
include/uapi/linux/stm.h new file +50 -1
new file mode 100644
index 0000000000..626a8d3f63
@@ -0,0 +1,50 @@
+/*
+ * System Trace Module (STM) userspace interfaces
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#ifndef _UAPI_LINUX_STM_H
+#define _UAPI_LINUX_STM_H
+
+#include <linux/types.h>
+
+/**
+ * struct stp_policy_id - identification for the STP policy
+ * @size:      size of the structure including real id[] length
+ * @master:    assigned master
+ * @channel:   first assigned channel
+ * @width:     number of requested channels
+ * @id:                identification string
+ *
+ * User must calculate the total size of the structure and put it into
+ * @size field, fill out the @id and desired @width. In return, kernel
+ * fills out @master, @channel and @width.
+ */
+struct stp_policy_id {
+       __u32           size;
+       __u16           master;
+       __u16           channel;
+       __u16           width;
+       /* padding */
+       __u16           __reserved_0;
+       __u32           __reserved_1;
+       char            id[0];
+};
+
+#define STP_POLICY_ID_SET      _IOWR('%', 0, struct stp_policy_id)
+#define STP_POLICY_ID_GET      _IOR('%', 1, struct stp_policy_id)
+#define STP_SET_OPTIONS                _IOW('%', 2, __u64)
+
+#endif /* _UAPI_LINUX_STM_H */
--
Quoted 2 lines

2.1.4

Reviewed-by on v3 3/11
#19 lore raw

Re: [PATCH v3 03/11] stm class: dummy_stm: Add dummy driver for testing stm class

In reply to Alexander Shishkin
Quoted 111 lines · Alexander Shishkin

This is a simple module that pretends to be an stm device and discards all the data that comes in. Useful for testing stm class and its users.

---
 drivers/hwtracing/stm/Kconfig     |  8 +++++
 drivers/hwtracing/stm/Makefile    |  2 ++
 drivers/hwtracing/stm/dummy_stm.c | 66 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 drivers/hwtracing/stm/dummy_stm.c
drivers/hwtracing/stm/Kconfig +8 -0
index 90ed327461..4fdb8f46d2 100644
@@ -6,3 +6,11 @@ config STM
          Examples of such devices are Intel Trace Hub and Coresight STM.
 
          Say Y here to enable System Trace Module device support.
+
+config STM_DUMMY
+       tristate "Dummy STM driver"
+       help
+         This is a simple dummy device that pretends to be an stm device
+         and discards your data. Use for stm class testing.
+
+         If you don't know what this is, say N.
drivers/hwtracing/stm/Makefile +2 -0
index adec701649..1b2725fd04 100644
@@ -1,3 +1,5 @@
 obj-$(CONFIG_STM)      += stm_core.o
 
 stm_core-y             := core.o policy.o
+
+obj-$(CONFIG_STM_DUMMY)        += dummy_stm.o
drivers/hwtracing/stm/dummy_stm.c new file +66 -1
new file mode 100644
index 0000000000..3709bef0b2
@@ -0,0 +1,66 @@
+/*
+ * A dummy STM device for stm/stm_source class testing.
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM class implements generic infrastructure for  System Trace Module devices
+ * as defined in MIPI STPv2 specification.
+ */
+
+#undef DEBUG
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+
+static ssize_t
+dummy_stm_packet(struct stm_data *stm_data, unsigned int master,
+                unsigned int channel, unsigned int packet, unsigned int flags,
+                unsigned int size, const unsigned char *payload)
+{
+#ifdef DEBUG
+       u64 pl = 0;
+
+       if (payload)
+               pl = *(u64 *)payload;
+
+       if (size < 8)
+               pl &= (1ull << (size * 8)) - 1;
+       trace_printk("[%u:%u] [pkt: %x/%x] (%llx)\n", master, channel,
+                    packet, size, pl);
+#endif
+       return size;
+}
+
+static struct stm_data dummy_stm = {
+       .name           = "dummy_stm",
+       .sw_start       = 0x0000,
+       .sw_end         = 0xffff,
+       .sw_nchannels   = 0xffff,
+       .packet         = dummy_stm_packet,
+};
+
+static int dummy_stm_init(void)
+{
+       return stm_register_device(NULL, &dummy_stm, THIS_MODULE);
+}
+
+static void dummy_stm_exit(void)
+{
+       stm_unregister_device(&dummy_stm);
+}
+
+module_init(dummy_stm_init);
+module_exit(dummy_stm_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("dummy_stm device");
+MODULE_AUTHOR("Alexander Shishkin <[email protected]>");
--
Quoted 2 lines

2.1.4

Reviewed-by on v3 4/11
#21 lore raw

Re: [PATCH v3 04/11] stm class: stm_console: Add kernel-console-over-stm driver

In reply to Alexander Shishkin
Quoted 129 lines · Alexander Shishkin

This is a simple stm_source class device driver (kernelspace stm trace source) that registers a console and sends kernel messages over STM devices.

---
 drivers/hwtracing/stm/Kconfig   |  9 +++++
 drivers/hwtracing/stm/Makefile  |  4 +++
 drivers/hwtracing/stm/console.c | 80 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 93 insertions(+)
 create mode 100644 drivers/hwtracing/stm/console.c
drivers/hwtracing/stm/Kconfig +9 -0
index 4fdb8f46d2..583c222218 100644
@@ -14,3 +14,12 @@ config STM_DUMMY
          and discards your data. Use for stm class testing.
 
          If you don't know what this is, say N.
+
+config STM_SOURCE_CONSOLE
+       tristate "Kernel console over STM devices"
+       help
+         This is a kernel space trace source that sends kernel log
+         messages to trace hosts over STM devices.
+
+         If you want to send kernel console messages over STM devices,
+         say Y.
drivers/hwtracing/stm/Makefile +4 -0
index 1b2725fd04..f9312c38dd 100644
@@ -3,3 +3,7 @@ obj-$(CONFIG_STM)       += stm_core.o
 stm_core-y             := core.o policy.o
 
 obj-$(CONFIG_STM_DUMMY)        += dummy_stm.o
+
+obj-$(CONFIG_STM_SOURCE_CONSOLE)       += stm_console.o
+
+stm_console-y          := console.o
drivers/hwtracing/stm/console.c new file +80 -1
new file mode 100644
index 0000000000..c9d9a8d2ff
@@ -0,0 +1,80 @@
+/*
+ * Simple kernel console driver for STM devices
+ * Copyright (c) 2014, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * STM console will send kernel messages over STM devices to a trace host.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/console.h>
+#include <linux/slab.h>
+#include <linux/stm.h>
+
+static int stm_console_link(struct stm_source_data *data);
+static void stm_console_unlink(struct stm_source_data *data);
+
+static struct stm_console {
+       struct stm_source_data  data;
+       struct console          console;
+} stm_console = {
+       .data   = {
+               .name           = "console",
+               .nr_chans       = 1,
+               .link           = stm_console_link,
+               .unlink         = stm_console_unlink,
+       },
+};
+
+static void
+stm_console_write(struct console *con, const char *buf, unsigned len)
+{
+       struct stm_console *sc = container_of(con, struct stm_console, console);
+
+       stm_source_write(&sc->data, 0, buf, len);
+}
+
+static int stm_console_link(struct stm_source_data *data)
+{
+       struct stm_console *sc = container_of(data, struct stm_console, data);
+
+       strcpy(sc->console.name, "stm_console");
+       sc->console.write = stm_console_write;
+       sc->console.flags = CON_ENABLED | CON_PRINTBUFFER;
+       register_console(&sc->console);
+
+       return 0;
+}
+
+static void stm_console_unlink(struct stm_source_data *data)
+{
+       struct stm_console *sc = container_of(data, struct stm_console, data);
+
+       unregister_console(&sc->console);
+}
+
+static int stm_console_init(void)
+{
+       return stm_source_register_device(NULL, &stm_console.data);
+}
+
+static void stm_console_exit(void)
+{
+       stm_source_unregister_device(&stm_console.data);
+}
+
+module_init(stm_console_init);
+module_exit(stm_console_exit);
+
+MODULE_LICENSE("GPL v2");
+MODULE_DESCRIPTION("stm_console driver");
+MODULE_AUTHOR("Alexander Shishkin <[email protected]>");
--
Quoted 2 lines

2.1.4