Skip to contentSkip to conversation

ASoC: Introduce the new caching API

PATCH Applied Dimitris Papastamos 3 participants 7 messages Nov 11, 2010

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover ASoC: Introduce the new caching API

This patch series introduces the new caching API. The idea behind this caching interface is that we can provide different means of organizing and accessing the register cache. This is useful for large and sparse register maps, where one can use some kind of compression algorithm to reduce the memory footprint. The caching API is designed in such way to eliminate the need for modifying any existing drivers.

TODO:
- Memory usage statistics, to make it easier to select the proper caching technique.
- Support for bulk reads/writes.
- Overridable caching behaviour from the machine driver.
- Optimize certain CPU intensive paths.
- Ensure proper locking in init()/exit()/sync().

Dimitris Papastamos (3):
ASoC: soc-cache: Add support for flat register caching
ASoC: soc-cache: Add support for LZO register caching
ASoC: soc-cache: Add support for rbtree based register caching

 include/sound/soc.h   |   29 ++
 sound/soc/Kconfig     |    2 +
 sound/soc/soc-cache.c |  984 +++++++++++++++++++++++++++++++++++++++++++++++--
 sound/soc/soc-core.c  |   37 +--
 4 files changed, 998 insertions(+), 54 deletions(-)
Signature · 1 line
1.7.3.2
#1 lore raw

PATCH v2 1/3 ASoC: soc-cache: Add support for flat register caching

This patch introduces the new caching API and migrates the old caching interface into the new one. The flat register caching technique does not use compression at all and it is equivalent to the old caching technique. One can still access codec->reg_cache directly but this is not advised as that will not be portable across different caching strategies.

None of the existing drivers need to be changed to adapt to this caching technique. There should be no noticeable overhead associated with using the new caching API.

---
 include/sound/soc.h   |   27 ++++
 sound/soc/soc-cache.c |  339 ++++++++++++++++++++++++++++++++++++++++++++-----
 sound/soc/soc-core.c  |   37 ++----
 3 files changed, 349 insertions(+), 54 deletions(-)
include/sound/soc.h +27 -0
index 5d39547..275e411 100644
@@ -238,6 +238,7 @@ struct soc_enum;
 struct snd_soc_ac97_ops;
 struct snd_soc_jack;
 struct snd_soc_jack_pin;
+struct snd_soc_cache_ops;
 #include <sound/soc-dapm.h>
 
 #ifdef CONFIG_GPIOLIB
@@ -254,6 +255,10 @@ enum snd_soc_control_type {
 	SND_SOC_SPI,
 };
 
+enum snd_soc_compress_type {
+	SND_SOC_NO_COMPRESSION
+};
+
 int snd_soc_register_platform(struct device *dev,
 		struct snd_soc_platform_driver *platform_drv);
 void snd_soc_unregister_platform(struct device *dev);
@@ -265,6 +270,13 @@ int snd_soc_codec_volatile_register(struct snd_soc_codec *codec, int reg);
 int snd_soc_codec_set_cache_io(struct snd_soc_codec *codec,
 			       int addr_bits, int data_bits,
 			       enum snd_soc_control_type control);
+int snd_soc_cache_sync(struct snd_soc_codec *codec);
+int snd_soc_cache_init(struct snd_soc_codec *codec);
+int snd_soc_cache_exit(struct snd_soc_codec *codec);
+int snd_soc_cache_write(struct snd_soc_codec *codec,
+			unsigned int reg, unsigned int value);
+int snd_soc_cache_read(struct snd_soc_codec *codec,
+		       unsigned int reg, unsigned int *value);
 
 /* Utility functions to get clock rates from various things */
 int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots);
@@ -421,6 +433,18 @@ struct snd_soc_ops {
 	int (*trigger)(struct snd_pcm_substream *, int);
 };
 
+/* SoC cache ops */
+struct snd_soc_cache_ops {
+	enum snd_soc_compress_type id;
+	int (*init)(struct snd_soc_codec *codec);
+	int (*exit)(struct snd_soc_codec *codec);
+	int (*read)(struct snd_soc_codec *codec, unsigned int reg,
+		unsigned int *value);
+	int (*write)(struct snd_soc_codec *codec, unsigned int reg,
+		unsigned int value);
+	int (*sync)(struct snd_soc_codec *codec);
+};
+
 /* SoC Audio Codec device */
 struct snd_soc_codec {
 	const char *name;
@@ -450,6 +474,8 @@ struct snd_soc_codec {
 	hw_write_t hw_write;
 	unsigned int (*hw_read)(struct snd_soc_codec *, unsigned int);
 	void *reg_cache;
+	const struct snd_soc_cache_ops *cache_ops;
+	struct mutex cache_rw_mutex;
 
 	/* dapm */
 	struct snd_soc_dapm_context dapm;
@@ -482,6 +508,7 @@ struct snd_soc_codec_driver {
 	short reg_cache_step;
 	short reg_word_size;
 	const void *reg_cache_default;
+	enum snd_soc_compress_type compress_type;
 
 	/* codec bias level */
 	int (*set_bias_level)(struct snd_soc_codec *,
sound/soc/soc-cache.c +308 -31

485 lines not on this page · raw message

sound/soc/soc-core.c +14 -23
index c18ce1d..f9f5f07 100644
@@ -3274,29 +3274,21 @@ int snd_soc_register_codec(struct device *dev,
 	codec->dapm.bias_level = SND_SOC_BIAS_OFF;
 	codec->dapm.dev = dev;
 	codec->dapm.codec = codec;
+	codec->dev = dev;
+	codec->driver = codec_drv;
+	codec->num_dai = num_dai;
+	mutex_init(&codec->mutex);
 
 	/* allocate CODEC register cache */
 	if (codec_drv->reg_cache_size && codec_drv->reg_word_size) {
-
-		if (codec_drv->reg_cache_default)
-			codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
-				codec_drv->reg_cache_size * codec_drv->reg_word_size, GFP_KERNEL);
-		else
-			codec->reg_cache = kzalloc(codec_drv->reg_cache_size *
-				codec_drv->reg_word_size, GFP_KERNEL);
-
-		if (codec->reg_cache == NULL) {
-			kfree(codec->name);
-			kfree(codec);
-			return -ENOMEM;
+		ret = snd_soc_cache_init(codec);
+		if (ret < 0) {
+			dev_err(codec->dev, "Failed to set cache compression type: %d\n",
+				ret);
+			goto error_cache;
 		}
 	}
 
-	codec->dev = dev;
-	codec->driver = codec_drv;
-	codec->num_dai = num_dai;
-	mutex_init(&codec->mutex);
-
 	for (i = 0; i < num_dai; i++) {
 		fixup_codec_formats(&dai_drv[i].playback);
 		fixup_codec_formats(&dai_drv[i].capture);
@@ -3306,7 +3298,7 @@ int snd_soc_register_codec(struct device *dev,
 	if (num_dai) {
 		ret = snd_soc_register_dais(dev, dai_drv, num_dai);
 		if (ret < 0)
-			goto error;
+			goto error_dais;
 	}
 
 	mutex_lock(&client_mutex);
@@ -3317,9 +3309,9 @@ int snd_soc_register_codec(struct device *dev,
 	pr_debug("Registered codec '%s'\n", codec->name);
 	return 0;
 
-error:
-	if (codec->reg_cache)
-		kfree(codec->reg_cache);
+error_dais:
+	snd_soc_cache_exit(codec);
+error_cache:
 	kfree(codec->name);
 	kfree(codec);
 	return ret;
@@ -3353,8 +3345,7 @@ found:
 
 	pr_debug("Unregistered codec '%s'\n", codec->name);
 
-	if (codec->reg_cache)
-		kfree(codec->reg_cache);
+	snd_soc_cache_exit(codec);
 	kfree(codec->name);
 	kfree(codec);
 }
Signature · 1 line
1.7.3.2
#2 lore raw

PATCH v2 2/3 ASoC: soc-cache: Add support for LZO register caching

In reply to Dimitris Papastamos

This patch adds support for LZO compression when storing the register cache. The initial register defaults cache is marked as __devinitconst and the only change required for a driver to use LZO compression is to set the compress_type member in codec->driver to SND_SOC_LZO_COMPRESSION.

For a typical device whose register map would normally occupy 25kB or 50kB by using the LZO compression technique, one can get down to ~5-7kB. There might be a performance penalty associated with each individual read/write due to decompressing/compressing the underlying cache, however that should not be noticeable. These memory benefits depend on whether the target architecture can get rid of the memory occupied by the original register defaults cache which is marked as __devinitconst. Nevertheless there will be some memory gain even if the target architecture can't get rid of the original register map, this should be around ~30-32kB instead of 50kB.

---
 include/sound/soc.h   |    3 +-
 sound/soc/Kconfig     |    2 +
 sound/soc/soc-cache.c |  413 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 417 insertions(+), 1 deletions(-)
include/sound/soc.h +2 -1
index 275e411..10f5932 100644
@@ -256,7 +256,8 @@ enum snd_soc_control_type {
 };
 
 enum snd_soc_compress_type {
-	SND_SOC_NO_COMPRESSION
+	SND_SOC_NO_COMPRESSION,
+	SND_SOC_LZO_COMPRESSION
 };
 
 int snd_soc_register_platform(struct device *dev,
sound/soc/Kconfig +2 -0
index 3e598e7..4562c89 100644
@@ -4,6 +4,8 @@
 
 menuconfig SND_SOC
 	tristate "ALSA for SoC audio support"
+	select LZO_COMPRESS
+	select LZO_DECOMPRESS
 	select SND_PCM
 	select AC97_BUS if SND_SOC_AC97_BUS
 	select SND_JACK if INPUT=y || INPUT=SND
sound/soc/soc-cache.c +413 -0

431 lines not on this page · raw message

Signature · 1 line
1.7.3.2
#3 lore raw

PATCH v2 3/3 ASoC: soc-cache: Add support for rbtree based register caching

In reply to Dimitris Papastamos

This patch adds support for rbtree compression when storing the register cache. It does this by not adding any uninitialized registers (those whose value is 0). If any of those registers is written with a nonzero value they get added into the rbtree.

Consider a sample device with a large sparse register map. The register indices are between [0, 0x31ff]. An array of 12800 registers is thus created each of which is 2 bytes. This results in a 25kB region. This array normally lives outside soc-core, normally in the driver itself. The original soc-core code would kmemdup this region resulting in 50kB total memory. When using the rbtree compression technique and __devinitconst on the original array the figures are as follows. For this typical device, you might have 100 initialized registers, that is registers that are nonzero by default. We build an rbtree with 100 nodes, each of which is 24 bytes. This results in ~2kB of memory. Assuming that the target arch can freeup the memory used by the initial __devinitconst array, we end up using about ~2kB bytes of actual memory. The memory footprint will increase as uninitialized registers get written and thus new nodes created in the rbtree. In practice, most of those registers are never changed. If the target arch can't freeup the __devinitconst array, we end up using a total of ~27kB. The difference between the rbtree and the LZO caching techniques, is that if using the LZO technique the size of the cache will increase slower as more uninitialized registers get changed.

---
 include/sound/soc.h   |    3 +-
 sound/soc/soc-cache.c |  232 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 234 insertions(+), 1 deletions(-)
include/sound/soc.h +2 -1
index 10f5932..af23f42 100644
@@ -257,7 +257,8 @@ enum snd_soc_control_type {
 
 enum snd_soc_compress_type {
 	SND_SOC_NO_COMPRESSION,
-	SND_SOC_LZO_COMPRESSION
+	SND_SOC_LZO_COMPRESSION,
+	SND_SOC_RBTREE_COMPRESSION
 };
 
 int snd_soc_register_platform(struct device *dev,
sound/soc/soc-cache.c +232 -0

250 lines not on this page · raw message

Signature · 1 line
1.7.3.2
Acked-by on the v2 series
#4 lore raw
In reply to Dimitris Papastamos
Quoted 15 lines · Dimitris Papastamos

This patch series introduces the new caching API. The idea behind this caching interface is that we can provide different means of organizing and accessing the register cache. This is useful for large and sparse register maps, where one can use some kind of compression algorithm to reduce the memory footprint. The caching API is designed in such way to eliminate the need for modifying any existing drivers.

TODO:
- Memory usage statistics, to make it easier to select the proper caching technique.
- Support for bulk reads/writes.
- Overridable caching behaviour from the machine driver.
- Optimize certain CPU intensive paths.
- Ensure proper locking in init()/exit()/sync().

Quoted 5 lines

Dimitris Papastamos (3):
ASoC: soc-cache: Add support for flat register caching
ASoC: soc-cache: Add support for LZO register caching
ASoC: soc-cache: Add support for rbtree based register caching

Signature · 3 lines
Freelance Developer, SlimLogic Ltd
ASoC and Voltage Regulator Maintainer.
http://www.slimlogic.co.uk
Applied
#5 lore raw
In reply to Dimitris Papastamos
Quoted 6 lines · Dimitris Papastamos

This patch series introduces the new caching API. The idea behind this caching interface is that we can provide different means of organizing and accessing the register cache. This is useful for large and sparse register maps, where one can use some kind of compression algorithm to reduce the memory footprint. The caching API is designed in such way to eliminate the need for modifying any existing drivers.

Applied, thanks.

Quoted 5 lines

TODO:
- Memory usage statistics, to make it easier to select the proper caching technique.
- Support for bulk reads/writes.
- Overridable caching behaviour from the machine driver.

This one in particular would be very good.

#6 lore raw
Quoted 10 lines · Mark Brown
Quoted 6 lines · Dimitris Papastamos

This patch series introduces the new caching API. The idea behind this caching interface is that we can provide different means of organizing and accessing the register cache. This is useful for large and sparse register maps, where one can use some kind of compression algorithm to reduce the memory footprint. The caching API is designed in such way to eliminate the need for modifying any existing drivers.

Applied, thanks.

Quoted 7 lines
Quoted 5 lines

TODO:
- Memory usage statistics, to make it easier to select the proper caching technique.
- Support for bulk reads/writes.
- Overridable caching behaviour from the machine driver.

This one in particular would be very good.

Yes, I have an idea on how to do that. I will send you a patch at some point.

Thanks,
Dimitrios