xref: /openbmc/linux/drivers/hwtracing/coresight/ultrasoc-smb.c (revision d0c44de2d8ffd2e4780d360b34ee6614aa4af080)
106f5c292SQi Liu // SPDX-License-Identifier: (GPL-2.0 OR MIT)
206f5c292SQi Liu /*
306f5c292SQi Liu  * Siemens System Memory Buffer driver.
406f5c292SQi Liu  * Copyright(c) 2022, HiSilicon Limited.
506f5c292SQi Liu  */
606f5c292SQi Liu 
706f5c292SQi Liu #include <linux/atomic.h>
806f5c292SQi Liu #include <linux/acpi.h>
906f5c292SQi Liu #include <linux/circ_buf.h>
1006f5c292SQi Liu #include <linux/err.h>
1106f5c292SQi Liu #include <linux/fs.h>
1206f5c292SQi Liu #include <linux/module.h>
1306f5c292SQi Liu #include <linux/mod_devicetable.h>
1406f5c292SQi Liu #include <linux/platform_device.h>
1506f5c292SQi Liu 
1606f5c292SQi Liu #include "coresight-etm-perf.h"
1706f5c292SQi Liu #include "coresight-priv.h"
1806f5c292SQi Liu #include "ultrasoc-smb.h"
1906f5c292SQi Liu 
2006f5c292SQi Liu DEFINE_CORESIGHT_DEVLIST(sink_devs, "ultra_smb");
2106f5c292SQi Liu 
2206f5c292SQi Liu #define ULTRASOC_SMB_DSM_UUID	"82ae1283-7f6a-4cbe-aa06-53e8fb24db18"
2306f5c292SQi Liu 
smb_buffer_not_empty(struct smb_drv_data * drvdata)2406f5c292SQi Liu static bool smb_buffer_not_empty(struct smb_drv_data *drvdata)
2506f5c292SQi Liu {
2606f5c292SQi Liu 	u32 buf_status = readl(drvdata->base + SMB_LB_INT_STS_REG);
2706f5c292SQi Liu 
2806f5c292SQi Liu 	return FIELD_GET(SMB_LB_INT_STS_NOT_EMPTY_MSK, buf_status);
2906f5c292SQi Liu }
3006f5c292SQi Liu 
smb_update_data_size(struct smb_drv_data * drvdata)3106f5c292SQi Liu static void smb_update_data_size(struct smb_drv_data *drvdata)
3206f5c292SQi Liu {
3306f5c292SQi Liu 	struct smb_data_buffer *sdb = &drvdata->sdb;
3406f5c292SQi Liu 	u32 buf_wrptr;
3506f5c292SQi Liu 
3606f5c292SQi Liu 	buf_wrptr = readl(drvdata->base + SMB_LB_WR_ADDR_REG) -
3706f5c292SQi Liu 			  sdb->buf_hw_base;
3806f5c292SQi Liu 
3906f5c292SQi Liu 	/* Buffer is full */
4006f5c292SQi Liu 	if (buf_wrptr == sdb->buf_rdptr && smb_buffer_not_empty(drvdata)) {
4106f5c292SQi Liu 		sdb->data_size = sdb->buf_size;
4206f5c292SQi Liu 		return;
4306f5c292SQi Liu 	}
4406f5c292SQi Liu 
4506f5c292SQi Liu 	/* The buffer mode is circular buffer mode */
4606f5c292SQi Liu 	sdb->data_size = CIRC_CNT(buf_wrptr, sdb->buf_rdptr,
4706f5c292SQi Liu 				  sdb->buf_size);
4806f5c292SQi Liu }
4906f5c292SQi Liu 
5006f5c292SQi Liu /*
5106f5c292SQi Liu  * The read pointer adds @nbytes bytes (may round up to the beginning)
5206f5c292SQi Liu  * after the data is read or discarded, while needing to update the
5306f5c292SQi Liu  * available data size.
5406f5c292SQi Liu  */
smb_update_read_ptr(struct smb_drv_data * drvdata,u32 nbytes)5506f5c292SQi Liu static void smb_update_read_ptr(struct smb_drv_data *drvdata, u32 nbytes)
5606f5c292SQi Liu {
5706f5c292SQi Liu 	struct smb_data_buffer *sdb = &drvdata->sdb;
5806f5c292SQi Liu 
5906f5c292SQi Liu 	sdb->buf_rdptr += nbytes;
6006f5c292SQi Liu 	sdb->buf_rdptr %= sdb->buf_size;
6106f5c292SQi Liu 	writel(sdb->buf_hw_base + sdb->buf_rdptr,
6206f5c292SQi Liu 	       drvdata->base + SMB_LB_RD_ADDR_REG);
6306f5c292SQi Liu 
6406f5c292SQi Liu 	sdb->data_size -= nbytes;
6506f5c292SQi Liu }
6606f5c292SQi Liu 
smb_reset_buffer(struct smb_drv_data * drvdata)6706f5c292SQi Liu static void smb_reset_buffer(struct smb_drv_data *drvdata)
6806f5c292SQi Liu {
6906f5c292SQi Liu 	struct smb_data_buffer *sdb = &drvdata->sdb;
7006f5c292SQi Liu 	u32 write_ptr;
7106f5c292SQi Liu 
7206f5c292SQi Liu 	/*
7306f5c292SQi Liu 	 * We must flush and discard any data left in hardware path
7406f5c292SQi Liu 	 * to avoid corrupting the next session.
7506f5c292SQi Liu 	 * Note: The write pointer will never exceed the read pointer.
7606f5c292SQi Liu 	 */
7706f5c292SQi Liu 	writel(SMB_LB_PURGE_PURGED, drvdata->base + SMB_LB_PURGE_REG);
7806f5c292SQi Liu 
7906f5c292SQi Liu 	/* Reset SMB logical buffer status flags */
8006f5c292SQi Liu 	writel(SMB_LB_INT_STS_RESET, drvdata->base + SMB_LB_INT_STS_REG);
8106f5c292SQi Liu 
8206f5c292SQi Liu 	write_ptr = readl(drvdata->base + SMB_LB_WR_ADDR_REG);
8306f5c292SQi Liu 
8406f5c292SQi Liu 	/* Do nothing, not data left in hardware path */
8506f5c292SQi Liu 	if (!write_ptr || write_ptr == sdb->buf_rdptr + sdb->buf_hw_base)
8606f5c292SQi Liu 		return;
8706f5c292SQi Liu 
8806f5c292SQi Liu 	/*
8906f5c292SQi Liu 	 * The SMB_LB_WR_ADDR_REG register is read-only,
9006f5c292SQi Liu 	 * Synchronize the read pointer to write pointer.
9106f5c292SQi Liu 	 */
9206f5c292SQi Liu 	writel(write_ptr, drvdata->base + SMB_LB_RD_ADDR_REG);
9306f5c292SQi Liu 	sdb->buf_rdptr = write_ptr - sdb->buf_hw_base;
9406f5c292SQi Liu }
9506f5c292SQi Liu 
smb_open(struct inode * inode,struct file * file)9606f5c292SQi Liu static int smb_open(struct inode *inode, struct file *file)
9706f5c292SQi Liu {
9806f5c292SQi Liu 	struct smb_drv_data *drvdata = container_of(file->private_data,
9906f5c292SQi Liu 					struct smb_drv_data, miscdev);
10006f5c292SQi Liu 	int ret = 0;
10106f5c292SQi Liu 
102ace850bdSJunhao He 	spin_lock(&drvdata->spinlock);
10306f5c292SQi Liu 
10406f5c292SQi Liu 	if (drvdata->reading) {
10506f5c292SQi Liu 		ret = -EBUSY;
10606f5c292SQi Liu 		goto out;
10706f5c292SQi Liu 	}
10806f5c292SQi Liu 
109ae7f2b5aSJames Clark 	if (atomic_read(&drvdata->csdev->refcnt)) {
11006f5c292SQi Liu 		ret = -EBUSY;
11106f5c292SQi Liu 		goto out;
11206f5c292SQi Liu 	}
11306f5c292SQi Liu 
11406f5c292SQi Liu 	smb_update_data_size(drvdata);
11506f5c292SQi Liu 
11606f5c292SQi Liu 	drvdata->reading = true;
11706f5c292SQi Liu out:
118ace850bdSJunhao He 	spin_unlock(&drvdata->spinlock);
11906f5c292SQi Liu 
12006f5c292SQi Liu 	return ret;
12106f5c292SQi Liu }
12206f5c292SQi Liu 
smb_read(struct file * file,char __user * data,size_t len,loff_t * ppos)12306f5c292SQi Liu static ssize_t smb_read(struct file *file, char __user *data, size_t len,
12406f5c292SQi Liu 			loff_t *ppos)
12506f5c292SQi Liu {
12606f5c292SQi Liu 	struct smb_drv_data *drvdata = container_of(file->private_data,
12706f5c292SQi Liu 					struct smb_drv_data, miscdev);
12806f5c292SQi Liu 	struct smb_data_buffer *sdb = &drvdata->sdb;
12906f5c292SQi Liu 	struct device *dev = &drvdata->csdev->dev;
13006f5c292SQi Liu 	ssize_t to_copy = 0;
13106f5c292SQi Liu 
13206f5c292SQi Liu 	if (!len)
13306f5c292SQi Liu 		return 0;
13406f5c292SQi Liu 
13506f5c292SQi Liu 	if (!sdb->data_size)
136ace850bdSJunhao He 		return 0;
13706f5c292SQi Liu 
13806f5c292SQi Liu 	to_copy = min(sdb->data_size, len);
13906f5c292SQi Liu 
14006f5c292SQi Liu 	/* Copy parts of trace data when read pointer wrap around SMB buffer */
14106f5c292SQi Liu 	if (sdb->buf_rdptr + to_copy > sdb->buf_size)
14206f5c292SQi Liu 		to_copy = sdb->buf_size - sdb->buf_rdptr;
14306f5c292SQi Liu 
14406f5c292SQi Liu 	if (copy_to_user(data, sdb->buf_base + sdb->buf_rdptr, to_copy)) {
14506f5c292SQi Liu 		dev_dbg(dev, "Failed to copy data to user\n");
146ace850bdSJunhao He 		return -EFAULT;
14706f5c292SQi Liu 	}
14806f5c292SQi Liu 
14906f5c292SQi Liu 	*ppos += to_copy;
15006f5c292SQi Liu 	smb_update_read_ptr(drvdata, to_copy);
15106f5c292SQi Liu 	if (!sdb->data_size)
15206f5c292SQi Liu 		smb_reset_buffer(drvdata);
15306f5c292SQi Liu 
154ace850bdSJunhao He 	dev_dbg(dev, "%zu bytes copied\n", to_copy);
15506f5c292SQi Liu 	return to_copy;
15606f5c292SQi Liu }
15706f5c292SQi Liu 
smb_release(struct inode * inode,struct file * file)15806f5c292SQi Liu static int smb_release(struct inode *inode, struct file *file)
15906f5c292SQi Liu {
16006f5c292SQi Liu 	struct smb_drv_data *drvdata = container_of(file->private_data,
16106f5c292SQi Liu 					struct smb_drv_data, miscdev);
16206f5c292SQi Liu 
163ace850bdSJunhao He 	spin_lock(&drvdata->spinlock);
16406f5c292SQi Liu 	drvdata->reading = false;
165ace850bdSJunhao He 	spin_unlock(&drvdata->spinlock);
16606f5c292SQi Liu 
16706f5c292SQi Liu 	return 0;
16806f5c292SQi Liu }
16906f5c292SQi Liu 
17006f5c292SQi Liu static const struct file_operations smb_fops = {
17106f5c292SQi Liu 	.owner		= THIS_MODULE,
17206f5c292SQi Liu 	.open		= smb_open,
17306f5c292SQi Liu 	.read		= smb_read,
17406f5c292SQi Liu 	.release	= smb_release,
17506f5c292SQi Liu 	.llseek		= no_llseek,
17606f5c292SQi Liu };
17706f5c292SQi Liu 
buf_size_show(struct device * dev,struct device_attribute * attr,char * buf)17806f5c292SQi Liu static ssize_t buf_size_show(struct device *dev, struct device_attribute *attr,
17906f5c292SQi Liu 			     char *buf)
18006f5c292SQi Liu {
18106f5c292SQi Liu 	struct smb_drv_data *drvdata = dev_get_drvdata(dev->parent);
18206f5c292SQi Liu 
18306f5c292SQi Liu 	return sysfs_emit(buf, "0x%lx\n", drvdata->sdb.buf_size);
18406f5c292SQi Liu }
18506f5c292SQi Liu static DEVICE_ATTR_RO(buf_size);
18606f5c292SQi Liu 
18706f5c292SQi Liu static struct attribute *smb_sink_attrs[] = {
18806f5c292SQi Liu 	coresight_simple_reg32(read_pos, SMB_LB_RD_ADDR_REG),
18906f5c292SQi Liu 	coresight_simple_reg32(write_pos, SMB_LB_WR_ADDR_REG),
19006f5c292SQi Liu 	coresight_simple_reg32(buf_status, SMB_LB_INT_STS_REG),
19106f5c292SQi Liu 	&dev_attr_buf_size.attr,
19206f5c292SQi Liu 	NULL
19306f5c292SQi Liu };
19406f5c292SQi Liu 
19506f5c292SQi Liu static const struct attribute_group smb_sink_group = {
19606f5c292SQi Liu 	.attrs = smb_sink_attrs,
19706f5c292SQi Liu 	.name = "mgmt",
19806f5c292SQi Liu };
19906f5c292SQi Liu 
20006f5c292SQi Liu static const struct attribute_group *smb_sink_groups[] = {
20106f5c292SQi Liu 	&smb_sink_group,
20206f5c292SQi Liu 	NULL
20306f5c292SQi Liu };
20406f5c292SQi Liu 
smb_enable_hw(struct smb_drv_data * drvdata)20506f5c292SQi Liu static void smb_enable_hw(struct smb_drv_data *drvdata)
20606f5c292SQi Liu {
20706f5c292SQi Liu 	writel(SMB_GLB_EN_HW_ENABLE, drvdata->base + SMB_GLB_EN_REG);
20806f5c292SQi Liu }
20906f5c292SQi Liu 
smb_disable_hw(struct smb_drv_data * drvdata)21006f5c292SQi Liu static void smb_disable_hw(struct smb_drv_data *drvdata)
21106f5c292SQi Liu {
21206f5c292SQi Liu 	writel(0x0, drvdata->base + SMB_GLB_EN_REG);
21306f5c292SQi Liu }
21406f5c292SQi Liu 
smb_enable_sysfs(struct coresight_device * csdev)21506f5c292SQi Liu static void smb_enable_sysfs(struct coresight_device *csdev)
21606f5c292SQi Liu {
21706f5c292SQi Liu 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
21806f5c292SQi Liu 
21906f5c292SQi Liu 	if (drvdata->mode != CS_MODE_DISABLED)
22006f5c292SQi Liu 		return;
22106f5c292SQi Liu 
22206f5c292SQi Liu 	smb_enable_hw(drvdata);
22306f5c292SQi Liu 	drvdata->mode = CS_MODE_SYSFS;
22406f5c292SQi Liu }
22506f5c292SQi Liu 
smb_enable_perf(struct coresight_device * csdev,void * data)22606f5c292SQi Liu static int smb_enable_perf(struct coresight_device *csdev, void *data)
22706f5c292SQi Liu {
22806f5c292SQi Liu 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
22906f5c292SQi Liu 	struct perf_output_handle *handle = data;
23006f5c292SQi Liu 	struct cs_buffers *buf = etm_perf_sink_config(handle);
23106f5c292SQi Liu 	pid_t pid;
23206f5c292SQi Liu 
23306f5c292SQi Liu 	if (!buf)
23406f5c292SQi Liu 		return -EINVAL;
23506f5c292SQi Liu 
23606f5c292SQi Liu 	/* Get a handle on the pid of the target process */
23706f5c292SQi Liu 	pid = buf->pid;
23806f5c292SQi Liu 
23906f5c292SQi Liu 	/* Device is already in used by other session */
24006f5c292SQi Liu 	if (drvdata->pid != -1 && drvdata->pid != pid)
24106f5c292SQi Liu 		return -EBUSY;
24206f5c292SQi Liu 
24306f5c292SQi Liu 	if (drvdata->pid == -1) {
24406f5c292SQi Liu 		smb_enable_hw(drvdata);
24506f5c292SQi Liu 		drvdata->pid = pid;
24606f5c292SQi Liu 		drvdata->mode = CS_MODE_PERF;
24706f5c292SQi Liu 	}
24806f5c292SQi Liu 
24906f5c292SQi Liu 	return 0;
25006f5c292SQi Liu }
25106f5c292SQi Liu 
smb_enable(struct coresight_device * csdev,enum cs_mode mode,void * data)2529fa36828SJames Clark static int smb_enable(struct coresight_device *csdev, enum cs_mode mode,
2539fa36828SJames Clark 		      void *data)
25406f5c292SQi Liu {
25506f5c292SQi Liu 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
25606f5c292SQi Liu 	int ret = 0;
25706f5c292SQi Liu 
258ace850bdSJunhao He 	spin_lock(&drvdata->spinlock);
25906f5c292SQi Liu 
26006f5c292SQi Liu 	/* Do nothing, the trace data is reading by other interface now */
26106f5c292SQi Liu 	if (drvdata->reading) {
26206f5c292SQi Liu 		ret = -EBUSY;
26306f5c292SQi Liu 		goto out;
26406f5c292SQi Liu 	}
26506f5c292SQi Liu 
26606f5c292SQi Liu 	/* Do nothing, the SMB is already enabled as other mode */
26706f5c292SQi Liu 	if (drvdata->mode != CS_MODE_DISABLED && drvdata->mode != mode) {
26806f5c292SQi Liu 		ret = -EBUSY;
26906f5c292SQi Liu 		goto out;
27006f5c292SQi Liu 	}
27106f5c292SQi Liu 
27206f5c292SQi Liu 	switch (mode) {
27306f5c292SQi Liu 	case CS_MODE_SYSFS:
27406f5c292SQi Liu 		smb_enable_sysfs(csdev);
27506f5c292SQi Liu 		break;
27606f5c292SQi Liu 	case CS_MODE_PERF:
27706f5c292SQi Liu 		ret = smb_enable_perf(csdev, data);
27806f5c292SQi Liu 		break;
27906f5c292SQi Liu 	default:
28006f5c292SQi Liu 		ret = -EINVAL;
28106f5c292SQi Liu 	}
28206f5c292SQi Liu 
28306f5c292SQi Liu 	if (ret)
28406f5c292SQi Liu 		goto out;
28506f5c292SQi Liu 
286ae7f2b5aSJames Clark 	atomic_inc(&csdev->refcnt);
28706f5c292SQi Liu 
28806f5c292SQi Liu 	dev_dbg(&csdev->dev, "Ultrasoc SMB enabled\n");
28906f5c292SQi Liu out:
290ace850bdSJunhao He 	spin_unlock(&drvdata->spinlock);
29106f5c292SQi Liu 
29206f5c292SQi Liu 	return ret;
29306f5c292SQi Liu }
29406f5c292SQi Liu 
smb_disable(struct coresight_device * csdev)29506f5c292SQi Liu static int smb_disable(struct coresight_device *csdev)
29606f5c292SQi Liu {
29706f5c292SQi Liu 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
29806f5c292SQi Liu 	int ret = 0;
29906f5c292SQi Liu 
300ace850bdSJunhao He 	spin_lock(&drvdata->spinlock);
30106f5c292SQi Liu 
30206f5c292SQi Liu 	if (drvdata->reading) {
30306f5c292SQi Liu 		ret = -EBUSY;
30406f5c292SQi Liu 		goto out;
30506f5c292SQi Liu 	}
30606f5c292SQi Liu 
307ae7f2b5aSJames Clark 	if (atomic_dec_return(&csdev->refcnt)) {
30806f5c292SQi Liu 		ret = -EBUSY;
30906f5c292SQi Liu 		goto out;
31006f5c292SQi Liu 	}
31106f5c292SQi Liu 
31206f5c292SQi Liu 	/* Complain if we (somehow) got out of sync */
31306f5c292SQi Liu 	WARN_ON_ONCE(drvdata->mode == CS_MODE_DISABLED);
31406f5c292SQi Liu 
31506f5c292SQi Liu 	smb_disable_hw(drvdata);
31606f5c292SQi Liu 
31706f5c292SQi Liu 	/* Dissociate from the target process. */
31806f5c292SQi Liu 	drvdata->pid = -1;
31906f5c292SQi Liu 	drvdata->mode = CS_MODE_DISABLED;
32006f5c292SQi Liu 
32106f5c292SQi Liu 	dev_dbg(&csdev->dev, "Ultrasoc SMB disabled\n");
32206f5c292SQi Liu out:
323ace850bdSJunhao He 	spin_unlock(&drvdata->spinlock);
32406f5c292SQi Liu 
32506f5c292SQi Liu 	return ret;
32606f5c292SQi Liu }
32706f5c292SQi Liu 
smb_alloc_buffer(struct coresight_device * csdev,struct perf_event * event,void ** pages,int nr_pages,bool overwrite)32806f5c292SQi Liu static void *smb_alloc_buffer(struct coresight_device *csdev,
32906f5c292SQi Liu 			      struct perf_event *event, void **pages,
33006f5c292SQi Liu 			      int nr_pages, bool overwrite)
33106f5c292SQi Liu {
33206f5c292SQi Liu 	struct cs_buffers *buf;
33306f5c292SQi Liu 	int node;
33406f5c292SQi Liu 
33506f5c292SQi Liu 	node = (event->cpu == -1) ? NUMA_NO_NODE : cpu_to_node(event->cpu);
33606f5c292SQi Liu 	buf = kzalloc_node(sizeof(struct cs_buffers), GFP_KERNEL, node);
33706f5c292SQi Liu 	if (!buf)
33806f5c292SQi Liu 		return NULL;
33906f5c292SQi Liu 
34006f5c292SQi Liu 	buf->snapshot = overwrite;
34106f5c292SQi Liu 	buf->nr_pages = nr_pages;
34206f5c292SQi Liu 	buf->data_pages = pages;
34306f5c292SQi Liu 	buf->pid = task_pid_nr(event->owner);
34406f5c292SQi Liu 
34506f5c292SQi Liu 	return buf;
34606f5c292SQi Liu }
34706f5c292SQi Liu 
smb_free_buffer(void * config)34806f5c292SQi Liu static void smb_free_buffer(void *config)
34906f5c292SQi Liu {
35006f5c292SQi Liu 	struct cs_buffers *buf = config;
35106f5c292SQi Liu 
35206f5c292SQi Liu 	kfree(buf);
35306f5c292SQi Liu }
35406f5c292SQi Liu 
smb_sync_perf_buffer(struct smb_drv_data * drvdata,struct cs_buffers * buf,unsigned long head)35506f5c292SQi Liu static void smb_sync_perf_buffer(struct smb_drv_data *drvdata,
35606f5c292SQi Liu 				 struct cs_buffers *buf,
35706f5c292SQi Liu 				 unsigned long head)
35806f5c292SQi Liu {
35906f5c292SQi Liu 	struct smb_data_buffer *sdb = &drvdata->sdb;
36006f5c292SQi Liu 	char **dst_pages = (char **)buf->data_pages;
36106f5c292SQi Liu 	unsigned long to_copy;
36206f5c292SQi Liu 	long pg_idx, pg_offset;
36306f5c292SQi Liu 
36406f5c292SQi Liu 	pg_idx = head >> PAGE_SHIFT;
36506f5c292SQi Liu 	pg_offset = head & (PAGE_SIZE - 1);
36606f5c292SQi Liu 
36706f5c292SQi Liu 	while (sdb->data_size) {
36806f5c292SQi Liu 		unsigned long pg_space = PAGE_SIZE - pg_offset;
36906f5c292SQi Liu 
37006f5c292SQi Liu 		to_copy = min(sdb->data_size, pg_space);
37106f5c292SQi Liu 
37206f5c292SQi Liu 		/* Copy parts of trace data when read pointer wrap around */
37306f5c292SQi Liu 		if (sdb->buf_rdptr + to_copy > sdb->buf_size)
37406f5c292SQi Liu 			to_copy = sdb->buf_size - sdb->buf_rdptr;
37506f5c292SQi Liu 
37606f5c292SQi Liu 		memcpy(dst_pages[pg_idx] + pg_offset,
37706f5c292SQi Liu 			      sdb->buf_base + sdb->buf_rdptr, to_copy);
37806f5c292SQi Liu 
37906f5c292SQi Liu 		pg_offset += to_copy;
38006f5c292SQi Liu 		if (pg_offset >= PAGE_SIZE) {
38106f5c292SQi Liu 			pg_offset = 0;
38206f5c292SQi Liu 			pg_idx++;
38306f5c292SQi Liu 			pg_idx %= buf->nr_pages;
38406f5c292SQi Liu 		}
38506f5c292SQi Liu 		smb_update_read_ptr(drvdata, to_copy);
38606f5c292SQi Liu 	}
38706f5c292SQi Liu 
38806f5c292SQi Liu 	smb_reset_buffer(drvdata);
38906f5c292SQi Liu }
39006f5c292SQi Liu 
smb_update_buffer(struct coresight_device * csdev,struct perf_output_handle * handle,void * sink_config)39106f5c292SQi Liu static unsigned long smb_update_buffer(struct coresight_device *csdev,
39206f5c292SQi Liu 				       struct perf_output_handle *handle,
39306f5c292SQi Liu 				       void *sink_config)
39406f5c292SQi Liu {
39506f5c292SQi Liu 	struct smb_drv_data *drvdata = dev_get_drvdata(csdev->dev.parent);
39606f5c292SQi Liu 	struct smb_data_buffer *sdb = &drvdata->sdb;
39706f5c292SQi Liu 	struct cs_buffers *buf = sink_config;
39806f5c292SQi Liu 	unsigned long data_size = 0;
39906f5c292SQi Liu 	bool lost = false;
40006f5c292SQi Liu 
40106f5c292SQi Liu 	if (!buf)
40206f5c292SQi Liu 		return 0;
40306f5c292SQi Liu 
404ace850bdSJunhao He 	spin_lock(&drvdata->spinlock);
40506f5c292SQi Liu 
40606f5c292SQi Liu 	/* Don't do anything if another tracer is using this sink. */
407ae7f2b5aSJames Clark 	if (atomic_read(&csdev->refcnt) != 1)
40806f5c292SQi Liu 		goto out;
40906f5c292SQi Liu 
41006f5c292SQi Liu 	smb_disable_hw(drvdata);
41106f5c292SQi Liu 	smb_update_data_size(drvdata);
41206f5c292SQi Liu 
41306f5c292SQi Liu 	/*
41406f5c292SQi Liu 	 * The SMB buffer may be bigger than the space available in the
41506f5c292SQi Liu 	 * perf ring buffer (handle->size). If so advance the offset so
41606f5c292SQi Liu 	 * that we get the latest trace data.
41706f5c292SQi Liu 	 */
41806f5c292SQi Liu 	if (sdb->data_size > handle->size) {
41906f5c292SQi Liu 		smb_update_read_ptr(drvdata, sdb->data_size - handle->size);
42006f5c292SQi Liu 		lost = true;
42106f5c292SQi Liu 	}
42206f5c292SQi Liu 
42306f5c292SQi Liu 	data_size = sdb->data_size;
42406f5c292SQi Liu 	smb_sync_perf_buffer(drvdata, buf, handle->head);
42506f5c292SQi Liu 	if (!buf->snapshot && lost)
42606f5c292SQi Liu 		perf_aux_output_flag(handle, PERF_AUX_FLAG_TRUNCATED);
42706f5c292SQi Liu out:
428ace850bdSJunhao He 	spin_unlock(&drvdata->spinlock);
42906f5c292SQi Liu 
43006f5c292SQi Liu 	return data_size;
43106f5c292SQi Liu }
43206f5c292SQi Liu 
43306f5c292SQi Liu static const struct coresight_ops_sink smb_cs_ops = {
43406f5c292SQi Liu 	.enable		= smb_enable,
43506f5c292SQi Liu 	.disable	= smb_disable,
43606f5c292SQi Liu 	.alloc_buffer	= smb_alloc_buffer,
43706f5c292SQi Liu 	.free_buffer	= smb_free_buffer,
43806f5c292SQi Liu 	.update_buffer	= smb_update_buffer,
43906f5c292SQi Liu };
44006f5c292SQi Liu 
44106f5c292SQi Liu static const struct coresight_ops cs_ops = {
44206f5c292SQi Liu 	.sink_ops	= &smb_cs_ops,
44306f5c292SQi Liu };
44406f5c292SQi Liu 
smb_init_data_buffer(struct platform_device * pdev,struct smb_data_buffer * sdb)44506f5c292SQi Liu static int smb_init_data_buffer(struct platform_device *pdev,
44606f5c292SQi Liu 				struct smb_data_buffer *sdb)
44706f5c292SQi Liu {
44806f5c292SQi Liu 	struct resource *res;
44906f5c292SQi Liu 	void *base;
45006f5c292SQi Liu 
45106f5c292SQi Liu 	res = platform_get_resource(pdev, IORESOURCE_MEM, SMB_BUF_ADDR_RES);
452075c7c04SYang Yingliang 	if (!res) {
45306f5c292SQi Liu 		dev_err(&pdev->dev, "SMB device failed to get resource\n");
45406f5c292SQi Liu 		return -EINVAL;
45506f5c292SQi Liu 	}
45606f5c292SQi Liu 
45706f5c292SQi Liu 	sdb->buf_rdptr = 0;
45806f5c292SQi Liu 	sdb->buf_hw_base = FIELD_GET(SMB_BUF_ADDR_LO_MSK, res->start);
45906f5c292SQi Liu 	sdb->buf_size = resource_size(res);
46006f5c292SQi Liu 	if (sdb->buf_size == 0)
46106f5c292SQi Liu 		return -EINVAL;
46206f5c292SQi Liu 
46306f5c292SQi Liu 	/*
46406f5c292SQi Liu 	 * This is a chunk of memory, use classic mapping with better
46506f5c292SQi Liu 	 * performance.
46606f5c292SQi Liu 	 */
46706f5c292SQi Liu 	base = devm_memremap(&pdev->dev, sdb->buf_hw_base, sdb->buf_size,
46806f5c292SQi Liu 				MEMREMAP_WB);
46906f5c292SQi Liu 	if (IS_ERR(base))
47006f5c292SQi Liu 		return PTR_ERR(base);
47106f5c292SQi Liu 
47206f5c292SQi Liu 	sdb->buf_base = base;
47306f5c292SQi Liu 
47406f5c292SQi Liu 	return 0;
47506f5c292SQi Liu }
47606f5c292SQi Liu 
smb_init_hw(struct smb_drv_data * drvdata)47706f5c292SQi Liu static void smb_init_hw(struct smb_drv_data *drvdata)
47806f5c292SQi Liu {
47906f5c292SQi Liu 	smb_disable_hw(drvdata);
48006f5c292SQi Liu 
48106f5c292SQi Liu 	writel(SMB_LB_CFG_LO_DEFAULT, drvdata->base + SMB_LB_CFG_LO_REG);
48206f5c292SQi Liu 	writel(SMB_LB_CFG_HI_DEFAULT, drvdata->base + SMB_LB_CFG_HI_REG);
48306f5c292SQi Liu 	writel(SMB_GLB_CFG_DEFAULT, drvdata->base + SMB_GLB_CFG_REG);
48406f5c292SQi Liu 	writel(SMB_GLB_INT_CFG, drvdata->base + SMB_GLB_INT_REG);
48506f5c292SQi Liu 	writel(SMB_LB_INT_CTRL_CFG, drvdata->base + SMB_LB_INT_CTRL_REG);
48606f5c292SQi Liu }
48706f5c292SQi Liu 
smb_register_sink(struct platform_device * pdev,struct smb_drv_data * drvdata)48806f5c292SQi Liu static int smb_register_sink(struct platform_device *pdev,
48906f5c292SQi Liu 			     struct smb_drv_data *drvdata)
49006f5c292SQi Liu {
49106f5c292SQi Liu 	struct coresight_platform_data *pdata = NULL;
49206f5c292SQi Liu 	struct coresight_desc desc = { 0 };
49306f5c292SQi Liu 	int ret;
49406f5c292SQi Liu 
49506f5c292SQi Liu 	pdata = coresight_get_platform_data(&pdev->dev);
49606f5c292SQi Liu 	if (IS_ERR(pdata))
49706f5c292SQi Liu 		return PTR_ERR(pdata);
49806f5c292SQi Liu 
49906f5c292SQi Liu 	desc.type = CORESIGHT_DEV_TYPE_SINK;
50006f5c292SQi Liu 	desc.subtype.sink_subtype = CORESIGHT_DEV_SUBTYPE_SINK_BUFFER;
50106f5c292SQi Liu 	desc.ops = &cs_ops;
50206f5c292SQi Liu 	desc.pdata = pdata;
50306f5c292SQi Liu 	desc.dev = &pdev->dev;
50406f5c292SQi Liu 	desc.groups = smb_sink_groups;
50506f5c292SQi Liu 	desc.name = coresight_alloc_device_name(&sink_devs, &pdev->dev);
50606f5c292SQi Liu 	if (!desc.name) {
50706f5c292SQi Liu 		dev_err(&pdev->dev, "Failed to alloc coresight device name");
50806f5c292SQi Liu 		return -ENOMEM;
50906f5c292SQi Liu 	}
51006f5c292SQi Liu 	desc.access = CSDEV_ACCESS_IOMEM(drvdata->base);
51106f5c292SQi Liu 
51206f5c292SQi Liu 	drvdata->csdev = coresight_register(&desc);
51306f5c292SQi Liu 	if (IS_ERR(drvdata->csdev))
51406f5c292SQi Liu 		return PTR_ERR(drvdata->csdev);
51506f5c292SQi Liu 
51606f5c292SQi Liu 	drvdata->miscdev.name = desc.name;
51706f5c292SQi Liu 	drvdata->miscdev.minor = MISC_DYNAMIC_MINOR;
51806f5c292SQi Liu 	drvdata->miscdev.fops = &smb_fops;
51906f5c292SQi Liu 	ret = misc_register(&drvdata->miscdev);
52006f5c292SQi Liu 	if (ret) {
52106f5c292SQi Liu 		coresight_unregister(drvdata->csdev);
52206f5c292SQi Liu 		dev_err(&pdev->dev, "Failed to register misc, ret=%d\n", ret);
52306f5c292SQi Liu 	}
52406f5c292SQi Liu 
52506f5c292SQi Liu 	return ret;
52606f5c292SQi Liu }
52706f5c292SQi Liu 
smb_unregister_sink(struct smb_drv_data * drvdata)52806f5c292SQi Liu static void smb_unregister_sink(struct smb_drv_data *drvdata)
52906f5c292SQi Liu {
53006f5c292SQi Liu 	misc_deregister(&drvdata->miscdev);
53106f5c292SQi Liu 	coresight_unregister(drvdata->csdev);
53206f5c292SQi Liu }
53306f5c292SQi Liu 
smb_config_inport(struct device * dev,bool enable)53406f5c292SQi Liu static int smb_config_inport(struct device *dev, bool enable)
53506f5c292SQi Liu {
53606f5c292SQi Liu 	u64 func = enable ? 1 : 0;
53706f5c292SQi Liu 	union acpi_object *obj;
53806f5c292SQi Liu 	guid_t guid;
53906f5c292SQi Liu 	u64 rev = 0;
54006f5c292SQi Liu 
54106f5c292SQi Liu 	/*
54206f5c292SQi Liu 	 * Using DSM calls to enable/disable ultrasoc hardwares on
54306f5c292SQi Liu 	 * tracing path, to prevent ultrasoc packet format being exposed.
54406f5c292SQi Liu 	 */
54506f5c292SQi Liu 	if (guid_parse(ULTRASOC_SMB_DSM_UUID, &guid)) {
54606f5c292SQi Liu 		dev_err(dev, "Get GUID failed\n");
54706f5c292SQi Liu 		return -EINVAL;
54806f5c292SQi Liu 	}
54906f5c292SQi Liu 
55006f5c292SQi Liu 	obj = acpi_evaluate_dsm(ACPI_HANDLE(dev), &guid, rev, func, NULL);
55106f5c292SQi Liu 	if (!obj) {
55206f5c292SQi Liu 		dev_err(dev, "ACPI handle failed\n");
55306f5c292SQi Liu 		return -ENODEV;
55406f5c292SQi Liu 	}
55506f5c292SQi Liu 
55606f5c292SQi Liu 	ACPI_FREE(obj);
55706f5c292SQi Liu 
55806f5c292SQi Liu 	return 0;
55906f5c292SQi Liu }
56006f5c292SQi Liu 
smb_probe(struct platform_device * pdev)56106f5c292SQi Liu static int smb_probe(struct platform_device *pdev)
56206f5c292SQi Liu {
56306f5c292SQi Liu 	struct device *dev = &pdev->dev;
56406f5c292SQi Liu 	struct smb_drv_data *drvdata;
56506f5c292SQi Liu 	int ret;
56606f5c292SQi Liu 
56706f5c292SQi Liu 	drvdata = devm_kzalloc(dev, sizeof(*drvdata), GFP_KERNEL);
56806f5c292SQi Liu 	if (!drvdata)
56906f5c292SQi Liu 		return -ENOMEM;
57006f5c292SQi Liu 
57106f5c292SQi Liu 	drvdata->base = devm_platform_ioremap_resource(pdev, SMB_REG_ADDR_RES);
57206f5c292SQi Liu 	if (IS_ERR(drvdata->base)) {
57306f5c292SQi Liu 		dev_err(dev, "Failed to ioremap resource\n");
57406f5c292SQi Liu 		return PTR_ERR(drvdata->base);
57506f5c292SQi Liu 	}
57606f5c292SQi Liu 
57706f5c292SQi Liu 	smb_init_hw(drvdata);
57806f5c292SQi Liu 
57906f5c292SQi Liu 	ret = smb_init_data_buffer(pdev, &drvdata->sdb);
58006f5c292SQi Liu 	if (ret) {
58106f5c292SQi Liu 		dev_err(dev, "Failed to init buffer, ret = %d\n", ret);
58206f5c292SQi Liu 		return ret;
58306f5c292SQi Liu 	}
58406f5c292SQi Liu 
585ab5091e1SJunhao He 	ret = smb_config_inport(dev, true);
586ab5091e1SJunhao He 	if (ret)
587ab5091e1SJunhao He 		return ret;
588ab5091e1SJunhao He 
589*c541d0edSJunhao He 	smb_reset_buffer(drvdata);
590ab5091e1SJunhao He 	platform_set_drvdata(pdev, drvdata);
591ace850bdSJunhao He 	spin_lock_init(&drvdata->spinlock);
59206f5c292SQi Liu 	drvdata->pid = -1;
59306f5c292SQi Liu 
59406f5c292SQi Liu 	ret = smb_register_sink(pdev, drvdata);
59506f5c292SQi Liu 	if (ret) {
596ab5091e1SJunhao He 		smb_config_inport(&pdev->dev, false);
59706f5c292SQi Liu 		dev_err(dev, "Failed to register SMB sink\n");
59806f5c292SQi Liu 		return ret;
59906f5c292SQi Liu 	}
60006f5c292SQi Liu 
60106f5c292SQi Liu 	return 0;
60206f5c292SQi Liu }
60306f5c292SQi Liu 
smb_remove(struct platform_device * pdev)60406f5c292SQi Liu static int smb_remove(struct platform_device *pdev)
60506f5c292SQi Liu {
60606f5c292SQi Liu 	struct smb_drv_data *drvdata = platform_get_drvdata(pdev);
60706f5c292SQi Liu 
60806f5c292SQi Liu 	smb_unregister_sink(drvdata);
60906f5c292SQi Liu 
610ab5091e1SJunhao He 	smb_config_inport(&pdev->dev, false);
611ab5091e1SJunhao He 
61206f5c292SQi Liu 	return 0;
61306f5c292SQi Liu }
61406f5c292SQi Liu 
61506f5c292SQi Liu #ifdef CONFIG_ACPI
61606f5c292SQi Liu static const struct acpi_device_id ultrasoc_smb_acpi_match[] = {
61706f5c292SQi Liu 	{"HISI03A1", 0},
61806f5c292SQi Liu 	{}
61906f5c292SQi Liu };
62006f5c292SQi Liu MODULE_DEVICE_TABLE(acpi, ultrasoc_smb_acpi_match);
62106f5c292SQi Liu #endif
62206f5c292SQi Liu 
62306f5c292SQi Liu static struct platform_driver smb_driver = {
62406f5c292SQi Liu 	.driver = {
62506f5c292SQi Liu 		.name = "ultrasoc-smb",
62606f5c292SQi Liu 		.acpi_match_table = ACPI_PTR(ultrasoc_smb_acpi_match),
62706f5c292SQi Liu 		.suppress_bind_attrs = true,
62806f5c292SQi Liu 	},
62906f5c292SQi Liu 	.probe = smb_probe,
63006f5c292SQi Liu 	.remove = smb_remove,
63106f5c292SQi Liu };
63206f5c292SQi Liu module_platform_driver(smb_driver);
63306f5c292SQi Liu 
63406f5c292SQi Liu MODULE_DESCRIPTION("UltraSoc SMB CoreSight driver");
63506f5c292SQi Liu MODULE_LICENSE("Dual MIT/GPL");
63606f5c292SQi Liu MODULE_AUTHOR("Jonathan Zhou <jonathan.zhouwen@huawei.com>");
63706f5c292SQi Liu MODULE_AUTHOR("Qi Liu <liuqi115@huawei.com>");
638