diff mbox series

[RFC,15/16] soundwire: debugfs: add interface for BPT/BRA transfers

Message ID 20231207222944.663893-16-pierre-louis.bossart@linux.intel.com
State New
Headers show
Series soundwire/ASoC: speed-up downloads with BTP/BRA protocol | expand

Commit Message

Pierre-Louis Bossart Dec. 7, 2023, 10:29 p.m. UTC
Add code to show what codec drivers will need to do to enable BPT/BRA
transfers. The only difference is to set the 'command_type' file to
1. A zero-value will rely on regular read/write commands in Column0.

For now the code will only return error codes since the Manager side
is not enabled yet.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
---
 drivers/soundwire/debugfs.c | 122 +++++++++++++++++++++++++++++++-----
 1 file changed, 106 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/drivers/soundwire/debugfs.c b/drivers/soundwire/debugfs.c
index 6d253d69871d..de35f5beec4d 100644
--- a/drivers/soundwire/debugfs.c
+++ b/drivers/soundwire/debugfs.c
@@ -138,9 +138,10 @@  static int sdw_slave_reg_show(struct seq_file *s_file, void *data)
 }
 DEFINE_SHOW_ATTRIBUTE(sdw_slave_reg);
 
-#define MAX_CMD_BYTES 256
+#define MAX_CMD_BYTES (1024 * 1024)
 
 static int cmd;
+static int cmd_type;
 static u32 start_addr;
 static size_t num_bytes;
 static u8 read_buffer[MAX_CMD_BYTES];
@@ -164,6 +165,25 @@  static int set_command(void *data, u64 value)
 DEFINE_DEBUGFS_ATTRIBUTE(set_command_fops, NULL,
 			 set_command, "%llu\n");
 
+static int set_command_type(void *data, u64 value)
+{
+	struct sdw_slave *slave = data;
+
+	if (value > 1)
+		return -EINVAL;
+
+	/* Userspace changed the hardware state behind the kernel's back */
+	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
+
+	dev_dbg(&slave->dev, "command type: %s\n", value ? "BRA" : "Column0");
+
+	cmd_type = value;
+
+	return 0;
+}
+DEFINE_DEBUGFS_ATTRIBUTE(set_command_type_fops, NULL,
+			 set_command_type, "%llu\n");
+
 static int set_start_address(void *data, u64 value)
 {
 	struct sdw_slave *slave = data;
@@ -199,9 +219,64 @@  static int set_num_bytes(void *data, u64 value)
 DEFINE_DEBUGFS_ATTRIBUTE(set_num_bytes_fops, NULL,
 			 set_num_bytes, "%llu\n");
 
+static int do_bpt_sequence(struct sdw_slave *slave, bool write,
+			   u8 *buffer)
+{
+	struct sdw_bpt_msg msg = {0};
+	int ret1;
+	int ret;
+
+	msg.addr = start_addr;
+	msg.len = num_bytes;
+	msg.dev_num = slave->dev_num;
+	if (write)
+		msg.flags = SDW_MSG_FLAG_WRITE;
+	else
+		msg.flags = SDW_MSG_FLAG_READ;
+	msg.buf = buffer;
+
+	ret = sdw_bpt_open_stream(slave->bus, slave, SDW_BRA, &msg);
+	if (ret < 0) {
+		dev_err(&slave->dev, "bpt_open_stream failed: %d\n", ret);
+		return ret;
+	}
+
+	ret = sdw_bpt_send_async(slave->bus,
+				 slave,
+				 &msg);
+	if (ret < 0) {
+		dev_err(&slave->dev, "bpt_send_async failed: %d\n", ret);
+		goto close;
+	}
+
+	dev_dbg(&slave->dev, "send_async done\n");
+
+	ret = sdw_bpt_wait(slave->bus,
+			   slave,
+			   &msg);
+	if (ret < 0)
+		dev_err(&slave->dev, "%s: bpt_wait failed: %d\n", __func__, ret);
+
+	dev_dbg(&slave->dev, "wait done\n");
+
+close:
+	ret1 = sdw_bpt_close_stream(slave->bus, slave, SDW_BRA, &msg);
+	if (ret1 < 0) {
+		dev_err(&slave->dev, "%s: bpt_close_stream failed: %d\n",
+			__func__, ret1);
+		if (!ret)
+			ret = ret1;
+	}
+
+	return ret;
+}
+
 static int cmd_go(void *data, u64 value)
 {
+	const struct firmware *fw = NULL;
 	struct sdw_slave *slave = data;
+	ktime_t start_t;
+	ktime_t finish_t;
 	int ret;
 
 	if (value != 1)
@@ -218,40 +293,54 @@  static int cmd_go(void *data, u64 value)
 		return ret;
 	}
 
-	/* Userspace changed the hardware state behind the kernel's back */
-	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
-
-	dev_dbg(&slave->dev, "starting command\n");
-
 	if (cmd == 0) {
-		const struct firmware *fw;
-
 		ret = request_firmware(&fw, firmware_file, &slave->dev);
 		if (ret < 0) {
 			dev_err(&slave->dev, "firmware %s not found\n", firmware_file);
 			goto out;
 		}
-
-		if (fw->size != num_bytes) {
+		if (fw->size < num_bytes) {
 			dev_err(&slave->dev,
-				"firmware %s: unexpected size %zd, desired %zd\n",
+				"firmware %s: firmware size %zd, desired %zd\n",
 				firmware_file, fw->size, num_bytes);
-			release_firmware(fw);
 			goto out;
 		}
+	}
 
-		ret = sdw_nwrite_no_pm(slave, start_addr, num_bytes, fw->data);
-		release_firmware(fw);
+	/* Userspace changed the hardware state behind the kernel's back */
+	add_taint(TAINT_USER, LOCKDEP_STILL_OK);
+
+	dev_dbg(&slave->dev, "starting command\n");
+	start_t = ktime_get();
+
+	if (cmd == 0) {
+		if (cmd_type) {
+			ret = do_bpt_sequence(slave, true, (u8 *)fw->data);
+		} else {
+			ret = sdw_nwrite_no_pm(slave, start_addr, num_bytes, fw->data);
+		}
 	} else {
-		ret = sdw_nread_no_pm(slave, start_addr, num_bytes, read_buffer);
+		memset(read_buffer, 0, sizeof(read_buffer));
+
+		if (cmd_type) {
+			ret = do_bpt_sequence(slave, false, read_buffer);
+		} else {
+			ret = sdw_nread_no_pm(slave, start_addr, num_bytes, read_buffer);
+		}
 	}
 
-	dev_dbg(&slave->dev, "command completed %d\n", ret);
+	finish_t = ktime_get();
 
 out:
+	if (fw)
+		release_firmware(fw);
+
 	pm_runtime_mark_last_busy(&slave->dev);
 	pm_runtime_put(&slave->dev);
 
+	dev_dbg(&slave->dev, "command completed, status %d, time %lld ms\n",
+		ret, div_u64(finish_t - start_t, NSEC_PER_MSEC));
+
 	return ret;
 }
 DEFINE_DEBUGFS_ATTRIBUTE(cmd_go_fops, NULL,
@@ -293,6 +382,7 @@  void sdw_slave_debugfs_init(struct sdw_slave *slave)
 
 	/* interface to send arbitrary commands */
 	debugfs_create_file("command", 0200, d, slave, &set_command_fops);
+	debugfs_create_file("command_type", 0200, d, slave, &set_command_type_fops);
 	debugfs_create_file("start_address", 0200, d, slave, &set_start_address_fops);
 	debugfs_create_file("num_bytes", 0200, d, slave, &set_num_bytes_fops);
 	debugfs_create_file("go", 0200, d, slave, &cmd_go_fops);