linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v14 0/2] Add support for UltraSoc System Memory Buffer
@ 2022-11-23 12:38 Junhao He
  2022-11-23 12:38 ` [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver Junhao He
  2022-11-23 12:38 ` [PATCH v14 2/2] Documentation: Add document for UltraSoc SMB driver Junhao He
  0 siblings, 2 replies; 9+ messages in thread
From: Junhao He @ 2022-11-23 12:38 UTC (permalink / raw)
  To: mathieu.poirier, suzuki.poulose, mike.leach, leo.yan,
	jonathan.cameron, john.garry
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng,
	hejunhao3

Add support for UltraSoc System Memory Buffer.

Change since v13:
- Modify document and the patches description according to Bagas's comment.
- Add dependency on config ACPI, drop redundant enable hw in smb_update_buffer(),
  Modify smb_purge_data() description according to Suzuki's comment.
- Link: https://lore.kernel.org/lkml/20221114090316.63157-1-hejunhao3@huawei.com/

Change since v12:
- Modify the code style and add "#ifdef CONFIG_ACPI" according to Jonathan's comment.
- Address the comments from Yicong, included drop "buf_base"__iomem attribute,
  modify the "reading" type to bool and fix FIELD_PREP.
- Link: https://lore.kernel.org/lkml/20221109135008.9485-1-hejunhao3@huawei.com/

Change since v11:
- Modify the code style and rename the macro according to Jonathan's comment.
- Link: https://lore.kernel.org/lkml/20221107130624.59886-1-hejunhao3@huawei.com/

Change since v10:
- Rebase onto v6.1-rc4, included similar sysfs register accessors (as same as James's patch)
- Link: https://lore.kernel.org/lkml/20221022115929.7503-1-hejunhao3@huawei.com/

Change since v9:
- Update the Contact tag in SMB document. 
- Replace the spinlock with mutex.
- Do some clean-ups in "smb_enable()" and "smb_release()".
- Use classic memory mapped interface.
- Link: https://lore.kernel.org/linux-arm-kernel/20220818132231.28240-1-hejunhao3@huawei.com/

Change since v8:
- Insert a blank line at the end of the config tag in Kconfig according to Randy's comment.
- Link: https://lore.kernel.org/linux-arm-kernel/20220816131634.38195-1-hejunhao3@huawei.com/

Change since v7:
- Use the macros for register bit flags and numbers of resource.
- Cleanup punctuation.
- Update the Date tag and the KernelVersion tag in the document.
- Link: https://lore.kernel.org/lkml/20220712091353.34540-1-hejunhao3@huawei.com/

Change since v6:
- Modify the code style and driver description according to Suzuki's comment.
- Modify configuration of "drvdata->reading", to void problems in open/read
  concurrency scenario.
- Rename the macro of "SMB_FLOW_MASK".
- Use the "handle->head" to determine the page number and offset.
- Link: https://lore.kernel.org/linux-arm-kernel/20220606130223.57354-1-liuqi115@huawei.com/

  Change since v5:
- Address the comments from Suzuki, add some comments in SMB document, and modify
  configuration of "drvdata->reading", to void problems in multi-core concurrency scenario
- Link: https://lore.kernel.org/linux-arm-kernel/20220416083953.52610-1-liuqi115@huawei.com/

Change since v4:
- Add a simple document of SMB driver according to Suzuki's comment.
- Address the comments from Suzuki.
- Link: https://lore.kernel.org/linux-arm-kernel/20220128061755.31909-1-liuqi115@huawei.com/

Change since v3:
- Modify the file header according to community specifications.
- Address the comments from Mathieu.
- Link: https://lore.kernel.org/linux-arm-kernel/20211118110016.40398-1-liuqi115@huawei.com/

Change since v2:
- Move ultrasoc driver to drivers/hwtracing/coresight by Mathieu's comment.
- Link: https://lists.linaro.org/pipermail/coresight/2021-November/007310.html

Change since v1:
- Drop the document of UltraSoc according to Mathieu's comment.
- Add comments to explain some private hardware settings.
- Address the comments from Mathieu.
- Link: https://lists.linaro.org/pipermail/coresight/2021-August/006842.html

Change since RFC:
- Move driver to drivers/hwtracing/coresight/ultrasoc.
- Remove ultrasoc-axi-com.c, as AXI-COM doesn't need to be configured in
  basic tracing function.
- Remove ultrasoc.c as SMB does not need to register with the ultrasoc core.
- Address the comments from Mathieu and Suzuki.
- Link: https://lists.linaro.org/pipermail/coresight/2021-June/006535.html

Qi Liu (2):
  drivers/coresight: Add UltraSoc System Memory Buffer driver
  Documentation: Add document for UltraSoc SMB driver

 .../sysfs-bus-coresight-devices-ultra_smb     |  31 +
 .../trace/coresight/ultrasoc-smb.rst          |  83 +++
 drivers/hwtracing/coresight/Kconfig           |  12 +
 drivers/hwtracing/coresight/Makefile          |   1 +
 drivers/hwtracing/coresight/ultrasoc-smb.c    | 658 ++++++++++++++++++
 drivers/hwtracing/coresight/ultrasoc-smb.h    | 129 ++++
 6 files changed, 914 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-coresight-devices-ultra_smb
 create mode 100644 Documentation/trace/coresight/ultrasoc-smb.rst
 create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
 create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h

-- 
2.33.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver
  2022-11-23 12:38 [PATCH v14 0/2] Add support for UltraSoc System Memory Buffer Junhao He
@ 2022-11-23 12:38 ` Junhao He
  2022-11-23 14:03   ` Suzuki K Poulose
  2022-11-23 12:38 ` [PATCH v14 2/2] Documentation: Add document for UltraSoc SMB driver Junhao He
  1 sibling, 1 reply; 9+ messages in thread
From: Junhao He @ 2022-11-23 12:38 UTC (permalink / raw)
  To: mathieu.poirier, suzuki.poulose, mike.leach, leo.yan,
	jonathan.cameron, john.garry
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng,
	hejunhao3

From: Qi Liu <liuqi115@huawei.com>

Add driver for UltraSoc SMB(System Memory Buffer) device.
SMB provides a way to buffer messages from ETM, and store
these "CPU instructions trace" in system memory.
The SMB device is identifier as ACPI HID "HISI03A1". Device
system memory address resources are allocated using the _CRS
method and buffer modes is the circular buffer mode.

SMB is developed by UltraSoc technology, which is acquired by
Siemens, and we still use "UltraSoc" to name driver.

Signed-off-by: Qi Liu <liuqi115@huawei.com>
Signed-off-by: Junhao He <hejunhao3@huawei.com>
Tested-by: JunHao He <hejunhao3@huawei.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 drivers/hwtracing/coresight/Kconfig        |  12 +
 drivers/hwtracing/coresight/Makefile       |   1 +
 drivers/hwtracing/coresight/ultrasoc-smb.c | 658 +++++++++++++++++++++
 drivers/hwtracing/coresight/ultrasoc-smb.h | 129 ++++
 4 files changed, 800 insertions(+)
 create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
 create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h

diff --git a/drivers/hwtracing/coresight/Kconfig b/drivers/hwtracing/coresight/Kconfig
index 45c1eb5dfcb7..ba035d7894e0 100644
--- a/drivers/hwtracing/coresight/Kconfig
+++ b/drivers/hwtracing/coresight/Kconfig
@@ -201,4 +201,16 @@ config CORESIGHT_TRBE
 
 	  To compile this driver as a module, choose M here: the module will be
 	  called coresight-trbe.
+
+config ULTRASOC_SMB
+	tristate "Ultrasoc system memory buffer drivers"
+	depends on ACPI || COMPILE_TEST
+	depends on ARM64 && CORESIGHT_LINKS_AND_SINKS
+	help
+	  This driver provides support for the Ultrasoc system memory buffer (SMB).
+	  SMB is responsible for receiving the trace data from Coresight ETM devices
+	  and storing them to a system buffer.
+
+	  To compile this driver as a module, choose M here: the module will be
+	  called ultrasoc-smb.
 endif
diff --git a/drivers/hwtracing/coresight/Makefile b/drivers/hwtracing/coresight/Makefile
index b6c4a48140ec..344dba8d6ff8 100644
--- a/drivers/hwtracing/coresight/Makefile
+++ b/drivers/hwtracing/coresight/Makefile
@@ -27,3 +27,4 @@ obj-$(CONFIG_CORESIGHT_CTI) += coresight-cti.o
 obj-$(CONFIG_CORESIGHT_TRBE) += coresight-trbe.o
 coresight-cti-y := coresight-cti-core.o	coresight-cti-platform.o \
 		   coresight-cti-sysfs.o
+obj-$(CONFIG_ULTRASOC_SMB) += ultrasoc-smb.o
diff --git a/drivers/hwtracing/coresight/ultrasoc-smb.c b/drivers/hwtracing/coresight/ultrasoc-smb.c
new file mode 100644
index 000000000000..449293e34844
--- /dev/null
+++ b/drivers/hwtracing/coresight/ultrasoc-smb.c
@@ -0,0 +1,658 @@
+// SPDX-License-Identifier: (GPL-2.0 OR MIT)
+/*
+ * Siemens System Memory Buffer driver.
+ * Copyright(c) 2022, HiSilicon Limited.
+ */
+
+#include <linux/atomic.h>
+#include <linux/acpi.h>
+#include <linux/circ_buf.h>
+#include <linux/err.h>
+#include <linux/fs.h>
+#include <linux/module.h>
+#include <linux/mod_devicetable.h>
+#include <linux/platform_device.h>
+
+#include "coresight-etm-perf.h"
+#include "coresight-priv.h"
+#include "ultrasoc-smb.h"
+
+DEFINE_CORESIGHT_DEVLIST(sink_devs, "ultra_smb");
+
+#define ULTRASOC_SMB_DSM_UUID	"82ae1283-7f6a-4cbe-aa06-53e8fb24db18"
+
+static bool smb_buffer_is_empty(struct smb_drv_data *drvdata)
+{
+	u32 buf_status = readl(drvdata->base + SMB_LB_INT_STS_REG);
+
+	return !FIELD_GET(SMB_LB_INT_STS_NOT_EMPTY_MSK, buf_status);
+}
+
+static void smb_buffer_sync_status(struct smb_drv_data *drvdata)
+{
+	struct smb_data_buffer *sdb = &drvdata->sdb;
+
+	sdb->wr_offset = readl(drvdata->base + SMB_LB_WR_ADDR_REG) -
+			 sdb->start_addr;
+	sdb->rd_offset = readl(drvdata->base + SMB_LB_RD_ADDR_REG) -
+			 sdb->start_addr;
+	sdb->full = sdb->wr_offset == sdb->rd_offset &&
+		    !smb_buffer_is_empty(drvdata);
+}
+
+static void smb_reset_buffer_status(struct smb_drv_data *drvdata)
+{
+	/* All other bits are reserved and shall be 0 */
+	writel(SMB_LB_INT_STS_RESET, drvdata->base + SMB_LB_INT_STS_REG);
+}
+
+/*
+ * Purge data remaining in hardware path to avoid corrupting the next
+ * session.
+ */
+static void smb_purge_data(struct smb_drv_data *drvdata)
+{
+	/* All other bits are reserved and shall be 0 */
+	writel(SMB_LB_PURGE_PURGED, drvdata->base + SMB_LB_PURGE_REG);
+}
+
+static void smb_update_data_size(struct smb_drv_data *drvdata)
+{
+	struct smb_data_buffer *sdb = &drvdata->sdb;
+
+	smb_purge_data(drvdata);
+	smb_buffer_sync_status(drvdata);
+	if (sdb->full) {
+		sdb->data_size = sdb->buf_size;
+		return;
+	}
+
+	/* The buffer mode is circular buffer mode */
+	sdb->data_size = CIRC_CNT(sdb->wr_offset, sdb->rd_offset,
+				  sdb->buf_size);
+}
+
+static int smb_open(struct inode *inode, struct file *file)
+{
+	struct smb_drv_data *drvdata = container_of(file->private_data,
+					struct smb_drv_data, miscdev);
+	int ret = 0;
+
+	mutex_lock(&drvdata->mutex);
+
+	if (drvdata->reading) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	if (atomic_read(drvdata->csdev->refcnt)) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	drvdata->reading = true;
+out:
+	mutex_unlock(&drvdata->mutex);
+
+	return ret;
+}
+
+static ssize_t smb_read(struct file *file, char __user *data, size_t len,
+			loff_t *ppos)
+{
+	struct smb_drv_data *drvdata = container_of(file->private_data,
+					struct smb_drv_data, miscdev);
+	struct smb_data_buffer *sdb = &drvdata->sdb;
+	struct device *dev = &drvdata->csdev->dev;
+	ssize_t to_copy = 0;
+
+	mutex_lock(&drvdata->mutex);
+
+	if (!len)
+		goto out;
+
+	/*
+	 * In sysfs mode, size need to be update in the following two cases:
+	 * 1) Start dumping data.
+	 * 2) End dump data, make sure there is no remaining data in
+	 *    the hardware path. Because the remaining data cannot be purged
+	 *    when the buffer is full.
+	 */
+	if (!sdb->data_size) {
+		smb_update_data_size(drvdata);
+		if (!sdb->data_size)
+			goto out;
+	}
+
+	to_copy = min(sdb->data_size, len);
+
+	/* Copy parts of trace data when read pointer wrap around SMB buffer */
+	if (sdb->rd_offset + to_copy > sdb->buf_size)
+		to_copy = sdb->buf_size - sdb->rd_offset;
+
+	if (copy_to_user(data, (void *)sdb->buf_base + sdb->rd_offset,
+			 to_copy)) {
+		dev_dbg(dev, "Failed to copy data to user\n");
+		to_copy = -EFAULT;
+		goto out;
+	}
+
+	*ppos += to_copy;
+	sdb->data_size -= to_copy;
+	sdb->rd_offset += to_copy;
+	sdb->rd_offset %= sdb->buf_size;
+	writel(sdb->start_addr + sdb->rd_offset,
+	       drvdata->base + SMB_LB_RD_ADDR_REG);
+	dev_dbg(dev, "%zu bytes copied\n", to_copy);
+out:
+	if (!sdb->data_size)
+		smb_reset_buffer_status(drvdata);
+	mutex_unlock(&drvdata->mutex);
+
+	return to_copy;
+}
+
+static int smb_release(struct inode *inode, struct file *file)
+{
+	struct smb_drv_data *drvdata = container_of(file->private_data,
+					struct smb_drv_data, miscdev);
+
+	mutex_lock(&drvdata->mutex);
+	drvdata->reading = false;
+	mutex_unlock(&drvdata->mutex);
+
+	return 0;
+}
+
+static const struct file_operations smb_fops = {
+	.owner		= THIS_MODULE,
+	.open		= smb_open,
+	.read		= smb_read,
+	.release	= smb_release,
+	.llseek		= no_llseek,
+};
+
+static ssize_t buf_size_show(struct device *dev, struct device_attribute *attr,
+			     char *buf)
+{
+	struct smb_drv_data *drvdata = dev_get_drvdata(dev->parent);
+
+	return sysfs_emit(buf, "0x%lx\n", drvdata->sdb.buf_size);
+}
+static DEVICE_ATTR_RO(buf_size);
+
+static struct attribute *smb_sink_attrs[] = {
+	coresight_simple_reg32(read_pos, SMB_LB_RD_ADDR_REG),
+	coresight_simple_reg32(write_pos, SMB_LB_WR_ADDR_REG),
+	coresight_simple_reg32(buf_status, SMB_LB_INT_STS_REG),
+	&dev_attr_buf_size.attr,
+	NULL
+};
+
+static const struct attribute_group smb_sink_group = {
+	.attrs = smb_sink_attrs,
+	.name = "mgmt",
+};
+
+static const struct attribute_group *smb_sink_groups[] = {
+	&smb_sink_group,
+	NULL
+};
+
+static void smb_enable_hw(struct smb_drv_data *drvdata)
+{
+	writel(SMB_GLB_EN_HW_ENABLE, drvdata->base + SMB_GLB_EN_REG);
+}
+
+static void smb_disable_hw(struct smb_drv_data *drvdata)
+{
+	writel(0x0, drvdata->base + SMB_GLB_EN_REG);
+}
+
+static void smb_enable_sysfs(struct coresight_device *csdev)
+{
+	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
+
+	if (drvdata->mode != CS_MODE_DISABLED)
+		return;
+
+	smb_enable_hw(drvdata);
+	drvdata->mode = CS_MODE_SYSFS;
+}
+
+static int smb_enable_perf(struct coresight_device *csdev, void *data)
+{
+	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
+	struct perf_output_handle *handle = data;
+	struct cs_buffers *buf = etm_perf_sink_config(handle);
+	pid_t pid;
+
+	if (!buf)
+		return -EINVAL;
+
+	/* Get a handle on the pid of the target process */
+	pid = buf->pid;
+
+	/* Device is already in used by other session */
+	if (drvdata->pid != -1 && drvdata->pid != pid)
+		return -EBUSY;
+
+	if (drvdata->pid == -1) {
+		smb_enable_hw(drvdata);
+		drvdata->pid = pid;
+		drvdata->mode = CS_MODE_PERF;
+	}
+
+	return 0;
+}
+
+static int smb_enable(struct coresight_device *csdev, u32 mode, void *data)
+{
+	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
+	int ret = 0;
+
+	mutex_lock(&drvdata->mutex);
+
+	/* Do nothing, the trace data is reading by other interface now */
+	if (drvdata->reading) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	/* Do nothing, the SMB is already enabled as other mode */
+	if (drvdata->mode != CS_MODE_DISABLED && drvdata->mode != mode) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	switch (mode) {
+	case CS_MODE_SYSFS:
+		smb_enable_sysfs(csdev);
+		break;
+	case CS_MODE_PERF:
+		ret = smb_enable_perf(csdev, data);
+		break;
+	default:
+		ret = -EINVAL;
+	}
+
+	if (ret)
+		goto out;
+
+	atomic_inc(csdev->refcnt);
+
+	dev_dbg(&csdev->dev, "Ultrasoc SMB enabled\n");
+out:
+	mutex_unlock(&drvdata->mutex);
+
+	return ret;
+}
+
+static int smb_disable(struct coresight_device *csdev)
+{
+	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
+	int ret = 0;
+
+	mutex_lock(&drvdata->mutex);
+
+	if (drvdata->reading) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	if (atomic_dec_return(csdev->refcnt)) {
+		ret = -EBUSY;
+		goto out;
+	}
+
+	/* Complain if we (somehow) got out of sync */
+	WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
+
+	smb_disable_hw(drvdata);
+	smb_purge_data(drvdata);
+
+	/* Dissociate from the target process. */
+	drvdata->pid = -1;
+	drvdata->mode = CS_MODE_DISABLED;
+
+	dev_dbg(&csdev->dev, "Ultrasoc SMB disabled\n");
+out:
+	mutex_unlock(&drvdata->mutex);
+
+	return ret;
+}
+
+static void *smb_alloc_buffer(struct coresight_device *csdev,
+			      struct perf_event *event, void **pages,
+			      int nr_pages, bool overwrite)
+{
+	struct cs_buffers *buf;
+	int node;
+
+	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
+	buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
+	if (!buf)
+		return NULL;
+
+	buf->snapshot = overwrite;
+	buf->nr_pages = nr_pages;
+	buf->data_pages = pages;
+	buf->pid = task_pid_nr(event->owner);
+
+	return buf;
+}
+
+static void smb_free_buffer(void *config)
+{
+	struct cs_buffers *buf = config;
+
+	kfree(buf);
+}
+
+static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
+				 struct cs_buffers *buf,
+				 unsigned long head,
+				 unsigned long data_size)
+{
+	struct smb_data_buffer *sdb = &drvdata->sdb;
+	char **dst_pages = (char **)buf->data_pages;
+	unsigned long to_copy;
+	long pg_idx, pg_offset;
+
+	pg_idx = head >> PAGE_SHIFT;
+	pg_offset = head & (PAGE_SIZE - 1);
+
+	while (data_size) {
+		unsigned long pg_space = PAGE_SIZE - pg_offset;
+
+		/* Copy parts of trace data when read pointer wrap around */
+		if (sdb->rd_offset + pg_space > sdb->buf_size)
+			to_copy = sdb->buf_size - sdb->rd_offset;
+		else
+			to_copy = min(data_size, pg_space);
+
+		memcpy(dst_pages[pg_idx] + pg_offset,
+			      sdb->buf_base + sdb->rd_offset, to_copy);
+
+		pg_offset += to_copy;
+		if (pg_offset >= PAGE_SIZE) {
+			pg_offset = 0;
+			pg_idx++;
+			pg_idx %= buf->nr_pages;
+		}
+		data_size -= to_copy;
+		sdb->rd_offset += to_copy;
+		sdb->rd_offset %= sdb->buf_size;
+	}
+
+	sdb->data_size = 0;
+	writel(sdb->start_addr + sdb->rd_offset,
+		drvdata->base + SMB_LB_RD_ADDR_REG);
+
+	/*
+	 * Data remained in link cannot be purged when SMB is full, so
+	 * synchronize the read pointer to write pointer, to make sure
+	 * these remained data won't influence next trace.
+	 */
+	if (sdb->full) {
+		smb_purge_data(drvdata);
+		writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
+		       drvdata->base + SMB_LB_RD_ADDR_REG);
+	}
+	smb_reset_buffer_status(drvdata);
+}
+
+static unsigned long smb_update_buffer(struct coresight_device *csdev,
+				       struct perf_output_handle *handle,
+				       void *sink_config)
+{
+	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
+	struct smb_data_buffer *sdb = &drvdata->sdb;
+	struct cs_buffers *buf = sink_config;
+	unsigned long data_size = 0;
+	bool lost = false;
+
+	if (!buf)
+		return 0;
+
+	mutex_lock(&drvdata->mutex);
+
+	/* Don't do anything if another tracer is using this sink. */
+	if (atomic_read(csdev->refcnt) != 1)
+		goto out;
+
+	smb_disable_hw(drvdata);
+	smb_update_data_size(drvdata);
+	data_size = sdb->data_size;
+
+	/*
+	 * The SMB buffer may be bigger than the space available in the
+	 * perf ring buffer (handle->size). If so advance the offset so
+	 * that we get the latest trace data.
+	 */
+	if (data_size > handle->size) {
+		sdb->rd_offset += data_size - handle->size;
+		sdb->rd_offset %= sdb->buf_size;
+		data_size = handle->size;
+		lost = true;
+	}
+
+	smb_sync_perf_buffer(drvdata, buf, handle->head, data_size);
+	if (!buf->snapshot && lost)
+		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
+out:
+	mutex_unlock(&drvdata->mutex);
+
+	return data_size;
+}
+
+static const struct coresight_ops_sink smb_cs_ops = {
+	.enable		= smb_enable,
+	.disable	= smb_disable,
+	.alloc_buffer	= smb_alloc_buffer,
+	.free_buffer	= smb_free_buffer,
+	.update_buffer	= smb_update_buffer,
+};
+
+static const struct coresight_ops cs_ops = {
+	.sink_ops	= &smb_cs_ops,
+};
+
+static int smb_init_data_buffer(struct platform_device *pdev,
+				struct smb_data_buffer *sdb)
+{
+	struct resource *res;
+	void *base;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, SMB_BUF_ADDR_RES);
+	if (IS_ERR(res)) {
+		dev_err(&pdev->dev, "SMB device failed to get resource\n");
+		return -EINVAL;
+	}
+
+	sdb->start_addr = FIELD_GET(SMB_BUF_ADDR_LO_MSK, res->start);
+	sdb->buf_size = resource_size(res);
+	if (sdb->buf_size == 0)
+		return -EINVAL;
+
+	/*
+	 * This is a chunk of memory, use classic mapping with better
+	 * performance.
+	 */
+	base = devm_memremap(&pdev->dev, sdb->start_addr, sdb->buf_size,
+				MEMREMAP_WB);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	sdb->buf_base = base;
+
+	return 0;
+}
+
+static void smb_init_hw(struct smb_drv_data *drvdata)
+{
+	/* First disable SMB and clear the status of SMB buffer */
+	smb_reset_buffer_status(drvdata);
+	smb_disable_hw(drvdata);
+	smb_purge_data(drvdata);
+
+	writel(SMB_LB_CFG_LO_DEFAULT, drvdata->base + SMB_LB_CFG_LO_REG);
+	writel(SMB_LB_CFG_HI_DEFAULT, drvdata->base + SMB_LB_CFG_HI_REG);
+	writel(SMB_GLB_CFG_DEFAULT, drvdata->base + SMB_GLB_CFG_REG);
+	writel(SMB_GLB_INT_CFG, drvdata->base + SMB_GLB_INT_REG);
+	writel(SMB_LB_INT_CTRL_CFG, drvdata->base + SMB_LB_INT_CTRL_REG);
+}
+
+static int smb_register_sink(struct platform_device *pdev,
+			     struct smb_drv_data *drvdata)
+{
+	struct coresight_platform_data *pdata = NULL;
+	struct coresight_desc desc = { 0 };
+	int ret;
+
+	pdata = coresight_get_platform_data(&pdev->dev);
+	if (IS_ERR(pdata))
+		return PTR_ERR(pdata);
+
+	desc.type = CORESIGHT_DEV_TYPE_SINK;
+	desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
+	desc.ops = &cs_ops;
+	desc.pdata = pdata;
+	desc.dev = &pdev->dev;
+	desc.groups = smb_sink_groups;
+	desc.name = coresight_alloc_device_name(&sink_devs, &pdev->dev);
+	if (!desc.name) {
+		dev_err(&pdev->dev, "Failed to alloc coresight device name");
+		return -ENOMEM;
+	}
+	desc.access = CSDEV_ACCESS_IOMEM(drvdata->base);
+
+	drvdata->csdev = coresight_register(&desc);
+	if (IS_ERR(drvdata->csdev))
+		return PTR_ERR(drvdata->csdev);
+
+	drvdata->miscdev.name = desc.name;
+	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
+	drvdata->miscdev.fops = &smb_fops;
+	ret = misc_register(&drvdata->miscdev);
+	if (ret) {
+		coresight_unregister(drvdata->csdev);
+		dev_err(&pdev->dev, "Failed to register misc, ret=%d\n", ret);
+	}
+
+	return ret;
+}
+
+static void smb_unregister_sink(struct smb_drv_data *drvdata)
+{
+	misc_deregister(&drvdata->miscdev);
+	coresight_unregister(drvdata->csdev);
+}
+
+static int smb_config_inport(struct device *dev, bool enable)
+{
+	u64 func = enable ? 1 : 0;
+	union acpi_object *obj;
+	guid_t guid;
+	u64 rev = 0;
+
+	/*
+	 * Using DSM calls to enable/disable ultrasoc hardwares on
+	 * tracing path, to prevent ultrasoc packet format being exposed.
+	 */
+	if (guid_parse(ULTRASOC_SMB_DSM_UUID, &guid)) {
+		dev_err(dev, "Get GUID failed\n");
+		return -EINVAL;
+	}
+
+	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid, rev, func, NULL);
+	if (!obj) {
+		dev_err(dev, "ACPI handle failed\n");
+		return -ENODEV;
+	}
+
+	ACPI_FREE(obj);
+
+	return 0;
+}
+
+static int smb_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct smb_drv_data *drvdata;
+	int ret;
+
+	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
+	if (!drvdata)
+		return -ENOMEM;
+
+	drvdata->base = devm_platform_ioremap_resource(pdev, SMB_REG_ADDR_RES);
+	if (IS_ERR(drvdata->base)) {
+		dev_err(dev, "Failed to ioremap resource\n");
+		return PTR_ERR(drvdata->base);
+	}
+
+	ret = smb_init_data_buffer(pdev, &drvdata->sdb);
+	if (ret) {
+		dev_err(dev, "Failed to init buffer, ret = %d\n", ret);
+		return ret;
+	}
+
+	smb_init_hw(drvdata);
+	mutex_init(&drvdata->mutex);
+	drvdata->pid = -1;
+
+	ret = smb_register_sink(pdev, drvdata);
+	if (ret) {
+		dev_err(dev, "Failed to register SMB sink\n");
+		return ret;
+	}
+
+	ret = smb_config_inport(dev, true);
+	if (ret) {
+		smb_unregister_sink(drvdata);
+		return ret;
+	}
+
+	platform_set_drvdata(pdev, drvdata);
+
+	return 0;
+}
+
+static int smb_remove(struct platform_device *pdev)
+{
+	struct smb_drv_data *drvdata = platform_get_drvdata(pdev);
+	int ret;
+
+	ret = smb_config_inport(&pdev->dev, false);
+	if (ret)
+		return ret;
+
+	smb_unregister_sink(drvdata);
+
+	return 0;
+}
+
+#ifdef CONFIG_ACPI
+static const struct acpi_device_id ultrasoc_smb_acpi_match[] = {
+	{"HISI03A1", 0},
+	{}
+};
+MODULE_DEVICE_TABLE(acpi, ultrasoc_smb_acpi_match);
+#endif
+
+static struct platform_driver smb_driver = {
+	.driver = {
+		.name = "ultrasoc-smb",
+		.acpi_match_table = ACPI_PTR(ultrasoc_smb_acpi_match),
+		.suppress_bind_attrs = true,
+	},
+	.probe = smb_probe,
+	.remove = smb_remove,
+};
+module_platform_driver(smb_driver);
+
+MODULE_DESCRIPTION("UltraSoc SMB CoreSight driver");
+MODULE_LICENSE("Dual MIT/GPL");
+MODULE_AUTHOR("Jonathan Zhou <jonathan.zhouwen@huawei.com>");
+MODULE_AUTHOR("Qi Liu <liuqi115@huawei.com>");
diff --git a/drivers/hwtracing/coresight/ultrasoc-smb.h b/drivers/hwtracing/coresight/ultrasoc-smb.h
new file mode 100644
index 000000000000..980139cd0f91
--- /dev/null
+++ b/drivers/hwtracing/coresight/ultrasoc-smb.h
@@ -0,0 +1,129 @@
+/* SPDX-License-Identifier: (GPL-2.0 OR MIT) */
+/*
+ * Siemens System Memory Buffer driver.
+ * Copyright(c) 2022, HiSilicon Limited.
+ */
+
+#ifndef _ULTRASOC_SMB_H
+#define _ULTRASOC_SMB_H
+
+#include <linux/miscdevice.h>
+#include <linux/mutex.h>
+
+/* Offset of SMB global registers */
+#define SMB_GLB_CFG_REG		0x00
+#define SMB_GLB_EN_REG		0x04
+#define SMB_GLB_INT_REG		0x08
+
+/* Offset of SMB logical buffer registers */
+#define SMB_LB_CFG_LO_REG	0x40
+#define SMB_LB_CFG_HI_REG	0x44
+#define SMB_LB_INT_CTRL_REG	0x48
+#define SMB_LB_INT_STS_REG	0x4c
+#define SMB_LB_RD_ADDR_REG	0x5c
+#define SMB_LB_WR_ADDR_REG	0x60
+#define SMB_LB_PURGE_REG	0x64
+
+/* Set global config register */
+#define SMB_GLB_CFG_BURST_LEN_MSK	GENMASK(11, 4)
+#define SMB_GLB_CFG_IDLE_PRD_MSK	GENMASK(15, 12)
+#define SMB_GLB_CFG_MEM_WR_MSK		GENMASK(21, 16)
+#define SMB_GLB_CFG_MEM_RD_MSK		GENMASK(27, 22)
+#define SMB_GLB_CFG_DEFAULT	(FIELD_PREP(SMB_GLB_CFG_BURST_LEN_MSK, 0xf) | \
+				 FIELD_PREP(SMB_GLB_CFG_IDLE_PRD_MSK, 0xf) | \
+				 FIELD_PREP(SMB_GLB_CFG_MEM_WR_MSK, 0x3) | \
+				 FIELD_PREP(SMB_GLB_CFG_MEM_RD_MSK, 0x1b))
+
+#define SMB_GLB_EN_HW_ENABLE	BIT(0)
+
+/* Set global interrupt control register */
+#define SMB_GLB_INT_EN		BIT(0)
+#define SMB_GLB_INT_PULSE	BIT(1) /* Interrupt type: 1 - Pulse */
+#define SMB_GLB_INT_ACT_H	BIT(2) /* Interrupt polarity: 1 - Active high */
+#define SMB_GLB_INT_CFG		(SMB_GLB_INT_EN | SMB_GLB_INT_PULSE | \
+				 SMB_GLB_INT_ACT_H)
+
+/* Set logical buffer config register lower 32 bits */
+#define SMB_LB_CFG_LO_EN		BIT(0)
+#define SMB_LB_CFG_LO_SINGLE_END	BIT(1)
+#define SMB_LB_CFG_LO_INIT		BIT(8)
+#define SMB_LB_CFG_LO_CONT		BIT(11)
+#define SMB_LB_CFG_LO_FLOW_MSK		GENMASK(19, 16)
+#define SMB_LB_CFG_LO_DEFAULT	(SMB_LB_CFG_LO_EN | SMB_LB_CFG_LO_SINGLE_END | \
+				 SMB_LB_CFG_LO_INIT | SMB_LB_CFG_LO_CONT | \
+				 FIELD_PREP(SMB_LB_CFG_LO_FLOW_MSK, 0xf))
+
+/* Set logical buffer config register upper 32 bits */
+#define SMB_LB_CFG_HI_RANGE_UP_MSK	GENMASK(15, 8)
+#define SMB_LB_CFG_HI_DEFAULT	FIELD_PREP(SMB_LB_CFG_HI_RANGE_UP_MSK, 0xff)
+
+/*
+ * Set logical buffer interrupt control register.
+ * The register control the validity of both real-time events and
+ * interrupts. When logical buffer status changes causes to issue
+ * an interrupt at the same time as it issues a real-time event.
+ * Real-time events are used in SMB driver, which needs to get the buffer
+ * status. Interrupts are used in debugger mode.
+ * SMB_LB_INT_CTRL_BUF_NOTE_MASK control which events flags or interrupts
+ * are valid.
+ */
+#define SMB_LB_INT_CTRL_EN		BIT(0)
+#define SMB_LB_INT_CTRL_BUF_NOTE_MSK	GENMASK(11, 8)
+#define SMB_LB_INT_CTRL_CFG	(SMB_LB_INT_CTRL_EN | \
+				 FIELD_PREP(SMB_LB_INT_CTRL_BUF_NOTE_MSK, 0xf))
+
+/* Set logical buffer interrupt status register */
+#define SMB_LB_INT_STS_NOT_EMPTY_MSK	BIT(0)
+#define SMB_LB_INT_STS_BUF_RESET_MSK	GENMASK(3, 0)
+#define SMB_LB_INT_STS_RESET	FIELD_PREP(SMB_LB_INT_STS_BUF_RESET_MSK, 0xf)
+
+#define SMB_LB_PURGE_PURGED	BIT(0)
+
+#define SMB_REG_ADDR_RES	0
+#define SMB_BUF_ADDR_RES	1
+#define SMB_BUF_ADDR_LO_MSK	GENMASK(31, 0)
+
+/**
+ * struct smb_data_buffer - Details of the buffer used by SMB
+ * @buf_base:	Memory mapped base address of SMB.
+ * @start_addr:	SMB buffer start Physical address, only used 32bits.
+ * @buf_size:	Size of the buffer.
+ * @data_size:	Size of Trace data copy to userspace.
+ * @rd_offset:	Offset of the read pointer in the buffer.
+ * @wr_offset:	Offset of the write pointer in the buffer.
+ * @full:	Trace data overflow.
+ */
+struct smb_data_buffer {
+	void *buf_base;
+	u32 start_addr;
+	unsigned long buf_size;
+	unsigned long data_size;
+	unsigned long rd_offset;
+	unsigned long wr_offset;
+	bool full;
+};
+
+/**
+ * struct smb_drv_data - specifics associated to an SMB component
+ * @base:	Memory mapped base address for SMB component.
+ * @csdev:	Component vitals needed by the framework.
+ * @sdb:	Data buffer for SMB.
+ * @miscdev:	Specifics to handle "/dev/xyz.smb" entry.
+ * @mutex:	Control data access to one at a time.
+ * @reading:	Synchronise user space access to SMB buffer.
+ * @pid:	Process ID of the process being monitored by the
+ *		session that is using this component.
+ * @mode:	How this SMB is being used, perf mode or sysfs mode.
+ */
+struct smb_drv_data {
+	void __iomem *base;
+	struct coresight_device	*csdev;
+	struct smb_data_buffer sdb;
+	struct miscdevice miscdev;
+	struct mutex mutex;
+	bool reading;
+	pid_t pid;
+	u32 mode;
+};
+
+#endif
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v14 2/2] Documentation: Add document for UltraSoc SMB driver
  2022-11-23 12:38 [PATCH v14 0/2] Add support for UltraSoc System Memory Buffer Junhao He
  2022-11-23 12:38 ` [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver Junhao He
@ 2022-11-23 12:38 ` Junhao He
  1 sibling, 0 replies; 9+ messages in thread
From: Junhao He @ 2022-11-23 12:38 UTC (permalink / raw)
  To: mathieu.poirier, suzuki.poulose, mike.leach, leo.yan,
	jonathan.cameron, john.garry
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng,
	hejunhao3

From: Qi Liu <liuqi115@huawei.com>

Bring in documentation for UltraSoc SMB driver.
It simply describes the device, sysfs interface and the
firmware bindings.

Signed-off-by: Qi Liu <liuqi115@huawei.com>
Signed-off-by: Junhao He <hejunhao3@huawei.com>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
---
 .../sysfs-bus-coresight-devices-ultra_smb     | 31 +++++++
 .../trace/coresight/ultrasoc-smb.rst          | 83 +++++++++++++++++++
 2 files changed, 114 insertions(+)
 create mode 100644 Documentation/ABI/testing/sysfs-bus-coresight-devices-ultra_smb
 create mode 100644 Documentation/trace/coresight/ultrasoc-smb.rst

diff --git a/Documentation/ABI/testing/sysfs-bus-coresight-devices-ultra_smb b/Documentation/ABI/testing/sysfs-bus-coresight-devices-ultra_smb
new file mode 100644
index 000000000000..deaefd508105
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-bus-coresight-devices-ultra_smb
@@ -0,0 +1,31 @@
+What:		/sys/bus/coresight/devices/ultra_smb<N>/enable_sink
+Date:		November 2022
+KernelVersion:	6.2
+Contact:	Junhao He <hejunhao3@huawei.com>
+Description:	(RW) Add/remove a SMB device from a trace path. There can be
+		multiple sources for a single SMB device.
+
+What:		/sys/bus/coresight/devices/ultra_smb<N>/mgmt/buf_size
+Date:		November 2022
+KernelVersion:	6.2
+Contact:	Junhao He <hejunhao3@huawei.com>
+Description:	(Read) Shows the buffer size of each UltraSoc SMB device.
+
+What:		/sys/bus/coresight/devices/ultra_smb<N>/mgmt/buf_status
+Date:		November 2022
+KernelVersion:	6.2
+Contact:	Junhao He <hejunhao3@huawei.com>
+Description:	(Read) Shows the value held by UltraSoc SMB status register.
+		BIT(0) is zero means buffer is empty.
+
+What:		/sys/bus/coresight/devices/ultra_smb<N>/mgmt/read_pos
+Date:		November 2022
+KernelVersion:	6.2
+Contact:	Junhao He <hejunhao3@huawei.com>
+Description:	(Read) Shows the value held by UltraSoc SMB Read Pointer register.
+
+What:		/sys/bus/coresight/devices/ultra_smb<N>/mgmt/write_pos
+Date:		November 2022
+KernelVersion:	6.2
+Contact:	Junhao He <hejunhao3@huawei.com>
+Description:	(Read) Shows the value held by UltraSoc SMB Write Pointer register.
diff --git a/Documentation/trace/coresight/ultrasoc-smb.rst b/Documentation/trace/coresight/ultrasoc-smb.rst
new file mode 100644
index 000000000000..3f3ebe158272
--- /dev/null
+++ b/Documentation/trace/coresight/ultrasoc-smb.rst
@@ -0,0 +1,83 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======================================
+UltraSoc - HW Assisted Tracing on SoC
+======================================
+   :Author:   Qi Liu <liuqi115@huawei.com>
+   :Date:     November 2022
+
+Introduction
+------------
+
+UltraSoc SMB is a per SCCL (Super CPU Cluster) hardware. It provides a
+way to buffer and store CPU trace messages in a region of shared system
+memory. The device acts as a coresight sink device and the
+corresponding trace generators (ETM) are attached as source devices.
+
+Sysfs files and directories
+---------------------------
+
+The SMB devices appear on the existing coresight bus alongside other
+devices::
+
+	$# ls /sys/bus/coresight/devices/
+	ultra_smb0   ultra_smb1   ultra_smb2   ultra_smb3
+
+The ``ultra_smb<N>`` names SMB device associated with SCCL.::
+
+	$# ls /sys/bus/coresight/devices/ultra_smb0
+	enable_sink   mgmt
+	$# ls /sys/bus/coresight/devices/ultra_smb0/mgmt
+	buf_size  buf_status  read_pos  write_pos
+
+Key file items are:
+
+   * ``read_pos``: Shows the value on the read pointer register.
+   * ``write_pos``: Shows the value on the write pointer register.
+   * ``buf_status``: Shows the value on the status register.
+     BIT(0) is zero value which means the buffer is empty.
+   * ``buf_size``: Shows the buffer size of each device.
+
+Firmware Bindings
+-----------------
+
+The device is only supported with ACPI. Its binding describes device
+identifier, resource information and graph structure.
+
+The device is identified as ACPI HID "HISI03A1". Device resources are allocated
+using the _CRS method. Each device must present two base address; the first one
+is the configuration base address of the device, the second one is the 32-bit
+base address of shared system memory.
+
+Example::
+
+    Device(USMB) {                                               \
+      Name(_HID, "HISI03A1")                                     \
+      Name(_CRS, ResourceTemplate() {                            \
+          QWordMemory (ResourceConsumer, , MinFixed, MaxFixed, NonCacheable, \
+		       ReadWrite, 0x0, 0x95100000, 0x951FFFFF, 0x0, 0x100000) \
+          QWordMemory (ResourceConsumer, , MinFixed, MaxFixed, Cacheable, \
+		       ReadWrite, 0x0, 0x50000000, 0x53FFFFFF, 0x0, 0x4000000) \
+      })                                                         \
+      Name(_DSD, Package() {                                     \
+        ToUUID("ab02a46b-74c7-45a2-bd68-f7d344ef2153"),          \
+	/* Use CoreSight Graph ACPI bindings to describe connections topology */
+        Package() {                                              \
+          0,                                                     \
+          1,                                                     \
+          Package() {                                            \
+            1,                                                   \
+            ToUUID("3ecbc8b6-1d0e-4fb3-8107-e627f805c6cd"),      \
+            8,                                                   \
+            Package() {0x8, 0, \_SB.S00.SL11.CL28.F008, 0},       \
+            Package() {0x9, 0, \_SB.S00.SL11.CL29.F009, 0},       \
+            Package() {0xa, 0, \_SB.S00.SL11.CL2A.F010, 0},       \
+            Package() {0xb, 0, \_SB.S00.SL11.CL2B.F011, 0},       \
+            Package() {0xc, 0, \_SB.S00.SL11.CL2C.F012, 0},       \
+            Package() {0xd, 0, \_SB.S00.SL11.CL2D.F013, 0},       \
+            Package() {0xe, 0, \_SB.S00.SL11.CL2E.F014, 0},       \
+            Package() {0xf, 0, \_SB.S00.SL11.CL2F.F015, 0},       \
+          }                                                      \
+        }                                                        \
+      })                                                         \
+    }
-- 
2.33.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver
  2022-11-23 12:38 ` [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver Junhao He
@ 2022-11-23 14:03   ` Suzuki K Poulose
  2022-11-24 13:33     ` hejunhao
  0 siblings, 1 reply; 9+ messages in thread
From: Suzuki K Poulose @ 2022-11-23 14:03 UTC (permalink / raw)
  To: Junhao He, mathieu.poirier, mike.leach, leo.yan,
	jonathan.cameron, john.garry
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng

On 23/11/2022 12:38, Junhao He wrote:
> From: Qi Liu <liuqi115@huawei.com>
> 
> Add driver for UltraSoc SMB(System Memory Buffer) device.
> SMB provides a way to buffer messages from ETM, and store
> these "CPU instructions trace" in system memory.
> The SMB device is identifier as ACPI HID "HISI03A1". Device
> system memory address resources are allocated using the _CRS
> method and buffer modes is the circular buffer mode.
> 
> SMB is developed by UltraSoc technology, which is acquired by
> Siemens, and we still use "UltraSoc" to name driver.
> 
> Signed-off-by: Qi Liu <liuqi115@huawei.com>
> Signed-off-by: Junhao He <hejunhao3@huawei.com>
> Tested-by: JunHao He <hejunhao3@huawei.com>
> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> ---
>   drivers/hwtracing/coresight/Kconfig        |  12 +
>   drivers/hwtracing/coresight/Makefile       |   1 +
>   drivers/hwtracing/coresight/ultrasoc-smb.c | 658 +++++++++++++++++++++
>   drivers/hwtracing/coresight/ultrasoc-smb.h | 129 ++++
>   4 files changed, 800 insertions(+)
>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h
> 

> +static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
> +				 struct cs_buffers *buf,
> +				 unsigned long head,
> +				 unsigned long data_size)
> +{
> +	struct smb_data_buffer *sdb = &drvdata->sdb;
> +	char **dst_pages = (char **)buf->data_pages;
> +	unsigned long to_copy;
> +	long pg_idx, pg_offset;
> +
> +	pg_idx = head >> PAGE_SHIFT;
> +	pg_offset = head & (PAGE_SIZE - 1);
> +
> +	while (data_size) {
> +		unsigned long pg_space = PAGE_SIZE - pg_offset;
> +
> +		/* Copy parts of trace data when read pointer wrap around */
> +		if (sdb->rd_offset + pg_space > sdb->buf_size)
> +			to_copy = sdb->buf_size - sdb->rd_offset;
> +		else
> +			to_copy = min(data_size, pg_space);
> +
> +		memcpy(dst_pages[pg_idx] + pg_offset,
> +			      sdb->buf_base + sdb->rd_offset, to_copy);
> +
> +		pg_offset += to_copy;
> +		if (pg_offset >= PAGE_SIZE) {
> +			pg_offset = 0;
> +			pg_idx++;
> +			pg_idx %= buf->nr_pages;
> +		}
> +		data_size -= to_copy;
> +		sdb->rd_offset += to_copy;
> +		sdb->rd_offset %= sdb->buf_size;
> +	}
> +
> +	sdb->data_size = 0;


--8>-- cut here --<8--

> +	writel(sdb->start_addr + sdb->rd_offset,
> +		drvdata->base + SMB_LB_RD_ADDR_REG);
> +
> +	/*
> +	 * Data remained in link cannot be purged when SMB is full, so
> +	 * synchronize the read pointer to write pointer, to make sure
> +	 * these remained data won't influence next trace.
> +	 */
> +	if (sdb->full) {
> +		smb_purge_data(drvdata);
> +		writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
> +		       drvdata->base + SMB_LB_RD_ADDR_REG);
> +	}

--<8-- end here --8>--

As pointed out in the last review, we must do this step
everytime for perf mode irrespective of whether the buffer
was "FULL" or not.

i.e, the above block should simply be:

	if (sdb->full)
		smb_purge_data(drvdata);

	/*
	 * The uncollected Data must be discarded for perf,
	 * as it cannot be clubbed with next schedule. We
	 * any way TRUNCATE the buffer in this case.
	 */
	writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
		drvdata->base + SMB_LB_RD_ADDR_REG);

Suzuki

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver
  2022-11-23 14:03   ` Suzuki K Poulose
@ 2022-11-24 13:33     ` hejunhao
  2022-11-24 13:45       ` Suzuki K Poulose
  0 siblings, 1 reply; 9+ messages in thread
From: hejunhao @ 2022-11-24 13:33 UTC (permalink / raw)
  To: Suzuki K Poulose, mathieu.poirier, mike.leach, leo.yan,
	jonathan.cameron, john.garry
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng


On 2022/11/23 22:03, Suzuki K Poulose wrote:
> On 23/11/2022 12:38, Junhao He wrote:
>> From: Qi Liu <liuqi115@huawei.com>
>>
>> Add driver for UltraSoc SMB(System Memory Buffer) device.
>> SMB provides a way to buffer messages from ETM, and store
>> these "CPU instructions trace" in system memory.
>> The SMB device is identifier as ACPI HID "HISI03A1". Device
>> system memory address resources are allocated using the _CRS
>> method and buffer modes is the circular buffer mode.
>>
>> SMB is developed by UltraSoc technology, which is acquired by
>> Siemens, and we still use "UltraSoc" to name driver.
>>
>> Signed-off-by: Qi Liu <liuqi115@huawei.com>
>> Signed-off-by: Junhao He <hejunhao3@huawei.com>
>> Tested-by: JunHao He <hejunhao3@huawei.com>
>> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>> ---
>>   drivers/hwtracing/coresight/Kconfig        |  12 +
>>   drivers/hwtracing/coresight/Makefile       |   1 +
>>   drivers/hwtracing/coresight/ultrasoc-smb.c | 658 +++++++++++++++++++++
>>   drivers/hwtracing/coresight/ultrasoc-smb.h | 129 ++++
>>   4 files changed, 800 insertions(+)
>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h
>>
>
>> +static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
>> +                 struct cs_buffers *buf,
>> +                 unsigned long head,
>> +                 unsigned long data_size)
>> +{
>> +    struct smb_data_buffer *sdb = &drvdata->sdb;
>> +    char **dst_pages = (char **)buf->data_pages;
>> +    unsigned long to_copy;
>> +    long pg_idx, pg_offset;
>> +
>> +    pg_idx = head >> PAGE_SHIFT;
>> +    pg_offset = head & (PAGE_SIZE - 1);
>> +
>> +    while (data_size) {
>> +        unsigned long pg_space = PAGE_SIZE - pg_offset;
>> +
>> +        /* Copy parts of trace data when read pointer wrap around */
>> +        if (sdb->rd_offset + pg_space > sdb->buf_size)
>> +            to_copy = sdb->buf_size - sdb->rd_offset;
>> +        else
>> +            to_copy = min(data_size, pg_space);
>> +
>> +        memcpy(dst_pages[pg_idx] + pg_offset,
>> +                  sdb->buf_base + sdb->rd_offset, to_copy);
>> +
>> +        pg_offset += to_copy;
>> +        if (pg_offset >= PAGE_SIZE) {
>> +            pg_offset = 0;
>> +            pg_idx++;
>> +            pg_idx %= buf->nr_pages;
>> +        }
>> +        data_size -= to_copy;
>> +        sdb->rd_offset += to_copy;
>> +        sdb->rd_offset %= sdb->buf_size;
>> +    }
>> +
>> +    sdb->data_size = 0;
>
>
> --8>-- cut here --<8--
>
>> +    writel(sdb->start_addr + sdb->rd_offset,
>> +        drvdata->base + SMB_LB_RD_ADDR_REG);
>> +
>> +    /*
>> +     * Data remained in link cannot be purged when SMB is full, so
>> +     * synchronize the read pointer to write pointer, to make sure
>> +     * these remained data won't influence next trace.
>> +     */
>> +    if (sdb->full) {
>> +        smb_purge_data(drvdata);
>> +        writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>> +               drvdata->base + SMB_LB_RD_ADDR_REG);
>> +    }
>
> --<8-- end here --8>--
>
> As pointed out in the last review, we must do this step
> everytime for perf mode irrespective of whether the buffer
> was "FULL" or not.
>
> i.e, the above block should simply be:
>
>     if (sdb->full)
>         smb_purge_data(drvdata);
>
>     /*
>      * The uncollected Data must be discarded for perf,
>      * as it cannot be clubbed with next schedule. We
>      * any way TRUNCATE the buffer in this case.
>      */
>     writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>         drvdata->base + SMB_LB_RD_ADDR_REG);
>
> Suzuki

Hi Suzuki,

We need to update SMB_LB_RD_ADDR_REG register first, then
check the "full" flag, whether the register needs to be
updated again.

If we don`t update the value of SMB_LB_RD_ADDR_REG register
or reset buffer state, the buffer state will still be "full".
The buffer has not free area,so the data will still remain
in link.

Thanks.
HeJunhao.

> _______________________________________________
> CoreSight mailing list -- coresight@lists.linaro.org
> To unsubscribe send an email to coresight-leave@lists.linaro.org
>
> .
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver
  2022-11-24 13:33     ` hejunhao
@ 2022-11-24 13:45       ` Suzuki K Poulose
  2022-11-25 14:26         ` hejunhao
  0 siblings, 1 reply; 9+ messages in thread
From: Suzuki K Poulose @ 2022-11-24 13:45 UTC (permalink / raw)
  To: hejunhao, mathieu.poirier, mike.leach, leo.yan, jonathan.cameron
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng

On 24/11/2022 13:33, hejunhao wrote:
> 
> On 2022/11/23 22:03, Suzuki K Poulose wrote:
>> On 23/11/2022 12:38, Junhao He wrote:
>>> From: Qi Liu <liuqi115@huawei.com>
>>>
>>> Add driver for UltraSoc SMB(System Memory Buffer) device.
>>> SMB provides a way to buffer messages from ETM, and store
>>> these "CPU instructions trace" in system memory.
>>> The SMB device is identifier as ACPI HID "HISI03A1". Device
>>> system memory address resources are allocated using the _CRS
>>> method and buffer modes is the circular buffer mode.
>>>
>>> SMB is developed by UltraSoc technology, which is acquired by
>>> Siemens, and we still use "UltraSoc" to name driver.
>>>
>>> Signed-off-by: Qi Liu <liuqi115@huawei.com>
>>> Signed-off-by: Junhao He <hejunhao3@huawei.com>
>>> Tested-by: JunHao He <hejunhao3@huawei.com>
>>> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>>> ---
>>>   drivers/hwtracing/coresight/Kconfig        |  12 +
>>>   drivers/hwtracing/coresight/Makefile       |   1 +
>>>   drivers/hwtracing/coresight/ultrasoc-smb.c | 658 +++++++++++++++++++++
>>>   drivers/hwtracing/coresight/ultrasoc-smb.h | 129 ++++
>>>   4 files changed, 800 insertions(+)
>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h
>>>
>>
>>> +static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
>>> +                 struct cs_buffers *buf,
>>> +                 unsigned long head,
>>> +                 unsigned long data_size)
>>> +{
>>> +    struct smb_data_buffer *sdb = &drvdata->sdb;
>>> +    char **dst_pages = (char **)buf->data_pages;
>>> +    unsigned long to_copy;
>>> +    long pg_idx, pg_offset;
>>> +
>>> +    pg_idx = head >> PAGE_SHIFT;
>>> +    pg_offset = head & (PAGE_SIZE - 1);
>>> +
>>> +    while (data_size) {
>>> +        unsigned long pg_space = PAGE_SIZE - pg_offset;
>>> +
>>> +        /* Copy parts of trace data when read pointer wrap around */
>>> +        if (sdb->rd_offset + pg_space > sdb->buf_size)
>>> +            to_copy = sdb->buf_size - sdb->rd_offset;
>>> +        else
>>> +            to_copy = min(data_size, pg_space);
>>> +
>>> +        memcpy(dst_pages[pg_idx] + pg_offset,
>>> +                  sdb->buf_base + sdb->rd_offset, to_copy);
>>> +
>>> +        pg_offset += to_copy;
>>> +        if (pg_offset >= PAGE_SIZE) {
>>> +            pg_offset = 0;
>>> +            pg_idx++;
>>> +            pg_idx %= buf->nr_pages;
>>> +        }
>>> +        data_size -= to_copy;
>>> +        sdb->rd_offset += to_copy;
>>> +        sdb->rd_offset %= sdb->buf_size;
>>> +    }
>>> +
>>> +    sdb->data_size = 0;
>>
>>
>> --8>-- cut here --<8--
>>
>>> +    writel(sdb->start_addr + sdb->rd_offset,
>>> +        drvdata->base + SMB_LB_RD_ADDR_REG);
>>> +
>>> +    /*
>>> +     * Data remained in link cannot be purged when SMB is full, so
>>> +     * synchronize the read pointer to write pointer, to make sure
>>> +     * these remained data won't influence next trace.
>>> +     */
>>> +    if (sdb->full) {
>>> +        smb_purge_data(drvdata);
>>> +        writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>> +               drvdata->base + SMB_LB_RD_ADDR_REG);
>>> +    }
>>
>> --<8-- end here --8>--
>>
>> As pointed out in the last review, we must do this step
>> everytime for perf mode irrespective of whether the buffer
>> was "FULL" or not.
>>
>> i.e, the above block should simply be:
>>
>>     if (sdb->full)
>>         smb_purge_data(drvdata);
>>
>>     /*
>>      * The uncollected Data must be discarded for perf,
>>      * as it cannot be clubbed with next schedule. We
>>      * any way TRUNCATE the buffer in this case.
>>      */
>>     writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>         drvdata->base + SMB_LB_RD_ADDR_REG);
>>
>> Suzuki
> 
> Hi Suzuki,
> 
> We need to update SMB_LB_RD_ADDR_REG register first, then
> check the "full" flag, whether the register needs to be
> updated again.

Why ? sdb->full is not updated after the write to RD_ADDR_REG.

> 
> If we don`t update the value of SMB_LB_RD_ADDR_REG register
> or reset buffer state, the buffer state will still be "full".
> The buffer has not free area,so the data will still remain
> in link.

My issue here is with potentially "leaving the trace from a previous
session for the next session". i.e., at the end of a run, we must always
make sure that the buffer is left empty (unlike the sysfs mode).

e.g.,

perf_session_x: RUN0: RD=0x0, WR=0x5000, HANDLE_SIZE=0x3000, full=false.
At the end of the above routine we will have :
	RD=0x3000, WR=0x5000,

and if a different perf session comes in, say perf_session_y, it will 
consume trace written by "x" at 0x3000-0x50000, right ?

This is fine in the sysfs mode as we expect the entire sysfs mode
to be owned by a single session and is left to the user to split it.
But for perf mode we cannot do that and thus must make sure we don't
leak trace from one session to antoher.

Suzuki


> 
> Thanks.
> HeJunhao.
> 
>> _______________________________________________
>> CoreSight mailing list -- coresight@lists.linaro.org
>> To unsubscribe send an email to coresight-leave@lists.linaro.org
>>
>> .
>>
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver
  2022-11-24 13:45       ` Suzuki K Poulose
@ 2022-11-25 14:26         ` hejunhao
  2022-11-28 11:06           ` Suzuki K Poulose
  0 siblings, 1 reply; 9+ messages in thread
From: hejunhao @ 2022-11-25 14:26 UTC (permalink / raw)
  To: Suzuki K Poulose, mathieu.poirier, mike.leach, leo.yan, jonathan.cameron
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng


On 2022/11/24 21:45, Suzuki K Poulose wrote:
> On 24/11/2022 13:33, hejunhao wrote:
>>
>> On 2022/11/23 22:03, Suzuki K Poulose wrote:
>>> On 23/11/2022 12:38, Junhao He wrote:
>>>> From: Qi Liu <liuqi115@huawei.com>
>>>>
>>>> Add driver for UltraSoc SMB(System Memory Buffer) device.
>>>> SMB provides a way to buffer messages from ETM, and store
>>>> these "CPU instructions trace" in system memory.
>>>> The SMB device is identifier as ACPI HID "HISI03A1". Device
>>>> system memory address resources are allocated using the _CRS
>>>> method and buffer modes is the circular buffer mode.
>>>>
>>>> SMB is developed by UltraSoc technology, which is acquired by
>>>> Siemens, and we still use "UltraSoc" to name driver.
>>>>
>>>> Signed-off-by: Qi Liu <liuqi115@huawei.com>
>>>> Signed-off-by: Junhao He <hejunhao3@huawei.com>
>>>> Tested-by: JunHao He <hejunhao3@huawei.com>
>>>> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>>>> ---
>>>>   drivers/hwtracing/coresight/Kconfig        |  12 +
>>>>   drivers/hwtracing/coresight/Makefile       |   1 +
>>>>   drivers/hwtracing/coresight/ultrasoc-smb.c | 658 
>>>> +++++++++++++++++++++
>>>>   drivers/hwtracing/coresight/ultrasoc-smb.h | 129 ++++
>>>>   4 files changed, 800 insertions(+)
>>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
>>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h
>>>>
>>>
>>>> +static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
>>>> +                 struct cs_buffers *buf,
>>>> +                 unsigned long head,
>>>> +                 unsigned long data_size)
>>>> +{
>>>> +    struct smb_data_buffer *sdb = &drvdata->sdb;
>>>> +    char **dst_pages = (char **)buf->data_pages;
>>>> +    unsigned long to_copy;
>>>> +    long pg_idx, pg_offset;
>>>> +
>>>> +    pg_idx = head >> PAGE_SHIFT;
>>>> +    pg_offset = head & (PAGE_SIZE - 1);
>>>> +
>>>> +    while (data_size) {
>>>> +        unsigned long pg_space = PAGE_SIZE - pg_offset;
>>>> +
>>>> +        /* Copy parts of trace data when read pointer wrap around */
>>>> +        if (sdb->rd_offset + pg_space > sdb->buf_size)
>>>> +            to_copy = sdb->buf_size - sdb->rd_offset;
>>>> +        else
>>>> +            to_copy = min(data_size, pg_space);
>>>> +
>>>> +        memcpy(dst_pages[pg_idx] + pg_offset,
>>>> +                  sdb->buf_base + sdb->rd_offset, to_copy);
>>>> +
>>>> +        pg_offset += to_copy;
>>>> +        if (pg_offset >= PAGE_SIZE) {
>>>> +            pg_offset = 0;
>>>> +            pg_idx++;
>>>> +            pg_idx %= buf->nr_pages;
>>>> +        }
>>>> +        data_size -= to_copy;
>>>> +        sdb->rd_offset += to_copy;
>>>> +        sdb->rd_offset %= sdb->buf_size;
>>>> +    }
>>>> +
>>>> +    sdb->data_size = 0;
>>>
>>>
>>> --8>-- cut here --<8--
>>>
>>>> +    writel(sdb->start_addr + sdb->rd_offset,
>>>> +        drvdata->base + SMB_LB_RD_ADDR_REG);
>>>> +
>>>> +    /*
>>>> +     * Data remained in link cannot be purged when SMB is full, so
>>>> +     * synchronize the read pointer to write pointer, to make sure
>>>> +     * these remained data won't influence next trace.
>>>> +     */
>>>> +    if (sdb->full) {
>>>> +        smb_purge_data(drvdata);
>>>> +        writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>>> +               drvdata->base + SMB_LB_RD_ADDR_REG);
>>>> +    }
>>>
>>> --<8-- end here --8>--
>>>
>>> As pointed out in the last review, we must do this step
>>> everytime for perf mode irrespective of whether the buffer
>>> was "FULL" or not.
>>>
>>> i.e, the above block should simply be:
>>>
>>>     if (sdb->full)
>>>         smb_purge_data(drvdata);
>>>
>>>     /*
>>>      * The uncollected Data must be discarded for perf,
>>>      * as it cannot be clubbed with next schedule. We
>>>      * any way TRUNCATE the buffer in this case.
>>>      */
>>>     writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>>         drvdata->base + SMB_LB_RD_ADDR_REG);
>>>
>>> Suzuki
>>
>> Hi Suzuki,
>>
>> We need to update SMB_LB_RD_ADDR_REG register first, then
>> check the "full" flag, whether the register needs to be
>> updated again.
>
> Why ? sdb->full is not updated after the write to RD_ADDR_REG.
>
Hi Suzuki,

Maybe using the code below is more appropriate.
i.e,

     writel(sdb->start_addr + sdb->rd_offset,
         drvdata->base + SMB_LB_RD_ADDR_REG);

     /*
      * The uncollected Data must be discarded for perf,
      * as it cannot be clubbed with next schedule. We
      * any way TRUNCATE the buffer in this case.
      */
     smb_update_data_size(drvdata);
     if (sdb->data_size)
         writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
                 drvdata->base + SMB_LB_RD_ADDR_REG);

Best regards,
Junhao.

>>
>> If we don`t update the value of SMB_LB_RD_ADDR_REG register
>> or reset buffer state, the buffer state will still be "full".
>> The buffer has not free area,so the data will still remain
>> in link.
>
> My issue here is with potentially "leaving the trace from a previous
> session for the next session". i.e., at the end of a run, we must always
> make sure that the buffer is left empty (unlike the sysfs mode).
>
> e.g.,
>
> perf_session_x: RUN0: RD=0x0, WR=0x5000, HANDLE_SIZE=0x3000, full=false.
> At the end of the above routine we will have :
>     RD=0x3000, WR=0x5000,
>
> and if a different perf session comes in, say perf_session_y, it will 
> consume trace written by "x" at 0x3000-0x50000, right ?
>
> This is fine in the sysfs mode as we expect the entire sysfs mode
> to be owned by a single session and is left to the user to split it.
> But for perf mode we cannot do that and thus must make sure we don't
> leak trace from one session to antoher.
>
> Suzuki
>

In this cace:  RUN0: RD=0x0, WR=0x5000, HANDLE_SIZE=0x3000, full=false.
We will update the "rd_offset" in smb_update_buffer() function.
like this:

     ...
     if (data_size > handle->size) {
         sdb->rd_offset += data_size - handle->size;
         sdb->rd_offset %= sdb->buf_size;
         ...
         }
     ...


So the rd_offset will advance to 0x2000 first,
then we dump the latest trace data (0x2000 - 0x5000) from "buf_base + 
rd_offset".

Best regards,
Junhao.

>
>>
>> Thanks.
>> HeJunhao.
>>
>>> _______________________________________________
>>> CoreSight mailing list -- coresight@lists.linaro.org
>>> To unsubscribe send an email to coresight-leave@lists.linaro.org
>>>
>>> .
>>>
>>
>
> _______________________________________________
> CoreSight mailing list -- coresight@lists.linaro.org
> To unsubscribe send an email to coresight-leave@lists.linaro.org


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver
  2022-11-25 14:26         ` hejunhao
@ 2022-11-28 11:06           ` Suzuki K Poulose
  2022-12-16  9:30             ` hejunhao
  0 siblings, 1 reply; 9+ messages in thread
From: Suzuki K Poulose @ 2022-11-28 11:06 UTC (permalink / raw)
  To: hejunhao, mathieu.poirier, mike.leach, leo.yan, jonathan.cameron
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng

On 25/11/2022 14:26, hejunhao wrote:
> 
> On 2022/11/24 21:45, Suzuki K Poulose wrote:
>> On 24/11/2022 13:33, hejunhao wrote:
>>>
>>> On 2022/11/23 22:03, Suzuki K Poulose wrote:
>>>> On 23/11/2022 12:38, Junhao He wrote:
>>>>> From: Qi Liu <liuqi115@huawei.com>
>>>>>
>>>>> Add driver for UltraSoc SMB(System Memory Buffer) device.
>>>>> SMB provides a way to buffer messages from ETM, and store
>>>>> these "CPU instructions trace" in system memory.
>>>>> The SMB device is identifier as ACPI HID "HISI03A1". Device
>>>>> system memory address resources are allocated using the _CRS
>>>>> method and buffer modes is the circular buffer mode.
>>>>>
>>>>> SMB is developed by UltraSoc technology, which is acquired by
>>>>> Siemens, and we still use "UltraSoc" to name driver.
>>>>>
>>>>> Signed-off-by: Qi Liu <liuqi115@huawei.com>
>>>>> Signed-off-by: Junhao He <hejunhao3@huawei.com>
>>>>> Tested-by: JunHao He <hejunhao3@huawei.com>
>>>>> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>>>>> ---
>>>>>   drivers/hwtracing/coresight/Kconfig        |  12 +
>>>>>   drivers/hwtracing/coresight/Makefile       |   1 +
>>>>>   drivers/hwtracing/coresight/ultrasoc-smb.c | 658 
>>>>> +++++++++++++++++++++
>>>>>   drivers/hwtracing/coresight/ultrasoc-smb.h | 129 ++++
>>>>>   4 files changed, 800 insertions(+)
>>>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
>>>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h
>>>>>
>>>>
>>>>> +static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
>>>>> +                 struct cs_buffers *buf,
>>>>> +                 unsigned long head,
>>>>> +                 unsigned long data_size)
>>>>> +{
>>>>> +    struct smb_data_buffer *sdb = &drvdata->sdb;
>>>>> +    char **dst_pages = (char **)buf->data_pages;
>>>>> +    unsigned long to_copy;
>>>>> +    long pg_idx, pg_offset;
>>>>> +
>>>>> +    pg_idx = head >> PAGE_SHIFT;
>>>>> +    pg_offset = head & (PAGE_SIZE - 1);
>>>>> +
>>>>> +    while (data_size) {
>>>>> +        unsigned long pg_space = PAGE_SIZE - pg_offset;
>>>>> +
>>>>> +        /* Copy parts of trace data when read pointer wrap around */
>>>>> +        if (sdb->rd_offset + pg_space > sdb->buf_size)
>>>>> +            to_copy = sdb->buf_size - sdb->rd_offset;
>>>>> +        else
>>>>> +            to_copy = min(data_size, pg_space);
>>>>> +
>>>>> +        memcpy(dst_pages[pg_idx] + pg_offset,
>>>>> +                  sdb->buf_base + sdb->rd_offset, to_copy);
>>>>> +
>>>>> +        pg_offset += to_copy;
>>>>> +        if (pg_offset >= PAGE_SIZE) {
>>>>> +            pg_offset = 0;
>>>>> +            pg_idx++;
>>>>> +            pg_idx %= buf->nr_pages;
>>>>> +        }
>>>>> +        data_size -= to_copy;
>>>>> +        sdb->rd_offset += to_copy;
>>>>> +        sdb->rd_offset %= sdb->buf_size;
>>>>> +    }
>>>>> +
>>>>> +    sdb->data_size = 0;
>>>>
>>>>
>>>> --8>-- cut here --<8--
>>>>
>>>>> +    writel(sdb->start_addr + sdb->rd_offset,
>>>>> +        drvdata->base + SMB_LB_RD_ADDR_REG);
>>>>> +
>>>>> +    /*
>>>>> +     * Data remained in link cannot be purged when SMB is full, so
>>>>> +     * synchronize the read pointer to write pointer, to make sure
>>>>> +     * these remained data won't influence next trace.
>>>>> +     */
>>>>> +    if (sdb->full) {
>>>>> +        smb_purge_data(drvdata);
>>>>> +        writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>>>> +               drvdata->base + SMB_LB_RD_ADDR_REG);
>>>>> +    }
>>>>
>>>> --<8-- end here --8>--
>>>>
>>>> As pointed out in the last review, we must do this step
>>>> everytime for perf mode irrespective of whether the buffer
>>>> was "FULL" or not.
>>>>
>>>> i.e, the above block should simply be:
>>>>
>>>>     if (sdb->full)
>>>>         smb_purge_data(drvdata);
>>>>
>>>>     /*
>>>>      * The uncollected Data must be discarded for perf,
>>>>      * as it cannot be clubbed with next schedule. We
>>>>      * any way TRUNCATE the buffer in this case.
>>>>      */
>>>>     writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>>>         drvdata->base + SMB_LB_RD_ADDR_REG);
>>>>
>>>> Suzuki
>>>
>>> Hi Suzuki,
>>>
>>> We need to update SMB_LB_RD_ADDR_REG register first, then
>>> check the "full" flag, whether the register needs to be
>>> updated again.
>>
>> Why ? sdb->full is not updated after the write to RD_ADDR_REG.
>>
> Hi Suzuki,
> 
> Maybe using the code below is more appropriate.
> i.e,
> 
>      writel(sdb->start_addr + sdb->rd_offset,
>          drvdata->base + SMB_LB_RD_ADDR_REG);
> 
>      /*
>       * The uncollected Data must be discarded for perf,
>       * as it cannot be clubbed with next schedule. We
>       * any way TRUNCATE the buffer in this case.
>       */
>      smb_update_data_size(drvdata);
>      if (sdb->data_size)
>          writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>                  drvdata->base + SMB_LB_RD_ADDR_REG);

That should work. However, my question is, we must set the
RD_ADDR_REG to the WR_ADDR_REG in all cases. So, why not :

	/*
	 * We must flush and discard any data left in the
	 * buffer for a perf session, to avoid mixing
	 * the data from sessions.
	 * We TRUNCATE the buffer anyways to indicate
	 * that the buffer was lost.
	 */
	smb_purge_data();
	writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
		drvdata->base + SMB_LB_RD_ADDR_REG);

and avoid the extra write ?

>>>
>>> If we don`t update the value of SMB_LB_RD_ADDR_REG register
>>> or reset buffer state, the buffer state will still be "full".
>>> The buffer has not free area,so the data will still remain
>>> in link.
>>
>> My issue here is with potentially "leaving the trace from a previous
>> session for the next session". i.e., at the end of a run, we must always
>> make sure that the buffer is left empty (unlike the sysfs mode).
>>
>> e.g.,
>>
>> perf_session_x: RUN0: RD=0x0, WR=0x5000, HANDLE_SIZE=0x3000, full=false.
>> At the end of the above routine we will have :
>>     RD=0x3000, WR=0x5000,
>>
>> and if a different perf session comes in, say perf_session_y, it will 
>> consume trace written by "x" at 0x3000-0x50000, right ?
>>
>> This is fine in the sysfs mode as we expect the entire sysfs mode
>> to be owned by a single session and is left to the user to split it.
>> But for perf mode we cannot do that and thus must make sure we don't
>> leak trace from one session to antoher.
>>
>> Suzuki
>>
> 
> In this cace:  RUN0: RD=0x0, WR=0x5000, HANDLE_SIZE=0x3000, full=false.
> We will update the "rd_offset" in smb_update_buffer() function.
> like this:
> 
>      ...
>      if (data_size > handle->size) {
>          sdb->rd_offset += data_size - handle->size;
>          sdb->rd_offset %= sdb->buf_size;
>          ...
>          }
>      ...
> 
> 
> So the rd_offset will advance to 0x2000 first,
> then we dump the latest trace data (0x2000 - 0x5000) from "buf_base + 
> rd_offset".

Right, that makes sense.


Thanks
Suzuki



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver
  2022-11-28 11:06           ` Suzuki K Poulose
@ 2022-12-16  9:30             ` hejunhao
  0 siblings, 0 replies; 9+ messages in thread
From: hejunhao @ 2022-12-16  9:30 UTC (permalink / raw)
  To: Suzuki K Poulose, mathieu.poirier, mike.leach, leo.yan, jonathan.cameron
  Cc: coresight, linux-kernel, linux-arm-kernel, linux-doc, lpieralisi,
	linuxarm, yangyicong, liuqi6124, f.fangjian, prime.zeng


On 2022/11/28 19:06, Suzuki K Poulose wrote:
> On 25/11/2022 14:26, hejunhao wrote:
>>
>> On 2022/11/24 21:45, Suzuki K Poulose wrote:
>>> On 24/11/2022 13:33, hejunhao wrote:
>>>>
>>>> On 2022/11/23 22:03, Suzuki K Poulose wrote:
>>>>> On 23/11/2022 12:38, Junhao He wrote:
>>>>>> From: Qi Liu <liuqi115@huawei.com>
>>>>>>
>>>>>> Add driver for UltraSoc SMB(System Memory Buffer) device.
>>>>>> SMB provides a way to buffer messages from ETM, and store
>>>>>> these "CPU instructions trace" in system memory.
>>>>>> The SMB device is identifier as ACPI HID "HISI03A1". Device
>>>>>> system memory address resources are allocated using the _CRS
>>>>>> method and buffer modes is the circular buffer mode.
>>>>>>
>>>>>> SMB is developed by UltraSoc technology, which is acquired by
>>>>>> Siemens, and we still use "UltraSoc" to name driver.
>>>>>>
>>>>>> Signed-off-by: Qi Liu <liuqi115@huawei.com>
>>>>>> Signed-off-by: Junhao He <hejunhao3@huawei.com>
>>>>>> Tested-by: JunHao He <hejunhao3@huawei.com>
>>>>>> Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
>>>>>> ---
>>>>>>   drivers/hwtracing/coresight/Kconfig        |  12 +
>>>>>>   drivers/hwtracing/coresight/Makefile       |   1 +
>>>>>>   drivers/hwtracing/coresight/ultrasoc-smb.c | 658 
>>>>>> +++++++++++++++++++++
>>>>>>   drivers/hwtracing/coresight/ultrasoc-smb.h | 129 ++++
>>>>>>   4 files changed, 800 insertions(+)
>>>>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.c
>>>>>>   create mode 100644 drivers/hwtracing/coresight/ultrasoc-smb.h
>>>>>>
>>>>>
>>>>>> +static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
>>>>>> +                 struct cs_buffers *buf,
>>>>>> +                 unsigned long head,
>>>>>> +                 unsigned long data_size)
>>>>>> +{
>>>>>> +    struct smb_data_buffer *sdb = &drvdata->sdb;
>>>>>> +    char **dst_pages = (char **)buf->data_pages;
>>>>>> +    unsigned long to_copy;
>>>>>> +    long pg_idx, pg_offset;
>>>>>> +
>>>>>> +    pg_idx = head >> PAGE_SHIFT;
>>>>>> +    pg_offset = head & (PAGE_SIZE - 1);
>>>>>> +
>>>>>> +    while (data_size) {
>>>>>> +        unsigned long pg_space = PAGE_SIZE - pg_offset;
>>>>>> +
>>>>>> +        /* Copy parts of trace data when read pointer wrap 
>>>>>> around */
>>>>>> +        if (sdb->rd_offset + pg_space > sdb->buf_size)
>>>>>> +            to_copy = sdb->buf_size - sdb->rd_offset;
>>>>>> +        else
>>>>>> +            to_copy = min(data_size, pg_space);
>>>>>> +
>>>>>> +        memcpy(dst_pages[pg_idx] + pg_offset,
>>>>>> +                  sdb->buf_base + sdb->rd_offset, to_copy);
>>>>>> +
>>>>>> +        pg_offset += to_copy;
>>>>>> +        if (pg_offset >= PAGE_SIZE) {
>>>>>> +            pg_offset = 0;
>>>>>> +            pg_idx++;
>>>>>> +            pg_idx %= buf->nr_pages;
>>>>>> +        }
>>>>>> +        data_size -= to_copy;
>>>>>> +        sdb->rd_offset += to_copy;
>>>>>> +        sdb->rd_offset %= sdb->buf_size;
>>>>>> +    }
>>>>>> +
>>>>>> +    sdb->data_size = 0;
>>>>>
>>>>>
>>>>> --8>-- cut here --<8--
>>>>>
>>>>>> +    writel(sdb->start_addr + sdb->rd_offset,
>>>>>> +        drvdata->base + SMB_LB_RD_ADDR_REG);
>>>>>> +
>>>>>> +    /*
>>>>>> +     * Data remained in link cannot be purged when SMB is full, so
>>>>>> +     * synchronize the read pointer to write pointer, to make sure
>>>>>> +     * these remained data won't influence next trace.
>>>>>> +     */
>>>>>> +    if (sdb->full) {
>>>>>> +        smb_purge_data(drvdata);
>>>>>> +        writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>>>>> +               drvdata->base + SMB_LB_RD_ADDR_REG);
>>>>>> +    }
>>>>>
>>>>> --<8-- end here --8>--
>>>>>
>>>>> As pointed out in the last review, we must do this step
>>>>> everytime for perf mode irrespective of whether the buffer
>>>>> was "FULL" or not.
>>>>>
>>>>> i.e, the above block should simply be:
>>>>>
>>>>>     if (sdb->full)
>>>>>         smb_purge_data(drvdata);
>>>>>
>>>>>     /*
>>>>>      * The uncollected Data must be discarded for perf,
>>>>>      * as it cannot be clubbed with next schedule. We
>>>>>      * any way TRUNCATE the buffer in this case.
>>>>>      */
>>>>>     writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>>>>         drvdata->base + SMB_LB_RD_ADDR_REG);
>>>>>
>>>>> Suzuki
>>>>
>>>> Hi Suzuki,
>>>>
>>>> We need to update SMB_LB_RD_ADDR_REG register first, then
>>>> check the "full" flag, whether the register needs to be
>>>> updated again.
>>>
>>> Why ? sdb->full is not updated after the write to RD_ADDR_REG.
>>>
>> Hi Suzuki,
>>
>> Maybe using the code below is more appropriate.
>> i.e,
>>
>>      writel(sdb->start_addr + sdb->rd_offset,
>>          drvdata->base + SMB_LB_RD_ADDR_REG);
>>
>>      /*
>>       * The uncollected Data must be discarded for perf,
>>       * as it cannot be clubbed with next schedule. We
>>       * any way TRUNCATE the buffer in this case.
>>       */
>>      smb_update_data_size(drvdata);
>>      if (sdb->data_size)
>>          writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>>                  drvdata->base + SMB_LB_RD_ADDR_REG);
>
> That should work. However, my question is, we must set the
> RD_ADDR_REG to the WR_ADDR_REG in all cases. So, why not :
>
>     /*
>      * We must flush and discard any data left in the
>      * buffer for a perf session, to avoid mixing
>      * the data from sessions.
>      * We TRUNCATE the buffer anyways to indicate
>      * that the buffer was lost.
>      */
>     smb_purge_data();
>     writel(readl(drvdata->base + SMB_LB_WR_ADDR_REG),
>         drvdata->base + SMB_LB_RD_ADDR_REG);
>
> and avoid the extra write ?
>
Hi Suzuki,

In sysfs and perf mode, the code that handles buffer after dump
was rewritten in the next version.

Thanks,
Junhao.
>>>>
>>>> If we don`t update the value of SMB_LB_RD_ADDR_REG register
>>>> or reset buffer state, the buffer state will still be "full".
>>>> The buffer has not free area,so the data will still remain
>>>> in link.
>>>
>>> My issue here is with potentially "leaving the trace from a previous
>>> session for the next session". i.e., at the end of a run, we must 
>>> always
>>> make sure that the buffer is left empty (unlike the sysfs mode).
>>>
>>> e.g.,
>>>
>>> perf_session_x: RUN0: RD=0x0, WR=0x5000, HANDLE_SIZE=0x3000, 
>>> full=false.
>>> At the end of the above routine we will have :
>>>     RD=0x3000, WR=0x5000,
>>>
>>> and if a different perf session comes in, say perf_session_y, it 
>>> will consume trace written by "x" at 0x3000-0x50000, right ?
>>>
>>> This is fine in the sysfs mode as we expect the entire sysfs mode
>>> to be owned by a single session and is left to the user to split it.
>>> But for perf mode we cannot do that and thus must make sure we don't
>>> leak trace from one session to antoher.
>>>
>>> Suzuki
>>>
>>
>> In this cace:  RUN0: RD=0x0, WR=0x5000, HANDLE_SIZE=0x3000, full=false.
>> We will update the "rd_offset" in smb_update_buffer() function.
>> like this:
>>
>>      ...
>>      if (data_size > handle->size) {
>>          sdb->rd_offset += data_size - handle->size;
>>          sdb->rd_offset %= sdb->buf_size;
>>          ...
>>          }
>>      ...
>>
>>
>> So the rd_offset will advance to 0x2000 first,
>> then we dump the latest trace data (0x2000 - 0x5000) from "buf_base + 
>> rd_offset".
>
> Right, that makes sense.
>
>
> Thanks
> Suzuki
>
>
>
> .
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-12-16  9:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-23 12:38 [PATCH v14 0/2] Add support for UltraSoc System Memory Buffer Junhao He
2022-11-23 12:38 ` [PATCH v14 1/2] drivers/coresight: Add UltraSoc System Memory Buffer driver Junhao He
2022-11-23 14:03   ` Suzuki K Poulose
2022-11-24 13:33     ` hejunhao
2022-11-24 13:45       ` Suzuki K Poulose
2022-11-25 14:26         ` hejunhao
2022-11-28 11:06           ` Suzuki K Poulose
2022-12-16  9:30             ` hejunhao
2022-11-23 12:38 ` [PATCH v14 2/2] Documentation: Add document for UltraSoc SMB driver Junhao He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).