Skip to contentSkip to conversation

TAS2563 DSP Firmware Loader

RFC PATCH In review Dan Murphy 3 participants 20 messages Jun 9, 2020 – Jun 18, 2020

Subsystems (4)
lore
Share & tools
lore

Dan Murphy

author
#0 lore raw

cover TAS2563 DSP Firmware Loader

Hello

The TAS2563 amplifier has a DSP that can run programs and configurations to produce alternate audio experiences. The DSP firmware is not a typical firmware as the binary may contain various programs and configurations that are selectable during run time.

These programs and configurations are selectable via files under the I2C dev node. There may be a better way to select this through ALSA controls but I was unable to find a good example of this. This is why this is an RFC patchset.

Dan

Dan Murphy (2):
dt-bindings: tas2562: Add firmware support for tas2563
ASoc: tas2563: DSP Firmware loading support

 .../devicetree/bindings/sound/tas2562.yaml    |   4 +
 sound/soc/codecs/Makefile                     |   2 +-
 sound/soc/codecs/tas2562.c                    |  48 ++-
 sound/soc/codecs/tas2562.h                    |  25 ++
 sound/soc/codecs/tas25xx_dsp_loader.c         | 377 ++++++++++++++++++
 sound/soc/codecs/tas25xx_dsp_loader.h         |  93 +++++
 6 files changed, 530 insertions(+), 19 deletions(-)
 create mode 100644 sound/soc/codecs/tas25xx_dsp_loader.c
 create mode 100644 sound/soc/codecs/tas25xx_dsp_loader.h
Signature · 1 line
2.26.2
author
#1 lore raw

RFC PATCH 1/2 dt-bindings: tas2562: Add firmware support for tas2563

Add a property called firmware-name that will be the name of the firmware that will reside in the file system or built into the kernel.

---
 Documentation/devicetree/bindings/sound/tas2562.yaml | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/devicetree/bindings/sound/tas2562.yaml +4 -0
index c6168aa32954..874f91f32d7f 100644
@@ -40,6 +40,10 @@ properties:
   '#sound-dai-cells':
     const: 1
 
+  firmware-name:
+    $ref: /schemas/types.yaml#/definitions/string
+    description: Name of the firmware to be loaded to the DSP. TAS2563 only.
+
 required:
   - compatible
   - reg
Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Quoted 2 lines · Dan Murphy

Add a property called firmware-name that will be the name of the firmware that will reside in the file system or built into the kernel.

Why not just use a standard name for the firmware? If the firmwares vary per-board then building it using the machine compatible (or DMI info) could handle that, with a fallback to a standard name for a default setup.

author
#3 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Mark

Quoted 7 lines · Mark Brown
Quoted 2 lines · Dan Murphy

Add a property called firmware-name that will be the name of the firmware that will reside in the file system or built into the kernel.

Why not just use a standard name for the firmware? If the firmwares vary per-board then building it using the machine compatible (or DMI info) could handle that, with a fallback to a standard name for a default setup.

The number of firmwares can vary per IC on the board itself.  So you may have X number of firmware files all with different names all targets for different TAS2563 ICs.

Also TI will not be providing the individual binaries to the customer.  There is a customer tool that the user uses to create the binaries.

So the output names are arbitrary.

I was going to mention this in the cover letter but did not think mentioning the user tool had any value

Dan

maintainer
#4 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Quoted 11 lines · Dan Murphy
Quoted 4 lines · Mark Brown

Why not just use a standard name for the firmware? If the firmwares vary per-board then building it using the machine compatible (or DMI info) could handle that, with a fallback to a standard name for a default setup.

The number of firmwares can vary per IC on the board itself.  So you may have X number of firmware files all with different names all targets for different TAS2563 ICs.

Quoted 7 lines

Also TI will not be providing the individual binaries to the customer. There is a customer tool that the user uses to create the binaries.

So the output names are arbitrary.

I was going to mention this in the cover letter but did not think mentioning the user tool had any value

That's all fairly standard for this sort of device. You could still cope with this by including the I2C address in the default name requested - do something like tas2562/myboard-addr.fw or whatever. The concern here is that someone shouldn't have to replace their DT if they decide they want to start using the DSP, and someone making a distro shouldn't be stuck dealing with what happens if multiple vendors decide to just reuse the same name (eg, just calling everything tas2562 regardless of plastics).

author
#5 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Mark

Quoted 14 lines · Mark Brown
Quoted 5 lines · Dan Murphy
Quoted 4 lines · Mark Brown

Why not just use a standard name for the firmware? If the firmwares vary per-board then building it using the machine compatible (or DMI info) could handle that, with a fallback to a standard name for a default setup.

Quoted 8 lines

The number of firmwares can vary per IC on the board itself.  So you may have X number of firmware files all with different names all targets for different TAS2563 ICs.
Also TI will not be providing the individual binaries to the customer. There is a customer tool that the user uses to create the binaries. So the output names are arbitrary.
I was going to mention this in the cover letter but did not think mentioning the user tool had any value

Quoted 8 lines

That's all fairly standard for this sort of device. You could still cope with this by including the I2C address in the default name requested - do something like tas2562/myboard-addr.fw or whatever. The concern here is that someone shouldn't have to replace their DT if they decide they want to start using the DSP, and someone making a distro shouldn't be stuck dealing with what happens if multiple vendors decide to just reuse the same name (eg, just calling everything tas2562 regardless of plastics).

I could make a default as you suggested to include i2c address and bus in the name.  But the TAS2563 does not need the firmware to operate and the 2562 does not have a DSP.

What if there was an ALSA control instead that passed in the firmware name from the user space instead of using the DT?

Then the control can load and parse the firmware and wait for the user to select the program.

This would solve a user from having ot update the DT to use a firmware.

Dan

maintainer
#6 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Quoted 3 lines · Dan Murphy

I could make a default as you suggested to include i2c address and bus in the name.  But the TAS2563 does not need the firmware to operate and the 2562 does not have a DSP.

That's fine, the driver can just use the compatible string to check this and not offer any of the DSP related stuff (it should do this regardless of the method used here). I'm guessing the regmap configs should also be different.

Quoted 7 lines

What if there was an ALSA control instead that passed in the firmware name from the user space instead of using the DT?

Then the control can load and parse the firmware and wait for the user to select the program.

This would solve a user from having ot update the DT to use a firmware.

That's really not very idiomatic for how Linux does stuff and seems to pretty much guarantee issues with hotplugging controls and ordering - you'd need special userspace to start up even if it was just a really simple DSP config doing only speaker correction or something. I'm not sure what the advantage would be - what problem is this solving over static names?

author
#7 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Mark

Quoted 9 lines · Mark Brown
Quoted 3 lines · Dan Murphy

I could make a default as you suggested to include i2c address and bus in the name.  But the TAS2563 does not need the firmware to operate and the 2562 does not have a DSP.

That's fine, the driver can just use the compatible string to check this and not offer any of the DSP related stuff (it should do this regardless of the method used here). I'm guessing the regmap configs should also be different.

The driver does check the compatible to determine if DSP loading is available for the device.

The driver also checks to see if the firmware file is declared in the DT.

So it has to pass 2 checks to even load and parse the firmware to present the controls for the programs and configs.

Quoted 11 lines
Quoted 5 lines

What if there was an ALSA control instead that passed in the firmware name from the user space instead of using the DT?
Then the control can load and parse the firmware and wait for the user to select the program.
This would solve a user from having ot update the DT to use a firmware.

That's really not very idiomatic for how Linux does stuff and seems to pretty much guarantee issues with hotplugging controls and ordering - you'd need special userspace to start up even if it was just a really simple DSP config doing only speaker correction or something. I'm not sure what the advantage would be - what problem is this solving over static names?

IMO having a static name is the problem. It is an inflexible design.  Besides the firmware-name property seems to be used in other drivers to declare firmwares for the boards.

But if no one is complaining or submitting patches within the codecs to be more flexible with firmware then I can just hard code the name like other drivers do.

Dan

maintainer
#8 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Quoted 9 lines · Dan Murphy
Quoted 6 lines · Mark Brown

That's really not very idiomatic for how Linux does stuff and seems to pretty much guarantee issues with hotplugging controls and ordering - you'd need special userspace to start up even if it was just a really simple DSP config doing only speaker correction or something. I'm not sure what the advantage would be - what problem is this solving over static names?

Quoted 7 lines

IMO having a static name is the problem. It is an inflexible design. Besides the firmware-name property seems to be used in other drivers to declare firmwares for the boards.

But if no one is complaining or submitting patches within the codecs to be more flexible with firmware then I can just hard code the name like other drivers do.

I'm not *completely* opposed to having the ability to suggest a name in firmware, the big problem is making use of the DSP completely dependent on having a DT property or doing some non-standard dance in userspace.

author
#9 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Mark

Quoted 14 lines · Mark Brown
Quoted 7 lines · Dan Murphy
Quoted 6 lines · Mark Brown

That's really not very idiomatic for how Linux does stuff and seems to pretty much guarantee issues with hotplugging controls and ordering - you'd need special userspace to start up even if it was just a really simple DSP config doing only speaker correction or something. I'm not sure what the advantage would be - what problem is this solving over static names?

Quoted 6 lines

IMO having a static name is the problem. It is an inflexible design. Besides the firmware-name property seems to be used in other drivers to declare firmwares for the boards.
But if no one is complaining or submitting patches within the codecs to be more flexible with firmware then I can just hard code the name like other drivers do.

Quoted 3 lines

I'm not *completely* opposed to having the ability to suggest a name in firmware, the big problem is making use of the DSP completely dependent on having a DT property or doing some non-standard dance in userspace.

Well from what I see we have 4 options.

1.  We can have a DT node like RFC'd (Need Rob's comments here)

2.  We can have a defconfig flag that hard codes the name (This will probably be met with some resistance if not some really bad reactions and I don't prefer to do it this way)

3.  We can hard code the name of the firmware in the c file.

4.  Dynamically derive a file name based on the I2C bus-address-device so it would be expected to be "2_4c_tas2563.bin".  Just need to figure out how to get the bus number.

I don't see the user space as a viable option because the codec will have to load and then the user space would have to request to load the firmware and then more mixer controls will appear.

Again only option 1 allows us to have different firmware binaries per IC instance and also denotes the use of the DSP.  The DSP is not programmed until the user space selects the program or configuration from the binary.  So special audio handling is very explicit in the user space.  More then likely most standard distributions will not even use the DSP for this device it is more of a specialized use case for each product.

maintainer
#10 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Quoted 7 lines · Dan Murphy
Quoted 3 lines · Mark Brown

I'm not *completely* opposed to having the ability to suggest a name in firmware, the big problem is making use of the DSP completely dependent on having a DT property or doing some non-standard dance in userspace.

Well from what I see we have 4 options.

These are not mutually exclusive approaches.

Quoted 1 line

1.  We can have a DT node like RFC'd (Need Rob's comments here)

This is compatible with any hardcoding option.

Quoted 3 lines

2.  We can have a defconfig flag that hard codes the name (This will probably be met with some resistance if not some really bad reactions and I don't prefer to do it this way)

This is even worse than the ALSA control suggestion.

Quoted 8 lines

3.  We can hard code the name of the firmware in the c file.

4.  Dynamically derive a file name based on the I2C bus-address-device so it would be expected to be "2_4c_tas2563.bin".  Just need to figure out how to get the bus number.

Again only option 1 allows us to have different firmware binaries per IC instance and also denotes the use of the DSP.  The DSP is not programmed

No, this is not the case at all - a per-device generated file allows this just as well.

Quoted 3 lines

So special audio handling is very explicit in the user space.  More then likely most standard distributions will not even use the DSP for this device it is more of a specialized use case for each product.

People do things like make AOSP derived distributions for phones.

maintainer
#11 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

In reply to Dan Murphy
Quoted 17 lines · Dan Murphy

Mark

Quoted 14 lines · Mark Brown
Quoted 13 lines · Dan Murphy
Quoted 6 lines · Mark Brown

That's really not very idiomatic for how Linux does stuff and seems to pretty much guarantee issues with hotplugging controls and ordering - you'd need special userspace to start up even if it was just a really simple DSP config doing only speaker correction or something. I'm not sure what the advantage would be - what problem is this solving over static names?

IMO having a static name is the problem. It is an inflexible design. Besides the firmware-name property seems to be used in other drivers to declare firmwares for the boards.
But if no one is complaining or submitting patches within the codecs to be more flexible with firmware then I can just hard code the name like other drivers do.

Quoted 7 lines
Quoted 3 lines

I'm not *completely* opposed to having the ability to suggest a name in firmware, the big problem is making use of the DSP completely dependent on having a DT property or doing some non-standard dance in userspace.

Well from what I see we have 4 options.

1.  We can have a DT node like RFC'd (Need Rob's comments here)

We've obviously already allowed 'firmware-name', but the preference is still not putting into DT. It's really a Linux userspace interface.

Quoted 9 lines

2.  We can have a defconfig flag that hard codes the name (This will probably be met with some resistance if not some really bad reactions and I don't prefer to do it this way)

3.  We can hard code the name of the firmware in the c file.

4.  Dynamically derive a file name based on the I2C bus-address-device so it would be expected to be "2_4c_tas2563.bin".  Just need to figure out how to get the bus number.

Given bus numbering may not be constant, that seems like not the best way to match up devices. I'd assume that userspace needs some way to identify which instance is which already, so maybe there's other data you can use already.

Quoted 13 lines

I don't see the user space as a viable option because the codec will have to load and then the user space would have to request to load the firmware and then more mixer controls will appear.

Again only option 1 allows us to have different firmware binaries per IC instance and also denotes the use of the DSP.  The DSP is not programmed until the user space selects the program or configuration from the binary. So special audio handling is very explicit in the user space.  More then likely most standard distributions will not even use the DSP for this device it is more of a specialized use case for each product.

maintainer
#12 lore raw

Re: [RFC PATCH 1/2] dt-bindings: tas2562: Add firmware support for tas2563

Quoted 4 lines · Rob Herring

Given bus numbering may not be constant, that seems like not the best way to match up devices. I'd assume that userspace needs some way to identify which instance is which already, so maybe there's other data you can use already.

There isn't really, you're putting stuff in the DT for that - usually as part of the card binding. I guess we could use that string rather than the dev_name().

author
#13 lore raw

RFC PATCH 2/2 ASoc: tas2563: DSP Firmware loading support

In reply to Dan Murphy

The TAS2563 device has a DSP that can run firmware.
The firmware can contain up to 5 programs and 10 configurations to support alternate audio processing.

Firmware loading is done dymacally and the program and configuration selection is done by the user.

The binary itself contains a list of instructions for either a single mode write or a burst write. The single mode write is list of register writes to different books and pages within the register map. The burst writes is a block of data that is written to a specific location in memory.

The firmware loader must parse and load the blocks in real time as the binary may contain different audio profiles.

If the DSP is not needed to do audio preocessing then the DSP program can be turned off and the device will effectively turn off the DSP.

---
 sound/soc/codecs/Makefile             |   2 +-
 sound/soc/codecs/tas2562.c            |  48 ++--
 sound/soc/codecs/tas2562.h            |  25 ++
 sound/soc/codecs/tas25xx_dsp_loader.c | 377 ++++++++++++++++++++++++++
 sound/soc/codecs/tas25xx_dsp_loader.h |  93 +++++++
 5 files changed, 526 insertions(+), 19 deletions(-)
 create mode 100644 sound/soc/codecs/tas25xx_dsp_loader.c
 create mode 100644 sound/soc/codecs/tas25xx_dsp_loader.h
sound/soc/codecs/Makefile +1 -1
index 47ae3cebb61e..c7a4e567b623 100644
@@ -298,7 +298,7 @@ snd-soc-max98504-objs := max98504.o
 snd-soc-simple-amplifier-objs := simple-amplifier.o
 snd-soc-tpa6130a2-objs := tpa6130a2.o
 snd-soc-tas2552-objs := tas2552.o
-snd-soc-tas2562-objs := tas2562.o
+snd-soc-tas2562-objs := tas2562.o tas25xx_dsp_loader.o
 
 obj-$(CONFIG_SND_SOC_88PM860X)	+= snd-soc-88pm860x.o
 obj-$(CONFIG_SND_SOC_AB8500_CODEC)	+= snd-soc-ab8500-codec.o
sound/soc/codecs/tas2562.c +30 -18

102 lines not on this page · raw message

sound/soc/codecs/tas2562.h +25 -0
index 28e75fc431d0..66c15b05cd35 100644
@@ -11,6 +11,7 @@
 #define __TAS2562_H__
 
 #define TAS2562_PAGE_CTRL	0x00
+#define TAS2562_BOOK_CTRL	0x7f
 
 #define TAS2562_REG(page, reg)	((page * 128) + reg)
 
@@ -40,6 +41,8 @@
 #define TAS2562_DVC_CFG3	TAS2562_REG(2, 0x0e)
 #define TAS2562_DVC_CFG4	TAS2562_REG(2, 0x0f)
 
+#define TAS25XX_DSP_MODE	TAS2562_REG(1, 2)
+
 #define TAS2562_RESET	BIT(0)
 
 #define TAS2562_MODE_MASK	GENMASK(1,0)
@@ -84,4 +87,26 @@
 #define TAS2562_TDM_CFG6_ISNS_EN	BIT(6)
 #define TAS2562_TDM_CFG6_ISNS_SLOT_MASK	GENMASK(5, 0)
 
+#define TAS2563_FW_HDR_OFFSET		134
+
+struct tas2562_data {
+	struct snd_soc_component *component;
+	struct gpio_desc *sdz_gpio;
+	struct regmap *regmap;
+	struct device *dev;
+	struct i2c_client *client;
+	struct tas25xx_fw_data *fw_data;
+	const char *firmware_name;
+	int load_firmware;
+	int model_id;
+	int v_sense_slot;
+	int i_sense_slot;
+	int volume_lvl;
+};
+
+enum tas2562_id {
+	TAS2562,
+	TAS2563,
+};
+
 #endif /* __TAS2562_H__ */
sound/soc/codecs/tas25xx_dsp_loader.c new file +377 -0

377 lines not on this page · raw message

sound/soc/codecs/tas25xx_dsp_loader.h new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.26.2
maintainer
#14 lore raw

Re: [RFC PATCH 2/2] ASoc: tas2563: DSP Firmware loading support

sound/soc/codecs/tas2562.c static const struct regmap_config tas2562_regmap_config = { · line 615

Quoted 7 lines · Dan Murphy
sound/soc/codecs/tas2562.c +1 -1
 	.val_bits = 8,
 
-	.max_register = 5 * 128,
+	.max_register = 255 * 128,
 	.cache_type = REGCACHE_RBTREE,
 	.reg_defaults = tas2562_reg_defaults,
 	.num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),

Should some or all of the DSP memory be marked as volatile? I guess if we only write program to it then on reload after power off it should be fine to just blast everything in again and ignore the fact that some will have changed, but it might be helpful for debugging to be able to read the live values back and do something more clever for restore.

Quoted 2 lines

#define TAS2562_PAGE_CTRL 0x00
+#define TAS2562_BOOK_CTRL 0x7f

*sigh* Of course the two levels of paging register are not located anywhere near each other so we can't easily pretend they're one double width page address. :/

Quoted 5 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +5 -0
+static int tas25xx_process_fw_single(struct tas2562_data *tas2562,
+				     struct tas25xx_cmd_data *cmd_data,
+				     u8 *fw_out)
+{
+	int num_writes = cpu_to_be16(cmd_data->length);

sound/soc/codecs/tas25xx_dsp_loader.c line 38

Quoted 10 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +10 -0
+	int i;
+	int ret;
+	int offset = 4;
+	int reg_data, write_reg;
+
+	for (i = 0; i < num_writes; i++) {
+		/* Reset Page to 0 */
+		ret = regmap_write(tas2562->regmap, TAS2562_PAGE_CTRL, 0);
+		if (ret)
+			return ret;

Why?

sound/soc/codecs/tas25xx_dsp_loader.c line 49

Quoted 11 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +11 -0
+
+		cmd_data->book = fw_out[offset];
+		cmd_data->page = fw_out[offset + 1];
+		cmd_data->offset = fw_out[offset + 2];
+		reg_data = fw_out[offset + 3];
+		offset += 4;
+
+		ret = regmap_write(tas2562->regmap, TAS2562_BOOK_CTRL,
+				   cmd_data->book);
+		if (ret)
+			return ret;

This manual paging doesn't fill me with with joy especially with regard to caching and doing the books behind the back of regmap. I didn't spot anything disabling cache or anything in the code. I think you should either bypass the cache while doing this or teach regmap about the books (which may require core updates, I can't remember if the range code copes with nested levels of paging - I remember thinking about it).

sound/soc/codecs/tas25xx_dsp_loader.c line 200

Quoted 4 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +4 -0
+static ssize_t write_config_store(struct device *dev,
+				struct device_attribute *tas25xx_attr,
+				const char *buf, size_t size)
+{

This looks like it could just be an enum (it looks like there's names we could use) or just a simple numbered control? Same for all the other controls, they're just small integers so don't look hard to handle. But perhaps I'm missing something?

sound/soc/codecs/tas25xx_dsp_loader.c line 343

Quoted 6 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +6 -0
+	tas2562->fw_data->fw_hdr = devm_kzalloc(tas2562->dev, hdr_size,
+						GFP_KERNEL);
+	if (!tas2562->fw_data->fw_hdr)
+		return -ENOMEM;
+
+	memcpy(tas2562->fw_data->fw_hdr, &fw->data[0], hdr_size);

Should validate that the firmware is actually at least hdr_size big, and similarly for all the other lengths we get from the header we should check that there's actually enough data in the file. ATM we just blindly copy.

It'd also be good to double check that the number of configs and programs is within bounds.

author
#15 lore raw

Re: [RFC PATCH 2/2] ASoc: tas2563: DSP Firmware loading support

Mark

Quoted 4 lines · Mark Brown
Quoted 2 lines · Dan Murphy

.val_bits = 8,

Quoted 10 lines

sound/soc/codecs/tas2562.c static const struct regmap_config tas2562_regmap_config = { · line 615

Quoted 5 lines
sound/soc/codecs/tas2562.c +1 -1
-	.max_register = 5 * 128,
+	.max_register = 255 * 128,
  	.cache_type = REGCACHE_RBTREE,
  	.reg_defaults = tas2562_reg_defaults,
  	.num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),

Should some or all of the DSP memory be marked as volatile? I guess if we only write program to it then on reload after power off it should be fine to just blast everything in again and ignore the fact that some will have changed, but it might be helpful for debugging to be able to read the live values back and do something more clever for restore.

Well the only values I see that change that regmap should care about are in first page of the register map.

After reverse engineering a binary I found that its contents modify page 0 registers of the device.

Not a fan of this as it causes un-wanted changes that may have been setup.

Quoted 6 lines
Quoted 3 lines

#define TAS2562_PAGE_CTRL 0x00
+#define TAS2562_BOOK_CTRL 0x7f

*sigh* Of course the two levels of paging register are not located anywhere near each other so we can't easily pretend they're one double width page address. :/

Yes I agree

Quoted 17 lines
Quoted 6 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +5 -0
+static int tas25xx_process_fw_single(struct tas2562_data *tas2562,
+				     struct tas25xx_cmd_data *cmd_data,
+				     u8 *fw_out)
+{
+	int num_writes = cpu_to_be16(cmd_data->length);

sound/soc/codecs/tas25xx_dsp_loader.c line 38

Quoted 10 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +10 -0
+	int i;
+	int ret;
+	int offset = 4;
+	int reg_data, write_reg;
+
+	for (i = 0; i < num_writes; i++) {
+		/* Reset Page to 0 */
+		ret = regmap_write(tas2562->regmap, TAS2562_PAGE_CTRL, 0);
+		if (ret)
+			return ret;

Why?

Well the reason to set this back to page 0 is that is where the book register is.

So setting this back to page 0 set the Book register appropriately.

Quoted 10 lines
Quoted 10 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +9 -0
+
+		cmd_data->book = fw_out[offset];
+		cmd_data->page = fw_out[offset + 1];
+		cmd_data->offset = fw_out[offset + 2];
+		reg_data = fw_out[offset + 3];
+		offset += 4;
+
+		ret = regmap_write(tas2562->regmap, TAS2562_BOOK_CTRL,
+				   cmd_data->book);
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+		if (ret)
+			return ret;

This manual paging doesn't fill me with with joy especially with regard to caching and doing the books behind the back of regmap. I didn't spot anything disabling cache or anything in the code. I think you should either bypass the cache while doing this or teach regmap about the books (which may require core updates, I can't remember if the range code copes with nested levels of paging - I remember thinking about it).

Yeah. After reading this and thinking about this for a couple days.  This actually has contention issues with the ALSA controls.

There needs to also be some locks put into place.

I prefer to disable the cache.  Not sure how many other devices use Books and pages for register maps besides TI.

Adding that to regmap might be to specific to our devices.

Quoted 9 lines

sound/soc/codecs/tas25xx_dsp_loader.c line 200

Quoted 5 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +4 -0
+static ssize_t write_config_store(struct device *dev,
+				struct device_attribute *tas25xx_attr,
+				const char *buf, size_t size)
+{

This looks like it could just be an enum (it looks like there's names we could use) or just a simple numbered control? Same for all the other controls, they're just small integers so don't look hard to handle. But perhaps I'm missing something?

No you are right.  The issue with using enums is that the binary is not parsed until after codec_probe and the device is registered.

So the controls would appear later which could be a race condition for the user space.

Quoted 11 lines

sound/soc/codecs/tas25xx_dsp_loader.c line 343

Quoted 7 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +6 -0
+	tas2562->fw_data->fw_hdr = devm_kzalloc(tas2562->dev, hdr_size,
+						GFP_KERNEL);
+	if (!tas2562->fw_data->fw_hdr)
+		return -ENOMEM;
+
+	memcpy(tas2562->fw_data->fw_hdr, &fw->data[0], hdr_size);

Should validate that the firmware is actually at least hdr_size big, and similarly for all the other lengths we get from the header we should check that there's actually enough data in the file. ATM we just blindly copy.

I will have to look into doing this.  I blindly copy this data because there is really not a viable and reliable way to check sizes against the structures.

Quoted 2 lines

It'd also be good to double check that the number of configs and programs is within bounds.

This I can check once the data is copied.

Dan

maintainer
#16 lore raw

Re: [RFC PATCH 2/2] ASoc: tas2563: DSP Firmware loading support

Quoted 10 lines · Dan Murphy
Quoted 8 lines · Mark Brown

sound/soc/codecs/tas25xx_dsp_loader.c line 38

Quoted 4 lines · Dan Murphy
sound/soc/codecs/tas25xx_dsp_loader.c new file +4 -0
+		/* Reset Page to 0 */
+		ret = regmap_write(tas2562->regmap, TAS2562_PAGE_CTRL, 0);
+		if (ret)
+			return ret;

Why?

Quoted 4 lines

Well the reason to set this back to page 0 is that is where the book register is.

So setting this back to page 0 set the Book register appropriately.

Oh dear, usually the paging register is always visible :/

Quoted 11 lines
Quoted 6 lines

This manual paging doesn't fill me with with joy especially with regard to caching and doing the books behind the back of regmap. I didn't spot anything disabling cache or anything in the code. I think you should either bypass the cache while doing this or teach regmap about the books (which may require core updates, I can't remember if the range code copes with nested levels of paging - I remember thinking about it).

Yeah. After reading this and thinking about this for a couple days.  This actually has contention issues with the ALSA controls.

There needs to also be some locks put into place.

That's not too surprising for something like this.

Quoted 4 lines

I prefer to disable the cache.  Not sure how many other devices use Books and pages for register maps besides TI.

Adding that to regmap might be to specific to our devices.

Single level pages are in there already, TI is a big user but I've seen this from other vendors too. I do remember some discussion of multi level paging before, I think with a TI part, and I *think* it already happens to work without needing to do anything but I'd need to go back and check and it's 7pm on a Friday night. IIRC if one paging register is within another paged region the code manages to recurse and sort things out, but I could be wrong. I agree that it's a bit specialist if it needs anything non-trivial and something driver local would be reasonable.

Quoted 12 lines
Quoted 11 lines

sound/soc/codecs/tas25xx_dsp_loader.c line 343

Quoted 6 lines
sound/soc/codecs/tas25xx_dsp_loader.c new file +6 -0
+	tas2562->fw_data->fw_hdr = devm_kzalloc(tas2562->dev, hdr_size,
+						GFP_KERNEL);
+	if (!tas2562->fw_data->fw_hdr)
+		return -ENOMEM;
+
+	memcpy(tas2562->fw_data->fw_hdr, &fw->data[0], hdr_size);

Should validate that the firmware is actually at least hdr_size big, and similarly for all the other lengths we get from the header we should check that there's actually enough data in the file. ATM we just blindly copy.

Quoted 3 lines

I will have to look into doing this.  I blindly copy this data because there is really not a viable and reliable way to check sizes against the structures.

Yeah, that's reasonable - I was just thinking about checking it against the size of the file to make sure it doesn't actually overflow the buffer and corrupt things or crash. Obviously sanity checking is good but there's limits on what's sensible.

maintainer
#17 lore raw
In reply to Dan Murphy
Quoted 3 lines · Dan Murphy

These programs and configurations are selectable via files under the I2C dev node. There may be a better way to select this through ALSA controls but I was unable to find a good example of this. This is why this is an RFC patchset.

I think you can just use enums for most of this - what you want to do I think is parse the firmware, build templates for the controls and then add them with snd_soc_add_component_controls(). Userspace *should* cope with controls being hotplugged.

author
#18 lore raw

Mark

Quoted 9 lines · Mark Brown
Quoted 3 lines · Dan Murphy

These programs and configurations are selectable via files under the I2C dev node. There may be a better way to select this through ALSA controls but I was unable to find a good example of this. This is why this is an RFC patchset.

I think you can just use enums for most of this - what you want to do I think is parse the firmware, build templates for the controls and then add them with snd_soc_add_component_controls(). Userspace *should* cope with controls being hotplugged.

Yes this was my concern if userspace could cope with dynamic controls.

Dan

maintainer
#19 lore raw
Quoted 8 lines · Dan Murphy
Quoted 4 lines · Mark Brown

I think you can just use enums for most of this - what you want to do I think is parse the firmware, build templates for the controls and then add them with snd_soc_add_component_controls(). Userspace *should* cope with controls being hotplugged.

Yes this was my concern if userspace could cope with dynamic controls.

Things like alsactl definitely do, and obviously anything that starts after the firmware loads will be fine too.